]> code.delx.au - gnu-emacs-elpa/blob - delight.el
Added support for FILE argument :major
[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.01
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.01 - Added support for using the keyword :major as the FILE argument
56 ;; for major modes, to avoid also processing them as minor modes.
57
58 ;;; Code:
59
60 (defvar delighted-modes ()
61 "List of specs for modifying the display of mode names in the mode line.
62
63 See `delight'.")
64
65 ;;;###autoload
66 (defun delight (spec &optional value file)
67 "Modify the lighter value displayed in the mode line for the given mode SPEC
68 if and when the mode is loaded.
69
70 SPEC can be either a mode symbol, or a list containing multiple elements of
71 the form (MODE VALUE FILE). In the latter case the two optional arguments are
72 omitted, as they are instead specified for each element of the list.
73
74 For minor modes, VALUE is the replacement lighter value (or nil to disable)
75 to set in the `minor-mode-alist' variable. For major modes VALUE is the
76 replacement buffer-local `mode-name' value to use when a buffer changes to
77 that mode.
78
79 In both cases VALUE is commonly a string, but may in fact contain any valid
80 mode-line construct. See `mode-line-format' for details.
81
82 The FILE argument is passed through to `eval-after-load'. If FILE is nil then
83 the mode symbol is passed as the required feature. Both of these cases are
84 relevant to minor modes only.
85
86 For major modes you should specify the keyword :major as the value of FILE,
87 to prevent the mode being treated as a minor mode."
88 (add-hook 'after-change-major-mode-hook 'delight-major-mode)
89 (let ((glum (if (consp spec) spec (list (list spec value file)))))
90 (while glum
91 (destructuring-bind (mode &optional value file) (pop glum)
92 (assq-delete-all mode delighted-modes)
93 (add-to-list 'delighted-modes (list mode value file))
94 (unless (eq file :major)
95 (eval-after-load (or file mode)
96 `(let ((minor-delight (assq ',mode minor-mode-alist)))
97 (when minor-delight
98 (setcar (cdr minor-delight) ',value)))))))))
99
100 (defun delight-major-mode ()
101 "Delight the 'pretty name' of the current buffer's major mode
102 when displayed in the mode-line.
103
104 When `mode-name' is displayed in other contexts (such as in the
105 `describe-mode' help buffer), its original value will be used."
106 (let ((major-delight (assq major-mode delighted-modes)))
107 (when major-delight
108 (setq mode-name `(inhibit-mode-name-delight
109 ,mode-name ;; glum
110 ,(cadr major-delight)))))) ;; delighted
111
112 (defadvice format-mode-line (around delighted-modes-are-glum activate)
113 "Delighted modes should exhibit their original `mode-name' when
114 `format-mode-line' is called. See `delight-major-mode'."
115 (let ((inhibit-mode-name-delight t))
116 ad-do-it))
117
118 (provide 'delight)