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