/ diminish.el
diminish.el
  1  ;;; diminish.el --- Diminished modes are minor modes with no modeline display
  2  
  3  ;; Copyright (C) 1998 Free Software Foundation, Inc.
  4  
  5  ;; Author: Will Mengarini <seldon@eskimo.com>
  6  ;; Maintainer: Martin Yrjölä <martin.yrjola@gmail.com>
  7  ;; URL: https://github.com/myrjola/diminish.el
  8  ;; Created: Th 19 Feb 98
  9  ;; Version: 0.46
 10  ;; Package-Requires: ((emacs "24.3"))
 11  ;; Keywords: extensions, diminish, minor, codeprose
 12  
 13  ;; This program is free software; you can redistribute it and/or modify
 14  ;; it under the terms of the GNU General Public License as published by
 15  ;; the Free Software Foundation, either version 3 of the License, or
 16  ;; (at your option) any later version.
 17  
 18  ;; This program is distributed in the hope that it will be useful,
 19  ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 20  ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21  ;; GNU General Public License for more details.
 22  
 23  ;; You should have received a copy of the GNU General Public License
 24  ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
 25  
 26  ;;; Commentary:
 27  
 28  ;; Minor modes each put a word on the mode line to signify that they're
 29  ;; active.  This can cause other displays, such as % of file that point is
 30  ;; at, to run off the right side of the screen.  For some minor modes, such
 31  ;; as mouse-avoidance-mode, the display is a waste of space, since users
 32  ;; typically set the mode in their .emacs & never change it.  For other
 33  ;; modes, such as my jiggle-mode, it's a waste because there's already a
 34  ;; visual indication of whether the mode is in effect.
 35  
 36  ;; A diminished mode is a minor mode that has had its mode line
 37  ;; display diminished, usually to nothing, although diminishing to a
 38  ;; shorter word or a single letter is also supported.  This package
 39  ;; implements diminished modes.
 40  
 41  ;; You can use this package either interactively or from your .emacs file.
 42  ;; In either case, first you'll need to copy this file to a directory that
 43  ;; appears in your load-path.  `load-path' is the name of a variable that
 44  ;; contains a list of directories Emacs searches for files to load.
 45  ;; To prepend another directory to load-path, put a line like
 46  ;; (add-to-list 'load-path "c:/My_Directory") in your .emacs file.
 47  
 48  ;; To create diminished modes interactively, type
 49  ;;   M-x load-library
 50  ;; to get a prompt like
 51  ;;   Load library:
 52  ;; and respond `diminish' (unquoted).  Then type
 53  ;;   M-x diminish
 54  ;; to get a prompt like
 55  ;;   Diminish what minor mode:
 56  ;; and respond with the name of some minor mode, like mouse-avoidance-mode.
 57  ;; You'll then get this prompt:
 58  ;;   To what mode-line display:
 59  ;; Respond by just hitting <Enter> if you want the name of the mode
 60  ;; completely removed from the mode line.  If you prefer, you can abbreviate
 61  ;; the name.  If your abbreviation is 2 characters or more, such as "Av",
 62  ;; it'll be displayed as a separate word on the mode line, just like minor
 63  ;; modes' names.  If it's a single character, such as "V", it'll be scrunched
 64  ;; up against the previous word, so for example if the undiminished mode line
 65  ;; display had been "Abbrev Fill Avoid", it would become "Abbrev FillV".
 66  ;; Multiple single-letter diminished modes will all be scrunched together.
 67  ;; The display of undiminished modes will not be affected.
 68  
 69  ;; To find out what the mode line would look like if all diminished modes
 70  ;; were still minor, type M-x diminished-modes.  This displays in the echo
 71  ;; area the complete list of minor or diminished modes now active, but
 72  ;; displays them all as minor.  They remain diminished on the mode line.
 73  
 74  ;; To convert a diminished mode back to a minor mode, type M-x diminish-undo
 75  ;; to get a prompt like
 76  ;;   Restore what diminished mode:
 77  ;; Respond with the name of some diminished mode.  To convert all
 78  ;; diminished modes back to minor modes, respond to that prompt
 79  ;; with `diminished-modes' (unquoted, & note the hyphen).
 80  
 81  ;; When you're responding to the prompts for mode names, you can use
 82  ;; completion to avoid extra typing; for example, m o u SPC SPC SPC
 83  ;; is usually enough to specify mouse-avoidance-mode.  Mode names
 84  ;; typically end in "-mode", but for historical reasons
 85  ;; auto-fill-mode is named by "auto-fill-function".
 86  
 87  ;; To create diminished modes noninteractively in your .emacs file, put
 88  ;; code like
 89  ;;   (require 'diminish)
 90  ;;   (diminish 'abbrev-mode "Abv")
 91  ;;   (diminish 'jiggle-mode)
 92  ;;   (diminish 'mouse-avoidance-mode "M")
 93  ;; near the end of your .emacs file.  It should be near the end so that any
 94  ;; minor modes your .emacs loads will already have been loaded by the time
 95  ;; they're to be converted to diminished modes.
 96  
 97  ;; To diminish a major mode, (setq mode-name "whatever") in the mode hook.
 98  
 99  ;;; Epigraph:
100  
101  ;;         "The quality of our thoughts is bordered on all sides
102  ;;          by our facility with language."
103  ;;               --J. Michael Straczynski
104  
105  ;;; Code:
106  
107  (eval-when-compile (require 'cl-lib))
108  
109  (defvar diminish-must-not-copy-minor-mode-alist nil
110    "Non-nil means loading diminish.el won't (copy-alist minor-mode-alist).
111  Normally `minor-mode-alist' is setq to that copy on loading diminish because
112  at least one of its cons cells, that for abbrev-mode, is read-only (see
113  ELisp Info on \"pure storage\").  If you setq this variable to t & then
114  try to diminish abbrev-mode under GNU Emacs 19.34, you'll get the error
115  message \"Attempt to modify read-only object\".")
116  
117  (or diminish-must-not-copy-minor-mode-alist
118      (cl-callf copy-alist minor-mode-alist))
119  
120  (defvar diminished-mode-alist nil
121    "The original `minor-mode-alist' value of all (diminish)ed modes.")
122  
123  (defvar diminish-history-symbols nil
124    "Command history for symbols of diminished modes.")
125  
126  (defvar diminish-history-names nil
127    "Command history for names of diminished modes.")
128  
129  ;; When we diminish a mode, we are saying we want it to continue doing its
130  ;; work for us, but we no longer want to be reminded of it.  It becomes a
131  ;; night worker, like a janitor; it becomes an invisible man; it remains a
132  ;; component, perhaps an important one, sometimes an indispensable one, of
133  ;; the mechanism that maintains the day-people's world, but its place in
134  ;; their thoughts is diminished, usually to nothing.  As we grow old we
135  ;; diminish more and more such thoughts, such people, usually to nothing.
136  
137  ;; "The wise man knows that to keep under is to endure."  The diminished
138  ;; often come to value their invisibility.  We speak--speak--of "the strong
139  ;; silent type", but only as a superficiality; a stereotype in a movie,
140  ;; perhaps, but even if an acquaintance, necessarily, by hypothesis, a
141  ;; distant one.  The strong silent type is actually a process.  It begins
142  ;; with introspection, continues with judgment, and is shaped by the
143  ;; discovery that these judgments are impractical to share; there is no
144  ;; appetite for the wisdom of the self-critical among the creatures of
145  ;; material appetite who dominate our world.  Their dominance's Darwinian
146  ;; implications reinforce the self-doubt that is the germ of higher wisdom.
147  ;; The thoughtful contemplate the evolutionary triumph of the predator.
148  ;; Gnostics deny the cosmos could be so evil; this must all be a prank; the
149  ;; thoughtful remain silent, invisible, self-diminished, and discover,
150  ;; perhaps at first in surprise, the freedom they thus gain, and grow strong.
151  
152  ;;;###autoload
153  (defun diminish (mode &optional to-what)
154    "Diminish mode-line display of minor mode MODE to TO-WHAT (default \"\").
155  
156  Interactively, enter (with completion) the name of any minor mode, followed
157  on the next line by what you want it diminished to (default empty string).
158  The response to neither prompt should be quoted.  However, in Lisp code,
159  both args must be quoted, the first as a symbol, the second as a string,
160  as in (diminish \\='jiggle-mode \" Jgl\").
161  
162  The mode-line displays of minor modes usually begin with a space, so
163  the modes' names appear as separate words on the mode line.  However, if
164  you're having problems with a cramped mode line, you may choose to use single
165  letters for some modes, without leading spaces.  Capitalizing them works
166  best; if you then diminish some mode to \"X\" but have `abbrev-mode' enabled as
167  well, you'll get a display like \"AbbrevX\".  This function prepends a space
168  to TO-WHAT if it's > 1 char long & doesn't already begin with a space."
169    (interactive (list (read (completing-read
170                              "Diminish what minor mode: "
171                              (mapcar (lambda (x) (list (symbol-name (car x))))
172                                      minor-mode-alist)
173                              nil t nil 'diminish-history-symbols))
174                       (read-from-minibuffer
175                        "To what mode-line display: "
176                        nil nil nil 'diminish-history-names)))
177    (let ((minor (assq mode minor-mode-alist)))
178      (when minor
179          (progn (cl-callf or to-what "")
180                 (when (and (stringp to-what)
181                            (> (length to-what) 1))
182                   (or (= (string-to-char to-what) ?\ )
183                       (cl-callf2 concat " " to-what)))
184                 (or (assq mode diminished-mode-alist)
185                     (push (copy-sequence minor) diminished-mode-alist))
186                 (setcdr minor (list to-what))))))
187  
188  ;; But an image comes to me, vivid in its unreality, of a loon alone on his
189  ;; forest lake, shrieking his soul out into a canopy of stars.  Alone this
190  ;; afternoon in my warm city apartment, I can feel the bite of his night air,
191  ;; and smell his conifers.  In him there is no acceptance of diminishment.
192  
193  ;; "I have a benevolent habit of pouring out myself to everybody,
194  ;;  and would even pay for a listener, and I am afraid
195  ;;  that the Athenians may think me too talkative."
196  ;;       --Socrates, in the /Euthyphro/
197  
198  ;; I remember a news story about a retired plumber who had somehow managed to
199  ;; steal a military tank.  He rode it down city streets, rode over a parked
200  ;; car--no one was hurt--rode onto a freeway, that concrete symbol of the
201  ;; American spirit, or so we fancy it, shouting "Plumber Bob!  Plumber Bob!".
202  ;; He was shot dead by police.
203  
204  ;;;###autoload
205  (defun diminish-undo (mode)
206    "Restore mode-line display of diminished mode MODE to its minor-mode value.
207  Do nothing if the arg is a minor mode that hasn't been diminished.
208  
209  Interactively, enter (with completion) the name of any diminished mode (a
210  mode that was formerly a minor mode on which you invoked \\[diminish]).
211  To restore all diminished modes to minor status, answer `diminished-modes'.
212  The response to the prompt shouldn't be quoted.  However, in Lisp code,
213  the arg must be quoted as a symbol, as in (diminish-undo \\='diminished-modes)."
214    (interactive
215     (list (read (completing-read
216                  "Restore what diminished mode: "
217                  (cons (list "diminished-modes")
218                        (mapcar (lambda (x) (list (symbol-name (car x))))
219                                diminished-mode-alist))
220                  nil t nil 'diminish-history-symbols))))
221    (if (eq mode 'diminished-modes)
222        (let ((diminished-modes diminished-mode-alist))
223          (while diminished-modes
224            (diminish-undo (caar diminished-modes))
225            (cl-callf cdr diminished-modes)))
226      (let ((minor      (assq mode      minor-mode-alist))
227            (diminished (assq mode diminished-mode-alist)))
228        (or minor
229            (error "%S is not currently registered as a minor mode" mode))
230        (when diminished
231          (setcdr minor (cdr diminished))))))
232  
233  ;; Plumber Bob was not from Seattle, my grey city, for rainy Seattle is a
234  ;; city of interiors, a city of the self-diminished.  When I moved here one
235  ;; sunny June I was delighted to find that ducks and geese were common in
236  ;; the streets.  But I hoped to find a loon or two, and all I found were
237  ;; ducks and geese.  I wondered about this; I wondered why there were no
238  ;; loons in Seattle; but my confusion resulted from my ignorance of the
239  ;; psychology of rain, which is to say my ignorance of diminished modes.
240  ;; What I needed, and lacked, was a way to discover they were there.
241  
242  ;;;###autoload
243  (defun diminished-modes ()
244    "Echo all active diminished or minor modes as if they were minor.
245  The display goes in the echo area; if it's too long even for that,
246  you can see the whole thing in the *Messages* buffer.
247  This doesn't change the status of any modes; it just lets you see
248  what diminished modes would be on the mode-line if they were still minor."
249    (interactive)
250    (let ((minor-modes minor-mode-alist)
251          message)
252      (while minor-modes
253        (when (symbol-value (caar minor-modes))
254          ;; This minor mode is active in this buffer
255          (let* ((mode-pair (car minor-modes))
256                 (mode (car mode-pair))
257                 (minor-pair (or (assq mode diminished-mode-alist) mode-pair))
258                 (minor-name (cadr minor-pair)))
259            (when (symbolp minor-name)
260              ;; This minor mode uses symbol indirection in the cdr
261              (let ((symbols-seen (list minor-name)))
262                (while (and (symbolp (cl-callf symbol-value minor-name))
263                            (not (memq minor-name symbols-seen)))
264                  (push minor-name symbols-seen))))
265            (push minor-name message)))
266        (cl-callf cdr minor-modes))
267      (setq message (mapconcat 'identity (nreverse message) ""))
268      (when (= (string-to-char message) ?\ )
269        (cl-callf substring message 1))
270      (message "%s" message)))
271  
272  ;; A human mind is a Black Forest of diminished modes.  Some are dangerous;
273  ;; most of the mind of an intimate is a secret stranger, and these diminished
274  ;; modes are rendered more unpredictable by their long isolation from the
275  ;; corrective influence of interaction with reality.  The student of history
276  ;; learns that this description applies to whole societies as well.  In some
277  ;; ways the self-diminished are better able to discern the night worker.
278  ;; They are rendered safer by their heightened awareness of others'
279  ;; diminished modes, and more congenial by the spare blandness of their own
280  ;; mode lines.  To some people rain is truly depressing, but others it just
281  ;; makes pensive, and, forcing them indoors where they may not have the
282  ;; luxury of solitude, teaches them to self-diminish.  That was what I had
283  ;; not understood when I was searching for loons among the ducks and geese.
284  ;; Loons come to Seattle all the time, but the ones that like it learn to be
285  ;; silent, learn to self-diminish, and take on the colors of ducks and geese.
286  ;; Now, here a dozen years, I can recognize them everywhere, standing quietly
287  ;; in line with the ducks and geese at the espresso counter, gazing placidly
288  ;; out on the world through loon-red eyes, thinking secret thoughts.
289  
290  (provide 'diminish)
291  
292  ;;; diminish.el ends here