strokes.el 67.1 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; strokes.el --- control Emacs through mouse strokes
Richard M. Stallman's avatar
Richard M. Stallman committed
2

3
;; Copyright (C) 1997, 2000-2014 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4

5
;; Author: David Bakhash <cadet@alum.mit.edu>
6
;; Maintainer: emacs-devel@gnu.org
Richard M. Stallman's avatar
Richard M. Stallman committed
7 8 9 10
;; Keywords: lisp, mouse, extensions

;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
15 16 17 18 19 20 21

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
23 24 25 26 27 28 29 30 31 32

;;; Commentary:

;; This is the strokes package.  It is intended to allow the user to
;; control Emacs by means of mouse strokes.  Once strokes is loaded, you
;; can always get help be invoking `strokes-help':

;; > M-x strokes-help

;; and you can learn how to use the package.  A mouse stroke, for now,
Dave Love's avatar
Dave Love committed
33 34 35 36 37 38 39 40
;; can be defined as holding the shift key and the middle button, for
;; instance, and then moving the mouse in whatever pattern you wish,
;; which you have set Emacs to understand as mapping to a given
;; command.  For example, you may wish the have a mouse stroke that
;; looks like a capital `C' which means `copy-region-as-kill'.  Treat
;; strokes just like you do key bindings.  For example, Emacs sets key
;; bindings globally with the `global-set-key' command.  Likewise, you
;; can do
Richard M. Stallman's avatar
Richard M. Stallman committed
41

Dave Love's avatar
Dave Love committed
42
;; > M-x strokes-global-set-stroke
Richard M. Stallman's avatar
Richard M. Stallman committed
43 44 45

;; to interactively program in a stroke.  It would be wise to set the
;; first one to this very command, so that from then on, you invoke
Dave Love's avatar
Dave Love committed
46 47 48
;; `strokes-global-set-stroke' with a stroke.  Likewise, there may
;; eventually be a `strokes-local-set-stroke' command, also analogous
;; to `local-set-key'.
Richard M. Stallman's avatar
Richard M. Stallman committed
49 50 51 52 53 54 55 56 57 58 59 60

;; You can always unset the last stroke definition with the command

;; > M-x strokes-unset-last-stroke

;; and the last stroke that was added to `strokes-global-map' will be
;; removed.

;; Other analogies between strokes and key bindings are as follows:

;;    1) To describe a stroke binding, you can type

Dave Love's avatar
Dave Love committed
61
;;       > M-x strokes-describe-stroke
Richard M. Stallman's avatar
Richard M. Stallman committed
62 63 64 65 66

;;       analogous to `describe-key'.  It's also wise to have a stroke,
;;       like an `h', for help, or a `?', mapped to `describe-stroke'.

;;    2) stroke bindings are set internally through the Lisp function
Dave Love's avatar
Dave Love committed
67 68
;;       `strokes-define-stroke', similar to the `define-key' function.
;;       some examples for a 3x3 stroke grid would be
Richard M. Stallman's avatar
Richard M. Stallman committed
69

Dave Love's avatar
Dave Love committed
70
;;       (strokes-define-stroke c-mode-stroke-map
Richard M. Stallman's avatar
Richard M. Stallman committed
71 72
;;                      '((0 . 0) (1 . 1) (2 . 2))
;;                      'kill-region)
Dave Love's avatar
Dave Love committed
73
;;       (strokes-define-stroke strokes-global-map
Richard M. Stallman's avatar
Richard M. Stallman committed
74 75 76 77 78
;;                      '((0 . 0) (0 . 1) (0 . 2) (1 . 2) (2 . 2))
;;                      'list-buffers)

;;       however, if you would probably just have the user enter in the
;;       stroke interactively and then set the stroke to whatever he/she
79
;;       entered.  The Lisp function to interactively read a stroke is
Richard M. Stallman's avatar
Richard M. Stallman committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
;;       `strokes-read-stroke'.  This is especially helpful when you're
;;       on a fast computer that can handle a 9x9 stroke grid.

;;       NOTE: only global stroke bindings are currently implemented,
;;       however mode- and buffer-local stroke bindings may eventually
;;       be implemented in a future version.

;; The important variables to be aware of for this package are listed
;; below.  They can all be altered through the customizing package via

;; > M-x customize

;; and customizing the group named `strokes'.  You can also read
;; documentation on the variables there.

;; `strokes-minimum-match-score' (determines the threshold of error that
96
;; makes a stroke acceptable or unacceptable.  If your strokes aren't
Richard M. Stallman's avatar
Richard M. Stallman committed
97 98 99 100 101
;; matching, then you should raise this variable.

;; `strokes-grid-resolution' (determines the grid dimensions that you use
;; when defining/reading strokes.  The finer the grid your computer can
;; handle, the more you can do, but even a 3x3 grid is pretty cool.)
102
;; The default value (9) should be fine for most decent computers.
Richard M. Stallman's avatar
Richard M. Stallman committed
103 104 105 106 107 108 109
;; NOTE: This variable should not be set to a number less than 3.

;; `strokes-display-strokes-buffer' will allow you to hide the strokes
;; buffer when doing simple strokes.  This is a speedup for slow
;; computers as well as people who don't want to see their strokes.

;; If you find that your mouse is accelerating too fast, you can
Dave Love's avatar
Dave Love committed
110
;; execute an X command to slow it down.  A good possibility is
Richard M. Stallman's avatar
Richard M. Stallman committed
111 112 113 114 115 116 117 118

;; % xset m 5/4 8

;; which seems, heuristically, to work okay, without much disruption.

;; Whenever you load in the strokes package, you will be able to save
;; what you've done upon exiting Emacs.  You can also do

Dave Love's avatar
Dave Love committed
119
;; > M-x strokes-prompt-user-save-strokes
Richard M. Stallman's avatar
Richard M. Stallman committed
120

121
;; and it will save your strokes in your `strokes-file'.
Richard M. Stallman's avatar
Richard M. Stallman committed
122 123 124 125

;; Note that internally, all of the routines that are part of this
;; package are able to deal with complex strokes, as they are a superset
;; of simple strokes.  However, the default of this package will map
Dave Love's avatar
Dave Love committed
126 127 128
;; S-mouse-2 to the command `strokes-do-stroke', and M-mouse-2 to
;; `strokes-do-complex-stroke'.  Complex strokes are terminated
;; with mouse button 3.
Richard M. Stallman's avatar
Richard M. Stallman committed
129

Dave Love's avatar
Dave Love committed
130
;; You can also toggle between strokes mode by simple typing
Richard M. Stallman's avatar
Richard M. Stallman committed
131 132 133

;; > M-x strokes-mode

Dave Love's avatar
Dave Love committed
134 135 136 137 138 139
;; I hope that, with the help of others, this package will be useful
;; in entering in pictographic-like language text using the mouse
;; (i.e. Korean).  Japanese and Chinese are a bit trickier, but I'm
;; sure that with help it can be done.  The next version will allow
;; the user to enter strokes which "remove the pencil from the paper"
;; so to speak, so one character can have multiple strokes.
Richard M. Stallman's avatar
Richard M. Stallman committed
140

141 142
;; NOTE (Oct 7, 2006): The URLs below seem to be invalid!!!

Richard M. Stallman's avatar
Richard M. Stallman committed
143 144 145 146 147 148 149 150 151 152
;; You can read more about strokes at:

;; http://www.mit.edu/people/cadet/strokes-help.html

;; If you're interested in using strokes for writing English into Emacs
;; using strokes, then you'll want to read about it on the web page above
;; or just download from http://www.mit.edu/people/cadet/strokes-abc.el,
;; which is nothing but a file with some helper commands for inserting
;; alphanumerics and punctuation.

153 154 155 156 157 158 159 160
;; Great thanks to Rob Ristroph for his generosity in letting me use
;; his PC to develop this, Jason Johnson for his help in algorithms,
;; Euna Kim for her help in Korean, and massive thanks to the helpful
;; guys on the help instance on athena (zeno, jered, amu, gsstark,
;; ghudson, etc) Special thanks to Steve Baur, Kyle Jones, and Hrvoje
;; Niksic for all their help.  And special thanks to Dave Gillespie
;; for all the elisp help--he is responsible for helping me use the cl
;; macros at (near) max speed.
Richard M. Stallman's avatar
Richard M. Stallman committed
161 162

;; Tasks: (what I'm getting ready for future version)...
Dave Love's avatar
Dave Love committed
163
;; 2) use 'strokes-read-complex-stroke for Korean, etc.
Richard M. Stallman's avatar
Richard M. Stallman committed
164 165 166 167 168 169
;; 4) buffer-local 'strokes-local-map, and mode-stroke-maps would be nice
;; 6) add some hooks, like `strokes-read-stroke-hook'
;; 7) See what people think of the factory settings.  Should I change
;;    them?  They're all pretty arbitrary in a way.  I guess they
;;    should be minimal, but computers are getting lots faster, and
;;    if I choose the defaults too conservatively, then strokes will
170
;;    surely disappoint some people on decent machines (until they
Richard M. Stallman's avatar
Richard M. Stallman committed
171 172 173 174
;;    figure out M-x customize).  I need feedback.
;; Other: I always have the most beta version of strokes, so if you
;;        want it just let me know.

Dave Love's avatar
Dave Love committed
175 176
;; Fixme: Use pbm instead of xpm for pixmaps to work generally.

Richard M. Stallman's avatar
Richard M. Stallman committed
177 178 179 180 181
;;; Code:

;;; Requirements and provisions...

(autoload 'mail-position-on-field "sendmail")
182
(eval-when-compile (require 'cl-lib))
Richard M. Stallman's avatar
Richard M. Stallman committed
183 184 185

;;; Constants...

186
(defconst strokes-lift :strokes-lift
Richard M. Stallman's avatar
Richard M. Stallman committed
187
  "Symbol representing a stroke lift event for complex strokes.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
Complex strokes are those which contain two or more simple strokes.")

(defconst strokes-xpm-header "/* XPM */
static char * stroke_xpm[] = {
/* width height ncolors cpp [x_hot y_hot] */
\"33 33 9 1 26 23\",
/* colors */
\" 	c none s none\",
\"*	c #000000 s foreground\",
\"R	c #FFFF00000000\",
\"O	c #FFFF80000000\",
\"Y	c #FFFFFFFF0000\",
\"G	c #0000FFFF0000\",
\"B	c #00000000FFFF\",
\"P	c #FFFF0000FFFF\",
\".	c #45458B8B0000\",
/* pixels */\n"
205
  "The header to all XPM buffers created by strokes.")
Richard M. Stallman's avatar
Richard M. Stallman committed
206 207 208 209

;;; user variables...

(defgroup strokes nil
210
  "Control Emacs through mouse strokes."
Dave Love's avatar
Dave Love committed
211
  :link '(emacs-commentary-link "strokes")
Richard M. Stallman's avatar
Richard M. Stallman committed
212 213
  :group 'mouse)

214 215 216
(define-obsolete-variable-alias 'strokes-modeline-string 'strokes-lighter
  "24.3")

217 218
(defcustom strokes-lighter " Strokes"
  "Mode line identifier for Strokes mode."
Richard M. Stallman's avatar
Richard M. Stallman committed
219 220 221 222
  :type 'string
  :group 'strokes)

(defcustom strokes-character ?@
Lute Kamstra's avatar
Lute Kamstra committed
223
  "Character used when drawing strokes in the strokes buffer.
224
\(The default is `@', which works well.)"
Richard M. Stallman's avatar
Richard M. Stallman committed
225 226 227 228
  :type 'character
  :group 'strokes)

