1

I teach, and I like to use real files as examples in class. I want to be able to edit the buffer to show student things, but I don't want to risk accidentally writing the changes on the file.

What I currently do is to open them, run write-file and save them as a different file in a temp directory. But I wonder if there's a more elegant way. I tried find-file-read-only, but it doesn't allow me to edit. I couldn't find anything in the Manual, at least in the File Handling chapter.

I wonder if I can open a file in a buffer that acts like the Tutorial buffer. I can edit the buffer, but if I try to close it, it won't prompt me to save it. And even if I try to save it, it won't assume me the original file path/name as the place to write it on.

Any ideas?

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 1
    Why not open a random buffer and do `insert-file` (usually bound to `C-x i`) to put the file contents into said buffer? – Fran Burstall May 15 '22 at 13:04
  • Trivial workaround: Make the file read-only on the filesystem. – phils May 15 '22 at 13:35
  • @phils, if I do that, it doesn't let me edit it. – elPolloDiablo May 15 '22 at 13:46
  • @FranBurstall, thanks I didn't know about insert-file. I tried that, but a side effect is that because I'm bringing the contents without the file-name, the buffer won't automatically activate the modes I need on the file. If it is an org file, I'll have to run `org-mode`, `python-mode` for python files, etc. That makes it kind of cumbersome as well. – elPolloDiablo May 15 '22 at 13:50
  • Maybe what I want could come as a different question: what is the mechanism that emacs uses to open the tutorial buffer, which reads from a file, and let us change things there, without prompting us to rewrite the original file? – elPolloDiablo May 15 '22 at 13:55
  • Well, you find out how the tutorial works by reading the source! Briefly, it makes a buffer, inserts the tutorial content using the non-interactive version of `insert-file` and sets hooks `kill-buffer-hook` and `kill-emacs-query-functions` to control what happens on close. – Fran Burstall May 15 '22 at 14:46
  • 3
    @elPolloDiablo The file itself being read-only will prevent you from being able to clobber it if you try to save changes, but you can still edit the buffer by togging read-only mode with `C-x C-q`. – phils May 15 '22 at 15:18
  • What @phils said. Please consider adding that as an answer. Of course, it makes sense only if you don't mind changing the file permissions (and you have the ability to do that). It's even worth pointing out this "trick" to your students, if you're teaching the use of Emacs. The difference between read-only mode and a read-only file is an important one. – Drew May 15 '22 at 15:23
  • @FranBurstall, I guess a solution would be to define a function that does all of that. I'll give it a try. Thanks! – elPolloDiablo May 15 '22 at 15:49
  • The main lesson here is the difference - and hence potential separation - between a file and a file-visiting buffer. A file has permissions, which are attributes for the file system (e.g. disk). A buffer has modes, including `read-only-mode`. There's no necessary connection between the two. – Drew May 15 '22 at 19:32
  • In Vanilla-Emacs you can just click on the read-only indicator in the Mode Line to leave `read-only-mode`. That is the first percent sign `%` in the Mode Line. If you have left `read-only-mode` the `%` changes into a minus sign `-`. If you are using the mouse this handling is fast enough to make an extra function needless. – Tobias May 17 '22 at 14:58

0 Answers0