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