git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] git.el: Add "git grep" functionality in a grep-style compilation buffer
@ 2008-08-30 18:55 David Christensen
  2008-09-09  9:25 ` David Kågedal
  0 siblings, 1 reply; 4+ messages in thread
From: David Christensen @ 2008-08-30 18:55 UTC (permalink / raw)
  To: git; +Cc: julliard, David Christensen


Signed-off-by: David Christensen <david@endpoint.com>
---
 contrib/emacs/git.el |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
index 279610f..46a9e49 100644
--- a/contrib/emacs/git.el
+++ b/contrib/emacs/git.el
@@ -1395,6 +1395,15 @@ amended version of it."
   (let ((info (ewoc-data (ewoc-locate git-status))))
     (view-file (git-fileinfo->name info))))
 
+(defun git-grep (pat)
+  "Run git grep on the repository with the provided pattern;
+results are placed in a grep-compilation buffer."
+  (interactive "sgit grep regexp: ")
+  (unless git-status (error "Not in git-status buffer."))
+  (let ((null-device nil)
+        (git-grep-flags nil))           ; for future extension
+    (grep (concat "git --no-pager grep -n " git-grep-flags " -e " pat))))
+
 (defun git-refresh-status ()
   "Refresh the git status buffer."
   (interactive)
@@ -1446,6 +1455,7 @@ amended version of it."
     (suppress-keymap map)
     (define-key map "?"   'git-help)
     (define-key map "*"    mark-map)
+    (define-key map "%"   'git-grep)
     (define-key map "h"   'git-help)
     (define-key map " "   'git-next-file)
     (define-key map "a"   'git-add-file)
-- 
1.6.0.1.90.g27a6e.dirty

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] git.el: Add "git grep" functionality in a grep-style compilation buffer
  2008-08-30 18:55 [PATCH] git.el: Add "git grep" functionality in a grep-style compilation buffer David Christensen
@ 2008-09-09  9:25 ` David Kågedal
  2008-09-09 14:51   ` Rémi Vanicat
  0 siblings, 1 reply; 4+ messages in thread
From: David Kågedal @ 2008-09-09  9:25 UTC (permalink / raw)
  To: git; +Cc: julliard, David Christensen

David Christensen <david@endpoint.com> writes:

> Signed-off-by: David Christensen <david@endpoint.com>

I posted a longer version of git-grep that built on the grep commands
in Emacs 22 a while ago. It gives you a better was to navigate to the
hits etc. I'll dig it up again.

-- 
David Kågedal

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] git.el: Add "git grep" functionality in a grep-style compilation buffer
  2008-09-09  9:25 ` David Kågedal
@ 2008-09-09 14:51   ` Rémi Vanicat
  2008-09-09 15:14     ` David Kågedal
  0 siblings, 1 reply; 4+ messages in thread
From: Rémi Vanicat @ 2008-09-09 14:51 UTC (permalink / raw)
  To: David Kågedal; +Cc: git, julliard, David Christensen

From: David Kågedal <davidk@lysator.liu.se>
Subject: [PATCH] [PATCH] git.el: Add a git-grep command

This allows easy access to git grep from Emacs.

Signed-off-by: David Kågedal <davidk@lysator.liu.se>
Tested-by: Rémi Vanicat <vanicat@debian.org>
---
David Kågedal <davidk@lysator.liu.se> writes:

> David Christensen <david@endpoint.com> writes:
>
>> Signed-off-by: David Christensen <david@endpoint.com>
>
> I posted a longer version of git-grep that built on the grep commands
> in Emacs 22 a while ago. It gives you a better was to navigate to the
> hits etc. I'll dig it up again.

Was it this one ?

-- 
Rémi Vanicat
 contrib/emacs/git.el |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
