+\f
+(defvar minibuffer-history nil
+ "Default minibuffer history list.
+This is used for all minibuffer input
+except when an alternate history list is specified.")
+(defvar minibuffer-history-sexp-flag nil
+ "Nonzero when doing history operations on `command-history'.
+More generally, indicates that the history list being acted on
+contains expressions rather than strings.")
+(setq minibuffer-history-variable 'minibuffer-history)
+(setq minibuffer-history-position nil)
+(defvar minibuffer-history-search-history nil)
+
+(mapcar
+ (lambda (key-and-command)
+ (mapcar
+ (lambda (keymap-and-completionp)
+ ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
+ ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
+ ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
+ (define-key (symbol-value (car keymap-and-completionp))
+ (car key-and-command)
+ (let ((command (cdr key-and-command)))
+ (if (consp command)
+ ;; (and ... nil) => ... turns back on the completion-oriented
+ ;; history commands which rms turned off since they seem to
+ ;; do things he doesn't like.
+ (if (and (cdr keymap-and-completionp) nil) ;XXX turned off
+ (progn (error "EMACS BUG!") (cdr command))
+ (car command))
+ command))))
+ '((minibuffer-local-map . nil)
+ (minibuffer-local-ns-map . nil)
+ (minibuffer-local-completion-map . t)
+ (minibuffer-local-must-match-map . t)
+ (read-expression-map . nil))))
+ '(("\en" . (next-history-element . next-complete-history-element))
+ ([next] . (next-history-element . next-complete-history-element))
+ ("\ep" . (previous-history-element . previous-complete-history-element))
+ ([prior] . (previous-history-element . previous-complete-history-element))
+ ("\er" . previous-matching-history-element)
+ ("\es" . next-matching-history-element)))
+
+(defun previous-matching-history-element (regexp n)
+ "Find the previous history element that matches REGEXP.
+\(Previous history elements refer to earlier actions.)
+With prefix argument N, search for Nth previous match.
+If N is negative, find the next or Nth next match."
+ (interactive
+ (let* ((enable-recursive-minibuffers t)
+ (minibuffer-history-sexp-flag nil)
+ (regexp (read-from-minibuffer "Previous element matching (regexp): "
+ nil
+ minibuffer-local-map
+ nil
+ 'minibuffer-history-search-history)))
+ ;; Use the last regexp specified, by default, if input is empty.
+ (list (if (string= regexp "")
+ (setcar minibuffer-history-search-history
+ (nth 1 minibuffer-history-search-history))
+ regexp)
+ (prefix-numeric-value current-prefix-arg))))
+ (let ((history (symbol-value minibuffer-history-variable))
+ prevpos
+ (pos minibuffer-history-position))
+ (while (/= n 0)
+ (setq prevpos pos)
+ (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
+ (if (= pos prevpos)
+ (error (if (= pos 1)
+ "No later matching history item"
+ "No earlier matching history item")))
+ (if (string-match regexp
+ (if minibuffer-history-sexp-flag
+ (prin1-to-string (nth (1- pos) history))
+ (nth (1- pos) history)))
+ (setq n (+ n (if (< n 0) 1 -1)))))
+ (setq minibuffer-history-position pos)
+ (erase-buffer)
+ (let ((elt (nth (1- pos) history)))
+ (insert (if minibuffer-history-sexp-flag
+ (prin1-to-string elt)
+ elt)))
+ (goto-char (point-min)))
+ (if (or (eq (car (car command-history)) 'previous-matching-history-element)
+ (eq (car (car command-history)) 'next-matching-history-element))
+ (setq command-history (cdr command-history))))
+
+(defun next-matching-history-element (regexp n)
+ "Find the next history element that matches REGEXP.
+\(The next history element refers to a more recent action.)
+With prefix argument N, search for Nth next match.
+If N is negative, find the previous or Nth previous match."
+ (interactive
+ (let* ((enable-recursive-minibuffers t)
+ (minibuffer-history-sexp-flag nil)
+ (regexp (read-from-minibuffer "Next element matching (regexp): "
+ nil
+ minibuffer-local-map
+ nil
+ 'minibuffer-history-search-history)))
+ ;; Use the last regexp specified, by default, if input is empty.
+ (list (if (string= regexp "")
+ (setcar minibuffer-history-search-history
+ (nth 1 minibuffer-history-search-history))
+ regexp)
+ (prefix-numeric-value current-prefix-arg))))
+ (previous-matching-history-element regexp (- n)))