* How to copy current buffer to a temporary buffer
@ 2014-05-16 7:59 Cecil Westerhof
2014-05-16 9:31 ` Pascal J. Bourguignon
0 siblings, 1 reply; 6+ messages in thread
From: Cecil Westerhof @ 2014-05-16 7:59 UTC (permalink / raw)
To: help-gnu-emacs
I want to copy the current buffer to a temporary buffer, to do some
edits on it. (Converting a text file to a html file.)
The following works:
(defun make-html-file ()
(interactive)
(let ((buffer-name (generate-new-buffer-name "tempori.html")))
(copy-region-as-kill (point-min) (point-max))
(switch-to-buffer buffer-name)
(html-mode)
(yank)))
But the ‘problem’ is that this adds your current buffer to the kill
ring. That is a side effect that should be circumvented.
So I tried the following:
(defun make-html-file ()
(interactive)
(let ((buffer-name (generate-new-buffer-name "tempori.html"))
(old-buffer (current-buffer)))
(switch-to-buffer buffer-name)
(auto-fill-mode 0)
(insert-buffer-substring old-buffer)
(html-mode)))
The problem is that this formats the lines and because of this the
regular expressions that are used for the converting do not work
anymore. How can I write the function in such a way that it does not
add to the kill ring, without the lines being formatted?
I could of-course use:
(pop kill-ring)
in the first function, but could the second function made to work?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to copy current buffer to a temporary buffer
2014-05-16 7:59 How to copy current buffer to a temporary buffer Cecil Westerhof
@ 2014-05-16 9:31 ` Pascal J. Bourguignon
2014-05-16 10:13 ` Eli Zaretskii
[not found] ` <mailman.1456.1400235243.1147.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2014-05-16 9:31 UTC (permalink / raw)
To: help-gnu-emacs
Cecil Westerhof <Cecil@decebal.nl> writes:
> I want to copy the current buffer to a temporary buffer, to do some
> edits on it. (Converting a text file to a html file.)
(let ((contents (buffer-substring (point-min) (point-max))))
(with-temp-buffer
(insert contents)
(do-something)))
--
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ? C'est le moment d'acheter !"
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to copy current buffer to a temporary buffer
2014-05-16 9:31 ` Pascal J. Bourguignon
@ 2014-05-16 10:13 ` Eli Zaretskii
[not found] ` <mailman.1456.1400235243.1147.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2014-05-16 10:13 UTC (permalink / raw)
To: help-gnu-emacs
> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
> Date: Fri, 16 May 2014 11:31:28 +0200
>
> Cecil Westerhof <Cecil@decebal.nl> writes:
>
> > I want to copy the current buffer to a temporary buffer, to do some
> > edits on it. (Converting a text file to a html file.)
>
>
> (let ((contents (buffer-substring (point-min) (point-max))))
> (with-temp-buffer
> (insert contents)
> (do-something)))
I think using the insert-buffer function will make this more efficient
(since no string needs to be consed, something that might not be
trivial with large buffers).
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to copy current buffer to a temporary buffer
[not found] ` <mailman.1456.1400235243.1147.help-gnu-emacs@gnu.org>
@ 2014-05-16 11:49 ` Cecil Westerhof
2014-05-16 18:22 ` Pascal J. Bourguignon
2014-05-16 19:05 ` Thien-Thi Nguyen
0 siblings, 2 replies; 6+ messages in thread
From: Cecil Westerhof @ 2014-05-16 11:49 UTC (permalink / raw)
To: help-gnu-emacs
Op Friday 16 May 2014 12:13 CEST schreef Eli Zaretskii:
>> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
>> Date: Fri, 16 May 2014 11:31:28 +0200
>>
>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>
>>> I want to copy the current buffer to a temporary buffer, to do
>>> some edits on it. (Converting a text file to a html file.)
>>
>>
>> (let ((contents (buffer-substring (point-min) (point-max))))
>> (with-temp-buffer
>> (insert contents)
>> (do-something)))
>
> I think using the insert-buffer function will make this more
> efficient (since no string needs to be consed, something that might
> not be trivial with large buffers).
I used insert-buffer earlier on, but that formatted the lines and that
is not acceptable.
I now have the following code:
(defun make-html-file ()
(copy-region-as-kill (point-min) (point-max))
(switch-to-buffer (generate-new-buffer-name "tempori.html"))
(yank)
(pop kill-ring)
(html-mode))
No need for a let anymore and I think the code is more clear also.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to copy current buffer to a temporary buffer
2014-05-16 11:49 ` Cecil Westerhof
@ 2014-05-16 18:22 ` Pascal J. Bourguignon
2014-05-16 19:05 ` Thien-Thi Nguyen
1 sibling, 0 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2014-05-16 18:22 UTC (permalink / raw)
To: help-gnu-emacs
Cecil Westerhof <Cecil@decebal.nl> writes:
> Op Friday 16 May 2014 12:13 CEST schreef Eli Zaretskii:
>
>>> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
>>> Date: Fri, 16 May 2014 11:31:28 +0200
>>>
>>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>>
>>>> I want to copy the current buffer to a temporary buffer, to do
>>>> some edits on it. (Converting a text file to a html file.)
>>>
>>>
>>> (let ((contents (buffer-substring (point-min) (point-max))))
>>> (with-temp-buffer
>>> (insert contents)
>>> (do-something)))
>>
>> I think using the insert-buffer function will make this more
>> efficient (since no string needs to be consed, something that might
>> not be trivial with large buffers).
Oops, I knew I was missing something.
> I used insert-buffer earlier on, but that formatted the lines and that
> is not acceptable.
You can do this then:
(let ((source-buffer (current-buffer)))
(with-temp-buffer
(insert-buffer source-buffer)
(set-text-properties (point-min) (point-max) '())
(do-something)))
--
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ? C'est le moment d'acheter !"
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to copy current buffer to a temporary buffer
2014-05-16 11:49 ` Cecil Westerhof
2014-05-16 18:22 ` Pascal J. Bourguignon
@ 2014-05-16 19:05 ` Thien-Thi Nguyen
1 sibling, 0 replies; 6+ messages in thread
From: Thien-Thi Nguyen @ 2014-05-16 19:05 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 556 bytes --]
() Cecil Westerhof <Cecil@decebal.nl>
() Fri, 16 May 2014 13:49:00 +0200
I used insert-buffer earlier on, but that formatted
the lines and that is not acceptable.
What does "formatted the lines" mean, precisely? The docstring for
‘insert-buffer’ recommends ‘insert-buffer-substring’. Did you try that?
--
Thien-Thi Nguyen
GPG key: 4C807502
(if you're human and you know it)
read my lisp: (responsep (questions 'technical)
(not (via 'mailing-list)))
=> nil
[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-05-16 19:05 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-16 7:59 How to copy current buffer to a temporary buffer Cecil Westerhof
2014-05-16 9:31 ` Pascal J. Bourguignon
2014-05-16 10:13 ` Eli Zaretskii
[not found] ` <mailman.1456.1400235243.1147.help-gnu-emacs@gnu.org>
2014-05-16 11:49 ` Cecil Westerhof
2014-05-16 18:22 ` Pascal J. Bourguignon
2014-05-16 19:05 ` Thien-Thi Nguyen
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).