4
votes

I want to be able to tell emacs to open files in read-only mode or in auto-revert mode by providing a command line argument, for example:

emacs -A file1 file2 file3 ...  

should open files in auto-revert mode

emacs -R file1 file2 file3 ... 

should open files in read-only mode

I have found the following:

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

the problem with this is that it only works for a single file at a time.

i.e.

emacs -R file1 

works, but

emacs -R file1 file2

does not work.

How to change the functions above so that they could open several files simultaneously in the specified modes? Could someone suggest a simple and elegant solution?

1

1 Answers

4
votes

Just consume items from command-line-args-left until the next switch:

(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, notice that this will open each file relative to the directory of the previous one.