John Lawrence Aspden
John Lawrence Aspden

Reputation: 17470

Can I script a canned find-grep in emacs?

Almost always, I want to run find-grep like this:

ack --no-heading --no-color "SOMETHING" ~/myco/firmwaresrc

Where SOMETHING is the only variable. There are actually a surprising number of keypresses and bits of thinking required to get this to happen.

How would I go about 'canning' this, so that M-x fgf would run find-grep with that command and the current region in place of SOMETHING?

Actually I've worked out how to do this (see answer below). Can anyone tell me how to get emacs to ask for SOMETHING if there's no selected region?

Upvotes: 3

Views: 660

Answers (3)

Ivan Andrus
Ivan Andrus

Reputation: 5301

You might also want to look at ack, full-ack or ack-and-a-half. The last is supposed to be the marriage of the two previous ones. They allow you to specify default arguments and such (although there's nothing wrong with learning elisp :-).

Upvotes: 1

huaiyuan
huaiyuan

Reputation: 26539

(defun fgf (term)
  (interactive
   (list (if (use-region-p)
             (buffer-substring (region-beginning) (region-end))
           (read-string "grep-find: "))))
  (grep-find (concat "ack --no-heading --no-color \""
                     term  "\" ~/myco/firmwaresrc")))

Upvotes: 7

John Lawrence Aspden
John Lawrence Aspden

Reputation: 17470

This works:

(defun fgf ()
  (interactive)
  (if (region-active-p)
      (let (start end term command)
        (setq start (region-beginning)
              end   (region-end)
              term (buffer-substring start end)
              command (concat "ack --no-heading --no-color \"" term  "\" ~/myco/firmwaresrc"))
    (grep-find command))))

So now I wonder if it's possible to get it to ask me for "SOMETHING" if there's no region selected. I'll change the question.

Upvotes: 0

Related Questions