#author("2020-07-20T13:34:30+00:00","","") #topicpath /////////////////////////////////////////////////////////////////////////////// * 目次 [#eeefac0c] #contents(); //////////////////////////////////////////////////////////////////////////////// * clang-format [#o04f0607] //============================================================================== ** 環境構築 [#qeac0163] + パッケージをインストールする apt-get install clang-tools-7 clang-format + clang-tools のパスを通す export PATH=/usr/lib/llvm-7/bin:${PATH} + emacs 向けパッケージの設定 -- package-install を使う場合 M-x package-install lsp-mode lsp-ui company-lsp dash-el elpa-f s-el elpa-markdown-mode dash-el --- これとは別途、パッケージが存在しない ht.el を、下記と同様に取得する。 -- 個別に lisp を取ってくる場合 +++ 以下のリポジトリを取得する: |~package name |~package repository URI |~remark | |lsp-mode |http://github.com/emacs-lsp/lsp-mode.git |各種LSPの言語モードを実装している。 C/C++ の設定もここに含まれる。 | |lsp-ui |https://github.com/emacs-lsp/lsp-ui.git |補完を担うcompany-modeのLSP向け設定 | |company-lsp |https://github.com/tigersoldier/company-lsp.git |ドキュメントの表示など各種アクションを実装している。 | |elpa |https://git.savannah.gnu.org/git/emacs/elpa.git |elpa/packages/cl-generic 等を使用する為に取得する。サイズが大きいので注意。 | |ht |https://github.com/Wilfred/ht.el.git |debian9のパッケージには存在しない。lsp-mode の動作に必要 | |dash |https://github.com/magnars/dash.el | | |f |https://github.com/rejeep/f.el | | |s |https://github.com/magnars/s.el | | |markdown-mode |https://github.com/jrblevin/markdown-mode.git | | |clang-format |%% https://github.com/sonatard/clang-format.git %% |このリポジトリのものではなく、clang-format-<version> package に含まれる物を使うこと | |flycheck-mode |https://github.com/flycheck/flycheck | | |projectile |https://github.com/bbatsov/projectile | | ~ +++ 上記各リポジトリを tag で checkout する |~package name |~tag |~remark | |lsp-mode |refs/tags/7.0 | | |lsp-ui |refs/tags/7.0 | | |company-lsp |refs/tags/2.1.0 | | |elpa |(HEAD) |tagは試行中。動かなかったら検討する。 | ~ +++ 上記各リポジトリのファイルが emacs から見える場所に配置する。~ ${HOME}/.emacs.d/init.el への記述例 ;;; ~/.emacs/init.el ;;; ~/.emacs.d/site-lisp 配下に各リポジトリを配置した場合の設定例 (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/dash") load-path)) ; dash (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/elpa/packages/cl-generic") load-path)) ; cl-generic (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/ht") load-path)) ; ht.el (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/elpa/packages/hydra") load-path)) ; lv.el (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/elpa/packages/spinner") load-path)) ; spinner.el (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/elpa/packages/company") load-path)) ; company.el (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/markdown-mode") load-path)) ; markdown-mode (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/flycheck") load-path)) ; flycheck (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/projectile") load-path)) ; projectile (require 'projectile) (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/lsp-mode") load-path)) (require 'lsp-mode) (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/lsp-ui") load-path)) (require 'lsp-ui) (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/company-lsp") load-path)) (require 'company-lsp) (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/clang-format") load-path)) (require 'clang-format) ~ + 設定を記載する ;;; .clang-firmat を読み込ませる関数を定義 ;; https://eklitzke.org/smarter-emacs-clang-format (defun clang-format-buffer-smart () "Reformat buffer if .clang-format exists in the projectile root." (interactive) (when (f-exists? (expand-file-name ".clang-format" (projectile-project-root))) (clang-format-buffer))) ;; https://eklitzke.org/smarter-emacs-clang-format ;(defun clang-format-buffer-smart-on-save () ; "Add auto-save hook for clang-format-buffer-smart." ; (add-hook 'before-save-hook 'clang-format-buffer-smart nil t)) ;;; hooks (add-hook 'c++-mode-hook 'company-mode) ; 補完用 (add-hook 'c-mode-hook 'company-mode) ; 補完用 (add-hook 'c++-mode-hook 'flycheck-mode) ; チェック用 (add-hook 'c-mode-hook 'flycheck-mode) ; チェック用 (add-hook 'c++-mode-hook #'lsp) (add-hook 'c-mode-hook #'lsp) ;;; key bindings ;;; 以下は clang-format の README.md の記載より転記した。 (global-set-key (kbd "C-c i") 'clang-format-region) (global-set-key (kbd "C-c u") 'clang-format-buffer-smart) ; (global-set-key (kbd "C-c i") 'clang-format-region) ; (global-set-key (kbd "C-c u") 'clang-format-buffer) ; (global-unset-key "\C-\M-q") ; (global-set-key (kbd "C-M q") 'clang-format-region) ~ -- clangd を呼び出せるようにする --- clangd は、PATH が通ってない(または、PATH が通っている場所に、バージョン番号を含むファイル名で配置されている)場合がある。 debian9 の場合は、以下にある: $ locate clangd /usr/bin/clangd-7 # 下記の SymLink /usr/lib/llvm-7/bin/clangd この場合、以下のように設定する: ;;; lsp-mode ;; https://qiita.com/false-git@github/items/3cb6ebd7f4d6f77acc54 (setq lsp-clients-clangd-executable "clangd-7") ; または ; (setq lsp-clients-clangd-executable "/usr/bin/clangd-7") //============================================================================== ** フォーマットルールを設定する [#fbad4231] //------------------------------------------------------------------------------ *** 用意されたフォーマットルールを利用する [#t4ca6625] ;;; ~/.emacs/init.el ;;; format-style (setq clang-format-style-option "llvm") ; llvm スタイルのフォーマットルールを利用する場合 //------------------------------------------------------------------------------ *** 個別に設定を行う [#xe3b81ee] - .clang-format にルールを記載すると、それが読み込まれて適用される。 -- .clang-format は再帰的に上位ディレクトリを探しに行くようになっているので、どんな場合でも適用したい書式が決まっている場合は、 ${HOME} 直下に .clang-format を配置すると良い。 - .clang-format は、規定の書式から dump して作成することが出来る。 -- google スタイルのフォーマットルールを .clang-format に出力する例: $ clang-format -style=google -dump-config > .clang-format -- 出力された .clang-format に手を加えて、独自のフォーマットルール設定を作ることが出来る。 //============================================================================== ** 参考 URI [#sb5f0ebe] - [[EmacsでC/C++用にclangdとclang-formatを使う - Qiita>https://qiita.com/kari_tech/items/4754fac39504dccfd7be]] - [[EmacsのC/C++開発環境を整える [lsp-mode, ccls] | | meda-log>https://blog.medalotte.net/archives/473]] - [[clang-format を導入してカスタマイズした :: プログラマになりたい人生 — プログラマになりたいおじさんの日記>https://blog.takuchalle.dev/post/2018/04/26/setup_clang_format/]] - [[ClangFormat — Clang 11 documentation>https://clang.llvm.org/docs/ClangFormat.html]] - [[GNU Emacs Lisp Package Archive>https://elpa.gnu.org/]] - f.el -- [[モダンなファイル、ディレクトリを扱うAPIのf-elのインストールと設定>https://emacs.tsutomuonoda.com/f-el/]] -- [[Emacsのモダンなライブラリ4+1選 (2013年版)>https://qiita.com/pogin503/items/01005402dc7cfd375b55]] - ht.el -- [[Emacsのハッシュテーブルライブラリht-elのインストールと設定>https://emacs.tsutomuonoda.com/ht-el/]] -- [[Emacsのモダンなライブラリ4+1選 (2013年版)>https://qiita.com/pogin503/items/01005402dc7cfd375b55]] -- [[ht.el : 【Rubyist歓喜】ハッシュテーブルを超絶使いやすくするライブラリ>http://emacs.rubikitch.com/ht/]] - markdown-mode -- [[markdown-modeで“GitHub Flavored Markdown”を実現する>https://qiita.com/ybiquitous/items/34a4c3cb33f26136f670]] - [[Smarter clang-format In Emacs>https://eklitzke.org/smarter-emacs-clang-format]] --- .clang-firmat を読み込ませるための設定 //////////////////////////////////////////////////////////////////////////////// * clang-format-plus [#e58e4cc6] - clang-format の機能拡張版 - clang-format に依存する |~package name |~package repository URI |~remark | |clang-format+ |https://github.com/SavchenkoValeriy/emacs-clang-format-plus | |