iny+news(a)iki.fi (Ilpo Nyyssönen) writes:
Loading a file with '-*- coding: utf-8 -*-' in the first line
works OK,
characters like ö and ä look OK.
Loading a file with
Local variables:
mode: text
coding: utf-8
End:
In addition to what Stephen already said. I've been using this for a
while now, and it works for me:
;; support the coding tag inside a local variables block
(defun jmh:find-coding-cookie-in-local-variables (filename visit)
(when (file-exists-p filename)
(with-temp-buffer
(let* ((attrs (file-attributes filename))
(size (nth 7 attrs))
(start (- size 3001))
(end (1- size)))
(when (< start 0) (setq start 0))
(when (< end 0) (setq end 0))
;; check only the last 3000 bytes of the file for the cookie
(insert-file-contents-literally filename nil start end)
(save-excursion
(let (start end prefix postfix)
(and (re-search-forward
;; \ between Local and Variables to make sure
;; XEmacs doesn't think it's an actual
;; Local Vars block
"^\\(.*?\\)[ \t]*Local\ Variables:\\(.*\\)" nil t)
(setq prefix (regexp-quote
(buffer-substring
(match-beginning 1) (match-end 1))))
(setq postfix (regexp-quote
(buffer-substring
(match-beginning 2) (match-end 2))))
(setq start (match-end 0))
(re-search-forward
(concat "\n" prefix "[ \t]*End:" postfix))
(setq end (match-beginning 0))
;; narrow to the correct region and look for the cookie
(save-restriction
(narrow-to-region start end)
(goto-char start)
(re-search-forward
(concat
"\n" prefix "[ \t]*coding: \\([^\n]+\\)" postfix) nil t))
(let ((codesys
(intern (buffer-substring
(match-beginning 1) (match-end 1)))))
(if (find-coding-system codesys)
(progn
(message (concat
"Coding system set automatically in"
" Local\ Variables: "
(symbol-name codesys)))
codesys))))))))))
;; add this defun to the right hook
(add-hook 'insert-file-contents-pre-hook
'jmh:find-coding-cookie-in-local-variables)
Warning, YMMV, and no guarantees. I need to use this to avoid hosing
the Gnus changelog files which specify the coding system at the
end. (because new entries are inserted at the top of the file...)
I wish I didn't have to use this however -- moving things to utf-8
would be nice. I've been sufficiently convinced that the coding
cookies are evil; at least this defun only checks the last 3000 bytes
in the file.
Enjoy,
--
Josh Huber