forked from vjohansen/emacs-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vj-helm-simple-git-grep.el
69 lines (56 loc) · 2.52 KB
/
vj-helm-simple-git-grep.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
;;; vj-helm-simple-git-grep.el --- helm source that calls git grep
;;; Commentary:
;; A helm source for calling git grep. There are no bells and whistles.
;;; Code:
(defvar vj-helm-simple-git-grep-colors t "Disabling colors may improve speed")
(defvar vj-helm-simple-git-grep-root nil "Last git repo found. Reused when
running helm outside a git repo")
(defun vj-helm-simple-git-grep-candidates ()
"Find the candidates.
Colorize result if vj-helm-simple-git-grep-colors is t.
If not in a git repo use the root of the previously used git repo"
(let ((root
(replace-regexp-in-string "[\015\012]+\\'" ""
(shell-command-to-string "git rev-parse --show-toplevel")))
fields)
(if (string-match "^fatal:" root)
(setq root nil))
(unless root
;; reuse previous root
(setq root vj-helm-simple-git-grep-root))
(setq vj-helm-simple-git-grep-root root)
(with-temp-buffer
(shell-command (format "cd \"%s\" && git grep -n -i %s" root
helm-pattern) t)
(if vj-helm-simple-git-grep-colors
(mapcar
(lambda (line)
(setq fields (helm-grep-split-line line))
(format "%s:%s:%s"
(propertize (format "%s" (nth 0 fields)) 'face 'compilation-info)
(propertize (format "%s" (nth 1 fields))
'face 'compilation-line-number)
(nth 2 fields)))
(split-string (buffer-string) "\n"))
(split-string (buffer-string) "\n")))))
(defvar vj-helm-simple-git-grep
'((name . "Git grep")
(candidates . vj-helm-simple-git-grep-candidates)
(action ("View" .
(lambda (line)
(let ((fields (helm-grep-split-line line)))
;; (message "View file: \"%s\" \"%s\""
;; vj-helm-simple-git-grep-root (nth 0 fields))
(find-file-other-window (format "%s/%s"
vj-helm-simple-git-grep-root
(nth 0 fields)))
(goto-line (string-to-number (nth 1 fields)))
(when (fboundp 'etags-select-highlight)
(etags-select-highlight (point-at-bol) (point-at-eol)))))))
(requires-pattern . 3))
"Source for git.")
(defun vj-helm-git-grep ()
(interactive)
(helm-other-buffer '(vj-helm-simple-git-grep) "*helm git grep*"))
(provide 'vj-helm-simple-git-grep)
;;; vj-helm-simple-git-grep.el ends here