]> code.delx.au - gnu-emacs-elpa/blob - delight.el
ba693c4adb3b97e68bed112d15a7e226508e89d2
[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 ;; Keywords: convenience
6 ;; Created: 25 Jun 2013
7 ;; Version: 1.05
8
9 ;; This file is not part of GNU Emacs.
10
11 ;; This file is free software: you can redistribute it and/or modify it under
12 ;; the terms of the GNU General Public License as published by the Free Software
13 ;; Foundation, either version 3 of the License, or (at your option) any later
14 ;; version. See <http://www.gnu.org/licenses/>.
15
16 ;;; Commentary:
17 ;;
18 ;; Enables you to customise the mode names displayed in the mode line.
19 ;;
20 ;; For major modes, the buffer-local `mode-name' variable is modified.
21 ;; For minor modes, the associated value in `minor-mode-alist' is set.
22 ;;
23 ;; Example usage:
24 ;;
25 ;; (require 'delight)
26 ;;
27 ;; (delight 'abbrev-mode " Abv" "abbrev")
28 ;;
29 ;; (delight '((abbrev-mode " Abv" "abbrev")
30 ;; (smart-tab-mode " \\t" "smart-tab")
31 ;; (eldoc-mode nil "eldoc")
32 ;; (rainbow-mode)
33 ;; (overwrite-mode " Ov" t)
34 ;; (emacs-lisp-mode "Elisp" :major)))
35 ;;
36 ;; The first argument is the mode symbol.
37 ;;
38 ;; The second argument is the replacement name to use in the mode line
39 ;; (or nil to hide it).
40 ;;
41 ;; The third argument is either the keyword :major for major modes or,
42 ;; for minor modes, the library which defines the mode. This is passed
43 ;; to ‘eval-after-load’ and so should be either the name (as a string)
44 ;; of the library file which defines the mode, or the feature (symbol)
45 ;; provided by that library. If this argument is nil, the mode symbol
46 ;; will be passed as the feature. If this argument is either t or 'emacs
47 ;; then it is assumed that the mode is already loaded (you can use this
48 ;; with standard minor modes that are pre-loaded by default when Emacs
49 ;; starts).
50 ;;
51 ;; To determine which library defines a mode, use e.g.: C-h f
52 ;; eldoc-mode RET. The name of the library is displayed in the first
53 ;; paragraph, with an “.el” suffix (in this example it displays
54 ;; “eldoc.el”, and therefore we could use the value “eldoc” for the
55 ;; library).
56 ;;
57 ;; Important note:
58 ;;
59 ;; Although strings are common, any mode-line construct is permitted
60 ;; as the value (for both minor and major modes); so before you
61 ;; override a value you should check the existing one, as you may
62 ;; want to replicate any structural elements in your replacement
63 ;; if it turns out not to be a simple string.
64 ;;
65 ;; For major modes, M-: mode-name
66 ;; For minor modes, M-: (cadr (assq 'MODE minor-mode-alist))
67 ;; for the minor MODE in question.
68 ;;
69 ;; Conversely, you may incorporate additional mode-line constructs in
70 ;; your replacement values, if you so wish. e.g.:
71 ;;
72 ;; (delight 'emacs-lisp-mode
73 ;; '("Elisp" (lexical-binding ":Lex" ":Dyn"))
74 ;; :major)
75 ;;
76 ;; See `mode-line-format' for information about mode-line constructs,
77 ;; and M-: (info "(elisp) Mode Line Format") for further details.
78 ;;
79 ;; Also bear in mind that some modes may dynamically update these
80 ;; values themselves (for instance dired-mode updates mode-name if
81 ;; you change the sorting criteria) in which cases this library may
82 ;; prove inadequate.
83
84 ;;; Change Log:
85 ;;
86 ;; 1.05 (2016-03-01) Support FILE value t, meaning that the minor MODE
87 ;; in question is guaranteed to already be loaded.
88 ;; 1.04 (2016-02-28) Respect `inhibit-mode-name-delight' when already set.
89 ;; 1.03 (2014-05-30) Added support for `mode-line-mode-menu'.
90 ;; 1.02 (2014-05-04) Bug fix for missing 'cl requirement for
91 ;; destructuring-bind macro.
92 ;; 1.01 (2014-05-04) Allow the keyword :major as the FILE argument for
93 ;; major modes, to avoid also processing them as minor modes.
94 ;; 1.00 (2013-06-25) Initial release.
95
96 ;;; Code:
97
98 (eval-when-compile
99 (require 'cl))
100
101 (defvar delighted-modes ()
102 "List of specs for modifying the display of mode names in the mode line.
103
104 See `delight'.")
105
106 ;;;###autoload
107 (defun delight (spec &optional value file)
108 "Modify the lighter value displayed in the mode line for the given mode SPEC
109 if and when the mode is loaded.
110
111 SPEC can be either a mode symbol, or a list containing multiple elements of
112 the form (MODE VALUE FILE). In the latter case the two optional arguments are
113 omitted, as they are instead specified for each element of the list.
114
115 For minor modes, VALUE is the replacement lighter value (or nil to disable)
116 to set in the `minor-mode-alist' variable. For major modes VALUE is the
117 replacement buffer-local `mode-name' value to use when a buffer changes to
118 that mode.
119
120 In both cases VALUE is commonly a string, but may in fact contain any valid
121 mode-line construct. For details see the `mode-line-format' variable, and
122 Info node `(elisp) Mode Line Format'.
123
124 The FILE argument is passed through to `eval-after-load'. If FILE is nil then
125 the mode symbol is passed as the required feature. If FILE is t then it is
126 assumed that the mode is already loaded. (Note that you can also use 'emacs
127 for this purpose). These FILE options are relevant to minor modes only.
128
129 For major modes you should specify the keyword :major as the value of FILE,
130 to prevent the mode being treated as a minor mode."
131 (add-hook 'after-change-major-mode-hook 'delight-major-mode)
132 (let ((glum (if (consp spec) spec (list (list spec value file)))))
133 (while glum
134 (destructuring-bind (mode &optional value file) (pop glum)
135 (assq-delete-all mode delighted-modes)
136 (add-to-list 'delighted-modes (list mode value file))
137 (unless (eq file :major)
138 (eval-after-load (if (eq file t) 'emacs (or file mode))
139 `(let ((minor-delight (assq ',mode minor-mode-alist)))
140 (when minor-delight
141 (setcar (cdr minor-delight) ',value)
142 (delight-mode-line-mode-menu ',mode ',value)))))))))
143
144 (defun delight-mode-line-mode-menu (mode value)
145 "Delight `mode-line-mode-menu' (the \"Toggle minor modes\" menu)
146 so that the Lighter text displayed in the menu matches that displayed in
147 the mode line (when such menu items exist).
148
149 The expected naming scheme for the menu items is: \"Friendly name (Lighter)\"
150 e.g.: \"Highlight changes (Chg)\".
151
152 We replace the \"Lighter\" portion of that with our delighted VALUE, for the
153 specified MODE, unless VALUE is empty/nil, in which case we remove the text
154 and parentheses altogether.
155
156 If the delighted VALUE is not a string and not nil, we do nothing."
157 (when (string-or-null-p value)
158 (let* ((menu-keymap mode-line-mode-menu)
159 (menu-item (assq mode (cdr menu-keymap))))
160 (when menu-item
161 ;; Lighter text is typically prefixed with a space to separate
162 ;; it from the preceding lighter. We need to trim that space.
163 (let* ((trimmed-value (if (and value (string-match "\\`\\s-+" value))
164 (replace-match "" t t value)
165 value))
166 (wrapped-value (if (> (length trimmed-value) 0)
167 (concat " (" trimmed-value ")")
168 ""))
169 (menu-def (cdr menu-item))
170 (label (cadr menu-def))
171 (new-label (and (stringp label)
172 (or (string-match "\\s-+(.+?)\\s-*\\'" label)
173 (string-match "\\s-*\\'" label))
174 (replace-match wrapped-value t t label))))
175 (when new-label
176 ;; Pure storage is used for the default menu items, so we
177 ;; cannot modify those objects directly.
178 (setq menu-def (copy-sequence menu-def))
179 (setf (cadr menu-def) new-label)
180 (define-key menu-keymap (vector mode) menu-def)))))))
181
182 (defun delight-major-mode ()
183 "Delight the 'pretty name' of the current buffer's major mode
184 when displayed in the mode-line.
185
186 When `mode-name' is displayed in other contexts (such as in the
187 `describe-mode' help buffer), its original value will be used."
188 (let ((major-delight (assq major-mode delighted-modes)))
189 (when major-delight
190 (setq mode-name `(inhibit-mode-name-delight
191 ,mode-name ;; glum
192 ,(cadr major-delight)))))) ;; delighted
193
194 (defvar inhibit-mode-name-delight)
195
196 (defadvice format-mode-line (around delighted-modes-are-glum activate)
197 "Delighted modes should exhibit their original `mode-name' when
198 `format-mode-line' is called. See `delight-major-mode'."
199 (let ((inhibit-mode-name-delight (if (boundp 'inhibit-mode-name-delight)
200 inhibit-mode-name-delight
201 t)))
202 ad-do-it))
203
204 (provide 'delight)
205 ;;; delight.el ends here