]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/symref/grep.el
5d1fea8c82980c98bc350ee8810698fd32cd0043
[gnu-emacs] / lisp / cedet / semantic / symref / grep.el
1 ;;; semantic/symref/grep.el --- Symref implementation using find/grep
2
3 ;; Copyright (C) 2008-2016 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Implement the symref tool API using the external tools find/grep.
25 ;;
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.
29
30 (require 'semantic/symref)
31 (require 'grep)
32
33 ;;; Code:
34
35 ;;; GREP
36 ;;;###autoload
37 (defclass semantic-symref-tool-grep (semantic-symref-tool-baseclass)
38 (
39 )
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.")
44
45 (defvar semantic-symref-filepattern-alist
46 '((c-mode "*.[ch]")
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 )
54 "List of major modes and file extension pattern.
55 See find -name man page for format.")
56
57 (defun semantic-symref-derive-find-filepatterns (&optional mode)
58 ;; FIXME: This should be moved to grep.el, where it could be used
59 ;; for "C-u M-x grep" as well.
60 "Derive a list of file patterns for the current buffer.
61 Looks first in `semantic-symref-filepattern-alist'. If it is not
62 there, it then looks in `auto-mode-alist', and attempts to derive something
63 from that.
64 Optional argument MODE specifies the `major-mode' to test."
65 ;; First, try the filepattern alist.
66 (let* ((mode (or mode major-mode))
67 (pat (cdr (assoc mode semantic-symref-filepattern-alist))))
68 (when (not pat)
69 ;; No hit, try auto-mode-alist.
70 (dolist (X auto-mode-alist)
71 (when (and (eq (cdr X) mode)
72 ;; Only take in simple patterns, so try to convert this one.
73 (string-match "\\\\\\.\\([^\\'>]+\\)\\\\'" (car X)))
74 (push (concat "*." (match-string 1 (car X))) pat))))
75 ;; Convert the list into some find-flags.
76 (if (null pat)
77 (error "Customize `semantic-symref-filepattern-alist' for %S"
78 major-mode)
79 (let ((args `("-name" ,(car pat))))
80 (if (null (cdr args))
81 args
82 `("(" ,@args
83 ,@(apply #'nconc (mapcar (lambda (s) `("-o" "-name" ,s)) pat))
84 ")"))))))
85
86 (defvar grepflags)
87 (defvar greppattern)
88
89 (defvar semantic-symref-grep-expand-keywords
90 (condition-case nil
91 (let* ((kw (copy-alist grep-expand-keywords))
92 (C (assoc "<C>" kw))
93 (R (assoc "<R>" kw)))
94 (setcdr C 'grepflags)
95 (setcdr R 'greppattern)
96 kw)
97 (error nil))
98 "Grep expand keywords used when expanding templates for symref.")
99
100 (defun semantic-symref-grep-use-template (rootdir filepattern flags pattern)
101 "Use the grep template expand feature to create a grep command.
102 ROOTDIR is the root location to run the `find' from.
103 FILEPATTERN is a string representing find flags for searching file patterns.
104 GREPFLAGS are flags passed to grep, such as -n or -l.
105 GREPPATTERN is the pattern used by grep."
106 ;; We have grep-compute-defaults. Let's use it.
107 (grep-compute-defaults)
108 (let* ((grepflags flags)
109 (greppattern pattern)
110 (grep-expand-keywords semantic-symref-grep-expand-keywords)
111 (cmd (grep-expand-template
112 (if (memq system-type '(windows-nt ms-dos))
113 ;; grep-find uses '--color=always' on MS-Windows
114 ;; because it wants the colorized output, to show
115 ;; it to the user. By contrast, here we don't show
116 ;; the output, and the SGR escapes get in the way
117 ;; of parsing the output.
118 (replace-regexp-in-string "--color=always" ""
119 grep-find-template t t)
120 grep-find-template)
121 greppattern
122 filepattern
123 rootdir)))
124 ;; http://debbugs.gnu.org/20719
125 (when (string-match "find \\(\\.\\)" cmd)
126 (setq cmd (replace-match rootdir t t cmd 1)))
127 ;;(message "New command: %s" cmd)
128 cmd))
129
130 (defcustom semantic-symref-grep-shell shell-file-name
131 "The shell command to use for executing find/grep.
132 This shell should support pipe redirect syntax."
133 :group 'semantic
134 :type 'string)
135
136 (cl-defmethod semantic-symref-perform-search ((tool semantic-symref-tool-grep))
137 "Perform a search with Grep."
138 ;; Grep doesn't support some types of searches.
139 (let ((st (oref tool :searchtype)))
140 (when (not (memq st '(symbol regexp)))
141 (error "Symref impl GREP does not support searchtype of %s" st))
142 )
143 ;; Find the root of the project, and do a find-grep...
144 (let* (;; Find the file patterns to use.
145 (rootdir (semantic-symref-calculate-rootdir))
146 (filepatterns (semantic-symref-derive-find-filepatterns))
147 (filepattern (mapconcat #'shell-quote-argument filepatterns " "))
148 ;; Grep based flags.
149 (grepflags (cond ((eq (oref tool :resulttype) 'file)
150 "-l ")
151 ((eq (oref tool :searchtype) 'regexp)
152 "-nE ")
153 (t "-n ")))
154 (greppat (shell-quote-argument
155 (cond ((eq (oref tool :searchtype) 'regexp)
156 (oref tool searchfor))
157 (t
158 ;; Can't use the word boundaries: Grep
159 ;; doesn't always agrees with the language
160 ;; syntax on those.
161 (format "\\(^\\|\\W\\)%s\\(\\W\\|$\\)"
162 (oref tool searchfor))))))
163 ;; Misc
164 (b (get-buffer-create "*Semantic SymRef*"))
165 (ans nil)
166 )
167
168 (with-current-buffer b
169 (erase-buffer)
170 (setq default-directory rootdir)
171
172 (if (not (fboundp 'grep-compute-defaults))
173
174 ;; find . -type f -print0 | xargs -0 -e grep -nH -e
175 ;; Note : I removed -e as it is not posix, nor necessary it seems.
176
177 (let ((cmd (concat "find " default-directory " -type f " filepattern " -print0 "
178 "| xargs -0 grep -H " grepflags "-e " greppat)))
179 ;;(message "Old command: %s" cmd)
180 (call-process semantic-symref-grep-shell nil b nil
181 shell-command-switch cmd)
182 )
183 (let ((cmd (semantic-symref-grep-use-template rootdir filepattern grepflags greppat)))
184 (call-process semantic-symref-grep-shell nil b nil
185 shell-command-switch cmd))
186 ))
187 (setq ans (semantic-symref-parse-tool-output tool b))
188 ;; Return the answer
189 ans))
190
191 (cl-defmethod semantic-symref-parse-tool-output-one-line ((tool semantic-symref-tool-grep))
192 "Parse one line of grep output, and return it as a match list.
193 Moves cursor to end of the match."
194 (cond ((eq (oref tool :resulttype) 'file)
195 ;; Search for files
196 (when (re-search-forward "^\\([^\n]+\\)$" nil t)
197 (match-string 1)))
198 (t
199 (when (re-search-forward "^\\(\\(?:[a-zA-Z]:\\)?[^:\n]+\\):\\([0-9]+\\):" nil t)
200 (cons (string-to-number (match-string 2))
201 (match-string 1))
202 ))))
203
204 (provide 'semantic/symref/grep)
205
206 ;; Local variables:
207 ;; generated-autoload-file: "../loaddefs.el"
208 ;; generated-autoload-load-name: "semantic/symref/grep"
209 ;; End:
210
211 ;;; semantic/symref/grep.el ends here