9

I know I can set something like -*- mode: read-only-mode -*- inside a commentary and Emacs will open the corresponding file as read-only. My problem is that I still want to read files that include such a line with the org-mode major mode; Emacs opens these files in fundamental-mode.

How can I tell Emacs to open a file as read-only and still make it use the correct major mode?

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
shackra
  • 2,702
  • 18
  • 47

2 Answers2

10

read-only-mode is a minor mode, and should not be set that way.

It's true that using mode: with minor modes used to work. I'm not sure offhand if it still does, but it's definitely deprecated (and if it's clobbering org-mode, then maybe it no longer works at all).

Only use mode: to set the major mode, and use eval: to enable minor modes:

You can probably use either of these:

-*- eval: (read-only-mode 1) -*-
-*- buffer-read-only: t -*-
phils
  • 48,657
  • 3
  • 76
  • 115
  • Using `mode:` with minor modes still works. I use it myself, typically listing a major mode first and then a minor one (like `outline-minor`) afterwards. But I see now that the manual says not to do that, so I guess I shall have to change this practice. Probably with a hook rather than an `eval`. – Harald Hanche-Olsen Dec 01 '14 at 14:21
6

The normal way would be to make the file read-only in your operating system. With the file open, run M-x set-file-modes RET (or its alias chmod), press down RET to enter the name of the current file, and enter a=r (for “all becomes read” — see file permissions for details). You can also do this from Dired or any other file manager.

Alternatively, if you want the file to be read-only in Emacs but writable in other applications, set the file variable buffer-read-only to t.

-*- buffer-read-only: t -*-