]> code.delx.au - gnu-emacs-elpa/blob - delight.el
Added support for `mode-line-mode-menu'
[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.03
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.03 (2014-05-30) Added support for `mode-line-mode-menu'.
56 ;; 1.02 (2014-05-04) Bug fix for missing 'cl requirement for
57 ;; destructuring-bind macro.
58 ;; 1.01 (2014-05-04) Allow the keyword :major as the FILE argument for
59 ;; major modes, to avoid also processing them as minor modes.
60 ;; 1.00 (2013-06-25) Initial release.
61
62 ;;; Code:
63
64 (eval-when-compile
65 (require 'cl))
66
67 (defvar delighted-modes ()
68 "List of specs for modifying the display of mode names in the mode line.
69
70 See `delight'.")
71
72 ;;;###autoload
73 (defun delight (spec &optional value file)
74 "Modify the lighter value displayed in the mode line for the given mode SPEC
75 if and when the mode is loaded.
76
77 SPEC can be either a mode symbol, or a list containing multiple elements of
78 the form (MODE VALUE FILE). In the latter case the two optional arguments are
79 omitted, as they are instead specified for each element of the list.
80
81 For minor modes, VALUE is the replacement lighter value (or nil to disable)
82 to set in the `minor-mode-alist' variable. For major modes VALUE is the
83 replacement buffer-local `mode-name' value to use when a buffer changes to
84 that mode.
85
86 In both cases VALUE is commonly a string, but may in fact contain any valid
87 mode-line construct. For details see the `mode-line-format' variable, and
88 Info node `(elisp) Mode Line Format'.
89
90 The FILE argument is passed through to `eval-after-load'. If FILE is nil then
91 the mode symbol is passed as the required feature. Both of these cases are
92 relevant to minor modes only.
93
94 For major modes you should specify the keyword :major as the value of FILE,
95 to prevent the mode being treated as a minor mode."
96 (add-hook 'after-change-major-mode-hook 'delight-major-mode)
97 (let ((glum (if (consp spec) spec (list (list spec value file)))))
98 (while glum
99 (destructuring-bind (mode &optional value file) (pop glum)
100 (assq-delete-all mode delighted-modes)
101 (add-to-list 'delighted-modes (list mode value file))
102 (unless (eq file :major)
103 (eval-after-load (or file mode)
104 `(let ((minor-delight (assq ',mode minor-mode-alist)))
105 (when minor-delight
106 (setcar (cdr minor-delight) ',value)
107 (delight-mode-line-mode-menu ',mode ',value)))))))))
108
109 (defun delight-mode-line-mode-menu (mode value)
110 "Delight `mode-line-mode-menu' (the \"Toggle minor modes\" menu)
111 so that the Lighter text displayed in the menu matches that displayed in
112 the mode line (when such menu items exist).
113
114 The expected naming scheme for the menu items is: \"Friendly name (Lighter)\"
115 e.g.: \"Highlight changes (Chg)\".
116
117 We replace the \"Lighter\" portion of that with our delighted VALUE, for the
118 specified MODE, unless VALUE is empty/nil, in which case we remove the text
119 and parentheses altogether.
120
121 If the delighted VALUE is not a string and not nil, we do nothing."
122 (when (string-or-null-p value)
123 (let* ((menu-keymap mode-line-mode-menu)
124 (menu-item (assq mode (cdr menu-keymap))))
125 (when menu-item
126 ;; Lighter text is typically prefixed with a space to separate
127 ;; it from the preceding lighter. We need to trim that space.
128 (let* ((trimmed-value (if (and value (string-match "\\`\\s-+" value))
129 (replace-match "" t t value)
130 value))
131 (wrapped-value (if (> (length trimmed-value) 0)
132 (concat " (" trimmed-value ")")
133 ""))
134 (menu-def (cdr menu-item))
135 (label (cadr menu-def))
136 (new-label (and (stringp label)
137 (or (string-match "\\s-+(.+?)\\s-*\\'" label)
138 (string-match "\\s-*\\'" label))
139 (replace-match wrapped-value t t label))))
140 (when new-label
141 ;; Pure storage is used for the default menu items, so we
142 ;; cannot modify those objects directly.
143 (setq menu-def (copy-sequence menu-def))
144 (setf (cadr menu-def) new-label)
145 (define-key menu-keymap (vector mode) menu-def)))))))
146
147 (defun delight-major-mode ()
148 "Delight the 'pretty name' of the current buffer's major mode
149 when displayed in the mode-line.
150
151 When `mode-name' is displayed in other contexts (such as in the
152 `describe-mode' help buffer), its original value will be used."
153 (let ((major-delight (assq major-mode delighted-modes)))
154 (when major-delight
155 (setq mode-name `(inhibit-mode-name-delight
156 ,mode-name ;; glum
157 ,(cadr major-delight)))))) ;; delighted
158
159 (defadvice format-mode-line (around delighted-modes-are-glum activate)
160 "Delighted modes should exhibit their original `mode-name' when
161 `format-mode-line' is called. See `delight-major-mode'."
162 (let ((inhibit-mode-name-delight t))
163 ad-do-it))
164
165 (provide 'delight)
166 ;;; delight.el ends here