(defcustom strokes-minimum-match-score 1000
Lute Kamstra's avatar
Lute Kamstra committed
229
  "Minimum score for a stroke to be considered a possible match.
230
Setting this variable to 0 would require a perfectly precise match.
Richard M. Stallman's avatar
Richard M. Stallman committed
231 232 233 234 235 236
The default value is 1000, but it's mostly dependent on how precisely
you manage to replicate your user-defined strokes.  It also depends on
the value of `strokes-grid-resolution', since a higher grid resolution
will correspond to more sample points, and thus more distance
measurements.  Usually, this is not a problem since you first set
`strokes-grid-resolution' based on what your computer seems to be able
Dave Love's avatar
Dave Love committed
237
to handle (though the defaults are usually more than sufficient), and
Richard M. Stallman's avatar
Richard M. Stallman committed
238 239 240 241 242 243 244 245
then you can set `strokes-minimum-match-score' to something that works
for you.  The only purpose of this variable is to insure that if you
do a bogus stroke that really doesn't match any of the predefined
ones, then strokes should NOT pick the one that came closest."
  :type 'integer
  :group 'strokes)

(defcustom strokes-grid-resolution 9
Lute Kamstra's avatar
Lute Kamstra committed
246
  "Integer defining dimensions of the stroke grid.
247
The grid is a square grid, where `strokes-grid-resolution' defaults to
Richard M. Stallman's avatar
Richard M. Stallman committed
248
`9', making a 9x9 grid whose coordinates go from (0 . 0) on the top
249
left to ((strokes-grid-resolution - 1) . (strokes-grid-resolution - 1))
Richard M. Stallman's avatar
Richard M. Stallman committed
250 251 252 253 254 255 256 257 258 259 260 261
on the bottom right.  The greater the resolution, the more intricate
your strokes can be.
NOTE: This variable should be odd and MUST NOT be less than 3 and need
      not be greater than 33, which is the resolution of the pixmaps.
WARNING: Changing the value of this variable will gravely affect the
         strokes you have already programmed in.  You should try to
         figure out what it should be based on your needs and on how
         quick the particular platform(s) you're operating on, and
         only then start programming in your custom strokes."
  :type 'integer
  :group 'strokes)

Stefan Monnier's avatar
Stefan Monnier committed
262
(defcustom strokes-file (locate-user-emacs-file "strokes" ".strokes")
263 264
  "File containing saved strokes for Strokes mode."
  :version "24.4"                       ; added locate-user-emacs-file
Richard M. Stallman's avatar
Richard M. Stallman committed
265 266 267
  :type 'file
  :group 'strokes)

Dave Love's avatar
Dave Love committed
268 269
(defvar strokes-buffer-name " *strokes*"
  "The name of the buffer that the strokes take place in.")
Richard M. Stallman's avatar
Richard M. Stallman committed
270 271

(defcustom strokes-use-strokes-buffer t
Lute Kamstra's avatar
Lute Kamstra committed
272
  "If non-nil, the strokes buffer is used and strokes are displayed.
Richard M. Stallman's avatar
Richard M. Stallman committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286
If nil, strokes will be read the same, however the user will not be
able to see the strokes.  This be helpful for people who don't like
the delay in switching to the strokes buffer."
  :type 'boolean
  :group 'strokes)

;;; internal variables...

(defvar strokes-window-configuration nil
  "The special window configuration used when entering strokes.
This is set properly in the function `strokes-update-window-configuration'.")

(defvar strokes-last-stroke nil
  "Last stroke entered by the user.
287 288
Its value gets set every time the function `strokes-fill-stroke'
gets called, since that is the best time to set the variable.")
Richard M. Stallman's avatar
Richard M. Stallman committed
289 290 291 292 293

(defvar strokes-global-map '()
  "Association list of strokes and their definitions.
Each entry is (STROKE . COMMAND) where STROKE is itself a list of
coordinates (X . Y) where X and Y are lists of positions on the
294 295
normalized stroke grid, with the top left at (0 . 0).  COMMAND is
the corresponding interactive function.")
Richard M. Stallman's avatar
Richard M. Stallman committed
296 297

(defvar strokes-load-hook nil
298
  "Functions to be called when Strokes is loaded.")
Richard M. Stallman's avatar
Richard M. Stallman committed
299

300 301 302 303 304 305 306 307 308 309 310 311
;;; ### NOT IMPLEMENTED YET ###
;;(defvar edit-strokes-menu
;;  '("Edit-Strokes"
;;    ["Add stroke..." strokes-global-set-stroke t]
;;    ["Delete stroke..." strokes-edit-delete-stroke t]
;;    ["Change stroke"	strokes-smaller	t]
;;    ["Change definition"	strokes-larger	t]
;;    ["[Re]List Strokes chronologically"	strokes-list-strokes	t]
;;    ["[Re]List Strokes alphabetically"	strokes-list-strokes	t]
;;    ["Quit"		strokes-edit-quit		t]
;;    ))

Richard M. Stallman's avatar
Richard M. Stallman committed
312 313
;;; Macros...

Dave Love's avatar
Dave Love committed
314 315 316 317 318
;; unused
;; (defmacro strokes-while-inhibiting-garbage-collector (&rest forms)
;;   "Execute FORMS without interference from the garbage collector."
;;   `(let ((gc-cons-threshold 134217727))
;;      ,@forms))
319

Richard M. Stallman's avatar
Richard M. Stallman committed
320 321
(defsubst strokes-click-p (stroke)
  "Non-nil if STROKE is really click."
Richard M. Stallman's avatar
Richard M. Stallman committed
322
  (< (length stroke) 2))
Richard M. Stallman's avatar
Richard M. Stallman committed
323 324 325 326

;;; old, but worked pretty good (just in case)...
;;(defmacro strokes-define-stroke (stroke-map stroke def)
;;  "Add STROKE to STROKE-MAP alist with given command DEF"
Richard M. Stallman's avatar
Richard M. Stallman committed
327
;;  (list 'if (list '< (list 'length stroke) 2)
Richard M. Stallman's avatar
Richard M. Stallman committed
328 329 330 331 332 333
;;	(list 'error
;;	      "That's a click, not a stroke.  See `strokes-click-command'")
;;	(list 'setq stroke-map (list 'cons (list 'cons stroke def)
;;				     (list 'remassoc stroke stroke-map)))))

(defsubst strokes-remassoc (key list)
334 335 336 337
  (let (elt)
    (while (setq elt (assoc key list))
      (setq list (delete elt list))))
  list)
Richard M. Stallman's avatar
Richard M. Stallman committed
338 339 340 341

(defmacro strokes-define-stroke (stroke-map stroke def)
  "Add STROKE to STROKE-MAP alist with given command DEF."
  `(if (strokes-click-p ,stroke)
Dave Love's avatar
Dave Love committed
342
       (error "That's a click, not a stroke")
Richard M. Stallman's avatar
Richard M. Stallman committed
343 344 345 346
     (setq ,stroke-map (cons (cons ,stroke ,def)
			     (strokes-remassoc ,stroke ,stroke-map)))))

(defsubst strokes-square (x)
Dave Love's avatar
Dave Love committed
347
  "Return the square of the number X."
Richard M. Stallman's avatar
Richard M. Stallman committed
348 349 350
  (* x x))

(defsubst strokes-distance-squared (p1 p2)
351
  "Compute the distance (squared) between to points P1 and P2.
Richard M. Stallman's avatar
Richard M. Stallman committed
352 353 354 355 356 357 358 359 360 361 362
P1 and P2 are cons cells in the form (X . Y)."
  (let ((x1 (car p1))
	(y1 (cdr p1))
	(x2 (car p2))
	(y2 (cdr p2)))
    (+ (strokes-square (- x2 x1))
       (strokes-square (- y2 y1)))))

;;; Functions...

(defsubst strokes-mouse-event-p (event)
363 364 365 366 367 368 369 370 371 372 373 374 375 376
  (and (consp event) (symbolp (car event))
       (or (eq (car event) 'mouse-movement)
	   (memq 'click (get (car event) 'event-symbol-elements))
	   (memq 'down (get (car event) 'event-symbol-elements))
	   (memq 'drag (get (car event) 'event-symbol-elements)))))

(defsubst strokes-button-press-event-p (event)
  (and (consp event) (symbolp (car event))
       (memq 'down (get (car event) 'event-symbol-elements))))

(defsubst strokes-button-release-event-p (event)
  (and (consp event) (symbolp (car event))
       (or (memq 'click (get (car event) 'event-symbol-elements))
	   (memq 'drag (get (car event) 'event-symbol-elements)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

(defun strokes-event-closest-point-1 (window &optional line)
  "Return position of start of line LINE in WINDOW.
If LINE is nil, return the last position visible in WINDOW."
  (let* ((total (- (window-height window)
		   (if (window-minibuffer-p window)
		       0 1)))
	 (distance (or line total)))
    (save-excursion
      (goto-char (window-start window))
      (if (= (vertical-motion distance) distance)
	  (if (not line)
	      (forward-char -1)))
      (point))))

(defun strokes-event-closest-point (event &optional start-window)
  "Return the nearest position to where EVENT ended its motion.
This is computed for the window where EVENT's motion started,
Dave Love's avatar
Dave Love committed
395
or for window START-WINDOW if that is specified."
Richard M. Stallman's avatar
Richard M. Stallman committed
396 397
  (or start-window (setq start-window (posn-window (event-start event))))
  (if (eq start-window (posn-window (event-end event)))
398
      (if (eq (posn-point (event-end event)) 'vertical-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
399 400
	  (strokes-event-closest-point-1 start-window
					 (cdr (posn-col-row (event-end event))))
401
	(if (eq (posn-point (event-end event)) 'mode-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
402
	    (strokes-event-closest-point-1 start-window)
403
	  (posn-point (event-end event))))
Richard M. Stallman's avatar
Richard M. Stallman committed
404 405 406 407 408 409 410 411
    ;; EVENT ended in some other window.
    (let* ((end-w (posn-window (event-end event)))
	   (end-w-top)
	   (w-top (nth 1 (window-edges start-window))))
      (setq end-w-top
	    (if (windowp end-w)
		(nth 1 (window-edges end-w))
	      (/ (cdr (posn-x-y (event-end event)))
Richard M. Stallman's avatar
Richard M. Stallman committed
412
		 (frame-char-height end-w))))
Richard M. Stallman's avatar
Richard M. Stallman committed
413 414 415 416 417
      (if (>= end-w-top w-top)
	  (strokes-event-closest-point-1 start-window)
	(window-start start-window)))))

(defun strokes-lift-p (object)
418
  "Return non-nil if OBJECT is a stroke-lift."
Richard M. Stallman's avatar
Richard M. Stallman committed
419 420 421 422 423 424
  (eq object strokes-lift))

(defun strokes-unset-last-stroke ()
  "Undo the last stroke definition."
  (interactive)
  (let ((command (cdar strokes-global-map)))
Richard M. Stallman's avatar
Richard M. Stallman committed
425
    (if (y-or-n-p
Dave Love's avatar
Dave Love committed
426
	 (format "Really delete last stroke definition, defined to `%s'? "
Richard M. Stallman's avatar
Richard M. Stallman committed
427 428 429 430 431 432 433 434 435
		 command))
	(progn
	  (setq strokes-global-map (cdr strokes-global-map))
	  (message "That stroke has been deleted"))
      (message "Nothing done"))))

;;;###autoload
(defun strokes-global-set-stroke (stroke command)
  "Interactively give STROKE the global binding as COMMAND.
436 437 438
Works just like `global-set-key', except for strokes.  COMMAND is
a symbol naming an interactively-callable function.  STROKE is a
list of sampled positions on the stroke grid as described in the
439 440 441
documentation for the `strokes-define-stroke' function.

See also `strokes-global-set-stroke-string'."
Richard M. Stallman's avatar
Richard M. Stallman committed
442 443 444 445
  (interactive
   (list
    (and (or strokes-mode (strokes-mode t))
	 (strokes-read-complex-stroke
Dave Love's avatar
Dave Love committed
446 447
	  "Draw with mouse button 1 (or 2).  End with button 3..."))
    (read-command "Command to map stroke to: ")))
Richard M. Stallman's avatar
Richard M. Stallman committed
448 449
  (strokes-define-stroke strokes-global-map stroke command))

450 451
(defun strokes-global-set-stroke-string (stroke string)
  "Interactively give STROKE the global binding as STRING.
452
Works just like `global-set-key', except for strokes.  STRING
453 454 455 456 457 458 459 460 461 462 463 464 465
is a string to be inserted by the stroke.  STROKE is a list of
sampled positions on the stroke grid as described in the
documentation for the `strokes-define-stroke' function.

Compare `strokes-global-set-stroke'."
  (interactive
   (list
    (and (or strokes-mode (strokes-mode t))
	 (strokes-read-complex-stroke
	  "Draw with mouse button 1 (or 2).  End with button 3..."))
    (read-string "String to map stroke to: ")))
  (strokes-define-stroke strokes-global-map stroke string))

Richard M. Stallman's avatar
Richard M. Stallman committed
466 467 468 469 470 471 472 473 474 475
;;(defun global-unset-stroke (stroke); FINISH THIS DEFUN!
;;  "delete all strokes matching STROKE from `strokes-global-map',
;; letting the user input
;; the stroke with the mouse"
;;  (interactive
;;   (list
;;    (strokes-read-stroke "Enter the stroke you want to delete...")))
;;  (strokes-define-stroke 'strokes-global-map stroke command))

(defun strokes-get-grid-position (stroke-extent position &optional grid-resolution)
Dave Love's avatar
Dave Love committed
476
  "Map POSITION to a new grid position.
477
Do so based on its STROKE-EXTENT and GRID-RESOLUTION.
478
STROKE-EXTENT is a list ((XMIN . YMIN) (XMAX . YMAX)).
Richard M. Stallman's avatar
Richard M. Stallman committed
479
If POSITION is a `strokes-lift', then it is itself returned.
480
Optional GRID-RESOLUTION may be used in place of `strokes-grid-resolution'.
Dave Love's avatar
Dave Love committed
481
The grid is a square whose dimension is [0,GRID-RESOLUTION)."
Richard M. Stallman's avatar
Richard M. Stallman committed
482 483 484 485 486 487 488 489
  (cond ((consp position)		; actual pixel location
	 (let ((grid-resolution (or grid-resolution strokes-grid-resolution))
	       (x (car position))
	       (y (cdr position))
	       (xmin (caar stroke-extent))
	       (ymin (cdar stroke-extent))
	       ;; the `1+' is there to insure that the
	       ;; formula evaluates correctly at the boundaries
490 491
	       (xmax (1+ (car (cadr stroke-extent))))
	       (ymax (1+ (cdr (cadr stroke-extent)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
492 493 494 495 496 497 498 499 500 501
	   (cons (floor (* grid-resolution
			   (/ (float (- x xmin))
			      (- xmax xmin))))
		 (floor (* grid-resolution
			   (/ (float (- y ymin))
			      (- ymax ymin)))))))
	((strokes-lift-p position)	; stroke lift
	 strokes-lift)))

(defun strokes-get-stroke-extent (pixel-positions)
Dave Love's avatar
Dave Love committed
502
  "From a list of absolute PIXEL-POSITIONS, return absolute spatial extent.
Richard M. Stallman's avatar
Richard M. Stallman committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
The return value is a list ((XMIN . YMIN) (XMAX . YMAX))."
  (if pixel-positions
      (let ((xmin (caar pixel-positions))
	    (xmax (caar pixel-positions))
	    (ymin (cdar pixel-positions))
	    (ymax (cdar pixel-positions))
	    (rest (cdr pixel-positions)))
	(while rest
	  (if (consp (car rest))
	      (let ((x (caar rest))
		    (y (cdar rest)))
		(if (< x xmin)
		    (setq xmin x))
		(if (> x xmax)
		    (setq xmax x))
		(if (< y ymin)
		    (setq ymin y))
		(if (> y ymax)
		    (setq ymax y))))
	  (setq rest (cdr rest)))
	(let ((delta-x (- xmax xmin))
	      (delta-y (- ymax ymin)))
	  (if (> delta-x delta-y)
	      (setq ymin (- ymin
			    (/ (- delta-x delta-y)
			       2))
		    ymax (+ ymax
			    (/ (- delta-x delta-y)
			       2)))
	    (setq xmin (- xmin
			  (/ (- delta-y delta-x)
			     2))
		  xmax (+ xmax
			  (/ (- delta-y delta-x)
			     2))))
	  (list (cons xmin ymin)
		(cons xmax ymax))))
    nil))

(defun strokes-eliminate-consecutive-redundancies (entries)
Dave Love's avatar
Dave Love committed
543
  "Return a list with no consecutive redundant entries."
Richard M. Stallman's avatar
Richard M. Stallman committed
544
  ;; defun a grande vitesse grace a Dave G.
545 546 547 548
  (cl-loop for element on entries
           if (not (equal (car element) (cadr element)))
           collect (car element)))
;;  (cl-loop for element on entries
Richard M. Stallman's avatar
Richard M. Stallman committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
;;        nconc (if (not (equal (car el) (cadr el)))
;;                  (list (car el)))))
;; yet another (orig) way of doing it...
;;  (if entries
;;      (let* ((current (car entries))
;;	     (rest (cdr entries))
;;	     (non-redundant-list (list current))
;;	     (next nil))
;;	(while rest
;;	  (setq next (car rest))
;;	  (if (equal current next)
;;	      (setq rest (cdr rest))
;;	    (setq non-redundant-list (cons next non-redundant-list)
;;		  current next
;;		  rest (cdr rest))))
;;	(nreverse non-redundant-list))
;;    nil))

(defun strokes-renormalize-to-grid (positions &optional grid-resolution)
  "Map POSITIONS to a new grid whose dimensions are based on GRID-RESOLUTION.
POSITIONS is a list of positions and stroke-lifts.
570
Optional GRID-RESOLUTION may be used in place of `strokes-grid-resolution'.
Dave Love's avatar
Dave Love committed
571
The grid is a square whose dimension is [0,GRID-RESOLUTION)."
Richard M. Stallman's avatar
Richard M. Stallman committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
  (or grid-resolution (setq grid-resolution strokes-grid-resolution))
  (let ((stroke-extent (strokes-get-stroke-extent positions)))
    (mapcar (function
	     (lambda (pos)
	       (strokes-get-grid-position stroke-extent pos grid-resolution)))
	    positions)))

(defun strokes-fill-stroke (unfilled-stroke &optional force)
  "Fill in missing grid locations in the list of UNFILLED-STROKE.
If FORCE is non-nil, then fill the stroke even if it's `stroke-click'.
NOTE: This is where the global variable `strokes-last-stroke' is set."
  (setq strokes-last-stroke		; this is global
	(if (and (strokes-click-p unfilled-stroke)
		 (not force))
	    unfilled-stroke
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	  (cl-loop
           for grid-locs on unfilled-stroke
           nconc (let* ((current (car grid-locs))
                        (current-is-a-point-p (consp current))
                        (next (cadr grid-locs))
                        (next-is-a-point-p (consp next))
                        (both-are-points-p (and current-is-a-point-p
                                                next-is-a-point-p))
                        (x1 (and current-is-a-point-p
                                 (car current)))
                        (y1 (and current-is-a-point-p
                                 (cdr current)))
                        (x2 (and next-is-a-point-p
                                 (car next)))
                        (y2 (and next-is-a-point-p
                                 (cdr next)))
                        (delta-x (and both-are-points-p
                                      (- x2 x1)))
                        (delta-y (and both-are-points-p
                                      (- y2 y1)))
                        (slope (and both-are-points-p
                                    (if (zerop delta-x)
                                        nil ; undefined vertical slope
                                      (/ (float delta-y)
                                         delta-x)))))
                   (cond ((not both-are-points-p)
                          (list current))
                         ((null slope)  ; undefined vertical slope
                          (if (>= delta-y 0)
                              (cl-loop for y from y1 below y2
                                       collect (cons x1 y))
                            (cl-loop for y from y1 above y2
                                     collect (cons x1 y))))
                         ((zerop slope) ; (= y1 y2)
                          (if (>= delta-x 0)
                              (cl-loop for x from x1 below x2
                                       collect (cons x y1))
                            (cl-loop for x from x1 above x2
                                     collect (cons x y1))))
                         ((>= (abs delta-x) (abs delta-y))
                          (if (> delta-x 0)
                              (cl-loop for x from x1 below x2
                                       collect (cons x
                                                     (+ y1
                                                        (round (* slope
                                                                  (- x x1))))))
                            (cl-loop for x from x1 above x2
                                     collect (cons x
                                                   (+ y1
                                                      (round (* slope
                                                                (- x x1))))))))
                         (t             ; (< (abs delta-x) (abs delta-y))
                          (if (> delta-y 0)
                              ;; FIXME: Reduce redundancy between branches.
                              (cl-loop for y from y1 below y2
                                       collect (cons (+ x1
                                                        (round (/ (- y y1)
                                                                  slope)))
                                                     y))
                            (cl-loop for y from y1 above y2
                                     collect (cons (+ x1
                                                      (round (/ (- y y1)
                                                                slope)))
                                                   y))))))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
651 652

(defun strokes-rate-stroke (stroke1 stroke2)
653
  "Rate STROKE1 with STROKE2 and return a score based on a distance metric.
Richard M. Stallman's avatar
Richard M. Stallman committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
Note: the rating is an error rating, and therefore, a return of 0
represents a perfect match.  Also note that the order of stroke
arguments is order-independent for the algorithm used here."
  (if (and stroke1 stroke2)
      (let ((rest1 (cdr stroke1))
	    (rest2 (cdr stroke2))
	    (err (strokes-distance-squared (car stroke1)
					   (car stroke2))))
	(while (and rest1 rest2)
	  (while (and (consp (car rest1))
		      (consp (car rest2)))
	    (setq err (+ err
			 (strokes-distance-squared (car rest1)
						   (car rest2)))
		  stroke1 rest1
		  stroke2 rest2
		  rest1 (cdr stroke1)
		  rest2 (cdr stroke2)))
	  (cond ((and (strokes-lift-p (car rest1))
		      (strokes-lift-p (car rest2)))
		 (setq rest1 (cdr rest1)
		       rest2 (cdr rest2)))
		((strokes-lift-p (car rest2))
		 (while (consp (car rest1))
		   (setq err (+ err
				(strokes-distance-squared (car rest1)
							  (car stroke2)))
			 rest1 (cdr rest1))))
		((strokes-lift-p (car rest1))
		 (while (consp (car rest2))
		   (setq err (+ err
				(strokes-distance-squared (car stroke1)
							  (car rest2)))
			 rest2 (cdr rest2))))))
	(if (null rest2)
	    (while (consp (car rest1))
	      (setq err (+ err
			   (strokes-distance-squared (car rest1)
						     (car stroke2)))
		    rest1 (cdr rest1))))
	(if (null rest1)
	    (while (consp (car rest2))
	      (setq err (+ err
			   (strokes-distance-squared (car stroke1)
						     (car rest2)))
		    rest2 (cdr rest2))))
	(if (or (strokes-lift-p (car rest1))
		(strokes-lift-p (car rest2)))
	    (setq err nil)
	  err))
    nil))

(defun strokes-match-stroke (stroke stroke-map)
Dave Love's avatar
Dave Love committed
707
  "Find the best matching command of STROKE in STROKE-MAP.
Richard M. Stallman's avatar
Richard M. Stallman committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
Returns the corresponding match as (COMMAND . SCORE)."
  (if (and stroke stroke-map)
      (let ((score (strokes-rate-stroke stroke (caar stroke-map)))
	    (command (cdar stroke-map))
	    (map (cdr stroke-map)))
	(while map
	  (let ((newscore (strokes-rate-stroke stroke (caar map))))
	    (if (or (and newscore score (< newscore score))
		    (and newscore (null score)))
		(setq score newscore
		      command (cdar map)))
	    (setq map (cdr map))))
	(if score
	    (cons command score)
	  nil))
    nil))

Glenn Morris's avatar
Glenn Morris committed
725 726 727
(defsubst strokes-fill-current-buffer-with-whitespace ()
  "Erase the contents of the current buffer and fill it with whitespace."
  (erase-buffer)
728 729 730
  (cl-loop repeat (frame-height) do
           (insert-char ?\s (1- (frame-width)))
           (newline))
Glenn Morris's avatar
Glenn Morris committed
731 732
  (goto-char (point-min)))

Richard M. Stallman's avatar
Richard M. Stallman committed
733 734 735 736 737 738 739
;;;###autoload
(defun strokes-read-stroke (&optional prompt event)
  "Read a simple stroke (interactively) and return the stroke.
Optional PROMPT in minibuffer displays before and during stroke reading.
This function will display the stroke interactively as it is being
entered in the strokes buffer if the variable
`strokes-use-strokes-buffer' is non-nil.
740
Optional EVENT is acceptable as the starting event of the stroke."
Richard M. Stallman's avatar
Richard M. Stallman committed
741
  (save-excursion
Richard M. Stallman's avatar
Richard M. Stallman committed
742 743 744 745 746 747 748 749
    (let ((pix-locs nil)
	  (grid-locs nil)
	  (safe-to-draw-p nil))
      (if strokes-use-strokes-buffer
	  ;; switch to the strokes buffer and
	  ;; display the stroke as it's being read
	  (save-window-excursion
	    (set-window-configuration strokes-window-configuration)
750 751 752 753 754
	    ;; The frame has been resized, so we need to refill the
	    ;; strokes buffer so that the strokes canvas is the whole
	    ;; visible buffer.
	    (unless (> 1 (abs (- (line-end-position) (window-width))))
	      (strokes-fill-current-buffer-with-whitespace))
Richard M. Stallman's avatar
Richard M. Stallman committed
755
	    (when prompt
Deepak Goel's avatar
Deepak Goel committed
756
	      (message "%s" prompt)
Richard M. Stallman's avatar
Richard M. Stallman committed
757
	      (setq event (read-event))
758
	      (or (strokes-button-press-event-p event)
Richard M. Stallman's avatar
Richard M. Stallman committed
759 760 761 762 763
		  (error "You must draw with the mouse")))
	    (unwind-protect
		(track-mouse
		  (or event (setq event (read-event)
				  safe-to-draw-p t))
764
		  (while (not (strokes-button-release-event-p event))
Richard M. Stallman's avatar
Richard M. Stallman committed
765 766
		    (if (strokes-mouse-event-p event)
			(let ((point (strokes-event-closest-point event)))
Richard M. Stallman's avatar
Richard M. Stallman committed
767 768 769 770
			  (if (and point safe-to-draw-p)
			      ;; we can draw that point
			      (progn
				(goto-char point)
Dave Love's avatar
Dave Love committed
771
				(subst-char-in-region point (1+ point)
772
						      ?\s strokes-character))
Richard M. Stallman's avatar
Richard M. Stallman committed
773 774
			    ;; otherwise, we can start drawing the next time...
			    (setq safe-to-draw-p t))
775
			  (push (cdr (mouse-pixel-position))
Richard M. Stallman's avatar
Richard M. Stallman committed
776
				pix-locs)))
Richard M. Stallman's avatar
Richard M. Stallman committed
777
		    (setq event (read-event)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
778
	    ;; protected
Richard M. Stallman's avatar
Richard M. Stallman committed
779
	    ;; clean up strokes buffer and then bury it.
Richard M. Stallman's avatar
Richard M. Stallman committed
780
	    (when (equal (buffer-name) strokes-buffer-name)
Dave Love's avatar
Dave Love committed
781
	      (subst-char-in-region (point-min) (point-max)
782
				    strokes-character ?\s)
Richard M. Stallman's avatar
Richard M. Stallman committed
783
	      (goto-char (point-min))
Richard M. Stallman's avatar
Richard M. Stallman committed
784 785 786
	      (bury-buffer))))
      ;; Otherwise, don't use strokes buffer and read stroke silently
      (when prompt
Deepak Goel's avatar
Deepak Goel committed
787
	(message "%s" prompt)
Richard M. Stallman's avatar
Richard M. Stallman committed
788
	(setq event (read-event))
789
	(or (strokes-button-press-event-p event)
Richard M. Stallman's avatar
Richard M. Stallman committed
790 791 792
	    (error "You must draw with the mouse")))
      (track-mouse
	(or event (setq event (read-event)))
793
	(while (not (strokes-button-release-event-p event))
Richard M. Stallman's avatar
Richard M. Stallman committed
794
	  (if (strokes-mouse-event-p event)
795
	      (push (cdr (mouse-pixel-position))
Richard M. Stallman's avatar
Richard M. Stallman committed
796 797 798
		    pix-locs))
	  (setq event (read-event))))
      (setq grid-locs (strokes-renormalize-to-grid (nreverse pix-locs)))
Dave Love's avatar
Dave Love committed
799 800
      (strokes-fill-stroke
       (strokes-eliminate-consecutive-redundancies grid-locs)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
801 802 803 804 805 806

;;;###autoload
(defun strokes-read-complex-stroke (&optional prompt event)
  "Read a complex stroke (interactively) and return the stroke.
Optional PROMPT in minibuffer displays before and during stroke reading.
Note that a complex stroke allows the user to pen-up and pen-down.  This
Dave Love's avatar
Dave Love committed
807 808
is implemented by allowing the user to paint with button 1 or button 2 and
then complete the stroke with button 3.
809
Optional EVENT is acceptable as the starting event of the stroke."
Richard M. Stallman's avatar
Richard M. Stallman committed
810 811 812 813 814 815
  (save-excursion
    (save-window-excursion
      (set-window-configuration strokes-window-configuration)
      (let ((pix-locs nil)
	    (grid-locs nil))
	(if prompt
816
	    (while (not (strokes-button-press-event-p event))
Deepak Goel's avatar
Deepak Goel committed
817
	      (message "%s" prompt)
Richard M. Stallman's avatar
Richard M. Stallman committed
818 819 820 821
	      (setq event (read-event))))
	(unwind-protect
	    (track-mouse
	      (or event (setq event (read-event)))
822 823 824 825 826
	      (while (not (and (strokes-button-press-event-p event)
			       (eq 'mouse-3
				   (car (get (car event)
					     'event-symbol-elements)))))
		(while (not (strokes-button-release-event-p event))
Richard M. Stallman's avatar
Richard M. Stallman committed
827 828 829 830
		  (if (strokes-mouse-event-p event)
		      (let ((point (strokes-event-closest-point event)))
			(when point
			  (goto-char point)
Dave Love's avatar
Dave Love committed
831
			  (subst-char-in-region point (1+ point)
832
						?\s strokes-character))
833
			(push (cdr (mouse-pixel-position))
Richard M. Stallman's avatar
Richard M. Stallman committed
834 835 836
			      pix-locs)))
		  (setq event (read-event)))
		(push strokes-lift pix-locs)
837
		(while (not (strokes-button-press-event-p event))
Richard M. Stallman's avatar
Richard M. Stallman committed
838 839 840 841
		  (setq event (read-event))))
	      ;; ### KLUDGE! ### sit and wait
	      ;; for some useless event to
	      ;; happen to fix the minibuffer bug.
842
	      (while (not (strokes-button-release-event-p (read-event))))
Richard M. Stallman's avatar
Richard M. Stallman committed
843 844 845 846 847 848
	      (setq pix-locs (nreverse (cdr pix-locs))
		    grid-locs (strokes-renormalize-to-grid pix-locs))
	      (strokes-fill-stroke
	       (strokes-eliminate-consecutive-redundancies grid-locs)))
	  ;; protected
	  (when (equal (buffer-name) strokes-buffer-name)
Dave Love's avatar
Dave Love committed
849
	    (subst-char-in-region (point-min) (point-max)
850
				  strokes-character ?\s)
Richard M. Stallman's avatar
Richard M. Stallman committed
851 852
	    (goto-char (point-min))
	    (bury-buffer)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
853 854 855 856 857 858

(defun strokes-execute-stroke (stroke)
  "Given STROKE, execute the command which corresponds to it.
The command will be executed provided one exists for that stroke,
based on the variable `strokes-minimum-match-score'.
If no stroke matches, nothing is done and return value is nil."
859 860 861
  ;; FIXME: Undocument return value.  It is not documented for all cases,
  ;; and doesn't allow to difference between no stroke matches and
  ;; command-execute returning nil, anyway.
Richard M. Stallman's avatar
Richard M. Stallman committed
862 863 864
  (let* ((match (strokes-match-stroke stroke strokes-global-map))
	 (command (car match))
	 (score (cdr match)))
Dave Love's avatar
Dave Love committed
865
    (cond ((and match (<= score strokes-minimum-match-score))
Richard M. Stallman's avatar
Richard M. Stallman committed
866 867 868 869
	   (message "%s" command)
	   (command-execute command))
	  ((null strokes-global-map)
	   (if (file-exists-p strokes-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
870
	       (and (y-or-n-p
Richard M. Stallman's avatar
Richard M. Stallman committed
871 872 873
		     (format "No strokes loaded.  Load `%s'? "
			     strokes-file))
		    (strokes-load-user-strokes))
Dave Love's avatar
Dave Love committed
874
	     (error "No strokes defined; use `strokes-global-set-stroke'")))
Richard M. Stallman's avatar
Richard M. Stallman committed
875 876 877 878 879 880 881
	  (t
	   (error
	    "No stroke matches; see variable `strokes-minimum-match-score'")
	   nil))))

;;;###autoload
(defun strokes-do-stroke (event)
882
  "Read a simple stroke from the user and then execute its command.
Richard M. Stallman's avatar
Richard M. Stallman committed
883 884 885 886 887 888 889
This must be bound to a mouse event."
  (interactive "e")
  (or strokes-mode (strokes-mode t))
  (strokes-execute-stroke (strokes-read-stroke nil event)))

;;;###autoload
(defun strokes-do-complex-stroke (event)
890
  "Read a complex stroke from the user and then execute its command.
Richard M. Stallman's avatar
Richard M. Stallman committed
891 892 893 894 895 896 897 898 899 900 901
This must be bound to a mouse event."
  (interactive "e")
  (or strokes-mode (strokes-mode t))
  (strokes-execute-stroke (strokes-read-complex-stroke nil event)))

;;;###autoload
(defun strokes-describe-stroke (stroke)
  "Displays the command which STROKE maps to, reading STROKE interactively."
  (interactive
   (list
    (strokes-read-complex-stroke
Dave Love's avatar
Dave Love committed
902
     "Enter stroke to describe; end with button 3...")))
Richard M. Stallman's avatar
Richard M. Stallman committed
903
  (let* ((match (strokes-match-stroke stroke strokes-global-map))
Dave Love's avatar
Dave Love committed
904
	 (command (car match))
Richard M. Stallman's avatar
Richard M. Stallman committed
905
	 (score (cdr match)))
Dave Love's avatar
Dave Love committed
906 907
    (if (and match
	     (<= score strokes-minimum-match-score))
Richard M. Stallman's avatar
Richard M. Stallman committed
908 909 910 911 912
	(message "That stroke maps to `%s'" command)
      (message "That stroke is undefined"))
    (sleep-for 1)))			; helpful for recursive edits

;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
913
(defun strokes-help ()
914
  "Get instruction on using the Strokes package."
Richard M. Stallman's avatar
Richard M. Stallman committed
915
  (interactive)
Dave Love's avatar
Dave Love committed
916 917
  (with-output-to-temp-buffer "*Help with Strokes*"
    (princ
918 919
     (substitute-command-keys
      "This is help for the strokes package.
Richard M. Stallman's avatar
Richard M. Stallman committed
920

Richard M. Stallman's avatar
Richard M. Stallman committed
921
------------------------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
922

Richard M. Stallman's avatar
Richard M. Stallman committed
923
** Strokes...
Richard M. Stallman's avatar
Richard M. Stallman committed
924

Richard M. Stallman's avatar
Richard M. Stallman committed
925 926 927 928 929
The strokes package allows you to define strokes, made with
the mouse or other pointer device, that Emacs can interpret as
corresponding to commands, and then executes the commands.  It does
character recognition, so you don't have to worry about getting it
right every time.
Richard M. Stallman's avatar
Richard M. Stallman committed
930

931
Strokes also allows you to compose documents graphically.  You can
932
fully edit documents in Chinese, Japanese, etc. based on Emacs
Dave Love's avatar
Dave Love committed
933
strokes.  Once you've done so, you can ASCII compress-and-encode them
934 935 936 937 938
and then safely save them for later use, send letters to friends
\(using Emacs, of course).  Strokes will later decode these documents,
extracting the strokes for editing use once again, so the editing
cycle can continue.

939
To toggle strokes-mode, invoke the command
Richard M. Stallman's avatar
Richard M. Stallman committed
940

Richard M. Stallman's avatar
Richard M. Stallman committed
941
> M-x strokes-mode
Richard M. Stallman's avatar
Richard M. Stallman committed
942

943
** Strokes for controlling the behavior of Emacs...
Richard M. Stallman's avatar
Richard M. Stallman committed
944

Richard M. Stallman's avatar
Richard M. Stallman committed
945
When you're ready to start defining strokes, just use the command
Richard M. Stallman's avatar
Richard M. Stallman committed
946

Dave Love's avatar
Dave Love committed
947
> M-x strokes-global-set-stroke
Richard M. Stallman's avatar
Richard M. Stallman committed
948

Richard M. Stallman's avatar
Richard M. Stallman committed
949
You will see a ` *strokes*' buffer which is waiting for you to enter in
Dave Love's avatar
Dave Love committed
950 951
your stroke.  When you enter in the stroke, you draw with button 1 or
button 2, and then end with button 3.  Next, you enter in the command
Richard M. Stallman's avatar
Richard M. Stallman committed
952 953 954
which will be executed when that stroke is invoked.  Simple as that.
For now, try to define a stroke to copy a region.  This is a popular
edit command, so type
Richard M. Stallman's avatar
Richard M. Stallman committed
955

Dave Love's avatar
Dave Love committed
956
> M-x strokes-global-set-stroke
Richard M. Stallman's avatar
Richard M. Stallman committed
957

958
Then, in the ` *strokes*' buffer, draw the letter `C' (for `copy')
Richard M. Stallman's avatar
Richard M. Stallman committed
959
and then, when it asks you to enter the command to map that to, type
Richard M. Stallman's avatar
Richard M. Stallman committed
960

Richard M. Stallman's avatar
Richard M. Stallman committed
961
> copy-region-as-kill
Richard M. Stallman's avatar
Richard M. Stallman committed
962

Richard M. Stallman's avatar
Richard M. Stallman committed
963
That's about as hard as it gets.
Dave Love's avatar
Dave Love committed
964
Remember: paint with button 1 or button 2 and then end with button 3.
Richard M. Stallman's avatar
Richard M. Stallman committed
965

Richard M. Stallman's avatar
Richard M. Stallman committed
966
If ever you want to know what a certain strokes maps to, then do
Richard M. Stallman's avatar
Richard M. Stallman committed
967

Dave Love's avatar
Dave Love committed
968
> M-x strokes-describe-stroke
Richard M. Stallman's avatar
Richard M. Stallman committed
969

Richard M. Stallman's avatar
Richard M. Stallman committed
970
and you can enter in any arbitrary stroke.  Remember: The strokes
971
package lets you program in simple and complex (multi-lift) strokes.
Richard M. Stallman's avatar
Richard M. Stallman committed
972 973
The only difference is how you *invoke* the two.  You will most likely
use simple strokes, as complex strokes were developed for
974 975
Chinese/Japanese/Korean.  So the shifted middle mouse button (S-mouse-2)
will invoke the command `strokes-do-stroke'.
Richard M. Stallman's avatar
Richard M. Stallman committed
976

Richard M. Stallman's avatar
Richard M. Stallman committed
977 978
If ever you define a stroke which you don't like, then you can unset
it with the command
Richard M. Stallman's avatar
Richard M. Stallman committed
979

Richard M. Stallman's avatar
Richard M. Stallman committed
980
> M-x strokes-unset-last-stroke
Richard M. Stallman's avatar
Richard M. Stallman committed
981

982 983 984 985 986 987 988 989 990 991 992 993 994 995
You can always get an idea of what your current strokes look like with
the command

> M-x strokes-list-strokes

Your strokes will be displayed in alphabetical order (based on command
names) and the beginning of each simple stroke will be marked by a
color dot.  Since you may have several simple strokes in a complex
stroke, the dot colors are arranged in the rainbow color sequence,
`ROYGBIV'.  If you want a listing of your strokes from most recent
down, then use a prefix argument:

> C-u M-x strokes-list-strokes

996 997
Your strokes are stored as you enter them.  They get saved into the
file specified by the `strokes-file' variable, along with other strokes
998 999
configuration variables.  You will be prompted to save them when you
exit Emacs, or you can save them with
Richard M. Stallman's avatar
Richard M. Stallman committed
1000

1001
> M-x strokes-prompt-user-save-strokes
Richard M. Stallman's avatar
Richard M. Stallman committed
1002

Richard M. Stallman's avatar
Richard M. Stallman committed
1003 1004
Your strokes get loaded automatically when you enable `strokes-mode'.
You can also load in your user-defined strokes with
Richard M. Stallman's avatar
Richard M. Stallman committed
1005

Dave Love's avatar
Dave Love committed
1006
> M-x strokes-load-user-strokes
Richard M. Stallman's avatar
Richard M. Stallman committed
1007

1008 1009 1010
** Strokes for pictographic editing...

If you'd like to create graphical files with strokes, you'll have to
Dave Love's avatar
Dave Love committed
1011 1012 1013
be running a version of Emacs with XPM support.  You use the binding
to `strokes-compose-complex-stroke' to start drawing your strokes.
These are just complex strokes, and thus continue drawing with mouse-1
1014
or mouse-2 and end with mouse-3.  Then the stroke image gets inserted
Dave Love's avatar
Dave Love committed
1015 1016 1017
into the buffer.  You treat it somewhat like any other character,
which you can copy, paste, delete, move, etc.  When all is done, you
may want to send the file, or save it.  This is done with
1018 1019 1020 1021 1022 1023 1024

> M-x strokes-encode-buffer

Likewise, to decode the strokes from a strokes-encoded buffer you do

> M-x strokes-decode-buffer

Richard M. Stallman's avatar
Richard M. Stallman committed
1025
** A few more important things...
Richard M. Stallman's avatar
Richard M. Stallman committed
1026

1027 1028 1029
o The command `strokes-do-complex-stroke' is invoked with M-mouse-2,
  so that you can execute complex strokes (i.e. with more than one lift)
  if preferred.
Richard M. Stallman's avatar
Richard M. Stallman committed
1030

Richard M. Stallman's avatar
Richard M. Stallman committed
1031 1032 1033
o Strokes are a bit computer-dependent in that they depend somewhat on
  the speed of the computer you're working on.  This means that you
  may have to tweak some variables.  You can read about them in the
Dave Love's avatar
Dave Love committed
1034
  commentary of `strokes.el'.  Better to just use \\[apropos] and read their
Richard M. Stallman's avatar
Richard M. Stallman committed
1035 1036 1037 1038
  docstrings.  All variables/functions start with `strokes'.  The one
  variable which many people wanted to see was
  `strokes-use-strokes-buffer' which allows the user to use strokes
  silently--without displaying the strokes.  All variables can be set
1039
  by customizing the group `strokes' via \\[customize-group]."))
Dave Love's avatar
Dave Love committed
1040
    (set-buffer standard-output)
1041
    (help-mode)
1042
    (help-print-return-message)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1043

Glenn Morris's avatar
Glenn Morris committed
1044
(define-obsolete-function-alias 'strokes-report-bug 'report-emacs-bug "24.1")
Richard M. Stallman's avatar
Richard M. Stallman committed
1045

1046 1047
(defun strokes-window-configuration-changed-p ()
  "Non-nil if the `strokes-window-configuration' frame properties changed.
Dave Love's avatar
Dave Love committed
1048
This is based on the last time `strokes-window-configuration' was updated."
1049 1050 1051
  (compare-window-configurations (current-window-configuration)
				 strokes-window-configuration))

Richard M. Stallman's avatar
Richard M. Stallman committed
1052
(defun strokes-update-window-configuration ()
1053
  "Ensure that `strokes-window-configuration' is up-to-date."
Richard M. Stallman's avatar
Richard M. Stallman committed
1054 1055 1056 1057 1058 1059 1060
  (interactive)
  (let ((current-window (selected-window)))
    (cond ((or (window-minibuffer-p current-window)
	       (window-dedicated-p current-window))
	   ;; don't try to update strokes window configuration
	   ;; if window is dedicated or a minibuffer
	   nil)
1061
	  ((or (called-interactively-p 'interactive)
1062
	       (not (buffer-live-p (get-buffer strokes-buffer-name)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1063 1064 1065 1066
	       (null strokes-window-configuration))
	   ;; create `strokes-window-configuration' from scratch...
	   (save-excursion
	     (save-window-excursion
1067
	       (set-buffer (get-buffer-create strokes-buffer-name))
Richard M. Stallman's avatar
Richard M. Stallman committed
1068 1069 1070 1071
	       (set-window-buffer current-window strokes-buffer-name)
	       (delete-other-windows)
	       (fundamental-mode)
	       (auto-save-mode 0)
1072
	       (font-lock-mode 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
1073 1074 1075 1076 1077 1078
	       (abbrev-mode 0)
	       (buffer-disable-undo (current-buffer))
	       (setq truncate-lines nil)
	       (strokes-fill-current-buffer-with-whitespace)
	       (setq strokes-window-configuration (current-window-configuration))
	       (bury-buffer))))
1079 1080 1081
	  ((strokes-window-configuration-changed-p) ; simple update
	   ;; update the strokes-window-configuration for this
	   ;; specific frame...
Richard M. Stallman's avatar
Richard M. Stallman committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	   (save-excursion
	     (save-window-excursion
	       (set-window-buffer current-window strokes-buffer-name)
	       (delete-other-windows)
	       (strokes-fill-current-buffer-with-whitespace)
	       (setq strokes-window-configuration (current-window-configuration))
	       (bury-buffer)))))))

;;;###autoload
(defun strokes-load-user-strokes ()
  "Load user-defined strokes from file named by `strokes-file'."
  (interactive)
  (cond ((and (file-exists-p strokes-file)
	      (file-readable-p strokes-file))
	 (load-file strokes-file))
1097
	((called-interactively-p 'interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
	 (error "Trouble loading user-defined strokes; nothing done"))
	(t
	 (message "No user-defined strokes, sorry"))))

(defun strokes-prompt-user-save-strokes ()
  "Save user-defined strokes to file named by `strokes-file'."
  (interactive)
  (save-excursion
    (let ((current strokes-global-map