echistory.el 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;; Electric Command History Mode
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; Principal author K. Shane Hartman
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is distributed in the hope that it will be useful,
  6. ;; but WITHOUT ANY WARRANTY. No author or distributor
  7. ;; accepts responsibility to anyone for the consequences of using it
  8. ;; or for whether it serves any particular purpose or works at all,
  9. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  10. ;; License for full details.
  11. ;; Everyone is granted permission to copy, modify and redistribute
  12. ;; GNU Emacs, but only under the conditions described in the
  13. ;; GNU Emacs General Public License. A copy of this license is
  14. ;; supposed to have been given to you along with GNU Emacs so you
  15. ;; can know your rights and responsibilities. It should be in a
  16. ;; file named COPYING. Among other things, the copyright notice
  17. ;; and this notice must be preserved on all copies.
  18. (require 'electric) ; command loop
  19. (require 'chistory) ; history lister
  20. (defun Electric-command-history-redo-expression (&optional noconfirm)
  21. "Edit current history line in minibuffer and execute result.
  22. With prefix argument NOCONFIRM, execute current line as is without editing."
  23. (interactive "P")
  24. (let (todo)
  25. (save-excursion
  26. (set-buffer "*Command History*")
  27. (beginning-of-line)
  28. (setq todo (read (current-buffer)))
  29. (if (boundp 'electric-history-in-progress)
  30. (if todo (throw 'electric-history-quit (list noconfirm todo)))))))
  31. (defvar electric-history-map ())
  32. (if electric-history-map
  33. ()
  34. (setq electric-history-map (make-keymap))
  35. (fillarray electric-history-map 'Electric-history-undefined)
  36. (define-key electric-history-map "\e" (make-keymap))
  37. (fillarray (lookup-key electric-history-map "\e") 'Electric-history-undefined)
  38. (define-key electric-history-map "\C-u" 'universal-argument)
  39. (define-key electric-history-map " " 'Electric-command-history-redo-expression)
  40. (define-key electric-history-map "!" 'Electric-command-history-redo-expression)
  41. (define-key electric-history-map "\e\C-x" 'eval-sexp)
  42. (define-key electric-history-map "\e\C-d" 'down-list)
  43. (define-key electric-history-map "\e\C-u" 'backward-up-list)
  44. (define-key electric-history-map "\e\C-b" 'backward-sexp)
  45. (define-key electric-history-map "\e\C-f" 'forward-sexp)
  46. (define-key electric-history-map "\e\C-a" 'beginning-of-defun)
  47. (define-key electric-history-map "\e\C-e" 'end-of-defun)
  48. (define-key electric-history-map "\e\C-n" 'forward-list)
  49. (define-key electric-history-map "\e\C-p" 'backward-list)
  50. (define-key electric-history-map "q" 'Electric-history-quit)
  51. (define-key electric-history-map "\C-c" nil)
  52. (define-key electric-history-map "\C-c\C-c" 'Electric-history-quit)
  53. (define-key electric-history-map "\C-]" 'Electric-history-quit)
  54. (define-key electric-history-map "\C-z" 'suspend-emacs)
  55. (define-key electric-history-map "\C-h" 'Helper-help)
  56. (define-key electric-history-map "?" 'Helper-describe-bindings)
  57. (define-key electric-history-map "\e>" 'end-of-buffer)
  58. (define-key electric-history-map "\e<" 'beginning-of-buffer)
  59. (define-key electric-history-map "\n" 'next-line)
  60. (define-key electric-history-map "\r" 'next-line)
  61. (define-key electric-history-map "\177" 'previous-line)
  62. (define-key electric-history-map "\C-n" 'next-line)
  63. (define-key electric-history-map "\C-p" 'previous-line)
  64. (define-key electric-history-map "\ev" 'scroll-down)
  65. (define-key electric-history-map "\C-v" 'scroll-up)
  66. (define-key electric-history-map "\C-l" 'recenter)
  67. (define-key electric-history-map "\e\C-v" 'scroll-other-window))
  68. (defvar electric-command-history-hook nil
  69. "If non-nil, its value is called by electric-command-history.")
  70. (defun electric-command-history ()
  71. "Major mode for examining and redoing commands from command-history.
  72. The number of command listed is controlled by list-command-history-max.
  73. The command history is filtered by list-command-history-filter if non-nil.
  74. Combines typeout Command History list window with menu like selection
  75. of an expression from the history for re-evaluation in the *original* buffer.
  76. The history displayed is filtered by list-command-history-filter if non-nil.
  77. This pops up a window with the Command History listing. If the very
  78. next character typed is Space, the listing is killed and the previous
  79. window configuration is restored. Otherwise, you can browse in the
  80. Command History with Return moving down and Delete moving up, possibly
  81. selecting an expression to be redone with Space or quitting with `Q'.
  82. Like Emacs-Lisp Mode except that characters do not insert themselves and
  83. Tab and linefeed do not indent. Instead these commands are provided:
  84. Space or ! edit then evaluate current line in history inside
  85. the ORIGINAL buffer which invoked this mode.
  86. The previous window configuration is restored
  87. unless the invoked command changes it.
  88. C-c C-c, C-], Q Quit and restore previous window configuration.
  89. LFD, RET Move to the next line in the history.
  90. DEL Move to the previous line in the history.
  91. ? Provides a complete list of commands.
  92. Calls the value of electric-command-history-hook if that is non-nil
  93. The Command History listing is recomputed each time this mode is invoked."
  94. (interactive)
  95. (let ((electric-history-in-progress t)
  96. (old-buffer (current-buffer))
  97. (todo))
  98. (unwind-protect
  99. (setq todo
  100. (catch 'electric-history-quit
  101. (save-window-excursion
  102. (save-window-excursion
  103. (list-command-history)
  104. (set-buffer "*Command History*")
  105. (Command-history-setup 'electric-command-history
  106. "Electric History"
  107. electric-history-map))
  108. (Electric-pop-up-window "*Command History*")
  109. (run-hooks 'electric-command-history-hook)
  110. (if (eobp)
  111. (progn (ding)
  112. (message "No command history.")
  113. (throw 'electric-history-quit nil))
  114. (let ((Helper-return-blurb "return to History"))
  115. (Electric-command-loop 'electric-history-quit
  116. "->" t))))))
  117. (set-buffer "*Command History*")
  118. (Command-history-setup)
  119. (bury-buffer (current-buffer)))
  120. (if (consp todo)
  121. (progn (set-buffer old-buffer)
  122. (if (car todo)
  123. (apply (car (car (cdr todo))) (cdr (car (cdr todo))))
  124. (edit-and-eval-command "Redo: " (car (cdr todo))))))))
  125. (defun Electric-history-undefined ()
  126. (interactive)
  127. (ding)
  128. (message "Type C-h for help, ? for commands, C-c to quit, Space to execute")
  129. (sit-for 4))
  130. (defun Electric-history-quit ()
  131. "Quit Electric Command History, restoring previous window configuration."
  132. (interactive)
  133. (if (boundp 'electric-history-in-progress)
  134. (progn (message "")
  135. (throw 'electric-history-quit nil))))