threads / patch / 12236

patchgit.el: Add a git-grep command

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

## tl;dr

17 messages between Feb 19, 2008 and Feb 24, 2008. Diffs are folded; open one to read it.

replies: 16people: 5as markdown or json

David Kågedal· Feb 19, 2008, 13:03 UTC · lore
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.
Show changes to contrib/emacs/git.el +50 −0
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· Feb 21, 2008, 13:12 UTC · re: David Kågedal · lore

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

David Kågedal <davidk@lysator.liu.se> writes:
> This allows easy access to git grep from Emacs.
>
You might want to add a menu:
Show changes to diff +1 −0
@@ -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· Feb 21, 2008, 14:55 UTC · re: Remi Vanicat · lore

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

Remi Vanicat <vanicat@debian.org> writes:
Show 7 quoted lines
> 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.

Show 8 quoted lines
> @@ -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· Feb 22, 2008, 02:00 UTC · re: David Kågedal · lore

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

   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· Feb 22, 2008, 07:31 UTC · re: David Kågedal · lore

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

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
David Kågedal· Feb 22, 2008, 09:15 UTC · re: Karl Hasselström · lore

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

Karl Hasselström <kha@treskal.com> writes:
Show 12 quoted lines
> 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
Karl Hasselström· Feb 22, 2008, 11:55 UTC · re: David Kågedal · lore

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

On 2008-02-22 10:15:29 +0100, David Kågedal wrote:
Show 11 quoted lines
> 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
Jakub Narebski· Feb 22, 2008, 09:03 UTC · re: David Kågedal · lore

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

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· Feb 22, 2008, 09:18 UTC · re: Jakub Narebski · lore

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

Jakub Narebski <jnareb@gmail.com> writes:
Show 16 quoted lines
> 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· Feb 22, 2008, 10:21 UTC · re: David Kågedal · lore

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

On Fri, 22 Feb 2008, David Kågedal wrote:
Show 20 quoted lines
> 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· Feb 22, 2008, 10:44 UTC · re: Jakub Narebski · lore

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

Jakub Narebski <jnareb@gmail.com> writes:
Show 24 quoted lines
> 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· Feb 22, 2008, 11:11 UTC · re: David Kågedal · lore

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

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(-)
Show changes to contrib/emacs/git.el +52 −0
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· Feb 22, 2008, 11:43 UTC · re: Remi Vanicat · lore

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

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.

Show 97 quoted lines
> 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
Xavier Maillard· Feb 23, 2008, 02:00 UTC · re: Remi Vanicat · lore

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

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· Feb 23, 2008, 19:39 UTC · re: Xavier Maillard · lore

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

Xavier Maillard <xma@gnu.org> writes:
Show 17 quoted lines
> 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· Feb 23, 2008, 22:41 UTC · re: Remi Vanicat · lore

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

   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· Feb 24, 2008, 02:00 UTC · re: Remi Vanicat · lore

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

   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

← back to recent threads