tags:

views:

125

answers:

1

I have been using emacs for some time now and am slowly getting the hang of things. However, I don't know enough emacs-lisp to implement the following functionality:

I want to define a list (say prog-modes), which will be a list of the programming modes I use (.c, .cpp, .h, .el, .py). If the file I'm opening is of a type mentioned in this list, I want it to be opened read-only. Otherwise, I want it to open normally.

I prefer opening my files read-only to avoid any mess-up with stray edits, but this gets irritating when emacs tries to auto-open files and write to them (eg. in org-mode), hence the need for such a function.

+5  A: 

Something like this should meet your needs. Obviously customize the list of major modes:

(defun make-some-files-read-only ()
  "when file opened is of a certain mode, make it read only"
  (when (memq major-mode '(c++-mode tcl-mode text-mode))
    (toggle-read-only 1)))

(add-hook 'find-file-hooks 'make-some-files-read-only)
Trey Jackson
Thanks!This works.
vedang