dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

diminish.el (14810B)


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