Make header line in some modes be sensitive to display-line-numbers
[emacs.git] / lisp / emacs-lisp / tabulated-list.el
blob8ff5cdf18e8a9d9fd959a888a518c919abd2d7fe
1 ;;; tabulated-list.el --- generic major mode for tabulated lists -*- lexical-binding: t -*-
3 ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 ;; Author: Chong Yidong <cyd@stupidchicken.com>
6 ;; Keywords: extensions, lisp
7 ;; Version: 1.0
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This file defines Tabulated List mode, a generic major mode for
27 ;; displaying lists of tabulated data, intended for other major modes
28 ;; to inherit from. It provides several utility routines, e.g. for
29 ;; pretty-printing lines of tabulated data to fit into the appropriate
30 ;; columns.
32 ;; For usage information, see the documentation of `tabulated-list-mode'.
34 ;; This package originated from Tom Tromey's Package Menu mode,
35 ;; extended and generalized to be used by other modes.
37 ;;; Code:
39 ;; The reason `tabulated-list-format' and other variables are
40 ;; permanent-local is to make it convenient to switch to a different
41 ;; major mode, switch back, and have the original Tabulated List data
42 ;; still valid. See, for example, ebuff-menu.el.
44 (defvar-local tabulated-list-format nil
45 "The format of the current Tabulated List mode buffer.
46 This should be a vector of elements (NAME WIDTH SORT . PROPS),
47 where:
48 - NAME is a string describing the column.
49 This is the label for the column in the header line.
50 Different columns must have non-`equal' names.
51 - WIDTH is the width to reserve for the column.
52 For the final element, its numerical value is ignored.
53 - SORT specifies how to sort entries by this column.
54 If nil, this column cannot be used for sorting.
55 If t, sort by comparing the string value printed in the column.
56 Otherwise, it should be a predicate function suitable for
57 `sort', accepting arguments with the same form as the elements
58 of `tabulated-list-entries'.
59 - PROPS is a plist of additional column properties.
60 Currently supported properties are:
61 - `:right-align': If non-nil, the column should be right-aligned.
62 - `:pad-right': Number of additional padding spaces to the
63 right of the column (defaults to 1 if omitted).")
64 (put 'tabulated-list-format 'permanent-local t)
66 (defvar-local tabulated-list-use-header-line t
67 "Whether the Tabulated List buffer should use a header line.")
69 (defvar-local tabulated-list-entries nil
70 "Entries displayed in the current Tabulated List buffer.
71 This should be either a function, or a list.
72 If a list, each element has the form (ID [DESC1 ... DESCN]),
73 where:
74 - ID is nil, or a Lisp object uniquely identifying this entry,
75 which is used to keep the cursor on the \"same\" entry when
76 rearranging the list. Comparison is done with `equal'.
78 - Each DESC is a column descriptor, one for each column
79 specified in `tabulated-list-format'. A descriptor is either
80 a string, which is printed as-is, or a list (LABEL . PROPS),
81 which means to use `insert-text-button' to insert a text
82 button with label LABEL and button properties PROPS.
83 The string, or button label, must not contain any newline.
85 If `tabulated-list-entries' is a function, it is called with no
86 arguments and must return a list of the above form.")
87 (put 'tabulated-list-entries 'permanent-local t)
89 (defvar-local tabulated-list-padding 0
90 "Number of characters preceding each Tabulated List mode entry.
91 By default, lines are padded with spaces, but you can use the
92 function `tabulated-list-put-tag' to change this.")
93 (put 'tabulated-list-padding 'permanent-local t)
95 (defvar tabulated-list-revert-hook nil
96 "Hook run before reverting a Tabulated List buffer.
97 This is commonly used to recompute `tabulated-list-entries'.")
99 (defvar-local tabulated-list-printer 'tabulated-list-print-entry
100 "Function for inserting a Tabulated List entry at point.
101 It is called with two arguments, ID and COLS. ID is a Lisp
102 object identifying the entry, and COLS is a vector of column
103 descriptors, as documented in `tabulated-list-entries'.")
105 (defvar tabulated-list--near-rows)
107 (defvar-local tabulated-list-sort-key nil
108 "Sort key for the current Tabulated List mode buffer.
109 If nil, no additional sorting is performed.
110 Otherwise, this should be a cons cell (NAME . FLIP).
111 NAME is a string matching one of the column names in
112 `tabulated-list-format' (the corresponding SORT entry in
113 `tabulated-list-format' then specifies how to sort). FLIP, if
114 non-nil, means to invert the resulting sort.")
115 (put 'tabulated-list-sort-key 'permanent-local t)
117 (defsubst tabulated-list-get-id (&optional pos)
118 "Return the entry ID of the Tabulated List entry at POS.
119 The value is an ID object from `tabulated-list-entries', or nil.
120 POS, if omitted or nil, defaults to point."
121 (get-text-property (or pos (point)) 'tabulated-list-id))
123 (defsubst tabulated-list-get-entry (&optional pos)
124 "Return the Tabulated List entry at POS.
125 The value is a vector of column descriptors, or nil if there is
126 no entry at POS. POS, if omitted or nil, defaults to point."
127 (get-text-property (or pos (point)) 'tabulated-list-entry))
129 (defun tabulated-list-put-tag (tag &optional advance)
130 "Put TAG in the padding area of the current line.
131 TAG should be a string, with length <= `tabulated-list-padding'.
132 If ADVANCE is non-nil, move forward by one line afterwards."
133 (unless (stringp tag)
134 (error "Invalid argument to `tabulated-list-put-tag'"))
135 (unless (> tabulated-list-padding 0)
136 (error "Unable to tag the current line"))
137 (save-excursion
138 (beginning-of-line)
139 (when (tabulated-list-get-entry)
140 (let ((beg (point))
141 (inhibit-read-only t))
142 (forward-char tabulated-list-padding)
143 (insert-and-inherit
144 (let ((width (string-width tag)))
145 (if (<= width tabulated-list-padding)
146 (concat tag
147 (make-string (- tabulated-list-padding width) ?\s))
148 (truncate-string-to-width tag tabulated-list-padding))))
149 (delete-region beg (+ beg tabulated-list-padding)))))
150 (if advance
151 (forward-line)))
153 (defvar tabulated-list-mode-map
154 (let ((map (copy-keymap special-mode-map)))
155 (set-keymap-parent map button-buffer-map)
156 (define-key map "n" 'next-line)
157 (define-key map "p" 'previous-line)
158 (define-key map "S" 'tabulated-list-sort)
159 (define-key map [follow-link] 'mouse-face)
160 (define-key map [mouse-2] 'mouse-select-window)
161 map)
162 "Local keymap for `tabulated-list-mode' buffers.")
164 (defvar tabulated-list-sort-button-map
165 (let ((map (make-sparse-keymap)))
166 (define-key map [header-line mouse-1] 'tabulated-list-col-sort)
167 (define-key map [header-line mouse-2] 'tabulated-list-col-sort)
168 (define-key map [mouse-1] 'tabulated-list-col-sort)
169 (define-key map [mouse-2] 'tabulated-list-col-sort)
170 (define-key map "\C-m" 'tabulated-list-sort)
171 (define-key map [follow-link] 'mouse-face)
172 map)
173 "Local keymap for `tabulated-list-mode' sort buttons.")
175 (defvar tabulated-list-glyphless-char-display
176 (let ((table (make-char-table 'glyphless-char-display nil)))
177 (set-char-table-parent table glyphless-char-display)
178 ;; Some text terminals can't display the Unicode arrows; be safe.
179 (aset table 9650 (cons nil "^"))
180 (aset table 9660 (cons nil "v"))
181 table)
182 "The `glyphless-char-display' table in Tabulated List buffers.")
184 (defvar tabulated-list--header-string nil
185 "Holds the header if `tabulated-list-use-header-line' is nil.
186 Populated by `tabulated-list-init-header'.")
187 (defvar tabulated-list--header-overlay nil)
189 (defun tabulated-list-init-header ()
190 "Set up header line for the Tabulated List buffer."
191 ;; FIXME: Should share code with tabulated-list-print-col!
192 (let ((x (max tabulated-list-padding 0))
193 (button-props `(help-echo "Click to sort by column"
194 mouse-face highlight
195 keymap ,tabulated-list-sort-button-map))
196 (cols nil))
197 (if display-line-numbers
198 (setq x (+ x (line-number-display-width) 2)))
199 (push (propertize " " 'display `(space :align-to ,x)) cols)
200 (dotimes (n (length tabulated-list-format))
201 (let* ((col (aref tabulated-list-format n))
202 (label (nth 0 col))
203 (width (nth 1 col))
204 (props (nthcdr 3 col))
205 (pad-right (or (plist-get props :pad-right) 1))
206 (right-align (plist-get props :right-align))
207 (next-x (+ x pad-right width)))
208 (push
209 (cond
210 ;; An unsortable column
211 ((not (nth 2 col))
212 (propertize label 'tabulated-list-column-name label))
213 ;; The selected sort column
214 ((equal (car col) (car tabulated-list-sort-key))
215 (apply 'propertize
216 (concat label
217 (cond
218 ((> (+ 2 (length label)) width) "")
219 ((cdr tabulated-list-sort-key) " â–²")
220 (t " â–¼")))
221 'face 'bold
222 'tabulated-list-column-name label
223 button-props))
224 ;; Unselected sortable column.
225 (t (apply 'propertize label
226 'tabulated-list-column-name label
227 button-props)))
228 cols)
229 (when right-align
230 (let ((shift (- width (string-width (car cols)))))
231 (when (> shift 0)
232 (setq cols
233 (cons (car cols)
234 (cons (propertize (make-string shift ?\s)
235 'display
236 `(space :align-to ,(+ x shift)))
237 (cdr cols))))
238 (setq x (+ x shift)))))
239 (if (>= pad-right 0)
240 (push (propertize " "
241 'display `(space :align-to ,next-x)
242 'face 'fixed-pitch)
243 cols))
244 (setq x next-x)))
245 (setq cols (apply 'concat (nreverse cols)))
246 (if tabulated-list-use-header-line
247 (setq header-line-format cols)
248 (setq header-line-format nil)
249 (setq-local tabulated-list--header-string cols))))
251 (defun tabulated-list-print-fake-header ()
252 "Insert a fake Tabulated List \"header line\" at the start of the buffer.
253 Do nothing if `tabulated-list--header-string' is nil."
254 (when tabulated-list--header-string
255 (goto-char (point-min))
256 (let ((inhibit-read-only t))
257 (insert tabulated-list--header-string "\n")
258 (if tabulated-list--header-overlay
259 (move-overlay tabulated-list--header-overlay (point-min) (point))
260 (setq-local tabulated-list--header-overlay
261 (make-overlay (point-min) (point))))
262 (overlay-put tabulated-list--header-overlay 'face 'underline))))
264 (defsubst tabulated-list-header-overlay-p (&optional pos)
265 "Return non-nil if there is a fake header.
266 Optional arg POS is a buffer position where to look for a fake header;
267 defaults to `point-min'."
268 (overlays-at (or pos (point-min))))
270 (defun tabulated-list-revert (&rest ignored)
271 "The `revert-buffer-function' for `tabulated-list-mode'.
272 It runs `tabulated-list-revert-hook', then calls `tabulated-list-print'."
273 (interactive)
274 (unless (derived-mode-p 'tabulated-list-mode)
275 (error "The current buffer is not in Tabulated List mode"))
276 (run-hooks 'tabulated-list-revert-hook)
277 (tabulated-list-print t))
279 (defun tabulated-list--column-number (name)
280 (let ((len (length tabulated-list-format))
281 (n 0)
282 found)
283 (while (and (< n len) (null found))
284 (if (equal (car (aref tabulated-list-format n)) name)
285 (setq found n))
286 (setq n (1+ n)))
287 (or found
288 (error "No column named %s" name))))
290 (defun tabulated-list--get-sorter ()
291 "Return a sorting predicate for the current tabulated-list.
292 Return nil if `tabulated-list-sort-key' specifies an unsortable
293 column. Negate the predicate that would be returned if
294 `tabulated-list-sort-key' has a non-nil cdr."
295 (when (and tabulated-list-sort-key
296 (car tabulated-list-sort-key))
297 (let* ((sort-column (car tabulated-list-sort-key))
298 (n (tabulated-list--column-number sort-column))
299 (sorter (nth 2 (aref tabulated-list-format n))))
300 (when (eq sorter t); Default sorter checks column N:
301 (setq sorter (lambda (A B)
302 (let ((a (aref (cadr A) n))
303 (b (aref (cadr B) n)))
304 (string< (if (stringp a) a (car a))
305 (if (stringp b) b (car b)))))))
306 ;; Reversed order.
307 (if (cdr tabulated-list-sort-key)
308 (lambda (a b) (not (funcall sorter a b)))
309 sorter))))
311 (defsubst tabulated-list--col-local-max-widths (col)
312 "Return maximum entry widths at column COL around current row.
313 Check the current row, the previous one and the next row."
314 (apply #'max (mapcar (lambda (x)
315 (let ((nt (elt x col)))
316 (string-width (if (stringp nt) nt (car nt)))))
317 tabulated-list--near-rows)))
319 (defun tabulated-list-print (&optional remember-pos update)
320 "Populate the current Tabulated List mode buffer.
321 This sorts the `tabulated-list-entries' list if sorting is
322 specified by `tabulated-list-sort-key'. It then erases the
323 buffer and inserts the entries with `tabulated-list-printer'.
325 Optional argument REMEMBER-POS, if non-nil, means to move point
326 to the entry with the same ID element as the current line and
327 recenter window line accordingly.
329 Non-nil UPDATE argument means to use an alternative printing
330 method which is faster if most entries haven't changed since the
331 last print. The only difference in outcome is that tags will not
332 be removed from entries that haven't changed (see
333 `tabulated-list-put-tag'). Don't use this immediately after
334 changing `tabulated-list-sort-key'."
335 (let ((inhibit-read-only t)
336 (entries (if (functionp tabulated-list-entries)
337 (funcall tabulated-list-entries)
338 tabulated-list-entries))
339 (sorter (tabulated-list--get-sorter))
340 entry-id saved-pt saved-col window-line)
341 (and remember-pos
342 (setq entry-id (tabulated-list-get-id))
343 (setq saved-col (current-column))
344 (when (eq (window-buffer) (current-buffer))
345 (setq window-line
346 (count-screen-lines (window-start) (point)))))
347 ;; Sort the entries, if necessary.
348 (when sorter
349 (setq entries (sort entries sorter)))
350 (unless (functionp tabulated-list-entries)
351 (setq tabulated-list-entries entries))
352 ;; Without a sorter, we have no way to just update.
353 (when (and update (not sorter))
354 (setq update nil))
355 (if update (goto-char (point-min))
356 ;; Redo the buffer, unless we're just updating.
357 (erase-buffer)
358 (unless tabulated-list-use-header-line
359 (tabulated-list-print-fake-header)))
360 ;; Finally, print the resulting list.
361 (while entries
362 (let* ((elt (car entries))
363 (tabulated-list--near-rows
364 (list
365 (or (tabulated-list-get-entry (point-at-bol 0)) (cadr elt))
366 (cadr elt)
367 (or (cadr (cadr entries)) (cadr elt))))
368 (id (car elt)))
369 (and entry-id
370 (equal entry-id id)
371 (setq entry-id nil
372 saved-pt (point)))
373 ;; If the buffer this empty, simply print each elt.
374 (if (or (not update) (eobp))
375 (apply tabulated-list-printer elt)
376 (while (let ((local-id (tabulated-list-get-id)))
377 ;; If we find id, then nothing to update.
378 (cond ((equal id local-id)
379 (forward-line 1)
380 nil)
381 ;; If this entry sorts after id (or it's the
382 ;; end), then just insert id and move on.
383 ((or (not local-id)
384 (funcall sorter elt
385 ;; FIXME: Might be faster if
386 ;; don't construct this list.
387 (list local-id (tabulated-list-get-entry))))
388 (apply tabulated-list-printer elt)
389 nil)
390 ;; We find an entry that sorts before id,
391 ;; it needs to be deleted.
392 (t t)))
393 (let ((old (point)))
394 (forward-line 1)
395 (delete-region old (point))))))
396 (setq entries (cdr entries)))
397 (set-buffer-modified-p nil)
398 ;; If REMEMBER-POS was specified, move to the "old" location.
399 (if saved-pt
400 (progn (goto-char saved-pt)
401 (move-to-column saved-col)
402 (when window-line
403 (recenter window-line)))
404 (goto-char (point-min)))))
406 (defun tabulated-list-print-entry (id cols)
407 "Insert a Tabulated List entry at point.
408 This is the default `tabulated-list-printer' function. ID is a
409 Lisp object identifying the entry to print, and COLS is a vector
410 of column descriptors."
411 (let ((beg (point))
412 (x (max tabulated-list-padding 0))
413 (ncols (length tabulated-list-format))
414 (inhibit-read-only t))
415 (if display-line-numbers
416 (setq x (+ x (line-number-display-width) 2)))
417 (if (> tabulated-list-padding 0)
418 (insert (make-string x ?\s)))
419 (let ((tabulated-list--near-rows ; Bind it if not bound yet (Bug#25506).
420 (or (bound-and-true-p tabulated-list--near-rows)
421 (list (or (tabulated-list-get-entry (point-at-bol 0))
422 cols)
423 cols))))
424 (dotimes (n ncols)
425 (setq x (tabulated-list-print-col n (aref cols n) x))))
426 (insert ?\n)
427 ;; Ever so slightly faster than calling `put-text-property' twice.
428 (add-text-properties
429 beg (point)
430 `(tabulated-list-id ,id tabulated-list-entry ,cols))))
432 (defun tabulated-list-print-col (n col-desc x)
433 "Insert a specified Tabulated List entry at point.
434 N is the column number, COL-DESC is a column descriptor (see
435 `tabulated-list-entries'), and X is the column number at point.
436 Return the column number after insertion."
437 (let* ((format (aref tabulated-list-format n))
438 (name (nth 0 format))
439 (width (nth 1 format))
440 (props (nthcdr 3 format))
441 (pad-right (or (plist-get props :pad-right) 1))
442 (right-align (plist-get props :right-align))
443 (label (if (stringp col-desc) col-desc (car col-desc)))
444 (label-width (string-width label))
445 (help-echo (concat (car format) ": " label))
446 (opoint (point))
447 (not-last-col (< (1+ n) (length tabulated-list-format)))
448 available-space)
449 (when not-last-col
450 (let* ((next-col-format (aref tabulated-list-format (1+ n)))
451 (next-col-right-align (plist-get (nthcdr 3 next-col-format)
452 :right-align))
453 (next-col-width (nth 1 next-col-format)))
454 (setq available-space
455 (if (and (not right-align)
456 next-col-right-align)
458 (+ width next-col-width)
459 (min next-col-width
460 (tabulated-list--col-local-max-widths (1+ n))))
461 width))))
462 ;; Truncate labels if necessary (except last column).
463 ;; Don't truncate to `width' if the next column is align-right
464 ;; and has some space left, truncate to `available-space' instead.
465 (when (and not-last-col
466 (> label-width available-space)
467 (setq label (truncate-string-to-width
468 label available-space nil nil t)
469 label-width available-space)))
470 (setq label (bidi-string-mark-left-to-right label))
471 (when (and right-align (> width label-width))
472 (let ((shift (- width label-width)))
473 (insert (propertize (make-string shift ?\s)
474 'display `(space :align-to ,(+ x shift))))
475 (setq width (- width shift))
476 (setq x (+ x shift))))
477 (if (stringp col-desc)
478 (insert (if (get-text-property 0 'help-echo label)
479 label
480 (propertize label 'help-echo help-echo)))
481 (apply 'insert-text-button label (cdr col-desc)))
482 (let ((next-x (+ x pad-right width)))
483 ;; No need to append any spaces if this is the last column.
484 (when not-last-col
485 (when (> pad-right 0) (insert (make-string pad-right ?\s)))
486 (insert (propertize
487 (make-string (- width (min width label-width)) ?\s)
488 'display `(space :align-to ,next-x))))
489 (put-text-property opoint (point) 'tabulated-list-column-name name)
490 next-x)))
492 (defun tabulated-list-delete-entry ()
493 "Delete the Tabulated List entry at point.
494 Return a list (ID COLS), where ID is the ID of the deleted entry
495 and COLS is a vector of its column descriptors. Move point to
496 the beginning of the deleted entry. Return nil if there is no
497 entry at point.
499 This function only changes the buffer contents; it does not alter
500 `tabulated-list-entries'."
501 ;; Assume that each entry occupies one line.
502 (let* ((id (tabulated-list-get-id))
503 (cols (tabulated-list-get-entry))
504 (inhibit-read-only t))
505 (when cols
506 (delete-region (line-beginning-position) (1+ (line-end-position)))
507 (list id cols))))
509 (defun tabulated-list-set-col (col desc &optional change-entry-data)
510 "Change the Tabulated List entry at point, setting COL to DESC.
511 COL is the column number to change, or the name of the column to change.
512 DESC is the new column descriptor, which is inserted via
513 `tabulated-list-print-col'.
515 If CHANGE-ENTRY-DATA is non-nil, modify the underlying entry data
516 by setting the appropriate slot of the vector originally used to
517 print this entry. If `tabulated-list-entries' has a list value,
518 this is the vector stored within it."
519 (let* ((opoint (point))
520 (eol (line-end-position))
521 (pos (line-beginning-position))
522 (id (tabulated-list-get-id pos))
523 (entry (tabulated-list-get-entry pos))
524 (prop 'tabulated-list-column-name)
525 (inhibit-read-only t)
526 name)
527 (cond ((numberp col)
528 (setq name (car (aref tabulated-list-format col))))
529 ((stringp col)
530 (setq name col
531 col (tabulated-list--column-number col)))
533 (error "Invalid column %s" col)))
534 (unless entry
535 (error "No Tabulated List entry at position %s" opoint))
536 (unless (equal (get-text-property pos prop) name)
537 (while (and (setq pos
538 (next-single-property-change pos prop nil eol))
539 (< pos eol)
540 (not (equal (get-text-property pos prop) name)))))
541 (when (< pos eol)
542 (delete-region pos (next-single-property-change pos prop nil eol))
543 (goto-char pos)
544 (let ((tabulated-list--near-rows
545 (list
546 (tabulated-list-get-entry (point-at-bol 0))
547 entry
548 (or (tabulated-list-get-entry (point-at-bol 2)) entry))))
549 (tabulated-list-print-col col desc (current-column)))
550 (if change-entry-data
551 (aset entry col desc))
552 (put-text-property pos (point) 'tabulated-list-id id)
553 (put-text-property pos (point) 'tabulated-list-entry entry)
554 (goto-char opoint))))
556 (defun tabulated-list-col-sort (&optional e)
557 "Sort Tabulated List entries by the column of the mouse click E."
558 (interactive "e")
559 (let* ((pos (event-start e))
560 (obj (posn-object pos)))
561 (with-current-buffer (window-buffer (posn-window pos))
562 (tabulated-list--sort-by-column-name
563 (get-text-property (if obj (cdr obj) (posn-point pos))
564 'tabulated-list-column-name
565 (car obj))))))
567 (defun tabulated-list-sort (&optional n)
568 "Sort Tabulated List entries by the column at point.
569 With a numeric prefix argument N, sort the Nth column."
570 (interactive "P")
571 (let ((name (if n
572 (car (aref tabulated-list-format n))
573 (get-text-property (point)
574 'tabulated-list-column-name))))
575 (if (nth 2 (assoc name (append tabulated-list-format nil)))
576 (tabulated-list--sort-by-column-name name)
577 (user-error "Cannot sort by %s" name))))
579 (defun tabulated-list--sort-by-column-name (name)
580 (when (and name (derived-mode-p 'tabulated-list-mode))
581 ;; Flip the sort order on a second click.
582 (if (equal name (car tabulated-list-sort-key))
583 (setcdr tabulated-list-sort-key
584 (not (cdr tabulated-list-sort-key)))
585 (setq tabulated-list-sort-key (cons name nil)))
586 (tabulated-list-init-header)
587 (tabulated-list-print t)))
589 ;;; The mode definition:
591 (define-derived-mode tabulated-list-mode special-mode "Tabulated"
592 "Generic major mode for browsing a list of items.
593 This mode is usually not used directly; instead, other major
594 modes are derived from it, using `define-derived-mode'.
596 In this major mode, the buffer is divided into multiple columns,
597 which are labeled using the header line. Each non-empty line
598 belongs to one \"entry\", and the entries can be sorted according
599 to their column values.
601 An inheriting mode should usually do the following in their body:
603 - Set `tabulated-list-format', specifying the column format.
604 - Set `tabulated-list-revert-hook', if the buffer contents need
605 to be specially recomputed prior to `revert-buffer'.
606 - Maybe set a `tabulated-list-entries' function (see below).
607 - Maybe set `tabulated-list-printer' (see below).
608 - Maybe set `tabulated-list-padding'.
609 - Call `tabulated-list-init-header' to initialize `header-line-format'
610 according to `tabulated-list-format'.
612 An inheriting mode is usually accompanied by a \"list-FOO\"
613 command (e.g. `list-packages', `list-processes'). This command
614 creates or switches to a buffer and enables the major mode in
615 that buffer. If `tabulated-list-entries' is not a function, the
616 command should initialize it to a list of entries for displaying.
617 Finally, it should call `tabulated-list-print'.
619 `tabulated-list-print' calls the printer function specified by
620 `tabulated-list-printer', once for each entry. The default
621 printer is `tabulated-list-print-entry', but a mode that keeps
622 data in an ewoc may instead specify a printer function (e.g., one
623 that calls `ewoc-enter-last'), with `tabulated-list-print-entry'
624 as the ewoc pretty-printer."
625 (setq-local truncate-lines t)
626 (setq-local buffer-undo-list t)
627 (setq-local revert-buffer-function #'tabulated-list-revert)
628 (setq-local glyphless-char-display tabulated-list-glyphless-char-display)
629 ;; Avoid messing up the entries' display just because the first
630 ;; column of the first entry happens to begin with a R2L letter.
631 (setq bidi-paragraph-direction 'left-to-right))
633 (put 'tabulated-list-mode 'mode-class 'special)
635 (provide 'tabulated-list)
637 ;;; tabulated-list.el ends here