Merge from trunk
[bpt/emacs.git] / lisp / echistory.el
CommitLineData
c0274f38
ER
1;;; echistory.el --- Electric Command History Mode
2
73b0cd50 3;; Copyright (C) 1985, 2001-2011 Free Software Foundation, Inc.
3a801d0c 4
e5167999
ER
5;; Author: K. Shane Hartman
6;; Maintainer: FSF
e9e56e09
RS
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
e9e56e09 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
e9e56e09
RS
14
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.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
e9e56e09 22
55535639
PJ
23;;; Commentary:
24
e5167999 25;;; Code:
e9e56e09
RS
26
27(require 'electric) ; command loop
28(require 'chistory) ; history lister
29
f9f9507e 30;;;###autoload
e9e56e09
RS
31(defun Electric-command-history-redo-expression (&optional noconfirm)
32 "Edit current history line in minibuffer and execute result.
33With prefix arg NOCONFIRM, execute current line as-is without editing."
34 (interactive "P")
35 (let (todo)
7fdbcd83 36 (with-current-buffer "*Command History*"
e9e56e09
RS
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)))))))
41
a0de8967
JPW
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.")
e9e56e09
RS
84
85(defvar electric-command-history-hook nil
86 "If non-nil, its value is called by `electric-command-history'.")
87
88(defun electric-command-history ()
89 "\\<electric-history-map>Major mode for examining and redoing commands from `command-history'.
90This pops up a window with the Command History listing.
91The number of command listed is controlled by `list-command-history-max'.
92The command history is filtered by `list-command-history-filter' if non-nil.
93Combines typeout Command History list window with menu like selection
94of an expression from the history for re-evaluation in the *original* buffer.
95
96The history displayed is filtered by `list-command-history-filter' if non-nil.
97
98Like Emacs-Lisp mode except that characters do not insert themselves and
99Tab and Linefeed do not indent. Instead these commands are provided:
100\\{electric-history-map}
101
102Calls the value of `electric-command-history-hook' if that is non-nil.
103The 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*")
e65f2cc0
RS
115 (Command-history-setup)
116 (setq major-mode 'electric-command-history)
117 (setq mode-name "Electric History")
118 (use-local-map electric-history-map))
e9e56e09
RS
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*")
e65f2cc0 129 (command-history-mode)
e9e56e09
RS
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))))))))
136
137(defun Electric-history-undefined ()
138 (interactive)
139 (ding)
8a26c165 140 (message "%s" (substitute-command-keys "Type \\[Helper-help] for help, ? for commands, C-c C-c to quit, Space to execute"))
e9e56e09
RS
141 (sit-for 4))
142
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))))
c0274f38 149
896546cd
RS
150(provide 'echistory)
151
c0274f38 152;;; echistory.el ends here