如何设置clang-format(编码时自动格式化代码而不做任何事情)?
How to set up clang-format (automatically format code when coding without doing anything)?
我正在尝试安装 clang-format 自动格式化工具,我已经用 M-x package-install clang-format
安装了 clang-format
我可以在 M-x list-packages
.
中看到它
我的 ~/.emacs 是:
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
(when no-ssl
(warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
(add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)
;; clang-format
(require 'clang-format)
(global-set-key [C-M-tab] 'clang-format-region)
(add-hook 'c-mode-common-hook
(function (lambda ()
(add-hook 'write-contents-functions
(lambda() (progn (clang-format-buffer) nil))))))
(add-hook 'cpp-mode-common-hook
(function (lambda ()
(add-hook 'write-contents-functions
(lambda() (progn (clang-format-buffer) nil))))))
;; irony-mode
(add-hook 'c++-mode-hook 'irony-mode)
(add-hook 'c-mode-hook 'irony-mode)
(add-hook 'objc-mode-hook 'irony-mode)
(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
;; company mode
(add-hook 'c++-mode-hook 'company-mode)
(add-hook 'c-mode-hook 'company-mode)
;; flycheck-mode
(add-hook 'c++-mode-hook 'flycheck-mode)
(add-hook 'c-mode-hook 'flycheck-mode)
(eval-after-load 'flycheck
'(add-hook 'flycheck-mode-hook #'flycheck-irony-setup))
;; eldoc-mode
(add-hook 'irony-mode-hook 'irony-eldoc)
(global-linum-mode)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages
(quote
(list-packages-ext clang-format flycheck-clang-tidy flycheck-clang-analyzer elpy irony-eldoc company-irony flycheck-irony flycheck irony))))
(custom-set-faces
;; custom-set-faces was added by Custom.
'(linum ((t (:inherit (shadow default) :foreground "yellow")))))
但 clang-format 不会出现在 emacs 的底部 window 而 Irony、FlyC、ElDoc 和 Abbrev 会出现。
有人能帮帮我吗?谢谢!
来自clang-format github 说明:
(require 'clang-format)
(global-set-key (kbd "C-c i") 'clang-format-region)
(global-set-key (kbd "C-c u") 'clang-format-buffer)
(setq clang-format-style-option "llvm")
它不会出现在状态行中,因为它不是次要模式。
您必须将键绑定设置为您要使用的功能,就像在给定的示例中一样。
我正在尝试安装 clang-format 自动格式化工具,我已经用 M-x package-install clang-format
安装了 clang-format
我可以在 M-x list-packages
.
我的 ~/.emacs 是:
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
(when no-ssl
(warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
(add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)
;; clang-format
(require 'clang-format)
(global-set-key [C-M-tab] 'clang-format-region)
(add-hook 'c-mode-common-hook
(function (lambda ()
(add-hook 'write-contents-functions
(lambda() (progn (clang-format-buffer) nil))))))
(add-hook 'cpp-mode-common-hook
(function (lambda ()
(add-hook 'write-contents-functions
(lambda() (progn (clang-format-buffer) nil))))))
;; irony-mode
(add-hook 'c++-mode-hook 'irony-mode)
(add-hook 'c-mode-hook 'irony-mode)
(add-hook 'objc-mode-hook 'irony-mode)
(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
;; company mode
(add-hook 'c++-mode-hook 'company-mode)
(add-hook 'c-mode-hook 'company-mode)
;; flycheck-mode
(add-hook 'c++-mode-hook 'flycheck-mode)
(add-hook 'c-mode-hook 'flycheck-mode)
(eval-after-load 'flycheck
'(add-hook 'flycheck-mode-hook #'flycheck-irony-setup))
;; eldoc-mode
(add-hook 'irony-mode-hook 'irony-eldoc)
(global-linum-mode)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages
(quote
(list-packages-ext clang-format flycheck-clang-tidy flycheck-clang-analyzer elpy irony-eldoc company-irony flycheck-irony flycheck irony))))
(custom-set-faces
;; custom-set-faces was added by Custom.
'(linum ((t (:inherit (shadow default) :foreground "yellow")))))
但 clang-format 不会出现在 emacs 的底部 window 而 Irony、FlyC、ElDoc 和 Abbrev 会出现。
有人能帮帮我吗?谢谢!
来自clang-format github 说明:
(require 'clang-format)
(global-set-key (kbd "C-c i") 'clang-format-region)
(global-set-key (kbd "C-c u") 'clang-format-buffer)
(setq clang-format-style-option "llvm")
它不会出现在状态行中,因为它不是次要模式。 您必须将键绑定设置为您要使用的功能,就像在给定的示例中一样。