-
Notifications
You must be signed in to change notification settings - Fork 34
/
egg-grep.el
132 lines (113 loc) · 4.2 KB
/
egg-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
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
;;; egg -- Emacs Got Git
;;; A magit fork
;; Copyright (C) 2008 Linh Dang
;;
;; Egg 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.
;;
;; Egg 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary
;;; This is my fork of Marius's excellent magit. his work is at:
;;; http://zagadka.vm.bytemark.co.uk/magit
;;;
(require 'egg)
(require 'compile)
(require 'grep)
(require 'cl)
(defvar egg-grep-saved-find-file-func nil)
(defun egg-grep-find-file (marker rev:name dir formats)
(save-match-data
(let* ((rev-name-lst (split-string rev:name ":" t))
(rev (car rev-name-lst))
(file (cadr rev-name-lst)))
(unless file
(setq file rev)
(setq rev nil))
(if rev
(egg-file-get-other-version file rev nil t)
(apply egg-grep-saved-find-file-func marker file dir formats)))))
(defun egg-grep-next-error-function (n &optional reset)
(interactive "P")
(let ((egg-grep-saved-find-file-func
(symbol-function 'compilation-find-file)))
(flet ((compilation-find-file (marker file-name dir &rest formats)
(egg-grep-find-file marker
file-name
dir formats)))
(compilation-next-error-function n reset))))
;;;###autoload
(defun egg-grep-process-setup ()
"Setup compilation variables and buffer for `egg-grep'.
Set up `compilation-exit-message-function' and run `egg-grep-setup-hook'."
(set (make-local-variable 'compilation-exit-message-function)
(lambda (status code msg)
(if (eq status 'exit)
(cond ((zerop code)
'("finished (matches found)\n" . "matched"))
((= code 1)
'("finished with no matches found\n" . "no match"))
(t
(cons msg code)))
(cons msg code))))
(run-hooks 'egg-grep-setup-hook))
(defvar egg-grep-mode-map
(let ((map (make-sparse-keymap "Egg:Grep")))
(set-keymap-parent map compilation-minor-mode-map)
(define-key map (kbd "SPC") 'scroll-up)
(define-key map (kbd "DEL") 'scroll-down)
(define-key map (kbd "RET") 'compile-goto-error)
(define-key map "n" 'next-error-no-select)
(define-key map "p" 'previous-error-no-select)
(define-key map "{" 'compilation-previous-file)
(define-key map "}" 'compilation-next-file)
(define-key map (kbd "TAB") 'compilation-next-error)
map)
"Keymap for git-grep buffers.
`compilation-minor-mode-map' is the parent keymap.")
;;;###autoload
(define-compilation-mode egg-grep-mode "Git-Grep"
"Sets `compilation-last-buffer' and `compilation-window-height'."
(setq compilation-last-buffer (current-buffer))
(set (make-local-variable 'compilation-error-face)
grep-hit-face)
(set (make-local-variable 'compilation-error-regexp-alist)
grep-regexp-alist)
(set (make-local-variable 'compilation-process-setup-function)
'egg-grep-process-setup)
(set (make-local-variable 'compilation-disable-input) t)
(set (make-local-variable 'next-error-function)
'egg-grep-next-error-function))
;;;###autoload
(defun egg-grep (level)
(interactive "p")
(let ((git-dir (or (egg-git-dir)
(error "Dir NOT in a git repo: %s"
default-directory)))
(cmd "git --no-pager grep -n ")
rev term)
(when (> level 15)
(setq rev (egg-read-rev "grep in revision: " "HEAD"))
(when (= (aref rev 0) ?:)
(setq cmd (concat cmd "--cached "))
(setq rev nil)))
(when (and (> level 3) (setq term (symbol-at-point)))
(when term
(setq term (symbol-name term))
(setq cmd (concat cmd term " "))))
(when rev
(setq cmd (concat cmd " " rev)))
(setq cmd
(read-string "run git grep (like this) : " cmd))
(compilation-start cmd 'egg-grep-mode
`(lambda (name)
(format "*git-grep@%s*" ,git-dir)))))