From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.bugs Subject: bug#28834: 25.1; dired-do-copy: allow to copy into a nonexistent directory Date: Tue, 17 Oct 2017 13:49:34 +0900 Message-ID: <87376i2xb5.fsf@gmail.com> References: <006201d344da$d9316a50$8b943ef0$@gmail.com> <87shel2f93.fsf@gmail.com> <329916c9-5a00-4460-83db-f16b98e79edb@default> <87mv4r3gat.fsf@gmail.com> <83o9p7f4wz.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1508216201 15346 195.159.176.226 (17 Oct 2017 04:56:41 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 17 Oct 2017 04:56:41 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: 28834@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Oct 17 06:56:36 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e4JvR-0001Uj-JB for geb-bug-gnu-emacs@m.gmane.org; Tue, 17 Oct 2017 06:56:21 +0200 Original-Received: from localhost ([::1]:36538 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4JvY-0004gL-Se for geb-bug-gnu-emacs@m.gmane.org; Tue, 17 Oct 2017 00:56:28 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58217) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4JpO-0007hH-7K for bug-gnu-emacs@gnu.org; Tue, 17 Oct 2017 00:50:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4JpK-0006cM-Vf for bug-gnu-emacs@gnu.org; Tue, 17 Oct 2017 00:50:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:35930) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e4JpK-0006cC-RW for bug-gnu-emacs@gnu.org; Tue, 17 Oct 2017 00:50:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1e4JpK-0003sO-6S for bug-gnu-emacs@gnu.org; Tue, 17 Oct 2017 00:50:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Tino Calancha Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 17 Oct 2017 04:50:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 28834 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 28834-submit@debbugs.gnu.org id=B28834.150821579014879 (code B ref 28834); Tue, 17 Oct 2017 04:50:02 +0000 Original-Received: (at 28834) by debbugs.gnu.org; 17 Oct 2017 04:49:50 +0000 Original-Received: from localhost ([127.0.0.1]:44611 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e4Jp7-0003ru-Og for submit@debbugs.gnu.org; Tue, 17 Oct 2017 00:49:50 -0400 Original-Received: from mail-pg0-f54.google.com ([74.125.83.54]:57172) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e4Jp6-0003rg-AB for 28834@debbugs.gnu.org; Tue, 17 Oct 2017 00:49:48 -0400 Original-Received: by mail-pg0-f54.google.com with SMTP id m18so483879pgd.13 for <28834@debbugs.gnu.org>; Mon, 16 Oct 2017 21:49:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:in-reply-to:references:user-agent:date :message-id:mime-version; bh=EhM1ag+17Je0ze3YEHOOik0tegeTrFwH0V7XZ9tmLdk=; b=B48Y0E6RkCqYRHgAO8bw7JSX2qIEBDZt5TH2qe2JxGpAsLJ24y5AFwvVOviuNAFw/S VzSztK4RKRnB4POaI57y8+SbCNr0Z9ZgBqjqlTpE4sP6yhSykPF9rZu8VIJZBYNxoMYT HfSMlVtqq1nDrYTxE8A2uYoKUHiPB4GttD/Ig9ItgBsRw8MiYF9J7aldCfqOHlXMYcpg VHusCH5IAVVBsEiWZg7cMrKZW0XWIRfC+J3PmNEJOer1KHpf3OZP0eS2CORVwxAB/rJm 9BoNf+3UQckt2Abw+dObtRcBaH/hlKKyJYpeaIIIXjjQGtjrUGDivv8cRIyj6+LFUwxx d+HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references :user-agent:date:message-id:mime-version; bh=EhM1ag+17Je0ze3YEHOOik0tegeTrFwH0V7XZ9tmLdk=; b=cHmMvKj4nP03K3Oo/3y9pMzIlRhlE/LQJ5Td1XVqCr3equluEi5wxHKfFYKGplzunB arvki1R0n9o2Wz72QoklPikXMSmYgCSiBL/21OfGQJsawAzwq8vBodqa4qSvzt9cIrk/ die0T5RH6IhedYSh4gKCUfesyKCL26zyxgdUuYv7krISCzd6/2XLe3DXKP5XDKaV1sey uETKC87lceV/Bc/GOsDfiBnv1YsTitYzENsmc/eix6BGUh1wrwNGBpaIbBZPT06jRchT U2rDpLOGjDomutxbyP0XS1fOVA4BTfVN0Bbnak4rSjWryVE3FciY3wU8+xQamKYTmg44 BYew== X-Gm-Message-State: AMCzsaWz0yAGoTwEFwrUGrJljG3yXVu3ZUYAzHxU6af5Mav9RZT+HkRd vfcVPeqsTgTkbs+LeReRI/s= X-Google-Smtp-Source: AOwi7QBXikNy7LFdMm5AFsNHn1Ld5jeQSHHobdhobarreCfTaOvL/Z+IP531mDrj5pUoAW5SHtiT8g== X-Received: by 10.98.163.196 with SMTP id q65mr10556816pfl.7.1508215782116; Mon, 16 Oct 2017 21:49:42 -0700 (PDT) Original-Received: from calancha-pc (203.191.218.133.dy.bbexcite.jp. [133.218.191.203]) by smtp.gmail.com with ESMTPSA id m195sm17442292pga.13.2017.10.16.21.49.40 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Oct 2017 21:49:41 -0700 (PDT) In-Reply-To: <83o9p7f4wz.fsf@gnu.org> (Eli Zaretskii's message of "Mon, 16 Oct 2017 19:12:12 +0300") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:138578 Archived-At: Eli Zaretskii writes: Thank you for the comments. * I have changed the new function name to `dired-maybe-create-dirs' as Drew suggested. * I think I have addressed all your comments as well. >> +The option @var{dired-create-destination-dirs} controls whether Dired >> +should create non-existent directories in @var{new}. > > This sentence is redundant (repeated right after it), and also uses > @var incorrectly. Removed that entence. >> +@videnx dired-create-destination-dirs > ^^^^^^^ > A typo. Fixed. >> +means Dired automatically creates them; the value @code{prompt} >> +means Dired asks you for confirmation before creating them. > > I think we generally use 'ask', not 'prompt' in these cases. Changed to 'ask. >> +** Dired >> + >> +--- > > Since you've updated the manual, this should be "+++", not "---". OK. >> +'dired-do-copy' and 'dired-rename-file' must create non-existent > ^^^^ > "Should", not "must". OK. >> +If never, don't create them. >> +If always, create them without ask. >> +If prompt, ask for user confirmation." > > The symbols should be quoted: `never', `always', etc. OK. > Btw, perhaps it's better to use nil instead of 'never'. Changed never ---> nil. >> +(defun dired--create-dirs (dir) >> + (unless (file-exists-p dir) >> + (pcase dired-create-destination-dirs >> + ('never nil) >> + ('always (dired-create-directory dir)) >> + ('prompt >> + (when (yes-or-no-p (format "Create destination dir '%s'? " dir)) >> + (dired-create-directory dir)))))) > > Is use of pcase really justified here? I rewrote the function: now it loooks more simple. >> +(ert-deftest dired-test-bug28834 () >> + "test for https://debbugs.gnu.org/28834 ." > This doesn't test the 3rd value. Why is that? Laziness? Bad memory? Busy? Stomach problems? Other not listed here? Updated the test: now it run all cases. --8<-----------------------------cut here---------------start------------->8--- commit fb91d159f241a6eeecc41c4101472f145a00c0d1 Author: Tino Calancha Date: Tue Oct 17 13:48:59 2017 +0900 Allow to copy/rename file into a non-existent dir * lisp/dired-aux.el (dired-create-destination-dirs): New option. (dired-maybe-create-dirs): New defun. (dired-copy-file-recursive, dired-rename-file): Use it (Bug#28834). * lisp/dired-aux-tests.el (dired-test-bug28834): Add test. * doc/emacs/dired.texi (Operating on Files): Update manual. * etc/NEWS (Changes in Specialized Modes and Packages in Emacs 27.1) Announce this change. diff --git a/doc/emacs/dired.texi b/doc/emacs/dired.texi index db5dea329b..9348ef5042 100644 --- a/doc/emacs/dired.texi +++ b/doc/emacs/dired.texi @@ -647,6 +647,14 @@ Operating on Files is the directory to copy into, or (if copying a single file) the new name. This is like the shell command @code{cp}. +@vindex dired-create-destination-dirs +The option @code{dired-create-destination-dirs} controls whether Dired +should create non-existent directories in the destination while +copying/renaming files. The default value @code{nil} means Dired +never creates such missing directories; the value @code{always}, +means Dired automatically creates them; the value @code{ask} +means Dired asks you for confirmation before creating them. + @vindex dired-copy-preserve-time If @code{dired-copy-preserve-time} is non-@code{nil}, then copying with this command preserves the modification time of the old file in @@ -678,6 +686,9 @@ Operating on Files you rename several files, the argument @var{new} is the directory into which to move the files (this is like the shell command @command{mv}). +The option @code{dired-create-destination-dirs} controls whether Dired +should create non-existent directories in @var{new}. + Dired automatically changes the visited file name of buffers associated with renamed files so that they refer to the new names. diff --git a/etc/NEWS b/etc/NEWS index 716b0309a5..acfc52ab52 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -56,6 +56,13 @@ whether '"' is also replaced in 'electric-quote-mode'. If non-nil, * Changes in Specialized Modes and Packages in Emacs 27.1 +** Dired + ++++ +*** The new user option 'dired-create-destination-dirs' controls whether +'dired-do-copy' and 'dired-rename-file' should create non-existent +directories in the destination. + ** Edebug +++ diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index 7e2252fcf1..5e92c58d12 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -1548,6 +1548,24 @@ dired-copy-file (declare-function make-symbolic-link "fileio.c") +(defcustom dired-create-destination-dirs nil + "Whether Dired should create destination dirs when copying/removing files. +If nil, don't create them. +If `always', create them without ask. +If `ask', ask for user confirmation." + :type '(choice (const :tag "Never create non-existent dirs" nil) + (const :tag "Always create non-existent dirs" always) + (const :tag "Ask for user confirmation" ask)) + :group 'dired + :version "27.1") + +(defun dired-maybe-create-dirs (dir) + "Create DIR if doesn't exist according with `dired-create-destination-dirs'." + (when (and dired-create-destination-dirs (not (file-exists-p dir))) + (if (or (eq dired-create-destination-dirs 'always) + (yes-or-no-p (format "Create destination dir `%s'? " dir))) + (dired-create-directory dir)))) + (defun dired-copy-file-recursive (from to ok-flag &optional preserve-time top recursive) (when (and (eq t (car (file-attributes from))) @@ -1564,6 +1582,7 @@ dired-copy-file-recursive (if (stringp (car attrs)) ;; It is a symlink (make-symbolic-link (car attrs) to ok-flag) + (dired-maybe-create-dirs (file-name-directory to)) (copy-file from to ok-flag preserve-time)) (file-date-error (push (dired-make-relative from) @@ -1573,6 +1592,7 @@ dired-copy-file-recursive ;;;###autoload (defun dired-rename-file (file newname ok-if-already-exists) (dired-handle-overwrite newname) + (dired-maybe-create-dirs (file-name-directory newname)) (rename-file file newname ok-if-already-exists) ; error is caught in -create-files ;; Silently rename the visited file of any buffer visiting this file. (and (get-file-buffer file) diff --git a/test/lisp/dired-aux-tests.el b/test/lisp/dired-aux-tests.el index d41feb1592..9316217dd2 100644 --- a/test/lisp/dired-aux-tests.el +++ b/test/lisp/dired-aux-tests.el @@ -20,7 +20,7 @@ ;;; Code: (require 'ert) (require 'dired-aux) - +(eval-when-compile (require 'cl-lib)) (ert-deftest dired-test-bug27496 () "Test for https://debbugs.gnu.org/27496 ." @@ -40,5 +40,59 @@ (should-not (dired-do-shell-command "ls ? ./`?`" nil files))) (delete-file foo)))) +;; Auxiliar macro for `dired-test-bug28834': it binds +;; `dired-create-destination-dirs' to CREATE-DIRS and execute BODY. +;; If YES-OR-NO is non-nil, it binds `yes-or-no-p' to +;; to avoid the prompt. +(defmacro with-dired-bug28834-test (create-dirs yes-or-no &rest body) + (declare ((debug form symbolp body))) + (let ((foo (make-symbol "foo"))) + `(let* ((,foo (make-temp-file "foo" 'dir)) + (dired-create-destination-dirs ,create-dirs)) + (setq from (make-temp-file "from")) + (setq to-cp + (expand-file-name + "foo-cp" (file-name-as-directory (expand-file-name "bar" ,foo)))) + (setq to-mv + (expand-file-name + "foo-mv" (file-name-as-directory (expand-file-name "qux" ,foo)))) + (unwind-protect + (if ,yes-or-no + (cl-letf (((symbol-function 'yes-or-no-p) + (lambda (prompt) (eq ,yes-or-no 'yes)))) + ,@body) + ,@body) + ;; clean up + (delete-directory ,foo 'recursive) + (delete-file from))))) + +(ert-deftest dired-test-bug28834 () + "test for https://debbugs.gnu.org/28834 ." + (let (from to-cp to-mv) + ;; `dired-create-destination-dirs' set to 'always. + (with-dired-bug28834-test + 'always nil + (dired-copy-file-recursive from to-cp nil) + (should (file-exists-p to-cp)) + (dired-rename-file from to-mv nil) + (should (file-exists-p to-mv))) + ;; `dired-create-destination-dirs' set to nil. + (with-dired-bug28834-test + nil nil + (should-error (dired-copy-file-recursive from to-cp nil)) + (should-error (dired-rename-file from to-mv nil))) + ;; `dired-create-destination-dirs' set to 'ask. + (with-dired-bug28834-test + 'ask 'yes ; Answer `yes' + (dired-copy-file-recursive from to-cp nil) + (should (file-exists-p to-cp)) + (dired-rename-file from to-mv nil) + (should (file-exists-p to-mv))) + (with-dired-bug28834-test + 'ask 'no ; Answer `no' + (should-error (dired-copy-file-recursive from to-cp nil)) + (should-error (dired-rename-file from to-mv nil))))) + + (provide 'dired-aux-tests) ;; dired-aux-tests.el ends here --8<-----------------------------cut here---------------end--------------->8--- In GNU Emacs 27.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.11) of 2017-10-17 Repository revision: 94281c9a1cc0f756841fdc9b266657853df94a29