我想要做的是智能地为compile
函数的字符串参数预先设置缓冲区本地默认值。
现在compile.el默认使用“make”作为命令。我可以通过设置compile-command
来设置它。我甚至可以将变量缓冲区本地化。如果我想要相同的静态值,那就行。
但我想根据缓冲区的内容,缓冲区的名称,文件的包含目录的内容(如果有的话)以及相关的阶段,智能地选择compile-command
。月亮。基本上我想控制默认值,然后允许交互式用户覆盖该预设值。
我希望通过以前的建议来做到这一点。但这并不像我预期的那样有效。
阅读advice.el文件,我看到了
假设一个函数/宏/子/特殊形式有N条前建议,M条左右的建议和K条后建议。假设没有任何建议受到保护,其建议的定义将如下所示(正文形式索引对应于该建议类中相应建议的位置):
([macro] lambda <arglist>
[ [<advised-docstring>] [(interactive ...)] ]
(let (ad-return-value)
{<before-0-body-form>}*
....
{<before-N-1-body-form>}*
{<around-0-body-form>}*
{<around-1-body-form>}*
....
{<around-M-1-body-form>}*
(setq ad-return-value
<apply original definition to <arglist>>)
{<other-around-M-1-body-form>}*
....
{<other-around-1-body-form>}*
{<other-around-0-body-form>}*
{<after-0-body-form>}*
....
{<after-K-1-body-form>}*
ad-return-value))
这对我说的是,当建议的功能是交互式的时,`call-interactively'会在调用之前的建议或任何建议之前调用交互式表单。
而且,当我向compile
添加建议时,我观察到的行为证实了这一点。在处理交互式表单后,将在中调用该通知。交互式表单建议用于编译的字符串,之前我的建议有机会猜测它应该是什么,并预先设置它。
所以......
compile-command
?赞赏的想法。
答案 0 :(得分:5)
一个选项是在模式挂钩中设置变量compile-command
,类似于
(add-hook 'c++-mode-hook 'my-c++-set-compile-command)
(defun my-c++-set-compile-command ()
(setq (make-local-variable 'compile-command) (format "gmake %s" (buffer-file-name))))
我有时会添加专门的命令来调整当前的编译行(打开/关闭调试标志,优化标志等),然后将这些命令绑定到迷你缓冲区中方便的按键。
关于在interactive
表单之前添加建议,您需要在拥有所需的交互式表单时提供建议(在之前或之前)。来自advice.el库,一个例子:
;;(defadvice switch-to-buffer (around confirm-non-existing-buffers activate)
;; "Switch to non-existing buffers only upon confirmation."
;; (interactive "BSwitch to buffer: ")
;; (if (or (get-buffer (ad-get-arg 0))
;; (y-or-n-p (format "`%s' does not exist, create? " (ad-get-arg 0))))
;; ad-do-it))
答案 1 :(得分:5)
compile-command不必是字符串。编译函数可以避免它,因此它可以是一个函数,它返回一个特定于缓冲区的字符串,或者取决于一天中的时间等等:
(setq compile-command (lambda () (if (eq phase-of-moon 'waning)
"make -DWANING=1"
"make -DWANING=0")))
此外,尽管对您的特定需求可能没有用,但您始终可以在文件变量部分中定义compile-command:
/* -*- compile-command: "make -DFOO"; -*- */
或
// Local Variables:
// compile-command: "make -DSOMETHING_SPECIAL"
// End:
compile-command实际上用作the manual中文件变量的示例。
答案 2 :(得分:4)
我全局将C-xC-e设置为compile
。我没有使用建议,而是定义了一个包装compile
的函数,然后将C-xC-e绑定到那个。在包装器中,我猜测了编译命令。
(defun cheeso-invoke-compile-interactively ()
"fn to wrap the `compile' function. This simply
checks to see if `compile-command' has been previously guessed, and
if not, invokes `cheeso-guess-compile-command' to set the value.
Then it invokes the `compile' function, interactively."
(interactive)
(cond
((not (boundp 'cheeso-local-compile-command-has-been-set))
(cheeso-guess-compile-command)
(set (make-local-variable 'cheeso-local-compile-command-has-been-set) t)))
;; local compile command has now been set
(call-interactively 'compile))
猜测函数是这样的:
(defun cheeso-guess-compile-command ()
"set `compile-command' intelligently depending on the
current buffer, or the contents of the current directory."
(interactive)
(set (make-local-variable 'compile-command)
(cond
((or (file-expand-wildcards "*.csproj" t)
(file-expand-wildcards "*.vcproj" t)
(file-expand-wildcards "*.vbproj" t)
(file-expand-wildcards "*.shfbproj" t)
(file-expand-wildcards "*.sln" t))
"msbuild ")
;; sometimes, not sure why, the buffer-file-name is
;; not set. Can use it only if set.
(buffer-file-name
(let ((filename (file-name-nondirectory buffer-file-name)))
(cond
;; editing a .wxs (WIX Soluition) file
((string-equal (substring buffer-file-name -4) ".wxs")
(concat "nmake "
;; (substring buffer-file-name 0 -4) ;; includes full path
(file-name-sans-extension filename)
".msi" ))
;; a javascript file - run jslint
((string-equal (substring buffer-file-name -3) ".js")
(concat (getenv "windir")
"\\system32\\cscript.exe c:\\cheeso\\bin\\jslint-for-wsh.js "
filename))
;; something else - do a typical .exe build
(t
(concat "nmake "
(file-name-sans-extension filename)
".exe")))))
(t
"nmake "))))
答案 3 :(得分:1)
以下是我正在使用的一些代码,它可以智能地选择University of Wyoming提供的编译命令。我目前已将其设置为C, C++
和Fortran
。您可以添加更多以满足您的需求。如果我打开一个具有C++
扩展名的编程并执行 M-x compile ,我的编译命令会吐出g++ -Wall currentfilename.cpp -o currentfilename -std=c++14
。然后我必须按输入。
;; M-x compile smarter in order to guess language
(require 'compile)
(defvar compile-guess-command-table
'((c-mode . "gcc -Wall -g %s -o %s -lm")
(c++-mode . "g++ -Wall %s -o %s -std=c++14")
(fortran-mode . "gfortran -C %s -o %s")
))
(defun compile-guess-command ()
(let ((command-for-mode (cdr (assq major-mode
compile-guess-command-table))))
(if (and command-for-mode
(stringp buffer-file-name))
(let* ((file-name (file-name-nondirectory buffer-file-name))
(file-name-sans-suffix (if (and (string-match "\\.[^.]*\\'"
file-name)
(> (match-beginning 0) 0))
(substring file-name
0 (match-beginning 0))
nil)))
(if file-name-sans-suffix
(progn
(make-local-variable 'compile-command)
(setq compile-command
(if (stringp command-for-mode)
;; Optimize the common case.
(format command-for-mode
file-name file-name-sans-suffix)
(funcall command-for-mode
file-name file-name-sans-suffix)))
compile-command)
nil))
nil)))
;; Add the appropriate mode hooks.
(add-hook 'c-mode-hook (function compile-guess-command))
(add-hook 'c++-mode-hook (function compile-guess-command))
(add-hook 'fortran-mode-hook (function compile-guess-command))
答案 4 :(得分:0)
根据手册,您可以通过在建议中包含交互式表单来简单地覆盖功能的交互式表单。我认为您不能修改或包装现有的交互式表单,只能完全覆盖它。
http://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Advice.html