我开始使用目标C,而目标C模式工作得非常好.但是,ObjC使用.h文件,就像C和C++一样.我需要能够使用这三个.有谁知道如何让emacs告诉它是否应该处于objC模式或C/C++模式?
编辑:我要求的东西似乎有些混乱.问题是我有一些与.m文件相关联的.h文件,以及一些与.c文件相关联的.h文件和一些与.cpp文件相关联的文件.我想要的是我可以坚持使用我的c-mode-common-hook或者某个地方,它会检查它是否是一个客观的C .h文件,然后将其强制转换为客观的C模式,或者某种东西.我的想法是,我可以打开一个.h文件,它将自动处于正确的模式.
显然,当我在文件中时,我可以手动更改模式,但这很痛苦,而且我经常忘记,直到我点击标签并发生一些古怪的事情.这是我现在使用的解决方案.
我认为标题注释可能是正确的方法,但为了使它真正有用,我需要弄清楚当它为我创建文件时如何让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")))))
Run Code Online (Sandbox Code Playgroud)
leg*_*cia 15
您可以在文件的第一行添加这样的注释:
/* -*- mode: objc -*- */
Run Code Online (Sandbox Code Playgroud)
要么
// -*- mode: c++ -*-
Run Code Online (Sandbox Code Playgroud)
作为适当的.有关在Emacs手册中指定文件变量的更多详细信息.
好吧,一个不需要在文件中添加特殊注释的解决方案怎么样?
看一下这个:
;; 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)
Run Code Online (Sandbox Code Playgroud)
这使用Emacs的内置包来查找相应的.h/.cc文件.因此,如果刚刚打开的头文件对应于c ++模式的文件,则.h文件将进入该模式,如果源是objc模式文件,则头文件将进入该模式.
在评论中没有特殊的Emacs变量.