From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Richard Stallman Newsgroups: gmane.emacs.devel Subject: Re: patch: write-file to arbitrary target directory Date: Wed, 21 Nov 2007 21:28:05 -0500 Message-ID: References: <86r6ip7k4x.fsf@nirvana.pusto.de> <861waobh7m.fsf@nirvana.pusto.de> <863av3ays2.fsf@nirvana.pusto.de> Reply-To: rms@gnu.org NNTP-Posting-Host: lo.gmane.org Content-Type: text/plain; charset=ISO-8859-15 X-Trace: ger.gmane.org 1195698656 3022 80.91.229.12 (22 Nov 2007 02:30:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 22 Nov 2007 02:30:56 +0000 (UTC) Cc: emacs-devel@gnu.org To: Eduard Wiebe Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Nov 22 03:31:02 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Iv1qH-0005u0-5m for ged-emacs-devel@m.gmane.org; Thu, 22 Nov 2007 03:31:01 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Iv1q2-0002AV-RP for ged-emacs-devel@m.gmane.org; Wed, 21 Nov 2007 21:30:46 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Iv1nU-0000m8-TS for emacs-devel@gnu.org; Wed, 21 Nov 2007 21:28:09 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Iv1nT-0000l5-18 for emacs-devel@gnu.org; Wed, 21 Nov 2007 21:28:07 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Iv1nS-0000kj-OJ for emacs-devel@gnu.org; Wed, 21 Nov 2007 21:28:06 -0500 Original-Received: from fencepost.gnu.org ([140.186.70.10]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Iv1nS-0000zW-DI for emacs-devel@gnu.org; Wed, 21 Nov 2007 21:28:06 -0500 Original-Received: from rms by fencepost.gnu.org with local (Exim 4.60) (envelope-from ) id 1Iv1nR-0003uz-Sz; Wed, 21 Nov 2007 21:28:05 -0500 In-reply-to: <863av3ays2.fsf@nirvana.pusto.de> (message from Eduard Wiebe on Mon, 19 Nov 2007 01:01:49 +0100) X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:83827 Archived-At: > And if DIR is nil, read-file-name use 'default-directory' >anyway. (If i am not mistaken.) > > Please double-check. It is so. Compare itself: Ok, I agree. Is anyone opposed to installing this patch? I attached a new patch of function with some additional comments, and for manual/docs. Please have a look a this. Index: doc/emacs/files.texi =================================================================== RCS file: /sources/emacs/emacs/doc/emacs/files.texi,v retrieving revision 1.14 diff -u -r1.14 files.texi --- doc/emacs/files.texi 20 Oct 2007 04:24:25 -0000 1.14 +++ doc/emacs/files.texi 18 Nov 2007 22:50:17 -0000 @@ -492,9 +492,10 @@ (except that @kbd{C-x C-w} asks for confirmation if the file exists). @kbd{C-x C-s} used on a buffer that is not visiting a file has the same effect as @kbd{C-x C-w}; that is, it reads a file name, marks the -buffer as visiting that file, and saves it there. The default file name in -a buffer that is not visiting a file is made by combining the buffer name -with the buffer's default directory (@pxref{File Names}). +buffer as visiting that file, and saves it there. Is directory of file +does not exist, you are asked for creating them. The default file +name in a buffer that is not visiting a file is made by combining the +buffer name with the buffer's default directory (@pxref{File Names}). If the new file name implies a major mode, then @kbd{C-x C-w} switches to that major mode, in most cases. The command Index: doc/lispref/files.texi =================================================================== RCS file: /sources/emacs/emacs/doc/lispref/files.texi,v retrieving revision 1.2 diff -u -r1.2 files.texi --- doc/lispref/files.texi 6 Sep 2007 04:27:42 -0000 1.2 +++ doc/lispref/files.texi 18 Nov 2007 22:50:40 -0000 @@ -355,12 +355,13 @@ @end deffn @deffn Command write-file filename &optional confirm -@anchor{Definition of write-file} -This function writes the current buffer into file @var{filename}, makes -the buffer visit that file, and marks it not modified. Then it renames -the buffer based on @var{filename}, appending a string like @samp{<2>} -if necessary to make a unique buffer name. It does most of this work by -calling @code{set-visited-file-name} (@pxref{Buffer File Name}) and +@anchor{Definition of write-file} +This function writes the current buffer into file @var{filename}, +thereby creates all parent directories if necessary, makes the buffer +visit that file, and marks it not modified. Then it renames the buffer +based on @var{filename}, appending a string like @samp{<2>} if necessary +to make a unique buffer name. It does most of this work by calling +@code{set-visited-file-name} (@pxref{Buffer File Name}) and @code{save-buffer}. If @var{confirm} is non-@code{nil}, that means to ask for confirmation @@ -370,7 +371,8 @@ If @var{filename} is an existing directory, or a symbolic link to one, @code{write-file} uses the name of the visited file, in directory @var{filename}. If the buffer is not visiting a file, it uses the -buffer name instead. +buffer name instead. Interactively, the user is asked for +creating of all parent directories of output file. @end deffn Saving a buffer runs several hooks. It also performs format Index: lisp/files.el =================================================================== RCS file: /sources/emacs/emacs/lisp/files.el,v retrieving revision 1.941 diff -u -r1.941 files.el --- lisp/files.el 16 Nov 2007 08:03:45 -0000 1.941 +++ lisp/files.el 18 Nov 2007 22:53:04 -0000 @@ -3075,8 +3075,10 @@ the default file name but in that directory. You 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 the buffer is not already visiting a file, the default file +name for the output file is the buffer name. Are parent +directories of output file not existent, the functon asks user +for creating those. Noninteractively this happens by default. If optional second arg CONFIRM is non-nil, this function asks for confirmation before overwriting an existing file. @@ -3086,14 +3088,25 @@ (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)) + ;; If buffer name has directory parts, propose this + ;; directory path for writing. Otherwise DIR is nil and we + ;; use `default-directory' by default. + (let ((dir (file-name-directory (buffer-name))) + (file (file-name-nondirectory (buffer-name)))) + (read-file-name "Write file: " + dir (expand-file-name file dir) nil nil))) (not current-prefix-arg))) (or (null filename) (string-equal filename "") (progn + ;; If directory of file is not existent, create it with all parents. + (let ((dir (file-name-directory filename))) + (when (and dir (not (file-exists-p dir))) + (and (interactive-p) + (or (y-or-n-p + (format "Directory `%s' does not exist; create? " dir)) + (error "Canceled"))) + (make-directory dir 'parents))) + ;; If arg is just a directory, ;; use the default file name, but in that directory. (if (file-directory-p filename) Index: src/fileio.c =================================================================== RCS file: /sources/emacs/emacs/src/fileio.c,v retrieving revision 1.594 diff -u -r1.594 fileio.c --- src/fileio.c 21 Oct 2007 10:53:16 -0000 1.594 +++ src/fileio.c 18 Nov 2007 22:54:52 -0000 @@ -6349,6 +6349,7 @@ DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 6, 0, doc: /* Read file name, prompting with PROMPT and completing in directory DIR. Value is not expanded---you must call `expand-file-name' yourself. +(If DIR is omitted or nil, the `default-directory' is used.) Default name to DEFAULT-FILENAME if user exits the minibuffer with the same non-empty string that was inserted by this function. (If DEFAULT-FILENAME is omitted, the visited file name is used, -- Eduard Wiebe