1 ;;; semantic/symref/grep.el --- Symref implementation using find/grep
3 ;; Copyright (C) 2008-2016 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Implement the symref tool API using the external tools find/grep.
26 ;; The symref GREP tool uses grep in a project to find symbol references.
27 ;; This is a lowest-common-denominator tool with sucky performance that
28 ;; can be used in small projects to find symbol references.
30 (require 'semantic/symref)
37 (defclass semantic-symref-tool-grep (semantic-symref-tool-baseclass)
40 "A symref tool implementation using grep.
41 This tool uses EDE to find he root of the project, then executes
42 find-grep in the project. The output is parsed for hits
43 and those hits returned.")
45 (defvar semantic-symref-filepattern-alist
47 (c++-mode "*.[chCH]" "*.[ch]pp" "*.cc" "*.hh")
48 (html-mode "*.s?html" "*.php")
49 (ruby-mode "*.r[bu]" "*.rake" "*.gemspec" "*.erb" "*.haml"
50 "Rakefile" "Thorfile" "Capfile" "Guardfile" "Vagrantfile")
51 (perl-mode "*.pl" "*.PL")
52 (cperl-mode "*.pl" "*.PL")
53 (lisp-interaction-mode "*.el" "*.ede" ".emacs" "_emacs")
55 "List of major modes and file extension pattern.
56 See find -name man page for format.")
58 (defun semantic-symref-derive-find-filepatterns (&optional mode)
59 ;; FIXME: This should be moved to grep.el, where it could be used
60 ;; for "C-u M-x grep" as well.
61 "Derive a list of file patterns for the current buffer.
62 Looks first in `semantic-symref-filepattern-alist'. If it is not
63 there, it then looks in `auto-mode-alist', and attempts to derive something
65 Optional argument MODE specifies the `major-mode' to test."
66 ;; First, try the filepattern alist.
67 (let* ((mode (or mode major-mode))
68 (pat (cdr (assoc mode semantic-symref-filepattern-alist))))
70 ;; No hit, try auto-mode-alist.
71 (dolist (X auto-mode-alist)
72 (when (and (eq (cdr X) mode)
73 ;; Only take in simple patterns, so try to convert this one.
74 (string-match "\\\\\\.\\([^\\'>]+\\)\\\\'" (car X)))
75 (push (concat "*." (match-string 1 (car X))) pat))))
76 ;; Convert the list into some find-flags.
78 (error "Customize `semantic-symref-filepattern-alist' for %S"
80 (let ((args `("-name" ,(car pat))))
84 ,@(mapcan (lambda (s) `("-o" "-name" ,s)) pat)
90 (defvar semantic-symref-grep-expand-keywords
92 (let* ((kw (copy-alist grep-expand-keywords))
96 (setcdr R 'greppattern)
99 "Grep expand keywords used when expanding templates for symref.")
101 (defun semantic-symref-grep-use-template (rootdir filepattern flags pattern)
102 "Use the grep template expand feature to create a grep command.
103 ROOTDIR is the root location to run the `find' from.
104 FILEPATTERN is a string representing find flags for searching file patterns.
105 GREPFLAGS are flags passed to grep, such as -n or -l.
106 GREPPATTERN is the pattern used by grep."
107 ;; We have grep-compute-defaults. Let's use it.
108 (grep-compute-defaults)
109 (let* ((grepflags flags)
110 (greppattern pattern)
111 (grep-expand-keywords semantic-symref-grep-expand-keywords)
112 (cmd (grep-expand-template
113 (if (memq system-type '(windows-nt ms-dos))
114 ;; grep-find uses '--color=always' on MS-Windows
115 ;; because it wants the colorized output, to show
116 ;; it to the user. By contrast, here we don't show
117 ;; the output, and the SGR escapes get in the way
118 ;; of parsing the output.
119 (replace-regexp-in-string "--color=always" ""
120 grep-find-template t t)
125 ;; http://debbugs.gnu.org/20719
126 (when (string-match "find \\(\\.\\)" cmd)
127 (setq cmd (replace-match rootdir t t cmd 1)))
128 ;;(message "New command: %s" cmd)
131 (defcustom semantic-symref-grep-shell shell-file-name
132 "The shell command to use for executing find/grep.
133 This shell should support pipe redirect syntax."
137 (cl-defmethod semantic-symref-perform-search ((tool semantic-symref-tool-grep))
138 "Perform a search with Grep."
139 ;; Grep doesn't support some types of searches.
140 (let ((st (oref tool :searchtype)))
141 (when (not (memq st '(symbol regexp)))
142 (error "Symref impl GREP does not support searchtype of %s" st))
144 ;; Find the root of the project, and do a find-grep...
145 (let* (;; Find the file patterns to use.
146 (rootdir (semantic-symref-calculate-rootdir))
147 (filepatterns (semantic-symref-derive-find-filepatterns))
148 (filepattern (mapconcat #'shell-quote-argument filepatterns " "))
150 (grepflags (cond ((eq (oref tool :resulttype) 'file)
152 ((eq (oref tool :searchtype) 'regexp)
155 (greppat (shell-quote-argument
156 (cond ((eq (oref tool :searchtype) 'regexp)
157 (oref tool searchfor))
159 ;; Can't use the word boundaries: Grep
160 ;; doesn't always agrees with the language
162 (format "\\(^\\|\\W\\)%s\\(\\W\\|$\\)"
163 (oref tool searchfor))))))
165 (b (get-buffer-create "*Semantic SymRef*"))
169 (with-current-buffer b
171 (setq default-directory rootdir)
173 (if (not (fboundp 'grep-compute-defaults))
175 ;; find . -type f -print0 | xargs -0 -e grep -nH -e
176 ;; Note : I removed -e as it is not posix, nor necessary it seems.
178 (let ((cmd (concat "find " default-directory " -type f " filepattern " -print0 "
179 "| xargs -0 grep -H " grepflags "-e " greppat)))
180 ;;(message "Old command: %s" cmd)
181 (call-process semantic-symref-grep-shell nil b nil
182 shell-command-switch cmd)
184 (let ((cmd (semantic-symref-grep-use-template rootdir filepattern grepflags greppat)))
185 (call-process semantic-symref-grep-shell nil b nil
186 shell-command-switch cmd))
188 (setq ans (semantic-symref-parse-tool-output tool b))
192 (defconst semantic-symref-grep--line-re
193 "^\\(\\(?:[a-zA-Z]:\\)?[^:\n]+\\):\\([0-9]+\\):")
195 (cl-defmethod semantic-symref-parse-tool-output-one-line ((tool semantic-symref-tool-grep))
196 "Parse one line of grep output, and return it as a match list.
197 Moves cursor to end of the match."
198 (cond ((eq (oref tool :resulttype) 'file)
200 (when (re-search-forward "^\\([^\n]+\\)$" nil t)
202 ((eq (oref tool :resulttype) 'line-and-text)
203 (when (re-search-forward semantic-symref-grep--line-re nil t)
204 (list (string-to-number (match-string 2))
206 (buffer-substring-no-properties (point) (line-end-position)))))
208 (when (re-search-forward semantic-symref-grep--line-re nil t)
209 (cons (string-to-number (match-string 2))
213 (provide 'semantic/symref/grep)
216 ;; generated-autoload-file: "../loaddefs.el"
217 ;; generated-autoload-load-name: "semantic/symref/grep"
220 ;;; semantic/symref/grep.el ends here