summaryrefslogtreecommitdiff
path: root/lisp/progmodes/xref.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/progmodes/xref.el')
-rw-r--r--lisp/progmodes/xref.el91
1 files changed, 66 insertions, 25 deletions
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 3dbf65ef6f5..db025d40aa3 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -448,43 +448,74 @@ If SELECT is non-nil, select the target window."
(when xref-w
(set-window-dedicated-p xref-w xref-w-dedicated)))))
-(defun xref--show-pos-in-buf (pos buf select)
- (let ((xref-buf (current-buffer))
- win)
+(defvar-local xref--original-window-intent nil
+ "Original window-switching intent before xref buffer creation.")
+
+(defvar-local xref--original-window nil
+ "The original window this xref buffer was created from.")
+
+(defun xref--show-pos-in-buf (pos buf)
+ "Goto and display position POS of buffer BUF in a window.
+Honor `xref--original-window-intent', run `xref-after-jump-hook'
+and finally return the window."
+ (let* ((xref-buf (current-buffer))
+ (pop-up-frames
+ (or (eq xref--original-window-intent 'frame)
+ pop-up-frames))
+ (action
+ (cond ((memq
+ xref--original-window-intent
+ '(window frame))
+ t)
+ ((and
+ (window-live-p xref--original-window)
+ (or (not (window-dedicated-p xref--original-window))
+ (eq (window-buffer xref--original-window) buf)))
+ `(,(lambda (buf _alist)
+ (set-window-buffer xref--original-window buf)
+ xref--original-window))))))
(with-selected-window
- (xref--with-dedicated-window
- (display-buffer buf))
+ (with-selected-window
+ ;; Just before `display-buffer', place ourselves in the
+ ;; original window to suggest preserving it. Of course, if
+ ;; user has deleted the original window, all bets are off,
+ ;; just use the selected one.
+ (or (and (window-live-p xref--original-window)
+ xref--original-window)
+ (selected-window))
+ (display-buffer buf action))
(xref--goto-char pos)
(run-hooks 'xref-after-jump-hook)
(let ((buf (current-buffer)))
- (setq win (selected-window))
(with-current-buffer xref-buf
- (setq-local other-window-scroll-buffer buf))))
- (when select
- (select-window win))))
+ (setq-local other-window-scroll-buffer buf)))
+ (selected-window))))
(defun xref--show-location (location &optional select)
+ "Help `xref-show-xref' and `xref-goto-xref' do their job.
+Go to LOCATION and if SELECT is non-nil select its window. If
+SELECT is `quit', also quit the *xref* window."
(condition-case err
(let* ((marker (xref-location-marker location))
- (buf (marker-buffer marker)))
- (xref--show-pos-in-buf marker buf select))
+ (buf (marker-buffer marker))
+ (xref-buffer (current-buffer)))
+ (cond (select
+ (if (eq select 'quit) (quit-window nil nil))
+ (with-current-buffer xref-buffer
+ (select-window (xref--show-pos-in-buf marker buf))))
+ (t
+ (save-selected-window
+ (xref--with-dedicated-window
+ (xref--show-pos-in-buf marker buf))))))
(user-error (message (error-message-string err)))))
-(defvar-local xref--window nil
- "The original window this xref buffer was created from.")
-
(defun xref-show-location-at-point ()
"Display the source of xref at point in the appropriate window, if any."
(interactive)
(let* ((xref (xref--item-at-point))
(xref--current-item xref))
(when xref
- ;; Try to avoid the window the current xref buffer was
- ;; originally created from.
- (if (window-live-p xref--window)
- (with-selected-window xref--window
- (xref--show-location (xref-item-location xref)))
- (xref--show-location (xref-item-location xref))))))
+ (xref--show-location (xref-item-location xref)))))
(defun xref-next-line ()
"Move to the next xref and display its source in the appropriate window."
@@ -503,12 +534,19 @@ If SELECT is non-nil, select the target window."
(back-to-indentation)
(get-text-property (point) 'xref-item)))
-(defun xref-goto-xref ()
- "Jump to the xref on the current line and select its window."
+(defun xref-goto-xref (&optional quit)
+ "Jump to the xref on the current line and select its window.
+Non-interactively, non-nil QUIT means to first quit the *xref*
+buffer."
(interactive)
(let ((xref (or (xref--item-at-point)
(user-error "No reference at point"))))
- (xref--show-location (xref-item-location xref) t)))
+ (xref--show-location (xref-item-location xref) (if quit 'quit t))))
+
+(defun xref-quit-and-goto-xref ()
+ "Quit *xref* buffer, then jump to xref on current line."
+ (interactive)
+ (xref-goto-xref t))
(defun xref-query-replace-in-results (from to)
"Perform interactive replacement of FROM with TO in all displayed xrefs.
@@ -632,6 +670,7 @@ references displayed in the current *xref* buffer."
(define-key map (kbd "p") #'xref-prev-line)
(define-key map (kbd "r") #'xref-query-replace-in-results)
(define-key map (kbd "RET") #'xref-goto-xref)
+ (define-key map (kbd "TAB") #'xref-quit-and-goto-xref)
(define-key map (kbd "C-o") #'xref-show-location-at-point)
;; suggested by Johan Claesson "to further reduce finger movement":
(define-key map (kbd ".") #'xref-next-line)
@@ -726,7 +765,8 @@ Return an alist of the form ((FILENAME . (XREF ...)) ...)."
(xref--xref-buffer-mode)
(pop-to-buffer (current-buffer))
(goto-char (point-min))
- (setq xref--window (assoc-default 'window alist))
+ (setq xref--original-window (assoc-default 'window alist)
+ xref--original-window-intent (assoc-default 'display-action alist))
(current-buffer)))))
@@ -753,7 +793,8 @@ Return an alist of the form ((FILENAME . (XREF ...)) ...)."
(t
(xref-push-marker-stack)
(funcall xref-show-xrefs-function xrefs
- `((window . ,(selected-window)))))))
+ `((window . ,(selected-window))
+ (display-action . ,display-action))))))
(defun xref--prompt-p (command)
(or (eq xref-prompt-for-identifier t)