From 2b5ced057a959ab51387ac5d45d2a7b10a414591 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sun, 22 May 2022 12:08:54 -0700 Subject: [PATCH] Add dired-jump-other-frame and dired-jump-other-tab * lisp/bindings.el: Drop binding C-x C-j and C-x 4 C-j. * lisp/dired.el: Bind C-x C-j, C-x 4 C-j, C-x 5 C-j and C-x t C-j using autoload cookies. (This parallels the way in which C-x d, C-x 4 d, C-x 5 d and C-x t d are bound.) (dired-up-directory): When optional argument is a function, switch to the buffer using that function. (dired-jump): Refactor to call dired-jump--internal. (dired-jump-other-window): Refactor to call dired-jump--internal and add "... in other window" to read-file-name prompt. (dired-jump-other-frame, dired-jump-other-tab): New commands. (dired-jump--internal): New function factored out of dired-jump. * etc/NEWS: * doc/emacs/dired.texi (Entering Dired): Document the change. --- doc/emacs/dired.texi | 9 +++++- etc/NEWS | 4 +++ lisp/bindings.el | 3 -- lisp/dired.el | 66 ++++++++++++++++++++++++++++++++------------ 4 files changed, 60 insertions(+), 22 deletions(-) diff --git a/doc/emacs/dired.texi b/doc/emacs/dired.texi index ed4ff5213f..5a95d6e8e1 100644 --- a/doc/emacs/dired.texi +++ b/doc/emacs/dired.texi @@ -111,8 +111,12 @@ Dired Enter @findex dired-jump @findex dired-jump-other-window +@findex dired-jump-other-frame +@findex dired-jump-other-tab @kindex C-x C-j @kindex C-x 4 C-j +@kindex C-x 5 C-j +@kindex C-x t C-j You can ask Emacs to invoke Dired on the default-directory (@pxref{File Names, default-directory}) of any buffer, by typing @kbd{C-x C-j} (@code{dired-jump}). If the buffer visits a file, this @@ -123,7 +127,10 @@ Dired Enter and places point on the line that corresponds to the directory where you invoked @code{dired-jump}. Typing @kbd{C-x 4 C-j} (@code{dired-jump-other-window}) has the same effect, but displays the -Dired buffer in a new window. +Dired buffer in a new window; similarly @kbd{C-x 5 C-j} +(@code{dired-jump-other-frame}) to display the Dired buffer in a new +frame and @kbd{C-x t C-j} (@code{dired-jump-other-tab}) to display the +Dired buffer in a new tab. The variable @code{dired-listing-switches} specifies the options to give to @command{ls} for listing the directory; this string diff --git a/etc/NEWS b/etc/NEWS index 190620619f..31105bfbec 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1493,6 +1493,10 @@ If non-nil (which is the default), hitting 'RET' or 'mouse-1' on the directory components at the directory displayed at the start of the buffer will take you to that directory. ++++ +*** New commands 'dired-jump-other-frame' and 'dired-jump-other-tab'. +These are bound to 'C-x 5 C-j' and 'C-x t C-j' respectively. + ** Exif --- diff --git a/lisp/bindings.el b/lisp/bindings.el index ed1325e326..4ed197f1d4 100644 --- a/lisp/bindings.el +++ b/lisp/bindings.el @@ -1501,9 +1501,6 @@ esc-map (define-key ctl-x-map "'" 'expand-abbrev) (define-key ctl-x-map "\C-b" 'list-buffers) -(define-key ctl-x-map "\C-j" 'dired-jump) -(define-key ctl-x-4-map "\C-j" 'dired-jump-other-window) - (define-key ctl-x-map "z" 'repeat) (defvar ctl-x-x-map diff --git a/lisp/dired.el b/lisp/dired.el index 89fbd52aa6..d8fa726c6f 100644 --- a/lisp/dired.el +++ b/lisp/dired.el @@ -2583,8 +2583,13 @@ dired-up-directory "Run Dired on parent directory of current directory. Find the parent directory either in this buffer or another buffer. Creates a buffer if necessary. -If OTHER-WINDOW (the optional prefix arg), display the parent -directory in another window." + +When called interactively with a prefix argument, display the +parent directory in another window. + +When called from Lisp, if OTHER-WINDOW is a function, switch to +the buffer by calling that function. When OTHER-WINDOW is +otherwise non-nil, use `switch-to-buffer-other-window'." (interactive "P") (let* ((dir (dired-current-directory)) (up (file-name-directory (directory-file-name dir)))) @@ -2592,11 +2597,12 @@ dired-up-directory ;; Only try dired-goto-subdir if buffer has more than one dir. (and (cdr dired-subdir-alist) (dired-goto-subdir up)) - (progn - (if other-window - (dired-other-window up) - (dired--find-possibly-alternative-file up)) - (dired-goto-file dir))))) + (progn (pcase other-window + ('nil (dired--find-possibly-alternative-file up)) + ((or (pred functionp) (pred symbolp)) + (funcall other-window (dired-noselect up))) + (_ (dired-other-window up))) + (dired-goto-file dir))))) (defun dired-get-file-for-visit () "Get the current line's file name, with an error if file does not exist." @@ -4704,6 +4710,7 @@ archive-superior-buffer (defvar tar-superior-buffer) (declare-function dired-omit-mode "dired-x" (&optional arg)) +;;;###autoload (define-key ctl-x-map "\C-j" #'dired-jump) ;;;###autoload (defun dired-jump (&optional other-window file-name) "Jump to Dired buffer corresponding to current buffer. @@ -4723,6 +4730,39 @@ dired-jump (interactive (list nil (and current-prefix-arg (read-file-name "Jump to Dired file: ")))) + (dired-jump--internal (if other-window + #'switch-to-buffer-other-window + #'pop-to-buffer-same-window) + file-name)) + +;;;###autoload (define-key ctl-x-4-map "\C-j" #'dired-jump-other-window) +;;;###autoload +(defun dired-jump-other-window (&optional file-name) + "Like \\[dired-jump] (`dired-jump') but in other window." + (interactive + (list (and current-prefix-arg + (read-file-name "Jump to Dired file in other window: ")))) + (dired-jump--internal #'switch-to-buffer-other-window file-name)) + +;;;###autoload (define-key ctl-x-5-map "\C-j" #'dired-jump-other-frame) +;;;###autoload +(defun dired-jump-other-frame (&optional file-name) + "Like \\[dired-jump] (`dired-jump') but in other frame." + (interactive + (list (and current-prefix-arg + (read-file-name "Jump to Dired file in other frame: ")))) + (dired-jump--internal #'switch-to-buffer-other-frame file-name)) + +;;;###autoload (define-key tab-prefix-map "\C-j" #'dired-jump-other-tab) +;;;###autoload +(defun dired-jump-other-tab (&optional file-name) + "Like \\[dired-jump] (`dired-jump') but make new tab." + (interactive + (list (and current-prefix-arg + (read-file-name "Jump to Dired file in other tab: ")))) + (dired-jump--internal #'switch-to-buffer-other-tab file-name)) + +(defun dired-jump--internal (other-window file-name) (cond ((and (bound-and-true-p archive-subfile-mode) (buffer-live-p archive-superior-buffer)) @@ -4746,9 +4786,7 @@ dired-jump ;; refresh and try again (dired-insert-subdir (file-name-directory dir)) (dired-goto-file dir))) - (if other-window - (dired-other-window dir) - (dired dir)) + (funcall other-window (dired-noselect dir)) (if file (or (dired-goto-file file) ;; refresh and try again @@ -4760,14 +4798,6 @@ dired-jump (dired-omit-mode) (dired-goto-file file))))))))) -;;;###autoload -(defun dired-jump-other-window (&optional file-name) - "Like \\[dired-jump] (`dired-jump') but in other window." - (interactive - (list (and current-prefix-arg - (read-file-name "Jump to Dired file: ")))) - (dired-jump t file-name)) - (provide 'dired) (run-hooks 'dired-load-hook) ; for your customizations -- 2.30.2