首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置clang-format (编码时自动格式化代码,不做任何事情)?

如何设置clang-format (编码时自动格式化代码,不做任何事情)?
EN

Stack Overflow用户
提问于 2019-02-14 10:46:30
回答 1查看 1.1K关注 0票数 1

我正在尝试安装clang-format自动格式化工具,我已经用M-x package-install clang-format安装了clang-format,我可以在M-x list-packages中看到它。

我的~/.emacs是:

代码语言:javascript
复制
(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")))))

但是在emacs窗口的底部没有出现clang-format,而出现了Irony,FlyC,ElDoc和Abbrev。

有人能帮帮我吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-14 16:41:44

clang-format github指令中:

代码语言:javascript
复制
(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")

它不会出现在状态行中,因为它不是次要模式。您必须为您想要使用的函数设置键绑定,就像在给定的示例中一样。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54682490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档