summaryrefslogtreecommitdiff
path: root/lisp/progmodes/xref.el
diff options
context:
space:
mode:
authorDmitry Gutov <dgutov@yandex.ru>2015-06-02 18:46:42 +0300
committerDmitry Gutov <dgutov@yandex.ru>2015-06-02 18:47:43 +0300
commit7f01832e1360b5203695d48605a45228f1362b42 (patch)
tree18331d8e6a5479eb160988faf0587b30c4be99b8 /lisp/progmodes/xref.el
parent64f2d346b762a6e3180eba92b5cc96f82f370687 (diff)
downloademacs-7f01832e1360b5203695d48605a45228f1362b42.tar.gz
Reuse rgrep mechanics in xref-find-regexp
* lisp/progmodes/grep.el (rgrep-default-command): Extract from `rgrep'. * lisp/progmodes/xref.el (xref-collect-references): Split from `xref-collect-matches'. Only handle the case of symbol search. (xref-collect-matches): Instead of Semantic Symref, use `rgrep-default-command', to take advantage of its directory and file ignore settings. (xref--collect-match): Remove the last argument, leaving the regexp construction up to the caller. * lisp/progmodes/elisp-mode.el (elisp--xref-find-matches): Change to take the xref-collect- function to use as an argument. (elisp-xref-find): Update accordingly. * lisp/progmodes/etags.el (etags--xref-find-matches) (etags-xref-find): Same.
Diffstat (limited to 'lisp/progmodes/xref.el')
-rw-r--r--lisp/progmodes/xref.el70
1 files changed, 47 insertions, 23 deletions
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 3bc66f884eb..d6f6ba89ab9 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -720,32 +720,59 @@ and just use etags."
(declare-function semantic-symref-find-references-by-name "semantic/symref")
(declare-function semantic-symref-find-text "semantic/symref")
(declare-function semantic-find-file-noselect "semantic/fw")
+(declare-function rgrep-default-command "grep")
-(defun xref-collect-matches (input dir &optional kind)
- "Collect KIND matches for INPUT inside DIR according.
-KIND can be `symbol', `regexp' or nil, the last of which means
-literal matches. This function uses the Semantic Symbol
-Reference API, see `semantic-symref-find-references-by-name' for
-details on which tools are used, and when."
+(defun xref-collect-references (symbol dir)
+ "Collect references to SYMBOL inside DIR.
+This function uses the Semantic Symbol Reference API, see
+`semantic-symref-find-references-by-name' for details on which
+tools are used, and when."
+ (cl-assert (directory-name-p dir))
(require 'semantic/symref)
(defvar semantic-symref-tool)
- (cl-assert (directory-name-p dir))
- (when (null kind)
- (setq input (regexp-quote input)))
- ;; FIXME: When regexp, search in all files, except
- ;; `grep-find-ignored-directories' and `grep-find-ignored-files',
- ;; like `rgrep' does.
(let* ((default-directory dir)
(semantic-symref-tool 'detect)
- (res (if (eq kind 'symbol)
- (semantic-symref-find-references-by-name input 'subdirs)
- (semantic-symref-find-text (xref--regexp-to-extended input)
- 'subdirs)))
+ (res (semantic-symref-find-references-by-name symbol 'subdirs))
(hits (and res (oref res :hit-lines)))
(orig-buffers (buffer-list)))
(unwind-protect
(delq nil
- (mapcar (lambda (hit) (xref--collect-match hit input kind)) hits))
+ (mapcar (lambda (hit) (xref--collect-match
+ hit (format "\\_<%s\\_>" (regexp-quote symbol))))
+ hits))
+ (mapc #'kill-buffer
+ (cl-set-difference (buffer-list) orig-buffers)))))
+
+(defun xref-collect-matches (regexp dir)
+ "Collect matches for REGEXP inside DIR using rgrep."
+ (cl-assert (directory-name-p dir))
+ (require 'semantic/fw)
+ (grep-compute-defaults)
+ (defvar grep-find-template)
+ (let* ((grep-find-template (replace-regexp-in-string "-e " "-E "
+ grep-find-template t t))
+ (command (rgrep-default-command (xref--regexp-to-extended regexp)
+ "*.*" dir))
+ (orig-buffers (buffer-list))
+ (buf (get-buffer-create " *xref-grep*"))
+ (grep-re (caar grep-regexp-alist))
+ hits)
+ ;; http://debbugs.gnu.org/20719
+ ;; We want to pass the exact directory to `find', because then
+ ;; `grep' output features absolute file names.
+ (when (string-match "find \\(\\.\\)" command)
+ (setq command (replace-match (shell-quote-argument dir) t t command 1)))
+ (with-current-buffer buf
+ (erase-buffer)
+ (when (eq (call-process-shell-command command nil t) 0)
+ (goto-char (point-min))
+ (while (re-search-forward grep-re nil t)
+ (push (cons (string-to-number (match-string 2))
+ (match-string 1))
+ hits))))
+ (unwind-protect
+ (delq nil
+ (mapcar (lambda (hit) (xref--collect-match hit regexp)) hits))
(mapc #'kill-buffer
(cl-set-difference (buffer-list) orig-buffers)))))
@@ -767,18 +794,15 @@ details on which tools are used, and when."
(match-string 1 str)))))
str t t))
-(defun xref--collect-match (hit input kind)
+(defun xref--collect-match (hit regexp)
(pcase-let* ((`(,line . ,file) hit)
(buf (or (find-buffer-visiting file)
- (semantic-find-file-noselect file)))
- (input (if (eq kind 'symbol)
- (format "\\_<%s\\_>" (regexp-quote input))
- input)))
+ (semantic-find-file-noselect file))))
(with-current-buffer buf
(save-excursion
(goto-char (point-min))
(forward-line (1- line))
- (when (re-search-forward input (line-end-position) t)
+ (when (re-search-forward regexp (line-end-position) t)
(goto-char (match-beginning 0))
(xref-make (buffer-substring
(line-beginning-position)