2012-06-06 149 views
4

我想通过提供命令行参数,例如能够告诉Emacs的只读模式 或自动复归模式打开文件: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 

不起作用。

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

回答

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

顺便说一句,请注意,这将每个相对于前一个目录文件打开。