diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2011-04-09 11:42:31 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2011-04-09 11:42:31 -0700 |
commit | 762f8d96719ba3e8a0e79d8bb99fe8e119fafb3a (patch) | |
tree | 7b2fe40a89bf327ea8b9ad8265a6b6f27cacd2a7 /lisp/emacs-lisp | |
parent | eb3f1cc8dfe6a96505f1c5f9174b2712998cb52f (diff) | |
parent | 8546720e6f25eb988e8215de6678798053031440 (diff) | |
download | emacs-762f8d96719ba3e8a0e79d8bb99fe8e119fafb3a.tar.gz emacs-762f8d96719ba3e8a0e79d8bb99fe8e119fafb3a.tar.bz2 emacs-762f8d96719ba3e8a0e79d8bb99fe8e119fafb3a.zip |
Merge from mainline.
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r-- | lisp/emacs-lisp/cconv.el | 4 | ||||
-rw-r--r-- | lisp/emacs-lisp/package.el | 460 | ||||
-rw-r--r-- | lisp/emacs-lisp/tabulated-list.el | 355 |
3 files changed, 522 insertions, 297 deletions
diff --git a/lisp/emacs-lisp/cconv.el b/lisp/emacs-lisp/cconv.el index 5cc9ecb4cf7..38584c437b8 100644 --- a/lisp/emacs-lisp/cconv.el +++ b/lisp/emacs-lisp/cconv.el @@ -536,7 +536,9 @@ FORM is the parent form that binds this var." ;; it is often non-trivial for the programmer to avoid such ;; unused vars. (not (intern-soft var)) - (eq ?_ (aref (symbol-name var) 0))) + (eq ?_ (aref (symbol-name var) 0)) + ;; As a special exception, ignore "ignore". + (eq var 'ignored)) (byte-compile-log-warning (format "Unused lexical %s `%S'" varkind var)))) ;; If it's unused, there's no point converting it into a cons-cell, even if diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el index 6aecc3615f3..4ce71b29d70 100644 --- a/lisp/emacs-lisp/package.el +++ b/lisp/emacs-lisp/package.el @@ -173,6 +173,8 @@ ;;; Code: +(require 'tabulated-list) + (defgroup package nil "Manager for Emacs Lisp packages." :group 'applications @@ -1249,12 +1251,10 @@ If optional arg NO-ACTIVATE is non-nil, don't activate packages." ;;;; Package menu mode. (defvar package-menu-mode-map - (let ((map (copy-keymap special-mode-map)) + (let ((map (make-sparse-keymap)) (menu-map (make-sparse-keymap "Package"))) - (set-keymap-parent map button-buffer-map) + (set-keymap-parent map tabulated-list-mode-map) (define-key map "\C-m" 'package-menu-describe-package) - (define-key map "n" 'next-line) - (define-key map "p" 'previous-line) (define-key map "u" 'package-menu-mark-unmark) (define-key map "\177" 'package-menu-backup-unmark) (define-key map "d" 'package-menu-mark-delete) @@ -1264,8 +1264,6 @@ If optional arg NO-ACTIVATE is non-nil, don't activate packages." (define-key map "x" 'package-menu-execute) (define-key map "h" 'package-menu-quick-help) (define-key map "?" 'package-menu-describe-package) - (define-key map [follow-link] 'mouse-face) - (define-key map [mouse-2] 'mouse-select-window) (define-key map [menu-bar package-menu] (cons "Package" menu-map)) (define-key menu-map [mq] '(menu-item "Quit" quit-window @@ -1314,49 +1312,93 @@ If optional arg NO-ACTIVATE is non-nil, don't activate packages." map) "Local keymap for `package-menu-mode' buffers.") -(defvar package-menu-sort-button-map - (let ((map (make-sparse-keymap))) - (define-key map [header-line mouse-1] 'package-menu-sort-by-column) - (define-key map [header-line mouse-2] 'package-menu-sort-by-column) - (define-key map [follow-link] 'mouse-face) - map) - "Local keymap for package menu sort buttons.") - -(put 'package-menu-mode 'mode-class 'special) - -(define-derived-mode package-menu-mode special-mode "Package Menu" +(define-derived-mode package-menu-mode tabulated-list-mode "Package Menu" "Major mode for browsing a list of packages. Letters do not insert themselves; instead, they are commands. \\<package-menu-mode-map> \\{package-menu-mode-map}" - (setq truncate-lines t) - (setq buffer-read-only t) - (set (make-local-variable 'revert-buffer-function) 'package-menu-revert) - (setq header-line-format - (mapconcat - (lambda (pair) - (let ((column (car pair)) - (name (cdr pair))) - (concat - ;; Insert a space that aligns the button properly. - (propertize " " 'display (list 'space :align-to column) - 'face 'fixed-pitch) - ;; Set up the column button. - (propertize name - 'column-name name - 'help-echo "mouse-1: sort by column" - 'mouse-face 'highlight - 'keymap package-menu-sort-button-map)))) - ;; We take a trick from buff-menu and have a dummy leading - ;; space to align the header line with the beginning of the - ;; text. This doesn't really work properly on Emacs 21, but - ;; it is close enough. - '((0 . "") - (2 . "Package") - (20 . "Version") - (32 . "Status") - (43 . "Description")) - ""))) + (setq tabulated-list-format [("Package" 18 package-menu--name-predicate) + ("Version" 12 nil) + ("Status" 10 package-menu--status-predicate) + ("Description" 0 nil)]) + (setq tabulated-list-padding 2) + (setq tabulated-list-sort-key (cons "Status" nil)) + (tabulated-list-init-header)) + +(defmacro package--push (package desc status listname) + "Convenience macro for `package-menu--generate'. +If the alist stored in the symbol LISTNAME lacks an entry for a +package PACKAGE with descriptor DESC, add one. The alist is +keyed with cons cells (PACKAGE . VERSION), where PACKAGE is a +symbol and VERSION is a version list." + `(let* ((version (package-desc-vers ,desc)) + (key (cons ,package version))) + (unless (assoc key ,listname) + (push (list key ,status (package-desc-doc ,desc)) ,listname)))) + +(defun package-menu--generate (&optional remember-pos) + "Populate the Package Menu. +Optional argument REMEMBER-POS, if non-nil, means to move point +to the entry as before." + ;; Construct list of ((PACKAGE . VERSION) STATUS DESCRIPTION). + (let (info-list name builtin) + ;; Installed packages: + (dolist (elt package-alist) + (setq name (car elt)) + (package--push name (cdr elt) + (if (stringp (cadr (assq name package-load-list))) + "held" "installed") + info-list)) + + ;; Built-in packages: + (dolist (elt package--builtins) + (setq name (car elt)) + (unless (eq name 'emacs) ; Hide the `emacs' package. + (package--push name (cdr elt) "built-in" info-list))) + + ;; Available and disabled packages: + (dolist (elt package-archive-contents) + (setq name (car elt)) + (let ((hold (assq name package-load-list))) + (package--push name (cdr elt) + (if (and hold (null (cadr hold))) "disabled" "available") + info-list))) + + ;; Obsolete packages: + (dolist (elt package-obsolete-alist) + (dolist (inner-elt (cdr elt)) + (package--push (car elt) (cdr inner-elt) "obsolete" info-list))) + + ;; Print the result. + (setq tabulated-list-entries (mapcar 'package-menu--print-info info-list)) + (tabulated-list-print remember-pos))) + +(defun package-menu--print-info (pkg) + "Return a package entry suitable for `tabulated-list-entries'. +PKG has the form ((PACKAGE . VERSION) STATUS DOC). +Return (KEY [NAME VERSION STATUS DOC]), where KEY is the +identifier (NAME . VERSION-LIST)." + (let* ((package (caar pkg)) + (version (cdr (car pkg))) + (status (nth 1 pkg)) + (doc (or (nth 2 pkg) "")) + (face (cond + ((string= status "built-in") 'font-lock-builtin-face) + ((string= status "available") 'default) + ((string= status "held") 'font-lock-constant-face) + ((string= status "disabled") 'font-lock-warning-face) + ((string= status "installed") 'font-lock-comment-face) + (t 'font-lock-warning-face)))) ; obsolete. + (list (cons package version) + (vector (list (symbol-name package) + 'face 'link + 'follow-link t + 'package-symbol package + 'action 'package-menu-describe-package) + (propertize (package-version-join version) + 'font-lock-face face) + (propertize status 'font-lock-face face) + (propertize doc 'font-lock-face face))))) (defun package-menu-refresh () "Download the Emacs Lisp package archive. @@ -1366,59 +1408,42 @@ This fetches the contents of each archive specified in (unless (eq major-mode 'package-menu-mode) (error "The current buffer is not a Package Menu")) (package-refresh-contents) - (package--generate-package-list)) + (package-menu--generate t)) -(defun package-menu-revert (&optional arg noconfirm) - "Update the list of packages. -This function is the `revert-buffer-function' for Package Menu -buffers. The arguments are ignored." +(defun package-menu-describe-package (&optional button) + "Describe the current package. +If optional arg BUTTON is non-nil, describe its associated package." (interactive) - (unless (eq major-mode 'package-menu-mode) - (error "The current buffer is not a Package Menu")) - (package--generate-package-list)) - -(defun package-menu-describe-package () - "Describe the package in the current line." - (interactive) - (let ((name (package-menu-get-package))) - (if name - (describe-package (intern name)) - (message "No package on this line")))) - -(defun package-menu-mark-internal (what) - (unless (eobp) - (let ((buffer-read-only nil)) - (beginning-of-line) - (delete-char 1) - (insert what) - (forward-line)))) + (let ((package (if button (button-get button 'package-symbol) + (car (tabulated-list-get-id))))) + (if package + (describe-package package)))) ;; fixme numeric argument (defun package-menu-mark-delete (num) "Mark a package for deletion and move to the next line." (interactive "p") (if (string-equal (package-menu-get-status) "installed") - (package-menu-mark-internal "D") + (tabulated-list-put-tag "D" t) (forward-line))) (defun package-menu-mark-install (num) "Mark a package for installation and move to the next line." (interactive "p") (if (string-equal (package-menu-get-status) "available") - (package-menu-mark-internal "I") + (tabulated-list-put-tag "I" t) (forward-line))) (defun package-menu-mark-unmark (num) "Clear any marks on a package and move to the next line." (interactive "p") - (package-menu-mark-internal " ")) + (tabulated-list-put-tag " " t)) (defun package-menu-backup-unmark () "Back up one line and clear any marks on that package." (interactive) (forward-line -1) - (package-menu-mark-internal " ") - (forward-line -1)) + (tabulated-list-put-tag " ")) (defun package-menu-mark-obsolete-for-deletion () "Mark all obsolete packages for deletion." @@ -1428,7 +1453,7 @@ buffers. The arguments are ignored." (forward-line 2) (while (not (eobp)) (if (looking-at ".*\\s obsolete\\s ") - (package-menu-mark-internal "D") + (tabulated-list-put-tag "D" t) (forward-line 1))))) (defun package-menu-quick-help () @@ -1439,20 +1464,6 @@ buffers. The arguments are ignored." (define-obsolete-function-alias 'package-menu-view-commentary 'package-menu-describe-package "24.1") -;; Return the name of the package on the current line. -(defun package-menu-get-package () - (save-excursion - (beginning-of-line) - (if (looking-at ". \\([^ \t]*\\)") - (match-string-no-properties 1)))) - -;; Return the version of the package on the current line. -(defun package-menu-get-version () - (save-excursion - (beginning-of-line) - (if (looking-at ". [^ \t]*[ \t]*\\([0-9.]*\\)") - (match-string 1)))) - (defun package-menu-get-status () (save-excursion (if (looking-at ". [^ \t]*[ \t]*[^ \t]*[ \t]*\\([^ \t]*\\)") @@ -1464,19 +1475,22 @@ buffers. The arguments are ignored." Packages marked for installation are downloaded and installed; packages marked for deletion are removed." (interactive) - (let (install-list delete-list cmd) + (unless (eq major-mode 'package-menu-mode) + (error "The current buffer is not in Package Menu mode")) + (let (install-list delete-list cmd id) (save-excursion (goto-char (point-min)) (while (not (eobp)) (setq cmd (char-after)) - (cond - ((eq cmd ?\s) t) - ((eq cmd ?D) - (push (cons (package-menu-get-package) - (package-menu-get-version)) - delete-list)) - ((eq cmd ?I) - (push (package-menu-get-package) install-list))) + (unless (eq cmd ?\s) + ;; This is the key (PACKAGE . VERSION-LIST). + (setq id (tabulated-list-get-id)) + (cond ((eq cmd ?D) + (push (cons (symbol-name (car id)) + (package-version-join (cdr id))) + delete-list)) + ((eq cmd ?I) + (push (car id) install-list)))) (forward-line))) ;; Delete packages, prompting if necessary. (when delete-list @@ -1502,217 +1516,71 @@ packages marked for deletion are removed." (format "Install package `%s'? " (car install-list)) (format "Install these %d packages (%s)? " (length install-list) - (mapconcat 'identity install-list ", ")))) - (dolist (elt install-list) - (package-install (intern elt))))) + (mapconcat 'symbol-name install-list ", ")))) + (mapc 'package-install install-list))) ;; If we deleted anything, regenerate `package-alist'. This is done ;; automatically if we installed a package. (and delete-list (null install-list) (package-initialize)) (if (or delete-list install-list) - (package-menu-revert) + (package-menu--generate t) (message "No operations specified.")))) -(defun package-print-package (package version key desc) - (let ((face - (cond ((string= key "built-in") 'font-lock-builtin-face) - ((string= key "available") 'default) - ((string= key "held") 'font-lock-constant-face) - ((string= key "disabled") 'font-lock-warning-face) - ((string= key "installed") 'font-lock-comment-face) - (t ; obsolete, but also the default. - 'font-lock-warning-face)))) - (insert (propertize " " 'font-lock-face face)) - (insert-text-button (symbol-name package) - 'face 'link - 'follow-link t - 'package-symbol package - 'action (lambda (button) - (describe-package - (button-get button 'package-symbol)))) - (indent-to 20 1) - (insert (propertize (package-version-join version) 'font-lock-face face)) - (indent-to 32 1) - (insert (propertize key 'font-lock-face face)) - ;; FIXME: this 'when' is bogus... - (when desc - (indent-to 43 1) - (let ((opoint (point))) - (insert (propertize desc 'font-lock-face face)) - (upcase-region opoint (min (point) (1+ opoint))))) - (insert "\n"))) - -(defun package-list-maybe-add (package version status description result) - (unless (assoc (cons package version) result) - (push (list (cons package version) status description) result)) - result) - -(defvar package-menu-package-list nil - "List of packages to display in the Package Menu buffer. -A value of nil means to display all packages.") - -(defvar package-menu-sort-key nil - "Sort key for the current Package Menu buffer.") - -(defun package--generate-package-list () - "Populate the current Package Menu buffer." - (let ((inhibit-read-only t) - info-list name desc hold builtin) - (erase-buffer) - ;; List installed packages - (dolist (elt package-alist) - (setq name (car elt)) - (when (or (null package-menu-package-list) - (memq name package-menu-package-list)) - (setq desc (cdr elt) - hold (cadr (assq name package-load-list))) - (setq info-list - (package-list-maybe-add - name (package-desc-vers desc) - ;; FIXME: it turns out to be tricky to see if this - ;; package is presently activated. - (if (stringp hold) "held" "installed") - (package-desc-doc desc) - info-list)))) - - ;; List built-in packages - (dolist (elt package--builtins) - (setq name (car elt)) - (when (and (not (eq name 'emacs)) ; Hide the `emacs' package. - (or (null package-menu-package-list) - (memq name package-menu-package-list))) - (setq desc (cdr elt)) - (setq info-list - (package-list-maybe-add - name (package-desc-vers desc) - "built-in" - (package-desc-doc desc) - info-list)))) - - ;; List available and disabled packages - (dolist (elt package-archive-contents) - (setq name (car elt) - desc (cdr elt) - hold (assq name package-load-list)) - (when (or (null package-menu-package-list) - (memq name package-menu-package-list)) - (setq info-list - (package-list-maybe-add name - (package-desc-vers desc) - (if (and hold (null (cadr hold))) - "disabled" - "available") - (package-desc-doc (cdr elt)) - info-list)))) - ;; List obsolete packages - (mapc (lambda (elt) - (mapc (lambda (inner-elt) - (setq info-list - (package-list-maybe-add (car elt) - (package-desc-vers - (cdr inner-elt)) - "obsolete" - (package-desc-doc - (cdr inner-elt)) - info-list))) - (cdr elt))) - package-obsolete-alist) - - (setq info-list - (sort info-list - (cond ((string= package-menu-sort-key "Package") - 'package-menu--name-predicate) - ((string= package-menu-sort-key "Version") - 'package-menu--version-predicate) - ((string= package-menu-sort-key "Description") - 'package-menu--description-predicate) - (t ; By default, sort by package status - 'package-menu--status-predicate)))) - - (dolist (elt info-list) - (package-print-package (car (car elt)) - (cdr (car elt)) - (car (cdr elt)) - (car (cdr (cdr elt))))) - (goto-char (point-min)) - (set-buffer-modified-p nil) - (current-buffer))) - -(defun package-menu--version-predicate (left right) - (let ((vleft (or (cdr (car left)) '(0))) - (vright (or (cdr (car right)) '(0)))) - (if (version-list-= vleft vright) - (package-menu--name-predicate left right) - (version-list-< vleft vright)))) - -(defun package-menu--status-predicate (left right) - (let ((sleft (cadr left)) - (sright (cadr right))) - (cond ((string= sleft sright) - (package-menu--name-predicate left right)) - ((string= sleft "available") t) - ((string= sright "available") nil) - ((string= sleft "installed") t) - ((string= sright "installed") nil) - ((string= sleft "held") t) - ((string= sright "held") nil) - ((string= sleft "built-in") t) - ((string= sright "built-in") nil) - ((string= sleft "obsolete") t) - ((string= sright "obsolete") nil) - (t (string< sleft sright))))) - -(defun package-menu--description-predicate (left right) - (let ((sleft (car (cddr left))) - (sright (car (cddr right)))) - (if (string= sleft sright) - (package-menu--name-predicate left right) - (string< sleft sright)))) - -(defun package-menu--name-predicate (left right) - (string< (symbol-name (caar left)) - (symbol-name (caar right)))) - -(defun package-menu-sort-by-column (&optional e) - "Sort the package menu by the column of the mouse click E." - (interactive "e") - (let* ((pos (event-start e)) - (obj (posn-object pos)) - (col (if obj - (get-text-property (cdr obj) 'column-name (car obj)) - (get-text-property (posn-point pos) 'column-name))) - (buf (window-buffer (posn-window (event-start e))))) - (with-current-buffer buf - (when (eq major-mode 'package-menu-mode) - (setq package-menu-sort-key col) - (package--generate-package-list))))) - -(defun package--list-packages (&optional packages) - "Generate and pop to the *Packages* buffer. -Optional PACKAGES is a list of names of packages (symbols) to -list; the default is to display everything in `package-alist'." - (require 'finder-inf nil t) - (let ((buf (get-buffer-create "*Packages*"))) - (with-current-buffer buf - (package-menu-mode) - (set (make-local-variable 'package-menu-package-list) packages) - (set (make-local-variable 'package-menu-sort-key) nil) - (package--generate-package-list)) - ;; The package menu buffer has keybindings. If the user types - ;; `M-x list-packages', that suggests it should become current. - (switch-to-buffer buf))) +(defun package-menu--version-predicate (A B) + (let ((vA (or (aref (cadr A) 1) '(0))) + (vB (or (aref (cadr B) 1) '(0)))) + (if (version-list-= vA vB) + (package-menu--name-predicate A B) + (version-list-< vA vB)))) + +(defun package-menu--status-predicate (A B) + (let ((sA (aref (cadr A) 2)) + (sB (aref (cadr B) 2))) + (cond ((string= sA sB) + (package-menu--name-predicate A B)) + ((string= sA "available") t) + ((string= sB "available") nil) + ((string= sA "installed") t) + ((string= sB "installed") nil) + ((string= sA "held") t) + ((string= sB "held") nil) + ((string= sA "built-in") t) + ((string= sB "built-in") nil) + ((string= sA "obsolete") t) + ((string= sB "obsolete") nil) + (t (string< sA sB))))) + +(defun package-menu--description-predicate (A B) + (let ((dA (aref (cadr A) 3)) + (dB (aref (cadr B) 3))) + (if (string= dA dB) + (package-menu--name-predicate A B) + (string< dA dB)))) + +(defun package-menu--name-predicate (A B) + (string< (symbol-name (caar A)) + (symbol-name (caar B)))) ;;;###autoload -(defun list-packages () +(defun list-packages (&optional no-fetch) "Display a list of packages. -Fetches the updated list of packages before displaying. +This first fetches the updated list of packages before +displaying, unless a prefix argument NO-FETCH is specified. The list is displayed in a buffer named `*Packages*'." - (interactive) + (interactive "P") + (require 'finder-inf nil t) ;; Initialize the package system if necessary. (unless package--initialized (package-initialize t)) - (package-refresh-contents) - (package--list-packages)) + (unless no-fetch + (package-refresh-contents)) + (let ((buf (get-buffer-create "*Packages*"))) + (with-current-buffer buf + (package-menu-mode) + (package-menu--generate)) + ;; The package menu buffer has keybindings. If the user types + ;; `M-x list-packages', that suggests it should become current. + (switch-to-buffer buf))) ;;;###autoload (defalias 'package-list-packages 'list-packages) @@ -1722,7 +1590,7 @@ The list is displayed in a buffer named `*Packages*'." Does not fetch the updated list of packages before displaying. The list is displayed in a buffer named `*Packages*'." (interactive) - (package--list-packages)) + (list-packages t)) (provide 'package) diff --git a/lisp/emacs-lisp/tabulated-list.el b/lisp/emacs-lisp/tabulated-list.el new file mode 100644 index 00000000000..03ee59dd89c --- /dev/null +++ b/lisp/emacs-lisp/tabulated-list.el @@ -0,0 +1,355 @@ +;;; tabulated-list.el --- generic major mode for tabulated lists. + +;; Copyright (C) 2011 Free Software Foundation, Inc. + +;; Author: Chong Yidong <cyd@stupidchicken.com> +;; Keywords: extensions, lisp + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. + +;;; Commentary: + +;; This file defines `tabulated-list-mode', a generic major mode for displaying +;; lists of tabulated data, intended for other major modes to inherit from. It +;; provides several utility routines, e.g. for pretty-printing lines of +;; tabulated data to fit into the appropriate columns. + +;; For usage information, see the documentation of `tabulated-list-mode'. + +;; This package originated from Tom Tromey's Package Menu mode, extended and +;; generalized to be used by other modes. + +;;; Code: + +(defvar tabulated-list-format nil + "The format of the current Tabulated List mode buffer. +This should be a vector of elements (NAME WIDTH SORT), where: + - NAME is a string describing the column. + - WIDTH is the width to reserve for the column. + For the final element, its numerical value is ignored. + - SORT specifies how to sort entries by this column. + If nil, this column cannot be used for sorting. + If t, sort by comparing the string value printed in the column. + Otherwise, it should be a predicate function suitable for + `sort', accepting arguments with the same form as the elements + of `tabulated-list-entries'.") +(make-variable-buffer-local 'tabulated-list-format) + +(defvar tabulated-list-entries nil + "Entries displayed in the current Tabulated List buffer. +This should be either a function, or a list. +If a list, each element has the form (ID [DESC1 ... DESCN]), +where: + - ID is nil, or a Lisp object uniquely identifying this entry, + which is used to keep the cursor on the \"same\" entry when + rearranging the list. Comparison is done with `equal'. + + - Each DESC is a column descriptor, one for each column + specified in `tabulated-list-format'. A descriptor is either + a string, which is printed as-is, or a list (LABEL . PROPS), + which means to use `insert-text-button' to insert a text + button with label LABEL and button properties PROPS. + The string, or button label, must not contain any newline. + +If `tabulated-list-entries' is a function, it is called with no +arguments and must return a list of the above form.") +(make-variable-buffer-local 'tabulated-list-entries) + +(defvar tabulated-list-padding 0 + "Number of characters preceding each Tabulated List mode entry. +By default, lines are padded with spaces, but you can use the +function `tabulated-list-put-tag' to change this.") +(make-variable-buffer-local 'tabulated-list-padding) + +(defvar tabulated-list-revert-hook nil + "Hook run before reverting a Tabulated List buffer. +This is commonly used to recompute `tabulated-list-entries'.") + +(defvar tabulated-list-printer 'tabulated-list-print-entry + "Function for inserting a Tabulated List entry at point. +It is called with two arguments, ID and COLS. ID is a Lisp +object identifying the entry, and COLS is a vector of column +descriptors, as documented in `tabulated-list-entries'.") +(make-variable-buffer-local 'tabulated-list-printer) + +(defvar tabulated-list-sort-key nil + "Sort key for the current Tabulated List mode buffer. +If nil, no additional sorting is performed. +Otherwise, this should be a cons cell (NAME . FLIP). +NAME is a string matching one of the column names in +`tabulated-list-format' (the corresponding SORT entry in +`tabulated-list-format' then specifies how to sort). FLIP, if +non-nil, means to invert the resulting sort.") +(make-variable-buffer-local 'tabulated-list-sort-key) + +(defun tabulated-list-get-id (&optional pos) + "Obtain the entry ID of the Tabulated List mode entry at POS. +This is an ID object from `tabulated-list-entries', or nil. +POS, if omitted or nil, defaults to point." + (get-text-property (or pos (point)) 'tabulated-list-id)) + +(defun tabulated-list-put-tag (tag &optional advance) + "Put TAG in the padding area of the current line. +TAG should be a string, with length <= `tabulated-list-padding'. +If ADVANCE is non-nil, move forward by one line afterwards." + (unless (stringp tag) + (error "Invalid argument to `tabulated-list-put-tag'")) + (unless (> tabulated-list-padding 0) + (error "Unable to tag the current line")) + (save-excursion + (beginning-of-line) + (when (get-text-property (point) 'tabulated-list-id) + (let ((beg (point)) + (inhibit-read-only t)) + (forward-char tabulated-list-padding) + (insert-and-inherit + (if (<= (length tag) tabulated-list-padding) + (concat tag + (make-string (- tabulated-list-padding (length tag)) + ?\s)) + (substring tag 0 tabulated-list-padding))) + (delete-region beg (+ beg tabulated-list-padding))))) + (if advance + (forward-line))) + +(defvar tabulated-list-mode-map + (let ((map (copy-keymap special-mode-map))) + (set-keymap-parent map button-buffer-map) + (define-key map "n" 'next-line) + (define-key map "p" 'previous-line) + (define-key map [follow-link] 'mouse-face) + (define-key map [mouse-2] 'mouse-select-window) + map) + "Local keymap for `tabulated-list-mode' buffers.") + +(defvar tabulated-list-sort-button-map + (let ((map (make-sparse-keymap))) + (define-key map [header-line mouse-1] 'tabulated-list-col-sort) + (define-key map [header-line mouse-2] 'tabulated-list-col-sort) + (define-key map [follow-link] 'mouse-face) + map) + "Local keymap for `tabulated-list-mode' sort buttons.") + +(defun tabulated-list-init-header () + "Set up header line for the Tabulated List buffer." + (let ((x tabulated-list-padding) + (button-props `(help-echo "Click to sort by column" + mouse-face highlight + keymap ,tabulated-list-sort-button-map)) + (cols nil)) + (if (> tabulated-list-padding 0) + (push (propertize " " 'display `(space :align-to ,x)) cols)) + (dotimes (n (length tabulated-list-format)) + (let* ((col (aref tabulated-list-format n)) + (width (nth 1 col)) + (label (car col))) + (setq x (+ x 1 width)) + (and (<= tabulated-list-padding 0) + (= n 0) + (setq label (concat " " label))) + (push + (cond + ;; An unsortable column + ((not (nth 2 col)) label) + ;; The selected sort column + ((equal (car col) (car tabulated-list-sort-key)) + (apply 'propertize + (concat label + (cond + ((> (+ 2 (length label)) width) + "") + ((cdr tabulated-list-sort-key) + " ▲") + (t " ▼"))) + 'face 'bold + 'tabulated-list-column-name (car col) + button-props)) + ;; Unselected sortable column. + (t (apply 'propertize label + 'tabulated-list-column-name (car col) + button-props))) + cols)) + (push (propertize " " + 'display (list 'space :align-to x) + 'face 'fixed-pitch) + cols)) + (setq header-line-format (mapconcat 'identity (nreverse cols) "")))) + +(defun tabulated-list-revert (&rest ignored) + "The `revert-buffer-function' for `tabulated-list-mode'. +It runs `tabulated-list-revert-hook', then calls `tabulated-list-print'." + (interactive) + (unless (derived-mode-p 'tabulated-list-mode) + (error "The current buffer is not in Tabulated List mode")) + (run-hooks 'tabulated-list-revert-hook) + (tabulated-list-print t)) + +(defun tabulated-list-print (&optional remember-pos) + "Populate the current Tabulated List mode buffer. +This sorts the `tabulated-list-entries' list if sorting is +specified by `tabulated-list-sort-key'. It then erases the +buffer and inserts the entries with `tabulated-list-printer'. + +Optional argument REMEMBER-POS, if non-nil, means to move point +to the entry with the same ID element as the current line." + (let ((inhibit-read-only t) + (entries (if (functionp 'tabulated-list-entries) + (funcall tabulated-list-entries) + tabulated-list-entries)) + entry-id saved-pt saved-col) + (and remember-pos + (setq entry-id (tabulated-list-get-id)) + (setq saved-col (current-column))) + (erase-buffer) + ;; Sort the buffers, if necessary. + (when tabulated-list-sort-key + (let ((sort-column (car tabulated-list-sort-key)) + (len (length tabulated-list-format)) + (n 0) + sorter) + ;; Which column is to be sorted? + (while (and (< n len) + (not (equal (car (aref tabulated-list-format n)) + sort-column))) + (setq n (1+ n))) + (when (< n len) + (setq sorter (nth 2 (aref tabulated-list-format n))) + (when (eq sorter t) + (setq sorter ; Default sorter checks column N: + (lambda (A B) + (setq A (aref (cadr A) n)) + (setq B (aref (cadr B) n)) + (string< (if (stringp A) A (car A)) + (if (stringp B) B (car B)))))) + (setq entries (sort entries sorter)) + (if (cdr tabulated-list-sort-key) + (setq entries (nreverse entries))) + (unless (functionp 'tabulated-list-entries) + (setq tabulated-list-entries entries))))) + ;; Print the resulting list. + (dolist (elt entries) + (and entry-id + (equal entry-id (car elt)) + (setq saved-pt (point))) + (apply tabulated-list-printer elt)) + (set-buffer-modified-p nil) + ;; If REMEMBER-POS was specified, move to the "old" location. + (if saved-pt + (progn (goto-char saved-pt) + (move-to-column saved-col)) + (goto-char (point-min))))) + +(defun tabulated-list-print-entry (id cols) + "Insert a Tabulated List entry at point. +This is the default `tabulated-list-printer' function. ID is a +Lisp object identifying the entry to print, and COLS is a vector +of column descriptors." + (let ((beg (point)) + (x (max tabulated-list-padding 0)) + (len (length tabulated-list-format))) + (if (> tabulated-list-padding 0) + (insert (make-string x ?\s))) + (dotimes (n len) + (let* ((format (aref tabulated-list-format n)) + (desc (aref cols n)) + (width (nth 1 format)) + (label (if (stringp desc) desc (car desc))) + (help-echo (concat (car format) ": " label))) + ;; Truncate labels if necessary. + (and (> width 6) + (> (length label) width) + (setq label (concat (substring desc 0 (- width 3)) + "..."))) + (if (stringp desc) + (insert (propertize label 'help-echo help-echo)) + (apply 'insert-text-button label (cdr desc))) + (setq x (+ x 1 width))) + ;; No need to append any spaces if this is the last column. + (if (< (1+ n) len) + (indent-to x 1))) + (insert ?\n) + (put-text-property beg (point) 'tabulated-list-id id))) + +(defun tabulated-list-col-sort (&optional e) + "Sort Tabulated List entries by the column of the mouse click E." + (interactive "e") + (let* ((pos (event-start e)) + (obj (posn-object pos)) + (name (get-text-property (if obj (cdr obj) (posn-point pos)) + 'tabulated-list-column-name + (car obj)))) + (with-current-buffer (window-buffer (posn-window pos)) + (when (derived-mode-p 'tabulated-list-mode) + ;; Flip the sort order on a second click. + (if (equal name (car tabulated-list-sort-key)) + (setcdr tabulated-list-sort-key + (not (cdr tabulated-list-sort-key))) + (setq tabulated-list-sort-key (cons name nil))) + (tabulated-list-init-header) + (tabulated-list-print t))))) + +;;; The mode definition: + +;;;###autoload +(define-derived-mode tabulated-list-mode special-mode "Tabulated" + "Generic major mode for browsing a list of items. +This mode is usually not used directly; instead, other major +modes are derived from it, using `define-derived-mode'. + +In this major mode, the buffer is divided into multiple columns, +which are labelled using the header line. Each non-empty line +belongs to one \"entry\", and the entries can be sorted according +to their column values. + +An inheriting mode should usually do the following in their body: + + - Set `tabulated-list-format', specifying the column format. + - Set `tabulated-list-revert-hook', if the buffer contents need + to be specially recomputed prior to `revert-buffer'. + - Maybe set a `tabulated-list-entries' function (see below). + - Maybe set `tabulated-list-printer' (see below). + - Maybe set `tabulated-list-padding'. + - Call `tabulated-list-init-header' to initialize `header-line-format' + according to `tabulated-list-format'. + +An inheriting mode is usually accompanied by a \"list-FOO\" +command (e.g. `list-packages', `list-processes'). This command +creates or switches to a buffer and enables the major mode in +that buffer. If `tabulated-list-entries' is not a function, the +command should initialize it to a list of entries for displaying. +Finally, it should call `tabulated-list-print'. + +`tabulated-list-print' calls the printer function specified by +`tabulated-list-printer', once for each entry. The default +printer is `tabulated-list-print-entry', but a mode that keeps +data in an ewoc may instead specify a printer function (e.g., one +that calls `ewoc-enter-last'), with `tabulated-list-print-entry' +as the ewoc pretty-printer." + (setq truncate-lines t) + (setq buffer-read-only t) + (set (make-local-variable 'revert-buffer-function) + 'tabulated-list-revert)) + +(put 'tabulated-list-mode 'mode-class 'special) + +(provide 'tabulated-list) + +;; Local Variables: +;; coding: utf-8 +;; lexical-binding: t +;; End: + +;;; tabulated-list.el ends here |