1 ;;; artist.el --- draw ascii graphics with your mouse
3 ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
5 ;; Author: Tomas Abrahamsson <tab@lysator.liu.se>
6 ;; Maintainer: Tomas Abrahamsson <tab@lysator.liu.se>
9 ;; Release-date: 6-Aug-2004
10 ;; Location: http://www.lysator.liu.se/~tab/artist/
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.
17 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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 \.
43 ;; * Intersecting: When a `|' intersects with a `-', a `+' is
44 ;; drawn, like this: | \ /
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
55 ;; * Drawing operations: The following drawing operations are implemented:
57 ;; lines straight-lines
59 ;; poly-lines straight poly-lines
61 ;; text (see-thru) text (overwrite)
62 ;; spray-can setting size for spraying
63 ;; vaporize line vaporize lines
64 ;; erase characters erase rectangles
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
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>.
76 ;; * Flood-filling: You can fill any area with a certain character by
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.
84 ;; * Drawing with keys: Everything you can do with the mouse, you can
85 ;; also do without the mouse.
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 >.
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.
96 ;; * Picture mode compatibility: Artist is picture mode compatible (this
97 ;; can be turned off).
99 ;; See the documentation for the function artist-mode for a detailed
100 ;; description on how to use artist.
103 ;; What about adding my own drawing modes?
104 ;; ---------------------------------------
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!
112 ;; To use artist, put this in your .emacs:
114 ;; (autoload 'artist-mode "artist" "Enter artist-mode" t)
119 ;; Artist requires Emacs 19.28 or higher.
121 ;; Artist requires the `rect' package (which comes with Emacs) to be
122 ;; loadable, unless the variable `artist-interface-with-rect' is set
125 ;; Artist also requires the Picture mode (which also comes with Emacs)
126 ;; to be loadable, unless the variable `artist-picture-compatibility'
131 ;; The shifted operations are not available when drawing with the mouse
132 ;; in Emacs 19.29 and 19.30.
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.)
142 ;; New: Coerced with the artist.el that's in Emacs-21.3.
143 ;; (minor editorial changes)
146 ;; New: Added tool selection via the mouse-wheel
147 ;; Function provided by Andreas Leue <al@sphenon.de>
150 ;; Bugfix: Some operations (the edit menu) got hidden
151 ;; Bugfix: The first arrow for poly-lines was always pointing
153 ;; Changed: Updated with changes made for Emacs 21.1
156 ;; Bugfix: Autoload cookie corrected
159 ;; Changed: More documentation fixes.
160 ;; Bugfix: The arrow characters (`artist-arrows'), which
161 ;; got wrong in 1.1, are now corrected.
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.
171 ;; New: Updated to work with Emacs 21
174 ;; Bugfix: Cursor follows mouse pointer more closely.
175 ;; New: Works with Emacs 20.x
176 ;; New: Variables are customizable
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
194 ;; First official release.
200 (defconst artist-version
"1.2.6")
201 (defconst artist-maintainer-address
"tab@lysator.liu.se")
203 (defvar x-pointer-crosshair
)
206 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
209 "Customization of the Artist mode."
212 (defgroup artist-text nil
213 "Customization of the text rendering."
216 (defcustom artist-rubber-banding t
217 "Interactively do rubber-banding when non-nil."
221 (defcustom artist-first-char ?
1
222 "Character to set at first point when not rubber-banding."
226 (defcustom artist-second-char ?
2
227 "Character to set at second point when not rubber-banding."
231 (defcustom artist-interface-with-rect t
232 "Whether to interface with the rect package or not.
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
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."
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.
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
255 The directions are as follows:
263 (defcustom artist-aspect-ratio
1
264 "Defines the character height-to-width aspect ratio.
265 This is used when drawing squares and circles."
269 (defcustom artist-trim-line-endings t
270 "Whether or not to remove white-space at end of lines.
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."
278 (defcustom artist-flood-fill-right-border
'window-width
279 "Right edge definition, used when flood-filling.
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."
285 :type
'(choice (const :tag
"limited to window" window-width
)
286 (const :tag
"limited to value of `fill-column'" fill-column
)))
288 (defcustom artist-flood-fill-show-incrementally t
289 "Whether or not to incrementally update display when flood-filling.
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."
298 (defcustom artist-ellipse-right-char ?\
)
299 "Character to use at the rightmost position when drawing narrow ellipses.
301 In this figure, it is the right parenthesis (the ``)'' character):
309 (defcustom artist-ellipse-left-char ?\
(
310 "Character to use at the leftmost position when drawing narrow ellipses.
312 In this figure, it is the left parenthesis (the ``('' character):
319 (defcustom artist-picture-compatibility t
320 "Whether or not picture mode compatibility is on."
327 (defcustom artist-vaporize-fuzziness
1
328 "How to vaporize lines that are cut off.
330 Accept this many characters cutting off a line and still treat
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 ``*''):
336 A----*------/-----------B
338 A----*----/\\------------B
341 but this one won't, since it is cut off by more than 2 characters:
343 A----*----/\\/----------B
345 (in fact, only the left part [between the A and the leftmost ``/''
346 crossing the line] will be vaporized)."
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.")
355 (defcustom artist-text-renderer-function
'artist-figlet
356 "Function for doing text rendering."
359 (defvaralias 'artist-text-renderer
'artist-text-renderer-function
)
362 (defcustom artist-figlet-program
"figlet"
363 "Program to run for `figlet'."
368 (defcustom artist-figlet-default-font
"standard"
369 "Default font for `figlet'."
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."
384 (defcustom artist-spray-interval
0.2
385 "Number of seconds between repeated spraying."
390 (defcustom artist-spray-radius
4
391 "Size of the area for spraying."
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.")
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.")
412 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
413 ;; End of user options
416 ;; Internal variables
418 (defvar artist-mode nil
419 "Non-nil to enable `artist-mode' and nil to disable.")
420 (make-variable-buffer-local 'artist-mode
)
422 (defvar artist-mode-name
" Artist"
423 "Name of Artist mode beginning with a space (appears in the mode-line).")
425 (defvar artist-curr-go
'pen-line
426 "Current selected graphics operation.")
427 (make-variable-buffer-local 'artist-curr-go
)
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
)
433 (defvar artist-line-char nil
434 "Char to use when drawing lines.")
435 (make-variable-buffer-local 'artist-line-char
)
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
)
441 (defvar artist-fill-char nil
442 "Char to use when filling.")
443 (make-variable-buffer-local 'artist-fill-char
)
445 (defvar artist-erase-char ?\s
446 "Char to use when erasing.")
447 (make-variable-buffer-local 'artist-erase-char
)
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
)
453 ; This variable is not buffer local
454 (defvar artist-copy-buffer nil
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
)
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
)
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
)
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
477 This variable is initialized by the `artist-make-prev-next-op-alist' function.")
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
485 (if artist-interface-with-rect
490 (if artist-picture-compatibility
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
)
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)
545 :help ,(format "Draw using the %s style" (nth 0 op))
546 :button (:radio . (eq artist-curr-go ',(nth 2 op))))))
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))
603 "Keymap for `artist-minor-mode'.")
605 (defvar artist-replacement-table (make-vector 256 0)
606 "Replacement table for `artist-replace-char'.")
610 ;;; The table of graphic operations
613 ;; Implementation note: Maybe this should be done using a structure
614 ;; in the cl package?
620 ( "Undo" do-undo undo))
624 ("Pen" (("Pen" pen-char "pen-c"
627 artist-do-continously
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
638 ("Line" (("line" line "line"
639 artist-arrows artist-set-arrow-points-for-2points
645 ("straight line" s-line "sline"
646 artist-arrows artist-set-arrow-points-for-2points
654 ("Rectangle" (("rectangle" rect "rect"
660 artist-t-if-fill-char-set artist-fill-rect))
661 ("square" square "square"
666 (artist-undraw-square
667 artist-t-if-fill-char-set artist-fill-square)))))
670 ("Poly-line" (("poly-line" polyline "poly"
671 artist-arrows artist-set-arrow-points-for-poly
677 ("straight poly-line" spolyline "s-poly"
678 artist-arrows artist-set-arrow-points-for-poly
686 ("Ellipse" (("ellipse" ellipse "ellipse"
691 (artist-undraw-ellipse
692 artist-t-if-fill-char-set artist-fill-ellipse))
693 ("circle" circle "circle"
698 (artist-undraw-circle
699 artist-t-if-fill-char-set artist-fill-circle)))))
702 ("Text" (("text see-thru" text-thru "text-thru"
708 ("text overwrite" text-ovwrt "text-ovwrt"
712 artist-text-overwrite
716 ("Spray-can" (("spray-can" spray-can "spray-can"
719 artist-do-continously
721 (artist-spray-get-interval))
722 ("spray set size" spray-get-size "spray-size"
724 nil artist-spray-clear-circle artist-spray-set-radius
727 (artist-undraw-circle
731 ("Erase" (("erase char" erase-char "erase-c"
734 artist-do-continously
737 ("erase rectangle" erase-rect "erase-r"
743 artist-t artist-erase-rect)))))
746 ("Vaporize" (("vaporize line" vaporize-line "vaporize-1"
752 ("vaporize lines" vaporize-lines "vaporize-n"
756 artist-vaporize-lines
762 ("Cut" (("cut rectangle" cut-r "cut-r"
768 artist-t artist-cut-rect))
769 ("cut square" cut-s "cut-s"
774 (artist-undraw-square
775 artist-t artist-cut-square)))))
778 ("Copy" (("copy rectangle" copy-r "copy-r"
784 artist-t artist-copy-rect))
785 ("copy square" copy-s "copy-s"
790 (artist-undraw-square
791 artist-t artist-copy-square)))))
794 ("Paste" (("paste" paste "paste"
800 ("paste" paste "paste"
808 ("Flood-fill" (("flood-fill" flood-fill "flood"
814 ("flood-fill" flood-fill "flood"
824 ("Set Fill" set-fill artist-select-fill-char))
827 ("Set Line" set-line artist-select-line-char))
830 ("Set Erase" set-erase artist-select-erase-char))
833 ("Rubber-banding" rubber-band artist-toggle-rubber-banding))
836 ("Trimming" trimming artist-toggle-trim-line-endings))
839 ("Borders" borders artist-toggle-borderless-shapes))
842 ("Spray-chars" spray-chars artist-select-spray-chars)))))
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
851 The master table is a list of table elements. The elements of this table
856 There are three kinds of TAG:
859 `separator' -- produce a separator in the popup menu
860 `function-call' -- call a function
861 `graphics-operation' -- a graphics operation
863 The layout of the INFO-PART for `menu' is
865 (TITLE ((TAG-1 INFO-PART-1) (TAG-2 INFO-PART-2) ...))
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).
871 The layout of the INFO-PART for `separator' is empty and not used.
874 This is the layout of the INFO-PART for `function-call':
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.
884 The layout of the INFO-PART for `graphics-operation' is
886 (TITLE (UNSHIFTED SHIFTED))
888 TITLE is the title that appears in the popup menu. UNSHIFTED
889 and SHIFTED specify for unshifted and shifted operation. Both
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)
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
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
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':
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
936 If DRAW-HOW is either `artist-do-poly' or 2:
938 (UNDRAW-FN FILL-PRED FILL-FN)
940 UNDRAW-FN is a function to call for undrawing the shape.
941 Arguments and return values for this function are
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
946 FILL-FN is a function to call for filling the shape.
947 Arguments and return values for this function are
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.
957 The following table describe function arguments and return value
958 for different functions and DRAW-HOWs.
960 If DRAW-HOW is either `artist-do-continously' or 1:
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
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
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.
983 If DRAW-HOW is `artist-do-poly':
986 PREP-FILL-FN POINT-LIST
987 ARROW-SET-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)
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].")
1001 ;; Accessors for the master table
1004 (defun artist-mt-get-tag (element)
1005 "Retrieve the tag component from the master table ELEMENT."
1008 (defun artist-mt-get-info-part (element)
1009 "Retrieve the info part component from the master table ELEMENT."
1012 ;; For the 'graphics-operation info-parts
1014 (defsubst artist-go-get-desc (info-part)
1015 "Retrieve the description component from a graphics operation INFO-PART."
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
1104 ;; For the 'function-call info-parts
1106 (defsubst artist-fc-get-keyword (info-part)
1107 "Retrieve the keyword component from a graphics operation INFO-PART."
1110 (defsubst artist-fc-get-symbol (info-part)
1111 "Retrieve the symbol component from a graphics operation INFO-PART."
1114 (defsubst artist-fc-get-fn (info-part)
1115 "Retrieve the function component from a graphics operation INFO-PART."
1118 ;; For the 'menu info-parts
1120 (defsubst artist-mn-get-title (info-part)
1121 "Retrieve the title component from a graphics operation INFO-PART."
1124 (defsubst artist-mn-get-items (info-part)
1125 "Retrieve the items component from a graphics operation INFO-PART."
1128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1129 ;; mouse wheel cyclic operation selection
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."
1135 (artist-get-last-non-nil-op (cdr op-list)
1136 (or (car (car op-list)) last-non-nil))
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))))
1143 (defun artist-is-in-op-list-p (op op-list)
1144 "Check whether OP is in 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)))))
1149 (defun artist-make-prev-next-op-alist (op-list
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
1171 (if prev-entry (setcdr (cdr prev-entry) op))
1173 (cons entry (artist-make-prev-next-op-alist
1175 last-non-nil first-non-nil
1177 (progn (setcdr (cdr prev-entry) first-non-nil) nil)))))
1179 (defun artist-select-next-op-in-list ()
1180 "Cyclically select next drawing mode operation."
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)))
1186 (defun artist-select-prev-op-in-list ()
1187 "Cyclically select previous drawing mode operation."
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)))
1193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1195 ;;; ---------------------------------
1197 ;;; ---------------------------------
1200 (defun artist-mode (&optional state)
1201 "Toggle Artist mode.
1202 With argument STATE, turn Artist mode on if STATE is positive.
1203 Artist lets you draw lines, squares, rectangles and poly-lines,
1204 ellipses and circles with your mouse and/or keyboard.
1206 How to quit Artist mode
1208 Type \\[artist-mode-off] to quit artist-mode.
1211 How to submit a bug report
1213 Type \\[artist-submit-bug-report] to submit a bug report.
1216 Drawing with the mouse:
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
1224 shift mouse-1 Draws lines, rectangles or poly-lines, erases, cuts, copies
1227 Operation Not shifted Shifted
1228 --------------------------------------------------------------
1229 Pen fill-char at point line from last 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
1248 --------------------------------------------------------------
1249 Cut Cut rectangle Cut square
1250 --------------------------------------------------------------
1251 Copy Copy rectangle Copy square
1252 --------------------------------------------------------------
1254 --------------------------------------------------------------
1255 Flood-fill Flood-fill Flood-fill
1256 --------------------------------------------------------------
1258 * Straight lines can only go horizontally, vertically
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
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.
1271 * Vaporizing connected lines only vaporizes lines whose
1272 _endpoints_ are connected. See also the variable
1273 `artist-vaporize-fuzziness'.
1275 * Cut copies, then clears the rectangle/square.
1277 * When drawing lines or poly-lines, you can set arrows.
1278 See below under ``Arrows'' for more info.
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.
1284 * Be patient when flood-filling -- large areas take quite
1288 mouse-3 Erases character under pointer
1289 shift mouse-3 Erases rectangle
1294 Set fill Sets the character used when filling rectangles/squares
1296 Set line Sets the character used when drawing lines
1298 Erase char Sets the character used when erasing
1300 Rubber-banding Toggles rubber-banding
1302 Trimming Toggles trimming of line-endings (that is: when the shape
1303 is drawn, extraneous white-space at end of lines is removed)
1305 Borders Toggles the drawing of line borders around filled shapes
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
1317 \\[artist-select-operation] Selects what to draw
1319 Move around with \\[artist-next-line], \\[artist-previous-line], \\[artist-forward-char] and \\[artist-backward-char].
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
1331 \\[artist-toggle-first-arrow] Sets/unsets an arrow at the beginning
1332 of the line/poly-line
1334 \\[artist-toggle-second-arrow] Sets/unsets an arrow at the end
1335 of the line/poly-line
1340 There are some keys for quickly selecting drawing operations:
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
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).
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''
1391 When entering artist-mode, the hook `artist-mode-init-hook' is called.
1392 When quitting artist-mode, the hook `artist-mode-exit-hook' is called.
1397 \\{artist-mode-map}"
1399 (if (setq artist-mode
1400 (if (null state) (not artist-mode)
1401 (> (prefix-numeric-value state) 0)))
1403 (artist-mode-exit)))
1405 ;; insert our minor mode string
1406 (or (assq 'artist-mode minor-mode-alist)
1407 (setq minor-mode-alist
1408 (cons '(artist-mode artist-mode-name)
1411 ;; insert our minor mode keymap
1412 (or (assq 'artist-mode minor-mode-map-alist)
1413 (setq minor-mode-map-alist
1414 (cons (cons 'artist-mode artist-mode-map)
1415 minor-mode-map-alist)))
1419 (defun artist-mode-init ()
1420 "Init Artist mode. This will call the hook `artist-mode-init-hook'."
1421 ;; Set up a conversion table for mapping tabs and new-lines to spaces.
1422 ;; the last case, 0, is for the last position in buffer/region, where
1423 ;; the `following-char' function returns 0.
1426 (aset artist-replacement-table i i)
1428 (aset artist-replacement-table ?\n ?\s)
1429 (aset artist-replacement-table ?\t ?\s)
1430 (aset artist-replacement-table 0 ?\s)
1432 (make-local-variable 'artist-key-is-drawing)
1433 (make-local-variable 'artist-key-endpoint1)
1434 (make-local-variable 'artist-key-poly-point-list)
1435 (make-local-variable 'artist-key-shape)
1436 (make-local-variable 'artist-key-draw-how)
1437 (make-local-variable 'artist-popup-menu-table)
1438 (make-local-variable 'artist-key-compl-table)
1439 (make-local-variable 'artist-prev-next-op-alist)
1440 (make-local-variable 'artist-rb-save-data)
1441 (make-local-variable 'artist-arrow-point-1)
1442 (make-local-variable 'artist-arrow-point-2)
1443 (setq artist-key-is-drawing nil)
1444 (setq artist-key-endpoint1 nil)
1445 (setq artist-key-poly-point-list nil)
1446 (setq artist-key-shape nil)
1447 (setq artist-popup-menu-table (artist-compute-popup-menu-table artist-mt))
1448 (setq artist-key-compl-table (artist-compute-key-compl-table artist-mt))
1449 (setq artist-prev-next-op-alist
1450 (artist-make-prev-next-op-alist artist-key-compl-table))
1451 (setq artist-rb-save-data (make-vector 7 0))
1452 (setq artist-arrow-point-1 nil)
1453 (setq artist-arrow-point-2 nil)
1454 (make-local-variable 'next-line-add-newlines)
1455 (setq next-line-add-newlines t)
1456 (setq artist-key-draw-how
1457 (artist-go-get-draw-how-from-symbol artist-curr-go))
1458 (if (and artist-picture-compatibility (not (eq major-mode 'picture-mode)))
1462 (run-hooks 'artist-mode-init-hook)
1463 (artist-mode-line-show-curr-operation artist-key-is-drawing))
1465 (defun artist-mode-exit ()
1466 "Exit Artist mode. This will call the hook `artist-mode-exit-hook'."
1467 (if (and artist-picture-compatibility (eq major-mode 'picture-mode))
1468 (picture-mode-exit))
1469 (kill-local-variable 'next-line-add-newlines)
1470 (run-hooks 'artist-mode-exit-hook))
1472 (defun artist-mode-off ()
1473 "Turn Artist mode off."
1476 (force-mode-line-update))
1482 (defun artist-update-display ()
1483 "Repaint the display."
1486 (defun artist-mode-line-show-curr-operation (is-drawing)
1487 "Show current operation in mode-line. If IS-DRAWING, show that."
1488 (let ((mtext (concat artist-mode-name "/"
1489 (artist-go-get-mode-line-from-symbol artist-curr-go)
1490 (if is-drawing "/*" ""))))
1491 (setcdr (assq 'artist-mode minor-mode-alist) (list mtext)))
1492 (force-mode-line-update))
1495 (defun artist-t-if-fill-char-set ()
1496 "Return the value of the variable `artist-fill-char-set'."
1497 artist-fill-char-set)
1503 (defun artist-nil ()
1504 "Always return nil."
1507 (defun artist-arrows ()
1508 "Say yes to arrows!"
1511 (defun artist-no-arrows ()
1516 ;; Auxiliary init-routines
1520 ; Computing the table for the x-popup-menu from the master table
1523 (defun artist-compute-popup-menu-table (menu-table)
1524 "Create a menu from MENU-TABLE data.
1525 The returned value is suitable for the `x-popup-menu' function."
1527 (artist-compute-popup-menu-table-sub menu-table)))
1529 (defun artist-compute-popup-menu-table-sub (menu-table)
1530 "Compute operation table suitable for `x-popup-menu' from MENU-TABLE."
1533 (let ((element-tag (artist-mt-get-tag element)))
1534 (cond ((eq element-tag 'graphics-operation)
1535 (let* ((info-part (artist-mt-get-info-part element))
1536 (descr (artist-go-get-desc info-part))
1537 (unshifted (artist-go-get-unshifted info-part))
1538 (symbol (artist-go-get-symbol unshifted)))
1539 (list descr symbol)))
1541 ((eq element-tag 'function-call)
1542 (let* ((info-part (artist-mt-get-info-part element))
1543 (keyword (artist-fc-get-keyword info-part))
1544 (symbol (artist-fc-get-symbol info-part)))
1545 (list keyword symbol)))
1547 ((eq element-tag 'separator)
1550 ((eq element-tag 'menu)
1551 (let* ((info-part (artist-mt-get-info-part element))
1552 (title (artist-mn-get-title info-part))
1553 (items (artist-mn-get-items info-part)))
1554 (cons title (artist-compute-popup-menu-table-sub items))))
1557 (error "Internal error: unknown element-tag: \"%s\""
1562 ; Computing the completion table from the master table
1565 (defun artist-compute-key-compl-table (menu-table)
1566 "Compute completion table from MENU-TABLE, suitable for `completing-read'."
1572 (let ((element-tag (artist-mt-get-tag element)))
1573 (cond ((eq element-tag 'graphics-operation)
1574 (let* ((info-part (artist-mt-get-info-part element))
1575 (unshifted (artist-go-get-unshifted info-part))
1576 (shifted (artist-go-get-shifted info-part))
1577 (unshifted-kwd (artist-go-get-keyword unshifted))
1578 (shifted-kwd (artist-go-get-keyword shifted)))
1579 (list (list unshifted-kwd) (list shifted-kwd))))
1580 ((eq element-tag 'menu)
1581 (let* ((info-part (artist-mt-get-info-part element))
1582 (items (artist-mn-get-items info-part)))
1583 (artist-compute-key-compl-table items)))
1590 ; Retrieving a symbol (graphics operation or function-call) from a keyword
1593 (defun artist-mt-get-symbol-from-keyword (kwd)
1594 "Search master table for keyword KWD and return its symbol."
1595 (artist-mt-get-symbol-from-keyword-sub artist-mt kwd))
1597 (defun artist-mt-get-symbol-from-keyword-sub (table kwd)
1598 "Search TABLE for keyword KWD and return its symbol."
1602 (let ((element-tag (artist-mt-get-tag element)))
1603 (cond ((eq element-tag 'graphics-operation)
1604 (let* ((info-part (artist-mt-get-info-part element))
1605 (unshifted (artist-go-get-unshifted info-part))
1606 (shifted (artist-go-get-shifted info-part))
1607 (unshifted-kwd (artist-go-get-keyword unshifted))
1608 (shifted-kwd (artist-go-get-keyword shifted))
1609 (unshifted-sym (artist-go-get-symbol unshifted))
1610 (shifted-sym (artist-go-get-symbol shifted)))
1611 (if (string-equal kwd unshifted-kwd)
1612 (throw 'found unshifted-sym))
1613 (if (string-equal kwd shifted-kwd)
1614 (throw 'found shifted-sym))))
1616 ((eq element-tag 'function-call)
1617 (let* ((info-part (artist-mt-get-info-part element))
1618 (keyword (artist-fc-get-keyword info-part))
1619 (symbol (artist-fc-get-symbol info-part)))
1620 (if (string-equal kwd keyword)
1621 (throw 'found symbol))))
1622 ((eq element-tag 'menu)
1623 (let* ((info-part (artist-mt-get-info-part element))
1624 (items (artist-mn-get-items info-part))
1625 (answer (artist-mt-get-symbol-from-keyword-sub
1627 (if answer (throw 'found answer))))
1635 ; Retrieving info from a graphics operation symbol
1638 (defun artist-go-retrieve-from-symbol (symbol retrieve-fn)
1639 "Search the master table for a graphics operation SYMBOL.
1640 Calls RETRIEVE-FN to retrieve information from that symbol's
1642 (artist-go-retrieve-from-symbol-sub artist-mt symbol retrieve-fn))
1644 (defun artist-go-retrieve-from-symbol-sub (table symbol retrieve-fn)
1645 "Search the TABLE for a graphics operation SYMBOL.
1646 Calls RETRIEVE-FN to retrieve information from that symbol's
1651 (let ((element-tag (artist-mt-get-tag element)))
1652 (cond ((eq element-tag 'graphics-operation)
1653 (let* ((info-part (artist-mt-get-info-part element))
1654 (unshifted (artist-go-get-unshifted info-part))
1655 (shifted (artist-go-get-shifted info-part))
1656 (unshifted-sym (artist-go-get-symbol unshifted))
1657 (shifted-sym (artist-go-get-symbol shifted))
1659 ((eq unshifted-sym symbol) unshifted)
1660 ((eq shifted-sym symbol) shifted)
1662 (if variant-part ; if found do:
1663 (throw 'found (funcall retrieve-fn variant-part)))))
1665 ((eq element-tag 'menu)
1666 (let* ((info-part (artist-mt-get-info-part element))
1667 (items (artist-mn-get-items info-part))
1668 (answer (artist-go-retrieve-from-symbol-sub
1669 items symbol retrieve-fn)))
1670 (if answer (throw 'found answer)))))))
1675 (defun artist-go-get-keyword-from-symbol (symbol)
1676 "Search the master table, get keyword from a graphics operation SYMBOL."
1677 (artist-go-retrieve-from-symbol symbol 'artist-go-get-keyword))
1679 (defun artist-go-get-mode-line-from-symbol (symbol)
1680 "Search the master table, get mode-line from a graphics operation SYMBOL."
1681 (artist-go-retrieve-from-symbol symbol 'artist-go-get-mode-line))
1683 (defun artist-go-get-arrow-pred-from-symbol (symbol)
1684 "Search the master table, get arrow-pred from a graphics operation SYMBOL."
1685 (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-pred))
1687 (defun artist-go-get-arrow-set-fn-from-symbol (symbol)
1688 "Search the master table, get arrow-set-fn from a graphics operation SYMBOL."
1689 (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-set-fn))
1691 (defun artist-go-get-init-fn-from-symbol (symbol)
1692 "Search the master table, get init-fn from a graphics operation SYMBOL."
1693 (artist-go-retrieve-from-symbol symbol 'artist-go-get-init-fn))
1695 (defun artist-go-get-prep-fill-fn-from-symbol (symbol)
1696 "Search the master table, get prep-fill-fn from a graphics operation SYMBOL."
1697 (artist-go-retrieve-from-symbol symbol 'artist-go-get-prep-fill-fn))
1699 (defun artist-go-get-exit-fn-from-symbol (symbol)
1700 "Search the master table, get exit-fn from a graphics operation SYMBOL."
1701 (artist-go-retrieve-from-symbol symbol 'artist-go-get-exit-fn))
1703 (defun artist-go-get-draw-fn-from-symbol (symbol)
1704 "Search the master table, get draw-fn from a graphics operation SYMBOL."
1705 (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-fn))
1707 (defun artist-go-get-draw-how-from-symbol (symbol)
1708 "Search the master table, get draw-how from a graphics operation SYMBOL."
1709 (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-how))
1711 (defun artist-go-get-undraw-fn-from-symbol (symbol)
1712 "Search the master table, get undraw-fn from a graphics operation SYMBOL."
1713 (artist-go-retrieve-from-symbol symbol 'artist-go-get-undraw-fn))
1715 (defun artist-go-get-interval-fn-from-symbol (symbol)
1716 "Search the master table, get interval-fn from a graphics operation SYMBOL."
1717 (artist-go-retrieve-from-symbol symbol 'artist-go-get-interval-fn))
1719 (defun artist-go-get-fill-pred-from-symbol (symbol)
1720 "Search the master table, get fill-pred from a graphics operation SYMBOL."
1721 (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-pred))
1723 (defun artist-go-get-fill-fn-from-symbol (symbol)
1724 "Search the master table, get fill-fn from a graphics operation SYMBOL."
1725 (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-fn))
1727 (defun artist-go-get-symbol-shift (symbol is-shifted)
1728 "Search for (shifted or unshifted) graphics operation SYMBOL.
1729 If IS-SHIFTED is non-nil, return the shifted symbol,
1730 otherwise the unshifted symbol."
1731 (artist-go-get-symbol-shift-sub artist-mt symbol is-shifted))
1733 (defun artist-go-get-symbol-shift-sub (table symbol is-shifted)
1734 "Search TABLE for (shifted or unshifted) graphics SYMBOL.
1735 If IS-SHIFTED is non-nil, return the shifted symbol,
1736 otherwise the unshifted symbol."
1740 (let ((element-tag (artist-mt-get-tag element)))
1741 (cond ((eq element-tag 'graphics-operation)
1742 (let* ((info-part (artist-mt-get-info-part element))
1743 (unshift-variant (artist-go-get-unshifted info-part))
1744 (shift-variant (artist-go-get-shifted info-part))
1745 (unshift-sym (artist-go-get-symbol unshift-variant))
1746 (shift-sym (artist-go-get-symbol shift-variant)))
1747 (if (or (eq symbol unshift-sym) (eq symbol shift-sym))
1748 (throw 'found (if is-shifted shift-sym unshift-sym)))))
1750 ((eq element-tag 'menu)
1751 (let* ((info-part (artist-mt-get-info-part element))
1752 (items (artist-mn-get-items info-part))
1753 (answer (artist-go-get-symbol-shift-sub
1754 items symbol is-shifted)))
1755 (if answer (throw 'found answer)))))))
1761 ; Retrieving info from a function-call symbol
1764 (defun artist-fc-retrieve-from-symbol (symbol retrieve-fn)
1765 "Search the master table for a function call SYMBOL.
1766 Calls RETRIEVE-FN to retrieve information from that symbol's
1768 (artist-fc-retrieve-from-symbol-sub artist-mt symbol retrieve-fn))
1770 (defun artist-fc-retrieve-from-symbol-sub (table symbol retrieve-fn)
1771 "Search TABLE for a function-call SYMBOL.
1772 Calls RETRIEVE-FN to retrieve information from that symbol's
1777 (let ((element-tag (artist-mt-get-tag element)))
1778 (cond ((eq element-tag 'function-call)
1779 (let* ((info-part (artist-mt-get-info-part element))
1780 (fc-symbol (artist-fc-get-symbol info-part)))
1781 (if (eq fc-symbol symbol)
1782 (throw 'found (funcall retrieve-fn info-part)))))
1784 ((eq element-tag 'menu)
1785 (let* ((info-part (artist-mt-get-info-part element))
1786 (items (artist-mn-get-items info-part))
1787 (answer (artist-fc-retrieve-from-symbol-sub
1788 items symbol retrieve-fn)))
1789 (if answer (throw 'found answer)))))))
1794 (defun artist-fc-get-fn-from-symbol (symbol)
1795 "Search the master table to get function from a function call SYMBOL."
1796 (artist-fc-retrieve-from-symbol symbol 'artist-fc-get-fn))
1803 ;; Macro that won't funcall the function if it is nil.
1805 (defmacro artist-funcall (fn &rest args)
1806 "Call function FN with ARGS, if FN is not nil."
1807 (list 'if fn (cons 'funcall (cons fn args))))
1809 (defun artist-uniq (l)
1810 "Remove consecutive duplicates in list L. Comparison is done with `equal'."
1811 (cond ((null l) nil)
1812 ((null (cdr l)) l) ; only one element in list
1813 ((equal (car l) (car (cdr l))) (artist-uniq (cdr l))) ; first 2 equal
1814 (t (cons (car l) (artist-uniq (cdr l)))))) ; first 2 are different
1816 (defun artist-string-split (str r)
1817 "Split string STR at occurrences of regexp R, returning a list of strings."
1821 (while (setq match-pos (string-match r str start))
1822 (setq res (cons (copy-sequence (substring str start match-pos)) res))
1823 (setq start (match-end 0)))
1826 (if (< (match-end 0) (- (length str) 1))
1827 (setq res (cons (substring str (match-end 0) (length str)) res)))
1830 (defun artist-string-to-file (str file-name)
1831 "Write string STR to file FILE-NAME."
1832 (write-region str 'end-is-ignored file-name nil 'no-message))
1834 (defun artist-file-to-string (file-name)
1835 "Read from file FILE-NAME into a string."
1837 (let ((tmp-buffer (get-buffer-create (concat "*artist-" file-name "*"))))
1838 (set-buffer tmp-buffer)
1839 (goto-char (point-min))
1840 (insert-file-contents file-name nil nil nil t)
1841 (let ((str (copy-sequence (buffer-substring (point-min)
1843 (kill-buffer tmp-buffer)
1846 (defun artist-clear-buffer (buf)
1847 "Clear contents of buffer BUF."
1848 (with-current-buffer buf
1849 (goto-char (point-min))
1850 (delete-char (- (point-max) (point-min)) nil)))
1853 (defun artist-system (program stdin &optional program-args)
1854 "Run PROGRAM synchronously with the contents of string STDIN to stdin.
1855 Optional args PROGRAM-ARGS are arguments to PROGRAM.
1856 Return a list (RETURN-CODE STDOUT STDERR)."
1858 (let* ((tmp-stdin-file-name (if stdin
1859 (make-temp-file "artist-stdin.")
1861 (tmp-stdout-buffer (get-buffer-create
1862 (concat "*artist-" program "*")))
1863 (tmp-stderr-file-name (make-temp-file "artist-stdout."))
1864 (binary-process-input nil) ; for msdos
1865 (binary-process-output nil))
1868 (if stdin (artist-string-to-file stdin tmp-stdin-file-name))
1871 (artist-clear-buffer tmp-stdout-buffer)
1873 ;; Start the program
1875 (let ((res (if program-args
1876 (apply 'call-process
1879 (list tmp-stdout-buffer
1880 tmp-stderr-file-name)
1882 (if (stringp program-args)
1885 (apply 'call-process
1888 (list tmp-stdout-buffer
1889 tmp-stderr-file-name)
1894 (with-current-buffer tmp-stdout-buffer
1895 (buffer-substring (point-min) (point-max)))
1896 (artist-file-to-string tmp-stderr-file-name)))
1898 ;; Unwind: remove temporary files and buffers
1899 (if (and stdin (file-exists-p tmp-stdin-file-name))
1900 (delete-file tmp-stdin-file-name))
1901 (if (file-exists-p tmp-stderr-file-name)
1902 (delete-file tmp-stderr-file-name))
1903 (if (memq tmp-stdout-buffer (buffer-list))
1904 (kill-buffer tmp-stdout-buffer))))))
1906 ;; Routines that deal with the buffer
1908 ;; artist-current-line get line number (top of buffer is 0)
1910 ;; artist-move-to-xy move to (x,y) (0,0) is beg-of-buffer
1912 ;; artist-get-char-at-xy get char in at (x,y)
1914 ;; artist-replace-char overwrite (replace) char at point
1915 ;; artist-replace-chars overwrite (replace) chars at point
1918 (defsubst artist-current-column ()
1919 "Return point's current column."
1922 (defsubst artist-current-line ()
1923 "Return point's current line, buffer-relative. Top of buffer is 0."
1924 (+ (count-lines 1 (point))
1925 (if (= (current-column) 0) 1 0)
1928 (defsubst artist-move-to-xy (x y)
1929 "Move to column X, at row Y from the top of buffer. Top line is 0."
1931 ;; Q: Why do we do forward-line twice?
1932 ;; A: The documentation for forward-line says
1934 ;; "... Returns the count of lines left to move. ... With
1935 ;; positive N, a non-empty line at the end counts as one
1936 ;; line successfully moved (for the return value)."
1938 ;; This means that if we are trying to move forward past the end
1939 ;; of the buffer, and that last line happened to be longer than
1940 ;; the current column, then we end up at the end of that last
1941 ;; line, and forward-line returns one less than we actually
1944 ;; Example: In the figure below, the `X' is the very last
1945 ;; character in the buffer ("a non-empty line at the
1946 ;; end"). Suppose point is at P. Then (forward-line 1)
1947 ;; returns 0 and puts point after the `X'.
1949 ;; --------top of buffer--------
1952 ;; -------bottom of buffer------
1954 ;; But, if we are at the end of buffer when trying to move
1955 ;; forward, then forward-line will return the (for us) correct
1956 ;; value, which is good, because we will come to the end of the
1957 ;; buffer by the first forward-line. The second forward-line
1958 ;; will then get us where we really wanted to go.
1960 ;; If we are not moving past the end of the buffer, then the
1961 ;; second forward-line will return 0.
1963 ;; Q: What happens if we are moving upwards?
1964 ;; A: That will work good. insert-char won't insert a negative
1965 ;; number of chars, and forward-line will fail silently if we are
1966 ;; moving past the beginning of the buffer.
1968 (forward-line (- y (artist-current-line)))
1969 (insert-char ?\n (forward-line (- y (artist-current-line))))
1970 (move-to-column (max x 0) t)
1971 (let ((curr-y (artist-current-line)))
1972 (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y))
1973 (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y))))
1975 (defsubst artist-get-char-at-xy (x y)
1976 "Return the character found at column X, row Y.
1977 Also updates the variables `artist-draw-min-y' and `artist-draw-max-y'."
1978 (artist-move-to-xy x y)
1979 (let ((curr-y (artist-current-line)))
1980 (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y))
1981 (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y)))
1985 (defsubst artist-get-replacement-char (c)
1986 "Retrieve a replacement for character C from `artist-replacement-table'.
1987 The replacement is used to convert tabs and new-lines to spaces."
1988 ;; Characters may be outside the range of the `artist-replacement-table',
1989 ;; for example if they are Unicode code points >= 256.
1990 ;; Check so we don't attempt to access the array out of its bounds,
1991 ;; assuming no such character needs to be replaced.
1992 (if (< c (length artist-replacement-table))
1993 (aref artist-replacement-table c)
1996 (defun artist-get-char-at-xy-conv (x y)
1997 "Retrieve the character at X, Y, converting tabs and new-lines to spaces."
1999 (artist-get-replacement-char (artist-get-char-at-xy x y))))
2002 (defun artist-replace-char (new-char)
2003 "Replace the character at point with NEW-CHAR."
2004 (let ((overwrite-mode 'overwrite-mode-textual)
2005 (fill-column 32765) ; Large :-)
2006 (blink-matching-paren nil))
2007 (setq last-command-event (artist-get-replacement-char new-char))
2008 (self-insert-command 1)))
2010 (defun artist-replace-chars (new-char count)
2011 "Replace characters at point with NEW-CHAR. COUNT chars are replaced."
2012 ;; Check that the variable exists first. The doc says it was added in 19.23.
2013 (if (and (and (boundp 'emacs-major-version) (= emacs-major-version 20))
2014 (and (boundp 'emacs-minor-version) (<= emacs-minor-version 3)))
2015 ;; This is a bug workaround for Emacs 20, versions up to 20.3:
2016 ;; The self-insert-command doesn't care about the overwrite-mode,
2017 ;; so the insertion is done in the same way as in picture mode.
2018 ;; This seems to be a little bit slower.
2019 (let* ((replaced-c (artist-get-replacement-char new-char))
2020 (replaced-s (make-string count replaced-c)))
2021 (artist-move-to-xy (+ (artist-current-column) count)
2022 (artist-current-line))
2023 (delete-char (- count))
2024 (insert replaced-s))
2025 ;; In emacs-19, the self-insert-command works better
2026 (let ((overwrite-mode 'overwrite-mode-textual)
2027 (fill-column 32765) ; Large :-)
2028 (blink-matching-paren nil))
2029 (setq last-command-event (artist-get-replacement-char new-char))
2030 (self-insert-command count))))
2032 (defsubst artist-replace-string (string &optional see-thru)
2033 "Replace contents at point with STRING.
2034 With optional argument SEE-THRU set to non-nil, text in the buffer
2035 ``shines thru'' blanks in the STRING."
2036 (let ((char-list (append string nil)) ; convert the string to a list
2037 (overwrite-mode 'overwrite-mode-textual)
2038 (fill-column 32765) ; Large :-)
2039 (blink-matching-paren nil))
2041 (let ((c (car char-list)))
2042 (if (and see-thru (= (artist-get-replacement-char c) ?\s))
2043 (artist-move-to-xy (1+ (artist-current-column))
2044 (artist-current-line))
2045 (artist-replace-char c)))
2046 (setq char-list (cdr char-list)))))
2049 ;; Routines for setting and unsetting points
2050 ;; Used when not rubber-banding
2052 (defun artist-no-rb-unset-point1 ()
2053 "Unsets point 1 when not rubber-banding."
2054 (let ((x-now (artist-current-column))
2055 (y-now (artist-current-line))
2056 (x (aref artist-rb-save-data 0))
2057 (y (aref artist-rb-save-data 1)))
2058 (artist-move-to-xy x y)
2059 (artist-replace-char (aref artist-rb-save-data 2))
2060 (artist-move-to-xy x-now y-now)))
2062 (defun artist-no-rb-set-point1 (x y)
2063 "Set point 1 at X, Y when not rubber-banding."
2064 (let ((x-now (artist-current-column))
2065 (y-now (artist-current-line)))
2066 (aset artist-rb-save-data 0 x)
2067 (aset artist-rb-save-data 1 y)
2068 (aset artist-rb-save-data 2 (artist-get-char-at-xy x y))
2069 (artist-move-to-xy x y)
2070 (artist-replace-char artist-first-char)
2071 (artist-move-to-xy x-now y-now)
2072 (aset artist-rb-save-data 6 0)))
2074 (defun artist-no-rb-unset-point2 ()
2075 "This function unsets point 2 when not rubber-banding."
2076 (if (= (aref artist-rb-save-data 6) 1)
2077 (let ((x-now (artist-current-column))
2078 (y-now (artist-current-line))
2079 (x (aref artist-rb-save-data 3))
2080 (y (aref artist-rb-save-data 4)))
2081 (artist-move-to-xy x y)
2082 (artist-replace-char (aref artist-rb-save-data 5))
2083 (artist-move-to-xy x-now y-now))))
2085 (defun artist-no-rb-set-point2 (x y)
2086 "Set point 2 at X, Y when not rubber-banding."
2087 (let ((x-now (artist-current-column))
2088 (y-now (artist-current-line)))
2089 (aset artist-rb-save-data 3 x)
2090 (aset artist-rb-save-data 4 y)
2091 (aset artist-rb-save-data 5 (artist-get-char-at-xy x y))
2092 (artist-move-to-xy x y)
2093 (artist-replace-char artist-second-char)
2094 (artist-move-to-xy x-now y-now)
2095 (aset artist-rb-save-data 6 1)))
2097 (defun artist-no-rb-unset-points ()
2098 "This function unsets point 1 and 2 when not rubber-banding."
2099 (artist-no-rb-unset-point1)
2100 (artist-no-rb-unset-point2))
2103 ;; artist-intersection-char
2105 ;; Note: If changing this, see the notes for artist-unintersection-char
2106 ;; and artist-vaporize-lines
2108 (defun artist-intersection-char (new-c old-c)
2109 "Calculates intersection character when drawing a NEW-C on top of an OLD-C.
2110 Return character according to this scheme:
2121 other combinations NEW-C"
2123 (cond ((and (= old-c ?- ) (= new-c ?| )) ?+ )
2124 ((and (= old-c ?| ) (= new-c ?- )) ?+ )
2125 ((and (= old-c ?+ ) (= new-c ?- )) ?+ )
2126 ((and (= old-c ?+ ) (= new-c ?| )) ?+ )
2127 ((and (= old-c ?\\ ) (= new-c ?/ )) ?X )
2128 ((and (= old-c ?/ ) (= new-c ?\\ )) ?X )
2129 ((and (= old-c ?X ) (= new-c ?/ )) ?X )
2130 ((and (= old-c ?X ) (= new-c ?\\ )) ?X )
2133 ;; artist-unintersection-char
2135 ;; Note: If changing this, see the note for artist-vaporize-lines
2137 (defun artist-unintersection-char (line-c buffer-c)
2138 "Restore character to before intersection when removing LINE-C from BUFFER-C.
2139 Return character according to this scheme:
2141 LINE-C BUFFER-C return
2146 other combinations `artist-erase-char'."
2148 (cond ((and (= line-c ?- ) (= buffer-c ?+ )) ?| )
2149 ((and (= line-c ?| ) (= buffer-c ?+ )) ?- )
2150 ((and (= line-c ?\\ ) (= buffer-c ?X )) ?/ )
2151 ((and (= line-c ?/ ) (= buffer-c ?X )) ?\\ )
2152 ((= line-c buffer-c) artist-erase-char)
2156 ;; Computing the line-char to use
2157 ;; for use with borderless shapes
2159 (defsubst artist-compute-line-char ()
2160 "Compute which character to use for lines, if any.
2161 Return value is either nil for the default characters that make up lines, or
2162 a character chosen depending on the variables `artist-borderless-shapes',
2163 `artist-fill-char-set', `artist-fill-char' and
2164 `artist-line-char-set' and `artist-line-char'."
2165 (if (and artist-borderless-shapes artist-fill-char-set)
2167 (if artist-line-char-set
2172 ;; Things for drawing horizontal, vertical and diagonal (straight) lines.
2174 ;; A line here is a vector:
2175 ;; [ start-x start-y length direction saved-char-1 saved-char-2 ... ]
2176 ;; directions start with 0 at the x-axis and counts anti clockwise.
2178 (defvar artist-direction-info
2180 [ [ 1 0 ?- ] ; direction 0
2181 [ 1 1 ?\\ ] ; direction 1
2182 [ 0 1 ?| ] ; direction 2
2183 [ -1 1 ?/ ] ; direction 3
2184 [ -1 0 ?- ] ; direction 4
2185 [ -1 -1 ?\\ ] ; direction 5
2186 [ 0 -1 ?| ] ; direction 6
2187 [ 1 -1 ?/ ] ] ; direction 7
2188 "Table used for stepping x and y coordinates in a specific direction.
2189 This table is also used for determining which char to use for that direction.")
2191 (defsubst artist-direction-step-x (direction)
2192 "Return the x-step for DIRECTION from the `artist-direction-info' table."
2193 (aref (aref artist-direction-info direction) 0))
2195 (defsubst artist-direction-step-y (direction)
2196 "Return the y-step for DIRECTION from the `artist-direction-info' table."
2197 (aref (aref artist-direction-info direction) 1))
2199 (defun artist-direction-char (direction)
2200 "Return the character for DIRECTION from the `artist-direction-info' table."
2201 (aref (aref artist-direction-info direction) 2))
2203 ;; artist-find-direction
2207 (defun artist-find-direction (x1 y1 x2 y2)
2208 "Find the direction from point X1,Y1 to X2,Y2.
2209 Returns a DIRECTION, a number 0--7, coded as follows:
2216 (let ((delta-x (- x2 x1))
2217 (delta-y (- y2 y1)))
2218 (cond ((>= delta-x (* 2 (abs delta-y))) 0)
2219 ((>= delta-y (* 2 (abs delta-x))) 2)
2220 ((>= (- delta-x) (* 2 (abs delta-y))) 4)
2221 ((>= (- delta-y) (* 2 (abs delta-x))) 6)
2222 ((and (>= delta-x 0) (>= delta-y 0)) 1)
2223 ((and (<= delta-x 0) (>= delta-y 0)) 3)
2224 ((and (<= delta-x 0) (<= delta-y 0)) 5)
2225 ((and (>= delta-x 0) (<= delta-y 0)) 7))))
2227 (defun artist-straight-calculate-length (direction x1 y1 x2 y2)
2228 "Calculate length for a straight line in DIRECTION from X1,Y1 to X2,Y2."
2229 (cond ((or (= direction 7)
2231 (= direction 1)) (1+ (- x2 x1)))
2232 ((or (= direction 3)
2234 (= direction 5)) (1+ (- x1 x2)))
2235 (t (1+ (abs (- y2 y1))))))
2237 (defun artist-sline (x1 y1 x2 y2)
2238 "Create a straight line from X1,Y1 to X2,Y2."
2239 (let* ((direction (artist-find-direction x1 y1 x2 y2))
2240 (length (artist-straight-calculate-length direction x1 y1 x2 y2))
2241 (line (make-vector (+ length 4) x1)))
2244 ;; because we set all elements to x1
2246 (aset line 2 length)
2247 (aset line 3 direction)
2250 (defun artist-save-chars-under-sline (line)
2251 "Save characters under a LINE."
2252 (let ((x (aref line 0))
2254 (length (+ (aref line 2) 4))
2255 (direction (aref line 3))
2258 (aset line i (artist-get-char-at-xy x y))
2259 (setq x (+ x (artist-direction-step-x direction)))
2260 (setq y (+ y (artist-direction-step-y direction)))
2266 ;; Things for drawing lines in all directions.
2267 ;; The line drawing engine is the eight-point algorithm.
2269 ;; A line is here a list of (x y saved-char new-char)s.
2271 (defvar artist-octant-info
2272 ;; Initial Step in Step in
2273 ;; coeffs x and y x and y
2274 ;; for if q >= 0 if g < 0
2276 [ [ 2 1 1 0 1 1 ] ; 1st octant
2277 [ 1 2 1 1 0 1 ] ; 2nd octant
2278 [ -1 2 0 1 -1 1 ] ; 3rd octant
2279 [ -2 1 -1 1 -1 0 ] ; 4th octant
2280 [ -2 -1 -1 0 -1 -1 ] ; 5th octant
2281 [ -1 -2 -1 -1 0 -1 ] ; 6th octant
2282 [ 1 -2 0 -1 1 -1 ] ; 7th octant
2283 [ 2 -1 1 -1 1 0 ] ] ; 8th octant
2284 "Table used by line drawing algorithm (eight point).")
2286 ;; Primitives for the artist-octant-info.
2287 ;; Decrease octant by 1 since elt counts from 0 and octant counts from 1.
2289 (defsubst artist-get-dfdx-init-coeff (octant)
2290 "Retrieve dfdx component for OCTANT."
2291 (aref (aref artist-octant-info (- octant 1)) 0))
2293 (defsubst artist-get-dfdy-init-coeff (octant)
2294 "Retrieve dfdy component for OCTANT."
2295 (aref (aref artist-octant-info (- octant 1)) 1))
2297 (defsubst artist-get-x-step-q>=0 (octant)
2298 "Retrieve x-step component for OCTANT when q >= 0."
2299 (aref (aref artist-octant-info (- octant 1)) 2))
2301 (defsubst artist-get-y-step-q>=0 (octant)
2302 "Retrieve y-step component for OCTANT when q >= 0."
2303 (aref (aref artist-octant-info (- octant 1)) 3))
2305 (defsubst artist-get-x-step-q<0 (octant)
2306 "Retrieve x-step component for OCTANT for q < 0."
2307 (aref (aref artist-octant-info (- octant 1)) 4))
2309 (defsubst artist-get-y-step-q<0 (octant)
2310 "Retrieve y-step component for OCTANT for q < 0."
2311 (aref (aref artist-octant-info (- octant 1)) 5))
2314 ;; Find octant from x1 y1 x2 y2 coordinates.
2316 (defun artist-find-octant (x1 y1 x2 y2)
2317 "Find octant for a line from X1,Y1 to X2,Y2.
2318 Octant are numbered 1--8, anti-clockwise as:
2326 (if (<= x1 x2) ; quadrant 1 or 4
2327 (if (<= y1 y2) ; quadrant 1, octant 1 or 2
2328 (if (>= (- x2 x1) (- y2 y1))
2331 (if (>= (- x2 x1) (- (- y2 y1))) ; quadrant 4, octant 7 or 8
2334 (if (<= y1 y2) ; quadrant 2 or 3
2335 (if (>= (- (- x2 x1)) (- y2 y1)) ; quadrant 2, octant 3 or 4
2338 (if (>= (- (- x2 x1)) (- (- y2 y1))) ; quadrant 3, octant 5 or 6
2342 ;; Some inline functions for creating, setting and reading
2343 ;; members of a coordinate
2346 (defsubst artist-new-coord (x y &optional new-char)
2347 "Create a new coordinate at X,Y for use in a line.
2348 Optional argument NEW-CHAR can be used for setting the new-char component
2350 (let ((coord (make-vector 4 x)))
2352 (aset coord 3 new-char)
2355 (defsubst artist-coord-get-x (coord)
2356 "Retrieve the x component of a COORD."
2359 (defsubst artist-coord-get-y (coord)
2360 "Retrieve the y component of a COORD."
2363 (defsubst artist-coord-set-x (coord new-x)
2364 "Set the x component of a COORD to NEW-X."
2365 (aset coord 0 new-x)
2368 (defsubst artist-coord-set-y (coord new-y)
2369 "Set the y component of a COORD to NEW-Y."
2370 (aset coord 1 new-y)
2373 (defsubst artist-coord-get-saved-char (coord)
2374 "Retrieve the saved char component of a COORD."
2377 (defsubst artist-coord-get-new-char (coord)
2378 "Retrieve the new char component of a COORD."
2381 (defsubst artist-coord-add-saved-char (coord saved-char)
2382 "Set the saved char component of a COORD to SAVED-CHAR."
2383 (aset coord 2 saved-char)
2386 (defsubst artist-coord-add-new-char (coord new-char)
2387 "Set the new char component of a COORD to NEW-CHAR."
2388 (aset coord 3 new-char)
2391 (defsubst artist-coord-set-new-char (coord new-char)
2392 "Set the new char component of a COORD to NEW-CHAR."
2393 (aset coord 3 new-char)
2397 ;; Pretend we are plotting a pixel. Instead we just list it
2399 (defmacro artist-put-pixel (point-list x y)
2400 "In POINT-LIST, store a ``pixel'' at coord X,Y."
2401 (list 'setq point-list
2402 (list 'append point-list (list 'list (list 'artist-new-coord x y)))))
2404 ;; Calculate list of points using eight point algorithm
2405 ;; return a list of coords
2407 (defun artist-eight-point (x1 y1 x2 y2)
2408 "Run the eight-point algorithm to get a list of coords from X1,Y1 to X2,Y2."
2409 (let* ((point-list nil)
2410 (octant (artist-find-octant x1 y1 x2 y2))
2411 (dfdx-coeff (artist-get-dfdx-init-coeff octant))
2412 (dfdy-coeff (artist-get-dfdy-init-coeff octant))
2413 (x-step-q>=0 (artist-get-x-step-q>=0 octant))
2414 (y-step-q>=0 (artist-get-y-step-q>=0 octant))
2415 (x-step-q<0 (artist-get-x-step-q<0 octant))
2416 (y-step-q<0 (artist-get-y-step-q<0 octant))
2417 (dfdx (- (- y2 y1)))
2424 (* dfdy-coeff dfdy))))
2425 (artist-put-pixel point-list x y)
2426 (while (or (not (eq x x2)) (not (eq y y2)))
2429 (setq x (+ x x-step-q>=0))
2430 (setq y (+ y y-step-q>=0))
2431 (setq f (+ f (* x-step-q>=0 dfdx) (* y-step-q>=0 dfdy))))
2433 (setq x (+ x x-step-q<0))
2434 (setq y (+ y y-step-q<0))
2435 (setq f (+ f (* x-step-q<0 dfdx) (* y-step-q<0 dfdy)))))
2436 (setq q (+ (* 2 f) (* dfdx-coeff dfdx) (* dfdy-coeff dfdy)))
2437 (artist-put-pixel point-list x y))
2440 ;; artist-save-chars-under-point-list
2441 ;; Remembers the chars that were there before we did draw the line.
2442 ;; Returns point-list.
2444 (defun artist-save-chars-under-point-list (point-list)
2445 "Save characters originally under POINT-LIST."
2448 (artist-coord-add-saved-char
2450 (artist-get-char-at-xy (artist-coord-get-x coord)
2451 (artist-coord-get-y coord))))
2454 ;; artist-calculate-new-char, artist-calculate-new-chars
2455 ;; Calculates which char to insert depending on direction of point-list.
2457 ;; Depending on new-coord's position relative to last-coord one of the
2458 ;; following chars are returned: \ | / - o, as indicated by this:
2464 ;; artist-calculate-new-char works on one coordinate, returns char.
2465 ;; artist-calculate-new-chars works on a point-list, returns point-list.
2467 (defun artist-calculate-new-char (last-coord new-coord)
2468 "Return a line-char to use when moving from LAST-COORD to NEW-COORD."
2469 (let ((last-x (artist-coord-get-x last-coord))
2470 (last-y (artist-coord-get-y last-coord))
2471 (new-x (artist-coord-get-x new-coord))
2472 (new-y (artist-coord-get-y new-coord)))
2473 (cond ((> new-x last-x) (cond ((< new-y last-y) ?/ )
2474 ((> new-y last-y) ?\\ )
2476 ((< new-x last-x) (cond ((< new-y last-y) ?\\ )
2477 ((> new-y last-y) ?/ )
2479 ((eq new-y last-y) ?o)
2482 (defun artist-calculate-new-chars (point-list)
2483 "Return a list of coords with line-chars calculated. Input: POINT-LIST."
2484 (if (null (cdr point-list))
2485 (list (artist-coord-add-new-char (car point-list) ?o ))
2486 (let ((last-coord (car point-list)))
2487 (cons (artist-coord-add-new-char
2489 (artist-calculate-new-char (car (cdr point-list))
2492 (lambda (this-coord)
2494 (artist-coord-add-new-char
2496 (artist-calculate-new-char last-coord this-coord))
2497 (setq last-coord this-coord)))
2498 (cdr point-list))))))
2500 ;; artist-modify-new-chars
2501 ;; Replaces some characters with some other characters.
2503 ;; artist-modify-new-chars works on a point-list, returns point-list.
2505 (defun artist-modify-new-chars (point-list)
2506 "Replace intersecting characters in POINT-LIST.
2507 This function returns a point-list."
2510 (let* ((new-c (artist-coord-get-new-char coord))
2511 (saved-c (artist-coord-get-saved-char coord))
2512 (modified-c (artist-intersection-char new-c saved-c)))
2513 (artist-coord-set-new-char coord modified-c)))
2518 ;; functions for accessing endpoints and elements in object requiring
2522 (defun artist-make-endpoint (x y)
2523 "Create an endpoint at X, Y."
2524 (let ((new-endpoint (make-vector 2 x)))
2525 (aset new-endpoint 1 y)
2528 (defun artist-endpoint-get-x (endpoint)
2529 "Retrieve the x component of an ENDPOINT."
2532 (defun artist-endpoint-get-y (endpoint)
2533 "Retrieve the y component of an ENDPOINT."
2536 (defun artist-make-2point-object (endpoint1 endpoint2 shapeinfo)
2537 "Create a 2-point object of ENDPOINT1, ENDPOINT2 and SHAPEINFO."
2538 (list endpoint1 endpoint2 shapeinfo))
2540 (defun artist-2point-get-endpoint1 (obj)
2541 "Retrieve the first endpoint of a 2-point object OBJ."
2544 (defun artist-2point-get-endpoint2 (obj)
2545 "Retrieve the second endpoint of a 2-point object OBJ."
2548 (defun artist-2point-get-shapeinfo (obj)
2549 "Retrieve the shapeinfo component of a 2-point object OBJ."
2554 ;; Drawing and undrawing lines (any direction)
2557 (defun artist-draw-line (x1 y1 x2 y2)
2558 "Draw a line from X1, Y1 to X2, Y2.
2560 Output is a line, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
2562 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
2563 SHAPE-INFO is a list of vectors [X Y SAVED-CHAR NEW-CHAR]."
2564 (let ((endpoint1 (artist-make-endpoint x1 y1))
2565 (endpoint2 (artist-make-endpoint x2 y2)))
2566 (artist-make-2point-object
2571 (artist-move-to-xy (artist-coord-get-x coord)
2572 (artist-coord-get-y coord))
2573 (if artist-line-char-set
2574 (artist-replace-char artist-line-char)
2575 (artist-replace-char (artist-coord-get-new-char coord)))
2577 (artist-modify-new-chars
2578 (artist-calculate-new-chars
2579 (artist-save-chars-under-point-list
2580 (artist-eight-point x1 y1 x2 y2))))))))
2582 (defun artist-undraw-line (line)
2586 (artist-move-to-xy (artist-coord-get-x coord)
2587 (artist-coord-get-y coord))
2588 (artist-replace-char (artist-coord-get-saved-char coord))
2590 (artist-2point-get-shapeinfo line)))
2593 ;; Drawing and undrawing straight lines
2596 (defun artist-draw-sline (x1 y1 x2 y2)
2597 "Draw a straight line from X1, Y1 to X2, Y2.
2598 Straight lines are vertical, horizontal or diagonal lines.
2599 They are faster to draw and most often they are what you need
2600 when drawing a simple image.
2602 Output is a straight line, which is a list on the form
2603 \(END-POINT-1 END-POINT-2 SHAPE-INFO).
2605 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
2606 SHAPE-INFO is a vector [START-X START-Y LENGTH-OF-LINE DIRECTION
2607 ORIGINAL-CHAR-1 ORIGINAL-CHAR-2 ... ]."
2608 (let* ((line (artist-save-chars-under-sline (artist-sline x1 y1 x2 y2)))
2611 (length (+ (aref line 2) 4))
2612 (direction (aref line 3))
2613 (line-char (artist-direction-char direction))
2615 (endpoint1 (artist-make-endpoint x y))
2618 (artist-move-to-xy x y)
2619 (if artist-line-char-set
2620 (artist-replace-char artist-line-char)
2621 (artist-replace-char (artist-intersection-char
2624 (if (not (< (1+ i) length))
2625 ;; This is the last element. Set the second endpoint
2626 (setq endpoint2 (artist-make-endpoint x y)))
2627 (setq x (+ x (artist-direction-step-x direction)))
2628 (setq y (+ y (artist-direction-step-y direction)))
2630 (artist-make-2point-object endpoint1 endpoint2 line)))
2633 (defun artist-undraw-sline (line)
2634 "Undraw a straight line LINE."
2636 (let* ((shape-info (artist-2point-get-shapeinfo line))
2637 (x (aref shape-info 0))
2638 (y (aref shape-info 1))
2639 (length (+ (aref shape-info 2) 4))
2640 (direction (aref shape-info 3))
2643 (artist-move-to-xy x y)
2644 (artist-replace-char (aref shape-info i))
2645 (setq x (+ x (artist-direction-step-x direction)))
2646 (setq y (+ y (artist-direction-step-y direction)))
2651 ;; Drawing and undrawing rectangles and squares
2654 (defun artist-draw-rect (x1 y1 x2 y2)
2655 "Draw a rectangle with corners at X1, Y1 and X2, Y2.
2657 Output is a rectangle, which is a list on the form
2658 \(END-POINT-1 END-POINT-2 SHAPE-INFO).
2660 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
2661 SHAPE-INFO is a list of four straight lines."
2662 (let* ((artist-line-char (artist-compute-line-char))
2663 (artist-line-char-set artist-line-char)
2664 (line1 (artist-draw-sline x1 y1 x2 y1))
2665 (line2 (artist-draw-sline x2 y1 x2 y2))
2666 (line3 (artist-draw-sline x2 y2 x1 y2))
2667 (line4 (artist-draw-sline x1 y2 x1 y1))
2668 (endpoint1 (artist-make-endpoint x1 y1))
2669 (endpoint2 (artist-make-endpoint x2 y2)))
2670 (artist-make-2point-object endpoint1
2672 (list line1 line2 line3 line4))))
2674 (defun artist-undraw-rect (rectangle)
2677 (let ((shape-info (artist-2point-get-shapeinfo rectangle)))
2678 (artist-undraw-sline (elt shape-info 3))
2679 (artist-undraw-sline (elt shape-info 2))
2680 (artist-undraw-sline (elt shape-info 1))
2681 (artist-undraw-sline (elt shape-info 0)))))
2684 (defun artist-rect-corners-squarify (x1 y1 x2 y2)
2685 "Compute square corners from rectangle corners at X1, Y1 and X2, Y2.
2686 The square's first corner will be X1, Y1. The position of the second
2687 corner depends on which of X2 and Y2 is most far away from X1, Y1."
2688 (let* ((delta-x (- x2 x1))
2690 (delta-x-sign (if (< delta-x 0) -1 1))
2691 (delta-y-sign (if (< delta-y 0) -1 1))
2692 (new-x2) ; set below
2693 (new-y2)) ; set below
2695 ;; Check which of x2 and y2 is most distant
2696 ;; take care to the aspect ratio
2697 (if (> (abs delta-x) (abs delta-y))
2699 ;; *** x2 more distant than y2 (with care taken to aspect ratio)
2702 (setq new-y2 (+ y1 (round (/ (* (abs delta-x) delta-y-sign)
2703 artist-aspect-ratio)))))
2705 ;; *** y2 more distant than x2 (with care taken to aspect ratio)
2707 (setq new-x2 (round (+ x1 (* (* (abs delta-y) delta-x-sign)
2708 artist-aspect-ratio))))
2712 (list x1 y1 new-x2 new-y2)))
2715 (defun artist-draw-square (x1 y1 x2 y2)
2716 "Draw a square with corners at X1, Y1 and X2, Y2.
2718 Output is a square, which is a list on the form
2719 \(END-POINT-1 END-POINT-2 SHAPE-INFO).
2721 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
2722 SHAPE-INFO is a list of four straight lines."
2723 (let* ((artist-line-char (artist-compute-line-char))
2724 (artist-line-char-set artist-line-char)
2725 (square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
2726 (new-x1 (elt square-corners 0))
2727 (new-y1 (elt square-corners 1))
2728 (new-x2 (elt square-corners 2))
2729 (new-y2 (elt square-corners 3))
2730 (endpoint1 (artist-make-endpoint new-x1 new-y1))
2731 (endpoint2 (artist-make-endpoint new-x2 new-y2))
2732 (line1 (artist-draw-sline new-x1 new-y1 new-x2 new-y1))
2733 (line2 (artist-draw-sline new-x2 new-y1 new-x2 new-y2))
2734 (line3 (artist-draw-sline new-x2 new-y2 new-x1 new-y2))
2735 (line4 (artist-draw-sline new-x1 new-y2 new-x1 new-y1)))
2736 (artist-make-2point-object endpoint1
2738 (list line1 line2 line3 line4))))
2740 (defun artist-undraw-square (square)
2743 (let ((shape-info (artist-2point-get-shapeinfo square)))
2744 (artist-undraw-sline (elt shape-info 3))
2745 (artist-undraw-sline (elt shape-info 2))
2746 (artist-undraw-sline (elt shape-info 1))
2747 (artist-undraw-sline (elt shape-info 0)))))
2750 ;; Filling rectangles and squares
2753 (defun artist-fill-rect (rect x1 y1 x2 y2)
2754 "Fill rectangle RECT from X1,Y1 to X2,Y2."
2755 (let ((x (1+ (min x1 x2)))
2756 (y (1+ (min y1 y2)))
2758 (y-max (max y1 y2)))
2759 (let ((w (- x-max x)))
2761 (artist-move-to-xy x y)
2762 (artist-replace-chars artist-fill-char w)
2765 (defun artist-fill-square (square x1 y1 x2 y2)
2766 "Fill a SQUARE from X1,Y1 to X2,Y2."
2767 (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
2768 (new-x1 (elt square-corners 0))
2769 (new-y1 (elt square-corners 1))
2770 (new-x2 (elt square-corners 2))
2771 (new-y2 (elt square-corners 3))
2772 (x (1+ (min new-x1 new-x2)))
2773 (y (1+ (min new-y1 new-y2)))
2774 (x-max (max new-x1 new-x2))
2775 (y-max (max new-y1 new-y2))
2778 (artist-move-to-xy x y)
2779 (artist-replace-chars artist-fill-char w)
2787 (defun artist-pen (x1 y1)
2788 "Draw a character at X1, Y1.
2789 The character is replaced with the character in `artist-fill-char'."
2790 (artist-move-to-xy x1 y1)
2791 (artist-replace-char (if artist-line-char-set
2793 (if artist-fill-char-set
2795 artist-default-fill-char))))
2798 (defun artist-pen-line (x1 y1)
2799 "Draw a line from last pen position to X1, Y1.
2800 The character is replaced with the character in `artist-fill-char'.
2801 This will store all points in `artist-key-poly-point-list' in reversed
2802 order (I assume it is faster to cons to the beginning of the list than
2803 to append to the end of the list, when doing free-hand drawing)."
2804 (let ((artist-line-char (if artist-line-char-set
2806 (if artist-fill-char-set
2808 artist-default-fill-char))))
2810 ;; Draw line from last point to this
2811 (let ((x-last (car (car artist-key-poly-point-list)))
2812 (y-last (cdr (car artist-key-poly-point-list))))
2813 (artist-move-to-xy x-last y-last)
2814 (artist-replace-char artist-line-char)
2815 (artist-draw-line x-last y-last x1 y1))
2817 ;; Update the point-list
2818 (setq artist-key-poly-point-list
2819 (cons (cons x1 y1) artist-key-poly-point-list))))
2821 (defun artist-pen-reset-last-xy (x1 y1)
2822 "Reset the last x and y points to X1, Y1 when doing pen-drawing."
2823 (artist-clear-arrow-points)
2824 (setq artist-key-poly-point-list (list (cons x1 y1))))
2827 (defun artist-pen-set-arrow-points (x1 y1)
2828 "Set arrow points for pen drawing using X1, Y1.
2829 Also, the `artist-key-poly-point-list' is reversed."
2831 (setq artist-key-poly-point-list
2832 (artist-uniq artist-key-poly-point-list))
2834 (if (>= (length artist-key-poly-point-list) 2)
2836 ;; Only set arrow-points if the point-list has two or more entries
2837 (let ((xn (car (car artist-key-poly-point-list)))
2838 (yn (cdr (car artist-key-poly-point-list)))
2839 (xn-1 (car (car (cdr artist-key-poly-point-list))))
2840 (yn-1 (cdr (car (cdr artist-key-poly-point-list))))
2841 (dirn)) ; direction for point n
2842 (setq artist-key-poly-point-list (reverse artist-key-poly-point-list))
2843 (let ((x0 (car (car artist-key-poly-point-list)))
2844 (y0 (cdr (car artist-key-poly-point-list)))
2845 (x1 (car (car (cdr artist-key-poly-point-list))))
2846 (y1 (cdr (car (cdr artist-key-poly-point-list))))
2847 (dir0)) ; direction for point 0
2848 (setq dir0 (artist-find-direction x1 y1 x0 y0))
2849 (setq dirn (artist-find-direction xn-1 yn-1 xn yn))
2850 (setq artist-arrow-point-1 (artist-make-arrow-point x0 y0 dir0))
2851 (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn))))))
2857 (defun artist-figlet-run (text font extra-args)
2858 "Run figlet rendering TEXT using FONT.
2859 EXTRA-ARGS for figlet, for the command line, may be specified."
2860 (let* ((figlet-args (cond ((and font extra-args)
2861 (cons (concat "-f" font)
2862 (artist-string-split extra-args "[ \t]+")))
2863 (font (concat "-f" font))
2865 (artist-string-split extra-args "[ \t]+"))
2867 (figlet-output (artist-system artist-figlet-program text figlet-args))
2868 (exit-code (elt figlet-output 0))
2869 (stdout (elt figlet-output 1))
2870 (stderr (elt figlet-output 2)))
2871 (if (not (= exit-code 0))
2872 (error "Failed to render font: %s (%d)" stderr exit-code))
2875 (defun artist-figlet-get-font-list ()
2876 "Read fonts in with the shell command.
2877 Returns a list of strings."
2878 (let* ((cmd-interpreter "/bin/sh")
2879 (ls-cmd artist-figlet-list-fonts-command)
2880 (result (artist-system cmd-interpreter ls-cmd nil))
2881 (exit-code (elt result 0))
2882 (stdout (elt result 1))
2883 (stderr (elt result 2)))
2884 (if (not (= exit-code 0))
2885 (error "Failed to read available fonts: %s (%d)" stderr exit-code))
2886 (artist-string-split stdout ".flf\n")))
2888 (defun artist-figlet-choose-font ()
2889 "Read any extra arguments for figlet."
2891 (let* ((avail-fonts (artist-figlet-get-font-list))
2892 (font (completing-read (concat "Select font (default "
2893 artist-figlet-default-font
2896 (lambda (font) (cons font font))
2898 (if (string= font "") artist-figlet-default-font font)))
2900 (defun artist-figlet-get-extra-args ()
2901 "Read any extra arguments for figlet."
2902 (let ((extra-args (read-string "Extra args to figlet: ")))
2903 (if (string= extra-args "")
2907 (defun artist-figlet (text)
2908 "Render TEXT using figlet."
2909 (let* ((figlet-font (artist-figlet-choose-font))
2910 (figlet-extra-args (artist-figlet-get-extra-args)))
2911 (artist-figlet-run text figlet-font figlet-extra-args)))
2914 (defun artist-text-insert-common (x y text see-thru)
2915 "At position X, Y, insert text TEXT.
2916 If SEE-THRU is non-nil, then blanks in TEXT do not replace text
2918 (let* ((string-list (artist-string-split text "\n"))
2920 (len (length string-list)))
2922 (artist-move-to-xy x (+ y i))
2923 (artist-replace-string (car string-list) see-thru)
2924 (setq string-list (cdr string-list))
2927 (defun artist-text-insert-see-thru (x y text)
2928 "At position X, Y, insert text TEXT.
2929 Let text already in buffer shine thru the TEXT inserted."
2930 (artist-text-insert-common x y text t))
2932 (defun artist-text-insert-overwrite (x y text)
2933 "At position X, Y, insert text TEXT.
2934 Let blanks in TEXT overwrite any text already in the buffer."
2935 (artist-text-insert-common x y text nil))
2937 (defun artist-text-see-thru (x y)
2938 "Prompt for text to render, render it at X,Y.
2939 This is done by calling the function specified by
2940 `artist-text-renderer-function', which must return a list of strings,
2941 to be inserted in the buffer.
2943 Text already in the buffer ``shines thru'' blanks in the rendered text."
2944 (let* ((input-text (read-string "Type text to render: "))
2945 (rendered-text (artist-funcall artist-text-renderer-function input-text)))
2946 (artist-text-insert-see-thru x y rendered-text)))
2949 (defun artist-text-overwrite (x y)
2950 "Prompt for text to render, render it at X,Y.
2951 This is done by calling the function specified by
2952 `artist-text-renderer-function', which must return a list of strings,
2953 to be inserted in the buffer.
2955 Blanks in the rendered text overwrite any text in the buffer."
2956 (let* ((input-text (read-string "Type text to render: "))
2957 (rendered-text (artist-funcall artist-text-renderer-function input-text)))
2958 (artist-text-insert-overwrite x y rendered-text)))
2964 (defun artist-spray-get-interval ()
2965 "Retrieve the interval for repeated spray."
2966 artist-spray-interval)
2968 (defun artist-spray-random-points (n radius)
2969 "Generate N random points within a radius of RADIUS.
2970 Returns a list of points. Each point is on the form (X1 . Y1)."
2973 (let* ((angle (* (random 359) (/ float-pi 180)))
2974 (dist (random radius))
2975 (point (cons (round (* dist (cos angle)))
2976 (round (* dist (sin angle))))))
2977 (setq points (cons point points)))
2981 (defun artist-spray (x1 y1)
2983 (let* ((num-points (* artist-spray-radius artist-spray-radius))
2984 (spray-points (artist-spray-random-points num-points
2985 artist-spray-radius)))
2987 ;; Replace one spray point
2988 (let* ((point (car spray-points))
2989 (x (+ x1 (car point)))
2990 (y (+ y1 (cdr point)))
2991 (buf-c (artist-get-char-at-xy-conv x y))
2992 (this-c (memq buf-c artist-spray-chars))
2993 (next-c (cond ((null this-c) artist-spray-new-char)
2994 ((null (cdr this-c)) (car this-c))
2995 (t (car (cdr this-c))))))
2996 (artist-move-to-xy x y)
2997 (artist-replace-char next-c))
2999 ;; Step to next spray point
3000 (setq spray-points (cdr spray-points)))))
3002 (defun artist-spray-clear-circle (circle x1 y1 x2 y2)
3003 "Clear circle CIRCLE at X1, Y1 through X2, Y2."
3004 (artist-undraw-circle circle))
3006 (defun artist-spray-set-radius (circle x1 y1 x2 y2)
3007 "Set spray radius from CIRCLE at X1, Y1 through X2, Y2."
3008 (let ((dx (- x2 x1))
3010 (setq artist-spray-radius (round (sqrt (+ (* dx dx) (* dy dy)))))
3011 (if (= 0 artist-spray-radius)
3012 (setq artist-spray-radius 1))))
3018 (defun artist-erase-char (x1 y1)
3019 "Erase a character at X1, Y1.
3020 The character is replaced with the character in `artist-erase-char'."
3021 (artist-move-to-xy x1 y1)
3022 (artist-replace-char artist-erase-char))
3024 (defun artist-erase-rect (rect x1 y1 x2 y2)
3025 "Erase rectangle RECT from X1, Y1, X2, Y2."
3026 (let ((artist-line-char-set t)
3027 (artist-fill-char-set t)
3028 (artist-line-char artist-erase-char)
3029 (artist-fill-char artist-erase-char))
3030 (artist-draw-rect x1 y1 x2 y2)
3031 (artist-fill-rect rect x1 y1 x2 y2)))
3035 ;; Vaporizing (erasing) line and lines
3039 (defun artist-vap-find-endpoint (x1 y1 step-x step-y accept-set reject-set)
3040 "Find one endpoint for line through X1, Y1.
3041 The endpoint is searched for in the direction defined by STEP-X, STEP-Y,
3042 accepting characters in the list ACCEPT-SET, stopping immediately
3043 when finding characters in the list REJECT-SET. Fuzziness, that is
3044 the number of consecutive characters not in ACCEPT-SET to allow as
3045 part of the line, is determined by the variable `artist-vaporize-fuzziness'.
3046 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3053 (let ((c (artist-get-char-at-xy-conv x y)))
3054 (cond ((memq c reject-set)
3057 ;; We found a character we are accepting as part of the line.
3059 ((memq c accept-set)
3064 (if (or (< x 0) (< y 0)) ;stop at the edge
3067 ;; We found a character we are not accepting as part of
3068 ;; the line Search `artist-vaporize-fuzziness'
3069 ;; characters away from this position in the same
3070 ;; direction to see if there are any characters in the
3071 ;; accept-set. If not, we have found the endpoint.
3073 (let ((fuzziness artist-vaporize-fuzziness)
3077 ;; while we have more fuzziness left and we have not
3078 ;; found a character accepted as a line, move
3080 (while (and (> fuzziness 0) (not (memq c accept-set)))
3081 (setq x-tmp (+ x-tmp step-x))
3082 (setq y-tmp (+ y-tmp step-y))
3083 (setq c (artist-get-char-at-xy-conv x-tmp y-tmp))
3084 (setq fuzziness (- fuzziness 1)))
3085 (if (memq c accept-set)
3087 ;; The line continues on the other side of the
3088 ;; not-accepted character.
3092 ;; Else: We couldn't find any line on the other side.
3093 ;; That means we are done searching for the endpoint.
3095 (cons x-last y-last)))
3098 (defun artist-vap-find-endpoints-horiz (x y)
3099 "Find endpoints for a horizontal line through X, Y.
3100 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3101 (list (artist-vap-find-endpoint x y 1 0 '(?- ?+) '(?\s))
3102 (artist-vap-find-endpoint x y -1 0 '(?- ?+) '(?\s))))
3104 (defun artist-vap-find-endpoints-vert (x y)
3105 "Find endpoints for a vertical line through X, Y.
3106 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3107 (list (artist-vap-find-endpoint x y 0 1 '(?| ?+) '(?\s))
3108 (artist-vap-find-endpoint x y 0 -1 '(?| ?+) '(?\s))))
3110 (defun artist-vap-find-endpoints-swne (x y)
3111 "Find endpoints for a diagonal line (made by /'s) through X, Y.
3112 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3113 (list (artist-vap-find-endpoint x y 1 -1 '(?/ ?X) '(?\s))
3114 (artist-vap-find-endpoint x y -1 1 '(?/ ?X) '(?\s))))
3116 (defun artist-vap-find-endpoints-nwse (x y)
3117 "Find endpoints for a diagonal line (made by \\'s) through X, Y.
3118 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3119 (list (artist-vap-find-endpoint x y 1 1 '(?\\ ?X) '(?\s))
3120 (artist-vap-find-endpoint x y -1 -1 '(?\\ ?X) '(?\s))))
3123 (defun artist-vap-find-endpoints (x y)
3124 "Given a point X1, Y1, return a list of endpoints of lines through X, Y.
3125 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3126 (if artist-line-char-set
3128 (let ((c (artist-get-char-at-xy-conv x y)))
3129 (cond ((eq c ?-) (artist-vap-find-endpoints-horiz x y))
3130 ((eq c ?|) (artist-vap-find-endpoints-vert x y))
3131 ((eq c ?/) (artist-vap-find-endpoints-swne x y))
3132 ((eq c ?\\) (artist-vap-find-endpoints-nwse x y))
3133 ((eq c ?+) (append (artist-vap-find-endpoints-horiz x y)
3134 (artist-vap-find-endpoints-vert x y)))
3135 ((eq c ?X) (append (artist-vap-find-endpoints-swne x y)
3136 (artist-vap-find-endpoints-nwse x y)))
3138 ;; We don't know how to find directions when we are on
3139 ;; another character
3143 (defun artist-vap-group-in-pairs (l)
3144 "Group elements in list L in pairs."
3145 (cond ((null l) nil)
3146 ((null (cdr l)) l) ; unevent number of elements in list
3147 (t (append (list (list (car l) (car (cdr l))))
3148 (artist-vap-group-in-pairs (cdr (cdr l)))))))
3150 (defun artist-vaporize-by-endpoints (endpoint1 endpoint2)
3151 "Given ENDPOINT1 and ENDPOINT2, vaporize the line between them.
3152 An endpoint is a pair (X . Y)."
3153 (let* ((x1 (car endpoint1))
3154 (y1 (cdr endpoint1))
3155 (x2 (car endpoint2))
3156 (y2 (cdr endpoint2))
3157 (dir (artist-find-direction x1 y1 x2 y2))
3158 (x-step (aref [1 1 0 -1 -1 -1 0 1] dir))
3159 (y-step (aref [0 1 1 1 0 -1 -1 -1] dir))
3160 (line-c (aref [?- ?\\ ?| ?/ ?- ?\\ ?| ?/] dir))
3161 (line-len (elt (list (abs (- x2 x1))
3172 (while (>= line-len 0)
3173 (let* ((buffer-c (artist-get-char-at-xy-conv x y))
3174 (new-c (artist-unintersection-char line-c buffer-c)))
3175 (artist-move-to-xy x y)
3176 (artist-replace-char new-c))
3177 (setq x (+ x x-step)
3179 line-len (- line-len 1)))))
3182 (defun artist-vaporize-line (x1 y1)
3183 "Vaporize (erase) the straight line through X1, Y1.
3184 Do this by replacing the characters that forms the line with
3185 `artist-erase-char'. Output is a list of endpoints for lines through
3186 X1, Y1. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3187 (let ((endpoints (artist-vap-find-endpoints x1 y1)))
3190 (let ((ep1 (car endpoints))
3191 (ep2 (car (cdr endpoints))))
3192 (artist-vaporize-by-endpoints ep1 ep2)))
3193 (artist-vap-group-in-pairs endpoints))
3197 ;; Implementation note: This depends on artist-vaporize-line doing
3198 ;; unintersections of intersecting lines.
3201 ;; Suppose the buffer looks like this and that we start vaporizing
3202 ;; lines at (3,0) (at the ``*'').
3210 ;; We will then push (0,0) and (6,0) on the stack, and vaporize the
3211 ;; topmost horizontal line:
3219 ;; We will then pop (0,0) and remove the left-most vertical line while
3220 ;; pushing the lower left corner (0,3) on the stack, and so on until
3221 ;; the entire rectangle is vaporized.
3223 ;; Now, What if the `+' in the upper left and upper right corners,
3224 ;; had not been changed to `|' but to spaces instead? We would
3225 ;; have failed when popping (0,0) and vaporizing that line because
3226 ;; we wouldn't find any line at (0,0):
3234 ;; That's why we depend on artist-vaporize-line doing unintersecting
3235 ;; of crossing lines. There are alternative ways to handle this
3236 ;; if it becomes too much a trouble.
3238 (defun artist-vaporize-lines (x1 y1)
3239 "Vaporize lines reachable from point X1, Y1."
3240 (let ((ep-stack nil))
3242 (lambda (ep) (push ep ep-stack))
3243 (artist-vap-find-endpoints x1 y1))
3244 (while (not (null ep-stack))
3245 (let* ((vaporize-point (pop ep-stack))
3246 (new-endpoints (artist-vaporize-line (car vaporize-point)
3247 (cdr vaporize-point))))
3249 (lambda (endpoint) (push endpoint ep-stack))
3254 ;; Circles and ellipses
3256 (defun artist-ellipse-generate-quadrant (x-radius y-radius)
3257 "Create a point-list for first quadrant.
3258 Points go from (X-RADIUS, 0) to (0, Y-RADIUS).
3259 Quadrant is generated around origin."
3260 (let* ((rx2 (* x-radius x-radius))
3261 (ry2 (* y-radius y-radius))
3270 (artist-put-pixel point-list x y)
3271 (setq p (round (+ ry2 (- (* rx2 y-radius)) (* 0.25 rx2))))
3276 (setq p (+ p ry2 px))
3279 p (+ p ry2 px (- py))))
3280 (artist-put-pixel point-list x y))
3281 (setq p (round (+ (* ry2 (+ x 0.5) (+ x 0.5))
3282 (* rx2 (- y 1) (- y 1))
3288 (setq p (+ p rx2 (- py)))
3291 p (+ p rx2 (- py) px)))
3292 (artist-put-pixel point-list x y))
3295 (defsubst artist-new-fill-item (x y width)
3296 "Create a new item at X, Y, with WIDTH.
3297 This is for use in fill-info in ellipses and circles."
3298 (let ((new-item (make-vector 3 x)))
3300 (aset new-item 2 width)
3303 (defsubst artist-fill-item-get-x (fill-item)
3304 "Retrieve the x component of a FILL-ITEM."
3307 (defsubst artist-fill-item-set-x (fill-item new-x)
3308 "Set the x component of a FILL-ITEM to NEW-X."
3309 (aset fill-item 0 new-x)
3312 (defsubst artist-fill-item-get-y (fill-item)
3313 "Retrieve the y component of a FILL-ITEM."
3316 (defsubst artist-fill-item-set-y (fill-item new-y)
3317 "Set the y component of a FILL-ITEM to NEW-Y."
3318 (aset fill-item 1 new-y)
3321 (defsubst artist-fill-item-get-width (fill-item)
3322 "Retrieve the width component of a FILL-ITEM."
3325 (defsubst artist-fill-item-set-width (fill-item new-width)
3326 "Set the width component of a FILL-ITEM to NEW-WIDTH."
3327 (aset fill-item 2 new-width)
3331 (defun artist-ellipse-point-list-add-center (x-center y-center point-list)
3332 "Add offsets X-CENTER and Y-CENTER to coordinates in POINT-LIST."
3335 (artist-coord-set-x p (+ x-center (artist-coord-get-x p)))
3336 (artist-coord-set-y p (+ y-center (artist-coord-get-y p))))
3340 (defun artist-ellipse-fill-info-add-center (x-center y-center fill-info)
3341 "Add offsets X-CENTER and Y-CENTER to fill-items in FILL-INFO."
3344 (artist-fill-item-set-x p (+ x-center (artist-fill-item-get-x p)))
3345 (artist-fill-item-set-y p (+ y-center (artist-fill-item-get-y p))))
3348 (defun artist-ellipse-remove-0-fills (fill-info)
3349 "Remove fill-infos from FILL-INFO that fills a zero-width field."
3350 (cond ((null fill-info)
3352 ((= 0 (artist-fill-item-get-width (car fill-info)))
3353 (artist-ellipse-remove-0-fills (cdr fill-info)))
3355 (append (list (car fill-info))
3356 (artist-ellipse-remove-0-fills (cdr fill-info))))))
3359 (defun artist-ellipse-compute-fill-info (point-list)
3360 "Compute fill info for ellipse around 0,0 from POINT-LIST.
3361 The POINT-LIST is expected to cover the first quadrant."
3362 (let ((first-half nil)
3366 ;; Create first half (the lower one (since y grows downwards)) from
3367 ;; the first quadrant.
3370 (let* ((x (artist-coord-get-x coord))
3371 (y (artist-coord-get-y coord))
3372 (width (max (- (* 2 x) 1) 0))
3373 (left-edge (- x width)))
3374 (if (or (null last-y) (not (= y last-y)))
3375 ;; This was either the first time,
3376 ;; or it was the first time on a new line
3379 ;; Fill info item starts at left-edge on line y
3380 (list (artist-new-fill-item left-edge y width)))))
3384 ;; Create the other half by mirroring the first half.
3389 (artist-new-fill-item (artist-fill-item-get-x i)
3390 (- (artist-fill-item-get-y i))
3391 (artist-fill-item-get-width i)))
3392 ;; The cdr below is so we don't include fill-info for
3393 ;;; the middle line twice
3394 (cdr (reverse first-half)))))
3395 (artist-ellipse-remove-0-fills both-halves)))
3398 (defun artist-ellipse-mirror-quadrant (point-list)
3399 "Mirror a POINT-LIST describing first quadrant to create a complete ellipse."
3400 (let ((right-half nil)
3403 ;; First, if last char in that quadrant is `/', then replace it with `)'
3404 ;; This way we avoids things
3405 ;; --------- ---------
3407 ;; that look like: \ / instead we get: ( )
3409 ;; --------- ---------
3410 (let ((last-coord (car (last point-list))))
3411 (if (= (artist-coord-get-new-char last-coord) ?/)
3412 (artist-coord-set-new-char last-coord artist-ellipse-right-char)))
3414 ;; Create the other part of the right half by mirroring the first part
3420 (let ((c (artist-coord-get-new-char coord)))
3421 (artist-new-coord (artist-coord-get-x coord)
3422 (- (artist-coord-get-y coord))
3423 (cond ((= c ?/) ?\\)
3426 ;; The cdr below is so we don't draw the middle right char twice
3427 (cdr (reverse point-list)))))
3429 ;; Create the left half by mirroring the right half.
3433 (let ((c (artist-coord-get-new-char coord)))
3434 (artist-new-coord (- (artist-coord-get-x coord))
3435 (artist-coord-get-y coord)
3436 (cond ((= c ?/) ?\\)
3438 ((= c artist-ellipse-right-char)
3439 artist-ellipse-left-char)
3441 ;; The cdr and butlast below is so we don't draw the middle top
3442 ;; and middle bottom char twice.
3443 (butlast (cdr (reverse right-half)))))
3444 (append right-half left-half)))
3447 (defun artist-draw-ellipse-general (x1 y1 x-radius y-radius)
3448 "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS.
3450 Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
3452 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
3453 SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
3455 POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
3456 FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE].
3458 Ellipses with zero Y-RADIUS are not drawn correctly."
3459 (let* ((point-list (artist-ellipse-generate-quadrant x-radius y-radius))
3460 (fill-info (artist-ellipse-compute-fill-info point-list))
3461 (shape-info (make-vector 2 0)))
3463 (setq point-list (artist-calculate-new-chars point-list))
3464 (setq point-list (artist-ellipse-mirror-quadrant point-list))
3465 (setq point-list (artist-ellipse-point-list-add-center x1 y1 point-list))
3466 (setq fill-info (artist-ellipse-fill-info-add-center x1 y1 fill-info))
3472 (artist-move-to-xy (artist-coord-get-x coord)
3473 (artist-coord-get-y coord))
3474 (if artist-line-char-set
3475 (artist-replace-char artist-line-char)
3476 (artist-replace-char (artist-coord-get-new-char coord)))
3478 (artist-modify-new-chars
3479 (artist-save-chars-under-point-list point-list))))
3481 (aset shape-info 0 point-list)
3482 (aset shape-info 1 fill-info)
3483 (artist-make-2point-object (artist-make-endpoint x1 y1)
3484 (artist-make-endpoint x-radius y-radius)
3487 (defun artist-draw-ellipse-with-0-height (x1 y1 x-radius y-radius)
3488 "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS.
3490 Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
3492 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
3493 SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
3495 POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
3496 FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE].
3498 The Y-RADIUS must be 0, but the X-RADIUS must not be 0."
3499 (let ((point-list nil)
3500 (width (max (- (abs (* 2 x-radius)) 1)))
3501 (left-edge (1+ (- x1 (abs x-radius))))
3502 (line-char (if artist-line-char-set artist-line-char ?-))
3506 (shape-info (make-vector 2 0)))
3508 (let* ((line-x (+ left-edge i))
3510 (new-coord (artist-new-coord line-x line-y)))
3511 (artist-coord-add-saved-char new-coord
3512 (artist-get-char-at-xy line-x line-y))
3513 (artist-move-to-xy line-x line-y)
3514 (artist-replace-char line-char)
3515 (setq point-list (append point-list (list new-coord)))
3517 (aset shape-info 0 point-list)
3518 (aset shape-info 1 fill-info)
3519 (artist-make-2point-object (artist-make-endpoint x1 y1)
3520 (artist-make-endpoint x-radius y-radius)
3523 (defun artist-draw-ellipse (x1 y1 x2 y2)
3524 "Draw an ellipse with center at X1, Y1 and point X2,Y2.
3526 Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
3528 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
3529 SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
3531 POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
3532 FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]."
3533 (let* ((artist-line-char (artist-compute-line-char))
3534 (artist-line-char-set artist-line-char)
3535 (width (abs (- x2 x1)))
3536 (height (abs (- y2 y1)))
3538 ;; When we draw our ellipse, we want it to go through the cursor
3539 ;; position, but since x1,y1, x2,y2 marks the corners of one
3540 ;; of the quadrants, we have to enlarge the ellipse a bit.
3541 ;; Ok, so then why by sqrt(2)?
3542 ;; It comes from the equation for the ellipse (where a is the
3543 ;; x-radius and b is the y-radius):
3544 ;; f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0
3545 ;; and the fact that we want the enlarged ellipse to have the
3546 ;; same proportions as the smaller square, therefore we have:
3548 ;; Solving this yields a-in-larger-ellipse = a-in-smaller * sqrt(2)
3549 (x-radius (round (* width (sqrt 2))))
3550 (y-radius (round (* height (sqrt 2))))
3553 (if (and (= y1 y2) (not (= x1 x2)))
3554 (artist-draw-ellipse-with-0-height x y x-radius y-radius)
3555 (artist-draw-ellipse-general x y x-radius y-radius))))
3558 (defun artist-undraw-ellipse (ellipse)
3561 (let ((point-list (aref (artist-2point-get-shapeinfo ellipse) 0)))
3564 (artist-move-to-xy (artist-coord-get-x coord)
3565 (artist-coord-get-y coord))
3566 (artist-replace-char (artist-coord-get-saved-char coord))
3571 (defun artist-draw-circle (x1 y1 x2 y2)
3572 "Draw a circle with center at X1, Y1 and point X2,Y2.
3574 Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
3576 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
3577 SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
3579 POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
3580 FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]."
3581 (let* ((artist-line-char (artist-compute-line-char))
3582 (artist-line-char-set artist-line-char)
3583 (width (abs (- x2 x1)))
3584 (height (abs (- y2 y1)))
3585 ;; When drawing our circle, we want it to through the cursor
3586 ;; just as when drawing the ellipse, but we have to take
3587 ;; care for the aspect-ratio.
3588 ;; The equation for the ellipse (where a is the x-radius and
3589 ;; b is the y-radius):
3590 ;; f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0
3591 ;; together with the relationship
3592 ;; a = aspect-ratio * b
3594 ;; a = sqrt( x^2 + (aspect-ratio * y)^2 ) and
3595 ;; b = a / aspect-ratio
3596 (x-radius (round (sqrt (+ (* width width)
3597 (* (* artist-aspect-ratio height)
3598 (* artist-aspect-ratio height))))))
3599 (y-radius (round (/ x-radius artist-aspect-ratio))))
3600 (artist-draw-ellipse-general x1 y1 x-radius y-radius)))
3602 (defalias 'artist-undraw-circle 'artist-undraw-ellipse)
3608 (defun artist-fill-ellipse (ellipse x y x-radius y-radius)
3609 "Fill an ELLIPSE centered at X,Y with radius X-RADIUS and Y-RADIUS."
3610 (let ((fill-info (aref (artist-2point-get-shapeinfo ellipse) 1)))
3613 (artist-move-to-xy (artist-fill-item-get-x fill-item)
3614 (artist-fill-item-get-y fill-item))
3615 (artist-replace-chars artist-fill-char
3616 (artist-fill-item-get-width fill-item))
3620 (defalias 'artist-fill-circle 'artist-fill-ellipse)
3624 ;; Cutting, copying and pasting rectangles and squares
3625 ;; (filling functions)
3628 (defun artist-cut-rect (rect x1 y1 x2 y2)
3629 "Copy rectangle RECT drawn from X1, Y1 to X2, Y2, then clear it."
3630 (artist-undraw-rect rect)
3631 (artist-copy-generic x1 y1 x2 y2)
3632 (artist-erase-rect rect x1 y1 x2 y2))
3634 (defun artist-cut-square (square x1 y1 x2 y2)
3635 "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (made square), then clears it."
3636 (artist-undraw-square square)
3637 (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
3638 (new-x1 (elt square-corners 0))
3639 (new-y1 (elt square-corners 1))
3640 (new-x2 (elt square-corners 2))
3641 (new-y2 (elt square-corners 3)))
3642 (artist-copy-generic new-x1 new-y1 new-x2 new-y2)
3643 (artist-erase-rect square new-x1 new-y1 new-x2 new-y2)))
3646 (defun artist-get-buffer-contents-at-xy (x y width)
3647 "Retrieve contents from the buffer at X, Y. WIDTH characters are returned."
3648 (artist-move-to-xy x y)
3649 (let ((here (point))
3650 (there (save-excursion (artist-move-to-xy (+ x width) y) (point))))
3651 (untabify here there)
3652 (setq there (save-excursion (artist-move-to-xy (+ x width) y) (point)))
3653 (buffer-substring here there)))
3656 (defun artist-copy-generic (x1 y1 x2 y2)
3657 "Copy a rectangular area with corners at X1, Y1 and X2, Y2.
3658 Output is a copy buffer, a list of strings, representing the
3659 original contents of that area in the buffer."
3660 (let* ((x (min x1 x2))
3664 (w (+ (- x-max x) 1))
3667 (setq l (cons (artist-get-buffer-contents-at-xy x y w) l))
3669 (if artist-interface-with-rect
3670 (setq killed-rectangle (reverse l))
3671 (setq artist-copy-buffer (reverse l)))))
3674 (defun artist-copy-rect (rect x1 y1 x2 y2)
3675 "Copy rectangle RECT drawn from X1, Y1 to X2, Y2."
3676 (artist-undraw-rect rect)
3677 (artist-copy-generic x1 y1 x2 y2))
3679 (defun artist-copy-square (square x1 y1 x2 y2)
3680 "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (but made square)."
3681 (artist-undraw-square square)
3682 (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
3683 (new-x1 (elt square-corners 0))
3684 (new-y1 (elt square-corners 1))
3685 (new-x2 (elt square-corners 2))
3686 (new-y2 (elt square-corners 3)))
3687 (artist-copy-generic new-x1 new-y1 new-x2 new-y2)))
3689 (defun artist-paste (x y)
3690 "Paste the contents of the copy-buffer at X,Y."
3691 (let ((copy-buf (if artist-interface-with-rect
3693 artist-copy-buffer)))
3694 (if (not (null copy-buf))
3695 (while (not (null copy-buf))
3696 (artist-move-to-xy x y)
3697 (artist-replace-string (car copy-buf))
3698 (setq copy-buf (cdr copy-buf))
3700 (message "Nothing to paste"))))
3706 (defun artist-ff-too-far-right (x)
3707 "Determine if the position X is too far to the right."
3708 (cond ((numberp artist-flood-fill-right-border)
3709 (> x artist-flood-fill-right-border))
3710 ((eq artist-flood-fill-right-border 'window-width)
3711 (> x (- (window-width) 2)))
3712 ((eq artist-flood-fill-right-border 'fill-column)
3714 (t (error "Invalid value for `artist-flood-fill-right-border'"))))
3716 (defun artist-ff-get-rightmost-from-xy (x y)
3717 "Find the rightmost position in this run, starting at X, Y."
3719 (let ((char-at-xy (artist-get-char-at-xy-conv x y))
3722 (while (and (not (artist-ff-too-far-right x))
3723 (= char-at-xy (artist-get-char-at-xy-conv x y)))
3728 (defun artist-ff-is-topmost-line (x y)
3729 "Determine whether the position X,Y is on the topmost line or not."
3732 (defun artist-ff-is-bottommost-line (x y)
3733 "Determine whether the position X,Y is on the bottommost line or not."
3735 (goto-char (point-max))
3737 (let ((last-line (artist-current-line)))
3738 (if (= (point) (point-max))
3740 ;; Last line is empty, don't paint on it, report previous line
3742 (>= y (- last-line 1))
3743 (>= y last-line)))))
3745 (defun artist-flood-fill (x1 y1)
3746 "Flood-fill starting at X1, Y1. Fill with the char in `artist-fill-char'."
3749 ;; We are flood-filling the area that has this character.
3750 (c (artist-get-char-at-xy-conv x1 y1))
3751 (artist-fill-char (if artist-fill-char-set
3753 artist-default-fill-char)))
3755 ;; Fill only if the fill-char is not the same as the character whose
3756 ;; area we are about to fill, or, in other words, don't fill if we
3758 (if (not (= c artist-fill-char))
3759 (push (artist-new-coord x1 y1) stack))
3761 (while (not (null stack))
3762 (let* ((coord (pop stack))
3763 (x (artist-coord-get-x coord))
3764 (y (artist-coord-get-y coord))
3766 ;; Here we keep track of the leftmost and rightmost position
3772 ;; Remember if line above and below are accessible
3773 ;; Lines below the last one, and prior to the first-one
3774 ;; are not accessible.
3778 ;; Remember char for position on line above and below, so we
3779 ;; can find the rightmost positions on the runs.
3783 (setq x-rightmost (artist-ff-get-rightmost-from-xy x y))
3784 (setq lines-above (not (artist-ff-is-topmost-line x y)))
3785 (setq lines-below (not (artist-ff-is-bottommost-line x y)))
3786 (setq last-x x-rightmost)
3787 (setq x x-rightmost)
3789 ;; Search line above, push rightmost positions of runs for that line
3790 (while (and (>= x 0) (= c (artist-get-char-at-xy-conv x y)))
3792 (let ((c-above (artist-get-char-at-xy-conv x (- y 1))))
3793 (if (and (= c-above c) (/= c-above last-c-above))
3794 (push (artist-new-coord x (- y 1)) stack))
3795 (setq last-c-above c-above)))
3799 ;; Remember the left-most position on this run
3800 (setq x-leftmost last-x)
3802 ;; Search line below, push rightmost positions of runs for that line
3803 (setq x x-rightmost)
3804 (while (>= x x-leftmost)
3806 (let ((c-below (artist-get-char-at-xy-conv x (1+ y))))
3807 (if (and (= c-below c) (/= c-below last-c-below))
3808 (push (artist-new-coord x (1+ y)) stack))
3809 (setq last-c-below c-below)))
3812 (artist-move-to-xy x-leftmost y)
3813 (artist-replace-chars artist-fill-char (1+ (- x-rightmost x-leftmost)))
3815 ;; If we are to show incrementally, we have to remove any pending
3816 ;; input from the input queue, because processing of pending input
3817 ;; always has priority over display updates (although this input
3818 ;; won't be processed until we are done). Later on we will queue
3819 ;; the input on the input queue again.
3820 (if artist-flood-fill-show-incrementally
3822 (if (input-pending-p)
3824 (artist-update-display)))))))
3827 ;; Accessors to arrow-points
3830 (defun artist-make-arrow-point (x y direction &optional state)
3831 "Create an arrow point at X, Y for a line in direction DIRECTION.
3832 Optional argument STATE can be used to set state (default is nil)."
3834 (let* ((arrow-point (make-vector 4 0))
3835 (arrow-marker (make-marker)))
3836 (artist-move-to-xy x y)
3837 (set-marker arrow-marker (point))
3838 (aset arrow-point 0 arrow-marker)
3839 (aset arrow-point 1 (artist-get-char-at-xy x y))
3840 (aset arrow-point 2 direction)
3841 (aset arrow-point 3 state)
3844 (defsubst artist-arrow-point-get-marker (arrow-point)
3845 "Retrieve the marker component of an ARROW-POINT."
3846 (aref arrow-point 0))
3848 (defsubst artist-arrow-point-get-orig-char (arrow-point)
3849 "Retrieve the orig char component of an ARROW-POINT."
3850 (aref arrow-point 1))
3852 (defsubst artist-arrow-point-get-direction (arrow-point)
3853 "Retrieve the direction component of an ARROW-POINT."
3854 (aref arrow-point 2))
3856 (defsubst artist-arrow-point-get-state (arrow-point)
3857 "Retrieve the state component of an ARROW-POINT."
3858 (aref arrow-point 3))
3860 (defsubst artist-arrow-point-set-state (arrow-point new-state)
3861 "Set the state component of an ARROW-POINT to NEW-STATE."
3862 (aset arrow-point 3 new-state))
3865 (defun artist-clear-arrow-points ()
3866 "Clear current endpoints."
3867 (setq artist-arrow-point-1 nil)
3868 (setq artist-arrow-point-2 nil))
3870 (defun artist-set-arrow-points-for-poly (point-list)
3871 "Generic function for setting arrow-points for poly-shapes from POINT-LIST."
3872 (let* ((ep1 (elt point-list 0))
3873 (ep2 (elt point-list 1))
3874 (x1 (artist-endpoint-get-x ep1))
3875 (y1 (artist-endpoint-get-y ep1))
3876 (x2 (artist-endpoint-get-x ep2))
3877 (y2 (artist-endpoint-get-y ep2))
3878 (dir1 (artist-find-direction x2 y2 x1 y1))
3879 (epn (car (last point-list)))
3880 (epn-1 (car (last point-list 2)))
3881 (xn (artist-endpoint-get-x epn))
3882 (yn (artist-endpoint-get-y epn))
3883 (xn-1 (artist-endpoint-get-x epn-1))
3884 (yn-1 (artist-endpoint-get-y epn-1))
3885 (dirn (artist-find-direction xn-1 yn-1 xn yn)))
3886 (setq artist-arrow-point-1 (artist-make-arrow-point x1 y1 dir1))
3887 (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn))))
3890 (defun artist-set-arrow-points-for-2points (shape x1 y1 x2 y2)
3891 "Generic function for setting arrow-points for 2-point shapes.
3892 The 2-point shape SHAPE is drawn from X1, Y1 to X2, Y2."
3893 (let* ((endpoint1 (artist-2point-get-endpoint1 shape))
3894 (endpoint2 (artist-2point-get-endpoint2 shape))
3895 (x1 (artist-endpoint-get-x endpoint1))
3896 (y1 (artist-endpoint-get-y endpoint1))
3897 (x2 (artist-endpoint-get-x endpoint2))
3898 (y2 (artist-endpoint-get-y endpoint2)))
3899 (setq artist-arrow-point-1
3900 (artist-make-arrow-point x1 y1
3901 (artist-find-direction x2 y2 x1 y1)))
3902 (setq artist-arrow-point-2
3903 (artist-make-arrow-point x2 y2
3904 (artist-find-direction x1 y1 x2 y2)))))
3908 ;; Common routine for drawing/undrawing shapes based
3912 (defun artist-key-undraw-continously (x y)
3913 "Undraw current continuous shape with point at X, Y."
3914 ;; No undraw-info for continuous shapes
3917 (defun artist-key-undraw-poly (x y)
3918 "Undraw current poly shape with point at X, Y."
3919 (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
3920 (x1 (artist-endpoint-get-x artist-key-endpoint1))
3921 (y1 (artist-endpoint-get-y artist-key-endpoint1)))
3922 (artist-funcall undraw-fn artist-key-shape)))
3924 (defun artist-key-undraw-1point (x y)
3925 "Undraw current 1-point shape at X, Y."
3926 ;; No undraw-info for 1-point shapes
3929 (defun artist-key-undraw-2points (x y)
3930 "Undraw current 2-point shape at X, Y."
3931 (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
3932 (x1 (artist-endpoint-get-x artist-key-endpoint1))
3933 (y1 (artist-endpoint-get-y artist-key-endpoint1)))
3934 (artist-funcall undraw-fn artist-key-shape)))
3936 (defun artist-key-undraw-common ()
3937 "Common routine undrawing current shape."
3938 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
3939 (col (artist-current-column))
3940 (row (artist-current-line)))
3942 ;; Depending on what we are currently drawing, call other routines
3943 ;; that knows how to do the job
3945 (cond ((eq draw-how 'artist-do-continously)
3946 (artist-key-undraw-continously col row))
3947 ((eq draw-how 'artist-do-poly)
3948 (artist-key-undraw-poly col row))
3949 ((and (numberp draw-how) (= draw-how 1))
3950 (artist-key-undraw-1point col row))
3951 ((and (numberp draw-how) (= draw-how 2))
3952 (artist-key-undraw-2points col row))
3953 (t (message "Undrawing \"%s\"s is not yet implemented" draw-how)))
3955 ;; Now restore the old position
3957 (artist-move-to-xy col row)))
3961 ;; Implementation note: This really should honor the interval-fn entry
3962 ;; in the master table, `artist-mt', which would mean leaving a timer
3963 ;; that calls `draw-fn' every now and then. That timer would then have
3964 ;; to be canceled and reinstalled whenever the user moves the cursor.
3965 ;; This could be done, but what if the user suddenly switches to another
3966 ;; drawing mode, or even kills the buffer! In the mouse case, it is much
3967 ;; simpler: when at the end of `artist-mouse-draw-continously', the
3968 ;; user has released the button, so the timer will always be canceled
3970 (defun artist-key-draw-continously (x y)
3971 "Draw current continuous shape at X,Y."
3972 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
3973 (setq artist-key-shape (artist-funcall draw-fn x y))))
3975 (defun artist-key-draw-poly (x y)
3976 "Draw current poly-point shape with nth point at X,Y."
3977 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
3978 (x1 (artist-endpoint-get-x artist-key-endpoint1))
3979 (y1 (artist-endpoint-get-y artist-key-endpoint1)))
3980 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y))))
3982 (defun artist-key-draw-1point (x y)
3983 "Draw current 1-point shape at X,Y."
3984 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
3985 (setq artist-key-shape (artist-funcall draw-fn x y))))
3988 (defun artist-key-draw-2points (x y)
3989 "Draw current 2-point shape at X,Y."
3990 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
3991 (x1 (artist-endpoint-get-x artist-key-endpoint1))
3992 (y1 (artist-endpoint-get-y artist-key-endpoint1)))
3993 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y))))
3995 (defun artist-key-draw-common ()
3996 "Common routine for drawing current shape."
3997 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
3998 (col (artist-current-column))
3999 (row (artist-current-line)))
4001 ;; Depending on what we are currently drawing, call other routines
4002 ;; that knows how to do the job
4004 (cond ((eq draw-how 'artist-do-continously)
4005 (artist-key-draw-continously col row))
4006 ((eq draw-how 'artist-do-poly)
4007 (artist-key-draw-poly col row))
4008 ((and (numberp draw-how) (= draw-how 1))
4009 (artist-key-draw-1point col row))
4010 ((and (numberp draw-how) (= draw-how 2))
4011 (artist-key-draw-2points col row))
4012 (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
4014 ;; Now restore the old position
4016 (artist-move-to-xy col row)))
4021 ;; Functions related to trimming line-endings
4022 ;; The region between the topmost and bottommost visited line is
4023 ;; called a draw-region.
4026 (defun artist-draw-region-reset ()
4027 "Reset the current draw-region."
4028 (setq artist-draw-region-max-y 0)
4029 (setq artist-draw-region-min-y 1000000))
4031 (defun artist-draw-region-trim-line-endings (min-y max-y)
4032 "Trim lines in current draw-region from MIN-Y to MAX-Y.
4033 Trimming here means removing white space at end of a line."
4034 ;; Safety check: switch min-y and max-y if max-y is smaller
4040 (let ((curr-y min-y))
4041 (while (<= curr-y max-y)
4042 (artist-move-to-xy 0 curr-y)
4044 (delete-horizontal-space)
4045 (setq curr-y (1+ curr-y))))))
4048 ;; Drawing shapes by using keys
4051 (defun artist-key-do-continously-continously (x y)
4052 "Update current continuous shape at X,Y."
4053 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
4054 (artist-funcall draw-fn x y)))
4057 (defun artist-key-do-continously-poly (x y)
4058 "Update current poly-point shape with nth point at X,Y."
4059 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4060 (undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
4061 (x1 (artist-endpoint-get-x artist-key-endpoint1))
4062 (y1 (artist-endpoint-get-y artist-key-endpoint1))
4065 ;; If not rubber-banding, then move the 2
4066 ;; Otherwise re-draw the shape to the new position
4068 (if (not artist-rubber-banding)
4070 (artist-no-rb-unset-point2)
4071 (artist-no-rb-set-point2 x y))
4073 (artist-funcall undraw-fn artist-key-shape)
4074 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))))
4077 (defun artist-key-do-continously-1point (x y)
4078 "Update current 1-point shape at X,Y."
4079 ;; Nothing to do continuously for operations
4080 ;; where we have only one input point
4083 (defun artist-key-do-continously-2points (x y)
4084 "Update current 2-point shape with 2nd point at X,Y."
4085 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4086 (undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
4087 (x1 (artist-endpoint-get-x artist-key-endpoint1))
4088 (y1 (artist-endpoint-get-y artist-key-endpoint1))
4091 ;; If not rubber-banding, then move the 2
4092 ;; Otherwise re-draw the shape to the new position
4094 (if (not artist-rubber-banding)
4096 (artist-no-rb-unset-point2)
4097 (artist-no-rb-set-point2 x y))
4099 (artist-funcall undraw-fn artist-key-shape)
4100 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))))
4103 (defun artist-key-do-continously-common ()
4104 "Common routine for updating current shape."
4105 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
4106 (col (artist-current-column))
4107 (row (artist-current-line)))
4109 ;; Depending on what we are currently drawing, call other routines
4110 ;; that knows how to do the job
4112 (cond ((eq draw-how 'artist-do-continously)
4113 (artist-key-do-continously-continously col row))
4114 ((eq draw-how 'artist-do-poly)
4115 (artist-key-do-continously-poly col row))
4116 ((and (numberp draw-how) (= draw-how 1))
4117 (artist-key-do-continously-1point col row))
4118 ((and (numberp draw-how) (= draw-how 2))
4119 (artist-key-do-continously-2points col row))
4120 (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
4122 ;; Now restore the old position
4124 (artist-move-to-xy col row)))
4127 (defun artist-key-set-point-continously (x y)
4128 "Set point for current continuous shape at X,Y."
4129 ;; Maybe set arrow-points for continuous shapes
4130 (let ((arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
4131 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go))
4132 (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
4133 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
4134 (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go)))
4136 (if (not artist-key-is-drawing)
4137 ;; *** We are about to begin drawing
4139 (artist-funcall init-fn x y))
4141 ;; *** We are about to stop drawing
4144 (artist-funcall prep-fill-fn x y)
4145 (if (artist-funcall arrow-pred)
4146 (artist-funcall arrow-set-fn x y)
4147 (artist-clear-arrow-points))
4148 (artist-funcall exit-fn x y))))
4150 ;; Toggle the is-drawing flag
4151 (setq artist-key-is-drawing (not artist-key-is-drawing)))
4155 (defun artist-key-set-point-poly (x y &optional this-is-last-point)
4156 "Set point for current poly-point shape at X,Y.
4157 If optional argument THIS-IS-LAST-POINT is non-nil, this point is the last."
4158 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4159 (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
4160 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
4161 (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
4162 (fill-pred (artist-go-get-fill-pred-from-symbol artist-curr-go))
4163 (fill-fn (artist-go-get-fill-fn-from-symbol artist-curr-go))
4164 (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
4165 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
4167 (if (not artist-key-is-drawing)
4169 ;; *** We were not drawing ==> set first point
4172 (artist-funcall init-fn x y)
4174 ;; If not rubber-banding, set first point.
4175 ;; Otherwise, draw the shape from x,y to x,y
4176 (if (not artist-rubber-banding)
4177 (artist-no-rb-set-point1 x y)
4178 (setq artist-key-shape (artist-funcall draw-fn x y x y)))
4180 ;; Set first endpoint
4181 (setq artist-key-endpoint1 (artist-make-endpoint x y))
4183 ;; Set point-list to contain start point
4184 (setq artist-key-poly-point-list (list (artist-make-endpoint x y)))
4186 ;; Since we are not ready, set the arrow-points to nil
4187 (artist-clear-arrow-points)
4189 ;; Change state to drawing
4190 (setq artist-key-is-drawing t)
4193 (message "%s" (substitute-command-keys
4194 (concat "First point set. "
4195 "Set next with \\[artist-key-set-point], "
4196 "set last with C-u \\[artist-key-set-point]"))))
4199 ;; *** We were drawing ==> we are about to set nth point
4200 ;; (last point if the argument this-is-last-point is non-nil)
4202 (let ((x1 (artist-endpoint-get-x artist-key-endpoint1))
4203 (y1 (artist-endpoint-get-y artist-key-endpoint1))
4207 ;; If not rubber-banding, undraw the 1's and 2's, then
4208 ;; draw the shape (if we were rubber-banding, then the
4209 ;; shape is already drawn in artist-key-do-continously-2points.)
4211 (if (not artist-rubber-banding)
4213 (artist-no-rb-unset-points)
4214 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))
4216 ;; Set x2 and y2 from shape's second point
4217 ;; (which might be different from the mouse's second point,
4218 ;; if, for example, we are drawing a straight line)
4220 (if (not (null artist-key-shape))
4221 (let ((endpoint2 (artist-2point-get-endpoint2 artist-key-shape)))
4222 (setq x2 (artist-endpoint-get-x endpoint2))
4223 (setq y2 (artist-endpoint-get-y endpoint2))))
4225 ;; Add the endpoint to the list of poly-points
4226 (setq artist-key-poly-point-list
4227 (append artist-key-poly-point-list
4228 (list (artist-make-endpoint x2 y2))))
4230 ;; Now do handle the case when this is the last point,
4231 ;; and the case when this point isn't the last
4233 (if (not this-is-last-point)
4234 ;; ** This is not the last point
4236 ;; Start drawing a new 2-point-shape from last endpoint.
4238 ;; First set the start-point
4241 (setq artist-key-endpoint1 (artist-make-endpoint x1 y1))
4243 ;; If we are not rubber-banding, then place the '1
4244 ;; Otherwise, draw the shape from x1,y1 to x1,y1
4245 (if (not artist-rubber-banding)
4246 (artist-no-rb-set-point1 x1 y1)
4247 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x1 y1)))
4250 (message "Point set"))
4252 ;; ** This is the last point
4255 (artist-funcall prep-fill-fn artist-key-poly-point-list)
4258 (if (artist-funcall fill-pred)
4259 (artist-funcall fill-fn artist-key-shape
4260 artist-key-poly-point-list))
4262 ;; Set the arrow-points
4263 (if (artist-funcall arrow-pred)
4264 (artist-funcall arrow-set-fn artist-key-poly-point-list)
4265 (artist-clear-arrow-points))
4267 (artist-funcall exit-fn artist-key-poly-point-list)
4269 ;; Change state to not drawing
4270 (setq artist-key-shape nil)
4271 (setq artist-key-endpoint1 nil)
4272 (setq artist-key-is-drawing nil)))))))
4275 (defun artist-key-set-point-1point (x y)
4276 "Set point for current 1-point shape at X,Y."
4277 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4278 (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
4279 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
4280 (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
4281 (draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4282 (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
4283 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
4284 (artist-funcall init-fn x y)
4285 (artist-funcall draw-fn x y)
4286 (artist-funcall prep-fill-fn x y)
4287 (if (artist-funcall arrow-pred)
4288 (artist-funcall arrow-set-fn x y)
4289 (artist-clear-arrow-points))
4290 (artist-funcall exit-fn x y))
4291 (setq artist-key-shape nil)
4292 (setq artist-key-is-drawing nil))
4295 (defun artist-key-set-point-2points (x y)
4296 "Set first or second point in current 2-point shape at X,Y."
4297 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4298 (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
4299 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
4300 (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
4301 (fill-pred (artist-go-get-fill-pred-from-symbol artist-curr-go))
4302 (fill-fn (artist-go-get-fill-fn-from-symbol artist-curr-go))
4303 (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
4304 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
4305 (if (not artist-key-is-drawing)
4307 ;; *** We were not drawing ==> set first point
4310 (artist-funcall init-fn x y)
4312 ;; If not rubber-banding, set first point.
4313 ;; Otherwise, draw the shape from x,y to x,y
4314 (if (not artist-rubber-banding)
4315 (artist-no-rb-set-point1 x y)
4316 (setq artist-key-shape (artist-funcall draw-fn x y x y)))
4318 ;; Set first endpoint
4319 (setq artist-key-endpoint1 (artist-make-endpoint x y))
4321 ;; Since we are not ready, clear the arrow-points
4322 (artist-clear-arrow-points)
4324 ;; Change state to drawing
4325 (setq artist-key-is-drawing t))
4327 ;; *** We were drawing ==> we are about to set 2nd point
4328 ;; and end the drawing operation
4330 (let ((x1 (artist-endpoint-get-x artist-key-endpoint1))
4331 (y1 (artist-endpoint-get-y artist-key-endpoint1))
4335 ;; If not rubber-banding, undraw the 1's and 2's, then
4336 ;; draw the shape (if we were rubber-banding, then the
4337 ;; shape is already drawn in artist-key-do-continously-2points.)
4339 (if (not artist-rubber-banding)
4341 (artist-no-rb-unset-points)
4342 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))
4344 (artist-funcall prep-fill-fn artist-key-shape x1 y1 x2 y2)
4348 (if (artist-funcall fill-pred)
4349 (artist-funcall fill-fn artist-key-shape x1 y1 x2 y2))
4351 ;; Maybe set the arrow-points
4353 (if (artist-funcall arrow-pred)
4354 (artist-funcall arrow-set-fn artist-key-shape x1 y1 x2 y2)
4355 (artist-clear-arrow-points))
4357 (artist-funcall exit-fn artist-key-shape x1 y1 x2 y2)
4359 ;; Change state to not drawing
4360 (setq artist-key-is-drawing nil)))))
4363 (defun artist-key-set-point-common (arg)
4364 "Common routine for setting point in current shape.
4365 With non-nil ARG, set the last point."
4366 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
4367 (col (artist-current-column))
4368 (row (artist-current-line))
4369 (was-drawing artist-key-is-drawing))
4371 ;; First, if we are about to draw, then reset the draw-region
4372 (if (not artist-key-is-drawing)
4373 (artist-draw-region-reset))
4375 ;; Depending on what we are currently drawing, call other routines
4376 ;; that knows how to do the job
4378 (cond ((eq draw-how 'artist-do-continously)
4379 (artist-key-set-point-continously col row)
4380 ;; Do this now, otherwise nothing will happen until we move.
4381 (artist-key-do-continously-continously col row))
4382 ((eq draw-how 'artist-do-poly)
4383 (artist-key-set-point-poly col row arg))
4384 ((and (numberp draw-how) (= draw-how 1))
4385 (artist-key-set-point-1point col row))
4386 ((and (numberp draw-how) (= draw-how 2))
4387 (artist-key-set-point-2points col row))
4388 (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
4390 ;; Maybe trim line endings
4391 (if (and artist-trim-line-endings
4393 (not artist-key-is-drawing))
4394 (artist-draw-region-trim-line-endings artist-draw-region-min-y
4395 artist-draw-region-max-y))
4397 ;; Now restore the old position
4399 (artist-move-to-xy col row)
4400 (artist-mode-line-show-curr-operation artist-key-is-drawing)))
4406 (defun artist-previous-line (&optional n)
4407 "Move cursor up N lines (default is 1), updating current shape.
4408 If N is negative, move cursor down."
4410 (let ((col (artist-current-column)))
4411 (forward-line (- n))
4412 (move-to-column col t))
4413 (when artist-key-is-drawing
4414 (artist-key-do-continously-common)))
4417 (defun artist-next-line (&optional n)
4418 "Move cursor down N lines (default is 1), updating current shape.
4419 If N is negative, move cursor up."
4421 (let ((col (artist-current-column)))
4423 (move-to-column col t))
4424 (when artist-key-is-drawing
4425 (artist-key-do-continously-common)))
4427 (defun artist-backward-char (&optional n)
4428 "Move cursor backward N chars (default is 1), updating current shape.
4429 If N is negative, move forward."
4432 (artist-forward-char (- n))
4433 (artist-forward-char n)))
4435 (defun artist-forward-char (&optional n)
4436 "Move cursor forward N chars (default is 1), updating current shape.
4437 If N is negative, move backward."
4439 (let* ((step-x (if (>= n 0) 1 -1))
4441 (curr-col (artist-current-column))
4442 (new-col (max 0 (+ curr-col (* distance step-x)))))
4443 (if (not artist-key-is-drawing)
4444 (move-to-column new-col t)
4445 (move-to-column new-col t)
4446 (artist-key-do-continously-common))))
4449 (defun artist-key-set-point (&optional arg)
4450 "Set a point for the current shape. With optional ARG, set the last point."
4452 (artist-key-set-point-common arg))
4455 (defun artist-select-fill-char (c)
4456 "Set current fill character to be C."
4457 (interactive "cType fill char (type RET to turn off): ")
4458 (cond ((eq c ?\r) (setq artist-fill-char-set nil)
4459 (message "Fill canceled"))
4460 (t (setq artist-fill-char-set t)
4461 (setq artist-fill-char c)
4462 (message "Fill set to \"%c\"" c))))
4465 (defun artist-select-line-char (c)
4466 "Set current line character to be C."
4467 (interactive "cType line char (type RET to turn off): ")
4468 (cond ((eq c ?\r) (setq artist-line-char-set nil)
4469 (message "Normal lines"))
4470 (t (setq artist-line-char-set t)
4471 (setq artist-line-char c)
4472 (message "Line drawn with \"%c\"" c)))
4473 (if artist-key-is-drawing
4474 (artist-key-do-continously-common)))
4477 (defun artist-select-erase-char (c)
4478 "Set current erase character to be C."
4479 (interactive "cType char to use when erasing (type RET for normal): ")
4480 (cond ((eq c ?\r) (setq artist-erase-char ?\s)
4481 (message "Normal erasing"))
4482 (t (setq artist-erase-char c)
4483 (message "Erasing with \"%c\"" c)))
4484 (if artist-key-is-drawing
4485 (artist-key-do-continously-common)))
4487 (defun artist-charlist-to-string (char-list)
4488 "Convert a list of characters, CHAR-LIST, to a string."
4491 (defun artist-string-to-charlist (str)
4492 "Convert a string, STR, to list of characters."
4495 (defun artist-select-spray-chars (chars initial-char)
4496 "Set current spray characters to be CHARS, starting with INITIAL-CHAR."
4497 ;; This huge unreadable `interactive'-clause does the following
4498 ;; 1. Asks for a string of spray-characters
4499 ;; 2. Asks for the initial character (default is the first),
4500 ;; and loops if the answer is not a char within the string in 1.
4502 (let* ((str (read-string "Select spray-can characters, lightest first: "
4503 (artist-charlist-to-string artist-spray-chars)))
4504 (char-list (artist-string-to-charlist str))
4505 (initial (let* ((err-msg "")
4507 (first-char-as-str (char-to-string (car char-list)))
4508 (first-s) (first-c))
4512 (format (concat "%sSelect initial-character, "
4513 "one of \"%s\" (%s): ")
4514 err-msg str first-char-as-str)))
4515 (if (equal first-s "")
4516 (setq first-s first-char-as-str))
4517 (setq first-c (car (artist-string-to-charlist first-s)))
4518 (setq ok (not (null (member first-c char-list))))
4520 (setq err-msg (format
4521 "Not in spray-chars: \"%s\". "
4522 (char-to-string first-c)))))
4524 (list char-list initial)))
4525 (setq artist-spray-chars chars)
4526 (setq artist-spray-new-char initial-char)
4527 (message "Spray-chars set to \"%s\", initial: \"%s\""
4528 (artist-charlist-to-string chars) (char-to-string initial-char)))
4531 (defun artist-select-operation (op-str)
4532 "Select drawing operation OP-STR."
4533 (interactive (list (completing-read "Select operation: "
4534 artist-key-compl-table)))
4535 (let* ((op-symbol (artist-mt-get-symbol-from-keyword op-str))
4536 (draw-how (if op-symbol
4537 (artist-go-get-draw-how-from-symbol op-symbol)
4539 ;; First check that the string was valid
4540 (if (null op-symbol)
4541 (error "Unknown drawing method: %s" op-str))
4543 ;; Second, check that we are not about to switch to a different
4544 ;; kind of shape (do that only if we are drawing with keys;
4545 ;; otherwise this function cannot get called).
4546 (if (and artist-key-is-drawing
4547 (not (equal artist-key-draw-how draw-how)))
4548 (error "Cannot switch to a different kind of shape while drawing"))
4550 ;; If we were drawing, undraw the shape
4551 (if (and artist-key-is-drawing
4552 artist-rubber-banding)
4553 (artist-key-undraw-common))
4555 ;; Set the current operation and draw-how
4556 (setq artist-curr-go op-symbol)
4557 (setq artist-key-draw-how draw-how)
4559 ;; If we were drawing, redraw the shape (but don't if shape
4560 ;; is drawn by setting only one point)
4561 (if (and artist-key-is-drawing
4562 artist-rubber-banding
4563 (not (eq artist-key-draw-how 1)))
4564 (artist-key-draw-common)))
4567 (artist-mode-line-show-curr-operation artist-key-is-drawing))
4570 (defun artist-toggle-rubber-banding (&optional state)
4571 "Toggle rubber-banding.
4572 If optional argument STATE is positive, turn rubber-banding on."
4574 (if artist-key-is-drawing
4575 (error "Cannot toggle rubber-banding while drawing"))
4576 (if (setq artist-rubber-banding
4577 (if (null state) (not artist-rubber-banding)
4578 (> (prefix-numeric-value state) 0)))
4579 (message "Rubber-banding is now on")
4580 (message "Rubber-banding is now off")))
4583 (defun artist-toggle-trim-line-endings (&optional state)
4584 "Toggle trimming of line-endings.
4585 If optional argument STATE is positive, turn trimming on."
4587 (if (setq artist-trim-line-endings
4588 (if (null state) (not artist-trim-line-endings)
4589 (> (prefix-numeric-value state) 0)))
4590 (message "Trimming is now on")
4591 (message "Trimming is now off")))
4594 (defun artist-toggle-borderless-shapes (&optional state)
4595 "Toggle borders of shapes.
4596 If optional argument STATE is positive, turn borders on."
4598 (if (setq artist-borderless-shapes
4599 (if (null state) (not artist-borderless-shapes)
4600 (> (prefix-numeric-value state) 0)))
4601 (message "Borders are now off")
4602 (message "Borders are now on")))
4605 (defun artist-toggle-first-arrow ()
4606 "Toggle first arrow for shape, if possible."
4609 (if (not (null artist-arrow-point-1))
4610 (let* ((arrow-point artist-arrow-point-1)
4611 (arrow-state (artist-arrow-point-get-state arrow-point))
4612 (arrow-marker (artist-arrow-point-get-marker arrow-point))
4613 (direction (artist-arrow-point-get-direction arrow-point))
4614 (orig-char (artist-arrow-point-get-orig-char arrow-point))
4615 (arrow-char (aref artist-arrows direction))
4616 (new-state (not arrow-state)))
4618 (goto-char (marker-position arrow-marker))
4622 (artist-replace-char arrow-char))
4623 (artist-replace-char orig-char))
4625 (artist-arrow-point-set-state artist-arrow-point-1 new-state)))))
4627 (defun artist-toggle-second-arrow ()
4628 "Toggle second arrow for shape, if possible."
4631 (if (not (null artist-arrow-point-2))
4632 (let* ((arrow-point artist-arrow-point-2)
4633 (arrow-state (artist-arrow-point-get-state arrow-point))
4634 (arrow-marker (artist-arrow-point-get-marker arrow-point))
4635 (direction (artist-arrow-point-get-direction arrow-point))
4636 (orig-char (artist-arrow-point-get-orig-char arrow-point))
4637 (arrow-char (aref artist-arrows direction))
4638 (new-state (not arrow-state)))
4640 (goto-char (marker-position arrow-marker))
4644 (artist-replace-char arrow-char))
4645 (artist-replace-char orig-char))
4647 (artist-arrow-point-set-state artist-arrow-point-2 new-state)))))
4649 (defun artist-select-op-pen-line ()
4650 "Select drawing pen lines."
4652 (artist-select-operation "Pen Line"))
4654 (defun artist-select-op-line ()
4655 "Select drawing lines."
4657 (artist-select-operation "line"))
4659 (defun artist-select-op-straight-line ()
4660 "Select drawing straight lines."
4662 (artist-select-operation "straight line"))
4664 (defun artist-select-op-rectangle ()
4665 "Select drawing rectangles."
4667 (artist-select-operation "rectangle"))
4669 (defun artist-select-op-square ()
4670 "Select drawing squares."
4672 (artist-select-operation "square"))
4674 (defun artist-select-op-poly-line ()
4675 "Select drawing poly-lines."
4677 (artist-select-operation "poly-line"))
4679 (defun artist-select-op-straight-poly-line ()
4680 "Select drawing straight poly-lines."
4682 (artist-select-operation "straight poly-line"))
4684 (defun artist-select-op-ellipse ()
4685 "Select drawing ellipses."
4687 (artist-select-operation "ellipse"))
4689 (defun artist-select-op-circle ()
4690 "Select drawing circles."
4692 (artist-select-operation "circle"))
4694 (defun artist-select-op-text-see-thru ()
4695 "Select rendering text (see thru)."
4697 (artist-select-operation "text see-thru"))
4699 (defun artist-select-op-text-overwrite ()
4700 "Select rendering text (overwrite)."
4702 (artist-select-operation "text overwrite"))
4704 (defun artist-select-op-spray-can ()
4707 (artist-select-operation "spray-can"))
4709 (defun artist-select-op-spray-set-size ()
4710 "Select setting size for spraying."
4712 (artist-select-operation "spray set size"))
4714 (defun artist-select-op-erase-char ()
4715 "Select erasing characters."
4717 (artist-select-operation "erase char"))
4719 (defun artist-select-op-erase-rectangle ()
4720 "Select erasing rectangles."
4722 (artist-select-operation "erase rectangle"))
4724 (defun artist-select-op-vaporize-line ()
4725 "Select vaporizing single lines."
4727 (artist-select-operation "vaporize line"))
4729 (defun artist-select-op-vaporize-lines ()
4730 "Select vaporizing connected lines."
4732 (artist-select-operation "vaporize lines"))
4734 (defun artist-select-op-cut-rectangle ()
4735 "Select cutting rectangles."
4737 (artist-select-operation "cut rectangle"))
4739 (defun artist-select-op-cut-square ()
4740 "Select cutting squares."
4742 (artist-select-operation "cut square"))
4744 (defun artist-select-op-copy-rectangle ()
4745 "Select copying rectangles."
4747 (artist-select-operation "copy rectangle"))
4749 (defun artist-select-op-copy-square ()
4750 "Select copying squares."
4752 (artist-select-operation "cut square"))
4754 (defun artist-select-op-paste ()
4757 (artist-select-operation "paste"))
4759 (defun artist-select-op-flood-fill ()
4760 "Select flood-filling."
4762 (artist-select-operation "flood-fill"))
4765 ;; Drawing lines by using mouse
4766 ;; Mouse button actions
4769 (defun artist-update-pointer-shape ()
4770 "Perform the update of the X Windows pointer shape."
4771 (set-mouse-color nil))
4773 (defvar x-pointer-shape)
4775 (defun artist-set-pointer-shape (new-pointer-shape)
4776 "Set the shape of the X Windows pointer to NEW-POINTER-SHAPE."
4777 (setq x-pointer-shape new-pointer-shape)
4778 (artist-update-pointer-shape))
4780 (defsubst artist-event-is-shifted (ev)
4781 "Check whether the shift-key is pressed in event EV."
4782 (memq 'shift (event-modifiers ev)))
4784 (defun artist-do-nothing ()
4785 "Function that does nothing."
4788 (defun artist-compute-up-event-key (ev)
4789 "Compute the corresponding up key sequence for event EV."
4790 (let* ((basic (event-basic-type ev))
4792 (shifted (make-symbol (concat "S-" (symbol-name basic)))))
4793 (if (artist-event-is-shifted ev)
4794 (make-vector 1 shifted)
4795 (make-vector 1 unshifted))))
4797 (defun artist-down-mouse-1 (ev)
4798 "Perform drawing action for event EV."
4800 (let* ((real (artist-go-get-symbol-shift
4801 artist-curr-go (artist-event-is-shifted ev)))
4802 (draw-how (artist-go-get-draw-how-from-symbol real))
4803 ;; Remember original values for draw-region-min-y and max-y
4804 ;; in case we are interrupting a key-draw operation.
4805 (orig-draw-region-min-y artist-draw-region-min-y)
4806 (orig-draw-region-max-y artist-draw-region-max-y)
4807 (orig-pointer-shape (if (eq window-system 'x) x-pointer-shape nil))
4808 (echoq-keystrokes 10000) ; a lot of seconds
4809 ;; Remember original binding for the button-up event to this
4810 ;; button-down event.
4811 (key (artist-compute-up-event-key ev))
4812 (orig-button-up-binding (lookup-key (current-global-map) key)))
4816 (if (eq window-system 'x)
4817 (artist-set-pointer-shape artist-pointer-shape))
4819 ;; Redefine the button-up binding temporarily (the original
4820 ;; binding is restored in the unwind-forms below). This is to
4821 ;; avoid the phenomenon outlined in this scenario:
4823 ;; 1. A routine which reads something from the mini-buffer (such
4824 ;; as the text renderer) is called from below.
4825 ;; 2. Meanwhile, the users releases the mouse button.
4826 ;; 3. As a (funny :-) coincidence, the binding for the
4827 ;; button-up event is often mouse-set-point, so Emacs
4828 ;; sets the point to where the button was released, which is
4829 ;; in the buffer where the user wants to place the text.
4830 ;; 4. The user types C-x o (or uses the mouse once again)
4831 ;; until he reaches the mini-buffer which is still prompting
4832 ;; for some text to render.
4834 ;; To do this foolproof, all local and minor-mode maps should
4835 ;; be searched and temporarily changed as well, since they
4836 ;; too might have some binding for the button-up event,
4837 ;; but I hope dealing with the global map will suffice.
4838 (define-key (current-global-map) key 'artist-do-nothing)
4840 (artist-draw-region-reset)
4842 (artist-mode-line-show-curr-operation t)
4844 (cond ((eq draw-how 'artist-do-continously)
4845 (artist-mouse-draw-continously ev))
4846 ((eq draw-how 'artist-do-poly)
4847 (artist-mouse-draw-poly ev))
4848 ((and (numberp draw-how) (= draw-how 1))
4849 (artist-mouse-draw-1point ev))
4850 ((and (numberp draw-how) (= draw-how 2))
4851 (artist-mouse-draw-2points ev))
4852 (t (message "Drawing \"%s\"s is not yet implemented"
4855 (if artist-trim-line-endings
4856 (artist-draw-region-trim-line-endings artist-draw-region-min-y
4857 artist-draw-region-max-y))
4858 (setq artist-draw-region-min-y orig-draw-region-min-y)
4859 (setq artist-draw-region-max-y orig-draw-region-max-y))
4862 (if (eq window-system 'x)
4863 (artist-set-pointer-shape orig-pointer-shape))
4865 (if orig-button-up-binding
4866 (define-key (current-global-map) key orig-button-up-binding))
4868 (artist-mode-line-show-curr-operation artist-key-is-drawing))))
4871 (defun artist-mouse-choose-operation (ev op)
4872 "Choose operation for event EV and operation OP."
4875 (select-window (posn-window (event-start last-input-event)))
4876 (list last-input-event
4877 (if (display-popup-menus-p)
4878 (x-popup-menu last-nonmenu-event artist-popup-menu-table)
4881 (if (eq op 'no-popup-menus)
4882 ;; No popup menus. Call `tmm-prompt' instead, but with the
4883 ;; up-mouse-button, if any, temporarily disabled, otherwise
4885 (let* ((key (artist-compute-up-event-key ev))
4886 (orig-button-up-binding (lookup-key (current-global-map) key)))
4888 (define-key (current-global-map) key 'artist-do-nothing)
4889 (setq op (tmm-prompt artist-popup-menu-table))
4890 (if orig-button-up-binding
4891 (define-key (current-global-map) key orig-button-up-binding)))))
4893 (let ((draw-fn (artist-go-get-draw-fn-from-symbol (car op)))
4894 (set-fn (artist-fc-get-fn-from-symbol (car op))))
4897 ;; *** It was a draw-function
4898 ((not (listp draw-fn))
4899 (let* ((unshifted (artist-go-get-symbol-shift (car op) nil))
4900 (shifted (artist-go-get-symbol-shift (car op) t))
4901 (shift-state (artist-event-is-shifted ev))
4902 (selected-op (if shift-state shifted unshifted))
4903 (keyword (artist-go-get-keyword-from-symbol selected-op)))
4904 (artist-select-operation keyword)))
4906 ;; *** It was a set/unset function
4907 ((not (listp set-fn))
4908 (call-interactively set-fn)))))
4911 (defun artist-down-mouse-3 (ev)
4912 "Erase character or rectangle, depending on event EV."
4914 (let ((artist-curr-go 'erase-char))
4915 (artist-down-mouse-1 ev))
4916 ;; Restore mode-line
4917 (artist-mode-line-show-curr-operation artist-key-is-drawing))
4924 (defsubst artist-shift-has-changed (shift-state ev)
4925 "From the last SHIFT-STATE and EV, determine if the shift-state has changed."
4926 ;; This one simply doesn't work.
4928 ;; There seems to be no way to tell whether the user has pressed shift
4929 ;; while dragging the cursor around when we are in a track-mouse
4930 ;; form. Calling (event-modifiers ev) yields nil :-( Neither is the
4931 ;; (event-basic-type ev) of any help (it is simply `mouse-movement').
4933 ;; So this doesn't work:
4934 ;; (cond ((and shift-state (not (artist-event-is-shifted ev))) t)
4935 ;; ((and (not shift-state) (artist-event-is-shifted ev)) t)
4939 (defun artist-coord-win-to-buf (coord)
4940 "Convert a window-relative coordinate COORD to a buffer-relative coordinate."
4941 (let ((window-x (car coord))
4942 (window-y (cdr coord))
4943 (window-start-x (window-hscroll))
4944 (window-start-y (save-excursion (goto-char (window-start))
4945 (artist-current-line))))
4946 (cons (+ window-x window-start-x)
4947 (+ window-y window-start-y))))
4950 (defun artist-mouse-draw-continously (ev)
4951 "Generic function for shapes that require 1 point as input.
4952 Operation is done continuously while the mouse button is hold down.
4953 The event, EV, is the mouse event."
4954 (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
4955 (shifted (artist-go-get-symbol-shift artist-curr-go t))
4956 (shift-state (artist-event-is-shifted ev))
4957 (op (if shift-state shifted unshifted))
4958 (draw-how (artist-go-get-draw-how-from-symbol op))
4959 (init-fn (artist-go-get-init-fn-from-symbol op))
4960 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
4961 (exit-fn (artist-go-get-exit-fn-from-symbol op))
4962 (draw-fn (artist-go-get-draw-fn-from-symbol op))
4963 (interval-fn (artist-go-get-interval-fn-from-symbol op))
4964 (interval (artist-funcall interval-fn))
4965 (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
4966 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
4967 (ev-start (event-start ev))
4968 (initial-win (posn-window ev-start))
4969 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
4970 (x1 (car ev-start-pos))
4971 (y1 (cdr ev-start-pos))
4974 (select-window (posn-window ev-start))
4975 (artist-funcall init-fn x1 y1)
4976 (if (not artist-rubber-banding)
4977 (artist-no-rb-set-point1 x1 y1))
4979 (while (or (mouse-movement-p ev)
4980 (member 'down (event-modifiers ev)))
4981 (setq ev-start-pos (artist-coord-win-to-buf
4982 (posn-col-row (event-start ev))))
4983 (setq x1 (car ev-start-pos))
4984 (setq y1 (cdr ev-start-pos))
4986 ;; Cancel previous timer
4988 (cancel-timer timer))
4990 (if (not (eq initial-win (posn-window (event-start ev))))
4991 ;; If we moved outside the window, do nothing
4994 ;; Still in same window:
4996 ;; Check if user presses or releases shift key
4997 (if (artist-shift-has-changed shift-state ev)
4999 ;; First check that the draw-how is the same as we
5000 ;; already have. Otherwise, ignore the changed shift-state.
5001 (if (not (eq draw-how
5002 (artist-go-get-draw-how-from-symbol
5003 (if (not shift-state) shifted unshifted))))
5004 (message "Cannot switch to shifted operation")
5006 ;; progn is "implicit" since this is the else-part
5007 (setq shift-state (not shift-state))
5008 (setq op (if shift-state shifted unshifted))
5009 (setq draw-how (artist-go-get-draw-how-from-symbol op))
5010 (setq draw-fn (artist-go-get-draw-fn-from-symbol op))))
5012 ;; Draw the new shape
5013 (setq shape (artist-funcall draw-fn x1 y1))
5014 (artist-move-to-xy x1 y1)
5016 ;; Start the timer to call `draw-fn' repeatedly every
5017 ;; `interval' second
5018 (if (and interval draw-fn)
5019 (setq timer (run-at-time interval interval draw-fn x1 y1))))
5022 (setq ev (read-event))))
5024 ;; Cancel any timers
5026 (cancel-timer timer))
5028 (artist-funcall prep-fill-fn x1 y1)
5030 (if (artist-funcall arrow-pred)
5031 (artist-funcall arrow-set-fn x1 y1)
5032 (artist-clear-arrow-points))
5034 (artist-funcall exit-fn x1 y1)
5035 (artist-move-to-xy x1 y1)))
5039 (defun artist-mouse-draw-poly (ev)
5040 "Generic function for shapes requiring several points as input.
5041 The event, EV, is the mouse event."
5043 (message "Mouse-1: set new point, mouse-2: set last point")
5044 (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
5045 (shifted (artist-go-get-symbol-shift artist-curr-go t))
5046 (shift-state (artist-event-is-shifted ev))
5047 (op (if shift-state shifted unshifted))
5048 (draw-how (artist-go-get-draw-how-from-symbol op))
5049 (init-fn (artist-go-get-init-fn-from-symbol op))
5050 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
5051 (exit-fn (artist-go-get-exit-fn-from-symbol op))
5052 (draw-fn (artist-go-get-draw-fn-from-symbol op))
5053 (undraw-fn (artist-go-get-undraw-fn-from-symbol op))
5054 (fill-pred (artist-go-get-fill-pred-from-symbol op))
5055 (fill-fn (artist-go-get-fill-fn-from-symbol op))
5056 (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
5057 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
5058 (ev-start (event-start ev))
5059 (initial-win (posn-window ev-start))
5060 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
5061 (x1-last (car ev-start-pos))
5062 (y1-last (cdr ev-start-pos))
5069 (select-window (posn-window ev-start))
5070 (artist-funcall init-fn x1-last y1-last)
5071 (if (not artist-rubber-banding)
5072 (artist-no-rb-set-point1 x1-last y1-last))
5075 ;; decide what to do
5078 ;; *** Mouse button is released.
5080 (or (member 'click (event-modifiers ev))
5081 (member 'drag (event-modifiers ev))))
5082 ;; First, if not rubber-banding, draw the line.
5084 (if (not artist-rubber-banding)
5086 (artist-no-rb-unset-points)
5087 (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))))
5089 ;; Set the second point to the shape's second point
5090 ;; (which might be different from the mouse's second point,
5091 ;; if, for example, we are drawing a straight line)
5093 (if (not (null shape))
5094 (let ((endpoint2 (artist-2point-get-endpoint2 shape)))
5095 (setq x1-last (artist-endpoint-get-x endpoint2))
5096 (setq y1-last (artist-endpoint-get-y endpoint2))))
5097 (setq point-list (cons (artist-make-endpoint x1-last y1-last)
5102 ;; *** Mouse button 2 or 3 down
5103 ((and (member 'down (event-modifiers ev))
5104 (or (equal (event-basic-type ev) 'mouse-2)
5105 (equal (event-basic-type ev) 'mouse-3)))
5109 ;; *** Mouse button 2 or 3 released
5110 ((and (or (member 'click (event-modifiers ev))
5111 (member 'drag (event-modifiers ev)))
5112 (or (equal (event-basic-type ev) 'mouse-2)
5113 (equal (event-basic-type ev) 'mouse-3)))
5115 ;; This means the end of our poly-line drawing-session.
5119 ;; *** Mouse button 1 went down
5121 (member 'down (event-modifiers ev))
5122 (equal (event-basic-type ev) 'mouse-1))
5123 ;; Check whether the (possibly new, that depends on if shift
5124 ;; has been pressed or released) symbol has the same draw-how
5125 ;; information as the previous had. If it hasn't, we can't
5128 (if (not (eq draw-how
5129 (artist-go-get-draw-how-from-symbol
5130 (if (not shift-state) shifted unshifted))))
5131 (message "Cannot switch operation")
5136 (artist-go-get-symbol-shift artist-curr-go nil)
5138 (artist-go-get-symbol-shift artist-curr-go t)
5139 shift-state (artist-event-is-shifted ev)
5140 op (if shift-state shifted unshifted)
5141 draw-how (artist-go-get-draw-how-from-symbol op)
5142 draw-fn (artist-go-get-draw-fn-from-symbol op)
5143 undraw-fn (artist-go-get-undraw-fn-from-symbol op)
5144 fill-pred (artist-go-get-fill-pred-from-symbol op)
5145 fill-fn (artist-go-get-fill-fn-from-symbol op))
5147 ;; Draw shape from last place to this place
5151 (setq ev-start-pos (artist-coord-win-to-buf
5152 (posn-col-row (event-start ev))))
5153 (setq x2 (car ev-start-pos))
5154 (setq y2 (cdr ev-start-pos))
5156 ;; Draw the new shape (if not rubber-banding, place both marks)
5158 (if artist-rubber-banding
5159 (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))
5161 (artist-no-rb-set-point1 x1-last y1-last)
5162 (artist-no-rb-set-point2 x2 y2)))
5164 ;; Show new operation in mode-line
5165 (let ((artist-curr-go op))
5166 (artist-mode-line-show-curr-operation t))))
5171 ;; *** Mouse moved, button is down and we are still in orig window
5172 ((and (mouse-movement-p ev)
5174 (eq initial-win (posn-window (event-start ev))))
5175 ;; Draw shape from last place to this place
5178 (setq ev-start-pos (artist-coord-win-to-buf
5179 (posn-col-row (event-start ev))))
5180 (setq x2 (car ev-start-pos))
5181 (setq y2 (cdr ev-start-pos))
5183 ;; First undraw last shape
5184 ;; (unset last point if not rubberbanding)
5186 (artist-funcall undraw-fn shape)
5188 ;; Draw the new shape (if not rubberbanding, set 2nd mark)
5190 (if artist-rubber-banding
5191 (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))
5193 (artist-no-rb-unset-point2)
5194 (artist-no-rb-set-point2 x2 y2)))
5196 (artist-move-to-xy x2 y2))
5198 ;; *** Mouse moved, button is down but we are NOT in orig window
5199 ((and (mouse-movement-p ev)
5201 (not (eq initial-win (posn-window (event-start ev)))))
5206 ;; *** Moving mouse while mouse button is not down
5207 ((and (mouse-movement-p ev) (not is-down))
5208 ;; don't do anything.
5212 ;; *** Mouse button 1 went down, first time
5214 (member 'down (event-modifiers ev))
5215 (equal (event-basic-type ev) 'mouse-1))
5216 ;; don't do anything
5220 ;; *** Another event
5226 ;; Read next event (only if we should not stop)
5228 (setq ev (read-event)))))
5230 ;; Reverse point-list (last points are cond'ed first)
5231 (setq point-list (reverse point-list))
5233 (artist-funcall prep-fill-fn point-list)
5236 (if (artist-funcall fill-pred)
5237 (artist-funcall fill-fn point-list))
5239 ;; Maybe set arrow points
5240 (if (and point-list (artist-funcall arrow-pred))
5241 (artist-funcall arrow-set-fn point-list)
5242 (artist-clear-arrow-points))
5244 (artist-funcall exit-fn point-list)
5245 (artist-move-to-xy x2 y2)))
5248 (defun artist-mouse-draw-1point (ev)
5249 "Generic function for shapes requiring only 1 point as input.
5250 Operation is done once. The event, EV, is the mouse event."
5252 (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
5253 (shifted (artist-go-get-symbol-shift artist-curr-go t))
5254 (shift-state (artist-event-is-shifted ev))
5255 (op (if shift-state shifted unshifted))
5256 (draw-how (artist-go-get-draw-how-from-symbol op))
5257 (init-fn (artist-go-get-init-fn-from-symbol op))
5258 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
5259 (exit-fn (artist-go-get-exit-fn-from-symbol op))
5260 (draw-fn (artist-go-get-draw-fn-from-symbol op))
5261 (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
5262 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
5263 (ev-start (event-start ev))
5264 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
5265 (x1 (car ev-start-pos))
5266 (y1 (cdr ev-start-pos)))
5267 (select-window (posn-window ev-start))
5268 (artist-funcall init-fn x1 y1)
5269 (artist-funcall draw-fn x1 y1)
5270 (artist-funcall prep-fill-fn x1 y1)
5271 (if (artist-funcall arrow-pred)
5272 (artist-funcall arrow-set-fn x1 y1)
5273 (artist-clear-arrow-points))
5274 (artist-funcall exit-fn x1 y1)
5275 (artist-move-to-xy x1 y1)))
5278 (defun artist-mouse-draw-2points (ev)
5279 "Generic function for shapes requiring 2 points as input.
5280 The event, EV, is the mouse event."
5282 (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
5283 (shifted (artist-go-get-symbol-shift artist-curr-go t))
5284 (shift-state (artist-event-is-shifted ev))
5285 (op (if shift-state shifted unshifted))
5286 (draw-how (artist-go-get-draw-how-from-symbol op))
5287 (init-fn (artist-go-get-init-fn-from-symbol op))
5288 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
5289 (exit-fn (artist-go-get-exit-fn-from-symbol op))
5290 (draw-fn (artist-go-get-draw-fn-from-symbol op))
5291 (undraw-fn (artist-go-get-undraw-fn-from-symbol op))
5292 (fill-pred (artist-go-get-fill-pred-from-symbol op))
5293 (fill-fn (artist-go-get-fill-fn-from-symbol op))
5294 (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
5295 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
5296 (ev-start (event-start ev))
5297 (initial-win (posn-window ev-start))
5298 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
5299 (x1 (car ev-start-pos))
5300 (y1 (cdr ev-start-pos))
5304 (select-window (posn-window ev-start))
5305 (artist-funcall init-fn x1 y1)
5306 (if (not artist-rubber-banding)
5307 (artist-no-rb-set-point1 x1 y1))
5309 (while (or (mouse-movement-p ev)
5310 (member 'down (event-modifiers ev)))
5311 (setq ev-start-pos (artist-coord-win-to-buf
5312 (posn-col-row (event-start ev))))
5313 (setq x2 (car ev-start-pos))
5314 (setq y2 (cdr ev-start-pos))
5316 (if (not (eq initial-win (posn-window (event-start ev))))
5317 ;; If we moved outside the window, do nothing
5320 ;; Still in same window:
5322 ;; First undraw last shape (unset last point if not rubberbanding)
5323 (if artist-rubber-banding
5324 (artist-funcall undraw-fn shape)
5325 (artist-no-rb-unset-point2))
5327 ;; Check if user presses or releases shift key
5328 (if (artist-shift-has-changed shift-state ev)
5330 ;; First check that the draw-how is the same as we
5331 ;; already have. Otherwise, ignore the changed shift-state.
5332 (if (not (eq draw-how
5333 (artist-go-get-draw-how-from-symbol
5334 (if (not shift-state) shifted unshifted))))
5335 (message "Cannot switch to shifted operation")
5337 (message "Switching")
5338 ;; progn is "implicit" since this is the else-part
5339 (setq shift-state (not shift-state))
5340 (setq op (if shift-state shifted unshifted))
5341 (setq draw-how (artist-go-get-draw-how-from-symbol op))
5342 (setq draw-fn (artist-go-get-draw-fn-from-symbol op))
5343 (setq undraw-fn (artist-go-get-undraw-fn-from-symbol op))
5344 (setq fill-pred (artist-go-get-fill-pred-from-symbol op))
5345 (setq fill-fn (artist-go-get-fill-fn-from-symbol op))))
5347 ;; Draw the new shape
5348 (if artist-rubber-banding
5349 (setq shape (artist-funcall draw-fn x1 y1 x2 y2))
5350 (artist-no-rb-set-point2 x2 y2))
5352 (artist-move-to-xy x2 y2))
5356 (setq ev (read-event))))
5358 ;; If we are not rubber-banding (that is, we were moving around the `2')
5360 (if (not artist-rubber-banding)
5362 (artist-no-rb-unset-points)
5363 (setq shape (artist-funcall draw-fn x1 y1 x2 y2))))
5365 (artist-funcall prep-fill-fn shape x1 y1 x2 y2)
5368 (if (artist-funcall fill-pred)
5369 (artist-funcall fill-fn shape x1 y1 x2 y2))
5371 ;; Maybe set arrow-points
5372 (if (artist-funcall arrow-pred)
5373 (artist-funcall arrow-set-fn shape x1 y1 x2 y2)
5374 (artist-clear-arrow-points))
5376 (artist-funcall exit-fn shape x1 y1 x2 y2)
5377 (artist-move-to-xy x2 y2)))
5381 ;; Bug-report-submitting
5383 (defun artist-submit-bug-report ()
5384 "Submit via mail a bug report on Artist."
5387 (if (y-or-n-p "Do you want to submit a bug report on Artist? ")
5388 (let ((to artist-maintainer-address)
5389 (vars '(window-system
5390 window-system-version
5392 artist-rubber-banding
5393 artist-interface-with-rect
5395 ;; Now the internal ones
5397 artist-key-poly-point-list
5400 artist-arrow-point-1
5401 artist-arrow-point-2)))
5402 ;; Remove those variables from vars that are not bound
5406 (if (not (and (boundp x) (symbol-value x)))
5407 (setq vars (delq x vars))))) vars)
5408 (reporter-submit-bug-report
5409 artist-maintainer-address
5410 (concat "artist.el " artist-version)
5413 (concat "Hello Tomas,\n\n"
5414 "I have a nice bug report on Artist for you! Here it is:")))))
5418 ;; Now provide this minor mode
5424 ;;; About adding drawing modes
5425 ;;; --------------------------
5427 ;; If you are going to add a new drawing mode, read the following
5428 ;; sketchy outlines to get started a bit easier.
5430 ;; 1. If your new drawing mode falls into one of the following
5431 ;; categories, goto point 2, otherwise goto point 3.
5433 ;; - Modes where the shapes are drawn continuously, as long as
5434 ;; the mouse button is held down (continuous modes).
5435 ;; Example: the erase-char mode, the pen and pen-line modes.
5437 ;; - Modes where the shape is made up of from 2 points to an
5438 ;; arbitrary number of points (poly-point modes).
5439 ;; Example: the poly-line mode
5441 ;; - Modes where the shape is made up of 2 points (2-point
5443 ;; Example: lines, rectangles
5445 ;; - Modes where the shape is made up of 1 point (1-point
5446 ;; modes). This mode differs from the continuous modes in
5447 ;; that the shape is drawn only once when the mouse button
5449 ;; Examples: paste, a flood-fill, vaporize modes
5452 ;; 2. To make it easier and more flexible to program new drawing
5453 ;; modes, you might choose to specify
5454 ;; init-fn: a function to be called at the very beginning
5455 ;; of the drawing phase,
5456 ;; prep-fill-fn: a function to be called before filling,
5457 ;; arrow-set-fn: a function for setting arrows, to be called
5458 ;; after filling, and
5459 ;; exit-fn: a function to be called at the very end of
5460 ;; the drawing phase.
5461 ;; For each of the cases below, the arguments given to the init-fn,
5462 ;; prep-fill-fn, arrow-set-fn and exit-fn are stated.
5464 ;; If your mode matches the continuous mode or the 1-point mode:
5466 ;; a. Create a draw-function that draws your shape. Your function
5467 ;; must take x and y as arguments. The return value is not
5470 ;; b. Add your mode to the master table, `artist-mt'.
5473 ;; prep-fill-fn: x y
5474 ;; arrow-set-fn: x y
5477 ;; If your mode matches the 2-point mode:
5479 ;; a. Create one draw-function that draws your shape and one
5480 ;; undraw-function that undraws it.
5482 ;; The draw-function must take x1, y1, x2 and y2 as
5483 ;; arguments. It must return a list with three elements:
5484 ;; Endpoint1: a vector [x1 y1]
5485 ;; Endpoint2: a vector [x2 y2]
5486 ;; Shapeinfo: all info necessary for your undraw-function to
5487 ;; be able to undraw the shape
5488 ;; Use the artist-endpoint-* accessors to create and inspect
5491 ;; If applicable, you must be able to draw your shape without
5492 ;; borders if the `artist-borderless-shapes' is non-nil.
5493 ;; See `artist-draw-rect' for an example.
5495 ;; The undraw-function must take one argument: the list created
5496 ;; by your draw-function. The return value is not used.
5498 ;; b. If you want to provide a fill-function, then create a
5499 ;; function that takes 5 arguments: the list created by your
5500 ;; draw-function, x1, y1, x2 and y2. The return value is not
5503 ;; c. Add your mode to the master table, `artist-mt'.
5506 ;; prep-fill-fn: shape x1 y1 x2 y2
5507 ;; arrow-set-fn: shape x1 y1 x2 y2
5508 ;; exit-fn: shape x1 y1 x2 y2
5510 ;; If your mode matches the poly-point mode:
5512 ;; a. Create one draw-function that draws your shape and one
5513 ;; undraw-function that undraws it. The draw- and
5514 ;; undraw-functions are used to draw/undraw a segment of
5515 ;; your poly-point mode between 2 points. The draw- and
5516 ;; undraw-functions are then really 2-point mode functions.
5517 ;; They must take the same arguments and return the same
5518 ;; values as those of the 2-point mode.
5520 ;; If applicable, you must be able to draw your shape without
5521 ;; borders if the `artist-borderless-shapes' is non-nil.
5522 ;; See `artist-draw-rect' for an example.
5524 ;; b. If you want to provide a fill-function, then create a
5525 ;; function that takes 1 argument: a list of points where each
5526 ;; point is a vector, [x, y].
5528 ;; c. Add your mode to the master table, `artist-mt'.
5531 ;; prep-fill-fn: point-list
5532 ;; arrow-set-fn: point-list
5533 ;; exit-fn: point-list
5535 ;; The arrow-set-fn must set the variables `artist-arrow-point-1'
5536 ;; and `artist-arrow-point-2'. If your mode does not take arrows,
5537 ;; you must set the variables to nil. Use the accessors
5538 ;; artist-arrow-point-* to create and inspect arrow-points.
5541 ;; 3. If your mode doesn't match any of the categories, you are facing
5542 ;; a bit more work, and I cannot be as detailed as above. Here is a
5543 ;; brief outline of what you have to do:
5545 ;; a. Decide on a name for your type of mode. Let's assume that
5546 ;; you decided on `xxx'. Then you should use the draw-how
5547 ;; symbol artist-do-xxx.
5549 ;; b. Create a function artist-mouse-draw-xxx for drawing with
5550 ;; mouse. It should be called from `artist-down-mouse-1'.
5552 ;; The all coordinates must be converted from window-relative
5553 ;; to buffer relative before saved or handed over to
5554 ;; any other function. Converting is done with
5555 ;; the function `artist-coord-win-to-buf'.
5557 ;; It must take care to the `artist-rubber-banding' variable
5558 ;; and perform rubber-banding accordingly. Use the
5559 ;; artist-no-rb-* functions if not rubber-banding.
5561 ;; If applicable, you must be able to draw your shape without
5562 ;; borders if the `artist-borderless-shapes' is non-nil.
5563 ;; See `artist-draw-rect' for an example.
5565 ;; You must call the init-fn, the prep-fill-fn, arrow-set-fn
5566 ;; and the exit-fn at the appropriate points.
5568 ;; When artist-mouse-draw-xxx ends, the shape for your mode
5569 ;; must be completely drawn.
5571 ;; c. Create functions for drawing with keys:
5573 ;; - artist-key-set-point-xxx for setting a point in the
5574 ;; mode, to be called from `artist-key-set-point-common'.
5576 ;; - artist-key-do-continuously-xxx to be called from
5577 ;; `artist-key-do-continuously-common' whenever the user
5580 ;; As for the artist-mouse-draw-xxx, these two functions must
5581 ;; take care to do rubber-banding, borderless shapes and to
5584 ;; These functions should set the variable `artist-key-shape'
5585 ;; to the shape drawn.
5587 ;; d. Create artist-key-draw-xxx and artist-key-undraw-xxx for
5588 ;; drawing and undrawing. These are needed when the user
5589 ;; switches operation to draw another shape of the same type
5592 ;; You should provide these functions. You might think that
5593 ;; only you is using your type of mode, so no one will be able
5594 ;; to switch to another operation of the same type of mode,
5595 ;; but someone else might base a new drawing mode upon your
5598 ;; You must call the init-fn, the prep-fill-fn, arrow-set-fn
5599 ;; and the exit-fn at the appropriate points.
5601 ;; e. Add your new mode to the master table, `artist-mt'.
5604 ;; Happy hacking! Please let me hear if you add any drawing modes!
5605 ;; Don't hesitate to ask me any questions.
5608 ;;; artist.el ends here