Emacs / Emacs Lisp:我可以在交互式表单之前插入建议吗?或者如何智能地预先设置编译命令? [英] Emacs/Emacs Lisp: can I insert advice before interactive form? or how to intelligently pre-set the compile-command?

查看:195
本文介绍了Emacs / Emacs Lisp:我可以在交互式表单之前插入建议吗?或者如何智能地预先设置编译命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是智能地为 compile 函数的字符串参数预设一个缓冲区本地默认值。

What I'd like to do is intelligently pre-set a buffer-local default value for the string argument to the compile function.

现在,compile.el默认使用make作为命令。我可以通过设置 compile-command 来设置。我甚至可以使该变量缓冲区本地。如果我想要相同的静态值,那么工作。

Right now compile.el defaults to using "make" as the command. I can set this by setting compile-command. I can even make that variable buffer-local. That works if I want the same static value, always.

但是,我想根据缓冲区的内容,名称,智能地选择 compile-command 的缓冲区,文件的包含目录的内容(如果有的话)和月相的相位。基本上,我想要控制默认值,然后允许交互式用户覆盖该预设值。

But I'd like to intelligently select the compile-command depending on the contents of the buffer, the name of the buffer, the contents of the containing directory of the file (if any), and the phase of the moon. Basically I want control over the default value, and then allow the interactive user to override that pre-set value.

我希望用before-advice这样做。但是这不符合我的预期。

I was hoping to do this with before-advice. But this isn't working as I expected.

阅读advice.el文件,我看到

Reading the advice.el file, I see


假设一个函数/宏/子/特殊格式有N个先前的建议,M个周围的建议和K个以后的建议。假设没有任何建议受到保护,其建议的定义将如下(身体形态指数对应于该建议类别中各自的建议的位置):

Suppose a function/macro/subr/special-form has N pieces of before advice, M pieces of around advice and K pieces of after advice. Assuming none of the advices is protected, its advised definition will look like this (body-form indices correspond to the position of the respective advice in that advice class):



([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))

这对我说的是Ť当建议的功能是交互式的时候,调用交互式在调用之前的建议或任何建议之前调用交互式表单。

What this says to me is that when the advised function is interactive, `call-interactively' invokes the interactive form before invoking the before advice, or any advice.

当我向编译添加建议时,我观察到的行为证实了这一点。在处理了交互式表单后,该建议被调用。交互式表单提示用于编译的字符串,之前,我的建议有机会猜测应该是什么,并预先设置它。

And, when I add advice to compile, the behavior I observe confirms this. The advice gets invoked after the interactive form is processed. The interactive form suggests the string to use for compilation, before my advice gets a chance to guess at what it should be, and to pre-set it.

所以...


  • 如何让我的代码运行互动形式?可以建议做这个吗如果不是建议,别的什么?或者

  • 如何为任何缓冲区动态预设 compile-command

  • how can I get my code to run before the interactive form? can advice do this? If not advice, something else? or
  • how can I dynamically pre-set compile-command for any buffer?

想法赞赏。

推荐答案

啊,你知道我做了什么吗?我用了一个倾斜的策略。

Ahh, you know what I did? I used an oblique strategy.

我将C-xC-e全局设置为 compile 。而不是使用建议,我定义了一个包含 compile 的函数,然后将C-xC-e绑定到 。在包装器中,我猜测编译命令。

I have globally set C-xC-e to compile. Instead of using advice, I defined a function that wraps compile, and then bound C-xC-e to that. Within the wrapper, I make the guess for the compile command.

(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 "))))

这篇关于Emacs / Emacs Lisp:我可以在交互式表单之前插入建议吗?或者如何智能地预先设置编译命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