* Re: simple function to prepend a string to a file
[not found] <mailman.5524.1239973304.31690.help-gnu-emacs@gnu.org>
@ 2009-04-17 14:10 ` harven
2009-04-17 16:28 ` Johan Bockgård
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: harven @ 2009-04-17 14:10 UTC (permalink / raw)
To: help-gnu-emacs
ken <gebser@mousecar.com> writes:
> Often I want to put the string "<!-- -*- coding: utf-8; -*- -->\n" at
> the top of the current buffer. However, I don't want to lose my place
> (the point) or any defined region. Optimally, such a function would
> know the mode of the current buffer and enclose the string in the
> mode-appropriate comment characters. Is there such a function? If not
> how would one code it?
>
> Thanks for your solutions.
The following seems to work.
(defun my-header ()
"put a commented header at the beginning of the buffer"
(interactive)
(save-excursion
(beginning-of-buffer)
(let ((start (point)))
(insert "-*- coding: utf-8; -*-")
(comment-region start (point))
(newline))))
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: simple function to prepend a string to a file
2009-04-17 14:10 ` simple function to prepend a string to a file harven
@ 2009-04-17 16:28 ` Johan Bockgård
2009-04-17 23:01 ` simple function to prepend a string to a file --> SOLUTION ken
[not found] ` <mailman.5569.1240009331.31690.help-gnu-emacs@gnu.org>
2 siblings, 0 replies; 6+ messages in thread
From: Johan Bockgård @ 2009-04-17 16:28 UTC (permalink / raw)
To: help-gnu-emacs
harven <harven@free.fr> writes:
> (beginning-of-buffer)
"Don't use this command in Lisp programs!
(goto-char (point-min)) is faster and avoids clobbering the mark."
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: simple function to prepend a string to a file --> SOLUTION
2009-04-17 14:10 ` simple function to prepend a string to a file harven
2009-04-17 16:28 ` Johan Bockgård
@ 2009-04-17 23:01 ` ken
[not found] ` <mailman.5569.1240009331.31690.help-gnu-emacs@gnu.org>
2 siblings, 0 replies; 6+ messages in thread
From: ken @ 2009-04-17 23:01 UTC (permalink / raw)
Cc: help-gnu-emacs
On 04/17/2009 10:10 AM harven wrote:
> ken <gebser@mousecar.com> writes:
>
>> Often I want to put the string "<!-- -*- coding: utf-8; -*- -->\n" at
>> the top of the current buffer. However, I don't want to lose my place
>> (the point) or any defined region. Optimally, such a function would
>> know the mode of the current buffer and enclose the string in the
>> mode-appropriate comment characters. Is there such a function? If not
>> how would one code it?
>>
>> Thanks for your solutions.
>
> The following seems to work.
>
> (defun my-header ()
> "put a commented header at the beginning of the buffer"
> (interactive)
> (save-excursion
> (beginning-of-buffer)
> (let ((start (point)))
> (insert "-*- coding: utf-8; -*-")
> (comment-region start (point))
> (newline))))
>
On 04/17/2009 12:28 PM Johan Bockgård wrote:
> harven <harven@free.fr> writes:
>
>> (beginning-of-buffer)
>
> "Don't use this command in Lisp programs!
> (goto-char (point-min)) is faster and avoids clobbering the mark."
>
Fantastic, guys! Thanks!! Surprisingly to myself, my code was pretty
close to yours. But I was missing crucial syntax and never would have
gotten it without you. Here's our final version. It does everything
right (that I can see). I found that "(newline)" was unnecessary, so
took it out. Counter-intuitive, that.
(defun utf-decl-prepend ()
"Prepend to current buffer UTF declaration within comment."
(interactive)
(save-excursion
(goto-char (point-min))
(let ((start (point)))
(insert "-*- coding: utf-8; -*-")
(comment-region start (point)))))
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: simple function to prepend a string to a file --> SOLUTION
[not found] ` <mailman.5569.1240009331.31690.help-gnu-emacs@gnu.org>
@ 2009-04-18 10:49 ` harven
2009-04-18 15:25 ` Johan Bockgård
[not found] ` <mailman.5616.1240068338.31690.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 6+ messages in thread
From: harven @ 2009-04-18 10:49 UTC (permalink / raw)
To: help-gnu-emacs
ken <gebser@mousecar.com> writes:
> (defun utf-decl-prepend ()
> "Prepend to current buffer UTF declaration within comment."
> (interactive)
> (save-excursion
> (goto-char (point-min))
> (let ((start (point)))
> (insert "-*- coding: utf-8; -*-")
> (comment-region start (point)))))
(point-min) goes to the beginning of the accessible part of the
buffer, if the buffer is narrowed. I think it's better to really go at
the start of the buffer. This is a minor point though. Here is another
version, which is a bit shorter.
(defun utf-decl-prepend ()
"Prepend to current buffer UTF declaration within comment."
(interactive)
(save-excursion
(goto-char 1)
(insert "-*- coding: utf-8; -*-")
(comment-region 1 (point))
(newline)))
The final newline is not needed in lisp mode or tex mode, where a
single delimiter is used for commenting. It is needed in c-mode or
html-mode however. I don't understand why. In these languages, a closing tag
ends the comment region, this may be the reason.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: simple function to prepend a string to a file --> SOLUTION
2009-04-18 10:49 ` harven
@ 2009-04-18 15:25 ` Johan Bockgård
[not found] ` <mailman.5616.1240068338.31690.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 6+ messages in thread
From: Johan Bockgård @ 2009-04-18 15:25 UTC (permalink / raw)
To: help-gnu-emacs
harven <harven@free.fr> writes:
> (point-min) goes to the beginning of the accessible part of the
> buffer, if the buffer is narrowed.
(goto-char 1) does the same.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: simple function to prepend a string to a file --> SOLUTION
[not found] ` <mailman.5616.1240068338.31690.help-gnu-emacs@gnu.org>
@ 2009-04-18 17:15 ` harven
0 siblings, 0 replies; 6+ messages in thread
From: harven @ 2009-04-18 17:15 UTC (permalink / raw)
To: help-gnu-emacs
bojohan+news@dd.chalmers.se (Johan Bockgård) writes:
> harven <harven@free.fr> writes:
>
>> (point-min) goes to the beginning of the accessible part of the
>> buffer, if the buffer is narrowed.
>
> (goto-char 1) does the same.
Ack ! Then I don't know why I used (goto-char (point-min))
instead of (goto-char 1) all these years.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2009-04-18 17:15 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <mailman.5524.1239973304.31690.help-gnu-emacs@gnu.org>
2009-04-17 14:10 ` simple function to prepend a string to a file harven
2009-04-17 16:28 ` Johan Bockgård
2009-04-17 23:01 ` simple function to prepend a string to a file --> SOLUTION ken
[not found] ` <mailman.5569.1240009331.31690.help-gnu-emacs@gnu.org>
2009-04-18 10:49 ` harven
2009-04-18 15:25 ` Johan Bockgård
[not found] ` <mailman.5616.1240068338.31690.help-gnu-emacs@gnu.org>
2009-04-18 17:15 ` harven
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).