ebuff-menu.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ;;; ebuff-menu.el --- electric-buffer-list mode
  2. ;; Copyright (C) 1985-1986, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Richard Mlynarik <mly@ai.mit.edu>
  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. ;; Who says one can't have typeout windows in GNU Emacs? The entry
  19. ;; point, `electric-buffer-list' works like ^r select buffer from the
  20. ;; ITS Emacs lunar or tmacs libraries.
  21. ;;; Code:
  22. (require 'electric)
  23. ;; this depends on the format of list-buffers (from src/buffer.c) and
  24. ;; on stuff in lisp/buff-menu.el
  25. (defvar electric-buffer-menu-mode-map
  26. (let ((map (make-keymap)))
  27. (fillarray (car (cdr map)) 'Electric-buffer-menu-undefined)
  28. (define-key map "\e" nil)
  29. (define-key map "\C-z" 'suspend-frame)
  30. (define-key map "v" 'Electric-buffer-menu-mode-view-buffer)
  31. (define-key map (char-to-string help-char) 'Helper-help)
  32. (define-key map "?" 'Helper-describe-bindings)
  33. (define-key map "\C-c" nil)
  34. (define-key map "\C-c\C-c" 'Electric-buffer-menu-quit)
  35. (define-key map "\C-]" 'Electric-buffer-menu-quit)
  36. (define-key map "q" 'Electric-buffer-menu-quit)
  37. (define-key map " " 'Electric-buffer-menu-select)
  38. (define-key map "\C-m" 'Electric-buffer-menu-select)
  39. (define-key map "\C-l" 'recenter)
  40. (define-key map "s" 'Buffer-menu-save)
  41. (define-key map "d" 'Buffer-menu-delete)
  42. (define-key map "k" 'Buffer-menu-delete)
  43. (define-key map "\C-d" 'Buffer-menu-delete-backwards)
  44. ;; (define-key map "\C-k" 'Buffer-menu-delete)
  45. (define-key map "\177" 'Buffer-menu-backup-unmark)
  46. (define-key map "~" 'Buffer-menu-not-modified)
  47. (define-key map "u" 'Buffer-menu-unmark)
  48. (let ((i ?0))
  49. (while (<= i ?9)
  50. (define-key map (char-to-string i) 'digit-argument)
  51. (define-key map (concat "\e" (char-to-string i)) 'digit-argument)
  52. (setq i (1+ i))))
  53. (define-key map "-" 'negative-argument)
  54. (define-key map "\e-" 'negative-argument)
  55. (define-key map "m" 'Buffer-menu-mark)
  56. (define-key map "\C-u" 'universal-argument)
  57. (define-key map "\C-p" 'previous-line)
  58. (define-key map "\C-n" 'next-line)
  59. (define-key map "p" 'previous-line)
  60. (define-key map "n" 'next-line)
  61. (define-key map "\C-v" 'scroll-up-command)
  62. (define-key map "\ev" 'scroll-down-command)
  63. (define-key map ">" 'scroll-right)
  64. (define-key map "<" 'scroll-left)
  65. (define-key map "\e\C-v" 'scroll-other-window)
  66. (define-key map "\e>" 'end-of-buffer)
  67. (define-key map "\e<" 'beginning-of-buffer)
  68. (define-key map "\e\e" nil)
  69. (define-key map "\e\e\e" 'Electric-buffer-menu-quit)
  70. ;; This binding prevents the "escape => ESC" function-key-map mapping from
  71. ;; kicking in!
  72. ;; (define-key map [escape escape escape] 'Electric-buffer-menu-quit)
  73. (define-key map [mouse-2] 'Electric-buffer-menu-mouse-select)
  74. map))
  75. (defvar electric-buffer-menu-mode-hook nil
  76. "Normal hook run by `electric-buffer-list'.")
  77. ;;;###autoload
  78. (defun electric-buffer-list (arg)
  79. "Pop up a buffer describing the set of Emacs buffers.
  80. Vaguely like ITS lunar select buffer; combining typeoutoid buffer
  81. listing with menuoid buffer selection.
  82. If the very next character typed is a space then the buffer list
  83. window disappears. Otherwise, one may move around in the buffer list
  84. window, marking buffers to be selected, saved or deleted.
  85. To exit and select a new buffer, type a space when the cursor is on
  86. the appropriate line of the buffer-list window. Other commands are
  87. much like those of `Buffer-menu-mode'.
  88. Run hooks in `electric-buffer-menu-mode-hook' on entry.
  89. \\{electric-buffer-menu-mode-map}"
  90. (interactive "P")
  91. (let (select buffer)
  92. (save-window-excursion
  93. (setq buffer (list-buffers-noselect arg))
  94. (Electric-pop-up-window buffer)
  95. (unwind-protect
  96. (progn
  97. (set-buffer buffer)
  98. (Electric-buffer-menu-mode)
  99. (electric-buffer-update-highlight)
  100. (setq select
  101. (catch 'electric-buffer-menu-select
  102. (message "<<< Press Return to bury the buffer list >>>")
  103. (if (eq (setq unread-command-events (list (read-event)))
  104. ?\s)
  105. (progn (setq unread-command-events nil)
  106. (throw 'electric-buffer-menu-select nil)))
  107. (let ((start-point (point))
  108. (first (progn (goto-char (point-min))
  109. (unless Buffer-menu-use-header-line
  110. (forward-line 2))
  111. (point)))
  112. (last (progn (goto-char (point-max))
  113. (forward-line -1)
  114. (point)))
  115. (goal-column 0))
  116. ;; Use start-point if it is meaningful.
  117. (goto-char (if (or (< start-point first)
  118. (> start-point last))
  119. first
  120. start-point))
  121. (Electric-command-loop 'electric-buffer-menu-select
  122. nil
  123. t
  124. 'electric-buffer-menu-looper
  125. (cons first last))))))
  126. (set-buffer buffer)
  127. (Buffer-menu-mode)
  128. (bury-buffer) ;Get rid of window, if dedicated.
  129. (message "")))
  130. (if select
  131. (progn (set-buffer buffer)
  132. (let ((opoint (point-marker)))
  133. (Buffer-menu-execute)
  134. (goto-char (point-min))
  135. (if (prog1 (search-forward "\n>" nil t)
  136. (goto-char opoint) (set-marker opoint nil))
  137. (Buffer-menu-select)
  138. (switch-to-buffer (Buffer-menu-buffer t))))))))
  139. (defun electric-buffer-menu-looper (state condition)
  140. (cond ((and condition
  141. (not (memq (car condition) '(buffer-read-only
  142. end-of-buffer
  143. beginning-of-buffer))))
  144. (signal (car condition) (cdr condition)))
  145. ((< (point) (car state))
  146. (goto-char (point-min))
  147. (unless Buffer-menu-use-header-line
  148. (forward-line 2)))
  149. ((> (point) (cdr state))
  150. (goto-char (point-max))
  151. (forward-line -1)
  152. (if (pos-visible-in-window-p (point-max))
  153. (recenter -1))))
  154. (electric-buffer-update-highlight))
  155. (defvar Helper-return-blurb)
  156. (put 'Electric-buffer-menu-mode 'mode-class 'special)
  157. (defun Electric-buffer-menu-mode ()
  158. "Major mode for editing a list of buffers.
  159. Each line describes one of the buffers in Emacs.
  160. Letters do not insert themselves; instead, they are commands.
  161. \\<electric-buffer-menu-mode-map>
  162. \\[keyboard-quit] or \\[Electric-buffer-menu-quit] -- exit buffer menu, returning to previous window and buffer
  163. configuration. If the very first character typed is a space, it
  164. also has this effect.
  165. \\[Electric-buffer-menu-select] -- select buffer of line point is on.
  166. Also show buffers marked with m in other windows,
  167. deletes buffers marked with \"D\", and saves those marked with \"S\".
  168. \\[Buffer-menu-mark] -- mark buffer to be displayed.
  169. \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
  170. \\[Buffer-menu-save] -- mark that buffer to be saved.
  171. \\[Buffer-menu-delete] or \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted.
  172. \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
  173. \\[Electric-buffer-menu-mode-view-buffer] -- view buffer, returning when done.
  174. \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
  175. \\{electric-buffer-menu-mode-map}
  176. Entry to this mode via command `electric-buffer-list' calls the value of
  177. `electric-buffer-menu-mode-hook'."
  178. (let ((saved header-line-format))
  179. (kill-all-local-variables)
  180. (setq header-line-format saved))
  181. (use-local-map electric-buffer-menu-mode-map)
  182. (setq mode-name "Electric Buffer Menu")
  183. (setq mode-line-buffer-identification "Electric Buffer List")
  184. (make-local-variable 'Helper-return-blurb)
  185. (setq Helper-return-blurb "return to buffer editing")
  186. (setq truncate-lines t)
  187. (setq buffer-read-only t)
  188. (setq major-mode 'Electric-buffer-menu-mode)
  189. (goto-char (point-min))
  190. (if (search-forward "\n." nil t) (forward-char -1))
  191. (run-mode-hooks 'electric-buffer-menu-mode-hook))
  192. ;; generally the same as Buffer-menu-mode-map
  193. ;; (except we don't indirect to global-map)
  194. (put 'Electric-buffer-menu-undefined 'suppress-keymap t)
  195. (defun Electric-buffer-menu-exit ()
  196. (interactive)
  197. (setq unread-command-events (listify-key-sequence (this-command-keys)))
  198. ;; for robustness
  199. (condition-case ()
  200. (throw 'electric-buffer-menu-select nil)
  201. (error (Buffer-menu-mode)
  202. (other-buffer))))
  203. (defun Electric-buffer-menu-select ()
  204. "Leave Electric Buffer Menu, selecting buffers and executing changes.
  205. Save buffers marked \"S\". Delete buffers marked \"K\".
  206. Select buffer at point and display buffers marked \">\" in other windows."
  207. (interactive)
  208. (throw 'electric-buffer-menu-select (point)))
  209. (defun Electric-buffer-menu-mouse-select (event)
  210. (interactive "e")
  211. (select-window (posn-window (event-end event)))
  212. (set-buffer (window-buffer (selected-window)))
  213. (goto-char (posn-point (event-end event)))
  214. (throw 'electric-buffer-menu-select (point)))
  215. (defun Electric-buffer-menu-quit ()
  216. "Leave Electric Buffer Menu, restoring previous window configuration.
  217. Skip execution of select, save, and delete commands."
  218. (interactive)
  219. (throw 'electric-buffer-menu-select nil))
  220. (defun Electric-buffer-menu-undefined ()
  221. (interactive)
  222. (ding)
  223. (message "%s"
  224. (if (and (eq (key-binding "\C-c\C-c") 'Electric-buffer-menu-quit)
  225. (eq (key-binding " ") 'Electric-buffer-menu-select)
  226. (eq (key-binding (char-to-string help-char)) 'Helper-help)
  227. (eq (key-binding "?") 'Helper-describe-bindings))
  228. (substitute-command-keys "Type C-c C-c to exit, Space to select, \\[Helper-help] for help, ? for commands")
  229. (substitute-command-keys "\
  230. Type \\[Electric-buffer-menu-quit] to exit, \
  231. \\[Electric-buffer-menu-select] to select, \
  232. \\[Helper-help] for help, \\[Helper-describe-bindings] for commands.")))
  233. (sit-for 4))
  234. (defun Electric-buffer-menu-mode-view-buffer ()
  235. "View buffer on current line in Electric Buffer Menu.
  236. Return to Electric Buffer Menu when done."
  237. (interactive)
  238. (let ((bufnam (Buffer-menu-buffer nil)))
  239. (if bufnam
  240. (view-buffer bufnam)
  241. (ding)
  242. (message "Buffer %s does not exist!" bufnam)
  243. (sit-for 4))))
  244. (defvar electric-buffer-overlay nil)
  245. (defun electric-buffer-update-highlight ()
  246. (when (eq major-mode 'Electric-buffer-menu-mode)
  247. ;; Make sure we have an overlay to use.
  248. (or electric-buffer-overlay
  249. (progn
  250. (make-local-variable 'electric-buffer-overlay)
  251. (setq electric-buffer-overlay (make-overlay (point) (point)))))
  252. (move-overlay electric-buffer-overlay
  253. (line-beginning-position)
  254. (line-end-position))
  255. (overlay-put electric-buffer-overlay 'face 'highlight)))
  256. (provide 'ebuff-menu)
  257. ;;; ebuff-menu.el ends here