From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "richard.christensen" Newsgroups: gmane.emacs.help Subject: Re: How to modify 'write-file' Date: Wed, 14 Apr 2010 16:35:36 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <87ochrgpbd.fsf@galatea.lan.informatimago.com> <9f65fba7-3fba-40b6-ad86-75aa4f336b91@g11g2000yqe.googlegroups.com> <87bpdpftpa.fsf@galatea.lan.informatimago.com> <9345ad4d-220c-4319-9e2f-d5acfa340d27@35g2000yqm.googlegroups.com> <87mxx698dq.fsf@galatea.lan.informatimago.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1273060099 9687 80.91.229.12 (5 May 2010 11:48:19 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 5 May 2010 11:48:19 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed May 05 13:48:18 2010 connect(): No such file or directory Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1O9d5K-0004K0-BQ for geh-help-gnu-emacs@m.gmane.org; Wed, 05 May 2010 13:48:14 +0200 Original-Received: from localhost ([127.0.0.1]:33284 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O9d5J-0007cw-DP for geh-help-gnu-emacs@m.gmane.org; Wed, 05 May 2010 07:48:13 -0400 Original-Path: usenet.stanford.edu!postnews.google.com!g30g2000yqc.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 201 Original-NNTP-Posting-Host: 199.3.246.231 Original-X-Trace: posting.google.com 1271288137 12213 127.0.0.1 (14 Apr 2010 23:35:37 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Wed, 14 Apr 2010 23:35:37 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: g30g2000yqc.googlegroups.com; posting-host=199.3.246.231; posting-account=1ibdcgoAAABow1tT8596gBIVrwKkiml- User-Agent: G2/1.0 X-HTTP-Via: 1.1 ftcwebcache2.ame.avagotech.net:8088 (IronPort-WSA/6.3.0-523) X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729), gzip(gfe) Original-Xref: usenet.stanford.edu gnu.emacs.help:177698 X-Mailman-Approved-At: Tue, 04 May 2010 16:53:46 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:73397 Archived-At: Thanks. This works great. Richard. On Apr 13, 6:37=A0pm, p...@informatimago.com (Pascal J. Bourguignon) wrote: > "richard.christensen" writes: > > Hi Pascal, > > > I am sorry for being thick. =A0I have tried several things and have > > tried to do some > > reading about how to implement this code. =A0I 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. =A0So, some how I need to re-direct the > > path and file name from the list > > back to the write-function. =A0Am 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: > =A0 =A0 =A0C-h f write-file RET =A0 =A0C-x o =A0 TAB =A0 =A0RET > > 2- Copy and paste the defun write-file form to the *scratch* buffer: > =A0 =A0 =A0C-SPC C-M-f M-w =A0C-x b *scratch* RET =A0RET C-y =A0C-M-b > > You get this in the *scratch* buffer: > > (defun write-file (filename &optional confirm) > =A0 "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. =A0You can also yank > the default file name into the minibuffer to edit it, using \\\\[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 argume= nt." > ;; =A0(interactive "FWrite file: ") > =A0 (interactive > =A0 =A0(list (if buffer-file-name > =A0 =A0 =A0 =A0 =A0 =A0 =A0(read-file-name "Write file: " > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil nil nil ni= l) > =A0 =A0 =A0 =A0 =A0 =A0(read-file-name "Write file: " default-directory > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(expand-file-name > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (file-name-nondir= ectory (buffer-name)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 default-directory= ) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil nil)) > =A0 =A0 =A0 =A0 =A0(not current-prefix-arg))) > =A0 (or (null filename) (string-equal filename "") > =A0 =A0 =A0 (progn > =A0 =A0 =A0 =A0 ;; If arg is just a directory, > =A0 =A0 =A0 =A0 ;; use the default file name, but in that directory. > =A0 =A0 =A0 =A0 (if (file-directory-p filename) > =A0 =A0 =A0 =A0 =A0 =A0 (setq filename (concat (file-name-as-directory fi= lename) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(f= ile-name-nondirectory > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (= or buffer-file-name (buffer-name)))))) > =A0 =A0 =A0 =A0 (and confirm > =A0 =A0 =A0 =A0 =A0 =A0 =A0(file-exists-p filename) > =A0 =A0 =A0 =A0 =A0 =A0 =A0(or (y-or-n-p (format "File `%s' exists; overw= rite? " filename)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(error "Canceled"))) > =A0 =A0 =A0 =A0 (set-visited-file-name filename (not confirm)))) > =A0 (set-buffer-modified-p t) > =A0 ;; Make buffer writable if file is writable. > =A0 (and buffer-file-name > =A0 =A0 =A0 =A0(file-writable-p buffer-file-name) > =A0 =A0 =A0 =A0(setq buffer-read-only nil)) > =A0 (save-buffer) > =A0 ;; It's likely that the VC status at the new location is different fr= om > =A0 ;; the one at the old location. > =A0 (vc-find-file-hook)) > > 3- Replace the interactive form in this defun by the one I provided: > > =A0 =A0 (interactive > =A0 =A0 =A0(list (if buffer-file-name > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(read-file-name "Write file: " default-dir= ectory > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(expand-fi= le-name > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (file-nam= e-nondirectory (buffer-name)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 default-d= irectory) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (buffer-n= ame)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(read-file-name "Write file: " default-dir= ectory > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(expand-fi= le-name > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (file-nam= e-nondirectory (buffer-name)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 default-d= irectory) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil nil)) > =A0 =A0 =A0 =A0 =A0 =A0(not current-prefix-arg))) > > so that you get: > > (defun write-file (filename &optional confirm) > =A0 "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. =A0You can also yank > the default file name into the minibuffer to edit it, using \\\\[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 argume= nt." > =A0 (interactive > =A0 =A0 =A0(list (if buffer-file-name > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(read-file-name "Write file: " default-dir= ectory > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(expand-fi= le-name > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (file-nam= e-nondirectory (buffer-name)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 default-d= irectory) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (buffer-n= ame)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(read-file-name "Write file: " default-dir= ectory > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(expand-fi= le-name > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (file-nam= e-nondirectory (buffer-name)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 default-d= irectory) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil nil)) > =A0 =A0 =A0 =A0 =A0 =A0(not current-prefix-arg))) > =A0 (or (null filename) (string-equal filename "") > =A0 =A0 =A0 (progn > =A0 =A0 =A0 =A0 ;; If arg is just a directory, > =A0 =A0 =A0 =A0 ;; use the default file name, but in that directory. > =A0 =A0 =A0 =A0 (if (file-directory-p filename) > =A0 =A0 =A0 =A0 =A0 =A0 (setq filename (concat (file-name-as-directory fi= lename) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(f= ile-name-nondirectory > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (= or buffer-file-name (buffer-name)))))) > =A0 =A0 =A0 =A0 (and confirm > =A0 =A0 =A0 =A0 =A0 =A0 =A0(file-exists-p filename) > =A0 =A0 =A0 =A0 =A0 =A0 =A0(or (y-or-n-p (format "File `%s' exists; overw= rite? " filename)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(error "Canceled"))) > =A0 =A0 =A0 =A0 (set-visited-file-name filename (not confirm)))) > =A0 (set-buffer-modified-p t) > =A0 ;; Make buffer writable if file is writable. > =A0 (and buffer-file-name > =A0 =A0 =A0 =A0(file-writable-p buffer-file-name) > =A0 =A0 =A0 =A0(setq buffer-read-only nil)) > =A0 (save-buffer) > =A0 ;; It's likely that the VC status at the new location is different fr= om > =A0 ;; the one at the old location. > =A0 (vc-find-file-hook)) > > 4- Evaluate this defun, for example positionning the cursor after it, > =A0 =A0and 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__