Commit | Line | Data |
---|---|---|
3b4741cd CY |
1 | ;;; semantic/symref/grep.el --- Symref implementation using find/grep |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2008-2011 Free Software Foundation, Inc. |
3b4741cd CY |
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 | ||
3b4741cd CY |
33 | ;;; Code: |
34 | ||
35 | ;;; GREP | |
2182c2cc | 36 | ;;;###autoload |
3b4741cd CY |
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 | ) | |
50 | "List of major modes and file extension pattern regexp. | |
51 | See find -regex man page for format.") | |
52 | ||
53 | (defun semantic-symref-derive-find-filepatterns (&optional mode) | |
54 | "Derive a list of file patterns for the current buffer. | |
55 | Looks first in `semantic-symref-filepattern-alist'. If it is not | |
56 | there, it then looks in `auto-mode-alist', and attempts to derive something | |
57 | from that. | |
58 | Optional argument MODE specifies the `major-mode' to test." | |
59 | ;; First, try the filepattern alist. | |
60 | (let* ((mode (or mode major-mode)) | |
61 | (pat (cdr (assoc mode semantic-symref-filepattern-alist)))) | |
62 | (when (not pat) | |
63 | ;; No hit, try auto-mode-alist. | |
64 | (dolist (X auto-mode-alist) | |
65 | (when (eq (cdr X) mode) | |
66 | ;; Only take in simple patterns, so try to convert this one. | |
67 | (let ((Xp | |
68 | (cond ((string-match "\\\\\\.\\([^\\'>]+\\)\\\\'" (car X)) | |
69 | (concat "*." (match-string 1 (car X)))) | |
70 | (t nil)))) | |
71 | (when Xp | |
72 | (setq pat (cons Xp pat)))) | |
73 | ))) | |
74 | ;; Convert the list into some find-flags. | |
75 | (cond ((= (length pat) 1) | |
76 | (concat "-name \"" (car pat) "\"")) | |
77 | ((consp pat) | |
78 | (concat "\\( " | |
79 | (mapconcat (lambda (s) | |
80 | (concat "-name \"" s "\"")) | |
81 | pat | |
82 | " -o ") | |
83 | " \\)")) | |
84 | (t | |
dd9af436 | 85 | (error "Customize `semantic-symref-filepattern-alist' for %s" major-mode)) |
3b4741cd CY |
86 | ))) |
87 | ||
88 | (defvar semantic-symref-grep-expand-keywords | |
89 | (condition-case nil | |
90 | (let* ((kw (copy-alist grep-expand-keywords)) | |
91 | (C (assoc "<C>" kw)) | |
92 | (R (assoc "<R>" kw))) | |
93 | (setcdr C 'grepflags) | |
94 | (setcdr R 'greppattern) | |
95 | kw) | |
96 | (error nil)) | |
97 | "Grep expand keywords used when expanding templates for symref.") | |
98 | ||
99 | (defun semantic-symref-grep-use-template (rootdir filepattern grepflags greppattern) | |
100 | "Use the grep template expand feature to create a grep command. | |
101 | ROOTDIR is the root location to run the `find' from. | |
9bf6c65c | 102 | FILEPATTERN is a string representing find flags for searching file patterns. |
3b4741cd | 103 | GREPFLAGS are flags passed to grep, such as -n or -l. |
9bf6c65c | 104 | GREPPATTERN is the pattern used by grep." |
3b4741cd CY |
105 | ;; We have grep-compute-defaults. Lets use it. |
106 | (grep-compute-defaults) | |
107 | (let* ((grep-expand-keywords semantic-symref-grep-expand-keywords) | |
108 | (cmd (grep-expand-template grep-find-template | |
109 | greppattern | |
110 | filepattern | |
111 | rootdir))) | |
112 | ;; For some reason, my default has no <D> in it. | |
113 | (when (string-match "find \\(\\.\\)" cmd) | |
114 | (setq cmd (replace-match rootdir t t cmd 1))) | |
115 | ;;(message "New command: %s" cmd) | |
116 | cmd)) | |
117 | ||
dd9af436 CY |
118 | (defcustom semantic-symref-grep-shell "sh" |
119 | "The shell command to use for executing find/grep. | |
120 | This shell should support pipe redirect syntax." | |
121 | :group 'semantic | |
122 | :type 'string) | |
123 | ||
3b4741cd CY |
124 | (defmethod semantic-symref-perform-search ((tool semantic-symref-tool-grep)) |
125 | "Perform a search with Grep." | |
126 | ;; Grep doesn't support some types of searches. | |
127 | (let ((st (oref tool :searchtype))) | |
128 | (when (not (eq st 'symbol)) | |
129 | (error "Symref impl GREP does not support searchtype of %s" st)) | |
130 | ) | |
131 | ;; Find the root of the project, and do a find-grep... | |
132 | (let* (;; Find the file patterns to use. | |
133 | (pat (cdr (assoc major-mode semantic-symref-filepattern-alist))) | |
dd9af436 | 134 | (rootdir (semantic-symref-calculate-rootdir)) |
3b4741cd CY |
135 | (filepattern (semantic-symref-derive-find-filepatterns)) |
136 | ;; Grep based flags. | |
137 | (grepflags (cond ((eq (oref tool :resulttype) 'file) | |
138 | "-l ") | |
139 | (t "-n "))) | |
140 | (greppat (cond ((eq (oref tool :searchtype) 'regexp) | |
141 | (oref tool searchfor)) | |
142 | (t | |
143 | (concat "'\\<" (oref tool searchfor) "\\>'")))) | |
144 | ;; Misc | |
145 | (b (get-buffer-create "*Semantic SymRef*")) | |
146 | (ans nil) | |
147 | ) | |
148 | ||
0816d744 | 149 | (with-current-buffer b |
3b4741cd CY |
150 | (erase-buffer) |
151 | (setq default-directory rootdir) | |
152 | ||
153 | (if (not (fboundp 'grep-compute-defaults)) | |
154 | ||
155 | ;; find . -type f -print0 | xargs -0 -e grep -nH -e | |
156 | ;; Note : I removed -e as it is not posix, nor necessary it seems. | |
157 | ||
158 | (let ((cmd (concat "find " default-directory " -type f " filepattern " -print0 " | |
159 | "| xargs -0 grep -H " grepflags "-e " greppat))) | |
160 | ;;(message "Old command: %s" cmd) | |
dd9af436 | 161 | (call-process semantic-symref-grep-shell nil b nil "-c" cmd) |
3b4741cd CY |
162 | ) |
163 | (let ((cmd (semantic-symref-grep-use-template rootdir filepattern grepflags greppat))) | |
dd9af436 | 164 | (call-process semantic-symref-grep-shell nil b nil "-c" cmd)) |
3b4741cd CY |
165 | )) |
166 | (setq ans (semantic-symref-parse-tool-output tool b)) | |
167 | ;; Return the answer | |
168 | ans)) | |
169 | ||
170 | (defmethod semantic-symref-parse-tool-output-one-line ((tool semantic-symref-tool-grep)) | |
171 | "Parse one line of grep output, and return it as a match list. | |
172 | Moves cursor to end of the match." | |
173 | (cond ((eq (oref tool :resulttype) 'file) | |
174 | ;; Search for files | |
175 | (when (re-search-forward "^\\([^\n]+\\)$" nil t) | |
176 | (match-string 1))) | |
177 | (t | |
178 | (when (re-search-forward "^\\(\\(?:[a-zA-Z]:\\)?[^:\n]+\\):\\([0-9]+\\):" nil t) | |
179 | (cons (string-to-number (match-string 2)) | |
180 | (match-string 1)) | |
181 | )))) | |
182 | ||
183 | (provide 'semantic/symref/grep) | |
184 | ||
2182c2cc CY |
185 | ;; Local variables: |
186 | ;; generated-autoload-file: "../loaddefs.el" | |
2182c2cc CY |
187 | ;; generated-autoload-load-name: "semantic/symref/grep" |
188 | ;; End: | |
189 | ||
3b4741cd | 190 | ;;; semantic/symref/grep.el ends here |