strokes.el 67 KB

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