emacs-devel
[Top][All Lists]
Advanced

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

Re: patch: write-file to arbitrary target directory


From: Richard Stallman
Subject: Re: patch: write-file to arbitrary target directory
Date: Sun, 02 Dec 2007 16:27:06 -0500

About two weeks ago I asked if anyone was opposed to this patch.
I did not get any criticisms.  Would someone please install this and ack?


Eduard Wiebe wrote:

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
address@hidden 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
address@hidden 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
address@hidden (@pxref{Buffer File Name}) and
 @code{save-buffer}.
 
 If @var{confirm} is address@hidden, 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 
\\<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 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





reply via email to

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