4

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.