comparison lisp/modes/strokes.el @ 153:25f70ba0133c r20-3b3

Import from CVS: tag r20-3b3
author cvs
date Mon, 13 Aug 2007 09:38:25 +0200
parents
children 6075d714658b
comparison
equal deleted inserted replaced
152:4c132ee2d62b 153:25f70ba0133c
1 ;;; strokes.el Sat May 24 14:18:08 1997
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5 ;; Author: David Bakhash <cadet@mit.edu>
6 ;; Maintainer: David Bakhash <cadet@mit.edu>
7 ;; Version: 2.3-beta
8 ;; Created: 12 April 1997
9 ;; Keywords: lisp, mouse, extensions
10
11 ;; This file is part of XEmacs.
12
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2 of the License, or
16 ;; (at your option) any later version.
17
18 ;; XEmacs 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 GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 ;; 02111-1307, USA.
27
28 ;;; Synched up with: Not in FSF.
29
30 ;;; Commentary:
31
32 ;; This package is written for for XEmacs v19.14 and up.
33 ;; This is the strokes package. It is intended to allow the user to
34 ;; control XEmacs by means of mouse strokes. Once strokes is loaded, you
35 ;; can always get help be invoking `strokes-help':
36
37 ;; > M-x strokes-help
38
39 ;; and you can learn how to use the package. A mouse stroke, for now,
40 ;; can be defined as holding the middle button, for instance, and then
41 ;; moving the mouse in whatever pattern you wish, which you have set
42 ;; XEmacs to understand as mapping to a given command. For example, you
43 ;; may wish the have a mouse stroke that looks like a capital `C' which
44 ;; means `copy-region-as-kill'. Treat strokes just like you do key
45 ;; bindings. For example, XEmacs sets key bindings globally with the
46 ;; `global-set-key' command. Likewise, you can do
47
48 ;; > M-x global-set-stroke
49
50 ;; to interactively program in a stroke. It would be wise to set the
51 ;; first one to this very command, so that from then on, you invoke
52 ;; `global-set-stroke' with a stroke. likewise, there may eventually
53 ;; be a `local-set-stroke' command, also analogous to `local-set-key'.
54
55 ;; You can always unset the last stroke definition with the command
56
57 ;; > M-x strokes-unset-last-stroke
58
59 ;; and the last stroke that was added to `strokes-global-map' will be
60 ;; removed.
61
62 ;; Other analogies between strokes and key bindings are as follows:
63
64 ;; 1) To describe a stroke binding, you can type
65
66 ;; > M-x describe-stroke
67
68 ;; analogous to `describe-key'. It's also wise to have a stroke,
69 ;; like an `h', for help, or a `?', mapped to `describe-stroke'.
70
71 ;; 2) stroke bindings are set internally through the lisp function
72 ;; `define-stroke', similar to the `define-key' function. some
73 ;; examples for a 3x3 stroke grid would be
74
75 ;; (define-stroke c-mode-stroke-map
76 ;; '((0 . 0) (1 . 1) (2 . 2))
77 ;; 'kill-region)
78 ;; (define-stroke strokes-global-map
79 ;; '((0 . 0) (0 . 1) (0 . 2) (1 . 2) (2 . 2))
80 ;; 'list-buffers)
81
82 ;; however, if you would probably just have the user enter in the
83 ;; stroke interactively and then set the stroke to whatever he/she
84 ;; entered. The lisp function to interactively read a stroke is
85 ;; `strokes-read-stroke'. This is especially helpful when you're
86 ;; on a fast computer that can handle a 9x9 stroke grid.
87
88 ;; NOTE: only global stroke bindings are currently implemented,
89 ;; however mode- and buffer-local stroke bindings may eventually
90 ;; be implemented in a future version.
91
92 ;; The important variables to be aware of for this package are listed
93 ;; below. They can all be altered through the customizing package via
94
95 ;; > M-x customize
96
97 ;; and customizing the group named `strokes'. You can also read
98 ;; documentation on the variables there.
99
100 ;; `strokes-minimum-match-score' (determines the threshold of error that
101 ;; makes a stroke acceptable or unacceptable. If your strokes arn't
102 ;; matching, then you should raise this variable.
103
104 ;; `strokes-grid-resolution' (determines the grid dimensions that you use
105 ;; when defining/reading strokes. The finer the grid your computer can
106 ;; handle, the more you can do, but even a 3x3 grid is pretty cool.)
107 ;; The default value (7) should be fine for most decent computers.
108 ;; NOTE: This variable should not be set to a number less than 3.
109
110 ;; `strokes-display-strokes-buffer' will allow you to hide the strokes
111 ;; buffer when doing simple strokes. This is a speedup for slow
112 ;; computers as well as people who don't want to see their strokes.
113
114 ;; If you find that your mouse is accelerating too fast, you can
115 ;; execute the UNIX X command to slow it down. A good possibility is
116
117 ;; % xset m 5/4 8
118
119 ;; which seems, heuristically, to work okay, without much disruption.
120
121 ;; Whenever you load in the strokes package, you will be able to save
122 ;; what you've done upon exiting XEmacs. You can also do
123
124 ;; > M-x save-strokes
125
126 ;; and it will save your strokes in ~/.strokes, or you may wish to change
127 ;; this by setting the variable `strokes-file'.
128
129 ;; Note that internally, all of the routines that are part of this
130 ;; package are able to deal with complex strokes, as they are a superset
131 ;; of simple strokes. However, the default of this package will map
132 ;; mouse button2 to the command `strokes-do-stroke', and NOT
133 ;; `strokes-do-complex-stroke'. If you wish to use complex strokes, you
134 ;; will have to override this key mapping. Complex strokes are terminated
135 ;; with mouse button3. The strokes package will not interfere with
136 ;; `mouse-yank', but you may want to examine how this is done (see the
137 ;; variable `strokes-click-command')
138
139 ;; To get strokes to work as part of your your setup, then you'll have
140 ;; put the strokes package in your load-path (preferably byte-compiled)
141 ;; and then add the following to your .xemacs-options file (or wherever
142 ;; you put XEmacs-specific startup preferences):
143
144 ;;(and (fboundp 'device-on-window-system-p)
145 ;; (device-on-window-system-p)
146 ;; (require 'strokes))
147
148 ;; Once loaded, you can start stroking. You can also toggle between
149 ;; strokes mode by simple typing
150
151 ;; > M-x strokes-mode
152
153 ;; I am now in the process of porting this package to emacs. I also hope
154 ;; that, with the help of others, this package will be useful in entering
155 ;; in pictographic-like language text using the mouse (i.e. Korean).
156 ;; Japanese and Chinese are a bit trickier, but I'm sure that with help
157 ;; it can be done. The next version will allow the user to enter strokes
158 ;; which "remove the pencil from the paper" so to speak, so one character
159 ;; can have multiple strokes.
160
161 ;; Great thanks to Rob Ristroph for his generosity in letting me use his
162 ;; PC to develop this, Jason Johnson for his help in algorithms, Euna
163 ;; Kim for her help in Korean, and massive thanks to the helpful guys
164 ;; on the help instance on athena (zeno, jered, amu, gsstark, ghudson, etc)
165 ;; Special thanks to Steve Baur and Hrvoje Niksic for all their help.
166
167 ;; Tasks: (what I'm getting ready for future version)...
168 ;; 2) use 'strokes-read-complex-stroke for korean, etc.
169 ;; 4) buffer-local 'strokes-local-map, and mode-stroke-maps would be nice
170 ;; 5) 'list-strokes (kinda important). What do people want?
171 ;; How about an optional docstring for each stroke so that a person
172 ;; can examine the strokes-file and actually make sense of it?
173 ;; (e.g. "This stroke is a pentagram")
174 ;; 6) add some hooks, like `strokes-read-stroke-hook'
175 ;; 7) See what people think of the factory settings. Should I change
176 ;; them? They're all pretty arbitrary in a way. I guess they
177 ;; should be minimal, but computers are getting lots faster, and
178 ;; if I choose the defaults too conservatively, then strokes will
179 ;; surely dissapoint some people on decent machines (until they
180 ;; figure out M-x customize). I need feedback.
181 ;; Other: I always have the most beta version of strokes, so if you
182 ;; want it just let me know.
183
184 ;;; Change Log:
185
186 ;; 1.3: provided user variable `strokes-use-strokes-buffer' to let users
187 ;; hide the strokes and strokes buffer when entering simple strokes.
188 ;; 1.3: cleaned up most leaks.
189 ;; 1.3: with Jari Aalto's help, cleaned up overall program.
190 ;; 1.3: added `strokes-help' for help on strokes
191 ;; 1.3: fixed 'strokes-load-hook bug
192 ;; 1.3: email address change: now <cadet@mit.edu>
193 ;; 1.3: added `strokes-report-bug' based on efs/dired's `dired-report-bug'
194 ;; 1.3: added more dialog-box queries for mouse-event stuff.
195 ;; 1.4: allowed strokes to invoke kbd macros as well (thanks gsstark!)
196 ;; 2.0: fixed up ordering of certain functions.
197 ;; 2.0: fixed bug applying to strokes in dedicated and minibuffer windows.
198 ;; 2.0: punted the C-h way of invoking strokes help routines.
199 ;; 2.0: fixed `strokes-define-stroke' so it would error check against
200 ;; defining strokes that were too short (really clicks)
201 ;; 2.0: added `strokes-toggle-strokes-buffer' interactive function
202 ;; 2.0: added `customize' support, thanks to patch from Hrvoje (thanks)
203 ;; 2.1: strokes no longer forces `mouse-yank-at-point' to t on mouse-yank
204 ;; (i.e. `mouse-yank-at-point' is up to you again)
205 ;; 2.1: toggling strokes-mode off and then back on no longer deletes
206 ;; the strokes that you programmed in but didn't save before
207 ;; toggling off strokes-mode.
208 ;; 2.1: advised may functions for modes like VM and w3 so that they too
209 ;; can use strokes, while still mantaining old button2 functionality.
210 ;; 2.1: with steve's help, got the autoload for `strokes-mode' and
211 ;; fixed up the package so loading it does not enable strokes
212 ;; until user calls `strokes-mode'.
213 ;; 2.2: made sure that abbrev-mode was off in the ` *strokes*' buffer
214 ;; 2.2: added more dired advice for mouse permissions commands
215 ;; 2.2: added some checks to see if saving strokes is really necessary so
216 ;; the user doesn't get promped aimlessly.
217 ;; 2.2: change the `strokes-lift' symbol to a keyword of value `:strokes-lift'
218 ;; for legibility. IF YOUR OLD STROKES DON'T WORK, THIS IS PROBABLY WHY.
219 ;; 2.2: I might have to change this back to `'strokes-lift' because the keyword
220 ;; fails in emacs, though I don't know why.
221 ;; 2.2: `strokes-describe-stroke' is nicer during recursive edits
222 ;; 2.2: provided `strokes-fill-stroke' to fill in empty spaces of strokes
223 ;; as an important step towards platform (speed) independence.
224 ;; Because of this, I moved the global setting of `strokes-last-stroke'
225 ;; from `strokes-eliminate-consecutive-redundancies' to
226 ;; `strokes-fill-stroke' since the latter comes later in processing
227 ;; a user stroke.
228 ;; 2.2: Finally changed the defaults, so now `strokes-grid-resolution' is 9
229 ;; and `strokes-minimum-match-score' is 1000 by default. This will surely
230 ;; mess some people up, but if so, just set it back w/ M-x customize.
231 ;; 2.2: Fixed up the mechanism for updating the `strokes-window-configuration'.
232 ;; Now it only uses one function (`strokes-update-window-configuration')
233 ;; which does it all, and much more efficiently (thanks RMS!).
234 ;; 2.2 Fixed up the appearance of the *strokes* buffer so that there are no
235 ;; ugly line truncations, and I got rid of the bug which would draw the stroke
236 ;; on the wrong line. I still wish that `event-closest-point' was smarter.
237 ;; In fact, `event-closest-point' does *not* do what its name suggests.
238 ;; 2.3 Added more to `strokes-update-window-configuration' so it goes to hell less often
239 ;; 2.3 `strokes-mode' no longer will undefine keys unless it's sure that the user had
240 ;; had them mapped to a strokes command.
241 ;; 2.3 added more magic autoload statements so strokes work more smoothly.
242 ;; similarly, I made strokes-mode turn itself on when the user defines a stroke
243 ;; (thanks Hrvoje).
244 ;; 2.3 Added "Strokes" to the modeline when strokes is on, and allow toggling strokes
245 ;; with mouse button2.
246 ;; 2.3 Added `list-strokes', which is a really nice function which graphically lists
247 ;; all the strokes that the user has defined and their corresponding commands.
248 ;; `list-strokes' will appropriately colorize the pixmaps to display some time info.
249
250 ;;; Code:
251
252 ;;; Requirements and provisions...
253
254 (autoload 'reporter-submit-bug-report "reporter")
255 (autoload 'mail-position-on-field "sendmail")
256
257 ;;; Constants...
258
259 (defconst strokes-version "2.3-beta")
260
261 (defconst strokes-bug-address "cadet@mit.edu")
262
263 (defconst strokes-lift :strokes-lift
264 "Symbol representing a stroke lift event for complex strokes.
265 Complex strokes are those which contain two or more simple strokes.
266 This will be useful for when XEmacs understands Chinese.")
267
268 ;;; user variables...
269
270 (defgroup strokes nil
271 "Control Emacs through mouse strokes"
272 :group 'mouse)
273
274 (defcustom strokes-modeline-string " Strokes"
275 "*Modeline identification when strokes are on \(default is \" Strokes\"\)."
276 :type 'string
277 :group 'strokes)
278
279 (defcustom strokes-character ?o
280 "*Character used when drawing strokes in the strokes buffer.
281 \(The default is lower-case `o', which works okay\)."
282 :type 'character
283 :group 'strokes)
284
285 (defcustom strokes-minimum-match-score 1000
286 "*Minimum score for a stroke to be considered a possible match.
287 Requiring a perfect match would set this variable to 0.
288 The default value is 1000, but it's mostly dependent on how precisely
289 you manage to replicate your user-defined strokes. It also depends on
290 the value of `strokes-grid-resolution', since a higher grid resolution
291 will correspond to more sample points, and thus more distance
292 measurements. Usually, this is not a problem since you first set
293 `strokes-grid-resolution' based on what your computer seems to be able
294 to handle (though the defaults are usually more than sufficent), and
295 then you can set `strokes-minimum-match-score' to something that works
296 for you. The only purpose of this variable is to insure that if you
297 do a bogus stroke that really doesn't match any of the predefined
298 ones, then strokes should NOT pick the one that came closest."
299 :type 'integer
300 :group 'strokes)
301
302 (defcustom strokes-grid-resolution 9
303 "*Integer defining dimensions of the stroke grid.
304 The grid is a square grid, where STROKES-GRID-RESOLUTION defaults to
305 `9', making a 9x9 grid whose coordinates go from (0 . 0) on the top
306 left to ((STROKES-GRID-RESOLUTION - 1) . (STROKES-GRID-RESOLUTION - 1))
307 on the bottom right. The greater the resolution, the more intricate
308 your strokes can be.
309 NOTE: This variable should be odd and MUST NOT be less than 3.
310 WARNING: Changing the value of this variable will gravely affect the
311 strokes you have already programmed in. You should try to
312 figure out what it should be based on your needs and on how
313 quick the particular platform(s) you're operating on, and
314 only then start programming in your custom strokes."
315 :type 'integer
316 :group 'strokes)
317
318 (defcustom strokes-file "~/.strokes"
319 "*File containing saved strokes for stroke-mode (default is ~/.strokes)."
320 :type 'file
321 :group 'strokes)
322
323 (defcustom strokes-buffer-name " *strokes*"
324 "The buffer that the strokes take place in (default is ` *strokes*')."
325 :type 'string
326 :group 'strokes)
327
328 (defcustom strokes-use-strokes-buffer t
329 "*If non-nil, the strokes buffer is used and strokes are displayed.
330 If nil, strokes will be read the same, however the user will not be
331 able to see the strokes. This be helpful for people who don't like
332 the delay in switching to the strokes buffer."
333 :type 'boolean
334 :group 'strokes)
335
336 (defcustom strokes-click-command 'mouse-yank
337 "*Command to execute when stroke is actually a `click' event.
338 This is set to `mouse-yank' by default."
339 :type 'function
340 :group 'strokes)
341
342 ;;; internal variables...
343
344 ;;;###autoload
345 (defvar strokes-mode nil
346 "Non-nil when `strokes' is globally enabled")
347
348 (defvar strokes-window-configuration nil
349 "The special window configuration used when entering strokes.
350 This is set properly in the function `strokes-update-window-configuration'.")
351
352 (defvar strokes-last-stroke nil
353 "Last stroke entered by the user.
354 Its value gets set every time the function
355 `strokes-fill-stroke' gets called,
356 since that is the best time to set the variable")
357
358 (defvar strokes-global-map '()
359 "Association list of strokes and their definitions.
360 Each entry is (STROKE . COMMAND) where STROKE is itself a list of
361 coordinates (X . Y) where X and Y are lists of positions on the
362 normalized stroke grid, with the top left at (0 . 0). COMMAND is the
363 corresponding interactive function")
364
365 (defvar strokes-load-hook nil
366 "Function or functions to be called when `strokes' is loaded.")
367
368 ;;; Macros...
369
370 (defsubst strokes-click-p (stroke)
371 "Non-nil if STROKE is really click."
372 (< (length stroke) 3))
373
374 ;;; old, but worked pretty good (just in case)...
375 ;;(defmacro strokes-define-stroke (stroke-map stroke def)
376 ;; "Add STROKE to STROKE-MAP alist with given command DEF"
377 ;; (list 'if (list '< (list 'length stroke) 3)
378 ;; (list 'error
379 ;; "That's a click, not a stroke. See `strokes-click-command'")
380 ;; (list 'setq stroke-map (list 'cons (list 'cons stroke def)
381 ;; (list 'remassoc stroke stroke-map)))))
382
383 (defmacro strokes-define-stroke (stroke-map stroke def)
384 "Add STROKE to STROKE-MAP alist with given command DEF"
385 `(if (strokes-click-p ,stroke)
386 (error "That's a click, not a stroke; see `strokes-click-command'")
387 (setq ,stroke-map (cons (cons ,stroke ,def)
388 (remassoc ,stroke ,stroke-map)))))
389
390 (defalias 'define-stroke 'strokes-define-stroke)
391
392 (defsubst strokes-square (x)
393 "Returns the square of the number X"
394 (* x x))
395
396 (defsubst strokes-distance-squared (p1 p2)
397 "Gets the distance (squared) between to points P1 and P2.
398 Each point is a cons cells (X . Y)"
399 (let ((x1 (car p1))
400 (y1 (cdr p1))
401 (x2 (car p2))
402 (y2 (cdr p2)))
403 (+ (strokes-square (- x2 x1))
404 (strokes-square (- y2 y1)))))
405
406 ;;; Advice for various functions...
407
408 ;; I'd originally wanted to write a macro that would just take in the
409 ;; generic functions which use mouse button2 in various modes. Most of
410 ;; them are identical in form: they take an event as the single argument
411 ;; and then do their thing. I tried writing a macro that looked
412 ;; something like this, but failed. Advice just ain't that easy. The
413 ;; one that bugged me the most was `Manual-follow-xref', because that had
414 ;; &rest arguments, and I didn't know how to work around it in defadvice.
415 ;; However, I was able to fix up most of the important modes (i.e. the
416 ;; ones I use all the time). One `bug' in the program that I just can't
417 ;; seem to figure out is why I can only advise other button2 functions
418 ;; successfully when the variable `strokes-use-strokes-buffer' is nil. I
419 ;; did all the save-excursion/save-window-excursion stuff SPECIFICALLY so
420 ;; that using the strokes buffer or not would absolutely not affect any
421 ;; other part of the program. If someone can figure out how to make the
422 ;; following advices work w/ regardless of that variable
423 ;; `strokes-use-strokes-buffer', then that would be a great victory. If
424 ;; someone out there would be kind enough to make the commented code
425 ;; below work, I'd be grateful. By the way, I put the `protect' keywords
426 ;; there to insure that if a stroke went bad, then
427 ;; `strokes-click-command' would be set back. If this isn't necessary,
428 ;; then feel free to let me know.
429
430 ;; For what follows, I really wanted something that would work like this:
431
432 ;;(strokes-fix-button2 'vm-mouse-button-2)
433
434 ;; Or even better, I could have simply done something like:
435
436 ;;(mapcar 'strokes-fix-button2
437 ;; '(vm-mouse-button-2
438 ;; rmail-summary-mouse-goto-msg
439 ;; <rest of them>))
440
441 ;;; With help from Hans (author of advice.el)...
442 (defmacro strokes-fix-button2-command (command)
443 "Fix COMMAND so that it can also work with strokes.
444 COMMAND must take one event argument.
445 Example of how one might fix up a command that's bound to button2
446 and which is an interactive funcion of one event argument:
447
448 (strokes-fix-button2-command 'vm-mouse-button-2)"
449 (let ((command (eval command)))
450 `(progn
451 (defadvice ,command (around strokes-fix-button2 compile preactivate)
452 ,(format "Fix %s to work with strokes." command)
453 (if strokes-use-strokes-buffer
454 ;; then strokes is no good and we'll have to use the original
455 ad-do-it
456 ;; otherwise, we can make strokes work too...
457 (let ((strokes-click-command
458 ',(intern (format "ad-Orig-%s" command))))
459 (strokes-do-stroke (ad-get-arg 0))))))))
460
461 (strokes-fix-button2-command 'vm-mouse-button-2)
462 (strokes-fix-button2-command 'rmail-summary-mouse-goto-msg)
463 (strokes-fix-button2-command 'Buffer-menu-mouse-select)
464 (strokes-fix-button2-command 'w3-widget-button-click)
465 (strokes-fix-button2-command 'widget-image-button-press)
466 (strokes-fix-button2-command 'Info-follow-clicked-node)
467 (strokes-fix-button2-command 'compile-mouse-goto-error)
468 (strokes-fix-button2-command 'gdbsrc-select-or-yank)
469 (strokes-fix-button2-command 'hypropos-mouse-get-doc)
470 (strokes-fix-button2-command 'gnus-mouse-pick-group)
471 (strokes-fix-button2-command 'gnus-mouse-pick-article)
472 (strokes-fix-button2-command 'gnus-article-push-button)
473 (strokes-fix-button2-command 'dired-mouse-find-file)
474 (strokes-fix-button2-command 'url-dired-find-file-mouse)
475 (strokes-fix-button2-command 'dired-u-r-mouse-toggle)
476 (strokes-fix-button2-command 'dired-u-w-mouse-toggle)
477 (strokes-fix-button2-command 'dired-u-x-mouse-toggle)
478 (strokes-fix-button2-command 'dired-g-r-mouse-toggle)
479 (strokes-fix-button2-command 'dired-g-w-mouse-toggle)
480 (strokes-fix-button2-command 'dired-g-x-mouse-toggle)
481 (strokes-fix-button2-command 'dired-o-r-mouse-toggle)
482 (strokes-fix-button2-command 'dired-o-w-mouse-toggle)
483 (strokes-fix-button2-command 'isearch-yank-x-selection)
484 (strokes-fix-button2-command 'occur-mode-mouse-goto)
485
486 ;;; I can fix the customize widget button click, but then
487 ;;; people will get confused when they try to customize
488 ;;; strokes with the mouse and customize tells them that
489 ;;; `strokes-click-command' is mapped to `ad-Orig-widget-button-click'
490 ;;(strokes-fix-button2-command 'widget-button-click)
491
492 ;;; without the advice, each advised function would look like...
493 ;;(defadvice vm-mouse-button-2 (around vm-strokes activate protect)
494 ;; "Allow strokes to work in VM."
495 ;; (if strokes-use-strokes-buffer
496 ;; ;; then strokes is no good and we'll have to use the original
497 ;; ad-do-it
498 ;; ;; otherwise, we can make strokes work too...
499 ;; (let ((strokes-click-command 'ad-Orig-vm-mouse-button-2))
500 ;; (strokes-do-stroke (ad-get-arg 0)))))
501
502 ;;; Functions...
503
504 (defun strokes-lift-p (object)
505 "Return non-nil if object is a stroke-lift"
506 (eq object strokes-lift))
507
508 (defun strokes-unset-last-stroke ()
509 "Undo the last stroke definition."
510 (interactive)
511 (let ((command (cdar strokes-global-map)))
512 (if (y-or-n-p-maybe-dialog-box
513 (format "really delete last stroke definition, defined to `%s'? "
514 command))
515 (progn
516 (setq strokes-global-map (cdr strokes-global-map))
517 (message "That stroke has been deleted"))
518 (message "Nothing done"))))
519
520 ;;;###autoload
521 (defun strokes-global-set-stroke (stroke command)
522 "Interactively give STROKE the global binding as COMMAND.
523 Operated just like `global-set-key', except for strokes.
524 COMMAND is a symbol naming an interactively-callable function. STROKE
525 is a list of sampled positions on the stroke grid as described in the
526 documentation for the `strokes-define-stroke' function."
527 (interactive
528 (list
529 (and (or strokes-mode (strokes-mode t))
530 (strokes-read-complex-stroke
531 "Define a new stroke. Draw with button1 (or 2). End with button3..."))
532 (read-command "command to map stroke to: ")))
533 (strokes-define-stroke strokes-global-map stroke command))
534
535 ;;;###autoload
536 (defalias 'global-set-stroke 'strokes-global-set-stroke)
537
538 ;;(defun global-unset-stroke (stroke); FINISH THIS DEFUN!
539 ;; "delete all strokes matching STROKE from `strokes-global-map',
540 ;; letting the user input
541 ;; the stroke with the mouse"
542 ;; (interactive
543 ;; (list
544 ;; (strokes-read-stroke "Enter the stroke you want to delete...")))
545 ;; (strokes-define-stroke 'strokes-global-map stroke command))
546
547 (defun strokes-get-grid-position (stroke-extent position &optional grid-resolution)
548 "Map POSITION to a new grid position based on its STROKE-EXTENT and GRID-RESOLUTION.
549 STROKE-EXTENT as a list \(\(xmin . ymin\) \(xmax . ymax\)\).
550 If POSITION is a `strokes-lift', then it is itself returned.
551 Optional GRID-RESOLUTION may be used in place of STROKES-GRID-RESOLUTION.
552 The grid is a square whose dimesion is [0,GRID-RESOLUTION)."
553 (cond ((consp position) ; actual pixel location
554 (let ((grid-resolution (or grid-resolution strokes-grid-resolution))
555 (x (car position))
556 (y (cdr position))
557 (xmin (caar stroke-extent))
558 (ymin (cdar stroke-extent))
559 ;; the `1+' is there to insure that the
560 ;; formula evaluates correctly at the boundaries
561 (xmax (1+ (caadr stroke-extent)))
562 (ymax (1+ (cdadr stroke-extent))))
563 (cons (floor (* grid-resolution
564 (/ (float (- x xmin))
565 (- xmax xmin))))
566 (floor (* grid-resolution
567 (/ (float (- y ymin))
568 (- ymax ymin)))))))
569 ((strokes-lift-p position) ; stroke lift
570 strokes-lift)))
571
572 ;;(defun strokes-get-grid-position (stroke-extent pix-pos)
573 ;; "Return the stroke-grid position for PIX-POS given the total STROKE-EXTENT.
574 ;;STROKE-EXTENT as a list \(\(xmin . ymin\) \(xmax . ymax\)\) and a particular
575 ;;pixel position or `strokes-lift', find the corresponding grid position
576 ;;\(based on `strokes-grid-resolution'\) for the PIX-POS."
577 ;; (cond ((consp pix-pos) ; actual pixel location
578 ;; (let ((x (car pix-pos))
579 ;; (y (cdr pix-pos))
580 ;; (xmin (caar stroke-extent))
581 ;; (ymin (cdar stroke-extent))
582 ;; ;; the `1+' is there to insure that the
583 ;; ;; formula evaluates correctly at the boundaries
584 ;; (xmax (1+ (caadr stroke-extent)))
585 ;; (ymax (1+ (cdadr stroke-extent))))
586 ;; (cons (floor (* strokes-grid-resolution
587 ;; (/ (float (- x xmin))
588 ;; (- xmax xmin))))
589 ;; (floor (* strokes-grid-resolution
590 ;; (/ (float (- y ymin))
591 ;; (- ymax ymin)))))))
592 ;; ((strokes-lift-p pix-pos) ; stroke lift
593 ;; strokes-lift)))
594
595 (defun strokes-get-stroke-extent (pixel-positions)
596 "From a list of absolute PIXEL-POSITIONS, returns absolute spatial extent.
597 The return value is a list ((xmin . ymin) (xmax . ymax))."
598 (if pixel-positions
599 (let ((xmin (caar pixel-positions))
600 (xmax (caar pixel-positions))
601 (ymin (cdar pixel-positions))
602 (ymax (cdar pixel-positions))
603 (rest (cdr pixel-positions)))
604 (while rest
605 (if (consp (car rest))
606 (let ((x (caar rest))
607 (y (cdar rest)))
608 (if (< x xmin)
609 (setq xmin x))
610 (if (> x xmax)
611 (setq xmax x))
612 (if (< y ymin)
613 (setq ymin y))
614 (if (> y ymax)
615 (setq ymax y))))
616 (setq rest (cdr rest)))
617 (let ((delta-x (- xmax xmin))
618 (delta-y (- ymax ymin)))
619 (if (> delta-x delta-y)
620 (setq ymin (- ymin
621 (/ (- delta-x delta-y)
622 2))
623 ymax (+ ymax
624 (/ (- delta-x delta-y)
625 2)))
626 (setq xmin (- xmin
627 (/ (- delta-y delta-x)
628 2))
629 xmax (+ xmax
630 (/ (- delta-y delta-x)
631 2))))
632 (list (cons xmin ymin)
633 (cons xmax ymax))))
634 nil))
635
636 (defun strokes-eliminate-consecutive-redundancies (entries)
637 "Returns a list with no consecutive redundant entries."
638 ;; defun a grande vitesse grace a Dave G.
639 (loop for element on entries
640 if (not (equal (car element) (cadr element)))
641 collect (car element)))
642 ;; (loop for element on entries
643 ;; nconc (if (not (equal (car el) (cadr el)))
644 ;; (list (car el)))))
645 ;; yet another (orig) way of doing it...
646 ;; (if entries
647 ;; (let* ((current (car entries))
648 ;; (rest (cdr entries))
649 ;; (non-redundant-list (list current))
650 ;; (next nil))
651 ;; (while rest
652 ;; (setq next (car rest))
653 ;; (if (equal current next)
654 ;; (setq rest (cdr rest))
655 ;; (setq non-redundant-list (cons next non-redundant-list)
656 ;; current next
657 ;; rest (cdr rest))))
658 ;; (nreverse non-redundant-list))
659 ;; nil))
660
661 (defun strokes-renormalize-to-grid (positions &optional grid-resolution)
662 "Map POSITIONS to a new grid whose dimensions are based on GRID-RESOLUTION.
663 POSITIONS is a list of positions and stroke-lifts.
664 Optional GRID-RESOLUTION may be used in place of STROKES-GRID-RESOLUTION.
665 The grid is a square whose dimesion is [0,GRID-RESOLUTION)."
666 (let ((stroke-extent (strokes-get-stroke-extent positions)))
667 (mapcar (function
668 (lambda (pos)
669 (strokes-get-grid-position stroke-extent pos grid-resolution)))
670 positions)))
671
672 ;;(defun strokes-normalize-pixels-to-grid (pixel-positions)
673 ;; "Map PIXEL-POSITIONS to the stroke grid.
674 ;;PIXEL-POSITIONS is a list of pixel-positions and stroke-lifts. The
675 ;;normalized stroke grid is defined by the variable STROKES-GRID-RESOLUTION"
676 ;; (let ((stroke-extent (strokes-get-stroke-extent pixel-positions)))
677 ;; (mapcar (function
678 ;; (lambda (pix-pos)
679 ;; (strokes-get-grid-position stroke-extent pix-pos)))
680 ;; pixel-positions)))
681
682 (defun strokes-fill-stroke (unfilled-stroke &optional force)
683 "Fill in missing grid locations in the list of UNFILLED-STROKE.
684 If FORCE is non-nil, then fill the stroke even if it's `stroke-click'.
685 NOTE: This is where the global variable `strokes-last-stroke' is set."
686 (setq strokes-last-stroke ; this is global
687 (if (and (strokes-click-p unfilled-stroke)
688 (not force))
689 unfilled-stroke
690 (loop for grid-locs on unfilled-stroke
691 nconc (let* ((current (car grid-locs))
692 (current-is-a-point-p (consp current))
693 (next (cadr grid-locs))
694 (next-is-a-point-p (consp next))
695 (both-are-points-p (and current-is-a-point-p
696 next-is-a-point-p))
697 (x1 (and current-is-a-point-p
698 (car current)))
699 (y1 (and current-is-a-point-p
700 (cdr current)))
701 (x2 (and next-is-a-point-p
702 (car next)))
703 (y2 (and next-is-a-point-p
704 (cdr next)))
705 (delta-x (and both-are-points-p
706 (- x2 x1)))
707 (delta-y (and both-are-points-p
708 (- y2 y1)))
709 (slope (and both-are-points-p
710 (if (zerop delta-x)
711 nil ; undefined vertical slope
712 (/ (float delta-y)
713 delta-x)))))
714 (cond ((not both-are-points-p)
715 (list current))
716 ((null slope) ; undefinded vertical slope
717 (if (>= delta-y 0)
718 (loop for y from y1 below y2
719 collect (cons x1 y))
720 (loop for y from y1 above y2
721 collect (cons x1 y))))
722 ((zerop slope) ; (= y1 y2)
723 (if (>= delta-x 0)
724 (loop for x from x1 below x2
725 collect (cons x y1))
726 (loop for x from x1 above x2
727 collect (cons x y1))))
728 ((>= (abs delta-x) (abs delta-y))
729 (if (> delta-x 0)
730 (loop for x from x1 below x2
731 collect (cons x
732 (+ y1
733 (round (* slope
734 (- x x1))))))
735 (loop for x from x1 above x2
736 collect (cons x
737 (+ y1
738 (round (* slope
739 (- x x1))))))))
740 (t ; (< (abs delta-x) (abs delta-y))
741 (if (> delta-y 0)
742 (loop for y from y1 below y2
743 collect (cons (+ x1
744 (round (/ (- y y1)
745 slope)))
746 y))
747 (loop for y from y1 above y2
748 collect (cons (+ x1
749 (round (/ (- y y1)
750 slope)))
751 y))))))))))
752
753 (defun strokes-rate-stroke (stroke1 stroke2)
754 "Rates STROKE1 with STROKE2 and returns a score based on a distance metric.
755 Note: the rating is an error rating, and therefore, a return of 0
756 represents a perfect match. Also note that the order of stroke
757 arguments is order-independent for the algorithm used here."
758 (if (and stroke1 stroke2)
759 (let ((rest1 (cdr stroke1))
760 (rest2 (cdr stroke2))
761 (err (strokes-distance-squared (car stroke1)
762 (car stroke2))))
763 (while (and rest1 rest2)
764 (while (and (consp (car rest1))
765 (consp (car rest2)))
766 (setq err (+ err
767 (strokes-distance-squared (car rest1)
768 (car rest2)))
769 stroke1 rest1
770 stroke2 rest2
771 rest1 (cdr stroke1)
772 rest2 (cdr stroke2)))
773 (cond ((and (strokes-lift-p (car rest1))
774 (strokes-lift-p (car rest2)))
775 (setq rest1 (cdr rest1)
776 rest2 (cdr rest2)))
777 ((strokes-lift-p (car rest2))
778 (while (consp (car rest1))
779 (setq err (+ err
780 (strokes-distance-squared (car rest1)
781 (car stroke2)))
782 rest1 (cdr rest1))))
783 ((strokes-lift-p (car rest1))
784 (while (consp (car rest2))
785 (setq err (+ err
786 (strokes-distance-squared (car stroke1)
787 (car rest2)))
788 rest2 (cdr rest2))))))
789 (if (null rest2)
790 (while (consp (car rest1))
791 (setq err (+ err
792 (strokes-distance-squared (car rest1)
793 (car stroke2)))
794 rest1 (cdr rest1))))
795 (if (null rest1)
796 (while (consp (car rest2))
797 (setq err (+ err
798 (strokes-distance-squared (car stroke1)
799 (car rest2)))
800 rest2 (cdr rest2))))
801 (if (or (strokes-lift-p (car rest1))
802 (strokes-lift-p (car rest2)))
803 (setq err nil)
804 err))
805 nil))
806
807 (defun strokes-match-stroke (stroke stroke-map)
808 "Finds the best matching command of STROKE in STROKE-MAP.
809 Returns the corresponding match as (COMMAND . SCORE)."
810 (if (and stroke stroke-map)
811 (let ((score (strokes-rate-stroke stroke (caar stroke-map)))
812 (command (cdar stroke-map))
813 (map (cdr stroke-map)))
814 (while map
815 (let ((newscore (strokes-rate-stroke stroke (caar map))))
816 (if (or (and newscore score (< newscore score))
817 (and newscore (null score)))
818 (setq score newscore
819 command (cdar map)))
820 (setq map (cdr map))))
821 (if score
822 (cons command score)
823 nil))
824 nil))
825
826 ;;;###autoload
827 (defun strokes-read-stroke (&optional prompt event)
828 "Read a simple stroke (interactively) and return the stroke.
829 Optional PROMPT in minibuffer displays before and during stroke reading.
830 This function will display the stroke interactively as it is being
831 entered in the strokes buffer if the variable
832 `strokes-use-strokes-buffer' is non-nil.
833 Optional EVENT is currently not used, but hopefully will be soon."
834 (save-excursion
835 (let ((pix-locs nil)
836 (grid-locs nil)
837 (event (or event (make-event))))
838 (if strokes-use-strokes-buffer
839 ;; switch to the strokes buffer and
840 ;; display the stroke as it's being read
841 (save-window-excursion
842 (set-window-configuration strokes-window-configuration)
843 (if prompt
844 (progn
845 (setq event (next-event event prompt))
846 (while (not (button-press-event-p event))
847 (dispatch-event event)
848 (setq event (next-event event)))))
849 (unwind-protect
850 (progn
851 (setq event (next-event event))
852 (while (not (button-release-event-p event))
853 (if (mouse-event-p event)
854 (let ((point (event-closest-point event)))
855 (when point
856 (goto-char point)
857 (subst-char-in-region point (1+ point) ?\ strokes-character))
858 (setq pix-locs (cons (cons (event-x-pixel event)
859 (event-y-pixel event))
860 pix-locs))))
861 (setq event (next-event event))))
862 ;; protected
863 ;; clean up strokes buffer and then bury it.
864 (subst-char-in-region (point-min) (point-max) strokes-character ?\ )
865 (goto-char (point-min))
866 (bury-buffer)))
867 ;; Otherwise, don't use strokes buffer and read stroke silently
868 (if prompt
869 (progn
870 (setq event (next-event event prompt))
871 (while (not (button-press-event-p event))
872 (dispatch-event event)
873 (setq event (next-event event)))))
874 (setq event (next-event))
875 (while (not (button-release-event-p event))
876 (if (mouse-event-p event)
877 (setq pix-locs (cons (cons (event-x-pixel event)
878 (event-y-pixel event))
879 pix-locs)))
880 (setq event (next-event event))))
881 (setq grid-locs (strokes-renormalize-to-grid (nreverse pix-locs)))
882 (strokes-fill-stroke (strokes-eliminate-consecutive-redundancies grid-locs)))))
883
884 ;;;###autoload
885 (defun strokes-read-complex-stroke (&optional prompt event)
886 "Read a complex stroke (interactively) and return the stroke.
887 Optional PROMPT in minibuffer displays before and during stroke reading.
888 Note that a complex stroke allows the user to pen-up and pen-down. This
889 is implemented by allowing the user to paint with button1 or button2 and
890 then complete the stroke with button3.
891 Optional EVENT is currently not used, but hopefully will be soon."
892 (save-excursion
893 (save-window-excursion
894 (set-window-configuration strokes-window-configuration)
895 (let ((pix-locs nil)
896 (grid-locs nil)
897 (event (or event (next-event nil prompt))))
898 (if prompt
899 (while (not (button-press-event-p event))
900 (dispatch-event event)
901 (setq event (next-event event))))
902 (unwind-protect
903 (progn
904 (while (not (and (button-press-event-p event)
905 (eq (event-button event) 3)))
906 (while (not (button-release-event-p event))
907 (if (mouse-event-p event)
908 (let ((point (event-closest-point event)))
909 (when point
910 (goto-char point)
911 (subst-char-in-region point (1+ point) ?\ strokes-character))
912 (setq pix-locs (cons (cons (event-x-pixel event)
913 (event-y-pixel event))
914 pix-locs))))
915 (setq event (next-event event prompt)))
916 (setq pix-locs (cons strokes-lift pix-locs))
917 (while (not (button-press-event-p event))
918 (dispatch-event event)
919 (setq event (next-event event prompt))))
920 (setq pix-locs (nreverse (cdr pix-locs))
921 grid-locs (strokes-renormalize-to-grid pix-locs))
922 (strokes-fill-stroke
923 (strokes-eliminate-consecutive-redundancies grid-locs)))
924 ;; protected
925 (subst-char-in-region (point-min) (point-max) strokes-character ?\ )
926 (goto-char (point-min))
927 (bury-buffer))))))
928
929 (defun strokes-execute-stroke (stroke)
930 "Given STROKE, execute the command which corresponds to it.
931 The command will be executed provided one exists for that stroke,
932 based on the variable `strokes-minimum-match-score'.
933 If no stroke matches, nothing is done and return value is nil."
934 (let* ((match (strokes-match-stroke stroke strokes-global-map))
935 (command (car match))
936 (score (cdr match)))
937 (cond ((strokes-click-p stroke)
938 ;; This is the case of a `click' type event
939 (command-execute strokes-click-command))
940 ((and match (<= score strokes-minimum-match-score))
941 (message "%s" command)
942 (command-execute command))
943 ((null strokes-global-map)
944 (if (file-exists-p strokes-file)
945 (and (y-or-n-p-maybe-dialog-box
946 (format "No strokes loaded. Load `%s'? "
947 strokes-file))
948 (strokes-load-user-strokes))
949 (error "No strokes defined; use `global-set-stroke'")))
950 (t
951 (error
952 "No stroke matches; see variable `strokes-minimum-match-score'")
953 nil))))
954
955 ;;;###autoload
956 (defun strokes-do-stroke (event)
957 "Read a simple stroke from the user and then exectute its comand.
958 This must be bound to a mouse event."
959 (interactive "e")
960 (or strokes-mode (strokes-mode t))
961 (strokes-execute-stroke (strokes-read-stroke nil event)))
962
963 ;;;###autoload
964 (defun strokes-do-complex-stroke (event)
965 "Read a complex stroke from the user and then exectute its command.
966 This must be bound to a mouse event."
967 (interactive "e")
968 (or strokes-mode (strokes-mode t))
969 (strokes-execute-stroke (strokes-read-complex-stroke nil event)))
970
971 ;;;###autoload
972 (defun strokes-describe-stroke (stroke)
973 "Displays the command which STROKE maps to, reading STROKE interactively."
974 (interactive
975 (list
976 (strokes-read-complex-stroke
977 "Enter stroke to describe; end with button3...")))
978 (let* ((match (strokes-match-stroke stroke strokes-global-map))
979 (command (or (and (strokes-click-p stroke)
980 strokes-click-command)
981 (car match)))
982 (score (cdr match)))
983 (if (or (and match
984 (<= score strokes-minimum-match-score))
985 (and (strokes-click-p stroke)
986 strokes-click-command))
987 (message "That stroke maps to `%s'" command)
988 (message "That stroke is undefined"))
989 (sleep-for 1))) ; helpful for recursive edits
990
991 ;;;###autoload
992 (defalias 'describe-stroke 'strokes-describe-stroke)
993
994 ;;;###autoload
995 (defun strokes-help ()
996 "Get instructional help on using the the `strokes' package."
997 (interactive)
998 (with-displaying-help-buffer
999 (function
1000 (lambda ()
1001 (save-excursion
1002 (let ((helpdoc
1003 "This is help for the strokes package.
1004
1005 If you find something wrong with it, or feel that it can be improved
1006 in some way, then please feel free to email me:
1007
1008 David Bakhash <cadet@mit.edu>
1009
1010 or just do
1011
1012 M-x strokes-report-bug
1013
1014 ------------------------------------------------------------
1015
1016 The strokes package allows you to define strokes (that you make with
1017 the mouse or other pointer device) that XEmacs can interpret as
1018 corresponding to commands, and then executes the commands. It does
1019 character recognition, so you don't have to worry about getting it
1020 right every time.
1021
1022 Strokes are easy to program and fun to use. To start strokes going,
1023 you'll want to put the following line in your .emacs file:
1024
1025 (and (fboundp 'device-on-window-system-p)
1026 (device-on-window-system-p)
1027 (require 'strokes))
1028
1029 This will load strokes when and only when you start XEmacs on a window
1030 system (i.e. that has a pointer (mouse) device, etc.).
1031
1032 To toggle strokes-mode, you just do
1033
1034 > M-x strokes-mode
1035
1036 When you're ready to start defining strokes, just use the command
1037
1038 > M-x global-set-stroke
1039
1040 You will see a ` *strokes*' buffer which is waiting for you to enter in
1041 your stroke. When you enter in the stroke, you draw with button1 or
1042 button2, and then end with button3. Next, you enter in the command
1043 which will be executed when that stroke is invoked. Simple as that.
1044 For now, try to define a stroke to copy a region. This is a popular
1045 edit command, so type
1046
1047 > M-x global-set-stroke
1048
1049 Then, in the ` *strokes*' buffer, draw the letter `C' (for `copy')
1050 and then, when it asks you to enter the command to map that to, type
1051
1052 > copy-region-as-kill
1053
1054 That's about as hard as it gets.
1055 Remember: paint with button1 or button2 and then end with button3.
1056
1057 If ever you want to know what a certain strokes maps to, then do
1058
1059 > M-x describe-stroke
1060
1061 and you can enter in any arbitrary stroke. Remember: The strokes
1062 package lets you program in simple and complex (multi-lift) strokes.
1063 The only difference is how you *invoke* the two. You will most likely
1064 use simple strokes, as complex strokes were developed for
1065 Chinese/Japanese/Korean. So the middle mouse button (button2) will
1066 invoke the command `strokes-do-stroke' in buffers where button2 doesn't
1067 already have a meaning other than its original, which is `mouse-yank'.
1068 But don't worry: `mouse-yank' will still work with strokes (see the
1069 variable `strokes-click-command').
1070
1071 If ever you define a stroke which you don't like, then you can unset
1072 it with the command
1073
1074 > M-x strokes-unset-last-stroke
1075
1076 You can always get an idea of what your current strokes look like with
1077 the command
1078
1079 > M-x list-strokes
1080
1081 Your strokes will be displayed in from most recent down, and the
1082 beginning of each simple stroke will be marked by a color dot. Since
1083 you may have several simple strokes in a complex stroke, the dot
1084 colors are arranged in the rainbow color sequence, `ROYGBIV'.
1085
1086 Your strokes are stored as you enter them. They get saved in a file
1087 called ~/.strokes, along with other strokes configuration variables.
1088 You can change this location by setting the variable `strokes-file'.
1089 You will be prompted to save them when you exit XEmacs, or you can save
1090 them with
1091
1092 > M-x save-strokes
1093
1094 Your strokes get loaded automatically when you enable `strokes-mode'.
1095 You can also load in your user-defined strokes with
1096
1097 > M-x load-user-strokes
1098
1099 A few more important things:
1100
1101 o The command `strokes-do-stroke' is also invoked with C-button2, so that you
1102 can still enter a stroke in modes which use button2 for other things,
1103 such as cross-referencing.
1104
1105 o Complex strokes (i.e. `strokes-do-complex-stroke'), by default, use
1106 Sh-button2.
1107
1108 o Strokes are a bit computer-dependent in that they depend somewhat on
1109 the speed of the computer you're working on. This means that you
1110 may have to tweak some variables. You can read about them in the
1111 commentary of `strokes.el'. Better to just use apropos and read their
1112 docstrings. All variables/functions start with `strokes'. The one
1113 variable which many people wanted to see was
1114 `strokes-use-strokes-buffer' which allows the user to use strokes
1115 silently--without displaying the strokes. All variables can be set
1116 by customizing the group named `strokes' via the customization package:
1117
1118 > M-x customize
1119
1120 o A problem with strokes happens when you resize windows. If you
1121 enlarge your XEmacs window a lot and realize that your strokes
1122 buffer is not big enough, you may need to fix it with
1123
1124 > M-x strokes-update-window-configuration."))
1125 (princ helpdoc standard-output)))))))
1126
1127 (defun strokes-report-bug ()
1128 "Submit a bug report for strokes."
1129 (interactive)
1130 (let ((reporter-prompt-for-summary-p t))
1131 (or (boundp 'reporter-version)
1132 (setq reporter-version
1133 "Your version of reporter is obsolete. Please upgrade."))
1134 (reporter-submit-bug-report
1135 strokes-bug-address "Strokes"
1136 (cons
1137 'strokes-version
1138 (nconc
1139 (mapcar
1140 'intern
1141 (sort
1142 (let (completion-ignore-case)
1143 (all-completions "strokes-" obarray 'user-variable-p))
1144 'string-lessp))
1145 (list 'reporter-version)))
1146 (function
1147 (lambda ()
1148 (save-excursion
1149 (mail-position-on-field "subject")
1150 (beginning-of-line)
1151 (skip-chars-forward "^:\n")
1152 (if (looking-at ": Strokes;")
1153 (progn
1154 (goto-char (match-end 0))
1155 (delete-char -1)
1156 (insert " " strokes-version " bug:")))))))))
1157
1158 (defsubst strokes-fill-current-buffer-with-whitespace ()
1159 "Erase the contents of the current buffer and fill it with whitespace"
1160 (erase-buffer)
1161 (loop for i from 1 to (frame-height) do
1162 (progn
1163 (insert-char ?\ (1- (frame-width)))
1164 (newline)))
1165 (goto-char (point-min)))
1166
1167 (defun strokes-update-window-configuration ()
1168 "Insure that `strokes-window-configuration' is up-to-date."
1169 (interactive)
1170 (let ((current-window (selected-window)))
1171 (cond ((or (window-minibuffer-p current-window)
1172 (window-dedicated-p current-window))
1173 ;; don't try to update strokes window configuration
1174 ;; if window is dedicated or a minibuffer
1175 nil)
1176 ((or (interactive-p)
1177 (not (buffer-live-p (get-buffer strokes-buffer-name)))
1178 (null strokes-window-configuration))
1179 ;; create `strokes-window-configuration' from scratch...
1180 (save-excursion
1181 (save-window-excursion
1182 (get-buffer-create strokes-buffer-name)
1183 (set-window-buffer current-window strokes-buffer-name)
1184 (delete-other-windows)
1185 (fundamental-mode)
1186 (auto-save-mode 0)
1187 (if (featurep 'font-lock)
1188 (font-lock-mode 0))
1189 (abbrev-mode 0)
1190 (buffer-disable-undo (current-buffer))
1191 (setq truncate-lines nil)
1192 (strokes-fill-current-buffer-with-whitespace)
1193 (setq strokes-window-configuration (current-window-configuration))
1194 (bury-buffer))))
1195 (t ; `strokes buffer' still exists...
1196 ;; update the strokes-window-configuration for this specific frame...
1197 (save-excursion
1198 (save-window-excursion
1199 (set-window-buffer current-window strokes-buffer-name)
1200 (delete-other-windows)
1201 (strokes-fill-current-buffer-with-whitespace)
1202 (setq strokes-window-configuration (current-window-configuration))
1203 (bury-buffer)))))))
1204
1205 ;;;###autoload
1206 (defun strokes-load-user-strokes ()
1207 "Load user-defined strokes from file named by `strokes-file'."
1208 (interactive)
1209 (cond ((and (file-exists-p strokes-file)
1210 (file-readable-p strokes-file))
1211 (load-file strokes-file))
1212 ((interactive-p)
1213 (error "Trouble loading user-defined strokes; nothing done"))
1214 (t
1215 (message "No user-defined strokes, sorry"))))
1216
1217 ;;;###autoload
1218 (defalias 'load-user-strokes 'strokes-load-user-strokes)
1219
1220 (defun strokes-prompt-user-save-strokes ()
1221 "Save user-defined strokes to file named by `strokes-file'."
1222 (interactive)
1223 (save-excursion
1224 (let ((current strokes-global-map))
1225 (unwind-protect
1226 (progn
1227 (setq strokes-global-map nil)
1228 (strokes-load-user-strokes)
1229 (if (and (not (equal current strokes-global-map))
1230 (or (interactive-p)
1231 (yes-or-no-p-maybe-dialog-box "save your strokes? ")))
1232 (progn
1233 (require 'pp) ; pretty-print variables
1234 (message "Saving strokes in %s..." strokes-file)
1235 (get-buffer-create "*saved-strokes*")
1236 (set-buffer "*saved-strokes*")
1237 (erase-buffer)
1238 (emacs-lisp-mode)
1239 (goto-char (point-min))
1240 (insert-string
1241 ";; -*- Syntax: Emacs-Lisp; Mode: emacs-lisp -*-\n")
1242 (insert-string (format ";;; saved strokes for %s, as of %s\n\n"
1243 (user-full-name)
1244 (format-time-string "%B %e, %Y" nil)))
1245 (message "Saving strokes in %s..." strokes-file)
1246 (insert-string (format "(setq strokes-global-map '%s)"
1247 (pp current)))
1248 (message "Saving strokes in %s..." strokes-file)
1249 (indent-region (point-min) (point-max) nil)
1250 (write-region (point-min)
1251 (point-max)
1252 strokes-file))
1253 (message "(no changes need to be saved)")))
1254 ;; protected
1255 (if (get-buffer "*saved-strokes*")
1256 (kill-buffer (get-buffer "*saved-strokes*")))
1257 (setq strokes-global-map current)))))
1258
1259 (defalias 'save-strokes 'strokes-prompt-user-save-strokes)
1260
1261 (defun strokes-toggle-strokes-buffer (&optional arg)
1262 "Toggle the use of the strokes buffer.
1263 In other words, toggle the variabe `strokes-use-strokes-buffer'.
1264 With ARG, use strokes buffer if and only if ARG is positive or true.
1265 Returns value of `strokes-use-strokes-buffer'."
1266 (interactive "P")
1267 (setq strokes-use-strokes-buffer
1268 (if arg (> (prefix-numeric-value arg) 0)
1269 (not strokes-use-strokes-buffer))))
1270
1271 (defun strokes-xpm-for-stroke (stroke &optional bufname)
1272 "Create an xpm pixmap for the given stroke in buffer `*strokes-xpm*'.
1273 Optional BUFNAME to name something else.
1274 The pixmap will contain time information via rainbow dot colors
1275 where each individual strokes begins."
1276 (save-excursion
1277 (let ((buf (get-buffer-create (or bufname "*strokes-xpm*")))
1278 (stroke (strokes-eliminate-consecutive-redundancies
1279 (strokes-fill-stroke
1280 (strokes-renormalize-to-grid stroke 31))))
1281 (lift-flag t)
1282 (rainbow-chars (list ?R ?O ?Y ?G ?B ?P)) ; ROYGBIV w/o indigo
1283 (header (format "/* XPM */
1284 static char * stroke_xpm[] = {
1285 /* width height ncolors cpp [x_hot y_hot] */
1286 \"33 33 9 1 26 23\",
1287 /* colors */
1288 \" c #FFFFFFFFFFFF\",
1289 \"* s iconColor1 m black c black\",
1290 \"R c #FFFF00000000\",
1291 \"O c #FFFF80000000\",
1292 \"Y c #FFFFFFFF0000\",
1293 \"G c #0000FFFF0000\",
1294 \"B c #00000000FFFF\",
1295 \"P c #FFFF0000FFFF\",
1296 \". c #45458B8B0000\",
1297 /* pixels */")))
1298 (set-buffer buf)
1299 (erase-buffer)
1300 (insert header)
1301 (loop repeat 33 do
1302 (newline)
1303 (insert-char ?\")
1304 (insert-char ?\ 33)
1305 (insert "\",")
1306 finally (insert "}\n"))
1307 (loop for point in stroke
1308 for x = (car-safe point)
1309 for y = (cdr-safe point) do
1310 (cond ((consp point)
1311 ;; draw a point, and possibly a starting-point
1312 (if lift-flag
1313 ;; mark starting point with the appropriate color
1314 (let ((char (or (car rainbow-chars) ?\.)))
1315 (loop for i from 0 to 2 do
1316 (loop for j from 0 to 2 do
1317 (goto-line (+ 16 i y))
1318 (forward-char (+ 1 j x))
1319 (delete-char 1)
1320 (insert-char char)))
1321 (setq rainbow-chars (cdr rainbow-chars)
1322 lift-flag nil))
1323 ;; Otherwise, just plot the point...
1324 (goto-line (+ 17 y))
1325 (forward-char (+ 2 x))
1326 (subst-char-in-region (point) (1+ (point)) ?\ ?\*)))
1327 ((strokes-lift-p point)
1328 ;; a lift--tell the loop to X out the next point...
1329 (setq lift-flag t)))))))
1330
1331 ;;;###autoload
1332 (defun strokes-list-strokes (&optional stroke-map)
1333 "Pop up a buffer containing a listing of all strokes defined in STROKE-MAP.
1334 If STROKE-MAP is not given, `strokes-global-map' will be used instead."
1335 (interactive)
1336 (push-window-configuration)
1337 (set-buffer (get-buffer-create "*Strokes List*"))
1338 (setq buffer-read-only nil)
1339 (erase-buffer)
1340 (insert
1341 "Command Stroke\n"
1342 "------- ------\n\n")
1343 (loop for def in (or stroke-map strokes-global-map) do
1344 (let ((stroke (car def))
1345 (command (cdr def)))
1346 (strokes-xpm-for-stroke stroke " *strokes-xpm*")
1347 (insert-char ?\ 60)
1348 (beginning-of-line)
1349 (insert (symbol-name command))
1350 (beginning-of-line)
1351 (forward-char 45)
1352 (make-annotation (make-glyph
1353 (list
1354 (vector 'xpm
1355 :data (buffer-substring
1356 (point-min " *strokes-xpm*")
1357 (point-max " *strokes-xpm*")
1358 " *strokes-xpm*"))
1359 [string :data "[Image]"]))
1360 (point) 'text)
1361 (newline 2)))
1362 (view-buffer "*Strokes List*" t)
1363 (goto-char (point-min))
1364 ;; (define-key
1365 ;; (current-local-map (get-buffer "*Strokes List*"))
1366 ;; [(q)]
1367 ;; 'pop-window-configuration))
1368 )
1369
1370 ;;;###autoload
1371 (defalias 'list-strokes 'strokes-list-strokes)
1372
1373 ;;;###autoload
1374 (defun strokes-mode (&optional arg)
1375 "Toggle strokes being enabled.
1376 With ARG, turn strokes on if and only if ARG is positive or true.
1377 Note that `strokes-mode' is a global mode. Think of it as a minor
1378 mode in all buffers when activated.
1379 By default, strokes are invoked with mouse button-2. You can define
1380 new strokes with
1381
1382 > M-x global-set-stroke"
1383 (interactive "P")
1384 (let ((on-p (if arg
1385 (> (prefix-numeric-value arg) 0)
1386 (not strokes-mode))))
1387 (cond ((not (device-on-window-system-p))
1388 (error "Can't use strokes without windows"))
1389 (on-p ; turn on strokes
1390 (and (file-exists-p strokes-file)
1391 (null strokes-global-map)
1392 (strokes-load-user-strokes))
1393 (add-hook 'kill-emacs-hook
1394 'strokes-prompt-user-save-strokes)
1395 (add-hook 'select-frame-hook
1396 'strokes-update-window-configuration)
1397 (strokes-update-window-configuration)
1398 (define-key global-map [(button2)] 'strokes-do-stroke)
1399 (define-key global-map [(control button2)] 'strokes-do-stroke)
1400 (define-key global-map [(shift button2)]
1401 'strokes-do-complex-stroke)
1402 (ad-activate-regexp "^strokes-") ; advise button2 commands
1403 (setq strokes-mode t))
1404 (t ; turn off strokes
1405 (if (get-buffer strokes-buffer-name)
1406 (kill-buffer (get-buffer strokes-buffer-name)))
1407 (remove-hook 'select-frame-hook
1408 'strokes-update-window-configuration)
1409 (if (string-match "^strokes-" (symbol-name (key-binding [(button2)])))
1410 (define-key global-map [(button2)] strokes-click-command))
1411 (if (string-match "^strokes-" (symbol-name (key-binding [(control button2)])))
1412 (global-unset-key [(control button2)]))
1413 (if (string-match "^strokes-" (symbol-name (key-binding [(shift button2)])))
1414 (global-unset-key [(shift button2)]))
1415 (ad-deactivate-regexp "^strokes-") ; unadvise strokes-button2 commands
1416 (setq strokes-mode nil))))
1417 (redraw-modeline))
1418
1419 (add-minor-mode 'strokes-mode strokes-modeline-string nil nil 'strokes-mode)
1420
1421 (provide 'strokes)
1422 (run-hooks 'strokes-load-hook)
1423
1424 ;;; strokes.el ends here