在Mx编译中使用当前缓冲区的文件名

ben*_*hsu 18 emacs

我希望emacs使用当前缓冲区的文件名作为传递给命令的一部分M-x compile.例如,如果我正在编辑〜/ foo.rb,我想M-x compile执行ruby ~/foo.rb

我尝试设置compilation-command(list "ruby" buffer-file-name),但显然你不能在这里传递一个s表达式.

Dai*_*rod 33

1.阅读该功能的文档

C-hfcompileRET

compile is an interactive autoloaded compiled Lisp function in
`compile.el'.
[snip]
Interactively, prompts for the command if `compilation-read-command' is
non-nil; otherwise uses `compile-command'.  With prefix arg, always prompts.
Additionally, with universal prefix arg, compilation buffer will be in
comint mode, i.e. interactive.
Run Code Online (Sandbox Code Playgroud)

现在我们找到了我们想要的东西,让我们......

2. ...阅读变量的文档......

C-hvcompile-commandRET

compile-command is a variable defined in `compile.el'.
Its value is "make -k "
[snip]
Sometimes it is useful for files to supply local values for this variable.
You might also use mode hooks to specify it in certain modes, like this:

    (add-hook 'c-mode-hook
       (lambda ()
     (unless (or (file-exists-p "makefile")
             (file-exists-p "Makefile"))
       (set (make-local-variable 'compile-command)
        (concat "make -k "
            (file-name-sans-extension buffer-file-name))))))
Run Code Online (Sandbox Code Playgroud)

3. ...最后根据您的需求调整示例

(add-hook 'ruby-mode-hook
          (lambda ()
            (set (make-local-variable 'compile-command)
                 (concat "ruby " buffer-file-name))))
Run Code Online (Sandbox Code Playgroud)

当然,rake如果有的话, 您可以轻松自定义它Rakefile.

  • +1教导男人钓鱼!从现在开始使用这个过程. (4认同)
  • 您需要 shell 引用文件名:`(concat "ruby " (shell-quote-argument buffer-file-name))`。 (2认同)