Emacs自定义命令行参数

4

我希望能够通过命令行参数告诉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个回答

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接