]> code.delx.au - gnu-emacs-elpa/blobdiff - delight.el
Respect `inhibit-mode-name-delight' when already set
[gnu-emacs-elpa] / delight.el
index 989bb8cca065996e589922543746efa3efb25714..8e588900ad2270e6c75c6261f1ae2b9614a0b653 100644 (file)
@@ -1,10 +1,19 @@
-;;; delight.el - A dimmer switch for your lighter text.
+;;; delight.el --- A dimmer switch for your lighter text.
 ;;
 ;; Author: Phil S.
 ;; URL: http://www.emacswiki.org/emacs/DelightedModes
-;; Version: 1.01
+;; Keywords: convenience
+;; Created: 25 Jun 2013
+;; Version: 1.04
 
-;; Commentary:
+;; This file is not part of GNU Emacs.
+
+;; This file is free software: you can redistribute it and/or modify it under
+;; the terms of the GNU General Public License as published by the Free Software
+;; Foundation, either version 3 of the License, or (at your option) any later
+;; version. See <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
 ;;
 ;; Enables you to customise the mode names displayed in the mode line.
 ;;
 
 ;;; Changelog:
 ;;
-;; 1.01 - Added support for using the keyword :major as the FILE argument
-;;        for major modes, to avoid also processing them as minor modes.
+;; 1.04 (2016-02-28) Respect `inhibit-mode-name-delight' when already set.
+;; 1.03 (2014-05-30) Added support for `mode-line-mode-menu'.
+;; 1.02 (2014-05-04) Bug fix for missing 'cl requirement for
+;;       destructuring-bind macro.
+;; 1.01 (2014-05-04) Allow the keyword :major as the FILE argument for
+;;       major modes, to avoid also processing them as minor modes.
+;; 1.00 (2013-06-25) Initial release.
 
 ;;; Code:
 
+(eval-when-compile
+  (require 'cl))
+
 (defvar delighted-modes ()
   "List of specs for modifying the display of mode names in the mode line.
 
@@ -77,7 +94,8 @@ replacement buffer-local `mode-name' value to use when a buffer changes to
 that mode.
 
 In both cases VALUE is commonly a string, but may in fact contain any valid
-mode-line construct. See `mode-line-format' for details.
+mode-line construct. For details see the `mode-line-format' variable, and
+Info node `(elisp) Mode Line Format'.
 
 The FILE argument is passed through to `eval-after-load'. If FILE is nil then
 the mode symbol is passed as the required feature. Both of these cases are
@@ -95,7 +113,46 @@ to prevent the mode being treated as a minor mode."
           (eval-after-load (or file mode)
             `(let ((minor-delight (assq ',mode minor-mode-alist)))
                (when minor-delight
-                 (setcar (cdr minor-delight) ',value)))))))))
+                 (setcar (cdr minor-delight) ',value)
+                 (delight-mode-line-mode-menu ',mode ',value)))))))))
+
+(defun delight-mode-line-mode-menu (mode value)
+  "Delight `mode-line-mode-menu' (the \"Toggle minor modes\" menu)
+so that the Lighter text displayed in the menu matches that displayed in
+the mode line (when such menu items exist).
+
+The expected naming scheme for the menu items is: \"Friendly name (Lighter)\"
+e.g.: \"Highlight changes (Chg)\".
+
+We replace the \"Lighter\" portion of that with our delighted VALUE, for the
+specified MODE, unless VALUE is empty/nil, in which case we remove the text
+and parentheses altogether.
+
+If the delighted VALUE is not a string and not nil, we do nothing."
+  (when (string-or-null-p value)
+    (let* ((menu-keymap mode-line-mode-menu)
+           (menu-item (assq mode (cdr menu-keymap))))
+      (when menu-item
+        ;; Lighter text is typically prefixed with a space to separate
+        ;; it from the preceding lighter. We need to trim that space.
+        (let* ((trimmed-value (if (and value (string-match "\\`\\s-+" value))
+                                  (replace-match "" t t value)
+                                value))
+               (wrapped-value (if (> (length trimmed-value) 0)
+                                  (concat " (" trimmed-value ")")
+                                ""))
+               (menu-def (cdr menu-item))
+               (label (cadr menu-def))
+               (new-label (and (stringp label)
+                               (or (string-match "\\s-+(.+?)\\s-*\\'" label)
+                                   (string-match "\\s-*\\'" label))
+                               (replace-match wrapped-value t t label))))
+          (when new-label
+            ;; Pure storage is used for the default menu items, so we
+            ;; cannot modify those objects directly.
+            (setq menu-def (copy-sequence menu-def))
+            (setf (cadr menu-def) new-label)
+            (define-key menu-keymap (vector mode) menu-def)))))))
 
 (defun delight-major-mode ()
   "Delight the 'pretty name' of the current buffer's major mode
@@ -109,10 +166,15 @@ When `mode-name' is displayed in other contexts (such as in the
                         ,mode-name ;; glum
                         ,(cadr major-delight)))))) ;; delighted
 
+(defvar inhibit-mode-name-delight)
+
 (defadvice format-mode-line (around delighted-modes-are-glum activate)
   "Delighted modes should exhibit their original `mode-name' when
 `format-mode-line' is called. See `delight-major-mode'."
-  (let ((inhibit-mode-name-delight t))
+  (let ((inhibit-mode-name-delight (if (boundp 'inhibit-mode-name-delight)
+                                       inhibit-mode-name-delight
+                                     t)))
     ad-do-it))
 
 (provide 'delight)
+;;; delight.el ends here