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