You are not logged in.

#1 2009-07-30 16:53:39

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Emacs edit read-only file, C-x C-s does Save As instead?

I am currently using emacs from the command line with AUCTeX to create LaTeX documents. I finally got the formatting just how I like it and would like to save a .tex file as a template. I created an alias that opens this file in emacs with the command "emacslatex". I would like to make this template.tex file read only, yet I can still edit the buffer and save as a new file. I could just do this by saving with C-x C-w, but I am so used to doing C-x C-s to save files that I slip occassionally. In vim, editing a read only file just shows a warning at the bottom of the screen that I will not be able to write this file, but then I am able to edit just fine save as a new file name. How can I get similar functionality with emacs? Googling and looking through the emacs documentation has turned up no results, unfortunately. Perhaps AUCTeX does something I am not aware of?


дɭɭɑӎɠїɾ

Offline

#2 2009-07-30 17:22:12

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Emacs edit read-only file, C-x C-s does Save As instead?

Why not make your "emacslatex" command copy the template to a new file and open that? Or make it open a buffer associated with no file then copy the text into it.

Offline

#3 2009-07-30 17:23:46

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: Emacs edit read-only file, C-x C-s does Save As instead?

C-x C-q.
You can also tell and enable/disable from the mode line.
In my current buffer it shows:

U:@%*

meaning
Unicode|undecided end-of-line style|emacsclient frame|read only|modified buffer

--Rasmus


Arch x64 on Thinkpad X200s/W530

Offline

#4 2009-07-30 17:31:47

&#32 Greg
Member
Registered: 2009-02-08
Posts: 80

Re: Emacs edit read-only file, C-x C-s does Save As instead?

Why not take something like this, and put it in /usr/bin- then when you run emacslatex new.tex, you get your template in a new editable file in a name of your choice.

#!/bin/bash

cat /path/to/template.tex > $1

emacs -nw $1

Offline

#5 2009-07-30 17:32:39

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: Emacs edit read-only file, C-x C-s does Save As instead?

pauldonnelly wrote:

Why not make your "emacslatex" command copy the template to a new file and open that? Or make it open a buffer associated with no file then copy the text into it.

How exactly would I do that? The command currently run when I type in "emacslatex" is:

emacs -nw +33:32 /home/agi/Documents/template.tex

дɭɭɑӎɠїɾ

Offline

#6 2009-07-30 17:52:49

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: Emacs edit read-only file, C-x C-s does Save As instead?

Ok I tried your script, greg, and every time I run it an extra blank buffer is opened along with my .tex template (which opens correctly). How do I make this extra blank buffer not open?

I commented out the alias for emacslatex in my .zshrc and sourced it


дɭɭɑӎɠїɾ

Offline

#7 2009-07-30 17:58:22

&#32 Greg
Member
Registered: 2009-02-08
Posts: 80

Re: Emacs edit read-only file, C-x C-s does Save As instead?

Odd, I don't get an extra blank buffer when I run it on my system...

Offline

#8 2009-07-30 18:04:34

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: Emacs edit read-only file, C-x C-s does Save As instead?

The top buffer has template.tex in read only mode (so I STILL can't edit it), while the bottom buffer has new.tex completely blank.

How to I make Pank's solution (C-x C-q) work for all read-only documents? What do I put in my .emacs file?


дɭɭɑӎɠїɾ

Offline

#9 2009-07-31 21:23:33

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Emacs edit read-only file, C-x C-s does Save As instead?

Allamgir wrote:
pauldonnelly wrote:

Why not make your "emacslatex" command copy the template to a new file and open that? Or make it open a buffer associated with no file then copy the text into it.

How exactly would I do that? The command currently run when I type in "emacslatex" is:

emacs -nw +33:32 /home/agi/Documents/template.tex

I figure you'd ideally want to use emacsclient to call a function that copies it into a new buffer. Something like the following.

; In .emacs:
(server-start) ; If it's not there already.

(defun create-tex-from-template ()
  (let ((b (generate-new-buffer "TeX-template")))
    (switch-to-buffer b)
    (tex-mode)
    (insert-file-contents "~/test.file")))

# In emacslatex:
emacsclient -e '(create-tex-from-template)'

There may be a better way to write it, or you may want to start a new Emacs and call that function in it. Or you might like to let it take an optional file name argument.

Last edited by pauldonnelly (2009-07-31 21:24:20)

Offline

Board footer

Powered by FluxBB