Update FSF's address.
[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
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
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
e2fbf49d
JB
34;;;###autoload
35(defun repeat-matching-complex-command (&optional pattern)
36 "Edit and re-evaluate complex command with name matching PATTERN.
37Matching occurrences are displayed, most recent first, until you select
38a form for evaluation. If PATTERN is empty (or nil), every form in the
39command history is offered. The form is placed in the minibuffer for
40editing and the result is evaluated."
41 (interactive "sRedo Command (regexp): ")
42 (if pattern
daaf0197
KH
43 (if (string-match "[^ \t]" pattern)
44 (setq pattern (substring pattern (match-beginning 0)))
45 (setq pattern nil)))
e2fbf49d
JB
46 (let ((history command-history)
47 (temp)
48 (what))
49 (while (and history (not what))
50 (setq temp (car history))
51 (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
daaf0197 52 (y-or-n-p (format "Redo %S? " temp)))
e2fbf49d
JB
53 (setq what (car history))
54 (setq history (cdr history))))
55 (if (not what)
56 (error "Command history exhausted")
57 ;; Try to remove any useless command history element for this command.
58 (if (eq (car (car command-history)) 'repeat-matching-complex-command)
59 (setq command-history (cdr command-history)))
60 (edit-and-eval-command "Redo: " what))))
61
62(defvar default-command-history-filter-garbage
63 '(command-history-mode
64 list-command-history
65 electric-command-history)
daaf0197
KH
66 "*A list of symbols to be ignored by `default-command-history-filter'.
67It that function is given a list whose car is an element of this list,
68then it will return non-nil (indicating the list should be discarded from
69the history).
e2fbf49d
JB
70Initially, all commands related to the command history are discarded.")
71
72(defvar list-command-history-filter 'default-command-history-filter
daaf0197
KH
73 "Predicate to test which commands should be excluded from the history listing.
74If non-nil, should be the name of a function of one argument.
e2fbf49d
JB
75It is passed each element of the command history when
76\\[list-command-history] is called. If the filter returns non-nil for
77some element, that element is excluded from the history listing. The
78default filter removes commands associated with the command-history.")
79
80(defun default-command-history-filter (frob)
81 "Filter commands matching `default-command-history-filter-garbage' list
82from the command history."
83 (or (not (consp frob))
84 (memq (car frob) default-command-history-filter-garbage)))
85
86(defvar list-command-history-max 32
daaf0197 87 "*If non-nil, maximum length of the listing produced by `list-command-history'.")
e2fbf49d
JB
88
89;;;###autoload
90(defun list-command-history ()
91 "List history of commands typed to minibuffer.
92The number of commands listed is controlled by `list-command-history-max'.
93Calls value of `list-command-history-filter' (if non-nil) on each history
94element to judge if that element should be excluded from the list.
95
96The buffer is left in Command History mode."
97 (interactive)
98 (with-output-to-temp-buffer
99 "*Command History*"
100 (let ((history command-history)
101 (buffer-read-only nil)
102 (count (or list-command-history-max -1)))
103 (while (and (/= count 0) history)
104 (if (and (boundp 'list-command-history-filter)
105 list-command-history-filter
106 (funcall list-command-history-filter (car history)))
107 nil
108 (setq count (1- count))
109 (prin1 (car history))
110 (terpri))
111 (setq history (cdr history))))
112 (save-excursion
113 (set-buffer "*Command History*")
114 (goto-char (point-min))
115 (if (eobp)
116 (error "No command history")
117 (Command-history-setup)))))
118
119(defun Command-history-setup (&optional majormode modename keymap)
120 (set-buffer "*Command History*")
121 (use-local-map (or keymap command-history-map))
122 (lisp-mode-variables nil)
123 (set-syntax-table emacs-lisp-mode-syntax-table)
124 (setq buffer-read-only t)
125 (use-local-map (or keymap command-history-map))
126 (setq major-mode (or majormode 'command-history-mode))
127 (setq mode-name (or modename "Command History")))
128
129(defvar command-history-hook nil
130 "If non-nil, its value is called on entry to `command-history-mode'.")
131
132(defvar command-history-map nil)
133(if command-history-map
134 nil
135 (setq command-history-map
136 (nconc (make-sparse-keymap) shared-lisp-mode-map))
137 (suppress-keymap command-history-map)
138 (define-key command-history-map "x" 'command-history-repeat)
139 (define-key command-history-map "\n" 'next-line)
140 (define-key command-history-map "\r" 'next-line)
141 (define-key command-history-map "\177" 'previous-line))
142
143(defun command-history-repeat ()
144 "Repeat the command shown on the current line.
145The buffer for that command is the previous current buffer."
146 (interactive)
147 (save-excursion
148 (eval (prog1
149 (save-excursion
150 (beginning-of-line)
151 (read (current-buffer)))
152 (set-buffer
dd4e1002 153 (car (cdr (buffer-list))))))))
e2fbf49d
JB
154
155;;;###autoload
156(defun command-history-mode ()
157 "Major mode for examining commands from `command-history'.
158The number of commands listed is controlled by `list-command-history-max'.
159The command history is filtered by `list-command-history-filter' if non-nil.
160Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
161
162Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
163and digits provide prefix arguments. Tab does not indent.
164\\{command-history-map}
165Calls the value of `command-history-hook' if that is non-nil.
166The Command History listing is recomputed each time this mode is invoked."
167 (interactive)
168 (list-command-history)
169 (pop-to-buffer "*Command History*")
170 (run-hooks 'command-history-hook))
49116ac0
JB
171
172(provide 'chistory)
173
c0274f38 174;;; chistory.el ends here