在M-x编译中使用当前缓冲区的文件名 [英] using the current buffer's file name in M-x compile

查看:108
本文介绍了在M-x编译中使用当前缓冲区的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like emacs to use the current buffer's file name as part of the command passed to M-x compile. For example, if I am editing ~/foo.rb, I would like M-x compile to execute ruby ~/foo.rb

我尝试将 compilation-command 设置为(列表ruby文件名),但显然你不能在这里传递一个s表达式。

I tried setting compilation-command to (list "ruby" buffer-file-name), but apparently you can't pass an s-expression here.

推荐答案

1。阅读函数的文档



Ch f compile RET

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.

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

Now that we've found what we were looking for, let's …

Ch v compile-command RET

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



3。 ...最后适应你的需要



3. … and finally adapt the example to your needs

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

当然,如果有
<$ c $,那么您当然可以轻松地自定义它,以使用 rake c> Rakefile

Of course you can easily customize it to use rake if there is a Rakefile.

这篇关于在M-x编译中使用当前缓冲区的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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