4b2a4ee7200cf55ff0d4709a66b94f7adb4b2baa
[emacs.git] / lisp / progmodes / xref.el
blob4b2a4ee7200cf55ff0d4709a66b94f7adb4b2baa
1 ;; xref.el --- Cross-referencing commands -*-lexical-binding:t-*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Commentary:
22 ;; This file provides a somewhat generic infrastructure for cross
23 ;; referencing commands, in particular "find-definition".
25 ;; Some part of the functionality must be implemented in a language
26 ;; dependent way and that's done by defining `xref-find-function',
27 ;; `xref-identifier-at-point-function' and
28 ;; `xref-identifier-completion-table-function', which see.
30 ;; A major mode should make these variables buffer-local first.
32 ;; `xref-find-function' can be called in several ways, see its
33 ;; description. It has to operate with "xref" and "location" values.
35 ;; One would usually call `make-xref' and `xref-make-file-location',
36 ;; `xref-make-buffer-location' or `xref-make-bogus-location' to create
37 ;; them. More generally, a location must be an instance of an EIEIO
38 ;; class inheriting from `xref-location' and implementing
39 ;; `xref-location-group' and `xref-location-marker'.
41 ;; Each identifier must be represented as a string. Implementers can
42 ;; use string properties to store additional information about the
43 ;; identifier, but they should keep in mind that values returned from
44 ;; `xref-identifier-completion-table-function' should still be
45 ;; distinct, because the user can't see the properties when making the
46 ;; choice.
48 ;; See the functions `etags-xref-find' and `elisp-xref-find' for full
49 ;; examples.
51 ;;; Code:
53 (require 'cl-lib)
54 (require 'eieio)
55 (require 'ring)
56 (require 'pcase)
58 (defgroup xref nil "Cross-referencing commands"
59 :group 'tools)
62 ;;; Locations
64 (defclass xref-location () ()
65 :documentation "A location represents a position in a file or buffer.")
67 ;; If a backend decides to subclass xref-location it can provide
68 ;; methods for some of the following functions:
69 (cl-defgeneric xref-location-marker (location)
70 "Return the marker for LOCATION.")
72 (cl-defgeneric xref-location-group (location)
73 "Return a string used to group a set of locations.
74 This is typically the filename.")
76 (cl-defgeneric xref-location-line (_location)
77 "Return the line number corresponding to the location."
78 nil)
80 ;;;; Commonly needed location classes are defined here:
82 ;; FIXME: might be useful to have an optional "hint" i.e. a string to
83 ;; search for in case the line number is sightly out of date.
84 (defclass xref-file-location (xref-location)
85 ((file :type string :initarg :file)
86 (line :type fixnum :initarg :line :reader xref-location-line)
87 (column :type fixnum :initarg :column))
88 :documentation "A file location is a file/line/column triple.
89 Line numbers start from 1 and columns from 0.")
91 (defun xref-make-file-location (file line column)
92 "Create and return a new xref-file-location."
93 (make-instance 'xref-file-location :file file :line line :column column))
95 (cl-defmethod xref-location-marker ((l xref-file-location))
96 (with-slots (file line column) l
97 (with-current-buffer
98 (or (get-file-buffer file)
99 (let ((find-file-suppress-same-file-warnings t))
100 (find-file-noselect file)))
101 (save-restriction
102 (widen)
103 (save-excursion
104 (goto-char (point-min))
105 (beginning-of-line line)
106 (move-to-column column)
107 (point-marker))))))
109 (cl-defmethod xref-location-group ((l xref-file-location))
110 (oref l :file))
112 (defclass xref-buffer-location (xref-location)
113 ((buffer :type buffer :initarg :buffer)
114 (position :type fixnum :initarg :position)))
116 (defun xref-make-buffer-location (buffer position)
117 "Create and return a new xref-buffer-location."
118 (make-instance 'xref-buffer-location :buffer buffer :position position))
120 (cl-defmethod xref-location-marker ((l xref-buffer-location))
121 (with-slots (buffer position) l
122 (let ((m (make-marker)))
123 (move-marker m position buffer))))
125 (cl-defmethod xref-location-group ((l xref-buffer-location))
126 (with-slots (buffer) l
127 (or (buffer-file-name buffer)
128 (format "(buffer %s)" (buffer-name buffer)))))
130 (defclass xref-bogus-location (xref-location)
131 ((message :type string :initarg :message
132 :reader xref-bogus-location-message))
133 :documentation "Bogus locations are sometimes useful to
134 indicate errors, e.g. when we know that a function exists but the
135 actual location is not known.")
137 (defun xref-make-bogus-location (message)
138 "Create and return a new xref-bogus-location."
139 (make-instance 'xref-bogus-location :message message))
141 (cl-defmethod xref-location-marker ((l xref-bogus-location))
142 (user-error "%s" (oref l :message)))
144 (cl-defmethod xref-location-group ((_ xref-bogus-location)) "(No location)")
146 ;; This should be in elisp-mode.el, but it's preloaded, and we can't
147 ;; preload defclass and defmethod (at least, not yet).
148 (defclass xref-elisp-location (xref-location)
149 ((symbol :type symbol :initarg :symbol)
150 (type :type symbol :initarg :type)
151 (file :type string :initarg :file
152 :reader xref-location-group))
153 :documentation "Location of an Emacs Lisp symbol definition.")
155 (defun xref-make-elisp-location (symbol type file)
156 (make-instance 'xref-elisp-location :symbol symbol :type type :file file))
158 (cl-defmethod xref-location-marker ((l xref-elisp-location))
159 (with-slots (symbol type file) l
160 (let ((buffer-point
161 (pcase type
162 (`defun (find-function-search-for-symbol symbol nil file))
163 ((or `defvar `defface)
164 (find-function-search-for-symbol symbol type file))
165 (`feature
166 (cons (find-file-noselect file) 1)))))
167 (with-current-buffer (car buffer-point)
168 (goto-char (or (cdr buffer-point) (point-min)))
169 (point-marker)))))
172 ;;; Cross-reference
174 (defclass xref--xref ()
175 ((description :type string :initarg :description
176 :reader xref--xref-description)
177 (location :type xref-location :initarg :location
178 :reader xref--xref-location))
179 :comment "An xref is used to display and locate constructs like
180 variables or functions.")
182 (defun xref-make (description location)
183 "Create and return a new xref.
184 DESCRIPTION is a short string to describe the xref.
185 LOCATION is an `xref-location'."
186 (make-instance 'xref--xref :description description :location location))
189 ;;; API
191 (declare-function etags-xref-find "etags" (action id))
192 (declare-function tags-lazy-completion-table "etags" ())
194 ;; For now, make the etags backend the default.
195 (defvar xref-find-function #'etags-xref-find
196 "Function to look for cross-references.
197 It can be called in several ways:
199 (definitions IDENTIFIER): Find definitions of IDENTIFIER. The
200 result must be a list of xref objects. If no definitions can be
201 found, return nil.
203 (references IDENTIFIER): Find references of IDENTIFIER. The
204 result must be a list of xref objects. If no references can be
205 found, return nil.
207 (apropos PATTERN): Find all symbols that match PATTERN. PATTERN
208 is a regexp.
210 IDENTIFIER can be any string returned by
211 `xref-identifier-at-point-function', or from the table returned
212 by `xref-identifier-completion-table-function'.
214 To create an xref object, call `xref-make'.")
216 (defvar xref-identifier-at-point-function #'xref-default-identifier-at-point
217 "Function to get the relevant identifier at point.
219 The return value must be a string or nil. nil means no
220 identifier at point found.
222 If it's hard to determine the identifier precisely (e.g., because
223 it's a method call on unknown type), the implementation can
224 return a simple string (such as symbol at point) marked with a
225 special text property which `xref-find-function' would recognize
226 and then delegate the work to an external process.")
228 (defvar xref-identifier-completion-table-function #'tags-lazy-completion-table
229 "Function that returns the completion table for identifiers.")
231 (defun xref-default-identifier-at-point ()
232 (let ((thing (thing-at-point 'symbol)))
233 (and thing (substring-no-properties thing))))
236 ;;; misc utilities
237 (defun xref--alistify (list key test)
238 "Partition the elements of LIST into an alist.
239 KEY extracts the key from an element and TEST is used to compare
240 keys."
241 (let ((alist '()))
242 (dolist (e list)
243 (let* ((k (funcall key e))
244 (probe (cl-assoc k alist :test test)))
245 (if probe
246 (setcdr probe (cons e (cdr probe)))
247 (push (cons k (list e)) alist))))
248 ;; Put them back in order.
249 (cl-loop for (key . value) in (reverse alist)
250 collect (cons key (reverse value)))))
252 (defun xref--insert-propertized (props &rest strings)
253 "Insert STRINGS with text properties PROPS."
254 (let ((start (point)))
255 (apply #'insert strings)
256 (add-text-properties start (point) props)))
258 (defun xref--search-property (property &optional backward)
259 "Search the next text range where text property PROPERTY is non-nil.
260 Return the value of PROPERTY. If BACKWARD is non-nil, search
261 backward."
262 (let ((next (if backward
263 #'previous-single-char-property-change
264 #'next-single-char-property-change))
265 (start (point))
266 (value nil))
267 (while (progn
268 (goto-char (funcall next (point) property))
269 (not (or (setq value (get-text-property (point) property))
270 (eobp)
271 (bobp)))))
272 (cond (value)
273 (t (goto-char start) nil))))
276 ;;; Marker stack (M-. pushes, M-, pops)
278 (defcustom xref-marker-ring-length 16
279 "Length of the xref marker ring."
280 :type 'integer
281 :version "25.1")
283 (defcustom xref-prompt-for-identifier nil
284 "When non-nil, always prompt for the identifier name.
286 Otherwise, only prompt when there's no value at point we can use,
287 or when the command has been called with the prefix argument."
288 :type '(choice (const :tag "always" t)
289 (const :tag "auto" nil))
290 :version "25.1")
292 (defcustom xref-pulse-on-jump t
293 "When non-nil, momentarily highlight jump locations."
294 :type 'boolean
295 :version "25.1")
297 (defvar xref--marker-ring (make-ring xref-marker-ring-length)
298 "Ring of markers to implement the marker stack.")
300 (defun xref-push-marker-stack (&optional m)
301 "Add point M (defaults to `point-marker') to the marker stack."
302 (ring-insert xref--marker-ring (or m (point-marker))))
304 ;;;###autoload
305 (defun xref-pop-marker-stack ()
306 "Pop back to where \\[xref-find-definitions] was last invoked."
307 (interactive)
308 (let ((ring xref--marker-ring))
309 (when (ring-empty-p ring)
310 (error "Marker stack is empty"))
311 (let ((marker (ring-remove ring 0)))
312 (switch-to-buffer (or (marker-buffer marker)
313 (error "The marked buffer has been deleted")))
314 (goto-char (marker-position marker))
315 (set-marker marker nil nil)
316 (xref--maybe-pulse))))
318 (defun xref--maybe-pulse ()
319 (when xref-pulse-on-jump
320 (let (beg end)
321 (save-excursion
322 (back-to-indentation)
323 (if (eolp)
324 (setq beg (line-beginning-position)
325 end (1+ (point)))
326 (setq beg (point)
327 end (line-end-position))))
328 (pulse-momentary-highlight-region beg end 'next-error))))
330 ;; etags.el needs this
331 (defun xref-clear-marker-stack ()
332 "Discard all markers from the marker stack."
333 (let ((ring xref--marker-ring))
334 (while (not (ring-empty-p ring))
335 (let ((marker (ring-remove ring)))
336 (set-marker marker nil nil)))))
338 ;;;###autoload
339 (defun xref-marker-stack-empty-p ()
340 "Return t if the marker stack is empty; nil otherwise."
341 (ring-empty-p xref--marker-ring))
344 (defun xref--goto-location (location)
345 "Set buffer and point according to xref-location LOCATION."
346 (let ((marker (xref-location-marker location)))
347 (set-buffer (marker-buffer marker))
348 (cond ((and (<= (point-min) marker) (<= marker (point-max))))
349 (widen-automatically (widen))
350 (t (error "Location is outside accessible part of buffer")))
351 (goto-char marker)))
353 (defun xref--pop-to-location (location &optional window)
354 "Goto xref-location LOCATION and display the buffer.
355 WINDOW controls how the buffer is displayed:
356 nil -- switch-to-buffer
357 'window -- pop-to-buffer (other window)
358 'frame -- pop-to-buffer (other frame)"
359 (xref--goto-location location)
360 (cl-ecase window
361 ((nil) (switch-to-buffer (current-buffer)))
362 (window (pop-to-buffer (current-buffer) t))
363 (frame (let ((pop-up-frames t)) (pop-to-buffer (current-buffer) t))))
364 (xref--maybe-pulse))
367 ;;; XREF buffer (part of the UI)
369 ;; The xref buffer is used to display a set of xrefs.
371 (defvar-local xref--display-history nil
372 "List of pairs (BUFFER . WINDOW), for temporarily displayed buffers.")
374 (defvar-local xref--temporary-buffers nil
375 "List of buffers created by xref code.")
377 (defvar-local xref--current nil
378 "Non-nil if this buffer was once current, except while displaying xrefs.
379 Used for temporary buffers.")
381 (defvar xref--inhibit-mark-current nil)
383 (defun xref--mark-selected ()
384 (unless xref--inhibit-mark-current
385 (setq xref--current t))
386 (remove-hook 'buffer-list-update-hook #'xref--mark-selected t))
388 (defun xref--save-to-history (buf win)
389 (let ((restore (window-parameter win 'quit-restore)))
390 ;; Save the new entry if the window displayed another buffer
391 ;; previously.
392 (when (and restore (not (eq (car restore) 'same)))
393 (push (cons buf win) xref--display-history))))
395 (defun xref--display-position (pos other-window recenter-arg xref-buf)
396 ;; Show the location, but don't hijack focus.
397 (with-selected-window (display-buffer (current-buffer) other-window)
398 (goto-char pos)
399 (recenter recenter-arg)
400 (xref--maybe-pulse)
401 (let ((buf (current-buffer))
402 (win (selected-window)))
403 (with-current-buffer xref-buf
404 (setq-local other-window-scroll-buffer buf)
405 (xref--save-to-history buf win)))))
407 (defun xref--show-location (location)
408 (condition-case err
409 (let ((xref-buf (current-buffer))
410 (bl (buffer-list))
411 (xref--inhibit-mark-current t))
412 (xref--goto-location location)
413 (let ((buf (current-buffer)))
414 (unless (memq buf bl)
415 ;; Newly created.
416 (add-hook 'buffer-list-update-hook #'xref--mark-selected nil t)
417 (with-current-buffer xref-buf
418 (push buf xref--temporary-buffers))))
419 (xref--display-position (point) t 1 xref-buf))
420 (user-error (message (error-message-string err)))))
422 (defun xref-show-location-at-point ()
423 "Display the source of xref at point in the other window, if any."
424 (interactive)
425 (let ((loc (xref--location-at-point)))
426 (when loc
427 (xref--show-location loc))))
429 (defun xref-next-line ()
430 "Move to the next xref and display its source in the other window."
431 (interactive)
432 (xref--search-property 'xref-location)
433 (xref-show-location-at-point))
435 (defun xref-prev-line ()
436 "Move to the previous xref and display its source in the other window."
437 (interactive)
438 (xref--search-property 'xref-location t)
439 (xref-show-location-at-point))
441 (defun xref--location-at-point ()
442 (save-excursion
443 (back-to-indentation)
444 (get-text-property (point) 'xref-location)))
446 (defvar-local xref--window nil
447 "ACTION argument to call `display-buffer' with.")
449 (defun xref-goto-xref ()
450 "Jump to the xref on the current line and bury the xref buffer."
451 (interactive)
452 (let ((loc (or (xref--location-at-point)
453 (user-error "No reference at point")))
454 (window xref--window))
455 (xref-quit)
456 (xref--pop-to-location loc window)))
458 (defvar xref--xref-buffer-mode-map
459 (let ((map (make-sparse-keymap)))
460 (define-key map [remap quit-window] #'xref-quit)
461 (define-key map (kbd "n") #'xref-next-line)
462 (define-key map (kbd "p") #'xref-prev-line)
463 (define-key map (kbd "RET") #'xref-goto-xref)
464 (define-key map (kbd "C-o") #'xref-show-location-at-point)
465 ;; suggested by Johan Claesson "to further reduce finger movement":
466 (define-key map (kbd ".") #'xref-next-line)
467 (define-key map (kbd ",") #'xref-prev-line)
468 map))
470 (define-derived-mode xref--xref-buffer-mode special-mode "XREF"
471 "Mode for displaying cross-references."
472 (setq buffer-read-only t)
473 (setq next-error-function #'xref--next-error-function)
474 (setq next-error-last-buffer (current-buffer)))
476 (defun xref--next-error-function (n reset?)
477 (when reset?
478 (goto-char (point-min)))
479 (let ((backward (< n 0))
480 (n (abs n))
481 (loc nil))
482 (dotimes (_ n)
483 (setq loc (xref--search-property 'xref-location backward)))
484 (cond (loc
485 (xref--pop-to-location loc))
487 (error "No %s xref" (if backward "previous" "next"))))))
489 (defun xref-quit (&optional kill)
490 "Bury temporarily displayed buffers, then quit the current window.
492 If KILL is non-nil, kill all buffers that were created in the
493 process of showing xrefs, and also kill the current buffer.
495 The buffers that the user has otherwise interacted with in the
496 meantime are preserved."
497 (interactive "P")
498 (let ((window (selected-window))
499 (history xref--display-history))
500 (setq xref--display-history nil)
501 (pcase-dolist (`(,buf . ,win) history)
502 (when (and (window-live-p win)
503 (eq buf (window-buffer win)))
504 (quit-window nil win)))
505 (when kill
506 (let ((xref--inhibit-mark-current t)
507 kill-buffer-query-functions)
508 (dolist (buf xref--temporary-buffers)
509 (unless (buffer-local-value 'xref--current buf)
510 (kill-buffer buf)))
511 (setq xref--temporary-buffers nil)))
512 (quit-window kill window)))
514 (defconst xref-buffer-name "*xref*"
515 "The name of the buffer to show xrefs.")
517 (defvar xref--button-map
518 (let ((map (make-sparse-keymap)))
519 (define-key map [(control ?m)] #'xref-goto-xref)
520 (define-key map [mouse-1] #'xref-goto-xref)
521 (define-key map [mouse-2] #'xref--mouse-2)
522 map))
524 (defun xref--mouse-2 (event)
525 "Move point to the button and show the xref definition."
526 (interactive "e")
527 (mouse-set-point event)
528 (forward-line 0)
529 (xref--search-property 'xref-location)
530 (xref-show-location-at-point))
532 (defun xref--insert-xrefs (xref-alist)
533 "Insert XREF-ALIST in the current-buffer.
534 XREF-ALIST is of the form ((GROUP . (XREF ...)) ...). Where
535 GROUP is a string for decoration purposes and XREF is an
536 `xref--xref' object."
537 (require 'compile) ;; For the compilation-info face.
538 (cl-loop for ((group . xrefs) . more1) on xref-alist
539 for max-line-width =
540 (cl-loop for xref in xrefs
541 maximize (let ((line (xref-location-line
542 (oref xref :location))))
543 (length (and line (format "%d" line)))))
544 for line-format = (and max-line-width
545 (format "%%%dd: " max-line-width))
547 (xref--insert-propertized '(face compilation-info) group "\n")
548 (cl-loop for (xref . more2) on xrefs do
549 (with-slots (description location) xref
550 (let* ((line (xref-location-line location))
551 (prefix
552 (if line
553 (propertize (format line-format line)
554 'face 'compilation-line-number)
555 " ")))
556 (xref--insert-propertized
557 (list 'xref-location location
558 ;; 'face 'font-lock-keyword-face
559 'mouse-face 'highlight
560 'keymap xref--button-map
561 'help-echo
562 (concat "mouse-2: display in another window, "
563 "RET or mouse-1: follow reference"))
564 prefix description)))
565 (insert "\n"))))
567 (defun xref--analyze (xrefs)
568 "Find common filenames in XREFS.
569 Return an alist of the form ((FILENAME . (XREF ...)) ...)."
570 (xref--alistify xrefs
571 (lambda (x)
572 (xref-location-group (xref--xref-location x)))
573 #'equal))
575 (defun xref--show-xref-buffer (xrefs alist)
576 (let ((xref-alist (xref--analyze xrefs)))
577 (with-current-buffer (get-buffer-create xref-buffer-name)
578 (let ((inhibit-read-only t))
579 (erase-buffer)
580 (xref--insert-xrefs xref-alist)
581 (xref--xref-buffer-mode)
582 (pop-to-buffer (current-buffer))
583 (goto-char (point-min))
584 (setq xref--window (assoc-default 'window alist))
585 (setq xref--temporary-buffers (assoc-default 'temporary-buffers alist))
586 (dolist (buf xref--temporary-buffers)
587 (with-current-buffer buf
588 (add-hook 'buffer-list-update-hook #'xref--mark-selected nil t)))
589 (current-buffer)))))
592 ;; This part of the UI seems fairly uncontroversial: it reads the
593 ;; identifier and deals with the single definition case.
595 ;; The controversial multiple definitions case is handed off to
596 ;; xref-show-xrefs-function.
598 (defvar xref-show-xrefs-function 'xref--show-xref-buffer
599 "Function to display a list of xrefs.")
601 (defvar xref--read-identifier-history nil)
603 (defvar xref--read-pattern-history nil)
605 (defun xref--show-xrefs (input kind arg window)
606 (let* ((bl (buffer-list))
607 (xrefs (funcall xref-find-function kind arg))
608 (tb (cl-set-difference (buffer-list) bl)))
609 (cond
610 ((null xrefs)
611 (user-error "No known %s for: %s" (symbol-name kind) input))
612 ((not (cdr xrefs))
613 (xref-push-marker-stack)
614 (xref--pop-to-location (xref--xref-location (car xrefs)) window))
616 (xref-push-marker-stack)
617 (funcall xref-show-xrefs-function xrefs
618 `((window . ,window)
619 (temporary-buffers . ,tb)))))))
621 (defun xref--read-identifier (prompt)
622 "Return the identifier at point or read it from the minibuffer."
623 (let ((id (funcall xref-identifier-at-point-function)))
624 (cond ((or current-prefix-arg xref-prompt-for-identifier (not id))
625 (completing-read prompt
626 (funcall xref-identifier-completion-table-function)
627 nil t nil
628 'xref--read-identifier-history id))
629 (t id))))
632 ;;; Commands
634 (defun xref--find-definitions (id window)
635 (xref--show-xrefs id 'definitions id window))
637 ;;;###autoload
638 (defun xref-find-definitions (identifier)
639 "Find the definition of the identifier at point.
640 With prefix argument or when there's no identifier at point,
641 prompt for it."
642 (interactive (list (xref--read-identifier "Find definitions of: ")))
643 (xref--find-definitions identifier nil))
645 ;;;###autoload
646 (defun xref-find-definitions-other-window (identifier)
647 "Like `xref-find-definitions' but switch to the other window."
648 (interactive (list (xref--read-identifier "Find definitions of: ")))
649 (xref--find-definitions identifier 'window))
651 ;;;###autoload
652 (defun xref-find-definitions-other-frame (identifier)
653 "Like `xref-find-definitions' but switch to the other frame."
654 (interactive (list (xref--read-identifier "Find definitions of: ")))
655 (xref--find-definitions identifier 'frame))
657 ;;;###autoload
658 (defun xref-find-references (identifier)
659 "Find references to the identifier at point.
660 With prefix argument, prompt for the identifier."
661 (interactive (list (xref--read-identifier "Find references of: ")))
662 (xref--show-xrefs identifier 'references identifier nil))
664 (declare-function apropos-parse-pattern "apropos" (pattern))
666 ;;;###autoload
667 (defun xref-find-apropos (pattern)
668 "Find all meaningful symbols that match PATTERN.
669 The argument has the same meaning as in `apropos'."
670 (interactive (list (read-from-minibuffer
671 "Search for pattern (word list or regexp): "
672 nil nil nil 'xref--read-pattern-history)))
673 (require 'apropos)
674 (xref--show-xrefs pattern 'apropos
675 (apropos-parse-pattern
676 (if (string-equal (regexp-quote pattern) pattern)
677 ;; Split into words
678 (or (split-string pattern "[ \t]+" t)
679 (user-error "No word list given"))
680 pattern))
681 nil))
684 ;;; Key bindings
686 ;;;###autoload (define-key esc-map "." #'xref-find-definitions)
687 ;;;###autoload (define-key esc-map "," #'xref-pop-marker-stack)
688 ;;;###autoload (define-key esc-map [?\C-.] #'xref-find-apropos)
689 ;;;###autoload (define-key ctl-x-4-map "." #'xref-find-definitions-other-window)
690 ;;;###autoload (define-key ctl-x-5-map "." #'xref-find-definitions-other-frame)
693 ;;; Helper functions
695 (defvar xref-etags-mode--saved nil)
697 (define-minor-mode xref-etags-mode
698 "Minor mode to make xref use etags again.
700 Certain major modes install their own mechanisms for listing
701 identifiers and navigation. Turn this on to undo those settings
702 and just use etags."
703 :lighter ""
704 (if xref-etags-mode
705 (progn
706 (setq xref-etags-mode--saved
707 (cons xref-find-function
708 xref-identifier-completion-table-function))
709 (kill-local-variable 'xref-find-function)
710 (kill-local-variable 'xref-identifier-completion-table-function))
711 (setq-local xref-find-function (car xref-etags-mode--saved))
712 (setq-local xref-identifier-completion-table-function
713 (cdr xref-etags-mode--saved))))
715 (declare-function semantic-symref-find-references-by-name "semantic/symref")
716 (declare-function semantic-find-file-noselect "semantic/fw")
718 (defun xref-collect-references (name dir)
719 "Collect mentions of NAME inside DIR.
720 Uses the Semantic Symbol Reference API, see
721 `semantic-symref-find-references-by-name' for details on which
722 tools are used, and when."
723 (require 'semantic/symref)
724 (defvar semantic-symref-tool)
725 (cl-assert (directory-name-p dir))
726 (let* ((default-directory dir)
727 (semantic-symref-tool 'detect)
728 (res (semantic-symref-find-references-by-name name 'subdirs))
729 (hits (and res (oref res :hit-lines)))
730 (orig-buffers (buffer-list)))
731 (unwind-protect
732 (delq nil
733 (mapcar (lambda (hit) (xref--collect-reference hit name)) hits))
734 (mapc #'kill-buffer
735 (cl-set-difference (buffer-list) orig-buffers)))))
737 (defun xref--collect-reference (hit name)
738 (pcase-let* ((`(,line . ,file) hit)
739 (buf (or (find-buffer-visiting file)
740 (semantic-find-file-noselect file))))
741 (with-current-buffer buf
742 (save-excursion
743 (goto-char (point-min))
744 (forward-line (1- line))
745 (when (re-search-forward (format "\\_<%s\\_>"
746 (regexp-quote name))
747 (line-end-position) t)
748 (goto-char (match-beginning 0))
749 (xref-make (buffer-substring
750 (line-beginning-position)
751 (line-end-position))
752 (xref-make-file-location file line
753 (current-column))))))))
756 (provide 'xref)
758 ;;; xref.el ends here