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