如何在Emacs中设置编译的默认目录?

Sof*_*mur 18 emacs ocaml compilation

我在Emacs下编写OCaml,我makefile在工作文件夹中有一个,以及包含.ml文件的几个子文件夹.如果我在缓冲区上启动M-x compilemake正常工作makefile,但在.ml文件缓冲区上不起作用,则会给出错误:

-*- mode: compilation; default-directory: "..." -*-
Compilation started at Fri Jan 27 18:51:35

make -k
make: *** No targets specified and no makefile found.  Stop.

Compilation exited abnormally with code 2 at Fri Jan 27 18:51:35
Run Code Online (Sandbox Code Playgroud)

这是可以理解的,因为default-directory是不包含的子文件夹makefile.有谁知道如何将makefilealways 的文件夹设置为编译的默认目录?

Tho*_*mas 17

您可以make使用正确的参数调用:

make -C .. -k
Run Code Online (Sandbox Code Playgroud)

..你的路径在哪里Makefile


Iva*_*rus 8

您可以通过编写(临时)设置default-directory和调用的函数从emacs中控制此操作compile.

(defun compile-in-parent-directory ()
  (interactive)
  (let ((default-directory
          (if (string= (file-name-extension buffer-file-name) "ml")
              (concat default-directory "..")
            default-directory))))
  (call-interactively #'compile))
Run Code Online (Sandbox Code Playgroud)

当使用compile-in-parent-directory所有ml文件时,将在父目录中编译它们所在的位置.当然,如果它们嵌套得更深,你可以改变逻辑来反映这一点.事实上,EmacsWiki上有一个版本可以搜索父目录,直到找到一个makefile.在我写完这个答案之后我发现了这个,否则我会在那里指出你. 叹了口气.关于我的方法的好处是,它不是特定的,make所以你可以对其他命令使用相同的"技巧".

如果您确切知道命令的内容,也可以将编译调用更改为非交互式.如果它绑定到适当模式钩子中的键,这将特别有效.


Dou*_*all 1

Matthias Puech 有一个解决方案,涉及.dir-local项目根目录中的一个文件:

((nil . ((eval . (setq default-directory 
                  (locate-dominating-file buffer-file-name
                   ".dir-locals.el")
)))))
Run Code Online (Sandbox Code Playgroud)

大概也可以使用类似:(shell-command-to-string "git rev-parse --show-toplevel")作为最里面的位。