comparison lisp/packages/chistory.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; chistory.el --- list command history
2
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
4
5 ;; Author: K. Shane Hartman
6 ;; Maintainer: FSF
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; 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.
14
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Synched up with: FSF 19.30.
25
26 ;;; Commentary:
27
28 ;; This really has nothing to do with list-command-history per se, but
29 ;; its a nice alternative to C-x ESC ESC (repeat-complex-command) and
30 ;; functions as a lister if given no pattern. It's not important
31 ;; enough to warrant a file of its own.
32
33 ;;; Code:
34
35 (defvar command-history-map nil)
36
37 (defvar command-history-hook nil
38 "If non-nil, its value is called on entry to `command-history-mode'.")
39
40 ;;;###autoload
41 (defun repeat-matching-complex-command (&optional pattern)
42 "Edit and re-evaluate complex command with name matching PATTERN.
43 Matching occurrences are displayed, most recent first, until you
44 select a form for evaluation. If PATTERN is empty (or nil), every form
45 in the command history is offered. The form is placed in the minibuffer
46 for editing and the result is evaluated."
47 (interactive "sRedo Command (regexp): ")
48 (if pattern
49 (if (string-match "[^ \t]" pattern)
50 (setq pattern (substring pattern (match-beginning 0)))
51 (setq pattern nil)))
52 (let ((history command-history)
53 (temp)
54 (what))
55 (while (and history (not what))
56 (setq temp (car history))
57 (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
58 (y-or-n-p (format "Redo %S? " temp)))
59 (setq what (car history))
60 (setq history (cdr history))))
61 (if (not what)
62 (error "Command history exhausted")
63 ;; Try to remove any useless command history element for this command.
64 (if (eq (car (car command-history)) 'repeat-matching-complex-command)
65 (setq command-history (cdr command-history)))
66 (edit-and-eval-command "Redo: " what))))
67
68 (defvar default-command-history-filter-garbage
69 '(command-history-mode
70 list-command-history
71 electric-command-history)
72 "*A list of symbols to be ignored by `default-command-history-filter'.
73 It that function is given a list whose car is an element of this list,
74 then it will return non-nil (indicating the list should be discarded from
75 the history).
76 Initially, all commands related to the command history are discarded.")
77
78 (defvar list-command-history-filter 'default-command-history-filter
79 "Predicate to test which commands should be excluded from the history listing.
80 If non-nil, should be the name of a function of one argument.
81 It is passed each element of the command history when
82 \\[list-command-history] is called. If the filter returns non-nil for
83 some element, that element is excluded from the history listing. The
84 default filter removes commands associated with the command-history.")
85
86 (defun default-command-history-filter (frob)
87 "Filter commands matching `default-command-history-filter-garbage' list
88 from the command history."
89 (or (not (consp frob))
90 (memq (car frob) default-command-history-filter-garbage)))
91
92 (defvar list-command-history-max 32
93 "*If non-nil, maximum length of the listing produced by `list-command-history'.")
94
95 ;;;###autoload
96 (defun list-command-history ()
97 "List history of commands typed to minibuffer.
98 The number of commands listed is controlled by `list-command-history-max'.
99 Calls value of `list-command-history-filter' (if non-nil) on each history
100 element to judge if that element should be excluded from the list.
101
102 The buffer is left in Command History mode."
103 (interactive)
104 (with-output-to-temp-buffer
105 "*Command History*"
106 (let ((history command-history)
107 (buffer-read-only nil)
108 (count (or list-command-history-max -1)))
109 (while (and (/= count 0) history)
110 (if (and (boundp 'list-command-history-filter)
111 list-command-history-filter
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))))
118 (save-excursion
119 (set-buffer "*Command History*")
120 (goto-char (point-min))
121 (if (eobp)
122 (error "No command history")
123 (Command-history-setup)))))
124
125 (defun Command-history-setup (&optional majormode modename keymap)
126 (set-buffer "*Command History*")
127 (use-local-map (or keymap command-history-map))
128 (lisp-mode-variables nil)
129 (set-syntax-table emacs-lisp-mode-syntax-table)
130 (setq buffer-read-only t)
131 (use-local-map (or keymap command-history-map))
132 (setq major-mode (or majormode 'command-history-mode))
133 (setq mode-name (or modename "Command History")))
134
135 (if command-history-map
136 nil
137 (setq command-history-map (make-sparse-keymap))
138 (set-keymap-parent command-history-map shared-lisp-mode-map)
139 (set-keymap-name command-history-map 'command-history-map)
140 (suppress-keymap command-history-map)
141 (define-key command-history-map "x" 'command-history-repeat)
142 (define-key command-history-map "\n" 'next-line)
143 (define-key command-history-map "\r" 'next-line)
144 (define-key command-history-map "\177" 'previous-line))
145
146 (defun command-history-repeat ()
147 "Repeat the command shown on the current line.
148 The buffer for that command is the previous current buffer."
149 (interactive)
150 (save-excursion
151 (eval (prog1
152 (save-excursion
153 (beginning-of-line)
154 (read (current-buffer)))
155 (set-buffer
156 (car (cdr (buffer-list))))))))
157
158 ;;;###autoload
159 (defun command-history-mode ()
160 "Major mode for examining commands from `command-history'.
161 The number of commands listed is controlled by `list-command-history-max'.
162 The command history is filtered by `list-command-history-filter' if non-nil.
163 Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
164
165 Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
166 and digits provide prefix arguments. Tab does not indent.
167 \\{command-history-map}
168 Calls the value of `command-history-hook' if that is non-nil.
169 The Command History listing is recomputed each time this mode is invoked."
170 (interactive)
171 (list-command-history)
172 (pop-to-buffer "*Command History*")
173 (run-hooks 'command-history-hook))
174
175 (provide 'chistory)
176
177 ;;; chistory.el ends here