-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnummm-mode.el
132 lines (106 loc) · 4.04 KB
/
nummm-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
;;; nummm-mode.el --- Display the number of minor modes instead of their names
;; This file is not part of Emacs
;; Copyright (C) 2013 Andreu Gil Pàmies
;; Filename: nummm-mode.el
;; Version: 0.1
;; Author: Andreu Gil <[email protected]>
;; Created: 02-06-2013
;; Description: Display the number of minor modes instead of their names in Emacs mode-line.
;; URL: http://github.com/agpchil/nummm-mode
;; 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.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Usage:
;; (require 'nummm-mode)
;; (nummm-mode t)
;;; Commentary:
;; This mode replaces the =minor-mode-alist= in =mode-line-modes= with
;; a custom one. =minor-mode-alist= is NOT modified. Also, a backup of
;; =mode-line-modes= is done when enabling =nummm-mode= and is
;; restored when is turned off.
;; The face can be changed customizing =nummm-face=.
;;; Code:
(defgroup nummm-mode nil
"Display the number of minor modes instead of their names in Emacs mode-line."
:prefix "nummm-mode"
:group 'help
:link '(url-link "http://github.com/agpchil/nummm-mode"))
(defcustom nummm-format "+%d"
"Format of nummm to show in mode line."
:type 'string
:group 'nummm-mode
:safe 'stringp)
(defvar nummm-mode-line-modes-backup nil)
(defvar nummm-minor-modes-names nil)
(defvar nummm-mode-string '(" " (:eval (nummm-counter))))
(defconst nummm-pattern '("" minor-mode-alist))
(defface nummm-face
'((t :inherit font-lock-warning-face :bold t))
"Face used in mode-line."
:group 'nummm-mode)
(defun nummm-find-minor-modes-alist (current-list)
"Find `minor-modes-alist` pattern in CURRENT-LIST.
Return a parent list that contains the pattern."
(let ((found nil)
(items current-list)
(item nil))
(while (and (not found) items)
(setq item (car items))
(if (and (listp item) (equal (cadr item) nummm-pattern))
(setq found items)
(setq items (cdr items))))
found))
(defun nummm-backup ()
"Backup current `mode-line-modes`."
(setq nummm-mode-line-modes-backup (copy-tree mode-line-modes)))
(defun nummm-restore ()
"Restore previous `mode-line-modes` value."
(setq mode-line-modes (copy-tree nummm-mode-line-modes-backup)))
(defun nummm-get-minor-modes-names ()
"Get the names of the enabled minor modes."
(delq nil (mapcar #'(lambda (m)
(when (symbol-value (car m))
(nth 1 m)))
minor-mode-alist)))
(defun nummm-count-minor-modes ()
"Count number of modes in `minor-mode-alist`."
(setq nummm-minor-modes-names (nummm-get-minor-modes-names))
(length nummm-minor-modes-names))
(defun nummm-counter ()
"Build the string to be displayed in `mode-line-modes`."
(propertize (format nummm-format (nummm-count-minor-modes))
'face 'nummm-face
'help-echo (format "%s" nummm-minor-modes-names)))
(defun nummm-turn-on ()
"Turn on nummm mode."
(unless nummm-mode-line-modes-backup
(nummm-backup))
(let ((modes nil))
(setq modes (nummm-find-minor-modes-alist mode-line-modes))
(when modes
(setf (car modes) nummm-mode-string))))
(defun nummm-turn-off ()
"Turn off nummm mode."
(nummm-restore))
(defvar nummm-mode-map (make-keymap)
"Keymap for nummm-mode.")
;;;###autoload
(define-minor-mode nummm-mode
"Display the number of minor modes instead of their names in Emacs mode-line."
:global t
:group 'nummm-mode
:init-value t
:lighter " nummm"
(progn
(if nummm-mode
(nummm-turn-on)
(nummm-turn-off))))
(provide 'nummm-mode)
;;; nummm-mode.el ends here