unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "richard.christensen" <richard.christensen@avagotech.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to modify 'write-file'
Date: Wed, 14 Apr 2010 16:35:36 -0700 (PDT)	[thread overview]
Message-ID: <a514f37c-69de-4ad6-a694-0bb87a1141e4@g30g2000yqc.googlegroups.com> (raw)
In-Reply-To: 87mxx698dq.fsf@galatea.lan.informatimago.com

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	other threads:[~2010-04-14 23:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-10 17:17 How to modify 'write-file' richard.christensen
2010-04-10 18:00 ` Pascal J. Bourguignon
2010-04-11 18:16   ` richard.christensen
2010-04-11 23:29   ` richard.christensen
2010-04-11 23:36     ` Pascal J. Bourguignon
2010-04-13 14:43       ` richard.christensen
2010-04-14  0:37         ` Pascal J. Bourguignon
2010-04-14 23:35           ` richard.christensen [this message]
2010-05-07  0:02           ` Kevin Rodgers
2010-05-07  5:30             ` Thierry Volpiatto
2010-04-10 18:19 ` Andreas Politz
2010-04-11 18:17   ` richard.christensen
2010-04-11 22:43     ` Andreas Politz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a514f37c-69de-4ad6-a694-0bb87a1141e4@g30g2000yqc.googlegroups.com \
    --to=richard.christensen@avagotech.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).