annotate lisp/easy-mmode.el @ 5750:66d2f63df75f

Correct some spelling and formatting in behavior.el. Mentioned in tracker issue 826, the third thing mentioned there (the file name at the bottom of the file) had already been fixed. lisp/ChangeLog addition: 2013-08-05 Aidan Kehoe <kehoea@parhasard.net> * behavior.el: (override-behavior): Correct some spelling and formatting here, thank you Steven Mitchell in tracker issue 826.
author Aidan Kehoe <kehoea@parhasard.net>
date Mon, 05 Aug 2013 10:05:32 +0100
parents 308d34e9f07d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2548
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
1 ;;; easy-mmode.el --- easy definition for major and minor modes
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
2
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
3 ;; Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
4
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
5 ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
7
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
8 ;; Keywords: extensions lisp
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
9
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
10 ;; This file is part of XEmacs.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
11
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
12 ;; XEmacs is free software: you can redistribute it and/or modify it
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
13 ;; under the terms of the GNU General Public License as published by the
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
14 ;; Free Software Foundation, either version 3 of the License, or (at your
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
15 ;; option) any later version.
2548
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
16
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
20 ;; for more details.
2548
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
21
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>.
2548
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
24
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
25 ;;; Synched up with: GNU Emacs 21.3.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
26
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
27 ;;; Commentary:
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
28
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
29 ;; Minor modes are useful and common. This package makes defining a
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
30 ;; minor mode easy, by focusing on the writing of the minor mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
31 ;; functionalities themselves. Moreover, this package enforces a
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
32 ;; conventional naming of user interface primitives, making things
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
33 ;; natural for the minor-mode end-users.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
34
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
35 ;; For each mode, easy-mmode defines the following:
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
36 ;; <mode> : The minor mode predicate. A buffer-local variable.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
37 ;; <mode>-map : The keymap possibly associated to <mode>.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
38 ;; <mode>-hook,<mode>-on-hook,<mode>-off-hook and <mode>-mode:
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
39 ;; see `define-minor-mode' documentation
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
40 ;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
41 ;; eval
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
42 ;; (pp (macroexpand '(define-minor-mode <your-mode> <doc>)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
43 ;; to check the result before using it.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
44
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
45 ;; The order in which minor modes are installed is important. Keymap
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
46 ;; lookup proceeds down minor-mode-map-alist, and the order there
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
47 ;; tends to be the reverse of the order in which the modes were
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
48 ;; installed. Perhaps there should be a feature to let you specify
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
49 ;; orderings.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
50
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
51 ;; Additionally to `define-minor-mode', the package provides convenient
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
52 ;; ways to define keymaps, and other helper functions for major and minor
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
53 ;; modes.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
54
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
55 ;;; Code:
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
56
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
57 (eval-when-compile (require 'cl))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
58
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
59 ;;; This file uses two functions that did not exist in some versions of
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
60 ;;; XEmacs: propertize and replace-regexp-in-string. We provide these
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
61 ;;; functions here for such XEmacsen.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
62 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
63 ;;; FIXME: These function definitions should go into the future or
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
64 ;;; forward-compat package, once that package exists.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
65
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
66 ;; XEmacs <= 21.4 does not have propertize, but XEmacs >= 21.5 dumps it (it is
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
67 ;; defined in subr.el). Therefore, it is either defined regardless of what
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
68 ;; has been loaded already, or it won't be defined regardless of what is
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
69 ;; loaded.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
70 (if (not (fboundp 'propertize))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
71 (defun propertize (string &rest properties)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
72 "Return a copy of STRING with text properties added.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
73 First argument is the string to copy.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
74 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
75 properties to add to the result."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
76 (let ((str (copy-sequence string)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
77 (add-text-properties 0 (length str)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
78 properties
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
79 str)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
80 str)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
81
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
82 ;; XEmacs <= 21.4 does not have replace-regexp-in-string, but XEmacs >= 21.5
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
83 ;; dumps it (it is defined in subr.el). Therefore, it is either defined
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
84 ;; regardless of what has been loaded already, or it won't be defined
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
85 ;; regardless of what is loaded.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
86 (if (not (fboundp 'replace-regexp-in-string))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
87 (defun replace-regexp-in-string (regexp rep string &optional
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
88 fixedcase literal subexp start)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
89 "Replace all matches for REGEXP with REP in STRING.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
90
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
91 Return a new string containing the replacements.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
92
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
93 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
94 arguments with the same names of function `replace-match'. If START
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
95 is non-nil, start replacements at that index in STRING.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
96
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
97 REP is either a string used as the NEWTEXT arg of `replace-match' or a
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
98 function. If it is a function it is applied to each match to generate
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
99 the replacement passed to `replace-match'; the match-data at this
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
100 point are such that match 0 is the function's argument.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
101
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
102 To replace only the first match (if any), make REGEXP match up to \\'
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
103 and replace a sub-expression, e.g.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
104 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
105 => \" bar foo\"
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
106 "
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
107 (let ((l (length string))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
108 (start (or start 0))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
109 matches str mb me)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
110 (save-match-data
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
111 (while (and (< start l) (string-match regexp string start))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
112 (setq mb (match-beginning 0)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
113 me (match-end 0))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
114 ;; If we matched the empty string, make sure we advance by one char
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
115 (when (= me mb) (setq me (min l (1+ mb))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
116 ;; Generate a replacement for the matched substring.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
117 ;; Operate only on the substring to minimize string consing.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
118 ;; Set up match data for the substring for replacement;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
119 ;; presumably this is likely to be faster than munging the
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
120 ;; match data directly in Lisp.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
121 (string-match regexp (setq str (substring string mb me)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
122 (setq matches
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
123 (cons (replace-match (if (stringp rep)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
124 rep
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
125 (funcall rep (match-string 0 str)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
126 fixedcase literal str subexp)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
127 (cons (substring string start mb) ; unmatched prefix
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
128 matches)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
129 (setq start me))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
130 ;; Reconstruct a string from the pieces.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
131 (setq matches (cons (substring string start l) matches)) ; leftover
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
132 (apply #'concat (nreverse matches))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
133
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
134
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
135 (defun easy-mmode-pretty-mode-name (mode &optional lighter)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
136 "Turn the symbol MODE into a string intended for the user.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
137 If provided LIGHTER will be used to help choose capitalization."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
138 (let* ((case-fold-search t)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
139 (name (concat (replace-regexp-in-string
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
140 "-Minor" " minor"
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
141 (capitalize (replace-regexp-in-string
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
142 "-mode\\'" "" (symbol-name mode))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
143 " mode")))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
144 (if (not (stringp lighter)) name
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
145 (setq lighter
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
146 (replace-regexp-in-string "\\`\\s-+\\|\\-s+\\'" "" lighter))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
147 (replace-regexp-in-string lighter lighter name t t))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
148
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
149 ;; XEmacs change: add -on-hook, -off-hook, and macro parameter documentation.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
150 ;;;###no-autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
151 (defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
152 ;;;###no-autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
153 (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
154 "Define a new minor mode MODE.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
155 This function defines the associated control variable MODE, keymap MODE-map,
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
156 toggle command MODE, and hook MODE-hook.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
157
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
158 DOC is the documentation for the mode toggle command.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
159 Optional INIT-VALUE is the initial value of the mode's variable.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
160 Optional LIGHTER is displayed in the modeline when the mode is on.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
161 Optional KEYMAP is the default (defvar) keymap bound to the mode keymap.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
162 If it is a list, it is passed to `easy-mmode-define-keymap'
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
163 in order to build a valid keymap. It's generally better to use
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
164 a separate MODE-map variable than to use this argument.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
165 The above three arguments can be skipped if keyword arguments are
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
166 used (see below).
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
167
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
168 BODY contains code that will be executed each time the mode is (de)activated.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
169 It will be executed after any toggling but before running the hooks.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
170 Before the actual body code, you can write
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
171 keyword arguments (alternating keywords and values).
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
172 These following keyword arguments are supported:
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
173 :group GROUP Custom group name to use in all generated `defcustom' forms.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
174 :global GLOBAL If non-nil specifies that the minor mode is not meant to be
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
175 buffer-local, so don't make the variable MODE buffer-local.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
176 By default, the mode is buffer-local.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
177 :init-value VAL Same as the INIT-VALUE argument.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
178 :lighter SPEC Same as the LIGHTER argument.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
179 :require SYM Same as in `defcustom'.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
180
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
181 For backwards compatibility, these hooks are run each time the mode is
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
182 \(de)activated. When the mode is toggled, MODE-hook is always run before the
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
183 other hook.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
184 MODE-hook: run if the mode is toggled.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
185 MODE-on-hook: run if the mode is activated.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
186 MODE-off-hook: run if the mode is deactivated.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
187
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
188 \(defmacro easy-mmode-define-minor-mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
189 (MODE DOC &optional INIT-VALUE LIGHTER KEYMAP &rest BODY)...\)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
190
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
191 For example, you could write
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
192 (define-minor-mode foo-mode \"If enabled, foo on you!\"
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
193 nil \"Foo \" foo-keymap
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
194 :require 'foo :global t :group 'inconvenience
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
195 ...BODY CODE...)"
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
196
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
197 ;; Allow skipping the first three args.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
198 (cond
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
199 ((keywordp init-value)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
200 (setq body (list* init-value lighter keymap body)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
201 init-value nil lighter nil keymap nil))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
202 ((keywordp lighter)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
203 (setq body (list* lighter keymap body) lighter nil keymap nil))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
204 ((keywordp keymap) (push keymap body) (setq keymap nil)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
205
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
206 (let* ((mode-name (symbol-name mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
207 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
208 (globalp nil)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
209 (group nil)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
210 (extra-args nil)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
211 (require t)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
212 (keymap-sym (if (and keymap (symbolp keymap)) keymap
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
213 (intern (concat mode-name "-map"))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
214 (hook (intern (concat mode-name "-hook")))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
215 (hook-on (intern (concat mode-name "-on-hook")))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
216 (hook-off (intern (concat mode-name "-off-hook"))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
217
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
218 ;; Check keys.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
219 (while (keywordp (car body))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
220 (case (pop body)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
221 (:init-value (setq init-value (pop body)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
222 (:lighter (setq lighter (pop body)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
223 (:global (setq globalp (pop body)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
224 (:extra-args (setq extra-args (pop body)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
225 (:group (setq group (nconc group (list :group (pop body)))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
226 (:require (setq require (pop body)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
227 (t (pop body))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
228
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
229 (unless group
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
230 ;; We might as well provide a best-guess default group.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
231 (setq group
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
232 `(:group ',(or (custom-current-group)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
233 (intern (replace-regexp-in-string
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
234 "-mode\\'" "" mode-name))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
235 ;; Add default properties to LIGHTER.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
236 ;; #### FSF comments this out in 21.3.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
237 ; (unless (or (not (stringp lighter))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
238 ; (get-text-property 0 'local-map lighter)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
239 ; (get-text-property 0 'keymap lighter))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
240 ; (setq lighter
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
241 ; (propertize lighter
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
242 ; 'local-map modeline-minor-mode-map ; XEmacs change
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
243 ; 'help-echo "mouse-3: minor mode menu")))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
244
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
245 `(progn
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
246 ;; Define the variable to enable or disable the mode.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
247 ,(if (not globalp)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
248 `(progn
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
249 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
250 Use the command `%s' to change this variable." pretty-name mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
251 (make-variable-buffer-local ',mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
252
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
253 (let ((curfile (or (and (boundp 'byte-compile-current-file)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
254 byte-compile-current-file)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
255 load-file-name)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
256 `(defcustom ,mode ,init-value
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
257 ,(format "Non-nil if %s is enabled.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
258 See the command `%s' for a description of this minor-mode.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
259 Setting this variable directly does not take effect;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
260 use either \\[customize] or the function `%s'."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
261 pretty-name mode mode)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
262 :set (lambda (symbol value) (funcall symbol (or value 0)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
263 :initialize 'custom-initialize-default
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
264 ,@group
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
265 :type 'boolean
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
266 ,@(cond
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
267 ((not (and curfile require)) nil)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
268 ((not (eq require t)) `(:require ,require))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
269 (t `(:require
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
270 ',(intern (file-name-nondirectory
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
271 (file-name-sans-extension curfile)))))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
272
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
273 ;; The actual function.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
274 (defun ,mode (&optional arg ,@extra-args)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
275 ,(or doc
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
276 (format (concat "Toggle %s on or off.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
277 Interactively, with no prefix argument, toggle the mode.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
278 With universal prefix ARG turn mode on.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
279 With zero or negative ARG turn mode off.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
280 \\{%s}") pretty-name keymap-sym))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
281 ;; Use `toggle' rather than (if ,mode 0 1) so that using
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
282 ;; repeat-command still does the toggling correctly.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
283 (interactive (list (or current-prefix-arg 'toggle)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
284 ;; XEmacs addition: save the old mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
285 (let ((old-mode ,mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
286 (setq ,mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
287 (cond
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
288 ((eq arg 'toggle) (not ,mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
289 (arg (or (listp arg);; XEmacs addition: C-u alone
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
290 (> (prefix-numeric-value arg) 0)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
291 (t
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
292 (if (null ,mode) t
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
293 (message
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
294 "Toggling %s off; better pass an explicit argument."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
295 ',mode)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
296 nil))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
297 ,@body
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
298 ;; The on/off hooks are here for backward compatibility only.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
299 ;; The on/off hooks are here for backward compatibility only.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
300 ;; XEmacs change: check mode before running hooks
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
301 (and ,hook
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
302 (not (equal old-mode ,mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
303 (run-hooks ',hook))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
304 (and ,hook-on
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
305 ,mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
306 (run-hooks ',hook-on))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
307 (and ,hook-off
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
308 (not ,mode)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
309 (run-hooks ',hook-off)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
310 (if (interactive-p)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
311 (progn
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
312 ,(if globalp `(customize-mark-as-set ',mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
313 (message ,(format "%s %%sabled" pretty-name)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
314 (if ,mode "en" "dis"))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
315 (force-mode-line-update)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
316 ;; Return the new setting.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
317 ,mode)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
318
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
319 ;; Autoloading an easy-mmode-define-minor-mode autoloads
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
320 ;; everything up-to-here.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
321 ;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
322 ;; XEmacs change: XEmacs does not support :autoload-end. On the other
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
323 ;; hand, I don't see why we need to support it. An autoload cookie
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
324 ;; just before a (define-minor-mode foo) form will generate an autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
325 ;; form for the file with name foo. But that's exactly right, since
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
326 ;; the defun created just above here has the name foo. There are no
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
327 ;; other top-level forms created above here by the macro, so we're done.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
328 ;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
329 ;;:autoload-end
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
330
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
331 ;; The toggle's hook.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
332 (defcustom ,hook nil
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
333 ,(format "Hook run at the end of function `%s'." mode-name)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
334 ,@group
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
335 :type 'hook)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
336
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
337 ;; XEmacs addition: declare the on and off hooks also
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
338 (defcustom ,hook-on nil
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
339 ,(format "Hook to run when entering %s." mode-name)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
340 :group ,(cadr group)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
341 :type 'hook)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
342
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
343 (defcustom ,hook-off nil
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
344 ,(format "Hook to run when exiting %s." mode-name)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
345 :group ,(cadr group)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
346 :type 'hook)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
347
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
348 ;; Define the minor-mode keymap.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
349 ,(unless (symbolp keymap) ;nil is also a symbol.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
350 `(defvar ,keymap-sym
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
351 (let ((m ,keymap))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
352 (cond ((keymapp m) m)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
353 ((listp m) (easy-mmode-define-keymap m))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
354 (t (error "Invalid keymap %S" ,keymap))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
355 ,(format "Keymap for `%s'." mode-name)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
356
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
357 (add-minor-mode ',mode ',lighter
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
358 ,(if keymap keymap-sym
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
359 `(if (boundp ',keymap-sym)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
360 (symbol-value ',keymap-sym)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
361 ;; XEmacs change: supply the AFTER and TOGGLE-FUN args
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
362 t ',mode)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
363
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
364 ;; If the mode is global, call the function according to the default.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
365 ,(if globalp
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
366 `(if (and load-file-name (not (equal ,init-value ,mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
367 ;; XEmacs addition:
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
368 (not purify-flag))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
369 (eval-after-load load-file-name '(,mode (if ,mode 1 -1))))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
370
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
371 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
372 ;;; make global minor mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
373 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
374
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
375 ;;;###no-autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
376 (defmacro easy-mmode-define-global-mode (global-mode mode turn-on
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
377 &rest keys)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
378 "Make GLOBAL-MODE out of the buffer-local minor MODE.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
379 TURN-ON is a function that will be called with no args in every buffer
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
380 and that should try to turn MODE on if applicable for that buffer.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
381 KEYS is a list of CL-style keyword arguments:
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
382 :group to specify the custom group."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
383 (let* ((global-mode-name (symbol-name global-mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
384 (pretty-name (easy-mmode-pretty-mode-name mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
385 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
386 (group nil)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
387 (extra-args nil)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
388 (buffers (intern (concat global-mode-name "-buffers")))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
389 (cmmh (intern (concat global-mode-name "-cmmh"))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
390
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
391 ;; Check keys.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
392 (while (keywordp (car keys))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
393 (case (pop keys)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
394 (:extra-args (setq extra-args (pop keys)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
395 (:group (setq group (nconc group (list :group (pop keys)))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
396 (t (setq keys (cdr keys)))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
397
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
398 (unless group
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
399 ;; We might as well provide a best-guess default group.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
400 (setq group
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
401 `(:group ',(or (custom-current-group)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
402 (intern (replace-regexp-in-string
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
403 "-mode\\'" "" (symbol-name mode)))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
404
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
405 `(progn
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
406 ;; The actual global minor-mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
407 (define-minor-mode ,global-mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
408 ,(format "Toggle %s in every buffer.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
409 With prefix ARG, turn %s on if and only if ARG is positive.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
410 %s is actually not turned on in every buffer but only in those
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
411 in which `%s' turns it on."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
412 pretty-name pretty-global-name pretty-name turn-on)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
413 :global t :extra-args ,extra-args ,@group
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
414
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
415 ;; Setup hook to handle future mode changes and new buffers.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
416 (if ,global-mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
417 ;; XEmacs: find-file-hooks not find-file-hook
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
418 (progn
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
419 (add-hook 'find-file-hooks ',buffers)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
420 (add-hook 'change-major-mode-hook ',cmmh))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
421 (remove-hook 'find-file-hooks ',buffers)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
422 (remove-hook 'change-major-mode-hook ',cmmh))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
423
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
424 ;; Go through existing buffers.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
425 (dolist (buf (buffer-list))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
426 (with-current-buffer buf
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
427 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
428
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
429 ;; TODO: XEmacs does not support :autoload-end
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
430 ;; Autoloading easy-mmode-define-global-mode
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
431 ;; autoloads everything up-to-here.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
432 :autoload-end
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
433
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
434 ;; List of buffers left to process.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
435 (defvar ,buffers nil)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
436
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
437 ;; The function that calls TURN-ON in each buffer.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
438 (defun ,buffers ()
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
439 (remove-hook 'post-command-hook ',buffers)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
440 (while ,buffers
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
441 (let ((buf (pop ,buffers)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
442 (when (buffer-live-p buf)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
443 (with-current-buffer buf (,turn-on))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
444 (put ',buffers 'definition-name ',global-mode)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
445
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
446 ;; The function that catches kill-all-local-variables.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
447 (defun ,cmmh ()
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
448 (add-to-list ',buffers (current-buffer))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
449 (add-hook 'post-command-hook ',buffers))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
450 (put ',cmmh 'definition-name ',global-mode))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
451
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
452 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
453 ;;; easy-mmode-defmap
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
454 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
455
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
456 (if (fboundp 'set-keymap-parents)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
457 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
458 (defun easy-mmode-set-keymap-parents (m parents)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
459 (set-keymap-parent
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
460 m
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
461 (cond
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
462 ((not (consp parents)) parents)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
463 ((not (cdr parents)) (car parents))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
464 (t (let ((m (copy-keymap (pop parents))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
465 (easy-mmode-set-keymap-parents m parents)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
466 m))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
467
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
468 ;;;###no-autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
469 (defun easy-mmode-define-keymap (bs &optional name m args)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
470 "Return a keymap built from bindings BS.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
471 BS must be a list of (KEY . BINDING) where
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
472 KEY and BINDINGS are suitable for `define-key'.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
473 Optional NAME is passed to `make-sparse-keymap'.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
474 Optional map M can be used to modify an existing map.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
475 ARGS is a list of additional keyword arguments."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
476 (let (inherit dense ;suppress
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
477 )
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
478 (while args
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
479 (let ((key (pop args))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
480 (val (pop args)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
481 (case key
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
482 (:name (setq name val))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
483 (:dense (setq dense val))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
484 (:inherit (setq inherit val))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
485 (:group)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
486 ;;((eq key :suppress) (setq suppress val))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
487 (t (message "Unknown argument %s in defmap" key)))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
488 (unless (keymapp m)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
489 (setq bs (append m bs))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
490 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
491 (dolist (b bs)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
492 (let ((keys (car b))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
493 (binding (cdr b)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
494 (dolist (key (if (consp keys) keys (list keys)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
495 (cond
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
496 ((symbolp key)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
497 (substitute-key-definition key binding m global-map))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
498 ((null binding)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
499 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
500 ((let ((o (lookup-key m key)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
501 (or (null o) (numberp o) (eq o 'undefined)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
502 (define-key m key binding))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
503 (cond
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
504 ((keymapp inherit) (set-keymap-parent m inherit))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
505 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
506 m))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
507
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
508 ;;;###no-autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
509 (defmacro easy-mmode-defmap (m bs doc &rest args)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
510 `(defconst ,m
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
511 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
512 ,doc))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
513
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
514
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
515 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
516 ;;; easy-mmode-defsyntax
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
517 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
518
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
519 (defun easy-mmode-define-syntax (css args)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
520 (let ((st (make-syntax-table (plist-get args :copy)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
521 (parent (plist-get args :inherit)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
522 (dolist (cs css)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
523 (let ((char (car cs))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
524 (syntax (cdr cs)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
525 (if (sequencep char)
4783
e29fcfd8df5f Eliminate most core code byte-compile warnings.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2548
diff changeset
526 (mapc (lambda (c) (modify-syntax-entry c syntax st)) char)
2548
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
527 (modify-syntax-entry char syntax st))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
528 ;; XEmacs change: we do not have set-char-table-parent
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
529 (if parent (derived-mode-merge-syntax-tables
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
530 (if (symbolp parent) (symbol-value parent) parent) st))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
531 st))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
532
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
533 ;;;###no-autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
534 (defmacro easy-mmode-defsyntax (st css doc &rest args)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
535 "Define variable ST as a syntax-table.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
536 CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
537 `(progn
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
538 (autoload 'easy-mmode-define-syntax "easy-mmode")
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
539 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
540
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
541
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
542
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
543 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
544 ;;; easy-mmode-define-navigation
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
545 ;;;
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
546
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
547 ;; XEmacs change: autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
548 ;;;###no-autoload
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
549 (defmacro easy-mmode-define-navigation (base re &optional name endfun)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
550 "Define BASE-next and BASE-prev to navigate in the buffer.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
551 RE determines the places the commands should move point to.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
552 NAME should describe the entities matched by RE. It is used to build
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
553 the docstrings of the two functions.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
554 BASE-next also tries to make sure that the whole entry is visible by
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
555 searching for its end (by calling ENDFUN if provided or by looking for
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
556 the next entry) and recentering if necessary.
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
557 ENDFUN should return the end position (with or without moving point)."
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
558 (let* ((base-name (symbol-name base))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
559 (prev-sym (intern (concat base-name "-prev")))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
560 (next-sym (intern (concat base-name "-next"))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
561 (unless name (setq name (symbol-name base-name)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
562 `(progn
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
563 (add-to-list 'debug-ignored-errors
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
564 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
565 (defun ,next-sym (&optional count)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
566 ,(format "Go to the next COUNT'th %s." name)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
567 (interactive)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
568 (unless count (setq count 1))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
569 (if (< count 0) (,prev-sym (- count))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
570 (if (looking-at ,re) (incf count))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
571 (if (not (re-search-forward ,re nil t count))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
572 (if (looking-at ,re)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
573 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
574 (error ,(format "No next %s" name)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
575 (goto-char (match-beginning 0))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
576 (when (and (eq (current-buffer) (window-buffer (selected-window)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
577 (interactive-p))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
578 (let ((endpt (or (save-excursion
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
579 ,(if endfun `(,endfun)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
580 `(re-search-forward ,re nil t 2)))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
581 (point-max))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
582 ;; XEmacs change: versions < 21.5.16 have a
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
583 ;; pos-visible-in-window-p that takes only 2 parameters
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
584 (unless
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
585 (if (eq (function-max-args #'pos-visible-in-window-p) 2)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
586 (pos-visible-in-window-p endpt nil)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
587 (pos-visible-in-window-p endpt nil t))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
588 (recenter '(0))))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
589 (defun ,prev-sym (&optional count)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
590 ,(format "Go to the previous COUNT'th %s" (or name base-name))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
591 (interactive)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
592 (unless count (setq count 1))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
593 (if (< count 0) (,next-sym (- count))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
594 (unless (re-search-backward ,re nil t count)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
595 (error ,(format "No previous %s" name))))))))
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
596
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
597 (provide 'easy-mmode)
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
598
c4c8a36043be [xemacs-hg @ 2005-02-03 07:11:19 by ben]
ben
parents:
diff changeset
599 ;;; easy-mmode.el ends here