*** empty log message ***
[emacs.git] / lisp / chistory.el
blobe6871f53e494deafa16ae2cbf453852c1dba23ce
1 ;;; chistory.el --- list command history
3 ;; Author: K. Shane Hartman
4 ;; Maintainer: FSF
5 ;; Last-Modified: 16 Mar 1992
7 ;; Copyright (C) 1985 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 (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 (equal (setq pattern
44 (substring pattern
45 (or (string-match "[ \t]*[^ \t]" pattern)
46 (length pattern))))
47 "")
48 (setq pattern nil)))
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))))
55 (y-or-n-p (format "Redo %s? " (setq temp (prin1-to-string temp)))))
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))))
65 (defvar default-command-history-filter-garbage
66 '(command-history-mode
67 list-command-history
68 electric-command-history)
69 "*A list of symbols. If `default-list-command-history-filter' is
70 given a list whose car is an element of this list, then it will return
71 non-nil (indicating the list should be discarded from the history).
72 Initially, all commands related to the command history are discarded.")
74 (defvar list-command-history-filter 'default-command-history-filter
75 "If non-nil, should be the name of a function of one argument.
76 It is passed each element of the command history when
77 \\[list-command-history] is called. If the filter returns non-nil for
78 some element, that element is excluded from the history listing. The
79 default filter removes commands associated with the command-history.")
81 (defun default-command-history-filter (frob)
82 "Filter commands matching `default-command-history-filter-garbage' list
83 from the command history."
84 (or (not (consp frob))
85 (memq (car frob) default-command-history-filter-garbage)))
87 (defvar list-command-history-max 32
88 "*If non-nil, should be a positive number which specifies the maximum
89 length of the Command History listing produced by `list-command-history'.")
91 ;;;###autoload
92 (defun list-command-history ()
93 "List history of commands typed to minibuffer.
94 The number of commands listed is controlled by `list-command-history-max'.
95 Calls value of `list-command-history-filter' (if non-nil) on each history
96 element to judge if that element should be excluded from the list.
98 The buffer is left in Command History mode."
99 (interactive)
100 (with-output-to-temp-buffer
101 "*Command History*"
102 (let ((history command-history)
103 (buffer-read-only nil)
104 (count (or list-command-history-max -1)))
105 (while (and (/= count 0) history)
106 (if (and (boundp 'list-command-history-filter)
107 list-command-history-filter
108 (funcall list-command-history-filter (car history)))
110 (setq count (1- count))
111 (prin1 (car history))
112 (terpri))
113 (setq history (cdr history))))
114 (save-excursion
115 (set-buffer "*Command History*")
116 (goto-char (point-min))
117 (if (eobp)
118 (error "No command history")
119 (Command-history-setup)))))
121 (defun Command-history-setup (&optional majormode modename keymap)
122 (set-buffer "*Command History*")
123 (use-local-map (or keymap command-history-map))
124 (lisp-mode-variables nil)
125 (set-syntax-table emacs-lisp-mode-syntax-table)
126 (setq buffer-read-only t)
127 (use-local-map (or keymap command-history-map))
128 (setq major-mode (or majormode 'command-history-mode))
129 (setq mode-name (or modename "Command History")))
131 (defvar command-history-hook nil
132 "If non-nil, its value is called on entry to `command-history-mode'.")
134 (defvar command-history-map nil)
135 (if command-history-map
137 (setq command-history-map
138 (nconc (make-sparse-keymap) shared-lisp-mode-map))
139 (suppress-keymap command-history-map)
140 (define-key command-history-map "x" 'command-history-repeat)
141 (define-key command-history-map "\n" 'next-line)
142 (define-key command-history-map "\r" 'next-line)
143 (define-key command-history-map "\177" 'previous-line))
145 (defun command-history-repeat ()
146 "Repeat the command shown on the current line.
147 The buffer for that command is the previous current buffer."
148 (interactive)
149 (save-excursion
150 (eval (prog1
151 (save-excursion
152 (beginning-of-line)
153 (read (current-buffer)))
154 (set-buffer
155 (cdr (buffer-list)))))))
157 ;;;###autoload
158 (defun command-history-mode ()
159 "Major mode for examining commands from `command-history'.
160 The number of commands listed is controlled by `list-command-history-max'.
161 The command history is filtered by `list-command-history-filter' if non-nil.
162 Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
164 Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
165 and digits provide prefix arguments. Tab does not indent.
166 \\{command-history-map}
167 Calls the value of `command-history-hook' if that is non-nil.
168 The Command History listing is recomputed each time this mode is invoked."
169 (interactive)
170 (list-command-history)
171 (pop-to-buffer "*Command History*")
172 (run-hooks 'command-history-hook))
174 (provide 'chistory)
176 ;;; chistory.el ends here