Wilfred / ag.el

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

Jump directly to result if only one match #91

Closed kaushalmodi closed 9 years ago

kaushalmodi commented 9 years ago

This is a PR associated to this issue: https://github.com/Wilfred/ag.el/issues/89 Once I figure out how to solve that issue, I can update this PR to be fully functional.

kaushalmodi commented 9 years ago

Explanation for adding ag/last-search-buffer-name as seen in the PR. A user could have multiple ag buffers open. The above var makes sure that if the user has

(add-hook 'ag-post-search-hook #'ag/jump-to-result-if-only-one-match)

only the buffer for the last ag search that that only 1 match gets closed.

kaushalmodi commented 9 years ago

This does not need to be merged upstream. But this functionality can be achieved by the below AFTER PR https://github.com/Wilfred/ag.el/pull/92 is accepted:

   (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)