Further speed up rpm completion, by caching the installed packages
[bpt/emacs.git] / lisp / strokes.el
CommitLineData
7bd27aed 1;;; strokes.el --- control Emacs through mouse strokes
aea01cd7 2
acaf905b 3;; Copyright (C) 1997, 2000-2012 Free Software Foundation, Inc.
aea01cd7 4
41c86e21 5;; Author: David Bakhash <cadet@alum.mit.edu>
8f21d64e 6;; Maintainer: FSF
aea01cd7
RS
7;; Keywords: lisp, mouse, extensions
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
aea01cd7 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
aea01cd7
RS
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
aea01cd7
RS
23
24;;; Commentary:
25
26;; This is the strokes package. It is intended to allow the user to
27;; control Emacs by means of mouse strokes. Once strokes is loaded, you
28;; can always get help be invoking `strokes-help':
29
30;; > M-x strokes-help
31
32;; and you can learn how to use the package. A mouse stroke, for now,
ded4da95
DL
33;; can be defined as holding the shift key and the middle button, for
34;; instance, and then moving the mouse in whatever pattern you wish,
35;; which you have set Emacs to understand as mapping to a given
36;; command. For example, you may wish the have a mouse stroke that
37;; looks like a capital `C' which means `copy-region-as-kill'. Treat
38;; strokes just like you do key bindings. For example, Emacs sets key
39;; bindings globally with the `global-set-key' command. Likewise, you
40;; can do
aea01cd7 41
ded4da95 42;; > M-x strokes-global-set-stroke
aea01cd7
RS
43
44;; to interactively program in a stroke. It would be wise to set the
45;; first one to this very command, so that from then on, you invoke
ded4da95
DL
46;; `strokes-global-set-stroke' with a stroke. Likewise, there may
47;; eventually be a `strokes-local-set-stroke' command, also analogous
48;; to `local-set-key'.
aea01cd7
RS
49
50;; You can always unset the last stroke definition with the command
51
52;; > M-x strokes-unset-last-stroke
53
54;; and the last stroke that was added to `strokes-global-map' will be
55;; removed.
56
57;; Other analogies between strokes and key bindings are as follows:
58
59;; 1) To describe a stroke binding, you can type
60
ded4da95 61;; > M-x strokes-describe-stroke
aea01cd7
RS
62
63;; analogous to `describe-key'. It's also wise to have a stroke,
64;; like an `h', for help, or a `?', mapped to `describe-stroke'.
65
66;; 2) stroke bindings are set internally through the Lisp function
ded4da95
DL
67;; `strokes-define-stroke', similar to the `define-key' function.
68;; some examples for a 3x3 stroke grid would be
aea01cd7 69
ded4da95 70;; (strokes-define-stroke c-mode-stroke-map
aea01cd7
RS
71;; '((0 . 0) (1 . 1) (2 . 2))
72;; 'kill-region)
ded4da95 73;; (strokes-define-stroke strokes-global-map
aea01cd7
RS
74;; '((0 . 0) (0 . 1) (0 . 2) (1 . 2) (2 . 2))
75;; 'list-buffers)
76
77;; however, if you would probably just have the user enter in the
78;; stroke interactively and then set the stroke to whatever he/she
99e0e3e2 79;; entered. The Lisp function to interactively read a stroke is
aea01cd7
RS
80;; `strokes-read-stroke'. This is especially helpful when you're
81;; on a fast computer that can handle a 9x9 stroke grid.
82
83;; NOTE: only global stroke bindings are currently implemented,
84;; however mode- and buffer-local stroke bindings may eventually
85;; be implemented in a future version.
86
87;; The important variables to be aware of for this package are listed
88;; below. They can all be altered through the customizing package via
89
90;; > M-x customize
91
92;; and customizing the group named `strokes'. You can also read
93;; documentation on the variables there.
94
95;; `strokes-minimum-match-score' (determines the threshold of error that
0d0db51e 96;; makes a stroke acceptable or unacceptable. If your strokes aren't
aea01cd7
RS
97;; matching, then you should raise this variable.
98
99;; `strokes-grid-resolution' (determines the grid dimensions that you use
100;; when defining/reading strokes. The finer the grid your computer can
101;; handle, the more you can do, but even a 3x3 grid is pretty cool.)
a81b56d5 102;; The default value (9) should be fine for most decent computers.
aea01cd7
RS
103;; NOTE: This variable should not be set to a number less than 3.
104
105;; `strokes-display-strokes-buffer' will allow you to hide the strokes
106;; buffer when doing simple strokes. This is a speedup for slow
107;; computers as well as people who don't want to see their strokes.
108
109;; If you find that your mouse is accelerating too fast, you can
ded4da95 110;; execute an X command to slow it down. A good possibility is
aea01cd7
RS
111
112;; % xset m 5/4 8
113
114;; which seems, heuristically, to work okay, without much disruption.
115
116;; Whenever you load in the strokes package, you will be able to save
117;; what you've done upon exiting Emacs. You can also do
118
ded4da95 119;; > M-x strokes-prompt-user-save-strokes
aea01cd7
RS
120
121;; and it will save your strokes in ~/.strokes, or you may wish to change
122;; this by setting the variable `strokes-file'.
123
124;; Note that internally, all of the routines that are part of this
125;; package are able to deal with complex strokes, as they are a superset
126;; of simple strokes. However, the default of this package will map
ded4da95
DL
127;; S-mouse-2 to the command `strokes-do-stroke', and M-mouse-2 to
128;; `strokes-do-complex-stroke'. Complex strokes are terminated
129;; with mouse button 3.
aea01cd7 130
ded4da95 131;; You can also toggle between strokes mode by simple typing
aea01cd7
RS
132
133;; > M-x strokes-mode
134
ded4da95
DL
135;; I hope that, with the help of others, this package will be useful
136;; in entering in pictographic-like language text using the mouse
137;; (i.e. Korean). Japanese and Chinese are a bit trickier, but I'm
138;; sure that with help it can be done. The next version will allow
139;; the user to enter strokes which "remove the pencil from the paper"
140;; so to speak, so one character can have multiple strokes.
aea01cd7 141
efae28d8
EZ
142;; NOTE (Oct 7, 2006): The URLs below seem to be invalid!!!
143
aea01cd7
RS
144;; You can read more about strokes at:
145
146;; http://www.mit.edu/people/cadet/strokes-help.html
147
148;; If you're interested in using strokes for writing English into Emacs
149;; using strokes, then you'll want to read about it on the web page above
150;; or just download from http://www.mit.edu/people/cadet/strokes-abc.el,
151;; which is nothing but a file with some helper commands for inserting
152;; alphanumerics and punctuation.
153
0d0db51e
DL
154;; Great thanks to Rob Ristroph for his generosity in letting me use
155;; his PC to develop this, Jason Johnson for his help in algorithms,
156;; Euna Kim for her help in Korean, and massive thanks to the helpful
157;; guys on the help instance on athena (zeno, jered, amu, gsstark,
158;; ghudson, etc) Special thanks to Steve Baur, Kyle Jones, and Hrvoje
159;; Niksic for all their help. And special thanks to Dave Gillespie
160;; for all the elisp help--he is responsible for helping me use the cl
161;; macros at (near) max speed.
aea01cd7
RS
162
163;; Tasks: (what I'm getting ready for future version)...
ded4da95 164;; 2) use 'strokes-read-complex-stroke for Korean, etc.
aea01cd7 165;; 4) buffer-local 'strokes-local-map, and mode-stroke-maps would be nice
aea01cd7
RS
166;; 6) add some hooks, like `strokes-read-stroke-hook'
167;; 7) See what people think of the factory settings. Should I change
168;; them? They're all pretty arbitrary in a way. I guess they
169;; should be minimal, but computers are getting lots faster, and
170;; if I choose the defaults too conservatively, then strokes will
0d0db51e 171;; surely disappoint some people on decent machines (until they
aea01cd7
RS
172;; figure out M-x customize). I need feedback.
173;; Other: I always have the most beta version of strokes, so if you
174;; want it just let me know.
175
f73c072c
DL
176;; Fixme: Use pbm instead of xpm for pixmaps to work generally.
177
aea01cd7
RS
178;;; Code:
179
180;;; Requirements and provisions...
181
aea01cd7 182(autoload 'mail-position-on-field "sendmail")
0d0db51e 183(eval-when-compile (require 'cl))
aea01cd7
RS
184
185;;; Constants...
186
0d0db51e 187(defconst strokes-lift :strokes-lift
aea01cd7 188 "Symbol representing a stroke lift event for complex strokes.
0d0db51e
DL
189Complex strokes are those which contain two or more simple strokes.")
190
191(defconst strokes-xpm-header "/* XPM */
192static char * stroke_xpm[] = {
193/* width height ncolors cpp [x_hot y_hot] */
194\"33 33 9 1 26 23\",
195/* colors */
196\" c none s none\",
197\"* c #000000 s foreground\",
198\"R c #FFFF00000000\",
199\"O c #FFFF80000000\",
200\"Y c #FFFFFFFF0000\",
201\"G c #0000FFFF0000\",
202\"B c #00000000FFFF\",
203\"P c #FFFF0000FFFF\",
204\". c #45458B8B0000\",
205/* pixels */\n"
ded4da95 206 "The header to all xpm buffers created by strokes.")
aea01cd7
RS
207
208;;; user variables...
209
210(defgroup strokes nil
61bf4252 211 "Control Emacs through mouse strokes."
ded4da95 212 :link '(emacs-commentary-link "strokes")
aea01cd7
RS
213 :group 'mouse)
214
37269466
CY
215(defcustom strokes-lighter " Strokes"
216 "Mode line identifier for Strokes mode."
aea01cd7
RS
217 :type 'string
218 :group 'strokes)
219
37269466
CY
220(define-obsolete-variable-alias 'strokes-modeline-string 'strokes-lighter "24.2")
221
aea01cd7 222(defcustom strokes-character ?@
9201cc28 223 "Character used when drawing strokes in the strokes buffer.
0d0db51e 224\(The default is `@', which works well.\)"
aea01cd7
RS
225 :type 'character
226 :group 'strokes)
227
228(defcustom strokes-minimum-match-score 1000
9201cc28 229 "Minimum score for a stroke to be considered a possible match.
0d0db51e 230Setting this variable to 0 would require a perfectly precise match.
aea01cd7
RS
231The default value is 1000, but it's mostly dependent on how precisely
232you manage to replicate your user-defined strokes. It also depends on
233the value of `strokes-grid-resolution', since a higher grid resolution
234will correspond to more sample points, and thus more distance
235measurements. Usually, this is not a problem since you first set
236`strokes-grid-resolution' based on what your computer seems to be able
ded4da95 237to handle (though the defaults are usually more than sufficient), and
aea01cd7
RS
238then you can set `strokes-minimum-match-score' to something that works
239for you. The only purpose of this variable is to insure that if you
240do a bogus stroke that really doesn't match any of the predefined
241ones, then strokes should NOT pick the one that came closest."
242 :type 'integer
243 :group 'strokes)
244
245(defcustom strokes-grid-resolution 9
9201cc28 246 "Integer defining dimensions of the stroke grid.
69c3280d 247The grid is a square grid, where `strokes-grid-resolution' defaults to
aea01cd7 248`9', making a 9x9 grid whose coordinates go from (0 . 0) on the top
69c3280d 249left to ((strokes-grid-resolution - 1) . (strokes-grid-resolution - 1))
aea01cd7
RS
250on the bottom right. The greater the resolution, the more intricate
251your strokes can be.
252NOTE: This variable should be odd and MUST NOT be less than 3 and need
253 not be greater than 33, which is the resolution of the pixmaps.
254WARNING: Changing the value of this variable will gravely affect the
255 strokes you have already programmed in. You should try to
256 figure out what it should be based on your needs and on how
257 quick the particular platform(s) you're operating on, and
258 only then start programming in your custom strokes."
259 :type 'integer
260 :group 'strokes)
261
364ce449 262(defcustom strokes-file (convert-standard-filename "~/.strokes")
9201cc28 263 "File containing saved strokes for Strokes mode (default is ~/.strokes)."
aea01cd7
RS
264 :type 'file
265 :group 'strokes)
266
ded4da95
DL
267(defvar strokes-buffer-name " *strokes*"
268 "The name of the buffer that the strokes take place in.")
aea01cd7
RS
269
270(defcustom strokes-use-strokes-buffer t
9201cc28 271 "If non-nil, the strokes buffer is used and strokes are displayed.
aea01cd7
RS
272If nil, strokes will be read the same, however the user will not be
273able to see the strokes. This be helpful for people who don't like
274the delay in switching to the strokes buffer."
275 :type 'boolean
276 :group 'strokes)
277
aea01cd7
RS
278;;; internal variables...
279
aea01cd7
RS
280(defvar strokes-window-configuration nil
281 "The special window configuration used when entering strokes.
282This is set properly in the function `strokes-update-window-configuration'.")
283
284(defvar strokes-last-stroke nil
285 "Last stroke entered by the user.
286Its value gets set every time the function
287`strokes-fill-stroke' gets called,
69c3280d 288since that is the best time to set the variable.")
aea01cd7
RS
289
290(defvar strokes-global-map '()
291 "Association list of strokes and their definitions.
292Each entry is (STROKE . COMMAND) where STROKE is itself a list of
293coordinates (X . Y) where X and Y are lists of positions on the
294normalized stroke grid, with the top left at (0 . 0). COMMAND is the
69c3280d 295corresponding interactive function.")
aea01cd7
RS
296
297(defvar strokes-load-hook nil
69c3280d 298 "Functions to be called when Strokes is loaded.")
aea01cd7 299
0d0db51e
DL
300;;; ### NOT IMPLEMENTED YET ###
301;;(defvar edit-strokes-menu
302;; '("Edit-Strokes"
303;; ["Add stroke..." strokes-global-set-stroke t]
304;; ["Delete stroke..." strokes-edit-delete-stroke t]
305;; ["Change stroke" strokes-smaller t]
306;; ["Change definition" strokes-larger t]
307;; ["[Re]List Strokes chronologically" strokes-list-strokes t]
308;; ["[Re]List Strokes alphabetically" strokes-list-strokes t]
309;; ["Quit" strokes-edit-quit t]
310;; ))
311
aea01cd7
RS
312;;; Macros...
313
ded4da95
DL
314;; unused
315;; (defmacro strokes-while-inhibiting-garbage-collector (&rest forms)
316;; "Execute FORMS without interference from the garbage collector."
317;; `(let ((gc-cons-threshold 134217727))
318;; ,@forms))
0d0db51e 319
aea01cd7
RS
320(defsubst strokes-click-p (stroke)
321 "Non-nil if STROKE is really click."
7bd27aed 322 (< (length stroke) 2))
aea01cd7
RS
323
324;;; old, but worked pretty good (just in case)...
325;;(defmacro strokes-define-stroke (stroke-map stroke def)
326;; "Add STROKE to STROKE-MAP alist with given command DEF"
7bd27aed 327;; (list 'if (list '< (list 'length stroke) 2)
aea01cd7
RS
328;; (list 'error
329;; "That's a click, not a stroke. See `strokes-click-command'")
330;; (list 'setq stroke-map (list 'cons (list 'cons stroke def)
331;; (list 'remassoc stroke stroke-map)))))
332
333(defsubst strokes-remassoc (key list)
0d0db51e
DL
334 (let (elt)
335 (while (setq elt (assoc key list))
336 (setq list (delete elt list))))
337 list)
aea01cd7
RS
338
339(defmacro strokes-define-stroke (stroke-map stroke def)
340 "Add STROKE to STROKE-MAP alist with given command DEF."
341 `(if (strokes-click-p ,stroke)
ded4da95 342 (error "That's a click, not a stroke")
aea01cd7
RS
343 (setq ,stroke-map (cons (cons ,stroke ,def)
344 (strokes-remassoc ,stroke ,stroke-map)))))
345
aea01cd7 346(defsubst strokes-square (x)
ded4da95 347 "Return the square of the number X."
aea01cd7
RS
348 (* x x))
349
350(defsubst strokes-distance-squared (p1 p2)
351 "Gets the distance (squared) between to points P1 and P2.
352P1 and P2 are cons cells in the form (X . Y)."
353 (let ((x1 (car p1))
354 (y1 (cdr p1))
355 (x2 (car p2))
356 (y2 (cdr p2)))
357 (+ (strokes-square (- x2 x1))
358 (strokes-square (- y2 y1)))))
359
aea01cd7
RS
360;;; Functions...
361
362(defsubst strokes-mouse-event-p (event)
0d0db51e
DL
363 (and (consp event) (symbolp (car event))
364 (or (eq (car event) 'mouse-movement)
365 (memq 'click (get (car event) 'event-symbol-elements))
366 (memq 'down (get (car event) 'event-symbol-elements))
367 (memq 'drag (get (car event) 'event-symbol-elements)))))
368
369(defsubst strokes-button-press-event-p (event)
370 (and (consp event) (symbolp (car event))
371 (memq 'down (get (car event) 'event-symbol-elements))))
372
373(defsubst strokes-button-release-event-p (event)
374 (and (consp event) (symbolp (car event))
375 (or (memq 'click (get (car event) 'event-symbol-elements))
376 (memq 'drag (get (car event) 'event-symbol-elements)))))
aea01cd7
RS
377
378(defun strokes-event-closest-point-1 (window &optional line)
379 "Return position of start of line LINE in WINDOW.
380If LINE is nil, return the last position visible in WINDOW."
381 (let* ((total (- (window-height window)
382 (if (window-minibuffer-p window)
383 0 1)))
384 (distance (or line total)))
385 (save-excursion
386 (goto-char (window-start window))
387 (if (= (vertical-motion distance) distance)
388 (if (not line)
389 (forward-char -1)))
390 (point))))
391
392(defun strokes-event-closest-point (event &optional start-window)
393 "Return the nearest position to where EVENT ended its motion.
394This is computed for the window where EVENT's motion started,
ded4da95 395or for window START-WINDOW if that is specified."
aea01cd7
RS
396 (or start-window (setq start-window (posn-window (event-start event))))
397 (if (eq start-window (posn-window (event-end event)))
0d0db51e 398 (if (eq (posn-point (event-end event)) 'vertical-line)
aea01cd7
RS
399 (strokes-event-closest-point-1 start-window
400 (cdr (posn-col-row (event-end event))))
0d0db51e 401 (if (eq (posn-point (event-end event)) 'mode-line)
aea01cd7 402 (strokes-event-closest-point-1 start-window)
0d0db51e 403 (posn-point (event-end event))))
aea01cd7
RS
404 ;; EVENT ended in some other window.
405 (let* ((end-w (posn-window (event-end event)))
406 (end-w-top)
407 (w-top (nth 1 (window-edges start-window))))
408 (setq end-w-top
409 (if (windowp end-w)
410 (nth 1 (window-edges end-w))
411 (/ (cdr (posn-x-y (event-end event)))
7bd27aed 412 (frame-char-height end-w))))
aea01cd7
RS
413 (if (>= end-w-top w-top)
414 (strokes-event-closest-point-1 start-window)
415 (window-start start-window)))))
416
417(defun strokes-lift-p (object)
0d0db51e 418 "Return non-nil if OBJECT is a stroke-lift."
aea01cd7
RS
419 (eq object strokes-lift))
420
421(defun strokes-unset-last-stroke ()
422 "Undo the last stroke definition."
423 (interactive)
424 (let ((command (cdar strokes-global-map)))
7bd27aed 425 (if (y-or-n-p
ded4da95 426 (format "Really delete last stroke definition, defined to `%s'? "
aea01cd7
RS
427 command))
428 (progn
429 (setq strokes-global-map (cdr strokes-global-map))
430 (message "That stroke has been deleted"))
431 (message "Nothing done"))))
432
433;;;###autoload
434(defun strokes-global-set-stroke (stroke command)
435 "Interactively give STROKE the global binding as COMMAND.
436Operated just like `global-set-key', except for strokes.
437COMMAND is a symbol naming an interactively-callable function. STROKE
438is a list of sampled positions on the stroke grid as described in the
99e0e3e2
EZ
439documentation for the `strokes-define-stroke' function.
440
441See also `strokes-global-set-stroke-string'."
aea01cd7
RS
442 (interactive
443 (list
444 (and (or strokes-mode (strokes-mode t))
445 (strokes-read-complex-stroke
ded4da95
DL
446 "Draw with mouse button 1 (or 2). End with button 3..."))
447 (read-command "Command to map stroke to: ")))
aea01cd7
RS
448 (strokes-define-stroke strokes-global-map stroke command))
449
99e0e3e2
EZ
450(defun strokes-global-set-stroke-string (stroke string)
451 "Interactively give STROKE the global binding as STRING.
452Operated just like `global-set-key', except for strokes. STRING
453is a string to be inserted by the stroke. STROKE is a list of
454sampled positions on the stroke grid as described in the
455documentation for the `strokes-define-stroke' function.
456
457Compare `strokes-global-set-stroke'."
458 (interactive
459 (list
460 (and (or strokes-mode (strokes-mode t))
461 (strokes-read-complex-stroke
462 "Draw with mouse button 1 (or 2). End with button 3..."))
463 (read-string "String to map stroke to: ")))
464 (strokes-define-stroke strokes-global-map stroke string))
465
aea01cd7
RS
466;;(defun global-unset-stroke (stroke); FINISH THIS DEFUN!
467;; "delete all strokes matching STROKE from `strokes-global-map',
468;; letting the user input
469;; the stroke with the mouse"
470;; (interactive
471;; (list
472;; (strokes-read-stroke "Enter the stroke you want to delete...")))
473;; (strokes-define-stroke 'strokes-global-map stroke command))
474
475(defun strokes-get-grid-position (stroke-extent position &optional grid-resolution)
ded4da95 476 "Map POSITION to a new grid position.
69c3280d 477Do so based on its STROKE-EXTENT and GRID-RESOLUTION.
aea01cd7
RS
478STROKE-EXTENT as a list \(\(XMIN . YMIN\) \(XMAX . YMAX\)\).
479If POSITION is a `strokes-lift', then it is itself returned.
69c3280d 480Optional GRID-RESOLUTION may be used in place of `strokes-grid-resolution'.
ded4da95 481The grid is a square whose dimension is [0,GRID-RESOLUTION)."
aea01cd7
RS
482 (cond ((consp position) ; actual pixel location
483 (let ((grid-resolution (or grid-resolution strokes-grid-resolution))
484 (x (car position))
485 (y (cdr position))
486 (xmin (caar stroke-extent))
487 (ymin (cdar stroke-extent))
488 ;; the `1+' is there to insure that the
489 ;; formula evaluates correctly at the boundaries
0d0db51e
DL
490 (xmax (1+ (car (cadr stroke-extent))))
491 (ymax (1+ (cdr (cadr stroke-extent)))))
aea01cd7
RS
492 (cons (floor (* grid-resolution
493 (/ (float (- x xmin))
494 (- xmax xmin))))
495 (floor (* grid-resolution
496 (/ (float (- y ymin))
497 (- ymax ymin)))))))
498 ((strokes-lift-p position) ; stroke lift
499 strokes-lift)))
500
aea01cd7 501(defun strokes-get-stroke-extent (pixel-positions)
ded4da95 502 "From a list of absolute PIXEL-POSITIONS, return absolute spatial extent.
aea01cd7
RS
503The return value is a list ((XMIN . YMIN) (XMAX . YMAX))."
504 (if pixel-positions
505 (let ((xmin (caar pixel-positions))
506 (xmax (caar pixel-positions))
507 (ymin (cdar pixel-positions))
508 (ymax (cdar pixel-positions))
509 (rest (cdr pixel-positions)))
510 (while rest
511 (if (consp (car rest))
512 (let ((x (caar rest))
513 (y (cdar rest)))
514 (if (< x xmin)
515 (setq xmin x))
516 (if (> x xmax)
517 (setq xmax x))
518 (if (< y ymin)
519 (setq ymin y))
520 (if (> y ymax)
521 (setq ymax y))))
522 (setq rest (cdr rest)))
523 (let ((delta-x (- xmax xmin))
524 (delta-y (- ymax ymin)))
525 (if (> delta-x delta-y)
526 (setq ymin (- ymin
527 (/ (- delta-x delta-y)
528 2))
529 ymax (+ ymax
530 (/ (- delta-x delta-y)
531 2)))
532 (setq xmin (- xmin
533 (/ (- delta-y delta-x)
534 2))
535 xmax (+ xmax
536 (/ (- delta-y delta-x)
537 2))))
538 (list (cons xmin ymin)
539 (cons xmax ymax))))
540 nil))
541
542(defun strokes-eliminate-consecutive-redundancies (entries)
ded4da95 543 "Return a list with no consecutive redundant entries."
aea01cd7
RS
544 ;; defun a grande vitesse grace a Dave G.
545 (loop for element on entries
546 if (not (equal (car element) (cadr element)))
547 collect (car element)))
548;; (loop for element on entries
549;; nconc (if (not (equal (car el) (cadr el)))
550;; (list (car el)))))
551;; yet another (orig) way of doing it...
552;; (if entries
553;; (let* ((current (car entries))
554;; (rest (cdr entries))
555;; (non-redundant-list (list current))
556;; (next nil))
557;; (while rest
558;; (setq next (car rest))
559;; (if (equal current next)
560;; (setq rest (cdr rest))
561;; (setq non-redundant-list (cons next non-redundant-list)
562;; current next
563;; rest (cdr rest))))
564;; (nreverse non-redundant-list))
565;; nil))
566
567(defun strokes-renormalize-to-grid (positions &optional grid-resolution)
568 "Map POSITIONS to a new grid whose dimensions are based on GRID-RESOLUTION.
569POSITIONS is a list of positions and stroke-lifts.
69c3280d 570Optional GRID-RESOLUTION may be used in place of `strokes-grid-resolution'.
ded4da95 571The grid is a square whose dimension is [0,GRID-RESOLUTION)."
aea01cd7
RS
572 (or grid-resolution (setq grid-resolution strokes-grid-resolution))
573 (let ((stroke-extent (strokes-get-stroke-extent positions)))
574 (mapcar (function
575 (lambda (pos)
576 (strokes-get-grid-position stroke-extent pos grid-resolution)))
577 positions)))
578
aea01cd7
RS
579(defun strokes-fill-stroke (unfilled-stroke &optional force)
580 "Fill in missing grid locations in the list of UNFILLED-STROKE.
581If FORCE is non-nil, then fill the stroke even if it's `stroke-click'.
582NOTE: This is where the global variable `strokes-last-stroke' is set."
583 (setq strokes-last-stroke ; this is global
584 (if (and (strokes-click-p unfilled-stroke)
585 (not force))
586 unfilled-stroke
587 (loop for grid-locs on unfilled-stroke
588 nconc (let* ((current (car grid-locs))
589 (current-is-a-point-p (consp current))
590 (next (cadr grid-locs))
591 (next-is-a-point-p (consp next))
592 (both-are-points-p (and current-is-a-point-p
593 next-is-a-point-p))
594 (x1 (and current-is-a-point-p
595 (car current)))
596 (y1 (and current-is-a-point-p
597 (cdr current)))
598 (x2 (and next-is-a-point-p
599 (car next)))
600 (y2 (and next-is-a-point-p
601 (cdr next)))
602 (delta-x (and both-are-points-p
603 (- x2 x1)))
604 (delta-y (and both-are-points-p
605 (- y2 y1)))
606 (slope (and both-are-points-p
607 (if (zerop delta-x)
608 nil ; undefined vertical slope
609 (/ (float delta-y)
610 delta-x)))))
611 (cond ((not both-are-points-p)
612 (list current))
ded4da95 613 ((null slope) ; undefined vertical slope
aea01cd7
RS
614 (if (>= delta-y 0)
615 (loop for y from y1 below y2
616 collect (cons x1 y))
617 (loop for y from y1 above y2
618 collect (cons x1 y))))
619 ((zerop slope) ; (= y1 y2)
620 (if (>= delta-x 0)
621 (loop for x from x1 below x2
622 collect (cons x y1))
623 (loop for x from x1 above x2
624 collect (cons x y1))))
625 ((>= (abs delta-x) (abs delta-y))
626 (if (> delta-x 0)
627 (loop for x from x1 below x2
628 collect (cons x
629 (+ y1
630 (round (* slope
631 (- x x1))))))
632 (loop for x from x1 above x2
633 collect (cons x
634 (+ y1
635 (round (* slope
636 (- x x1))))))))
637 (t ; (< (abs delta-x) (abs delta-y))
638 (if (> delta-y 0)
639 (loop for y from y1 below y2
640 collect (cons (+ x1
641 (round (/ (- y y1)
642 slope)))
643 y))
644 (loop for y from y1 above y2
645 collect (cons (+ x1
646 (round (/ (- y y1)
647 slope)))
648 y))))))))))
649
650(defun strokes-rate-stroke (stroke1 stroke2)
ded4da95 651 "Rates STROKE1 with STROKE2 and return a score based on a distance metric.
aea01cd7
RS
652Note: the rating is an error rating, and therefore, a return of 0
653represents a perfect match. Also note that the order of stroke
654arguments is order-independent for the algorithm used here."
655 (if (and stroke1 stroke2)
656 (let ((rest1 (cdr stroke1))
657 (rest2 (cdr stroke2))
658 (err (strokes-distance-squared (car stroke1)
659 (car stroke2))))
660 (while (and rest1 rest2)
661 (while (and (consp (car rest1))
662 (consp (car rest2)))
663 (setq err (+ err
664 (strokes-distance-squared (car rest1)
665 (car rest2)))
666 stroke1 rest1
667 stroke2 rest2
668 rest1 (cdr stroke1)
669 rest2 (cdr stroke2)))
670 (cond ((and (strokes-lift-p (car rest1))
671 (strokes-lift-p (car rest2)))
672 (setq rest1 (cdr rest1)
673 rest2 (cdr rest2)))
674 ((strokes-lift-p (car rest2))
675 (while (consp (car rest1))
676 (setq err (+ err
677 (strokes-distance-squared (car rest1)
678 (car stroke2)))
679 rest1 (cdr rest1))))
680 ((strokes-lift-p (car rest1))
681 (while (consp (car rest2))
682 (setq err (+ err
683 (strokes-distance-squared (car stroke1)
684 (car rest2)))
685 rest2 (cdr rest2))))))
686 (if (null rest2)
687 (while (consp (car rest1))
688 (setq err (+ err
689 (strokes-distance-squared (car rest1)
690 (car stroke2)))
691 rest1 (cdr rest1))))
692 (if (null rest1)
693 (while (consp (car rest2))
694 (setq err (+ err
695 (strokes-distance-squared (car stroke1)
696 (car rest2)))
697 rest2 (cdr rest2))))
698 (if (or (strokes-lift-p (car rest1))
699 (strokes-lift-p (car rest2)))
700 (setq err nil)
701 err))
702 nil))
703
704(defun strokes-match-stroke (stroke stroke-map)
ded4da95 705 "Find the best matching command of STROKE in STROKE-MAP.
aea01cd7
RS
706Returns the corresponding match as (COMMAND . SCORE)."
707 (if (and stroke stroke-map)
708 (let ((score (strokes-rate-stroke stroke (caar stroke-map)))
709 (command (cdar stroke-map))
710 (map (cdr stroke-map)))
711 (while map
712 (let ((newscore (strokes-rate-stroke stroke (caar map))))
713 (if (or (and newscore score (< newscore score))
714 (and newscore (null score)))
715 (setq score newscore
716 command (cdar map)))
717 (setq map (cdr map))))
718 (if score
719 (cons command score)
720 nil))
721 nil))
722
8aa88760
GM
723(defsubst strokes-fill-current-buffer-with-whitespace ()
724 "Erase the contents of the current buffer and fill it with whitespace."
725 (erase-buffer)
726 (loop repeat (frame-height) do
727 (insert-char ?\s (1- (frame-width)))
728 (newline))
729 (goto-char (point-min)))
730
aea01cd7
RS
731;;;###autoload
732(defun strokes-read-stroke (&optional prompt event)
733 "Read a simple stroke (interactively) and return the stroke.
734Optional PROMPT in minibuffer displays before and during stroke reading.
735This function will display the stroke interactively as it is being
736entered in the strokes buffer if the variable
737`strokes-use-strokes-buffer' is non-nil.
69c3280d 738Optional EVENT is acceptable as the starting event of the stroke."
aea01cd7 739 (save-excursion
7bd27aed
RS
740 (let ((pix-locs nil)
741 (grid-locs nil)
742 (safe-to-draw-p nil))
743 (if strokes-use-strokes-buffer
744 ;; switch to the strokes buffer and
745 ;; display the stroke as it's being read
746 (save-window-excursion
747 (set-window-configuration strokes-window-configuration)
721be9cd
TH
748 ;; The frame has been resized, so we need to refill the
749 ;; strokes buffer so that the strokes canvas is the whole
750 ;; visible buffer.
751 (unless (> 1 (abs (- (line-end-position) (window-width))))
752 (strokes-fill-current-buffer-with-whitespace))
7bd27aed 753 (when prompt
8a26c165 754 (message "%s" prompt)
7bd27aed 755 (setq event (read-event))
0d0db51e 756 (or (strokes-button-press-event-p event)
7bd27aed
RS
757 (error "You must draw with the mouse")))
758 (unwind-protect
759 (track-mouse
760 (or event (setq event (read-event)
761 safe-to-draw-p t))
0d0db51e 762 (while (not (strokes-button-release-event-p event))
aea01cd7
RS
763 (if (strokes-mouse-event-p event)
764 (let ((point (strokes-event-closest-point event)))
7bd27aed
RS
765 (if (and point safe-to-draw-p)
766 ;; we can draw that point
767 (progn
768 (goto-char point)
ded4da95 769 (subst-char-in-region point (1+ point)
61bf4252 770 ?\s strokes-character))
7bd27aed
RS
771 ;; otherwise, we can start drawing the next time...
772 (setq safe-to-draw-p t))
0d0db51e 773 (push (cdr (mouse-pixel-position))
aea01cd7 774 pix-locs)))
7bd27aed 775 (setq event (read-event)))))
aea01cd7 776 ;; protected
7bd27aed 777 ;; clean up strokes buffer and then bury it.
aea01cd7 778 (when (equal (buffer-name) strokes-buffer-name)
ded4da95 779 (subst-char-in-region (point-min) (point-max)
61bf4252 780 strokes-character ?\s)
aea01cd7 781 (goto-char (point-min))
7bd27aed
RS
782 (bury-buffer))))
783 ;; Otherwise, don't use strokes buffer and read stroke silently
784 (when prompt
8a26c165 785 (message "%s" prompt)
7bd27aed 786 (setq event (read-event))
0d0db51e 787 (or (strokes-button-press-event-p event)
7bd27aed
RS
788 (error "You must draw with the mouse")))
789 (track-mouse
790 (or event (setq event (read-event)))
0d0db51e 791 (while (not (strokes-button-release-event-p event))
7bd27aed 792 (if (strokes-mouse-event-p event)
0d0db51e 793 (push (cdr (mouse-pixel-position))
7bd27aed
RS
794 pix-locs))
795 (setq event (read-event))))
796 (setq grid-locs (strokes-renormalize-to-grid (nreverse pix-locs)))
ded4da95
DL
797 (strokes-fill-stroke
798 (strokes-eliminate-consecutive-redundancies grid-locs)))))
7bd27aed
RS
799
800;;;###autoload
801(defun strokes-read-complex-stroke (&optional prompt event)
802 "Read a complex stroke (interactively) and return the stroke.
803Optional PROMPT in minibuffer displays before and during stroke reading.
804Note that a complex stroke allows the user to pen-up and pen-down. This
ded4da95
DL
805is implemented by allowing the user to paint with button 1 or button 2 and
806then complete the stroke with button 3.
69c3280d 807Optional EVENT is acceptable as the starting event of the stroke."
7bd27aed
RS
808 (save-excursion
809 (save-window-excursion
810 (set-window-configuration strokes-window-configuration)
811 (let ((pix-locs nil)
812 (grid-locs nil))
813 (if prompt
0d0db51e 814 (while (not (strokes-button-press-event-p event))
8a26c165 815 (message "%s" prompt)
7bd27aed
RS
816 (setq event (read-event))))
817 (unwind-protect
818 (track-mouse
819 (or event (setq event (read-event)))
0d0db51e
DL
820 (while (not (and (strokes-button-press-event-p event)
821 (eq 'mouse-3
822 (car (get (car event)
823 'event-symbol-elements)))))
824 (while (not (strokes-button-release-event-p event))
7bd27aed
RS
825 (if (strokes-mouse-event-p event)
826 (let ((point (strokes-event-closest-point event)))
827 (when point
828 (goto-char point)
ded4da95 829 (subst-char-in-region point (1+ point)
61bf4252 830 ?\s strokes-character))
0d0db51e 831 (push (cdr (mouse-pixel-position))
7bd27aed
RS
832 pix-locs)))
833 (setq event (read-event)))
834 (push strokes-lift pix-locs)
0d0db51e 835 (while (not (strokes-button-press-event-p event))
7bd27aed
RS
836 (setq event (read-event))))
837 ;; ### KLUDGE! ### sit and wait
838 ;; for some useless event to
839 ;; happen to fix the minibuffer bug.
0d0db51e 840 (while (not (strokes-button-release-event-p (read-event))))
7bd27aed
RS
841 (setq pix-locs (nreverse (cdr pix-locs))
842 grid-locs (strokes-renormalize-to-grid pix-locs))
843 (strokes-fill-stroke
844 (strokes-eliminate-consecutive-redundancies grid-locs)))
845 ;; protected
846 (when (equal (buffer-name) strokes-buffer-name)
ded4da95 847 (subst-char-in-region (point-min) (point-max)
61bf4252 848 strokes-character ?\s)
7bd27aed
RS
849 (goto-char (point-min))
850 (bury-buffer)))))))
aea01cd7
RS
851
852(defun strokes-execute-stroke (stroke)
853 "Given STROKE, execute the command which corresponds to it.
854The command will be executed provided one exists for that stroke,
855based on the variable `strokes-minimum-match-score'.
856If no stroke matches, nothing is done and return value is nil."
857 (let* ((match (strokes-match-stroke stroke strokes-global-map))
858 (command (car match))
859 (score (cdr match)))
ded4da95 860 (cond ((and match (<= score strokes-minimum-match-score))
aea01cd7
RS
861 (message "%s" command)
862 (command-execute command))
863 ((null strokes-global-map)
864 (if (file-exists-p strokes-file)
7bd27aed 865 (and (y-or-n-p
aea01cd7
RS
866 (format "No strokes loaded. Load `%s'? "
867 strokes-file))
868 (strokes-load-user-strokes))
ded4da95 869 (error "No strokes defined; use `strokes-global-set-stroke'")))
aea01cd7
RS
870 (t
871 (error
872 "No stroke matches; see variable `strokes-minimum-match-score'")
873 nil))))
874
875;;;###autoload
876(defun strokes-do-stroke (event)
a81b56d5 877 "Read a simple stroke from the user and then execute its command.
aea01cd7
RS
878This must be bound to a mouse event."
879 (interactive "e")
880 (or strokes-mode (strokes-mode t))
881 (strokes-execute-stroke (strokes-read-stroke nil event)))
882
883;;;###autoload
884(defun strokes-do-complex-stroke (event)
a81b56d5 885 "Read a complex stroke from the user and then execute its command.
aea01cd7
RS
886This must be bound to a mouse event."
887 (interactive "e")
888 (or strokes-mode (strokes-mode t))
889 (strokes-execute-stroke (strokes-read-complex-stroke nil event)))
890
891;;;###autoload
892(defun strokes-describe-stroke (stroke)
893 "Displays the command which STROKE maps to, reading STROKE interactively."
894 (interactive
895 (list
896 (strokes-read-complex-stroke
ded4da95 897 "Enter stroke to describe; end with button 3...")))
aea01cd7 898 (let* ((match (strokes-match-stroke stroke strokes-global-map))
ded4da95 899 (command (car match))
aea01cd7 900 (score (cdr match)))
ded4da95
DL
901 (if (and match
902 (<= score strokes-minimum-match-score))
aea01cd7
RS
903 (message "That stroke maps to `%s'" command)
904 (message "That stroke is undefined"))
905 (sleep-for 1))) ; helpful for recursive edits
906
aea01cd7 907;;;###autoload
7bd27aed 908(defun strokes-help ()
69c3280d 909 "Get instruction on using the Strokes package."
7bd27aed 910 (interactive)
ded4da95
DL
911 (with-output-to-temp-buffer "*Help with Strokes*"
912 (princ
69c3280d
JB
913 (substitute-command-keys
914 "This is help for the strokes package.
aea01cd7 915
7bd27aed 916------------------------------------------------------------
aea01cd7 917
7bd27aed 918** Strokes...
aea01cd7 919
7bd27aed
RS
920The strokes package allows you to define strokes, made with
921the mouse or other pointer device, that Emacs can interpret as
922corresponding to commands, and then executes the commands. It does
923character recognition, so you don't have to worry about getting it
924right every time.
aea01cd7 925
0d0db51e 926Strokes also allows you to compose documents graphically. You can
a81b56d5 927fully edit documents in Chinese, Japanese, etc. based on Emacs
ded4da95 928strokes. Once you've done so, you can ASCII compress-and-encode them
0d0db51e
DL
929and then safely save them for later use, send letters to friends
930\(using Emacs, of course). Strokes will later decode these documents,
931extracting the strokes for editing use once again, so the editing
932cycle can continue.
933
7bd27aed
RS
934Strokes are easy to program and fun to use. To start strokes going,
935you'll want to put the following line in your .emacs file as mentioned
936in the commentary to strokes.el.
aea01cd7 937
7bd27aed
RS
938This will load strokes when and only when you start Emacs on a window
939system, with a mouse or other pointer device defined.
aea01cd7 940
7bd27aed 941To toggle strokes-mode, you just do
aea01cd7 942
7bd27aed 943> M-x strokes-mode
aea01cd7 944
0d0db51e 945** Strokes for controlling the behavior of Emacs...
aea01cd7 946
7bd27aed 947When you're ready to start defining strokes, just use the command
aea01cd7 948
ded4da95 949> M-x strokes-global-set-stroke
aea01cd7 950
7bd27aed 951You will see a ` *strokes*' buffer which is waiting for you to enter in
ded4da95
DL
952your stroke. When you enter in the stroke, you draw with button 1 or
953button 2, and then end with button 3. Next, you enter in the command
7bd27aed
RS
954which will be executed when that stroke is invoked. Simple as that.
955For now, try to define a stroke to copy a region. This is a popular
956edit command, so type
aea01cd7 957
ded4da95 958> M-x strokes-global-set-stroke
aea01cd7 959
0d0db51e 960Then, in the ` *strokes*' buffer, draw the letter `C' (for `copy')
7bd27aed 961and then, when it asks you to enter the command to map that to, type
aea01cd7 962
7bd27aed 963> copy-region-as-kill
aea01cd7 964
7bd27aed 965That's about as hard as it gets.
ded4da95 966Remember: paint with button 1 or button 2 and then end with button 3.
aea01cd7 967
7bd27aed 968If ever you want to know what a certain strokes maps to, then do
aea01cd7 969
ded4da95 970> M-x strokes-describe-stroke
aea01cd7 971
7bd27aed 972and you can enter in any arbitrary stroke. Remember: The strokes
0d0db51e 973package lets you program in simple and complex (multi-lift) strokes.
7bd27aed
RS
974The only difference is how you *invoke* the two. You will most likely
975use simple strokes, as complex strokes were developed for
ded4da95
DL
976Chinese/Japanese/Korean. So the shifted middle mouse button (S-mouse-2) will
977invoke the command `strokes-do-stroke'.
aea01cd7 978
7bd27aed
RS
979If ever you define a stroke which you don't like, then you can unset
980it with the command
aea01cd7 981
7bd27aed 982> M-x strokes-unset-last-stroke
aea01cd7 983
0d0db51e
DL
984You can always get an idea of what your current strokes look like with
985the command
986
987> M-x strokes-list-strokes
988
989Your strokes will be displayed in alphabetical order (based on command
990names) and the beginning of each simple stroke will be marked by a
991color dot. Since you may have several simple strokes in a complex
992stroke, the dot colors are arranged in the rainbow color sequence,
993`ROYGBIV'. If you want a listing of your strokes from most recent
994down, then use a prefix argument:
995
996> C-u M-x strokes-list-strokes
997
7bd27aed
RS
998Your strokes are stored as you enter them. They get saved in a file
999called ~/.strokes, along with other strokes configuration variables.
1000You can change this location by setting the variable `strokes-file'.
1001You will be prompted to save them when you exit Emacs, or you can save
1002them with
aea01cd7 1003
69c3280d 1004> M-x strokes-prompt-user-save-strokes
aea01cd7 1005
7bd27aed
RS
1006Your strokes get loaded automatically when you enable `strokes-mode'.
1007You can also load in your user-defined strokes with
aea01cd7 1008
ded4da95 1009> M-x strokes-load-user-strokes
aea01cd7 1010
0d0db51e
DL
1011** Strokes for pictographic editing...
1012
1013If you'd like to create graphical files with strokes, you'll have to
ded4da95
DL
1014be running a version of Emacs with XPM support. You use the binding
1015to `strokes-compose-complex-stroke' to start drawing your strokes.
1016These are just complex strokes, and thus continue drawing with mouse-1
721be9cd 1017or mouse-2 and end with mouse-3. Then the stroke image gets inserted
ded4da95
DL
1018into the buffer. You treat it somewhat like any other character,
1019which you can copy, paste, delete, move, etc. When all is done, you
1020may want to send the file, or save it. This is done with
0d0db51e
DL
1021
1022> M-x strokes-encode-buffer
1023
1024Likewise, to decode the strokes from a strokes-encoded buffer you do
1025
1026> M-x strokes-decode-buffer
1027
7bd27aed 1028** A few more important things...
aea01cd7 1029
0d0db51e
DL
1030o The command `strokes-do-complex-stroke' is invoked with M-mouse-2,
1031 so that you can execute complex strokes (i.e. with more than one lift)
1032 if preferred.
aea01cd7 1033
7bd27aed
RS
1034o Strokes are a bit computer-dependent in that they depend somewhat on
1035 the speed of the computer you're working on. This means that you
1036 may have to tweak some variables. You can read about them in the
ded4da95 1037 commentary of `strokes.el'. Better to just use \\[apropos] and read their
7bd27aed
RS
1038 docstrings. All variables/functions start with `strokes'. The one
1039 variable which many people wanted to see was
1040 `strokes-use-strokes-buffer' which allows the user to use strokes
1041 silently--without displaying the strokes. All variables can be set
69c3280d 1042 by customizing the group `strokes' via \\[customize-group]."))
ded4da95 1043 (set-buffer standard-output)
7d317bca 1044 (help-mode)
d5d105e8 1045 (help-print-return-message)))
aea01cd7 1046
8aa88760 1047(define-obsolete-function-alias 'strokes-report-bug 'report-emacs-bug "24.1")
aea01cd7 1048
0d0db51e
DL
1049(defun strokes-window-configuration-changed-p ()
1050 "Non-nil if the `strokes-window-configuration' frame properties changed.
ded4da95 1051This is based on the last time `strokes-window-configuration' was updated."
0d0db51e
DL
1052 (compare-window-configurations (current-window-configuration)
1053 strokes-window-configuration))
1054
aea01cd7 1055(defun strokes-update-window-configuration ()
0d0db51e 1056 "Ensure that `strokes-window-configuration' is up-to-date."
aea01cd7
RS
1057 (interactive)
1058 (let ((current-window (selected-window)))
1059 (cond ((or (window-minibuffer-p current-window)
1060 (window-dedicated-p current-window))
1061 ;; don't try to update strokes window configuration
1062 ;; if window is dedicated or a minibuffer
1063 nil)
32226619 1064 ((or (called-interactively-p 'interactive)
0d0db51e 1065 (not (buffer-live-p (get-buffer strokes-buffer-name)))
aea01cd7
RS
1066 (null strokes-window-configuration))
1067 ;; create `strokes-window-configuration' from scratch...
1068 (save-excursion
1069 (save-window-excursion
d7063de9 1070 (set-buffer (get-buffer-create strokes-buffer-name))
aea01cd7
RS
1071 (set-window-buffer current-window strokes-buffer-name)
1072 (delete-other-windows)
1073 (fundamental-mode)
1074 (auto-save-mode 0)
d7063de9 1075 (font-lock-mode 0)
aea01cd7
RS
1076 (abbrev-mode 0)
1077 (buffer-disable-undo (current-buffer))
1078 (setq truncate-lines nil)
1079 (strokes-fill-current-buffer-with-whitespace)
1080 (setq strokes-window-configuration (current-window-configuration))
1081 (bury-buffer))))
0d0db51e
DL
1082 ((strokes-window-configuration-changed-p) ; simple update
1083 ;; update the strokes-window-configuration for this
1084 ;; specific frame...
aea01cd7
RS
1085 (save-excursion
1086 (save-window-excursion
1087 (set-window-buffer current-window strokes-buffer-name)
1088 (delete-other-windows)
1089 (strokes-fill-current-buffer-with-whitespace)
1090 (setq strokes-window-configuration (current-window-configuration))
1091 (bury-buffer)))))))
1092
1093;;;###autoload
1094(defun strokes-load-user-strokes ()
1095 "Load user-defined strokes from file named by `strokes-file'."
1096 (interactive)
1097 (cond ((and (file-exists-p strokes-file)
1098 (file-readable-p strokes-file))
1099 (load-file strokes-file))
32226619 1100 ((called-interactively-p 'interactive)
aea01cd7
RS
1101 (error "Trouble loading user-defined strokes; nothing done"))
1102 (t
1103 (message "No user-defined strokes, sorry"))))
1104
aea01cd7
RS
1105(defun strokes-prompt-user-save-strokes ()
1106 "Save user-defined strokes to file named by `strokes-file'."
1107 (interactive)
1108 (save-excursion
1109 (let ((current strokes-global-map))
1110 (unwind-protect
1111 (progn
1112 (setq strokes-global-map nil)
1113 (strokes-load-user-strokes)
1114 (if (and (not (equal current strokes-global-map))
32226619 1115 (or (called-interactively-p 'interactive)
a81b56d5 1116 (yes-or-no-p "Save your strokes? ")))
aea01cd7
RS
1117 (progn
1118 (require 'pp) ; pretty-print variables
1119 (message "Saving strokes in %s..." strokes-file)
1120 (get-buffer-create "*saved-strokes*")
1121 (set-buffer "*saved-strokes*")
1122 (erase-buffer)
1123 (emacs-lisp-mode)
1124 (goto-char (point-min))
051f9830 1125 (insert
ded4da95 1126 ";; -*- emacs-lisp -*-\n")
051f9830 1127 (insert (format ";;; saved strokes for %s, as of %s\n\n"
47e1b9a6
RS
1128 (user-full-name)
1129 (format-time-string "%B %e, %Y" nil)))
aea01cd7 1130 (message "Saving strokes in %s..." strokes-file)
ded4da95 1131 (insert (format "(setq strokes-global-map\n'%s)"
47e1b9a6 1132 (pp current)))
aea01cd7
RS
1133 (message "Saving strokes in %s..." strokes-file)
1134 (indent-region (point-min) (point-max) nil)
1135 (write-region (point-min)
1136 (point-max)
1137 strokes-file))
1138 (message "(no changes need to be saved)")))
1139 ;; protected
1140 (if (get-buffer "*saved-strokes*")
1141 (kill-buffer (get-buffer "*saved-strokes*")))
1142 (setq strokes-global-map current)))))
1143
aea01cd7
RS
1144(defun strokes-toggle-strokes-buffer (&optional arg)
1145 "Toggle the use of the strokes buffer.
ded4da95 1146In other words, toggle the variable `strokes-use-strokes-buffer'.
aea01cd7
RS
1147With ARG, use strokes buffer if and only if ARG is positive or true.
1148Returns value of `strokes-use-strokes-buffer'."
1149 (interactive "P")
1150 (setq strokes-use-strokes-buffer
1151 (if arg (> (prefix-numeric-value arg) 0)
1152 (not strokes-use-strokes-buffer))))
1153
0d0db51e 1154(defun strokes-xpm-for-stroke (&optional stroke bufname b/w-only)
ded4da95 1155 "Create an XPM pixmap for the given STROKE in buffer ` *strokes-xpm*'.
0d0db51e
DL
1156If STROKE is not supplied, then `strokes-last-stroke' will be used.
1157Optional BUFNAME to name something else.
1158The pixmap will contain time information via rainbow dot colors
1159where each individual strokes begins.
1160Optional B/W-ONLY non-nil will create a mono pixmap, not intended
1161for trying to figure out the order of strokes, but rather for reading
1162the stroke as a character in some language."
1163 (interactive)
1164 (save-excursion
1165 (let ((buf (get-buffer-create (or bufname " *strokes-xpm*")))
1166 (stroke (strokes-eliminate-consecutive-redundancies
1167 (strokes-fill-stroke
1168 (strokes-renormalize-to-grid (or stroke
1169 strokes-last-stroke)
1170 31))))
1171 (lift-flag t)
1172 (rainbow-chars (list ?R ?O ?Y ?G ?B ?P))) ; ROYGBIV w/o indigo
1173 (set-buffer buf)
1174 (erase-buffer)
1175 (insert strokes-xpm-header)
1176 (loop repeat 33 do
1177 (insert ?\")
61bf4252 1178 (insert-char ?\s 33)
0d0db51e
DL
1179 (insert "\",")
1180 (newline)
1181 finally
1182 (forward-line -1)
1183 (end-of-line)
1184 (insert "}\n"))
1185 (loop for point in stroke
1186 for x = (car-safe point)
1187 for y = (cdr-safe point) do
1188 (cond ((consp point)
1189 ;; draw a point, and possibly a starting-point
1190 (if (and lift-flag (not b/w-only))
1191 ;; mark starting point with the appropriate color
1192 (let ((char (or (car rainbow-chars) ?\.)))
1193 (loop for i from 0 to 2 do
1194 (loop for j from 0 to 2 do
e6ce8c42
GM
1195 (goto-char (point-min))
1196 (forward-line (+ 15 i y))
0d0db51e
DL
1197 (forward-char (+ 1 j x))
1198 (delete-char 1)
1199 (insert char)))
1200 (setq rainbow-chars (cdr rainbow-chars)
1201 lift-flag nil))
1202 ;; Otherwise, just plot the point...
e6ce8c42
GM
1203 (goto-char (point-min))
1204 (forward-line (+ 16 y))
ded4da95 1205 (forward-char (+ 2 x))
61bf4252 1206 (subst-char-in-region (point) (1+ (point)) ?\s ?\*)))
0d0db51e
DL
1207 ((strokes-lift-p point)
1208 ;; a lift--tell the loop to X out the next point...
1209 (setq lift-flag t))))
32226619 1210 (when (called-interactively-p 'interactive)
0d0db51e
DL
1211 (pop-to-buffer " *strokes-xpm*")
1212 ;; (xpm-mode 1)
1213 (goto-char (point-min))
1214 (put-image (create-image (buffer-string) 'xpm t :ascent 100)
1215 (line-end-position))))))
1216
ded4da95 1217;;; Strokes Edit stuff... ### NOT IMPLEMENTED YET ###
0d0db51e
DL
1218
1219;;(defun strokes-edit-quit ()
1220;; (interactive)
1221;; (or (one-window-p t 0)
1222;; (delete-window))
1223;; (kill-buffer "*Strokes List*"))
1224
1225;;(define-derived-mode edit-strokes-mode list-mode
1226;; "Edit-Strokes"
1227;; "Major mode for `edit-strokes' and `list-strokes' buffers.
1228
1229;;Editing commands:
1230
1231;;\\{edit-strokes-mode-map}"
1232;; (setq truncate-lines nil
1233;; auto-show-mode nil ; don't want problems here either
1234;; mode-popup-menu edit-strokes-menu) ; what about extent-specific stuff?
1235;; (and (featurep 'menubar)
1236;; current-menubar
1237;; (set (make-local-variable 'current-menubar)
1238;; (copy-sequence current-menubar))
1239;; (add-submenu nil edit-strokes-menu)))
1240
1241;;(let ((map edit-strokes-mode-map))
1242;; (define-key map "<" 'beginning-of-buffer)
1243;; (define-key map ">" 'end-of-buffer)
1244;; ;; (define-key map "c" 'strokes-copy-other-face)
1245;; ;; (define-key map "C" 'strokes-copy-this-face)
1246;; ;; (define-key map "s" 'strokes-smaller)
1247;; ;; (define-key map "l" 'strokes-larger)
1248;; ;; (define-key map "b" 'strokes-bold)
1249;; ;; (define-key map "i" 'strokes-italic)
1250;; (define-key map "e" 'strokes-list-edit)
1251;; ;; (define-key map "f" 'strokes-font)
1252;; ;; (define-key map "u" 'strokes-underline)
1253;; ;; (define-key map "t" 'strokes-truefont)
1254;; ;; (define-key map "F" 'strokes-foreground)
1255;; ;; (define-key map "B" 'strokes-background)
1256;; ;; (define-key map "D" 'strokes-doc-string)
1257;; (define-key map "a" 'strokes-global-set-stroke)
1258;; (define-key map "d" 'strokes-list-delete-stroke)
1259;; ;; (define-key map "n" 'strokes-list-next)
1260;; ;; (define-key map "p" 'strokes-list-prev)
1261;; ;; (define-key map " " 'strokes-list-next)
1262;; ;; (define-key map "\C-?" 'strokes-list-prev)
1263;; (define-key map "g" 'strokes-list-strokes) ; refresh display
1264;; (define-key map "q" 'strokes-edit-quit)
1265;; (define-key map [(control c) (control c)] 'bury-buffer))
1266
1267;;;;;###autoload
1268;;(defun strokes-edit-strokes (&optional chronological strokes-map)
1269;; ;; ### DEAL WITH THE 2nd ARGUMENT ISSUE! ###
1270;; "Edit strokes in a pop-up buffer containing strokes and their definitions.
1271;;If STROKES-MAP is not given, `strokes-global-map' will be used instead.
1272
1273;;Editing commands:
1274
1275;;\\{edit-faces-mode-map}"
1276;; (interactive "P")
1277;; (pop-to-buffer (get-buffer-create "*Strokes List*"))
1278;; (reset-buffer (current-buffer)) ; handy function from minibuf.el
1279;; (setq strokes-map (or strokes-map
1280;; strokes-global-map
1281;; (progn
1282;; (strokes-load-user-strokes)
1283;; strokes-global-map)))
1284;; (or chronological
1285;; (setq strokes-map (sort (copy-sequence strokes-map)
1286;; 'strokes-alphabetic-lessp)))
1287;; ;; (push-window-configuration)
1288;; (insert
1289;; "Command Stroke\n"
1290;; "------- ------")
1291;; (loop for def in strokes-map
1292;; for i from 0 to (1- (length strokes-map)) do
1293;; (let ((stroke (car def))
1294;; (command-name (symbol-name (cdr def))))
1295;; (strokes-xpm-for-stroke stroke " *strokes-xpm*")
1296;; (newline 2)
61bf4252 1297;; (insert-char ?\s 45)
0d0db51e
DL
1298;; (beginning-of-line)
1299;; (insert command-name)
1300;; (beginning-of-line)
1301;; (forward-char 45)
1302;; (set (intern (format "strokes-list-annotation-%d" i))
1303;; (make-annotation (make-glyph
1304;; (list
1305;; (vector 'xpm
1306;; :data (buffer-substring
1307;; (point-min " *strokes-xpm*")
1308;; (point-max " *strokes-xpm*")
1309;; " *strokes-xpm*"))
1310;; [string :data "[Stroke]"]))
1311;; (point) 'text))
1312;; (set-annotation-data (symbol-value (intern (format "strokes-list-annotation-%d" i)))
1313;; def))
1314;; finally do (kill-region (1+ (point)) (point-max)))
1315;; (edit-strokes-mode)
1316;; (goto-char (point-min)))
1317
1318;;;;;###autoload
1319;;(defalias 'edit-strokes 'strokes-edit-strokes)
1320
937382fa 1321(defvar view-mode-map)
0d0db51e
DL
1322
1323;;;###autoload
1324(defun strokes-list-strokes (&optional chronological strokes-map)
1325 "Pop up a buffer containing an alphabetical listing of strokes in STROKES-MAP.
1326With CHRONOLOGICAL prefix arg \(\\[universal-argument]\) list strokes
1327chronologically by command name.
1328If STROKES-MAP is not given, `strokes-global-map' will be used instead."
1329 (interactive "P")
1330 (setq strokes-map (or strokes-map
1331 strokes-global-map
1332 (progn
1333 (strokes-load-user-strokes)
1334 strokes-global-map)))
1335 (if (not chronological)
1336 ;; then alphabetize the strokes based on command names...
1337 (setq strokes-map (sort (copy-sequence strokes-map)
1338 (function strokes-alphabetic-lessp))))
1339 (let ((config (current-window-configuration)))
1340 (set-buffer (get-buffer-create "*Strokes List*"))
1341 (setq buffer-read-only nil)
1342 (erase-buffer)
1343 (insert
1344 "Command Stroke\n"
1345 "------- ------")
1346 (loop for def in strokes-map do
1347 (let ((stroke (car def))
99e0e3e2
EZ
1348 (command-name (if (symbolp (cdr def))
1349 (symbol-name (cdr def))
1350 (prin1-to-string (cdr def)))))
0d0db51e
DL
1351 (strokes-xpm-for-stroke stroke " *strokes-xpm*")
1352 (newline 2)
61bf4252 1353 (insert-char ?\s 45)
0d0db51e
DL
1354 (beginning-of-line)
1355 (insert command-name)
1356 (beginning-of-line)
1357 (forward-char 45)
99e0e3e2
EZ
1358 (insert-image
1359 (create-image (with-current-buffer " *strokes-xpm*"
1360 (buffer-string))
1361 'xpm t
1362 :color-symbols
1363 `(("foreground"
1364 . ,(frame-parameter nil 'foreground-color))))))
4c3fca9c
RS
1365 finally do (unless (eobp)
1366 (kill-region (1+ (point)) (point-max))))
a81b56d5 1367 (view-buffer "*Strokes List*" nil)
0d0db51e
DL
1368 (set (make-local-variable 'view-mode-map)
1369 (let ((map (copy-keymap view-mode-map)))
1370 (define-key map "q" `(lambda ()
1371 (interactive)
1372 (View-quit)
1373 (set-window-configuration ,config)))
1374 map))
1375 (goto-char (point-min))))
1376
1377(defun strokes-alphabetic-lessp (stroke1 stroke2)
26e96680 1378 "Return t if STROKE1's command name precedes STROKE2's in lexicographic order."
0d0db51e
DL
1379 (let ((command-name-1 (symbol-name (cdr stroke1)))
1380 (command-name-2 (symbol-name (cdr stroke2))))
1381 (string-lessp command-name-1 command-name-2)))
1382
ded4da95
DL
1383(defvar strokes-mode-map
1384 (let ((map (make-sparse-keymap)))
1385 (define-key map [(shift down-mouse-2)] 'strokes-do-stroke)
1386 (define-key map [(meta down-mouse-2)] 'strokes-do-complex-stroke)
1387 map))
aea01cd7 1388
ded4da95
DL
1389;;;###autoload
1390(define-minor-mode strokes-mode
06e21633
CY
1391 "Toggle Strokes mode, a global minor mode.
1392With a prefix argument ARG, enable Strokes mode if ARG is
1393positive, and disable it otherwise. If called from Lisp, enable
1394the mode if ARG is omitted or nil.
1395
1396\\<strokes-mode-map>
ded4da95
DL
1397Strokes are pictographic mouse gestures which invoke commands.
1398Strokes are invoked with \\[strokes-do-stroke]. You can define
1399new strokes with \\[strokes-global-set-stroke]. See also
1400\\[strokes-do-complex-stroke] for `complex' strokes.
aea01cd7
RS
1401
1402To use strokes for pictographic editing, such as Chinese/Japanese, use
ded4da95
DL
1403\\[strokes-compose-complex-stroke], which draws strokes and inserts them.
1404Encode/decode your strokes with \\[strokes-encode-buffer],
1405\\[strokes-decode-buffer].
1406
1407\\{strokes-mode-map}"
37269466 1408 nil strokes-lighter strokes-mode-map
ded4da95
DL
1409 :group 'strokes :global t
1410 (cond ((not (display-mouse-p))
1411 (error "Can't use Strokes without a mouse"))
1412 (strokes-mode ; turn on strokes
1413 (and (file-exists-p strokes-file)
1414 (null strokes-global-map)
1415 (strokes-load-user-strokes))
1416 (add-hook 'kill-emacs-query-functions
1417 'strokes-prompt-user-save-strokes)
1418 (add-hook 'select-frame-hook
1419 'strokes-update-window-configuration)
1420 (strokes-update-window-configuration))
1421 (t ; turn off strokes
1422 (if (get-buffer strokes-buffer-name)
1423 (kill-buffer (get-buffer strokes-buffer-name)))
1424 (remove-hook 'select-frame-hook
1425 'strokes-update-window-configuration))))
aea01cd7 1426
aea01cd7 1427
0d0db51e
DL
1428;;;; strokes-xpm stuff (later may be separate)...
1429
ded4da95 1430;; This is the stuff that will eventually be used for composing letters in
0d0db51e
DL
1431;; any language, compression, decompression, graphics, editing, etc.
1432
10853fc3 1433(defface strokes-char '((t (:background "lightgray")))
0d0db51e
DL
1434 "Face for strokes characters."
1435 :version "21.1"
1436 :group 'strokes)
1437
1438(put 'strokes 'char-table-extra-slots 0)
1439(defconst strokes-char-table (make-char-table 'strokes) ;
1440 "The table which stores values for the character keys.")
1441(aset strokes-char-table ?0 0)
1442(aset strokes-char-table ?1 1)
1443(aset strokes-char-table ?2 2)
1444(aset strokes-char-table ?3 3)
1445(aset strokes-char-table ?4 4)
1446(aset strokes-char-table ?5 5)
1447(aset strokes-char-table ?6 6)
1448(aset strokes-char-table ?7 7)
1449(aset strokes-char-table ?8 8)
1450(aset strokes-char-table ?9 9)
1451(aset strokes-char-table ?a 10)
1452(aset strokes-char-table ?b 11)
1453(aset strokes-char-table ?c 12)
1454(aset strokes-char-table ?d 13)
1455(aset strokes-char-table ?e 14)
1456(aset strokes-char-table ?f 15)
1457(aset strokes-char-table ?g 16)
1458(aset strokes-char-table ?h 17)
1459(aset strokes-char-table ?i 18)
1460(aset strokes-char-table ?j 19)
1461(aset strokes-char-table ?k 20)
1462(aset strokes-char-table ?l 21)
1463(aset strokes-char-table ?m 22)
1464(aset strokes-char-table ?n 23)
1465(aset strokes-char-table ?o 24)
1466(aset strokes-char-table ?p 25)
1467(aset strokes-char-table ?q 26)
1468(aset strokes-char-table ?r 27)
1469(aset strokes-char-table ?s 28)
1470(aset strokes-char-table ?t 29)
1471(aset strokes-char-table ?u 30)
1472(aset strokes-char-table ?v 31)
1473(aset strokes-char-table ?w 32)
1474(aset strokes-char-table ?x 33)
1475(aset strokes-char-table ?y 34)
1476(aset strokes-char-table ?z 35)
1477(aset strokes-char-table ?A 36)
1478(aset strokes-char-table ?B 37)
1479(aset strokes-char-table ?C 38)
1480(aset strokes-char-table ?D 39)
1481(aset strokes-char-table ?E 40)
1482(aset strokes-char-table ?F 41)
1483(aset strokes-char-table ?G 42)
1484(aset strokes-char-table ?H 43)
1485(aset strokes-char-table ?I 44)
1486(aset strokes-char-table ?J 45)
1487(aset strokes-char-table ?K 46)
1488(aset strokes-char-table ?L 47)
1489(aset strokes-char-table ?M 48)
1490(aset strokes-char-table ?N 49)
1491(aset strokes-char-table ?O 50)
1492(aset strokes-char-table ?P 51)
1493(aset strokes-char-table ?Q 52)
1494(aset strokes-char-table ?R 53)
1495(aset strokes-char-table ?S 54)
1496(aset strokes-char-table ?T 55)
1497(aset strokes-char-table ?U 56)
1498(aset strokes-char-table ?V 57)
1499(aset strokes-char-table ?W 58)
1500(aset strokes-char-table ?X 59)
1501(aset strokes-char-table ?Y 60)
1502(aset strokes-char-table ?Z 61)
1503
1504(defconst strokes-base64-chars
1505 ;; I wanted to make this a vector of individual like (vector ?0
ded4da95 1506 ;; ?1 ?2 ...), but `concat' refuses to accept single
0d0db51e
DL
1507 ;; characters.
1508 (vector "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
1509 "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o"
1510 "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D"
1511 "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
1512 "T" "U" "V" "W" "X" "Y" "Z")
1513;; (vector [?0] [?1] [?2] [?3] [?4] [?5] [?6] [?7] [?8] [?9]
1514;; [?a] [?b] [?c] [?d] [?e] [?f] [?g] [?h] [?i] [?j]
1515;; [?k] [?l] [?m] [?n] [?o] [?p] [?q] [?r] [?s] [?t]
1516;; [?u] [?v] [?w] [?x] [?y] [?z]
1517;; [?A] [?B] [?C] [?D] [?E] [?F] [?G] [?H] [?I] [?J]
1518;; [?K] [?L] [?M] [?N] [?O] [?P] [?Q] [?R] [?S] [?T]
1519;; [?U] [?V] [?W] [?X] [?Y] [?Z])
1520 "Character vector for fast lookup of base-64 encoding of numbers in [0,61].")
1521
1522(defsubst strokes-xpm-char-on-p (char)
ded4da95 1523 "Non-nil if CHAR represents an `on' bit in the XPM."
0d0db51e
DL
1524 (eq char ?*))
1525
1526(defsubst strokes-xpm-char-bit-p (char)
ded4da95 1527 "Non-nil if CHAR represents an `on' or `off' bit in the XPM."
61bf4252 1528 (or (eq char ?\s)
0d0db51e
DL
1529 (eq char ?*)))
1530
1531;;(defsubst strokes-xor (a b) ### Should I make this an inline function? ###
4837b516 1532;; "T if one and only one of A and B is non-nil; otherwise, returns nil.
0d0db51e
DL
1533;;NOTE: Don't use this as a numeric xor since it treats all non-nil
1534;; values as t including `0' (zero)."
1535;; (eq (null a) (not (null b))))
1536
1537(defsubst strokes-xpm-encode-length-as-string (length)
ded4da95 1538 "Given some LENGTH in [0,62) do a fast lookup of its encoding."
0d0db51e 1539 (aref strokes-base64-chars length))
f1180544 1540
0d0db51e
DL
1541(defsubst strokes-xpm-decode-char (character)
1542 "Given a CHARACTER, do a fast lookup to find its corresponding integer value."
1543 (aref strokes-char-table character))
f1180544 1544
0d0db51e 1545(defun strokes-xpm-to-compressed-string (&optional xpm-buffer)
ded4da95
DL
1546 "Convert XPM in XPM-BUFFER to compressed string representing the stroke.
1547XPM-BUFFER defaults to ` *strokes-xpm*'."
7fdbcd83 1548 (with-current-buffer (setq xpm-buffer (or xpm-buffer " *strokes-xpm*"))
0d0db51e
DL
1549 (goto-char (point-min))
1550 (search-forward "/* pixels */") ; skip past header junk
1551 (forward-char 2)
1552 ;; a note for below:
1553 ;; the `current-char' is the char being counted -- NOT the char at (point)
1554 ;; which happens to be called `char-at-point'
1555 (let ((compressed-string "+/") ; initialize the output
1556 (count 0) ; keep a current count of
1557 ; `current-char'
1558 (last-char-was-on-p t) ; last entered stream
1559 ; represented `on' bits
1560 (current-char-is-on-p nil) ; current stream represents `on' bits
1561 (char-at-point (char-after))) ; read the first char
1562 (while (not (eq char-at-point ?})) ; a `}' denotes the
1563 ; end of the pixmap
1564 (cond ((zerop count) ; must restart counting
1565 ;; check to see if the `char-at-point' is an actual pixmap bit
1566 (when (strokes-xpm-char-bit-p char-at-point)
1567 (setq count 1
ded4da95 1568 current-char-is-on-p (strokes-xpm-char-on-p char-at-point)))
0d0db51e
DL
1569 (forward-char 1))
1570 ((= count 61) ; maximum single char's
1571 ; encoding length
ded4da95
DL
1572 (setq compressed-string
1573 (concat compressed-string
1574 ;; add a zero-length encoding when
1575 ;; necessary
1576 (when (eq last-char-was-on-p
1577 current-char-is-on-p)
1578 ;; "0"
1579 (strokes-xpm-encode-length-as-string 0))
1580 (strokes-xpm-encode-length-as-string 61))
0d0db51e
DL
1581 last-char-was-on-p current-char-is-on-p
1582 count 0)) ; note that we just set
1583 ; count=0 and *don't* advance
1584 ; (point)
1585 ((strokes-xpm-char-bit-p char-at-point) ; an actual xpm bit
1586 (if (eq current-char-is-on-p
1587 (strokes-xpm-char-on-p char-at-point))
1588 ;; yet another of the same bit-type, so we continue
1589 ;; counting...
1590 (progn
1591 (incf count)
1592 (forward-char 1))
1593 ;; otherwise, it's the opposite bit-type, so we do a
1594 ;; write and then restart count ### NOTE (for myself
1595 ;; to be aware of) ### I really should advance
1596 ;; (point) in this case instead of letting another
1597 ;; iteration go through and letting the case: count=0
1598 ;; take care of this stuff for me. That's why
1599 ;; there's no (forward-char 1) below.
ded4da95
DL
1600 (setq compressed-string
1601 (concat compressed-string
1602 ;; add a zero-length encoding when
1603 ;; necessary
1604 (when (eq last-char-was-on-p
1605 current-char-is-on-p)
1606 ;; "0"
1607 (strokes-xpm-encode-length-as-string 0))
1608 (strokes-xpm-encode-length-as-string count))
0d0db51e
DL
1609 count 0
1610 last-char-was-on-p current-char-is-on-p)))
1611 (t ; ELSE it's some other useless
1612 ; char, like `"' or `,'
1613 (forward-char 1)))
1614 (setq char-at-point (char-after)))
1615 (concat compressed-string
1616 (when (> count 0)
1617 (concat (when (eq last-char-was-on-p
1618 current-char-is-on-p)
1619 ;; "0"
1620 (strokes-xpm-encode-length-as-string 0))
1621 (strokes-xpm-encode-length-as-string count)))
1622 "/"))))
1623
1624;;;###autoload
1625(defun strokes-decode-buffer (&optional buffer force)
1626 "Decode stroke strings in BUFFER and display their corresponding glyphs.
1627Optional BUFFER defaults to the current buffer.
1628Optional FORCE non-nil will ignore the buffer's read-only status."
1629 (interactive)
1630 ;; (interactive "*bStrokify buffer: ")
7fdbcd83 1631 (with-current-buffer (setq buffer (get-buffer (or buffer (current-buffer))))
0d0db51e
DL
1632 (when (or (not buffer-read-only)
1633 force
1634 inhibit-read-only
1635 (y-or-n-p
1636 (format "Buffer %s is read-only. Strokify anyway? " buffer)))
1637 (let ((inhibit-read-only t))
1638 (message "Strokifying %s..." buffer)
1639 (goto-char (point-min))
06b60517 1640 (let (string image)
ded4da95 1641 ;; The comment below is what I'd have to do if I wanted to
0d0db51e
DL
1642 ;; deal with random newlines in the midst of the compressed
1643 ;; strings. If I do this, I'll also have to change
1644 ;; `strokes-xpm-to-compress-string' to deal with the newline,
1645 ;; and possibly other whitespace stuff. YUCK!
1646 ;; (while (re-search-forward "\\+/\\(\\w\\|\\)+/" nil t nil (get-buffer buffer))
1647 (while (with-current-buffer buffer
1648 (when (re-search-forward "\\+/\\(\\w+\\)/" nil t nil)
1649 (setq string (match-string 1))
1650 (goto-char (match-end 0))
1651 (replace-match " ")
1652 t))
1653 (strokes-xpm-for-compressed-string string " *strokes-xpm*")
1654 (setq image (create-image (with-current-buffer " *strokes-xpm*"
1655 (buffer-string))
1656 'xpm t))
1657 (insert-image image
1658 (propertize " "
1659 'type 'stroke-glyph
1660 'stroke-glyph image
1661 'data string))))
1662 (message "Strokifying %s...done" buffer)))))
1663
1664(defun strokes-encode-buffer (&optional buffer force)
ded4da95 1665 "Convert the glyphs in BUFFER to their base-64 ASCII representations.
0d0db51e
DL
1666Optional BUFFER defaults to the current buffer.
1667Optional FORCE non-nil will ignore the buffer's read-only status."
1668 ;; ### NOTE !!! ### (for me)
1669 ;; For later on, you can/should make the inserted strings atomic
1670 ;; extents, so that the users have a clue that they shouldn't be
1671 ;; editing inside them. Plus, if you make them extents, you can
1672 ;; very easily just hide the glyphs, so if you unstrokify, and the
1673 ;; restrokify, then those that already are glyphed don't need to be
1674 ;; re-calculated, etc. It's just nicer that way. The only things
1675 ;; to worry about is cleanup (i.e. do the glyphs get gc'd when the
1676 ;; buffer is killed?
1677 ;; (interactive "*bUnstrokify buffer: ")
1678 (interactive)
7fdbcd83 1679 (with-current-buffer (setq buffer (or buffer (current-buffer)))
0d0db51e
DL
1680 (when (or (not buffer-read-only)
1681 force
1682 inhibit-read-only
1683 (y-or-n-p
1684 (format "Buffer %s is read-only. Encode anyway? " buffer)))
1685 (message "Encoding strokes in %s..." buffer)
1686 ;; (map-extents
1687 ;; (lambda (ext buf)
1688 ;; (when (eq (extent-property ext 'type) 'stroke-glyph)
1689 ;; (goto-char (extent-start-position ext))
1690 ;; (delete-char 1) ; ### What the hell do I do here? ###
1691 ;; (insert "+/" (extent-property ext 'data) "/")
1692 ;; (delete-extent ext))))))
1693 (let ((inhibit-read-only t)
1694 (start nil)
1695 glyph)
1696 (while (or (and (bobp)
1697 (get-text-property (point) 'type))
1698 (setq start (next-single-property-change (point) 'type)))
1699 (when (eq 'stroke-glyph (get-text-property (point) 'type))
1700 (goto-char start)
1701 (setq start (point-marker)
1702 glyph (get-text-property start 'display))
1703 (insert "+/" (get-text-property (point) 'data) ?/)
1704 (delete-char 1)
1705 (add-text-properties start (point)
1706 (list 'type 'stroke-string
10853fc3 1707 'face 'strokes-char
0d0db51e
DL
1708 'stroke-glyph glyph
1709 'display nil))))
1710 (message "Encoding strokes in %s...done" buffer)))))
1711
1712(defun strokes-xpm-for-compressed-string (compressed-string &optional bufname)
ded4da95
DL
1713 "Convert the stroke represented by COMPRESSED-STRING into an XPM.
1714Store XPM in buffer BUFNAME if supplied \(default is ` *strokes-xpm*'\)"
7fdbcd83
SM
1715 (or bufname (setq bufname " *strokes-xpm*"))
1716 (with-current-buffer (get-buffer-create bufname)
0d0db51e
DL
1717 (erase-buffer)
1718 (insert compressed-string)
1719 (goto-char (point-min))
1720 (let ((current-char-is-on-p nil))
1721 (while (not (eobp))
1722 (insert-char
1723 (if current-char-is-on-p
1724 ?*
61bf4252 1725 ?\s)
0d0db51e
DL
1726 (strokes-xpm-decode-char (char-after)))
1727 (delete-char 1)
1728 (setq current-char-is-on-p (not current-char-is-on-p)))
1729 (goto-char (point-min))
1730 (loop repeat 33 do
1731 (insert ?\")
1732 (forward-char 33)
1733 (insert "\",\n"))
1734 (goto-char (point-min))
1735 (insert strokes-xpm-header))))
1736
1737;;;###autoload
1738(defun strokes-compose-complex-stroke ()
1739 ;; ### NOTE !!! ###
ded4da95 1740 ;; Even though we don't have lexical scoping, it's somewhat ugly how I
0d0db51e
DL
1741 ;; pass around variables in the global name space. I can/should
1742 ;; change this.
1743 "Read a complex stroke and insert its glyph into the current buffer."
1744 (interactive "*")
1745 (let ((strokes-grid-resolution 33))
1746 (strokes-read-complex-stroke)
1747 (strokes-xpm-for-stroke nil " *strokes-xpm*" t)
1748 (insert (strokes-xpm-to-compressed-string " *strokes-xpm*"))
1749 (strokes-decode-buffer)
1750 ;; strokes-decode-buffer does a save-excursion.
1751 (forward-char)))
1752
26e96680
JB
1753(defun strokes-unload-function ()
1754 "Unload the Strokes library."
ded4da95 1755 (strokes-mode -1)
26e96680
JB
1756 ;; continue standard unloading
1757 nil)
4c3fca9c 1758
aea01cd7 1759(run-hooks 'strokes-load-hook)
ded4da95 1760(provide 'strokes)
aea01cd7
RS
1761
1762;;; strokes.el ends here