我开始使用目标C,而目标C模式完全正常。但是,ObjC使用.h文件,就像C和C ++一样。我需要能够使用这三个。有谁知道如何让emacs告诉它是否应该处于objC模式或C / C ++模式?
编辑:我要求的东西似乎有些混乱。问题是我有一些与.m文件关联的.h文件,以及与.c文件关联的一些.h文件和与.cpp文件关联的一些.h文件。我想要的东西是我可以坚持在我的c-mode-common-hook或某个地方,它将检查它是否是一个客观的C .h文件,然后强制它到目标C模式, 或者其他的东西。我的想法是,我可以打开一个.h文件,它将自动处于正确的模式。显然,当我在文件中时,我可以手动更改模式,但这很痛苦,而且我常常忘记,直到我点击tab并发生一些古怪的事情。这是我现在使用的解决方案。
我认为标题注释可能是正确的方法,但是为了使它真正有用,我需要弄清楚当它为我创建文件时如何让XCode将注释放在那里...
EDIT2:
我目前正在使用标头注释解决方案。直到我能弄清楚如何让XCode自动添加注释,我正在使用以下elisp函数:
(defun bp-add-objC-comment ()
"Adds the /* -*- mode: objc -*- */ line at the top of the file"
(interactive)
(objc-mode)
(let((p (point)))
(goto-char 0)
(insert "/* -*- mode: objc -*- */\n")
(goto-char (+ p (length "/* -*- mode: objc -*- */\n")))))
答案 0 :(得分:15)
您可以在文件的第一行添加这样的注释:
/* -*- mode: objc -*- */
或
// -*- mode: c++ -*-
酌情。有关Emacs手册中Specifying File Variables的更多详细信息。
答案 1 :(得分:5)
好的,一个不需要在文件中添加特殊注释的解决方案怎么样?
检查出来:
;; need find-file to do this
(require 'find-file)
;; find-file doesn't grok objc files for some reason, add that
(push ".m" (cadr (assoc "\\.h\\'" cc-other-file-alist)))
(defun my-find-proper-mode ()
(interactive)
;; only run on .h files
(when (string-match "\\.h\\'" (buffer-file-name))
(save-window-excursion
(save-excursion
(let* ((alist (append auto-mode-alist nil)) ;; use whatever auto-mode-alist has
(ff-ignore-include t) ;; operate on buffer name only
(src (ff-other-file-name)) ;; find the src file corresponding to .h
re mode)
;; go through the association list
;; and find the mode associated with the source file
;; that is the mode we want to use for the .h file
(while (and alist
(setq mode (cdar alist))
(setq re (caar alist))
(not (string-match re src)))
(setq alist (cdr alist)))
(when mode (funcall mode)))))))
(add-hook 'find-file-hook 'my-find-proper-mode)
这使用Emacs的内置包来查找相应的.h / .cc文件。因此,如果刚刚打开的头文件对应于c ++模式的文件,则.h文件将进入该模式,如果源是objc模式文件,则头文件将进入该模式。
所需评论中没有特殊的Emacs变量。
答案 2 :(得分:1)
我会用eproject来帮助我处理这件事。假设我没有在给定项目中混合匹配语言,我可以在项目根目录中创建一个包含文本的.eproject
文件:
:cc-header-type :objc
然后,我为.h文件设置了某种默认模式:
(add-to-list 'auto-mode-alist '("[.]h$" . c-mode))
然后,为该模式添加一个钩子:
(add-hook 'c-mode-hook (lambda ()
(let ((header-style (eproject-attribute :cc-header-type)))
(when (and header-style
(string-match "[.]h$" (buffer-file-name)))
(case header-style
(:objc (objc-mode))
(:c++ (c++-mode))
(:c (c-mode))))))
现在,模式将根据您在.eproject
文件中的设置自动更改。 (请注意,.eproject
文件不是为eproject设置变量的唯一方法;如果您有启发式检测目标c项目,则eproject也可以通过这种方式获取变量。)
无论如何,如果这对您有用,请告诉我,以便我可以将其添加到eproject wiki。