diff options
| author | Chong Yidong | 2009-09-07 17:26:29 +0000 |
|---|---|---|
| committer | Chong Yidong | 2009-09-07 17:26:29 +0000 |
| commit | 3b4741cdf06da56c01cd094a7877de3773bccb93 (patch) | |
| tree | ca26043890a1afa38245d7a16908062ca86a5865 | |
| parent | 4b38b37a413714b4d32ab39763a390a796363452 (diff) | |
| download | emacs-3b4741cdf06da56c01cd094a7877de3773bccb93.tar.gz emacs-3b4741cdf06da56c01cd094a7877de3773bccb93.zip | |
lisp/cedet/semantic/symref/grep.el: New file.
| -rw-r--r-- | lisp/cedet/semantic/symref/grep.el | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/lisp/cedet/semantic/symref/grep.el b/lisp/cedet/semantic/symref/grep.el new file mode 100644 index 00000000000..3d9e7b43d9a --- /dev/null +++ b/lisp/cedet/semantic/symref/grep.el | |||
| @@ -0,0 +1,196 @@ | |||
| 1 | ;;; semantic/symref/grep.el --- Symref implementation using find/grep | ||
| 2 | |||
| 3 | ;;; Copyright (C) 2008, 2009 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 | (defvar ede-minor-mode) | ||
| 34 | (declare-function ede-toplevel "ede/files") | ||
| 35 | (declare-function ede-project-root-directory "ede/files") | ||
| 36 | |||
| 37 | ;;; Code: | ||
| 38 | |||
| 39 | ;;; GREP | ||
| 40 | |||
| 41 | (defclass semantic-symref-tool-grep (semantic-symref-tool-baseclass) | ||
| 42 | ( | ||
| 43 | ) | ||
| 44 | "A symref tool implementation using grep. | ||
| 45 | This tool uses EDE to find he root of the project, then executes | ||
| 46 | find-grep in the project. The output is parsed for hits | ||
| 47 | and those hits returned.") | ||
| 48 | |||
| 49 | (defvar semantic-symref-filepattern-alist | ||
| 50 | '((c-mode "*.[ch]") | ||
| 51 | (c++-mode "*.[chCH]" "*.[ch]pp" "*.cc" "*.hh") | ||
| 52 | (html-mode "*.s?html" "*.php") | ||
| 53 | ) | ||
| 54 | "List of major modes and file extension pattern regexp. | ||
| 55 | See find -regex man page for format.") | ||
| 56 | |||
| 57 | (defun semantic-symref-derive-find-filepatterns (&optional mode) | ||
| 58 | "Derive a list of file patterns for the current buffer. | ||
| 59 | Looks first in `semantic-symref-filepattern-alist'. If it is not | ||
| 60 | there, it then looks in `auto-mode-alist', and attempts to derive something | ||
| 61 | from that. | ||
| 62 | Optional argument MODE specifies the `major-mode' to test." | ||
| 63 | ;; First, try the filepattern alist. | ||
| 64 | (let* ((mode (or mode major-mode)) | ||
| 65 | (pat (cdr (assoc mode semantic-symref-filepattern-alist)))) | ||
| 66 | (when (not pat) | ||
| 67 | ;; No hit, try auto-mode-alist. | ||
| 68 | (dolist (X auto-mode-alist) | ||
| 69 | (when (eq (cdr X) mode) | ||
| 70 | ;; Only take in simple patterns, so try to convert this one. | ||
| 71 | (let ((Xp | ||
| 72 | (cond ((string-match "\\\\\\.\\([^\\'>]+\\)\\\\'" (car X)) | ||
| 73 | (concat "*." (match-string 1 (car X)))) | ||
| 74 | (t nil)))) | ||
| 75 | (when Xp | ||
| 76 | (setq pat (cons Xp pat)))) | ||
| 77 | ))) | ||
| 78 | ;; Convert the list into some find-flags. | ||
| 79 | (cond ((= (length pat) 1) | ||
| 80 | (concat "-name \"" (car pat) "\"")) | ||
| 81 | ((consp pat) | ||
| 82 | (concat "\\( " | ||
| 83 | (mapconcat (lambda (s) | ||
| 84 | (concat "-name \"" s "\"")) | ||
| 85 | pat | ||
| 86 | " -o ") | ||
| 87 | " \\)")) | ||
| 88 | (t | ||
| 89 | (error "Configuration for `semantic-symref-tool-grep' needed for %s" major-mode)) | ||
| 90 | ))) | ||
| 91 | |||
| 92 | (defvar semantic-symref-grep-expand-keywords | ||
| 93 | (condition-case nil | ||
| 94 | (let* ((kw (copy-alist grep-expand-keywords)) | ||
| 95 | (C (assoc "<C>" kw)) | ||
| 96 | (R (assoc "<R>" kw))) | ||
| 97 | (setcdr C 'grepflags) | ||
| 98 | (setcdr R 'greppattern) | ||
| 99 | kw) | ||
| 100 | (error nil)) | ||
| 101 | "Grep expand keywords used when expanding templates for symref.") | ||
| 102 | |||
| 103 | (defun semantic-symref-grep-use-template (rootdir filepattern grepflags greppattern) | ||
| 104 | "Use the grep template expand feature to create a grep command. | ||
| 105 | ROOTDIR is the root location to run the `find' from. | ||
| 106 | FILEPATTERN is a string represeting find flags for searching file patterns. | ||
| 107 | GREPFLAGS are flags passed to grep, such as -n or -l. | ||
| 108 | GREPPATTERN is the pattren used by grep." | ||
| 109 | ;; We have grep-compute-defaults. Lets use it. | ||
| 110 | (grep-compute-defaults) | ||
| 111 | (let* ((grep-expand-keywords semantic-symref-grep-expand-keywords) | ||
| 112 | (cmd (grep-expand-template grep-find-template | ||
| 113 | greppattern | ||
| 114 | filepattern | ||
| 115 | rootdir))) | ||
| 116 | ;; For some reason, my default has no <D> in it. | ||
| 117 | (when (string-match "find \\(\\.\\)" cmd) | ||
| 118 | (setq cmd (replace-match rootdir t t cmd 1))) | ||
| 119 | ;;(message "New command: %s" cmd) | ||
| 120 | cmd)) | ||
| 121 | |||
| 122 | (defmethod semantic-symref-perform-search ((tool semantic-symref-tool-grep)) | ||
| 123 | "Perform a search with Grep." | ||
| 124 | ;; Grep doesn't support some types of searches. | ||
| 125 | (let ((st (oref tool :searchtype))) | ||
| 126 | (when (not (eq st 'symbol)) | ||
| 127 | (error "Symref impl GREP does not support searchtype of %s" st)) | ||
| 128 | ) | ||
| 129 | ;; Find the root of the project, and do a find-grep... | ||
| 130 | (let* (;; Find the file patterns to use. | ||
| 131 | (pat (cdr (assoc major-mode semantic-symref-filepattern-alist))) | ||
| 132 | (rootdir (cond | ||
| 133 | ;; Project root via EDE. | ||
| 134 | ((eq (oref tool :searchscope) 'project) | ||
| 135 | (let ((rootproj (when (and (featurep 'ede) ede-minor-mode) | ||
| 136 | (ede-toplevel)))) | ||
| 137 | (if rootproj | ||
| 138 | (ede-project-root-directory rootproj) | ||
| 139 | default-directory))) | ||
| 140 | ;; Calculate the target files as just in | ||
| 141 | ;; this directory... cause I'm lazy. | ||
| 142 | ((eq (oref tool :searchscope) 'target) | ||
| 143 | default-directory) | ||
| 144 | )) | ||
| 145 | (filepattern (semantic-symref-derive-find-filepatterns)) | ||
| 146 | ;; Grep based flags. | ||
| 147 | (grepflags (cond ((eq (oref tool :resulttype) 'file) | ||
| 148 | "-l ") | ||
| 149 | (t "-n "))) | ||
| 150 | (greppat (cond ((eq (oref tool :searchtype) 'regexp) | ||
| 151 | (oref tool searchfor)) | ||
| 152 | (t | ||
| 153 | (concat "'\\<" (oref tool searchfor) "\\>'")))) | ||
| 154 | ;; Misc | ||
| 155 | (b (get-buffer-create "*Semantic SymRef*")) | ||
| 156 | (ans nil) | ||
| 157 | ) | ||
| 158 | |||
| 159 | (save-excursion | ||
| 160 | (set-buffer b) | ||
| 161 | (erase-buffer) | ||
| 162 | (setq default-directory rootdir) | ||
| 163 | |||
| 164 | (if (not (fboundp 'grep-compute-defaults)) | ||
| 165 | |||
| 166 | ;; find . -type f -print0 | xargs -0 -e grep -nH -e | ||
| 167 | ;; Note : I removed -e as it is not posix, nor necessary it seems. | ||
| 168 | |||
| 169 | (let ((cmd (concat "find " default-directory " -type f " filepattern " -print0 " | ||
| 170 | "| xargs -0 grep -H " grepflags "-e " greppat))) | ||
| 171 | ;;(message "Old command: %s" cmd) | ||
| 172 | (call-process "sh" nil b nil "-c" cmd) | ||
| 173 | ) | ||
| 174 | (let ((cmd (semantic-symref-grep-use-template rootdir filepattern grepflags greppat))) | ||
| 175 | (call-process "sh" nil b nil "-c" cmd)) | ||
| 176 | )) | ||
| 177 | (setq ans (semantic-symref-parse-tool-output tool b)) | ||
| 178 | ;; Return the answer | ||
| 179 | ans)) | ||
| 180 | |||
| 181 | (defmethod semantic-symref-parse-tool-output-one-line ((tool semantic-symref-tool-grep)) | ||
| 182 | "Parse one line of grep output, and return it as a match list. | ||
| 183 | Moves cursor to end of the match." | ||
| 184 | (cond ((eq (oref tool :resulttype) 'file) | ||
| 185 | ;; Search for files | ||
| 186 | (when (re-search-forward "^\\([^\n]+\\)$" nil t) | ||
| 187 | (match-string 1))) | ||
| 188 | (t | ||
| 189 | (when (re-search-forward "^\\(\\(?:[a-zA-Z]:\\)?[^:\n]+\\):\\([0-9]+\\):" nil t) | ||
| 190 | (cons (string-to-number (match-string 2)) | ||
| 191 | (match-string 1)) | ||
| 192 | )))) | ||
| 193 | |||
| 194 | (provide 'semantic/symref/grep) | ||
| 195 | |||
| 196 | ;;; semantic/symref/grep.el ends here | ||