小智 66
试试这个:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
Run Code Online (Sandbox Code Playgroud)
每当你打开.h文件时,都会使用C++模式.
phi*_*ils 33
另一种同时使用c-mode和c ++模式的方法是使用目录局部变量来设置模式.
在模式设置为1之后评估目录变量,因此您实际上可以.dir-locals.el为包含以下内容的C++项目编写一个文件:
((c-mode . ((mode . c++))))
Run Code Online (Sandbox Code Playgroud)
并且Emacs会将模式更改为c++-mode最初设置的模式c-mode.
如果您使用混合的C和C++项目,这将为每个项目提供一个非常简单的解决方案.
当然,如果您的大多数项目都是C++,您可以将c ++ - mode设置为默认值2,然后您可以反过来使用此方法在适当的时候切换到c模式.
1 normal-mode调用(set-auto-mode)和(hack-local-variables)按顺序.另请参阅:如何在主模式挂钩中访问目录本地变量?
2为此,请添加
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
Run Code Online (Sandbox Code Playgroud)
到默认情况下以C++模式.emacs打开.h文件的文件.
Kei*_*thB 21
如果您不希望将其应用于每个.h文件,则可以将以下内容添加到C++头文件的底部.
// Local Variables:
// mode: c++
// End:
Run Code Online (Sandbox Code Playgroud)
这适用于您要基于每个文件设置的任何Emacs变量.Emacs忽略前导字符,因此请使用适合文件类型的任何注释字符.
Pro*_*ica 19
显然你也可以把它放在文件的顶部:
// -*-c++-*-
Run Code Online (Sandbox Code Playgroud)
告诉Emacs它是一个C++文件.
我使用它,因为我经常最终使用香草Emacs并且无需以任何方式配置Emacs.
Bor*_*bus 17
由于我经常使用C和C++,我编写了这个函数来尝试"猜测".h文件是C还是C++
;; function decides whether .h file is C or C++ header, sets C++ by
;; default because there's more chance of there being a .h without a
;; .cc than a .h without a .c (ie. for C++ template files)
(defun c-c++-header ()
"sets either c-mode or c++-mode, whichever is appropriate for
header"
(interactive)
(let ((c-file (concat (substring (buffer-file-name) 0 -1) "c")))
(if (file-exists-p c-file)
(c-mode)
(c++-mode))))
(add-to-list 'auto-mode-alist '("\\.h\\'" . c-c++-header))
Run Code Online (Sandbox Code Playgroud)
如果这不起作用,我设置一个键来切换C和C++模式
;; and if that doesn't work, a function to toggle between c-mode and
;; c++-mode
(defun c-c++-toggle ()
"toggles between c-mode and c++-mode"
(interactive)
(cond ((string= major-mode "c-mode")
(c++-mode))
((string= major-mode "c++-mode")
(c-mode))))
Run Code Online (Sandbox Code Playgroud)
它并不完美,可能有更好的启发式方法来判断标题是C还是C++,但它对我有用.
| 归档时间: |
|
| 查看次数: |
19089 次 |
| 最近记录: |