emacs-devel
[Top][All Lists]
Advanced

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

dired-dwim-target-defaults (was: dired-dwim-target)


From: Juri Linkov
Subject: dired-dwim-target-defaults (was: dired-dwim-target)
Date: Mon, 23 Nov 2009 23:12:56 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (x86_64-pc-linux-gnu)

> The solution is simple: provide both the current and other
> directory names as default values.  The following patch keeps
> the old behavior when there is one Dired window:
>
> 0. current directory name
> 1. file name from the current Dired line
>
> but with more Dired windows it provides more default values:
>
> 0. current directory name
> 1. other directory name
> 2. file name from the other directory
> 3. file name from the current Dired line
>
> When `dired-dwim-target' is t (whose only purpose now will be
> to show the other directory name as initial input, but with
> `dired-dwim-target' = nil the same will be easily achievable via M-n):
>
> 0. other directory name
> 1. current directory name
> 2. file name from the current Dired line
> 3. file name from the other directory

Below is a complete patch that implements this scheme for
`dired-do-create-files' related commands (copy, rename, etc.),
`dired-diff', `dired-compare-directories' and `ediff-read-file-name'.

(This is a separate issue not depending on `find-file-read-args'.)

Index: lisp/dired-aux.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/dired-aux.el,v
retrieving revision 1.198
diff -c -r1.198 dired-aux.el
*** lisp/dired-aux.el   19 Nov 2009 07:42:24 -0000      1.198
--- lisp/dired-aux.el   23 Nov 2009 21:11:16 -0000
***************
*** 59,88 ****
  With prefix arg, prompt for second argument SWITCHES,
  which is options for `diff'."
    (interactive
!    (let ((current (dired-get-filename t))
!        (default (if (mark t)
!                     (save-excursion (goto-char (mark t))
!                                     (dired-get-filename t t)))))
!      (if (or (equal default current)
!            (and (not (equal (dired-dwim-target-directory)
!                             (dired-current-directory)))
!                 (not mark-active)))
!        (setq default nil))
       (require 'diff)
!      (list (read-file-name (format "Diff %s with%s: "
!                                  current
!                                  (if default
!                                      (concat " (default " default ")")
!                                    ""))
!                          (if default
!                              (dired-current-directory)
!                            (dired-dwim-target-directory))
!                          default t)
!          (if current-prefix-arg
!              (read-string "Options for diff: "
!                           (if (stringp diff-switches)
!                               diff-switches
!                             (mapconcat 'identity diff-switches " ")))))))
    (diff file (dired-get-filename t) switches))
  
  ;;;###autoload
--- 59,85 ----
  With prefix arg, prompt for second argument SWITCHES,
  which is options for `diff'."
    (interactive
!    (let* ((current (dired-get-filename t))
!         (target-dir (dired-dwim-target-directory))
!         (marked (and (mark t) (save-excursion
!                                 (goto-char (mark t))
!                                 (dired-get-filename nil t))))
!         (defaults
!           (append (dired-dwim-target-defaults nil target-dir)
!                   ;; Additional file with the mark.
!                   (and marked (list marked)))))
       (require 'diff)
!      (list
!       (minibuffer-with-setup-hook
!         (lambda ()
!           (set (make-local-variable 'minibuffer-default-add-function) nil)
!           (setq minibuffer-default defaults))
!       (read-file-name (format "Diff %s with: " current) target-dir nil t))
!       (if current-prefix-arg
!         (read-string "Options for diff: "
!                      (if (stringp diff-switches)
!                          diff-switches
!                        (mapconcat 'identity diff-switches " ")))))))
    (diff file (dired-get-filename t) switches))
  
  ;;;###autoload
***************
*** 128,138 ****
      (not (and (= (nth 2 fa1) (nth 2 fa2))   - mark files with different UID
                (= (nth 3 fa1) (nth 3 fa2))))   and GID."
    (interactive
!    (list (read-directory-name (format "Compare %s with: "
!                                     (dired-current-directory))
!                             (dired-dwim-target-directory)
!                             (dired-dwim-target-directory))
!          (read-from-minibuffer "Mark if (lisp expr or RET): " nil nil t nil 
"nil")))
    (let* ((dir1 (dired-current-directory))
           (file-alist1 (dired-files-attributes dir1))
           (file-alist2 (dired-files-attributes dir2))
--- 125,141 ----
      (not (and (= (nth 2 fa1) (nth 2 fa2))   - mark files with different UID
                (= (nth 3 fa1) (nth 3 fa2))))   and GID."
    (interactive
!    (list
!     (let* ((target-dir (dired-dwim-target-directory))
!          (defaults (dired-dwim-target-defaults nil target-dir)))
!       (minibuffer-with-setup-hook
!         (lambda ()
!           (set (make-local-variable 'minibuffer-default-add-function) nil)
!           (setq minibuffer-default defaults))
!       (read-directory-name (format "Compare %s with: "
!                                    (dired-current-directory))
!                            target-dir target-dir t)))
!     (read-from-minibuffer "Mark if (lisp expr or RET): " nil nil t nil 
"nil")))
    (let* ((dir1 (dired-current-directory))
           (file-alist1 (dired-files-attributes dir1))
           (file-alist2 (dired-files-attributes dir2))
***************
*** 1463,1472 ****
         (default (and dired-one-file
                       (expand-file-name (file-name-nondirectory (car fn-list))
                                         target-dir)))
         (target (expand-file-name ; fluid variable inside dired-create-files
!                  (dired-mark-read-file-name
!                   (concat (if dired-one-file op1 operation) " %s to: ")
!                   target-dir op-symbol arg rfn-list default)))
         (into-dir (cond ((null how-to)
                          ;; Allow DOS/Windows users to change the letter
                          ;; case of a directory.  If we don't test these
--- 1466,1480 ----
         (default (and dired-one-file
                       (expand-file-name (file-name-nondirectory (car fn-list))
                                         target-dir)))
+        (defaults (dired-dwim-target-defaults fn-list target-dir))
         (target (expand-file-name ; fluid variable inside dired-create-files
!                 (minibuffer-with-setup-hook
!                     (lambda ()
!                       (set (make-local-variable 
'minibuffer-default-add-function) nil)
!                       (setq minibuffer-default defaults))
!                   (dired-mark-read-file-name
!                    (concat (if dired-one-file op1 operation) " %s to: ")
!                    target-dir op-symbol arg rfn-list default))))
         (into-dir (cond ((null how-to)
                          ;; Allow DOS/Windows users to change the letter
                          ;; case of a directory.  If we don't test these
***************
*** 1523,1540 ****
          (or other-dir this-dir))
        this-dir)))
+ 
+ (defun dired-dwim-target-defaults (fn-list target-dir)
+   ;; Return a list of default values for file-reading functions in Dired.
+   ;; This list may contain directories from Dired buffers in other windows.
+   ;; `fn-list' is a list of file names used to build a list of defaults.
+   ;; When nil, a list of defaults will contain only directory names.
+   ;; `target-dir' is a directory name to exclude from the returned list,
+   ;; when this directory name is already presented in initial input.
+   ;; For Dired operations that support `dired-dwim-target', the argument
+   ;; `target-dir' should have the value returned from 
`dired-dwim-target-directory'.
+   (let ((dired-one-file
+        (and (consp fn-list) (null (cdr fn-list)) (car fn-list)))
+       (current-dir (and (eq major-mode 'dired-mode)
+                         (dired-current-directory)))
+       dired-dirs)
+     ;; Get a list of directories of visible buffers in dired-mode.
+     (walk-windows (lambda (w)
+                   (with-current-buffer (window-buffer w)
+                     (and (eq major-mode 'dired-mode)
+                          (push (dired-current-directory) dired-dirs)))))
+     ;; Force the current dir to be the first in the list.
+     (setq dired-dirs
+         (delete-dups (delq nil (cons current-dir (nreverse dired-dirs)))))
+     ;; Return default values.
+     ;; For nil `dired-dwim-target', remove the current dir from
+     ;; default values, because it should be already in initial input.
+     (setq dired-dirs (delete (or target-dir current-dir) dired-dirs))
+     (if dired-one-file
+       ;; For one file operation, provide a list that contains
+       ;; other directories, other directories with the appended filename
+       ;; and the current directory with the appended filename, e.g.
+       ;; 1. /TARGET-DIR/
+       ;; 2. /TARGET-DIR/FILENAME
+       ;; 3. /CURRENT-DIR/FILENAME
+       (append dired-dirs
+               (mapcar (lambda (dir)
+                         (expand-file-name
+                          (file-name-nondirectory (car fn-list)) dir))
+                       (reverse dired-dirs))
+               (list (expand-file-name
+                      (file-name-nondirectory (car fn-list))
+                      (or target-dir current-dir))))
+       ;; For multi-file operation, provide a list of other directories.
+       dired-dirs)))
  
  ;;;###autoload
  (defun dired-create-directory (directory)

Index: lisp/ediff-util.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/ediff-util.el,v
retrieving revision 1.100
diff -c -r1.100 ediff-util.el
*** lisp/ediff-util.el  22 Nov 2009 03:14:15 -0000      1.100
--- lisp/ediff-util.el  23 Nov 2009 21:12:23 -0000
***************
*** 3090,3095 ****
--- 3090,3096 ----
    (ediff-unhighlight-diffs-totally-in-one-buffer 'Ancestor)
    )
  
+ (declare-function dired-dwim-target-defaults "dired-aux" (fn-list target-dir))
  
  ;; This is adapted from a similar function in `emerge.el'.
  ;; PROMPT should not have a trailing ': ', so that it can be modified
***************
*** 3113,3133 ****
    (if (string= default-file "")
        (setq default-file nil))
  
!   (let (f)
!     (setq f (expand-file-name
!            (read-file-name
!             (format "%s%s "
!                     prompt
!                     (cond (default-file
!                             (concat " (default " default-file "):"))
!                           (t (concat " (default " default-dir "):"))))
!             default-dir
!             (or default-file default-dir)
!             t  ; must match, no-confirm
!             (if default-file (file-name-directory default-file))
!             )
!            default-dir
!            ))
      ;; If user entered a directory name, expand the default file in that
      ;; directory.  This allows the user to enter a directory name for the
      ;; B-file and diff against the default-file in that directory instead
--- 3114,3136 ----
    (if (string= default-file "")
        (setq default-file nil))
  
!   (let ((defaults (dired-dwim-target-defaults
!                  (and default-file (list default-file))
!                  default-dir))
!       f)
!     (setq f (minibuffer-with-setup-hook
!               (lambda () (setq minibuffer-default defaults))
!             (read-file-name
!              (format "%s%s "
!                      prompt
!                      (cond (default-file
!                              (concat " (default " default-file "):"))
!                            (t (concat " (default " default-dir "):"))))
!              default-dir
!              (or default-file default-dir)
!              t                        ; must match, no-confirm
!              (if default-file (file-name-directory default-file)))))
!     (setq f (expand-file-name f default-dir))
      ;; If user entered a directory name, expand the default file in that
      ;; directory.  This allows the user to enter a directory name for the
      ;; B-file and diff against the default-file in that directory instead

-- 
Juri Linkov
http://www.jurta.org/emacs/




reply via email to

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