help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: simple function to prepend a string to a file --> SOLUTION


From: ken
Subject: Re: simple function to prepend a string to a file --> SOLUTION
Date: Fri, 17 Apr 2009 19:01:32 -0400
User-agent: Thunderbird 2.0.0.0 (X11/20070326)

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)))))






reply via email to

[Prev in Thread] Current Thread [Next in Thread]