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

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

Re: How to modify 'write-file'


From: richard.christensen
Subject: Re: How to modify 'write-file'
Date: Tue, 04 May 2010 15:44:33 -0000
User-agent: G2/1.0

Thanks.  This works great.

Richard.

On Apr 13, 6:37 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> "richard.christensen" <richard.christen...@avagotech.com> writes:
> > Hi Pascal,
>
> > I am sorry for being thick.  I have tried several things and have
> > tried to do some
> > reading about how to implement this code.  I can't get anything to
> > work. I can
> > see what appears to be the correct words on the minibuffer, but upon
> > hitting enter,
> > either nothing happens or I get an error about wrong number of
> > arguments.
>
> > Should I be doing this (defun write-file (arg) or should I be creating
> > a new function?
> > I see that a list is created but I am sure the write-file does not
> > want the "Write file:" text
> > to be part of the operation.  So, some how I need to re-direct the
> > path and file name from the list
> > back to the write-function.  Am I missing anything else?
>
> Sorry, I assumed you were already a seasoned emacs lisp programmer.
> Here are detailed instructions:
>
> 1- Find the sources of write-file, type:
>      C-h f write-file RET    C-x o   TAB    RET
>
> 2- Copy and paste the defun write-file form to the *scratch* buffer:
>      C-SPC C-M-f M-w  C-x b *scratch* RET  RET C-y  C-M-b
>
> You get this in the *scratch* buffer:
>
> (defun write-file (filename &optional confirm)
>   "Write current buffer into file FILENAME.
> This makes the buffer visit that file, and marks it as not modified.
>
> If you specify just a directory name as FILENAME, that means to use
> the default file name but in that directory.  You can also yank
> the default file name into the minibuffer to edit it, using 
> \\<minibuffer-local-map>\\[next-history-element].
>
> If the buffer is not already visiting a file, the default file name
> for the output file is the buffer name.
>
> If optional second arg CONFIRM is non-nil, this function
> asks for confirmation before overwriting an existing file.
> Interactively, confirmation is required unless you supply a prefix argument."
> ;;  (interactive "FWrite file: ")
>   (interactive
>    (list (if buffer-file-name
>              (read-file-name "Write file: "
>                              nil nil nil nil)
>            (read-file-name "Write file: " default-directory
>                            (expand-file-name
>                             (file-name-nondirectory (buffer-name))
>                             default-directory)
>                            nil nil))
>          (not current-prefix-arg)))
>   (or (null filename) (string-equal filename "")
>       (progn
>         ;; If arg is just a directory,
>         ;; use the default file name, but in that directory.
>         (if (file-directory-p filename)
>             (setq filename (concat (file-name-as-directory filename)
>                                    (file-name-nondirectory
>                                     (or buffer-file-name (buffer-name))))))
>         (and confirm
>              (file-exists-p filename)
>              (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
>                  (error "Canceled")))
>         (set-visited-file-name filename (not confirm))))
>   (set-buffer-modified-p t)
>   ;; Make buffer writable if file is writable.
>   (and buffer-file-name
>        (file-writable-p buffer-file-name)
>        (setq buffer-read-only nil))
>   (save-buffer)
>   ;; It's likely that the VC status at the new location is different from
>   ;; the one at the old location.
>   (vc-find-file-hook))
>
> 3- Replace the interactive form in this defun by the one I provided:
>
>     (interactive
>      (list (if buffer-file-name
>                (read-file-name "Write file: " default-directory
>                                (expand-file-name
>                                 (file-name-nondirectory (buffer-name))
>                                 default-directory)
>                                nil
>                                 (buffer-name))
>                (read-file-name "Write file: " default-directory
>                                (expand-file-name
>                                 (file-name-nondirectory (buffer-name))
>                                 default-directory)
>                                nil nil))
>            (not current-prefix-arg)))
>
> so that you get:
>
> (defun write-file (filename &optional confirm)
>   "Write current buffer into file FILENAME.
> This makes the buffer visit that file, and marks it as not modified.
>
> If you specify just a directory name as FILENAME, that means to use
> the default file name but in that directory.  You can also yank
> the default file name into the minibuffer to edit it, using 
> \\<minibuffer-local-map>\\[next-history-element].
>
> If the buffer is not already visiting a file, the default file name
> for the output file is the buffer name.
>
> If optional second arg CONFIRM is non-nil, this function
> asks for confirmation before overwriting an existing file.
> Interactively, confirmation is required unless you supply a prefix argument."
>   (interactive
>      (list (if buffer-file-name
>                (read-file-name "Write file: " default-directory
>                                (expand-file-name
>                                 (file-name-nondirectory (buffer-name))
>                                 default-directory)
>                                nil
>                                 (buffer-name))
>                (read-file-name "Write file: " default-directory
>                                (expand-file-name
>                                 (file-name-nondirectory (buffer-name))
>                                 default-directory)
>                                nil nil))
>            (not current-prefix-arg)))
>   (or (null filename) (string-equal filename "")
>       (progn
>         ;; If arg is just a directory,
>         ;; use the default file name, but in that directory.
>         (if (file-directory-p filename)
>             (setq filename (concat (file-name-as-directory filename)
>                                    (file-name-nondirectory
>                                     (or buffer-file-name (buffer-name))))))
>         (and confirm
>              (file-exists-p filename)
>              (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
>                  (error "Canceled")))
>         (set-visited-file-name filename (not confirm))))
>   (set-buffer-modified-p t)
>   ;; Make buffer writable if file is writable.
>   (and buffer-file-name
>        (file-writable-p buffer-file-name)
>        (setq buffer-read-only nil))
>   (save-buffer)
>   ;; It's likely that the VC status at the new location is different from
>   ;; the one at the old location.
>   (vc-find-file-hook))
>
> 4- Evaluate this defun, for example positionning the cursor after it,
>    and typing C-x C-e.
>
> 5- Use the new write-file.
>
> If you want to keep this version of write-file, you can copy-and-paste
> it into your ~/.emacs file.
>
> --
> __Pascal Bourguignon__



reply via email to

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