]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/symref/grep.el
Add semantic/symref/grep file patterns for ruby-mode
[gnu-emacs] / lisp / cedet / semantic / symref / grep.el
1 ;;; semantic/symref/grep.el --- Symref implementation using find/grep
2
3 ;; Copyright (C) 2008-2015 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 )
52 "List of major modes and file extension pattern.
53 See find -name man page for format.")
54
55 (defun semantic-symref-derive-find-filepatterns (&optional mode)
56 "Derive a list of file patterns for the current buffer.
57 Looks first in `semantic-symref-filepattern-alist'. If it is not
58 there, it then looks in `auto-mode-alist', and attempts to derive something
59 from that.
60 Optional argument MODE specifies the `major-mode' to test."
61 ;; First, try the filepattern alist.
62 (let* ((mode (or mode major-mode))
63 (pat (cdr (assoc mode semantic-symref-filepattern-alist))))
64 (when (not pat)
65 ;; No hit, try auto-mode-alist.
66 (dolist (X auto-mode-alist)
67 (when (eq (cdr X) mode)
68 ;; Only take in simple patterns, so try to convert this one.
69 (let ((Xp
70 (cond ((string-match "\\\\\\.\\([^\\'>]+\\)\\\\'" (car X))
71 (concat "*." (match-string 1 (car X))))
72 (t nil))))
73 (when Xp
74 (setq pat (cons Xp pat))))
75 )))
76 ;; Convert the list into some find-flags.
77 (cond ((= (length pat) 1)
78 (concat "-name \"" (car pat) "\""))
79 ((consp pat)
80 (concat "\\( "
81 (mapconcat (lambda (s)
82 (concat "-name \"" s "\""))
83 pat
84 " -o ")
85 " \\)"))
86 (t
87 (error "Customize `semantic-symref-filepattern-alist' for %s" major-mode))
88 )))
89
90 (defvar semantic-symref-grep-expand-keywords
91 (condition-case nil
92 (let* ((kw (copy-alist grep-expand-keywords))
93 (C (assoc "<C>" kw))
94 (R (assoc "<R>" kw)))
95 (setcdr C 'grepflags)
96 (setcdr R 'greppattern)
97 kw)
98 (error nil))
99 "Grep expand keywords used when expanding templates for symref.")
100
101 (defun semantic-symref-grep-use-template (rootdir filepattern grepflags greppattern)
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* ((grep-expand-keywords semantic-symref-grep-expand-keywords)
110 (cmd (grep-expand-template
111 (if (memq system-type '(windows-nt ms-dos))
112 ;; grep-find uses '--color=always' on MS-Windows
113 ;; because it wants the colorized output, to show
114 ;; it to the user. By contrast, here we don't show
115 ;; the output, and the SGR escapes get in the way
116 ;; of parsing the output.
117 (replace-regexp-in-string "--color=always" ""
118 grep-find-template t t)
119 grep-find-template)
120 greppattern
121 filepattern
122 rootdir)))
123 ;; For some reason, my default has no <D> in it.
124 (when (string-match "find \\(\\.\\)" cmd)
125 (setq cmd (replace-match rootdir t t cmd 1)))
126 ;;(message "New command: %s" cmd)
127 cmd))
128
129 (defcustom semantic-symref-grep-shell shell-file-name
130 "The shell command to use for executing find/grep.
131 This shell should support pipe redirect syntax."
132 :group 'semantic
133 :type 'string)
134
135 (cl-defmethod semantic-symref-perform-search ((tool semantic-symref-tool-grep))
136 "Perform a search with Grep."
137 ;; Grep doesn't support some types of searches.
138 (let ((st (oref tool :searchtype)))
139 (when (not (eq st 'symbol))
140 (error "Symref impl GREP does not support searchtype of %s" st))
141 )
142 ;; Find the root of the project, and do a find-grep...
143 (let* (;; Find the file patterns to use.
144 (pat (cdr (assoc major-mode semantic-symref-filepattern-alist)))
145 (rootdir (semantic-symref-calculate-rootdir))
146 (filepattern (semantic-symref-derive-find-filepatterns))
147 ;; Grep based flags.
148 (grepflags (cond ((eq (oref tool :resulttype) 'file)
149 "-l ")
150 (t "-n ")))
151 (greppat (cond ((eq (oref tool :searchtype) 'regexp)
152 (oref tool searchfor))
153 (t
154 (shell-quote-argument
155 (concat "\\<" (oref tool searchfor) "\\>")))))
156 ;; Misc
157 (b (get-buffer-create "*Semantic SymRef*"))
158 (ans nil)
159 )
160
161 (with-current-buffer b
162 (erase-buffer)
163 (setq default-directory rootdir)
164
165 (if (not (fboundp 'grep-compute-defaults))
166
167 ;; find . -type f -print0 | xargs -0 -e grep -nH -e
168 ;; Note : I removed -e as it is not posix, nor necessary it seems.
169
170 (let ((cmd (concat "find " default-directory " -type f " filepattern " -print0 "
171 "| xargs -0 grep -H " grepflags "-e " greppat)))
172 ;;(message "Old command: %s" cmd)
173 (call-process semantic-symref-grep-shell nil b nil
174 shell-command-switch cmd)
175 )
176 (let ((cmd (semantic-symref-grep-use-template rootdir filepattern grepflags greppat)))
177 (call-process semantic-symref-grep-shell nil b nil
178 shell-command-switch cmd))
179 ))
180 (setq ans (semantic-symref-parse-tool-output tool b))
181 ;; Return the answer
182 ans))
183
184 (cl-defmethod semantic-symref-parse-tool-output-one-line ((tool semantic-symref-tool-grep))
185 "Parse one line of grep output, and return it as a match list.
186 Moves cursor to end of the match."
187 (cond ((eq (oref tool :resulttype) 'file)
188 ;; Search for files
189 (when (re-search-forward "^\\([^\n]+\\)$" nil t)
190 (match-string 1)))
191 (t
192 (when (re-search-forward "^\\(\\(?:[a-zA-Z]:\\)?[^:\n]+\\):\\([0-9]+\\):" nil t)
193 (cons (string-to-number (match-string 2))
194 (match-string 1))
195 ))))
196
197 (provide 'semantic/symref/grep)
198
199 ;; Local variables:
200 ;; generated-autoload-file: "../loaddefs.el"
201 ;; generated-autoload-load-name: "semantic/symref/grep"
202 ;; End:
203
204 ;;; semantic/symref/grep.el ends here