emacs自定义命令行参数

时间:2012-06-06 07:15:38

标签: emacs elisp

我希望能够告诉emacs以只读模式打开文件 或者通过提供命令行参数在自动恢复模式下,例如:

emacs -A file1 file2 file3 ...  

应以自动恢复模式打开文件

emacs -R file1 file2 file3 ... 

应该以只读模式打开文件

我找到了以下内容:

(defun open-read-only (switch)
  (let ((file1 (expand-file-name (pop command-line-args-left))))
  (find-file-read-only file1)))
(add-to-list 'command-switch-alist '("-R" . open-read-only))

(defun open-tail-revert (switch)
  (let ((file1 (expand-file-name (pop command-line-args-left))))
  (find-file-read-only file1)
  (auto-revert-tail-mode t)))
(add-to-list 'command-switch-alist '("-A" . open-tail-revert))

这个问题是它一次只适用于一个文件。

即。

emacs -R file1 

有效,但

emacs -R file1 file2

不起作用。

如何更改上述功能以便他们可以 在指定的模式下同时打开几个文件? 有人可以建议一个简单而优雅的解决方案吗?

1 个答案:

答案 0 :(得分:4)

只需使用command-line-args-left中的项目,直到下一次切换:

(defun open-read-only (switch)
  (while (and command-line-args-left
              (not (string-match "^-" (car command-line-args-left))))
    (let ((file1 (expand-file-name (pop command-line-args-left))))
      (find-file-read-only file1))))

BTW,请注意,这将打开相对于前一个目录的每个文件。

相关问题