aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Magne Ingebrigtsen2016-04-03 19:46:50 +0200
committerLars Magne Ingebrigtsen2016-04-03 19:46:58 +0200
commita32eea60ac90d367435860fe3a10bf843e6f497c (patch)
treeaf270436d5b55079070f92e51dfa2b2f8b86e8fa
parent1b33cd8e7e5c747afde29e10b5fe192c5a37b67a (diff)
downloademacs-a32eea60ac90d367435860fe3a10bf843e6f497c.tar.gz
emacs-a32eea60ac90d367435860fe3a10bf843e6f497c.zip
Add `r'/`l' grep command history commands
* doc/emacs/building.texi (Grep Searching): Mention the `r'/`l' commands. * lisp/progmodes/grep.el (grep-forward-history): New command. (grep-backward-history): Ditto. (grep--save-buffers): New function. (grep): Use it to record the history. (grep--command-history, grep--history-inhibit) (grep--history-place): New internal variables for the grep history (bug#22627).
-rw-r--r--doc/emacs/building.texi7
-rw-r--r--etc/NEWS5
-rw-r--r--lisp/progmodes/grep.el47
3 files changed, 58 insertions, 1 deletions
diff --git a/doc/emacs/building.texi b/doc/emacs/building.texi
index 03fa0ed83b2..059c7cae89c 100644
--- a/doc/emacs/building.texi
+++ b/doc/emacs/building.texi
@@ -390,6 +390,13 @@ is called with the file name as the parameter and should return
390non-nil if the buffer is to be saved), and any other non-@code{nil} 390non-nil if the buffer is to be saved), and any other non-@code{nil}
391value means that all buffers should be saved without asking. 391value means that all buffers should be saved without asking.
392 392
393 In addition to the normal compilation mode commands (for
394next/previous match and so on), two additional commands are available
395for accessing the @command{grep} command history. @kbd{l}
396(@code{grep-backward-history}) will re-run successively the previous
397@command{grep} commands, and @kbd{r} (@code{grep-forward-history}
398will go ``forward'' in the command history.
399
393@findex grep-find 400@findex grep-find
394@findex find-grep 401@findex find-grep
395 The command @kbd{M-x grep-find} (also available as @kbd{M-x 402 The command @kbd{M-x grep-find} (also available as @kbd{M-x
diff --git a/etc/NEWS b/etc/NEWS
index d878228720e..88e101ee0f0 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -57,6 +57,10 @@ has been added. They are: 'file-attribute-type',
57** The new function 'buffer-hash' has been added, and can be used to 57** The new function 'buffer-hash' has been added, and can be used to
58compute a fash, non-consing hash of the contents of a buffer. 58compute a fash, non-consing hash of the contents of a buffer.
59 59
60+++
61** The grep mode now has a command history that you can access via the
62`r' and `l' commands.
63
60--- 64---
61** 'fill-paragraph' no longer marks the buffer as changed unless it 65** 'fill-paragraph' no longer marks the buffer as changed unless it
62actually changed something. 66actually changed something.
@@ -1807,6 +1811,7 @@ behavior, set 'diff-switches' to '-c'.
1807dynamically. Any third-party code that changes these templates should 1811dynamically. Any third-party code that changes these templates should
1808be updated accordingly. 1812be updated accordingly.
1809 1813
1814+++
1810** The grep/rgrep/lgrep functions will now ask about saving files 1815** The grep/rgrep/lgrep functions will now ask about saving files
1811before running. This is controlled by the 'grep-save-buffers' 1816before running. This is controlled by the 'grep-save-buffers'
1812variable. 1817variable.
diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index 2b44b58f245..7ce787e7284 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -271,6 +271,8 @@ See `compilation-error-screen-columns'"
271 (define-key map "{" 'compilation-previous-file) 271 (define-key map "{" 'compilation-previous-file)
272 (define-key map "}" 'compilation-next-file) 272 (define-key map "}" 'compilation-next-file)
273 (define-key map "\t" 'compilation-next-error) 273 (define-key map "\t" 'compilation-next-error)
274 (define-key map "r" 'grep-forward-history)
275 (define-key map "l" 'grep-backward-history)
274 (define-key map [backtab] 'compilation-previous-error) 276 (define-key map [backtab] 'compilation-previous-error)
275 277
276 ;; Set up the menu-bar 278 ;; Set up the menu-bar
@@ -309,6 +311,12 @@ See `compilation-error-screen-columns'"
309 (define-key map [menu-bar grep compilation-next-error] 311 (define-key map [menu-bar grep compilation-next-error]
310 '(menu-item "Next Match" next-error 312 '(menu-item "Next Match" next-error
311 :help "Visit the next match and corresponding location")) 313 :help "Visit the next match and corresponding location"))
314 (define-key map [menu-bar grep grep-backward-history]
315 '(menu-item "Previous Command" grep-backward-history
316 :help "Run the previous grep command from the command history"))
317 (define-key map [menu-bar grep grep-forward-history]
318 '(menu-item "Next Command" grep-forward-history
319 :help "Run the next grep command from the command history"))
312 map) 320 map)
313 "Keymap for grep buffers. 321 "Keymap for grep buffers.
314`compilation-minor-mode-map' is a cdr of this.") 322`compilation-minor-mode-map' is a cdr of this.")
@@ -744,6 +752,43 @@ This function is called from `compilation-filter-hook'."
744 grep-error-screen-columns) 752 grep-error-screen-columns)
745 (add-hook 'compilation-filter-hook 'grep-filter nil t)) 753 (add-hook 'compilation-filter-hook 'grep-filter nil t))
746 754
755(defvar grep--command-history nil)
756(defvar grep--history-inhibit nil)
757(defvar grep--history-place 0)
758
759(defun grep--save-history (command)
760 (unless grep--history-inhibit
761 (push (cons default-directory command) grep--command-history)
762 (setq grep--history-place 0)
763 ;; Don't let the history grow without bounds.
764 (when (> (length grep--command-history) 100)
765 (setcdr (nthcdr 100 grep--command-history) nil))))
766
767(defun grep-forward-history ()
768 "Go to the next result in the grep command history.
769Also see `grep-backward-history'."
770 (interactive)
771 (let ((elem (and (> grep--history-place 0)
772 (nth (1- grep--history-place) grep--command-history)))
773 (grep--history-inhibit t))
774 (unless elem
775 (error "Nothing further in the command history"))
776 (cl-decf grep--history-place)
777 (let ((default-directory (car elem)))
778 (grep (cdr elem)))))
779
780(defun grep-backward-history ()
781 "Go to the previous result in the grep command history.
782Also see `grep-forward-history'."
783 (interactive)
784 (let ((elem (nth (1+ grep--history-place) grep--command-history))
785 (grep--history-inhibit t))
786 (unless elem
787 (error "Nothing further in the command history"))
788 (cl-incf grep--history-place)
789 (let ((default-directory (car elem)))
790 (grep (cdr elem)))))
791
747(defun grep--save-buffers () 792(defun grep--save-buffers ()
748 (when grep-save-buffers 793 (when grep-save-buffers
749 (save-some-buffers (and (not (eq grep-save-buffers 'ask)) 794 (save-some-buffers (and (not (eq grep-save-buffers 'ask))
@@ -780,7 +825,7 @@ list is empty)."
780 (if current-prefix-arg default grep-command) 825 (if current-prefix-arg default grep-command)
781 'grep-history 826 'grep-history
782 (if current-prefix-arg nil default)))))) 827 (if current-prefix-arg nil default))))))
783 828 (grep--save-history command-args)
784 (grep--save-buffers) 829 (grep--save-buffers)
785 ;; Setting process-setup-function makes exit-message-function work 830 ;; Setting process-setup-function makes exit-message-function work
786 ;; even when async processes aren't supported. 831 ;; even when async processes aren't supported.