comparison lisp/chistory.el @ 411:cb75ab565260

Initial revision
author Jim Blandy <jimb@redhat.com>
date Tue, 27 Aug 1991 03:29:52 +0000
parents
children 4cd7543be581
comparison
equal deleted inserted replaced
410:7812e9efc1af 411:cb75ab565260
1 ;; chistory -- List command history
2 ;; Copyright (C) 1985 Free Software Foundation, Inc.
3 ;; Principal author K. Shane Hartman
4
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
20
21
22 (provide 'chistory)
23
24 ;; This really has nothing to do with list-command-history per se, but
25 ;; its a nice alternative to C-x ESC (repeat-complex-command) and
26 ;; functions as a lister if given no pattern. It's not important
27 ;; enough to warrant a file of its own.
28
29 ;;;###autoload
30 (defun repeat-matching-complex-command (&optional pattern)
31 "Edit and re-evaluate complex command with name matching PATTERN.
32 Matching occurrences are displayed, most recent first, until you select
33 a form for evaluation. If PATTERN is empty (or nil), every form in the
34 command history is offered. The form is placed in the minibuffer for
35 editing and the result is evaluated."
36 (interactive "sRedo Command (regexp): ")
37 (if pattern
38 (if (equal (setq pattern
39 (substring pattern
40 (or (string-match "[ \t]*[^ \t]" pattern)
41 (length pattern))))
42 "")
43 (setq pattern nil)))
44 (let ((history command-history)
45 (temp)
46 (what))
47 (while (and history (not what))
48 (setq temp (car history))
49 (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
50 (y-or-n-p (format "Redo %s? " (setq temp (prin1-to-string temp)))))
51 (setq what (car history))
52 (setq history (cdr history))))
53 (if (not what)
54 (error "Command history exhausted")
55 ;; Try to remove any useless command history element for this command.
56 (if (eq (car (car command-history)) 'repeat-matching-complex-command)
57 (setq command-history (cdr command-history)))
58 (edit-and-eval-command "Redo: " what))))
59
60 (defvar default-command-history-filter-garbage
61 '(command-history-mode
62 list-command-history
63 electric-command-history)
64 "*A list of symbols. If `default-list-command-history-filter' is
65 given a list whose car is an element of this list, then it will return
66 non-nil (indicating the list should be discarded from the history).
67 Initially, all commands related to the command history are discarded.")
68
69 (defvar list-command-history-filter 'default-command-history-filter
70 "If non-nil, should be the name of a function of one argument.
71 It is passed each element of the command history when
72 \\[list-command-history] is called. If the filter returns non-nil for
73 some element, that element is excluded from the history listing. The
74 default filter removes commands associated with the command-history.")
75
76 (defun default-command-history-filter (frob)
77 "Filter commands matching `default-command-history-filter-garbage' list
78 from the command history."
79 (or (not (consp frob))
80 (memq (car frob) default-command-history-filter-garbage)))
81
82 (defvar list-command-history-max 32
83 "*If non-nil, should be a positive number which specifies the maximum
84 length of the Command History listing produced by `list-command-history'.")
85
86 ;;;###autoload
87 (defun list-command-history ()
88 "List history of commands typed to minibuffer.
89 The number of commands listed is controlled by `list-command-history-max'.
90 Calls value of `list-command-history-filter' (if non-nil) on each history
91 element to judge if that element should be excluded from the list.
92
93 The buffer is left in Command History mode."
94 (interactive)
95 (with-output-to-temp-buffer
96 "*Command History*"
97 (let ((history command-history)
98 (buffer-read-only nil)
99 (count (or list-command-history-max -1)))
100 (while (and (/= count 0) history)
101 (if (and (boundp 'list-command-history-filter)
102 list-command-history-filter
103 (funcall list-command-history-filter (car history)))
104 nil
105 (setq count (1- count))
106 (prin1 (car history))
107 (terpri))
108 (setq history (cdr history))))
109 (save-excursion
110 (set-buffer "*Command History*")
111 (goto-char (point-min))
112 (if (eobp)
113 (error "No command history")
114 (Command-history-setup)))))
115
116 (defun Command-history-setup (&optional majormode modename keymap)
117 (set-buffer "*Command History*")
118 (use-local-map (or keymap command-history-map))
119 (lisp-mode-variables nil)
120 (set-syntax-table emacs-lisp-mode-syntax-table)
121 (setq buffer-read-only t)
122 (use-local-map (or keymap command-history-map))
123 (setq major-mode (or majormode 'command-history-mode))
124 (setq mode-name (or modename "Command History")))
125
126 (defvar command-history-hook nil
127 "If non-nil, its value is called on entry to `command-history-mode'.")
128
129 (defvar command-history-map nil)
130 (if command-history-map
131 nil
132 (setq command-history-map
133 (nconc (make-sparse-keymap) shared-lisp-mode-map))
134 (suppress-keymap command-history-map)
135 (define-key command-history-map "x" 'command-history-repeat)
136 (define-key command-history-map "\n" 'next-line)
137 (define-key command-history-map "\r" 'next-line)
138 (define-key command-history-map "\177" 'previous-line))
139
140 (defun command-history-repeat ()
141 "Repeat the command shown on the current line.
142 The buffer for that command is the previous current buffer."
143 (interactive)
144 (save-excursion
145 (eval (prog1
146 (save-excursion
147 (beginning-of-line)
148 (read (current-buffer)))
149 (set-buffer
150 (cdr (buffer-list)))))))
151
152 ;;;###autoload
153 (defun command-history-mode ()
154 "Major mode for examining commands from `command-history'.
155 The number of commands listed is controlled by `list-command-history-max'.
156 The command history is filtered by `list-command-history-filter' if non-nil.
157 Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
158
159 Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
160 and digits provide prefix arguments. Tab does not indent.
161 \\{command-history-map}
162 Calls the value of `command-history-hook' if that is non-nil.
163 The Command History listing is recomputed each time this mode is invoked."
164 (interactive)
165 (list-command-history)
166 (pop-to-buffer "*Command History*")
167 (run-hooks 'command-history-hook))