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