[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: simple function to prepend a string to a file --> SOLUTION
From: |
harven |
Subject: |
Re: simple function to prepend a string to a file --> SOLUTION |
Date: |
Sat, 18 Apr 2009 12:49:29 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin) |
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.