]> code.delx.au - gnu-emacs-elpa/blob - gist-git-grep.el
poptoshell - obtain tramp remote expression if present in shell arg
[gnu-emacs-elpa] / gist-git-grep.el
1 ;; From https://gist.github.com/offby1/1240799
2
3 ;; There's something similar (but fancier) in vc-git.el: vc-git-grep
4
5 ;; -I means don't search through binary files
6
7 ;; --no-color, oddly enough, is required to allow emacs to colorize the output
8
9 (defcustom git-grep-switches
10 "git --no-pager grep --extended-regexp -I -n --ignore-case --no-color"
11 "Switches to pass to `git grep'."
12 :type 'string)
13
14 (defcustom git-grep-default-work-tree (expand-file-name "~/work/adtrack")
15 "Top of your favorite git working tree. \\[git-grep] will search from here if it cannot figure out where else to look."
16 :type 'directory
17 )
18
19 (when (require 'vc-git nil t)
20
21 ;; Uncomment this to try out the built-in-to-Emacs function.
22 ;;(defalias 'git-grep 'vc-git-grep)
23
24 (defun git-grep (command-args)
25 (interactive
26 (let ((root (vc-git-root default-directory)))
27 (when (not root)
28 (setq root git-grep-default-work-tree)
29 (message "git-grep: %s doesn't look like a git working tree; searching from %s instead" default-directory root))
30 (list (read-shell-command "Run git-grep (like this): "
31 (format (concat
32 "cd %s && "
33 "%s -e %s")
34 root
35 git-grep-switches
36 (let ((thing (and
37
38 ; don't snarf stuff from the
39 ; buffer if we're not looking
40 ; at a file. Perhaps we
41 ; should also check to see if
42 ; the file is part of a git
43 ; repo.
44 buffer-file-name
45 (thing-at-point 'symbol))))
46 (or (and thing (progn
47 (set-text-properties 0 (length thing) nil thing)
48 (shell-quote-argument (regexp-quote thing))))
49 "")))
50 'git-grep-history))))
51 (let ((grep-use-null-device nil))
52 (grep command-args))))
53
54 (provide 'gist-git-grep)