grep.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ;;; semantic/symref/grep.el --- Symref implementation using find/grep
  2. ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Implement the symref tool API using the external tools find/grep.
  18. ;;
  19. ;; The symref GREP tool uses grep in a project to find symbol references.
  20. ;; This is a lowest-common-denominator tool with sucky performance that
  21. ;; can be used in small projects to find symbol references.
  22. (require 'semantic/symref)
  23. (require 'grep)
  24. ;;; Code:
  25. ;;; GREP
  26. ;;;###autoload
  27. (defclass semantic-symref-tool-grep (semantic-symref-tool-baseclass)
  28. (
  29. )
  30. "A symref tool implementation using grep.
  31. This tool uses EDE to find he root of the project, then executes
  32. find-grep in the project. The output is parsed for hits
  33. and those hits returned.")
  34. (defvar semantic-symref-filepattern-alist
  35. '((c-mode "*.[ch]")
  36. (c++-mode "*.[chCH]" "*.[ch]pp" "*.cc" "*.hh")
  37. (html-mode "*.s?html" "*.php")
  38. )
  39. "List of major modes and file extension pattern regexp.
  40. See find -regex man page for format.")
  41. (defun semantic-symref-derive-find-filepatterns (&optional mode)
  42. "Derive a list of file patterns for the current buffer.
  43. Looks first in `semantic-symref-filepattern-alist'. If it is not
  44. there, it then looks in `auto-mode-alist', and attempts to derive something
  45. from that.
  46. Optional argument MODE specifies the `major-mode' to test."
  47. ;; First, try the filepattern alist.
  48. (let* ((mode (or mode major-mode))
  49. (pat (cdr (assoc mode semantic-symref-filepattern-alist))))
  50. (when (not pat)
  51. ;; No hit, try auto-mode-alist.
  52. (dolist (X auto-mode-alist)
  53. (when (eq (cdr X) mode)
  54. ;; Only take in simple patterns, so try to convert this one.
  55. (let ((Xp
  56. (cond ((string-match "\\\\\\.\\([^\\'>]+\\)\\\\'" (car X))
  57. (concat "*." (match-string 1 (car X))))
  58. (t nil))))
  59. (when Xp
  60. (setq pat (cons Xp pat))))
  61. )))
  62. ;; Convert the list into some find-flags.
  63. (cond ((= (length pat) 1)
  64. (concat "-name \"" (car pat) "\""))
  65. ((consp pat)
  66. (concat "\\( "
  67. (mapconcat (lambda (s)
  68. (concat "-name \"" s "\""))
  69. pat
  70. " -o ")
  71. " \\)"))
  72. (t
  73. (error "Customize `semantic-symref-filepattern-alist' for %s" major-mode))
  74. )))
  75. (defvar semantic-symref-grep-expand-keywords
  76. (condition-case nil
  77. (let* ((kw (copy-alist grep-expand-keywords))
  78. (C (assoc "<C>" kw))
  79. (R (assoc "<R>" kw)))
  80. (setcdr C 'grepflags)
  81. (setcdr R 'greppattern)
  82. kw)
  83. (error nil))
  84. "Grep expand keywords used when expanding templates for symref.")
  85. (defun semantic-symref-grep-use-template (rootdir filepattern grepflags greppattern)
  86. "Use the grep template expand feature to create a grep command.
  87. ROOTDIR is the root location to run the `find' from.
  88. FILEPATTERN is a string representing find flags for searching file patterns.
  89. GREPFLAGS are flags passed to grep, such as -n or -l.
  90. GREPPATTERN is the pattern used by grep."
  91. ;; We have grep-compute-defaults. Let's use it.
  92. (grep-compute-defaults)
  93. (let* ((grep-expand-keywords semantic-symref-grep-expand-keywords)
  94. (cmd (grep-expand-template grep-find-template
  95. greppattern
  96. filepattern
  97. rootdir)))
  98. ;; For some reason, my default has no <D> in it.
  99. (when (string-match "find \\(\\.\\)" cmd)
  100. (setq cmd (replace-match rootdir t t cmd 1)))
  101. ;;(message "New command: %s" cmd)
  102. cmd))
  103. (defcustom semantic-symref-grep-shell "sh"
  104. "The shell command to use for executing find/grep.
  105. This shell should support pipe redirect syntax."
  106. :group 'semantic
  107. :type 'string)
  108. (defmethod semantic-symref-perform-search ((tool semantic-symref-tool-grep))
  109. "Perform a search with Grep."
  110. ;; Grep doesn't support some types of searches.
  111. (let ((st (oref tool :searchtype)))
  112. (when (not (eq st 'symbol))
  113. (error "Symref impl GREP does not support searchtype of %s" st))
  114. )
  115. ;; Find the root of the project, and do a find-grep...
  116. (let* (;; Find the file patterns to use.
  117. (pat (cdr (assoc major-mode semantic-symref-filepattern-alist)))
  118. (rootdir (semantic-symref-calculate-rootdir))
  119. (filepattern (semantic-symref-derive-find-filepatterns))
  120. ;; Grep based flags.
  121. (grepflags (cond ((eq (oref tool :resulttype) 'file)
  122. "-l ")
  123. (t "-n ")))
  124. (greppat (cond ((eq (oref tool :searchtype) 'regexp)
  125. (oref tool searchfor))
  126. (t
  127. (concat "'\\<" (oref tool searchfor) "\\>'"))))
  128. ;; Misc
  129. (b (get-buffer-create "*Semantic SymRef*"))
  130. (ans nil)
  131. )
  132. (with-current-buffer b
  133. (erase-buffer)
  134. (setq default-directory rootdir)
  135. (if (not (fboundp 'grep-compute-defaults))
  136. ;; find . -type f -print0 | xargs -0 -e grep -nH -e
  137. ;; Note : I removed -e as it is not posix, nor necessary it seems.
  138. (let ((cmd (concat "find " default-directory " -type f " filepattern " -print0 "
  139. "| xargs -0 grep -H " grepflags "-e " greppat)))
  140. ;;(message "Old command: %s" cmd)
  141. (call-process semantic-symref-grep-shell nil b nil "-c" cmd)
  142. )
  143. (let ((cmd (semantic-symref-grep-use-template rootdir filepattern grepflags greppat)))
  144. (call-process semantic-symref-grep-shell nil b nil "-c" cmd))
  145. ))
  146. (setq ans (semantic-symref-parse-tool-output tool b))
  147. ;; Return the answer
  148. ans))
  149. (defmethod semantic-symref-parse-tool-output-one-line ((tool semantic-symref-tool-grep))
  150. "Parse one line of grep output, and return it as a match list.
  151. Moves cursor to end of the match."
  152. (cond ((eq (oref tool :resulttype) 'file)
  153. ;; Search for files
  154. (when (re-search-forward "^\\([^\n]+\\)$" nil t)
  155. (match-string 1)))
  156. (t
  157. (when (re-search-forward "^\\(\\(?:[a-zA-Z]:\\)?[^:\n]+\\):\\([0-9]+\\):" nil t)
  158. (cons (string-to-number (match-string 2))
  159. (match-string 1))
  160. ))))
  161. (provide 'semantic/symref/grep)
  162. ;; Local variables:
  163. ;; generated-autoload-file: "../loaddefs.el"
  164. ;; generated-autoload-load-name: "semantic/symref/grep"
  165. ;; End:
  166. ;;; semantic/symref/grep.el ends here