(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)
;; 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))
;; 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"
;; 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)
(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.
(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