1 ;; chistory -- List command history
2 ;; Copyright (C) 1985 Free Software Foundation, Inc.
3 ;; Principal author K. Shane Hartman
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;; This really has nothing to do with list-command-history per se, but
23 ;; its a nice alternative to C-x ESC (repeat-complex-command) and
24 ;; functions as a lister if given no pattern. It's not important
25 ;; enough to warrant a file of its own.
28 (defun repeat-matching-complex-command (&optional pattern)
29 "Edit and re-evaluate complex command with name matching PATTERN.
30 Matching occurrences are displayed, most recent first, until you select
31 a form for evaluation. If PATTERN is empty (or nil), every form in the
32 command history is offered. The form is placed in the minibuffer for
33 editing and the result is evaluated."
34 (interactive "sRedo Command (regexp): ")
36 (if (equal (setq pattern
38 (or (string-match "[ \t]*[^ \t]" pattern)
42 (let ((history command-history)
45 (while (and history (not what))
46 (setq temp (car history))
47 (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
48 (y-or-n-p (format "Redo %s? " (setq temp (prin1-to-string temp)))))
49 (setq what (car history))
50 (setq history (cdr history))))
52 (error "Command history exhausted")
53 ;; Try to remove any useless command history element for this command.
54 (if (eq (car (car command-history)) 'repeat-matching-complex-command)
55 (setq command-history (cdr command-history)))
56 (edit-and-eval-command "Redo: " what))))
58 (defvar default-command-history-filter-garbage
59 '(command-history-mode
61 electric-command-history)
62 "*A list of symbols. If `default-list-command-history-filter' is
63 given a list whose car is an element of this list, then it will return
64 non-nil (indicating the list should be discarded from the history).
65 Initially, all commands related to the command history are discarded.")
67 (defvar list-command-history-filter 'default-command-history-filter
68 "If non-nil, should be the name of a function of one argument.
69 It is passed each element of the command history when
70 \\[list-command-history] is called. If the filter returns non-nil for
71 some element, that element is excluded from the history listing. The
72 default filter removes commands associated with the command-history.")
74 (defun default-command-history-filter (frob)
75 "Filter commands matching `default-command-history-filter-garbage' list
76 from the command history."
77 (or (not (consp frob))
78 (memq (car frob) default-command-history-filter-garbage)))
80 (defvar list-command-history-max 32
81 "*If non-nil, should be a positive number which specifies the maximum
82 length of the Command History listing produced by `list-command-history'.")
85 (defun list-command-history ()
86 "List history of commands typed to minibuffer.
87 The number of commands listed is controlled by `list-command-history-max'.
88 Calls value of `list-command-history-filter' (if non-nil) on each history
89 element to judge if that element should be excluded from the list.
91 The buffer is left in Command History mode."
93 (with-output-to-temp-buffer
95 (let ((history command-history)
96 (buffer-read-only nil)
97 (count (or list-command-history-max -1)))
98 (while (and (/= count 0) history)
99 (if (and (boundp 'list-command-history-filter)
100 list-command-history-filter
101 (funcall list-command-history-filter (car history)))
103 (setq count (1- count))
104 (prin1 (car history))
106 (setq history (cdr history))))
108 (set-buffer "*Command History*")
109 (goto-char (point-min))
111 (error "No command history")
112 (Command-history-setup)))))
114 (defun Command-history-setup (&optional majormode modename keymap)
115 (set-buffer "*Command History*")
116 (use-local-map (or keymap command-history-map))
117 (lisp-mode-variables nil)
118 (set-syntax-table emacs-lisp-mode-syntax-table)
119 (setq buffer-read-only t)
120 (use-local-map (or keymap command-history-map))
121 (setq major-mode (or majormode 'command-history-mode))
122 (setq mode-name (or modename "Command History")))
124 (defvar command-history-hook nil
125 "If non-nil, its value is called on entry to `command-history-mode'.")
127 (defvar command-history-map nil)
128 (if command-history-map
130 (setq command-history-map
131 (nconc (make-sparse-keymap) shared-lisp-mode-map))
132 (suppress-keymap command-history-map)
133 (define-key command-history-map "x" 'command-history-repeat)
134 (define-key command-history-map "\n" 'next-line)
135 (define-key command-history-map "\r" 'next-line)
136 (define-key command-history-map "\177" 'previous-line))
138 (defun command-history-repeat ()
139 "Repeat the command shown on the current line.
140 The buffer for that command is the previous current buffer."
146 (read (current-buffer)))
148 (cdr (buffer-list)))))))
151 (defun command-history-mode ()
152 "Major mode for examining commands from `command-history'.
153 The number of commands listed is controlled by `list-command-history-max'.
154 The command history is filtered by `list-command-history-filter' if non-nil.
155 Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
157 Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
158 and digits provide prefix arguments. Tab does not indent.
159 \\{command-history-map}
160 Calls the value of `command-history-hook' if that is non-nil.
161 The Command History listing is recomputed each time this mode is invoked."
163 (list-command-history)
164 (pop-to-buffer "*Command History*")
165 (run-hooks 'command-history-hook))