Commit | Line | Data |
---|---|---|
b95b34e5 GM |
1 | ;;; artist.el --- draw ascii graphics with your mouse |
2 | ||
acaf905b | 3 | ;; Copyright (C) 2000-2012 Free Software Foundation, Inc. |
b95b34e5 GM |
4 | |
5 | ;; Author: Tomas Abrahamsson <tab@lysator.liu.se> | |
6 | ;; Maintainer: Tomas Abrahamsson <tab@lysator.liu.se> | |
7 | ;; Keywords: mouse | |
9d8b0f9c RS |
8 | ;; Version: 1.2.6 |
9 | ;; Release-date: 6-Aug-2004 | |
b95b34e5 GM |
10 | ;; Location: http://www.lysator.liu.se/~tab/artist/ |
11 | ||
241760a3 SM |
12 | ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this |
13 | ;; file on 19/3/2008, and the maintainer agreed that when a bug is filed in | |
14 | ;; the Emacs bug reporting system against this file, a copy of the bug | |
15 | ;; report be sent to the maintainer's email address. | |
16 | ||
b95b34e5 GM |
17 | ;; This file is part of GNU Emacs. |
18 | ||
1fecc8fe | 19 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
b95b34e5 | 20 | ;; it under the terms of the GNU General Public License as published by |
1fecc8fe GM |
21 | ;; the Free Software Foundation, either version 3 of the License, or |
22 | ;; (at your option) any later version. | |
b95b34e5 GM |
23 | |
24 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
25 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | ;; GNU General Public License for more details. | |
28 | ||
29 | ;; You should have received a copy of the GNU General Public License | |
1fecc8fe | 30 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
b95b34e5 GM |
31 | |
32 | ;;; Commentary: | |
33 | ||
34 | ;; What is artist? | |
35 | ;; --------------- | |
36 | ;; | |
37 | ;; Artist is an Emacs lisp package that allows you to draw lines, | |
b27a51db | 38 | ;; rectangles and ellipses by using your mouse and/or keyboard. The |
b95b34e5 GM |
39 | ;; shapes are made up with the ascii characters |, -, / and \. |
40 | ;; | |
41 | ;; Features are: | |
42 | ;; | |
43 | ;; * Intersecting: When a `|' intersects with a `-', a `+' is | |
44 | ;; drawn, like this: | \ / | |
45 | ;; --+-- X | |
46 | ;; | / \ | |
47 | ;; | |
48 | ;; * Rubber-banding: When drawing lines you can interactively see the | |
49 | ;; result while holding the mouse button down and moving the mouse. If | |
50 | ;; your machine is not fast enough (a 386 is a bit to slow, but a | |
51 | ;; pentium is well enough), you can turn this feature off. You will | |
52 | ;; then see 1's and 2's which mark the 1st and 2nd endpoint of the line | |
53 | ;; you are drawing. | |
54 | ;; | |
55 | ;; * Drawing operations: The following drawing operations are implemented: | |
56 | ;; | |
57 | ;; lines straight-lines | |
58 | ;; rectangles squares | |
59 | ;; poly-lines straight poly-lines | |
60 | ;; ellipses circles | |
61 | ;; text (see-thru) text (overwrite) | |
62 | ;; spray-can setting size for spraying | |
63 | ;; vaporize line vaporize lines | |
64 | ;; erase characters erase rectangles | |
65 | ;; | |
66 | ;; Straight lines are lines that go horizontally, vertically or | |
b27a51db | 67 | ;; diagonally. Plain lines go in any direction. The operations in |
b95b34e5 GM |
68 | ;; the right column are accessed by holding down the shift key while |
69 | ;; drawing. | |
70 | ;; | |
71 | ;; It is possible to vaporize (erase) entire lines and connected lines | |
72 | ;; (rectangles for example) as long as the lines being vaporized are | |
b27a51db | 73 | ;; straight and connected at their endpoints. Vaporizing is inspired |
b95b34e5 GM |
74 | ;; by the drawrect package by Jari Aalto <jari.aalto@poboxes.com>. |
75 | ;; | |
76 | ;; * Flood-filling: You can fill any area with a certain character by | |
77 | ;; flood-filling. | |
78 | ;; | |
79 | ;; * Cut copy and paste: You can cut, copy and paste rectangular | |
b27a51db | 80 | ;; regions. Artist also interfaces with the rect package (this can be |
b95b34e5 GM |
81 | ;; turned off if it causes you any trouble) so anything you cut in |
82 | ;; artist can be yanked with C-x r y and vice versa. | |
83 | ;; | |
84 | ;; * Drawing with keys: Everything you can do with the mouse, you can | |
85 | ;; also do without the mouse. | |
86 | ;; | |
87 | ;; * Arrows: After having drawn a (straight) line or a (straight) | |
88 | ;; poly-line, you can set arrows on the line-ends by typing < or >. | |
89 | ;; | |
90 | ;; * Aspect-ratio: You can set the variable artist-aspect-ratio to | |
b27a51db | 91 | ;; reflect the height-width ratio for the font you are using. Squares |
b95b34e5 GM |
92 | ;; and circles are then drawn square/round. Note, that once your |
93 | ;; ascii-file is shown with font with a different height-width ratio, | |
94 | ;; the squares won't be square and the circles won't be round. | |
95 | ;; | |
96 | ;; * Picture mode compatibility: Artist is picture mode compatible (this | |
97 | ;; can be turned off). | |
98 | ;; | |
99 | ;; See the documentation for the function artist-mode for a detailed | |
100 | ;; description on how to use artist. | |
101 | ;; | |
102 | ;; | |
103 | ;; What about adding my own drawing modes? | |
104 | ;; --------------------------------------- | |
105 | ;; | |
106 | ;; See the short guide at the end of this file. | |
107 | ;; If you add a new drawing mode, send it to me, and I would gladly | |
108 | ;; include in the next release! | |
109 | ||
b95b34e5 GM |
110 | ;;; Installation: |
111 | ||
112 | ;; To use artist, put this in your .emacs: | |
113 | ;; | |
114 | ;; (autoload 'artist-mode "artist" "Enter artist-mode" t) | |
115 | ||
116 | ||
117 | ;;; Requirements: | |
118 | ||
119 | ;; Artist requires Emacs 19.28 or higher. | |
120 | ;; | |
121 | ;; Artist requires the `rect' package (which comes with Emacs) to be | |
122 | ;; loadable, unless the variable `artist-interface-with-rect' is set | |
123 | ;; to nil. | |
124 | ;; | |
125 | ;; Artist also requires the Picture mode (which also comes with Emacs) | |
126 | ;; to be loadable, unless the variable `artist-picture-compatibility' | |
127 | ;; is set to nil. | |
128 | ||
129 | ;;; Known bugs: | |
130 | ||
131 | ;; The shifted operations are not available when drawing with the mouse | |
132 | ;; in Emacs 19.29 and 19.30. | |
133 | ;; | |
134 | ;; It is not possible to change between shifted and unshifted operation | |
135 | ;; while drawing with the mouse. (See the comment in the function | |
136 | ;; artist-shift-has-changed for further details.) | |
137 | ||
138 | ||
139 | ;;; ChangeLog: | |
140 | ||
9d8b0f9c RS |
141 | ;; 1.2.6 6-Aug-2004 |
142 | ;; New: Coerced with the artist.el that's in Emacs-21.3. | |
143 | ;; (minor editorial changes) | |
144 | ;; | |
145 | ;; 1.2.5 4-Aug-2004 | |
146 | ;; New: Added tool selection via the mouse-wheel | |
147 | ;; Function provided by Andreas Leue <al@sphenon.de> | |
148 | ;; | |
63db25ed GM |
149 | ;; 1.2.4 25-Oct-2001 |
150 | ;; Bugfix: Some operations (the edit menu) got hidden | |
151 | ;; Bugfix: The first arrow for poly-lines was always pointing | |
152 | ;; to the right | |
153 | ;; Changed: Updated with changes made for Emacs 21.1 | |
154 | ;; | |
155 | ;; 1.2.3 20-Nov-2000 | |
156 | ;; Bugfix: Autoload cookie corrected | |
157 | ;; | |
b95b34e5 GM |
158 | ;; 1.2.2 19-Nov-2000 |
159 | ;; Changed: More documentation fixes. | |
a9645a66 | 160 | ;; Bugfix: The arrow characters (`artist-arrows'), which |
b95b34e5 GM |
161 | ;; got wrong in 1.1, are now corrected. |
162 | ;; | |
163 | ;; 1.2.1 15-Nov-2000 | |
164 | ;; New: Documentation fixes. | |
165 | ;; Bugfix: Sets next-line-add-newlines to t while in artist-mode. | |
166 | ;; Drawing with keys was confusing without this fix, if | |
167 | ;; next-line-add-newlines was set to nil. | |
168 | ;; Thanks to Tatsuo Furukawa <tatsuo@kobe.hp.com> for this. | |
169 | ;; | |
170 | ;; 1.2 22-Oct-2000 | |
171 | ;; New: Updated to work with Emacs 21 | |
172 | ;; | |
173 | ;; 1.1 15-Aug-2000 | |
174 | ;; Bugfix: Cursor follows mouse pointer more closely. | |
175 | ;; New: Works with Emacs 20.x | |
176 | ;; New: Variables are customizable | |
177 | ;; | |
178 | ;; 1.1-beta1 21-Apr-1998 | |
179 | ;; New: Spray-can (Utterly useless, I believe, but it was fun | |
180 | ;; to implement :-) after an idea by Karl-Johan Karlsson | |
181 | ;; <kj@lysator.liu.se>. | |
182 | ;; New: Freehand drawing (with pen). | |
183 | ;; New: Vaporizing lines. | |
184 | ;; New: Text-rendering using figlet. | |
185 | ;; New: Picture mode compatibility. | |
186 | ;; Changed: All Artist keys now uses the prefix C-c C-a not to conflict | |
187 | ;; with Picture mode. | |
188 | ;; Bugfix: No longer leaves traces of lines when rubberbanding | |
189 | ;; if the buffer auto-scrolls. | |
190 | ;; Bugfix: Infinite loop sometimes when rubberbanding was turned | |
191 | ;; off. | |
192 | ;; | |
193 | ;; 1.0 01-Mar-1998 | |
194 | ;; First official release. | |
195 | ||
196 | ;;; Code: | |
197 | ||
198 | ;; Variables | |
199 | ||
9d8b0f9c | 200 | (defconst artist-version "1.2.6") |
b95b34e5 GM |
201 | (defconst artist-maintainer-address "tab@lysator.liu.se") |
202 | ||
fe6e86db | 203 | (defvar x-pointer-crosshair) |
b95b34e5 | 204 | |
b95b34e5 GM |
205 | ;; User options |
206 | ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv | |
207 | ||
208 | (defgroup artist nil | |
209 | "Customization of the Artist mode." | |
210 | :group 'mouse) | |
211 | ||
212 | (defgroup artist-text nil | |
213 | "Customization of the text rendering." | |
214 | :group 'artist) | |
215 | ||
216 | (defcustom artist-rubber-banding t | |
217 | "Interactively do rubber-banding when non-nil." | |
218 | :group 'artist | |
219 | :type 'boolean) | |
220 | ||
221 | (defcustom artist-first-char ?1 | |
222 | "Character to set at first point when not rubber-banding." | |
223 | :group 'artist | |
224 | :type 'character) | |
225 | ||
226 | (defcustom artist-second-char ?2 | |
227 | "Character to set at second point when not rubber-banding." | |
228 | :group 'artist | |
229 | :type 'character) | |
230 | ||
231 | (defcustom artist-interface-with-rect t | |
232 | "Whether to interface with the rect package or not. | |
233 | ||
234 | Interfacing to the rect package means that the Copy and Paste operations | |
235 | will use the rectangle buffer when accessing the copied area. This means | |
236 | that you can insert a rectangle which is copied using the artist package | |
237 | and vice versa. | |
238 | ||
239 | If this causes any problem for you (for example, if the implementation of | |
240 | the rectangle package changes), you can set this variable to nil, and the | |
241 | artist package will use its own copy buffer." | |
242 | :group 'artist | |
243 | :type 'boolean) | |
244 | ||
245 | (defvar artist-arrows [ ?> nil ?v ?L ?< nil ?^ nil ] | |
246 | ;; This is a defvar, not a defcustom, since the custom | |
247 | ;; package shows vectors of characters as a vector of integers, | |
248 | ;; which is confusing | |
249 | "A vector of characters to use as arrows. | |
250 | ||
251 | The vector is 8 elements long and contains a character for each | |
252 | direction, or nil if there is no suitable character to use for arrow | |
253 | in that direction. | |
254 | ||
255 | The directions are as follows: | |
256 | ||
257 | 5 6 7 | |
258 | \\ | / | |
259 | 4 - * - 0 | |
260 | / | \\ | |
261 | 3 2 1") | |
262 | ||
263 | (defcustom artist-aspect-ratio 1 | |
264 | "Defines the character height-to-width aspect ratio. | |
98d92ce3 | 265 | This is used when drawing squares and circles." |
b95b34e5 GM |
266 | :group 'artist |
267 | :type 'number) | |
268 | ||
269 | (defcustom artist-trim-line-endings t | |
270 | "Whether or not to remove white-space at end of lines. | |
271 | ||
272 | If non-nil, line-endings are trimmed (that is, extraneous white-space | |
273 | at the end of the line is removed) when the shape is drawn." | |
274 | :group 'artist | |
275 | :type 'boolean) | |
276 | ||
277 | ||
278 | (defcustom artist-flood-fill-right-border 'window-width | |
279 | "Right edge definition, used when flood-filling. | |
280 | ||
281 | When flood-filling, if the area is not closed off to the right, then | |
282 | flood-filling will fill no more to the right than specified by this | |
283 | variable. This limit is called the fill-border." | |
284 | :group 'artist | |
285 | :type '(choice (const :tag "limited to window" window-width) | |
286 | (const :tag "limited to value of `fill-column'" fill-column))) | |
287 | ||
288 | (defcustom artist-flood-fill-show-incrementally t | |
289 | "Whether or not to incrementally update display when flood-filling. | |
290 | ||
291 | If non-nil, incrementally update display when flood-filling. | |
292 | If set to non-nil, this currently implies discarding any input events | |
293 | during the flood-fill." | |
294 | :group 'artist | |
295 | :type 'boolean) | |
296 | ||
297 | ||
298 | (defcustom artist-ellipse-right-char ?\) | |
299 | "Character to use at the rightmost position when drawing narrow ellipses. | |
300 | ||
301 | In this figure, it is the right parenthesis (the ``)'' character): | |
302 | ----- | |
303 | ( ) | |
304 | -----" | |
305 | :group 'artist | |
306 | :type 'character) | |
307 | ||
308 | ||
309 | (defcustom artist-ellipse-left-char ?\( | |
310 | "Character to use at the leftmost position when drawing narrow ellipses. | |
311 | ||
312 | In this figure, it is the left parenthesis (the ``('' character): | |
313 | ----- | |
314 | ( ) | |
315 | -----" | |
316 | :group 'artist | |
317 | :type 'character) | |
318 | ||
319 | (defcustom artist-picture-compatibility t | |
320 | "Whether or not picture mode compatibility is on." | |
321 | :group 'artist | |
322 | :type 'boolean) | |
323 | ||
324 | ||
325 | ||
326 | ||
327 | (defcustom artist-vaporize-fuzziness 1 | |
328 | "How to vaporize lines that are cut off. | |
329 | ||
330 | Accept this many characters cutting off a line and still treat | |
331 | it as one line. | |
332 | Example: | |
333 | If `artist-vaporize-fuzziness' is 2, then those will be recognized as | |
334 | lines from A to B (provided you start vaporizing them at the ``*''): | |
335 | / | |
336 | A----*------/-----------B | |
337 | \\/ | |
338 | A----*----/\\------------B | |
339 | / \\ | |
340 | ||
341 | but this one won't, since it is cut off by more than 2 characters: | |
342 | \\/ / | |
343 | A----*----/\\/----------B | |
344 | / /\\ | |
98d92ce3 JB |
345 | (in fact, only the left part [between the A and the leftmost ``/'' |
346 | crossing the line] will be vaporized)." | |
b95b34e5 GM |
347 | :group 'artist |
348 | :type 'integer) | |
349 | ||
350 | ||
351 | (defvar artist-pointer-shape (if (eq window-system 'x) x-pointer-crosshair nil) | |
352 | "*If in X Windows, use this pointer shape while drawing with the mouse.") | |
353 | ||
354 | ||
f904adbb | 355 | (defcustom artist-text-renderer-function 'artist-figlet |
b95b34e5 GM |
356 | "Function for doing text rendering." |
357 | :group 'artist-text | |
358 | :type 'symbol) | |
f904adbb | 359 | (defvaralias 'artist-text-renderer 'artist-text-renderer-function) |
b95b34e5 GM |
360 | |
361 | ||
362 | (defcustom artist-figlet-program "figlet" | |
363 | "Program to run for `figlet'." | |
364 | :group 'artist-text | |
365 | :type 'string) | |
366 | ||
367 | ||
368 | (defcustom artist-figlet-default-font "standard" | |
369 | "Default font for `figlet'." | |
370 | :group 'artist-text | |
371 | :type 'string) | |
372 | ||
373 | ||
374 | (defcustom artist-figlet-list-fonts-command | |
375 | ;; list files ending with *.flf in any directory printed by the | |
376 | ;; ``figlet -I2'' command. I think this will not produce more than | |
377 | ;; one directory, but it never hurts to be on the safe side... | |
378 | "for dir in `figlet -I2`; do cd $dir; ls *.flf; done" | |
379 | "Command to run to get list of available fonts." | |
380 | :group 'artist-text | |
381 | :type 'string) | |
382 | ||
383 | ||
384 | (defcustom artist-spray-interval 0.2 | |
385 | "Number of seconds between repeated spraying." | |
386 | :group 'artist | |
387 | :type 'number) | |
388 | ||
389 | ||
390 | (defcustom artist-spray-radius 4 | |
391 | "Size of the area for spraying." | |
392 | :group 'artist | |
393 | :type 'integer) | |
394 | ||
395 | ||
fe6e86db | 396 | (defvar artist-spray-chars '(?\s ?. ?- ?+ ?m ?% ?* ?#) |
b95b34e5 GM |
397 | ;; This is a defvar, not a defcustom, since the custom |
398 | ;; package shows lists of characters as a lists of integers, | |
399 | ;; which is confusing | |
400 | "*Characters (``color'') to use when spraying. | |
98d92ce3 JB |
401 | They should be ordered from the ``lightest'' to the ``heaviest'' |
402 | since spraying replaces a light character with the next heavier one.") | |
b95b34e5 GM |
403 | |
404 | ||
405 | (defvar artist-spray-new-char ?. | |
406 | "*Initial character to use when spraying. | |
98d92ce3 JB |
407 | This character is used if spraying upon a character that is not in |
408 | `artist-spray-chars'. The character defined by this variable should | |
409 | be in `artist-spray-chars', or spraying will behave strangely.") | |
b95b34e5 GM |
410 | |
411 | ||
412 | ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
413 | ;; End of user options | |
414 | ||
415 | ||
416 | ;; Internal variables | |
417 | ;; | |
418 | (defvar artist-mode nil | |
419 | "Non-nil to enable `artist-mode' and nil to disable.") | |
420 | (make-variable-buffer-local 'artist-mode) | |
421 | ||
422 | (defvar artist-mode-name " Artist" | |
98d92ce3 | 423 | "Name of Artist mode beginning with a space (appears in the mode-line).") |
b95b34e5 | 424 | |
d77aaf6f | 425 | (defvar artist-curr-go 'pen-line |
b95b34e5 GM |
426 | "Current selected graphics operation.") |
427 | (make-variable-buffer-local 'artist-curr-go) | |
428 | ||
429 | (defvar artist-line-char-set nil | |
430 | "Boolean to tell whether user has set some char to use when drawing lines.") | |
431 | (make-variable-buffer-local 'artist-line-char-set) | |
432 | ||
433 | (defvar artist-line-char nil | |
434 | "Char to use when drawing lines.") | |
435 | (make-variable-buffer-local 'artist-line-char) | |
436 | ||
437 | (defvar artist-fill-char-set nil | |
438 | "Boolean to tell whether user has set some char to use when filling.") | |
439 | (make-variable-buffer-local 'artist-fill-char-set) | |
440 | ||
441 | (defvar artist-fill-char nil | |
442 | "Char to use when filling.") | |
443 | (make-variable-buffer-local 'artist-fill-char) | |
444 | ||
177f7c51 | 445 | (defvar artist-erase-char ?\s |
b95b34e5 GM |
446 | "Char to use when erasing.") |
447 | (make-variable-buffer-local 'artist-erase-char) | |
448 | ||
449 | (defvar artist-default-fill-char ?. | |
450 | "Char to use when a fill-char is required but none is set.") | |
451 | (make-variable-buffer-local 'artist-default-fill-char) | |
452 | ||
453 | ; This variable is not buffer local | |
454 | (defvar artist-copy-buffer nil | |
455 | "Copy buffer.") | |
456 | ||
457 | (defvar artist-draw-region-min-y 0 | |
458 | "Line-number for top-most visited line for draw operation.") | |
459 | (make-variable-buffer-local 'artist-draw-region-min-y) | |
460 | ||
461 | (defvar artist-draw-region-max-y 0 | |
462 | "Line-number for bottom-most visited line for draw operation.") | |
463 | (make-variable-buffer-local 'artist-draw-region-max-y) | |
464 | ||
465 | (defvar artist-borderless-shapes nil | |
466 | "When non-nil, draw shapes without border. | |
467 | The fill char is used instead, if it is set.") | |
468 | (make-variable-buffer-local 'artist-borderless-shapes) | |
469 | ||
9d8b0f9c RS |
470 | (defvar artist-prev-next-op-alist nil |
471 | "Assoc list for looking up next and/or previous draw operation. | |
472 | The structure is as follows: (OP . (PREV-OP . NEXT-OP)) | |
473 | where the elements are as follows: | |
474 | * OP is an atom: the KEY-SYMBOL in the `artist-mt' structure | |
475 | * PREV-OP and NEXT-OP are strings: the KEYWORD in the `artist-mt' structure | |
476 | ||
98d92ce3 | 477 | This variable is initialized by the `artist-make-prev-next-op-alist' function.") |
b95b34e5 GM |
478 | |
479 | (eval-when-compile | |
480 | ;; Make rect available at compile-time | |
481 | (require 'rect) ; for interfacing with rect | |
482 | (require 'reporter) ; the bug-reporting tool | |
483 | (require 'picture)) ; picture mode compatibility | |
484 | ||
485 | (if artist-interface-with-rect | |
486 | (require 'rect)) | |
487 | ||
488 | (require 'reporter) | |
489 | ||
490 | (if artist-picture-compatibility | |
491 | (require 'picture)) | |
492 | ||
44ee3fee RS |
493 | ;; Variables that are made local in artist-mode-init |
494 | (defvar artist-key-is-drawing nil) | |
495 | (defvar artist-key-endpoint1 nil) | |
496 | (defvar artist-key-poly-point-list nil) | |
497 | (defvar artist-key-shape nil) | |
498 | (defvar artist-key-draw-how nil) | |
499 | (defvar artist-popup-menu-table nil) | |
500 | (defvar artist-key-compl-table nil) | |
501 | (defvar artist-rb-save-data nil) | |
502 | (defvar artist-arrow-point-1 nil) | |
503 | (defvar artist-arrow-point-2 nil) | |
504 | \f | |
d77aaf6f CY |
505 | (defvar artist-menu-map |
506 | (let ((map (make-sparse-keymap))) | |
507 | (define-key map [spray-chars] | |
508 | '(menu-item "Characters for Spray" artist-select-spray-chars | |
509 | :help "Choose characters for sprayed by the spray-can")) | |
510 | (define-key map [borders] | |
511 | '(menu-item "Draw Shape Borders" artist-toggle-borderless-shapes | |
512 | :help "Toggle whether shapes are drawn with borders" | |
513 | :button (:toggle . (not artist-borderless-shapes)))) | |
514 | (define-key map [trimming] | |
515 | '(menu-item "Trim Line Endings" artist-toggle-trim-line-endings | |
516 | :help "Toggle trimming of line-endings" | |
517 | :button (:toggle . artist-trim-line-endings))) | |
518 | (define-key map [rubber-band] | |
519 | '(menu-item "Rubber-banding" artist-toggle-rubber-banding | |
520 | :help "Toggle rubber-banding" | |
521 | :button (:toggle . artist-rubber-banding))) | |
522 | (define-key map [set-erase] | |
523 | '(menu-item "Character to Erase..." artist-select-erase-char | |
524 | :help "Choose a specific character to erase")) | |
525 | (define-key map [set-line] | |
526 | '(menu-item "Character for Line..." artist-select-line-char | |
527 | :help "Choose the character to insert when drawing lines")) | |
528 | (define-key map [set-fill] | |
529 | '(menu-item "Character for Fill..." artist-select-fill-char | |
530 | :help "Choose the character to insert when filling in shapes")) | |
531 | (define-key map [artist-separator] '(menu-item "--")) | |
532 | (dolist (op '(("Vaporize" artist-select-op-vaporize-lines vaporize-lines) | |
533 | ("Erase" artist-select-op-erase-rectangle erase-rect) | |
534 | ("Spray-can" artist-select-op-spray-set-size spray-get-size) | |
535 | ("Text" artist-select-op-text-overwrite text-ovwrt) | |
536 | ("Ellipse" artist-select-op-circle circle) | |
537 | ("Poly-line" artist-select-op-straight-poly-line spolyline) | |
538 | ("Rectangle" artist-select-op-square square) | |
539 | ("Line" artist-select-op-straight-line s-line) | |
540 | ("Pen" artist-select-op-pen-line pen-line))) | |
541 | (define-key map (vector (nth 2 op)) | |
542 | `(menu-item ,(nth 0 op) | |
543 | ,(nth 1 op) | |
544 | :help ,(format "Draw using the %s style" (nth 0 op)) | |
545 | :button (:radio . (eq artist-curr-go ',(nth 2 op)))))) | |
546 | map)) | |
547 | ||
b95b34e5 GM |
548 | (defvar artist-mode-map |
549 | (let ((map (make-sparse-keymap))) | |
550 | (setq artist-mode-map (make-sparse-keymap)) | |
551 | (define-key map [down-mouse-1] 'artist-down-mouse-1) | |
552 | (define-key map [S-down-mouse-1] 'artist-down-mouse-1) | |
553 | (define-key map [down-mouse-2] 'artist-mouse-choose-operation) | |
554 | (define-key map [S-down-mouse-2] 'artist-mouse-choose-operation) | |
555 | (define-key map [down-mouse-3] 'artist-down-mouse-3) | |
556 | (define-key map [S-down-mouse-3] 'artist-down-mouse-3) | |
9d8b0f9c RS |
557 | (define-key map [C-mouse-4] 'artist-select-prev-op-in-list) |
558 | (define-key map [C-mouse-5] 'artist-select-next-op-in-list) | |
b95b34e5 GM |
559 | (define-key map "\r" 'artist-key-set-point) ; return |
560 | (define-key map [up] 'artist-previous-line) | |
561 | (define-key map "\C-p" 'artist-previous-line) | |
562 | (define-key map [down] 'artist-next-line) | |
563 | (define-key map "\C-n" 'artist-next-line) | |
564 | (define-key map [left] 'artist-backward-char) | |
565 | (define-key map "\C-b" 'artist-backward-char) | |
566 | (define-key map [right] 'artist-forward-char) | |
567 | (define-key map "\C-f" 'artist-forward-char) | |
568 | (define-key map "<" 'artist-toggle-first-arrow) | |
569 | (define-key map ">" 'artist-toggle-second-arrow) | |
570 | (define-key map "\C-c\C-a\C-e" 'artist-select-erase-char) | |
571 | (define-key map "\C-c\C-a\C-f" 'artist-select-fill-char) | |
572 | (define-key map "\C-c\C-a\C-l" 'artist-select-line-char) | |
573 | (define-key map "\C-c\C-a\C-o" 'artist-select-operation) | |
574 | (define-key map "\C-c\C-a\C-r" 'artist-toggle-rubber-banding) | |
575 | (define-key map "\C-c\C-a\C-t" 'artist-toggle-trim-line-endings) | |
576 | (define-key map "\C-c\C-a\C-s" 'artist-toggle-borderless-shapes) | |
577 | (define-key map "\C-c\C-c" 'artist-mode-off) | |
578 | (define-key map "\C-c\C-al" 'artist-select-op-line) | |
579 | (define-key map "\C-c\C-aL" 'artist-select-op-straight-line) | |
580 | (define-key map "\C-c\C-ar" 'artist-select-op-rectangle) | |
581 | (define-key map "\C-c\C-aR" 'artist-select-op-square) | |
582 | (define-key map "\C-c\C-as" 'artist-select-op-square) | |
583 | (define-key map "\C-c\C-ap" 'artist-select-op-poly-line) | |
584 | (define-key map "\C-c\C-aP" 'artist-select-op-straight-poly-line) | |
585 | (define-key map "\C-c\C-ae" 'artist-select-op-ellipse) | |
586 | (define-key map "\C-c\C-ac" 'artist-select-op-circle) | |
587 | (define-key map "\C-c\C-at" 'artist-select-op-text-see-thru) | |
588 | (define-key map "\C-c\C-aT" 'artist-select-op-text-overwrite) | |
589 | (define-key map "\C-c\C-aS" 'artist-select-op-spray-can) | |
590 | (define-key map "\C-c\C-az" 'artist-select-op-spray-set-size) | |
591 | (define-key map "\C-c\C-a\C-d" 'artist-select-op-erase-char) | |
592 | (define-key map "\C-c\C-aE" 'artist-select-op-erase-rectangle) | |
593 | (define-key map "\C-c\C-av" 'artist-select-op-vaporize-line) | |
594 | (define-key map "\C-c\C-aV" 'artist-select-op-vaporize-lines) | |
595 | (define-key map "\C-c\C-a\C-k" 'artist-select-op-cut-rectangle) | |
596 | (define-key map "\C-c\C-a\M-w" 'artist-select-op-copy-rectangle) | |
597 | (define-key map "\C-c\C-a\C-y" 'artist-select-op-paste) | |
598 | (define-key map "\C-c\C-af" 'artist-select-op-flood-fill) | |
599 | (define-key map "\C-c\C-a\C-b" 'artist-submit-bug-report) | |
d77aaf6f | 600 | (define-key map [menu-bar artist] (cons "Artist" artist-menu-map)) |
b95b34e5 GM |
601 | map) |
602 | "Keymap for `artist-minor-mode'.") | |
603 | ||
604 | (defvar artist-replacement-table (make-vector 256 0) | |
605 | "Replacement table for `artist-replace-char'.") | |
606 | ||
607 | ||
608 | ;;; | |
609 | ;;; The table of graphic operations | |
610 | ;;; | |
611 | (defvar artist-mt | |
612 | ;; Implementation note: Maybe this should be done using a structure | |
613 | ;; in the cl package? | |
614 | ;; | |
615 | '( | |
616 | (menu | |
617 | ("Drawing" | |
618 | ((function-call | |
619 | ( "Undo" do-undo undo)) | |
620 | ||
621 | (separator ) | |
622 | (graphics-operation | |
623 | ("Pen" (("Pen" pen-char "pen-c" | |
624 | artist-no-arrows nil | |
625 | nil nil nil | |
626 | artist-do-continously | |
627 | artist-pen | |
628 | (nil)) | |
629 | ("Pen Line" pen-line "pen-l" | |
630 | artist-arrows artist-pen-set-arrow-points | |
631 | artist-pen-reset-last-xy nil nil | |
632 | artist-do-continously | |
633 | artist-pen-line | |
634 | (nil))))) | |
635 | ||
636 | (graphics-operation | |
637 | ("Line" (("line" line "line" | |
638 | artist-arrows artist-set-arrow-points-for-2points | |
639 | nil nil nil | |
640 | 2 | |
641 | artist-draw-line | |
642 | (artist-undraw-line | |
643 | artist-nil nil)) | |
644 | ("straight line" s-line "sline" | |
645 | artist-arrows artist-set-arrow-points-for-2points | |
646 | nil nil nil | |
647 | 2 | |
648 | artist-draw-sline | |
649 | (artist-undraw-sline | |
650 | artist-nil nil))))) | |
651 | ||
652 | (graphics-operation | |
653 | ("Rectangle" (("rectangle" rect "rect" | |
654 | artist-no-arrows nil | |
655 | nil nil nil | |
656 | 2 | |
657 | artist-draw-rect | |
658 | (artist-undraw-rect | |
659 | artist-t-if-fill-char-set artist-fill-rect)) | |
660 | ("square" square "square" | |
661 | artist-no-arrows nil | |
662 | nil nil nil | |
663 | 2 | |
664 | artist-draw-square | |
665 | (artist-undraw-square | |
666 | artist-t-if-fill-char-set artist-fill-square))))) | |
667 | ||
668 | (graphics-operation | |
669 | ("Poly-line" (("poly-line" polyline "poly" | |
670 | artist-arrows artist-set-arrow-points-for-poly | |
671 | nil nil nil | |
672 | artist-do-poly | |
673 | artist-draw-line | |
674 | (artist-undraw-line | |
675 | artist-nil nil)) | |
676 | ("straight poly-line" spolyline "s-poly" | |
677 | artist-arrows artist-set-arrow-points-for-poly | |
678 | nil nil nil | |
679 | artist-do-poly | |
680 | artist-draw-sline | |
681 | (artist-undraw-sline | |
682 | artist-nil nil))))) | |
683 | ||
684 | (graphics-operation | |
685 | ("Ellipse" (("ellipse" ellipse "ellipse" | |
686 | artist-no-arrows nil | |
687 | nil nil nil | |
688 | 2 | |
689 | artist-draw-ellipse | |
690 | (artist-undraw-ellipse | |
691 | artist-t-if-fill-char-set artist-fill-ellipse)) | |
692 | ("circle" circle "circle" | |
693 | artist-no-arrows nil | |
694 | nil nil nil | |
695 | 2 | |
696 | artist-draw-circle | |
697 | (artist-undraw-circle | |
698 | artist-t-if-fill-char-set artist-fill-circle))))) | |
699 | ||
700 | (graphics-operation | |
701 | ("Text" (("text see-thru" text-thru "text-thru" | |
702 | artist-no-arrows nil | |
703 | nil nil nil | |
704 | 1 | |
705 | artist-text-see-thru | |
706 | nil) | |
707 | ("text overwrite" text-ovwrt "text-ovwrt" | |
708 | artist-no-arrows nil | |
709 | nil nil nil | |
710 | 1 | |
711 | artist-text-overwrite | |
712 | nil)))) | |
713 | ||
714 | (graphics-operation | |
715 | ("Spray-can" (("spray-can" spray-can "spray-can" | |
716 | artist-no-arrows nil | |
717 | nil nil nil | |
718 | artist-do-continously | |
719 | artist-spray | |
720 | (artist-spray-get-interval)) | |
721 | ("spray set size" spray-get-size "spray-size" | |
722 | artist-no-arrows nil | |
723 | nil artist-spray-clear-circle artist-spray-set-radius | |
724 | 2 | |
725 | artist-draw-circle | |
726 | (artist-undraw-circle | |
727 | artist-nil nil))))) | |
728 | ||
729 | (graphics-operation | |
730 | ("Erase" (("erase char" erase-char "erase-c" | |
731 | artist-no-arrows nil | |
732 | nil nil nil | |
733 | artist-do-continously | |
734 | artist-erase-char | |
735 | (nil)) | |
736 | ("erase rectangle" erase-rect "erase-r" | |
737 | artist-no-arrows nil | |
738 | nil nil nil | |
739 | 2 | |
740 | artist-draw-rect | |
741 | (artist-undraw-rect | |
742 | artist-t artist-erase-rect))))) | |
743 | ||
744 | (graphics-operation | |
745 | ("Vaporize" (("vaporize line" vaporize-line "vaporize-1" | |
746 | artist-no-arrows nil | |
747 | nil nil nil | |
748 | 1 | |
749 | artist-vaporize-line | |
750 | nil) | |
751 | ("vaporize lines" vaporize-lines "vaporize-n" | |
752 | artist-no-arrows nil | |
753 | nil nil nil | |
754 | 1 | |
755 | artist-vaporize-lines | |
756 | nil))))))) | |
757 | ||
758 | (menu | |
759 | ("Edit" | |
760 | ((graphics-operation | |
761 | ("Cut" (("cut rectangle" cut-r "cut-r" | |
762 | artist-no-arrows nil | |
763 | nil nil nil | |
764 | 2 | |
765 | artist-draw-rect | |
766 | (artist-undraw-rect | |
921d84aa | 767 | artist-t artist-cut-rect)) |
b95b34e5 GM |
768 | ("cut square" cut-s "cut-s" |
769 | artist-no-arrows nil | |
770 | nil nil nil | |
771 | 2 | |
772 | artist-draw-square | |
773 | (artist-undraw-square | |
921d84aa | 774 | artist-t artist-cut-square))))) |
b95b34e5 GM |
775 | |
776 | (graphics-operation | |
777 | ("Copy" (("copy rectangle" copy-r "copy-r" | |
778 | artist-no-arrows nil | |
779 | nil nil nil | |
780 | 2 | |
781 | artist-draw-rect | |
782 | (artist-undraw-rect | |
921d84aa | 783 | artist-t artist-copy-rect)) |
b95b34e5 GM |
784 | ("copy square" copy-s "copy-s" |
785 | artist-no-arrows nil | |
786 | nil nil nil | |
787 | 2 | |
788 | artist-draw-square | |
789 | (artist-undraw-square | |
921d84aa | 790 | artist-t artist-copy-square))))) |
b95b34e5 GM |
791 | |
792 | (graphics-operation | |
793 | ("Paste" (("paste" paste "paste" | |
794 | artist-no-arrows nil | |
795 | nil nil nil | |
796 | 1 | |
797 | artist-paste | |
798 | nil) | |
799 | ("paste" paste "paste" | |
800 | artist-no-arrows nil | |
801 | nil nil nil | |
802 | 1 | |
803 | artist-paste | |
804 | nil)))) | |
805 | ||
806 | (graphics-operation | |
807 | ("Flood-fill" (("flood-fill" flood-fill "flood" | |
808 | artist-no-arrows nil | |
809 | nil nil nil | |
810 | 1 | |
811 | artist-flood-fill | |
812 | nil) | |
813 | ("flood-fill" flood-fill "flood" | |
814 | artist-no-arrows nil | |
815 | nil nil nil | |
816 | 1 | |
817 | artist-flood-fill | |
63db25ed | 818 | nil))))))) |
b95b34e5 GM |
819 | |
820 | (menu | |
821 | ("Settings" | |
822 | ((function-call | |
823 | ("Set Fill" set-fill artist-select-fill-char)) | |
824 | ||
825 | (function-call | |
826 | ("Set Line" set-line artist-select-line-char)) | |
827 | ||
828 | (function-call | |
829 | ("Set Erase" set-erase artist-select-erase-char)) | |
830 | ||
831 | (function-call | |
832 | ("Rubber-banding" rubber-band artist-toggle-rubber-banding)) | |
833 | ||
834 | (function-call | |
835 | ("Trimming" trimming artist-toggle-trim-line-endings)) | |
836 | ||
837 | (function-call | |
838 | ("Borders" borders artist-toggle-borderless-shapes)) | |
839 | ||
840 | (function-call | |
841 | ("Spray-chars" spray-chars artist-select-spray-chars))))) | |
842 | ||
843 | ) ;; end of list | |
844 | ||
845 | "Master Table for `artist-mode'. | |
846 | This table is primarily a table over the different graphics operations | |
98d92ce3 | 847 | available in Artist mode, but it also holds layout information for the |
b95b34e5 GM |
848 | popup menu. |
849 | ||
850 | The master table is a list of table elements. The elements of this table | |
851 | have the layout | |
852 | ||
853 | (TAG INFO-PART) | |
854 | ||
855 | There are three kinds of TAG: | |
856 | ||
857 | `menu' -- a sub-menu | |
858 | `separator' -- produce a separator in the popup menu | |
859 | `function-call' -- call a function | |
860 | `graphics-operation' -- a graphics operation | |
861 | ||
862 | The layout of the INFO-PART for `menu' is | |
863 | ||
864 | (TITLE ((TAG-1 INFO-PART-1) (TAG-2 INFO-PART-2) ...)) | |
865 | ||
866 | TITLE is the title of the submenu; this is followed by a list of | |
867 | menu items, each on the general form (TAG INFO-PART). | |
868 | ||
869 | ||
870 | The layout of the INFO-PART for `separator' is empty and not used. | |
871 | ||
872 | ||
873 | This is the layout of the INFO-PART for `function-call': | |
874 | ||
875 | (KEYWORD SYMBOL FN) | |
876 | ||
877 | KEYWORD is a string naming the operation, and appears in the popup menu. | |
878 | SYMBOL is the symbol for the operations. | |
879 | FN is the function performing the operation. This function | |
880 | is called with no arguments. Its return value is ignored. | |
881 | ||
882 | ||
883 | The layout of the INFO-PART for `graphics-operation' is | |
884 | ||
885 | (TITLE (UNSHIFTED SHIFTED)) | |
886 | ||
110c171f | 887 | TITLE is the title that appears in the popup menu. UNSHIFTED |
b95b34e5 GM |
888 | and SHIFTED specify for unshifted and shifted operation. Both |
889 | have the form | |
890 | ||
891 | (KEYWORD KEY-SYMBOL MODE-LINE ARROW-PRED ARROW-SET-FN | |
892 | INIT-FN PREP-FILL-FN EXIT-FN DRAW-HOW DRAW-FN EXTRA-DRAW-INFO) | |
893 | ||
894 | KEYWORD is a string specifying the name of the shape to draw. | |
895 | This is used when selecting drawing operation. | |
896 | KEY-SYMBOL is the key which is used when looking up members | |
897 | through the functions `artist-go-get-MEMBER-from-symbol' | |
898 | and `artist-fc-get-MEMBER-from-symbol'. | |
899 | MODE-LINE is a string that appears in the mode-line when drawing | |
900 | the shape. | |
901 | ARROW-PRED is a function that is called to find out if the shape | |
902 | can have arrows. The function is called with no arguments and | |
903 | must return nil or t. | |
904 | ARROW-SET-FN is a function that is called to set arrow end-points. | |
0e374247 | 905 | Arguments and return values for this function are described below. |
b95b34e5 GM |
906 | INIT-FN is, if non-nil, a function that is called when the first |
907 | point of the shape is set. Arguments and return values for | |
0e374247 | 908 | this function are described below. |
b95b34e5 GM |
909 | PREP-FILL-FN is, if non-nil, a function that is called after |
910 | the last point is set, but before the filling is done. | |
0e374247 | 911 | Arguments and return values for this function are described below. |
b95b34e5 | 912 | EXIT-FN is, if non-nil, a function that is called after filling |
0e374247 | 913 | is done. Arguments and return values for this function are |
b95b34e5 GM |
914 | described below. |
915 | DRAW-HOW defines the kind of shape. The kinds of shapes are: | |
4963739e | 916 | `artist-do-continously' -- Do drawing operation continuously, |
b95b34e5 GM |
917 | as long as the mouse button is held down. |
918 | `artist-do-poly' -- Do drawing operation many times. | |
919 | 1 -- Do drawing operation only once. | |
920 | 2 -- The drawing operation requires two points. | |
921 | DRAW-FN is the function to call for drawing. Arguments and | |
0e374247 | 922 | return values for this function are described below. |
b95b34e5 GM |
923 | EXTRA-DRAW-INFO the layout of this depends on the value of DRAW-HOW: |
924 | If DRAW-HOW is `artist-do-continously': | |
925 | ||
926 | (INTERVAL-FN) | |
927 | ||
928 | INTERVAL-FN is, if non-nil, a function to call for getting | |
929 | an interval between repeated calls to the DRAW-FN. | |
930 | This function is called with no arguments and must | |
931 | return a number, the interval in seconds. | |
932 | If nil, calls to DRAW-FN are done only when the mouse | |
933 | or cursor is moved. | |
934 | ||
935 | If DRAW-HOW is either `artist-do-poly' or 2: | |
936 | ||
937 | (UNDRAW-FN FILL-PRED FILL-FN) | |
938 | ||
939 | UNDRAW-FN is a function to call for undrawing the shape. | |
0e374247 | 940 | Arguments and return values for this function are |
b95b34e5 GM |
941 | described below. |
942 | FILL-PRED is a function that is called to find out if the shape | |
943 | can have arrows. The function must take no arguments and | |
944 | return nil or t. | |
945 | FILL-FN is a function to call for filling the shape. | |
0e374247 | 946 | Arguments and return values for this function are |
b95b34e5 GM |
947 | described below. |
948 | ||
949 | If DRAW-HOW is 1: | |
950 | ||
951 | () | |
952 | ||
0e374247 | 953 | Note! All symbols and keywords (both in the `function-call' INFO-PART |
b95b34e5 GM |
954 | as well as in the `graphics-operation' INFO-PART) must be unique. |
955 | ||
956 | The following table describe function arguments and return value | |
957 | for different functions and DRAW-HOWs. | |
958 | ||
959 | If DRAW-HOW is either `artist-do-continously' or 1: | |
960 | ||
961 | INIT-FN X Y ==> ignored | |
962 | PREP-FILL-FN X Y ==> ignored | |
963 | EXIT-FN X Y ==> ignored | |
964 | ARROW-SET-FN X Y ==> ignored | |
965 | DRAW-FN X Y ==> ignored | |
966 | ||
967 | If DRAW-HOW is 2: | |
968 | ||
969 | INIT-FN X1 Y1 ==> ignored | |
970 | PREP-FILL-FN X1 Y1 X2 Y2 ==> ignored | |
971 | EXIT-FN X1 Y1 X2 Y2 ==> ignored | |
972 | ARROW-SET-FN X1 Y1 X2 Y2 ==> ignored | |
973 | DRAW-FN X1 Y1 X2 Y2 ==> (ENDPOINT-1 ENDPOINT-2 SHAPE) | |
974 | UNDRAW-FN (ENDPOINT-1 ENDPOINT-2 SHAPE) ==> ignored | |
975 | FILL-FN (ENDPOINT-1 ENDPOINT-2 SHAPE) X1 Y1 X2 Y2 ==> ignored | |
976 | ||
977 | ENDPOINT-1 and ENDPOINT-2 are endpoints which are created with | |
978 | `artist-make-endpoint' | |
98d92ce3 | 979 | SHAPE is an opaque structure, created by the DRAW-FN and intended |
b95b34e5 GM |
980 | to be used only by the UNDRAW-FN. |
981 | ||
982 | If DRAW-HOW is `artist-do-poly': | |
983 | ||
984 | INIT-FN X1 Y1 | |
985 | PREP-FILL-FN POINT-LIST | |
986 | ARROW-SET-FN POINT-LIST | |
987 | EXIT-FN POINT-LIST | |
988 | DRAW-FN X-LAST Y-LAST X-NEW Y-NEW ==> (ENDPOINT-1 ENDPOINT-2 SHAPE) | |
989 | UNDRAW-FN (ENDPOINT-1 ENDPOINT-2 SHAPE) | |
990 | FILL-FN POINT-LIST | |
991 | ||
992 | ENDPOINT-1 and ENDPOINT-2 are endpoints which are created with | |
993 | `artist-make-endpoint'. | |
98d92ce3 | 994 | SHAPE is an opaque structure, created by the DRAW-FN and intended |
b95b34e5 GM |
995 | to be used only by the UNDRAW-FN. |
996 | POINT-LIST is a list of vectors [X Y].") | |
997 | ||
998 | ||
999 | ;; | |
1000 | ;; Accessors for the master table | |
1001 | ;; | |
1002 | ||
1003 | (defun artist-mt-get-tag (element) | |
1004 | "Retrieve the tag component from the master table ELEMENT." | |
1005 | (elt element 0)) | |
1006 | ||
1007 | (defun artist-mt-get-info-part (element) | |
1008 | "Retrieve the info part component from the master table ELEMENT." | |
1009 | (elt element 1)) | |
1010 | ||
1011 | ;; For the 'graphics-operation info-parts | |
1012 | ;; | |
1013 | (defsubst artist-go-get-desc (info-part) | |
1014 | "Retrieve the description component from a graphics operation INFO-PART." | |
1015 | (elt info-part 0)) | |
1016 | ||
1017 | (defsubst artist-go-get-unshifted (info-part) | |
1018 | "Retrieve the unshifted info from a graphics operation INFO-PART." | |
1019 | (elt (elt info-part 1) 0)) | |
1020 | ||
1021 | (defsubst artist-go-get-shifted (info-part) | |
1022 | "Retrieve the shifted info from a graphics operation INFO-PART." | |
1023 | (elt (elt info-part 1) 1)) | |
1024 | ||
1025 | (defsubst artist-go-get-keyword (info-variant-part) | |
1026 | "Retrieve the keyword component from an INFO-VARIANT-PART. | |
1027 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1028 | (elt info-variant-part 0)) | |
1029 | ||
1030 | (defsubst artist-go-get-symbol (info-variant-part) | |
1031 | "Retrieve the symbol component from an INFO-VARIANT-PART. | |
1032 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1033 | (elt info-variant-part 1)) | |
1034 | ||
1035 | (defsubst artist-go-get-mode-line (info-variant-part) | |
1036 | "Retrieve the mode line component from an INFO-VARIANT-PART. | |
1037 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1038 | (elt info-variant-part 2)) | |
1039 | ||
1040 | (defsubst artist-go-get-arrow-pred (info-variant-part) | |
1041 | "Retrieve the arrow predicate component from an INFO-VARIANT-PART. | |
1042 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1043 | (elt info-variant-part 3)) | |
1044 | ||
1045 | (defsubst artist-go-get-arrow-set-fn (info-variant-part) | |
1046 | "Retrieve the arrow set component from an INFO-VARIANT-PART. | |
1047 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1048 | (elt info-variant-part 4)) | |
1049 | ||
1050 | (defsubst artist-go-get-init-fn (info-variant-part) | |
1051 | "Retrieve the init function component from an INFO-VARIANT-PART. | |
1052 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1053 | (elt info-variant-part 5)) | |
1054 | ||
1055 | (defsubst artist-go-get-prep-fill-fn (info-variant-part) | |
1056 | "Retrieve the fill preparation function component from an INFO-VARIANT-PART. | |
1057 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1058 | (elt info-variant-part 6)) | |
1059 | ||
1060 | (defsubst artist-go-get-exit-fn (info-variant-part) | |
1061 | "Retrieve the exit component from an INFO-VARIANT-PART. | |
1062 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1063 | (elt info-variant-part 7)) | |
1064 | ||
1065 | (defsubst artist-go-get-draw-how (info-variant-part) | |
1066 | "Retrieve the draw how component from an INFO-VARIANT-PART. | |
1067 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1068 | (elt info-variant-part 8)) | |
1069 | ||
1070 | (defsubst artist-go-get-draw-fn (info-variant-part) | |
1071 | "Retrieve the draw function component from an INFO-VARIANT-PART. | |
1072 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part." | |
1073 | (elt info-variant-part 9)) | |
1074 | ||
1075 | (defsubst artist-go-get-undraw-fn (info-variant-part) | |
1076 | "Retrieve the undraw function component from an INFO-VARIANT-PART. | |
1077 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part. | |
1078 | This interval function component is available only if the `draw-how' | |
1079 | component is other than `artist-do-continously' or 1." | |
1080 | (elt (elt info-variant-part 10) 0)) | |
1081 | ||
1082 | (defsubst artist-go-get-interval-fn (info-variant-part) | |
1083 | "Retrieve the interval function component from an INFO-VARIANT-PART. | |
1084 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part. | |
1085 | This interval function component is available only if the `draw-how' | |
1086 | component is `artist-do-continously'." | |
1087 | (elt (elt info-variant-part 10) 0)) | |
1088 | ||
1089 | (defsubst artist-go-get-fill-pred (info-variant-part) | |
1090 | "Retrieve the fill predicate component from an INFO-VARIANT-PART. | |
1091 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part. | |
1092 | This interval function component is available only if the `draw-how' | |
1093 | component is other than `artist-do-continously' or 1." | |
1094 | (elt (elt info-variant-part 10) 1)) | |
1095 | ||
1096 | (defsubst artist-go-get-fill-fn (info-variant-part) | |
1097 | "Retrieve the fill function component from an INFO-VARIANT-PART. | |
1098 | An INFO-VARIANT-PART is the shifted or unshifted info from a info-part. | |
1099 | This interval function component is available only if the `draw-how' | |
1100 | component is other than `artist-do-continously' or 1." | |
1101 | (elt (elt info-variant-part 10) 2)) | |
1102 | ||
1103 | ;; For the 'function-call info-parts | |
1104 | ;; | |
1105 | (defsubst artist-fc-get-keyword (info-part) | |
1106 | "Retrieve the keyword component from a graphics operation INFO-PART." | |
1107 | (elt info-part 0)) | |
1108 | ||
1109 | (defsubst artist-fc-get-symbol (info-part) | |
1110 | "Retrieve the symbol component from a graphics operation INFO-PART." | |
1111 | (elt info-part 1)) | |
1112 | ||
1113 | (defsubst artist-fc-get-fn (info-part) | |
1114 | "Retrieve the function component from a graphics operation INFO-PART." | |
1115 | (elt info-part 2)) | |
1116 | ||
1117 | ;; For the 'menu info-parts | |
1118 | ;; | |
1119 | (defsubst artist-mn-get-title (info-part) | |
1120 | "Retrieve the title component from a graphics operation INFO-PART." | |
1121 | (elt info-part 0)) | |
1122 | ||
1123 | (defsubst artist-mn-get-items (info-part) | |
1124 | "Retrieve the items component from a graphics operation INFO-PART." | |
1125 | (elt info-part 1)) | |
1126 | ||
9d8b0f9c RS |
1127 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1128 | ;; mouse wheel cyclic operation selection | |
1129 | ||
1130 | (defun artist-get-last-non-nil-op (op-list &optional last-non-nil) | |
1131 | "Find the last non-nil draw operation in OP-LIST. | |
1132 | Optional LAST-NON-NIL will be returned if OP-LIST is nil." | |
1133 | (if op-list | |
1134 | (artist-get-last-non-nil-op (cdr op-list) | |
1135 | (or (car (car op-list)) last-non-nil)) | |
1136 | last-non-nil)) | |
1137 | ||
1138 | (defun artist-get-first-non-nil-op (op-list) | |
1139 | "Find the first non-nil draw operation in OP-LIST." | |
1140 | (or (car (car op-list)) (artist-get-first-non-nil-op (cdr op-list)))) | |
1141 | ||
1142 | (defun artist-is-in-op-list-p (op op-list) | |
1143 | "Check whether OP is in OP-LIST." | |
1144 | (and op-list | |
1145 | (or (and (car (car op-list)) (string= op (car (car op-list)))) | |
1146 | (artist-is-in-op-list-p op (cdr op-list))))) | |
1147 | ||
1148 | (defun artist-make-prev-next-op-alist (op-list | |
1149 | &optional | |
1150 | last-non-nil-arg first-non-nil-arg | |
1151 | prev-entry prev-op-arg) | |
1152 | "Build an assoc-list of OP-LIST. | |
1153 | The arguments LAST-NON-NIL-ARG, FIRST-NON-NIL-ARG, PREV-ENTRY and | |
1154 | PREV-OP-ARG are used when invoked recursively during the build-up." | |
1155 | (let* ((last-non-nil (or last-non-nil-arg | |
1156 | (artist-get-last-non-nil-op | |
1157 | artist-key-compl-table))) | |
1158 | (first-non-nil (or first-non-nil-arg | |
1159 | (artist-get-first-non-nil-op | |
1160 | artist-key-compl-table))) | |
1161 | (prev-op (or prev-op-arg last-non-nil)) | |
1162 | (op (car (car op-list))) | |
1163 | (opsym (artist-mt-get-symbol-from-keyword op)) | |
1164 | (entry (cons opsym (cons prev-op nil)))) | |
1165 | (if (or (and op-list (not op)) | |
1166 | (artist-is-in-op-list-p op (cdr op-list))) | |
1167 | (artist-make-prev-next-op-alist (cdr op-list) | |
1168 | last-non-nil first-non-nil | |
1169 | prev-entry prev-op) | |
1170 | (if prev-entry (setcdr (cdr prev-entry) op)) | |
1171 | (if op-list | |
1172 | (cons entry (artist-make-prev-next-op-alist | |
1173 | (cdr op-list) | |
1174 | last-non-nil first-non-nil | |
1175 | entry op)) | |
1176 | (progn (setcdr (cdr prev-entry) first-non-nil) nil))))) | |
1177 | ||
1178 | (defun artist-select-next-op-in-list () | |
1179 | "Cyclically select next drawing mode operation." | |
1180 | (interactive) | |
1181 | (let ((next-op (cdr (cdr (assoc artist-curr-go artist-prev-next-op-alist))))) | |
1182 | (artist-select-operation next-op) | |
274f1353 | 1183 | (message "%s" next-op))) |
9d8b0f9c RS |
1184 | |
1185 | (defun artist-select-prev-op-in-list () | |
1186 | "Cyclically select previous drawing mode operation." | |
1187 | (interactive) | |
1188 | (let ((prev-op (car (cdr (assoc artist-curr-go artist-prev-next-op-alist))))) | |
1189 | (artist-select-operation prev-op) | |
274f1353 | 1190 | (message "%s" prev-op))) |
9d8b0f9c RS |
1191 | |
1192 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1193 | ||
b95b34e5 GM |
1194 | ;;; --------------------------------- |
1195 | ;;; The artist-mode | |
1196 | ;;; --------------------------------- | |
1197 | ||
fb39bbbd | 1198 | ;;;###autoload |
b95b34e5 | 1199 | (defun artist-mode (&optional state) |
98d92ce3 JB |
1200 | "Toggle Artist mode. |
1201 | With argument STATE, turn Artist mode on if STATE is positive. | |
1202 | Artist lets you draw lines, squares, rectangles and poly-lines, | |
1203 | ellipses and circles with your mouse and/or keyboard. | |
b95b34e5 | 1204 | |
98d92ce3 | 1205 | How to quit Artist mode |
b95b34e5 GM |
1206 | |
1207 | Type \\[artist-mode-off] to quit artist-mode. | |
1208 | ||
1209 | ||
1210 | How to submit a bug report | |
1211 | ||
1212 | Type \\[artist-submit-bug-report] to submit a bug report. | |
1213 | ||
1214 | ||
1215 | Drawing with the mouse: | |
1216 | ||
1217 | mouse-2 | |
1218 | shift mouse-2 Pops up a menu where you can select what to draw with | |
1219 | mouse-1, and where you can do some settings (described | |
1220 | below). | |
1221 | ||
1222 | mouse-1 | |
1223 | shift mouse-1 Draws lines, rectangles or poly-lines, erases, cuts, copies | |
1224 | or pastes: | |
1225 | ||
1226 | Operation Not shifted Shifted | |
1227 | -------------------------------------------------------------- | |
1228 | Pen fill-char at point line from last point | |
1229 | to new point | |
1230 | -------------------------------------------------------------- | |
1231 | Line Line in any direction Straight line | |
1232 | -------------------------------------------------------------- | |
1233 | Rectangle Rectangle Square | |
1234 | -------------------------------------------------------------- | |
1235 | Poly-line Poly-line in any dir Straight poly-lines | |
1236 | -------------------------------------------------------------- | |
1237 | Ellipses Ellipses Circles | |
1238 | -------------------------------------------------------------- | |
1239 | Text Text (see thru) Text (overwrite) | |
1240 | -------------------------------------------------------------- | |
1241 | Spray-can Spray-can Set size for spray | |
1242 | -------------------------------------------------------------- | |
1243 | Erase Erase character Erase rectangle | |
1244 | -------------------------------------------------------------- | |
1245 | Vaporize Erase single line Erase connected | |
1246 | lines | |
1247 | -------------------------------------------------------------- | |
1248 | Cut Cut rectangle Cut square | |
1249 | -------------------------------------------------------------- | |
1250 | Copy Copy rectangle Copy square | |
1251 | -------------------------------------------------------------- | |
1252 | Paste Paste Paste | |
1253 | -------------------------------------------------------------- | |
1254 | Flood-fill Flood-fill Flood-fill | |
1255 | -------------------------------------------------------------- | |
1256 | ||
bc984175 | 1257 | * Straight lines can only go horizontally, vertically |
b95b34e5 GM |
1258 | or diagonally. |
1259 | ||
98d92ce3 JB |
1260 | * Poly-lines are drawn while holding mouse-1 down. When you |
1261 | release the button, the point is set. If you want a segment | |
b95b34e5 | 1262 | to be straight, hold down shift before pressing the |
98d92ce3 | 1263 | mouse-1 button. Click mouse-2 or mouse-3 to stop drawing |
b95b34e5 GM |
1264 | poly-lines. |
1265 | ||
1266 | * See thru for text means that text already in the buffer | |
1267 | will be visible through blanks in the text rendered, while | |
1268 | overwrite means the opposite. | |
1269 | ||
1270 | * Vaporizing connected lines only vaporizes lines whose | |
98d92ce3 | 1271 | _endpoints_ are connected. See also the variable |
b95b34e5 GM |
1272 | `artist-vaporize-fuzziness'. |
1273 | ||
1274 | * Cut copies, then clears the rectangle/square. | |
1275 | ||
1276 | * When drawing lines or poly-lines, you can set arrows. | |
1277 | See below under ``Arrows'' for more info. | |
1278 | ||
1279 | * The mode line shows the currently selected drawing operation. | |
1280 | In addition, if it has an asterisk (*) at the end, you | |
1281 | are currently drawing something. | |
1282 | ||
1283 | * Be patient when flood-filling -- large areas take quite | |
1284 | some time to fill. | |
1285 | ||
1286 | ||
1287 | mouse-3 Erases character under pointer | |
1288 | shift mouse-3 Erases rectangle | |
1289 | ||
1290 | ||
1291 | Settings | |
1292 | ||
1293 | Set fill Sets the character used when filling rectangles/squares | |
1294 | ||
1295 | Set line Sets the character used when drawing lines | |
1296 | ||
1297 | Erase char Sets the character used when erasing | |
1298 | ||
1299 | Rubber-banding Toggles rubber-banding | |
1300 | ||
1301 | Trimming Toggles trimming of line-endings (that is: when the shape | |
1302 | is drawn, extraneous white-space at end of lines is removed) | |
1303 | ||
98d92ce3 | 1304 | Borders Toggles the drawing of line borders around filled shapes |
b95b34e5 GM |
1305 | |
1306 | ||
1307 | Drawing with keys | |
1308 | ||
1309 | \\[artist-key-set-point] Does one of the following: | |
1310 | For lines/rectangles/squares: sets the first/second endpoint | |
1311 | For poly-lines: sets a point (use C-u \\[artist-key-set-point] to set last point) | |
1312 | When erase characters: toggles erasing | |
1313 | When cutting/copying: Sets first/last endpoint of rect/square | |
1314 | When pasting: Pastes | |
1315 | ||
1316 | \\[artist-select-operation] Selects what to draw | |
1317 | ||
1318 | Move around with \\[artist-next-line], \\[artist-previous-line], \\[artist-forward-char] and \\[artist-backward-char]. | |
1319 | ||
f81b2db1 JB |
1320 | \\[artist-select-fill-char] Sets the character to use when filling |
1321 | \\[artist-select-line-char] Sets the character to use when drawing | |
1322 | \\[artist-select-erase-char] Sets the character to use when erasing | |
b95b34e5 GM |
1323 | \\[artist-toggle-rubber-banding] Toggles rubber-banding |
1324 | \\[artist-toggle-trim-line-endings] Toggles trimming of line-endings | |
1325 | \\[artist-toggle-borderless-shapes] Toggles borders on drawn shapes | |
1326 | ||
1327 | ||
1328 | Arrows | |
1329 | ||
1330 | \\[artist-toggle-first-arrow] Sets/unsets an arrow at the beginning | |
1331 | of the line/poly-line | |
1332 | ||
1333 | \\[artist-toggle-second-arrow] Sets/unsets an arrow at the end | |
1334 | of the line/poly-line | |
1335 | ||
1336 | ||
1337 | Selecting operation | |
1338 | ||
1339 | There are some keys for quickly selecting drawing operations: | |
1340 | ||
1341 | \\[artist-select-op-line] Selects drawing lines | |
1342 | \\[artist-select-op-straight-line] Selects drawing straight lines | |
1343 | \\[artist-select-op-rectangle] Selects drawing rectangles | |
1344 | \\[artist-select-op-square] Selects drawing squares | |
1345 | \\[artist-select-op-poly-line] Selects drawing poly-lines | |
1346 | \\[artist-select-op-straight-poly-line] Selects drawing straight poly-lines | |
1347 | \\[artist-select-op-ellipse] Selects drawing ellipses | |
1348 | \\[artist-select-op-circle] Selects drawing circles | |
1349 | \\[artist-select-op-text-see-thru] Selects rendering text (see thru) | |
1350 | \\[artist-select-op-text-overwrite] Selects rendering text (overwrite) | |
1351 | \\[artist-select-op-spray-can] Spray with spray-can | |
1352 | \\[artist-select-op-spray-set-size] Set size for the spray-can | |
1353 | \\[artist-select-op-erase-char] Selects erasing characters | |
1354 | \\[artist-select-op-erase-rectangle] Selects erasing rectangles | |
1355 | \\[artist-select-op-vaporize-line] Selects vaporizing single lines | |
1356 | \\[artist-select-op-vaporize-lines] Selects vaporizing connected lines | |
1357 | \\[artist-select-op-cut-rectangle] Selects cutting rectangles | |
1358 | \\[artist-select-op-copy-rectangle] Selects copying rectangles | |
1359 | \\[artist-select-op-paste] Selects pasting | |
1360 | \\[artist-select-op-flood-fill] Selects flood-filling | |
1361 | ||
1362 | ||
1363 | Variables | |
1364 | ||
b42da387 | 1365 | This is a brief overview of the different variables. For more info, |
b95b34e5 GM |
1366 | see the documentation for the variables (type \\[describe-variable] <variable> RET). |
1367 | ||
1368 | artist-rubber-banding Interactively do rubber-banding or not | |
1369 | artist-first-char What to set at first/second point... | |
1370 | artist-second-char ...when not rubber-banding | |
1371 | artist-interface-with-rect If cut/copy/paste should interface with rect | |
1372 | artist-arrows The arrows to use when drawing arrows | |
1373 | artist-aspect-ratio Character height-to-width for squares | |
1374 | artist-trim-line-endings Trimming of line endings | |
1375 | artist-flood-fill-right-border Right border when flood-filling | |
1376 | artist-flood-fill-show-incrementally Update display while filling | |
1377 | artist-pointer-shape Pointer shape to use while drawing | |
1378 | artist-ellipse-left-char Character to use for narrow ellipses | |
1379 | artist-ellipse-right-char Character to use for narrow ellipses | |
1380 | artist-borderless-shapes If shapes should have borders | |
1381 | artist-picture-compatibility Whether or not to be picture mode compatible | |
1382 | artist-vaporize-fuzziness Tolerance when recognizing lines | |
1383 | artist-spray-interval Seconds between repeated sprayings | |
1384 | artist-spray-radius Size of the spray-area | |
1385 | artist-spray-chars The spray-``color'' | |
1386 | artist-spray-new-chars Initial spray-``color'' | |
1387 | ||
1388 | Hooks | |
1389 | ||
1390 | When entering artist-mode, the hook `artist-mode-init-hook' is called. | |
1391 | When quitting artist-mode, the hook `artist-mode-exit-hook' is called. | |
1392 | ||
1393 | ||
1394 | Keymap summary | |
1395 | ||
1396 | \\{artist-mode-map}" | |
1397 | (interactive) | |
1398 | (if (setq artist-mode | |
1399 | (if (null state) (not artist-mode) | |
1400 | (> (prefix-numeric-value state) 0))) | |
1401 | (artist-mode-init) | |
1402 | (artist-mode-exit))) | |
1403 | ||
1404 | ;; insert our minor mode string | |
1405 | (or (assq 'artist-mode minor-mode-alist) | |
1406 | (setq minor-mode-alist | |
1407 | (cons '(artist-mode artist-mode-name) | |
1408 | minor-mode-alist))) | |
1409 | ||
1410 | ;; insert our minor mode keymap | |
1411 | (or (assq 'artist-mode minor-mode-map-alist) | |
1412 | (setq minor-mode-map-alist | |
1413 | (cons (cons 'artist-mode artist-mode-map) | |
1414 | minor-mode-map-alist))) | |
1415 | ||
1416 | ||
b95b34e5 GM |
1417 | ;; Init and exit |
1418 | (defun artist-mode-init () | |
1419 | "Init Artist mode. This will call the hook `artist-mode-init-hook'." | |
921d84aa CY |
1420 | ;; Set up a conversion table for mapping tabs and new-lines to spaces. |
1421 | ;; the last case, 0, is for the last position in buffer/region, where | |
1422 | ;; the `following-char' function returns 0. | |
b95b34e5 GM |
1423 | (let ((i 0)) |
1424 | (while (< i 256) | |
1425 | (aset artist-replacement-table i i) | |
1426 | (setq i (1+ i)))) | |
fe6e86db JB |
1427 | (aset artist-replacement-table ?\n ?\s) |
1428 | (aset artist-replacement-table ?\t ?\s) | |
1429 | (aset artist-replacement-table 0 ?\s) | |
921d84aa | 1430 | ;; More setup |
b95b34e5 GM |
1431 | (make-local-variable 'artist-key-is-drawing) |
1432 | (make-local-variable 'artist-key-endpoint1) | |
1433 | (make-local-variable 'artist-key-poly-point-list) | |
1434 | (make-local-variable 'artist-key-shape) | |
1435 | (make-local-variable 'artist-key-draw-how) | |
1436 | (make-local-variable 'artist-popup-menu-table) | |
1437 | (make-local-variable 'artist-key-compl-table) | |
9d8b0f9c | 1438 | (make-local-variable 'artist-prev-next-op-alist) |
b95b34e5 GM |
1439 | (make-local-variable 'artist-rb-save-data) |
1440 | (make-local-variable 'artist-arrow-point-1) | |
1441 | (make-local-variable 'artist-arrow-point-2) | |
1442 | (setq artist-key-is-drawing nil) | |
1443 | (setq artist-key-endpoint1 nil) | |
1444 | (setq artist-key-poly-point-list nil) | |
1445 | (setq artist-key-shape nil) | |
1446 | (setq artist-popup-menu-table (artist-compute-popup-menu-table artist-mt)) | |
1447 | (setq artist-key-compl-table (artist-compute-key-compl-table artist-mt)) | |
9d8b0f9c RS |
1448 | (setq artist-prev-next-op-alist |
1449 | (artist-make-prev-next-op-alist artist-key-compl-table)) | |
b95b34e5 GM |
1450 | (setq artist-rb-save-data (make-vector 7 0)) |
1451 | (setq artist-arrow-point-1 nil) | |
1452 | (setq artist-arrow-point-2 nil) | |
1453 | (make-local-variable 'next-line-add-newlines) | |
1454 | (setq next-line-add-newlines t) | |
1455 | (setq artist-key-draw-how | |
1456 | (artist-go-get-draw-how-from-symbol artist-curr-go)) | |
1457 | (if (and artist-picture-compatibility (not (eq major-mode 'picture-mode))) | |
1458 | (progn | |
1459 | (picture-mode) | |
1460 | (message ""))) | |
1461 | (run-hooks 'artist-mode-init-hook) | |
1462 | (artist-mode-line-show-curr-operation artist-key-is-drawing)) | |
1463 | ||
1464 | (defun artist-mode-exit () | |
1465 | "Exit Artist mode. This will call the hook `artist-mode-exit-hook'." | |
1466 | (if (and artist-picture-compatibility (eq major-mode 'picture-mode)) | |
1467 | (picture-mode-exit)) | |
1468 | (kill-local-variable 'next-line-add-newlines) | |
1469 | (run-hooks 'artist-mode-exit-hook)) | |
1470 | ||
1471 | (defun artist-mode-off () | |
1472 | "Turn Artist mode off." | |
1473 | (interactive) | |
1474 | (artist-mode -1) | |
1475 | (force-mode-line-update)) | |
1476 | ||
1477 | ;; | |
1478 | ;; General routines | |
1479 | ;; | |
1480 | ||
1481 | (defun artist-update-display () | |
1482 | "Repaint the display." | |
1483 | (sit-for 0)) | |
1484 | ||
1485 | (defun artist-mode-line-show-curr-operation (is-drawing) | |
98d92ce3 | 1486 | "Show current operation in mode-line. If IS-DRAWING, show that." |
b95b34e5 GM |
1487 | (let ((mtext (concat artist-mode-name "/" |
1488 | (artist-go-get-mode-line-from-symbol artist-curr-go) | |
1489 | (if is-drawing "/*" "")))) | |
1490 | (setcdr (assq 'artist-mode minor-mode-alist) (list mtext))) | |
1491 | (force-mode-line-update)) | |
1492 | ||
1493 | ||
1494 | (defun artist-t-if-fill-char-set () | |
1495 | "Return the value of the variable `artist-fill-char-set'." | |
1496 | artist-fill-char-set) | |
1497 | ||
1498 | (defun artist-t () | |
1499 | "Always return t." | |
1500 | t) | |
1501 | ||
1502 | (defun artist-nil () | |
1503 | "Always return nil." | |
1504 | nil) | |
1505 | ||
1506 | (defun artist-arrows () | |
1507 | "Say yes to arrows!" | |
1508 | t) | |
1509 | ||
1510 | (defun artist-no-arrows () | |
1511 | "Say no to arrows!" | |
1512 | nil) | |
1513 | ||
1514 | ;; | |
1515 | ;; Auxiliary init-routines | |
1516 | ;; | |
1517 | ||
1518 | ; | |
1519 | ; Computing the table for the x-popup-menu from the master table | |
1520 | ; | |
1521 | ||
1522 | (defun artist-compute-popup-menu-table (menu-table) | |
045b9da7 | 1523 | "Create a menu from MENU-TABLE data. |
b95b34e5 GM |
1524 | The returned value is suitable for the `x-popup-menu' function." |
1525 | (cons "Artist menu" | |
1526 | (artist-compute-popup-menu-table-sub menu-table))) | |
1527 | ||
1528 | (defun artist-compute-popup-menu-table-sub (menu-table) | |
1529 | "Compute operation table suitable for `x-popup-menu' from MENU-TABLE." | |
1530 | (mapcar | |
1531 | (lambda (element) | |
1532 | (let ((element-tag (artist-mt-get-tag element))) | |
1533 | (cond ((eq element-tag 'graphics-operation) | |
1534 | (let* ((info-part (artist-mt-get-info-part element)) | |
1535 | (descr (artist-go-get-desc info-part)) | |
1536 | (unshifted (artist-go-get-unshifted info-part)) | |
1537 | (symbol (artist-go-get-symbol unshifted))) | |
1538 | (list descr symbol))) | |
1539 | ||
1540 | ((eq element-tag 'function-call) | |
1541 | (let* ((info-part (artist-mt-get-info-part element)) | |
1542 | (keyword (artist-fc-get-keyword info-part)) | |
1543 | (symbol (artist-fc-get-symbol info-part))) | |
1544 | (list keyword symbol))) | |
1545 | ||
1546 | ((eq element-tag 'separator) | |
1547 | '("" "")) | |
1548 | ||
1549 | ((eq element-tag 'menu) | |
1550 | (let* ((info-part (artist-mt-get-info-part element)) | |
1551 | (title (artist-mn-get-title info-part)) | |
1552 | (items (artist-mn-get-items info-part))) | |
1553 | (cons title (artist-compute-popup-menu-table-sub items)))) | |
1554 | ||
1555 | (t | |
1556 | (error "Internal error: unknown element-tag: \"%s\"" | |
1557 | element-tag))))) | |
1558 | menu-table)) | |
1559 | ||
1560 | ; | |
1561 | ; Computing the completion table from the master table | |
1562 | ; | |
1563 | ||
1564 | (defun artist-compute-key-compl-table (menu-table) | |
1565 | "Compute completion table from MENU-TABLE, suitable for `completing-read'." | |
1566 | (apply | |
1567 | 'nconc | |
a4a5aa2b | 1568 | (remq nil |
b95b34e5 GM |
1569 | (mapcar |
1570 | (lambda (element) | |
1571 | (let ((element-tag (artist-mt-get-tag element))) | |
1572 | (cond ((eq element-tag 'graphics-operation) | |
1573 | (let* ((info-part (artist-mt-get-info-part element)) | |
1574 | (unshifted (artist-go-get-unshifted info-part)) | |
1575 | (shifted (artist-go-get-shifted info-part)) | |
1576 | (unshifted-kwd (artist-go-get-keyword unshifted)) | |
1577 | (shifted-kwd (artist-go-get-keyword shifted))) | |
1578 | (list (list unshifted-kwd) (list shifted-kwd)))) | |
1579 | ((eq element-tag 'menu) | |
1580 | (let* ((info-part (artist-mt-get-info-part element)) | |
1581 | (items (artist-mn-get-items info-part))) | |
1582 | (artist-compute-key-compl-table items))) | |
1583 | (t | |
1584 | nil)))) | |
1585 | menu-table)))) | |
1586 | ||
1587 | ||
1588 | ; | |
1589 | ; Retrieving a symbol (graphics operation or function-call) from a keyword | |
1590 | ; | |
1591 | ||
1592 | (defun artist-mt-get-symbol-from-keyword (kwd) | |
1593 | "Search master table for keyword KWD and return its symbol." | |
1594 | (artist-mt-get-symbol-from-keyword-sub artist-mt kwd)) | |
1595 | ||
1596 | (defun artist-mt-get-symbol-from-keyword-sub (table kwd) | |
1597 | "Search TABLE for keyword KWD and return its symbol." | |
1598 | (catch 'found | |
b5242984 | 1599 | (mapc |
b95b34e5 GM |
1600 | (lambda (element) |
1601 | (let ((element-tag (artist-mt-get-tag element))) | |
1602 | (cond ((eq element-tag 'graphics-operation) | |
1603 | (let* ((info-part (artist-mt-get-info-part element)) | |
1604 | (unshifted (artist-go-get-unshifted info-part)) | |
1605 | (shifted (artist-go-get-shifted info-part)) | |
1606 | (unshifted-kwd (artist-go-get-keyword unshifted)) | |
1607 | (shifted-kwd (artist-go-get-keyword shifted)) | |
1608 | (unshifted-sym (artist-go-get-symbol unshifted)) | |
1609 | (shifted-sym (artist-go-get-symbol shifted))) | |
1610 | (if (string-equal kwd unshifted-kwd) | |
1611 | (throw 'found unshifted-sym)) | |
1612 | (if (string-equal kwd shifted-kwd) | |
1613 | (throw 'found shifted-sym)))) | |
1614 | ||
1615 | ((eq element-tag 'function-call) | |
1616 | (let* ((info-part (artist-mt-get-info-part element)) | |
1617 | (keyword (artist-fc-get-keyword info-part)) | |
1618 | (symbol (artist-fc-get-symbol info-part))) | |
1619 | (if (string-equal kwd keyword) | |
1620 | (throw 'found symbol)))) | |
1621 | ((eq element-tag 'menu) | |
1622 | (let* ((info-part (artist-mt-get-info-part element)) | |
1623 | (items (artist-mn-get-items info-part)) | |
1624 | (answer (artist-mt-get-symbol-from-keyword-sub | |
1625 | items kwd))) | |
1626 | (if answer (throw 'found answer)))) | |
1627 | (t | |
1628 | nil)))) | |
1629 | table) | |
1630 | nil)) | |
1631 | ||
1632 | ||
1633 | ; | |
1634 | ; Retrieving info from a graphics operation symbol | |
1635 | ; | |
1636 | ||
1637 | (defun artist-go-retrieve-from-symbol (symbol retrieve-fn) | |
1638 | "Search the master table for a graphics operation SYMBOL. | |
1639 | Calls RETRIEVE-FN to retrieve information from that symbol's | |
1640 | info-variant-part." | |
1641 | (artist-go-retrieve-from-symbol-sub artist-mt symbol retrieve-fn)) | |
1642 | ||
1643 | (defun artist-go-retrieve-from-symbol-sub (table symbol retrieve-fn) | |
1644 | "Search the TABLE for a graphics operation SYMBOL. | |
1645 | Calls RETRIEVE-FN to retrieve information from that symbol's | |
1646 | info-variant-part." | |
1647 | (catch 'found | |
b5242984 | 1648 | (mapc |
b95b34e5 GM |
1649 | (lambda (element) |
1650 | (let ((element-tag (artist-mt-get-tag element))) | |
1651 | (cond ((eq element-tag 'graphics-operation) | |
1652 | (let* ((info-part (artist-mt-get-info-part element)) | |
1653 | (unshifted (artist-go-get-unshifted info-part)) | |
1654 | (shifted (artist-go-get-shifted info-part)) | |
1655 | (unshifted-sym (artist-go-get-symbol unshifted)) | |
1656 | (shifted-sym (artist-go-get-symbol shifted)) | |
1657 | (variant-part (cond | |
1658 | ((eq unshifted-sym symbol) unshifted) | |
1659 | ((eq shifted-sym symbol) shifted) | |
1660 | (t nil)))) | |
1661 | (if variant-part ; if found do: | |
1662 | (throw 'found (funcall retrieve-fn variant-part))))) | |
1663 | ||
1664 | ((eq element-tag 'menu) | |
1665 | (let* ((info-part (artist-mt-get-info-part element)) | |
1666 | (items (artist-mn-get-items info-part)) | |
1667 | (answer (artist-go-retrieve-from-symbol-sub | |
1668 | items symbol retrieve-fn))) | |
1669 | (if answer (throw 'found answer))))))) | |
1670 | ||
1671 | table) | |
1672 | nil)) | |
1673 | ||
1674 | (defun artist-go-get-keyword-from-symbol (symbol) | |
1675 | "Search the master table, get keyword from a graphics operation SYMBOL." | |
1676 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-keyword)) | |
1677 | ||
1678 | (defun artist-go-get-mode-line-from-symbol (symbol) | |
1679 | "Search the master table, get mode-line from a graphics operation SYMBOL." | |
1680 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-mode-line)) | |
1681 | ||
1682 | (defun artist-go-get-arrow-pred-from-symbol (symbol) | |
1683 | "Search the master table, get arrow-pred from a graphics operation SYMBOL." | |
1684 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-pred)) | |
1685 | ||
1686 | (defun artist-go-get-arrow-set-fn-from-symbol (symbol) | |
1687 | "Search the master table, get arrow-set-fn from a graphics operation SYMBOL." | |
1688 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-set-fn)) | |
1689 | ||
1690 | (defun artist-go-get-init-fn-from-symbol (symbol) | |
1691 | "Search the master table, get init-fn from a graphics operation SYMBOL." | |
1692 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-init-fn)) | |
1693 | ||
1694 | (defun artist-go-get-prep-fill-fn-from-symbol (symbol) | |
1695 | "Search the master table, get prep-fill-fn from a graphics operation SYMBOL." | |
1696 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-prep-fill-fn)) | |
1697 | ||
1698 | (defun artist-go-get-exit-fn-from-symbol (symbol) | |
1699 | "Search the master table, get exit-fn from a graphics operation SYMBOL." | |
1700 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-exit-fn)) | |
1701 | ||
1702 | (defun artist-go-get-draw-fn-from-symbol (symbol) | |
1703 | "Search the master table, get draw-fn from a graphics operation SYMBOL." | |
1704 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-fn)) | |
1705 | ||
1706 | (defun artist-go-get-draw-how-from-symbol (symbol) | |
1707 | "Search the master table, get draw-how from a graphics operation SYMBOL." | |
1708 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-how)) | |
1709 | ||
1710 | (defun artist-go-get-undraw-fn-from-symbol (symbol) | |
1711 | "Search the master table, get undraw-fn from a graphics operation SYMBOL." | |
1712 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-undraw-fn)) | |
1713 | ||
1714 | (defun artist-go-get-interval-fn-from-symbol (symbol) | |
1715 | "Search the master table, get interval-fn from a graphics operation SYMBOL." | |
1716 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-interval-fn)) | |
1717 | ||
1718 | (defun artist-go-get-fill-pred-from-symbol (symbol) | |
1719 | "Search the master table, get fill-pred from a graphics operation SYMBOL." | |
1720 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-pred)) | |
1721 | ||
1722 | (defun artist-go-get-fill-fn-from-symbol (symbol) | |
1723 | "Search the master table, get fill-fn from a graphics operation SYMBOL." | |
1724 | (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-fn)) | |
1725 | ||
1726 | (defun artist-go-get-symbol-shift (symbol is-shifted) | |
1727 | "Search for (shifted or unshifted) graphics operation SYMBOL. | |
1728 | If IS-SHIFTED is non-nil, return the shifted symbol, | |
98d92ce3 | 1729 | otherwise the unshifted symbol." |
b95b34e5 GM |
1730 | (artist-go-get-symbol-shift-sub artist-mt symbol is-shifted)) |
1731 | ||
1732 | (defun artist-go-get-symbol-shift-sub (table symbol is-shifted) | |
1733 | "Search TABLE for (shifted or unshifted) graphics SYMBOL. | |
1734 | If IS-SHIFTED is non-nil, return the shifted symbol, | |
98d92ce3 | 1735 | otherwise the unshifted symbol." |
b95b34e5 | 1736 | (catch 'found |
b5242984 | 1737 | (mapc |
b95b34e5 GM |
1738 | (lambda (element) |
1739 | (let ((element-tag (artist-mt-get-tag element))) | |
1740 | (cond ((eq element-tag 'graphics-operation) | |
1741 | (let* ((info-part (artist-mt-get-info-part element)) | |
1742 | (unshift-variant (artist-go-get-unshifted info-part)) | |
1743 | (shift-variant (artist-go-get-shifted info-part)) | |
1744 | (unshift-sym (artist-go-get-symbol unshift-variant)) | |
1745 | (shift-sym (artist-go-get-symbol shift-variant))) | |
1746 | (if (or (eq symbol unshift-sym) (eq symbol shift-sym)) | |
1747 | (throw 'found (if is-shifted shift-sym unshift-sym))))) | |
1748 | ||
1749 | ((eq element-tag 'menu) | |
1750 | (let* ((info-part (artist-mt-get-info-part element)) | |
1751 | (items (artist-mn-get-items info-part)) | |
1752 | (answer (artist-go-get-symbol-shift-sub | |
1753 | items symbol is-shifted))) | |
1754 | (if answer (throw 'found answer))))))) | |
1755 | ||
1756 | table) | |
1757 | nil)) | |
1758 | ||
1759 | ; | |
1760 | ; Retrieving info from a function-call symbol | |
1761 | ; | |
1762 | ||
1763 | (defun artist-fc-retrieve-from-symbol (symbol retrieve-fn) | |
1764 | "Search the master table for a function call SYMBOL. | |
1765 | Calls RETRIEVE-FN to retrieve information from that symbol's | |
1766 | info-variant-part." | |
1767 | (artist-fc-retrieve-from-symbol-sub artist-mt symbol retrieve-fn)) | |
1768 | ||
1769 | (defun artist-fc-retrieve-from-symbol-sub (table symbol retrieve-fn) | |
1770 | "Search TABLE for a function-call SYMBOL. | |
1771 | Calls RETRIEVE-FN to retrieve information from that symbol's | |
1772 | info-variant-part." | |
1773 | (catch 'found | |
b5242984 | 1774 | (mapc |
b95b34e5 GM |
1775 | (lambda (element) |
1776 | (let ((element-tag (artist-mt-get-tag element))) | |
1777 | (cond ((eq element-tag 'function-call) | |
1778 | (let* ((info-part (artist-mt-get-info-part element)) | |
1779 | (fc-symbol (artist-fc-get-symbol info-part))) | |
1780 | (if (eq fc-symbol symbol) | |
1781 | (throw 'found (funcall retrieve-fn info-part))))) | |
1782 | ||
1783 | ((eq element-tag 'menu) | |
1784 | (let* ((info-part (artist-mt-get-info-part element)) | |
1785 | (items (artist-mn-get-items info-part)) | |
1786 | (answer (artist-fc-retrieve-from-symbol-sub | |
1787 | items symbol retrieve-fn))) | |
1788 | (if answer (throw 'found answer))))))) | |
1789 | ||
1790 | table) | |
1791 | nil)) | |
1792 | ||
1793 | (defun artist-fc-get-fn-from-symbol (symbol) | |
1794 | "Search the master table to get function from a function call SYMBOL." | |
1795 | (artist-fc-retrieve-from-symbol symbol 'artist-fc-get-fn)) | |
1796 | ||
1797 | ||
1798 | ;; | |
1799 | ;; Utilities | |
1800 | ;; | |
1801 | ||
1802 | ;; Macro that won't funcall the function if it is nil. | |
1803 | ;; | |
1804 | (defmacro artist-funcall (fn &rest args) | |
3ecd3a56 | 1805 | "Call function FN with ARGS, if FN is not nil." |
b95b34e5 GM |
1806 | (list 'if fn (cons 'funcall (cons fn args)))) |
1807 | ||
b95b34e5 GM |
1808 | (defun artist-uniq (l) |
1809 | "Remove consecutive duplicates in list L. Comparison is done with `equal'." | |
1810 | (cond ((null l) nil) | |
1811 | ((null (cdr l)) l) ; only one element in list | |
1812 | ((equal (car l) (car (cdr l))) (artist-uniq (cdr l))) ; first 2 equal | |
1813 | (t (cons (car l) (artist-uniq (cdr l)))))) ; first 2 are different | |
1814 | ||
b95b34e5 GM |
1815 | (defun artist-string-split (str r) |
1816 | "Split string STR at occurrences of regexp R, returning a list of strings." | |
1817 | (let ((res nil) | |
1818 | (start 0) | |
1819 | (match-pos 0)) | |
1820 | (while (setq match-pos (string-match r str start)) | |
1821 | (setq res (cons (copy-sequence (substring str start match-pos)) res)) | |
1822 | (setq start (match-end 0))) | |
1823 | (if (null res) | |
1824 | (list str) | |
1825 | (if (< (match-end 0) (- (length str) 1)) | |
1826 | (setq res (cons (substring str (match-end 0) (length str)) res))) | |
1827 | (reverse res)))) | |
1828 | ||
1829 | (defun artist-string-to-file (str file-name) | |
1830 | "Write string STR to file FILE-NAME." | |
1831 | (write-region str 'end-is-ignored file-name nil 'no-message)) | |
1832 | ||
1833 | (defun artist-file-to-string (file-name) | |
1834 | "Read from file FILE-NAME into a string." | |
1835 | (save-excursion | |
1836 | (let ((tmp-buffer (get-buffer-create (concat "*artist-" file-name "*")))) | |
1837 | (set-buffer tmp-buffer) | |
1838 | (goto-char (point-min)) | |
1839 | (insert-file-contents file-name nil nil nil t) | |
1840 | (let ((str (copy-sequence (buffer-substring (point-min) | |
1841 | (point-max))))) | |
1842 | (kill-buffer tmp-buffer) | |
1843 | str)))) | |
1844 | ||
1845 | (defun artist-clear-buffer (buf) | |
1846 | "Clear contents of buffer BUF." | |
9a529312 | 1847 | (with-current-buffer buf |
b95b34e5 GM |
1848 | (goto-char (point-min)) |
1849 | (delete-char (- (point-max) (point-min)) nil))) | |
1850 | ||
a9645a66 | 1851 | |
b95b34e5 GM |
1852 | (defun artist-system (program stdin &optional program-args) |
1853 | "Run PROGRAM synchronously with the contents of string STDIN to stdin. | |
1854 | Optional args PROGRAM-ARGS are arguments to PROGRAM. | |
1855 | Return a list (RETURN-CODE STDOUT STDERR)." | |
1856 | (save-excursion | |
1857 | (let* ((tmp-stdin-file-name (if stdin | |
e3fd4ebf | 1858 | (make-temp-file "artist-stdin.") |
b95b34e5 GM |
1859 | nil)) |
1860 | (tmp-stdout-buffer (get-buffer-create | |
1861 | (concat "*artist-" program "*"))) | |
e3fd4ebf | 1862 | (tmp-stderr-file-name (make-temp-file "artist-stdout.")) |
b95b34e5 GM |
1863 | (binary-process-input nil) ; for msdos |
1864 | (binary-process-output nil)) | |
1865 | ||
1866 | ;; Prepare stdin | |
1867 | (if stdin (artist-string-to-file stdin tmp-stdin-file-name)) | |
1868 | ||
1869 | ;; Clear the buffer | |
1870 | (artist-clear-buffer tmp-stdout-buffer) | |
1871 | ||
1872 | ;; Start the program | |
1873 | (unwind-protect | |
1874 | (let ((res (if program-args | |
1875 | (apply 'call-process | |
1876 | program | |
1877 | tmp-stdin-file-name | |
1878 | (list tmp-stdout-buffer | |
1879 | tmp-stderr-file-name) | |
1880 | nil | |
1881 | (if (stringp program-args) | |
1882 | (list program-args) | |
1883 | program-args)) | |
1884 | (apply 'call-process | |
1885 | program | |
1886 | tmp-stdin-file-name | |
1887 | (list tmp-stdout-buffer | |
1888 | tmp-stderr-file-name) | |
1889 | nil)))) | |
1890 | ||
1891 | ;; the return value | |
1892 | (list res | |
9a529312 SM |
1893 | (with-current-buffer tmp-stdout-buffer |
1894 | (buffer-substring (point-min) (point-max))) | |
b95b34e5 GM |
1895 | (artist-file-to-string tmp-stderr-file-name))) |
1896 | ||
1897 | ;; Unwind: remove temporary files and buffers | |
1898 | (if (and stdin (file-exists-p tmp-stdin-file-name)) | |
1899 | (delete-file tmp-stdin-file-name)) | |
1900 | (if (file-exists-p tmp-stderr-file-name) | |
1901 | (delete-file tmp-stderr-file-name)) | |
1902 | (if (memq tmp-stdout-buffer (buffer-list)) | |
1903 | (kill-buffer tmp-stdout-buffer)))))) | |
1904 | ||
1905 | ;; Routines that deal with the buffer | |
1906 | ;; | |
1907 | ;; artist-current-line get line number (top of buffer is 0) | |
1908 | ;; | |
1909 | ;; artist-move-to-xy move to (x,y) (0,0) is beg-of-buffer | |
1910 | ;; | |
1911 | ;; artist-get-char-at-xy get char in at (x,y) | |
1912 | ;; | |
1913 | ;; artist-replace-char overwrite (replace) char at point | |
1914 | ;; artist-replace-chars overwrite (replace) chars at point | |
1915 | ;; | |
1916 | ||
1917 | (defsubst artist-current-column () | |
1918 | "Return point's current column." | |
1919 | (current-column)) | |
1920 | ||
1921 | (defsubst artist-current-line () | |
1922 | "Return point's current line, buffer-relative. Top of buffer is 0." | |
1923 | (+ (count-lines 1 (point)) | |
1924 | (if (= (current-column) 0) 1 0) | |
1925 | -1)) | |
1926 | ||
1927 | (defsubst artist-move-to-xy (x y) | |
1928 | "Move to column X, at row Y from the top of buffer. Top line is 0." | |
1929 | ;; | |
1930 | ;; Q: Why do we do forward-line twice? | |
1931 | ;; A: The documentation for forward-line says | |
1932 | ;; | |
1933 | ;; "... Returns the count of lines left to move. ... With | |
1934 | ;; positive N, a non-empty line at the end counts as one | |
1935 | ;; line successfully moved (for the return value)." | |
1936 | ;; | |
1937 | ;; This means that if we are trying to move forward past the end | |
1938 | ;; of the buffer, and that last line happened to be longer than | |
1939 | ;; the current column, then we end up at the end of that last | |
1940 | ;; line, and forward-line returns one less than we actually | |
1941 | ;; wanted to move. | |
1942 | ;; | |
1943 | ;; Example: In the figure below, the `X' is the very last | |
1944 | ;; character in the buffer ("a non-empty line at the | |
9b053e76 | 1945 | ;; end"). Suppose point is at P. Then (forward-line 1) |
b95b34e5 GM |
1946 | ;; returns 0 and puts point after the `X'. |
1947 | ;; | |
1948 | ;; --------top of buffer-------- | |
1949 | ;; | |
1950 | ;; P X | |
1951 | ;; -------bottom of buffer------ | |
1952 | ;; | |
1953 | ;; But, if we are at the end of buffer when trying to move | |
1954 | ;; forward, then forward-line will return the (for us) correct | |
1955 | ;; value, which is good, because we will come to the end of the | |
1956 | ;; buffer by the first forward-line. The second forward-line | |
1957 | ;; will then get us where we really wanted to go. | |
1958 | ;; | |
1959 | ;; If we are not moving past the end of the buffer, then the | |
1960 | ;; second forward-line will return 0. | |
1961 | ;; | |
1962 | ;; Q: What happens if we are moving upwards? | |
1963 | ;; A: That will work good. insert-char won't insert a negative | |
1964 | ;; number of chars, and forward-line will fail silently if we are | |
1965 | ;; moving past the beginning of the buffer. | |
1966 | ;; | |
1967 | (forward-line (- y (artist-current-line))) | |
1968 | (insert-char ?\n (forward-line (- y (artist-current-line)))) | |
1969 | (move-to-column (max x 0) t) | |
1970 | (let ((curr-y (artist-current-line))) | |
1971 | (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y)) | |
1972 | (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y)))) | |
1973 | ||
1974 | (defsubst artist-get-char-at-xy (x y) | |
1975 | "Return the character found at column X, row Y. | |
1976 | Also updates the variables `artist-draw-min-y' and `artist-draw-max-y'." | |
1977 | (artist-move-to-xy x y) | |
1978 | (let ((curr-y (artist-current-line))) | |
1979 | (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y)) | |
1980 | (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y))) | |
1981 | (following-char)) | |
1982 | ||
1983 | ||
921d84aa CY |
1984 | (defsubst artist-get-replacement-char (c) |
1985 | "Retrieve a replacement for character C from `artist-replacement-table'. | |
1986 | The replacement is used to convert tabs and new-lines to spaces." | |
1987 | ;; Characters may be outside the range of the `artist-replacement-table', | |
fe7a3057 | 1988 | ;; for example if they are Unicode code points >= 256. |
921d84aa CY |
1989 | ;; Check so we don't attempt to access the array out of its bounds, |
1990 | ;; assuming no such character needs to be replaced. | |
1991 | (if (< c (length artist-replacement-table)) | |
1992 | (aref artist-replacement-table c) | |
1993 | c)) | |
1994 | ||
b95b34e5 GM |
1995 | (defun artist-get-char-at-xy-conv (x y) |
1996 | "Retrieve the character at X, Y, converting tabs and new-lines to spaces." | |
1997 | (save-excursion | |
921d84aa | 1998 | (artist-get-replacement-char (artist-get-char-at-xy x y)))) |
b95b34e5 GM |
1999 | |
2000 | ||
2001 | (defun artist-replace-char (new-char) | |
2002 | "Replace the character at point with NEW-CHAR." | |
f88cd114 GM |
2003 | (let ((overwrite-mode 'overwrite-mode-textual) |
2004 | (fill-column 32765) ; Large :-) | |
2005 | (blink-matching-paren nil)) | |
2006 | (setq last-command-event (artist-get-replacement-char new-char)) | |
2007 | (self-insert-command 1))) | |
b95b34e5 GM |
2008 | |
2009 | (defun artist-replace-chars (new-char count) | |
2010 | "Replace characters at point with NEW-CHAR. COUNT chars are replaced." | |
2011 | ;; Check that the variable exists first. The doc says it was added in 19.23. | |
5a047002 | 2012 | (if (and (and (boundp 'emacs-major-version) (= emacs-major-version 20)) |
b95b34e5 GM |
2013 | (and (boundp 'emacs-minor-version) (<= emacs-minor-version 3))) |
2014 | ;; This is a bug workaround for Emacs 20, versions up to 20.3: | |
2015 | ;; The self-insert-command doesn't care about the overwrite-mode, | |
2016 | ;; so the insertion is done in the same way as in picture mode. | |
2017 | ;; This seems to be a little bit slower. | |
921d84aa | 2018 | (let* ((replaced-c (artist-get-replacement-char new-char)) |
5a047002 | 2019 | (replaced-s (make-string count replaced-c))) |
b95b34e5 GM |
2020 | (artist-move-to-xy (+ (artist-current-column) count) |
2021 | (artist-current-line)) | |
2022 | (delete-char (- count)) | |
2023 | (insert replaced-s)) | |
2024 | ;; In emacs-19, the self-insert-command works better | |
2025 | (let ((overwrite-mode 'overwrite-mode-textual) | |
2026 | (fill-column 32765) ; Large :-) | |
2027 | (blink-matching-paren nil)) | |
921d84aa | 2028 | (setq last-command-event (artist-get-replacement-char new-char)) |
b95b34e5 GM |
2029 | (self-insert-command count)))) |
2030 | ||
2031 | (defsubst artist-replace-string (string &optional see-thru) | |
2032 | "Replace contents at point with STRING. | |
98d92ce3 | 2033 | With optional argument SEE-THRU set to non-nil, text in the buffer |
b95b34e5 GM |
2034 | ``shines thru'' blanks in the STRING." |
2035 | (let ((char-list (append string nil)) ; convert the string to a list | |
2036 | (overwrite-mode 'overwrite-mode-textual) | |
2037 | (fill-column 32765) ; Large :-) | |
2038 | (blink-matching-paren nil)) | |
2039 | (while char-list | |
2040 | (let ((c (car char-list))) | |
921d84aa | 2041 | (if (and see-thru (= (artist-get-replacement-char c) ?\s)) |
b95b34e5 GM |
2042 | (artist-move-to-xy (1+ (artist-current-column)) |
2043 | (artist-current-line)) | |
2044 | (artist-replace-char c))) | |
2045 | (setq char-list (cdr char-list))))) | |
2046 | ||
2047 | ;; | |
2048 | ;; Routines for setting and unsetting points | |
2049 | ;; Used when not rubber-banding | |
2050 | ;; | |
2051 | (defun artist-no-rb-unset-point1 () | |
2052 | "Unsets point 1 when not rubber-banding." | |
2053 | (let ((x-now (artist-current-column)) | |
2054 | (y-now (artist-current-line)) | |
2055 | (x (aref artist-rb-save-data 0)) | |
2056 | (y (aref artist-rb-save-data 1))) | |
2057 | (artist-move-to-xy x y) | |
2058 | (artist-replace-char (aref artist-rb-save-data 2)) | |
2059 | (artist-move-to-xy x-now y-now))) | |
2060 | ||
2061 | (defun artist-no-rb-set-point1 (x y) | |
2062 | "Set point 1 at X, Y when not rubber-banding." | |
2063 | (let ((x-now (artist-current-column)) | |
2064 | (y-now (artist-current-line))) | |
2065 | (aset artist-rb-save-data 0 x) | |
2066 | (aset artist-rb-save-data 1 y) | |
2067 | (aset artist-rb-save-data 2 (artist-get-char-at-xy x y)) | |
2068 | (artist-move-to-xy x y) | |
2069 | (artist-replace-char artist-first-char) | |
2070 | (artist-move-to-xy x-now y-now) | |
2071 | (aset artist-rb-save-data 6 0))) | |
2072 | ||
2073 | (defun artist-no-rb-unset-point2 () | |
2074 | "This function unsets point 2 when not rubber-banding." | |
2075 | (if (= (aref artist-rb-save-data 6) 1) | |
2076 | (let ((x-now (artist-current-column)) | |
2077 | (y-now (artist-current-line)) | |
2078 | (x (aref artist-rb-save-data 3)) | |
2079 | (y (aref artist-rb-save-data 4))) | |
2080 | (artist-move-to-xy x y) | |
2081 | (artist-replace-char (aref artist-rb-save-data 5)) | |
2082 | (artist-move-to-xy x-now y-now)))) | |
2083 | ||
2084 | (defun artist-no-rb-set-point2 (x y) | |
2085 | "Set point 2 at X, Y when not rubber-banding." | |
2086 | (let ((x-now (artist-current-column)) | |
2087 | (y-now (artist-current-line))) | |
2088 | (aset artist-rb-save-data 3 x) | |
2089 | (aset artist-rb-save-data 4 y) | |
2090 | (aset artist-rb-save-data 5 (artist-get-char-at-xy x y)) | |
2091 | (artist-move-to-xy x y) | |
2092 | (artist-replace-char artist-second-char) | |
2093 | (artist-move-to-xy x-now y-now) | |
2094 | (aset artist-rb-save-data 6 1))) | |
2095 | ||
2096 | (defun artist-no-rb-unset-points () | |
2097 | "This function unsets point 1 and 2 when not rubber-banding." | |
2098 | (artist-no-rb-unset-point1) | |
2099 | (artist-no-rb-unset-point2)) | |
2100 | ||
2101 | ||
2102 | ;; artist-intersection-char | |
2103 | ;; | |
2104 | ;; Note: If changing this, see the notes for artist-unintersection-char | |
2105 | ;; and artist-vaporize-lines | |
2106 | ;; | |
2107 | (defun artist-intersection-char (new-c old-c) | |
2108 | "Calculates intersection character when drawing a NEW-C on top of an OLD-C. | |
2109 | Return character according to this scheme: | |
2110 | ||
2111 | OLD-C NEW-C return | |
2112 | - | + | |
2113 | | - + | |
2114 | + | + | |
2115 | + - + | |
2116 | \\ / X | |
2117 | / \\ X | |
2118 | X / X | |
2119 | X \\ X | |
2120 | other combinations NEW-C" | |
2121 | ||
2122 | (cond ((and (= old-c ?- ) (= new-c ?| )) ?+ ) | |
2123 | ((and (= old-c ?| ) (= new-c ?- )) ?+ ) | |
2124 | ((and (= old-c ?+ ) (= new-c ?- )) ?+ ) | |
2125 | ((and (= old-c ?+ ) (= new-c ?| )) ?+ ) | |
2126 | ((and (= old-c ?\\ ) (= new-c ?/ )) ?X ) | |
2127 | ((and (= old-c ?/ ) (= new-c ?\\ )) ?X ) | |
2128 | ((and (= old-c ?X ) (= new-c ?/ )) ?X ) | |
2129 | ((and (= old-c ?X ) (= new-c ?\\ )) ?X ) | |
2130 | (t new-c))) | |
2131 | ||
2132 | ;; artist-unintersection-char | |
2133 | ;; | |
2134 | ;; Note: If changing this, see the note for artist-vaporize-lines | |
2135 | ;; | |
2136 | (defun artist-unintersection-char (line-c buffer-c) | |
2137 | "Restore character to before intersection when removing LINE-C from BUFFER-C. | |
2138 | Return character according to this scheme: | |
2139 | ||
2140 | LINE-C BUFFER-C return | |
2141 | - + | | |
2142 | | + - | |
2143 | \\ X / | |
2144 | / X \\ | |
2145 | other combinations `artist-erase-char'." | |
2146 | ||
2147 | (cond ((and (= line-c ?- ) (= buffer-c ?+ )) ?| ) | |
2148 | ((and (= line-c ?| ) (= buffer-c ?+ )) ?- ) | |
2149 | ((and (= line-c ?\\ ) (= buffer-c ?X )) ?/ ) | |
2150 | ((and (= line-c ?/ ) (= buffer-c ?X )) ?\\ ) | |
2151 | ((= line-c buffer-c) artist-erase-char) | |
2152 | (t buffer-c))) | |
2153 | ||
2154 | ||
2155 | ;; Computing the line-char to use | |
2156 | ;; for use with borderless shapes | |
2157 | ;; | |
2158 | (defsubst artist-compute-line-char () | |
2159 | "Compute which character to use for lines, if any. | |
2160 | Return value is either nil for the default characters that make up lines, or | |
2161 | a character chosen depending on the variables `artist-borderless-shapes', | |
2162 | `artist-fill-char-set', `artist-fill-char' and | |
2163 | `artist-line-char-set' and `artist-line-char'." | |
2164 | (if (and artist-borderless-shapes artist-fill-char-set) | |
2165 | artist-fill-char | |
2166 | (if artist-line-char-set | |
2167 | artist-line-char | |
2168 | nil))) | |
2169 | ||
2170 | ||
2171 | ;; Things for drawing horizontal, vertical and diagonal (straight) lines. | |
2172 | ;; | |
2173 | ;; A line here is a vector: | |
2174 | ;; [ start-x start-y length direction saved-char-1 saved-char-2 ... ] | |
2175 | ;; directions start with 0 at the x-axis and counts anti clockwise. | |
2176 | ;; | |
2177 | (defvar artist-direction-info | |
2178 | ;; x y char | |
2179 | [ [ 1 0 ?- ] ; direction 0 | |
2180 | [ 1 1 ?\\ ] ; direction 1 | |
2181 | [ 0 1 ?| ] ; direction 2 | |
2182 | [ -1 1 ?/ ] ; direction 3 | |
2183 | [ -1 0 ?- ] ; direction 4 | |
2184 | [ -1 -1 ?\\ ] ; direction 5 | |
2185 | [ 0 -1 ?| ] ; direction 6 | |
2186 | [ 1 -1 ?/ ] ] ; direction 7 | |
2187 | "Table used for stepping x and y coordinates in a specific direction. | |
2188 | This table is also used for determining which char to use for that direction.") | |
2189 | ||
2190 | (defsubst artist-direction-step-x (direction) | |
2191 | "Return the x-step for DIRECTION from the `artist-direction-info' table." | |
2192 | (aref (aref artist-direction-info direction) 0)) | |
2193 | ||
2194 | (defsubst artist-direction-step-y (direction) | |
2195 | "Return the y-step for DIRECTION from the `artist-direction-info' table." | |
2196 | (aref (aref artist-direction-info direction) 1)) | |
2197 | ||
2198 | (defun artist-direction-char (direction) | |
2199 | "Return the character for DIRECTION from the `artist-direction-info' table." | |
2200 | (aref (aref artist-direction-info direction) 2)) | |
2201 | ||
2202 | ;; artist-find-direction | |
2203 | ;; | |
2204 | ;; | |
2205 | ;; | |
2206 | (defun artist-find-direction (x1 y1 x2 y2) | |
2207 | "Find the direction from point X1,Y1 to X2,Y2. | |
2208 | Returns a DIRECTION, a number 0--7, coded as follows: | |
2209 | ||
2210 | 5 6 7 | |
2211 | \\ | / | |
2212 | 4 - * - 0 | |
2213 | / | \\ | |
2214 | 3 2 1" | |
2215 | (let ((delta-x (- x2 x1)) | |
2216 | (delta-y (- y2 y1))) | |
2217 | (cond ((>= delta-x (* 2 (abs delta-y))) 0) | |
2218 | ((>= delta-y (* 2 (abs delta-x))) 2) | |
2219 | ((>= (- delta-x) (* 2 (abs delta-y))) 4) | |
2220 | ((>= (- delta-y) (* 2 (abs delta-x))) 6) | |
2221 | ((and (>= delta-x 0) (>= delta-y 0)) 1) | |
2222 | ((and (<= delta-x 0) (>= delta-y 0)) 3) | |
2223 | ((and (<= delta-x 0) (<= delta-y 0)) 5) | |
2224 | ((and (>= delta-x 0) (<= delta-y 0)) 7)))) | |
2225 | ||
2226 | (defun artist-straight-calculate-length (direction x1 y1 x2 y2) | |
2227 | "Calculate length for a straight line in DIRECTION from X1,Y1 to X2,Y2." | |
2228 | (cond ((or (= direction 7) | |
2229 | (= direction 0) | |
2230 | (= direction 1)) (1+ (- x2 x1))) | |
2231 | ((or (= direction 3) | |
2232 | (= direction 4) | |
2233 | (= direction 5)) (1+ (- x1 x2))) | |
2234 | (t (1+ (abs (- y2 y1)))))) | |
2235 | ||
2236 | (defun artist-sline (x1 y1 x2 y2) | |
2237 | "Create a straight line from X1,Y1 to X2,Y2." | |
2238 | (let* ((direction (artist-find-direction x1 y1 x2 y2)) | |
2239 | (length (artist-straight-calculate-length direction x1 y1 x2 y2)) | |
2240 | (line (make-vector (+ length 4) x1))) | |
2241 | ;; not needed: | |
2242 | ;; (aset line 0 x1) | |
2243 | ;; because we set all elements to x1 | |
2244 | (aset line 1 y1) | |
2245 | (aset line 2 length) | |
2246 | (aset line 3 direction) | |
2247 | line)) | |
2248 | ||
2249 | (defun artist-save-chars-under-sline (line) | |
2250 | "Save characters under a LINE." | |
2251 | (let ((x (aref line 0)) | |
2252 | (y (aref line 1)) | |
2253 | (length (+ (aref line 2) 4)) | |
2254 | (direction (aref line 3)) | |
2255 | (i 4)) | |
2256 | (while (< i length) | |
2257 | (aset line i (artist-get-char-at-xy x y)) | |
2258 | (setq x (+ x (artist-direction-step-x direction))) | |
2259 | (setq y (+ y (artist-direction-step-y direction))) | |
2260 | (setq i (1+ i)))) | |
2261 | line) | |
2262 | ||
2263 | ||
2264 | ||
2265 | ;; Things for drawing lines in all directions. | |
91af3942 | 2266 | ;; The line drawing engine is the eight-point algorithm. |
b95b34e5 GM |
2267 | ;; |
2268 | ;; A line is here a list of (x y saved-char new-char)s. | |
2269 | ;; | |
2270 | (defvar artist-octant-info | |
2271 | ;; Initial Step in Step in | |
2272 | ;; coeffs x and y x and y | |
2273 | ;; for if q >= 0 if g < 0 | |
2274 | ;; dfdx,dfdy | |
2275 | [ [ 2 1 1 0 1 1 ] ; 1st octant | |
2276 | [ 1 2 1 1 0 1 ] ; 2nd octant | |
2277 | [ -1 2 0 1 -1 1 ] ; 3rd octant | |
2278 | [ -2 1 -1 1 -1 0 ] ; 4th octant | |
2279 | [ -2 -1 -1 0 -1 -1 ] ; 5th octant | |
2280 | [ -1 -2 -1 -1 0 -1 ] ; 6th octant | |
2281 | [ 1 -2 0 -1 1 -1 ] ; 7th octant | |
2282 | [ 2 -1 1 -1 1 0 ] ] ; 8th octant | |
2283 | "Table used by line drawing algorithm (eight point).") | |
2284 | ||
2285 | ;; Primitives for the artist-octant-info. | |
2286 | ;; Decrease octant by 1 since elt counts from 0 and octant counts from 1. | |
2287 | ;; | |
2288 | (defsubst artist-get-dfdx-init-coeff (octant) | |
2289 | "Retrieve dfdx component for OCTANT." | |
2290 | (aref (aref artist-octant-info (- octant 1)) 0)) | |
2291 | ||
2292 | (defsubst artist-get-dfdy-init-coeff (octant) | |
2293 | "Retrieve dfdy component for OCTANT." | |
2294 | (aref (aref artist-octant-info (- octant 1)) 1)) | |
2295 | ||
2296 | (defsubst artist-get-x-step-q>=0 (octant) | |
2297 | "Retrieve x-step component for OCTANT when q >= 0." | |
2298 | (aref (aref artist-octant-info (- octant 1)) 2)) | |
2299 | ||
2300 | (defsubst artist-get-y-step-q>=0 (octant) | |
2301 | "Retrieve y-step component for OCTANT when q >= 0." | |
2302 | (aref (aref artist-octant-info (- octant 1)) 3)) | |
2303 | ||
2304 | (defsubst artist-get-x-step-q<0 (octant) | |
2305 | "Retrieve x-step component for OCTANT for q < 0." | |
2306 | (aref (aref artist-octant-info (- octant 1)) 4)) | |
2307 | ||
2308 | (defsubst artist-get-y-step-q<0 (octant) | |
2309 | "Retrieve y-step component for OCTANT for q < 0." | |
2310 | (aref (aref artist-octant-info (- octant 1)) 5)) | |
2311 | ||
2312 | ||
2313 | ;; Find octant from x1 y1 x2 y2 coordinates. | |
2314 | ;; | |
2315 | (defun artist-find-octant (x1 y1 x2 y2) | |
2316 | "Find octant for a line from X1,Y1 to X2,Y2. | |
2317 | Octant are numbered 1--8, anti-clockwise as: | |
2318 | ||
2319 | \\3|2/ | |
2320 | 4\\|/1 | |
2321 | ---+--- | |
2322 | 5/|\\8 | |
2323 | /6|7\\" | |
2324 | ||
2325 | (if (<= x1 x2) ; quadrant 1 or 4 | |
2326 | (if (<= y1 y2) ; quadrant 1, octant 1 or 2 | |
2327 | (if (>= (- x2 x1) (- y2 y1)) | |
2328 | 1 | |
2329 | 2) | |
2330 | (if (>= (- x2 x1) (- (- y2 y1))) ; quadrant 4, octant 7 or 8 | |
2331 | 8 | |
2332 | 7)) | |
2333 | (if (<= y1 y2) ; quadrant 2 or 3 | |
2334 | (if (>= (- (- x2 x1)) (- y2 y1)) ; quadrant 2, octant 3 or 4 | |
2335 | 4 | |
2336 | 3) | |
2337 | (if (>= (- (- x2 x1)) (- (- y2 y1))) ; quadrant 3, octant 5 or 6 | |
2338 | 5 | |
2339 | 6)))) | |
2340 | ||
cd1181db | 2341 | ;; Some inline functions for creating, setting and reading |
b95b34e5 GM |
2342 | ;; members of a coordinate |
2343 | ;; | |
98d92ce3 | 2344 | |
b95b34e5 GM |
2345 | (defsubst artist-new-coord (x y &optional new-char) |
2346 | "Create a new coordinate at X,Y for use in a line. | |
2347 | Optional argument NEW-CHAR can be used for setting the new-char component | |
2348 | in the coord." | |
2349 | (let ((coord (make-vector 4 x))) | |
2350 | (aset coord 1 y) | |
2351 | (aset coord 3 new-char) | |
2352 | coord)) | |
2353 | ||
2354 | (defsubst artist-coord-get-x (coord) | |
2355 | "Retrieve the x component of a COORD." | |
2356 | (aref coord 0)) | |
2357 | ||
2358 | (defsubst artist-coord-get-y (coord) | |
2359 | "Retrieve the y component of a COORD." | |
2360 | (aref coord 1)) | |
2361 | ||
2362 | (defsubst artist-coord-set-x (coord new-x) | |
2363 | "Set the x component of a COORD to NEW-X." | |
2364 | (aset coord 0 new-x) | |
2365 | coord) | |
2366 | ||
2367 | (defsubst artist-coord-set-y (coord new-y) | |
2368 | "Set the y component of a COORD to NEW-Y." | |
2369 | (aset coord 1 new-y) | |
2370 | coord) | |
2371 | ||
2372 | (defsubst artist-coord-get-saved-char (coord) | |
2373 | "Retrieve the saved char component of a COORD." | |
2374 | (aref coord 2)) | |
2375 | ||
2376 | (defsubst artist-coord-get-new-char (coord) | |
2377 | "Retrieve the new char component of a COORD." | |
2378 | (aref coord 3)) | |
2379 | ||
2380 | (defsubst artist-coord-add-saved-char (coord saved-char) | |
2381 | "Set the saved char component of a COORD to SAVED-CHAR." | |
2382 | (aset coord 2 saved-char) | |
2383 | coord) | |
2384 | ||
2385 | (defsubst artist-coord-add-new-char (coord new-char) | |
2386 | "Set the new char component of a COORD to NEW-CHAR." | |
2387 | (aset coord 3 new-char) | |
2388 | coord) | |
2389 | ||
2390 | (defsubst artist-coord-set-new-char (coord new-char) | |
2391 | "Set the new char component of a COORD to NEW-CHAR." | |
2392 | (aset coord 3 new-char) | |
2393 | coord) | |
2394 | ||
2395 | ||
2396 | ;; Pretend we are plotting a pixel. Instead we just list it | |
2397 | ;; | |
2398 | (defmacro artist-put-pixel (point-list x y) | |
2399 | "In POINT-LIST, store a ``pixel'' at coord X,Y." | |
2400 | (list 'setq point-list | |
2401 | (list 'append point-list (list 'list (list 'artist-new-coord x y))))) | |
2402 | ||
2403 | ;; Calculate list of points using eight point algorithm | |
2404 | ;; return a list of coords | |
2405 | ;; | |
2406 | (defun artist-eight-point (x1 y1 x2 y2) | |
2407 | "Run the eight-point algorithm to get a list of coords from X1,Y1 to X2,Y2." | |
2408 | (let* ((point-list nil) | |
2409 | (octant (artist-find-octant x1 y1 x2 y2)) | |
2410 | (dfdx-coeff (artist-get-dfdx-init-coeff octant)) | |
2411 | (dfdy-coeff (artist-get-dfdy-init-coeff octant)) | |
2412 | (x-step-q>=0 (artist-get-x-step-q>=0 octant)) | |
2413 | (y-step-q>=0 (artist-get-y-step-q>=0 octant)) | |
2414 | (x-step-q<0 (artist-get-x-step-q<0 octant)) | |
2415 | (y-step-q<0 (artist-get-y-step-q<0 octant)) | |
2416 | (dfdx (- (- y2 y1))) | |
2417 | (dfdy (- x2 x1)) | |
2418 | (x x1) | |
2419 | (y y1) | |
2420 | (f 0) | |
2421 | (q (+ (* 2 f) | |
2422 | (* dfdx-coeff dfdx) | |
2423 | (* dfdy-coeff dfdy)))) | |
2424 | (artist-put-pixel point-list x y) | |
2425 | (while (or (not (eq x x2)) (not (eq y y2))) | |
2426 | (if (>= q 0) | |
2427 | (progn | |
2428 | (setq x (+ x x-step-q>=0)) | |
2429 | (setq y (+ y y-step-q>=0)) | |
2430 | (setq f (+ f (* x-step-q>=0 dfdx) (* y-step-q>=0 dfdy)))) | |
2431 | (progn | |
2432 | (setq x (+ x x-step-q<0)) | |
2433 | (setq y (+ y y-step-q<0)) | |
2434 | (setq f (+ f (* x-step-q<0 dfdx) (* y-step-q<0 dfdy))))) | |
2435 | (setq q (+ (* 2 f) (* dfdx-coeff dfdx) (* dfdy-coeff dfdy))) | |
2436 | (artist-put-pixel point-list x y)) | |
2437 | point-list)) | |
2438 | ||
2439 | ;; artist-save-chars-under-point-list | |
09e80d9f | 2440 | ;; Remembers the chars that were there before we did draw the line. |
b95b34e5 GM |
2441 | ;; Returns point-list. |
2442 | ;; | |
2443 | (defun artist-save-chars-under-point-list (point-list) | |
2444 | "Save characters originally under POINT-LIST." | |
2445 | (mapcar | |
2446 | (lambda (coord) | |
2447 | (artist-coord-add-saved-char | |
2448 | coord | |
2449 | (artist-get-char-at-xy (artist-coord-get-x coord) | |
2450 | (artist-coord-get-y coord)))) | |
2451 | point-list)) | |
2452 | ||
2453 | ;; artist-calculate-new-char, artist-calculate-new-chars | |
2454 | ;; Calculates which char to insert depending on direction of point-list. | |
2455 | ;; | |
2456 | ;; Depending on new-coord's position relative to last-coord one of the | |
2457 | ;; following chars are returned: \ | / - o, as indicated by this: | |
2458 | ;; | |
2459 | ;; \ | / | |
2460 | ;; - o - | |
2461 | ;; / | \ | |
2462 | ;; | |
2463 | ;; artist-calculate-new-char works on one coordinate, returns char. | |
2464 | ;; artist-calculate-new-chars works on a point-list, returns point-list. | |
2465 | ;; | |
2466 | (defun artist-calculate-new-char (last-coord new-coord) | |
2467 | "Return a line-char to use when moving from LAST-COORD to NEW-COORD." | |
2468 | (let ((last-x (artist-coord-get-x last-coord)) | |
2469 | (last-y (artist-coord-get-y last-coord)) | |
2470 | (new-x (artist-coord-get-x new-coord)) | |
2471 | (new-y (artist-coord-get-y new-coord))) | |
2472 | (cond ((> new-x last-x) (cond ((< new-y last-y) ?/ ) | |
2473 | ((> new-y last-y) ?\\ ) | |
2474 | (t ?- ))) | |
2475 | ((< new-x last-x) (cond ((< new-y last-y) ?\\ ) | |
2476 | ((> new-y last-y) ?/ ) | |
2477 | (t ?- ))) | |
2478 | ((eq new-y last-y) ?o) | |
2479 | (t ?| )))) | |
2480 | ||
2481 | (defun artist-calculate-new-chars (point-list) | |
2482 | "Return a list of coords with line-chars calculated. Input: POINT-LIST." | |
2483 | (if (null (cdr point-list)) | |
2484 | (list (artist-coord-add-new-char (car point-list) ?o )) | |
2485 | (let ((last-coord (car point-list))) | |
2486 | (cons (artist-coord-add-new-char | |
2487 | (car point-list) | |
2488 | (artist-calculate-new-char (car (cdr point-list)) | |
2489 | (car point-list))) | |
2490 | (mapcar | |
2491 | (lambda (this-coord) | |
2492 | (prog1 | |
2493 | (artist-coord-add-new-char | |
2494 | this-coord | |
2495 | (artist-calculate-new-char last-coord this-coord)) | |
2496 | (setq last-coord this-coord))) | |
2497 | (cdr point-list)))))) | |
2498 | ||
2499 | ;; artist-modify-new-chars | |
2500 | ;; Replaces some characters with some other characters. | |
2501 | ;; | |
2502 | ;; artist-modify-new-chars works on a point-list, returns point-list. | |
2503 | ;; | |
2504 | (defun artist-modify-new-chars (point-list) | |
2505 | "Replace intersecting characters in POINT-LIST. | |
2506 | This function returns a point-list." | |
2507 | (mapcar | |
2508 | (lambda (coord) | |
2509 | (let* ((new-c (artist-coord-get-new-char coord)) | |
2510 | (saved-c (artist-coord-get-saved-char coord)) | |
2511 | (modified-c (artist-intersection-char new-c saved-c))) | |
2512 | (artist-coord-set-new-char coord modified-c))) | |
2513 | point-list)) | |
2514 | ||
2515 | ||
2516 | ;; | |
4c36be58 | 2517 | ;; functions for accessing endpoints and elements in object requiring |
b95b34e5 GM |
2518 | ;; 2 endpoints |
2519 | ;; | |
2520 | ||
2521 | (defun artist-make-endpoint (x y) | |
2522 | "Create an endpoint at X, Y." | |
2523 | (let ((new-endpoint (make-vector 2 x))) | |
2524 | (aset new-endpoint 1 y) | |
2525 | new-endpoint)) | |
2526 | ||
2527 | (defun artist-endpoint-get-x (endpoint) | |
2528 | "Retrieve the x component of an ENDPOINT." | |
2529 | (aref endpoint 0)) | |
2530 | ||
2531 | (defun artist-endpoint-get-y (endpoint) | |
2532 | "Retrieve the y component of an ENDPOINT." | |
2533 | (aref endpoint 1)) | |
2534 | ||
2535 | (defun artist-make-2point-object (endpoint1 endpoint2 shapeinfo) | |
2536 | "Create a 2-point object of ENDPOINT1, ENDPOINT2 and SHAPEINFO." | |
2537 | (list endpoint1 endpoint2 shapeinfo)) | |
2538 | ||
2539 | (defun artist-2point-get-endpoint1 (obj) | |
2540 | "Retrieve the first endpoint of a 2-point object OBJ." | |
2541 | (elt obj 0)) | |
2542 | ||
2543 | (defun artist-2point-get-endpoint2 (obj) | |
2544 | "Retrieve the second endpoint of a 2-point object OBJ." | |
2545 | (elt obj 1)) | |
2546 | ||
2547 | (defun artist-2point-get-shapeinfo (obj) | |
2548 | "Retrieve the shapeinfo component of a 2-point object OBJ." | |
2549 | (elt obj 2)) | |
2550 | ||
2551 | ||
2552 | ;; | |
2553 | ;; Drawing and undrawing lines (any direction) | |
2554 | ;; | |
2555 | ||
2556 | (defun artist-draw-line (x1 y1 x2 y2) | |
98d92ce3 | 2557 | "Draw a line from X1, Y1 to X2, Y2. |
b95b34e5 GM |
2558 | |
2559 | Output is a line, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO). | |
2560 | ||
2561 | END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y]. | |
2562 | SHAPE-INFO is a list of vectors [X Y SAVED-CHAR NEW-CHAR]." | |
2563 | (let ((endpoint1 (artist-make-endpoint x1 y1)) | |
2564 | (endpoint2 (artist-make-endpoint x2 y2))) | |
2565 | (artist-make-2point-object | |
2566 | endpoint1 | |
2567 | endpoint2 | |
2568 | (mapcar | |
2569 | (lambda (coord) | |
2570 | (artist-move-to-xy (artist-coord-get-x coord) | |
2571 | (artist-coord-get-y coord)) | |
2572 | (if artist-line-char-set | |
2573 | (artist-replace-char artist-line-char) | |
2574 | (artist-replace-char (artist-coord-get-new-char coord))) | |
2575 | coord) | |
2576 | (artist-modify-new-chars | |
2577 | (artist-calculate-new-chars | |
2578 | (artist-save-chars-under-point-list | |
2579 | (artist-eight-point x1 y1 x2 y2)))))))) | |
2580 | ||
2581 | (defun artist-undraw-line (line) | |
98d92ce3 | 2582 | "Undraw LINE." |
b95b34e5 GM |
2583 | (mapcar |
2584 | (lambda (coord) | |
2585 | (artist-move-to-xy (artist-coord-get-x coord) | |
2586 | (artist-coord-get-y coord)) | |
2587 | (artist-replace-char (artist-coord-get-saved-char coord)) | |
2588 | coord) | |
2589 | (artist-2point-get-shapeinfo line))) | |
2590 | ||
2591 | ;; | |
2592 | ;; Drawing and undrawing straight lines | |
2593 | ;; | |
2594 | ||
2595 | (defun artist-draw-sline (x1 y1 x2 y2) | |
98d92ce3 | 2596 | "Draw a straight line from X1, Y1 to X2, Y2. |
b95b34e5 GM |
2597 | Straight lines are vertical, horizontal or diagonal lines. |
2598 | They are faster to draw and most often they are what you need | |
2599 | when drawing a simple image. | |
2600 | ||
2601 | Output is a straight line, which is a list on the form | |
b27a51db | 2602 | \(END-POINT-1 END-POINT-2 SHAPE-INFO). |
b95b34e5 GM |
2603 | |
2604 | END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y]. | |
2605 | SHAPE-INFO is a vector [START-X START-Y LENGTH-OF-LINE DIRECTION | |
2606 | ORIGINAL-CHAR-1 ORIGINAL-CHAR-2 ... ]." | |
2607 | (let* ((line (artist-save-chars-under-sline (artist-sline x1 y1 x2 y2))) | |
2608 | (x (aref line 0)) | |
2609 | (y (aref line 1)) | |
2610 | (length (+ (aref line 2) 4)) | |
2611 | (direction (aref line 3)) | |
2612 | (line-char (artist-direction-char direction)) | |
2613 | (i 4) | |
2614 | (endpoint1 (artist-make-endpoint x y)) | |
2615 | (endpoint2 nil)) | |
2616 | (while (< i length) | |
2617 | (artist-move-to-xy x y) | |
2618 | (if artist-line-char-set | |
2619 | (artist-replace-char artist-line-char) | |
2620 | (artist-replace-char (artist-intersection-char | |
2621 | line-char | |
2622 | (aref line i)))) | |
2623 | (if (not (< (1+ i) length)) | |
2624 | ;; This is the last element. Set the second endpoint | |
2625 | (setq endpoint2 (artist-make-endpoint x y))) | |
2626 | (setq x (+ x (artist-direction-step-x direction))) | |
2627 | (setq y (+ y (artist-direction-step-y direction))) | |
2628 | (setq i (1+ i))) | |
2629 | (artist-make-2point-object endpoint1 endpoint2 line))) | |
2630 | ||
2631 | ||
2632 | (defun artist-undraw-sline (line) | |
2633 | "Undraw a straight line LINE." | |
2634 | (if line | |
2635 | (let* ((shape-info (artist-2point-get-shapeinfo line)) | |
2636 | (x (aref shape-info 0)) | |
2637 | (y (aref shape-info 1)) | |
2638 | (length (+ (aref shape-info 2) 4)) | |
2639 | (direction (aref shape-info 3)) | |
2640 | (i 4)) | |
2641 | (while (< i length) | |
2642 | (artist-move-to-xy x y) | |
2643 | (artist-replace-char (aref shape-info i)) | |
2644 | (setq x (+ x (artist-direction-step-x direction))) | |
2645 | (setq y (+ y (artist-direction-step-y direction))) | |
2646 | (setq i (1+ i)))))) | |
2647 | ||
2648 | ||
2649 | ;; | |
2650 | ;; Drawing and undrawing rectangles and squares | |
2651 | ;; | |
2652 | ||
2653 | (defun artist-draw-rect (x1 y1 x2 y2) | |
98d92ce3 | 2654 | "Draw a rectangle with corners at X1, Y1 and X2, Y2. |
b95b34e5 GM |
2655 | |
2656 | Output is a rectangle, which is a list on the form | |
b27a51db | 2657 | \(END-POINT-1 END-POINT-2 SHAPE-INFO). |
b95b34e5 GM |
2658 | |
2659 | END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y]. | |
2660 | SHAPE-INFO is a list of four straight lines." | |
2661 | (let* ((artist-line-char (artist-compute-line-char)) | |
2662 | (artist-line-char-set artist-line-char) | |
2663 | (line1 (artist-draw-sline x1 y1 x2 y1)) | |
2664 | (line2 (artist-draw-sline x2 y1 x2 y2)) | |
2665 | (line3 (artist-draw-sline x2 y2 x1 y2)) | |
2666 | (line4 (artist-draw-sline x1 y2 x1 y1)) | |
2667 | (endpoint1 (artist-make-endpoint x1 y1)) | |
2668 | (endpoint2 (artist-make-endpoint x2 y2))) | |
2669 | (artist-make-2point-object endpoint1 | |
2670 | endpoint2 | |
2671 | (list line1 line2 line3 line4)))) | |
2672 | ||
2673 | (defun artist-undraw-rect (rectangle) | |
98d92ce3 | 2674 | "Undraw RECTANGLE." |
b95b34e5 GM |
2675 | (if rectangle |
2676 | (let ((shape-info (artist-2point-get-shapeinfo rectangle))) | |
2677 | (artist-undraw-sline (elt shape-info 3)) | |
2678 | (artist-undraw-sline (elt shape-info 2)) | |
2679 | (artist-undraw-sline (elt shape-info 1)) | |
2680 | (artist-undraw-sline (elt shape-info 0))))) | |
2681 | ||
2682 | ||
2683 | (defun artist-rect-corners-squarify (x1 y1 x2 y2) | |
2684 | "Compute square corners from rectangle corners at X1, Y1 and X2, Y2. | |
98d92ce3 JB |
2685 | The square's first corner will be X1, Y1. The position of the second |
2686 | corner depends on which of X2 and Y2 is most far away from X1, Y1." | |
b95b34e5 GM |
2687 | (let* ((delta-x (- x2 x1)) |
2688 | (delta-y (- y2 y1)) | |
2689 | (delta-x-sign (if (< delta-x 0) -1 1)) | |
2690 | (delta-y-sign (if (< delta-y 0) -1 1)) | |
2691 | (new-x2) ; set below | |
2692 | (new-y2)) ; set below | |
2693 | ||
2694 | ;; Check which of x2 and y2 is most distant | |
2695 | ;; take care to the aspect ratio | |
2696 | (if (> (abs delta-x) (abs delta-y)) | |
2697 | ||
2698 | ;; *** x2 more distant than y2 (with care taken to aspect ratio) | |
2699 | (progn | |
2700 | (setq new-x2 x2) | |
2701 | (setq new-y2 (+ y1 (round (/ (* (abs delta-x) delta-y-sign) | |
2702 | artist-aspect-ratio))))) | |
2703 | ||
2704 | ;; *** y2 more distant than x2 (with care taken to aspect ratio) | |
2705 | (progn | |
2706 | (setq new-x2 (round (+ x1 (* (* (abs delta-y) delta-x-sign) | |
2707 | artist-aspect-ratio)))) | |
2708 | (setq new-y2 y2))) | |
2709 | ||
2710 | ;; Return this | |
2711 | (list x1 y1 new-x2 new-y2))) | |
2712 | ||
2713 | ||
2714 | (defun artist-draw-square (x1 y1 x2 y2) | |
2715 | "Draw a square with corners at X1, Y1 and X2, Y2. | |
2716 | ||
2717 | Output is a square, which is a list on the form | |
b27a51db | 2718 | \(END-POINT-1 END-POINT-2 SHAPE-INFO). |
b95b34e5 GM |
2719 | |
2720 | END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y]. | |
2721 | SHAPE-INFO is a list of four straight lines." | |
2722 | (let* ((artist-line-char (artist-compute-line-char)) | |
2723 | (artist-line-char-set artist-line-char) | |
2724 | (square-corners (artist-rect-corners-squarify x1 y1 x2 y2)) | |
2725 | (new-x1 (elt square-corners 0)) | |
2726 | (new-y1 (elt square-corners 1)) | |
2727 | (new-x2 (elt square-corners 2)) | |
2728 | (new-y2 (elt square-corners 3)) | |
2729 | (endpoint1 (artist-make-endpoint new-x1 new-y1)) | |
2730 | (endpoint2 (artist-make-endpoint new-x2 new-y2)) | |
2731 | (line1 (artist-draw-sline new-x1 new-y1 new-x2 new-y1)) | |
2732 | (line2 (artist-draw-sline new-x2 new-y1 new-x2 new-y2)) | |
2733 | (line3 (artist-draw-sline new-x2 new-y2 new-x1 new-y2)) | |
2734 | (line4 (artist-draw-sline new-x1 new-y2 new-x1 new-y1))) | |
2735 | (artist-make-2point-object endpoint1 | |
2736 | endpoint2 | |
2737 | (list line1 line2 line3 line4)))) | |
2738 | ||
2739 | (defun artist-undraw-square (square) | |
98d92ce3 | 2740 | "Undraw SQUARE." |
b95b34e5 GM |
2741 | (if square |
2742 | (let ((shape-info (artist-2point-get-shapeinfo square))) | |
2743 | (artist-undraw-sline (elt shape-info 3)) | |
2744 | (artist-undraw-sline (elt shape-info 2)) | |
2745 | (artist-undraw-sline (elt shape-info 1)) | |
2746 | (artist-undraw-sline (elt shape-info 0))))) | |
2747 | ||
2748 | ;; | |
2749 | ;; Filling rectangles and squares | |
2750 | ;; | |
2751 | ||
2752 | (defun artist-fill-rect (rect x1 y1 x2 y2) | |
2753 | "Fill rectangle RECT from X1,Y1 to X2,Y2." | |
2754 | (let ((x (1+ (min x1 x2))) | |
2755 | (y (1+ (min y1 y2))) | |
2756 | (x-max (max x1 x2)) | |
2757 | (y-max (max y1 y2))) | |
2758 | (let ((w (- x-max x))) | |
2759 | (while (< y y-max) | |
2760 | (artist-move-to-xy x y) | |
2761 | (artist-replace-chars artist-fill-char w) | |
2762 | (setq y (1+ y)))))) | |
2763 | ||
2764 | (defun artist-fill-square (square x1 y1 x2 y2) | |
98d92ce3 | 2765 | "Fill a SQUARE from X1,Y1 to X2,Y2." |
b95b34e5 GM |
2766 | (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2)) |
2767 | (new-x1 (elt square-corners 0)) | |
2768 | (new-y1 (elt square-corners 1)) | |
2769 | (new-x2 (elt square-corners 2)) | |
2770 | (new-y2 (elt square-corners 3)) | |
2771 | (x (1+ (min new-x1 new-x2))) | |
2772 | (y (1+ (min new-y1 new-y2))) | |
2773 | (x-max (max new-x1 new-x2)) | |
2774 | (y-max (max new-y1 new-y2)) | |
2775 | (w (- x-max x))) | |
2776 | (while (< y y-max) | |
2777 | (artist-move-to-xy x y) | |
2778 | (artist-replace-chars artist-fill-char w) | |
2779 | (setq y (1+ y))))) | |
2780 | ||
2781 | ||
2782 | ;; | |
2783 | ;; Pen drawing | |
2784 | ;; | |
2785 | ||
2786 | (defun artist-pen (x1 y1) | |
98d92ce3 | 2787 | "Draw a character at X1, Y1. |
b95b34e5 GM |
2788 | The character is replaced with the character in `artist-fill-char'." |
2789 | (artist-move-to-xy x1 y1) | |
2790 | (artist-replace-char (if artist-line-char-set | |
2791 | artist-line-char | |
2792 | (if artist-fill-char-set | |
2793 | artist-fill-char | |
2794 | artist-default-fill-char)))) | |
2795 | ||
2796 | ||
2797 | (defun artist-pen-line (x1 y1) | |
98d92ce3 | 2798 | "Draw a line from last pen position to X1, Y1. |
b95b34e5 GM |
2799 | The character is replaced with the character in `artist-fill-char'. |
2800 | This will store all points in `artist-key-poly-point-list' in reversed | |
2801 | order (I assume it is faster to cons to the beginning of the list than | |
2802 | to append to the end of the list, when doing free-hand drawing)." | |
2803 | (let ((artist-line-char (if artist-line-char-set | |
2804 | artist-line-char | |
2805 | (if artist-fill-char-set | |
2806 | artist-fill-char | |
2807 | artist-default-fill-char)))) | |
2808 | ||
2809 | ;; Draw line from last point to this | |
2810 | (let ((x-last (car (car artist-key-poly-point-list))) | |
2811 | (y-last (cdr (car artist-key-poly-point-list)))) | |
2812 | (artist-move-to-xy x-last y-last) | |
2813 | (artist-replace-char artist-line-char) | |
2814 | (artist-draw-line x-last y-last x1 y1)) | |
2815 | ||
2816 | ;; Update the point-list | |
2817 | (setq artist-key-poly-point-list | |
2818 | (cons (cons x1 y1) artist-key-poly-point-list)))) | |
2819 | ||
2820 | (defun artist-pen-reset-last-xy (x1 y1) | |
2821 | "Reset the last x and y points to X1, Y1 when doing pen-drawing." | |
2822 | (artist-clear-arrow-points) | |
2823 | (setq artist-key-poly-point-list (list (cons x1 y1)))) | |
2824 | ||
2825 | ||
2826 | (defun artist-pen-set-arrow-points (x1 y1) | |
2827 | "Set arrow points for pen drawing using X1, Y1. | |
2828 | Also, the `artist-key-poly-point-list' is reversed." | |
2829 | ||
2830 | (setq artist-key-poly-point-list | |
2831 | (artist-uniq artist-key-poly-point-list)) | |
2832 | ||
2833 | (if (>= (length artist-key-poly-point-list) 2) | |
2834 | ||
2835 | ;; Only set arrow-points if the point-list has two or more entries | |
2836 | (let ((xn (car (car artist-key-poly-point-list))) | |
2837 | (yn (cdr (car artist-key-poly-point-list))) | |
2838 | (xn-1 (car (car (cdr artist-key-poly-point-list)))) | |
2839 | (yn-1 (cdr (car (cdr artist-key-poly-point-list)))) | |
2840 | (dirn)) ; direction for point n | |
2841 | (setq artist-key-poly-point-list (reverse artist-key-poly-point-list)) | |
2842 | (let ((x0 (car (car artist-key-poly-point-list))) | |
2843 | (y0 (cdr (car artist-key-poly-point-list))) | |
2844 | (x1 (car (car (cdr artist-key-poly-point-list)))) | |
2845 | (y1 (cdr (car (cdr artist-key-poly-point-list)))) | |
2846 | (dir0)) ; direction for point 0 | |
2847 | (setq dir0 (artist-find-direction x1 y1 x0 y0)) | |
2848 | (setq dirn (artist-find-direction xn-1 yn-1 xn yn)) | |
2849 | (setq artist-arrow-point-1 (artist-make-arrow-point x0 y0 dir0)) | |
2850 | (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn)))))) | |
2851 | ||
2852 | ||
2853 | ;; | |
2854 | ;; Text rendering | |
2855 | ;; | |
2856 | (defun artist-figlet-run (text font extra-args) | |
2857 | "Run figlet rendering TEXT using FONT. | |
2858 | EXTRA-ARGS for figlet, for the command line, may be specified." | |
2859 | (let* ((figlet-args (cond ((and font extra-args) | |
2860 | (cons (concat "-f" font) | |
2861 | (artist-string-split extra-args "[ \t]+"))) | |
2862 | (font (concat "-f" font)) | |
2863 | (extra-args | |
2864 | (artist-string-split extra-args "[ \t]+")) | |
2865 | (t nil))) | |
2866 | (figlet-output (artist-system artist-figlet-program text figlet-args)) | |
2867 | (exit-code (elt figlet-output 0)) | |
2868 | (stdout (elt figlet-output 1)) | |
2869 | (stderr (elt figlet-output 2))) | |
2870 | (if (not (= exit-code 0)) | |
2871 | (error "Failed to render font: %s (%d)" stderr exit-code)) | |
2872 | stdout)) | |
2873 | ||
2874 | (defun artist-figlet-get-font-list () | |
2875 | "Read fonts in with the shell command. | |
2876 | Returns a list of strings." | |
2877 | (let* ((cmd-interpreter "/bin/sh") | |
2878 | (ls-cmd artist-figlet-list-fonts-command) | |
2879 | (result (artist-system cmd-interpreter ls-cmd nil)) | |
2880 | (exit-code (elt result 0)) | |
2881 | (stdout (elt result 1)) | |
2882 | (stderr (elt result 2))) | |
2883 | (if (not (= exit-code 0)) | |
2884 | (error "Failed to read available fonts: %s (%d)" stderr exit-code)) | |
2885 | (artist-string-split stdout ".flf\n"))) | |
2886 | ||
2887 | (defun artist-figlet-choose-font () | |
2888 | "Read any extra arguments for figlet." | |
2889 | (interactive) | |
2890 | (let* ((avail-fonts (artist-figlet-get-font-list)) | |
5b76833f | 2891 | (font (completing-read (concat "Select font (default " |
b95b34e5 | 2892 | artist-figlet-default-font |
5b76833f | 2893 | "): ") |
b95b34e5 GM |
2894 | (mapcar |
2895 | (lambda (font) (cons font font)) | |
2896 | avail-fonts)))) | |
2897 | (if (string= font "") artist-figlet-default-font font))) | |
2898 | ||
2899 | (defun artist-figlet-get-extra-args () | |
2900 | "Read any extra arguments for figlet." | |
e327a62d | 2901 | (let ((extra-args (read-string "Extra args to figlet: "))) |
b95b34e5 GM |
2902 | (if (string= extra-args "") |
2903 | nil | |
2904 | extra-args))) | |
2905 | ||
2906 | (defun artist-figlet (text) | |
2907 | "Render TEXT using figlet." | |
2908 | (let* ((figlet-font (artist-figlet-choose-font)) | |
2909 | (figlet-extra-args (artist-figlet-get-extra-args))) | |
2910 | (artist-figlet-run text figlet-font figlet-extra-args))) | |
2911 | ||
2912 | ||
2913 | (defun artist-text-insert-common (x y text see-thru) | |
2914 | "At position X, Y, insert text TEXT. | |
98d92ce3 | 2915 | If SEE-THRU is non-nil, then blanks in TEXT do not replace text |
b95b34e5 GM |
2916 | in the buffer." |
2917 | (let* ((string-list (artist-string-split text "\n")) | |
2918 | (i 0) | |
2919 | (len (length string-list))) | |
2920 | (while (< i len) | |
2921 | (artist-move-to-xy x (+ y i)) | |
2922 | (artist-replace-string (car string-list) see-thru) | |
2923 | (setq string-list (cdr string-list)) | |
2924 | (setq i (1+ i))))) | |
a9645a66 | 2925 | |
b95b34e5 GM |
2926 | (defun artist-text-insert-see-thru (x y text) |
2927 | "At position X, Y, insert text TEXT. | |
2928 | Let text already in buffer shine thru the TEXT inserted." | |
2929 | (artist-text-insert-common x y text t)) | |
2930 | ||
2931 | (defun artist-text-insert-overwrite (x y text) | |
2932 | "At position X, Y, insert text TEXT. | |
2933 | Let blanks in TEXT overwrite any text already in the buffer." | |
2934 | (artist-text-insert-common x y text nil)) | |
2935 | ||
2936 | (defun artist-text-see-thru (x y) | |
2937 | "Prompt for text to render, render it at X,Y. | |
f904adbb RW |
2938 | This is done by calling the function specified by |
2939 | `artist-text-renderer-function', which must return a list of strings, | |
2940 | to be inserted in the buffer. | |
b95b34e5 GM |
2941 | |
2942 | Text already in the buffer ``shines thru'' blanks in the rendered text." | |
e327a62d | 2943 | (let* ((input-text (read-string "Type text to render: ")) |
f904adbb | 2944 | (rendered-text (artist-funcall artist-text-renderer-function input-text))) |
b95b34e5 GM |
2945 | (artist-text-insert-see-thru x y rendered-text))) |
2946 | ||
2947 | ||
2948 | (defun artist-text-overwrite (x y) | |
2949 | "Prompt for text to render, render it at X,Y. | |
f904adbb RW |
2950 | This is done by calling the function specified by |
2951 | `artist-text-renderer-function', which must return a list of strings, | |
2952 | to be inserted in the buffer. | |
b95b34e5 | 2953 | |
98d92ce3 | 2954 | Blanks in the rendered text overwrite any text in the buffer." |
e327a62d | 2955 | (let* ((input-text (read-string "Type text to render: ")) |
f904adbb | 2956 | (rendered-text (artist-funcall artist-text-renderer-function input-text))) |
b95b34e5 GM |
2957 | (artist-text-insert-overwrite x y rendered-text))) |
2958 | ||
2959 | ;; | |
2960 | ;; Spraying | |
a9645a66 | 2961 | ;; |
b95b34e5 GM |
2962 | |
2963 | (defun artist-spray-get-interval () | |
98d92ce3 | 2964 | "Retrieve the interval for repeated spray." |
b95b34e5 GM |
2965 | artist-spray-interval) |
2966 | ||
2967 | (defun artist-spray-random-points (n radius) | |
2968 | "Generate N random points within a radius of RADIUS. | |
2969 | Returns a list of points. Each point is on the form (X1 . Y1)." | |
2970 | (let ((points)) | |
2971 | (while (> n 0) | |
9e0d4f9e | 2972 | (let* ((angle (* (random 359) (/ float-pi 180))) |
b95b34e5 GM |
2973 | (dist (random radius)) |
2974 | (point (cons (round (* dist (cos angle))) | |
2975 | (round (* dist (sin angle)))))) | |
2976 | (setq points (cons point points))) | |
2977 | (setq n (- n 1))) | |
2978 | points)) | |
2979 | ||
2980 | (defun artist-spray (x1 y1) | |
2981 | "Spray at X1, Y1." | |
2982 | (let* ((num-points (* artist-spray-radius artist-spray-radius)) | |
2983 | (spray-points (artist-spray-random-points num-points | |
2984 | artist-spray-radius))) | |
2985 | (while spray-points | |
2986 | ;; Replace one spray point | |
2987 | (let* ((point (car spray-points)) | |
2988 | (x (+ x1 (car point))) | |
2989 | (y (+ y1 (cdr point))) | |
2990 | (buf-c (artist-get-char-at-xy-conv x y)) | |
2991 | (this-c (memq buf-c artist-spray-chars)) | |
2992 | (next-c (cond ((null this-c) artist-spray-new-char) | |
2993 | ((null (cdr this-c)) (car this-c)) | |
2994 | (t (car (cdr this-c)))))) | |
2995 | (artist-move-to-xy x y) | |
2996 | (artist-replace-char next-c)) | |
2997 | ||
2998 | ;; Step to next spray point | |
2999 | (setq spray-points (cdr spray-points))))) | |
3000 | ||
3001 | (defun artist-spray-clear-circle (circle x1 y1 x2 y2) | |
98d92ce3 | 3002 | "Clear circle CIRCLE at X1, Y1 through X2, Y2." |
b95b34e5 GM |
3003 | (artist-undraw-circle circle)) |
3004 | ||
3005 | (defun artist-spray-set-radius (circle x1 y1 x2 y2) | |
3006 | "Set spray radius from CIRCLE at X1, Y1 through X2, Y2." | |
3007 | (let ((dx (- x2 x1)) | |
3008 | (dy (- y2 y1))) | |
3009 | (setq artist-spray-radius (round (sqrt (+ (* dx dx) (* dy dy))))) | |
3010 | (if (= 0 artist-spray-radius) | |
3011 | (setq artist-spray-radius 1)))) | |
3012 | ||
3013 | ;; | |
3014 | ;; Erasing | |
3015 | ;; | |
3016 | ||
3017 | (defun artist-erase-char (x1 y1) | |
98d92ce3 | 3018 | "Erase a character at X1, Y1. |
b95b34e5 GM |
3019 | The character is replaced with the character in `artist-erase-char'." |
3020 | (artist-move-to-xy x1 y1) | |
3021 | (artist-replace-char artist-erase-char)) | |
3022 | ||
3023 | (defun artist-erase-rect (rect x1 y1 x2 y2) | |
3024 | "Erase rectangle RECT from X1, Y1, X2, Y2." | |
3025 | (let ((artist-line-char-set t) | |
3026 | (artist-fill-char-set t) | |
3027 | (artist-line-char artist-erase-char) | |
3028 | (artist-fill-char artist-erase-char)) | |
3029 | (artist-draw-rect x1 y1 x2 y2) | |
3030 | (artist-fill-rect rect x1 y1 x2 y2))) | |
3031 | ||
3032 | ||
3033 | ;; | |
3034 | ;; Vaporizing (erasing) line and lines | |
3035 | ;; | |
3036 | ||
3037 | ||
3038 | (defun artist-vap-find-endpoint (x1 y1 step-x step-y accept-set reject-set) | |
3039 | "Find one endpoint for line through X1, Y1. | |
3040 | The endpoint is searched for in the direction defined by STEP-X, STEP-Y, | |
3041 | accepting characters in the list ACCEPT-SET, stopping immediately | |
3042 | when finding characters in the list REJECT-SET. Fuzziness, that is | |
3043 | the number of consecutive characters not in ACCEPT-SET to allow as | |
3044 | part of the line, is determined by the variable `artist-vaporize-fuzziness'. | |
3045 | An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)." | |
3046 | (let ((x x1) | |
3047 | (y y1) | |
3048 | (x-last x1) | |
3049 | (y-last y1) | |
3050 | (done nil)) | |
3051 | (while (not done) | |
3052 | (let ((c (artist-get-char-at-xy-conv x y))) | |
3053 | (cond ((memq c reject-set) | |
3054 | (setq done t)) | |
3055 | ||
3056 | ;; We found a character we are accepting as part of the line. | |
3057 | ;; Update position | |
3058 | ((memq c accept-set) | |
3059 | (setq x-last x | |
3060 | y-last y | |
3061 | x (+ x step-x) | |
3062 | y (+ y step-y)) | |
3063 | (if (or (< x 0) (< y 0)) ;stop at the edge | |
3064 | (setq done t))) | |
3065 | ||
3066 | ;; We found a character we are not accepting as part of | |
3067 | ;; the line Search `artist-vaporize-fuzziness' | |
3068 | ;; characters away from this position in the same | |
3069 | ;; direction to see if there are any characters in the | |
3070 | ;; accept-set. If not, we have found the endpoint. | |
3071 | (t | |
3072 | (let ((fuzziness artist-vaporize-fuzziness) | |
3073 | (x-tmp x) | |
3074 | (y-tmp y)) | |
3075 | ||
3076 | ;; while we have more fuzziness left and we have not | |
3077 | ;; found a character accepted as a line, move | |
3078 | ;; forward! | |
3079 | (while (and (> fuzziness 0) (not (memq c accept-set))) | |
3080 | (setq x-tmp (+ x-tmp step-x)) | |
3081 | (setq y-tmp (+ y-tmp step-y)) | |
3082 | (setq c (artist-get-char-at-xy-conv x-tmp y-tmp)) | |
3083 | (setq fuzziness (- fuzziness 1))) | |
3084 | (if (memq c accept-set) | |
3085 | ||
3086 | ;; The line continues on the other side of the | |
3087 | ;; not-accepted character. | |
3088 | (setq x x-tmp | |
3089 | y y-tmp) | |
3090 | ||
3091 | ;; Else: We couldn't find any line on the other side. | |
3092 | ;; That means we are done searching for the endpoint. | |
3093 | (setq done t))))))) | |
3094 | (cons x-last y-last))) | |
3095 | ||
3096 | ||
3097 | (defun artist-vap-find-endpoints-horiz (x y) | |
3098 | "Find endpoints for a horizontal line through X, Y. | |
3099 | An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)." | |
fe6e86db JB |
3100 | (list (artist-vap-find-endpoint x y 1 0 '(?- ?+) '(?\s)) |
3101 | (artist-vap-find-endpoint x y -1 0 '(?- ?+) '(?\s)))) | |
b95b34e5 GM |
3102 | |
3103 | (defun artist-vap-find-endpoints-vert (x y) | |
3104 | "Find endpoints for a vertical line through X, Y. | |
3105 | An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)." | |
fe6e86db JB |
3106 | (list (artist-vap-find-endpoint x y 0 1 '(?| ?+) '(?\s)) |
3107 | (artist-vap-find-endpoint x y 0 -1 '(?| ?+) '(?\s)))) | |
b95b34e5 GM |
3108 | |
3109 | (defun artist-vap-find-endpoints-swne (x y) | |
3110 | "Find endpoints for a diagonal line (made by /'s) through X, Y. | |
3111 | An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)." | |
fe6e86db JB |
3112 | (list (artist-vap-find-endpoint x y 1 -1 '(?/ ?X) '(?\s)) |
3113 | (artist-vap-find-endpoint x y -1 1 '(?/ ?X) '(?\s)))) | |
b95b34e5 GM |
3114 | |
3115 | (defun artist-vap-find-endpoints-nwse (x y) | |
3116 | "Find endpoints for a diagonal line (made by \\'s) through X, Y. | |
3117 | An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)." | |
fe6e86db JB |
3118 | (list (artist-vap-find-endpoint x y 1 1 '(?\\ ?X) '(?\s)) |
3119 | (artist-vap-find-endpoint x y -1 -1 '(?\\ ?X) '(?\s)))) | |
b95b34e5 GM |
3120 | |
3121 | ||
3122 | (defun artist-vap-find-endpoints (x y) | |
3123 | "Given a point X1, Y1, return a list of endpoints of lines through X, Y. | |
3124 | An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)." | |
3125 | (if artist-line-char-set | |
3126 | nil | |
3127 | (let ((c (artist-get-char-at-xy-conv x y))) | |
3128 | (cond ((eq c ?-) (artist-vap-find-endpoints-horiz x y)) | |
3129 | ((eq c ?|) (artist-vap-find-endpoints-vert x y)) | |
3130 | ((eq c ?/) (artist-vap-find-endpoints-swne x y)) | |
3131 | ((eq c ?\\) (artist-vap-find-endpoints-nwse x y)) | |
3132 | ((eq c ?+) (append (artist-vap-find-endpoints-horiz x y) | |
3133 | (artist-vap-find-endpoints-vert x y))) | |
3134 | ((eq c ?X) (append (artist-vap-find-endpoints-swne x y) | |
3135 | (artist-vap-find-endpoints-nwse x y))) | |
3136 | ||
3137 | ;; We don't know how to find directions when we are on | |
3138 | ;; another character | |
3139 | (t nil))))) | |
3140 | ||
3141 | ||
3142 | (defun artist-vap-group-in-pairs (l) | |
3143 | "Group elements in list L in pairs." | |
3144 | (cond ((null l) nil) | |
3145 | ((null (cdr l)) l) ; unevent number of elements in list | |
3146 | (t (append (list (list (car l) (car (cdr l)))) | |
3147 | (artist-vap-group-in-pairs (cdr (cdr l))))))) | |
3148 | ||
3149 | (defun artist-vaporize-by-endpoints (endpoint1 endpoint2) | |
3150 | "Given ENDPOINT1 and ENDPOINT2, vaporize the line between them. | |
3151 | An endpoint is a pair (X . Y)." | |
3152 | (let* ((x1 (car endpoint1)) | |
3153 | (y1 (cdr endpoint1)) | |
3154 | (x2 (car endpoint2)) | |
3155 | (y2 (cdr endpoint2)) | |
3156 | (dir (artist-find-direction x1 y1 x2 y2)) | |
3157 | (x-step (aref [1 1 0 -1 -1 -1 0 1] dir)) | |
3158 | (y-step (aref [0 1 1 1 0 -1 -1 -1] dir)) | |
3159 | (line-c (aref [?- ?\\ ?| ?/ ?- ?\\ ?| ?/] dir)) | |
3160 | (line-len (elt (list (abs (- x2 x1)) | |
3161 | (abs (- x2 x1)) | |
3162 | (abs (- y2 y1)) | |
3163 | (abs (- y2 y1)) | |
3164 | (abs (- x1 x2)) | |
3165 | (abs (- x1 x2)) | |
3166 | (abs (- y1 y2)) | |
3167 | (abs (- y1 y2))) | |
3168 | dir)) | |
3169 | (x x1) | |
3170 | (y y1)) | |
3171 | (while (>= line-len 0) | |
3172 | (let* ((buffer-c (artist-get-char-at-xy-conv x y)) | |
3173 | (new-c (artist-unintersection-char line-c buffer-c))) | |
3174 | (artist-move-to-xy x y) | |
3175 | (artist-replace-char new-c)) | |
3176 | (setq x (+ x x-step) | |
3177 | y (+ y y-step) | |
3178 | line-len (- line-len 1))))) | |
3179 | ||
3180 | ||
3181 | (defun artist-vaporize-line (x1 y1) | |
3182 | "Vaporize (erase) the straight line through X1, Y1. | |
3183 | Do this by replacing the characters that forms the line with | |
98d92ce3 JB |
3184 | `artist-erase-char'. Output is a list of endpoints for lines through |
3185 | X1, Y1. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)." | |
b95b34e5 | 3186 | (let ((endpoints (artist-vap-find-endpoints x1 y1))) |
b5242984 | 3187 | (mapc |
b95b34e5 GM |
3188 | (lambda (endpoints) |
3189 | (let ((ep1 (car endpoints)) | |
3190 | (ep2 (car (cdr endpoints)))) | |
3191 | (artist-vaporize-by-endpoints ep1 ep2))) | |
3192 | (artist-vap-group-in-pairs endpoints)) | |
3193 | endpoints)) | |
3194 | ||
3195 | ||
3196 | ;; Implementation note: This depends on artist-vaporize-line doing | |
3197 | ;; unintersections of intersecting lines. | |
3198 | ;; | |
3199 | ;; Example: | |
3200 | ;; Suppose the buffer looks like this and that we start vaporizing | |
3201 | ;; lines at (3,0) (at the ``*''). | |
3202 | ;; | |
3203 | ;; 0123456 | |
3204 | ;; 0+--*--+ | |
3205 | ;; 1| | | |
3206 | ;; 2| | | |
3207 | ;; 3+-----+ | |
3208 | ;; | |
3209 | ;; We will then push (0,0) and (6,0) on the stack, and vaporize the | |
3210 | ;; topmost horizontal line: | |
3211 | ;; | |
3212 | ;; 0123456 | |
3213 | ;; 0| | | |
3214 | ;; 1| | | |
3215 | ;; 2| | | |
3216 | ;; 3+-----+ | |
3217 | ;; | |
ee7683eb | 3218 | ;; We will then pop (0,0) and remove the left-most vertical line while |
b95b34e5 GM |
3219 | ;; pushing the lower left corner (0,3) on the stack, and so on until |
3220 | ;; the entire rectangle is vaporized. | |
3221 | ;; | |
3222 | ;; Now, What if the `+' in the upper left and upper right corners, | |
a9645a66 | 3223 | ;; had not been changed to `|' but to spaces instead? We would |
b95b34e5 GM |
3224 | ;; have failed when popping (0,0) and vaporizing that line because |
3225 | ;; we wouldn't find any line at (0,0): | |
a9645a66 | 3226 | ;; |
b95b34e5 | 3227 | ;; 0123456 |
a9645a66 | 3228 | ;; 0 |
b95b34e5 GM |
3229 | ;; 1| | |
3230 | ;; 2| | | |
3231 | ;; 3+-----+ | |
3232 | ;; | |
3233 | ;; That's why we depend on artist-vaporize-line doing unintersecting | |
3234 | ;; of crossing lines. There are alternative ways to handle this | |
3235 | ;; if it becomes too much a trouble. | |
3236 | ;; | |
3237 | (defun artist-vaporize-lines (x1 y1) | |
3238 | "Vaporize lines reachable from point X1, Y1." | |
3239 | (let ((ep-stack nil)) | |
b5242984 | 3240 | (mapc |
a4a5aa2b | 3241 | (lambda (ep) (push ep ep-stack)) |
b95b34e5 GM |
3242 | (artist-vap-find-endpoints x1 y1)) |
3243 | (while (not (null ep-stack)) | |
a4a5aa2b | 3244 | (let* ((vaporize-point (pop ep-stack)) |
b95b34e5 GM |
3245 | (new-endpoints (artist-vaporize-line (car vaporize-point) |
3246 | (cdr vaporize-point)))) | |
b5242984 | 3247 | (mapc |
a4a5aa2b | 3248 | (lambda (endpoint) (push endpoint ep-stack)) |
b95b34e5 GM |
3249 | new-endpoints))))) |
3250 | ||
3251 | ||
3252 | ;; | |
3253 | ;; Circles and ellipses | |
3254 | ;; | |
3255 | (defun artist-ellipse-generate-quadrant (x-radius y-radius) | |
3256 | "Create a point-list for first quadrant. | |
3257 | Points go from (X-RADIUS, 0) to (0, Y-RADIUS). | |
98d92ce3 | 3258 | Quadrant is generated around origin." |
b95b34e5 GM |
3259 | (let* ((rx2 (* x-radius x-radius)) |
3260 | (ry2 (* y-radius y-radius)) | |
3261 | (2rx2 (* 2 rx2)) | |
3262 | (2ry2 (* 2 ry2)) | |
3263 | (p) | |
3264 | (x 0) | |
3265 | (y y-radius) | |
3266 | (px 0) | |
3267 | (py (* 2rx2 y)) | |
3268 | (point-list nil)) | |
3269 | (artist-put-pixel point-list x y) | |
3270 | (setq p (round (+ ry2 (- (* rx2 y-radius)) (* 0.25 rx2)))) | |
3271 | (while (< px py) | |
3272 | (setq x (1+ x) | |
3273 | px (+ px 2ry2)) | |
3274 | (if (< p 0) | |
3275 | (setq p (+ p ry2 px)) | |
3276 | (setq y (- y 1) | |
3277 | py (- py 2rx2) | |
3278 | p (+ p ry2 px (- py)))) | |
3279 | (artist-put-pixel point-list x y)) | |
3280 | (setq p (round (+ (* ry2 (+ x 0.5) (+ x 0.5)) | |
3281 | (* rx2 (- y 1) (- y 1)) | |
3282 | (- (* rx2 ry2))))) | |
3283 | (while (> y 0) | |
3284 | (setq y (- y 1) | |
3285 | py (- py 2rx2)) | |
3286 | (if (> p 0) | |
3287 | (setq p (+ p rx2 (- py))) | |
3288 | (setq x (1+ x) | |
3289 | px (+ px 2ry2) | |
3290 | p (+ p rx2 (- py) px))) | |
3291 | (artist-put-pixel point-list x y)) | |
3292 | point-list)) | |
3293 | ||
3294 | (defsubst artist-new-fill-item (x y width) | |
3295 | "Create a new item at X, Y, with WIDTH. | |
3296 | This is for use in fill-info in ellipses and circles." | |
3297 | (let ((new-item (make-vector 3 x))) | |
3298 | (aset new-item 1 y) | |
3299 | (aset new-item 2 width) | |
3300 | new-item)) | |
3301 | ||
3302 | (defsubst artist-fill-item-get-x (fill-item) | |
3303 | "Retrieve the x component of a FILL-ITEM." | |
3304 | (aref fill-item 0)) | |
3305 | ||
3306 | (defsubst artist-fill-item-set-x (fill-item new-x) | |
3307 | "Set the x component of a FILL-ITEM to NEW-X." | |
3308 | (aset fill-item 0 new-x) | |
3309 | fill-item) | |
3310 | ||
3311 | (defsubst artist-fill-item-get-y (fill-item) | |
3312 | "Retrieve the y component of a FILL-ITEM." | |
3313 | (aref fill-item 1)) | |
3314 | ||
3315 | (defsubst artist-fill-item-set-y (fill-item new-y) | |
3316 | "Set the y component of a FILL-ITEM to NEW-Y." | |
3317 | (aset fill-item 1 new-y) | |
3318 | fill-item) | |
3319 | ||
3320 | (defsubst artist-fill-item-get-width (fill-item) | |
3321 | "Retrieve the width component of a FILL-ITEM." | |
3322 | (aref fill-item 2)) | |
3323 | ||
3324 | (defsubst artist-fill-item-set-width (fill-item new-width) | |
3325 | "Set the width component of a FILL-ITEM to NEW-WIDTH." | |
3326 | (aset fill-item 2 new-width) | |
3327 | fill-item) | |
3328 | ||
3329 | ||
3330 | (defun artist-ellipse-point-list-add-center (x-center y-center point-list) | |
3331 | "Add offsets X-CENTER and Y-CENTER to coordinates in POINT-LIST." | |
3332 | (mapcar | |
3333 | (lambda (p) | |
3334 | (artist-coord-set-x p (+ x-center (artist-coord-get-x p))) | |
3335 | (artist-coord-set-y p (+ y-center (artist-coord-get-y p)))) | |
3336 | point-list)) | |
3337 | ||
3338 | ||
3339 | (defun artist-ellipse-fill-info-add-center (x-center y-center fill-info) | |
3340 | "Add offsets X-CENTER and Y-CENTER to fill-items in FILL-INFO." | |
3341 | (mapcar | |
3342 | (lambda (p) | |
3343 | (artist-fill-item-set-x p (+ x-center (artist-fill-item-get-x p))) | |
3344 | (artist-fill-item-set-y p (+ y-center (artist-fill-item-get-y p)))) | |
3345 | fill-info)) | |
3346 | ||
3347 | (defun artist-ellipse-remove-0-fills (fill-info) | |
3348 | "Remove fill-infos from FILL-INFO that fills a zero-width field." | |
3349 | (cond ((null fill-info) | |
3350 | nil) | |
3351 | ((= 0 (artist-fill-item-get-width (car fill-info))) | |
3352 | (artist-ellipse-remove-0-fills (cdr fill-info))) | |
3353 | (t | |
3354 | (append (list (car fill-info)) | |
3355 | (artist-ellipse-remove-0-fills (cdr fill-info)))))) | |
3356 | ||
3357 | ||
3358 | (defun artist-ellipse-compute-fill-info (point-list) | |
3359 | "Compute fill info for ellipse around 0,0 from POINT-LIST. | |
3360 | The POINT-LIST is expected to cover the first quadrant." | |
3361 | (let ((first-half nil) | |
3362 | (both-halves nil) | |
3363 | (last-y nil)) | |
3364 | ||
3365 | ;; Create first half (the lower one (since y grows downwards)) from | |
3366 | ;; the first quadrant. | |
b5242984 | 3367 | (mapc |
b95b34e5 GM |
3368 | (lambda (coord) |
3369 | (let* ((x (artist-coord-get-x coord)) | |
3370 | (y (artist-coord-get-y coord)) | |
3371 | (width (max (- (* 2 x) 1) 0)) | |
3372 | (left-edge (- x width))) | |
3373 | (if (or (null last-y) (not (= y last-y))) | |
3374 | ;; This was either the first time, | |
3375 | ;; or it was the first time on a new line | |
3376 | (setq first-half | |
3377 | (append first-half | |
3378 | ;; Fill info item starts at left-edge on line y | |
3379 | (list (artist-new-fill-item left-edge y width))))) | |
3380 | (setq last-y y))) | |
3381 | point-list) | |
3382 | ||
3383 | ;; Create the other half by mirroring the first half. | |
3384 | (setq both-halves | |
3385 | (append first-half | |
b5242984 | 3386 | (mapc |
b95b34e5 GM |
3387 | (lambda (i) |
3388 | (artist-new-fill-item (artist-fill-item-get-x i) | |
3389 | (- (artist-fill-item-get-y i)) | |
3390 | (artist-fill-item-get-width i))) | |
3391 | ;; The cdr below is so we don't include fill-info for | |
3392 | ;;; the middle line twice | |
3393 | (cdr (reverse first-half))))) | |
3394 | (artist-ellipse-remove-0-fills both-halves))) | |
3395 | ||
3396 | ||
3397 | (defun artist-ellipse-mirror-quadrant (point-list) | |
3398 | "Mirror a POINT-LIST describing first quadrant to create a complete ellipse." | |
3399 | (let ((right-half nil) | |
3400 | (left-half nil)) | |
3401 | ||
3402 | ;; First, if last char in that quadrant is `/', then replace it with `)' | |
3403 | ;; This way we avoids things | |
3404 | ;; --------- --------- | |
3405 | ;; / \ / \ | |
3406 | ;; that look like: \ / instead we get: ( ) | |
3407 | ;; \ / \ / | |
3408 | ;; --------- --------- | |
de4ce191 | 3409 | (let ((last-coord (car (last point-list)))) |
b95b34e5 GM |
3410 | (if (= (artist-coord-get-new-char last-coord) ?/) |
3411 | (artist-coord-set-new-char last-coord artist-ellipse-right-char))) | |
3412 | ||
3413 | ;; Create the other part of the right half by mirroring the first part | |
3414 | (setq right-half | |
3415 | (append | |
3416 | point-list | |
3417 | (mapcar | |
3418 | (lambda (coord) | |
3419 | (let ((c (artist-coord-get-new-char coord))) | |
3420 | (artist-new-coord (artist-coord-get-x coord) | |
3421 | (- (artist-coord-get-y coord)) | |
3422 | (cond ((= c ?/) ?\\) | |
3423 | ((= c ?\\) ?/) | |
3424 | (t c))))) | |
3425 | ;; The cdr below is so we don't draw the middle right char twice | |
3426 | (cdr (reverse point-list))))) | |
3427 | ||
3428 | ;; Create the left half by mirroring the right half. | |
3429 | (setq left-half | |
3430 | (mapcar | |
3431 | (lambda (coord) | |
3432 | (let ((c (artist-coord-get-new-char coord))) | |
3433 | (artist-new-coord (- (artist-coord-get-x coord)) | |
3434 | (artist-coord-get-y coord) | |
3435 | (cond ((= c ?/) ?\\) | |
3436 | ((= c ?\\) ?/) | |
3437 | ((= c artist-ellipse-right-char) | |
3438 | artist-ellipse-left-char) | |
3439 | (t c))))) | |
3440 | ;; The cdr and butlast below is so we don't draw the middle top | |
3441 | ;; and middle bottom char twice. | |
a4a5aa2b | 3442 | (butlast (cdr (reverse right-half))))) |
b95b34e5 GM |
3443 | (append right-half left-half))) |
3444 | ||
3445 | ||
5445d287 JB |
3446 | (defun artist-draw-ellipse-general (x1 y1 x-radius y-radius) |
3447 | "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS. | |
b95b34e5 GM |
3448 | |
3449 | Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO). | |
3450 | ||
3451 | END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y]. | |
3452 | SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO]. | |
3453 | ||
3454 | POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR]. | |
3455 | FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]. | |
3456 | ||
5445d287 | 3457 | Ellipses with zero Y-RADIUS are not drawn correctly." |
b95b34e5 GM |
3458 | (let* ((point-list (artist-ellipse-generate-quadrant x-radius y-radius)) |
3459 | (fill-info (artist-ellipse-compute-fill-info point-list)) | |
3460 | (shape-info (make-vector 2 0))) | |
3461 | ||
3462 | (setq point-list (artist-calculate-new-chars point-list)) | |
3463 | (setq point-list (artist-ellipse-mirror-quadrant point-list)) | |
5445d287 JB |
3464 | (setq point-list (artist-ellipse-point-list-add-center x1 y1 point-list)) |
3465 | (setq fill-info (artist-ellipse-fill-info-add-center x1 y1 fill-info)) | |
b95b34e5 GM |
3466 | |
3467 | ;; Draw the ellipse | |
3468 | (setq point-list | |
3469 | (mapcar | |
3470 | (lambda (coord) | |
3471 | (artist-move-to-xy (artist-coord-get-x coord) | |
3472 | (artist-coord-get-y coord)) | |
3473 | (if artist-line-char-set | |
3474 | (artist-replace-char artist-line-char) | |
3475 | (artist-replace-char (artist-coord-get-new-char coord))) | |
3476 | coord) | |
3477 | (artist-modify-new-chars | |
3478 | (artist-save-chars-under-point-list point-list)))) | |
3479 | ||
3480 | (aset shape-info 0 point-list) | |
3481 | (aset shape-info 1 fill-info) | |
5445d287 | 3482 | (artist-make-2point-object (artist-make-endpoint x1 y1) |
b95b34e5 GM |
3483 | (artist-make-endpoint x-radius y-radius) |
3484 | shape-info))) | |
3485 | ||
5445d287 JB |
3486 | (defun artist-draw-ellipse-with-0-height (x1 y1 x-radius y-radius) |
3487 | "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS. | |
b95b34e5 GM |
3488 | |
3489 | Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO). | |
3490 | ||
3491 | END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y]. | |
3492 | SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO]. | |
3493 | ||
3494 | POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR]. | |
3495 | FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]. | |
3496 | ||
5445d287 | 3497 | The Y-RADIUS must be 0, but the X-RADIUS must not be 0." |
b95b34e5 GM |
3498 | (let ((point-list nil) |
3499 | (width (max (- (abs (* 2 x-radius)) 1))) | |
5445d287 | 3500 | (left-edge (1+ (- x1 (abs x-radius)))) |
b95b34e5 GM |
3501 | (line-char (if artist-line-char-set artist-line-char ?-)) |
3502 | (i 0) | |
3503 | (point-list nil) | |
3504 | (fill-info nil) | |
3505 | (shape-info (make-vector 2 0))) | |
3506 | (while (< i width) | |
3507 | (let* ((line-x (+ left-edge i)) | |
5445d287 | 3508 | (line-y y1) |
b95b34e5 GM |
3509 | (new-coord (artist-new-coord line-x line-y))) |
3510 | (artist-coord-add-saved-char new-coord | |
3511 | (artist-get-char-at-xy line-x line-y)) | |
3512 | (artist-move-to-xy line-x line-y) | |
3513 | (artist-replace-char line-char) | |
3514 | (setq point-list (append point-list (list new-coord))) | |
3515 | (setq i (1+ i)))) | |
3516 | (aset shape-info 0 point-list) | |
3517 | (aset shape-info 1 fill-info) | |
5445d287 | 3518 | (artist-make-2point-object (artist-make-endpoint x1 y1) |
b95b34e5 GM |
3519 | (artist-make-endpoint x-radius y-radius) |
3520 | shape-info))) | |
3521 | ||
3522 | (defun artist-draw-ellipse (x1 y1 x2 y2) | |
3523 | "Draw an ellipse with center at X1, Y1 and point X2,Y2. | |
3524 | ||
3525 | Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO). | |
3526 | ||
3527 | END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y]. | |
3528 | SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO]. | |
3529 | ||
3530 | POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR]. | |
3531 | FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]." | |
3532 | (let* ((artist-line-char (artist-compute-line-char)) | |
3533 | (artist-line-char-set artist-line-char) | |
3534 | (width (abs (- x2 x1))) | |
3535 | (height (abs (- y2 y1))) | |
3536 | ;; | |
3537 | ;; When we draw our ellipse, we want it to go through the cursor | |
3538 | ;; position, but since x1,y1, x2,y2 marks the corners of one | |
3539 | ;; of the quadrants, we have to enlarge the ellipse a bit. | |
3540 | ;; Ok, so then why by sqrt(2)? | |
3541 | ;; It comes from the equation for the ellipse (where a is the | |
3542 | ;; x-radius and b is the y-radius): | |
3543 | ;; f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0 | |
3544 | ;; and the fact that we want the enlarged ellipse to have the | |
3545 | ;; same proportions as the smaller square, therefore we have: | |
3546 | ;; a/b = x/y | |
3547 | ;; Solving this yields a-in-larger-ellipse = a-in-smaller * sqrt(2) | |
3548 | (x-radius (round (* width (sqrt 2)))) | |
3549 | (y-radius (round (* height (sqrt 2)))) | |
3550 | (x x1) | |
3551 | (y y1)) | |
3552 | (if (and (= y1 y2) (not (= x1 x2))) | |
3553 | (artist-draw-ellipse-with-0-height x y x-radius y-radius) | |
3554 | (artist-draw-ellipse-general x y x-radius y-radius)))) | |
3555 | ||
3556 | ||
3557 | (defun artist-undraw-ellipse (ellipse) | |
3558 | "Undraw ELLIPSE." | |
3559 | (if ellipse | |
3560 | (let ((point-list (aref (artist-2point-get-shapeinfo ellipse) 0))) | |
3561 | (mapcar | |
3562 | (lambda (coord) | |
3563 | (artist-move-to-xy (artist-coord-get-x coord) | |
3564 | (artist-coord-get-y coord)) | |
3565 | (artist-replace-char (artist-coord-get-saved-char coord)) | |
3566 | coord) | |
3567 | point-list)))) | |
3568 | ||
3569 | ||
3570 | (defun artist-draw-circle (x1 y1 x2 y2) | |
3571 | "Draw a circle with center at X1, Y1 and point X2,Y2. | |
3572 | ||
3573 | Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO). | |
3574 | ||
3575 | END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y]. | |
3576 | SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO]. | |
3577 | ||
3578 | POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR]. | |
3579 | FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]." | |
3580 | (let* ((artist-line-char (artist-compute-line-char)) | |
3581 | (artist-line-char-set artist-line-char) | |
3582 | (width (abs (- x2 x1))) | |
3583 | (height (abs (- y2 y1))) | |
3584 | ;; When drawing our circle, we want it to through the cursor | |
4c36be58 | 3585 | ;; just as when drawing the ellipse, but we have to take |
b95b34e5 GM |
3586 | ;; care for the aspect-ratio. |
3587 | ;; The equation for the ellipse (where a is the x-radius and | |
3588 | ;; b is the y-radius): | |
3589 | ;; f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0 | |
3590 | ;; together with the relationship | |
3591 | ;; a = aspect-ratio * b | |
3592 | ;; gives | |
3593 | ;; a = sqrt( x^2 + (aspect-ratio * y)^2 ) and | |
3594 | ;; b = a / aspect-ratio | |
3595 | (x-radius (round (sqrt (+ (* width width) | |
3596 | (* (* artist-aspect-ratio height) | |
3597 | (* artist-aspect-ratio height)))))) | |
3598 | (y-radius (round (/ x-radius artist-aspect-ratio)))) | |
3599 | (artist-draw-ellipse-general x1 y1 x-radius y-radius))) | |
3600 | ||
3601 | (defalias 'artist-undraw-circle 'artist-undraw-ellipse) | |
3602 | ||
3603 | ||
3604 | ; | |
3605 | ; Filling ellipses | |
3606 | ; | |
3607 | (defun artist-fill-ellipse (ellipse x y x-radius y-radius) | |
3608 | "Fill an ELLIPSE centered at X,Y with radius X-RADIUS and Y-RADIUS." | |
3609 | (let ((fill-info (aref (artist-2point-get-shapeinfo ellipse) 1))) | |
3610 | (mapcar | |
3611 | (lambda (fill-item) | |
3612 | (artist-move-to-xy (artist-fill-item-get-x fill-item) | |
3613 | (artist-fill-item-get-y fill-item)) | |
3614 | (artist-replace-chars artist-fill-char | |
3615 | (artist-fill-item-get-width fill-item)) | |
3616 | fill-item) | |
3617 | fill-info))) | |
3618 | ||
3619 | (defalias 'artist-fill-circle 'artist-fill-ellipse) | |
3620 | ||
3621 | ||
3622 | ;; | |
3623 | ;; Cutting, copying and pasting rectangles and squares | |
3624 | ;; (filling functions) | |
3625 | ;; | |
3626 | ||
3627 | (defun artist-cut-rect (rect x1 y1 x2 y2) | |
3628 | "Copy rectangle RECT drawn from X1, Y1 to X2, Y2, then clear it." | |
3629 | (artist-undraw-rect rect) | |
3630 | (artist-copy-generic x1 y1 x2 y2) | |
3631 | (artist-erase-rect rect x1 y1 x2 y2)) | |
3632 | ||
3633 | (defun artist-cut-square (square x1 y1 x2 y2) | |
3634 | "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (made square), then clears it." | |
3635 | (artist-undraw-square square) | |
3636 | (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2)) | |
3637 | (new-x1 (elt square-corners 0)) | |
3638 | (new-y1 (elt square-corners 1)) | |
3639 | (new-x2 (elt square-corners 2)) | |
3640 | (new-y2 (elt square-corners 3))) | |
3641 | (artist-copy-generic new-x1 new-y1 new-x2 new-y2) | |
3642 | (artist-erase-rect square new-x1 new-y1 new-x2 new-y2))) | |
3643 | ||
3644 | ||
3645 | (defun artist-get-buffer-contents-at-xy (x y width) | |
3646 | "Retrieve contents from the buffer at X, Y. WIDTH characters are returned." | |
3647 | (artist-move-to-xy x y) | |
3648 | (let ((here (point)) | |
3649 | (there (save-excursion (artist-move-to-xy (+ x width) y) (point)))) | |
3650 | (untabify here there) | |
3651 | (setq there (save-excursion (artist-move-to-xy (+ x width) y) (point))) | |
3652 | (buffer-substring here there))) | |
3653 | ||
3654 | ||
3655 | (defun artist-copy-generic (x1 y1 x2 y2) | |
3656 | "Copy a rectangular area with corners at X1, Y1 and X2, Y2. | |
3657 | Output is a copy buffer, a list of strings, representing the | |
3658 | original contents of that area in the buffer." | |
3659 | (let* ((x (min x1 x2)) | |
3660 | (y (min y1 y2)) | |
3661 | (x-max (max x1 x2)) | |
3662 | (y-max (max y1 y2)) | |
3663 | (w (+ (- x-max x) 1)) | |
3664 | (l nil)) | |
3665 | (while (<= y y-max) | |
3666 | (setq l (cons (artist-get-buffer-contents-at-xy x y w) l)) | |
3667 | (setq y (1+ y))) | |
3668 | (if artist-interface-with-rect | |
3669 | (setq killed-rectangle (reverse l)) | |
3670 | (setq artist-copy-buffer (reverse l))))) | |
3671 | ||
3672 | ||
3673 | (defun artist-copy-rect (rect x1 y1 x2 y2) | |
3674 | "Copy rectangle RECT drawn from X1, Y1 to X2, Y2." | |
3675 | (artist-undraw-rect rect) | |
3676 | (artist-copy-generic x1 y1 x2 y2)) | |
3677 | ||
3678 | (defun artist-copy-square (square x1 y1 x2 y2) | |
98d92ce3 | 3679 | "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (but made square)." |
b95b34e5 GM |
3680 | (artist-undraw-square square) |
3681 | (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2)) | |
3682 | (new-x1 (elt square-corners 0)) | |
3683 | (new-y1 (elt square-corners 1)) | |
3684 | (new-x2 (elt square-corners 2)) | |
3685 | (new-y2 (elt square-corners 3))) | |
3686 | (artist-copy-generic new-x1 new-y1 new-x2 new-y2))) | |
3687 | ||
3688 | (defun artist-paste (x y) | |
98d92ce3 | 3689 | "Paste the contents of the copy-buffer at X,Y." |
b95b34e5 GM |
3690 | (let ((copy-buf (if artist-interface-with-rect |
3691 | killed-rectangle | |
3692 | artist-copy-buffer))) | |
3693 | (if (not (null copy-buf)) | |
3694 | (while (not (null copy-buf)) | |
3695 | (artist-move-to-xy x y) | |
3696 | (artist-replace-string (car copy-buf)) | |
3697 | (setq copy-buf (cdr copy-buf)) | |
3698 | (setq y (1+ y))) | |
3699 | (message "Nothing to paste")))) | |
3700 | ||
3701 | ||
3702 | ;; | |
3703 | ;; Flood filling | |
3704 | ;; | |
3705 | (defun artist-ff-too-far-right (x) | |
3706 | "Determine if the position X is too far to the right." | |
3707 | (cond ((numberp artist-flood-fill-right-border) | |
3708 | (> x artist-flood-fill-right-border)) | |
3709 | ((eq artist-flood-fill-right-border 'window-width) | |
3710 | (> x (- (window-width) 2))) | |
3711 | ((eq artist-flood-fill-right-border 'fill-column) | |
3712 | (> x fill-column)) | |
3713 | (t (error "Invalid value for `artist-flood-fill-right-border'")))) | |
3714 | ||
3715 | (defun artist-ff-get-rightmost-from-xy (x y) | |
3716 | "Find the rightmost position in this run, starting at X, Y." | |
3717 | (save-excursion | |
3718 | (let ((char-at-xy (artist-get-char-at-xy-conv x y)) | |
3719 | (last-x x)) | |
3720 | (setq x (1+ x)) | |
3721 | (while (and (not (artist-ff-too-far-right x)) | |
3722 | (= char-at-xy (artist-get-char-at-xy-conv x y))) | |
3723 | (setq last-x x) | |
3724 | (setq x (1+ x))) | |
3725 | last-x))) | |
3726 | ||
3727 | (defun artist-ff-is-topmost-line (x y) | |
3728 | "Determine whether the position X,Y is on the topmost line or not." | |
3729 | (= y 0)) | |
3730 | ||
3731 | (defun artist-ff-is-bottommost-line (x y) | |
3732 | "Determine whether the position X,Y is on the bottommost line or not." | |
3733 | (save-excursion | |
3734 | (goto-char (point-max)) | |
3735 | (beginning-of-line) | |
3736 | (let ((last-line (artist-current-line))) | |
3737 | (if (= (point) (point-max)) | |
3738 | ||
3739 | ;; Last line is empty, don't paint on it, report previous line | |
3740 | ;; as last line | |
a9645a66 JB |
3741 | (>= y (- last-line 1)) |
3742 | (>= y last-line))))) | |
b95b34e5 GM |
3743 | |
3744 | (defun artist-flood-fill (x1 y1) | |
98d92ce3 | 3745 | "Flood-fill starting at X1, Y1. Fill with the char in `artist-fill-char'." |
b95b34e5 GM |
3746 | (let ((stack nil) |
3747 | (input-queue nil) | |
3748 | ;; We are flood-filling the area that has this character. | |
3749 | (c (artist-get-char-at-xy-conv x1 y1)) | |
3750 | (artist-fill-char (if artist-fill-char-set | |
3751 | artist-fill-char | |
3752 | artist-default-fill-char))) | |
3753 | ||
3754 | ;; Fill only if the fill-char is not the same as the character whose | |
3755 | ;; area we are about to fill, or, in other words, don't fill if we | |
3756 | ;; needn't. | |
3757 | (if (not (= c artist-fill-char)) | |
a4a5aa2b | 3758 | (push (artist-new-coord x1 y1) stack)) |
b95b34e5 GM |
3759 | |
3760 | (while (not (null stack)) | |
a4a5aa2b | 3761 | (let* ((coord (pop stack)) |
b95b34e5 GM |
3762 | (x (artist-coord-get-x coord)) |
3763 | (y (artist-coord-get-y coord)) | |
3764 | ||
3765 | ;; Here we keep track of the leftmost and rightmost position | |
3766 | ;; for this run | |
3767 | (x-leftmost 0) | |
3768 | (x-rightmost 0) | |
3769 | (last-x 0) | |
3770 | ||
3771 | ;; Remember if line above and below are accessible | |
3772 | ;; Lines below the last one, and prior to the first-one | |
3773 | ;; are not accessible. | |
3774 | (lines-above nil) | |
3775 | (lines-below nil) | |
3776 | ||
3777 | ;; Remember char for position on line above and below, so we | |
3778 | ;; can find the rightmost positions on the runs. | |
3779 | (last-c-above -1) | |
3780 | (last-c-below -1)) | |
3781 | ||
3782 | (setq x-rightmost (artist-ff-get-rightmost-from-xy x y)) | |
3783 | (setq lines-above (not (artist-ff-is-topmost-line x y))) | |
3784 | (setq lines-below (not (artist-ff-is-bottommost-line x y))) | |
3785 | (setq last-x x-rightmost) | |
3786 | (setq x x-rightmost) | |
3787 | ||
3788 | ;; Search line above, push rightmost positions of runs for that line | |
3789 | (while (and (>= x 0) (= c (artist-get-char-at-xy-conv x y))) | |
3790 | (if lines-above | |
3791 | (let ((c-above (artist-get-char-at-xy-conv x (- y 1)))) | |
3792 | (if (and (= c-above c) (/= c-above last-c-above)) | |
a4a5aa2b | 3793 | (push (artist-new-coord x (- y 1)) stack)) |
b95b34e5 GM |
3794 | (setq last-c-above c-above))) |
3795 | (setq last-x x) | |
3796 | (setq x (- x 1))) | |
3797 | ||
3798 | ;; Remember the left-most position on this run | |
3799 | (setq x-leftmost last-x) | |
3800 | ||
3801 | ;; Search line below, push rightmost positions of runs for that line | |
3802 | (setq x x-rightmost) | |
3803 | (while (>= x x-leftmost) | |
3804 | (if lines-below | |
3805 | (let ((c-below (artist-get-char-at-xy-conv x (1+ y)))) | |
3806 | (if (and (= c-below c) (/= c-below last-c-below)) | |
a4a5aa2b | 3807 | (push (artist-new-coord x (1+ y)) stack)) |
b95b34e5 GM |
3808 | (setq last-c-below c-below))) |
3809 | (setq x (- x 1))) | |
3810 | ||
3811 | (artist-move-to-xy x-leftmost y) | |
3812 | (artist-replace-chars artist-fill-char (1+ (- x-rightmost x-leftmost))) | |
3813 | ||
3814 | ;; If we are to show incrementally, we have to remove any pending | |
3815 | ;; input from the input queue, because processing of pending input | |
3816 | ;; always has priority over display updates (although this input | |
3817 | ;; won't be processed until we are done). Later on we will queue | |
3818 | ;; the input on the input queue again. | |
3819 | (if artist-flood-fill-show-incrementally | |
3820 | (progn | |
3821 | (if (input-pending-p) | |
3822 | (discard-input)) | |
3823 | (artist-update-display))))))) | |
3824 | ||
3825 | ;; | |
3826 | ;; Accessors to arrow-points | |
3827 | ;; | |
3828 | ||
3829 | (defun artist-make-arrow-point (x y direction &optional state) | |
3830 | "Create an arrow point at X, Y for a line in direction DIRECTION. | |
3831 | Optional argument STATE can be used to set state (default is nil)." | |
3832 | (save-excursion | |
3833 | (let* ((arrow-point (make-vector 4 0)) | |
3834 | (arrow-marker (make-marker))) | |
3835 | (artist-move-to-xy x y) | |
3836 | (set-marker arrow-marker (point)) | |
3837 | (aset arrow-point 0 arrow-marker) | |
3838 | (aset arrow-point 1 (artist-get-char-at-xy x y)) | |
3839 | (aset arrow-point 2 direction) | |
3840 | (aset arrow-point 3 state) | |
3841 | arrow-point))) | |
3842 | ||
3843 | (defsubst artist-arrow-point-get-marker (arrow-point) | |
3844 | "Retrieve the marker component of an ARROW-POINT." | |
3845 | (aref arrow-point 0)) | |
3846 | ||
3847 | (defsubst artist-arrow-point-get-orig-char (arrow-point) | |
3848 | "Retrieve the orig char component of an ARROW-POINT." | |
3849 | (aref arrow-point 1)) | |
3850 | ||
3851 | (defsubst artist-arrow-point-get-direction (arrow-point) | |
3852 | "Retrieve the direction component of an ARROW-POINT." | |
3853 | (aref arrow-point 2)) | |
3854 | ||
3855 | (defsubst artist-arrow-point-get-state (arrow-point) | |
3856 | "Retrieve the state component of an ARROW-POINT." | |
3857 | (aref arrow-point 3)) | |
3858 | ||
3859 | (defsubst artist-arrow-point-set-state (arrow-point new-state) | |
3860 | "Set the state component of an ARROW-POINT to NEW-STATE." | |
3861 | (aset arrow-point 3 new-state)) | |
3862 | ||
3863 | ||
3864 | (defun artist-clear-arrow-points () | |
3865 | "Clear current endpoints." | |
3866 | (setq artist-arrow-point-1 nil) | |
3867 | (setq artist-arrow-point-2 nil)) | |
3868 | ||
3869 | (defun artist-set-arrow-points-for-poly (point-list) | |
3870 | "Generic function for setting arrow-points for poly-shapes from POINT-LIST." | |
3871 | (let* ((ep1 (elt point-list 0)) | |
3872 | (ep2 (elt point-list 1)) | |
3873 | (x1 (artist-endpoint-get-x ep1)) | |
3874 | (y1 (artist-endpoint-get-y ep1)) | |
3875 | (x2 (artist-endpoint-get-x ep2)) | |
3876 | (y2 (artist-endpoint-get-y ep2)) | |
3877 | (dir1 (artist-find-direction x2 y2 x1 y1)) | |
de4ce191 TTN |
3878 | (epn (car (last point-list))) |
3879 | (epn-1 (car (last point-list 2))) | |
b95b34e5 GM |
3880 | (xn (artist-endpoint-get-x epn)) |
3881 | (yn (artist-endpoint-get-y epn)) | |
3882 | (xn-1 (artist-endpoint-get-x epn-1)) | |
3883 | (yn-1 (artist-endpoint-get-y epn-1)) | |
3884 | (dirn (artist-find-direction xn-1 yn-1 xn yn))) | |
3885 | (setq artist-arrow-point-1 (artist-make-arrow-point x1 y1 dir1)) | |
3886 | (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn)))) | |
3887 | ||
3888 | ||
3889 | (defun artist-set-arrow-points-for-2points (shape x1 y1 x2 y2) | |
3890 | "Generic function for setting arrow-points for 2-point shapes. | |
3891 | The 2-point shape SHAPE is drawn from X1, Y1 to X2, Y2." | |
3892 | (let* ((endpoint1 (artist-2point-get-endpoint1 shape)) | |
3893 | (endpoint2 (artist-2point-get-endpoint2 shape)) | |
3894 | (x1 (artist-endpoint-get-x endpoint1)) | |
3895 | (y1 (artist-endpoint-get-y endpoint1)) | |
3896 | (x2 (artist-endpoint-get-x endpoint2)) | |
3897 | (y2 (artist-endpoint-get-y endpoint2))) | |
3898 | (setq artist-arrow-point-1 | |
3899 | (artist-make-arrow-point x1 y1 | |
3900 | (artist-find-direction x2 y2 x1 y1))) | |
3901 | (setq artist-arrow-point-2 | |
3902 | (artist-make-arrow-point x2 y2 | |
3903 | (artist-find-direction x1 y1 x2 y2))))) | |
3904 | ||
3905 | ||
3906 | ;; | |
3907 | ;; Common routine for drawing/undrawing shapes based | |
3908 | ;; on the draw-how | |
3909 | ;; | |
3910 | ||
3911 | (defun artist-key-undraw-continously (x y) | |
4963739e JB |
3912 | "Undraw current continuous shape with point at X, Y." |
3913 | ;; No undraw-info for continuous shapes | |
b95b34e5 GM |
3914 | nil) |
3915 | ||
3916 | (defun artist-key-undraw-poly (x y) | |
3917 | "Undraw current poly shape with point at X, Y." | |
3918 | (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go)) | |
3919 | (x1 (artist-endpoint-get-x artist-key-endpoint1)) | |
3920 | (y1 (artist-endpoint-get-y artist-key-endpoint1))) | |
3921 | (artist-funcall undraw-fn artist-key-shape))) | |
3922 | ||
3923 | (defun artist-key-undraw-1point (x y) | |
3924 | "Undraw current 1-point shape at X, Y." | |
3925 | ;; No undraw-info for 1-point shapes | |
3926 | nil) | |
3927 | ||
3928 | (defun artist-key-undraw-2points (x y) | |
3929 | "Undraw current 2-point shape at X, Y." | |
3930 | (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go)) | |
3931 | (x1 (artist-endpoint-get-x artist-key-endpoint1)) | |
3932 | (y1 (artist-endpoint-get-y artist-key-endpoint1))) | |
3933 | (artist-funcall undraw-fn artist-key-shape))) | |
3934 | ||
3935 | (defun artist-key-undraw-common () | |
3936 | "Common routine undrawing current shape." | |
3937 | (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go)) | |
3938 | (col (artist-current-column)) | |
3939 | (row (artist-current-line))) | |
3940 | ||
3941 | ;; Depending on what we are currently drawing, call other routines | |
3942 | ;; that knows how to do the job | |
3943 | ;; | |
3944 | (cond ((eq draw-how 'artist-do-continously) | |
3945 | (artist-key-undraw-continously col row)) | |
3946 | ((eq draw-how 'artist-do-poly) | |
3947 | (artist-key-undraw-poly col row)) | |
3948 | ((and (numberp draw-how) (= draw-how 1)) | |
3949 | (artist-key-undraw-1point col row)) | |
3950 | ((and (numberp draw-how) (= draw-how 2)) | |
3951 | (artist-key-undraw-2points col row)) | |
3952 | (t (message "Undrawing \"%s\"s is not yet implemented" draw-how))) | |
3953 | ||
3954 | ;; Now restore the old position | |
3955 | ;; | |
3956 | (artist-move-to-xy col row))) | |
3957 | ||
3958 | ||
3959 | ||
3960 | ;; Implementation note: This really should honor the interval-fn entry | |
3961 | ;; in the master table, `artist-mt', which would mean leaving a timer | |
3962 | ;; that calls `draw-fn' every now and then. That timer would then have | |
c80e3b4a | 3963 | ;; to be canceled and reinstalled whenever the user moves the cursor. |
b95b34e5 GM |
3964 | ;; This could be done, but what if the user suddenly switches to another |
3965 | ;; drawing mode, or even kills the buffer! In the mouse case, it is much | |
3966 | ;; simpler: when at the end of `artist-mouse-draw-continously', the | |
c80e3b4a | 3967 | ;; user has released the button, so the timer will always be canceled |
b95b34e5 GM |
3968 | ;; at that point. |
3969 | (defun artist-key-draw-continously (x y) | |
4963739e | 3970 | "Draw current continuous shape at X,Y." |
b95b34e5 GM |
3971 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))) |
3972 | (setq artist-key-shape (artist-funcall draw-fn x y)))) | |
3973 | ||
3974 | (defun artist-key-draw-poly (x y) | |
98d92ce3 | 3975 | "Draw current poly-point shape with nth point at X,Y." |
b95b34e5 GM |
3976 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)) |
3977 | (x1 (artist-endpoint-get-x artist-key-endpoint1)) | |
3978 | (y1 (artist-endpoint-get-y artist-key-endpoint1))) | |
3979 | (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y)))) | |
3980 | ||
3981 | (defun artist-key-draw-1point (x y) | |
98d92ce3 | 3982 | "Draw current 1-point shape at X,Y." |
b95b34e5 GM |
3983 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))) |
3984 | (setq artist-key-shape (artist-funcall draw-fn x y)))) | |
3985 | ||
3986 | ||
3987 | (defun artist-key-draw-2points (x y) | |
98d92ce3 | 3988 | "Draw current 2-point shape at X,Y." |
b95b34e5 GM |
3989 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)) |
3990 | (x1 (artist-endpoint-get-x artist-key-endpoint1)) | |
3991 | (y1 (artist-endpoint-get-y artist-key-endpoint1))) | |
3992 | (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y)))) | |
3993 | ||
3994 | (defun artist-key-draw-common () | |
3995 | "Common routine for drawing current shape." | |
3996 | (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go)) | |
3997 | (col (artist-current-column)) | |
3998 | (row (artist-current-line))) | |
3999 | ||
4000 | ;; Depending on what we are currently drawing, call other routines | |
4001 | ;; that knows how to do the job | |
4002 | ;; | |
4003 | (cond ((eq draw-how 'artist-do-continously) | |
4004 | (artist-key-draw-continously col row)) | |
4005 | ((eq draw-how 'artist-do-poly) | |
4006 | (artist-key-draw-poly col row)) | |
4007 | ((and (numberp draw-how) (= draw-how 1)) | |
4008 | (artist-key-draw-1point col row)) | |
4009 | ((and (numberp draw-how) (= draw-how 2)) | |
4010 | (artist-key-draw-2points col row)) | |
4011 | (t (message "Drawing \"%s\"s is not yet implemented" draw-how))) | |
4012 | ||
4013 | ;; Now restore the old position | |
4014 | ;; | |
4015 | (artist-move-to-xy col row))) | |
4016 | ||
4017 | ||
4018 | ||
4019 | ;; | |
4020 | ;; Functions related to trimming line-endings | |
4021 | ;; The region between the topmost and bottommost visited line is | |
4022 | ;; called a draw-region. | |
4023 | ;; | |
4024 | ||
4025 | (defun artist-draw-region-reset () | |
4026 | "Reset the current draw-region." | |
4027 | (setq artist-draw-region-max-y 0) | |
4028 | (setq artist-draw-region-min-y 1000000)) | |
4029 | ||
4030 | (defun artist-draw-region-trim-line-endings (min-y max-y) | |
4031 | "Trim lines in current draw-region from MIN-Y to MAX-Y. | |
5445d287 | 4032 | Trimming here means removing white space at end of a line." |
9b053e76 | 4033 | ;; Safety check: switch min-y and max-y if max-y is smaller |
b95b34e5 GM |
4034 | (if (< max-y min-y) |
4035 | (let ((tmp min-y)) | |
4036 | (setq min-y max-y) | |
4037 | (setq max-y tmp))) | |
4038 | (save-excursion | |
4039 | (let ((curr-y min-y)) | |
4040 | (while (<= curr-y max-y) | |
4041 | (artist-move-to-xy 0 curr-y) | |
4042 | (end-of-line) | |
4043 | (delete-horizontal-space) | |
4044 | (setq curr-y (1+ curr-y)))))) | |
4045 | ||
4046 | ;; | |
4047 | ;; Drawing shapes by using keys | |
4048 | ;; | |
4049 | ||
4050 | (defun artist-key-do-continously-continously (x y) | |
4963739e | 4051 | "Update current continuous shape at X,Y." |
b95b34e5 GM |
4052 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))) |
4053 | (artist-funcall draw-fn x y))) | |
4054 | ||
4055 | ||
4056 | (defun artist-key-do-continously-poly (x y) | |
4057 | "Update current poly-point shape with nth point at X,Y." | |
4058 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)) | |
4059 | (undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go)) | |
4060 | (x1 (artist-endpoint-get-x artist-key-endpoint1)) | |
4061 | (y1 (artist-endpoint-get-y artist-key-endpoint1)) | |
4062 | (x2 x) | |
4063 | (y2 y)) | |
4064 | ;; If not rubber-banding, then move the 2 | |
4065 | ;; Otherwise re-draw the shape to the new position | |
4066 | ;; | |
4067 | (if (not artist-rubber-banding) | |
4068 | (progn | |
4069 | (artist-no-rb-unset-point2) | |
4070 | (artist-no-rb-set-point2 x y)) | |
4071 | (progn | |
4072 | (artist-funcall undraw-fn artist-key-shape) | |
4073 | (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2)))))) | |
4074 | ||
4075 | ||
4076 | (defun artist-key-do-continously-1point (x y) | |
4077 | "Update current 1-point shape at X,Y." | |
4963739e | 4078 | ;; Nothing to do continuously for operations |
b95b34e5 GM |
4079 | ;; where we have only one input point |
4080 | nil) | |
4081 | ||
4082 | (defun artist-key-do-continously-2points (x y) | |
4083 | "Update current 2-point shape with 2nd point at X,Y." | |
4084 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)) | |
4085 | (undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go)) | |
4086 | (x1 (artist-endpoint-get-x artist-key-endpoint1)) | |
4087 | (y1 (artist-endpoint-get-y artist-key-endpoint1)) | |
4088 | (x2 x) | |
4089 | (y2 y)) | |
4090 | ;; If not rubber-banding, then move the 2 | |
4091 | ;; Otherwise re-draw the shape to the new position | |
4092 | ;; | |
4093 | (if (not artist-rubber-banding) | |
4094 | (progn | |
4095 | (artist-no-rb-unset-point2) | |
4096 | (artist-no-rb-set-point2 x y)) | |
4097 | (progn | |
4098 | (artist-funcall undraw-fn artist-key-shape) | |
4099 | (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2)))))) | |
4100 | ||
4101 | ||
4102 | (defun artist-key-do-continously-common () | |
4103 | "Common routine for updating current shape." | |
4104 | (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go)) | |
4105 | (col (artist-current-column)) | |
4106 | (row (artist-current-line))) | |
4107 | ||
4108 | ;; Depending on what we are currently drawing, call other routines | |
4109 | ;; that knows how to do the job | |
4110 | ;; | |
4111 | (cond ((eq draw-how 'artist-do-continously) | |
4112 | (artist-key-do-continously-continously col row)) | |
4113 | ((eq draw-how 'artist-do-poly) | |
4114 | (artist-key-do-continously-poly col row)) | |
4115 | ((and (numberp draw-how) (= draw-how 1)) | |
4116 | (artist-key-do-continously-1point col row)) | |
4117 | ((and (numberp draw-how) (= draw-how 2)) | |
4118 | (artist-key-do-continously-2points col row)) | |
4119 | (t (message "Drawing \"%s\"s is not yet implemented" draw-how))) | |
4120 | ||
4121 | ;; Now restore the old position | |
4122 | ;; | |
4123 | (artist-move-to-xy col row))) | |
4124 | ||
4125 | ||
4126 | (defun artist-key-set-point-continously (x y) | |
4963739e JB |
4127 | "Set point for current continuous shape at X,Y." |
4128 | ;; Maybe set arrow-points for continuous shapes | |
b95b34e5 GM |
4129 | (let ((arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go)) |
4130 | (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)) | |
4131 | (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go)) | |
4132 | (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go)) | |
4133 | (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))) | |
4134 | ||
4135 | (if (not artist-key-is-drawing) | |
4136 | ;; *** We are about to begin drawing | |
4137 | (progn | |
4138 | (artist-funcall init-fn x y)) | |
4139 | ||
4140 | ;; *** We are about to stop drawing | |
4141 | (progn | |
4142 | ||
4143 | (artist-funcall prep-fill-fn x y) | |
4144 | (if (artist-funcall arrow-pred) | |
4145 | (artist-funcall arrow-set-fn x y) | |
4146 | (artist-clear-arrow-points)) | |
4147 | (artist-funcall exit-fn x y)))) | |
4148 | ||
4149 | ;; Toggle the is-drawing flag | |
4150 | (setq artist-key-is-drawing (not artist-key-is-drawing))) | |
4151 | ||
4152 | ||
4153 | ||
4154 | (defun artist-key-set-point-poly (x y &optional this-is-last-point) | |
4155 | "Set point for current poly-point shape at X,Y. | |
4156 | If optional argument THIS-IS-LAST-POINT is non-nil, this point is the last." | |
4157 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)) | |
4158 | (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go)) | |
4159 | (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go)) | |
4160 | (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go)) | |
4161 | (fill-pred (artist-go-get-fill-pred-from-symbol artist-curr-go)) | |
4162 | (fill-fn (artist-go-get-fill-fn-from-symbol artist-curr-go)) | |
4163 | (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go)) | |
4164 | (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go))) | |
4165 | ||
4166 | (if (not artist-key-is-drawing) | |
4167 | ||
4168 | ;; *** We were not drawing ==> set first point | |
4169 | (progn | |
4170 | ||
4171 | (artist-funcall init-fn x y) | |
4172 | ||
4173 | ;; If not rubber-banding, set first point. | |
4174 | ;; Otherwise, draw the shape from x,y to x,y | |
4175 | (if (not artist-rubber-banding) | |
4176 | (artist-no-rb-set-point1 x y) | |
4177 | (setq artist-key-shape (artist-funcall draw-fn x y x y))) | |
4178 | ||
4179 | ;; Set first endpoint | |
4180 | (setq artist-key-endpoint1 (artist-make-endpoint x y)) | |
4181 | ||
4182 | ;; Set point-list to contain start point | |
4183 | (setq artist-key-poly-point-list (list (artist-make-endpoint x y))) | |
4184 | ||
4185 | ;; Since we are not ready, set the arrow-points to nil | |
4186 | (artist-clear-arrow-points) | |
4187 | ||
4188 | ;; Change state to drawing | |
4189 | (setq artist-key-is-drawing t) | |
4190 | ||
4191 | ;; Feedback | |
5673af85 | 4192 | (message "%s" (substitute-command-keys |
b95b34e5 GM |
4193 | (concat "First point set. " |
4194 | "Set next with \\[artist-key-set-point], " | |
4195 | "set last with C-u \\[artist-key-set-point]")))) | |
4196 | ||
4197 | ||
4198 | ;; *** We were drawing ==> we are about to set nth point | |
4199 | ;; (last point if the argument this-is-last-point is non-nil) | |
4200 | ;; | |
4201 | (let ((x1 (artist-endpoint-get-x artist-key-endpoint1)) | |
4202 | (y1 (artist-endpoint-get-y artist-key-endpoint1)) | |
4203 | (x2 x) | |
4204 | (y2 y)) | |
4205 | ||
4206 | ;; If not rubber-banding, undraw the 1's and 2's, then | |
4207 | ;; draw the shape (if we were rubber-banding, then the | |
4208 | ;; shape is already drawn in artist-key-do-continously-2points.) | |
4209 | ;; | |
4210 | (if (not artist-rubber-banding) | |
4211 | (progn | |
4212 | (artist-no-rb-unset-points) | |
4213 | (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2)))) | |
4214 | ||
4215 | ;; Set x2 and y2 from shape's second point | |
4216 | ;; (which might be different from the mouse's second point, | |
4217 | ;; if, for example, we are drawing a straight line) | |
4218 | ;; | |
4219 | (if (not (null artist-key-shape)) | |
4220 | (let ((endpoint2 (artist-2point-get-endpoint2 artist-key-shape))) | |
4221 | (setq x2 (artist-endpoint-get-x endpoint2)) | |
4222 | (setq y2 (artist-endpoint-get-y endpoint2)))) | |
4223 | ||
4224 | ;; Add the endpoint to the list of poly-points | |
4225 | (setq artist-key-poly-point-list | |
4226 | (append artist-key-poly-point-list | |
4227 | (list (artist-make-endpoint x2 y2)))) | |
4228 | ||
4229 | ;; Now do handle the case when this is the last point, | |
4230 | ;; and the case when this point isn't the last | |
4231 | ;; | |
4232 | (if (not this-is-last-point) | |
4233 | ;; ** This is not the last point | |
4234 | (progn | |
4235 | ;; Start drawing a new 2-point-shape from last endpoint. | |
4236 | ||
4237 | ;; First set the start-point | |
4238 | (setq x1 x2) | |
4239 | (setq y1 y2) | |
4240 | (setq artist-key-endpoint1 (artist-make-endpoint x1 y1)) | |
4241 | ||
4242 | ;; If we are not rubber-banding, then place the '1 | |
4243 | ;; Otherwise, draw the shape from x1,y1 to x1,y1 | |
4244 | (if (not artist-rubber-banding) | |
4245 | (artist-no-rb-set-point1 x1 y1) | |
4246 | (setq artist-key-shape (artist-funcall draw-fn x1 y1 x1 y1))) | |
4247 | ||
4248 | ;; Feedback | |
4249 | (message "Point set")) | |
4250 | ||
4251 | ;; ** This is the last point | |
4252 | (progn | |
4253 | ||
4254 | (artist-funcall prep-fill-fn artist-key-poly-point-list) | |
4255 | ||
4256 | ;; Maybe fill | |
4257 | (if (artist-funcall fill-pred) | |
4258 | (artist-funcall fill-fn artist-key-shape | |
4259 | artist-key-poly-point-list)) | |
4260 | ||
4261 | ;; Set the arrow-points | |
4262 | (if (artist-funcall arrow-pred) | |
4263 | (artist-funcall arrow-set-fn artist-key-poly-point-list) | |
4264 | (artist-clear-arrow-points)) | |
4265 | ||
4266 | (artist-funcall exit-fn artist-key-poly-point-list) | |
4267 | ||
4268 | ;; Change state to not drawing | |
4269 | (setq artist-key-shape nil) | |
4270 | (setq artist-key-endpoint1 nil) | |
4271 | (setq artist-key-is-drawing nil))))))) | |
4272 | ||
4273 | ||
4274 | (defun artist-key-set-point-1point (x y) | |
4275 | "Set point for current 1-point shape at X,Y." | |
4276 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)) | |
4277 | (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go)) | |
4278 | (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go)) | |
4279 | (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go)) | |
4280 | (draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)) | |
4281 | (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go)) | |
4282 | (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go))) | |
4283 | (artist-funcall init-fn x y) | |
4284 | (artist-funcall draw-fn x y) | |
4285 | (artist-funcall prep-fill-fn x y) | |
4286 | (if (artist-funcall arrow-pred) | |
4287 | (artist-funcall arrow-set-fn x y) | |
4288 | (artist-clear-arrow-points)) | |
4289 | (artist-funcall exit-fn x y)) | |
4290 | (setq artist-key-shape nil) | |
4291 | (setq artist-key-is-drawing nil)) | |
4292 | ||
4293 | ||
4294 | (defun artist-key-set-point-2points (x y) | |
4295 | "Set first or second point in current 2-point shape at X,Y." | |
4296 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)) | |
4297 | (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go)) | |
4298 | (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go)) | |
4299 | (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go)) | |
4300 | (fill-pred (artist-go-get-fill-pred-from-symbol artist-curr-go)) | |
4301 | (fill-fn (artist-go-get-fill-fn-from-symbol artist-curr-go)) | |
4302 | (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go)) | |
4303 | (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go))) | |
4304 | (if (not artist-key-is-drawing) | |
4305 | ||
4306 | ;; *** We were not drawing ==> set first point | |
4307 | (progn | |
4308 | ||
4309 | (artist-funcall init-fn x y) | |
4310 | ||
4311 | ;; If not rubber-banding, set first point. | |
4312 | ;; Otherwise, draw the shape from x,y to x,y | |
4313 | (if (not artist-rubber-banding) | |
4314 | (artist-no-rb-set-point1 x y) | |
4315 | (setq artist-key-shape (artist-funcall draw-fn x y x y))) | |
4316 | ||
4317 | ;; Set first endpoint | |
4318 | (setq artist-key-endpoint1 (artist-make-endpoint x y)) | |
4319 | ||
4320 | ;; Since we are not ready, clear the arrow-points | |
4321 | (artist-clear-arrow-points) | |
4322 | ||
4323 | ;; Change state to drawing | |
4324 | (setq artist-key-is-drawing t)) | |
4325 | ||
4326 | ;; *** We were drawing ==> we are about to set 2nd point | |
4327 | ;; and end the drawing operation | |
4328 | ||
4329 | (let ((x1 (artist-endpoint-get-x artist-key-endpoint1)) | |
4330 | (y1 (artist-endpoint-get-y artist-key-endpoint1)) | |
4331 | (x2 x) | |
4332 | (y2 y)) | |
4333 | ||
4334 | ;; If not rubber-banding, undraw the 1's and 2's, then | |
4335 | ;; draw the shape (if we were rubber-banding, then the | |
4336 | ;; shape is already drawn in artist-key-do-continously-2points.) | |
4337 | ;; | |
4338 | (if (not artist-rubber-banding) | |
4339 | (progn | |
4340 | (artist-no-rb-unset-points) | |
4341 | (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2)))) | |
4342 | ||
4343 | (artist-funcall prep-fill-fn artist-key-shape x1 y1 x2 y2) | |
4344 | ||
4345 | ;; Maybe fill | |
4346 | ;; | |
4347 | (if (artist-funcall fill-pred) | |
4348 | (artist-funcall fill-fn artist-key-shape x1 y1 x2 y2)) | |
4349 | ||
4350 | ;; Maybe set the arrow-points | |
4351 | ;; | |
4352 | (if (artist-funcall arrow-pred) | |
4353 | (artist-funcall arrow-set-fn artist-key-shape x1 y1 x2 y2) | |
4354 | (artist-clear-arrow-points)) | |
4355 | ||
4356 | (artist-funcall exit-fn artist-key-shape x1 y1 x2 y2) | |
4357 | ||
4358 | ;; Change state to not drawing | |
4359 | (setq artist-key-is-drawing nil))))) | |
4360 | ||
4361 | ||
4362 | (defun artist-key-set-point-common (arg) | |
4363 | "Common routine for setting point in current shape. | |
5445d287 | 4364 | With non-nil ARG, set the last point." |
b95b34e5 GM |
4365 | (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go)) |
4366 | (col (artist-current-column)) | |
4367 | (row (artist-current-line)) | |
4368 | (was-drawing artist-key-is-drawing)) | |
4369 | ||
4370 | ;; First, if we are about to draw, then reset the draw-region | |
4371 | (if (not artist-key-is-drawing) | |
4372 | (artist-draw-region-reset)) | |
4373 | ||
4374 | ;; Depending on what we are currently drawing, call other routines | |
4375 | ;; that knows how to do the job | |
4376 | ;; | |
4377 | (cond ((eq draw-how 'artist-do-continously) | |
4378 | (artist-key-set-point-continously col row) | |
4379 | ;; Do this now, otherwise nothing will happen until we move. | |
4380 | (artist-key-do-continously-continously col row)) | |
4381 | ((eq draw-how 'artist-do-poly) | |
4382 | (artist-key-set-point-poly col row arg)) | |
4383 | ((and (numberp draw-how) (= draw-how 1)) | |
4384 | (artist-key-set-point-1point col row)) | |
4385 | ((and (numberp draw-how) (= draw-how 2)) | |
4386 | (artist-key-set-point-2points col row)) | |
4387 | (t (message "Drawing \"%s\"s is not yet implemented" draw-how))) | |
4388 | ||
4389 | ;; Maybe trim line endings | |
4390 | (if (and artist-trim-line-endings | |
4391 | was-drawing | |
4392 | (not artist-key-is-drawing)) | |
4393 | (artist-draw-region-trim-line-endings artist-draw-region-min-y | |
4394 | artist-draw-region-max-y)) | |
4395 | ||
4396 | ;; Now restore the old position | |
4397 | ;; | |
4398 | (artist-move-to-xy col row) | |
4399 | (artist-mode-line-show-curr-operation artist-key-is-drawing))) | |
4400 | ||
4401 | ;; | |
4402 | ;; Key navigation | |
4403 | ;; | |
4404 | ||
4405 | (defun artist-previous-line (&optional n) | |
98d92ce3 | 4406 | "Move cursor up N lines (default is 1), updating current shape. |
b95b34e5 GM |
4407 | If N is negative, move cursor down." |
4408 | (interactive "p") | |
4409 | (let ((col (artist-current-column))) | |
cb7bddf6 TTN |
4410 | (forward-line (- n)) |
4411 | (move-to-column col t)) | |
4412 | (when artist-key-is-drawing | |
4413 | (artist-key-do-continously-common))) | |
b95b34e5 GM |
4414 | |
4415 | ||
4416 | (defun artist-next-line (&optional n) | |
98d92ce3 | 4417 | "Move cursor down N lines (default is 1), updating current shape. |
b95b34e5 GM |
4418 | If N is negative, move cursor up." |
4419 | (interactive "p") | |
4420 | (let ((col (artist-current-column))) | |
cb7bddf6 TTN |
4421 | (forward-line n) |
4422 | (move-to-column col t)) | |
4423 | (when artist-key-is-drawing | |
4424 | (artist-key-do-continously-common))) | |
b95b34e5 GM |
4425 | |
4426 | (defun artist-backward-char (&optional n) | |
98d92ce3 | 4427 | "Move cursor backward N chars (default is 1), updating current shape. |
b95b34e5 GM |
4428 | If N is negative, move forward." |
4429 | (interactive "p") | |
4430 | (if (> n 0) | |
4431 | (artist-forward-char (- n)) | |
4432 | (artist-forward-char n))) | |
4433 | ||
4434 | (defun artist-forward-char (&optional n) | |
98d92ce3 | 4435 | "Move cursor forward N chars (default is 1), updating current shape. |
b95b34e5 GM |
4436 | If N is negative, move backward." |
4437 | (interactive "p") | |
4438 | (let* ((step-x (if (>= n 0) 1 -1)) | |
4439 | (distance (abs n)) | |
4440 | (curr-col (artist-current-column)) | |
4441 | (new-col (max 0 (+ curr-col (* distance step-x))))) | |
4442 | (if (not artist-key-is-drawing) | |
4443 | (move-to-column new-col t) | |
4444 | (move-to-column new-col t) | |
4445 | (artist-key-do-continously-common)))) | |
4446 | ||
4447 | ||
4448 | (defun artist-key-set-point (&optional arg) | |
4449 | "Set a point for the current shape. With optional ARG, set the last point." | |
4450 | (interactive "P") | |
4451 | (artist-key-set-point-common arg)) | |
4452 | ||
4453 | ||
4454 | (defun artist-select-fill-char (c) | |
4455 | "Set current fill character to be C." | |
4456 | (interactive "cType fill char (type RET to turn off): ") | |
4457 | (cond ((eq c ?\r) (setq artist-fill-char-set nil) | |
301b181a | 4458 | (message "Fill canceled")) |
b95b34e5 GM |
4459 | (t (setq artist-fill-char-set t) |
4460 | (setq artist-fill-char c) | |
4461 | (message "Fill set to \"%c\"" c)))) | |
4462 | ||
4463 | ||
4464 | (defun artist-select-line-char (c) | |
4465 | "Set current line character to be C." | |
4466 | (interactive "cType line char (type RET to turn off): ") | |
4467 | (cond ((eq c ?\r) (setq artist-line-char-set nil) | |
4468 | (message "Normal lines")) | |
4469 | (t (setq artist-line-char-set t) | |
4470 | (setq artist-line-char c) | |
4471 | (message "Line drawn with \"%c\"" c))) | |
4472 | (if artist-key-is-drawing | |
4473 | (artist-key-do-continously-common))) | |
4474 | ||
4475 | ||
4476 | (defun artist-select-erase-char (c) | |
4477 | "Set current erase character to be C." | |
4478 | (interactive "cType char to use when erasing (type RET for normal): ") | |
fe6e86db | 4479 | (cond ((eq c ?\r) (setq artist-erase-char ?\s) |
b95b34e5 GM |
4480 | (message "Normal erasing")) |
4481 | (t (setq artist-erase-char c) | |
4482 | (message "Erasing with \"%c\"" c))) | |
4483 | (if artist-key-is-drawing | |
4484 | (artist-key-do-continously-common))) | |
4485 | ||
4486 | (defun artist-charlist-to-string (char-list) | |
4487 | "Convert a list of characters, CHAR-LIST, to a string." | |
98d92ce3 | 4488 | (concat char-list)) |
b95b34e5 GM |
4489 | |
4490 | (defun artist-string-to-charlist (str) | |
4491 | "Convert a string, STR, to list of characters." | |
4492 | (append str nil)) | |
4493 | ||
4494 | (defun artist-select-spray-chars (chars initial-char) | |
4495 | "Set current spray characters to be CHARS, starting with INITIAL-CHAR." | |
4496 | ;; This huge unreadable `interactive'-clause does the following | |
4497 | ;; 1. Asks for a string of spray-characters | |
4498 | ;; 2. Asks for the initial character (default is the first), | |
4499 | ;; and loops if the answer is not a char within the string in 1. | |
4500 | (interactive | |
4501 | (let* ((str (read-string "Select spray-can characters, lightest first: " | |
4502 | (artist-charlist-to-string artist-spray-chars))) | |
4503 | (char-list (artist-string-to-charlist str)) | |
4504 | (initial (let* ((err-msg "") | |
4505 | (ok nil) | |
4506 | (first-char-as-str (char-to-string (car char-list))) | |
4507 | (first-s) (first-c)) | |
4508 | (while (not ok) | |
4509 | (setq first-s | |
4510 | (read-string | |
4511 | (format (concat "%sSelect initial-character, " | |
4512 | "one of \"%s\" (%s): ") | |
4513 | err-msg str first-char-as-str))) | |
4514 | (if (equal first-s "") | |
4515 | (setq first-s first-char-as-str)) | |
4516 | (setq first-c (car (artist-string-to-charlist first-s))) | |
4517 | (setq ok (not (null (member first-c char-list)))) | |
4518 | (if (not ok) | |
4519 | (setq err-msg (format | |
4520 | "Not in spray-chars: \"%s\". " | |
4521 | (char-to-string first-c))))) | |
4522 | first-c))) | |
4523 | (list char-list initial))) | |
4524 | (setq artist-spray-chars chars) | |
4525 | (setq artist-spray-new-char initial-char) | |
4526 | (message "Spray-chars set to \"%s\", initial: \"%s\"" | |
4527 | (artist-charlist-to-string chars) (char-to-string initial-char))) | |
4528 | ||
4529 | ||
4530 | (defun artist-select-operation (op-str) | |
4531 | "Select drawing operation OP-STR." | |
4532 | (interactive (list (completing-read "Select operation: " | |
4533 | artist-key-compl-table))) | |
4534 | (let* ((op-symbol (artist-mt-get-symbol-from-keyword op-str)) | |
4535 | (draw-how (if op-symbol | |
4536 | (artist-go-get-draw-how-from-symbol op-symbol) | |
4537 | nil))) | |
4538 | ;; First check that the string was valid | |
4539 | (if (null op-symbol) | |
4540 | (error "Unknown drawing method: %s" op-str)) | |
4541 | ||
4542 | ;; Second, check that we are not about to switch to a different | |
4543 | ;; kind of shape (do that only if we are drawing with keys; | |
4544 | ;; otherwise this function cannot get called). | |
4545 | (if (and artist-key-is-drawing | |
4546 | (not (equal artist-key-draw-how draw-how))) | |
4547 | (error "Cannot switch to a different kind of shape while drawing")) | |
4548 | ||
4549 | ;; If we were drawing, undraw the shape | |
4550 | (if (and artist-key-is-drawing | |
4551 | artist-rubber-banding) | |
4552 | (artist-key-undraw-common)) | |
4553 | ||
4554 | ;; Set the current operation and draw-how | |
4555 | (setq artist-curr-go op-symbol) | |
4556 | (setq artist-key-draw-how draw-how) | |
4557 | ||
4558 | ;; If we were drawing, redraw the shape (but don't if shape | |
4559 | ;; is drawn by setting only one point) | |
4560 | (if (and artist-key-is-drawing | |
4561 | artist-rubber-banding | |
4562 | (not (eq artist-key-draw-how 1))) | |
4563 | (artist-key-draw-common))) | |
4564 | ||
4565 | ;; Feedback | |
4566 | (artist-mode-line-show-curr-operation artist-key-is-drawing)) | |
4567 | ||
4568 | ||
4569 | (defun artist-toggle-rubber-banding (&optional state) | |
4570 | "Toggle rubber-banding. | |
4571 | If optional argument STATE is positive, turn rubber-banding on." | |
4572 | (interactive) | |
4573 | (if artist-key-is-drawing | |
4574 | (error "Cannot toggle rubber-banding while drawing")) | |
4575 | (if (setq artist-rubber-banding | |
4576 | (if (null state) (not artist-rubber-banding) | |
4577 | (> (prefix-numeric-value state) 0))) | |
4578 | (message "Rubber-banding is now on") | |
4579 | (message "Rubber-banding is now off"))) | |
4580 | ||
4581 | ||
4582 | (defun artist-toggle-trim-line-endings (&optional state) | |
4583 | "Toggle trimming of line-endings. | |
4584 | If optional argument STATE is positive, turn trimming on." | |
4585 | (interactive) | |
4586 | (if (setq artist-trim-line-endings | |
4587 | (if (null state) (not artist-trim-line-endings) | |
4588 | (> (prefix-numeric-value state) 0))) | |
4589 | (message "Trimming is now on") | |
4590 | (message "Trimming is now off"))) | |
4591 | ||
4592 | ||
4593 | (defun artist-toggle-borderless-shapes (&optional state) | |
4594 | "Toggle borders of shapes. | |
4595 | If optional argument STATE is positive, turn borders on." | |
4596 | (interactive) | |
4597 | (if (setq artist-borderless-shapes | |
4598 | (if (null state) (not artist-borderless-shapes) | |
4599 | (> (prefix-numeric-value state) 0))) | |
4600 | (message "Borders are now off") | |
4601 | (message "Borders are now on"))) | |
4602 | ||
4603 | ||
4604 | (defun artist-toggle-first-arrow () | |
4605 | "Toggle first arrow for shape, if possible." | |
4606 | (interactive) | |
4607 | (save-excursion | |
4608 | (if (not (null artist-arrow-point-1)) | |
4609 | (let* ((arrow-point artist-arrow-point-1) | |
4610 | (arrow-state (artist-arrow-point-get-state arrow-point)) | |
4611 | (arrow-marker (artist-arrow-point-get-marker arrow-point)) | |
4612 | (direction (artist-arrow-point-get-direction arrow-point)) | |
4613 | (orig-char (artist-arrow-point-get-orig-char arrow-point)) | |
4614 | (arrow-char (aref artist-arrows direction)) | |
4615 | (new-state (not arrow-state))) | |
4616 | ||
4617 | (goto-char (marker-position arrow-marker)) | |
4618 | ||
4619 | (if new-state | |
4620 | (if arrow-char | |
4621 | (artist-replace-char arrow-char)) | |
4622 | (artist-replace-char orig-char)) | |
4623 | ||
4624 | (artist-arrow-point-set-state artist-arrow-point-1 new-state))))) | |
4625 | ||
4626 | (defun artist-toggle-second-arrow () | |
4627 | "Toggle second arrow for shape, if possible." | |
4628 | (interactive) | |
4629 | (save-excursion | |
4630 | (if (not (null artist-arrow-point-2)) | |
4631 | (let* ((arrow-point artist-arrow-point-2) | |
4632 | (arrow-state (artist-arrow-point-get-state arrow-point)) | |
4633 | (arrow-marker (artist-arrow-point-get-marker arrow-point)) | |
4634 | (direction (artist-arrow-point-get-direction arrow-point)) | |
4635 | (orig-char (artist-arrow-point-get-orig-char arrow-point)) | |
4636 | (arrow-char (aref artist-arrows direction)) | |
4637 | (new-state (not arrow-state))) | |
4638 | ||
4639 | (goto-char (marker-position arrow-marker)) | |
4640 | ||
4641 | (if new-state | |
4642 | (if arrow-char | |
4643 | (artist-replace-char arrow-char)) | |
4644 | (artist-replace-char orig-char)) | |
4645 | ||
4646 | (artist-arrow-point-set-state artist-arrow-point-2 new-state))))) | |
4647 | ||
d77aaf6f CY |
4648 | (defun artist-select-op-pen-line () |
4649 | "Select drawing pen lines." | |
4650 | (interactive) | |
4651 | (artist-select-operation "Pen Line")) | |
b95b34e5 GM |
4652 | |
4653 | (defun artist-select-op-line () | |
4654 | "Select drawing lines." | |
4655 | (interactive) | |
4656 | (artist-select-operation "line")) | |
4657 | ||
4658 | (defun artist-select-op-straight-line () | |
4659 | "Select drawing straight lines." | |
4660 | (interactive) | |
4661 | (artist-select-operation "straight line")) | |
4662 | ||
4663 | (defun artist-select-op-rectangle () | |
4664 | "Select drawing rectangles." | |
4665 | (interactive) | |
4666 | (artist-select-operation "rectangle")) | |
4667 | ||
4668 | (defun artist-select-op-square () | |
4669 | "Select drawing squares." | |
4670 | (interactive) | |
4671 | (artist-select-operation "square")) | |
4672 | ||
4673 | (defun artist-select-op-poly-line () | |
4674 | "Select drawing poly-lines." | |
4675 | (interactive) | |
4676 | (artist-select-operation "poly-line")) | |
4677 | ||
4678 | (defun artist-select-op-straight-poly-line () | |
4679 | "Select drawing straight poly-lines." | |
4680 | (interactive) | |
4681 | (artist-select-operation "straight poly-line")) | |
4682 | ||
4683 | (defun artist-select-op-ellipse () | |
4684 | "Select drawing ellipses." | |
4685 | (interactive) | |
4686 | (artist-select-operation "ellipse")) | |
4687 | ||
4688 | (defun artist-select-op-circle () | |
4689 | "Select drawing circles." | |
4690 | (interactive) | |
4691 | (artist-select-operation "circle")) | |
4692 | ||
4693 | (defun artist-select-op-text-see-thru () | |
4694 | "Select rendering text (see thru)." | |
4695 | (interactive) | |
4696 | (artist-select-operation "text see-thru")) | |
4697 | ||
4698 | (defun artist-select-op-text-overwrite () | |
4699 | "Select rendering text (overwrite)." | |
4700 | (interactive) | |
4701 | (artist-select-operation "text overwrite")) | |
4702 | ||
4703 | (defun artist-select-op-spray-can () | |
4704 | "Select spraying." | |
4705 | (interactive) | |
4706 | (artist-select-operation "spray-can")) | |
4707 | ||
4708 | (defun artist-select-op-spray-set-size () | |
4709 | "Select setting size for spraying." | |
4710 | (interactive) | |
4711 | (artist-select-operation "spray set size")) | |
4712 | ||
4713 | (defun artist-select-op-erase-char () | |
4714 | "Select erasing characters." | |
4715 | (interactive) | |
4716 | (artist-select-operation "erase char")) | |
4717 | ||
4718 | (defun artist-select-op-erase-rectangle () | |
4719 | "Select erasing rectangles." | |
4720 | (interactive) | |
4721 | (artist-select-operation "erase rectangle")) | |
4722 | ||
4723 | (defun artist-select-op-vaporize-line () | |
4724 | "Select vaporizing single lines." | |
4725 | (interactive) | |
4726 | (artist-select-operation "vaporize line")) | |
4727 | ||
4728 | (defun artist-select-op-vaporize-lines () | |
4729 | "Select vaporizing connected lines." | |
4730 | (interactive) | |
4731 | (artist-select-operation "vaporize lines")) | |
4732 | ||
4733 | (defun artist-select-op-cut-rectangle () | |
4734 | "Select cutting rectangles." | |
4735 | (interactive) | |
4736 | (artist-select-operation "cut rectangle")) | |
4737 | ||
4738 | (defun artist-select-op-cut-square () | |
4739 | "Select cutting squares." | |
4740 | (interactive) | |
4741 | (artist-select-operation "cut square")) | |
4742 | ||
4743 | (defun artist-select-op-copy-rectangle () | |
4744 | "Select copying rectangles." | |
4745 | (interactive) | |
4746 | (artist-select-operation "copy rectangle")) | |
4747 | ||
4748 | (defun artist-select-op-copy-square () | |
4749 | "Select copying squares." | |
4750 | (interactive) | |
4751 | (artist-select-operation "cut square")) | |
4752 | ||
4753 | (defun artist-select-op-paste () | |
4754 | "Select pasting." | |
4755 | (interactive) | |
4756 | (artist-select-operation "paste")) | |
4757 | ||
4758 | (defun artist-select-op-flood-fill () | |
4759 | "Select flood-filling." | |
4760 | (interactive) | |
4761 | (artist-select-operation "flood-fill")) | |
4762 | ||
4763 | ||
4764 | ;; Drawing lines by using mouse | |
4765 | ;; Mouse button actions | |
4766 | ;; | |
4767 | ||
4768 | (defun artist-update-pointer-shape () | |
4769 | "Perform the update of the X Windows pointer shape." | |
4770 | (set-mouse-color nil)) | |
4771 | ||
aa360da1 GM |
4772 | (defvar x-pointer-shape) |
4773 | ||
b95b34e5 GM |
4774 | (defun artist-set-pointer-shape (new-pointer-shape) |
4775 | "Set the shape of the X Windows pointer to NEW-POINTER-SHAPE." | |
4776 | (setq x-pointer-shape new-pointer-shape) | |
4777 | (artist-update-pointer-shape)) | |
4778 | ||
4779 | (defsubst artist-event-is-shifted (ev) | |
4780 | "Check whether the shift-key is pressed in event EV." | |
4781 | (memq 'shift (event-modifiers ev))) | |
4782 | ||
4783 | (defun artist-do-nothing () | |
4784 | "Function that does nothing." | |
4785 | (interactive)) | |
4786 | ||
14bd267d EZ |
4787 | (defun artist-compute-up-event-key (ev) |
4788 | "Compute the corresponding up key sequence for event EV." | |
4789 | (let* ((basic (event-basic-type ev)) | |
4790 | (unshifted basic) | |
4791 | (shifted (make-symbol (concat "S-" (symbol-name basic))))) | |
4792 | (if (artist-event-is-shifted ev) | |
4793 | (make-vector 1 shifted) | |
4794 | (make-vector 1 unshifted)))) | |
4795 | ||
b95b34e5 GM |
4796 | (defun artist-down-mouse-1 (ev) |
4797 | "Perform drawing action for event EV." | |
4798 | (interactive "@e") | |
4799 | (let* ((real (artist-go-get-symbol-shift | |
4800 | artist-curr-go (artist-event-is-shifted ev))) | |
4801 | (draw-how (artist-go-get-draw-how-from-symbol real)) | |
4802 | ;; Remember original values for draw-region-min-y and max-y | |
4803 | ;; in case we are interrupting a key-draw operation. | |
4804 | (orig-draw-region-min-y artist-draw-region-min-y) | |
4805 | (orig-draw-region-max-y artist-draw-region-max-y) | |
4806 | (orig-pointer-shape (if (eq window-system 'x) x-pointer-shape nil)) | |
14bd267d | 4807 | (echoq-keystrokes 10000) ; a lot of seconds |
b95b34e5 GM |
4808 | ;; Remember original binding for the button-up event to this |
4809 | ;; button-down event. | |
14bd267d | 4810 | (key (artist-compute-up-event-key ev)) |
b95b34e5 GM |
4811 | (orig-button-up-binding (lookup-key (current-global-map) key))) |
4812 | ||
4813 | (unwind-protect | |
4814 | (progn | |
4815 | (if (eq window-system 'x) | |
4816 | (artist-set-pointer-shape artist-pointer-shape)) | |
4817 | ||
a9645a66 | 4818 | ;; Redefine the button-up binding temporarily (the original |
b95b34e5 GM |
4819 | ;; binding is restored in the unwind-forms below). This is to |
4820 | ;; avoid the phenomenon outlined in this scenario: | |
4821 | ;; | |
4822 | ;; 1. A routine which reads something from the mini-buffer (such | |
4823 | ;; as the text renderer) is called from below. | |
4824 | ;; 2. Meanwhile, the users releases the mouse button. | |
a9645a66 | 4825 | ;; 3. As a (funny :-) coincidence, the binding for the |
b95b34e5 GM |
4826 | ;; button-up event is often mouse-set-point, so Emacs |
4827 | ;; sets the point to where the button was released, which is | |
4828 | ;; in the buffer where the user wants to place the text. | |
4829 | ;; 4. The user types C-x o (or uses the mouse once again) | |
4830 | ;; until he reaches the mini-buffer which is still prompting | |
4831 | ;; for some text to render. | |
4832 | ;; | |
4833 | ;; To do this foolproof, all local and minor-mode maps should | |
4834 | ;; be searched and temporarily changed as well, since they | |
4835 | ;; too might have some binding for the button-up event, | |
4836 | ;; but I hope dealing with the global map will suffice. | |
4837 | (define-key (current-global-map) key 'artist-do-nothing) | |
4838 | ||
4839 | (artist-draw-region-reset) | |
4840 | ||
4841 | (artist-mode-line-show-curr-operation t) | |
4842 | ||
4843 | (cond ((eq draw-how 'artist-do-continously) | |
4844 | (artist-mouse-draw-continously ev)) | |
4845 | ((eq draw-how 'artist-do-poly) | |
4846 | (artist-mouse-draw-poly ev)) | |
4847 | ((and (numberp draw-how) (= draw-how 1)) | |
4848 | (artist-mouse-draw-1point ev)) | |
4849 | ((and (numberp draw-how) (= draw-how 2)) | |
4850 | (artist-mouse-draw-2points ev)) | |
4851 | (t (message "Drawing \"%s\"s is not yet implemented" | |
4852 | draw-how))) | |
4853 | ||
4854 | (if artist-trim-line-endings | |
4855 | (artist-draw-region-trim-line-endings artist-draw-region-min-y | |
4856 | artist-draw-region-max-y)) | |
4857 | (setq artist-draw-region-min-y orig-draw-region-min-y) | |
4858 | (setq artist-draw-region-max-y orig-draw-region-max-y)) | |
4859 | ||
4860 | ; This is protected | |
4861 | (if (eq window-system 'x) | |
4862 | (artist-set-pointer-shape orig-pointer-shape)) | |
4863 | ||
4864 | (if orig-button-up-binding | |
4865 | (define-key (current-global-map) key orig-button-up-binding)) | |
4866 | ||
4867 | (artist-mode-line-show-curr-operation artist-key-is-drawing)))) | |
4868 | ||
4869 | ||
4870 | (defun artist-mouse-choose-operation (ev op) | |
5445d287 | 4871 | "Choose operation for event EV and operation OP." |
b95b34e5 GM |
4872 | (interactive |
4873 | (progn | |
4874 | (select-window (posn-window (event-start last-input-event))) | |
4875 | (list last-input-event | |
14bd267d EZ |
4876 | (if (display-popup-menus-p) |
4877 | (x-popup-menu last-nonmenu-event artist-popup-menu-table) | |
4878 | 'no-popup-menus)))) | |
4879 | ||
4880 | (if (eq op 'no-popup-menus) | |
4881 | ;; No popup menus. Call `tmm-prompt' instead, but with the | |
4882 | ;; up-mouse-button, if any, temporarily disabled, otherwise | |
4883 | ;; it'll interfere. | |
4884 | (let* ((key (artist-compute-up-event-key ev)) | |
4885 | (orig-button-up-binding (lookup-key (current-global-map) key))) | |
4886 | (unwind-protect | |
4887 | (define-key (current-global-map) key 'artist-do-nothing) | |
4888 | (setq op (tmm-prompt artist-popup-menu-table)) | |
4889 | (if orig-button-up-binding | |
4890 | (define-key (current-global-map) key orig-button-up-binding))))) | |
b95b34e5 GM |
4891 | |
4892 | (let ((draw-fn (artist-go-get-draw-fn-from-symbol (car op))) | |
4893 | (set-fn (artist-fc-get-fn-from-symbol (car op)))) | |
4894 | (cond | |
4895 | ||
4896 | ;; *** It was a draw-function | |
4897 | ((not (listp draw-fn)) | |
4898 | (let* ((unshifted (artist-go-get-symbol-shift (car op) nil)) | |
4899 | (shifted (artist-go-get-symbol-shift (car op) t)) | |
4900 | (shift-state (artist-event-is-shifted ev)) | |
4901 | (selected-op (if shift-state shifted unshifted)) | |
4902 | (keyword (artist-go-get-keyword-from-symbol selected-op))) | |
4903 | (artist-select-operation keyword))) | |
4904 | ||
4905 | ;; *** It was a set/unset function | |
4906 | ((not (listp set-fn)) | |
4907 | (call-interactively set-fn))))) | |
4908 | ||
4909 | ||
4910 | (defun artist-down-mouse-3 (ev) | |
4911 | "Erase character or rectangle, depending on event EV." | |
4912 | (interactive "@e") | |
4913 | (let ((artist-curr-go 'erase-char)) | |
4914 | (artist-down-mouse-1 ev)) | |
4915 | ;; Restore mode-line | |
4916 | (artist-mode-line-show-curr-operation artist-key-is-drawing)) | |
4917 | ||
4918 | ||
4919 | ;; | |
4920 | ;; Mouse routines | |
4921 | ;; | |
4922 | ||
4923 | (defsubst artist-shift-has-changed (shift-state ev) | |
4924 | "From the last SHIFT-STATE and EV, determine if the shift-state has changed." | |
4925 | ;; This one simply doesn't work. | |
4926 | ;; | |
4927 | ;; There seems to be no way to tell whether the user has pressed shift | |
4928 | ;; while dragging the cursor around when we are in a track-mouse | |
4929 | ;; form. Calling (event-modifiers ev) yields nil :-( Neither is the | |
4930 | ;; (event-basic-type ev) of any help (it is simply `mouse-movement'). | |
4931 | ;; | |
4932 | ;; So this doesn't work: | |
4933 | ;; (cond ((and shift-state (not (artist-event-is-shifted ev))) t) | |
4934 | ;; ((and (not shift-state) (artist-event-is-shifted ev)) t) | |
4935 | ;; (t nil)) | |
4936 | nil) | |
4937 | ||
4938 | (defun artist-coord-win-to-buf (coord) | |
4939 | "Convert a window-relative coordinate COORD to a buffer-relative coordinate." | |
4940 | (let ((window-x (car coord)) | |
4941 | (window-y (cdr coord)) | |
4942 | (window-start-x (window-hscroll)) | |
4943 | (window-start-y (save-excursion (goto-char (window-start)) | |
4944 | (artist-current-line)))) | |
4945 | (cons (+ window-x window-start-x) | |
4946 | (+ window-y window-start-y)))) | |
4947 | ||
4948 | ||
4949 | (defun artist-mouse-draw-continously (ev) | |
98d92ce3 | 4950 | "Generic function for shapes that require 1 point as input. |
4963739e | 4951 | Operation is done continuously while the mouse button is hold down. |
b95b34e5 GM |
4952 | The event, EV, is the mouse event." |
4953 | (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil)) | |
4954 | (shifted (artist-go-get-symbol-shift artist-curr-go t)) | |
4955 | (shift-state (artist-event-is-shifted ev)) | |
4956 | (op (if shift-state shifted unshifted)) | |
4957 | (draw-how (artist-go-get-draw-how-from-symbol op)) | |
4958 | (init-fn (artist-go-get-init-fn-from-symbol op)) | |
4959 | (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op)) | |
4960 | (exit-fn (artist-go-get-exit-fn-from-symbol op)) | |
4961 | (draw-fn (artist-go-get-draw-fn-from-symbol op)) | |
4962 | (interval-fn (artist-go-get-interval-fn-from-symbol op)) | |
4963 | (interval (artist-funcall interval-fn)) | |
4964 | (arrow-pred (artist-go-get-arrow-pred-from-symbol op)) | |
4965 | (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op)) | |
4966 | (ev-start (event-start ev)) | |
4967 | (initial-win (posn-window ev-start)) | |
4968 | (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start))) | |
4969 | (x1 (car ev-start-pos)) | |
4970 | (y1 (cdr ev-start-pos)) | |
4971 | (shape) | |
4972 | (timer)) | |
4973 | (select-window (posn-window ev-start)) | |
4974 | (artist-funcall init-fn x1 y1) | |
4975 | (if (not artist-rubber-banding) | |
4976 | (artist-no-rb-set-point1 x1 y1)) | |
4977 | (track-mouse | |
4978 | (while (or (mouse-movement-p ev) | |
4979 | (member 'down (event-modifiers ev))) | |
4980 | (setq ev-start-pos (artist-coord-win-to-buf | |
4981 | (posn-col-row (event-start ev)))) | |
4982 | (setq x1 (car ev-start-pos)) | |
4983 | (setq y1 (cdr ev-start-pos)) | |
4984 | ||
4985 | ;; Cancel previous timer | |
4986 | (if timer | |
4987 | (cancel-timer timer)) | |
4988 | ||
4989 | (if (not (eq initial-win (posn-window (event-start ev)))) | |
4990 | ;; If we moved outside the window, do nothing | |
4991 | nil | |
4992 | ||
4993 | ;; Still in same window: | |
4994 | ;; | |
4995 | ;; Check if user presses or releases shift key | |
4996 | (if (artist-shift-has-changed shift-state ev) | |
4997 | ||
4998 | ;; First check that the draw-how is the same as we | |
4999 | ;; already have. Otherwise, ignore the changed shift-state. | |
5000 | (if (not (eq draw-how | |
5001 | (artist-go-get-draw-how-from-symbol | |
5002 | (if (not shift-state) shifted unshifted)))) | |
5003 | (message "Cannot switch to shifted operation") | |
5004 | ||
5005 | ;; progn is "implicit" since this is the else-part | |
5006 | (setq shift-state (not shift-state)) | |
5007 | (setq op (if shift-state shifted unshifted)) | |
5008 | (setq draw-how (artist-go-get-draw-how-from-symbol op)) | |
5009 | (setq draw-fn (artist-go-get-draw-fn-from-symbol op)))) | |
5010 | ||
5011 | ;; Draw the new shape | |
5012 | (setq shape (artist-funcall draw-fn x1 y1)) | |
5013 | (artist-move-to-xy x1 y1) | |
5014 | ||
5015 | ;; Start the timer to call `draw-fn' repeatedly every | |
5016 | ;; `interval' second | |
5017 | (if (and interval draw-fn) | |
5018 | (setq timer (run-at-time interval interval draw-fn x1 y1)))) | |
5019 | ||
5020 | ;; Read next event | |
5021 | (setq ev (read-event)))) | |
5022 | ||
5023 | ;; Cancel any timers | |
5024 | (if timer | |
5025 | (cancel-timer timer)) | |
5026 | ||
5027 | (artist-funcall prep-fill-fn x1 y1) | |
5028 | ||
5029 | (if (artist-funcall arrow-pred) | |
5030 | (artist-funcall arrow-set-fn x1 y1) | |
5031 | (artist-clear-arrow-points)) | |
5032 | ||
5033 | (artist-funcall exit-fn x1 y1) | |
5034 | (artist-move-to-xy x1 y1))) | |
5035 | ||
5036 | ||
5037 | ||
5038 | (defun artist-mouse-draw-poly (ev) | |
5039 | "Generic function for shapes requiring several points as input. | |
5040 | The event, EV, is the mouse event." | |
5041 | (interactive "@e") | |
5042 | (message "Mouse-1: set new point, mouse-2: set last point") | |
5043 | (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil)) | |
5044 | (shifted (artist-go-get-symbol-shift artist-curr-go t)) | |
5045 | (shift-state (artist-event-is-shifted ev)) | |
5046 | (op (if shift-state shifted unshifted)) | |
5047 | (draw-how (artist-go-get-draw-how-from-symbol op)) | |
5048 | (init-fn (artist-go-get-init-fn-from-symbol op)) | |
5049 | (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op)) | |
5050 | (exit-fn (artist-go-get-exit-fn-from-symbol op)) | |
5051 | (draw-fn (artist-go-get-draw-fn-from-symbol op)) | |
5052 | (undraw-fn (artist-go-get-undraw-fn-from-symbol op)) | |
5053 | (fill-pred (artist-go-get-fill-pred-from-symbol op)) | |
5054 | (fill-fn (artist-go-get-fill-fn-from-symbol op)) | |
5055 | (arrow-pred (artist-go-get-arrow-pred-from-symbol op)) | |
5056 | (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op)) | |
5057 | (ev-start (event-start ev)) | |
5058 | (initial-win (posn-window ev-start)) | |
5059 | (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start))) | |
5060 | (x1-last (car ev-start-pos)) | |
5061 | (y1-last (cdr ev-start-pos)) | |
5062 | (x2 x1-last) | |
5063 | (y2 y1-last) | |
5064 | (is-down t) | |
5065 | (shape nil) | |
63db25ed | 5066 | (point-list nil) |
b95b34e5 GM |
5067 | (done nil)) |
5068 | (select-window (posn-window ev-start)) | |
5069 | (artist-funcall init-fn x1-last y1-last) | |
5070 | (if (not artist-rubber-banding) | |
5071 | (artist-no-rb-set-point1 x1-last y1-last)) | |
5072 | (track-mouse | |
5073 | (while (not done) | |
5074 | ;; decide what to do | |
5075 | (cond | |
5076 | ||
5077 | ;; *** Mouse button is released. | |
5078 | ((and is-down | |
5079 | (or (member 'click (event-modifiers ev)) | |
5080 | (member 'drag (event-modifiers ev)))) | |
5081 | ;; First, if not rubber-banding, draw the line. | |
5082 | ;; | |
5083 | (if (not artist-rubber-banding) | |
5084 | (progn | |
5085 | (artist-no-rb-unset-points) | |
5086 | (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2)))) | |
5087 | ||
5088 | ;; Set the second point to the shape's second point | |
5089 | ;; (which might be different from the mouse's second point, | |
5090 | ;; if, for example, we are drawing a straight line) | |
5091 | ;; | |
5092 | (if (not (null shape)) | |
5093 | (let ((endpoint2 (artist-2point-get-endpoint2 shape))) | |
5094 | (setq x1-last (artist-endpoint-get-x endpoint2)) | |
5095 | (setq y1-last (artist-endpoint-get-y endpoint2)))) | |
5096 | (setq point-list (cons (artist-make-endpoint x1-last y1-last) | |
5097 | point-list)) | |
5098 | (setq shape nil) | |
5099 | (setq is-down nil)) | |
5100 | ||
5101 | ;; *** Mouse button 2 or 3 down | |
5102 | ((and (member 'down (event-modifiers ev)) | |
5103 | (or (equal (event-basic-type ev) 'mouse-2) | |
5104 | (equal (event-basic-type ev) 'mouse-3))) | |
5105 | ;; Ignore | |
5106 | nil) | |
5107 | ||
5108 | ;; *** Mouse button 2 or 3 released | |
5109 | ((and (or (member 'click (event-modifiers ev)) | |
5110 | (member 'drag (event-modifiers ev))) | |
5111 | (or (equal (event-basic-type ev) 'mouse-2) | |
5112 | (equal (event-basic-type ev) 'mouse-3))) | |
5113 | ||
5114 | ;; This means the end of our poly-line drawing-session. | |
5115 | ;; | |
5116 | (setq done t)) | |
5117 | ||
5118 | ;; *** Mouse button 1 went down | |
5119 | ((and (not is-down) | |
5120 | (member 'down (event-modifiers ev)) | |
5121 | (equal (event-basic-type ev) 'mouse-1)) | |
5122 | ;; Check whether the (possibly new, that depends on if shift | |
5123 | ;; has been pressed or released) symbol has the same draw-how | |
5124 | ;; information as the previous had. If it hasn't, we can't | |
5125 | ;; proceed. | |
5126 | ;; | |
5127 | (if (not (eq draw-how | |
5128 | (artist-go-get-draw-how-from-symbol | |
5129 | (if (not shift-state) shifted unshifted)))) | |
5130 | (message "Cannot switch operation") | |
5131 | (progn | |
5132 | ;; Decide operation | |
5133 | ;; | |
5134 | (setq unshifted | |
5135 | (artist-go-get-symbol-shift artist-curr-go nil) | |
5136 | shifted | |
5137 | (artist-go-get-symbol-shift artist-curr-go t) | |
5138 | shift-state (artist-event-is-shifted ev) | |
5139 | op (if shift-state shifted unshifted) | |
5140 | draw-how (artist-go-get-draw-how-from-symbol op) | |
5141 | draw-fn (artist-go-get-draw-fn-from-symbol op) | |
5142 | undraw-fn (artist-go-get-undraw-fn-from-symbol op) | |
5143 | fill-pred (artist-go-get-fill-pred-from-symbol op) | |
5144 | fill-fn (artist-go-get-fill-fn-from-symbol op)) | |
5145 | ||
5146 | ;; Draw shape from last place to this place | |
5147 | ||
5148 | ;; set x2 and y2 | |
5149 | ;; | |
5150 | (setq ev-start-pos (artist-coord-win-to-buf | |
5151 | (posn-col-row (event-start ev)))) | |
5152 | (setq x2 (car ev-start-pos)) | |
5153 | (setq y2 (cdr ev-start-pos)) | |
5154 | ||
5155 | ;; Draw the new shape (if not rubber-banding, place both marks) | |
5156 | ;; | |
5157 | (if artist-rubber-banding | |
5158 | (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2)) | |
5159 | (progn | |
5160 | (artist-no-rb-set-point1 x1-last y1-last) | |
5161 | (artist-no-rb-set-point2 x2 y2))) | |
5162 | ||
5163 | ;; Show new operation in mode-line | |
5164 | (let ((artist-curr-go op)) | |
5165 | (artist-mode-line-show-curr-operation t)))) | |
5166 | ||
5167 | (setq is-down t)) | |
5168 | ||
5169 | ||
5170 | ;; *** Mouse moved, button is down and we are still in orig window | |
5171 | ((and (mouse-movement-p ev) | |
5172 | is-down | |
5173 | (eq initial-win (posn-window (event-start ev)))) | |
5174 | ;; Draw shape from last place to this place | |
5175 | ;; | |
5176 | ;; set x2 and y2 | |
5177 | (setq ev-start-pos (artist-coord-win-to-buf | |
5178 | (posn-col-row (event-start ev)))) | |
5179 | (setq x2 (car ev-start-pos)) | |
5180 | (setq y2 (cdr ev-start-pos)) | |
5181 | ||
5182 | ;; First undraw last shape | |
5183 | ;; (unset last point if not rubberbanding) | |
5184 | ;; | |
5185 | (artist-funcall undraw-fn shape) | |
5186 | ||
5187 | ;; Draw the new shape (if not rubberbanding, set 2nd mark) | |
5188 | ;; | |
5189 | (if artist-rubber-banding | |
5190 | (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2)) | |
5191 | (progn | |
5192 | (artist-no-rb-unset-point2) | |
5193 | (artist-no-rb-set-point2 x2 y2))) | |
5194 | ;; Move cursor | |
5195 | (artist-move-to-xy x2 y2)) | |
5196 | ||
5197 | ;; *** Mouse moved, button is down but we are NOT in orig window | |
5198 | ((and (mouse-movement-p ev) | |
5199 | is-down | |
5200 | (not (eq initial-win (posn-window (event-start ev))))) | |
5201 | ;; Ignore | |
5202 | nil) | |
5203 | ||
5204 | ||
5205 | ;; *** Moving mouse while mouse button is not down | |
5206 | ((and (mouse-movement-p ev) (not is-down)) | |
5207 | ;; don't do anything. | |
5208 | nil) | |
5209 | ||
5210 | ||
5211 | ;; *** Mouse button 1 went down, first time | |
5212 | ((and is-down | |
5213 | (member 'down (event-modifiers ev)) | |
5214 | (equal (event-basic-type ev) 'mouse-1)) | |
5215 | ;; don't do anything | |
5216 | nil) | |
5217 | ||
5218 | ||
5219 | ;; *** Another event | |
5220 | (t | |
5221 | ;; End drawing | |
5222 | ;; | |
5223 | (setq done t))) | |
5224 | ||
5225 | ;; Read next event (only if we should not stop) | |
5226 | (if (not done) | |
5227 | (setq ev (read-event))))) | |
5228 | ||
5229 | ;; Reverse point-list (last points are cond'ed first) | |
5230 | (setq point-list (reverse point-list)) | |
5231 | ||
5232 | (artist-funcall prep-fill-fn point-list) | |
5233 | ||
5234 | ;; Maybe fill | |
5235 | (if (artist-funcall fill-pred) | |
5236 | (artist-funcall fill-fn point-list)) | |
5237 | ||
5238 | ;; Maybe set arrow points | |
63db25ed | 5239 | (if (and point-list (artist-funcall arrow-pred)) |
b95b34e5 GM |
5240 | (artist-funcall arrow-set-fn point-list) |
5241 | (artist-clear-arrow-points)) | |
5242 | ||
5243 | (artist-funcall exit-fn point-list) | |
5244 | (artist-move-to-xy x2 y2))) | |
5245 | ||
5246 | ||
5247 | (defun artist-mouse-draw-1point (ev) | |
5248 | "Generic function for shapes requiring only 1 point as input. | |
5249 | Operation is done once. The event, EV, is the mouse event." | |
5250 | (interactive "@e") | |
5251 | (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil)) | |
5252 | (shifted (artist-go-get-symbol-shift artist-curr-go t)) | |
5253 | (shift-state (artist-event-is-shifted ev)) | |
5254 | (op (if shift-state shifted unshifted)) | |
5255 | (draw-how (artist-go-get-draw-how-from-symbol op)) | |
5256 | (init-fn (artist-go-get-init-fn-from-symbol op)) | |
5257 | (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op)) | |
5258 | (exit-fn (artist-go-get-exit-fn-from-symbol op)) | |
5259 | (draw-fn (artist-go-get-draw-fn-from-symbol op)) | |
5260 | (arrow-pred (artist-go-get-arrow-pred-from-symbol op)) | |
5261 | (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op)) | |
5262 | (ev-start (event-start ev)) | |
5263 | (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start))) | |
5264 | (x1 (car ev-start-pos)) | |
5265 | (y1 (cdr ev-start-pos))) | |
5266 | (select-window (posn-window ev-start)) | |
5267 | (artist-funcall init-fn x1 y1) | |
5268 | (artist-funcall draw-fn x1 y1) | |
5269 | (artist-funcall prep-fill-fn x1 y1) | |
5270 | (if (artist-funcall arrow-pred) | |
5271 | (artist-funcall arrow-set-fn x1 y1) | |
5272 | (artist-clear-arrow-points)) | |
5273 | (artist-funcall exit-fn x1 y1) | |
5274 | (artist-move-to-xy x1 y1))) | |
5275 | ||
5276 | ||
5277 | (defun artist-mouse-draw-2points (ev) | |
5278 | "Generic function for shapes requiring 2 points as input. | |
5279 | The event, EV, is the mouse event." | |
5280 | (interactive "@e") | |
5281 | (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil)) | |
5282 | (shifted (artist-go-get-symbol-shift artist-curr-go t)) | |
5283 | (shift-state (artist-event-is-shifted ev)) | |
5284 | (op (if shift-state shifted unshifted)) | |
5285 | (draw-how (artist-go-get-draw-how-from-symbol op)) | |
5286 | (init-fn (artist-go-get-init-fn-from-symbol op)) | |
5287 | (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op)) | |
5288 | (exit-fn (artist-go-get-exit-fn-from-symbol op)) | |
5289 | (draw-fn (artist-go-get-draw-fn-from-symbol op)) | |
5290 | (undraw-fn (artist-go-get-undraw-fn-from-symbol op)) | |
5291 | (fill-pred (artist-go-get-fill-pred-from-symbol op)) | |
5292 | (fill-fn (artist-go-get-fill-fn-from-symbol op)) | |
5293 | (arrow-pred (artist-go-get-arrow-pred-from-symbol op)) | |
5294 | (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op)) | |
5295 | (ev-start (event-start ev)) | |
5296 | (initial-win (posn-window ev-start)) | |
5297 | (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start))) | |
5298 | (x1 (car ev-start-pos)) | |
5299 | (y1 (cdr ev-start-pos)) | |
5300 | (x2) | |
5301 | (y2) | |
5302 | (shape)) | |
5303 | (select-window (posn-window ev-start)) | |
5304 | (artist-funcall init-fn x1 y1) | |
5305 | (if (not artist-rubber-banding) | |
5306 | (artist-no-rb-set-point1 x1 y1)) | |
5307 | (track-mouse | |
5308 | (while (or (mouse-movement-p ev) | |
5309 | (member 'down (event-modifiers ev))) | |
5310 | (setq ev-start-pos (artist-coord-win-to-buf | |
5311 | (posn-col-row (event-start ev)))) | |
5312 | (setq x2 (car ev-start-pos)) | |
5313 | (setq y2 (cdr ev-start-pos)) | |
5314 | ||
5315 | (if (not (eq initial-win (posn-window (event-start ev)))) | |
5316 | ;; If we moved outside the window, do nothing | |
5317 | nil | |
5318 | ||
5319 | ;; Still in same window: | |
5320 | ;; | |
5321 | ;; First undraw last shape (unset last point if not rubberbanding) | |
5322 | (if artist-rubber-banding | |
5323 | (artist-funcall undraw-fn shape) | |
5324 | (artist-no-rb-unset-point2)) | |
5325 | ||
5326 | ;; Check if user presses or releases shift key | |
5327 | (if (artist-shift-has-changed shift-state ev) | |
5328 | ||
5329 | ;; First check that the draw-how is the same as we | |
5330 | ;; already have. Otherwise, ignore the changed shift-state. | |
5331 | (if (not (eq draw-how | |
5332 | (artist-go-get-draw-how-from-symbol | |
5333 | (if (not shift-state) shifted unshifted)))) | |
5334 | (message "Cannot switch to shifted operation") | |
5335 | ||
5336 | (message "Switching") | |
5337 | ;; progn is "implicit" since this is the else-part | |
5338 | (setq shift-state (not shift-state)) | |
5339 | (setq op (if shift-state shifted unshifted)) | |
5340 | (setq draw-how (artist-go-get-draw-how-from-symbol op)) | |
5341 | (setq draw-fn (artist-go-get-draw-fn-from-symbol op)) | |
5342 | (setq undraw-fn (artist-go-get-undraw-fn-from-symbol op)) | |
5343 | (setq fill-pred (artist-go-get-fill-pred-from-symbol op)) | |
5344 | (setq fill-fn (artist-go-get-fill-fn-from-symbol op)))) | |
5345 | ||
5346 | ;; Draw the new shape | |
5347 | (if artist-rubber-banding | |
5348 | (setq shape (artist-funcall draw-fn x1 y1 x2 y2)) | |
5349 | (artist-no-rb-set-point2 x2 y2)) | |
5350 | ;; Move cursor | |
5351 | (artist-move-to-xy x2 y2)) | |
5352 | ||
5353 | ||
5354 | ;; Read next event | |
5355 | (setq ev (read-event)))) | |
5356 | ||
5357 | ;; If we are not rubber-banding (that is, we were moving around the `2') | |
5358 | ;; draw the shape | |
5359 | (if (not artist-rubber-banding) | |
5360 | (progn | |
5361 | (artist-no-rb-unset-points) | |
5362 | (setq shape (artist-funcall draw-fn x1 y1 x2 y2)))) | |
5363 | ||
5364 | (artist-funcall prep-fill-fn shape x1 y1 x2 y2) | |
5365 | ||
5366 | ;; Maybe fill | |
5367 | (if (artist-funcall fill-pred) | |
5368 | (artist-funcall fill-fn shape x1 y1 x2 y2)) | |
5369 | ||
5370 | ;; Maybe set arrow-points | |
5371 | (if (artist-funcall arrow-pred) | |
5372 | (artist-funcall arrow-set-fn shape x1 y1 x2 y2) | |
5373 | (artist-clear-arrow-points)) | |
5374 | ||
5375 | (artist-funcall exit-fn shape x1 y1 x2 y2) | |
5376 | (artist-move-to-xy x2 y2))) | |
5377 | ||
5378 | ||
5379 | ;; | |
5380 | ;; Bug-report-submitting | |
5381 | ;; | |
5382 | (defun artist-submit-bug-report () | |
5383 | "Submit via mail a bug report on Artist." | |
5384 | (interactive) | |
5385 | (require 'reporter) | |
5386 | (if (y-or-n-p "Do you want to submit a bug report on Artist? ") | |
5387 | (let ((to artist-maintainer-address) | |
5388 | (vars '(window-system | |
5389 | window-system-version | |
5390 | ;; | |
5391 | artist-rubber-banding | |
5392 | artist-interface-with-rect | |
5393 | artist-aspect-ratio | |
5394 | ;; Now the internal ones | |
5395 | artist-curr-go | |
5396 | artist-key-poly-point-list | |
5397 | artist-key-shape | |
5398 | artist-key-draw-how | |
5399 | artist-arrow-point-1 | |
5400 | artist-arrow-point-2))) | |
5401 | ;; Remove those variables from vars that are not bound | |
b5242984 | 5402 | (mapc |
b95b34e5 GM |
5403 | (function |
5404 | (lambda (x) | |
5405 | (if (not (and (boundp x) (symbol-value x))) | |
5406 | (setq vars (delq x vars))))) vars) | |
5407 | (reporter-submit-bug-report | |
5408 | artist-maintainer-address | |
5409 | (concat "artist.el " artist-version) | |
5410 | vars | |
5411 | nil nil | |
5412 | (concat "Hello Tomas,\n\n" | |
5413 | "I have a nice bug report on Artist for you! Here it is:"))))) | |
5414 | ||
5415 | ||
5416 | ;; | |
5417 | ;; Now provide this minor mode | |
5418 | ;; | |
5419 | ||
5420 | (provide 'artist) | |
5421 | ||
5422 | ||
5423 | ;;; About adding drawing modes | |
5424 | ;;; -------------------------- | |
5425 | ||
5426 | ;; If you are going to add a new drawing mode, read the following | |
5427 | ;; sketchy outlines to get started a bit easier. | |
5428 | ;; | |
5429 | ;; 1. If your new drawing mode falls into one of the following | |
5430 | ;; categories, goto point 2, otherwise goto point 3. | |
5431 | ;; | |
4963739e JB |
5432 | ;; - Modes where the shapes are drawn continuously, as long as |
5433 | ;; the mouse button is held down (continuous modes). | |
b95b34e5 GM |
5434 | ;; Example: the erase-char mode, the pen and pen-line modes. |
5435 | ;; | |
5436 | ;; - Modes where the shape is made up of from 2 points to an | |
5437 | ;; arbitrary number of points (poly-point modes). | |
5438 | ;; Example: the poly-line mode | |
5439 | ;; | |
5440 | ;; - Modes where the shape is made up of 2 points (2-point | |
5441 | ;; modes). | |
5442 | ;; Example: lines, rectangles | |
5443 | ;; | |
5444 | ;; - Modes where the shape is made up of 1 point (1-point | |
4963739e | 5445 | ;; modes). This mode differs from the continuous modes in |
b95b34e5 GM |
5446 | ;; that the shape is drawn only once when the mouse button |
5447 | ;; is pressed. | |
5448 | ;; Examples: paste, a flood-fill, vaporize modes | |
5449 | ;; | |
5450 | ;; | |
5451 | ;; 2. To make it easier and more flexible to program new drawing | |
5452 | ;; modes, you might choose to specify | |
5453 | ;; init-fn: a function to be called at the very beginning | |
5454 | ;; of the drawing phase, | |
5455 | ;; prep-fill-fn: a function to be called before filling, | |
5456 | ;; arrow-set-fn: a function for setting arrows, to be called | |
5457 | ;; after filling, and | |
5458 | ;; exit-fn: a function to be called at the very end of | |
5459 | ;; the drawing phase. | |
5460 | ;; For each of the cases below, the arguments given to the init-fn, | |
5461 | ;; prep-fill-fn, arrow-set-fn and exit-fn are stated. | |
5462 | ;; | |
4963739e | 5463 | ;; If your mode matches the continuous mode or the 1-point mode: |
b95b34e5 GM |
5464 | ;; |
5465 | ;; a. Create a draw-function that draws your shape. Your function | |
5466 | ;; must take x and y as arguments. The return value is not | |
5467 | ;; used. | |
5468 | ;; | |
5469 | ;; b. Add your mode to the master table, `artist-mt'. | |
5470 | ;; | |
5471 | ;; init-fn: x y | |
5472 | ;; prep-fill-fn: x y | |
5473 | ;; arrow-set-fn: x y | |
5474 | ;; exit-fn: x y | |
5475 | ;; | |
5476 | ;; If your mode matches the 2-point mode: | |
5477 | ;; | |
5478 | ;; a. Create one draw-function that draws your shape and one | |
5479 | ;; undraw-function that undraws it. | |
5480 | ;; | |
5481 | ;; The draw-function must take x1, y1, x2 and y2 as | |
5482 | ;; arguments. It must return a list with three elements: | |
5483 | ;; Endpoint1: a vector [x1 y1] | |
5484 | ;; Endpoint2: a vector [x2 y2] | |
5485 | ;; Shapeinfo: all info necessary for your undraw-function to | |
5486 | ;; be able to undraw the shape | |
5487 | ;; Use the artist-endpoint-* accessors to create and inspect | |
5488 | ;; the endpoints. | |
5489 | ;; | |
5490 | ;; If applicable, you must be able to draw your shape without | |
5491 | ;; borders if the `artist-borderless-shapes' is non-nil. | |
5492 | ;; See `artist-draw-rect' for an example. | |
5493 | ;; | |
5494 | ;; The undraw-function must take one argument: the list created | |
5495 | ;; by your draw-function. The return value is not used. | |
5496 | ;; | |
5497 | ;; b. If you want to provide a fill-function, then create a | |
5498 | ;; function that takes 5 arguments: the list created by your | |
5499 | ;; draw-function, x1, y1, x2 and y2. The return value is not | |
5500 | ;; used. | |
5501 | ;; | |
5502 | ;; c. Add your mode to the master table, `artist-mt'. | |
5503 | ;; | |
5504 | ;; init-fn: x1 y1 | |
5505 | ;; prep-fill-fn: shape x1 y1 x2 y2 | |
5506 | ;; arrow-set-fn: shape x1 y1 x2 y2 | |
5507 | ;; exit-fn: shape x1 y1 x2 y2 | |
5508 | ;; | |
5509 | ;; If your mode matches the poly-point mode: | |
5510 | ;; | |
5511 | ;; a. Create one draw-function that draws your shape and one | |
5512 | ;; undraw-function that undraws it. The draw- and | |
e6608c12 | 5513 | ;; undraw-functions are used to draw/undraw a segment of |
b95b34e5 GM |
5514 | ;; your poly-point mode between 2 points. The draw- and |
5515 | ;; undraw-functions are then really 2-point mode functions. | |
5516 | ;; They must take the same arguments and return the same | |
5517 | ;; values as those of the 2-point mode. | |
5518 | ;; | |
5519 | ;; If applicable, you must be able to draw your shape without | |
5520 | ;; borders if the `artist-borderless-shapes' is non-nil. | |
5521 | ;; See `artist-draw-rect' for an example. | |
5522 | ;; | |
5523 | ;; b. If you want to provide a fill-function, then create a | |
5524 | ;; function that takes 1 argument: a list of points where each | |
5525 | ;; point is a vector, [x, y]. | |
5526 | ;; | |
5527 | ;; c. Add your mode to the master table, `artist-mt'. | |
5528 | ;; | |
5529 | ;; init-fn: x1 y1 | |
5530 | ;; prep-fill-fn: point-list | |
5531 | ;; arrow-set-fn: point-list | |
5532 | ;; exit-fn: point-list | |
5533 | ;; | |
5534 | ;; The arrow-set-fn must set the variables `artist-arrow-point-1' | |
5535 | ;; and `artist-arrow-point-2'. If your mode does not take arrows, | |
5536 | ;; you must set the variables to nil. Use the accessors | |
5537 | ;; artist-arrow-point-* to create and inspect arrow-points. | |
5538 | ;; | |
5539 | ;; | |
5540 | ;; 3. If your mode doesn't match any of the categories, you are facing | |
5541 | ;; a bit more work, and I cannot be as detailed as above. Here is a | |
5542 | ;; brief outline of what you have to do: | |
5543 | ;; | |
5544 | ;; a. Decide on a name for your type of mode. Let's assume that | |
5545 | ;; you decided on `xxx'. Then you should use the draw-how | |
5546 | ;; symbol artist-do-xxx. | |
5547 | ;; | |
5548 | ;; b. Create a function artist-mouse-draw-xxx for drawing with | |
5549 | ;; mouse. It should be called from `artist-down-mouse-1'. | |
5550 | ;; | |
5551 | ;; The all coordinates must be converted from window-relative | |
5552 | ;; to buffer relative before saved or handed over to | |
5553 | ;; any other function. Converting is done with | |
5554 | ;; the function `artist-coord-win-to-buf'. | |
5555 | ;; | |
5556 | ;; It must take care to the `artist-rubber-banding' variable | |
5557 | ;; and perform rubber-banding accordingly. Use the | |
5558 | ;; artist-no-rb-* functions if not rubber-banding. | |
5559 | ;; | |
5560 | ;; If applicable, you must be able to draw your shape without | |
5561 | ;; borders if the `artist-borderless-shapes' is non-nil. | |
5562 | ;; See `artist-draw-rect' for an example. | |
5563 | ;; | |
5564 | ;; You must call the init-fn, the prep-fill-fn, arrow-set-fn | |
33972e80 | 5565 | ;; and the exit-fn at the appropriate points. |
b95b34e5 GM |
5566 | ;; |
5567 | ;; When artist-mouse-draw-xxx ends, the shape for your mode | |
5568 | ;; must be completely drawn. | |
5569 | ;; | |
5570 | ;; c. Create functions for drawing with keys: | |
5571 | ;; | |
5572 | ;; - artist-key-set-point-xxx for setting a point in the | |
5573 | ;; mode, to be called from `artist-key-set-point-common'. | |
5574 | ;; | |
4963739e JB |
5575 | ;; - artist-key-do-continuously-xxx to be called from |
5576 | ;; `artist-key-do-continuously-common' whenever the user | |
b95b34e5 GM |
5577 | ;; moves around. |
5578 | ;; | |
5579 | ;; As for the artist-mouse-draw-xxx, these two functions must | |
5580 | ;; take care to do rubber-banding, borderless shapes and to | |
5581 | ;; set arrows. | |
5582 | ;; | |
5583 | ;; These functions should set the variable `artist-key-shape' | |
5584 | ;; to the shape drawn. | |
5585 | ;; | |
5586 | ;; d. Create artist-key-draw-xxx and artist-key-undraw-xxx for | |
5587 | ;; drawing and undrawing. These are needed when the user | |
5588 | ;; switches operation to draw another shape of the same type | |
5589 | ;; of drawing mode. | |
5590 | ;; | |
5591 | ;; You should provide these functions. You might think that | |
c80e3b4a | 5592 | ;; only you is using your type of mode, so no one will be able |
b95b34e5 GM |
5593 | ;; to switch to another operation of the same type of mode, |
5594 | ;; but someone else might base a new drawing mode upon your | |
5595 | ;; work. | |
5596 | ;; | |
5597 | ;; You must call the init-fn, the prep-fill-fn, arrow-set-fn | |
33972e80 | 5598 | ;; and the exit-fn at the appropriate points. |
b95b34e5 GM |
5599 | ;; |
5600 | ;; e. Add your new mode to the master table, `artist-mt'. | |
5601 | ;; | |
5602 | ;; | |
5603 | ;; Happy hacking! Please let me hear if you add any drawing modes! | |
5604 | ;; Don't hesitate to ask me any questions. | |
5605 | ||
5606 | ||
e8af40ee | 5607 | ;;; artist.el ends here |