我应该在我的 _emacs(Windows上)文件中添加哪些行来使其在C++模式下打开.h文件?默认是C模式。
试试这个:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
另一种同时使用c-mode和c++-mode的方法是,使用目录本地变量来设置模式。
目录变量在模式设置后进行评估1,所以你可以为C ++项目编写一个.dir-locals.el文件,其中包含这个:
((c-mode . ((mode . c++))))
当Emacs初始设置为c-mode
时,它将改变模式为c++-mode
。
如果你同时使用C和C++项目,这对于每个项目来说都是一个非常简单的解决方案。
当然,如果你大部分的项目都是C++,你可以将设置为默认值2,然后可以反过来使用这种方法,在适当的情况下切换到。
1normal-mode
按照顺序调用(set-auto-mode)
和(hack-local-variables)
函数。有关详情,请参见:如何在我的主模式挂钩中访问目录本地变量?
2要这样做,请添加
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
将以下代码添加到您的.emacs
文件中,以默认以C++模式打开.h
文件。
("\\.glsl\\'" . glsl-mode)
添加到列表 auto-mode-alist
中。 - phils(c++-mode .
设置的目录本地值(至少在Emacs 25中),所以你需要通过(nil .
来应用这些值。 - mzuther如果您不想将此应用于每个.h文件,则可以将以下内容添加到C++头文件的底部。
// Local Variables:
// mode: c++
// End:
对于任何你想在每个文件上设置的 Emacs 变量,这将起作用。Emacs 忽略前导字符,因此请使用适合文件类型的任何注释字符。
显然,你也可以将这个放在文件的顶部:
Apparently you can also put this at the top of the file:
// -*-c++-*-
告诉Emacs这是一个C++文件。
我经常在没有对Emacs进行任何配置的情况下使用它,因此我使用这个方法。
// -*- mode: c++ -*-
。您还可以在其中添加其他变量,例如 -*- coding: utf-8; mode: python; tab-width: 4; -*-
。 - donkopotamus由于我经常使用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))
如果这不起作用,我会设置一个键来在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))))
并非完美,也许有更好的启发式方法来判断一个标题是C还是C ++,但对我而言它起作用。
(string-match "llvm" (buffer-file-name))
这样的东西来确定.h文件是否是c++文件(llvm是一个c++项目)。 - zdav我敢肯定我已经看到这个问题得到了适当的回答了?奇怪。
您想要这个:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))