在Emacs中可以创建“项目文件”吗?
我说的“项目文件”其实是个宽泛的说法。我有几个用Python写的项目,都是在Windows上用emacs和ropemacs来做的。理想情况下,我希望能在桌面上有一个图标,点一下就能打开emacs,加载这个rope项目,并且在项目的顶层目录显示速度条。这样的话,我也希望能有办法打开下一个项目,设置成一样的emacs环境(不过是针对那个项目的)。当然,如果有emacs命令或者shell命令可以实现同样的效果,而不需要桌面上的图标,那也是可以接受的。
有没有办法做到这一点呢?我对elisp完全不懂。 :-(
7 个回答
我自己用的“解决方案”是这样的:项目的根目录是一个包含 Makefile
的文件夹。我把 F12 键绑定到一个特定模式的 elisp 函数,这个函数可以通过执行对应的 Makefile 的第一个目标来“编译”项目。这个 Makefile 是通过从当前文件所在的目录向上递归查找得到的。
虽然这需要一点设置,但你再也不需要像以前在 Eclipse 中那样频繁地重建 .metadata
目录。一旦设置好,你只需要把合适的 Makefile 放在合适的位置,就可以开始你的项目了。
(defun get-makefile-recursively-higher ()
(loop for i below 100 for cwd = (expand-file-name default-directory)
then next for next = (expand-file-name (concat cwd "/..")) for file =
(concat cwd "/" "Makefile") do (cond ((and (file-exists-p file))
(return file))) until (equal cwd next))
)
接下来,比如 LaTeX 和 Python 模式就可以这样使用:
(defun py-execute-prog (&optional target)
"Invoke python on the file being edited in the current buffer using
arguments obtained from the minibuffer. It will save all of the modified
buffers before trying to execute the file."
(interactive)
(let* (makefile file cmd)
(setq makefile (get-makefile-recursively-higher))
(setq file (buffer-file-name (current-buffer)))
(setq cmd (concat "make -f " makefile))
(setq default-directory (file-name-directory makefile))
(save-some-buffers (not py-ask-about-save) nil)
(setq py-pdbtrack-do-tracking-p t)
(if (get-buffer py-output-buffer)
(kill-buffer py-output-buffer)) ; Get rid of buffer if it exists.
(global-unset-key "\C-x\C-l" )
(global-unset-key "\C-x\C-p" )
(global-unset-key "\C-x\C-e" )
(global-unset-key "\C-x\C-n" )
(global-set-key "\C-x\C-l" 'py-last-exception)
(global-set-key "\C-x\C-p" 'py-previous-exception)
(global-set-key "\C-x\C-e" 'py-current-line-exception)
(global-set-key "\C-x\C-n" 'py-next-exception)
(define-key comint-mode-map [mouse-3] 'py-current-line-exception)
(make-comint "Python Output" "make" nil "-f" makefile)
(if (not (get-buffer py-output-buffer))
(message "No output.")
(setq py-exception-buffer (current-buffer))
(pop-to-buffer py-output-buffer)
)))
(defun make-tex (&optional target)
(interactive)
(let (makefile cmd)
(setq makefile (get-makefile-recursively-higher))
(save-buffer)
(TeX-save-document (TeX-master-file))
(setq cmd (concat "make -j4 -f " makefile " LATEXPARAM=\"-halt-on-error -file-line-error\""
" TEXMASTER=" (expand-file-name (TeX-master-file)) ".tex"
" TEXMASTERDIR=" (file-name-directory makefile) "/"))
(when (stringp target)
(setq cmd (concat cmd " " target))
)
(ad-activate-regexp "auto-compile-yes-or-no-p-always-yes")
(compile cmd)
(ad-deactivate-regexp "auto-compile-yes-or-no-p-always-yes")
)
)
我其实正在解决这个问题。我总是有一组文件想要同时打开或关闭,还想做一些其他事情,比如打开一个 magit-status 窗口、一个 dired 缓冲区等等。我开始了一个自己的项目模式,叫做 metaproject。现在这个项目还处于早期阶段,但已经足够好用,我现在在工作中用它来管理项目组。
你可以在这里查看:http://nafai77.github.com/metaproject/
在 git 上的内容相对稳定,虽然文档不多。我很快会重新开始对它进行开发。目前我已经有了一个小插件架构的基础,这样你就可以注册一些自定义的操作,在打开项目时可以执行这些操作。
你可以把项目的所有设置都调整到你想要的样子,然后参考我之前发的关于使用desktop.el和命名会话的内容: