emacs-devel
[Top][All Lists]
Advanced

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

Re: mode setting question


From: Stefan Monnier
Subject: Re: mode setting question
Date: Tue, 14 Jul 2009 20:47:52 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.94 (gnu/linux)

>> I would like to set the mode AS IF the visited
>> file name were "hw.c", without that actually being
>> the visited file name.  That is, if the contents
>> include automode foo within the buffer, use that.
>> If not, consult the auto-mode-alist using hw.c
>> and use that.
>> 
>> What is the clean way to do so?

> Ask here ;-)

> This is a thing I have asked for several times. The code in
> normal-mode should be broken up so that it is possible to ask "what
> mode is the normal modes for files named nn.ext".

Agreed.
See below an example of how I hacked around this problem in PCL-CVS.


        Stefan


(defun cvs-retrieve-revision (fileinfo rev)
  "Retrieve the given REVision of the file in FILEINFO into a new buffer."
  (let* ((file (cvs-fileinfo->full-name fileinfo))
         (buffile (concat file "." rev)))
    (or (find-buffer-visiting buffile)
        (with-current-buffer (create-file-buffer buffile)
          (message "Retrieving revision %s..." rev)
          ;; Discard stderr output to work around the CVS+SSH+libc
          ;; problem when stdout and stderr are the same.
          (let ((res
                 (let ((coding-system-for-read 'binary))
                   (apply 'process-file cvs-program nil '(t nil) nil
                          "-q" "update" "-p"
                          ;; If `rev' is HEAD, don't pass it at all:
                          ;; the default behavior is to get the head
                          ;; of the current branch whereas "-r HEAD"
                          ;; stupidly gives you the head of the trunk.
                          (append (unless (equal rev "HEAD") (list "-r" rev))
                                  (list file))))))
            (when (and res (not (and (equal 0 res))))
              (error "Something went wrong retrieving revision %s: %s" rev res))
            ;; Figure out the encoding used and decode the byte-sequence
            ;; into a sequence of chars.
            (decode-coding-inserted-region
             (point-min) (point-max) file t nil nil t)
            ;; Set buffer-file-coding-system.
            (after-insert-file-set-coding (buffer-size) t)
            (set-buffer-modified-p nil)
            (let ((buffer-file-name (expand-file-name file)))
              (after-find-file))
            (toggle-read-only 1)
            (message "Retrieving revision %s... Done" rev)
            (current-buffer))))))




reply via email to

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