-
Notifications
You must be signed in to change notification settings - Fork 4
/
blackout.el
162 lines (139 loc) · 5.23 KB
/
blackout.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
;;; blackout.el --- Better mode lighter overriding -*- lexical-binding: t -*-
;; Copyright (C) 2018-2022 Radian LLC and contributors
;; Author: Radian LLC <[email protected]>
;; Created: 12 Aug 2018
;; Homepage: https://github.com/radian-software/blackout
;; Keywords: extensions
;; Package-Requires: ((emacs "26"))
;; SPDX-License-Identifier: MIT
;; Version: 1.0
;;; Commentary:
;; Blackout is a package which allows you to hide or customize the
;; display of major and minor modes in the mode line. It can replace
;; diminish.el, delight.el, and dim.el.
;; Hide a minor mode:
;;
;; (blackout 'auto-fill-mode)
;;
;; Change the display of a minor mode (note the leading space; this is
;; needed if you want there to be a space between this mode lighter and
;; the previous one):
;;
;; (blackout 'auto-fill-mode " Auto-Fill")
;;
;; Change the display of a major mode:
;;
;; (blackout 'emacs-lisp-mode "Elisp")
;;
;; Operate on a mode which hasn't yet been loaded:
;;
;; (with-eval-after-load 'ivy
;; (blackout 'ivy-mode))
;;
;; Usage with use-package:
;;
;; (use-package foo
;; :blackout t)
;;
;; (use-package foo
;; :blackout " Foo")
;;
;; (use-package quux
;; :blackout ((foo-mode . " Foo")
;; (bar-mode . " Bar")))
;; Please see https://github.com/radian-software/blackout for more information.
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'subr-x)
(defvar use-package-keywords)
(declare-function use-package-as-mode "ext:use-package-core")
(declare-function use-package-concat "ext:use-package-core")
(declare-function use-package-error "ext:use-package-core")
(declare-function use-package-process-keywords "ext:use-package-core")
(defgroup blackout nil
"Better mode lighter overriding."
:group 'lisp
:link '(url-link :tag "GitHub" "https://github.com/radian-software/blackout")
:link '(emacs-commentary-link :tag "Commentary" "blackout"))
(defcustom blackout-minor-mode-variables
;; Use backquote to avoid this code getting modified if someone
;; mutates the list.
`((auto-fill-mode . auto-fill-function))
"Alist of minor modes with nonstandard variable names.
\(Such minor modes are produced by passing a custom `:variable' to
`define-minor-mode'.) The keys are minor mode symbols and the
values are variable names."
:type '(alist :key-type function :value-type variable))
(defvar blackout--mode-names nil
"Alist mapping mode name symbols to mode line constructs, or nil.")
(defun blackout--handle-minor-mode (mode)
"Update the name for given minor MODE in `minor-mode-alist'."
(setq mode (alist-get mode blackout-minor-mode-variables mode))
(when-let ((spec (assq mode minor-mode-alist)))
(setf (nth 1 spec) (alist-get mode blackout--mode-names))))
(defun blackout--handle-major-mode ()
"Update `mode-name' for the current buffer, if necessary."
(when-let ((spec (assq major-mode blackout--mode-names)))
(setq-local mode-name (cdr spec))))
;;;###autoload
(defun blackout (mode &optional replacement)
"Do not display MODE in the mode line.
If REPLACEMENT is given, then display it instead. REPLACEMENT may
be a string or more generally any mode line construct (see
`mode-line-format')."
(setf (alist-get mode blackout--mode-names) replacement)
(blackout--handle-minor-mode mode)
(dolist (buf (buffer-list))
(with-current-buffer buf
(blackout--handle-major-mode))))
(add-hook 'after-change-major-mode-hook #'blackout--handle-major-mode)
;;;###autoload
(defun use-package-normalize/:blackout (name _keyword args)
"Normalize the arguments to `:blackout'.
The return value is an alist whose cars are mode names and whose
cdrs are mode line constructs. For documentation on NAME,
KEYWORD, and ARGS, refer to `use-package'."
(when (> (length args) 1)
(use-package-error ":blackout wants at most one argument"))
;; If no args given, default to t.
(let ((alist (if args (car args) t)))
;; Default to feature name as mode if t given.
(when (eq alist t)
(setq alist (use-package-as-mode name)))
;; If mode name given, assume we want to blacken it to nil.
;; Otherwise, assume we want to blacken the feature name as a mode
;; to the given construct.
(cond
((and (symbolp alist)
(string-suffix-p "-mode" (symbol-name alist)))
(setq alist (cons alist nil)))
((or (null alist) (not (listp alist)))
(setq alist (cons (use-package-as-mode name) alist))))
;; If only a cons cell given, make it into an alist.
(unless (cl-every #'consp alist)
(setq alist (list alist)))
alist))
;;;###autoload
(defun use-package-handler/:blackout (name _keyword arg rest state)
"Handle `:blackout' keyword.
For documentation on NAME, KEYWORD, ARG, REST, and STATE, refer
to `use-package'."
(let ((body (use-package-process-keywords name rest state)))
(use-package-concat
body
(mapcar
(lambda (spec)
`(blackout ',(car spec) ,(cdr spec)))
arg))))
;;;###autoload
(with-eval-after-load 'use-package-core
(when (and (boundp 'use-package-keywords)
(listp use-package-keywords))
(add-to-list 'use-package-keywords :blackout 'append)))
(provide 'blackout)
;; Local Variables:
;; sentence-end-double-space: nil
;; outline-regexp: ";;;;* "
;; End:
;;; blackout.el ends here