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