* How to write the current buffer into some file?
@ 2014-10-10 0:44 Marcin Borkowski
2014-10-10 1:23 ` John Mastro
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Marcin Borkowski @ 2014-10-10 0:44 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org
Hello list,
I'd like to do /some stuff/ on the current buffer. /Some stuff/
includes creating a new file, similar to the current buffer, with some
modifications. Say that I'm visiting file "./test.org"; I want /some
stuff/ to happen in "./test-output/test-output.org". I can handle the
filename change (easy), creation of the directory (easy), but here's
where I'm (a bit) stuck: how to create a new buffer, do the
modifications, save it, and close/bury so that after my function
returns, the user sees the same buffer as before? I can't just
write-file, since this will change the file I'm visiting. Here's what
I've come up with:
I can clone the existing buffer (too bad that clone-buffer doesn't let
me do this if I'm visiting a file; why is that so?) by means of manually
creating it, copying the contents (by means of insert-buffer-substring,
for instance), doing my modifications, saving it, and (possibly)
burying.
Question 1: did I forget about something? Should I wrap all this in
some (with-current-buffer ...)? (I guess that if I call all this
interactively, and do not change the buffer of the selected window,
(with-current-buffer) is not necessary - but maybe (a) it's bad style
and/or (b) it can backfire when e.g. I decide to run my function
non-interactively (or maybe in some other circumstances)?
Question 2: What would happen if I didn't (bury-buffer) after all that
stuff? Would it end up "just below" the current buffer in the buffer
list?
TIA,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to write the current buffer into some file?
2014-10-10 0:44 How to write the current buffer into some file? Marcin Borkowski
@ 2014-10-10 1:23 ` John Mastro
2014-10-10 1:41 ` John Mastro
2014-10-10 1:24 ` Stefan Monnier
2014-10-10 3:42 ` Drew Adams
2 siblings, 1 reply; 5+ messages in thread
From: John Mastro @ 2014-10-10 1:23 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org
Hi Marcin,
Marcin Borkowski <mbork@wmi.amu.edu.pl> wrote:
> Question 1: did I forget about something? Should I wrap all this in
> some (with-current-buffer ...)? (I guess that if I call all this
> interactively, and do not change the buffer of the selected window,
> (with-current-buffer) is not necessary - but maybe (a) it's bad style
> and/or (b) it can backfire when e.g. I decide to run my function
> non-interactively (or maybe in some other circumstances)?
>
> Question 2: What would happen if I didn't (bury-buffer) after all that
> stuff? Would it end up "just below" the current buffer in the buffer
Yes, I always use `with-current-buffer' when working with a buffer,
because there are many functions that operate on "the current buffer" by
default.
If you use a combination of `with-current-buffer' and `write-region', no
call to `bury-buffer' will be necessary. Goofy example below:
(with-current-buffer (clone-buffer)
(insert "\nStuff happening!\n")
(write-region (point-min) (point-max) "~/destination.txt"))
Hope that helps.
--
john
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to write the current buffer into some file?
2014-10-10 0:44 How to write the current buffer into some file? Marcin Borkowski
2014-10-10 1:23 ` John Mastro
@ 2014-10-10 1:24 ` Stefan Monnier
2014-10-10 3:42 ` Drew Adams
2 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2014-10-10 1:24 UTC (permalink / raw)
To: help-gnu-emacs
> Question 2: What would happen if I didn't (bury-buffer) after all that
> stuff?
No: instead you should simply never display the auxiliary buffer.
E.g. don't use switch-to-buffer, find-file or any other
such abomination (they're nice *commands* to use interactively, but
they shouldn't be used within Elisp code).
Stefan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to write the current buffer into some file?
2014-10-10 1:23 ` John Mastro
@ 2014-10-10 1:41 ` John Mastro
0 siblings, 0 replies; 5+ messages in thread
From: John Mastro @ 2014-10-10 1:41 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org
John Mastro <john.b.mastro@gmail.com> wrote:
> If you use a combination of `with-current-buffer' and `write-region', no
> call to `bury-buffer' will be necessary. Goofy example below:
>
> (with-current-buffer (clone-buffer)
> (insert "\nStuff happening!\n")
> (write-region (point-min) (point-max) "~/destination.txt"))
I didn't address the fact that `clone-buffer' won't work with a
file-visiting buffer, as you noted. Also, `with-temp-buffer' is probably
an even better fit than `with-current-buffer', if you don't need that
buffer to stick around when you're done.
Here's another example approach, hopefully more useful:
(let ((buffer (current-buffer)))
(with-temp-buffer
(insert-buffer-substring buffer)
(insert "\nStuff happening!\n")
(write-region (point-min) (point-max) "~/destination.txt")))
--
john
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: How to write the current buffer into some file?
2014-10-10 0:44 How to write the current buffer into some file? Marcin Borkowski
2014-10-10 1:23 ` John Mastro
2014-10-10 1:24 ` Stefan Monnier
@ 2014-10-10 3:42 ` Drew Adams
2 siblings, 0 replies; 5+ messages in thread
From: Drew Adams @ 2014-10-10 3:42 UTC (permalink / raw)
To: Marcin Borkowski, help-gnu-emacs
> I'd like to do /some stuff/ on the current buffer. /Some stuff/
> includes creating a new file, similar to the current buffer, with
> some modifications. Say that I'm visiting file "./test.org"; I want
> /some stuff/ to happen in "./test-output/test-output.org".
You probably want `write-region' (or possibly `write-file', if you
want to visit the file).
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-10-10 3:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-10 0:44 How to write the current buffer into some file? Marcin Borkowski
2014-10-10 1:23 ` John Mastro
2014-10-10 1:41 ` John Mastro
2014-10-10 1:24 ` Stefan Monnier
2014-10-10 3:42 ` Drew Adams
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).