Remove non-free old and crusty clearcase pkg
[packages] / mule-packages / edict / ts-mode.el
1 ;;; ts-mode.el --- Insert time-stamps in buffers
2
3 ;; Copyright (C) 1998, 2002 by Free Software Foundation, Inc.
4
5 ;; Author:      Stephen J. Turnbull <stephen@xemacs.org>
6 ;; Keywords:    minor mode
7 ;; Version:     1.0
8 ;; Created:     Sun Apr  5 19:49:36 1998
9
10 ;;   This file is part of XEmacs.
11
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.
16
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.
21 ;; 
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.
25
26 ;;; Commentary:
27
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.
31
32 ;;; To do:
33
34 ;; 1.  Support Custom.
35
36 ;;; Changelog:
37
38 ;; 1998-04-05  Stephen Turnbull  <stephen@xemacs.org>
39 ;;        tm-mode.el:  created
40
41 ;;; Code
42
43 (provide 'ts-mode)
44
45 ;;; User customization variables
46
47 ;; #### Is this really worth cluttering up Customize with?
48 ;;(defgroup 'time-stamp nil)
49
50 ;; #### Provide Customize interface someday....
51 (defvar ts-mode-prefix '[(control ?c) (?\$)]
52   "*Prefix key sequence for ts-mode command keys.
53
54 After loading, change the mode's prefix by using ts-mode-set-prefix;
55 setq'ing this variable can't work.")
56
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.")
60
61 ;; Auxiliary customizations
62
63 (defcustom ts-conflict-warning "Binding conflict: %s -> %s."
64   "*Format string warning about key sequences with conflicting bindings.
65
66 Must contain two `%s' descriptors.  The first formats the key sequence,
67 the second the description of the existing binding."
68   :type 'string
69 ;  :group 'time-stamp
70   )
71
72 (defcustom ts-warn-conflict-verbosity 3
73   "*Controls verbosity of binding conflict warnings.
74
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.
80
81 Each positive level performs all actions of lower levels."
82   :type 'integer
83 ;  :group 'time-stamp
84   )
85
86 ;;; The basic mode conventions.
87
88 ;; Mode flag and keymaps
89 ;;
90 (defvar ts-mode nil "Activation flag for ts-mode.")
91
92 (defvar ts-mode-submap nil
93   "Sub-keymap for ts-mode.
94
95 All key sequences are prefixed by the sequence defined in ts-mode-map.")
96
97 (defvar ts-mode-map nil
98   "Keymap for ts-mode.  Holds the prefix key for ts-mode functions.
99
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.")
102
103 ;; Mode toggle
104 ;;
105 (defun ts-mode (&optional arg)
106   "Minor mode for inserting time stamps in buffers.
107
108 An example minor mode.
109
110 \\{ts-mode-map}"
111
112   (interactive "P")
113
114   ;; ts-warn-binding-conflicts doesn't make sense in the mode
115   (if (null ts-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))
120
121 ;;; Internal mode data
122
123 (or ts-mode-submap
124     (progn
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)
129      ))
130
131 ;;; Helper functions
132
133 ;; Set the mode prefix
134 ;;
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.
137 ;;
138 (defun ts-mode-set-prefix (key &optional context)
139   "Set the prefix key sequence for ts-mode to KEY.
140
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. 
145
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)
157                        (if slot
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)))
162   ts-mode-map)
163
164 ;; Deal with binding conflicts
165 ;;
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.
170 ;;
171 (defun ts-warn-binding-conflicts (map)
172   "Warn about key bindings that will conflict with those in MAP.
173
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)."
179   (let ((found 0))
180     (if (> ts-warn-conflict-verbosity 1)
181         (progn
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))
188                     (map (cdr 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)))
194                                 (if (and binding
195                                          (> ts-warn-conflict-verbosity 0))
196                                     (progn
197                                       (if (not (keymapp binding))
198                                           (setq found (1+ found)))
199                                       (message ts-conflict-warning
200                                                key binding)))))
201                             map)))
202             (accessible-keymaps map))
203     (if (> ts-warn-conflict-verbosity 1)
204         (message "Checking for conflicting bindings...done%s"
205                  (if (> found 0)
206                      (format ".  Found %d." found)
207                    ".")))))
208
209 ;;; Define a trivial command for a trivial example mode.
210
211 (defun ts-timestamp ()
212   "Insert the current time string in the current buffer at point."
213   (interactive)
214   (insert (current-time-string)))
215
216 ;;; Register the mode with XEmacs
217
218 (add-minor-mode 'ts-mode
219                 " ts"
220                 (ts-mode-set-prefix ts-mode-prefix 'adding-minor-mode))
221
222 ;;; end of ts-mode.el