-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrep-o-matic.el
239 lines (211 loc) · 8.45 KB
/
grep-o-matic.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
;;; grep-o-matic.el --- auto grep word under cursor
;; Copyright (C) 2008-2021 Avi Rozen
;; Author: Avi Rozen <avi.rozen@gmail.com>
;; Keywords: tools, processes, search
;; URL: https://github.com/ZungBang/emacs-grep-o-matic
;; Version: 1.0.7
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This package lets the user launch a search, with a single key
;; combination, for the word under the cursor, in the current
;; repository, or the current directory, or in the set of currently
;; open files.
;;
;; Repository-wide search should work out-of-the-box for vc backends
;; known to Emacs, and may be further enhanced with the
;; repository-root library. Git grep may optionally be used instead of
;; grep in git repositories.
;;
;; Works nicely in combination with grep-a-lot.
;;
;; Installation:
;;
;; 1. Put this file in a directory that is a member of load-path, and
;; byte-compile it (e.g. with `M-x byte-compile-file') for better
;; performance.
;; 2. Add the following to your ~/.emacs:
;; (require 'grep-o-matic)
;; 3. Customize grep-o-matic with `M-x customize-group grep-o-matic'
;;
;; Default Key Bindings:
;;
;; M-] M-/ Search for current word in current repository
;; M-] M-. Search for current word in current directory (recursive)
;; M-] M-, Search for current word in currently opened files
;; Prefix these with C-u to edit the search regexp
;;
;;; Code:
(require 'grep)
(require 'vc)
(eval-when-compile
(unless (featurep 'repository-root)
(defsubst repository-root (filename)
nil)))
(defgroup grep-o-matic nil
"Automation layer for grep.el"
:group 'grep
:group 'convenience
:prefix "grep-o-matic-")
(defcustom grep-o-matic-search-patterns
(list "*.cpp" "*.c" "*.h" "*.awk" "*.sh" "*.py" "*.pl" "[Mm]akefile" "*.el")
"*Search file patterns for use with grep-o-matic-* commands."
:group 'grep-o-matic
:type '(repeat string))
(defcustom grep-o-matic-ask-about-save t
"*If non-nil ask which buffers to save before performing a search.
Otherwise, all modified buffers are saved without asking."
:group 'grep-o-matic
:type 'boolean)
(defcustom grep-o-matic-use-git-grep nil
"*If non-nil use git grep to perfrom search in git
repositories."
:group 'grep-o-matic
:type 'boolean)
(defcustom grep-o-matic-git-grep-template "git grep <C> -n -e <R> -- <F>"
"Template for git grep command. See `grep-template' for
more details."
:group 'grep-o-matic
:type 'string)
(defun grep-o-matic-repository-root (filename)
"Attempt to deduce the current file's repository root directory."
(if (null filename)
default-directory
(let* ((directory (file-name-directory filename))
(backend (vc-backend filename))
(vc_rootdir (if backend
(ignore-errors
(vc-call-backend backend 'root directory))
nil))
(rr_rootdir (if (featurep 'repository-root)
(repository-root filename)
nil)))
(or rr_rootdir vc_rootdir directory))))
(defun grep-o-matic-get-regexp (prompt)
"Get the default regexp or query the user for it."
(let ((regexp (grep-tag-default)))
(if (and (not prompt) regexp)
(progn
(add-to-list 'grep-regexp-history regexp)
regexp)
(grep-read-regexp))))
(defun grep-o-matic-compute-search-patterns ()
"Compute file search glob patterns."
(let ((patterns grep-o-matic-search-patterns)
(extension (if buffer-file-name
(file-name-extension buffer-file-name)
nil))
(filename (if buffer-file-name
(file-name-nondirectory buffer-file-name)
nil)))
(let ((nomatch (or (null filename)
(and filename
(let ((head (car patterns))
(tail (cdr patterns)))
(while (and (not (null head))
(not (string-match (wildcard-to-regexp head) filename)))
(progn
(setq head (car tail))
(setq tail (cdr tail))))
(null head))))))
;; if current file does not match search patterns then select a default pattern
(when nomatch
(setq patterns (list (if extension
(concat "*." extension)
"*"))))
(mapconcat (lambda (s) s) patterns " "))))
(defun grep-o-matic-directory (prompt directory)
"Search directory for word at point.
Optionaly prompt for regexp to search."
(let ((patterns (grep-o-matic-compute-search-patterns)))
(grep-compute-defaults)
(save-some-buffers (not grep-o-matic-ask-about-save) nil)
(rgrep (grep-o-matic-get-regexp prompt)
patterns
(if directory
directory
default-directory))))
(defun grep-o-matic-git-repository (prompt repository-root)
"Search a git repository for word at point.
Optionaly prompt for regexp to search."
(interactive "P")
(let ((regexp (grep-o-matic-get-regexp prompt))
(patterns (grep-o-matic-compute-search-patterns)))
(grep-compute-defaults)
(save-some-buffers (not compilation-ask-about-save) nil)
(let ((default-directory repository-root))
;; Running git grep with no pager (as is necessary) does not play
;; well with font-locking, so that next/previous-error do not
;; work at all. So we pipe the output of 'git grep' through a
;; dummy 'cat'.
(grep (grep-expand-template
(concat (grep-expand-template
grep-o-matic-git-grep-template
regexp
patterns)
" | cat"))))))
;;;###autoload
(defun grep-o-matic-repository (&optional prompt)
"Search repository for word at point.
Optionaly prompt for regexp to search."
(interactive "P")
(let ((repository-root (grep-o-matic-repository-root buffer-file-name)))
(if (and grep-o-matic-use-git-grep
(string-equal "git"
(downcase (symbol-name (vc-backend
buffer-file-name)))))
(grep-o-matic-git-repository prompt repository-root)
(grep-o-matic-directory prompt repository-root))))
;;;###autoload
(defun grep-o-matic-current-directory (&optional prompt)
"Search current directory for word at point.
Optionaly prompt for regexp to search."
(interactive "P")
(grep-o-matic-directory prompt (if buffer-file-name
(file-name-directory buffer-file-name)
nil)))
;;;###autoload
(defun grep-o-matic-visited-files (&optional prompt)
"Search currently visited files for word at point.
Optionaly prompt for regexp to search."
(interactive "P")
(let ((regexp (grep-o-matic-get-regexp prompt))
(files (let ((directory-abbrev-alist
(cons (cons (regexp-quote (expand-file-name default-directory)) "./") directory-abbrev-alist)))
(mapconcat (lambda (fn) (abbreviate-file-name (shell-quote-argument fn)))
(apply 'nconc
(mapcar #'(lambda (buffer)
(let ((file (buffer-file-name buffer)))
(if (and file
(not (file-remote-p file)))
(list file))))
(buffer-list)))
" ")))
(dir "")
(excl ""))
(progn
(grep-compute-defaults)
(save-some-buffers (not compilation-ask-about-save) nil)
(grep (grep-expand-template grep-template regexp files dir excl)))))
;; key bindings
(define-prefix-command 'grep-o-matic-map)
(define-key 'grep-o-matic-map "\M-/" 'grep-o-matic-repository)
(define-key 'grep-o-matic-map "/" 'grep-o-matic-repository)
(define-key 'grep-o-matic-map "\M-." 'grep-o-matic-current-directory)
(define-key 'grep-o-matic-map "." 'grep-o-matic-current-directory)
(define-key 'grep-o-matic-map "\M-," 'grep-o-matic-visited-files)
(define-key 'grep-o-matic-map "," 'grep-o-matic-visited-files)
(global-set-key "\M-]" 'grep-o-matic-map)
(provide 'grep-o-matic)
;;; grep-o-matic.el ends here