echistory.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ;;; echistory.el --- Electric Command History Mode
  2. ;; Copyright (C) 1985, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: K. Shane Hartman
  4. ;; Maintainer: FSF
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'electric) ; command loop
  19. (require 'chistory) ; history lister
  20. ;; Dynamically bound in electric-command-history
  21. (defvar electric-history-in-progress)
  22. ;;;###autoload
  23. (defun Electric-command-history-redo-expression (&optional noconfirm)
  24. "Edit current history line in minibuffer and execute result.
  25. With prefix arg NOCONFIRM, execute current line as-is without editing."
  26. (interactive "P")
  27. (let (todo)
  28. (with-current-buffer "*Command History*"
  29. (beginning-of-line)
  30. (setq todo (read (current-buffer)))
  31. (if (boundp 'electric-history-in-progress)
  32. (if todo (throw 'electric-history-quit (list noconfirm todo)))))))
  33. (defvar electric-history-map
  34. (let ((map (make-sparse-keymap)))
  35. (define-key map [t] 'Electric-history-undefined)
  36. (define-key map "\e" (make-sparse-keymap))
  37. (define-key map [?\e t] 'Electric-history-undefined)
  38. (define-key map "\C-u" 'universal-argument)
  39. (define-key map " " 'Electric-command-history-redo-expression)
  40. (define-key map "!" 'Electric-command-history-redo-expression)
  41. (define-key map "\e\C-x" 'eval-sexp)
  42. (define-key map "\e\C-d" 'down-list)
  43. (define-key map "\e\C-u" 'backward-up-list)
  44. (define-key map "\e\C-b" 'backward-sexp)
  45. (define-key map "\e\C-f" 'forward-sexp)
  46. (define-key map "\e\C-a" 'beginning-of-defun)
  47. (define-key map "\e\C-e" 'end-of-defun)
  48. (define-key map "\e\C-n" 'forward-list)
  49. (define-key map "\e\C-p" 'backward-list)
  50. (define-key map "q" 'Electric-history-quit)
  51. (define-key map "\C-c" nil)
  52. (define-key map "\C-c\C-c" 'Electric-history-quit)
  53. (define-key map "\C-]" 'Electric-history-quit)
  54. (define-key map "\C-z" 'suspend-frame)
  55. (define-key map (char-to-string help-char) 'Helper-help)
  56. (define-key map "?" 'Helper-describe-bindings)
  57. (define-key map "\e>" 'end-of-buffer)
  58. (define-key map "\e<" 'beginning-of-buffer)
  59. (define-key map "\n" 'next-line)
  60. (define-key map "\r" 'next-line)
  61. (define-key map "\177" 'previous-line)
  62. (define-key map "\C-n" 'next-line)
  63. (define-key map "\C-p" 'previous-line)
  64. (define-key map "\ev" 'scroll-down)
  65. (define-key map "\C-v" 'scroll-up)
  66. (define-key map [home] 'beginning-of-buffer)
  67. (define-key map [down] 'next-line)
  68. (define-key map [up] 'previous-line)
  69. (define-key map [prior] 'scroll-down)
  70. (define-key map [next] 'scroll-up)
  71. (define-key map "\C-l" 'recenter)
  72. (define-key map "\e\C-v" 'scroll-other-window)
  73. map)
  74. "Keymap for Electric Command History mode.")
  75. (defvar electric-command-history-hook nil
  76. "If non-nil, its value is called by `electric-command-history'.")
  77. (defvar Helper-return-blurb) ; from helper.el
  78. (defun electric-command-history ()
  79. "\\<electric-history-map>Major mode for examining and redoing commands from `command-history'.
  80. This pops up a window with the Command History listing.
  81. The number of command listed is controlled by `list-command-history-max'.
  82. The command history is filtered by `list-command-history-filter' if non-nil.
  83. Combines typeout Command History list window with menu like selection
  84. of an expression from the history for re-evaluation in the *original* buffer.
  85. The history displayed is filtered by `list-command-history-filter' if non-nil.
  86. Like Emacs-Lisp mode except that characters do not insert themselves and
  87. Tab and Linefeed do not indent. Instead these commands are provided:
  88. \\{electric-history-map}
  89. Calls the value of `electric-command-history-hook' if that is non-nil.
  90. The Command History listing is recomputed each time this mode is invoked."
  91. (interactive)
  92. (let ((electric-history-in-progress t)
  93. (old-buffer (current-buffer))
  94. (todo))
  95. (unwind-protect
  96. (setq todo
  97. (catch 'electric-history-quit
  98. (save-window-excursion
  99. (save-window-excursion
  100. (list-command-history)
  101. (set-buffer "*Command History*")
  102. (Command-history-setup)
  103. (setq major-mode 'electric-command-history)
  104. (setq mode-name "Electric History")
  105. (use-local-map electric-history-map))
  106. (Electric-pop-up-window "*Command History*")
  107. (run-hooks 'electric-command-history-hook)
  108. (if (eobp)
  109. (progn (ding)
  110. (message "No command history.")
  111. (throw 'electric-history-quit nil))
  112. (let ((Helper-return-blurb "return to History"))
  113. (Electric-command-loop 'electric-history-quit
  114. "->" t))))))
  115. (set-buffer "*Command History*")
  116. (command-history-mode)
  117. (bury-buffer (current-buffer)))
  118. (if (consp todo)
  119. (progn (set-buffer old-buffer)
  120. (if (car todo)
  121. (apply (car (car (cdr todo))) (cdr (car (cdr todo))))
  122. (edit-and-eval-command "Redo: " (car (cdr todo))))))))
  123. (defun Electric-history-undefined ()
  124. (interactive)
  125. (ding)
  126. (message "%s" (substitute-command-keys "Type \\[Helper-help] for help, ? for commands, C-c C-c to quit, Space to execute"))
  127. (sit-for 4))
  128. (defun Electric-history-quit ()
  129. "Quit Electric Command History, restoring previous window configuration."
  130. (interactive)
  131. (if (boundp 'electric-history-in-progress)
  132. (progn (message "")
  133. (throw 'electric-history-quit nil))))
  134. (provide 'echistory)
  135. ;;; echistory.el ends here