# [PATCH] git.el: Add a git-grep command

17 messages from 2008-02-19 to 2008-02-24. Participants: David Kågedal, Remi Vanicat, Xavier Maillard, Karl Hasselström, Jakub Narebski.
Thread: https://gitlist.dev/t/12236

## David Kågedal, 2008-02-19 13:03

Subject: [PATCH] git.el: Add a git-grep command
Message-ID: <87odaa4tcl.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/87odaa4tcl.fsf%40lysator.liu.se

```
This allows easy access to git grep from Emacs.

Signed-off-by: David Kågedal <davidk@lysator.liu.se>
---
 contrib/emacs/git.el |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)

This works for me, but before including it someone else should try
it. It might only work in Emacs 22, for instance.

But when it works, it is really useful.

diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
index f69b697..898e70a 100644
--- a/contrib/emacs/git.el
+++ b/contrib/emacs/git.el
@@ -49,6 +49,7 @@
 (require 'ewoc)
 (require 'log-edit)
 (require 'easymenu)
+(require 'grep)
 
 
 ;;;; Customizations
@@ -1584,5 +1585,54 @@ Meant to be used in `after-save-hook'."
   (interactive)
   (describe-function 'git-status-mode))
 
+(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.5.4.2.148.g410dc


-- 
David Kågedal

```

## Remi Vanicat, 2008-02-21 13:12

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <87ve4itpw0.dlv@maison.homelinux.org>
URL: https://gitlist.dev/e/87ve4itpw0.dlv%40maison.homelinux.org
In-Reply-To: <87odaa4tcl.fsf@lysator.liu.se>

```
David Kågedal <davidk@lysator.liu.se> writes:

> This allows easy access to git grep from Emacs.
>


You might want to add a menu:

@@ -1497,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]
+      ["Grep" git-grep t]
       "--------"
       ["Mark" git-mark-file t]
       ["Mark All" git-mark-all t]

-- 
Rémi Vanicat

```

## David Kågedal, 2008-02-21 14:55

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <873armbbqk.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/873armbbqk.fsf%40lysator.liu.se
In-Reply-To: <87ve4itpw0.dlv@maison.homelinux.org>

```
Remi Vanicat <vanicat@debian.org> writes:

> David Kågedal <davidk@lysator.liu.se> writes:
>
>> This allows easy access to git grep from Emacs.
>>
>
>
> You might want to add a menu:

Note that git-grep is completely independent of git-status. But a menu
entry in git-status might still be a good idea.

> @@ -1497,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]
> +      ["Grep" git-grep t]
>        "--------"
>        ["Mark" git-mark-file t]
>        ["Mark All" git-mark-all t]

-- 
David Kågedal

```

## Xavier Maillard, 2008-02-22 02:00

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <200802220200.m1M203f3003434@localhost.localdomain>
URL: https://gitlist.dev/e/200802220200.m1M203f3003434%40localhost.localdomain
In-Reply-To: <87odaa4tcl.fsf@lysator.liu.se>

```
   This allows easy access to git grep from Emacs.

Very useful. Would it be possible to save the history from
sessions to sessions ?

	Xavier
-- 
http://www.gnu.org
http://www.april.org
http://www.lolica.org

```

## Karl Hasselström, 2008-02-22 07:31

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <20080222073157.GA6630@diana.vm.bytemark.co.uk>
URL: https://gitlist.dev/e/20080222073157.GA6630%40diana.vm.bytemark.co.uk
In-Reply-To: <87odaa4tcl.fsf@lysator.liu.se>

```
On 2008-02-19 14:03:18 +0100, David Kågedal wrote:

> This works for me, but before including it someone else should try
> it. It might only work in Emacs 22, for instance.

It works for me (also using emacs 22).

