1 ;;; ts-mode.el --- Insert time-stamps in buffers
3 ;; Copyright (C) 1998, 2002 by Free Software Foundation, Inc.
5 ;; Author: Stephen J. Turnbull <stephen@xemacs.org>
6 ;; Keywords: minor mode
8 ;; Created: Sun Apr 5 19:49:36 1998
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your
15 ;; option) any later version.
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; if not, write to the Free Software Foundation,
24 ;; Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; Insert a timestamp in a buffer, implemented as a minor mode.
29 ;; Written to try out some techniques in the implementation of minor
30 ;; modes, in particular courteous key-binding.
38 ;; 1998-04-05 Stephen Turnbull <stephen@xemacs.org>
39 ;; tm-mode.el: created
45 ;;; User customization variables
47 ;; #### Is this really worth cluttering up Customize with?
48 ;;(defgroup 'time-stamp nil)
50 ;; #### Provide Customize interface someday....
51 (defvar ts-mode-prefix '[(control ?c) (?\$)]
52 "*Prefix key sequence for ts-mode command keys.
54 After loading, change the mode's prefix by using ts-mode-set-prefix;
55 setq'ing this variable can't work.")
57 ;; A convention for modes; here honored by observance, not breach.
58 (defvar ts-mode-hook nil
59 "*If you can think of a use for this, you're more clever than I.")
61 ;; Auxiliary customizations
63 (defcustom ts-conflict-warning "Binding conflict: %s -> %s."
64 "*Format string warning about key sequences with conflicting bindings.
66 Must contain two `%s' descriptors. The first formats the key sequence,
67 the second the description of the existing binding."
72 (defcustom ts-warn-conflict-verbosity 3
73 "*Controls verbosity of binding conflict warnings.
75 0 turns off warnings entirely.
76 1 issues a warning for each binding conflict (including sub-keymaps).
77 2 prints a summary message with a count of conflicts (not including
78 sub-keymaps, only keys in those maps that have conflicts).
79 3 adds verbose detail about what is being done.
81 Each positive level performs all actions of lower levels."
86 ;;; The basic mode conventions.
88 ;; Mode flag and keymaps
90 (defvar ts-mode nil "Activation flag for ts-mode.")
92 (defvar ts-mode-submap nil
93 "Sub-keymap for ts-mode.
95 All key sequences are prefixed by the sequence defined in ts-mode-map.")
97 (defvar ts-mode-map nil
98 "Keymap for ts-mode. Holds the prefix key for ts-mode functions.
100 Its value will be installed into minor-mode-map-alist. Prefix cannot
101 be altered by setq'ing ts-mode-map. Use ts-mode-set-prefix instead.")
105 (defun ts-mode (&optional arg)
106 "Minor mode for inserting time stamps in buffers.
108 An example minor mode.
114 ;; ts-warn-binding-conflicts doesn't make sense in the mode
116 (ts-warn-binding-conflicts ts-mode-map))
117 (setq ts-mode (if (null arg) (not ts-mode)
118 (> (prefix-numeric-value arg) 0)))
119 (run-hooks ts-mode-hook))
121 ;;; Internal mode data
125 (setq ts-mode-submap (make-sparse-keymap))
126 (define-prefix-command 'ts-mode-submap 'ts-mode-submap)
127 (define-key ts-mode-submap ?T 'ts-timestamp)
128 (define-key ts-mode-submap ?s 'ts-timestamp)
133 ;; Set the mode prefix
135 ;; This can't be done simply by setq'ing ts-mode-map; minor-mode-map-alist
136 ;; does not refer to that variable but contains a copy of its value.
138 (defun ts-mode-set-prefix (key &optional context)
139 "Set the prefix key sequence for ts-mode to KEY.
141 Return the new ts-mode-map. When called interactively read KEY from
142 the minibuffer (as a string; keys not bound to `self-insert' must be
143 quoted with ^Q). If you need more flexibility than ASCII gives, you'll
144 have to use the `eval-expression' interface.
146 Allowed values of CONTEXT:
147 NIL substitute a map containing KEY in minor-mode-map-alist.
148 adding-minor-mode manipulation of minor-mode-map-alist is done elsewhere."
149 ;; Should read key events but I don't know how to make that work.
150 (interactive "Key sequence (quote control characters with ^Q): ")
151 (setq ts-mode-map (make-sparse-keymap))
152 (define-key ts-mode-map key 'ts-mode-submap)
153 (cond ((null context)
154 (let ((slot (assq 'ts-mode minor-mode-map-alist)))
155 (setq minor-mode-map-alist
156 (cons (cons 'ts-mode ts-mode-map)
158 (delete slot minor-mode-map-alist)
159 minor-mode-map-alist)))))
160 ((equal context 'adding-minor-mode))
161 (t (error "Illegal context `%s' in ts-mode-set-prefix." context)))
164 ;; Deal with binding conflicts
166 ;; Search keymaps for binding conflicts for each key in the mode's keymap.
167 ;; Current implementation searches only active keymaps; it won't tell
168 ;; about inactive keymaps, including those of minor modes that might be
169 ;; invoked later or (worse) major modes already invoked in other buffers.
171 (defun ts-warn-binding-conflicts (map)
172 "Warn about key bindings that will conflict with those in MAP.
174 Results will be non-sensical if MAP is invoked via a prefix or is
175 already active. The current implementation only looks in the active
176 keymaps. Maps of inactive minor modes and local maps major modes of
177 other buffers will not be searched (although the latter will be shadowed
178 since ts-mode is a global variable)."
180 (if (> ts-warn-conflict-verbosity 1)
182 (message "Checking for conflicting bindings...")
183 (if (> ts-warn-conflict-verbosity 2)
184 (message "Examining accessible maps of map:\n `%s'" map))))
185 ;; A map is accessible from itself
186 (mapcar (lambda (slot)
187 (let ((prefix (car slot))
189 (if (> ts-warn-conflict-verbosity 2)
190 (message "Examining keys of map:\n `%s'" map))
191 (map-keymap (lambda (key binding)
192 (let* ((key (vconcat prefix (vector key)))
193 (binding (key-binding key)))
195 (> ts-warn-conflict-verbosity 0))
197 (if (not (keymapp binding))
198 (setq found (1+ found)))
199 (message ts-conflict-warning
202 (accessible-keymaps map))
203 (if (> ts-warn-conflict-verbosity 1)
204 (message "Checking for conflicting bindings...done%s"
206 (format ". Found %d." found)
209 ;;; Define a trivial command for a trivial example mode.
211 (defun ts-timestamp ()
212 "Insert the current time string in the current buffer at point."
214 (insert (current-time-string)))
216 ;;; Register the mode with XEmacs
218 (add-minor-mode 'ts-mode
220 (ts-mode-set-prefix ts-mode-prefix 'adding-minor-mode))
222 ;;; end of ts-mode.el