Wilfred / ag.el

An Emacs frontend to The Silver Searcher
http://agel.readthedocs.org/en/latest/
525 stars 61 forks source link

Cache the last ag search buffer name #92

Closed kaushalmodi closed 9 years ago

kaushalmodi commented 9 years ago

Can you please add this so that I can have a function like below in my config:

Using ag/last-search-buffer-name ensures that I kill only the last ag search buffer and not one of the prior open ones (if any).

    (defun ag/jump-to-result-if-only-one-match ()
      "Jump to the first ag result if that ag search came up with just one match."
      (let (only-one-match)
        (when (member "--stats" ag-arguments)
          (with-current-buffer ag/last-search-buffer-name
            (save-excursion
              (goto-char (point-min))
              (setq only-one-match (re-search-forward "^1 matches\\s-*$" nil :noerror))))
          (when only-one-match
            (next-error)
            (kill-buffer ag/last-search-buffer-name)))))
    (add-hook 'ag-search-finished-hook #'ag/jump-to-result-if-only-one-match)
Wilfred commented 9 years ago

This shouldn't be necessary. ag-search-finished-hook is executed in the context of the results buffer, so you should just be able to use (current-buffer). Does that meet your need?

kaushalmodi commented 9 years ago

You're correct. Operations happen in the current ag buffer. Thanks.

https://github.com/kaushalmodi/.emacs.d/commit/be25ade55c519789ca19d9a3f69dea4f72232d0c