chistory.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;; chistory -- List command history
  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. (provide 'chistory)
  19. ;; This really has nothing to do with list-command-history per se, but
  20. ;; its a nice alternative to C-x ESC (repeat-complex-command) and
  21. ;; functions as a lister if given no pattern. It's not important
  22. ;; enough to warrant a file of its own.
  23. (defun repeat-matching-complex-command (&optional pattern)
  24. "Edit and re-evaluate complex command with name matching PATTERN.
  25. Matching occurrences are displayed, most recent first, until you
  26. select a form for evaluation. If PATTERN is empty (or nil), every form
  27. in the command history is offered. The form is placed in the minibuffer
  28. for editing and the result is evaluated."
  29. (interactive "sRedo Command (regexp): ")
  30. (if pattern
  31. (if (equal (setq pattern
  32. (substring pattern
  33. (or (string-match "[ \t]*[^ \t]" pattern)
  34. (length pattern))))
  35. "")
  36. (setq pattern nil)))
  37. (let ((history command-history)
  38. (temp)
  39. (what))
  40. (while (and history (not what))
  41. (setq temp (car history))
  42. (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
  43. (y-or-n-p (format "Redo %s? " (setq temp (prin1-to-string temp)))))
  44. (setq what (car history))
  45. (setq history (cdr history))))
  46. (if (not what)
  47. (error "Command history exhausted.")
  48. (edit-and-eval-command "Redo: " what))))
  49. (defvar default-command-history-filter-garbage
  50. '(command-history-mode
  51. list-command-history
  52. electric-command-history)
  53. "*A list of symbols. If default-list-command-history-filter is
  54. given a list whose car is an element of this list, then it will return
  55. non-nil (indicating the list should be discarded from the history).
  56. Initially, all commands related to the command history are discarded.")
  57. (defvar list-command-history-filter 'default-command-history-filter
  58. "If non-nil, should be the name of a function of one argument.
  59. It is passed each element of the command history when
  60. \\[list-command-history] is called. If the filter returns non-nil for
  61. some element, that element is excluded from the history listing. The
  62. default filter removes commands associated with the command-history.")
  63. (defun default-command-history-filter (frob)
  64. "Filter commands matching default-command-history-filter-garbage list
  65. from the command history."
  66. (or (not (consp frob))
  67. (memq (car frob) default-command-history-filter-garbage)))
  68. (defvar list-command-history-max 32
  69. "*If non-nil, should be a positive number which specifies the maximum
  70. length of the Command History listing produced by list-command-history.")
  71. (defun list-command-history ()
  72. "List history of commands typed to minibuffer.
  73. The number of commands listed is controlled by list-command-history-max.
  74. Calls value of list-command-history-filter (if non-nil) on each history
  75. element to judge if that element should be excluded from the list.
  76. The buffer is left in Command History mode."
  77. (interactive)
  78. (with-output-to-temp-buffer
  79. "*Command History*"
  80. (let ((history command-history)
  81. (buffer-read-only nil)
  82. (count (or list-command-history-max -1)))
  83. (while (and (/= count 0) history)
  84. (if (and (boundp 'list-command-history-filter)
  85. list-command-history-filter
  86. (funcall list-command-history-filter (car history)))
  87. nil
  88. (setq count (1- count))
  89. (prin1 (car history))
  90. (terpri))
  91. (setq history (cdr history))))
  92. (goto-char (point-min))
  93. (if (eobp)
  94. (error "No command history.")
  95. (Command-history-setup))))
  96. (defun Command-history-setup (&optional majormode modename keymap)
  97. (set-buffer "*Command History*")
  98. (use-local-map (or keymap command-history-map))
  99. (lisp-mode-variables)
  100. (set-syntax-table lisp-mode-syntax-table)
  101. (setq buffer-read-only t)
  102. (use-local-map (or keymap command-history-map))
  103. (setq major-mode (or majormode 'command-history-mode))
  104. (setq mode-name (or modename "Command History")))
  105. (defvar command-history-hook nil
  106. "If non-nil, its value is called on entry to command-history-mode.")
  107. (defvar command-history-map nil)
  108. (if command-history-map
  109. nil
  110. (setq command-history-map (make-keymap))
  111. (lisp-mode-commands command-history-map)
  112. (suppress-keymap command-history-map)
  113. (define-key command-history-map "\n" 'next-line)
  114. (define-key command-history-map "\r" 'next-line)
  115. (define-key command-history-map "\177" 'previous-line))
  116. (defun command-history-mode ()
  117. "Major mode for examining commands from command-history.
  118. The number of commands listed is controlled by list-command-history-max.
  119. The command history is filtered by list-command-history-filter if non-nil.
  120. Like Emacs-Lisp Mode except that characters do not insert themselves and
  121. Digits provide prefix arguments. Tab does not indent.
  122. \\{command-history-map}
  123. Calls the value of command-history-hook if that is non-nil
  124. The Command History listing is recomputed each time this mode is
  125. invoked."
  126. (interactive)
  127. (list-command-history)
  128. (pop-to-buffer "*Command History*")
  129. (run-hooks 'command-history-hook))