I was a bit confused about the filename pattern selection, though: the
promt suggested "ch", which, when I accepted it, was converted to
"*.[ch]". However, manually specifying "*.c" works as well. I'm not
sure if this is specific to your patch or something affecting all grep
stuff in emacs (though I can't recall seeing it before).

-- 
Karl Hasselström, kha@treskal.com
      www.treskal.com/kalle

```

## Jakub Narebski, 2008-02-22 09:03

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <m3myptcqji.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3myptcqji.fsf%40localhost.localdomain
In-Reply-To: <87odaa4tcl.fsf@lysator.liu.se>

```
David Kågedal <davidk@lysator.liu.se> writes:

> This works for me, but before including it someone else should try
> it. It might only work in Emacs 22, for instance.

1077:[emacs@git/contrib/emacs]# LC_ALL=en_EN make
emacs -batch -f batch-byte-compile git.el
Loading /usr/share/emacs/site-lisp/site-start.d/php-mode-init.el (source)...
[...]
While compiling toplevel forms in file /home/jnareb/git/contrib/emacs/git.el:
  !! File error (("Cannot open load file" "grep"))
Done
make: *** [git.elc] Error 1

1078:[emacs@git/contrib/emacs]# emacs --version
GNU Emacs 21.4.1

-- 
Jakub Narebski
Poland
ShadeHawk on #git

```

## David Kågedal, 2008-02-22 09:15

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <87wsox9wsu.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/87wsox9wsu.fsf%40lysator.liu.se
In-Reply-To: <20080222073157.GA6630@diana.vm.bytemark.co.uk>

```
Karl Hasselström <kha@treskal.com> writes:

> On 2008-02-19 14:03:18 +0100, David Kågedal wrote:
>
>> This works for me, but before including it someone else should try
>> it. It might only work in Emacs 22, for instance.
>
> It works for me (also using emacs 22).
>
> I was a bit confused about the filename pattern selection, though: the
> promt suggested "ch", which, when I accepted it, was converted to
> "*.[ch]". However, manually specifying "*.c" works as well. I'm not
> sure if this is specific to your patch or something affecting all grep
> stuff in emacs (though I can't recall seeing it before).

This isn't invented by me, but is "standard" in the grep commands in
Emacs 22.  See the grep-files-aliases variable and the documentation
of rgrep.

-- 
David Kågedal

```

## David Kågedal, 2008-02-22 09:18

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <87tzk19wn5.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/87tzk19wn5.fsf%40lysator.liu.se
In-Reply-To: <m3myptcqji.fsf@localhost.localdomain>

```
Jakub Narebski <jnareb@gmail.com> writes:

> David Kågedal <davidk@lysator.liu.se> writes:
>
>> This works for me, but before including it someone else should try
>> it. It might only work in Emacs 22, for instance.
>
> 1077:[emacs@git/contrib/emacs]# LC_ALL=en_EN make
> emacs -batch -f batch-byte-compile git.el
> Loading /usr/share/emacs/site-lisp/site-start.d/php-mode-init.el (source)...
> [...]
> While compiling toplevel forms in file /home/jnareb/git/contrib/emacs/git.el:
>   !! File error (("Cannot open load file" "grep"))
> Done
> make: *** [git.elc] Error 1
>
> 1078:[emacs@git/contrib/emacs]# emacs --version
> GNU Emacs 21.4.1

As I suspected. The problem is that the grep commands were rewritten
for Emacs 22, and lots of it can be reused. So to write a similar
command for Emacs 21 probably requires a bit more work. And since I
use Emacs 22 I'm not sure I will feel motivated enough...

-- 
David Kågedal

```

## Jakub Narebski, 2008-02-22 10:21

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <200802221121.35706.jnareb@gmail.com>
URL: https://gitlist.dev/e/200802221121.35706.jnareb%40gmail.com
In-Reply-To: <87tzk19wn5.fsf@lysator.liu.se>

```
On Fri, 22 Feb 2008, David Kågedal wrote:
> Jakub Narebski <jnareb@gmail.com> writes:
>> David Kågedal <davidk@lysator.liu.se> writes:
>>
>>> This works for me, but before including it someone else should try
>>> it. It might only work in Emacs 22, for instance.
>>
>> emacs -batch -f batch-byte-compile git.el
>> [...]
>> While compiling toplevel forms in file git/contrib/emacs/git.el:
>>   !! File error (("Cannot open load file" "grep"))
>> Done
>> make: *** [git.elc] Error 1
>>
>> 1078:[emacs@git/contrib/emacs]# emacs --version
>> GNU Emacs 21.4.1
> 
> As I suspected. The problem is that the grep commands were rewritten
> for Emacs 22, and lots of it can be reused. So to write a similar
> command for Emacs 21 probably requires a bit more work. And since I
> use Emacs 22 I'm not sure I will feel motivated enough...

Could you make it at least conditional on the Emacs version used,
or even better depending on the fact if "grep" 'library' is available?

I'd like to be able to use git.el with my Emacs, even without some
functionality. TIA.
-- 
Jakub Narebski
Poland

```

## David Kågedal, 2008-02-22 10:44

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <87ir0h9soh.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/87ir0h9soh.fsf%40lysator.liu.se
In-Reply-To: <200802221121.35706.jnareb@gmail.com>

```
Jakub Narebski <jnareb@gmail.com> writes:

> On Fri, 22 Feb 2008, David Kågedal wrote:
>> Jakub Narebski <jnareb@gmail.com> writes:
>>> David Kågedal <davidk@lysator.liu.se> writes:
>>>
>>>> This works for me, but before including it someone else should try
>>>> it. It might only work in Emacs 22, for instance.
>>>
>>> emacs -batch -f batch-byte-compile git.el
>>> [...]
>>> While compiling toplevel forms in file git/contrib/emacs/git.el:
>>>   !! File error (("Cannot open load file" "grep"))
>>> Done
>>> make: *** [git.elc] Error 1
>>>
>>> 1078:[emacs@git/contrib/emacs]# emacs --version
>>> GNU Emacs 21.4.1
>> 
>> As I suspected. The problem is that the grep commands were rewritten
>> for Emacs 22, and lots of it can be reused. So to write a similar
>> command for Emacs 21 probably requires a bit more work. And since I
>> use Emacs 22 I'm not sure I will feel motivated enough...
>
> Could you make it at least conditional on the Emacs version used,
> or even better depending on the fact if "grep" 'library' is available?

Sure. We could e.g. put it in its own file, git-grep.el.

> I'd like to be able to use git.el with my Emacs, even without some
> functionality. TIA.

Absolutely, I hope this doesn't get included until this is resolved. 

-- 
David Kågedal

```

## Remi Vanicat, 2008-02-22 11:11

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <87d4qpgs9y.dlv@maison.homelinux.org>
URL: https://gitlist.dev/e/87d4qpgs9y.dlv%40maison.homelinux.org
In-Reply-To: <87ir0h9soh.fsf@lysator.liu.se>

```
Here is a modification with inclusion of git-grep only when the grep
library is available. 

To put it in another file might be a good idea to, but in this case,
may be we could break this huge 1786 file in several smaller file.

From 403143a61bf8f77d042893765b19cf7cc7062e59 Mon Sep 17 00:00:00 2001
From: David Kågedal <davidk@lysator.liu.se>
Date: Fri, 22 Feb 2008 11:57:25 +0100
Subject: [PATCH] git.el: Add a git-grep command
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

This allows easy access to git grep from Emacs.

Signed-off-by: David Kågedal <davidk@lysator.liu.se>
Signed-off-by: Rémi Vanicat <vanicat@debian.org>
---
 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 f69b697..afaf187 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.5.4.2.191.g7b407



-- 
Rémi Vanicat

```

## David Kågedal, 2008-02-22 11:43

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <877igx9pxu.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/877igx9pxu.fsf%40lysator.liu.se
In-Reply-To: <87d4qpgs9y.dlv@maison.homelinux.org>

```
Remi Vanicat <vanicat@debian.org> writes:

> Here is a modification with inclusion of git-grep only when the grep
> library is available. 

Nice. I didn't even know that require had more parameters. But I'd
write nil instead of () in this case.

> To put it in another file might be a good idea to, but in this case,
> may be we could break this huge 1786 file in several smaller file.
>
> From 403143a61bf8f77d042893765b19cf7cc7062e59 Mon Sep 17 00:00:00 2001
> From: David Kågedal <davidk@lysator.liu.se>
> Date: Fri, 22 Feb 2008 11:57:25 +0100
> Subject: [PATCH] git.el: Add a git-grep command
> MIME-Version: 1.0
> Content-Type: text/plain; charset=utf-8
> Content-Transfer-Encoding: 8bit
>
> This allows easy access to git grep from Emacs.
>
> Signed-off-by: David Kågedal <davidk@lysator.liu.se>
> Signed-off-by: Rémi Vanicat <vanicat@debian.org>
> ---
>  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 f69b697..afaf187 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.5.4.2.191.g7b407

-- 
David Kågedal

```

## Karl Hasselström, 2008-02-22 11:55

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <20080222115532.GA14707@diana.vm.bytemark.co.uk>
URL: https://gitlist.dev/e/20080222115532.GA14707%40diana.vm.bytemark.co.uk
In-Reply-To: <87wsox9wsu.fsf@lysator.liu.se>

```
On 2008-02-22 10:15:29 +0100, David Kågedal wrote:

> Karl Hasselström <kha@treskal.com> writes:
>
> > I was a bit confused about the filename pattern selection, though:
> > the promt suggested "ch", which, when I accepted it, was converted
> > to "*.[ch]". However, manually specifying "*.c" works as well. I'm
> > not sure if this is specific to your patch or something affecting
> > all grep stuff in emacs (though I can't recall seeing it before).
>
> This isn't invented by me, but is "standard" in the grep commands in
> Emacs 22. See the grep-files-aliases variable and the documentation
> of rgrep.

Oh. OK. Thanks.

-- 
Karl Hasselström, kha@treskal.com
      www.treskal.com/kalle

```

## Xavier Maillard, 2008-02-23 02:00

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <200802230200.m1N202Y7007392@localhost.localdomain>
URL: https://gitlist.dev/e/200802230200.m1N202Y7007392%40localhost.localdomain
In-Reply-To: <87d4qpgs9y.dlv@maison.homelinux.org>

```
Hi,

   Here is a modification with inclusion of git-grep only when the grep
   library is available. 

   +(require 'grep () t)

   +(when (featurep 'grep)
   +  (defvar git-grep-history nil)
   +
   +  (defun git-grep (regexp &optional files dir)

Why not just do something like this ?

(when (require 'grep () t)
      (defvar ...)
      (defun git-grep ...))

Regards,

	Xavier
-- 
http://www.gnu.org
http://www.april.org
http://www.lolica.org

```

## Remi Vanicat, 2008-02-23 19:39

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <8763wfwjg6.dlv@maison.homelinux.org>
URL: https://gitlist.dev/e/8763wfwjg6.dlv%40maison.homelinux.org
In-Reply-To: <200802230200.m1N202Y7007392@localhost.localdomain>

```
Xavier Maillard <xma@gnu.org> writes:

> Hi,
>
>    Here is a modification with inclusion of git-grep only when the grep
>    library is available. 
>
>    +(require 'grep () t)
>
>    +(when (featurep 'grep)
>    +  (defvar git-grep-history nil)
>    +
>    +  (defun git-grep (regexp &optional files dir)
>
> Why not just do something like this ?
>
> (when (require 'grep () t)
>       (defvar ...)
>       (defun git-grep ...))

Because I wanted require to stay on top of the file, but I didn't want
to put the rest of the git-grep stuff there.

(the real reason is because I did not check in the doc for the return
value of require, and so I didn't knew that I could do that, but
still, I believe that my after the fact argument are good.)
-- 
Rémi Vanicat

```

## Xavier Maillard, 2008-02-23 22:41

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <200802232241.m1NMfZpg014264@localhost.localdomain>
URL: https://gitlist.dev/e/200802232241.m1NMfZpg014264%40localhost.localdomain
In-Reply-To: <8763wfwjg6.dlv@maison.homelinux.org>

```

   Xavier Maillard <xma@gnu.org> writes:

   > Hi,
   >
   >    Here is a modification with inclusion of git-grep only when the grep
   >    library is available. 
   >
   >    +(require 'grep () t)
   >
   >    +(when (featurep 'grep)
   >    +  (defvar git-grep-history nil)
   >    +
   >    +  (defun git-grep (regexp &optional files dir)
   >
   > Why not just do something like this ?
   >
   > (when (require 'grep () t)
   >       (defvar ...)
   >       (defun git-grep ...))

   Because I wanted require to stay on top of the file, but I didn't want
   to put the rest of the git-grep stuff there.

Good point. Though, you can still "embed" the require form
directly into the defun. This is thing I have already seen in the
past. Dunno if it is a convention or a coding style but something
like:

(defun git-grep ()
 "Docstring"
 (interactive)
 (when (require 'grep nil t)
       here the rest
  ))

is doable too. Maybe the if-else form would be better though with
an else clause to (error "No grep package foud.").

Well just kidding, your patch is okay ;)

	Xavier
-- 
http://www.gnu.org
http://www.april.org
http://www.lolica.org

```

## Xavier Maillard, 2008-02-24 02:00

Subject: Re: [PATCH] git.el: Add a git-grep command
Message-ID: <200802240200.m1O201Rh015406@localhost.localdomain>
URL: https://gitlist.dev/e/200802240200.m1O201Rh015406%40localhost.localdomain
In-Reply-To: <8763wfwjg6.dlv@maison.homelinux.org>

```

   Xavier Maillard <xma@gnu.org> writes:

   > Hi,
   >
   >    Here is a modification with inclusion of git-grep only when the grep
   >    library is available. 
   >
   >    +(require 'grep () t)
   >
   >    +(when (featurep 'grep)
   >    +  (defvar git-grep-history nil)
   >    +
   >    +  (defun git-grep (regexp &optional files dir)
   >
   > Why not just do something like this ?
   >
   > (when (require 'grep () t)
   >       (defvar ...)
   >       (defun git-grep ...))

   Because I wanted require to stay on top of the file, but I didn't want
   to put the rest of the git-grep stuff there.

Good point. Though, you can still "embed" the require form
directly into the defun. This is thing I have already seen in the
past. Dunno if it is a convention or a coding style but something
like:

(defun git-grep ()
 "Docstring"
 (interactive)
 (when (require 'grep nil t)
       here the rest
  ))

is doable too. Maybe the if-else form would be better though with
an else clause to (error "No grep package foud.").

Well just kidding, your patch is okay ;)

	Xavier
-- 
http://www.gnu.org
http://www.april.org
http://www.lolica.org

```
