[Top][All Lists]

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

How to overwrite completely a buffer with a file, with overwriting also

From: Vincent Belaïche
Subject: How to overwrite completely a buffer with a file, with overwriting also the coding system ?
Date: Fri, 8 May 2009 23:07:44 +0200

Dear emacs experts,

I am raising a question to this group: I have a problem with the emacs-template extension package used in conjunction with GNU-Emacs and with my MS-Windows-machine.

When I create a new target-file from a template, emacs-template creates a buffer for this target-file, which therefore is created with the default coding-system (iso-latin-1-dos for my machine), and then emacs-template insert into this buffer the content of the template file. Now I want that the file be created with iso-latin-1-unix coding. Even though the template file contains a -*- coding: iso-latin-1-unix -*- the target file is created with dos end-of-lines. This means that after saving and reopening the file I get all those CR at end of lines.

Christoph Wedler told me that the code of emacs-template does not have this problem with XEmacs.

I found a hack that works for me. The current mal-functionning function is the following (current buffer is the buffer for target file, and filename is the name of file containing the template):

(defun template-find-template (filename &optional replace)
  (let ((file-name-buffer-file-type-alist nil))    ; Emacs on DOS/NT
    (if replace
    (insert-file-contents filename nil nil nil
                  ;; 5th arg not t with empty accessible part
                  ;; (XEmacs bug workaround: would infloop)
                  (> (point-max) (point-min)))
      (let ((template-auto-insert nil))
    (switch-to-buffer (find-file-noselect filename))))))

I replaced it as follows with use of revert-buffer, and this seems to partially solve the problem for me (actually there is still an issue with the created buffer being not marked as modified untill some editing occurs).

  (let ((file-name-buffer-file-type-alist nil))    ; Emacs on DOS/NT
    (if replace
      (let ((buffer-file-name filename)
          (buffer-file-truename (abbreviate-file-name (file-truename filename))))
        (revert-buffer  nil t t)
      (set-buffer-modified-p t))
      (let ((template-auto-insert nil))
    (switch-to-buffer (find-file-noselect filename))))))

We both agree with Christoph that this code is hacky and is probably not very portable and future-proof. I am calling for your expertise: what would be the correct way to do this ?

I am considering another trick that would basically consist in saving the target file, reopen it, and then replace all the CR by an empty string, but I don't like it either, because in my opinion the buffer for target file shall not be saved until the user request it (although in the template creation process, the existence of the target file is already tested at this point, and the user already has accepted to overwrite it ...)

Very best regards,


Vous voulez savoir ce que vous pouvez faire avec le nouveau Windows Live ? Lancez-vous !

reply via email to

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