tabulated-list.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. ;;; tabulated-list.el --- generic major mode for tabulated lists -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. ;; Author: Chong Yidong <cyd@stupidchicken.com>
  4. ;; Keywords: extensions, lisp
  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, or (at your option)
  9. ;; 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. ;; This file defines `tabulated-list-mode', a generic major mode for displaying
  18. ;; lists of tabulated data, intended for other major modes to inherit from. It
  19. ;; provides several utility routines, e.g. for pretty-printing lines of
  20. ;; tabulated data to fit into the appropriate columns.
  21. ;; For usage information, see the documentation of `tabulated-list-mode'.
  22. ;; This package originated from Tom Tromey's Package Menu mode, extended and
  23. ;; generalized to be used by other modes.
  24. ;;; Code:
  25. (defvar tabulated-list-format nil
  26. "The format of the current Tabulated List mode buffer.
  27. This should be a vector of elements (NAME WIDTH SORT), where:
  28. - NAME is a string describing the column.
  29. - WIDTH is the width to reserve for the column.
  30. For the final element, its numerical value is ignored.
  31. - SORT specifies how to sort entries by this column.
  32. If nil, this column cannot be used for sorting.
  33. If t, sort by comparing the string value printed in the column.
  34. Otherwise, it should be a predicate function suitable for
  35. `sort', accepting arguments with the same form as the elements
  36. of `tabulated-list-entries'.")
  37. (make-variable-buffer-local 'tabulated-list-format)
  38. (defvar tabulated-list-entries nil
  39. "Entries displayed in the current Tabulated List buffer.
  40. This should be either a function, or a list.
  41. If a list, each element has the form (ID [DESC1 ... DESCN]),
  42. where:
  43. - ID is nil, or a Lisp object uniquely identifying this entry,
  44. which is used to keep the cursor on the \"same\" entry when
  45. rearranging the list. Comparison is done with `equal'.
  46. - Each DESC is a column descriptor, one for each column
  47. specified in `tabulated-list-format'. A descriptor is either
  48. a string, which is printed as-is, or a list (LABEL . PROPS),
  49. which means to use `insert-text-button' to insert a text
  50. button with label LABEL and button properties PROPS.
  51. The string, or button label, must not contain any newline.
  52. If `tabulated-list-entries' is a function, it is called with no
  53. arguments and must return a list of the above form.")
  54. (make-variable-buffer-local 'tabulated-list-entries)
  55. (defvar tabulated-list-padding 0
  56. "Number of characters preceding each Tabulated List mode entry.
  57. By default, lines are padded with spaces, but you can use the
  58. function `tabulated-list-put-tag' to change this.")
  59. (make-variable-buffer-local 'tabulated-list-padding)
  60. (defvar tabulated-list-revert-hook nil
  61. "Hook run before reverting a Tabulated List buffer.
  62. This is commonly used to recompute `tabulated-list-entries'.")
  63. (defvar tabulated-list-printer 'tabulated-list-print-entry
  64. "Function for inserting a Tabulated List entry at point.
  65. It is called with two arguments, ID and COLS. ID is a Lisp
  66. object identifying the entry, and COLS is a vector of column
  67. descriptors, as documented in `tabulated-list-entries'.")
  68. (make-variable-buffer-local 'tabulated-list-printer)
  69. (defvar tabulated-list-sort-key nil
  70. "Sort key for the current Tabulated List mode buffer.
  71. If nil, no additional sorting is performed.
  72. Otherwise, this should be a cons cell (NAME . FLIP).
  73. NAME is a string matching one of the column names in
  74. `tabulated-list-format' (the corresponding SORT entry in
  75. `tabulated-list-format' then specifies how to sort). FLIP, if
  76. non-nil, means to invert the resulting sort.")
  77. (make-variable-buffer-local 'tabulated-list-sort-key)
  78. (defun tabulated-list-get-id (&optional pos)
  79. "Obtain the entry ID of the Tabulated List mode entry at POS.
  80. This is an ID object from `tabulated-list-entries', or nil.
  81. POS, if omitted or nil, defaults to point."
  82. (get-text-property (or pos (point)) 'tabulated-list-id))
  83. (defun tabulated-list-put-tag (tag &optional advance)
  84. "Put TAG in the padding area of the current line.
  85. TAG should be a string, with length <= `tabulated-list-padding'.
  86. If ADVANCE is non-nil, move forward by one line afterwards."
  87. (unless (stringp tag)
  88. (error "Invalid argument to `tabulated-list-put-tag'"))
  89. (unless (> tabulated-list-padding 0)
  90. (error "Unable to tag the current line"))
  91. (save-excursion
  92. (beginning-of-line)
  93. (when (get-text-property (point) 'tabulated-list-id)
  94. (let ((beg (point))
  95. (inhibit-read-only t))
  96. (forward-char tabulated-list-padding)
  97. (insert-and-inherit
  98. (if (<= (length tag) tabulated-list-padding)
  99. (concat tag
  100. (make-string (- tabulated-list-padding (length tag))
  101. ?\s))
  102. (substring tag 0 tabulated-list-padding)))
  103. (delete-region beg (+ beg tabulated-list-padding)))))
  104. (if advance
  105. (forward-line)))
  106. (defvar tabulated-list-mode-map
  107. (let ((map (copy-keymap special-mode-map)))
  108. (set-keymap-parent map button-buffer-map)
  109. (define-key map "n" 'next-line)
  110. (define-key map "p" 'previous-line)
  111. (define-key map [follow-link] 'mouse-face)
  112. (define-key map [mouse-2] 'mouse-select-window)
  113. map)
  114. "Local keymap for `tabulated-list-mode' buffers.")
  115. (defvar tabulated-list-sort-button-map
  116. (let ((map (make-sparse-keymap)))
  117. (define-key map [header-line mouse-1] 'tabulated-list-col-sort)
  118. (define-key map [header-line mouse-2] 'tabulated-list-col-sort)
  119. (define-key map [follow-link] 'mouse-face)
  120. map)
  121. "Local keymap for `tabulated-list-mode' sort buttons.")
  122. (defvar tabulated-list-glyphless-char-display
  123. (let ((table (make-char-table 'glyphless-char-display nil)))
  124. (set-char-table-parent table glyphless-char-display)
  125. ;; Some text terminals can't display the Unicode arrows; be safe.
  126. (aset table 9650 (cons nil "^"))
  127. (aset table 9660 (cons nil "v"))
  128. table)
  129. "The `glyphless-char-display' table in Tabulated List buffers.")
  130. (defun tabulated-list-init-header ()
  131. "Set up header line for the Tabulated List buffer."
  132. (let ((x tabulated-list-padding)
  133. (button-props `(help-echo "Click to sort by column"
  134. mouse-face highlight
  135. keymap ,tabulated-list-sort-button-map))
  136. (cols nil))
  137. (if (> tabulated-list-padding 0)
  138. (push (propertize " " 'display `(space :align-to ,x)) cols))
  139. (dotimes (n (length tabulated-list-format))
  140. (let* ((col (aref tabulated-list-format n))
  141. (width (nth 1 col))
  142. (label (car col)))
  143. (setq x (+ x 1 width))
  144. (and (<= tabulated-list-padding 0)
  145. (= n 0)
  146. (setq label (concat " " label)))
  147. (push
  148. (cond
  149. ;; An unsortable column
  150. ((not (nth 2 col)) label)
  151. ;; The selected sort column
  152. ((equal (car col) (car tabulated-list-sort-key))
  153. (apply 'propertize
  154. (concat label
  155. (cond
  156. ((> (+ 2 (length label)) width)
  157. "")
  158. ((cdr tabulated-list-sort-key)
  159. " ▲")
  160. (t " ▼")))
  161. 'face 'bold
  162. 'tabulated-list-column-name (car col)
  163. button-props))
  164. ;; Unselected sortable column.
  165. (t (apply 'propertize label
  166. 'tabulated-list-column-name (car col)
  167. button-props)))
  168. cols))
  169. (push (propertize " "
  170. 'display (list 'space :align-to x)
  171. 'face 'fixed-pitch)
  172. cols))
  173. (setq header-line-format (mapconcat 'identity (nreverse cols) ""))))
  174. (defun tabulated-list-revert (&rest ignored)
  175. "The `revert-buffer-function' for `tabulated-list-mode'.
  176. It runs `tabulated-list-revert-hook', then calls `tabulated-list-print'."
  177. (interactive)
  178. (unless (derived-mode-p 'tabulated-list-mode)
  179. (error "The current buffer is not in Tabulated List mode"))
  180. (run-hooks 'tabulated-list-revert-hook)
  181. (tabulated-list-print t))
  182. (defun tabulated-list-print (&optional remember-pos)
  183. "Populate the current Tabulated List mode buffer.
  184. This sorts the `tabulated-list-entries' list if sorting is
  185. specified by `tabulated-list-sort-key'. It then erases the
  186. buffer and inserts the entries with `tabulated-list-printer'.
  187. Optional argument REMEMBER-POS, if non-nil, means to move point
  188. to the entry with the same ID element as the current line."
  189. (let ((inhibit-read-only t)
  190. (entries (if (functionp tabulated-list-entries)
  191. (funcall tabulated-list-entries)
  192. tabulated-list-entries))
  193. entry-id saved-pt saved-col)
  194. (and remember-pos
  195. (setq entry-id (tabulated-list-get-id))
  196. (setq saved-col (current-column)))
  197. (erase-buffer)
  198. ;; Sort the buffers, if necessary.
  199. (when tabulated-list-sort-key
  200. (let ((sort-column (car tabulated-list-sort-key))
  201. (len (length tabulated-list-format))
  202. (n 0)
  203. sorter)
  204. ;; Which column is to be sorted?
  205. (while (and (< n len)
  206. (not (equal (car (aref tabulated-list-format n))
  207. sort-column)))
  208. (setq n (1+ n)))
  209. (when (< n len)
  210. (setq sorter (nth 2 (aref tabulated-list-format n)))
  211. (when (eq sorter t)
  212. (setq sorter ; Default sorter checks column N:
  213. (lambda (A B)
  214. (setq A (aref (cadr A) n))
  215. (setq B (aref (cadr B) n))
  216. (string< (if (stringp A) A (car A))
  217. (if (stringp B) B (car B))))))
  218. (setq entries (sort entries sorter))
  219. (if (cdr tabulated-list-sort-key)
  220. (setq entries (nreverse entries)))
  221. (unless (functionp tabulated-list-entries)
  222. (setq tabulated-list-entries entries)))))
  223. ;; Print the resulting list.
  224. (dolist (elt entries)
  225. (and entry-id
  226. (equal entry-id (car elt))
  227. (setq saved-pt (point)))
  228. (apply tabulated-list-printer elt))
  229. (set-buffer-modified-p nil)
  230. ;; If REMEMBER-POS was specified, move to the "old" location.
  231. (if saved-pt
  232. (progn (goto-char saved-pt)
  233. (move-to-column saved-col)
  234. (recenter))
  235. (goto-char (point-min)))))
  236. (defun tabulated-list-print-entry (id cols)
  237. "Insert a Tabulated List entry at point.
  238. This is the default `tabulated-list-printer' function. ID is a
  239. Lisp object identifying the entry to print, and COLS is a vector
  240. of column descriptors."
  241. (let ((beg (point))
  242. (x (max tabulated-list-padding 0))
  243. (len (length tabulated-list-format)))
  244. (if (> tabulated-list-padding 0)
  245. (insert (make-string x ?\s)))
  246. (dotimes (n len)
  247. (let* ((format (aref tabulated-list-format n))
  248. (desc (aref cols n))
  249. (width (nth 1 format))
  250. (label (if (stringp desc) desc (car desc)))
  251. (help-echo (concat (car format) ": " label)))
  252. ;; Truncate labels if necessary (except last column).
  253. (and (< (1+ n) len)
  254. (> (string-width label) width)
  255. (setq label (truncate-string-to-width label width nil nil t)))
  256. (setq label (bidi-string-mark-left-to-right label))
  257. (if (stringp desc)
  258. (insert (propertize label 'help-echo help-echo))
  259. (apply 'insert-text-button label (cdr desc)))
  260. (setq x (+ x 1 width)))
  261. ;; No need to append any spaces if this is the last column.
  262. (if (< (1+ n) len)
  263. (indent-to x 1)))
  264. (insert ?\n)
  265. (put-text-property beg (point) 'tabulated-list-id id)))
  266. (defun tabulated-list-col-sort (&optional e)
  267. "Sort Tabulated List entries by the column of the mouse click E."
  268. (interactive "e")
  269. (let* ((pos (event-start e))
  270. (obj (posn-object pos))
  271. (name (get-text-property (if obj (cdr obj) (posn-point pos))
  272. 'tabulated-list-column-name
  273. (car obj))))
  274. (with-current-buffer (window-buffer (posn-window pos))
  275. (when (derived-mode-p 'tabulated-list-mode)
  276. ;; Flip the sort order on a second click.
  277. (if (equal name (car tabulated-list-sort-key))
  278. (setcdr tabulated-list-sort-key
  279. (not (cdr tabulated-list-sort-key)))
  280. (setq tabulated-list-sort-key (cons name nil)))
  281. (tabulated-list-init-header)
  282. (tabulated-list-print t)))))
  283. ;;; The mode definition:
  284. ;;;###autoload
  285. (define-derived-mode tabulated-list-mode special-mode "Tabulated"
  286. "Generic major mode for browsing a list of items.
  287. This mode is usually not used directly; instead, other major
  288. modes are derived from it, using `define-derived-mode'.
  289. In this major mode, the buffer is divided into multiple columns,
  290. which are labeled using the header line. Each non-empty line
  291. belongs to one \"entry\", and the entries can be sorted according
  292. to their column values.
  293. An inheriting mode should usually do the following in their body:
  294. - Set `tabulated-list-format', specifying the column format.
  295. - Set `tabulated-list-revert-hook', if the buffer contents need
  296. to be specially recomputed prior to `revert-buffer'.
  297. - Maybe set a `tabulated-list-entries' function (see below).
  298. - Maybe set `tabulated-list-printer' (see below).
  299. - Maybe set `tabulated-list-padding'.
  300. - Call `tabulated-list-init-header' to initialize `header-line-format'
  301. according to `tabulated-list-format'.
  302. An inheriting mode is usually accompanied by a \"list-FOO\"
  303. command (e.g. `list-packages', `list-processes'). This command
  304. creates or switches to a buffer and enables the major mode in
  305. that buffer. If `tabulated-list-entries' is not a function, the
  306. command should initialize it to a list of entries for displaying.
  307. Finally, it should call `tabulated-list-print'.
  308. `tabulated-list-print' calls the printer function specified by
  309. `tabulated-list-printer', once for each entry. The default
  310. printer is `tabulated-list-print-entry', but a mode that keeps
  311. data in an ewoc may instead specify a printer function (e.g., one
  312. that calls `ewoc-enter-last'), with `tabulated-list-print-entry'
  313. as the ewoc pretty-printer."
  314. (setq truncate-lines t)
  315. (setq buffer-read-only t)
  316. (set (make-local-variable 'revert-buffer-function)
  317. 'tabulated-list-revert)
  318. (set (make-local-variable 'glyphless-char-display)
  319. tabulated-list-glyphless-char-display))
  320. (put 'tabulated-list-mode 'mode-class 'special)
  321. (provide 'tabulated-list)
  322. ;; Local Variables:
  323. ;; coding: utf-8
  324. ;; End:
  325. ;;; tabulated-list.el ends here