LoginSignup
3
3

More than 3 years have passed since last update.

emacsにモダンなエディタに負けない補完機能を...(2)

Last updated at Posted at 2019-09-15

どうも”おもり”です。
前回に引き続き、emacsの補完機能について書いて行きます。

company

コーディング中の補完についてはcompanyを使用します。

私はMSYS2での開発構築をしているため、下記のコマンドを使用してcmakeをインストールしましょう。
MSYS2でのemacsの導入については私の以前の記事が参考になるかもしれません。

MSYS2でemacs生活開始
https://qiita.com/RIO18020/items/2ef7a4973384463d554e

pasman -S mingw-w64-x86_64-cmake
pacman -S mingw-w64-i686-cmake

clanのインストール

pasman -S mingw-w64-x86_64-clang
pacman -S mingw-w64-i686-clang

ironyのインストール

M-X packge-install [RET] irony, M-X packge-install [RET] company-ironyによってironyをインストールしましょう。
ironyとはC/C++でコーディングするときに補完などを行うときに必要になります。インストール後にinit.elに下記を追加する。

(use-package irony
  :defer t
  :commands irony-mode
  :init
  (add-hook 'c-mode-hook 'irony-mode)
  (add-hook 'c++-mode-hook 'irony-mode)
  :config
  ;; C言語用にコンパイルオプションを設定する.
  (add-hook 'c-mode-hook
            '(lambda ()
               (setq irony-additional-clang-options '("-std=c11" "-Wall" "-Wextra"))))
  ;; C++言語用にコンパイルオプションを設定する.
  (add-hook 'c++-mode-hook
            '(lambda ()
               (setq irony-additional-clang-options '("-std=c++14" "-Wall" "-Wextra"))))
  (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
  ;; Windows環境でパフォーマンスを落とす要因を回避.
  (when (boundp 'w32-pipe-read-delay)
    (setq w32-pipe-read-delay 0))
  ;; バッファサイズ設定(default:4KB -> 64KB)
  (when (boundp 'w32-pipe-buffer-size)
    (setq irony-server-w32-pipe-buffer-size (* 64 1024)))
  )

(use-package company-irony
  :defer t
  :config
  ;; companyの補完のバックエンドにironyを使用する.
  (add-to-list 'company-backends '(company-irony-c-headers company-irony))
  )

その他、C++で書いていく時に入れておく&設定しておくと便利な設定を下に書いておきます。

(require 'cc-mode)
;; c-modeやc++-modeなどcc-modeベースのモード共通の設定
(add-hook
 'c-mode-common-hook
 (lambda ()
   ;; k&rスタイルをベースにする
   (c-set-style "k&r")
   ;; スペースでインデントをする
   (setq indent-tabs-mode nil)
   ;; インデント幅を2にする
   (setq c-basic-offset 2)
   ;; 自動改行(auto-new-line)と
   ;; 連続する空白の一括削除(hungry-delete)を
   ;; 有効にする
   (c-toggle-auto-hungry-state 1)
   ;; CamelCaseの語でも単語単位に分解して編集する
   ;; GtkWindow         => Gtk Window
   ;; EmacsFrameClass   => Emacs Frame Class
   ;; NSGraphicsContext => NS Graphics Context
   (subword-mode 1)
   ))

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

;; undo-tree
(require 'undo-tree)
(global-undo-tree-mode)

;; flycheck
(require 'package)
(global-flycheck-mode)

(require 'google-c-style)
(defun cc-mode-init ()
  (google-set-c-style)
  (setq indent-tabs-mode t)
  (setq c-basic-offset 4)
  (c-set-offset 'case-label 0)
)

以上の設定から実現された補完の様子を、「emacsにモダンなエディタに負けない補完機能を...(1)」で記述した同じ内容を書きながら確認してみましょう。QtCreatorとの比較になります。
https://qiita.com/RIO18020/items/57abac4ededf9b426cf1

qiita1.gif

includeの記述は補完されないのだが。。。

qiita2.gif

補完が出るのが少し遅いのと、Ctrl+Alt+iを入力しないと補完候補が出てこないのも不満である。
QtCreatorを越えることができてねー。。。

まだまだ修行が必要ですな。以下の書籍などから情報を調べながら育て上げていこうと思う。
良いものが見つかったら記事も更新して行きます。

  • Emacsに関して購入した書籍リスト
    • Emacs実践入門 ~思考を直感的にコード化し、開発を加速する
    • Emacsテクニックバイブル ~作業効率をカイゼンする200の技~
    • 入門 GNU Emacs

image.png

image.png

image.png

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3