From f7c5f79ca565d3ef3feeb1e0af5ca261f1bcf58a Mon Sep 17 00:00:00 2001 From: Lars Magne Ingebrigtsen Date: Sun, 3 Apr 2016 20:13:52 +0200 Subject: [PATCH] Restore the point in grep buffers when traversing the history * lisp/progmodes/grep.el (grep-process-setup): Allow moving point to a specific place after finishing the grep command. (grep-mode): Use it to restore point after traversing the history. (grep--history-point): New internal variable. (grep--save-history, grep-forward-history): Use it to restore the point. --- lisp/progmodes/grep.el | 49 ++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index 7ce787e7284..fd55576e135 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -472,9 +472,12 @@ This variable's value takes effect when `grep-compute-defaults' is called.") (defvar grep-files-history nil) ;;;###autoload -(defun grep-process-setup () +(defun grep-process-setup (&optional point) "Setup compilation variables and buffer for `grep'. -Set up `compilation-exit-message-function' and run `grep-setup-hook'." +Set up `compilation-exit-message-function' and run +`grep-setup-hook'. If the optional parameter POINT is given, +point will be moved to this vicinity when the grep command has +finished." (when (eq grep-highlight-matches 'auto-detect) (grep-compute-defaults)) (unless (or (eq grep-highlight-matches 'auto-detect) @@ -495,12 +498,14 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'." ;; This relies on the fact that `compilation-start' ;; sets buffer-modified to nil before running the command, ;; so the buffer is still unmodified if there is no output. - (cond ((and (zerop code) (buffer-modified-p)) - '("finished (matches found)\n" . "matched")) - ((not (buffer-modified-p)) - '("finished with no matches found\n" . "no match")) - (t - (cons msg code))) + (progn + (goto-char (min point (point-max))) + (cond ((and (zerop code) (buffer-modified-p)) + '("finished (matches found)\n" . "matched")) + ((not (buffer-modified-p)) + '("finished with no matches found\n" . "no match")) + (t + (cons msg code)))) (cons msg code)))) (run-hooks 'grep-setup-hook)) @@ -732,6 +737,10 @@ This function is called from `compilation-filter-hook'." ;; Now replace the pattern with the default tag. (replace-match tag-default t t grep-default 1)))) +(defvar grep--command-history nil) +(defvar grep--history-inhibit nil) +(defvar grep--history-place 0) +(defvar grep--history-point 0) ;;;###autoload (define-compilation-mode grep-mode "Grep" @@ -746,19 +755,19 @@ This function is called from `compilation-filter-hook'." ;; can never match. (set (make-local-variable 'compilation-directory-matcher) '("\\`a\\`")) (set (make-local-variable 'compilation-process-setup-function) - 'grep-process-setup) + (lambda () + (grep-process-setup grep--history-point))) (set (make-local-variable 'compilation-disable-input) t) (set (make-local-variable 'compilation-error-screen-columns) grep-error-screen-columns) (add-hook 'compilation-filter-hook 'grep-filter nil t)) -(defvar grep--command-history nil) -(defvar grep--history-inhibit nil) -(defvar grep--history-place 0) - (defun grep--save-history (command) (unless grep--history-inhibit - (push (cons default-directory command) grep--command-history) + (when grep--command-history + (setcar (cdr (car grep--command-history)) (point))) + (push (list default-directory 0 command) + grep--command-history) (setq grep--history-place 0) ;; Don't let the history grow without bounds. (when (> (length grep--command-history) 100) @@ -773,9 +782,11 @@ Also see `grep-backward-history'." (grep--history-inhibit t)) (unless elem (error "Nothing further in the command history")) + (setcar (cdr (nth grep--history-place grep--command-history)) (point)) (cl-decf grep--history-place) - (let ((default-directory (car elem))) - (grep (cdr elem))))) + (let ((default-directory (car elem)) + (grep--history-point (nth 1 elem))) + (grep (nth 2 elem))))) (defun grep-backward-history () "Go to the previous result in the grep command history. @@ -785,9 +796,11 @@ Also see `grep-forward-history'." (grep--history-inhibit t)) (unless elem (error "Nothing further in the command history")) + (setcar (cdr (nth grep--history-place grep--command-history)) (point)) (cl-incf grep--history-place) - (let ((default-directory (car elem))) - (grep (cdr elem))))) + (let ((default-directory (car elem)) + (grep--history-point (nth 1 elem))) + (grep (nth 2 elem))))) (defun grep--save-buffers () (when grep-save-buffers -- 2.39.2