anything-grep.el (agrep-do-grep): use default-directory instead of cd in shell command
[anything-config.git] / contrib / anything-grep.el
blobf9388ea8e16ad20dc62f2d2016346484fd82bd0f
1 ;;; anything-grep.el --- search refinement of grep result with anything
2 ;; $Id: anything-grep.el,v 1.27 2010-03-21 11:31:04 rubikitch Exp $
4 ;; Copyright (C) 2008, 2009, 2010 rubikitch
6 ;; Author: rubikitch <rubikitch@ruby-lang.org>
7 ;; Keywords: convenience, unix
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-grep.el
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; Do grep in anything buffer. When we search information with grep,
28 ;; we often narrow the candidates. Let's use `anything' to do it.
30 ;;; Commands:
32 ;; Below are complete command list:
34 ;; `anything-grep'
35 ;; Run grep in `anything' buffer to narrow results.
36 ;; `anything-grep-by-name'
37 ;; Do `anything-grep' from predefined location.
38 ;; `anything-grep-by-name-reversed'
39 ;; Do `anything-grep' from predefined location.
41 ;;; Customizable Options:
43 ;; Below are customizable option list:
46 ;; `anything-grep' is simple interface to grep a query. It asks
47 ;; directory to grep. The grep process is synchronous process. You may
48 ;; have to wait when you grep the target for the first time. But once
49 ;; the target is on the disk cache, queries are grepped at lightning
50 ;; speed. Even if older Pentium4 computer, grepping from 180MB takes
51 ;; only 0.2s! GNU grep is amazingly fast.
53 ;; `anything-grep-by-name' asks query and predefined location. It is
54 ;; good idea to have ack (ack-grep), grep implemented in Perl, to
55 ;; exclude unneeded files. Such as RCS, .svn and so on.
57 ;; ack -- better than grep, a power search tool for programmers
58 ;; http://petdance.com/ack/
62 ;;; History:
64 ;; $Log: anything-grep.el,v $
65 ;; Revision 1.27 2010-03-21 11:31:04 rubikitch
66 ;; Resume bug fix
68 ;; Revision 1.26 2010/03/21 11:13:30 rubikitch
69 ;; `anything-grep' works asynchronously
71 ;; Revision 1.25 2010/03/21 06:34:25 rubikitch
72 ;; New function: `anything-grep-by-name-reversed'
74 ;; Revision 1.24 2010/03/21 06:28:42 rubikitch
75 ;; update copyright
77 ;; Revision 1.23 2010/03/21 06:28:32 rubikitch
78 ;; refactoring
80 ;; Revision 1.22 2009/12/28 08:56:56 rubikitch
81 ;; `anything-grep-by-name': INCOMPATIBLE!!! swap optional arguments
82 ;; `anything-grep-by-name' can utilize `repeat-complex-command'.
84 ;; Revision 1.21 2009/12/18 11:01:11 rubikitch
85 ;; `agrep-real-to-display': erase "nil" message
87 ;; Revision 1.20 2009/06/25 03:36:38 rubikitch
88 ;; `agrep-real-to-display': avoid error
89 ;; auto-document
91 ;; Revision 1.19 2009/02/03 21:06:49 rubikitch
92 ;; fontify file name and line number.
93 ;; New variable: `anything-grep-fontify-file-name'
95 ;; Revision 1.18 2009/02/03 20:48:12 rubikitch
96 ;; multi-line support.
97 ;; New variable: `anything-grep-multiline'
99 ;; Revision 1.17 2009/02/03 20:35:03 rubikitch
100 ;; Use `anything-quit-if-no-candidate' not to open *anything* buffer when no matches found.
102 ;; Revision 1.16 2009/01/20 09:56:19 rubikitch
103 ;; New variable: `anything-grep-filter-command'
105 ;; Revision 1.15 2009/01/03 07:04:30 rubikitch
106 ;; copyright
108 ;; Revision 1.14 2009/01/02 16:00:07 rubikitch
109 ;; * Fixed invalid value of `anything-grep-alist'.
110 ;; * Implemented functionality to search all buffers with `buffer-file-name'.
111 ;; See `anything-grep-alist'.
113 ;; Revision 1.13 2008/12/29 09:43:59 rubikitch
114 ;; Rename variables:
115 ;; `agrep-goto-hook' => `anything-grep-goto-hook'
116 ;; `agrep-find-file-function' => `anything-grep-find-file-function'
118 ;; Revision 1.12 2008/12/29 09:40:23 rubikitch
119 ;; document
121 ;; Revision 1.11 2008/12/29 07:58:37 rubikitch
122 ;; refactoring
124 ;; Revision 1.10 2008/10/21 18:02:02 rubikitch
125 ;; use *anything grep* buffer instead.
127 ;; Revision 1.9 2008/10/12 17:17:23 rubikitch
128 ;; `anything-grep-by-name': swapped query order
130 ;; Revision 1.8 2008/10/09 00:33:40 rubikitch
131 ;; New variable: `anything-grep-save-buffers-before-grep'
133 ;; Revision 1.7 2008/10/09 00:26:00 rubikitch
134 ;; `anything-grep-by-name': nil argument
136 ;; Revision 1.6 2008/10/05 15:43:09 rubikitch
137 ;; changed spec: `anything-grep-alist'
139 ;; Revision 1.5 2008/10/02 18:27:55 rubikitch
140 ;; Use original fontify code instead of font-lock.
141 ;; New variable: `agrep-find-file-function'
143 ;; Revision 1.4 2008/10/01 18:18:18 rubikitch
144 ;; use ack-grep command to select files for search.
146 ;; Revision 1.3 2008/10/01 17:18:59 rubikitch
147 ;; silence byte compiler
149 ;; Revision 1.2 2008/10/01 17:17:59 rubikitch
150 ;; many bug fix
151 ;; New command: `anything-grep-by-name'
153 ;; Revision 1.1 2008/10/01 10:58:59 rubikitch
154 ;; Initial revision
157 ;;; Code:
159 (defvar anything-grep-version "$Id: anything-grep.el,v 1.27 2010-03-21 11:31:04 rubikitch Exp $")
160 (require 'anything)
161 (require 'grep)
163 (defvar anything-grep-save-buffers-before-grep nil
164 "Do `save-some-buffers' before performing `anything-grep'.")
166 (defvar anything-grep-goto-hook nil
167 "List of functions to be called after `agrep-goto' opens file.")
169 (defvar anything-grep-find-file-function 'find-file
170 "Function to visit a file with.
171 It takes one argument, a file name to visit.")
173 (defvar anything-grep-multiline t
174 "If non-nil, use multi-line display. It is prettier.
175 Use anything.el v1.147 or newer.")
177 (defvar anything-grep-fontify-file-name t
178 "If non-nil, fontify file name and line number of matches.")
180 (defvar anything-grep-alist
181 '(("buffers" ("egrep -Hin %s $buffers" "/"))
182 ("memo" ("ack-grep -af | xargs egrep -Hin %s" "~/memo"))
183 ("PostgreSQL" ("egrep -Hin %s *.txt" "~/doc/postgresql-74/"))
184 ("~/bin and ~/ruby"
185 ("ack-grep -afG 'rb$' | xargs egrep -Hin %s" "~/ruby")
186 ("ack-grep -af | xargs egrep -Hin %s" "~/bin")))
187 "Mapping of location and command/pwd used by `anything-grep-by-name'.
188 The command is grep command line. Note that %s is replaced by query.
189 The command is typically \"ack-grep -af | xargs egrep -Hin %s\", which means
190 regexp/case-insensitive search for all files (including subdirectories)
191 except unneeded files.
192 The occurrence of $file in command is replaced with `buffer-file-name' of
193 all buffers.
195 The pwd is current directory to grep.
197 The format is:
199 ((LOCATION1
200 (COMMAND1-1 PWD1-1)
201 (COMMAND1-2 PWD1-2)
202 ...)
203 (LOCATION2
204 (COMMAND2-1 PWD2-1)
205 (COMMAND2-2 PWD2-2)
206 ...)
207 ...)
210 (defvar anything-grep-filter-command nil
211 "If non-nil, filter the result of grep command.
213 For example, normalizing many Japanese encodings to EUC-JP,
214 set this variable to \"ruby -rkconv -pe '$_.replace $_.toeuc'\".
215 The command is converting standard input to EUC-JP line by line. ")
218 ;; (@* "core")
219 (defvar anything-grep-sources nil
220 "`anything-sources' for last invoked `anything-grep'.")
221 (defvar anything-grep-buffer-name nil)
222 (defun anything-grep-base (sources &optional bufname)
223 "Invoke `anything' for `anything-grep'."
224 (and anything-grep-save-buffers-before-grep
225 (save-some-buffers (not compilation-ask-about-save) nil))
226 (setq anything-grep-sources sources)
227 (setq anything-grep-buffer-name (or bufname "*anything grep*"))
228 (let ((anything-quit-if-no-candidate t)
229 (anything-compile-source-functions
230 (cons 'anything-compile-source--agrep-init anything-compile-source-functions)))
231 (anything sources nil nil nil nil bufname)))
233 ;; (anything (list (agrep-source "grep -Hin agrep anything-grep.el" default-directory) (agrep-source "grep -Hin pwd anything-grep.el" default-directory)))
235 (defun agrep-source (command pwd)
236 "Anything Source of `anything-grep'."
237 `((command . ,command)
238 (pwd . ,pwd)
239 (name . ,(format "%s [%s]" command pwd))
240 (action . agrep-goto)
241 (anything-grep)
242 (candidate-number-limit . 9999)
243 (migemo)
244 ;; to inherit faces
245 (candidates-in-buffer)
246 (get-line . buffer-substring)
247 ,@(when anything-grep-multiline
248 '((multiline)
249 (real-to-display . agrep-real-to-display)))))
251 (defun anything-compile-source--agrep-init (source)
252 (if (assq 'anything-grep source)
253 (append '((init . agrep-init)
254 (candidates)) source)
255 source))
257 (defun agrep-init ()
258 (agrep-create-buffer (anything-attr 'command) (anything-attr 'pwd)))
260 (defun agrep-real-to-display (file-line-content)
261 (if (string-match ":\\([0-9]+\\):" file-line-content)
262 (format "%s:%s\n %s"
263 (substring file-line-content 0 (match-beginning 0))
264 (match-string 1 file-line-content)
265 (substring file-line-content (match-end 0)))
266 file-line-content))
268 (defvar agrep-source-local nil)
269 (defvar agrep-waiting-source nil
270 "`anything' sources to get together in `agrep-sentinel'.")
271 (defun agrep-do-grep (command pwd)
272 "Insert result of COMMAND. The current directory is PWD.
273 GNU grep is expected for COMMAND. The grep result is colorized."
274 (let ((process-environment process-environment))
275 (when (eq grep-highlight-matches t)
276 ;; Modify `process-environment' locally bound in `call-process-shell-command'.
277 (setenv "GREP_OPTIONS" (concat (getenv "GREP_OPTIONS") " --color=always"))
278 ;; for GNU grep 2.5.1
279 (setenv "GREP_COLOR" "01;31")
280 ;; for GNU grep 2.5.1-cvs
281 (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:ml=:cx=:ne"))
282 (set (make-local-variable 'agrep-source-local) (anything-get-current-source))
283 (add-to-list 'agrep-waiting-source agrep-source-local)
284 (set-process-sentinel
285 (let ((default-directory (expand-file-name pwd)))
286 (start-process-shell-command "anything-grep" (current-buffer)
287 command))
288 'agrep-sentinel)))
290 (defvar agrep-do-after-minibuffer-exit nil)
291 (defun agrep-minibuffer-exit-hook ()
292 (when agrep-do-after-minibuffer-exit
293 (run-at-time 1 nil agrep-do-after-minibuffer-exit)
294 (setq agrep-do-after-minibuffer-exit nil)))
295 (add-hook 'minibuffer-exit-hook 'agrep-minibuffer-exit-hook)
297 (defun agrep-show (func)
298 (if (active-minibuffer-window)
299 (setq agrep-do-after-minibuffer-exit func)
300 (funcall func)))
301 ;; (anything-grep "sleep 1; grep -Hin grep anything-grep.el" "~/src/anything-config/extensions/")
303 (defun agrep-sentinel (proc stat)
304 (with-current-buffer (process-buffer proc)
305 (setq agrep-waiting-source (delete agrep-source-local agrep-waiting-source))
306 (agrep-fontify))
307 (unless agrep-waiting-source
308 ;; call anything
309 (agrep-show
310 (lambda ()
311 (let ((anything-quit-if-no-candidate (lambda () (message "No matches"))))
312 (anything anything-grep-sources nil nil nil nil anything-grep-buffer-name))))))
314 (defun agrep-fontify ()
315 "Fontify the result of `agrep-do-grep'."
316 ;; Color matches.
317 (goto-char 1)
318 (while (re-search-forward "\\(\033\\[01;31m\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)" nil t)
319 (put-text-property (match-beginning 2) (match-end 2) 'face grep-match-face)
320 (replace-match "" t t nil 1)
321 (replace-match "" t t nil 3))
322 ;; Delete other escape sequences.
323 (goto-char 1)
324 (while (re-search-forward "\\(\033\\[[0-9;]*[mK]\\)" nil t)
325 (replace-match "" t t nil 0))
326 (when anything-grep-fontify-file-name
327 (goto-char 1)
328 (while (re-search-forward ":\\([0-9]+\\):" nil t)
329 (put-text-property (point-at-bol) (match-beginning 0) 'face compilation-info-face)
330 (put-text-property (match-beginning 1) (match-end 1) 'face compilation-line-face)
331 (forward-line 1))))
332 ;; (anything-grep "grep -n grep *.el" "~/emacs/init.d")
334 (defun agrep-create-buffer (command pwd)
335 "Create candidate buffer for `anything-grep'.
336 Its contents is fontified grep result."
337 (with-current-buffer (anything-candidate-buffer 'global)
338 (setq default-directory pwd)
339 (agrep-do-grep command pwd)
340 (current-buffer)))
341 ;; (display-buffer (agrep-create-buffer "grep --color=always -Hin agrep anything-grep.el" default-directory))
342 ;; (anything '(((name . "test") (init . (lambda () (anything-candidate-buffer (get-buffer " *anything grep:grep --color=always -Hin agrep anything-grep.el*")) )) (candidates-in-buffer) (get-line . buffer-substring))))
344 (defun agrep-goto (file-line-content)
345 "Visit the source for the grep result at point."
346 (string-match ":\\([0-9]+\\):" file-line-content)
347 (save-match-data
348 (funcall anything-grep-find-file-function
349 (expand-file-name (substring file-line-content
350 0 (match-beginning 0))
351 (anything-attr 'pwd))))
352 (goto-line (string-to-number (match-string 1 file-line-content)))
353 (run-hooks 'anything-grep-goto-hook))
355 ;; (@* "simple grep interface")
356 (defun anything-grep (command pwd)
357 "Run grep in `anything' buffer to narrow results.
358 It asks COMMAND for grep command line and PWD for current directory."
359 (interactive
360 (progn
361 (grep-compute-defaults)
362 (let ((default (grep-default-command)))
363 (list (read-from-minibuffer "Run grep (like this): "
364 (if current-prefix-arg
365 default grep-command)
366 nil nil 'grep-history
367 (if current-prefix-arg nil default))
368 (read-directory-name "Directory: " default-directory default-directory t)))))
369 (anything-grep-base (list (agrep-source (agrep-preprocess-command command) pwd))
370 (format "*anything grep:%s [%s]*" command (abbreviate-file-name pwd))))
371 ;; (anything-grep "grep -Hin agrep anything-grep.el" default-directory)
373 (defun agrep-preprocess-command (command)
374 (with-temp-buffer
375 (insert command)
376 (goto-char 1)
377 (when (search-forward "$buffers" nil t)
378 (delete-region (match-beginning 0) (match-end 0))
379 (insert (mapconcat 'shell-quote-argument
380 (delq nil (mapcar 'buffer-file-name (buffer-list))) " ")))
381 (when anything-grep-filter-command
382 (goto-char (point-max))
383 (insert "|" anything-grep-filter-command))
384 (buffer-string)))
386 ;; (@* "grep in predefined files")
387 (defvar agbn-last-name nil
388 "The last used name by `anything-grep-by-name'.")
390 (defun agrep-by-name-read-info (&rest kinds)
391 (let* ((default (or (thing-at-point 'symbol) ""))
392 (result (mapcar (lambda (kind)
393 (case kind
394 ('query (read-string
395 (format "Grep query (default:%s): " default)
396 nil nil default))
397 ('name (completing-read
398 "Grep by name: "
399 anything-grep-alist
400 nil t nil nil agbn-last-name))))
401 kinds)))
402 (if (cdr result) ; length >= 1
403 result
404 (car result))))
406 (defun anything-grep-by-name (&optional query name)
407 "Do `anything-grep' from predefined location.
408 It asks NAME for location name and QUERY."
409 (interactive (agrep-by-name-read-info 'query 'name))
410 (setq query (or query (agrep-by-name-read-info 'query)))
411 (setq name (or name (agrep-by-name-read-info 'name)))
412 (setq agbn-last-name name)
413 (anything-aif (assoc-default name anything-grep-alist)
414 (progn
415 (grep-compute-defaults)
416 (anything-grep-base
417 (mapcar (lambda (args)
418 (destructuring-bind (cmd dir) args
419 (agrep-source (format (agrep-preprocess-command cmd)
420 (shell-quote-argument query)) dir)))
422 (format "*anything grep:%s [%s]" query name)))
423 (error "no such name %s" name)))
425 (defun anything-grep-by-name-reversed (&optional name query)
426 "Do `anything-grep' from predefined location.
427 It asks QUERY and NAME for location name.
429 Difference with `anything-grep-by-name' is prompt order."
430 (interactive (agrep-by-name-read-info (quote name) (quote query)))
431 (anything-grep-by-name query name))
433 ;;;; unit test
434 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
435 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
436 (dont-compile
437 (when (fboundp 'expectations)
438 (expectations
439 (desc "agrep-by-name-read-info")
440 (expect "query1"
441 (stub read-string => "query1")
442 (agrep-by-name-read-info 'query))
443 (expect "elinit"
444 (stub completing-read => "elinit")
445 (agrep-by-name-read-info 'name))
446 (expect '("query1" "elinit")
447 (stub read-string => "query1")
448 (stub completing-read => "elinit")
449 (agrep-by-name-read-info 'query 'name))
450 (expect '("elinit" "query1")
451 (stub read-string => "query1")
452 (stub completing-read => "elinit")
453 (agrep-by-name-read-info 'name 'query))
456 (provide 'anything-grep)
458 ;; How to save (DO NOT REMOVE!!)
459 ;; (progn (magit-push) (emacswiki-post "anything-grep.el"))
460 ;;; anything-grep.el ends here