(compare-windows): Push mark in both buffers at start.
[bpt/emacs.git] / lisp / chistory.el
CommitLineData
c0274f38
ER
1;;; chistory.el --- list command history
2
9750e079
ER
3;; Copyright (C) 1985 Free Software Foundation, Inc.
4
e5167999
ER
5;; Author: K. Shane Hartman
6;; Maintainer: FSF
e2fbf49d
JB
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
e2fbf49d
JB
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to
22;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
e5167999 24;;; Commentary:
e2fbf49d 25
e2fbf49d 26;; This really has nothing to do with list-command-history per se, but
daaf0197 27;; its a nice alternative to C-x ESC ESC (repeat-complex-command) and
e2fbf49d
JB
28;; functions as a lister if given no pattern. It's not important
29;; enough to warrant a file of its own.
30
e5167999
ER
31;;; Code:
32
e2fbf49d
JB
33;;;###autoload
34(defun repeat-matching-complex-command (&optional pattern)
35 "Edit and re-evaluate complex command with name matching PATTERN.
36Matching occurrences are displayed, most recent first, until you select
37a form for evaluation. If PATTERN is empty (or nil), every form in the
38command history is offered. The form is placed in the minibuffer for
39editing and the result is evaluated."
40 (interactive "sRedo Command (regexp): ")
41 (if pattern
daaf0197
KH
42 (if (string-match "[^ \t]" pattern)
43 (setq pattern (substring pattern (match-beginning 0)))
44 (setq pattern nil)))
e2fbf49d
JB
45 (let ((history command-history)
46 (temp)
47 (what))
48 (while (and history (not what))
49 (setq temp (car history))
50 (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
daaf0197 51 (y-or-n-p (format "Redo %S? " temp)))
e2fbf49d
JB
52 (setq what (car history))
53 (setq history (cdr history))))
54 (if (not what)
55 (error "Command history exhausted")
56 ;; Try to remove any useless command history element for this command.
57 (if (eq (car (car command-history)) 'repeat-matching-complex-command)
58 (setq command-history (cdr command-history)))
59 (edit-and-eval-command "Redo: " what))))
60
61(defvar default-command-history-filter-garbage
62 '(command-history-mode
63 list-command-history
64 electric-command-history)
daaf0197
KH
65 "*A list of symbols to be ignored by `default-command-history-filter'.
66It that function is given a list whose car is an element of this list,
67then it will return non-nil (indicating the list should be discarded from
68the history).
e2fbf49d
JB
69Initially, all commands related to the command history are discarded.")
70
71(defvar list-command-history-filter 'default-command-history-filter
daaf0197
KH
72 "Predicate to test which commands should be excluded from the history listing.
73If non-nil, should be the name of a function of one argument.
e2fbf49d
JB
74It is passed each element of the command history when
75\\[list-command-history] is called. If the filter returns non-nil for
76some element, that element is excluded from the history listing. The
77default filter removes commands associated with the command-history.")
78
79(defun default-command-history-filter (frob)
80 "Filter commands matching `default-command-history-filter-garbage' list
81from the command history."
82 (or (not (consp frob))
83 (memq (car frob) default-command-history-filter-garbage)))
84
85(defvar list-command-history-max 32
daaf0197 86 "*If non-nil, maximum length of the listing produced by `list-command-history'.")
e2fbf49d
JB
87
88;;;###autoload
89(defun list-command-history ()
90 "List history of commands typed to minibuffer.
91The number of commands listed is controlled by `list-command-history-max'.
92Calls value of `list-command-history-filter' (if non-nil) on each history
93element to judge if that element should be excluded from the list.
94
95The buffer is left in Command History mode."
96 (interactive)
97 (with-output-to-temp-buffer
98 "*Command History*"
99 (let ((history command-history)
100 (buffer-read-only nil)
101 (count (or list-command-history-max -1)))
102 (while (and (/= count 0) history)
103 (if (and (boundp 'list-command-history-filter)
104 list-command-history-filter
105 (funcall list-command-history-filter (car history)))
106 nil
107 (setq count (1- count))
108 (prin1 (car history))
109 (terpri))
110 (setq history (cdr history))))
111 (save-excursion
112 (set-buffer "*Command History*")
113 (goto-char (point-min))
114 (if (eobp)
115 (error "No command history")
116 (Command-history-setup)))))
117
118(defun Command-history-setup (&optional majormode modename keymap)
119 (set-buffer "*Command History*")
120 (use-local-map (or keymap command-history-map))
121 (lisp-mode-variables nil)
122 (set-syntax-table emacs-lisp-mode-syntax-table)
123 (setq buffer-read-only t)
124 (use-local-map (or keymap command-history-map))
125 (setq major-mode (or majormode 'command-history-mode))
126 (setq mode-name (or modename "Command History")))
127
128(defvar command-history-hook nil
129 "If non-nil, its value is called on entry to `command-history-mode'.")
130
131(defvar command-history-map nil)
132(if command-history-map
133 nil
134 (setq command-history-map
135 (nconc (make-sparse-keymap) shared-lisp-mode-map))
136 (suppress-keymap command-history-map)
137 (define-key command-history-map "x" 'command-history-repeat)
138 (define-key command-history-map "\n" 'next-line)
139 (define-key command-history-map "\r" 'next-line)
140 (define-key command-history-map "\177" 'previous-line))
141
142(defun command-history-repeat ()
143 "Repeat the command shown on the current line.
144The buffer for that command is the previous current buffer."
145 (interactive)
146 (save-excursion
147 (eval (prog1
148 (save-excursion
149 (beginning-of-line)
150 (read (current-buffer)))
151 (set-buffer
dd4e1002 152 (car (cdr (buffer-list))))))))
e2fbf49d
JB
153
154;;;###autoload
155(defun command-history-mode ()
156 "Major mode for examining commands from `command-history'.
157The number of commands listed is controlled by `list-command-history-max'.
158The command history is filtered by `list-command-history-filter' if non-nil.
159Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
160
161Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
162and digits provide prefix arguments. Tab does not indent.
163\\{command-history-map}
164Calls the value of `command-history-hook' if that is non-nil.
165The Command History listing is recomputed each time this mode is invoked."
166 (interactive)
167 (list-command-history)
168 (pop-to-buffer "*Command History*")
169 (run-hooks 'command-history-hook))
49116ac0
JB
170
171(provide 'chistory)
172
c0274f38 173;;; chistory.el ends here