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