177
|
1 ;;; strokes.el -- Control XEmacs through mouse strokes --
|
185
|
2 ;; Mon Jul 25 12:40:41 EDT 1997
|
153
|
3
|
|
4 ;; Copyright (C) 1997 Free Software Foundation, Inc.
|
|
5
|
|
6 ;; Author: David Bakhash <cadet@mit.edu>
|
|
7 ;; Maintainer: David Bakhash <cadet@mit.edu>
|
185
|
8 ;; Version: 2.4-beta
|
153
|
9 ;; Created: 12 April 1997
|
|
10 ;; Keywords: lisp, mouse, extensions
|
|
11
|
|
12 ;; This file is part of XEmacs.
|
|
13
|
|
14 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
15 ;; under the terms of the GNU General Public License as published by
|
|
16 ;; the Free Software Foundation; either version 2 of the License, or
|
|
17 ;; (at your option) any later version.
|
|
18
|
|
19 ;; XEmacs program is distributed in the hope that it will be useful,
|
|
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
22 ;; General Public License for more details.
|
|
23
|
|
24 ;; You should have received a copy of the GNU General Public License
|
|
25 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
26 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
27 ;; 02111-1307, USA.
|
|
28
|
|
29 ;;; Synched up with: Not in FSF.
|
|
30
|
|
31 ;;; Commentary:
|
|
32
|
185
|
33 ;; This package is written for for XEmacs v19.15 and up. This is the
|
|
34 ;; strokes package. It is intended to allow the user to control
|
|
35 ;; XEmacs by means of mouse strokes. Once strokes is loaded, you can
|
|
36 ;; always get help be invoking `strokes-help':
|
153
|
37
|
|
38 ;; > M-x strokes-help
|
|
39
|
|
40 ;; and you can learn how to use the package. A mouse stroke, for now,
|
|
41 ;; can be defined as holding the middle button, for instance, and then
|
|
42 ;; moving the mouse in whatever pattern you wish, which you have set
|
185
|
43 ;; XEmacs to understand as mapping to a given command. For example,
|
|
44 ;; you may wish the have a mouse stroke that looks like a capital `C'
|
|
45 ;; which means `copy-region-as-kill'. Treat strokes just like you do
|
|
46 ;; key bindings. For example, XEmacs sets key bindings globally with
|
|
47 ;; the `global-set-key' command. Likewise, you can do
|
153
|
48
|
|
49 ;; > M-x global-set-stroke
|
|
50
|
|
51 ;; to interactively program in a stroke. It would be wise to set the
|
|
52 ;; first one to this very command, so that from then on, you invoke
|
|
53 ;; `global-set-stroke' with a stroke. likewise, there may eventually
|
|
54 ;; be a `local-set-stroke' command, also analogous to `local-set-key'.
|
|
55
|
|
56 ;; You can always unset the last stroke definition with the command
|
|
57
|
|
58 ;; > M-x strokes-unset-last-stroke
|
|
59
|
|
60 ;; and the last stroke that was added to `strokes-global-map' will be
|
|
61 ;; removed.
|
|
62
|
|
63 ;; Other analogies between strokes and key bindings are as follows:
|
|
64
|
|
65 ;; 1) To describe a stroke binding, you can type
|
|
66
|
|
67 ;; > M-x describe-stroke
|
|
68
|
185
|
69 ;; analogous to `describe-key'. It's also wise to have a
|
|
70 ;; stroke, like an `h', for help, or a `?', mapped to
|
|
71 ;; `describe-stroke'.
|
153
|
72
|
185
|
73 ;; 2) stroke bindings are set internally through the Lisp function
|
153
|
74 ;; `define-stroke', similar to the `define-key' function. some
|
|
75 ;; examples for a 3x3 stroke grid would be
|
|
76
|
|
77 ;; (define-stroke c-mode-stroke-map
|
|
78 ;; '((0 . 0) (1 . 1) (2 . 2))
|
|
79 ;; 'kill-region)
|
|
80 ;; (define-stroke strokes-global-map
|
|
81 ;; '((0 . 0) (0 . 1) (0 . 2) (1 . 2) (2 . 2))
|
|
82 ;; 'list-buffers)
|
|
83
|
185
|
84 ;; however, if you would probably just have the user enter in
|
|
85 ;; the stroke interactively and then set the stroke to whatever
|
|
86 ;; he/she entered. The Lisp function to interactively read a
|
|
87 ;; stroke is `strokes-read-stroke'. This is especially helpful
|
|
88 ;; when you're on a fast computer that can handle a 9x9 stroke
|
|
89 ;; grid.
|
153
|
90
|
|
91 ;; NOTE: only global stroke bindings are currently implemented,
|
|
92 ;; however mode- and buffer-local stroke bindings may eventually
|
|
93 ;; be implemented in a future version.
|
|
94
|
|
95 ;; The important variables to be aware of for this package are listed
|
|
96 ;; below. They can all be altered through the customizing package via
|
|
97
|
|
98 ;; > M-x customize
|
|
99
|
|
100 ;; and customizing the group named `strokes'. You can also read
|
|
101 ;; documentation on the variables there.
|
|
102
|
185
|
103 ;; `strokes-minimum-match-score' (determines the threshold of error
|
|
104 ;; that makes a stroke acceptable or unacceptable. If your strokes
|
|
105 ;; aren't matching, then you should raise this variable.
|
153
|
106
|
185
|
107 ;; `strokes-grid-resolution' (determines the grid dimensions that you
|
|
108 ;; use when defining/reading strokes. The finer the grid your
|
|
109 ;; computer can handle, the more you can do, but even a 3x3 grid is
|
|
110 ;; pretty cool.) The default value (7) should be fine for most decent
|
|
111 ;; computers. NOTE: This variable should not be set to a number less
|
|
112 ;; than 3.
|
153
|
113
|
|
114 ;; `strokes-display-strokes-buffer' will allow you to hide the strokes
|
|
115 ;; buffer when doing simple strokes. This is a speedup for slow
|
|
116 ;; computers as well as people who don't want to see their strokes.
|
|
117
|
|
118 ;; If you find that your mouse is accelerating too fast, you can
|
|
119 ;; execute the UNIX X command to slow it down. A good possibility is
|
|
120
|
|
121 ;; % xset m 5/4 8
|
|
122
|
|
123 ;; which seems, heuristically, to work okay, without much disruption.
|
|
124
|
|
125 ;; Whenever you load in the strokes package, you will be able to save
|
|
126 ;; what you've done upon exiting XEmacs. You can also do
|
|
127
|
|
128 ;; > M-x save-strokes
|
|
129
|
185
|
130 ;; and it will save your strokes in ~/.strokes, or you may wish to
|
|
131 ;; change this by setting the variable `strokes-file'.
|
153
|
132
|
|
133 ;; Note that internally, all of the routines that are part of this
|
185
|
134 ;; package are able to deal with complex strokes, as they are a
|
|
135 ;; superset of simple strokes. However, the default of this package
|
|
136 ;; will map mouse button2 to the command `strokes-do-stroke', and NOT
|
|
137 ;; `strokes-do-complex-stroke'. If you wish to use complex strokes,
|
|
138 ;; you will have to override this key mapping. Complex strokes are
|
|
139 ;; terminated with mouse button3. The strokes package will not
|
|
140 ;; interfere with `mouse-yank', but you may want to examine how this
|
|
141 ;; is done (see the variable `strokes-click-command')
|
153
|
142
|
|
143 ;; To get strokes to work as part of your your setup, then you'll have
|
185
|
144 ;; put the strokes package in your load-path (preferably
|
|
145 ;; byte-compiled) and then add the following to your .emacs file (or
|
|
146 ;; wherever you put XEmacs-specific startup preferences):
|
153
|
147
|
185
|
148 ;; (if window-system (require 'strokes))
|
153
|
149
|
|
150 ;; Once loaded, you can start stroking. You can also toggle between
|
|
151 ;; strokes mode by simple typing
|
|
152
|
|
153 ;; > M-x strokes-mode
|
|
154
|
185
|
155 ;; I am now in the process of porting this package to Emacs. I also
|
|
156 ;; hope that, with the help of others, this package will be useful in
|
|
157 ;; entering in pictographic-like language text using the mouse
|
|
158 ;; (i.e. Korean). Japanese and Chinese are a bit trickier, but I'm
|
|
159 ;; sure that with help it can be done. The next version will allow
|
|
160 ;; the user to enter strokes which "remove the pencil from the paper"
|
|
161 ;; so to speak, so one character can have multiple strokes.
|
153
|
162
|
177
|
163 ;; You can read more about strokes at:
|
|
164
|
185
|
165 ;; http://www.mit.edu/people/cadet/strokes-help.html
|
177
|
166
|
185
|
167 ;; If you're interested in using strokes for writing English into
|
|
168 ;; XEmacs using strokes, then you'll want to read about it on the web
|
|
169 ;; page above or just download from
|
|
170 ;; http://www.mit.edu/people/cadet/strokes-abc.el, which is nothing
|
|
171 ;; but a file with some helper commands for inserting alphanumerics
|
|
172 ;; and punctuation.
|
177
|
173
|
185
|
174 ;; Great thanks to Rob Ristroph for his generosity in letting me use
|
|
175 ;; his PC to develop this, Jason Johnson for his help in algorithms,
|
|
176 ;; Euna Kim for her help in Korean, and massive thanks to the helpful
|
|
177 ;; guys on the help instance on athena (zeno, jered, amu, gsstark,
|
|
178 ;; ghudson, etc) Special thanks to Steve Baur, Kyle Jones, and Hrvoje
|
|
179 ;; Niksic for all their help. And special thanks to Dave Gillespie
|
|
180 ;; for all the elisp help--he is responsible for helping me use the cl
|
|
181 ;; macros at (near) max speed.
|
153
|
182
|
|
183 ;; Tasks: (what I'm getting ready for future version)...
|
|
184 ;; 2) use 'strokes-read-complex-stroke for korean, etc.
|
|
185 ;; 4) buffer-local 'strokes-local-map, and mode-stroke-maps would be nice
|
|
186 ;; 6) add some hooks, like `strokes-read-stroke-hook'
|
|
187 ;; 7) See what people think of the factory settings. Should I change
|
|
188 ;; them? They're all pretty arbitrary in a way. I guess they
|
|
189 ;; should be minimal, but computers are getting lots faster, and
|
|
190 ;; if I choose the defaults too conservatively, then strokes will
|
185
|
191 ;; surely disappoint some people on decent machines (until they
|
153
|
192 ;; figure out M-x customize). I need feedback.
|
|
193 ;; Other: I always have the most beta version of strokes, so if you
|
|
194 ;; want it just let me know.
|
|
195
|
|
196 ;;; Change Log:
|
|
197
|
185
|
198 ;; 1.3: provided user variable `strokes-use-strokes-buffer' to let
|
|
199 ;; users hide the strokes and strokes buffer when entering simple
|
|
200 ;; strokes.
|
153
|
201 ;; 1.3: cleaned up most leaks.
|
|
202 ;; 1.3: with Jari Aalto's help, cleaned up overall program.
|
|
203 ;; 1.3: added `strokes-help' for help on strokes
|
|
204 ;; 1.3: fixed 'strokes-load-hook bug
|
|
205 ;; 1.3: email address change: now <cadet@mit.edu>
|
185
|
206 ;; 1.3: added `strokes-report-bug' based on efs/dired's
|
|
207 ;; `dired-report-bug'
|
153
|
208 ;; 1.3: added more dialog-box queries for mouse-event stuff.
|
|
209 ;; 1.4: allowed strokes to invoke kbd macros as well (thanks gsstark!)
|
|
210 ;; 2.0: fixed up ordering of certain functions.
|
185
|
211 ;; 2.0: fixed bug applying to strokes in dedicated and minibuffer
|
|
212 ;; windows.
|
153
|
213 ;; 2.0: punted the C-h way of invoking strokes help routines.
|
|
214 ;; 2.0: fixed `strokes-define-stroke' so it would error check against
|
185
|
215 ;; defining strokes that were too short (really clicks) 2.0:
|
|
216 ;; added `strokes-toggle-strokes-buffer' interactive function
|
|
217 ;; 2.0: added `customize' support, thanks to patch from Hrvoje
|
|
218 ;; (thanks)
|
|
219 ;; 2.1: strokes no longer forces `mouse-yank-at-point' to t on
|
|
220 ;; mouse-yank (i.e. `mouse-yank-at-point' is up to you again)
|
153
|
221 ;; 2.1: toggling strokes-mode off and then back on no longer deletes
|
|
222 ;; the strokes that you programmed in but didn't save before
|
|
223 ;; toggling off strokes-mode.
|
185
|
224 ;; 2.1: advised may functions for modes like VM and w3 so that they
|
|
225 ;; too can use strokes, while still maintaining old button2
|
|
226 ;; functionality.
|
|
227 ;; 2.1: with Steve's help, got the autoload for `strokes-mode' and
|
153
|
228 ;; fixed up the package so loading it does not enable strokes
|
|
229 ;; until user calls `strokes-mode'.
|
|
230 ;; 2.2: made sure that abbrev-mode was off in the ` *strokes*' buffer
|
|
231 ;; 2.2: added more dired advice for mouse permissions commands
|
185
|
232 ;; 2.2: added some checks to see if saving strokes is really necessary
|
|
233 ;; so the user doesn't get prompted aimlessly.
|
|
234 ;; 2.2: change the `strokes-lift' symbol to a keyword of value
|
|
235 ;; `:strokes-lift' for legibility. IF YOUR OLD STROKES DON'T
|
|
236 ;; WORK, THIS IS PROBABLY WHY.
|
|
237 ;; 2.2: I might have to change this back to `'strokes-lift' because
|
|
238 ;; the keyword fails in emacs, though I don't know why.
|
153
|
239 ;; 2.2: `strokes-describe-stroke' is nicer during recursive edits
|
|
240 ;; 2.2: provided `strokes-fill-stroke' to fill in empty spaces of strokes
|
|
241 ;; as an important step towards platform (speed) independence.
|
185
|
242 ;; Because of this, I moved the global setting of
|
|
243 ;; `strokes-last-stroke' from
|
|
244 ;; `strokes-eliminate-consecutive-redundancies' to
|
|
245 ;; `strokes-fill-stroke' since the latter comes later in
|
|
246 ;; processing a user stroke.
|
153
|
247 ;; 2.2: Finally changed the defaults, so now `strokes-grid-resolution' is 9
|
185
|
248 ;; and `strokes-minimum-match-score' is 1000 by default. This
|
|
249 ;; will surely mess some people up, but if so, just set it back
|
|
250 ;; w/ M-x customize.
|
|
251 ;; 2.2: Fixed up the mechanism for updating the
|
|
252 ;; `strokes-window-configuration'. Now it only uses one function
|
|
253 ;; (`strokes-update-window-configuration') which does it all, and
|
|
254 ;; much more efficiently (thanks RMS!).
|
|
255 ;; 2.2 Fixed up the appearance of the *strokes* buffer so that there
|
|
256 ;; are no ugly line truncations, and I got rid of the bug which
|
|
257 ;; would draw the stroke on the wrong line. I still wish that
|
|
258 ;; `event-closest-point' was smarter. In fact,
|
|
259 ;; `event-closest-point' does *not* do what its name suggests.
|
|
260 ;; 2.3 Added more to `strokes-update-window-configuration' so it goes
|
|
261 ;; to hell less often
|
|
262 ;; 2.3 `strokes-mode' no longer will undefined keys unless it's sure
|
|
263 ;; that the user had had them mapped to a strokes command.
|
|
264 ;; 2.3 Added more magic autoload statements so strokes work more
|
|
265 ;; smoothly. similarly, I made strokes-mode turn itself on when
|
|
266 ;; the user defines a stroke (thanks Hrvoje).
|
|
267 ;; 2.3 Added "Strokes" to the modeline when strokes is on, and allow
|
|
268 ;; toggling strokes with mouse button2.
|
|
269 ;; 2.3 Added `list-strokes', which is a really nice function which
|
|
270 ;; graphically lists all the strokes that the user has defined
|
|
271 ;; and their corresponding commands. `list-strokes' will
|
|
272 ;; appropriately colorize the pixmaps to display some time info.
|
|
273 ;; 2.4 Added all new functionality to strokes by allowing the user to
|
|
274 ;; enter strokes in graphically into XEmacs, allowing true graphic
|
|
275 ;; editing, Chinese/Japanese, etc. User simply uses C-button2 to
|
|
276 ;; draw strokes (function: `strokes-compose-complex-stroke'). Then
|
|
277 ;; after the glyph gets inserted into the current buffer at (point),
|
|
278 ;; the use can treat that glyph as any other character, and
|
|
279 ;; copy/paste/delete/undo, etc. Also, when the user would like to
|
|
280 ;; save/send the glyphs (to other XEmacs users, of course), he/she
|
|
281 ;; can use the helper functions:
|
|
282 ;;
|
|
283 ;; i. M-x strokes-encode-buffer -- Ascii-encodes and compresses
|
|
284 ;; strokes to base-64.
|
|
285 ;; ii. M-x strokes-decode-buffer -- Decodes ascii-encoded strokes
|
|
286 ;; back into glyphs.
|
|
287 ;; 2.4 With help from Kyle fixed the itimer (timeout event) bug, where I
|
|
288 ;; forgot to check for timeouts.
|
|
289 ;; 2.4 Around this time, made a successful port of strokes.el for emacs.
|
|
290 ;; 2.4 Made added `strokes-xpm-header' as a variable.
|
|
291 ;; 2.4 Changed the default value of `strokes-character' from `o' to
|
|
292 ;; `@' since it looks nicer when drawn.
|
|
293 ;; 2.4 Changed `strokes-click-p' so that it considers only a stroke
|
|
294 ;; of length <= 1 a click, as opposed to a length 2 being a
|
|
295 ;; click.
|
|
296 ;; 2.4 Totally made the the function `strokes-read-stroke' (and a bit
|
|
297 ;; on `strokes-read-complex-stroke') more efficient and robust,
|
|
298 ;; making the former use the optional event passed to it, and
|
|
299 ;; thus not losing the first mouse event position when reading a
|
|
300 ;; stroke on the fly.
|
|
301 ;; 2.4 Finally fixed the mouse-yank / mouse-yank-at-point bug (after
|
|
302 ;; months of struggling with it). I simply inserted a (sit-for 0)
|
|
303 ;; before the (command-execute strokes-click-command) and that
|
|
304 ;; patched it up. I'd thought that it was a kludge, but I later
|
|
305 ;; found out that it wasn't, as redisplay has several states, and
|
|
306 ;; command-execute often must decide which of two states must be
|
|
307 ;; considered when executing a command. The (sit-for 0) merely
|
|
308 ;; allowed redisplay to be sure to wait for the ` *strokes*'
|
|
309 ;; buffer to vanish before executing the command (thanks for the
|
|
310 ;; explanation of why my frobbing worked Kyle). Fixing this bug
|
|
311 ;; also (magically) fixed the bug which prevented strokes from
|
|
312 ;; executing a stroke in a mode which had it's own binding for
|
|
313 ;; button-2, such as w3 when the variable
|
|
314 ;; `strokes-use-strokes-buffer' is non-nil. It used to be that
|
|
315 ;; if you chose to view your strokes, then you couldn't use
|
|
316 ;; strokes properly in modes like VM or w3. Now you can!
|
|
317 ;; 2.4 Replaced `kill-emacs-hook' with `kill-emacs-query-functions'
|
|
318 ;; for prompting the user to save his/her strokes, since
|
|
319 ;; `kill-emacs-hook' was not the right hook to use.
|
|
320 ;; 2.4 Having `strokes-update-window-configuration' bound to
|
|
321 ;; `select-frame-hook' was a heavy function for such a commonly
|
|
322 ;; run hook -- especially since event-Xt.c (?) will add the
|
|
323 ;; eval-event to the event queue. So the effect was that if XEmacs
|
|
324 ;; was doing an interpreter-intensive task while the user (re)selected
|
|
325 ;; the frame n times, then the intensive window config updating
|
|
326 ;; took place n times. So to deal, I put in some extra checks to
|
|
327 ;; see if the frame parameters really changed, making an update
|
|
328 ;; worthwhile. See `strokes-update-window-configuration-plist'.
|
153
|
329
|
|
330 ;;; Code:
|
|
331
|
|
332 ;;; Requirements and provisions...
|
|
333
|
|
334 (autoload 'reporter-submit-bug-report "reporter")
|
|
335 (autoload 'mail-position-on-field "sendmail")
|
177
|
336 (eval-when-compile
|
185
|
337 (mapc 'require '(xpm-mode pp annotations reporter advice view-less)))
|
153
|
338
|
|
339 ;;; Constants...
|
|
340
|
185
|
341 (defconst strokes-version "2.4-beta")
|
153
|
342
|
|
343 (defconst strokes-bug-address "cadet@mit.edu")
|
|
344
|
|
345 (defconst strokes-lift :strokes-lift
|
|
346 "Symbol representing a stroke lift event for complex strokes.
|
|
347 Complex strokes are those which contain two or more simple strokes.
|
|
348 This will be useful for when XEmacs understands Chinese.")
|
|
349
|
177
|
350 (defconst strokes-xpm-header "/* XPM */
|
|
351 static char * stroke_xpm[] = {
|
|
352 /* width height ncolors cpp [x_hot y_hot] */
|
|
353 \"33 33 9 1 26 23\",
|
|
354 /* colors */
|
185
|
355 \" c none s none\",
|
|
356 \"* c #000000 s foreground\",
|
177
|
357 \"R c #FFFF00000000\",
|
|
358 \"O c #FFFF80000000\",
|
|
359 \"Y c #FFFFFFFF0000\",
|
|
360 \"G c #0000FFFF0000\",
|
|
361 \"B c #00000000FFFF\",
|
|
362 \"P c #FFFF0000FFFF\",
|
|
363 \". c #45458B8B0000\",
|
|
364 /* pixels */\n"
|
|
365 "The header to all xpm buffers created by strokes")
|
|
366
|
153
|
367 ;;; user variables...
|
|
368
|
|
369 (defgroup strokes nil
|
185
|
370 "Control Emacs through mouse strokes."
|
153
|
371 :group 'mouse)
|
|
372
|
|
373 (defcustom strokes-modeline-string " Strokes"
|
|
374 "*Modeline identification when strokes are on \(default is \" Strokes\"\)."
|
|
375 :type 'string
|
|
376 :group 'strokes)
|
|
377
|
185
|
378 (defcustom strokes-character ?@
|
153
|
379 "*Character used when drawing strokes in the strokes buffer.
|
185
|
380 \(The default is lower-case `@', which works okay\)."
|
153
|
381 :type 'character
|
|
382 :group 'strokes)
|
|
383
|
|
384 (defcustom strokes-minimum-match-score 1000
|
|
385 "*Minimum score for a stroke to be considered a possible match.
|
|
386 Requiring a perfect match would set this variable to 0.
|
|
387 The default value is 1000, but it's mostly dependent on how precisely
|
|
388 you manage to replicate your user-defined strokes. It also depends on
|
|
389 the value of `strokes-grid-resolution', since a higher grid resolution
|
|
390 will correspond to more sample points, and thus more distance
|
|
391 measurements. Usually, this is not a problem since you first set
|
|
392 `strokes-grid-resolution' based on what your computer seems to be able
|
|
393 to handle (though the defaults are usually more than sufficent), and
|
|
394 then you can set `strokes-minimum-match-score' to something that works
|
|
395 for you. The only purpose of this variable is to insure that if you
|
|
396 do a bogus stroke that really doesn't match any of the predefined
|
|
397 ones, then strokes should NOT pick the one that came closest."
|
|
398 :type 'integer
|
|
399 :group 'strokes)
|
|
400
|
|
401 (defcustom strokes-grid-resolution 9
|
|
402 "*Integer defining dimensions of the stroke grid.
|
|
403 The grid is a square grid, where STROKES-GRID-RESOLUTION defaults to
|
|
404 `9', making a 9x9 grid whose coordinates go from (0 . 0) on the top
|
|
405 left to ((STROKES-GRID-RESOLUTION - 1) . (STROKES-GRID-RESOLUTION - 1))
|
|
406 on the bottom right. The greater the resolution, the more intricate
|
|
407 your strokes can be.
|
185
|
408 NOTE: This variable should be odd and MUST NOT be less than 3 and need
|
|
409 not be greater than 33, which is the resolution of the pixmaps.
|
153
|
410 WARNING: Changing the value of this variable will gravely affect the
|
|
411 strokes you have already programmed in. You should try to
|
|
412 figure out what it should be based on your needs and on how
|
|
413 quick the particular platform(s) you're operating on, and
|
|
414 only then start programming in your custom strokes."
|
|
415 :type 'integer
|
|
416 :group 'strokes)
|
|
417
|
|
418 (defcustom strokes-file "~/.strokes"
|
|
419 "*File containing saved strokes for stroke-mode (default is ~/.strokes)."
|
|
420 :type 'file
|
|
421 :group 'strokes)
|
|
422
|
|
423 (defcustom strokes-buffer-name " *strokes*"
|
|
424 "The buffer that the strokes take place in (default is ` *strokes*')."
|
|
425 :type 'string
|
|
426 :group 'strokes)
|
|
427
|
|
428 (defcustom strokes-use-strokes-buffer t
|
|
429 "*If non-nil, the strokes buffer is used and strokes are displayed.
|
|
430 If nil, strokes will be read the same, however the user will not be
|
|
431 able to see the strokes. This be helpful for people who don't like
|
|
432 the delay in switching to the strokes buffer."
|
|
433 :type 'boolean
|
|
434 :group 'strokes)
|
|
435
|
|
436 (defcustom strokes-click-command 'mouse-yank
|
|
437 "*Command to execute when stroke is actually a `click' event.
|
|
438 This is set to `mouse-yank' by default."
|
|
439 :type 'function
|
|
440 :group 'strokes)
|
|
441
|
|
442 ;;; internal variables...
|
|
443
|
|
444 ;;;###autoload
|
|
445 (defvar strokes-mode nil
|
|
446 "Non-nil when `strokes' is globally enabled")
|
|
447
|
|
448 (defvar strokes-window-configuration nil
|
|
449 "The special window configuration used when entering strokes.
|
|
450 This is set properly in the function `strokes-update-window-configuration'.")
|
|
451
|
185
|
452 (defvar strokes-window-configuration-plist
|
|
453 (list 'frame nil 'frame-height nil 'frame-width nil)
|
|
454 "Plist describing the state of the current strokes-window-configuration.
|
|
455 The plist consists of the following keys:
|
|
456
|
|
457 'frame Frame to draw strokes in.
|
|
458 'frame-height Height of the frame.
|
|
459 'frame-width Width of the frame.")
|
|
460
|
153
|
461 (defvar strokes-last-stroke nil
|
|
462 "Last stroke entered by the user.
|
|
463 Its value gets set every time the function
|
|
464 `strokes-fill-stroke' gets called,
|
|
465 since that is the best time to set the variable")
|
|
466
|
|
467 (defvar strokes-global-map '()
|
|
468 "Association list of strokes and their definitions.
|
|
469 Each entry is (STROKE . COMMAND) where STROKE is itself a list of
|
|
470 coordinates (X . Y) where X and Y are lists of positions on the
|
|
471 normalized stroke grid, with the top left at (0 . 0). COMMAND is the
|
|
472 corresponding interactive function")
|
|
473
|
|
474 (defvar strokes-load-hook nil
|
|
475 "Function or functions to be called when `strokes' is loaded.")
|
|
476
|
185
|
477 ;;; ### NOT IMPLEMENTED YET ###
|
|
478 ;;(defvar edit-strokes-menu
|
|
479 ;; '("Edit-Strokes"
|
|
480 ;; ["Add stroke..." strokes-global-set-stroke t]
|
|
481 ;; ["Delete stroke..." strokes-edit-delete-stroke t]
|
|
482 ;; ["Change stroke" strokes-smaller t]
|
|
483 ;; ["Change definition" strokes-larger t]
|
|
484 ;; ["[Re]List Strokes chronologically" strokes-list-strokes t]
|
|
485 ;; ["[Re]List Strokes alphabetically" strokes-list-strokes t]
|
|
486 ;; ["Quit" strokes-edit-quit t]
|
|
487 ;; ))
|
177
|
488
|
153
|
489 ;;; Macros...
|
|
490
|
185
|
491 (defmacro strokes-while-inhibiting-garbage-collector (&rest forms)
|
|
492 "Execute FORMS without interference from the garbage collector."
|
|
493 `(let ((gc-cons-threshold 134217727))
|
|
494 ,@forms))
|
|
495
|
153
|
496 (defsubst strokes-click-p (stroke)
|
|
497 "Non-nil if STROKE is really click."
|
185
|
498 (< (length stroke) 2))
|
153
|
499
|
|
500 ;;; old, but worked pretty good (just in case)...
|
|
501 ;;(defmacro strokes-define-stroke (stroke-map stroke def)
|
|
502 ;; "Add STROKE to STROKE-MAP alist with given command DEF"
|
|
503 ;; (list 'if (list '< (list 'length stroke) 3)
|
|
504 ;; (list 'error
|
|
505 ;; "That's a click, not a stroke. See `strokes-click-command'")
|
|
506 ;; (list 'setq stroke-map (list 'cons (list 'cons stroke def)
|
|
507 ;; (list 'remassoc stroke stroke-map)))))
|
|
508
|
|
509 (defmacro strokes-define-stroke (stroke-map stroke def)
|
185
|
510 "Add STROKE to STROKE-MAP alist with given command DEF."
|
153
|
511 `(if (strokes-click-p ,stroke)
|
|
512 (error "That's a click, not a stroke; see `strokes-click-command'")
|
|
513 (setq ,stroke-map (cons (cons ,stroke ,def)
|
|
514 (remassoc ,stroke ,stroke-map)))))
|
|
515
|
|
516 (defalias 'define-stroke 'strokes-define-stroke)
|
|
517
|
|
518 (defsubst strokes-square (x)
|
|
519 "Returns the square of the number X"
|
|
520 (* x x))
|
|
521
|
|
522 (defsubst strokes-distance-squared (p1 p2)
|
|
523 "Gets the distance (squared) between to points P1 and P2.
|
185
|
524 P1 and P2 are cons cells in the form (X . Y)."
|
153
|
525 (let ((x1 (car p1))
|
|
526 (y1 (cdr p1))
|
|
527 (x2 (car p2))
|
|
528 (y2 (cdr p2)))
|
|
529 (+ (strokes-square (- x2 x1))
|
|
530 (strokes-square (- y2 y1)))))
|
|
531
|
|
532 ;;; Advice for various functions...
|
|
533
|
|
534 ;; I'd originally wanted to write a macro that would just take in the
|
185
|
535 ;; generic functions which use mouse button2 in various modes. Most
|
|
536 ;; of them are identical in form: they take an event as the single
|
|
537 ;; argument and then do their thing. I tried writing a macro that
|
|
538 ;; looked something like this, but failed. Advice just ain't that
|
|
539 ;; easy. The one that bugged me the most was `Manual-follow-xref',
|
|
540 ;; because that had &rest arguments, and I didn't know how to work
|
|
541 ;; around it in defadvice. However, I was able to fix up most of the
|
|
542 ;; important modes (i.e. the ones I use all the time). One `bug' in
|
|
543 ;; the program that I just can't seem to figure out is why I can only
|
|
544 ;; advise other button2 functions successfully when the variable
|
|
545 ;; `strokes-use-strokes-buffer' is nil. I did all the
|
|
546 ;; save-excursion/save-window-excursion stuff SPECIFICALLY so that
|
|
547 ;; using the strokes buffer or not would absolutely not affect any
|
|
548 ;; other part of the program. If someone can figure out how to make
|
|
549 ;; the following advices work w/ regardless of that variable
|
|
550 ;; `strokes-use-strokes-buffer', then that would be a great victory.
|
|
551 ;; If someone out there would be kind enough to make the commented
|
|
552 ;; code below work, I'd be grateful. By the way, I put the `protect'
|
|
553 ;; keywords there to insure that if a stroke went bad, then
|
|
554 ;; `strokes-click-command' would be set back. If this isn't
|
|
555 ;; necessary, then feel free to let me know.
|
153
|
556
|
|
557 ;; For what follows, I really wanted something that would work like this:
|
|
558
|
|
559 ;;(strokes-fix-button2 'vm-mouse-button-2)
|
|
560
|
|
561 ;; Or even better, I could have simply done something like:
|
|
562
|
|
563 ;;(mapcar 'strokes-fix-button2
|
|
564 ;; '(vm-mouse-button-2
|
|
565 ;; rmail-summary-mouse-goto-msg
|
|
566 ;; <rest of them>))
|
|
567
|
|
568 ;;; With help from Hans (author of advice.el)...
|
|
569 (defmacro strokes-fix-button2-command (command)
|
|
570 "Fix COMMAND so that it can also work with strokes.
|
|
571 COMMAND must take one event argument.
|
|
572 Example of how one might fix up a command that's bound to button2
|
|
573 and which is an interactive funcion of one event argument:
|
|
574
|
|
575 (strokes-fix-button2-command 'vm-mouse-button-2)"
|
|
576 (let ((command (eval command)))
|
|
577 `(progn
|
|
578 (defadvice ,command (around strokes-fix-button2 compile preactivate)
|
|
579 ,(format "Fix %s to work with strokes." command)
|
185
|
580 (let ((strokes-click-command
|
153
|
581 ',(intern (format "ad-Orig-%s" command))))
|
185
|
582 (strokes-do-stroke (ad-get-arg 0)))))))
|
153
|
583
|
|
584 (strokes-fix-button2-command 'vm-mouse-button-2)
|
|
585 (strokes-fix-button2-command 'rmail-summary-mouse-goto-msg)
|
|
586 (strokes-fix-button2-command 'Buffer-menu-mouse-select)
|
|
587 (strokes-fix-button2-command 'w3-widget-button-click)
|
|
588 (strokes-fix-button2-command 'widget-image-button-press)
|
|
589 (strokes-fix-button2-command 'Info-follow-clicked-node)
|
|
590 (strokes-fix-button2-command 'compile-mouse-goto-error)
|
|
591 (strokes-fix-button2-command 'gdbsrc-select-or-yank)
|
|
592 (strokes-fix-button2-command 'hypropos-mouse-get-doc)
|
|
593 (strokes-fix-button2-command 'gnus-mouse-pick-group)
|
|
594 (strokes-fix-button2-command 'gnus-mouse-pick-article)
|
|
595 (strokes-fix-button2-command 'gnus-article-push-button)
|
|
596 (strokes-fix-button2-command 'dired-mouse-find-file)
|
|
597 (strokes-fix-button2-command 'url-dired-find-file-mouse)
|
|
598 (strokes-fix-button2-command 'dired-u-r-mouse-toggle)
|
|
599 (strokes-fix-button2-command 'dired-u-w-mouse-toggle)
|
|
600 (strokes-fix-button2-command 'dired-u-x-mouse-toggle)
|
|
601 (strokes-fix-button2-command 'dired-g-r-mouse-toggle)
|
|
602 (strokes-fix-button2-command 'dired-g-w-mouse-toggle)
|
|
603 (strokes-fix-button2-command 'dired-g-x-mouse-toggle)
|
|
604 (strokes-fix-button2-command 'dired-o-r-mouse-toggle)
|
|
605 (strokes-fix-button2-command 'dired-o-w-mouse-toggle)
|
|
606 (strokes-fix-button2-command 'isearch-yank-x-selection)
|
|
607 (strokes-fix-button2-command 'occur-mode-mouse-goto)
|
177
|
608 (strokes-fix-button2-command 'cvs-mouse-find-file)
|
153
|
609
|
|
610 ;;; I can fix the customize widget button click, but then
|
|
611 ;;; people will get confused when they try to customize
|
|
612 ;;; strokes with the mouse and customize tells them that
|
|
613 ;;; `strokes-click-command' is mapped to `ad-Orig-widget-button-click'
|
|
614 ;;(strokes-fix-button2-command 'widget-button-click)
|
|
615
|
|
616 ;;; without the advice, each advised function would look like...
|
|
617 ;;(defadvice vm-mouse-button-2 (around vm-strokes activate protect)
|
|
618 ;; "Allow strokes to work in VM."
|
|
619 ;; (if strokes-use-strokes-buffer
|
|
620 ;; ;; then strokes is no good and we'll have to use the original
|
|
621 ;; ad-do-it
|
|
622 ;; ;; otherwise, we can make strokes work too...
|
|
623 ;; (let ((strokes-click-command 'ad-Orig-vm-mouse-button-2))
|
|
624 ;; (strokes-do-stroke (ad-get-arg 0)))))
|
|
625
|
|
626 ;;; Functions...
|
|
627
|
|
628 (defun strokes-lift-p (object)
|
185
|
629 "Return non-nil if object is a stroke-lift."
|
153
|
630 (eq object strokes-lift))
|
|
631
|
|
632 (defun strokes-unset-last-stroke ()
|
|
633 "Undo the last stroke definition."
|
|
634 (interactive)
|
|
635 (let ((command (cdar strokes-global-map)))
|
|
636 (if (y-or-n-p-maybe-dialog-box
|
|
637 (format "really delete last stroke definition, defined to `%s'? "
|
|
638 command))
|
|
639 (progn
|
|
640 (setq strokes-global-map (cdr strokes-global-map))
|
|
641 (message "That stroke has been deleted"))
|
|
642 (message "Nothing done"))))
|
|
643
|
|
644 ;;;###autoload
|
|
645 (defun strokes-global-set-stroke (stroke command)
|
|
646 "Interactively give STROKE the global binding as COMMAND.
|
|
647 Operated just like `global-set-key', except for strokes.
|
|
648 COMMAND is a symbol naming an interactively-callable function. STROKE
|
|
649 is a list of sampled positions on the stroke grid as described in the
|
|
650 documentation for the `strokes-define-stroke' function."
|
|
651 (interactive
|
|
652 (list
|
|
653 (and (or strokes-mode (strokes-mode t))
|
|
654 (strokes-read-complex-stroke
|
|
655 "Define a new stroke. Draw with button1 (or 2). End with button3..."))
|
185
|
656 (read-command-or-command-sexp "command to map stroke to: ")))
|
153
|
657 (strokes-define-stroke strokes-global-map stroke command))
|
|
658
|
|
659 ;;;###autoload
|
|
660 (defalias 'global-set-stroke 'strokes-global-set-stroke)
|
|
661
|
|
662 ;;(defun global-unset-stroke (stroke); FINISH THIS DEFUN!
|
|
663 ;; "delete all strokes matching STROKE from `strokes-global-map',
|
|
664 ;; letting the user input
|
|
665 ;; the stroke with the mouse"
|
|
666 ;; (interactive
|
|
667 ;; (list
|
|
668 ;; (strokes-read-stroke "Enter the stroke you want to delete...")))
|
|
669 ;; (strokes-define-stroke 'strokes-global-map stroke command))
|
|
670
|
|
671 (defun strokes-get-grid-position (stroke-extent position &optional grid-resolution)
|
|
672 "Map POSITION to a new grid position based on its STROKE-EXTENT and GRID-RESOLUTION.
|
185
|
673 STROKE-EXTENT as a list \(\(XMIN . YMIN\) \(XMAX . YMAX\)\).
|
153
|
674 If POSITION is a `strokes-lift', then it is itself returned.
|
|
675 Optional GRID-RESOLUTION may be used in place of STROKES-GRID-RESOLUTION.
|
|
676 The grid is a square whose dimesion is [0,GRID-RESOLUTION)."
|
|
677 (cond ((consp position) ; actual pixel location
|
|
678 (let ((grid-resolution (or grid-resolution strokes-grid-resolution))
|
|
679 (x (car position))
|
|
680 (y (cdr position))
|
|
681 (xmin (caar stroke-extent))
|
|
682 (ymin (cdar stroke-extent))
|
|
683 ;; the `1+' is there to insure that the
|
|
684 ;; formula evaluates correctly at the boundaries
|
|
685 (xmax (1+ (caadr stroke-extent)))
|
|
686 (ymax (1+ (cdadr stroke-extent))))
|
|
687 (cons (floor (* grid-resolution
|
|
688 (/ (float (- x xmin))
|
|
689 (- xmax xmin))))
|
|
690 (floor (* grid-resolution
|
|
691 (/ (float (- y ymin))
|
|
692 (- ymax ymin)))))))
|
|
693 ((strokes-lift-p position) ; stroke lift
|
|
694 strokes-lift)))
|
|
695
|
|
696 (defun strokes-get-stroke-extent (pixel-positions)
|
|
697 "From a list of absolute PIXEL-POSITIONS, returns absolute spatial extent.
|
185
|
698 The return value is a list ((XMIN . YMIN) (XMAX . YMAX))."
|
153
|
699 (if pixel-positions
|
|
700 (let ((xmin (caar pixel-positions))
|
|
701 (xmax (caar pixel-positions))
|
|
702 (ymin (cdar pixel-positions))
|
|
703 (ymax (cdar pixel-positions))
|
|
704 (rest (cdr pixel-positions)))
|
|
705 (while rest
|
|
706 (if (consp (car rest))
|
|
707 (let ((x (caar rest))
|
|
708 (y (cdar rest)))
|
|
709 (if (< x xmin)
|
|
710 (setq xmin x))
|
|
711 (if (> x xmax)
|
|
712 (setq xmax x))
|
|
713 (if (< y ymin)
|
|
714 (setq ymin y))
|
|
715 (if (> y ymax)
|
|
716 (setq ymax y))))
|
|
717 (setq rest (cdr rest)))
|
|
718 (let ((delta-x (- xmax xmin))
|
|
719 (delta-y (- ymax ymin)))
|
|
720 (if (> delta-x delta-y)
|
|
721 (setq ymin (- ymin
|
|
722 (/ (- delta-x delta-y)
|
|
723 2))
|
|
724 ymax (+ ymax
|
|
725 (/ (- delta-x delta-y)
|
|
726 2)))
|
|
727 (setq xmin (- xmin
|
|
728 (/ (- delta-y delta-x)
|
|
729 2))
|
|
730 xmax (+ xmax
|
|
731 (/ (- delta-y delta-x)
|
|
732 2))))
|
|
733 (list (cons xmin ymin)
|
|
734 (cons xmax ymax))))
|
|
735 nil))
|
|
736
|
|
737 (defun strokes-eliminate-consecutive-redundancies (entries)
|
|
738 "Returns a list with no consecutive redundant entries."
|
|
739 ;; defun a grande vitesse grace a Dave G.
|
|
740 (loop for element on entries
|
177
|
741 if (not (equal (car element) (cadr element)))
|
153
|
742 collect (car element)))
|
|
743 ;; (loop for element on entries
|
177
|
744 ;; nconc (if (not (equal (car el) (cadr el)))
|
153
|
745 ;; (list (car el)))))
|
|
746 ;; yet another (orig) way of doing it...
|
|
747 ;; (if entries
|
|
748 ;; (let* ((current (car entries))
|
|
749 ;; (rest (cdr entries))
|
|
750 ;; (non-redundant-list (list current))
|
|
751 ;; (next nil))
|
|
752 ;; (while rest
|
|
753 ;; (setq next (car rest))
|
|
754 ;; (if (equal current next)
|
|
755 ;; (setq rest (cdr rest))
|
|
756 ;; (setq non-redundant-list (cons next non-redundant-list)
|
|
757 ;; current next
|
|
758 ;; rest (cdr rest))))
|
|
759 ;; (nreverse non-redundant-list))
|
|
760 ;; nil))
|
|
761
|
|
762 (defun strokes-renormalize-to-grid (positions &optional grid-resolution)
|
|
763 "Map POSITIONS to a new grid whose dimensions are based on GRID-RESOLUTION.
|
|
764 POSITIONS is a list of positions and stroke-lifts.
|
|
765 Optional GRID-RESOLUTION may be used in place of STROKES-GRID-RESOLUTION.
|
|
766 The grid is a square whose dimesion is [0,GRID-RESOLUTION)."
|
177
|
767 (or grid-resolution (setq grid-resolution strokes-grid-resolution))
|
153
|
768 (let ((stroke-extent (strokes-get-stroke-extent positions)))
|
|
769 (mapcar (function
|
|
770 (lambda (pos)
|
|
771 (strokes-get-grid-position stroke-extent pos grid-resolution)))
|
|
772 positions)))
|
|
773
|
|
774 (defun strokes-fill-stroke (unfilled-stroke &optional force)
|
|
775 "Fill in missing grid locations in the list of UNFILLED-STROKE.
|
|
776 If FORCE is non-nil, then fill the stroke even if it's `stroke-click'.
|
|
777 NOTE: This is where the global variable `strokes-last-stroke' is set."
|
|
778 (setq strokes-last-stroke ; this is global
|
|
779 (if (and (strokes-click-p unfilled-stroke)
|
|
780 (not force))
|
|
781 unfilled-stroke
|
|
782 (loop for grid-locs on unfilled-stroke
|
|
783 nconc (let* ((current (car grid-locs))
|
|
784 (current-is-a-point-p (consp current))
|
|
785 (next (cadr grid-locs))
|
|
786 (next-is-a-point-p (consp next))
|
|
787 (both-are-points-p (and current-is-a-point-p
|
|
788 next-is-a-point-p))
|
|
789 (x1 (and current-is-a-point-p
|
|
790 (car current)))
|
|
791 (y1 (and current-is-a-point-p
|
|
792 (cdr current)))
|
|
793 (x2 (and next-is-a-point-p
|
|
794 (car next)))
|
|
795 (y2 (and next-is-a-point-p
|
|
796 (cdr next)))
|
|
797 (delta-x (and both-are-points-p
|
|
798 (- x2 x1)))
|
|
799 (delta-y (and both-are-points-p
|
|
800 (- y2 y1)))
|
|
801 (slope (and both-are-points-p
|
|
802 (if (zerop delta-x)
|
|
803 nil ; undefined vertical slope
|
|
804 (/ (float delta-y)
|
|
805 delta-x)))))
|
|
806 (cond ((not both-are-points-p)
|
|
807 (list current))
|
|
808 ((null slope) ; undefinded vertical slope
|
|
809 (if (>= delta-y 0)
|
|
810 (loop for y from y1 below y2
|
|
811 collect (cons x1 y))
|
|
812 (loop for y from y1 above y2
|
|
813 collect (cons x1 y))))
|
|
814 ((zerop slope) ; (= y1 y2)
|
|
815 (if (>= delta-x 0)
|
|
816 (loop for x from x1 below x2
|
|
817 collect (cons x y1))
|
|
818 (loop for x from x1 above x2
|
|
819 collect (cons x y1))))
|
|
820 ((>= (abs delta-x) (abs delta-y))
|
|
821 (if (> delta-x 0)
|
|
822 (loop for x from x1 below x2
|
|
823 collect (cons x
|
|
824 (+ y1
|
|
825 (round (* slope
|
|
826 (- x x1))))))
|
|
827 (loop for x from x1 above x2
|
|
828 collect (cons x
|
|
829 (+ y1
|
|
830 (round (* slope
|
|
831 (- x x1))))))))
|
|
832 (t ; (< (abs delta-x) (abs delta-y))
|
|
833 (if (> delta-y 0)
|
|
834 (loop for y from y1 below y2
|
|
835 collect (cons (+ x1
|
|
836 (round (/ (- y y1)
|
|
837 slope)))
|
|
838 y))
|
|
839 (loop for y from y1 above y2
|
|
840 collect (cons (+ x1
|
|
841 (round (/ (- y y1)
|
|
842 slope)))
|
|
843 y))))))))))
|
|
844
|
|
845 (defun strokes-rate-stroke (stroke1 stroke2)
|
|
846 "Rates STROKE1 with STROKE2 and returns a score based on a distance metric.
|
|
847 Note: the rating is an error rating, and therefore, a return of 0
|
|
848 represents a perfect match. Also note that the order of stroke
|
|
849 arguments is order-independent for the algorithm used here."
|
|
850 (if (and stroke1 stroke2)
|
|
851 (let ((rest1 (cdr stroke1))
|
|
852 (rest2 (cdr stroke2))
|
|
853 (err (strokes-distance-squared (car stroke1)
|
|
854 (car stroke2))))
|
|
855 (while (and rest1 rest2)
|
|
856 (while (and (consp (car rest1))
|
|
857 (consp (car rest2)))
|
|
858 (setq err (+ err
|
|
859 (strokes-distance-squared (car rest1)
|
|
860 (car rest2)))
|
|
861 stroke1 rest1
|
|
862 stroke2 rest2
|
|
863 rest1 (cdr stroke1)
|
|
864 rest2 (cdr stroke2)))
|
|
865 (cond ((and (strokes-lift-p (car rest1))
|
|
866 (strokes-lift-p (car rest2)))
|
|
867 (setq rest1 (cdr rest1)
|
|
868 rest2 (cdr rest2)))
|
|
869 ((strokes-lift-p (car rest2))
|
|
870 (while (consp (car rest1))
|
|
871 (setq err (+ err
|
|
872 (strokes-distance-squared (car rest1)
|
|
873 (car stroke2)))
|
|
874 rest1 (cdr rest1))))
|
|
875 ((strokes-lift-p (car rest1))
|
|
876 (while (consp (car rest2))
|
|
877 (setq err (+ err
|
|
878 (strokes-distance-squared (car stroke1)
|
|
879 (car rest2)))
|
|
880 rest2 (cdr rest2))))))
|
|
881 (if (null rest2)
|
|
882 (while (consp (car rest1))
|
|
883 (setq err (+ err
|
|
884 (strokes-distance-squared (car rest1)
|
|
885 (car stroke2)))
|
|
886 rest1 (cdr rest1))))
|
|
887 (if (null rest1)
|
|
888 (while (consp (car rest2))
|
|
889 (setq err (+ err
|
|
890 (strokes-distance-squared (car stroke1)
|
|
891 (car rest2)))
|
|
892 rest2 (cdr rest2))))
|
|
893 (if (or (strokes-lift-p (car rest1))
|
|
894 (strokes-lift-p (car rest2)))
|
|
895 (setq err nil)
|
|
896 err))
|
|
897 nil))
|
|
898
|
|
899 (defun strokes-match-stroke (stroke stroke-map)
|
|
900 "Finds the best matching command of STROKE in STROKE-MAP.
|
|
901 Returns the corresponding match as (COMMAND . SCORE)."
|
|
902 (if (and stroke stroke-map)
|
|
903 (let ((score (strokes-rate-stroke stroke (caar stroke-map)))
|
|
904 (command (cdar stroke-map))
|
|
905 (map (cdr stroke-map)))
|
|
906 (while map
|
|
907 (let ((newscore (strokes-rate-stroke stroke (caar map))))
|
|
908 (if (or (and newscore score (< newscore score))
|
|
909 (and newscore (null score)))
|
|
910 (setq score newscore
|
|
911 command (cdar map)))
|
|
912 (setq map (cdr map))))
|
|
913 (if score
|
|
914 (cons command score)
|
|
915 nil))
|
|
916 nil))
|
|
917
|
|
918 ;;;###autoload
|
|
919 (defun strokes-read-stroke (&optional prompt event)
|
|
920 "Read a simple stroke (interactively) and return the stroke.
|
|
921 Optional PROMPT in minibuffer displays before and during stroke reading.
|
|
922 This function will display the stroke interactively as it is being
|
|
923 entered in the strokes buffer if the variable
|
|
924 `strokes-use-strokes-buffer' is non-nil.
|
185
|
925 Optional EVENT is acceptable as the starting event of the stroke"
|
153
|
926 (save-excursion
|
|
927 (let ((pix-locs nil)
|
|
928 (grid-locs nil)
|
185
|
929 (safe-to-draw-p nil))
|
|
930 (strokes-while-inhibiting-garbage-collector
|
|
931 (if strokes-use-strokes-buffer
|
|
932 ;; switch to the strokes buffer and
|
|
933 ;; display the stroke as it's being read
|
|
934 (save-window-excursion
|
|
935 (set-window-configuration strokes-window-configuration)
|
|
936 (when prompt
|
|
937 (setq event (next-command-event event prompt))
|
|
938 (or (button-press-event-p event)
|
|
939 (error "You must draw with the mouse")))
|
|
940 (or event (setq event (next-event nil prompt)
|
|
941 safe-to-draw-p t))
|
|
942 (unwind-protect
|
|
943 (progn
|
|
944 (while (not (button-release-event-p event))
|
|
945 (if (mouse-event-p event)
|
|
946 (let ((point (event-closest-point event)))
|
|
947 (if (and point safe-to-draw-p)
|
|
948 ;; we can draw that point
|
|
949 (progn
|
|
950 (goto-char point)
|
|
951 (subst-char-in-region point (1+ point) ?\ strokes-character))
|
|
952 ;; otherwise, we can start drawing the next time...
|
|
953 (setq safe-to-draw-p t))
|
|
954 (push (cons (event-x-pixel event)
|
|
955 (event-y-pixel event))
|
|
956 pix-locs))
|
|
957 ;; otherwise, if it's not a mouse-event...
|
|
958 (dispatch-event event))
|
|
959 (setq event (next-event event))))
|
|
960 ;; protected
|
|
961 ;; clean up strokes buffer and then bury it.
|
|
962 (when (equal (buffer-name) strokes-buffer-name)
|
|
963 (subst-char-in-region (point-min) (point-max) strokes-character ?\ )
|
|
964 (goto-char (point-min))
|
|
965 (bury-buffer))))
|
|
966 ;; Otherwise, don't use strokes buffer and read stroke silently
|
|
967 (when prompt
|
|
968 (setq event (next-command-event event prompt))
|
|
969 (or (button-press-event-p event)
|
|
970 (error "You must draw with the mouse")))
|
|
971 (or event (setq event (next-event nil prompt)))
|
|
972 (while (not (button-release-event-p event))
|
|
973 (if (mouse-event-p event)
|
|
974 (push (cons (event-x-pixel event)
|
|
975 (event-y-pixel event))
|
|
976 pix-locs)
|
|
977 (dispatch-event event))
|
|
978 (setq event (next-event event)))))
|
153
|
979 (setq grid-locs (strokes-renormalize-to-grid (nreverse pix-locs)))
|
|
980 (strokes-fill-stroke (strokes-eliminate-consecutive-redundancies grid-locs)))))
|
|
981
|
185
|
982 ;;; This version of `strokes-read-stroke' is not broken, but pathetic.
|
|
983 ;;(defun strokes-read-stroke (&optional prompt event)
|
|
984 ;; "Read a simple stroke (interactively) and return the stroke.
|
|
985 ;;Optional PROMPT in minibuffer displays before and during stroke reading.
|
|
986 ;;This function will display the stroke interactively as it is being
|
|
987 ;;entered in the strokes buffer if the variable
|
|
988 ;;`strokes-use-strokes-buffer' is non-nil.
|
|
989 ;;Optional EVENT is currently not used, but hopefully will be soon."
|
|
990 ;; (save-excursion
|
|
991 ;; (strokes-while-inhibiting-garbage-collector
|
|
992 ;; (let ((pix-locs nil)
|
|
993 ;; (grid-locs nil)
|
|
994 ;; (event (or event (make-event))))
|
|
995 ;; (if strokes-use-strokes-buffer
|
|
996 ;; ;; switch to the strokes buffer and
|
|
997 ;; ;; display the stroke as it's being read
|
|
998 ;; (save-window-excursion
|
|
999 ;; (set-window-configuration strokes-window-configuration)
|
|
1000 ;; (if prompt
|
|
1001 ;; (progn
|
|
1002 ;; (setq event (next-event event prompt))
|
|
1003 ;; (while (not (button-press-event-p event))
|
|
1004 ;; (dispatch-event event)
|
|
1005 ;; (setq event (next-event event)))))
|
|
1006 ;; (unwind-protect
|
|
1007 ;; (progn
|
|
1008 ;; (setq event (next-event event))
|
|
1009 ;; (while (not (button-release-event-p event))
|
|
1010 ;; (if (mouse-event-p event)
|
|
1011 ;; (let ((point (event-closest-point event)))
|
|
1012 ;; (when point
|
|
1013 ;; (goto-char point)
|
|
1014 ;; (subst-char-in-region point (1+ point) ?\ strokes-character))
|
|
1015 ;; (push (cons (event-x-pixel event)
|
|
1016 ;; (event-y-pixel event))
|
|
1017 ;; pix-locs)))
|
|
1018 ;; (setq event (next-event event))))
|
|
1019 ;; ;; protected
|
|
1020 ;; ;; clean up strokes buffer and then bury it.
|
|
1021 ;; (when (equal (buffer-name) strokes-buffer-name)
|
|
1022 ;; (subst-char-in-region (point-min) (point-max) strokes-character ?\ )
|
|
1023 ;; (goto-char (point-min))
|
|
1024 ;; (bury-buffer))))
|
|
1025 ;; ;; Otherwise, don't use strokes buffer and read stroke silently
|
|
1026 ;; (if prompt
|
|
1027 ;; (progn
|
|
1028 ;; (setq event (next-event event prompt))
|
|
1029 ;; (while (not (button-press-event-p event))
|
|
1030 ;; (dispatch-event event)
|
|
1031 ;; (setq event (next-event event)))))
|
|
1032 ;; (setq event (next-event))
|
|
1033 ;; (while (not (button-release-event-p event))
|
|
1034 ;; (if (mouse-event-p event)
|
|
1035 ;; (push (cons (event-x-pixel event)
|
|
1036 ;; (event-y-pixel event))
|
|
1037 ;; pix-locs))
|
|
1038 ;; (setq event (next-event event))))
|
|
1039 ;; (setq grid-locs (strokes-renormalize-to-grid (nreverse pix-locs)))
|
|
1040 ;; (strokes-fill-stroke (strokes-eliminate-consecutive-redundancies grid-locs))))))
|
|
1041
|
153
|
1042 (defun strokes-read-complex-stroke (&optional prompt event)
|
|
1043 "Read a complex stroke (interactively) and return the stroke.
|
|
1044 Optional PROMPT in minibuffer displays before and during stroke reading.
|
|
1045 Note that a complex stroke allows the user to pen-up and pen-down. This
|
|
1046 is implemented by allowing the user to paint with button1 or button2 and
|
|
1047 then complete the stroke with button3.
|
185
|
1048 Optional EVENT is acceptable as the starting event of the stroke"
|
153
|
1049 (save-excursion
|
|
1050 (save-window-excursion
|
185
|
1051 (strokes-while-inhibiting-garbage-collector
|
|
1052 (set-window-configuration strokes-window-configuration)
|
|
1053 (let ((pix-locs nil)
|
|
1054 (grid-locs nil)
|
|
1055 (safe-to-draw-p nil))
|
|
1056 (when prompt
|
|
1057 (setq event (next-command-event event prompt))
|
|
1058 (or (button-press-event-p event)
|
|
1059 (error "You must draw with the mouse")))
|
|
1060 (or event (setq event (next-event nil prompt)
|
|
1061 safe-to-draw-p t))
|
|
1062 (unwind-protect
|
|
1063 (progn
|
|
1064 (while (not (and (button-press-event-p event)
|
|
1065 (eq (event-button event) 3)))
|
|
1066 (while (not (button-release-event-p event))
|
|
1067 (if (mouse-event-p event)
|
|
1068 (let ((point (event-closest-point event)))
|
|
1069 (if (and point safe-to-draw-p)
|
|
1070 ;; we can draw that point
|
|
1071 (progn
|
|
1072 (goto-char point)
|
|
1073 (subst-char-in-region point (1+ point) ?\ strokes-character))
|
|
1074 ;; otherwise, we can start drawing the next time...
|
|
1075 (setq safe-to-draw-p t))
|
|
1076 (push (cons (event-x-pixel event)
|
|
1077 (event-y-pixel event))
|
|
1078 pix-locs))
|
|
1079 (dispatch-event event))
|
|
1080 (setq event (next-event event prompt)))
|
|
1081 (push strokes-lift pix-locs)
|
|
1082 (while (not (button-press-event-p event))
|
|
1083 (dispatch-event event)
|
|
1084 (setq event (next-event event prompt))))
|
|
1085 (setq pix-locs (nreverse (cdr pix-locs)))
|
|
1086 ;; minor bug fix here for when user enters ` *strokes*'
|
|
1087 ;; buffer with a click instead of a drag...
|
|
1088 (when (strokes-lift-p (car pix-locs))
|
|
1089 (setq pix-locs (cdr pix-locs)))
|
|
1090 (setq grid-locs (strokes-renormalize-to-grid pix-locs))
|
|
1091 (strokes-fill-stroke
|
|
1092 (strokes-eliminate-consecutive-redundancies grid-locs)))
|
|
1093 ;; protected
|
|
1094 (when (equal (buffer-name) strokes-buffer-name)
|
|
1095 (subst-char-in-region (point-min) (point-max) strokes-character ?\ )
|
|
1096 (goto-char (point-min))
|
|
1097 (bury-buffer))))))))
|
153
|
1098
|
|
1099 (defun strokes-execute-stroke (stroke)
|
|
1100 "Given STROKE, execute the command which corresponds to it.
|
|
1101 The command will be executed provided one exists for that stroke,
|
|
1102 based on the variable `strokes-minimum-match-score'.
|
|
1103 If no stroke matches, nothing is done and return value is nil."
|
|
1104 (let* ((match (strokes-match-stroke stroke strokes-global-map))
|
|
1105 (command (car match))
|
|
1106 (score (cdr match)))
|
|
1107 (cond ((strokes-click-p stroke)
|
185
|
1108 ;; This is the case of a `click' type event.
|
|
1109 ;; The `sit-for' is a minor frob that has to do with timing
|
|
1110 ;; problems. Without the `sit-for', mouse-yank will not
|
|
1111 ;; yank at the proper location if the user opted for
|
|
1112 ;; mouse-yank-at-point to be nil (i.e. mouse-yank takes
|
|
1113 ;; place at pointer position). The sit-for tells redisplay
|
|
1114 ;; to be sure to wait for the `*strokes*' buffer to vanish
|
|
1115 ;; from consideration when deciding on a point to be used
|
|
1116 ;; for mouse-yank.
|
|
1117 (sit-for 0)
|
153
|
1118 (command-execute strokes-click-command))
|
|
1119 ((and match (<= score strokes-minimum-match-score))
|
|
1120 (message "%s" command)
|
|
1121 (command-execute command))
|
|
1122 ((null strokes-global-map)
|
|
1123 (if (file-exists-p strokes-file)
|
|
1124 (and (y-or-n-p-maybe-dialog-box
|
|
1125 (format "No strokes loaded. Load `%s'? "
|
|
1126 strokes-file))
|
|
1127 (strokes-load-user-strokes))
|
|
1128 (error "No strokes defined; use `global-set-stroke'")))
|
|
1129 (t
|
|
1130 (error
|
|
1131 "No stroke matches; see variable `strokes-minimum-match-score'")
|
|
1132 nil))))
|
|
1133
|
|
1134 ;;;###autoload
|
|
1135 (defun strokes-do-stroke (event)
|
185
|
1136 "Read a simple stroke from the user and then exectute its command.
|
153
|
1137 This must be bound to a mouse event."
|
|
1138 (interactive "e")
|
|
1139 (or strokes-mode (strokes-mode t))
|
|
1140 (strokes-execute-stroke (strokes-read-stroke nil event)))
|
|
1141
|
|
1142 ;;;###autoload
|
|
1143 (defun strokes-do-complex-stroke (event)
|
|
1144 "Read a complex stroke from the user and then exectute its command.
|
|
1145 This must be bound to a mouse event."
|
|
1146 (interactive "e")
|
|
1147 (or strokes-mode (strokes-mode t))
|
|
1148 (strokes-execute-stroke (strokes-read-complex-stroke nil event)))
|
|
1149
|
|
1150 ;;;###autoload
|
|
1151 (defun strokes-describe-stroke (stroke)
|
|
1152 "Displays the command which STROKE maps to, reading STROKE interactively."
|
|
1153 (interactive
|
|
1154 (list
|
|
1155 (strokes-read-complex-stroke
|
|
1156 "Enter stroke to describe; end with button3...")))
|
|
1157 (let* ((match (strokes-match-stroke stroke strokes-global-map))
|
|
1158 (command (or (and (strokes-click-p stroke)
|
|
1159 strokes-click-command)
|
|
1160 (car match)))
|
|
1161 (score (cdr match)))
|
|
1162 (if (or (and match
|
|
1163 (<= score strokes-minimum-match-score))
|
|
1164 (and (strokes-click-p stroke)
|
|
1165 strokes-click-command))
|
|
1166 (message "That stroke maps to `%s'" command)
|
|
1167 (message "That stroke is undefined"))
|
|
1168 (sleep-for 1))) ; helpful for recursive edits
|
|
1169
|
|
1170 ;;;###autoload
|
|
1171 (defalias 'describe-stroke 'strokes-describe-stroke)
|
|
1172
|
|
1173 ;;;###autoload
|
|
1174 (defun strokes-help ()
|
|
1175 "Get instructional help on using the the `strokes' package."
|
|
1176 (interactive)
|
|
1177 (with-displaying-help-buffer
|
|
1178 (function
|
|
1179 (lambda ()
|
|
1180 (save-excursion
|
|
1181 (let ((helpdoc
|
|
1182 "This is help for the strokes package.
|
|
1183
|
185
|
1184 If you find something wrong with strokes, or feel that it can be
|
|
1185 improved in some way, then please feel free to email me:
|
153
|
1186
|
|
1187 David Bakhash <cadet@mit.edu>
|
|
1188
|
|
1189 or just do
|
|
1190
|
|
1191 M-x strokes-report-bug
|
|
1192
|
|
1193 ------------------------------------------------------------
|
|
1194
|
185
|
1195 ** Strokes...
|
|
1196
|
153
|
1197 The strokes package allows you to define strokes (that you make with
|
|
1198 the mouse or other pointer device) that XEmacs can interpret as
|
|
1199 corresponding to commands, and then executes the commands. It does
|
|
1200 character recognition, so you don't have to worry about getting it
|
|
1201 right every time.
|
|
1202
|
185
|
1203 Strokes also allows you to compose documents graphically. You can
|
|
1204 fully edit documents in Chinese, Japanese, etc. based on XEmacs
|
|
1205 strokes. Once you've done so, you can ascii compress-and-encode them
|
|
1206 and then safely save them for later use, send letters to friends
|
|
1207 (using XEmacs, of course). Strokes will later decode these documents,
|
|
1208 extracting the strokes for editing use once again, so the editing
|
|
1209 cycle can continue.
|
|
1210
|
153
|
1211 Strokes are easy to program and fun to use. To start strokes going,
|
|
1212 you'll want to put the following line in your .emacs file:
|
|
1213
|
185
|
1214 (if window-system
|
|
1215 (require 'strokes))
|
153
|
1216
|
|
1217 This will load strokes when and only when you start XEmacs on a window
|
|
1218 system (i.e. that has a pointer (mouse) device, etc.).
|
|
1219
|
|
1220 To toggle strokes-mode, you just do
|
|
1221
|
|
1222 > M-x strokes-mode
|
|
1223
|
185
|
1224 ** Strokes for controlling the behavior of XEmacs...
|
|
1225
|
153
|
1226 When you're ready to start defining strokes, just use the command
|
|
1227
|
|
1228 > M-x global-set-stroke
|
|
1229
|
|
1230 You will see a ` *strokes*' buffer which is waiting for you to enter in
|
|
1231 your stroke. When you enter in the stroke, you draw with button1 or
|
|
1232 button2, and then end with button3. Next, you enter in the command
|
|
1233 which will be executed when that stroke is invoked. Simple as that.
|
|
1234 For now, try to define a stroke to copy a region. This is a popular
|
|
1235 edit command, so type
|
|
1236
|
|
1237 > M-x global-set-stroke
|
|
1238
|
|
1239 Then, in the ` *strokes*' buffer, draw the letter `C' (for `copy')
|
|
1240 and then, when it asks you to enter the command to map that to, type
|
|
1241
|
|
1242 > copy-region-as-kill
|
|
1243
|
|
1244 That's about as hard as it gets.
|
|
1245 Remember: paint with button1 or button2 and then end with button3.
|
|
1246
|
|
1247 If ever you want to know what a certain strokes maps to, then do
|
|
1248
|
|
1249 > M-x describe-stroke
|
|
1250
|
|
1251 and you can enter in any arbitrary stroke. Remember: The strokes
|
|
1252 package lets you program in simple and complex (multi-lift) strokes.
|
|
1253 The only difference is how you *invoke* the two. You will most likely
|
|
1254 use simple strokes, as complex strokes were developed for
|
|
1255 Chinese/Japanese/Korean. So the middle mouse button (button2) will
|
|
1256 invoke the command `strokes-do-stroke' in buffers where button2 doesn't
|
|
1257 already have a meaning other than its original, which is `mouse-yank'.
|
|
1258 But don't worry: `mouse-yank' will still work with strokes (see the
|
|
1259 variable `strokes-click-command').
|
|
1260
|
|
1261 If ever you define a stroke which you don't like, then you can unset
|
|
1262 it with the command
|
|
1263
|
|
1264 > M-x strokes-unset-last-stroke
|
|
1265
|
|
1266 You can always get an idea of what your current strokes look like with
|
|
1267 the command
|
|
1268
|
|
1269 > M-x list-strokes
|
|
1270
|
177
|
1271 Your strokes will be displayed in alphabetical order (based on command
|
|
1272 names) and the beginning of each simple stroke will be marked by a
|
|
1273 color dot. Since you may have several simple strokes in a complex
|
|
1274 stroke, the dot colors are arranged in the rainbow color sequence,
|
|
1275 `ROYGBIV'. If you want a listing of your strokes from most recent
|
|
1276 down, then use a prefix argument:
|
|
1277
|
|
1278 > C-u M-x list-strokes
|
153
|
1279
|
|
1280 Your strokes are stored as you enter them. They get saved in a file
|
|
1281 called ~/.strokes, along with other strokes configuration variables.
|
|
1282 You can change this location by setting the variable `strokes-file'.
|
|
1283 You will be prompted to save them when you exit XEmacs, or you can save
|
|
1284 them with
|
|
1285
|
|
1286 > M-x save-strokes
|
|
1287
|
|
1288 Your strokes get loaded automatically when you enable `strokes-mode'.
|
|
1289 You can also load in your user-defined strokes with
|
|
1290
|
|
1291 > M-x load-user-strokes
|
|
1292
|
185
|
1293 ** Strokes for pictographic editing...
|
153
|
1294
|
185
|
1295 If you'd like to create graphical files with strokes, you'll have to
|
|
1296 be running XEmacs on a window system, with XPM support. You use the
|
|
1297 binding C-button2 to start drawing your strokes. These are just
|
|
1298 complex strokes, and thus you continue drawing with buttons 1 or 2 and
|
|
1299 end with button-3. Then the stroke glyph gets inserted into the
|
|
1300 buffer. You treat it like any other character, which you can copy,
|
|
1301 paste, delete, move, etc. The command which is bound to C-button2 is
|
|
1302 called `strokes-compose-complex-stroke'. When all is done, you may
|
|
1303 want to send the file, or save it. This is done with
|
153
|
1304
|
185
|
1305 > M-x strokes-encode-buffer
|
|
1306
|
|
1307 Likewise, to decode the strokes from a strokes-encoded buffer you do
|
|
1308
|
|
1309 > M-x strokes-decode-buffer
|
|
1310
|
|
1311 ** A few more important things...
|
|
1312
|
|
1313 o The command `strokes-do-complex-stroke' is invoked with M-button2, so that you
|
|
1314 can execute complex strokes (i.e. with more than one lift) if preferred.
|
153
|
1315
|
|
1316 o Strokes are a bit computer-dependent in that they depend somewhat on
|
|
1317 the speed of the computer you're working on. This means that you
|
|
1318 may have to tweak some variables. You can read about them in the
|
|
1319 commentary of `strokes.el'. Better to just use apropos and read their
|
|
1320 docstrings. All variables/functions start with `strokes'. The one
|
|
1321 variable which many people wanted to see was
|
|
1322 `strokes-use-strokes-buffer' which allows the user to use strokes
|
|
1323 silently--without displaying the strokes. All variables can be set
|
|
1324 by customizing the group named `strokes' via the customization package:
|
|
1325
|
185
|
1326 > M-x customize"))
|
153
|
1327 (princ helpdoc standard-output)))))))
|
|
1328
|
|
1329 (defun strokes-report-bug ()
|
|
1330 "Submit a bug report for strokes."
|
|
1331 (interactive)
|
|
1332 (let ((reporter-prompt-for-summary-p t))
|
|
1333 (or (boundp 'reporter-version)
|
|
1334 (setq reporter-version
|
|
1335 "Your version of reporter is obsolete. Please upgrade."))
|
|
1336 (reporter-submit-bug-report
|
|
1337 strokes-bug-address "Strokes"
|
|
1338 (cons
|
|
1339 'strokes-version
|
|
1340 (nconc
|
|
1341 (mapcar
|
|
1342 'intern
|
|
1343 (sort
|
|
1344 (let (completion-ignore-case)
|
|
1345 (all-completions "strokes-" obarray 'user-variable-p))
|
|
1346 'string-lessp))
|
|
1347 (list 'reporter-version)))
|
|
1348 (function
|
|
1349 (lambda ()
|
|
1350 (save-excursion
|
|
1351 (mail-position-on-field "subject")
|
|
1352 (beginning-of-line)
|
|
1353 (skip-chars-forward "^:\n")
|
|
1354 (if (looking-at ": Strokes;")
|
|
1355 (progn
|
|
1356 (goto-char (match-end 0))
|
|
1357 (delete-char -1)
|
|
1358 (insert " " strokes-version " bug:")))))))))
|
|
1359
|
|
1360 (defsubst strokes-fill-current-buffer-with-whitespace ()
|
185
|
1361 "Erase the contents of the current buffer and fill it with whitespace."
|
153
|
1362 (erase-buffer)
|
177
|
1363 (loop repeat (frame-height) do
|
|
1364 (insert-char ?\ (1- (frame-width)))
|
|
1365 (newline))
|
153
|
1366 (goto-char (point-min)))
|
|
1367
|
185
|
1368 (defun strokes-window-configuration-changed-p ()
|
|
1369 "Non-nil if the `strokes-window-configuration' frame properties changed.
|
|
1370 This is based on the last time the `strokes-window-configuration was updated."
|
|
1371 (not (and (eq (selected-frame)
|
|
1372 (plist-get strokes-window-configuration-plist
|
|
1373 'frame))
|
|
1374 (eq (frame-height)
|
|
1375 (plist-get strokes-window-configuration-plist
|
|
1376 'frame-height))
|
|
1377 (eq (frame-width)
|
|
1378 (plist-get strokes-window-configuration-plist
|
|
1379 'frame-width)))))
|
|
1380
|
|
1381 (defun strokes-update-window-configuration-plist ()
|
|
1382 "Update the `strokes-window-configuration-plist' based on the current state."
|
|
1383 (plist-put strokes-window-configuration-plist
|
|
1384 'frame
|
|
1385 (selected-frame))
|
|
1386 (plist-put strokes-window-configuration-plist
|
|
1387 'frame-height
|
|
1388 (frame-height))
|
|
1389 (plist-put strokes-window-configuration-plist
|
|
1390 'frame-width
|
|
1391 (frame-width)))
|
|
1392
|
153
|
1393 (defun strokes-update-window-configuration ()
|
185
|
1394 "Update the `strokes-window-configuration'."
|
153
|
1395 (interactive)
|
|
1396 (let ((current-window (selected-window)))
|
|
1397 (cond ((or (window-minibuffer-p current-window)
|
|
1398 (window-dedicated-p current-window))
|
|
1399 ;; don't try to update strokes window configuration
|
|
1400 ;; if window is dedicated or a minibuffer
|
|
1401 nil)
|
|
1402 ((or (interactive-p)
|
|
1403 (not (buffer-live-p (get-buffer strokes-buffer-name)))
|
|
1404 (null strokes-window-configuration))
|
|
1405 ;; create `strokes-window-configuration' from scratch...
|
|
1406 (save-excursion
|
|
1407 (save-window-excursion
|
|
1408 (get-buffer-create strokes-buffer-name)
|
|
1409 (set-window-buffer current-window strokes-buffer-name)
|
|
1410 (delete-other-windows)
|
|
1411 (fundamental-mode)
|
|
1412 (auto-save-mode 0)
|
|
1413 (if (featurep 'font-lock)
|
|
1414 (font-lock-mode 0))
|
|
1415 (abbrev-mode 0)
|
|
1416 (buffer-disable-undo (current-buffer))
|
|
1417 (setq truncate-lines nil)
|
|
1418 (strokes-fill-current-buffer-with-whitespace)
|
|
1419 (setq strokes-window-configuration (current-window-configuration))
|
185
|
1420 (strokes-update-window-configuration-plist)
|
153
|
1421 (bury-buffer))))
|
185
|
1422 ((strokes-window-configuration-changed-p) ; simple update
|
|
1423 ;; update the strokes-window-configuration for this
|
|
1424 ;; specific frame...
|
153
|
1425 (save-excursion
|
|
1426 (save-window-excursion
|
|
1427 (set-window-buffer current-window strokes-buffer-name)
|
|
1428 (delete-other-windows)
|
|
1429 (strokes-fill-current-buffer-with-whitespace)
|
|
1430 (setq strokes-window-configuration (current-window-configuration))
|
185
|
1431 (strokes-update-window-configuration-plist)
|
153
|
1432 (bury-buffer)))))))
|
|
1433
|
|
1434 ;;;###autoload
|
|
1435 (defun strokes-load-user-strokes ()
|
|
1436 "Load user-defined strokes from file named by `strokes-file'."
|
|
1437 (interactive)
|
|
1438 (cond ((and (file-exists-p strokes-file)
|
|
1439 (file-readable-p strokes-file))
|
|
1440 (load-file strokes-file))
|
|
1441 ((interactive-p)
|
|
1442 (error "Trouble loading user-defined strokes; nothing done"))
|
|
1443 (t
|
|
1444 (message "No user-defined strokes, sorry"))))
|
|
1445
|
|
1446 ;;;###autoload
|
|
1447 (defalias 'load-user-strokes 'strokes-load-user-strokes)
|
|
1448
|
|
1449 (defun strokes-prompt-user-save-strokes ()
|
|
1450 "Save user-defined strokes to file named by `strokes-file'."
|
|
1451 (interactive)
|
|
1452 (save-excursion
|
|
1453 (let ((current strokes-global-map))
|
|
1454 (unwind-protect
|
|
1455 (progn
|
|
1456 (setq strokes-global-map nil)
|
|
1457 (strokes-load-user-strokes)
|
|
1458 (if (and (not (equal current strokes-global-map))
|
|
1459 (or (interactive-p)
|
|
1460 (yes-or-no-p-maybe-dialog-box "save your strokes? ")))
|
|
1461 (progn
|
|
1462 (require 'pp) ; pretty-print variables
|
|
1463 (message "Saving strokes in %s..." strokes-file)
|
|
1464 (get-buffer-create "*saved-strokes*")
|
|
1465 (set-buffer "*saved-strokes*")
|
|
1466 (erase-buffer)
|
|
1467 (emacs-lisp-mode)
|
|
1468 (goto-char (point-min))
|
|
1469 (insert-string
|
|
1470 ";; -*- Syntax: Emacs-Lisp; Mode: emacs-lisp -*-\n")
|
|
1471 (insert-string (format ";;; saved strokes for %s, as of %s\n\n"
|
|
1472 (user-full-name)
|
|
1473 (format-time-string "%B %e, %Y" nil)))
|
|
1474 (message "Saving strokes in %s..." strokes-file)
|
|
1475 (insert-string (format "(setq strokes-global-map '%s)"
|
|
1476 (pp current)))
|
|
1477 (message "Saving strokes in %s..." strokes-file)
|
|
1478 (indent-region (point-min) (point-max) nil)
|
|
1479 (write-region (point-min)
|
|
1480 (point-max)
|
|
1481 strokes-file))
|
|
1482 (message "(no changes need to be saved)")))
|
|
1483 ;; protected
|
|
1484 (if (get-buffer "*saved-strokes*")
|
|
1485 (kill-buffer (get-buffer "*saved-strokes*")))
|
|
1486 (setq strokes-global-map current)))))
|
|
1487
|
|
1488 (defalias 'save-strokes 'strokes-prompt-user-save-strokes)
|
|
1489
|
|
1490 (defun strokes-toggle-strokes-buffer (&optional arg)
|
|
1491 "Toggle the use of the strokes buffer.
|
|
1492 In other words, toggle the variabe `strokes-use-strokes-buffer'.
|
|
1493 With ARG, use strokes buffer if and only if ARG is positive or true.
|
|
1494 Returns value of `strokes-use-strokes-buffer'."
|
|
1495 (interactive "P")
|
|
1496 (setq strokes-use-strokes-buffer
|
|
1497 (if arg (> (prefix-numeric-value arg) 0)
|
|
1498 (not strokes-use-strokes-buffer))))
|
|
1499
|
177
|
1500 (defun strokes-xpm-for-stroke (&optional stroke bufname b/w-only)
|
|
1501 "Create an xpm pixmap for the given STROKE in buffer `*strokes-xpm*'.
|
|
1502 If STROKE is not supplied, then `strokes-last-stroke' will be used.
|
153
|
1503 Optional BUFNAME to name something else.
|
|
1504 The pixmap will contain time information via rainbow dot colors
|
177
|
1505 where each individual strokes begins.
|
|
1506 Optional B/W-ONLY non-nil will create a mono pixmap, not intended
|
|
1507 for trying to figure out the order of strokes, but rather for reading
|
|
1508 the stroke as a character in some language."
|
|
1509 (interactive)
|
153
|
1510 (save-excursion
|
|
1511 (let ((buf (get-buffer-create (or bufname "*strokes-xpm*")))
|
|
1512 (stroke (strokes-eliminate-consecutive-redundancies
|
|
1513 (strokes-fill-stroke
|
177
|
1514 (strokes-renormalize-to-grid (or stroke
|
|
1515 strokes-last-stroke)
|
|
1516 31))))
|
153
|
1517 (lift-flag t)
|
177
|
1518 (rainbow-chars (list ?R ?O ?Y ?G ?B ?P))) ; ROYGBIV w/o indigo
|
153
|
1519 (set-buffer buf)
|
|
1520 (erase-buffer)
|
177
|
1521 (insert strokes-xpm-header)
|
153
|
1522 (loop repeat 33 do
|
|
1523 (insert-char ?\")
|
|
1524 (insert-char ?\ 33)
|
|
1525 (insert "\",")
|
177
|
1526 (newline)
|
|
1527 finally
|
|
1528 (forward-line -1)
|
|
1529 (end-of-line)
|
|
1530 (insert "}\n"))
|
153
|
1531 (loop for point in stroke
|
|
1532 for x = (car-safe point)
|
|
1533 for y = (cdr-safe point) do
|
|
1534 (cond ((consp point)
|
|
1535 ;; draw a point, and possibly a starting-point
|
177
|
1536 (if (and lift-flag (not b/w-only))
|
153
|
1537 ;; mark starting point with the appropriate color
|
|
1538 (let ((char (or (car rainbow-chars) ?\.)))
|
|
1539 (loop for i from 0 to 2 do
|
|
1540 (loop for j from 0 to 2 do
|
|
1541 (goto-line (+ 16 i y))
|
|
1542 (forward-char (+ 1 j x))
|
|
1543 (delete-char 1)
|
|
1544 (insert-char char)))
|
|
1545 (setq rainbow-chars (cdr rainbow-chars)
|
|
1546 lift-flag nil))
|
|
1547 ;; Otherwise, just plot the point...
|
|
1548 (goto-line (+ 17 y))
|
|
1549 (forward-char (+ 2 x))
|
|
1550 (subst-char-in-region (point) (1+ (point)) ?\ ?\*)))
|
|
1551 ((strokes-lift-p point)
|
|
1552 ;; a lift--tell the loop to X out the next point...
|
177
|
1553 (setq lift-flag t))))
|
|
1554 (when (interactive-p)
|
|
1555 (require 'xpm-mode)
|
|
1556 (pop-to-buffer "*strokes-xpm*")
|
|
1557 ;; (xpm-mode 1)
|
|
1558 (xpm-show-image)
|
|
1559 (goto-char (point-min))))))
|
|
1560
|
185
|
1561 ;;; Strokes Edit stuff... ### NOT IMLEMENTED YET ###
|
177
|
1562
|
185
|
1563 ;;(defun strokes-edit-quit ()
|
|
1564 ;; (interactive)
|
|
1565 ;; (or (one-window-p t 0)
|
|
1566 ;; (delete-window))
|
|
1567 ;; (kill-buffer "*Strokes List*"))
|
177
|
1568
|
185
|
1569 ;;(define-derived-mode edit-strokes-mode list-mode
|
|
1570 ;; "Edit-Strokes"
|
|
1571 ;; "Major mode for `edit-strokes' and `list-strokes' buffers.
|
177
|
1572
|
185
|
1573 ;;Editing commands:
|
177
|
1574
|
185
|
1575 ;;\\{edit-strokes-mode-map}"
|
|
1576 ;; (setq truncate-lines nil
|
|
1577 ;; auto-show-mode nil ; don't want problems here either
|
|
1578 ;; mode-popup-menu edit-strokes-menu) ; what about extent-specific stuff?
|
|
1579 ;; (and (featurep 'menubar)
|
|
1580 ;; current-menubar
|
|
1581 ;; (set (make-local-variable 'current-menubar)
|
|
1582 ;; (copy-sequence current-menubar))
|
|
1583 ;; (add-submenu nil edit-strokes-menu)))
|
177
|
1584
|
185
|
1585 ;;(let ((map edit-strokes-mode-map))
|
|
1586 ;; (define-key map "<" 'beginning-of-buffer)
|
|
1587 ;; (define-key map ">" 'end-of-buffer)
|
|
1588 ;; ;; (define-key map "c" 'strokes-copy-other-face)
|
|
1589 ;; ;; (define-key map "C" 'strokes-copy-this-face)
|
|
1590 ;; ;; (define-key map "s" 'strokes-smaller)
|
|
1591 ;; ;; (define-key map "l" 'strokes-larger)
|
|
1592 ;; ;; (define-key map "b" 'strokes-bold)
|
|
1593 ;; ;; (define-key map "i" 'strokes-italic)
|
|
1594 ;; (define-key map "e" 'strokes-list-edit)
|
|
1595 ;; ;; (define-key map "f" 'strokes-font)
|
|
1596 ;; ;; (define-key map "u" 'strokes-underline)
|
|
1597 ;; ;; (define-key map "t" 'strokes-truefont)
|
|
1598 ;; ;; (define-key map "F" 'strokes-foreground)
|
|
1599 ;; ;; (define-key map "B" 'strokes-background)
|
|
1600 ;; ;; (define-key map "D" 'strokes-doc-string)
|
|
1601 ;; (define-key map "a" 'strokes-global-set-stroke)
|
|
1602 ;; (define-key map "d" 'strokes-list-delete-stroke)
|
|
1603 ;; ;; (define-key map "n" 'strokes-list-next)
|
|
1604 ;; ;; (define-key map "p" 'strokes-list-prev)
|
|
1605 ;; ;; (define-key map " " 'strokes-list-next)
|
|
1606 ;; ;; (define-key map "\C-?" 'strokes-list-prev)
|
|
1607 ;; (define-key map "g" 'strokes-list-strokes) ; refresh display
|
|
1608 ;; (define-key map "q" 'strokes-edit-quit)
|
|
1609 ;; (define-key map [(control c) (control c)] 'bury-buffer))
|
153
|
1610
|
185
|
1611 ;;;;;###autoload
|
|
1612 ;;(defun strokes-edit-strokes (&optional chronological strokes-map)
|
|
1613 ;; ;; ### DEAL WITH THE 2nd ARGUMENT ISSUE! ###
|
|
1614 ;; "Edit strokes in a pop-up buffer containing strokes and their definitions.
|
|
1615 ;;If STROKES-MAP is not given, `strokes-global-map' will be used instead.
|
177
|
1616
|
185
|
1617 ;;Editing commands:
|
177
|
1618
|
185
|
1619 ;;\\{edit-faces-mode-map}"
|
|
1620 ;; (interactive "P")
|
|
1621 ;; (pop-to-buffer (get-buffer-create "*Strokes List*"))
|
|
1622 ;; (reset-buffer (current-buffer)) ; handy function from minibuf.el
|
|
1623 ;; (setq strokes-map (or strokes-map
|
|
1624 ;; strokes-global-map
|
|
1625 ;; (progn
|
|
1626 ;; (strokes-load-user-strokes)
|
|
1627 ;; strokes-global-map)))
|
|
1628 ;; (or chronological
|
|
1629 ;; (setq strokes-map (sort (copy-sequence strokes-map)
|
|
1630 ;; 'strokes-alphabetic-lessp)))
|
|
1631 ;; ;; (push-window-configuration)
|
|
1632 ;; (insert
|
|
1633 ;; "Command Stroke\n"
|
|
1634 ;; "------- ------")
|
|
1635 ;; (loop for def in strokes-map
|
|
1636 ;; for i from 0 to (1- (length strokes-map)) do
|
|
1637 ;; (let ((stroke (car def))
|
|
1638 ;; (command-name (symbol-name (cdr def))))
|
|
1639 ;; (strokes-xpm-for-stroke stroke " *strokes-xpm*")
|
|
1640 ;; (newline 2)
|
|
1641 ;; (insert-char ?\ 45)
|
|
1642 ;; (beginning-of-line)
|
|
1643 ;; (insert command-name)
|
|
1644 ;; (beginning-of-line)
|
|
1645 ;; (forward-char 45)
|
|
1646 ;; (set (intern (format "strokes-list-annotation-%d" i))
|
|
1647 ;; (make-annotation (make-glyph
|
|
1648 ;; (list
|
|
1649 ;; (vector 'xpm
|
|
1650 ;; :data (buffer-substring
|
|
1651 ;; (point-min " *strokes-xpm*")
|
|
1652 ;; (point-max " *strokes-xpm*")
|
|
1653 ;; " *strokes-xpm*"))
|
|
1654 ;; [string :data "[Stroke]"]))
|
|
1655 ;; (point) 'text))
|
|
1656 ;; (set-annotation-data (symbol-value (intern (format "strokes-list-annotation-%d" i)))
|
|
1657 ;; def))
|
|
1658 ;; finally do (kill-region (1+ (point)) (point-max)))
|
|
1659 ;; (edit-strokes-mode)
|
|
1660 ;; (goto-char (point-min)))
|
177
|
1661
|
185
|
1662 ;;;;;###autoload
|
|
1663 ;;(defalias 'edit-strokes 'strokes-edit-strokes)
|
177
|
1664
|
|
1665 ;;;###autoload
|
|
1666 (defun strokes-list-strokes (&optional chronological strokes-map)
|
|
1667 "Pop up a buffer containing an alphabetical listing of strokes in STROKES-MAP.
|
|
1668 With CHRONOLOGICAL prefix arg \(\\[universal-argument]\) list strokes
|
|
1669 chronologically by command name.
|
|
1670 If STROKES-MAP is not given, `strokes-global-map' will be used instead."
|
|
1671 (interactive "P")
|
|
1672 (setq strokes-map (or strokes-map
|
|
1673 strokes-global-map
|
|
1674 (progn
|
|
1675 (strokes-load-user-strokes)
|
|
1676 strokes-global-map)))
|
|
1677 (if (not chronological)
|
|
1678 ;; then alphabetize the strokes based on command names...
|
|
1679 (setq strokes-map (sort (copy-sequence strokes-map)
|
|
1680 'strokes-alphabetic-lessp)))
|
153
|
1681 (push-window-configuration)
|
|
1682 (set-buffer (get-buffer-create "*Strokes List*"))
|
|
1683 (setq buffer-read-only nil)
|
|
1684 (erase-buffer)
|
|
1685 (insert
|
|
1686 "Command Stroke\n"
|
177
|
1687 "------- ------")
|
|
1688 (loop for def in strokes-map do
|
153
|
1689 (let ((stroke (car def))
|
177
|
1690 (command-name (symbol-name (cdr def))))
|
153
|
1691 (strokes-xpm-for-stroke stroke " *strokes-xpm*")
|
177
|
1692 (newline 2)
|
185
|
1693 (insert-char ?\ 45)
|
153
|
1694 (beginning-of-line)
|
177
|
1695 (insert command-name)
|
153
|
1696 (beginning-of-line)
|
|
1697 (forward-char 45)
|
|
1698 (make-annotation (make-glyph
|
|
1699 (list
|
|
1700 (vector 'xpm
|
|
1701 :data (buffer-substring
|
|
1702 (point-min " *strokes-xpm*")
|
|
1703 (point-max " *strokes-xpm*")
|
|
1704 " *strokes-xpm*"))
|
|
1705 [string :data "[Image]"]))
|
177
|
1706 (point) 'text))
|
|
1707 finally do (kill-region (1+ (point)) (point-max)))
|
153
|
1708 (view-buffer "*Strokes List*" t)
|
|
1709 (goto-char (point-min))
|
177
|
1710 (define-key view-minor-mode-map [(q)] (lambda ()
|
|
1711 (interactive)
|
|
1712 (view-quit)
|
|
1713 (pop-window-configuration)
|
|
1714 ;; (bury-buffer "*Strokes List*")
|
|
1715 (define-key view-minor-mode-map [(q)] 'view-quit))))
|
|
1716
|
|
1717 (defun strokes-alphabetic-lessp (stroke1 stroke2)
|
|
1718 "T iff command name for STROKE1 is less than STROKE2's in lexicographic order."
|
|
1719 (let ((command-name-1 (symbol-name (cdr stroke1)))
|
|
1720 (command-name-2 (symbol-name (cdr stroke2))))
|
|
1721 (string-lessp command-name-1 command-name-2)))
|
153
|
1722
|
|
1723 ;;;###autoload
|
|
1724 (defalias 'list-strokes 'strokes-list-strokes)
|
|
1725
|
|
1726 ;;;###autoload
|
|
1727 (defun strokes-mode (&optional arg)
|
|
1728 "Toggle strokes being enabled.
|
|
1729 With ARG, turn strokes on if and only if ARG is positive or true.
|
|
1730 Note that `strokes-mode' is a global mode. Think of it as a minor
|
|
1731 mode in all buffers when activated.
|
|
1732 By default, strokes are invoked with mouse button-2. You can define
|
|
1733 new strokes with
|
|
1734
|
185
|
1735 > M-x global-set-stroke
|
|
1736
|
|
1737 To use strokes for pictographic editing, such as Chinese/Japanese, use
|
|
1738 Sh-button-2, which draws strokes and inserts them. Encode/decode your
|
|
1739 strokes with
|
|
1740
|
|
1741 > M-x strokes-encode-buffer
|
|
1742 > M-x strokes-decode-buffer"
|
153
|
1743 (interactive "P")
|
|
1744 (let ((on-p (if arg
|
|
1745 (> (prefix-numeric-value arg) 0)
|
|
1746 (not strokes-mode))))
|
|
1747 (cond ((not (device-on-window-system-p))
|
|
1748 (error "Can't use strokes without windows"))
|
|
1749 (on-p ; turn on strokes
|
|
1750 (and (file-exists-p strokes-file)
|
|
1751 (null strokes-global-map)
|
|
1752 (strokes-load-user-strokes))
|
185
|
1753 (add-hook 'kill-emacs-query-functions
|
153
|
1754 'strokes-prompt-user-save-strokes)
|
|
1755 (add-hook 'select-frame-hook
|
|
1756 'strokes-update-window-configuration)
|
|
1757 (strokes-update-window-configuration)
|
|
1758 (define-key global-map [(button2)] 'strokes-do-stroke)
|
185
|
1759 (define-key global-map [(meta button2)] 'strokes-do-complex-stroke)
|
|
1760 ;; (define-key global-map [(control button2)] 'strokes-do-complex-stroke)
|
|
1761 (define-key global-map [(control button2)]
|
|
1762 'strokes-compose-complex-stroke)
|
153
|
1763 (ad-activate-regexp "^strokes-") ; advise button2 commands
|
|
1764 (setq strokes-mode t))
|
|
1765 (t ; turn off strokes
|
|
1766 (if (get-buffer strokes-buffer-name)
|
|
1767 (kill-buffer (get-buffer strokes-buffer-name)))
|
|
1768 (remove-hook 'select-frame-hook
|
|
1769 'strokes-update-window-configuration)
|
|
1770 (if (string-match "^strokes-" (symbol-name (key-binding [(button2)])))
|
|
1771 (define-key global-map [(button2)] strokes-click-command))
|
185
|
1772 (if (string-match "^strokes-" (symbol-name (key-binding [(meta button2)])))
|
|
1773 (global-unset-key [(meta button2)]))
|
153
|
1774 (if (string-match "^strokes-" (symbol-name (key-binding [(control button2)])))
|
|
1775 (global-unset-key [(control button2)]))
|
185
|
1776 ;; (if (string-match "^strokes-" (symbol-name (key-binding [(shift button2)])))
|
|
1777 ;; (global-unset-key [(shift button2)]))
|
153
|
1778 (ad-deactivate-regexp "^strokes-") ; unadvise strokes-button2 commands
|
|
1779 (setq strokes-mode nil))))
|
|
1780 (redraw-modeline))
|
|
1781
|
|
1782 (add-minor-mode 'strokes-mode strokes-modeline-string nil nil 'strokes-mode)
|
|
1783
|
185
|
1784 ;;;; strokes-xpm stuff (later may be separate)...
|
|
1785
|
|
1786 ;; This is the stuff that will eventuall be used for composing letters in
|
|
1787 ;; any language, compression, decompression, graphics, editing, etc.
|
|
1788
|
|
1789 (require 'atomic-extents) ; might as well say
|
|
1790 ; (require 'not-so-atomic-extents)
|
|
1791 ; but what can you do?
|
|
1792
|
|
1793 ;;(unless (find-face 'strokes-char-face)
|
|
1794 (copy-face 'default 'strokes-char-face)
|
|
1795 (set-face-background 'strokes-char-face "lightgray") ; I should really
|
|
1796 ; make this a
|
|
1797 ; user-option,
|
|
1798 ; but I'm too
|
|
1799 ; lazy right now.
|
|
1800 ; In a few days.
|
177
|
1801
|
|
1802 (defconst strokes-char-value-hashtable (make-hashtable 62) ;
|
|
1803 ; (make-char-table
|
|
1804 ; 'syntax)
|
|
1805 ; in 20.*
|
185
|
1806 ;; ### This will/should become a char-table for XEmacs-20 !!! ###
|
177
|
1807 "The table which stores values for the character keys.")
|
|
1808 (puthash ?0 0 strokes-char-value-hashtable) ; (put-char-table ?0 0
|
|
1809 ; strokes-value-chartable)
|
|
1810 ; in 20.*
|
|
1811 (puthash ?1 1 strokes-char-value-hashtable)
|
|
1812 (puthash ?2 2 strokes-char-value-hashtable)
|
|
1813 (puthash ?3 3 strokes-char-value-hashtable)
|
|
1814 (puthash ?4 4 strokes-char-value-hashtable)
|
|
1815 (puthash ?5 5 strokes-char-value-hashtable)
|
|
1816 (puthash ?6 6 strokes-char-value-hashtable)
|
|
1817 (puthash ?7 7 strokes-char-value-hashtable)
|
|
1818 (puthash ?8 8 strokes-char-value-hashtable)
|
|
1819 (puthash ?9 9 strokes-char-value-hashtable)
|
|
1820 (puthash ?a 10 strokes-char-value-hashtable)
|
|
1821 (puthash ?b 11 strokes-char-value-hashtable)
|
|
1822 (puthash ?c 12 strokes-char-value-hashtable)
|
|
1823 (puthash ?d 13 strokes-char-value-hashtable)
|
|
1824 (puthash ?e 14 strokes-char-value-hashtable)
|
|
1825 (puthash ?f 15 strokes-char-value-hashtable)
|
|
1826 (puthash ?g 16 strokes-char-value-hashtable)
|
|
1827 (puthash ?h 17 strokes-char-value-hashtable)
|
|
1828 (puthash ?i 18 strokes-char-value-hashtable)
|
|
1829 (puthash ?j 19 strokes-char-value-hashtable)
|
|
1830 (puthash ?k 20 strokes-char-value-hashtable)
|
|
1831 (puthash ?l 21 strokes-char-value-hashtable)
|
|
1832 (puthash ?m 22 strokes-char-value-hashtable)
|
|
1833 (puthash ?n 23 strokes-char-value-hashtable)
|
|
1834 (puthash ?o 24 strokes-char-value-hashtable)
|
|
1835 (puthash ?p 25 strokes-char-value-hashtable)
|
|
1836 (puthash ?q 26 strokes-char-value-hashtable)
|
|
1837 (puthash ?r 27 strokes-char-value-hashtable)
|
|
1838 (puthash ?s 28 strokes-char-value-hashtable)
|
|
1839 (puthash ?t 29 strokes-char-value-hashtable)
|
|
1840 (puthash ?u 30 strokes-char-value-hashtable)
|
|
1841 (puthash ?v 31 strokes-char-value-hashtable)
|
|
1842 (puthash ?w 32 strokes-char-value-hashtable)
|
|
1843 (puthash ?x 33 strokes-char-value-hashtable)
|
|
1844 (puthash ?y 34 strokes-char-value-hashtable)
|
|
1845 (puthash ?z 35 strokes-char-value-hashtable)
|
|
1846 (puthash ?A 36 strokes-char-value-hashtable)
|
|
1847 (puthash ?B 37 strokes-char-value-hashtable)
|
|
1848 (puthash ?C 38 strokes-char-value-hashtable)
|
|
1849 (puthash ?D 39 strokes-char-value-hashtable)
|
|
1850 (puthash ?E 40 strokes-char-value-hashtable)
|
|
1851 (puthash ?F 41 strokes-char-value-hashtable)
|
|
1852 (puthash ?G 42 strokes-char-value-hashtable)
|
|
1853 (puthash ?H 43 strokes-char-value-hashtable)
|
|
1854 (puthash ?I 44 strokes-char-value-hashtable)
|
|
1855 (puthash ?J 45 strokes-char-value-hashtable)
|
|
1856 (puthash ?K 46 strokes-char-value-hashtable)
|
|
1857 (puthash ?L 47 strokes-char-value-hashtable)
|
|
1858 (puthash ?M 48 strokes-char-value-hashtable)
|
|
1859 (puthash ?N 49 strokes-char-value-hashtable)
|
|
1860 (puthash ?O 50 strokes-char-value-hashtable)
|
|
1861 (puthash ?P 51 strokes-char-value-hashtable)
|
|
1862 (puthash ?Q 52 strokes-char-value-hashtable)
|
|
1863 (puthash ?R 53 strokes-char-value-hashtable)
|
|
1864 (puthash ?S 54 strokes-char-value-hashtable)
|
|
1865 (puthash ?T 55 strokes-char-value-hashtable)
|
|
1866 (puthash ?U 56 strokes-char-value-hashtable)
|
|
1867 (puthash ?V 57 strokes-char-value-hashtable)
|
|
1868 (puthash ?W 58 strokes-char-value-hashtable)
|
|
1869 (puthash ?X 59 strokes-char-value-hashtable)
|
|
1870 (puthash ?Y 60 strokes-char-value-hashtable)
|
|
1871 (puthash ?Z 61 strokes-char-value-hashtable)
|
|
1872
|
|
1873 (defconst strokes-base64-chars
|
|
1874 ;; I can easily have made this a vector of single-character strings,
|
|
1875 ;; like (vector "0" "1" "2" ...), and then the program would run
|
|
1876 ;; faster since it wouldn't then have to call `char-to-string' when it
|
|
1877 ;; did the `concat'. I left them as chars here because I want
|
|
1878 ;; *them* to change `concat' so that it accepts chars and deals with
|
|
1879 ;; them properly. i.e. the form: (concat "abc" ?T "xyz") should
|
|
1880 ;; return "abcTxyz" NOT "abc84xyz" (XEmacs 19.*) and NOT an error
|
|
1881 ;; (XEmacs 20.*).
|
|
1882 ;; (vector "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
|
|
1883 ;; "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o"
|
|
1884 ;; "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D"
|
|
1885 ;; "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
|
|
1886 ;; "T" "U" "V" "W" "X" "Y" "Z")
|
|
1887 (vector ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
|
|
1888 ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z
|
|
1889 ?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z)
|
|
1890 "Character vector for fast lookup of base-64 encoding of numbers in [0,61].")
|
|
1891
|
|
1892 (defsubst strokes-xpm-char-on-p (char)
|
|
1893 ;; ### CAUTION: `char-equal' may need to change to `char=' ###
|
|
1894 "Non-nil if CHAR represents an `on' bit in the xpm."
|
|
1895 (char-equal char ?*))
|
|
1896
|
|
1897 (defsubst strokes-xpm-char-bit-p (char)
|
|
1898 "Non-nil if CHAR represents an `on' or `off' bit in the xpm."
|
|
1899 ;; ### CAUTION: `char-equal' may need to change to `char=' ###
|
|
1900 (or (char-equal char ?\ )
|
|
1901 (char-equal char ?*)))
|
|
1902
|
|
1903 ;;(defsubst strokes-xor (a b) ### Should I make this an inline function? ###
|
|
1904 ;; "T iff one and only one of A and B is non-nil; otherwise, returns nil.
|
|
1905 ;;NOTE: Don't use this as a numeric xor since it treats all non-nil
|
|
1906 ;; values as t including `0' (zero)."
|
|
1907 ;; (eq (null a) (not (null b))))
|
|
1908
|
|
1909 (defsubst strokes-xpm-encode-length-as-string (length)
|
|
1910 "Given some LENGTH in [0,62) do a fast lookup of it's encoding."
|
|
1911 (char-to-string (aref strokes-base64-chars length)))
|
|
1912
|
|
1913 (defsubst strokes-xpm-decode-char (character)
|
|
1914 "Given a CHARACTER, do a fast lookup to find its corresponding integer value."
|
|
1915 ;; ### NOTE: for XEmacs-20.* this will need to be changed to deal w/
|
|
1916 ;; char-tables !!! ###
|
|
1917 (gethash character strokes-char-value-hashtable)) ; (get-char-table
|
|
1918 ; character
|
|
1919 ; strokes-value-chartable)
|
|
1920
|
|
1921 (defun strokes-xpm-to-compressed-string (&optional xpm-buffer)
|
|
1922 "Convert the xpm in XPM-BUFFER into a compressed string representing the stroke.
|
|
1923 XPM-BUFFER is an optional argument, and defaults to `*strokes-xpm*'."
|
|
1924 (save-excursion
|
|
1925 (set-buffer (setq xpm-buffer (or xpm-buffer "*strokes-xpm*")))
|
|
1926 (goto-char (point-min))
|
|
1927 (search-forward "/* pixels */") ; skip past header junk
|
|
1928 (forward-char 2)
|
|
1929 ;; a note for below:
|
|
1930 ;; the `current-char' is the char being counted -- NOT the char at (point)
|
|
1931 ;; which happens to be called `char-at-point'
|
|
1932 (let ((compressed-string "+/") ; initialize the output
|
|
1933 (count 0) ; keep a current count of
|
|
1934 ; `current-char'
|
|
1935 (last-char-was-on-p t) ; last entered stream
|
|
1936 ; represented `on' bits
|
|
1937 (current-char-is-on-p nil) ; current stream represents `on' bits
|
|
1938 (char-at-point (char-after))) ; read the first char
|
|
1939 (while (not (char-equal char-at-point ?})) ; a `}' denotes the
|
|
1940 ; end of the pixmap
|
|
1941 (cond ((zerop count) ; must restart counting
|
|
1942 ;; check to see if the `char-at-point' is an actual pixmap bit
|
|
1943 (when (strokes-xpm-char-bit-p char-at-point)
|
|
1944 (setq count 1
|
|
1945 current-char-is-on-p (strokes-xpm-char-on-p char-at-point)))
|
|
1946 (forward-char 1))
|
|
1947 ((= count 61) ; maximum single char's
|
|
1948 ; encoding length
|
|
1949 (setq compressed-string (concat compressed-string
|
|
1950 ;; add a zero-length
|
|
1951 ;; encoding when
|
|
1952 ;; necessary
|
|
1953 (when (eq last-char-was-on-p
|
|
1954 current-char-is-on-p)
|
|
1955 ;; "0"
|
|
1956 (strokes-xpm-encode-length-as-string 0))
|
|
1957 (strokes-xpm-encode-length-as-string 61))
|
|
1958 last-char-was-on-p current-char-is-on-p
|
|
1959 count 0)) ; note that we just set
|
|
1960 ; count=0 and *don't* advance
|
|
1961 ; (point)
|
|
1962 ((strokes-xpm-char-bit-p char-at-point) ; an actual xpm bit
|
|
1963 (if (eq current-char-is-on-p
|
|
1964 (strokes-xpm-char-on-p char-at-point))
|
|
1965 ;; yet another of the same bit-type, so we continue
|
|
1966 ;; counting...
|
|
1967 (progn
|
|
1968 (incf count)
|
|
1969 (forward-char 1))
|
|
1970 ;; otherwise, it's the opposite bit-type, so we do a
|
|
1971 ;; write and then restart count ### NOTE (for myself
|
|
1972 ;; to be aware of) ### I really should advance
|
|
1973 ;; (point) in this case instead of letting another
|
|
1974 ;; iteration go through and letting the case: count=0
|
|
1975 ;; take care of this stuff for me. That's why
|
|
1976 ;; there's no (forward-char 1) below.
|
|
1977 (setq compressed-string (concat compressed-string
|
|
1978 ;; add a zero-length
|
|
1979 ;; encoding when
|
|
1980 ;; necessary
|
|
1981 (when (eq last-char-was-on-p
|
|
1982 current-char-is-on-p)
|
|
1983 ;; "0"
|
|
1984 (strokes-xpm-encode-length-as-string 0))
|
|
1985 (strokes-xpm-encode-length-as-string count))
|
|
1986 count 0
|
|
1987 last-char-was-on-p current-char-is-on-p)))
|
|
1988 (t ; ELSE it's some other useless
|
|
1989 ; char, like `"' or `,'
|
|
1990 (forward-char 1)))
|
|
1991 (setq char-at-point (char-after)))
|
|
1992 (concat compressed-string
|
|
1993 (when (> count 0)
|
|
1994 (concat (when (eq last-char-was-on-p
|
|
1995 current-char-is-on-p)
|
|
1996 ;; "0"
|
|
1997 (strokes-xpm-encode-length-as-string 0))
|
|
1998 (strokes-xpm-encode-length-as-string count)))
|
|
1999 "/"))))
|
|
2000
|
185
|
2001 ;;;###autoload
|
|
2002 (defun strokes-decode-buffer (&optional buffer force)
|
177
|
2003 "Decode stroke strings in BUFFER and display their corresponding glyphs.
|
185
|
2004 Optional BUFFER defaults to the current buffer.
|
|
2005 Optional FORCE non-nil will ignore the buffer's read-only status."
|
177
|
2006 (interactive)
|
|
2007 ;; (interactive "*bStrokify buffer: ")
|
|
2008 (save-excursion
|
185
|
2009 (set-buffer (setq buffer (get-buffer (or buffer (current-buffer)))))
|
|
2010 (when (or (not buffer-read-only)
|
|
2011 force
|
|
2012 inhibit-read-only
|
|
2013 (y-or-n-p-maybe-dialog-box
|
|
2014 (format "Buffer %s is read-only. Strokify anyway? " buffer)))
|
|
2015 (let ((inhibit-read-only t))
|
|
2016 (message "Strokifying %s..." buffer)
|
|
2017 (goto-char (point-min))
|
|
2018 (let (ext string)
|
|
2019 ;; The comment below is what i'd have to do if I wanted to
|
|
2020 ;; deal with random newlines in the midst of the compressed
|
|
2021 ;; strings. If I do this, I'll also have to change
|
|
2022 ;; `strokes-xpm-to-compress-string' to deal with the newline,
|
|
2023 ;; and possibly other whitespace stuff. YUCK!
|
|
2024 ;; (while (re-search-forward "\\+/\\(\\w\\|\\)+/" nil t nil (get-buffer buffer))
|
|
2025 (while (re-search-forward "\\+/\\w+/" nil t nil buffer)
|
|
2026 (setq string (buffer-substring (+ 2 (match-beginning 0))
|
|
2027 (1- (match-end 0))))
|
|
2028 (strokes-xpm-for-compressed-string string " *strokes-xpm*")
|
|
2029 (replace-match " ")
|
|
2030 (setq ext (make-extent (1- (point)) (point)))
|
|
2031 (set-extent-property ext 'type 'stroke-glyph)
|
|
2032 (set-extent-property ext 'start-open t)
|
|
2033 (set-extent-property ext 'end-open t)
|
|
2034 (set-extent-property ext 'detachable t)
|
|
2035 (set-extent-property ext 'duplicable t)
|
|
2036 (set-extent-property ext 'data string)
|
|
2037 (set-extent-face ext 'default)
|
|
2038 (set-extent-end-glyph ext (make-glyph
|
|
2039 (list
|
|
2040 (vector 'xpm
|
|
2041 :data (buffer-substring
|
|
2042 (point-min " *strokes-xpm*")
|
|
2043 (point-max " *strokes-xpm*")
|
|
2044 " *strokes-xpm*"))
|
|
2045 [string :data "[Stroke]"])))))
|
|
2046 (message "Strokifying %s...done" buffer)))))
|
177
|
2047
|
185
|
2048 (defun strokes-encode-buffer (&optional buffer force)
|
177
|
2049 "Convert the glyphs in BUFFER to thier base-64 ASCII representations.
|
185
|
2050 Optional BUFFER defaults to the current buffer.
|
|
2051 Optional FORCE non-nil will ignore the buffer's read-only status."
|
177
|
2052 ;; ### NOTE !!! ### (for me)
|
|
2053 ;; For later on, you can/should make the inserted strings atomic
|
|
2054 ;; extents, so that the users have a clue that they shouldn't be
|
|
2055 ;; editing inside them. Plus, if you make them extents, you can
|
|
2056 ;; very easily just hide the glyphs, so if you unstrokify, and the
|
|
2057 ;; restrokify, then those that already are glyphed don't need to be
|
|
2058 ;; re-calculated, etc. It's just nicer that way. The only things
|
|
2059 ;; to worry about is cleanup (i.e. do the glyphs get gc'd when the
|
|
2060 ;; buffer is killed?
|
|
2061 ;; (interactive "*bUnstrokify buffer: ")
|
|
2062 (interactive)
|
|
2063 (save-excursion
|
|
2064 (set-buffer (setq buffer (or buffer (current-buffer))))
|
185
|
2065 (when (or (not buffer-read-only)
|
|
2066 force
|
|
2067 inhibit-read-only
|
|
2068 (y-or-n-p-maybe-dialog-box
|
|
2069 (format "Buffer %s is read-only. Encode anyway? " buffer)))
|
|
2070 (message "Encoding strokes in %s..." buffer)
|
|
2071 ;; (map-extents
|
|
2072 ;; (lambda (ext buf)
|
|
2073 ;; (when (eq (extent-property ext 'type) 'stroke-glyph)
|
|
2074 ;; (goto-char (extent-start-position ext))
|
|
2075 ;; (delete-char 1) ; ### What the hell do I do here? ###
|
|
2076 ;; (insert "+/" (extent-property ext 'data) "/")
|
|
2077 ;; (delete-extent ext))))))
|
|
2078 (let ((inhibit-read-only t)
|
|
2079 (start nil))
|
|
2080 (loop repeat 2 do ; ### KLUDGE!!! This it pure crap! ###
|
|
2081 (map-extents
|
|
2082 (lambda (ext buf)
|
|
2083 (when (eq (extent-property ext 'type) 'stroke-glyph)
|
|
2084 (setq start (goto-char (extent-start-position ext)))
|
|
2085 ;; (insert "+/" (extent-property ext 'data) "/")
|
|
2086 (insert-string "+/")
|
|
2087 (insert-string (extent-property ext 'data))
|
|
2088 (insert-string "/")
|
|
2089 (delete-char 1)
|
|
2090 (set-extent-endpoints ext start (point))
|
|
2091 (set-extent-property ext 'type 'stroke-string)
|
|
2092 (set-extent-property ext 'atomic t)
|
|
2093 ;; (set-extent-property ext 'read-only t)
|
|
2094 (set-extent-face ext 'strokes-char-face)
|
|
2095 (set-extent-property ext 'stroke-glyph (extent-end-glyph ext))
|
|
2096 (set-extent-end-glyph ext nil))))))
|
|
2097 (message "Encoding strokes in %s...done" buffer))))
|
177
|
2098
|
|
2099 (defun strokes-xpm-for-compressed-string (compressed-string &optional bufname)
|
|
2100 "Convert the stroke represented by COMPRESSED-STRING into an xpm.
|
|
2101 Store xpm in buffer BUFNAME if supplied \(default is `*strokes-xpm*'\)"
|
|
2102 (save-excursion
|
|
2103 (or bufname (setq bufname "*strokes-xpm*"))
|
|
2104 (erase-buffer (set-buffer (get-buffer-create bufname)))
|
|
2105 (insert compressed-string)
|
|
2106 (goto-char (point-min))
|
|
2107 (let ((current-char-is-on-p nil))
|
|
2108 (while (not (eobp))
|
|
2109 (insert-char
|
|
2110 (if current-char-is-on-p
|
|
2111 ?*
|
|
2112 ?\ )
|
|
2113 (strokes-xpm-decode-char (char-after)))
|
|
2114 (delete-char 1)
|
|
2115 (setq current-char-is-on-p (not current-char-is-on-p)))
|
|
2116 (goto-char (point-min))
|
|
2117 (loop repeat 33 do
|
|
2118 (insert-char ?\")
|
|
2119 (forward-char 33)
|
|
2120 (insert "\",\n"))
|
|
2121 (goto-char (point-min))
|
|
2122 (insert strokes-xpm-header))))
|
|
2123
|
185
|
2124 ;;;###autoload
|
177
|
2125 (defun strokes-compose-complex-stroke ()
|
185
|
2126 ;; ### NOTE !!! ###
|
|
2127 ;; Even though we have lexical scoping, it's somewhat ugly how I
|
|
2128 ;; pass around variables in the global name space. I can/should
|
|
2129 ;; change this.
|
|
2130 "Read a complex stroke and insert its glyph into the current buffer."
|
177
|
2131 (interactive "*")
|
|
2132 (let ((strokes-grid-resolution 33))
|
|
2133 (strokes-read-complex-stroke)
|
185
|
2134 (strokes-xpm-for-stroke nil " *strokes-xpm*" t)
|
|
2135 (insert (strokes-xpm-to-compressed-string " *strokes-xpm*"))
|
|
2136 (strokes-decode-buffer)))
|
177
|
2137
|
153
|
2138 (provide 'strokes)
|
|
2139 (run-hooks 'strokes-load-hook)
|
|
2140
|
177
|
2141 ;;; strokes.el ends here |