strokes.el 65 KB

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