chistory.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ;;; chistory.el --- list command history
  2. ;; Copyright (C) 1985, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: K. Shane Hartman
  4. ;; Maintainer: FSF
  5. ;; Keywords: convenience
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This really has nothing to do with list-command-history per se, but
  19. ;; its a nice alternative to C-x ESC ESC (repeat-complex-command) and
  20. ;; functions as a lister if given no pattern. It's not important
  21. ;; enough to warrant a file of its own.
  22. ;;; Code:
  23. (defgroup chistory nil
  24. "List command history."
  25. :group 'keyboard)
  26. ;;;###autoload
  27. (defun repeat-matching-complex-command (&optional pattern)
  28. "Edit and re-evaluate complex command with name matching PATTERN.
  29. Matching occurrences are displayed, most recent first, until you select
  30. a form for evaluation. If PATTERN is empty (or nil), every form in the
  31. command history is offered. The form is placed in the minibuffer for
  32. editing and the result is evaluated."
  33. (interactive "sRedo Command (regexp): ")
  34. (if pattern
  35. (if (string-match "[^ \t]" pattern)
  36. (setq pattern (substring pattern (match-beginning 0)))
  37. (setq pattern nil)))
  38. (let ((history command-history)
  39. (temp)
  40. (what))
  41. (while (and history (not what))
  42. (setq temp (car history))
  43. (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
  44. (y-or-n-p (format "Redo %S? " temp)))
  45. (setq what (car history))
  46. (setq history (cdr history))))
  47. (if (not what)
  48. (error "Command history exhausted")
  49. ;; Try to remove any useless command history element for this command.
  50. (if (eq (car (car command-history)) 'repeat-matching-complex-command)
  51. (setq command-history (cdr command-history)))
  52. (edit-and-eval-command "Redo: " what))))
  53. (defcustom default-command-history-filter-garbage
  54. '(command-history-mode
  55. list-command-history
  56. electric-command-history)
  57. "A list of symbols to be ignored by `default-command-history-filter'.
  58. If that function is given a list whose car is an element of this list,
  59. then it will return non-nil (indicating the list should be discarded from
  60. the history).
  61. Initially, all commands related to the command history are discarded."
  62. :type '(repeat symbol)
  63. :group 'chistory)
  64. (defvar list-command-history-filter 'default-command-history-filter
  65. "Predicate to test which commands should be excluded from the history listing.
  66. If non-nil, should be the name of a function of one argument.
  67. It is passed each element of the command history when
  68. \\[list-command-history] is called. If the filter returns non-nil for
  69. some element, that element is excluded from the history listing. The
  70. default filter removes commands associated with the command-history.")
  71. (defun default-command-history-filter (frob)
  72. "Filter commands matching `default-command-history-filter-garbage' list
  73. from the command history."
  74. (or (not (consp frob))
  75. (memq (car frob) default-command-history-filter-garbage)))
  76. (defcustom list-command-history-max 32
  77. "If non-nil, maximum length of the listing produced by `list-command-history'."
  78. :type '(choice integer (const nil))
  79. :group 'chistory)
  80. ;;;###autoload
  81. (defun list-command-history ()
  82. "List history of commands typed to minibuffer.
  83. The number of commands listed is controlled by `list-command-history-max'.
  84. Calls value of `list-command-history-filter' (if non-nil) on each history
  85. element to judge if that element should be excluded from the list.
  86. The buffer is left in Command History mode."
  87. (interactive)
  88. (with-output-to-temp-buffer
  89. "*Command History*"
  90. (let ((history command-history)
  91. (buffer-read-only nil)
  92. (count (or list-command-history-max -1)))
  93. (while (and (/= count 0) history)
  94. (if (and (bound-and-true-p list-command-history-filter)
  95. (funcall list-command-history-filter (car history)))
  96. nil
  97. (setq count (1- count))
  98. (prin1 (car history))
  99. (terpri))
  100. (setq history (cdr history))))
  101. (with-current-buffer "*Command History*"
  102. (goto-char (point-min))
  103. (if (eobp)
  104. (error "No command history")
  105. (command-history-mode)))))
  106. (defvar command-history-map
  107. (let ((map (make-sparse-keymap)))
  108. (set-keymap-parent map lisp-mode-shared-map)
  109. (suppress-keymap map)
  110. (define-key map "x" 'command-history-repeat)
  111. (define-key map "\n" 'next-line)
  112. (define-key map "\r" 'next-line)
  113. (define-key map "\177" 'previous-line)
  114. map)
  115. "Keymap for `command-history-mode'.")
  116. (defun command-history-mode ()
  117. "Major mode for listing and repeating recent commands.
  118. Keybindings:
  119. \\{command-history-map}"
  120. (interactive)
  121. (Command-history-setup)
  122. (setq major-mode 'command-history-mode)
  123. (setq mode-name "Command History")
  124. (use-local-map command-history-map)
  125. (run-mode-hooks 'command-history-mode-hook))
  126. (defun Command-history-setup ()
  127. (kill-all-local-variables)
  128. (use-local-map command-history-map)
  129. (lisp-mode-variables nil)
  130. (set-syntax-table emacs-lisp-mode-syntax-table)
  131. (setq buffer-read-only t))
  132. (defcustom command-history-hook nil
  133. "If non-nil, its value is called on entry to `command-history-mode'."
  134. :type 'hook
  135. :group 'chistory)
  136. (defun command-history-repeat ()
  137. "Repeat the command shown on the current line.
  138. The buffer for that command is the previous current buffer."
  139. (interactive)
  140. (save-excursion
  141. (eval (prog1
  142. (save-excursion
  143. (beginning-of-line)
  144. (read (current-buffer)))
  145. (set-buffer
  146. (car (cdr (buffer-list))))))))
  147. ;;;###autoload
  148. (defun command-history ()
  149. "Examine commands from `command-history' in a buffer.
  150. The number of commands listed is controlled by `list-command-history-max'.
  151. The command history is filtered by `list-command-history-filter' if non-nil.
  152. Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
  153. Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
  154. and digits provide prefix arguments. Tab does not indent.
  155. \\{command-history-map}
  156. This command always recompiles the Command History listing
  157. and runs the normal hook `command-history-hook'."
  158. (interactive)
  159. (list-command-history)
  160. (pop-to-buffer "*Command History*")
  161. (run-hooks 'command-history-hook))
  162. (provide 'chistory)
  163. ;;; chistory.el ends here