new version
[emacs.git] / lisp / chistory.el
blob3685e01db5c33396d09349e671f97b96457b7e8c
1 ;;; chistory.el --- list command history
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
5 ;; Author: K. Shane Hartman
6 ;; Maintainer: FSF
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
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.
25 ;;; Commentary:
27 ;; This really has nothing to do with list-command-history per se, but
28 ;; its a nice alternative to C-x ESC ESC (repeat-complex-command) and
29 ;; functions as a lister if given no pattern. It's not important
30 ;; enough to warrant a file of its own.
32 ;;; Code:
34 ;;;###autoload
35 (defun repeat-matching-complex-command (&optional pattern)
36 "Edit and re-evaluate complex command with name matching PATTERN.
37 Matching occurrences are displayed, most recent first, until you select
38 a form for evaluation. If PATTERN is empty (or nil), every form in the
39 command history is offered. The form is placed in the minibuffer for
40 editing and the result is evaluated."
41 (interactive "sRedo Command (regexp): ")
42 (if pattern
43 (if (string-match "[^ \t]" pattern)
44 (setq pattern (substring pattern (match-beginning 0)))
45 (setq pattern nil)))
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))))
52 (y-or-n-p (format "Redo %S? " temp)))
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))))
62 (defvar default-command-history-filter-garbage
63 '(command-history-mode
64 list-command-history
65 electric-command-history)
66 "*A list of symbols to be ignored by `default-command-history-filter'.
67 It that function is given a list whose car is an element of this list,
68 then it will return non-nil (indicating the list should be discarded from
69 the history).
70 Initially, all commands related to the command history are discarded.")
72 (defvar list-command-history-filter 'default-command-history-filter
73 "Predicate to test which commands should be excluded from the history listing.
74 If non-nil, should be the name of a function of one argument.
75 It is passed each element of the command history when
76 \\[list-command-history] is called. If the filter returns non-nil for
77 some element, that element is excluded from the history listing. The
78 default filter removes commands associated with the command-history.")
80 (defun default-command-history-filter (frob)
81 "Filter commands matching `default-command-history-filter-garbage' list
82 from the command history."
83 (or (not (consp frob))
84 (memq (car frob) default-command-history-filter-garbage)))
86 (defvar list-command-history-max 32
87 "*If non-nil, maximum length of the listing produced by `list-command-history'.")
89 ;;;###autoload
90 (defun list-command-history ()
91 "List history of commands typed to minibuffer.
92 The number of commands listed is controlled by `list-command-history-max'.
93 Calls value of `list-command-history-filter' (if non-nil) on each history
94 element to judge if that element should be excluded from the list.
96 The 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)))
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)))))
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")))
129 (defvar command-history-hook nil
130 "If non-nil, its value is called on entry to `command-history-mode'.")
132 (defvar command-history-map nil)
133 (if command-history-map
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))
143 (defun command-history-repeat ()
144 "Repeat the command shown on the current line.
145 The 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
153 (car (cdr (buffer-list))))))))
155 ;;;###autoload
156 (defun command-history-mode ()
157 "Major mode for examining commands from `command-history'.
158 The number of commands listed is controlled by `list-command-history-max'.
159 The command history is filtered by `list-command-history-filter' if non-nil.
160 Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
162 Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
163 and digits provide prefix arguments. Tab does not indent.
164 \\{command-history-map}
165 Calls the value of `command-history-hook' if that is non-nil.
166 The 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))
172 (provide 'chistory)
174 ;;; chistory.el ends here