1

One of the commands I find myself running very often is git grep. I would really like it to be accessible through the same magit menu as other commands. However, from my understanding of magit.el, it looks like the items there are hard-coded, and there's no way to add one more, unless I modify the source of this function.

Am I wrong?

Is there a better way?

I read this answer: https://emacs.stackexchange.com/a/12739/563 but I cannot understand what arguments transient-append-suffix needs. Also, it looks like the answer is too old / maybe doesn't work anymore.


What I have so far

(defun wvxvw-magit-grep (regex &optional args)
  (interactive (list (read-from-minibuffer "Expression: ")
                     (transient-args 'magit-grep)))
  (let ((invert "")
        (extended ""))
    (while args
      (let ((val (car args)))
        (cond
         ((string-equal val "v")
          (setq invert "-v"))
         ((string-equal val "E")
          (setq extended "-E")))
      (setq args (cdr args))))
    (grep (format "git --no-pager grep -nH%s%s '%s'" invert extended regex))))

(defclass wvxvw-git-grep-expression-cls (transient-variable)
  ((scope :initarg :scope)
   (expression :initarg nil)))

(transient-define-infix wvxvw-git-grep-expression-cmd ()
  :class wvxvw-git-grep-expression-cls)

(transient-define-prefix magit-grep ()
  "Display git-grep command options."
  :man-page "git-grep"
  ["Arguments"
   ("-v" "Invert match" "-v")
   ("-E" "Extended regexp" "-E")]
  ["Actions"
   ("g" "Grep" wvxvw-magit-grep)])

(define-key magit-mode-map "G" 'magit-grep)

There's a thing I don't like about it: This doesn't actually add it to the magit-dispatcher menu, so, this isn't available if you press h in Magit status buffer.

wvxvw
  • 11,222
  • 2
  • 30
  • 55
  • I will suggest a slightly different solution. Use `project-find-regexp` from any file or directory within a given git repository. You can bind it to a key of course. – aadcg Sep 16 '21 at 15:03

1 Answers1

2

SECOND EDIT (in response to you edit)

To format the expression differently, simply modify the string after the :argument keyword in your magit:--grep-expression argument definition (e.g. make it "Expression: ")

To add the prefix to the magit-dispatch popup, simply do it like I explained in the original answer (replace counsel-projectile-rg by magit-grep)

EDIT

To bind a command directly in the magit-status-mode-map, bind it as follows using define-key:

(define-key magit-status-mode-map "G" 'counsel-projectile-rg)

I have bound it under G as g is already used as a prefix command in the magit-status buffer.

END EDIT

You can add a transient suffix with the following line of code:

(transient-insert-suffix 'magit-dispatch (kbd "h") '("G" "grep" counsel-projectile-rg))

read the docstring of transient-insert-suffix for an explanation. There is also the command transient-append-suffix which works similarly.

Here I am adding the command counsel-projectile-rg, which I use myself for this case (well actually I am just using the Spacemacs key-sequence SPC /), and which I can really recommend. There are equally good alternatives for helm, consult and probably more... also for other backends like grep, ag etc...

dalanicolai
  • 6,108
  • 7
  • 23
  • Well, this sort of accomplishes part of it, but not quite: 1. `G` key in the `magit-status` mode is unbound. I need to first press `h` to get the list of all possible bindings and only then pressing `G` invokes the `git-grep` command. 2. I still have no idea how to handle the options set in this popup. – wvxvw Sep 19 '21 at 08:03
  • In other words, it seems you are suggesting to add a new suffix to existing popup, but that's not what I want. I want to add a new prefix, that I can trigger from status mode like all other magit commands. – wvxvw Sep 19 '21 at 08:28
  • Ah okay, I see now. I was misled by your suggestion of using `transient-append-suffix`. In the magit-status buffer you can just bind a new command in its keymap. I will add how to do that then for completion (for `counsel-projectile-rg`). Or maybe you really want some prefix (i.e. some magit popup buffer)? But then I don't know from your question which infixes/suffixes you would like to add to it. – dalanicolai Sep 19 '21 at 08:52
  • I'll post what I have so far tomorrow, hopefully it'll make it more clear. – wvxvw Sep 19 '21 at 18:09