在M-x编译中使用当前缓冲区的文件名

时间:2022-11-12 19:19:21

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

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

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

我尝试将编译命令设置为(列出“ruby”缓冲文件名称),但显然不能在这里传递s表达式。

1 个解决方案

#1


28  

1. Read the documentation of the function

C-hfcompileRET

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.

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

既然我们已经找到了我们要找的东西,让我们……

2. … read the documentation of the variable …

C-hvcompile-commandRET

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

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

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

当然,如果存在Rakefile,您可以轻松地定制它来使用rake。

#1


28  

1. Read the documentation of the function

C-hfcompileRET

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.

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

既然我们已经找到了我们要找的东西,让我们……

2. … read the documentation of the variable …

C-hvcompile-commandRET

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

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

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

当然,如果存在Rakefile,您可以轻松地定制它来使用rake。