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