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