index c1cf1cb..57351a5 100644
--- a/contrib/emacs/git.el
+++ b/contrib/emacs/git.el
@@ -49,6 +49,7 @@
 (require 'ewoc)
 (require 'log-edit)
 (require 'easymenu)
+(require 'grep () t)
 
 
 ;;;; Customizations
@@ -1496,6 +1497,7 @@ amended version of it."
       ["Diff File" git-diff-file t]
       ["Interactive Diff File" git-diff-file-idiff t]
       ["Log" git-log-file t]
+      ,@(if (featurep 'grep) (list ["Grep" git-grep t]) ())
       "--------"
       ["Mark" git-mark-file t]
       ["Mark All" git-mark-all t]
@@ -1584,5 +1586,55 @@ Meant to be used in `after-save-hook'."
   (interactive)
   (describe-function 'git-status-mode))
 
+(when (featurep 'grep)
+  (defvar git-grep-history nil)
+
+  (defun git-grep (regexp &optional files dir)
+    "Recursively grep for REGEXP in FILES in directory tree rooted at DIR.
+The search is limited to file names matching shell pattern FILES.
+FILES may use abbreviations defined in `grep-files-aliases', e.g.
+entering `ch' is equivalent to `*.[ch]'.
+
+With \\[universal-argument] prefix, you can edit the constructed shell command line
+before it is executed.
+With two \\[universal-argument] prefixes, directly edit and run `git-grep-find-command'.
+
+Collect output in a buffer.  While find runs asynchronously, you
+can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error]
+in the grep output buffer, to go to the lines where grep found matches."
+    (interactive
+     (cond
+       ((equal current-prefix-arg '(16))
+       (list (read-from-minibuffer "Run: " "git grep "
+                                   nil nil 'git-grep-history)
+             nil))
+       (t (let* ((regexp (grep-read-regexp))
+                (files (grep-read-files regexp))
+                (dir (read-directory-name "Base directory: "
+                                          nil default-directory t)))
+           (list regexp files dir)))))
+    (when (and (stringp regexp) (> (length regexp) 0))
+      (if (null files)
+         (if (not (string= regexp grep-find-command))
+             (compilation-start regexp 'grep-mode))
+         (setq dir (file-name-as-directory (expand-file-name dir)))
+         (let ((command (concat
+                         "git grep -n "
+                         "-e " (shell-quote-argument regexp)
+                         (if (string= files "*")
+                             ""
+                             (concat " -- " (shell-quote-argument files))))))
+           (when command
+             (if current-prefix-arg
+                 (setq command
+                       (read-from-minibuffer "Confirm: "
+                                             command nil nil 'git-grep-history))
+                 (add-to-history 'git-grep-history command))
+             (let ((default-directory dir))
+               (compilation-start (concat "PAGER= " command) 'grep-mode))
+             ;; Set default-directory if we started rgrep in the *grep* buffer.
+             (if (eq next-error-last-buffer (current-buffer))
+                 (setq default-directory dir))))))))
+
 (provide 'git)
 ;;; git.el ends here
-- 
1.6.0.1.183.gb8d57

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] git.el: Add "git grep" functionality in a grep-style compilation buffer
  2008-09-09 14:51   ` Rémi Vanicat
@ 2008-09-09 15:14     ` David Kågedal
  0 siblings, 0 replies; 4+ messages in thread
From: David Kågedal @ 2008-09-09 15:14 UTC (permalink / raw)
  To: Rémi Vanicat; +Cc: git, julliard, David Christensen

vanicat@debian.org (Rémi Vanicat) writes:

> From: David Kågedal <davidk@lysator.liu.se>
> Subject: [PATCH] [PATCH] git.el: Add a git-grep command
>
> This allows easy access to git grep from Emacs.
>
> Signed-off-by: David Kågedal <davidk@lysator.liu.se>
> Tested-by: Rémi Vanicat <vanicat@debian.org>
> ---
> David Kågedal <davidk@lysator.liu.se> writes:
>
>> David Christensen <david@endpoint.com> writes:
>>
>>> Signed-off-by: David Christensen <david@endpoint.com>
>>
>> I posted a longer version of git-grep that built on the grep commands
>> in Emacs 22 a while ago. It gives you a better was to navigate to the
>> hits etc. I'll dig it up again.
>
> Was it this one ?

Kindof. This seems to be an improved version that doesn't fail on
older emacsen and that adds a menu. I'm not sure who contributed that,
but it's probably also in some mail somewhere. I use this every day
(but without the improvements) and I think it is really useful.

> -- 
> Rémi Vanicat
>  contrib/emacs/git.el |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 52 insertions(+), 0 deletions(-)
>
> diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
> index c1cf1cb..57351a5 100644
> --- a/contrib/emacs/git.el
> +++ b/contrib/emacs/git.el
> @@ -49,6 +49,7 @@
>  (require 'ewoc)
>  (require 'log-edit)
>  (require 'easymenu)
> +(require 'grep () t)
>  
>  
>  ;;;; Customizations
> @@ -1496,6 +1497,7 @@ amended version of it."
>        ["Diff File" git-diff-file t]
>        ["Interactive Diff File" git-diff-file-idiff t]
>        ["Log" git-log-file t]
> +      ,@(if (featurep 'grep) (list ["Grep" git-grep t]) ())
>        "--------"
>        ["Mark" git-mark-file t]
>        ["Mark All" git-mark-all t]
> @@ -1584,5 +1586,55 @@ Meant to be used in `after-save-hook'."
>    (interactive)
>    (describe-function 'git-status-mode))
>  
> +(when (featurep 'grep)
> +  (defvar git-grep-history nil)
> +
> +  (defun git-grep (regexp &optional files dir)
> +    "Recursively grep for REGEXP in FILES in directory tree rooted at DIR.
> +The search is limited to file names matching shell pattern FILES.
> +FILES may use abbreviations defined in `grep-files-aliases', e.g.
> +entering `ch' is equivalent to `*.[ch]'.
> +
> +With \\[universal-argument] prefix, you can edit the constructed shell command line
> +before it is executed.
> +With two \\[universal-argument] prefixes, directly edit and run `git-grep-find-command'.
> +
> +Collect output in a buffer.  While find runs asynchronously, you
> +can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error]
> +in the grep output buffer, to go to the lines where grep found matches."
> +    (interactive
> +     (cond
> +       ((equal current-prefix-arg '(16))
> +       (list (read-from-minibuffer "Run: " "git grep "
> +                                   nil nil 'git-grep-history)
> +             nil))
> +       (t (let* ((regexp (grep-read-regexp))
> +                (files (grep-read-files regexp))
> +                (dir (read-directory-name "Base directory: "
> +                                          nil default-directory t)))
> +           (list regexp files dir)))))
> +    (when (and (stringp regexp) (> (length regexp) 0))
> +      (if (null files)
> +         (if (not (string= regexp grep-find-command))
> +             (compilation-start regexp 'grep-mode))
> +         (setq dir (file-name-as-directory (expand-file-name dir)))
> +         (let ((command (concat
> +                         "git grep -n "
> +                         "-e " (shell-quote-argument regexp)
> +                         (if (string= files "*")
> +                             ""
> +                             (concat " -- " (shell-quote-argument files))))))
> +           (when command
> +             (if current-prefix-arg
> +                 (setq command
> +                       (read-from-minibuffer "Confirm: "
> +                                             command nil nil 'git-grep-history))
> +                 (add-to-history 'git-grep-history command))
> +             (let ((default-directory dir))
> +               (compilation-start (concat "PAGER= " command) 'grep-mode))
> +             ;; Set default-directory if we started rgrep in the *grep* buffer.
> +             (if (eq next-error-last-buffer (current-buffer))
> +                 (setq default-directory dir))))))))
> +
>  (provide 'git)
>  ;;; git.el ends here

-- 
David Kågedal

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-09-09 15:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-30 18:55 [PATCH] git.el: Add "git grep" functionality in a grep-style compilation buffer David Christensen
2008-09-09  9:25 ` David Kågedal
2008-09-09 14:51   ` Rémi Vanicat
2008-09-09 15:14     ` David Kågedal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).