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