eugene
eugene

Reputation: 41735

helm-grep-do-git-grep searches in the current directory

helm-grep-do-git-grep with a prefix arg grep whole repo and without grep only current dir.

I want it reversed

helm-grep-do-git-grep without a prefix arg grep whole repo and with grep only current dir.

How can i define a function to reverse it or define keyboard shortcut ?

Upvotes: 1

Views: 460

Answers (1)

jpkotta
jpkotta

Reputation: 9437

This is a general solution for any command where you want to reverse the meaning of the prefix argument (at least for commands that interpret their prefix args as booleans).

(defun foo (&optional arg)
  "Do something.  Do something else with prefix ARG."
  (interactive "P")
  (if arg
      (message "something else")
    (message "something")))

(defun anti-foo (&optional arg)
  "Like `foo', but with opposite behavior w.r.t. ARG."
  (interactive "P")
  (if arg
      (foo nil)
    (foo '(4))))

If you just want to replace the function (may or may not be OK, see below), you can use advice:

(advice-add 'foo
            :around
            (lambda (orig &rest args)
              "Reverse sense of prefix arg."
              (let ((arg (car args)))
                (if arg
                    (setq args (cons nil (cdr args)))
                  (setq args (cons '(4) (cdr args))))
                (apply orig args))))

If the function is only an end user function, it's probably OK to use advice on it (skimming the helm source, helm-grep-do-git-grep is probably OK to advise). If the function is used by other functions, advice may cause the dependent functions to misbehave (because you're effectively changing the advised function's definition).


Aside: If you're not familiar with Emacs's prefix arguments, nil is used for no prefix, '(4) is used for a single prefix, '(16) is used for a double prefix, and so on.

Upvotes: 1

Related Questions