]> code.delx.au - gnu-emacs-elpa/blob - delight.el
840d03ee85561c3d57571d0f6efb5ea4bc39977a
[gnu-emacs-elpa] / delight.el
1 ;;; delight.el - A dimmer switch for your lighter text.
2 ;;
3 ;; Author: Phil S.
4 ;; URL: http://www.emacswiki.org/emacs/DelightedModes
5 ;; Version: 1.02
6
7 ;; Commentary:
8 ;;
9 ;; Enables you to customise the mode names displayed in the mode line.
10 ;;
11 ;; For major modes, the buffer-local `mode-name' variable is modified.
12 ;; For minor modes, the associated value in `minor-mode-alist' is set.
13 ;;
14 ;; Example usage:
15 ;;
16 ;; (require 'delight)
17 ;;
18 ;; (delight 'abbrev-mode " Abv" "abbrev")
19 ;;
20 ;; (delight '((abbrev-mode " Abv" "abbrev")
21 ;; (smart-tab-mode " \\t" "smart-tab")
22 ;; (eldoc-mode nil "eldoc")
23 ;; (rainbow-mode)
24 ;; (emacs-lisp-mode "Elisp" :major)))
25 ;;
26 ;; Important note:
27 ;;
28 ;; Although strings are common, any mode-line construct is permitted
29 ;; as the value (for both minor and major modes); so before you
30 ;; override a value you should check the existing one, as you may
31 ;; want to replicate any structural elements in your replacement
32 ;; if it turns out not to be a simple string.
33 ;;
34 ;; For major modes, M-: mode-name
35 ;; For minor modes, M-: (cadr (assq 'MODE minor-mode-alist))
36 ;; for the minor MODE in question.
37 ;;
38 ;; Conversely, you may incorporate additional mode-line constructs in
39 ;; your replacement values, if you so wish. e.g.:
40 ;;
41 ;; (delight 'emacs-lisp-mode
42 ;; '("Elisp" (lexical-binding ":Lex" ":Dyn"))
43 ;; :major)
44 ;;
45 ;; See `mode-line-format' for information about mode-line constructs,
46 ;; and M-: (info "(elisp) Mode Line Format") for further details.
47 ;;
48 ;; Also bear in mind that some modes may dynamically update these
49 ;; values themselves (for instance dired-mode updates mode-name if
50 ;; you change the sorting criteria) in which cases this library may
51 ;; prove inadequate.
52
53 ;;; Changelog:
54 ;;
55 ;; 1.02 - Bug fix for missing 'cl requirement for destructuring-bind macro.
56 ;; 1.01 - Added support for using the keyword :major as the FILE argument
57 ;; for major modes, to avoid also processing them as minor modes.
58
59 ;;; Code:
60
61 (eval-when-compile
62 (require 'cl))
63
64 (defvar delighted-modes ()
65 "List of specs for modifying the display of mode names in the mode line.
66
67 See `delight'.")
68
69 ;;;###autoload
70 (defun delight (spec &optional value file)
71 "Modify the lighter value displayed in the mode line for the given mode SPEC
72 if and when the mode is loaded.
73
74 SPEC can be either a mode symbol, or a list containing multiple elements of
75 the form (MODE VALUE FILE). In the latter case the two optional arguments are
76 omitted, as they are instead specified for each element of the list.
77
78 For minor modes, VALUE is the replacement lighter value (or nil to disable)
79 to set in the `minor-mode-alist' variable. For major modes VALUE is the
80 replacement buffer-local `mode-name' value to use when a buffer changes to
81 that mode.
82
83 In both cases VALUE is commonly a string, but may in fact contain any valid
84 mode-line construct. See `mode-line-format' for details.
85
86 The FILE argument is passed through to `eval-after-load'. If FILE is nil then
87 the mode symbol is passed as the required feature. Both of these cases are
88 relevant to minor modes only.
89
90 For major modes you should specify the keyword :major as the value of FILE,
91 to prevent the mode being treated as a minor mode."
92 (add-hook 'after-change-major-mode-hook 'delight-major-mode)
93 (let ((glum (if (consp spec) spec (list (list spec value file)))))
94 (while glum
95 (destructuring-bind (mode &optional value file) (pop glum)
96 (assq-delete-all mode delighted-modes)
97 (add-to-list 'delighted-modes (list mode value file))
98 (unless (eq file :major)
99 (eval-after-load (or file mode)
100 `(let ((minor-delight (assq ',mode minor-mode-alist)))
101 (when minor-delight
102 (setcar (cdr minor-delight) ',value)))))))))
103
104 (defun delight-major-mode ()
105 "Delight the 'pretty name' of the current buffer's major mode
106 when displayed in the mode-line.
107
108 When `mode-name' is displayed in other contexts (such as in the
109 `describe-mode' help buffer), its original value will be used."
110 (let ((major-delight (assq major-mode delighted-modes)))
111 (when major-delight
112 (setq mode-name `(inhibit-mode-name-delight
113 ,mode-name ;; glum
114 ,(cadr major-delight)))))) ;; delighted
115
116 (defadvice format-mode-line (around delighted-modes-are-glum activate)
117 "Delighted modes should exhibit their original `mode-name' when
118 `format-mode-line' is called. See `delight-major-mode'."
119 (let ((inhibit-mode-name-delight t))
120 ad-do-it))
121
122 (provide 'delight)