contrib/anything-grep.el: update copyright / remove extra comments (no code change)
[anything-config.git] / contrib / anything-grep.el
blob57070647e71d6e59cf583df6f855a9dd0415eac6
1 ;;; anything-grep.el --- search refinement of grep result with anything
3 ;; Copyright (C) 2008-2015, 2017 rubikitch
5 ;; Author: rubikitch <rubikitch@ruby-lang.org>
6 ;; Keywords: convenience, unix
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-grep.el
9 ;; This file 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 2, or (at your option)
12 ;; any later version.
14 ;; This file 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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
24 ;;; Commentary:
26 ;; Do grep in anything buffer. When we search information with grep,
27 ;; we often narrow the candidates. Let's use `anything' to do it.
29 ;;; Commands:
31 ;; Below are complete command list:
33 ;; `anything-grep'
34 ;; Run grep in `anything' buffer to narrow results.
35 ;; `anything-grep-by-name'
36 ;; Do `anything-grep' from predefined location.
37 ;; `anything-grep-by-name-reversed'
38 ;; Do `anything-grep' from predefined location.
40 ;;; Customizable Options:
42 ;; Below are customizable option list:
45 ;; `anything-grep' is simple interface to grep a query. It asks
46 ;; directory to grep. The grep process is synchronous process. You may
47 ;; have to wait when you grep the target for the first time. But once
48 ;; the target is on the disk cache, queries are grepped at lightning
49 ;; speed. Even if older Pentium4 computer, grepping from 180MB takes
50 ;; only 0.2s! GNU grep is amazingly fast.
52 ;; `anything-grep-by-name' asks query and predefined location. It is
53 ;; good idea to have ack (ack-grep), grep implemented in Perl, to
54 ;; exclude unneeded files. Such as RCS, .svn and so on.
56 ;; ack -- better than grep, a power search tool for programmers
57 ;; http://petdance.com/ack/
59 ;;; Code:
61 (defvar anything-grep-version "$Id: anything-grep.el,v 1.27 2010-03-21 11:31:04 rubikitch Exp $")
62 (require 'anything-config)
63 (require 'grep)
64 (require 'format-spec)
66 (defvar anything-grep-save-buffers-before-grep nil
67 "Do `save-some-buffers' before performing `anything-grep'.")
69 (defvar anything-grep-goto-hook nil
70 "List of functions to be called after `agrep-goto' opens file.")
72 (defvar anything-grep-find-file-function 'find-file
73 "Function to visit a file with.
74 It takes one argument, a file name to visit.")
76 (defvar anything-grep-multiline nil
77 "If non-nil, use multi-line display. It is prettier.
78 Use anything.el v1.147 or newer.")
80 (defvar anything-grep-fontify-file-name t
81 "If non-nil, fontify file name and line number of matches.")
83 (defvar anything-grep-sh-program
84 (or (executable-find "zsh")
85 (executable-find "sh")))
87 (defvar anything-grep-alist
88 '(("buffers" ("egrep -Hin %s $buffers" "/"))
89 ("memo" ("ack-grep -af | xargs egrep -Hin %s" "~/memo"))
90 ("PostgreSQL" ("egrep -Hin %s *.txt" "~/doc/postgresql-74/"))
91 ("~/bin and ~/ruby"
92 ("ack-grep -afG 'rb$' | xargs egrep -Hin %s" "~/ruby")
93 ("ack-grep -af | xargs egrep -Hin %s" "~/bin")))
94 "Mapping of location and command/pwd used by `anything-grep-by-name'.
95 The command is grep command line. Note that %s is replaced by query.
96 The command is typically \"ack-grep -af | xargs egrep -Hin %s\", which means
97 regexp/case-insensitive search for all files (including subdirectories)
98 except unneeded files.
99 The occurrence of $file in command is replaced with `buffer-file-name' of
100 all buffers.
102 The pwd is current directory to grep.
104 The format is:
106 ((LOCATION1
107 (COMMAND1-1 PWD1-1)
108 (COMMAND1-2 PWD1-2)
109 ...)
110 (LOCATION2
111 (COMMAND2-1 PWD2-1)
112 (COMMAND2-2 PWD2-2)
113 ...)
114 ...)
117 (defvar anything-grep-filter-command nil
118 "If non-nil, filter the result of grep command.
120 For example, normalizing many Japanese encodings to EUC-JP,
121 set this variable to \"ruby -rkconv -pe '$_.replace $_.toeuc'\".
122 The command is converting standard input to EUC-JP line by line. ")
124 (defvar anything-grep-repository-root-function (if (require 'repository-root nil t)
125 'repository-root
126 nil)
127 "*If non-nil, a function that returns the current file's repository root directory.
128 The function is called with a single string argument (a file name) and should
129 return either nil, or a string, which is the root directory of that file's repository.")
131 ;; (@* "core")
132 (defvar anything-grep-sources nil
133 "`anything-sources' for last invoked `anything-grep'.")
134 (defvar anything-grep-buffer-name nil)
135 (defun anything-grep-base (sources &optional bufname)
136 "Invoke `anything' for `anything-grep'."
137 (and anything-grep-save-buffers-before-grep
138 (save-some-buffers (not compilation-ask-about-save) nil))
139 (setq anything-grep-sources sources)
140 (setq anything-grep-buffer-name (or bufname "*anything grep*"))
141 (let ((anything-quit-if-no-candidate t)
142 (anything-compile-source-functions
143 (cons 'anything-compile-source--agrep-init anything-compile-source-functions)))
144 (anything sources nil nil nil nil bufname)))
146 ;; (anything (list (agrep-source "grep -Hin agrep anything-grep.el" default-directory) (agrep-source "grep -Hin pwd anything-grep.el" default-directory)))
147 (defvar agrep-map
148 (let ((map (make-sparse-keymap)))
149 (set-keymap-parent map anything-map)
150 (define-key map (kbd "RET") 'agrep-return)
151 (define-key map (kbd "C-o") 'agrep-next-source-or-detail)
152 (define-key map (kbd "<right>") 'agrep-next-source-or-detail)
153 map))
155 (defun agrep-source (command pwd)
156 "Anything Source of `anything-grep'."
157 `((command . ,command)
158 (pwd . ,pwd)
159 (name . ,(format "%s [%s]" command pwd))
160 (action . agrep-goto)
161 (anything-grep)
162 (candidate-number-limit . 9999)
163 (migemo)
164 (keymap . ,agrep-map)
165 ;; to inherit faces
166 (candidates-in-buffer)
167 (get-line . buffer-substring)
168 ,@(when anything-grep-multiline
169 '((multiline)
170 (real-to-display . agrep-real-to-display)))))
172 (defun agrep-return ()
173 (interactive)
174 (let ((it (anything-get-selection nil t)))
175 (if (string-match ":[0-9]+:." it)
176 (anything-exit-minibuffer)
177 (anything-set-pattern (concat it ": ")))))
178 (defun agrep-next-source-or-detail ()
179 (interactive)
180 (anything-move-selection-common
181 (lambda ()
182 (goto-char (or (re-search-forward " details .+\n" (anything-get-next-header-pos) t)
183 (anything-get-next-header-pos)
184 (point-min))))
185 'source 'next))
187 (defun anything-compile-source--agrep-init (source)
188 (if (assq 'anything-grep source)
189 (append '((init . agrep-init)
190 (candidates)) source)
191 source))
193 (defun agrep-init ()
194 (agrep-create-buffer (anything-attr 'command) (anything-attr 'pwd)))
196 (defun agrep-real-to-display (file-line-content)
197 (if (string-match ":\\([0-9]+\\):" file-line-content)
198 (format "%s:%s\n %s"
199 (substring file-line-content 0 (match-beginning 0))
200 (match-string 1 file-line-content)
201 (substring file-line-content (match-end 0)))
202 file-line-content))
204 (defvar agrep-source-local nil)
205 (defvar agrep-waiting-source nil
206 "`anything' sources to get together in `agrep-sentinel'.")
207 (defvar agrep-proc-tmpfile-alist nil)
208 (defun agrep-do-grep (command pwd)
209 "Insert result of COMMAND. The current directory is PWD.
210 GNU grep is expected for COMMAND. The grep result is colorized."
211 (let ((process-environment process-environment)
212 proc
213 (tmpfile (make-temp-file "agrep-")))
214 (when (eq grep-highlight-matches t)
215 ;; Modify `process-environment' locally bound in `call-process-shell-command'.
216 (setenv "GREP_OPTIONS" (concat (getenv "GREP_OPTIONS") " --color=always"))
217 ;; for GNU grep 2.5.1
218 (setenv "GREP_COLOR" "01;31")
219 ;; for GNU grep 2.5.1-cvs
220 (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:ml=:cx=:ne"))
221 (set (make-local-variable 'agrep-source-local) (anything-get-current-source))
222 (add-to-list 'agrep-waiting-source agrep-source-local)
223 (setq proc (start-process "anything-grep" (current-buffer)
224 anything-grep-sh-program "-c"
225 (format "cd %s; %s > %s" pwd command tmpfile)))
226 (push (cons proc tmpfile) agrep-proc-tmpfile-alist)
227 (set-process-sentinel proc 'agrep-sentinel)))
229 (defvar agrep-do-after-minibuffer-exit nil)
230 (defun agrep-minibuffer-exit-hook ()
231 (when agrep-do-after-minibuffer-exit
232 (run-at-time 1 nil agrep-do-after-minibuffer-exit)
233 (setq agrep-do-after-minibuffer-exit nil)))
234 (add-hook 'minibuffer-exit-hook 'agrep-minibuffer-exit-hook)
236 (defun agrep-highlight-line-after-persistent-action ()
237 (when anything-in-persistent-action
238 (anything-persistent-highlight-point (point-at-bol) (point-at-eol))))
239 (add-hook 'anything-grep-goto-hook 'agrep-highlight-line-after-persistent-action)
241 (defun agrep-show (func)
242 (if (active-minibuffer-window)
243 (setq agrep-do-after-minibuffer-exit func)
244 (funcall func)))
245 ;; (anything-grep "sleep 1; grep -Hin grep anything-grep.el" "~/src/anything-config/extensions/")
247 (defun agrep-sentinel (proc stat)
248 (with-current-buffer (process-buffer proc)
249 (setq agrep-waiting-source (delete agrep-source-local agrep-waiting-source))
250 (let ((tmpfile (assoc-default proc agrep-proc-tmpfile-alist)))
251 (insert-file-contents tmpfile)
252 (goto-char 1)
253 (delete-file tmpfile))
254 (agrep-fontify))
255 (unless agrep-waiting-source
256 ;; call anything
257 (setq agrep-proc-tmpfile-alist nil)
258 (agrep-show
259 (lambda ()
260 (let ((anything-quit-if-no-candidate (lambda () (message "No matches"))))
261 (anything anything-grep-sources nil nil nil nil anything-grep-buffer-name))))))
263 (defun agrep-fontify ()
264 "Fontify the result of `agrep-do-grep'."
265 ;; Color matches.
266 (goto-char 1)
267 (while (re-search-forward "\\(\033\\[01;31m\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)" nil t)
268 (put-text-property (match-beginning 2) (match-end 2) 'face grep-match-face)
269 (replace-match "" t t nil 1)
270 (replace-match "" t t nil 3))
271 ;; Delete other escape sequences.
272 (goto-char 1)
273 (while (re-search-forward "\\(\033\\[[0-9;]*[mK]\\)" nil t)
274 (replace-match "" t t nil 0))
275 (when anything-grep-fontify-file-name
276 (goto-char 1)
277 (while (re-search-forward ":\\([0-9]+\\):" nil t)
278 (put-text-property (point-at-bol) (match-beginning 0) 'face compilation-info-face)
279 (put-text-property (match-beginning 1) (match-end 1) 'face compilation-line-face)
280 (forward-line 1))))
281 ;; (anything-grep "grep -n grep *.el" "~/emacs/init.d")
283 (defun agrep-create-buffer (command pwd)
284 "Create candidate buffer for `anything-grep'.
285 Its contents is fontified grep result."
286 (with-current-buffer (anything-candidate-buffer 'global)
287 (setq default-directory pwd)
288 (agrep-do-grep command pwd)
289 (current-buffer)))
290 ;; (display-buffer (agrep-create-buffer "grep --color=always -Hin agrep anything-grep.el" default-directory))
291 ;; (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))))
293 (defun agrep-goto (file-line-content)
294 "Visit the source for the grep result at point."
295 (if (not (string-match ":\\([0-9]+\\):" file-line-content))
296 ;; If lineno is unavailable, just open file
297 (funcall anything-grep-find-file-function
298 (expand-file-name file-line-content (anything-attr 'pwd)))
299 (save-match-data
300 (funcall anything-grep-find-file-function
301 (expand-file-name (substring file-line-content
302 0 (match-beginning 0))
303 (anything-attr 'pwd))))
304 (anything-goto-line (string-to-number (match-string 1 file-line-content))))
305 (run-hooks 'anything-grep-goto-hook))
307 ;; (@* "simple grep interface")
308 (defun anything-grep (command pwd)
309 "Run grep in `anything' buffer to narrow results.
310 It asks COMMAND for grep command line and PWD for current directory."
311 (interactive
312 (progn
313 (grep-compute-defaults)
314 (let ((default (grep-default-command)))
315 (list (read-from-minibuffer "Run grep (like this): "
316 (if current-prefix-arg
317 default grep-command)
318 nil nil 'grep-history
319 (if current-prefix-arg nil default))
320 (read-directory-name "Directory: " default-directory default-directory t)))))
321 (anything-grep-base (list (agrep-source (agrep-preprocess-command command) pwd))
322 (format "*anything grep:%s [%s]*" command (abbreviate-file-name pwd))))
323 ;; (anything-grep "grep -Hin agrep anything-grep.el" default-directory)
325 (defun agrep-preprocess-command--buffers ()
326 (when (search-forward "$buffers" nil t)
327 (delete-region (match-beginning 0) (match-end 0))
328 (insert (mapconcat 'shell-quote-argument
329 (delq nil (mapcar 'agrep-abbreviated-buffer-file-name
330 (buffer-list))) " "))))
331 (defun agrep-preprocess-command--filter (command)
332 (when anything-grep-filter-command
333 (goto-char (point-max))
334 (insert "|" anything-grep-filter-command)))
336 (defun agrep-preprocess-command (command)
337 (with-temp-buffer
338 (insert command)
339 (goto-char 1)
340 (agrep-preprocess-command--buffers)
341 (agrep-preprocess-command--filter command)
342 (buffer-string)))
344 (defun agrep-abbreviated-buffer-file-name (b)
345 "Abbreviated buffer-file-name by `default-directory'"
346 (let ((dir-re (concat "^" (regexp-quote default-directory))))
347 (anything-aif (buffer-file-name b)
348 (and (file-exists-p it)
349 (if (string-match dir-re it)
350 (substring it (match-end 0))
351 it)))))
353 ;; (@* "grep in predefined files")
354 (defvar agbn-last-name nil
355 "The last used name by `anything-grep-by-name'.")
357 (defun agrep-by-name-read-info (&rest kinds)
358 (let* ((default (or (and (region-active-p)
359 (buffer-substring (region-beginning) (region-end)))
360 (thing-at-point 'symbol) ""))
361 (result (mapcar (lambda (kind)
362 (case kind
363 ('query (read-string
364 (format "Grep query (default:%s): " default)
365 nil nil default))
366 ('name (completing-read
367 "Grep by name: "
368 anything-grep-alist
369 nil t nil nil agbn-last-name))))
370 kinds)))
371 (deactivate-mark)
372 (if (cdr result) ; length >= 1
373 result
374 (car result))))
375 (defun anything-grep-by-name (&optional query name)
376 "Do `anything-grep' from predefined location.
377 It asks NAME for location name and QUERY."
378 (interactive (agrep-by-name-read-info 'query 'name))
379 (setq query (or query (agrep-by-name-read-info 'query)))
380 (setq name (or name (agrep-by-name-read-info 'name)))
381 (setq agbn-last-name name)
382 (anything-aif (assoc-default name anything-grep-alist)
383 (progn
384 (grep-compute-defaults)
385 (anything-grep-base
386 (mapcar (lambda (args)
387 (destructuring-bind (cmd dir) args
388 (agrep-source
389 (format-spec (agrep-preprocess-command cmd)
390 `((?s . ,(shell-quote-argument query))
391 (?a . ,query)))
392 dir)))
394 (format "*anything grep:%s [%s]" query name)))
395 (error "no such name %s" name)))
397 (defun anything-grep-by-name-reversed (&optional name query)
398 "Do `anything-grep' from predefined location.
399 It asks QUERY and NAME for location name.
401 Difference with `anything-grep-by-name' is prompt order."
402 (interactive (agrep-by-name-read-info (quote name) (quote query)))
403 (anything-grep-by-name query name))
405 ;;; repository root
406 (defun agrep-repository-root (filename)
407 "Attempt to deduce the current file's repository root directory.
408 You should customize `anything-grep-repository-root-function' and provide a function that
409 does the actual work, based of the type of SCM tool that you're using."
410 (if (null filename)
412 (let* ((directory (file-name-directory filename))
413 (repository-root (if (and anything-grep-repository-root-function
414 (functionp anything-grep-repository-root-function))
415 (apply anything-grep-repository-root-function (list filename))
416 nil)))
417 (or repository-root directory))))
419 (defun anything-grep-repository-1 (command)
420 "Run `anything-grep' in repository."
421 (interactive
422 (progn
423 (grep-compute-defaults)
424 (let ((default (grep-default-command)))
425 (list (read-from-minibuffer
426 (format "Run grep in %s (like this): "
427 (agrep-repository-root
428 (or buffer-file-name default-directory)))
429 (if current-prefix-arg
430 default grep-command)
431 nil nil 'grep-history
432 (if current-prefix-arg nil default))))))
433 (anything-grep command (agrep-repository-root (or buffer-file-name default-directory))))
435 (defun anything-grep-repository (&optional query)
436 "Do `anything-grep' from predefined location.
437 It asks NAME for location name and QUERY."
438 (interactive (list (agrep-by-name-read-info 'query)))
439 (grep-compute-defaults)
440 (anything-grep-repository-1
441 (format (concat grep-command " %s")
442 (shell-quote-argument query))))
444 ;;; visited file
445 ;;;; unit test
446 (dont-compile
447 (when (fboundp 'expectations)
448 (expectations
449 (desc "agrep-by-name-read-info")
450 (expect "query1"
451 (stub read-string => "query1")
452 (agrep-by-name-read-info 'query))
453 (expect "elinit"
454 (stub completing-read => "elinit")
455 (agrep-by-name-read-info 'name))
456 (expect '("query1" "elinit")
457 (stub read-string => "query1")
458 (stub completing-read => "elinit")
459 (agrep-by-name-read-info 'query 'name))
460 (expect '("elinit" "query1")
461 (stub read-string => "query1")
462 (stub completing-read => "elinit")
463 (agrep-by-name-read-info 'name 'query))
466 (provide 'anything-grep)
468 ;; How to save (DO NOT REMOVE!!)
469 ;; (progn (magit-push) (emacswiki-post "anything-grep.el"))
470 ;;; anything-grep.el ends here