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