Merge from emacs-23; up to 2010-06-08T03:06:47Z!dann@ics.uci.edu.
[emacs.git] / lisp / echistory.el
blob1d2c5d8a388501eb847f28ee9dd83129bee2c261
1 ;;; echistory.el --- Electric Command History Mode
3 ;; Copyright (C) 1985, 2001-2011 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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'electric) ; command loop
28 (require 'chistory) ; history lister
30 ;;;###autoload
31 (defun Electric-command-history-redo-expression (&optional noconfirm)
32 "Edit current history line in minibuffer and execute result.
33 With prefix arg NOCONFIRM, execute current line as-is without editing."
34 (interactive "P")
35 (let (todo)
36 (with-current-buffer "*Command History*"
37 (beginning-of-line)
38 (setq todo (read (current-buffer)))
39 (if (boundp 'electric-history-in-progress)
40 (if todo (throw 'electric-history-quit (list noconfirm todo)))))))
42 (defvar electric-history-map
43 (let ((map (make-sparse-keymap)))
44 (define-key map [t] 'Electric-history-undefined)
45 (define-key map "\e" (make-sparse-keymap))
46 (define-key map [?\e t] 'Electric-history-undefined)
47 (define-key map "\C-u" 'universal-argument)
48 (define-key map " " 'Electric-command-history-redo-expression)
49 (define-key map "!" 'Electric-command-history-redo-expression)
50 (define-key map "\e\C-x" 'eval-sexp)
51 (define-key map "\e\C-d" 'down-list)
52 (define-key map "\e\C-u" 'backward-up-list)
53 (define-key map "\e\C-b" 'backward-sexp)
54 (define-key map "\e\C-f" 'forward-sexp)
55 (define-key map "\e\C-a" 'beginning-of-defun)
56 (define-key map "\e\C-e" 'end-of-defun)
57 (define-key map "\e\C-n" 'forward-list)
58 (define-key map "\e\C-p" 'backward-list)
59 (define-key map "q" 'Electric-history-quit)
60 (define-key map "\C-c" nil)
61 (define-key map "\C-c\C-c" 'Electric-history-quit)
62 (define-key map "\C-]" 'Electric-history-quit)
63 (define-key map "\C-z" 'suspend-frame)
64 (define-key map (char-to-string help-char) 'Helper-help)
65 (define-key map "?" 'Helper-describe-bindings)
66 (define-key map "\e>" 'end-of-buffer)
67 (define-key map "\e<" 'beginning-of-buffer)
68 (define-key map "\n" 'next-line)
69 (define-key map "\r" 'next-line)
70 (define-key map "\177" 'previous-line)
71 (define-key map "\C-n" 'next-line)
72 (define-key map "\C-p" 'previous-line)
73 (define-key map "\ev" 'scroll-down)
74 (define-key map "\C-v" 'scroll-up)
75 (define-key map [home] 'beginning-of-buffer)
76 (define-key map [down] 'next-line)
77 (define-key map [up] 'previous-line)
78 (define-key map [prior] 'scroll-down)
79 (define-key map [next] 'scroll-up)
80 (define-key map "\C-l" 'recenter)
81 (define-key map "\e\C-v" 'scroll-other-window)
82 map)
83 "Keymap for Electric Command History mode.")
85 (defvar electric-command-history-hook nil
86 "If non-nil, its value is called by `electric-command-history'.")
88 (defun electric-command-history ()
89 "\\<electric-history-map>Major mode for examining and redoing commands from `command-history'.
90 This pops up a window with the Command History listing.
91 The number of command listed is controlled by `list-command-history-max'.
92 The command history is filtered by `list-command-history-filter' if non-nil.
93 Combines typeout Command History list window with menu like selection
94 of an expression from the history for re-evaluation in the *original* buffer.
96 The history displayed is filtered by `list-command-history-filter' if non-nil.
98 Like Emacs-Lisp mode except that characters do not insert themselves and
99 Tab and Linefeed do not indent. Instead these commands are provided:
100 \\{electric-history-map}
102 Calls the value of `electric-command-history-hook' if that is non-nil.
103 The Command History listing is recomputed each time this mode is invoked."
104 (interactive)
105 (let ((electric-history-in-progress t)
106 (old-buffer (current-buffer))
107 (todo))
108 (unwind-protect
109 (setq todo
110 (catch 'electric-history-quit
111 (save-window-excursion
112 (save-window-excursion
113 (list-command-history)
114 (set-buffer "*Command History*")
115 (Command-history-setup)
116 (setq major-mode 'electric-command-history)
117 (setq mode-name "Electric History")
118 (use-local-map electric-history-map))
119 (Electric-pop-up-window "*Command History*")
120 (run-hooks 'electric-command-history-hook)
121 (if (eobp)
122 (progn (ding)
123 (message "No command history.")
124 (throw 'electric-history-quit nil))
125 (let ((Helper-return-blurb "return to History"))
126 (Electric-command-loop 'electric-history-quit
127 "->" t))))))
128 (set-buffer "*Command History*")
129 (command-history-mode)
130 (bury-buffer (current-buffer)))
131 (if (consp todo)
132 (progn (set-buffer old-buffer)
133 (if (car todo)
134 (apply (car (car (cdr todo))) (cdr (car (cdr todo))))
135 (edit-and-eval-command "Redo: " (car (cdr todo))))))))
137 (defun Electric-history-undefined ()
138 (interactive)
139 (ding)
140 (message "%s" (substitute-command-keys "Type \\[Helper-help] for help, ? for commands, C-c C-c to quit, Space to execute"))
141 (sit-for 4))
143 (defun Electric-history-quit ()
144 "Quit Electric Command History, restoring previous window configuration."
145 (interactive)
146 (if (boundp 'electric-history-in-progress)
147 (progn (message "")
148 (throw 'electric-history-quit nil))))
150 (provide 'echistory)
152 ;;; echistory.el ends here