]> code.delx.au - gnu-emacs-elpa/blob - delight.el
Original release.
[gnu-emacs-elpa] / delight.el
1 ;;; delight.el - A dimmer switch for your lighter text.
2
3 ;; Commentary:
4 ;;
5 ;; Enables you to customise the mode names displayed in the mode line.
6 ;;
7 ;; For major modes, the buffer-local `mode-name' variable is modified,
8 ;; with advice around the `format-mode-line' function ensuring that the
9 ;; original value is used in contexts outside of mode line redraws.
10 ;;
11 ;; For minor modes, the associated value in `minor-mode-alist' is set.
12 ;;
13 ;; Example usage:
14 ;;
15 ;; (delight 'abbrev-mode " Abv" "abbrev")
16 ;;
17 ;; (delight '((abbrev-mode " Abv" "abbrev")
18 ;; (smart-tab-mode " \\t" smart-tab)
19 ;; (eldoc-mode nil "eldoc")
20 ;; (rainbow-mode)
21 ;; (emacs-lisp-mode "Elisp" "lisp-mode")))
22
23 ;;; Code:
24
25 (defvar delighted ()
26 "List of specs for modifying the display of mode names in the mode line.")
27
28 ;;;###autoload
29 (defun delight (spec &optional value file)
30 "Modify the lighter value displayed in the mode line for the given mode SPEC
31 if and when the mode is loaded.
32
33 SPEC can be either a mode symbol, or a list of the form ((MODE VALUE FILE) ...)
34
35 For minor modes, VALUE is the replacement lighter value (or nil to disable).
36 VALUE is typically a string, but may have other values. See `minor-mode-alist'
37 for details.
38
39 For major modes, VALUE is typically a string to which `mode-name' will be set,
40 but any value suitable for `mode-line-format' may be used.
41
42 The optional FILE argument is the file to pass to `eval-after-load'.
43 If FILE is nil then the mode symbol is passed as the required feature."
44 (add-hook 'after-change-major-mode-hook 'delight-major-mode)
45 (let ((glum (if (consp spec) spec (list (list spec value file)))))
46 (while glum
47 (destructuring-bind (mode &optional value file) (pop glum)
48 (assq-delete-all mode delighted)
49 (add-to-list 'delighted (list mode value file))
50 (eval-after-load (or file mode)
51 `(let ((minor-delight (assq ',mode minor-mode-alist)))
52 (when minor-delight
53 (setcar (cdr minor-delight) ',value))))))))
54
55 (defun delight-major-mode ()
56 "Delight the 'pretty name' of the current buffer's major mode
57 during mode-line redraws. For other uses of `mode-name', this
58 delight will be inhibited."
59 (let ((major-delight (assq major-mode delighted)))
60 (when major-delight
61 (set (make-local-variable 'mode-name-glum) mode-name)
62 (set (make-local-variable 'mode-name-delighted) (cadr major-delight))
63 (setq mode-name '(inhibit-mode-name-delight
64 mode-name-glum
65 mode-name-delighted)))))
66
67 (defadvice format-mode-line (around delight-glum-mode-name activate)
68 "Delighted major modes must exhibit their original glum `mode-name' when
69 `format-mode-line' is called. See `delight-major-mode'."
70 (let ((inhibit-mode-name-delight t))
71 ad-do-it))