(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / textmodes / artist.el
blob2d40d6da0266c007653d545f8e3c78df47b94624
1 ;;; artist.el --- draw ascii graphics with your mouse
3 ;; Copyright (C) 2000, 2001, 2004 Free Software Foundation, Inc.
5 ;; Author: Tomas Abrahamsson <tab@lysator.liu.se>
6 ;; Maintainer: Tomas Abrahamsson <tab@lysator.liu.se>
7 ;; Keywords: mouse
8 ;; Version: 1.2.6
9 ;; Release-date: 6-Aug-2004
10 ;; Location: http://www.lysator.liu.se/~tab/artist/
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
29 ;;; Commentary:
31 ;; What is artist?
32 ;; ---------------
34 ;; Artist is an Emacs lisp package that allows you to draw lines,
35 ;; rectangles and ellipses by using your mouse and/or keyboard. The
36 ;; shapes are made up with the ascii characters |, -, / and \.
38 ;; Features are:
40 ;; * Intersecting: When a `|' intersects with a `-', a `+' is
41 ;; drawn, like this: | \ /
42 ;; --+-- X
43 ;; | / \
45 ;; * Rubber-banding: When drawing lines you can interactively see the
46 ;; result while holding the mouse button down and moving the mouse. If
47 ;; your machine is not fast enough (a 386 is a bit to slow, but a
48 ;; pentium is well enough), you can turn this feature off. You will
49 ;; then see 1's and 2's which mark the 1st and 2nd endpoint of the line
50 ;; you are drawing.
52 ;; * Drawing operations: The following drawing operations are implemented:
54 ;; lines straight-lines
55 ;; rectangles squares
56 ;; poly-lines straight poly-lines
57 ;; ellipses circles
58 ;; text (see-thru) text (overwrite)
59 ;; spray-can setting size for spraying
60 ;; vaporize line vaporize lines
61 ;; erase characters erase rectangles
63 ;; Straight lines are lines that go horizontally, vertically or
64 ;; diagonally. Plain lines go in any direction. The operations in
65 ;; the right column are accessed by holding down the shift key while
66 ;; drawing.
68 ;; It is possible to vaporize (erase) entire lines and connected lines
69 ;; (rectangles for example) as long as the lines being vaporized are
70 ;; straight and connected at their endpoints. Vaporizing is inspired
71 ;; by the drawrect package by Jari Aalto <jari.aalto@poboxes.com>.
73 ;; * Flood-filling: You can fill any area with a certain character by
74 ;; flood-filling.
76 ;; * Cut copy and paste: You can cut, copy and paste rectangular
77 ;; regions. Artist also interfaces with the rect package (this can be
78 ;; turned off if it causes you any trouble) so anything you cut in
79 ;; artist can be yanked with C-x r y and vice versa.
81 ;; * Drawing with keys: Everything you can do with the mouse, you can
82 ;; also do without the mouse.
84 ;; * Arrows: After having drawn a (straight) line or a (straight)
85 ;; poly-line, you can set arrows on the line-ends by typing < or >.
87 ;; * Aspect-ratio: You can set the variable artist-aspect-ratio to
88 ;; reflect the height-width ratio for the font you are using. Squares
89 ;; and circles are then drawn square/round. Note, that once your
90 ;; ascii-file is shown with font with a different height-width ratio,
91 ;; the squares won't be square and the circles won't be round.
93 ;; * Picture mode compatibility: Artist is picture mode compatible (this
94 ;; can be turned off).
96 ;; See the documentation for the function artist-mode for a detailed
97 ;; description on how to use artist.
100 ;; What about adding my own drawing modes?
101 ;; ---------------------------------------
103 ;; See the short guide at the end of this file.
104 ;; If you add a new drawing mode, send it to me, and I would gladly
105 ;; include in the next release!
108 ;;; Installation:
110 ;; To use artist, put this in your .emacs:
112 ;; (autoload 'artist-mode "artist" "Enter artist-mode" t)
115 ;;; Requirements:
117 ;; Artist requires Emacs 19.28 or higher.
119 ;; Artist requires the `rect' package (which comes with Emacs) to be
120 ;; loadable, unless the variable `artist-interface-with-rect' is set
121 ;; to nil.
123 ;; Artist also requires the Picture mode (which also comes with Emacs)
124 ;; to be loadable, unless the variable `artist-picture-compatibility'
125 ;; is set to nil.
127 ;;; Known bugs:
129 ;; The shifted operations are not available when drawing with the mouse
130 ;; in Emacs 19.29 and 19.30.
132 ;; It is not possible to change between shifted and unshifted operation
133 ;; while drawing with the mouse. (See the comment in the function
134 ;; artist-shift-has-changed for further details.)
137 ;;; ChangeLog:
139 ;; 1.2.6 6-Aug-2004
140 ;; New: Coerced with the artist.el that's in Emacs-21.3.
141 ;; (minor editorial changes)
143 ;; 1.2.5 4-Aug-2004
144 ;; New: Added tool selection via the mouse-wheel
145 ;; Function provided by Andreas Leue <al@sphenon.de>
147 ;; 1.2.4 25-Oct-2001
148 ;; Bugfix: Some operations (the edit menu) got hidden
149 ;; Bugfix: The first arrow for poly-lines was always pointing
150 ;; to the right
151 ;; Changed: Updated with changes made for Emacs 21.1
153 ;; 1.2.3 20-Nov-2000
154 ;; Bugfix: Autoload cookie corrected
156 ;; 1.2.2 19-Nov-2000
157 ;; Changed: More documentation fixes.
158 ;; Bugfix: The arrow characters (`artist-arrows'), which
159 ;; got wrong in 1.1, are now corrected.
161 ;; 1.2.1 15-Nov-2000
162 ;; New: Documentation fixes.
163 ;; Bugfix: Sets next-line-add-newlines to t while in artist-mode.
164 ;; Drawing with keys was confusing without this fix, if
165 ;; next-line-add-newlines was set to nil.
166 ;; Thanks to Tatsuo Furukawa <tatsuo@kobe.hp.com> for this.
168 ;; 1.2 22-Oct-2000
169 ;; New: Updated to work with Emacs 21
171 ;; 1.1 15-Aug-2000
172 ;; Bugfix: Cursor follows mouse pointer more closely.
173 ;; New: Works with Emacs 20.x
174 ;; New: Variables are customizable
176 ;; 1.1-beta1 21-Apr-1998
177 ;; New: Spray-can (Utterly useless, I believe, but it was fun
178 ;; to implement :-) after an idea by Karl-Johan Karlsson
179 ;; <kj@lysator.liu.se>.
180 ;; New: Freehand drawing (with pen).
181 ;; New: Vaporizing lines.
182 ;; New: Text-rendering using figlet.
183 ;; New: Picture mode compatibility.
184 ;; Changed: All Artist keys now uses the prefix C-c C-a not to conflict
185 ;; with Picture mode.
186 ;; Bugfix: No longer leaves traces of lines when rubberbanding
187 ;; if the buffer auto-scrolls.
188 ;; Bugfix: Infinite loop sometimes when rubberbanding was turned
189 ;; off.
191 ;; 1.0 01-Mar-1998
192 ;; First official release.
194 ;;; Code:
196 ;; Variables
198 (defconst artist-version "1.2.6")
199 (defconst artist-maintainer-address "tab@lysator.liu.se")
202 (eval-and-compile
203 (condition-case ()
204 (require 'custom)
205 (error nil))
206 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
207 nil ;; We've got what we needed
208 ;; We have the old custom-library, hack around it!
209 (defmacro defgroup (&rest args)
210 nil)
211 (defmacro defface (var values doc &rest args)
212 `(make-face ,var))
213 (defmacro defcustom (var value doc &rest args)
214 `(defvar ,var ,value ,doc))))
216 ;; User options
217 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
219 (defgroup artist nil
220 "Customization of the Artist mode."
221 :group 'mouse)
223 (defgroup artist-text nil
224 "Customization of the text rendering."
225 :group 'artist)
227 (defcustom artist-rubber-banding t
228 "Interactively do rubber-banding when non-nil."
229 :group 'artist
230 :type 'boolean)
232 (defcustom artist-first-char ?1
233 "Character to set at first point when not rubber-banding."
234 :group 'artist
235 :type 'character)
237 (defcustom artist-second-char ?2
238 "Character to set at second point when not rubber-banding."
239 :group 'artist
240 :type 'character)
242 (defcustom artist-interface-with-rect t
243 "Whether to interface with the rect package or not.
245 Interfacing to the rect package means that the Copy and Paste operations
246 will use the rectangle buffer when accessing the copied area. This means
247 that you can insert a rectangle which is copied using the artist package
248 and vice versa.
250 If this causes any problem for you (for example, if the implementation of
251 the rectangle package changes), you can set this variable to nil, and the
252 artist package will use its own copy buffer."
253 :group 'artist
254 :type 'boolean)
256 (defvar artist-arrows [ ?> nil ?v ?L ?< nil ?^ nil ]
257 ;; This is a defvar, not a defcustom, since the custom
258 ;; package shows vectors of characters as a vector of integers,
259 ;; which is confusing
260 "A vector of characters to use as arrows.
262 The vector is 8 elements long and contains a character for each
263 direction, or nil if there is no suitable character to use for arrow
264 in that direction.
266 The directions are as follows:
268 5 6 7
269 \\ | /
270 4 - * - 0
271 / | \\
272 3 2 1")
274 (defcustom artist-aspect-ratio 1
275 "Defines the character height-to-width aspect ratio.
276 This is used when drawing squares and circles. If the height of the"
277 :group 'artist
278 :type 'number)
280 (defcustom artist-trim-line-endings t
281 "Whether or not to remove white-space at end of lines.
283 If non-nil, line-endings are trimmed (that is, extraneous white-space
284 at the end of the line is removed) when the shape is drawn."
285 :group 'artist
286 :type 'boolean)
289 (defcustom artist-flood-fill-right-border 'window-width
290 "Right edge definition, used when flood-filling.
292 When flood-filling, if the area is not closed off to the right, then
293 flood-filling will fill no more to the right than specified by this
294 variable. This limit is called the fill-border."
295 :group 'artist
296 :type '(choice (const :tag "limited to window" window-width)
297 (const :tag "limited to value of `fill-column'" fill-column)))
299 (defcustom artist-flood-fill-show-incrementally t
300 "Whether or not to incrementally update display when flood-filling.
302 If non-nil, incrementally update display when flood-filling.
303 If set to non-nil, this currently implies discarding any input events
304 during the flood-fill."
305 :group 'artist
306 :type 'boolean)
309 (defcustom artist-ellipse-right-char ?\)
310 "Character to use at the rightmost position when drawing narrow ellipses.
312 In this figure, it is the right parenthesis (the ``)'' character):
313 -----
315 -----"
316 :group 'artist
317 :type 'character)
320 (defcustom artist-ellipse-left-char ?\(
321 "Character to use at the leftmost position when drawing narrow ellipses.
323 In this figure, it is the left parenthesis (the ``('' character):
324 -----
326 -----"
327 :group 'artist
328 :type 'character)
330 (defcustom artist-picture-compatibility t
331 "Whether or not picture mode compatibility is on."
332 :group 'artist
333 :type 'boolean)
338 (defcustom artist-vaporize-fuzziness 1
339 "How to vaporize lines that are cut off.
341 Accept this many characters cutting off a line and still treat
342 it as one line.
343 Example:
344 If `artist-vaporize-fuzziness' is 2, then those will be recognized as
345 lines from A to B (provided you start vaporizing them at the ``*''):
347 A----*------/-----------B
349 A----*----/\\------------B
350 / \\
352 but this one won't, since it is cut off by more than 2 characters:
353 \\/ /
354 A----*----/\\/----------B
355 / /\\
356 (in fact, only the left part (between the A and the leftmost ``/''
357 crossing the line) will be vaporized)"
358 :group 'artist
359 :type 'integer)
362 (defvar artist-pointer-shape (if (eq window-system 'x) x-pointer-crosshair nil)
363 "*If in X Windows, use this pointer shape while drawing with the mouse.")
366 (defcustom artist-text-renderer 'artist-figlet
367 "Function for doing text rendering."
368 :group 'artist-text
369 :type 'symbol)
372 (defcustom artist-figlet-program "figlet"
373 "Program to run for `figlet'."
374 :group 'artist-text
375 :type 'string)
378 (defcustom artist-figlet-default-font "standard"
379 "Default font for `figlet'."
380 :group 'artist-text
381 :type 'string)
384 (defcustom artist-figlet-list-fonts-command
385 ;; list files ending with *.flf in any directory printed by the
386 ;; ``figlet -I2'' command. I think this will not produce more than
387 ;; one directory, but it never hurts to be on the safe side...
388 "for dir in `figlet -I2`; do cd $dir; ls *.flf; done"
389 "Command to run to get list of available fonts."
390 :group 'artist-text
391 :type 'string)
394 (defcustom artist-spray-interval 0.2
395 "Number of seconds between repeated spraying."
396 :group 'artist
397 :type 'number)
400 (defcustom artist-spray-radius 4
401 "Size of the area for spraying."
402 :group 'artist
403 :type 'integer)
406 (defvar artist-spray-chars '(?\ ?. ?- ?+ ?m ?% ?* ?#)
407 ;; This is a defvar, not a defcustom, since the custom
408 ;; package shows lists of characters as a lists of integers,
409 ;; which is confusing
410 "*Characters (``color'') to use when spraying.
411 They should be ordered
412 from the ``lightest'' to the ``heaviest'' since spraying replaces a
413 light character with the next heavier one.")
416 (defvar artist-spray-new-char ?.
417 "*Initial character to use when spraying.
418 This character is used if spraying upon a character that is
419 not in `artist-spray-chars'. The character defined by this variable
420 should be in `artist-spray-chars', or spraying will behave
421 strangely.")
424 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
425 ;; End of user options
428 ;; Internal variables
430 (defvar artist-mode nil
431 "Non-nil to enable `artist-mode' and nil to disable.")
432 (make-variable-buffer-local 'artist-mode)
434 (defvar artist-mode-name " Artist"
435 "Name of artist mode beginning with a space (appears in the mode-line).")
437 (defvar artist-curr-go 'pen-char
438 "Current selected graphics operation.")
439 (make-variable-buffer-local 'artist-curr-go)
441 (defvar artist-line-char-set nil
442 "Boolean to tell whether user has set some char to use when drawing lines.")
443 (make-variable-buffer-local 'artist-line-char-set)
445 (defvar artist-line-char nil
446 "Char to use when drawing lines.")
447 (make-variable-buffer-local 'artist-line-char)
449 (defvar artist-fill-char-set nil
450 "Boolean to tell whether user has set some char to use when filling.")
451 (make-variable-buffer-local 'artist-fill-char-set)
453 (defvar artist-fill-char nil
454 "Char to use when filling.")
455 (make-variable-buffer-local 'artist-fill-char)
457 (defvar artist-erase-char ?\s
458 "Char to use when erasing.")
459 (make-variable-buffer-local 'artist-erase-char)
461 (defvar artist-default-fill-char ?.
462 "Char to use when a fill-char is required but none is set.")
463 (make-variable-buffer-local 'artist-default-fill-char)
465 ; This variable is not buffer local
466 (defvar artist-copy-buffer nil
467 "Copy buffer.")
469 (defvar artist-draw-region-min-y 0
470 "Line-number for top-most visited line for draw operation.")
471 (make-variable-buffer-local 'artist-draw-region-min-y)
473 (defvar artist-draw-region-max-y 0
474 "Line-number for bottom-most visited line for draw operation.")
475 (make-variable-buffer-local 'artist-draw-region-max-y)
477 (defvar artist-borderless-shapes nil
478 "When non-nil, draw shapes without border.
479 The fill char is used instead, if it is set.")
480 (make-variable-buffer-local 'artist-borderless-shapes)
482 (defvar artist-prev-next-op-alist nil
483 "Assoc list for looking up next and/or previous draw operation.
484 The structure is as follows: (OP . (PREV-OP . NEXT-OP))
485 where the elements are as follows:
486 * OP is an atom: the KEY-SYMBOL in the `artist-mt' structure
487 * PREV-OP and NEXT-OP are strings: the KEYWORD in the `artist-mt' structure
489 This variable is initialized by the artist-make-prev-next-op-alist function.")
491 (eval-when-compile
492 ;; Make rect available at compile-time
493 (require 'rect) ; for interfacing with rect
494 (require 'reporter) ; the bug-reporting tool
495 (require 'picture)) ; picture mode compatibility
497 (if artist-interface-with-rect
498 (require 'rect))
500 (require 'reporter)
502 (if artist-picture-compatibility
503 (require 'picture))
506 (defvar artist-mode-map
507 (let ((map (make-sparse-keymap)))
508 (setq artist-mode-map (make-sparse-keymap))
509 (define-key map [down-mouse-1] 'artist-down-mouse-1)
510 (define-key map [S-down-mouse-1] 'artist-down-mouse-1)
511 (define-key map [down-mouse-2] 'artist-mouse-choose-operation)
512 (define-key map [S-down-mouse-2] 'artist-mouse-choose-operation)
513 (define-key map [down-mouse-3] 'artist-down-mouse-3)
514 (define-key map [S-down-mouse-3] 'artist-down-mouse-3)
515 (define-key map [C-mouse-4] 'artist-select-prev-op-in-list)
516 (define-key map [C-mouse-5] 'artist-select-next-op-in-list)
517 (define-key map "\r" 'artist-key-set-point) ; return
518 (define-key map [up] 'artist-previous-line)
519 (define-key map "\C-p" 'artist-previous-line)
520 (define-key map [down] 'artist-next-line)
521 (define-key map "\C-n" 'artist-next-line)
522 (define-key map [left] 'artist-backward-char)
523 (define-key map "\C-b" 'artist-backward-char)
524 (define-key map [right] 'artist-forward-char)
525 (define-key map "\C-f" 'artist-forward-char)
526 (define-key map "<" 'artist-toggle-first-arrow)
527 (define-key map ">" 'artist-toggle-second-arrow)
528 (define-key map "\C-c\C-a\C-e" 'artist-select-erase-char)
529 (define-key map "\C-c\C-a\C-f" 'artist-select-fill-char)
530 (define-key map "\C-c\C-a\C-l" 'artist-select-line-char)
531 (define-key map "\C-c\C-a\C-o" 'artist-select-operation)
532 (define-key map "\C-c\C-a\C-r" 'artist-toggle-rubber-banding)
533 (define-key map "\C-c\C-a\C-t" 'artist-toggle-trim-line-endings)
534 (define-key map "\C-c\C-a\C-s" 'artist-toggle-borderless-shapes)
535 (define-key map "\C-c\C-c" 'artist-mode-off)
536 (define-key map "\C-c\C-al" 'artist-select-op-line)
537 (define-key map "\C-c\C-aL" 'artist-select-op-straight-line)
538 (define-key map "\C-c\C-ar" 'artist-select-op-rectangle)
539 (define-key map "\C-c\C-aR" 'artist-select-op-square)
540 (define-key map "\C-c\C-as" 'artist-select-op-square)
541 (define-key map "\C-c\C-ap" 'artist-select-op-poly-line)
542 (define-key map "\C-c\C-aP" 'artist-select-op-straight-poly-line)
543 (define-key map "\C-c\C-ae" 'artist-select-op-ellipse)
544 (define-key map "\C-c\C-ac" 'artist-select-op-circle)
545 (define-key map "\C-c\C-at" 'artist-select-op-text-see-thru)
546 (define-key map "\C-c\C-aT" 'artist-select-op-text-overwrite)
547 (define-key map "\C-c\C-aS" 'artist-select-op-spray-can)
548 (define-key map "\C-c\C-az" 'artist-select-op-spray-set-size)
549 (define-key map "\C-c\C-a\C-d" 'artist-select-op-erase-char)
550 (define-key map "\C-c\C-aE" 'artist-select-op-erase-rectangle)
551 (define-key map "\C-c\C-av" 'artist-select-op-vaporize-line)
552 (define-key map "\C-c\C-aV" 'artist-select-op-vaporize-lines)
553 (define-key map "\C-c\C-a\C-k" 'artist-select-op-cut-rectangle)
554 (define-key map "\C-c\C-a\M-w" 'artist-select-op-copy-rectangle)
555 (define-key map "\C-c\C-a\C-y" 'artist-select-op-paste)
556 (define-key map "\C-c\C-af" 'artist-select-op-flood-fill)
557 (define-key map "\C-c\C-a\C-b" 'artist-submit-bug-report)
558 map)
559 "Keymap for `artist-minor-mode'.")
561 (defvar artist-replacement-table (make-vector 256 0)
562 "Replacement table for `artist-replace-char'.")
566 ;;; The table of graphic operations
568 (defvar artist-mt
569 ;; Implementation note: Maybe this should be done using a structure
570 ;; in the cl package?
573 (menu
574 ("Drawing"
575 ((function-call
576 ( "Undo" do-undo undo))
578 (separator )
579 (graphics-operation
580 ("Pen" (("Pen" pen-char "pen-c"
581 artist-no-arrows nil
582 nil nil nil
583 artist-do-continously
584 artist-pen
585 (nil))
586 ("Pen Line" pen-line "pen-l"
587 artist-arrows artist-pen-set-arrow-points
588 artist-pen-reset-last-xy nil nil
589 artist-do-continously
590 artist-pen-line
591 (nil)))))
593 (graphics-operation
594 ("Line" (("line" line "line"
595 artist-arrows artist-set-arrow-points-for-2points
596 nil nil nil
598 artist-draw-line
599 (artist-undraw-line
600 artist-nil nil))
601 ("straight line" s-line "sline"
602 artist-arrows artist-set-arrow-points-for-2points
603 nil nil nil
605 artist-draw-sline
606 (artist-undraw-sline
607 artist-nil nil)))))
609 (graphics-operation
610 ("Rectangle" (("rectangle" rect "rect"
611 artist-no-arrows nil
612 nil nil nil
614 artist-draw-rect
615 (artist-undraw-rect
616 artist-t-if-fill-char-set artist-fill-rect))
617 ("square" square "square"
618 artist-no-arrows nil
619 nil nil nil
621 artist-draw-square
622 (artist-undraw-square
623 artist-t-if-fill-char-set artist-fill-square)))))
625 (graphics-operation
626 ("Poly-line" (("poly-line" polyline "poly"
627 artist-arrows artist-set-arrow-points-for-poly
628 nil nil nil
629 artist-do-poly
630 artist-draw-line
631 (artist-undraw-line
632 artist-nil nil))
633 ("straight poly-line" spolyline "s-poly"
634 artist-arrows artist-set-arrow-points-for-poly
635 nil nil nil
636 artist-do-poly
637 artist-draw-sline
638 (artist-undraw-sline
639 artist-nil nil)))))
641 (graphics-operation
642 ("Ellipse" (("ellipse" ellipse "ellipse"
643 artist-no-arrows nil
644 nil nil nil
646 artist-draw-ellipse
647 (artist-undraw-ellipse
648 artist-t-if-fill-char-set artist-fill-ellipse))
649 ("circle" circle "circle"
650 artist-no-arrows nil
651 nil nil nil
653 artist-draw-circle
654 (artist-undraw-circle
655 artist-t-if-fill-char-set artist-fill-circle)))))
657 (graphics-operation
658 ("Text" (("text see-thru" text-thru "text-thru"
659 artist-no-arrows nil
660 nil nil nil
662 artist-text-see-thru
663 nil)
664 ("text overwrite" text-ovwrt "text-ovwrt"
665 artist-no-arrows nil
666 nil nil nil
668 artist-text-overwrite
669 nil))))
671 (graphics-operation
672 ("Spray-can" (("spray-can" spray-can "spray-can"
673 artist-no-arrows nil
674 nil nil nil
675 artist-do-continously
676 artist-spray
677 (artist-spray-get-interval))
678 ("spray set size" spray-get-size "spray-size"
679 artist-no-arrows nil
680 nil artist-spray-clear-circle artist-spray-set-radius
682 artist-draw-circle
683 (artist-undraw-circle
684 artist-nil nil)))))
686 (graphics-operation
687 ("Erase" (("erase char" erase-char "erase-c"
688 artist-no-arrows nil
689 nil nil nil
690 artist-do-continously
691 artist-erase-char
692 (nil))
693 ("erase rectangle" erase-rect "erase-r"
694 artist-no-arrows nil
695 nil nil nil
697 artist-draw-rect
698 (artist-undraw-rect
699 artist-t artist-erase-rect)))))
701 (graphics-operation
702 ("Vaporize" (("vaporize line" vaporize-line "vaporize-1"
703 artist-no-arrows nil
704 nil nil nil
706 artist-vaporize-line
707 nil)
708 ("vaporize lines" vaporize-lines "vaporize-n"
709 artist-no-arrows nil
710 nil nil nil
712 artist-vaporize-lines
713 nil)))))))
715 (menu
716 ("Edit"
717 ((graphics-operation
718 ("Cut" (("cut rectangle" cut-r "cut-r"
719 artist-no-arrows nil
720 nil nil nil
722 artist-draw-rect
723 (artist-undraw-rect
724 artist-t artist-cut-rect)
725 ("cut square" cut-s "cut-s"
726 artist-no-arrows nil
727 nil nil nil
729 artist-draw-square
730 (artist-undraw-square
731 artist-t artist-cut-square))))))
733 (graphics-operation
734 ("Copy" (("copy rectangle" copy-r "copy-r"
735 artist-no-arrows nil
736 nil nil nil
738 artist-draw-rect
739 (artist-undraw-rect
740 artist-t artist-copy-rect)
741 ("copy square" copy-s "copy-s"
742 artist-no-arrows nil
743 nil nil nil
745 artist-draw-square
746 (artist-undraw-square
747 artist-t artist-copy-square))))))
749 (graphics-operation
750 ("Paste" (("paste" paste "paste"
751 artist-no-arrows nil
752 nil nil nil
754 artist-paste
755 nil)
756 ("paste" paste "paste"
757 artist-no-arrows nil
758 nil nil nil
760 artist-paste
761 nil))))
763 (graphics-operation
764 ("Flood-fill" (("flood-fill" flood-fill "flood"
765 artist-no-arrows nil
766 nil nil nil
768 artist-flood-fill
769 nil)
770 ("flood-fill" flood-fill "flood"
771 artist-no-arrows nil
772 nil nil nil
774 artist-flood-fill
775 nil)))))))
777 (menu
778 ("Settings"
779 ((function-call
780 ("Set Fill" set-fill artist-select-fill-char))
782 (function-call
783 ("Set Line" set-line artist-select-line-char))
785 (function-call
786 ("Set Erase" set-erase artist-select-erase-char))
788 (function-call
789 ("Rubber-banding" rubber-band artist-toggle-rubber-banding))
791 (function-call
792 ("Trimming" trimming artist-toggle-trim-line-endings))
794 (function-call
795 ("Borders" borders artist-toggle-borderless-shapes))
797 (function-call
798 ("Spray-chars" spray-chars artist-select-spray-chars)))))
800 ) ;; end of list
802 "Master Table for `artist-mode'.
803 This table is primarily a table over the different graphics operations
804 available in artist mode, but it also holds layout information for the
805 popup menu.
807 The master table is a list of table elements. The elements of this table
808 have the layout
810 (TAG INFO-PART)
812 There are three kinds of TAG:
814 `menu' -- a sub-menu
815 `separator' -- produce a separator in the popup menu
816 `function-call' -- call a function
817 `graphics-operation' -- a graphics operation
819 The layout of the INFO-PART for `menu' is
821 (TITLE ((TAG-1 INFO-PART-1) (TAG-2 INFO-PART-2) ...))
823 TITLE is the title of the submenu; this is followed by a list of
824 menu items, each on the general form (TAG INFO-PART).
827 The layout of the INFO-PART for `separator' is empty and not used.
830 This is the layout of the INFO-PART for `function-call':
832 (KEYWORD SYMBOL FN)
834 KEYWORD is a string naming the operation, and appears in the popup menu.
835 SYMBOL is the symbol for the operations.
836 FN is the function performing the operation. This function
837 is called with no arguments. Its return value is ignored.
840 The layout of the INFO-PART for `graphics-operation' is
842 (TITLE (UNSHIFTED SHIFTED))
844 TITLE is the title that appears in the popup menu. UNSHIFTED
845 and SHIFTED specify for unshifted and shifted operation. Both
846 have the form
848 (KEYWORD KEY-SYMBOL MODE-LINE ARROW-PRED ARROW-SET-FN
849 INIT-FN PREP-FILL-FN EXIT-FN DRAW-HOW DRAW-FN EXTRA-DRAW-INFO)
851 KEYWORD is a string specifying the name of the shape to draw.
852 This is used when selecting drawing operation.
853 KEY-SYMBOL is the key which is used when looking up members
854 through the functions `artist-go-get-MEMBER-from-symbol'
855 and `artist-fc-get-MEMBER-from-symbol'.
856 MODE-LINE is a string that appears in the mode-line when drawing
857 the shape.
858 ARROW-PRED is a function that is called to find out if the shape
859 can have arrows. The function is called with no arguments and
860 must return nil or t.
861 ARROW-SET-FN is a function that is called to set arrow end-points.
862 Arguments and return values for this funcion are described below.
863 INIT-FN is, if non-nil, a function that is called when the first
864 point of the shape is set. Arguments and return values for
865 this funcion are described below.
866 PREP-FILL-FN is, if non-nil, a function that is called after
867 the last point is set, but before the filling is done.
868 Arguments and return values for this funcion are described below.
869 EXIT-FN is, if non-nil, a function that is called after filling
870 is done. Arguments and return values for this funcion are
871 described below.
872 DRAW-HOW defines the kind of shape. The kinds of shapes are:
873 `artist-do-continously' -- Do drawing operation continously,
874 as long as the mouse button is held down.
875 `artist-do-poly' -- Do drawing operation many times.
876 1 -- Do drawing operation only once.
877 2 -- The drawing operation requires two points.
878 DRAW-FN is the function to call for drawing. Arguments and
879 return values for this funcion are described below.
880 EXTRA-DRAW-INFO the layout of this depends on the value of DRAW-HOW:
881 If DRAW-HOW is `artist-do-continously':
883 (INTERVAL-FN)
885 INTERVAL-FN is, if non-nil, a function to call for getting
886 an interval between repeated calls to the DRAW-FN.
887 This function is called with no arguments and must
888 return a number, the interval in seconds.
889 If nil, calls to DRAW-FN are done only when the mouse
890 or cursor is moved.
892 If DRAW-HOW is either `artist-do-poly' or 2:
894 (UNDRAW-FN FILL-PRED FILL-FN)
896 UNDRAW-FN is a function to call for undrawing the shape.
897 Arguments and return values for this funcion are
898 described below.
899 FILL-PRED is a function that is called to find out if the shape
900 can have arrows. The function must take no arguments and
901 return nil or t.
902 FILL-FN is a function to call for filling the shape.
903 Arguments and return values for this funcion are
904 described below.
906 If DRAW-HOW is 1:
910 Note! All symbols and keywords (both in the `funcion-call' INFO-PART
911 as well as in the `graphics-operation' INFO-PART) must be unique.
913 The following table describe function arguments and return value
914 for different functions and DRAW-HOWs.
916 If DRAW-HOW is either `artist-do-continously' or 1:
918 INIT-FN X Y ==> ignored
919 PREP-FILL-FN X Y ==> ignored
920 EXIT-FN X Y ==> ignored
921 ARROW-SET-FN X Y ==> ignored
922 DRAW-FN X Y ==> ignored
924 If DRAW-HOW is 2:
926 INIT-FN X1 Y1 ==> ignored
927 PREP-FILL-FN X1 Y1 X2 Y2 ==> ignored
928 EXIT-FN X1 Y1 X2 Y2 ==> ignored
929 ARROW-SET-FN X1 Y1 X2 Y2 ==> ignored
930 DRAW-FN X1 Y1 X2 Y2 ==> (ENDPOINT-1 ENDPOINT-2 SHAPE)
931 UNDRAW-FN (ENDPOINT-1 ENDPOINT-2 SHAPE) ==> ignored
932 FILL-FN (ENDPOINT-1 ENDPOINT-2 SHAPE) X1 Y1 X2 Y2 ==> ignored
934 ENDPOINT-1 and ENDPOINT-2 are endpoints which are created with
935 `artist-make-endpoint'
936 SHAPE is an opaque structure, created by the DRAW-FN and intented
937 to be used only by the UNDRAW-FN.
939 If DRAW-HOW is `artist-do-poly':
941 INIT-FN X1 Y1
942 PREP-FILL-FN POINT-LIST
943 ARROW-SET-FN POINT-LIST
944 EXIT-FN POINT-LIST
945 DRAW-FN X-LAST Y-LAST X-NEW Y-NEW ==> (ENDPOINT-1 ENDPOINT-2 SHAPE)
946 UNDRAW-FN (ENDPOINT-1 ENDPOINT-2 SHAPE)
947 FILL-FN POINT-LIST
949 ENDPOINT-1 and ENDPOINT-2 are endpoints which are created with
950 `artist-make-endpoint'.
951 SHAPE is an opaque structure, created by the DRAW-FN and intented
952 to be used only by the UNDRAW-FN.
953 POINT-LIST is a list of vectors [X Y].")
957 ;; Accessors for the master table
960 (defun artist-mt-get-tag (element)
961 "Retrieve the tag component from the master table ELEMENT."
962 (elt element 0))
964 (defun artist-mt-get-info-part (element)
965 "Retrieve the info part component from the master table ELEMENT."
966 (elt element 1))
968 ;; For the 'graphics-operation info-parts
970 (defsubst artist-go-get-desc (info-part)
971 "Retrieve the description component from a graphics operation INFO-PART."
972 (elt info-part 0))
974 (defsubst artist-go-get-unshifted (info-part)
975 "Retrieve the unshifted info from a graphics operation INFO-PART."
976 (elt (elt info-part 1) 0))
978 (defsubst artist-go-get-shifted (info-part)
979 "Retrieve the shifted info from a graphics operation INFO-PART."
980 (elt (elt info-part 1) 1))
982 (defsubst artist-go-get-keyword (info-variant-part)
983 "Retrieve the keyword component from an INFO-VARIANT-PART.
984 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
985 (elt info-variant-part 0))
987 (defsubst artist-go-get-symbol (info-variant-part)
988 "Retrieve the symbol component from an INFO-VARIANT-PART.
989 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
990 (elt info-variant-part 1))
992 (defsubst artist-go-get-mode-line (info-variant-part)
993 "Retrieve the mode line component from an INFO-VARIANT-PART.
994 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
995 (elt info-variant-part 2))
997 (defsubst artist-go-get-arrow-pred (info-variant-part)
998 "Retrieve the arrow predicate component from an INFO-VARIANT-PART.
999 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
1000 (elt info-variant-part 3))
1002 (defsubst artist-go-get-arrow-set-fn (info-variant-part)
1003 "Retrieve the arrow set component from an INFO-VARIANT-PART.
1004 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
1005 (elt info-variant-part 4))
1007 (defsubst artist-go-get-init-fn (info-variant-part)
1008 "Retrieve the init function component from an INFO-VARIANT-PART.
1009 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
1010 (elt info-variant-part 5))
1012 (defsubst artist-go-get-prep-fill-fn (info-variant-part)
1013 "Retrieve the fill preparation function component from an INFO-VARIANT-PART.
1014 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
1015 (elt info-variant-part 6))
1017 (defsubst artist-go-get-exit-fn (info-variant-part)
1018 "Retrieve the exit component from an INFO-VARIANT-PART.
1019 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
1020 (elt info-variant-part 7))
1022 (defsubst artist-go-get-draw-how (info-variant-part)
1023 "Retrieve the draw how component from an INFO-VARIANT-PART.
1024 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
1025 (elt info-variant-part 8))
1027 (defsubst artist-go-get-draw-fn (info-variant-part)
1028 "Retrieve the draw function component from an INFO-VARIANT-PART.
1029 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
1030 (elt info-variant-part 9))
1032 (defsubst artist-go-get-undraw-fn (info-variant-part)
1033 "Retrieve the undraw function component from an INFO-VARIANT-PART.
1034 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
1035 This interval function component is available only if the `draw-how'
1036 component is other than `artist-do-continously' or 1."
1037 (elt (elt info-variant-part 10) 0))
1039 (defsubst artist-go-get-interval-fn (info-variant-part)
1040 "Retrieve the interval function component from an INFO-VARIANT-PART.
1041 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
1042 This interval function component is available only if the `draw-how'
1043 component is `artist-do-continously'."
1044 (elt (elt info-variant-part 10) 0))
1046 (defsubst artist-go-get-fill-pred (info-variant-part)
1047 "Retrieve the fill predicate component from an INFO-VARIANT-PART.
1048 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
1049 This interval function component is available only if the `draw-how'
1050 component is other than `artist-do-continously' or 1."
1051 (elt (elt info-variant-part 10) 1))
1053 (defsubst artist-go-get-fill-fn (info-variant-part)
1054 "Retrieve the fill function component from an INFO-VARIANT-PART.
1055 An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
1056 This interval function component is available only if the `draw-how'
1057 component is other than `artist-do-continously' or 1."
1058 (elt (elt info-variant-part 10) 2))
1060 ;; For the 'function-call info-parts
1062 (defsubst artist-fc-get-keyword (info-part)
1063 "Retrieve the keyword component from a graphics operation INFO-PART."
1064 (elt info-part 0))
1066 (defsubst artist-fc-get-symbol (info-part)
1067 "Retrieve the symbol component from a graphics operation INFO-PART."
1068 (elt info-part 1))
1070 (defsubst artist-fc-get-fn (info-part)
1071 "Retrieve the function component from a graphics operation INFO-PART."
1072 (elt info-part 2))
1074 ;; For the 'menu info-parts
1076 (defsubst artist-mn-get-title (info-part)
1077 "Retrieve the title component from a graphics operation INFO-PART."
1078 (elt info-part 0))
1080 (defsubst artist-mn-get-items (info-part)
1081 "Retrieve the items component from a graphics operation INFO-PART."
1082 (elt info-part 1))
1084 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1085 ;; mouse wheel cyclic operation selection
1087 (defun artist-get-last-non-nil-op (op-list &optional last-non-nil)
1088 "Find the last non-nil draw operation in OP-LIST.
1089 Optional LAST-NON-NIL will be returned if OP-LIST is nil."
1090 (if op-list
1091 (artist-get-last-non-nil-op (cdr op-list)
1092 (or (car (car op-list)) last-non-nil))
1093 last-non-nil))
1095 (defun artist-get-first-non-nil-op (op-list)
1096 "Find the first non-nil draw operation in OP-LIST."
1097 (or (car (car op-list)) (artist-get-first-non-nil-op (cdr op-list))))
1099 (defun artist-is-in-op-list-p (op op-list)
1100 "Check whether OP is in OP-LIST."
1101 (and op-list
1102 (or (and (car (car op-list)) (string= op (car (car op-list))))
1103 (artist-is-in-op-list-p op (cdr op-list)))))
1105 (defun artist-make-prev-next-op-alist (op-list
1106 &optional
1107 last-non-nil-arg first-non-nil-arg
1108 prev-entry prev-op-arg)
1109 "Build an assoc-list of OP-LIST.
1110 The arguments LAST-NON-NIL-ARG, FIRST-NON-NIL-ARG, PREV-ENTRY and
1111 PREV-OP-ARG are used when invoked recursively during the build-up."
1112 (let* ((last-non-nil (or last-non-nil-arg
1113 (artist-get-last-non-nil-op
1114 artist-key-compl-table)))
1115 (first-non-nil (or first-non-nil-arg
1116 (artist-get-first-non-nil-op
1117 artist-key-compl-table)))
1118 (prev-op (or prev-op-arg last-non-nil))
1119 (op (car (car op-list)))
1120 (opsym (artist-mt-get-symbol-from-keyword op))
1121 (entry (cons opsym (cons prev-op nil))))
1122 (if (or (and op-list (not op))
1123 (artist-is-in-op-list-p op (cdr op-list)))
1124 (artist-make-prev-next-op-alist (cdr op-list)
1125 last-non-nil first-non-nil
1126 prev-entry prev-op)
1127 (if prev-entry (setcdr (cdr prev-entry) op))
1128 (if op-list
1129 (cons entry (artist-make-prev-next-op-alist
1130 (cdr op-list)
1131 last-non-nil first-non-nil
1132 entry op))
1133 (progn (setcdr (cdr prev-entry) first-non-nil) nil)))))
1135 (defun artist-select-next-op-in-list ()
1136 "Cyclically select next drawing mode operation."
1137 (interactive)
1138 (let ((next-op (cdr (cdr (assoc artist-curr-go artist-prev-next-op-alist)))))
1139 (artist-select-operation next-op)
1140 (message next-op)))
1142 (defun artist-select-prev-op-in-list ()
1143 "Cyclically select previous drawing mode operation."
1144 (interactive)
1145 (let ((prev-op (car (cdr (assoc artist-curr-go artist-prev-next-op-alist)))))
1146 (artist-select-operation prev-op)
1147 (message prev-op)))
1149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1151 ;;; ---------------------------------
1152 ;;; The artist-mode
1153 ;;; ---------------------------------
1155 ;;;###autoload
1156 (defun artist-mode (&optional state)
1157 "Toggle artist mode. With arg, turn artist mode on if arg is positive.
1158 Artist lets you draw lines, squares, rectangles and poly-lines, ellipses
1159 and circles with your mouse and/or keyboard.
1161 How to quit artist mode
1163 Type \\[artist-mode-off] to quit artist-mode.
1166 How to submit a bug report
1168 Type \\[artist-submit-bug-report] to submit a bug report.
1171 Drawing with the mouse:
1173 mouse-2
1174 shift mouse-2 Pops up a menu where you can select what to draw with
1175 mouse-1, and where you can do some settings (described
1176 below).
1178 mouse-1
1179 shift mouse-1 Draws lines, rectangles or poly-lines, erases, cuts, copies
1180 or pastes:
1182 Operation Not shifted Shifted
1183 --------------------------------------------------------------
1184 Pen fill-char at point line from last point
1185 to new point
1186 --------------------------------------------------------------
1187 Line Line in any direction Straight line
1188 --------------------------------------------------------------
1189 Rectangle Rectangle Square
1190 --------------------------------------------------------------
1191 Poly-line Poly-line in any dir Straight poly-lines
1192 --------------------------------------------------------------
1193 Ellipses Ellipses Circles
1194 --------------------------------------------------------------
1195 Text Text (see thru) Text (overwrite)
1196 --------------------------------------------------------------
1197 Spray-can Spray-can Set size for spray
1198 --------------------------------------------------------------
1199 Erase Erase character Erase rectangle
1200 --------------------------------------------------------------
1201 Vaporize Erase single line Erase connected
1202 lines
1203 --------------------------------------------------------------
1204 Cut Cut rectangle Cut square
1205 --------------------------------------------------------------
1206 Copy Copy rectangle Copy square
1207 --------------------------------------------------------------
1208 Paste Paste Paste
1209 --------------------------------------------------------------
1210 Flood-fill Flood-fill Flood-fill
1211 --------------------------------------------------------------
1213 * Straight lines can only go horizontally, vertically
1214 or diagonally.
1216 * Poly-lines are drawn while holding mouse-1 down. When you
1217 release the button, the point is set. If you want a segment
1218 to be straight, hold down shift before pressing the
1219 mouse-1 button. Click mouse-2 or mouse-3 to stop drawing
1220 poly-lines.
1222 * See thru for text means that text already in the buffer
1223 will be visible through blanks in the text rendered, while
1224 overwrite means the opposite.
1226 * Vaporizing connected lines only vaporizes lines whose
1227 _endpoints_ are connected. See also the variable
1228 `artist-vaporize-fuzziness'.
1230 * Cut copies, then clears the rectangle/square.
1232 * When drawing lines or poly-lines, you can set arrows.
1233 See below under ``Arrows'' for more info.
1235 * The mode line shows the currently selected drawing operation.
1236 In addition, if it has an asterisk (*) at the end, you
1237 are currently drawing something.
1239 * Be patient when flood-filling -- large areas take quite
1240 some time to fill.
1243 mouse-3 Erases character under pointer
1244 shift mouse-3 Erases rectangle
1247 Settings
1249 Set fill Sets the character used when filling rectangles/squares
1251 Set line Sets the character used when drawing lines
1253 Erase char Sets the character used when erasing
1255 Rubber-banding Toggles rubber-banding
1257 Trimming Toggles trimming of line-endings (that is: when the shape
1258 is drawn, extraneous white-space at end of lines is removed)
1260 Borders Toggles the drawing of line borders around filled shapes.
1263 Drawing with keys
1265 \\[artist-key-set-point] Does one of the following:
1266 For lines/rectangles/squares: sets the first/second endpoint
1267 For poly-lines: sets a point (use C-u \\[artist-key-set-point] to set last point)
1268 When erase characters: toggles erasing
1269 When cutting/copying: Sets first/last endpoint of rect/square
1270 When pasting: Pastes
1272 \\[artist-select-operation] Selects what to draw
1274 Move around with \\[artist-next-line], \\[artist-previous-line], \\[artist-forward-char] and \\[artist-backward-char].
1276 \\[artist-select-fill-char] Sets the charater to use when filling
1277 \\[artist-select-line-char] Sets the charater to use when drawing
1278 \\[artist-select-erase-char] Sets the charater to use when erasing
1279 \\[artist-toggle-rubber-banding] Toggles rubber-banding
1280 \\[artist-toggle-trim-line-endings] Toggles trimming of line-endings
1281 \\[artist-toggle-borderless-shapes] Toggles borders on drawn shapes
1284 Arrows
1286 \\[artist-toggle-first-arrow] Sets/unsets an arrow at the beginning
1287 of the line/poly-line
1289 \\[artist-toggle-second-arrow] Sets/unsets an arrow at the end
1290 of the line/poly-line
1293 Selecting operation
1295 There are some keys for quickly selecting drawing operations:
1297 \\[artist-select-op-line] Selects drawing lines
1298 \\[artist-select-op-straight-line] Selects drawing straight lines
1299 \\[artist-select-op-rectangle] Selects drawing rectangles
1300 \\[artist-select-op-square] Selects drawing squares
1301 \\[artist-select-op-poly-line] Selects drawing poly-lines
1302 \\[artist-select-op-straight-poly-line] Selects drawing straight poly-lines
1303 \\[artist-select-op-ellipse] Selects drawing ellipses
1304 \\[artist-select-op-circle] Selects drawing circles
1305 \\[artist-select-op-text-see-thru] Selects rendering text (see thru)
1306 \\[artist-select-op-text-overwrite] Selects rendering text (overwrite)
1307 \\[artist-select-op-spray-can] Spray with spray-can
1308 \\[artist-select-op-spray-set-size] Set size for the spray-can
1309 \\[artist-select-op-erase-char] Selects erasing characters
1310 \\[artist-select-op-erase-rectangle] Selects erasing rectangles
1311 \\[artist-select-op-vaporize-line] Selects vaporizing single lines
1312 \\[artist-select-op-vaporize-lines] Selects vaporizing connected lines
1313 \\[artist-select-op-cut-rectangle] Selects cutting rectangles
1314 \\[artist-select-op-copy-rectangle] Selects copying rectangles
1315 \\[artist-select-op-paste] Selects pasting
1316 \\[artist-select-op-flood-fill] Selects flood-filling
1319 Variables
1321 This is a brief overview of the different varaibles. For more info,
1322 see the documentation for the variables (type \\[describe-variable] <variable> RET).
1324 artist-rubber-banding Interactively do rubber-banding or not
1325 artist-first-char What to set at first/second point...
1326 artist-second-char ...when not rubber-banding
1327 artist-interface-with-rect If cut/copy/paste should interface with rect
1328 artist-arrows The arrows to use when drawing arrows
1329 artist-aspect-ratio Character height-to-width for squares
1330 artist-trim-line-endings Trimming of line endings
1331 artist-flood-fill-right-border Right border when flood-filling
1332 artist-flood-fill-show-incrementally Update display while filling
1333 artist-pointer-shape Pointer shape to use while drawing
1334 artist-ellipse-left-char Character to use for narrow ellipses
1335 artist-ellipse-right-char Character to use for narrow ellipses
1336 artist-borderless-shapes If shapes should have borders
1337 artist-picture-compatibility Whether or not to be picture mode compatible
1338 artist-vaporize-fuzziness Tolerance when recognizing lines
1339 artist-spray-interval Seconds between repeated sprayings
1340 artist-spray-radius Size of the spray-area
1341 artist-spray-chars The spray-``color''
1342 artist-spray-new-chars Initial spray-``color''
1344 Hooks
1346 When entering artist-mode, the hook `artist-mode-init-hook' is called.
1347 When quitting artist-mode, the hook `artist-mode-exit-hook' is called.
1350 Keymap summary
1352 \\{artist-mode-map}"
1353 (interactive)
1354 (if (setq artist-mode
1355 (if (null state) (not artist-mode)
1356 (> (prefix-numeric-value state) 0)))
1357 (artist-mode-init)
1358 (artist-mode-exit)))
1360 ;; insert our minor mode string
1361 (or (assq 'artist-mode minor-mode-alist)
1362 (setq minor-mode-alist
1363 (cons '(artist-mode artist-mode-name)
1364 minor-mode-alist)))
1366 ;; insert our minor mode keymap
1367 (or (assq 'artist-mode minor-mode-map-alist)
1368 (setq minor-mode-map-alist
1369 (cons (cons 'artist-mode artist-mode-map)
1370 minor-mode-map-alist)))
1374 (eval-when-compile
1375 ;; Variables that are made local in artist-mode-init
1376 (defvar artist-key-is-drawing nil)
1377 (defvar artist-key-endpoint1 nil)
1378 (defvar artist-key-poly-point-list nil)
1379 (defvar artist-key-shape nil)
1380 (defvar artist-key-draw-how nil)
1381 (defvar artist-popup-menu-table nil)
1382 (defvar artist-key-compl-table nil)
1383 (defvar artist-rb-save-data nil)
1384 (defvar artist-arrow-point-1 nil)
1385 (defvar artist-arrow-point-2 nil))
1388 ;; Init and exit
1389 (defun artist-mode-init ()
1390 "Init Artist mode. This will call the hook `artist-mode-init-hook'."
1391 (let ((i 0))
1392 (while (< i 256)
1393 (aset artist-replacement-table i i)
1394 (setq i (1+ i))))
1395 (aset artist-replacement-table ?\n ?\ )
1396 (aset artist-replacement-table ?\t ?\ )
1397 (aset artist-replacement-table 0 ?\ )
1398 (make-local-variable 'artist-key-is-drawing)
1399 (make-local-variable 'artist-key-endpoint1)
1400 (make-local-variable 'artist-key-poly-point-list)
1401 (make-local-variable 'artist-key-shape)
1402 (make-local-variable 'artist-key-draw-how)
1403 (make-local-variable 'artist-popup-menu-table)
1404 (make-local-variable 'artist-key-compl-table)
1405 (make-local-variable 'artist-prev-next-op-alist)
1406 (make-local-variable 'artist-rb-save-data)
1407 (make-local-variable 'artist-arrow-point-1)
1408 (make-local-variable 'artist-arrow-point-2)
1409 (setq artist-key-is-drawing nil)
1410 (setq artist-key-endpoint1 nil)
1411 (setq artist-key-poly-point-list nil)
1412 (setq artist-key-shape nil)
1413 (setq artist-popup-menu-table (artist-compute-popup-menu-table artist-mt))
1414 (setq artist-key-compl-table (artist-compute-key-compl-table artist-mt))
1415 (setq artist-prev-next-op-alist
1416 (artist-make-prev-next-op-alist artist-key-compl-table))
1417 (setq artist-rb-save-data (make-vector 7 0))
1418 (setq artist-arrow-point-1 nil)
1419 (setq artist-arrow-point-2 nil)
1420 (make-local-variable 'next-line-add-newlines)
1421 (setq next-line-add-newlines t)
1422 (setq artist-key-draw-how
1423 (artist-go-get-draw-how-from-symbol artist-curr-go))
1424 (if (and artist-picture-compatibility (not (eq major-mode 'picture-mode)))
1425 (progn
1426 (picture-mode)
1427 (message "")))
1428 (run-hooks 'artist-mode-init-hook)
1429 (artist-mode-line-show-curr-operation artist-key-is-drawing))
1431 (defun artist-mode-exit ()
1432 "Exit Artist mode. This will call the hook `artist-mode-exit-hook'."
1433 (if (and artist-picture-compatibility (eq major-mode 'picture-mode))
1434 (picture-mode-exit))
1435 (kill-local-variable 'next-line-add-newlines)
1436 (run-hooks 'artist-mode-exit-hook))
1438 (defun artist-mode-off ()
1439 "Turn Artist mode off."
1440 (interactive)
1441 (artist-mode -1)
1442 (force-mode-line-update))
1445 ;; General routines
1448 (defun artist-update-display ()
1449 "Repaint the display."
1450 (sit-for 0))
1452 (defun artist-mode-line-show-curr-operation (is-drawing)
1453 "Show current operation in mode-line. If IS-DRAWING, show that."
1454 (let ((mtext (concat artist-mode-name "/"
1455 (artist-go-get-mode-line-from-symbol artist-curr-go)
1456 (if is-drawing "/*" ""))))
1457 (setcdr (assq 'artist-mode minor-mode-alist) (list mtext)))
1458 (force-mode-line-update))
1461 (defun artist-t-if-fill-char-set ()
1462 "Return the value of the variable `artist-fill-char-set'."
1463 artist-fill-char-set)
1465 (defun artist-t ()
1466 "Always return t."
1469 (defun artist-nil ()
1470 "Always return nil."
1471 nil)
1473 (defun artist-arrows ()
1474 "Say yes to arrows!"
1477 (defun artist-no-arrows ()
1478 "Say no to arrows!"
1479 nil)
1482 ;; Auxiliary init-routines
1486 ; Computing the table for the x-popup-menu from the master table
1489 (defun artist-compute-popup-menu-table (menu-table)
1490 "Create a menu from from MENU-TABLE data.
1491 The returned value is suitable for the `x-popup-menu' function."
1492 (cons "Artist menu"
1493 (artist-compute-popup-menu-table-sub menu-table)))
1495 (defun artist-compute-popup-menu-table-sub (menu-table)
1496 "Compute operation table suitable for `x-popup-menu' from MENU-TABLE."
1497 (mapcar
1498 (lambda (element)
1499 (let ((element-tag (artist-mt-get-tag element)))
1500 (cond ((eq element-tag 'graphics-operation)
1501 (let* ((info-part (artist-mt-get-info-part element))
1502 (descr (artist-go-get-desc info-part))
1503 (unshifted (artist-go-get-unshifted info-part))
1504 (symbol (artist-go-get-symbol unshifted)))
1505 (list descr symbol)))
1507 ((eq element-tag 'function-call)
1508 (let* ((info-part (artist-mt-get-info-part element))
1509 (keyword (artist-fc-get-keyword info-part))
1510 (symbol (artist-fc-get-symbol info-part)))
1511 (list keyword symbol)))
1513 ((eq element-tag 'separator)
1514 '("" ""))
1516 ((eq element-tag 'menu)
1517 (let* ((info-part (artist-mt-get-info-part element))
1518 (title (artist-mn-get-title info-part))
1519 (items (artist-mn-get-items info-part)))
1520 (cons title (artist-compute-popup-menu-table-sub items))))
1523 (error "Internal error: unknown element-tag: \"%s\""
1524 element-tag)))))
1525 menu-table))
1528 ; Computing the completion table from the master table
1531 (defun artist-compute-key-compl-table (menu-table)
1532 "Compute completion table from MENU-TABLE, suitable for `completing-read'."
1533 (apply
1534 'nconc
1535 (remq nil
1536 (mapcar
1537 (lambda (element)
1538 (let ((element-tag (artist-mt-get-tag element)))
1539 (cond ((eq element-tag 'graphics-operation)
1540 (let* ((info-part (artist-mt-get-info-part element))
1541 (unshifted (artist-go-get-unshifted info-part))
1542 (shifted (artist-go-get-shifted info-part))
1543 (unshifted-kwd (artist-go-get-keyword unshifted))
1544 (shifted-kwd (artist-go-get-keyword shifted)))
1545 (list (list unshifted-kwd) (list shifted-kwd))))
1546 ((eq element-tag 'menu)
1547 (let* ((info-part (artist-mt-get-info-part element))
1548 (items (artist-mn-get-items info-part)))
1549 (artist-compute-key-compl-table items)))
1551 nil))))
1552 menu-table))))
1556 ; Retrieving a symbol (graphics operation or function-call) from a keyword
1559 (defun artist-mt-get-symbol-from-keyword (kwd)
1560 "Search master table for keyword KWD and return its symbol."
1561 (artist-mt-get-symbol-from-keyword-sub artist-mt kwd))
1563 (defun artist-mt-get-symbol-from-keyword-sub (table kwd)
1564 "Search TABLE for keyword KWD and return its symbol."
1565 (catch 'found
1566 (mapcar
1567 (lambda (element)
1568 (let ((element-tag (artist-mt-get-tag element)))
1569 (cond ((eq element-tag 'graphics-operation)
1570 (let* ((info-part (artist-mt-get-info-part element))
1571 (unshifted (artist-go-get-unshifted info-part))
1572 (shifted (artist-go-get-shifted info-part))
1573 (unshifted-kwd (artist-go-get-keyword unshifted))
1574 (shifted-kwd (artist-go-get-keyword shifted))
1575 (unshifted-sym (artist-go-get-symbol unshifted))
1576 (shifted-sym (artist-go-get-symbol shifted)))
1577 (if (string-equal kwd unshifted-kwd)
1578 (throw 'found unshifted-sym))
1579 (if (string-equal kwd shifted-kwd)
1580 (throw 'found shifted-sym))))
1582 ((eq element-tag 'function-call)
1583 (let* ((info-part (artist-mt-get-info-part element))
1584 (keyword (artist-fc-get-keyword info-part))
1585 (symbol (artist-fc-get-symbol info-part)))
1586 (if (string-equal kwd keyword)
1587 (throw 'found symbol))))
1588 ((eq element-tag 'menu)
1589 (let* ((info-part (artist-mt-get-info-part element))
1590 (items (artist-mn-get-items info-part))
1591 (answer (artist-mt-get-symbol-from-keyword-sub
1592 items kwd)))
1593 (if answer (throw 'found answer))))
1595 nil))))
1596 table)
1597 nil))
1601 ; Retrieving info from a graphics operation symbol
1604 (defun artist-go-retrieve-from-symbol (symbol retrieve-fn)
1605 "Search the master table for a graphics operation SYMBOL.
1606 Calls RETRIEVE-FN to retrieve information from that symbol's
1607 info-variant-part."
1608 (artist-go-retrieve-from-symbol-sub artist-mt symbol retrieve-fn))
1610 (defun artist-go-retrieve-from-symbol-sub (table symbol retrieve-fn)
1611 "Search the TABLE for a graphics operation SYMBOL.
1612 Calls RETRIEVE-FN to retrieve information from that symbol's
1613 info-variant-part."
1614 (catch 'found
1615 (mapcar
1616 (lambda (element)
1617 (let ((element-tag (artist-mt-get-tag element)))
1618 (cond ((eq element-tag 'graphics-operation)
1619 (let* ((info-part (artist-mt-get-info-part element))
1620 (unshifted (artist-go-get-unshifted info-part))
1621 (shifted (artist-go-get-shifted info-part))
1622 (unshifted-sym (artist-go-get-symbol unshifted))
1623 (shifted-sym (artist-go-get-symbol shifted))
1624 (variant-part (cond
1625 ((eq unshifted-sym symbol) unshifted)
1626 ((eq shifted-sym symbol) shifted)
1627 (t nil))))
1628 (if variant-part ; if found do:
1629 (throw 'found (funcall retrieve-fn variant-part)))))
1631 ((eq element-tag 'menu)
1632 (let* ((info-part (artist-mt-get-info-part element))
1633 (items (artist-mn-get-items info-part))
1634 (answer (artist-go-retrieve-from-symbol-sub
1635 items symbol retrieve-fn)))
1636 (if answer (throw 'found answer)))))))
1638 table)
1639 nil))
1641 (defun artist-go-get-keyword-from-symbol (symbol)
1642 "Search the master table, get keyword from a graphics operation SYMBOL."
1643 (artist-go-retrieve-from-symbol symbol 'artist-go-get-keyword))
1645 (defun artist-go-get-mode-line-from-symbol (symbol)
1646 "Search the master table, get mode-line from a graphics operation SYMBOL."
1647 (artist-go-retrieve-from-symbol symbol 'artist-go-get-mode-line))
1649 (defun artist-go-get-arrow-pred-from-symbol (symbol)
1650 "Search the master table, get arrow-pred from a graphics operation SYMBOL."
1651 (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-pred))
1653 (defun artist-go-get-arrow-set-fn-from-symbol (symbol)
1654 "Search the master table, get arrow-set-fn from a graphics operation SYMBOL."
1655 (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-set-fn))
1657 (defun artist-go-get-init-fn-from-symbol (symbol)
1658 "Search the master table, get init-fn from a graphics operation SYMBOL."
1659 (artist-go-retrieve-from-symbol symbol 'artist-go-get-init-fn))
1661 (defun artist-go-get-prep-fill-fn-from-symbol (symbol)
1662 "Search the master table, get prep-fill-fn from a graphics operation SYMBOL."
1663 (artist-go-retrieve-from-symbol symbol 'artist-go-get-prep-fill-fn))
1665 (defun artist-go-get-exit-fn-from-symbol (symbol)
1666 "Search the master table, get exit-fn from a graphics operation SYMBOL."
1667 (artist-go-retrieve-from-symbol symbol 'artist-go-get-exit-fn))
1669 (defun artist-go-get-draw-fn-from-symbol (symbol)
1670 "Search the master table, get draw-fn from a graphics operation SYMBOL."
1671 (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-fn))
1673 (defun artist-go-get-draw-how-from-symbol (symbol)
1674 "Search the master table, get draw-how from a graphics operation SYMBOL."
1675 (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-how))
1677 (defun artist-go-get-undraw-fn-from-symbol (symbol)
1678 "Search the master table, get undraw-fn from a graphics operation SYMBOL."
1679 (artist-go-retrieve-from-symbol symbol 'artist-go-get-undraw-fn))
1681 (defun artist-go-get-interval-fn-from-symbol (symbol)
1682 "Search the master table, get interval-fn from a graphics operation SYMBOL."
1683 (artist-go-retrieve-from-symbol symbol 'artist-go-get-interval-fn))
1685 (defun artist-go-get-fill-pred-from-symbol (symbol)
1686 "Search the master table, get fill-pred from a graphics operation SYMBOL."
1687 (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-pred))
1689 (defun artist-go-get-fill-fn-from-symbol (symbol)
1690 "Search the master table, get fill-fn from a graphics operation SYMBOL."
1691 (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-fn))
1693 (defun artist-go-get-symbol-shift (symbol is-shifted)
1694 "Search for (shifted or unshifted) graphics operation SYMBOL.
1695 If IS-SHIFTED is non-nil, return the shifted symbol,
1696 otherwise the shifted symbol."
1697 (artist-go-get-symbol-shift-sub artist-mt symbol is-shifted))
1699 (defun artist-go-get-symbol-shift-sub (table symbol is-shifted)
1700 "Search TABLE for (shifted or unshifted) graphics SYMBOL.
1701 If IS-SHIFTED is non-nil, return the shifted symbol,
1702 otherwise the shifted symbol."
1703 (catch 'found
1704 (mapcar
1705 (lambda (element)
1706 (let ((element-tag (artist-mt-get-tag element)))
1707 (cond ((eq element-tag 'graphics-operation)
1708 (let* ((info-part (artist-mt-get-info-part element))
1709 (unshift-variant (artist-go-get-unshifted info-part))
1710 (shift-variant (artist-go-get-shifted info-part))
1711 (unshift-sym (artist-go-get-symbol unshift-variant))
1712 (shift-sym (artist-go-get-symbol shift-variant)))
1713 (if (or (eq symbol unshift-sym) (eq symbol shift-sym))
1714 (throw 'found (if is-shifted shift-sym unshift-sym)))))
1716 ((eq element-tag 'menu)
1717 (let* ((info-part (artist-mt-get-info-part element))
1718 (items (artist-mn-get-items info-part))
1719 (answer (artist-go-get-symbol-shift-sub
1720 items symbol is-shifted)))
1721 (if answer (throw 'found answer)))))))
1723 table)
1724 nil))
1727 ; Retrieving info from a function-call symbol
1730 (defun artist-fc-retrieve-from-symbol (symbol retrieve-fn)
1731 "Search the master table for a function call SYMBOL.
1732 Calls RETRIEVE-FN to retrieve information from that symbol's
1733 info-variant-part."
1734 (artist-fc-retrieve-from-symbol-sub artist-mt symbol retrieve-fn))
1736 (defun artist-fc-retrieve-from-symbol-sub (table symbol retrieve-fn)
1737 "Search TABLE for a function-call SYMBOL.
1738 Calls RETRIEVE-FN to retrieve information from that symbol's
1739 info-variant-part."
1740 (catch 'found
1741 (mapcar
1742 (lambda (element)
1743 (let ((element-tag (artist-mt-get-tag element)))
1744 (cond ((eq element-tag 'function-call)
1745 (let* ((info-part (artist-mt-get-info-part element))
1746 (fc-symbol (artist-fc-get-symbol info-part)))
1747 (if (eq fc-symbol symbol)
1748 (throw 'found (funcall retrieve-fn info-part)))))
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-fc-retrieve-from-symbol-sub
1754 items symbol retrieve-fn)))
1755 (if answer (throw 'found answer)))))))
1757 table)
1758 nil))
1760 (defun artist-fc-get-fn-from-symbol (symbol)
1761 "Search the master table to get function from a function call SYMBOL."
1762 (artist-fc-retrieve-from-symbol symbol 'artist-fc-get-fn))
1766 ;; Utilities
1769 ;; Macro that won't funcall the function if it is nil.
1771 (defmacro artist-funcall (fn &rest args)
1772 "Call function FN with ARGS iff FN is not nil."
1773 (list 'if fn (cons 'funcall (cons fn args))))
1775 (defun artist-uniq (l)
1776 "Remove consecutive duplicates in list L. Comparison is done with `equal'."
1777 (cond ((null l) nil)
1778 ((null (cdr l)) l) ; only one element in list
1779 ((equal (car l) (car (cdr l))) (artist-uniq (cdr l))) ; first 2 equal
1780 (t (cons (car l) (artist-uniq (cdr l)))))) ; first 2 are different
1782 (defun artist-string-split (str r)
1783 "Split string STR at occurrences of regexp R, returning a list of strings."
1784 (let ((res nil)
1785 (start 0)
1786 (match-pos 0))
1787 (while (setq match-pos (string-match r str start))
1788 (setq res (cons (copy-sequence (substring str start match-pos)) res))
1789 (setq start (match-end 0)))
1790 (if (null res)
1791 (list str)
1792 (if (< (match-end 0) (- (length str) 1))
1793 (setq res (cons (substring str (match-end 0) (length str)) res)))
1794 (reverse res))))
1796 (defun artist-string-to-file (str file-name)
1797 "Write string STR to file FILE-NAME."
1798 (write-region str 'end-is-ignored file-name nil 'no-message))
1800 (defun artist-file-to-string (file-name)
1801 "Read from file FILE-NAME into a string."
1802 (save-excursion
1803 (let ((tmp-buffer (get-buffer-create (concat "*artist-" file-name "*"))))
1804 (set-buffer tmp-buffer)
1805 (goto-char (point-min))
1806 (insert-file-contents file-name nil nil nil t)
1807 (let ((str (copy-sequence (buffer-substring (point-min)
1808 (point-max)))))
1809 (kill-buffer tmp-buffer)
1810 str))))
1812 (defun artist-clear-buffer (buf)
1813 "Clear contents of buffer BUF."
1814 (save-excursion
1815 (set-buffer buf)
1816 (goto-char (point-min))
1817 (delete-char (- (point-max) (point-min)) nil)))
1820 (defun artist-system (program stdin &optional program-args)
1821 "Run PROGRAM synchronously with the contents of string STDIN to stdin.
1822 Optional args PROGRAM-ARGS are arguments to PROGRAM.
1823 Return a list (RETURN-CODE STDOUT STDERR)."
1824 (save-excursion
1825 (let* ((tmp-stdin-file-name (if stdin
1826 (make-temp-file
1827 (concat (file-name-as-directory
1828 (or (getenv "TMPDIR") "/tmp"))
1829 "artist-stdin."))
1830 nil))
1831 (tmp-stdout-buffer (get-buffer-create
1832 (concat "*artist-" program "*")))
1833 (tmp-stderr-file-name (make-temp-file
1834 (concat (file-name-as-directory
1835 (or (getenv "TMPDIR") "/tmp"))
1836 "artist-stdout.")))
1837 (binary-process-input nil) ; for msdos
1838 (binary-process-output nil))
1840 ;; Prepare stdin
1841 (if stdin (artist-string-to-file stdin tmp-stdin-file-name))
1843 ;; Clear the buffer
1844 (artist-clear-buffer tmp-stdout-buffer)
1846 ;; Start the program
1847 (unwind-protect
1848 (let ((res (if program-args
1849 (apply 'call-process
1850 program
1851 tmp-stdin-file-name
1852 (list tmp-stdout-buffer
1853 tmp-stderr-file-name)
1855 (if (stringp program-args)
1856 (list program-args)
1857 program-args))
1858 (apply 'call-process
1859 program
1860 tmp-stdin-file-name
1861 (list tmp-stdout-buffer
1862 tmp-stderr-file-name)
1863 nil))))
1865 ;; the return value
1866 (list res
1867 (save-excursion
1868 (set-buffer tmp-stdout-buffer)
1869 (copy-sequence (buffer-substring (point-min)
1870 (point-max))))
1871 (artist-file-to-string tmp-stderr-file-name)))
1873 ;; Unwind: remove temporary files and buffers
1874 (if (and stdin (file-exists-p tmp-stdin-file-name))
1875 (delete-file tmp-stdin-file-name))
1876 (if (file-exists-p tmp-stderr-file-name)
1877 (delete-file tmp-stderr-file-name))
1878 (if (memq tmp-stdout-buffer (buffer-list))
1879 (kill-buffer tmp-stdout-buffer))))))
1881 ;; Routines that deal with the buffer
1883 ;; artist-current-line get line number (top of buffer is 0)
1885 ;; artist-move-to-xy move to (x,y) (0,0) is beg-of-buffer
1887 ;; artist-get-char-at-xy get char in at (x,y)
1889 ;; artist-replace-char overwrite (replace) char at point
1890 ;; artist-replace-chars overwrite (replace) chars at point
1893 (defsubst artist-current-column ()
1894 "Return point's current column."
1895 (current-column))
1897 (defsubst artist-current-line ()
1898 "Return point's current line, buffer-relative. Top of buffer is 0."
1899 (+ (count-lines 1 (point))
1900 (if (= (current-column) 0) 1 0)
1901 -1))
1903 (defsubst artist-move-to-xy (x y)
1904 "Move to column X, at row Y from the top of buffer. Top line is 0."
1906 ;; Q: Why do we do forward-line twice?
1907 ;; A: The documentation for forward-line says
1909 ;; "... Returns the count of lines left to move. ... With
1910 ;; positive N, a non-empty line at the end counts as one
1911 ;; line successfully moved (for the return value)."
1913 ;; This means that if we are trying to move forward past the end
1914 ;; of the buffer, and that last line happened to be longer than
1915 ;; the current column, then we end up at the end of that last
1916 ;; line, and forward-line returns one less than we actually
1917 ;; wanted to move.
1919 ;; Example: In the figure below, the `X' is the very last
1920 ;; character in the buffer ("a non-empty line at the
1921 ;; end"). Suppose point is at at P. Then (forward-line 1)
1922 ;; returns 0 and puts point after the `X'.
1924 ;; --------top of buffer--------
1926 ;; P X
1927 ;; -------bottom of buffer------
1929 ;; But, if we are at the end of buffer when trying to move
1930 ;; forward, then forward-line will return the (for us) correct
1931 ;; value, which is good, because we will come to the end of the
1932 ;; buffer by the first forward-line. The second forward-line
1933 ;; will then get us where we really wanted to go.
1935 ;; If we are not moving past the end of the buffer, then the
1936 ;; second forward-line will return 0.
1938 ;; Q: What happens if we are moving upwards?
1939 ;; A: That will work good. insert-char won't insert a negative
1940 ;; number of chars, and forward-line will fail silently if we are
1941 ;; moving past the beginning of the buffer.
1943 (forward-line (- y (artist-current-line)))
1944 (insert-char ?\n (forward-line (- y (artist-current-line))))
1945 (move-to-column (max x 0) t)
1946 (let ((curr-y (artist-current-line)))
1947 (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y))
1948 (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y))))
1950 (defsubst artist-get-char-at-xy (x y)
1951 "Return the character found at column X, row Y.
1952 Also updates the variables `artist-draw-min-y' and `artist-draw-max-y'."
1953 (artist-move-to-xy x y)
1954 (let ((curr-y (artist-current-line)))
1955 (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y))
1956 (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y)))
1957 (following-char))
1960 (defun artist-get-char-at-xy-conv (x y)
1961 "Retrieve the character at X, Y, converting tabs and new-lines to spaces."
1962 (save-excursion
1963 (aref artist-replacement-table (artist-get-char-at-xy x y))))
1966 (defun artist-replace-char (new-char)
1967 "Replace the character at point with NEW-CHAR."
1968 ;; Check that the variable exists first. The doc says it was added in 19.23.
1969 (if (and (and (boundp 'emacs-major-version) (= emacs-major-version 20))
1970 (and (boundp 'emacs-minor-version) (<= emacs-minor-version 3)))
1971 ;; This is a bug workaround for Emacs 20, versions up to 20.3:
1972 ;; The self-insert-command doesn't care about the overwrite-mode,
1973 ;; so the insertion is done in the same way as in picture mode.
1974 ;; This seems to be a little bit slower.
1975 (progn
1976 (artist-move-to-xy (1+ (artist-current-column))
1977 (artist-current-line))
1978 (delete-char -1)
1979 (insert (aref artist-replacement-table new-char)))
1980 ;; In emacs-19, the self-insert-command works better and faster
1981 (let ((overwrite-mode 'overwrite-mode-textual)
1982 (fill-column 32765) ; Large :-)
1983 (blink-matching-paren nil))
1984 (setq last-command-event (aref artist-replacement-table new-char))
1985 (self-insert-command 1))))
1987 (defun artist-replace-chars (new-char count)
1988 "Replace characters at point with NEW-CHAR. COUNT chars are replaced."
1989 ;; Check that the variable exists first. The doc says it was added in 19.23.
1990 (if (and (and (boundp 'emacs-major-version) (= emacs-major-version 20))
1991 (and (boundp 'emacs-minor-version) (<= emacs-minor-version 3)))
1992 ;; This is a bug workaround for Emacs 20, versions up to 20.3:
1993 ;; The self-insert-command doesn't care about the overwrite-mode,
1994 ;; so the insertion is done in the same way as in picture mode.
1995 ;; This seems to be a little bit slower.
1996 (let* ((replaced-c (aref artist-replacement-table new-char))
1997 (replaced-s (make-string count replaced-c)))
1998 (artist-move-to-xy (+ (artist-current-column) count)
1999 (artist-current-line))
2000 (delete-char (- count))
2001 (insert replaced-s))
2002 ;; In emacs-19, the self-insert-command works better
2003 (let ((overwrite-mode 'overwrite-mode-textual)
2004 (fill-column 32765) ; Large :-)
2005 (blink-matching-paren nil))
2006 (setq last-command-event (aref artist-replacement-table new-char))
2007 (self-insert-command count))))
2009 (defsubst artist-replace-string (string &optional see-thru)
2010 "Replace contents at point with STRING.
2011 With optional argument SEE-THRU, set to non-nil, text in the buffer
2012 ``shines thru'' blanks in the STRING."
2013 (let ((char-list (append string nil)) ; convert the string to a list
2014 (overwrite-mode 'overwrite-mode-textual)
2015 (fill-column 32765) ; Large :-)
2016 (blink-matching-paren nil))
2017 (while char-list
2018 (let ((c (car char-list)))
2019 (if (and see-thru (= (aref artist-replacement-table c) ?\ ))
2020 (artist-move-to-xy (1+ (artist-current-column))
2021 (artist-current-line))
2022 (artist-replace-char c)))
2023 (setq char-list (cdr char-list)))))
2026 ;; Routines for setting and unsetting points
2027 ;; Used when not rubber-banding
2029 (defun artist-no-rb-unset-point1 ()
2030 "Unsets point 1 when not rubber-banding."
2031 (let ((x-now (artist-current-column))
2032 (y-now (artist-current-line))
2033 (x (aref artist-rb-save-data 0))
2034 (y (aref artist-rb-save-data 1)))
2035 (artist-move-to-xy x y)
2036 (artist-replace-char (aref artist-rb-save-data 2))
2037 (artist-move-to-xy x-now y-now)))
2039 (defun artist-no-rb-set-point1 (x y)
2040 "Set point 1 at X, Y when not rubber-banding."
2041 (let ((x-now (artist-current-column))
2042 (y-now (artist-current-line)))
2043 (aset artist-rb-save-data 0 x)
2044 (aset artist-rb-save-data 1 y)
2045 (aset artist-rb-save-data 2 (artist-get-char-at-xy x y))
2046 (artist-move-to-xy x y)
2047 (artist-replace-char artist-first-char)
2048 (artist-move-to-xy x-now y-now)
2049 (aset artist-rb-save-data 6 0)))
2051 (defun artist-no-rb-unset-point2 ()
2052 "This function unsets point 2 when not rubber-banding."
2053 (if (= (aref artist-rb-save-data 6) 1)
2054 (let ((x-now (artist-current-column))
2055 (y-now (artist-current-line))
2056 (x (aref artist-rb-save-data 3))
2057 (y (aref artist-rb-save-data 4)))
2058 (artist-move-to-xy x y)
2059 (artist-replace-char (aref artist-rb-save-data 5))
2060 (artist-move-to-xy x-now y-now))))
2062 (defun artist-no-rb-set-point2 (x y)
2063 "Set point 2 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 3 x)
2067 (aset artist-rb-save-data 4 y)
2068 (aset artist-rb-save-data 5 (artist-get-char-at-xy x y))
2069 (artist-move-to-xy x y)
2070 (artist-replace-char artist-second-char)
2071 (artist-move-to-xy x-now y-now)
2072 (aset artist-rb-save-data 6 1)))
2074 (defun artist-no-rb-unset-points ()
2075 "This function unsets point 1 and 2 when not rubber-banding."
2076 (artist-no-rb-unset-point1)
2077 (artist-no-rb-unset-point2))
2080 ;; artist-intersection-char
2082 ;; Note: If changing this, see the notes for artist-unintersection-char
2083 ;; and artist-vaporize-lines
2085 (defun artist-intersection-char (new-c old-c)
2086 "Calculates intersection character when drawing a NEW-C on top of an OLD-C.
2087 Return character according to this scheme:
2089 OLD-C NEW-C return
2090 - | +
2091 | - +
2092 + | +
2093 + - +
2094 \\ / X
2095 / \\ X
2096 X / X
2097 X \\ X
2098 other combinations NEW-C"
2100 (cond ((and (= old-c ?- ) (= new-c ?| )) ?+ )
2101 ((and (= old-c ?| ) (= new-c ?- )) ?+ )
2102 ((and (= old-c ?+ ) (= new-c ?- )) ?+ )
2103 ((and (= old-c ?+ ) (= new-c ?| )) ?+ )
2104 ((and (= old-c ?\\ ) (= new-c ?/ )) ?X )
2105 ((and (= old-c ?/ ) (= new-c ?\\ )) ?X )
2106 ((and (= old-c ?X ) (= new-c ?/ )) ?X )
2107 ((and (= old-c ?X ) (= new-c ?\\ )) ?X )
2108 (t new-c)))
2110 ;; artist-unintersection-char
2112 ;; Note: If changing this, see the note for artist-vaporize-lines
2114 (defun artist-unintersection-char (line-c buffer-c)
2115 "Restore character to before intersection when removing LINE-C from BUFFER-C.
2116 Return character according to this scheme:
2118 LINE-C BUFFER-C return
2119 - + |
2120 | + -
2121 \\ X /
2122 / X \\
2123 other combinations `artist-erase-char'."
2125 (cond ((and (= line-c ?- ) (= buffer-c ?+ )) ?| )
2126 ((and (= line-c ?| ) (= buffer-c ?+ )) ?- )
2127 ((and (= line-c ?\\ ) (= buffer-c ?X )) ?/ )
2128 ((and (= line-c ?/ ) (= buffer-c ?X )) ?\\ )
2129 ((= line-c buffer-c) artist-erase-char)
2130 (t buffer-c)))
2133 ;; Computing the line-char to use
2134 ;; for use with borderless shapes
2136 (defsubst artist-compute-line-char ()
2137 "Compute which character to use for lines, if any.
2138 Return value is either nil for the default characters that make up lines, or
2139 a character chosen depending on the variables `artist-borderless-shapes',
2140 `artist-fill-char-set', `artist-fill-char' and
2141 `artist-line-char-set' and `artist-line-char'."
2142 (if (and artist-borderless-shapes artist-fill-char-set)
2143 artist-fill-char
2144 (if artist-line-char-set
2145 artist-line-char
2146 nil)))
2149 ;; Things for drawing horizontal, vertical and diagonal (straight) lines.
2151 ;; A line here is a vector:
2152 ;; [ start-x start-y length direction saved-char-1 saved-char-2 ... ]
2153 ;; directions start with 0 at the x-axis and counts anti clockwise.
2155 (defvar artist-direction-info
2156 ;; x y char
2157 [ [ 1 0 ?- ] ; direction 0
2158 [ 1 1 ?\\ ] ; direction 1
2159 [ 0 1 ?| ] ; direction 2
2160 [ -1 1 ?/ ] ; direction 3
2161 [ -1 0 ?- ] ; direction 4
2162 [ -1 -1 ?\\ ] ; direction 5
2163 [ 0 -1 ?| ] ; direction 6
2164 [ 1 -1 ?/ ] ] ; direction 7
2165 "Table used for stepping x and y coordinates in a specific direction.
2166 This table is also used for determining which char to use for that direction.")
2168 (defsubst artist-direction-step-x (direction)
2169 "Return the x-step for DIRECTION from the `artist-direction-info' table."
2170 (aref (aref artist-direction-info direction) 0))
2172 (defsubst artist-direction-step-y (direction)
2173 "Return the y-step for DIRECTION from the `artist-direction-info' table."
2174 (aref (aref artist-direction-info direction) 1))
2176 (defun artist-direction-char (direction)
2177 "Return the character for DIRECTION from the `artist-direction-info' table."
2178 (aref (aref artist-direction-info direction) 2))
2180 ;; artist-find-direction
2184 (defun artist-find-direction (x1 y1 x2 y2)
2185 "Find the direction from point X1,Y1 to X2,Y2.
2186 Returns a DIRECTION, a number 0--7, coded as follows:
2188 5 6 7
2189 \\ | /
2190 4 - * - 0
2191 / | \\
2192 3 2 1"
2193 (let ((delta-x (- x2 x1))
2194 (delta-y (- y2 y1)))
2195 (cond ((>= delta-x (* 2 (abs delta-y))) 0)
2196 ((>= delta-y (* 2 (abs delta-x))) 2)
2197 ((>= (- delta-x) (* 2 (abs delta-y))) 4)
2198 ((>= (- delta-y) (* 2 (abs delta-x))) 6)
2199 ((and (>= delta-x 0) (>= delta-y 0)) 1)
2200 ((and (<= delta-x 0) (>= delta-y 0)) 3)
2201 ((and (<= delta-x 0) (<= delta-y 0)) 5)
2202 ((and (>= delta-x 0) (<= delta-y 0)) 7))))
2204 (defun artist-straight-calculate-length (direction x1 y1 x2 y2)
2205 "Calculate length for a straight line in DIRECTION from X1,Y1 to X2,Y2."
2206 (cond ((or (= direction 7)
2207 (= direction 0)
2208 (= direction 1)) (1+ (- x2 x1)))
2209 ((or (= direction 3)
2210 (= direction 4)
2211 (= direction 5)) (1+ (- x1 x2)))
2212 (t (1+ (abs (- y2 y1))))))
2214 (defun artist-sline (x1 y1 x2 y2)
2215 "Create a straight line from X1,Y1 to X2,Y2."
2216 (let* ((direction (artist-find-direction x1 y1 x2 y2))
2217 (length (artist-straight-calculate-length direction x1 y1 x2 y2))
2218 (line (make-vector (+ length 4) x1)))
2219 ;; not needed:
2220 ;; (aset line 0 x1)
2221 ;; because we set all elements to x1
2222 (aset line 1 y1)
2223 (aset line 2 length)
2224 (aset line 3 direction)
2225 line))
2227 (defun artist-save-chars-under-sline (line)
2228 "Save characters under a LINE."
2229 (let ((x (aref line 0))
2230 (y (aref line 1))
2231 (length (+ (aref line 2) 4))
2232 (direction (aref line 3))
2233 (i 4))
2234 (while (< i length)
2235 (aset line i (artist-get-char-at-xy x y))
2236 (setq x (+ x (artist-direction-step-x direction)))
2237 (setq y (+ y (artist-direction-step-y direction)))
2238 (setq i (1+ i))))
2239 line)
2243 ;; Things for drawing lines in all directions.
2244 ;; The line drawing engine is the eight-point alrogithm.
2246 ;; A line is here a list of (x y saved-char new-char)s.
2248 (defvar artist-octant-info
2249 ;; Initial Step in Step in
2250 ;; coeffs x and y x and y
2251 ;; for if q >= 0 if g < 0
2252 ;; dfdx,dfdy
2253 [ [ 2 1 1 0 1 1 ] ; 1st octant
2254 [ 1 2 1 1 0 1 ] ; 2nd octant
2255 [ -1 2 0 1 -1 1 ] ; 3rd octant
2256 [ -2 1 -1 1 -1 0 ] ; 4th octant
2257 [ -2 -1 -1 0 -1 -1 ] ; 5th octant
2258 [ -1 -2 -1 -1 0 -1 ] ; 6th octant
2259 [ 1 -2 0 -1 1 -1 ] ; 7th octant
2260 [ 2 -1 1 -1 1 0 ] ] ; 8th octant
2261 "Table used by line drawing algorithm (eight point).")
2263 ;; Primitives for the artist-octant-info.
2264 ;; Decrease octant by 1 since elt counts from 0 and octant counts from 1.
2266 (defsubst artist-get-dfdx-init-coeff (octant)
2267 "Retrieve dfdx component for OCTANT."
2268 (aref (aref artist-octant-info (- octant 1)) 0))
2270 (defsubst artist-get-dfdy-init-coeff (octant)
2271 "Retrieve dfdy component for OCTANT."
2272 (aref (aref artist-octant-info (- octant 1)) 1))
2274 (defsubst artist-get-x-step-q>=0 (octant)
2275 "Retrieve x-step component for OCTANT when q >= 0."
2276 (aref (aref artist-octant-info (- octant 1)) 2))
2278 (defsubst artist-get-y-step-q>=0 (octant)
2279 "Retrieve y-step component for OCTANT when q >= 0."
2280 (aref (aref artist-octant-info (- octant 1)) 3))
2282 (defsubst artist-get-x-step-q<0 (octant)
2283 "Retrieve x-step component for OCTANT for q < 0."
2284 (aref (aref artist-octant-info (- octant 1)) 4))
2286 (defsubst artist-get-y-step-q<0 (octant)
2287 "Retrieve y-step component for OCTANT for q < 0."
2288 (aref (aref artist-octant-info (- octant 1)) 5))
2291 ;; Find octant from x1 y1 x2 y2 coordinates.
2293 (defun artist-find-octant (x1 y1 x2 y2)
2294 "Find octant for a line from X1,Y1 to X2,Y2.
2295 Octant are numbered 1--8, anti-clockwise as:
2297 \\3|2/
2298 4\\|/1
2299 ---+---
2300 5/|\\8
2301 /6|7\\"
2303 (if (<= x1 x2) ; quadrant 1 or 4
2304 (if (<= y1 y2) ; quadrant 1, octant 1 or 2
2305 (if (>= (- x2 x1) (- y2 y1))
2308 (if (>= (- x2 x1) (- (- y2 y1))) ; quadrant 4, octant 7 or 8
2311 (if (<= y1 y2) ; quadrant 2 or 3
2312 (if (>= (- (- x2 x1)) (- y2 y1)) ; quadrant 2, octant 3 or 4
2315 (if (>= (- (- x2 x1)) (- (- y2 y1))) ; quadrant 3, octant 5 or 6
2317 6))))
2319 ;; Some inline funtions for creating, setting and reading
2320 ;; members of a coordinate
2322 (defsubst artist-new-coord (x y &optional new-char)
2323 "Create a new coordinate at X,Y for use in a line.
2324 Optional argument NEW-CHAR can be used for setting the new-char component
2325 in the coord."
2326 (let ((coord (make-vector 4 x)))
2327 (aset coord 1 y)
2328 (aset coord 3 new-char)
2329 coord))
2331 (defsubst artist-coord-get-x (coord)
2332 "Retrieve the x component of a COORD."
2333 (aref coord 0))
2335 (defsubst artist-coord-get-y (coord)
2336 "Retrieve the y component of a COORD."
2337 (aref coord 1))
2339 (defsubst artist-coord-set-x (coord new-x)
2340 "Set the x component of a COORD to NEW-X."
2341 (aset coord 0 new-x)
2342 coord)
2344 (defsubst artist-coord-set-y (coord new-y)
2345 "Set the y component of a COORD to NEW-Y."
2346 (aset coord 1 new-y)
2347 coord)
2349 (defsubst artist-coord-get-saved-char (coord)
2350 "Retrieve the saved char component of a COORD."
2351 (aref coord 2))
2353 (defsubst artist-coord-get-new-char (coord)
2354 "Retrieve the new char component of a COORD."
2355 (aref coord 3))
2357 (defsubst artist-coord-add-saved-char (coord saved-char)
2358 "Set the saved char component of a COORD to SAVED-CHAR."
2359 (aset coord 2 saved-char)
2360 coord)
2362 (defsubst artist-coord-add-new-char (coord new-char)
2363 "Set the new char component of a COORD to NEW-CHAR."
2364 (aset coord 3 new-char)
2365 coord)
2367 (defsubst artist-coord-set-new-char (coord new-char)
2368 "Set the new char component of a COORD to NEW-CHAR."
2369 (aset coord 3 new-char)
2370 coord)
2373 ;; Pretend we are plotting a pixel. Instead we just list it
2375 (defmacro artist-put-pixel (point-list x y)
2376 "In POINT-LIST, store a ``pixel'' at coord X,Y."
2377 (list 'setq point-list
2378 (list 'append point-list (list 'list (list 'artist-new-coord x y)))))
2380 ;; Calculate list of points using eight point algorithm
2381 ;; return a list of coords
2383 (defun artist-eight-point (x1 y1 x2 y2)
2384 "Run the eight-point algorithm to get a list of coords from X1,Y1 to X2,Y2."
2385 (let* ((point-list nil)
2386 (octant (artist-find-octant x1 y1 x2 y2))
2387 (dfdx-coeff (artist-get-dfdx-init-coeff octant))
2388 (dfdy-coeff (artist-get-dfdy-init-coeff octant))
2389 (x-step-q>=0 (artist-get-x-step-q>=0 octant))
2390 (y-step-q>=0 (artist-get-y-step-q>=0 octant))
2391 (x-step-q<0 (artist-get-x-step-q<0 octant))
2392 (y-step-q<0 (artist-get-y-step-q<0 octant))
2393 (dfdx (- (- y2 y1)))
2394 (dfdy (- x2 x1))
2395 (x x1)
2396 (y y1)
2397 (f 0)
2398 (q (+ (* 2 f)
2399 (* dfdx-coeff dfdx)
2400 (* dfdy-coeff dfdy))))
2401 (artist-put-pixel point-list x y)
2402 (while (or (not (eq x x2)) (not (eq y y2)))
2403 (if (>= q 0)
2404 (progn
2405 (setq x (+ x x-step-q>=0))
2406 (setq y (+ y y-step-q>=0))
2407 (setq f (+ f (* x-step-q>=0 dfdx) (* y-step-q>=0 dfdy))))
2408 (progn
2409 (setq x (+ x x-step-q<0))
2410 (setq y (+ y y-step-q<0))
2411 (setq f (+ f (* x-step-q<0 dfdx) (* y-step-q<0 dfdy)))))
2412 (setq q (+ (* 2 f) (* dfdx-coeff dfdx) (* dfdy-coeff dfdy)))
2413 (artist-put-pixel point-list x y))
2414 point-list))
2416 ;; artist-save-chars-under-point-list
2417 ;; Remebers the chars that were there before we did draw the line.
2418 ;; Returns point-list.
2420 (defun artist-save-chars-under-point-list (point-list)
2421 "Save characters originally under POINT-LIST."
2422 (mapcar
2423 (lambda (coord)
2424 (artist-coord-add-saved-char
2425 coord
2426 (artist-get-char-at-xy (artist-coord-get-x coord)
2427 (artist-coord-get-y coord))))
2428 point-list))
2430 ;; artist-calculate-new-char, artist-calculate-new-chars
2431 ;; Calculates which char to insert depending on direction of point-list.
2433 ;; Depending on new-coord's position relative to last-coord one of the
2434 ;; following chars are returned: \ | / - o, as indicated by this:
2436 ;; \ | /
2437 ;; - o -
2438 ;; / | \
2440 ;; artist-calculate-new-char works on one coordinate, returns char.
2441 ;; artist-calculate-new-chars works on a point-list, returns point-list.
2443 (defun artist-calculate-new-char (last-coord new-coord)
2444 "Return a line-char to use when moving from LAST-COORD to NEW-COORD."
2445 (let ((last-x (artist-coord-get-x last-coord))
2446 (last-y (artist-coord-get-y last-coord))
2447 (new-x (artist-coord-get-x new-coord))
2448 (new-y (artist-coord-get-y new-coord)))
2449 (cond ((> new-x last-x) (cond ((< new-y last-y) ?/ )
2450 ((> new-y last-y) ?\\ )
2451 (t ?- )))
2452 ((< new-x last-x) (cond ((< new-y last-y) ?\\ )
2453 ((> new-y last-y) ?/ )
2454 (t ?- )))
2455 ((eq new-y last-y) ?o)
2456 (t ?| ))))
2458 (defun artist-calculate-new-chars (point-list)
2459 "Return a list of coords with line-chars calculated. Input: POINT-LIST."
2460 (if (null (cdr point-list))
2461 (list (artist-coord-add-new-char (car point-list) ?o ))
2462 (let ((last-coord (car point-list)))
2463 (cons (artist-coord-add-new-char
2464 (car point-list)
2465 (artist-calculate-new-char (car (cdr point-list))
2466 (car point-list)))
2467 (mapcar
2468 (lambda (this-coord)
2469 (prog1
2470 (artist-coord-add-new-char
2471 this-coord
2472 (artist-calculate-new-char last-coord this-coord))
2473 (setq last-coord this-coord)))
2474 (cdr point-list))))))
2476 ;; artist-modify-new-chars
2477 ;; Replaces some characters with some other characters.
2479 ;; artist-modify-new-chars works on a point-list, returns point-list.
2481 (defun artist-modify-new-chars (point-list)
2482 "Replace intersecting characters in POINT-LIST.
2483 This function returns a point-list."
2484 (mapcar
2485 (lambda (coord)
2486 (let* ((new-c (artist-coord-get-new-char coord))
2487 (saved-c (artist-coord-get-saved-char coord))
2488 (modified-c (artist-intersection-char new-c saved-c)))
2489 (artist-coord-set-new-char coord modified-c)))
2490 point-list))
2494 ;; functions for accessing endoints and elements in object requiring
2495 ;; 2 endpoints
2498 (defun artist-make-endpoint (x y)
2499 "Create an endpoint at X, Y."
2500 (let ((new-endpoint (make-vector 2 x)))
2501 (aset new-endpoint 1 y)
2502 new-endpoint))
2504 (defun artist-endpoint-get-x (endpoint)
2505 "Retrieve the x component of an ENDPOINT."
2506 (aref endpoint 0))
2508 (defun artist-endpoint-get-y (endpoint)
2509 "Retrieve the y component of an ENDPOINT."
2510 (aref endpoint 1))
2512 (defun artist-make-2point-object (endpoint1 endpoint2 shapeinfo)
2513 "Create a 2-point object of ENDPOINT1, ENDPOINT2 and SHAPEINFO."
2514 (list endpoint1 endpoint2 shapeinfo))
2516 (defun artist-2point-get-endpoint1 (obj)
2517 "Retrieve the first endpoint of a 2-point object OBJ."
2518 (elt obj 0))
2520 (defun artist-2point-get-endpoint2 (obj)
2521 "Retrieve the second endpoint of a 2-point object OBJ."
2522 (elt obj 1))
2524 (defun artist-2point-get-shapeinfo (obj)
2525 "Retrieve the shapeinfo component of a 2-point object OBJ."
2526 (elt obj 2))
2530 ;; Drawing and undrawing lines (any direction)
2533 (defun artist-draw-line (x1 y1 x2 y2)
2534 "Draws a line from X1, Y1 to X2, Y2.
2536 Output is a line, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
2538 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
2539 SHAPE-INFO is a list of vectors [X Y SAVED-CHAR NEW-CHAR]."
2540 (let ((endpoint1 (artist-make-endpoint x1 y1))
2541 (endpoint2 (artist-make-endpoint x2 y2)))
2542 (artist-make-2point-object
2543 endpoint1
2544 endpoint2
2545 (mapcar
2546 (lambda (coord)
2547 (artist-move-to-xy (artist-coord-get-x coord)
2548 (artist-coord-get-y coord))
2549 (if artist-line-char-set
2550 (artist-replace-char artist-line-char)
2551 (artist-replace-char (artist-coord-get-new-char coord)))
2552 coord)
2553 (artist-modify-new-chars
2554 (artist-calculate-new-chars
2555 (artist-save-chars-under-point-list
2556 (artist-eight-point x1 y1 x2 y2))))))))
2558 (defun artist-undraw-line (line)
2559 "Undraws LINE."
2560 (mapcar
2561 (lambda (coord)
2562 (artist-move-to-xy (artist-coord-get-x coord)
2563 (artist-coord-get-y coord))
2564 (artist-replace-char (artist-coord-get-saved-char coord))
2565 coord)
2566 (artist-2point-get-shapeinfo line)))
2569 ;; Drawing and undrawing straight lines
2572 (defun artist-draw-sline (x1 y1 x2 y2)
2573 "Draw a strait line from X1, Y1 to X2, Y2.
2574 Straight lines are vertical, horizontal or diagonal lines.
2575 They are faster to draw and most often they are what you need
2576 when drawing a simple image.
2578 Output is a straight line, which is a list on the form
2579 \(END-POINT-1 END-POINT-2 SHAPE-INFO).
2581 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
2582 SHAPE-INFO is a vector [START-X START-Y LENGTH-OF-LINE DIRECTION
2583 ORIGINAL-CHAR-1 ORIGINAL-CHAR-2 ... ]."
2584 (let* ((line (artist-save-chars-under-sline (artist-sline x1 y1 x2 y2)))
2585 (x (aref line 0))
2586 (y (aref line 1))
2587 (length (+ (aref line 2) 4))
2588 (direction (aref line 3))
2589 (line-char (artist-direction-char direction))
2590 (i 4)
2591 (endpoint1 (artist-make-endpoint x y))
2592 (endpoint2 nil))
2593 (while (< i length)
2594 (artist-move-to-xy x y)
2595 (if artist-line-char-set
2596 (artist-replace-char artist-line-char)
2597 (artist-replace-char (artist-intersection-char
2598 line-char
2599 (aref line i))))
2600 (if (not (< (1+ i) length))
2601 ;; This is the last element. Set the second endpoint
2602 (setq endpoint2 (artist-make-endpoint x y)))
2603 (setq x (+ x (artist-direction-step-x direction)))
2604 (setq y (+ y (artist-direction-step-y direction)))
2605 (setq i (1+ i)))
2606 (artist-make-2point-object endpoint1 endpoint2 line)))
2609 (defun artist-undraw-sline (line)
2610 "Undraw a straight line LINE."
2611 (if line
2612 (let* ((shape-info (artist-2point-get-shapeinfo line))
2613 (x (aref shape-info 0))
2614 (y (aref shape-info 1))
2615 (length (+ (aref shape-info 2) 4))
2616 (direction (aref shape-info 3))
2617 (i 4))
2618 (while (< i length)
2619 (artist-move-to-xy x y)
2620 (artist-replace-char (aref shape-info i))
2621 (setq x (+ x (artist-direction-step-x direction)))
2622 (setq y (+ y (artist-direction-step-y direction)))
2623 (setq i (1+ i))))))
2627 ;; Drawing and undrawing rectangles and squares
2630 (defun artist-draw-rect (x1 y1 x2 y2)
2631 "Draws a rectangle with corners at X1, Y1 and X2, Y2.
2633 Output is a rectangle, which is a list on the form
2634 \(END-POINT-1 END-POINT-2 SHAPE-INFO).
2636 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
2637 SHAPE-INFO is a list of four straight lines."
2638 (let* ((artist-line-char (artist-compute-line-char))
2639 (artist-line-char-set artist-line-char)
2640 (line1 (artist-draw-sline x1 y1 x2 y1))
2641 (line2 (artist-draw-sline x2 y1 x2 y2))
2642 (line3 (artist-draw-sline x2 y2 x1 y2))
2643 (line4 (artist-draw-sline x1 y2 x1 y1))
2644 (endpoint1 (artist-make-endpoint x1 y1))
2645 (endpoint2 (artist-make-endpoint x2 y2)))
2646 (artist-make-2point-object endpoint1
2647 endpoint2
2648 (list line1 line2 line3 line4))))
2650 (defun artist-undraw-rect (rectangle)
2651 "Undraws RECTANGLE."
2652 (if rectangle
2653 (let ((shape-info (artist-2point-get-shapeinfo rectangle)))
2654 (artist-undraw-sline (elt shape-info 3))
2655 (artist-undraw-sline (elt shape-info 2))
2656 (artist-undraw-sline (elt shape-info 1))
2657 (artist-undraw-sline (elt shape-info 0)))))
2660 (defun artist-rect-corners-squarify (x1 y1 x2 y2)
2661 "Compute square corners from rectangle corners at X1, Y1 and X2, Y2.
2662 The square's first corner will be X1, Y1. The position of the second corner
2663 depends on which of X2 and Y2 is most far away from X1, Y1."
2664 (let* ((delta-x (- x2 x1))
2665 (delta-y (- y2 y1))
2666 (delta-x-sign (if (< delta-x 0) -1 1))
2667 (delta-y-sign (if (< delta-y 0) -1 1))
2668 (new-x2) ; set below
2669 (new-y2)) ; set below
2671 ;; Check which of x2 and y2 is most distant
2672 ;; take care to the aspect ratio
2673 (if (> (abs delta-x) (abs delta-y))
2675 ;; *** x2 more distant than y2 (with care taken to aspect ratio)
2676 (progn
2677 (setq new-x2 x2)
2678 (setq new-y2 (+ y1 (round (/ (* (abs delta-x) delta-y-sign)
2679 artist-aspect-ratio)))))
2681 ;; *** y2 more distant than x2 (with care taken to aspect ratio)
2682 (progn
2683 (setq new-x2 (round (+ x1 (* (* (abs delta-y) delta-x-sign)
2684 artist-aspect-ratio))))
2685 (setq new-y2 y2)))
2687 ;; Return this
2688 (list x1 y1 new-x2 new-y2)))
2691 (defun artist-draw-square (x1 y1 x2 y2)
2692 "Draw a square with corners at X1, Y1 and X2, Y2.
2694 Output is a square, which is a list on the form
2695 \(END-POINT-1 END-POINT-2 SHAPE-INFO).
2697 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
2698 SHAPE-INFO is a list of four straight lines."
2699 (let* ((artist-line-char (artist-compute-line-char))
2700 (artist-line-char-set artist-line-char)
2701 (square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
2702 (new-x1 (elt square-corners 0))
2703 (new-y1 (elt square-corners 1))
2704 (new-x2 (elt square-corners 2))
2705 (new-y2 (elt square-corners 3))
2706 (endpoint1 (artist-make-endpoint new-x1 new-y1))
2707 (endpoint2 (artist-make-endpoint new-x2 new-y2))
2708 (line1 (artist-draw-sline new-x1 new-y1 new-x2 new-y1))
2709 (line2 (artist-draw-sline new-x2 new-y1 new-x2 new-y2))
2710 (line3 (artist-draw-sline new-x2 new-y2 new-x1 new-y2))
2711 (line4 (artist-draw-sline new-x1 new-y2 new-x1 new-y1)))
2712 (artist-make-2point-object endpoint1
2713 endpoint2
2714 (list line1 line2 line3 line4))))
2716 (defun artist-undraw-square (square)
2717 "Undraws SQUARE."
2718 (if square
2719 (let ((shape-info (artist-2point-get-shapeinfo square)))
2720 (artist-undraw-sline (elt shape-info 3))
2721 (artist-undraw-sline (elt shape-info 2))
2722 (artist-undraw-sline (elt shape-info 1))
2723 (artist-undraw-sline (elt shape-info 0)))))
2726 ;; Filling rectangles and squares
2729 (defun artist-fill-rect (rect x1 y1 x2 y2)
2730 "Fill rectangle RECT from X1,Y1 to X2,Y2."
2731 (let ((x (1+ (min x1 x2)))
2732 (y (1+ (min y1 y2)))
2733 (x-max (max x1 x2))
2734 (y-max (max y1 y2)))
2735 (let ((w (- x-max x)))
2736 (while (< y y-max)
2737 (artist-move-to-xy x y)
2738 (artist-replace-chars artist-fill-char w)
2739 (setq y (1+ y))))))
2741 (defun artist-fill-square (square x1 y1 x2 y2)
2742 "Fills a SQUARE from X1,Y1 to X2,Y2."
2743 (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
2744 (new-x1 (elt square-corners 0))
2745 (new-y1 (elt square-corners 1))
2746 (new-x2 (elt square-corners 2))
2747 (new-y2 (elt square-corners 3))
2748 (x (1+ (min new-x1 new-x2)))
2749 (y (1+ (min new-y1 new-y2)))
2750 (x-max (max new-x1 new-x2))
2751 (y-max (max new-y1 new-y2))
2752 (w (- x-max x)))
2753 (while (< y y-max)
2754 (artist-move-to-xy x y)
2755 (artist-replace-chars artist-fill-char w)
2756 (setq y (1+ y)))))
2760 ;; Pen drawing
2763 (defun artist-pen (x1 y1)
2764 "Draws a character at X1, Y1.
2765 The character is replaced with the character in `artist-fill-char'."
2766 (artist-move-to-xy x1 y1)
2767 (artist-replace-char (if artist-line-char-set
2768 artist-line-char
2769 (if artist-fill-char-set
2770 artist-fill-char
2771 artist-default-fill-char))))
2774 (defun artist-pen-line (x1 y1)
2775 "Draws a line from last pen position to X1, Y1.
2776 The character is replaced with the character in `artist-fill-char'.
2777 This will store all points in `artist-key-poly-point-list' in reversed
2778 order (I assume it is faster to cons to the beginning of the list than
2779 to append to the end of the list, when doing free-hand drawing)."
2780 (let ((artist-line-char (if artist-line-char-set
2781 artist-line-char
2782 (if artist-fill-char-set
2783 artist-fill-char
2784 artist-default-fill-char))))
2786 ;; Draw line from last point to this
2787 (let ((x-last (car (car artist-key-poly-point-list)))
2788 (y-last (cdr (car artist-key-poly-point-list))))
2789 (artist-move-to-xy x-last y-last)
2790 (artist-replace-char artist-line-char)
2791 (artist-draw-line x-last y-last x1 y1))
2793 ;; Update the point-list
2794 (setq artist-key-poly-point-list
2795 (cons (cons x1 y1) artist-key-poly-point-list))))
2797 (defun artist-pen-reset-last-xy (x1 y1)
2798 "Reset the last x and y points to X1, Y1 when doing pen-drawing."
2799 (artist-clear-arrow-points)
2800 (setq artist-key-poly-point-list (list (cons x1 y1))))
2803 (defun artist-pen-set-arrow-points (x1 y1)
2804 "Set arrow points for pen drawing using X1, Y1.
2805 Also, the `artist-key-poly-point-list' is reversed."
2807 (setq artist-key-poly-point-list
2808 (artist-uniq artist-key-poly-point-list))
2810 (if (>= (length artist-key-poly-point-list) 2)
2812 ;; Only set arrow-points if the point-list has two or more entries
2813 (let ((xn (car (car artist-key-poly-point-list)))
2814 (yn (cdr (car artist-key-poly-point-list)))
2815 (xn-1 (car (car (cdr artist-key-poly-point-list))))
2816 (yn-1 (cdr (car (cdr artist-key-poly-point-list))))
2817 (dirn)) ; direction for point n
2818 (setq artist-key-poly-point-list (reverse artist-key-poly-point-list))
2819 (let ((x0 (car (car artist-key-poly-point-list)))
2820 (y0 (cdr (car artist-key-poly-point-list)))
2821 (x1 (car (car (cdr artist-key-poly-point-list))))
2822 (y1 (cdr (car (cdr artist-key-poly-point-list))))
2823 (dir0)) ; direction for point 0
2824 (setq dir0 (artist-find-direction x1 y1 x0 y0))
2825 (setq dirn (artist-find-direction xn-1 yn-1 xn yn))
2826 (setq artist-arrow-point-1 (artist-make-arrow-point x0 y0 dir0))
2827 (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn))))))
2831 ;; Text rendering
2833 (defun artist-figlet-run (text font extra-args)
2834 "Run figlet rendering TEXT using FONT.
2835 EXTRA-ARGS for figlet, for the command line, may be specified."
2836 (let* ((figlet-args (cond ((and font extra-args)
2837 (cons (concat "-f" font)
2838 (artist-string-split extra-args "[ \t]+")))
2839 (font (concat "-f" font))
2840 (extra-args
2841 (artist-string-split extra-args "[ \t]+"))
2842 (t nil)))
2843 (figlet-output (artist-system artist-figlet-program text figlet-args))
2844 (exit-code (elt figlet-output 0))
2845 (stdout (elt figlet-output 1))
2846 (stderr (elt figlet-output 2)))
2847 (if (not (= exit-code 0))
2848 (error "Failed to render font: %s (%d)" stderr exit-code))
2849 stdout))
2851 (defun artist-figlet-get-font-list ()
2852 "Read fonts in with the shell command.
2853 Returns a list of strings."
2854 (let* ((cmd-interpreter "/bin/sh")
2855 (ls-cmd artist-figlet-list-fonts-command)
2856 (result (artist-system cmd-interpreter ls-cmd nil))
2857 (exit-code (elt result 0))
2858 (stdout (elt result 1))
2859 (stderr (elt result 2)))
2860 (if (not (= exit-code 0))
2861 (error "Failed to read available fonts: %s (%d)" stderr exit-code))
2862 (artist-string-split stdout ".flf\n")))
2864 (defun artist-figlet-choose-font ()
2865 "Read any extra arguments for figlet."
2866 (interactive)
2867 (let* ((avail-fonts (artist-figlet-get-font-list))
2868 (font (completing-read (concat "Select font: (default "
2869 artist-figlet-default-font
2870 ") ")
2871 (mapcar
2872 (lambda (font) (cons font font))
2873 avail-fonts))))
2874 (if (string= font "") artist-figlet-default-font font)))
2876 (defun artist-figlet-get-extra-args ()
2877 "Read any extra arguments for figlet."
2878 (let ((extra-args (read-input "Extra args to figlet: ")))
2879 (if (string= extra-args "")
2881 extra-args)))
2883 (defun artist-figlet (text)
2884 "Render TEXT using figlet."
2885 (let* ((figlet-font (artist-figlet-choose-font))
2886 (figlet-extra-args (artist-figlet-get-extra-args)))
2887 (artist-figlet-run text figlet-font figlet-extra-args)))
2890 (defun artist-text-insert-common (x y text see-thru)
2891 "At position X, Y, insert text TEXT.
2892 If SEE-THRU is non-nil, then blanks in TEXT does not replace text
2893 in the buffer."
2894 (let* ((string-list (artist-string-split text "\n"))
2895 (i 0)
2896 (len (length string-list)))
2897 (while (< i len)
2898 (artist-move-to-xy x (+ y i))
2899 (artist-replace-string (car string-list) see-thru)
2900 (setq string-list (cdr string-list))
2901 (setq i (1+ i)))))
2903 (defun artist-text-insert-see-thru (x y text)
2904 "At position X, Y, insert text TEXT.
2905 Let text already in buffer shine thru the TEXT inserted."
2906 (artist-text-insert-common x y text t))
2908 (defun artist-text-insert-overwrite (x y text)
2909 "At position X, Y, insert text TEXT.
2910 Let blanks in TEXT overwrite any text already in the buffer."
2911 (artist-text-insert-common x y text nil))
2913 (defun artist-text-see-thru (x y)
2914 "Prompt for text to render, render it at X,Y.
2915 This is done by calling the function specified by `artist-text-renderer',
2916 which must return a list of strings, to be inserted in the buffer.
2918 Text already in the buffer ``shines thru'' blanks in the rendered text."
2919 (let* ((input-text (read-input "Type text to render: "))
2920 (rendered-text (artist-funcall artist-text-renderer input-text)))
2921 (artist-text-insert-see-thru x y rendered-text)))
2924 (defun artist-text-overwrite (x y)
2925 "Prompt for text to render, render it at X,Y.
2926 This is done by calling the function specified by `artist-text-renderer',
2927 which must return a list of strings, to be inserted in the buffer.
2929 Blanks in the rendered text overwrites any text in the buffer."
2930 (let* ((input-text (read-input "Type text to render: "))
2931 (rendered-text (artist-funcall artist-text-renderer input-text)))
2932 (artist-text-insert-overwrite x y rendered-text)))
2935 ;; Spraying
2938 (defun artist-spray-get-interval ()
2939 "Retrieves the interval for repeated spray."
2940 artist-spray-interval)
2942 (defun artist-spray-random-points (n radius)
2943 "Generate N random points within a radius of RADIUS.
2944 Returns a list of points. Each point is on the form (X1 . Y1)."
2945 (let ((points))
2946 (while (> n 0)
2947 (let* ((angle (* (random 359) (/ pi 180)))
2948 (dist (random radius))
2949 (point (cons (round (* dist (cos angle)))
2950 (round (* dist (sin angle))))))
2951 (setq points (cons point points)))
2952 (setq n (- n 1)))
2953 points))
2955 (defun artist-spray (x1 y1)
2956 "Spray at X1, Y1."
2957 (let* ((num-points (* artist-spray-radius artist-spray-radius))
2958 (spray-points (artist-spray-random-points num-points
2959 artist-spray-radius)))
2960 (while spray-points
2961 ;; Replace one spray point
2962 (let* ((point (car spray-points))
2963 (x (+ x1 (car point)))
2964 (y (+ y1 (cdr point)))
2965 (buf-c (artist-get-char-at-xy-conv x y))
2966 (this-c (memq buf-c artist-spray-chars))
2967 (next-c (cond ((null this-c) artist-spray-new-char)
2968 ((null (cdr this-c)) (car this-c))
2969 (t (car (cdr this-c))))))
2970 (artist-move-to-xy x y)
2971 (artist-replace-char next-c))
2973 ;; Step to next spray point
2974 (setq spray-points (cdr spray-points)))))
2976 (defun artist-spray-clear-circle (circle x1 y1 x2 y2)
2977 "Clears circle CIRCLE at X1, Y1 through X2, Y2."
2978 (artist-undraw-circle circle))
2980 (defun artist-spray-set-radius (circle x1 y1 x2 y2)
2981 "Set spray radius from CIRCLE at X1, Y1 through X2, Y2."
2982 (let ((dx (- x2 x1))
2983 (dy (- y2 y1)))
2984 (setq artist-spray-radius (round (sqrt (+ (* dx dx) (* dy dy)))))
2985 (if (= 0 artist-spray-radius)
2986 (setq artist-spray-radius 1))))
2989 ;; Erasing
2992 (defun artist-erase-char (x1 y1)
2993 "Erases a character at X1, Y1.
2994 The character is replaced with the character in `artist-erase-char'."
2995 (artist-move-to-xy x1 y1)
2996 (artist-replace-char artist-erase-char))
2998 (defun artist-erase-rect (rect x1 y1 x2 y2)
2999 "Erase rectangle RECT from X1, Y1, X2, Y2."
3000 (let ((artist-line-char-set t)
3001 (artist-fill-char-set t)
3002 (artist-line-char artist-erase-char)
3003 (artist-fill-char artist-erase-char))
3004 (artist-draw-rect x1 y1 x2 y2)
3005 (artist-fill-rect rect x1 y1 x2 y2)))
3009 ;; Vaporizing (erasing) line and lines
3013 (defun artist-vap-find-endpoint (x1 y1 step-x step-y accept-set reject-set)
3014 "Find one endpoint for line through X1, Y1.
3015 The endpoint is searched for in the direction defined by STEP-X, STEP-Y,
3016 accepting characters in the list ACCEPT-SET, stopping immediately
3017 when finding characters in the list REJECT-SET. Fuzziness, that is
3018 the number of consecutive characters not in ACCEPT-SET to allow as
3019 part of the line, is determined by the variable `artist-vaporize-fuzziness'.
3020 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3021 (let ((x x1)
3022 (y y1)
3023 (x-last x1)
3024 (y-last y1)
3025 (done nil))
3026 (while (not done)
3027 (let ((c (artist-get-char-at-xy-conv x y)))
3028 (cond ((memq c reject-set)
3029 (setq done t))
3031 ;; We found a character we are accepting as part of the line.
3032 ;; Update position
3033 ((memq c accept-set)
3034 (setq x-last x
3035 y-last y
3036 x (+ x step-x)
3037 y (+ y step-y))
3038 (if (or (< x 0) (< y 0)) ;stop at the edge
3039 (setq done t)))
3041 ;; We found a character we are not accepting as part of
3042 ;; the line Search `artist-vaporize-fuzziness'
3043 ;; characters away from this position in the same
3044 ;; direction to see if there are any characters in the
3045 ;; accept-set. If not, we have found the endpoint.
3047 (let ((fuzziness artist-vaporize-fuzziness)
3048 (x-tmp x)
3049 (y-tmp y))
3051 ;; while we have more fuzziness left and we have not
3052 ;; found a character accepted as a line, move
3053 ;; forward!
3054 (while (and (> fuzziness 0) (not (memq c accept-set)))
3055 (setq x-tmp (+ x-tmp step-x))
3056 (setq y-tmp (+ y-tmp step-y))
3057 (setq c (artist-get-char-at-xy-conv x-tmp y-tmp))
3058 (setq fuzziness (- fuzziness 1)))
3059 (if (memq c accept-set)
3061 ;; The line continues on the other side of the
3062 ;; not-accepted character.
3063 (setq x x-tmp
3064 y y-tmp)
3066 ;; Else: We couldn't find any line on the other side.
3067 ;; That means we are done searching for the endpoint.
3068 (setq done t)))))))
3069 (cons x-last y-last)))
3072 (defun artist-vap-find-endpoints-horiz (x y)
3073 "Find endpoints for a horizontal line through X, Y.
3074 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3075 (list (artist-vap-find-endpoint x y 1 0 '(?- ?+) '(? ))
3076 (artist-vap-find-endpoint x y -1 0 '(?- ?+) '(? ))))
3078 (defun artist-vap-find-endpoints-vert (x y)
3079 "Find endpoints for a vertical line through X, Y.
3080 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3081 (list (artist-vap-find-endpoint x y 0 1 '(?| ?+) '(? ))
3082 (artist-vap-find-endpoint x y 0 -1 '(?| ?+) '(? ))))
3084 (defun artist-vap-find-endpoints-swne (x y)
3085 "Find endpoints for a diagonal line (made by /'s) through X, Y.
3086 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3087 (list (artist-vap-find-endpoint x y 1 -1 '(?/ ?X) '(? ))
3088 (artist-vap-find-endpoint x y -1 1 '(?/ ?X) '(? ))))
3090 (defun artist-vap-find-endpoints-nwse (x y)
3091 "Find endpoints for a diagonal line (made by \\'s) through X, Y.
3092 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3093 (list (artist-vap-find-endpoint x y 1 1 '(?\\ ?X) '(? ))
3094 (artist-vap-find-endpoint x y -1 -1 '(?\\ ?X) '(? ))))
3097 (defun artist-vap-find-endpoints (x y)
3098 "Given a point X1, Y1, return a list of endpoints of lines through X, Y.
3099 An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3100 (if artist-line-char-set
3102 (let ((c (artist-get-char-at-xy-conv x y)))
3103 (cond ((eq c ?-) (artist-vap-find-endpoints-horiz x y))
3104 ((eq c ?|) (artist-vap-find-endpoints-vert x y))
3105 ((eq c ?/) (artist-vap-find-endpoints-swne x y))
3106 ((eq c ?\\) (artist-vap-find-endpoints-nwse x y))
3107 ((eq c ?+) (append (artist-vap-find-endpoints-horiz x y)
3108 (artist-vap-find-endpoints-vert x y)))
3109 ((eq c ?X) (append (artist-vap-find-endpoints-swne x y)
3110 (artist-vap-find-endpoints-nwse x y)))
3112 ;; We don't know how to find directions when we are on
3113 ;; another character
3114 (t nil)))))
3117 (defun artist-vap-group-in-pairs (l)
3118 "Group elements in list L in pairs."
3119 (cond ((null l) nil)
3120 ((null (cdr l)) l) ; unevent number of elements in list
3121 (t (append (list (list (car l) (car (cdr l))))
3122 (artist-vap-group-in-pairs (cdr (cdr l)))))))
3124 (defun artist-vaporize-by-endpoints (endpoint1 endpoint2)
3125 "Given ENDPOINT1 and ENDPOINT2, vaporize the line between them.
3126 An endpoint is a pair (X . Y)."
3127 (let* ((x1 (car endpoint1))
3128 (y1 (cdr endpoint1))
3129 (x2 (car endpoint2))
3130 (y2 (cdr endpoint2))
3131 (dir (artist-find-direction x1 y1 x2 y2))
3132 (x-step (aref [1 1 0 -1 -1 -1 0 1] dir))
3133 (y-step (aref [0 1 1 1 0 -1 -1 -1] dir))
3134 (line-c (aref [?- ?\\ ?| ?/ ?- ?\\ ?| ?/] dir))
3135 (line-len (elt (list (abs (- x2 x1))
3136 (abs (- x2 x1))
3137 (abs (- y2 y1))
3138 (abs (- y2 y1))
3139 (abs (- x1 x2))
3140 (abs (- x1 x2))
3141 (abs (- y1 y2))
3142 (abs (- y1 y2)))
3143 dir))
3144 (x x1)
3145 (y y1))
3146 (while (>= line-len 0)
3147 (let* ((buffer-c (artist-get-char-at-xy-conv x y))
3148 (new-c (artist-unintersection-char line-c buffer-c)))
3149 (artist-move-to-xy x y)
3150 (artist-replace-char new-c))
3151 (setq x (+ x x-step)
3152 y (+ y y-step)
3153 line-len (- line-len 1)))))
3156 (defun artist-vaporize-line (x1 y1)
3157 "Vaporize (erase) the straight line through X1, Y1.
3158 Do this by replacing the characters that forms the line with
3159 `artist-erase-char'. Output is a list of endpoints for lines
3160 through X1, Y1. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
3161 (let ((endpoints (artist-vap-find-endpoints x1 y1)))
3162 (mapcar
3163 (lambda (endpoints)
3164 (let ((ep1 (car endpoints))
3165 (ep2 (car (cdr endpoints))))
3166 (artist-vaporize-by-endpoints ep1 ep2)))
3167 (artist-vap-group-in-pairs endpoints))
3168 endpoints))
3171 ;; Implementation note: This depends on artist-vaporize-line doing
3172 ;; unintersections of intersecting lines.
3174 ;; Example:
3175 ;; Suppose the buffer looks like this and that we start vaporizing
3176 ;; lines at (3,0) (at the ``*'').
3178 ;; 0123456
3179 ;; 0+--*--+
3180 ;; 1| |
3181 ;; 2| |
3182 ;; 3+-----+
3184 ;; We will then push (0,0) and (6,0) on the stack, and vaporize the
3185 ;; topmost horizontal line:
3187 ;; 0123456
3188 ;; 0| |
3189 ;; 1| |
3190 ;; 2| |
3191 ;; 3+-----+
3193 ;; We will then pop (0,0) and remove the left-most vertival line while
3194 ;; pushing the lower left corner (0,3) on the stack, and so on until
3195 ;; the entire rectangle is vaporized.
3197 ;; Now, What if the `+' in the upper left and upper right corners,
3198 ;; had not been changed to `|' but to spaces instead? We would
3199 ;; have failed when popping (0,0) and vaporizing that line because
3200 ;; we wouldn't find any line at (0,0):
3202 ;; 0123456
3203 ;; 0
3204 ;; 1| |
3205 ;; 2| |
3206 ;; 3+-----+
3208 ;; That's why we depend on artist-vaporize-line doing unintersecting
3209 ;; of crossing lines. There are alternative ways to handle this
3210 ;; if it becomes too much a trouble.
3212 (defun artist-vaporize-lines (x1 y1)
3213 "Vaporize lines reachable from point X1, Y1."
3214 (let ((ep-stack nil))
3215 (mapcar
3216 (lambda (ep) (push ep ep-stack))
3217 (artist-vap-find-endpoints x1 y1))
3218 (while (not (null ep-stack))
3219 (let* ((vaporize-point (pop ep-stack))
3220 (new-endpoints (artist-vaporize-line (car vaporize-point)
3221 (cdr vaporize-point))))
3222 (mapcar
3223 (lambda (endpoint) (push endpoint ep-stack))
3224 new-endpoints)))))
3228 ;; Circles and ellipses
3230 (defun artist-ellipse-generate-quadrant (x-radius y-radius)
3231 "Create a point-list for first quadrant.
3232 Points go from (X-RADIUS, 0) to (0, Y-RADIUS).
3233 Quadrant is generated around origo."
3234 (let* ((rx2 (* x-radius x-radius))
3235 (ry2 (* y-radius y-radius))
3236 (2rx2 (* 2 rx2))
3237 (2ry2 (* 2 ry2))
3239 (x 0)
3240 (y y-radius)
3241 (px 0)
3242 (py (* 2rx2 y))
3243 (point-list nil))
3244 (artist-put-pixel point-list x y)
3245 (setq p (round (+ ry2 (- (* rx2 y-radius)) (* 0.25 rx2))))
3246 (while (< px py)
3247 (setq x (1+ x)
3248 px (+ px 2ry2))
3249 (if (< p 0)
3250 (setq p (+ p ry2 px))
3251 (setq y (- y 1)
3252 py (- py 2rx2)
3253 p (+ p ry2 px (- py))))
3254 (artist-put-pixel point-list x y))
3255 (setq p (round (+ (* ry2 (+ x 0.5) (+ x 0.5))
3256 (* rx2 (- y 1) (- y 1))
3257 (- (* rx2 ry2)))))
3258 (while (> y 0)
3259 (setq y (- y 1)
3260 py (- py 2rx2))
3261 (if (> p 0)
3262 (setq p (+ p rx2 (- py)))
3263 (setq x (1+ x)
3264 px (+ px 2ry2)
3265 p (+ p rx2 (- py) px)))
3266 (artist-put-pixel point-list x y))
3267 point-list))
3269 (defsubst artist-new-fill-item (x y width)
3270 "Create a new item at X, Y, with WIDTH.
3271 This is for use in fill-info in ellipses and circles."
3272 (let ((new-item (make-vector 3 x)))
3273 (aset new-item 1 y)
3274 (aset new-item 2 width)
3275 new-item))
3277 (defsubst artist-fill-item-get-x (fill-item)
3278 "Retrieve the x component of a FILL-ITEM."
3279 (aref fill-item 0))
3281 (defsubst artist-fill-item-set-x (fill-item new-x)
3282 "Set the x component of a FILL-ITEM to NEW-X."
3283 (aset fill-item 0 new-x)
3284 fill-item)
3286 (defsubst artist-fill-item-get-y (fill-item)
3287 "Retrieve the y component of a FILL-ITEM."
3288 (aref fill-item 1))
3290 (defsubst artist-fill-item-set-y (fill-item new-y)
3291 "Set the y component of a FILL-ITEM to NEW-Y."
3292 (aset fill-item 1 new-y)
3293 fill-item)
3295 (defsubst artist-fill-item-get-width (fill-item)
3296 "Retrieve the width component of a FILL-ITEM."
3297 (aref fill-item 2))
3299 (defsubst artist-fill-item-set-width (fill-item new-width)
3300 "Set the width component of a FILL-ITEM to NEW-WIDTH."
3301 (aset fill-item 2 new-width)
3302 fill-item)
3305 (defun artist-ellipse-point-list-add-center (x-center y-center point-list)
3306 "Add offsets X-CENTER and Y-CENTER to coordinates in POINT-LIST."
3307 (mapcar
3308 (lambda (p)
3309 (artist-coord-set-x p (+ x-center (artist-coord-get-x p)))
3310 (artist-coord-set-y p (+ y-center (artist-coord-get-y p))))
3311 point-list))
3314 (defun artist-ellipse-fill-info-add-center (x-center y-center fill-info)
3315 "Add offsets X-CENTER and Y-CENTER to fill-items in FILL-INFO."
3316 (mapcar
3317 (lambda (p)
3318 (artist-fill-item-set-x p (+ x-center (artist-fill-item-get-x p)))
3319 (artist-fill-item-set-y p (+ y-center (artist-fill-item-get-y p))))
3320 fill-info))
3322 (defun artist-ellipse-remove-0-fills (fill-info)
3323 "Remove fill-infos from FILL-INFO that fills a zero-width field."
3324 (cond ((null fill-info)
3325 nil)
3326 ((= 0 (artist-fill-item-get-width (car fill-info)))
3327 (artist-ellipse-remove-0-fills (cdr fill-info)))
3329 (append (list (car fill-info))
3330 (artist-ellipse-remove-0-fills (cdr fill-info))))))
3333 (defun artist-ellipse-compute-fill-info (point-list)
3334 "Compute fill info for ellipse around 0,0 from POINT-LIST.
3335 The POINT-LIST is expected to cover the first quadrant."
3336 (let ((first-half nil)
3337 (both-halves nil)
3338 (last-y nil))
3340 ;; Create first half (the lower one (since y grows downwards)) from
3341 ;; the first quadrant.
3342 (mapcar
3343 (lambda (coord)
3344 (let* ((x (artist-coord-get-x coord))
3345 (y (artist-coord-get-y coord))
3346 (width (max (- (* 2 x) 1) 0))
3347 (left-edge (- x width)))
3348 (if (or (null last-y) (not (= y last-y)))
3349 ;; This was either the first time,
3350 ;; or it was the first time on a new line
3351 (setq first-half
3352 (append first-half
3353 ;; Fill info item starts at left-edge on line y
3354 (list (artist-new-fill-item left-edge y width)))))
3355 (setq last-y y)))
3356 point-list)
3358 ;; Create the other half by mirroring the first half.
3359 (setq both-halves
3360 (append first-half
3361 (mapcar
3362 (lambda (i)
3363 (artist-new-fill-item (artist-fill-item-get-x i)
3364 (- (artist-fill-item-get-y i))
3365 (artist-fill-item-get-width i)))
3366 ;; The cdr below is so we don't include fill-info for
3367 ;;; the middle line twice
3368 (cdr (reverse first-half)))))
3369 (artist-ellipse-remove-0-fills both-halves)))
3372 (defun artist-ellipse-mirror-quadrant (point-list)
3373 "Mirror a POINT-LIST describing first quadrant to create a complete ellipse."
3374 (let ((right-half nil)
3375 (left-half nil))
3377 ;; First, if last char in that quadrant is `/', then replace it with `)'
3378 ;; This way we avoids things
3379 ;; --------- ---------
3380 ;; / \ / \
3381 ;; that look like: \ / instead we get: ( )
3382 ;; \ / \ /
3383 ;; --------- ---------
3384 (let ((last-coord (last point-list)))
3385 (if (= (artist-coord-get-new-char last-coord) ?/)
3386 (artist-coord-set-new-char last-coord artist-ellipse-right-char)))
3388 ;; Create the other part of the right half by mirroring the first part
3389 (setq right-half
3390 (append
3391 point-list
3392 (mapcar
3393 (lambda (coord)
3394 (let ((c (artist-coord-get-new-char coord)))
3395 (artist-new-coord (artist-coord-get-x coord)
3396 (- (artist-coord-get-y coord))
3397 (cond ((= c ?/) ?\\)
3398 ((= c ?\\) ?/)
3399 (t c)))))
3400 ;; The cdr below is so we don't draw the middle right char twice
3401 (cdr (reverse point-list)))))
3403 ;; Create the left half by mirroring the right half.
3404 (setq left-half
3405 (mapcar
3406 (lambda (coord)
3407 (let ((c (artist-coord-get-new-char coord)))
3408 (artist-new-coord (- (artist-coord-get-x coord))
3409 (artist-coord-get-y coord)
3410 (cond ((= c ?/) ?\\)
3411 ((= c ?\\) ?/)
3412 ((= c artist-ellipse-right-char)
3413 artist-ellipse-left-char)
3414 (t c)))))
3415 ;; The cdr and butlast below is so we don't draw the middle top
3416 ;; and middle bottom char twice.
3417 (butlast (cdr (reverse right-half)))))
3418 (append right-half left-half)))
3421 (defun artist-draw-ellipse-general (x1 y1 x-radius y-radius)
3422 "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS.
3424 Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
3426 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
3427 SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
3429 POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
3430 FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE].
3432 Ellipses with zero Y-RADIUS are not drawn correctly."
3433 (let* ((point-list (artist-ellipse-generate-quadrant x-radius y-radius))
3434 (fill-info (artist-ellipse-compute-fill-info point-list))
3435 (shape-info (make-vector 2 0)))
3437 (setq point-list (artist-calculate-new-chars point-list))
3438 (setq point-list (artist-ellipse-mirror-quadrant point-list))
3439 (setq point-list (artist-ellipse-point-list-add-center x1 y1 point-list))
3440 (setq fill-info (artist-ellipse-fill-info-add-center x1 y1 fill-info))
3442 ;; Draw the ellipse
3443 (setq point-list
3444 (mapcar
3445 (lambda (coord)
3446 (artist-move-to-xy (artist-coord-get-x coord)
3447 (artist-coord-get-y coord))
3448 (if artist-line-char-set
3449 (artist-replace-char artist-line-char)
3450 (artist-replace-char (artist-coord-get-new-char coord)))
3451 coord)
3452 (artist-modify-new-chars
3453 (artist-save-chars-under-point-list point-list))))
3455 (aset shape-info 0 point-list)
3456 (aset shape-info 1 fill-info)
3457 (artist-make-2point-object (artist-make-endpoint x1 y1)
3458 (artist-make-endpoint x-radius y-radius)
3459 shape-info)))
3461 (defun artist-draw-ellipse-with-0-height (x1 y1 x-radius y-radius)
3462 "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS.
3464 Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
3466 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
3467 SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
3469 POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
3470 FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE].
3472 The Y-RADIUS must be 0, but the X-RADIUS must not be 0."
3473 (let ((point-list nil)
3474 (width (max (- (abs (* 2 x-radius)) 1)))
3475 (left-edge (1+ (- x1 (abs x-radius))))
3476 (line-char (if artist-line-char-set artist-line-char ?-))
3477 (i 0)
3478 (point-list nil)
3479 (fill-info nil)
3480 (shape-info (make-vector 2 0)))
3481 (while (< i width)
3482 (let* ((line-x (+ left-edge i))
3483 (line-y y1)
3484 (new-coord (artist-new-coord line-x line-y)))
3485 (artist-coord-add-saved-char new-coord
3486 (artist-get-char-at-xy line-x line-y))
3487 (artist-move-to-xy line-x line-y)
3488 (artist-replace-char line-char)
3489 (setq point-list (append point-list (list new-coord)))
3490 (setq i (1+ i))))
3491 (aset shape-info 0 point-list)
3492 (aset shape-info 1 fill-info)
3493 (artist-make-2point-object (artist-make-endpoint x1 y1)
3494 (artist-make-endpoint x-radius y-radius)
3495 shape-info)))
3497 (defun artist-draw-ellipse (x1 y1 x2 y2)
3498 "Draw an ellipse with center at X1, Y1 and point X2,Y2.
3500 Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
3502 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
3503 SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
3505 POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
3506 FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]."
3507 (let* ((artist-line-char (artist-compute-line-char))
3508 (artist-line-char-set artist-line-char)
3509 (width (abs (- x2 x1)))
3510 (height (abs (- y2 y1)))
3512 ;; When we draw our ellipse, we want it to go through the cursor
3513 ;; position, but since x1,y1, x2,y2 marks the corners of one
3514 ;; of the quadrants, we have to enlarge the ellipse a bit.
3515 ;; Ok, so then why by sqrt(2)?
3516 ;; It comes from the equation for the ellipse (where a is the
3517 ;; x-radius and b is the y-radius):
3518 ;; f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0
3519 ;; and the fact that we want the enlarged ellipse to have the
3520 ;; same proportions as the smaller square, therefore we have:
3521 ;; a/b = x/y
3522 ;; Solving this yields a-in-larger-ellipse = a-in-smaller * sqrt(2)
3523 (x-radius (round (* width (sqrt 2))))
3524 (y-radius (round (* height (sqrt 2))))
3525 (x x1)
3526 (y y1))
3527 (if (and (= y1 y2) (not (= x1 x2)))
3528 (artist-draw-ellipse-with-0-height x y x-radius y-radius)
3529 (artist-draw-ellipse-general x y x-radius y-radius))))
3532 (defun artist-undraw-ellipse (ellipse)
3533 "Undraw ELLIPSE."
3534 (if ellipse
3535 (let ((point-list (aref (artist-2point-get-shapeinfo ellipse) 0)))
3536 (mapcar
3537 (lambda (coord)
3538 (artist-move-to-xy (artist-coord-get-x coord)
3539 (artist-coord-get-y coord))
3540 (artist-replace-char (artist-coord-get-saved-char coord))
3541 coord)
3542 point-list))))
3545 (defun artist-draw-circle (x1 y1 x2 y2)
3546 "Draw a circle with center at X1, Y1 and point X2,Y2.
3548 Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
3550 END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
3551 SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
3553 POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
3554 FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]."
3555 (let* ((artist-line-char (artist-compute-line-char))
3556 (artist-line-char-set artist-line-char)
3557 (width (abs (- x2 x1)))
3558 (height (abs (- y2 y1)))
3559 ;; When drawing our circle, we want it to through the cursor
3560 ;; just as when drawing the ellispe, but we have to take
3561 ;; care for the aspect-ratio.
3562 ;; The equation for the ellipse (where a is the x-radius and
3563 ;; b is the y-radius):
3564 ;; f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0
3565 ;; together with the relationship
3566 ;; a = aspect-ratio * b
3567 ;; gives
3568 ;; a = sqrt( x^2 + (aspect-ratio * y)^2 ) and
3569 ;; b = a / aspect-ratio
3570 (x-radius (round (sqrt (+ (* width width)
3571 (* (* artist-aspect-ratio height)
3572 (* artist-aspect-ratio height))))))
3573 (y-radius (round (/ x-radius artist-aspect-ratio))))
3574 (artist-draw-ellipse-general x1 y1 x-radius y-radius)))
3576 (defalias 'artist-undraw-circle 'artist-undraw-ellipse)
3580 ; Filling ellipses
3582 (defun artist-fill-ellipse (ellipse x y x-radius y-radius)
3583 "Fill an ELLIPSE centered at X,Y with radius X-RADIUS and Y-RADIUS."
3584 (let ((fill-info (aref (artist-2point-get-shapeinfo ellipse) 1)))
3585 (mapcar
3586 (lambda (fill-item)
3587 (artist-move-to-xy (artist-fill-item-get-x fill-item)
3588 (artist-fill-item-get-y fill-item))
3589 (artist-replace-chars artist-fill-char
3590 (artist-fill-item-get-width fill-item))
3591 fill-item)
3592 fill-info)))
3594 (defalias 'artist-fill-circle 'artist-fill-ellipse)
3598 ;; Cutting, copying and pasting rectangles and squares
3599 ;; (filling functions)
3602 (defun artist-cut-rect (rect x1 y1 x2 y2)
3603 "Copy rectangle RECT drawn from X1, Y1 to X2, Y2, then clear it."
3604 (artist-undraw-rect rect)
3605 (artist-copy-generic x1 y1 x2 y2)
3606 (artist-erase-rect rect x1 y1 x2 y2))
3608 (defun artist-cut-square (square x1 y1 x2 y2)
3609 "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (made square), then clears it."
3610 (artist-undraw-square square)
3611 (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
3612 (new-x1 (elt square-corners 0))
3613 (new-y1 (elt square-corners 1))
3614 (new-x2 (elt square-corners 2))
3615 (new-y2 (elt square-corners 3)))
3616 (artist-copy-generic new-x1 new-y1 new-x2 new-y2)
3617 (artist-erase-rect square new-x1 new-y1 new-x2 new-y2)))
3620 (defun artist-get-buffer-contents-at-xy (x y width)
3621 "Retrieve contents from the buffer at X, Y. WIDTH characters are returned."
3622 (artist-move-to-xy x y)
3623 (let ((here (point))
3624 (there (save-excursion (artist-move-to-xy (+ x width) y) (point))))
3625 (untabify here there)
3626 (setq there (save-excursion (artist-move-to-xy (+ x width) y) (point)))
3627 (buffer-substring here there)))
3630 (defun artist-copy-generic (x1 y1 x2 y2)
3631 "Copy a rectangular area with corners at X1, Y1 and X2, Y2.
3632 Output is a copy buffer, a list of strings, representing the
3633 original contents of that area in the buffer."
3634 (let* ((x (min x1 x2))
3635 (y (min y1 y2))
3636 (x-max (max x1 x2))
3637 (y-max (max y1 y2))
3638 (w (+ (- x-max x) 1))
3639 (l nil))
3640 (while (<= y y-max)
3641 (setq l (cons (artist-get-buffer-contents-at-xy x y w) l))
3642 (setq y (1+ y)))
3643 (if artist-interface-with-rect
3644 (setq killed-rectangle (reverse l))
3645 (setq artist-copy-buffer (reverse l)))))
3648 (defun artist-copy-rect (rect x1 y1 x2 y2)
3649 "Copy rectangle RECT drawn from X1, Y1 to X2, Y2."
3650 (artist-undraw-rect rect)
3651 (artist-copy-generic x1 y1 x2 y2))
3653 (defun artist-copy-square (square x1 y1 x2 y2)
3654 "Copies a SQUARE drawn from X1, Y1 to X2, Y2 (but made square)."
3655 (artist-undraw-square square)
3656 (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
3657 (new-x1 (elt square-corners 0))
3658 (new-y1 (elt square-corners 1))
3659 (new-x2 (elt square-corners 2))
3660 (new-y2 (elt square-corners 3)))
3661 (artist-copy-generic new-x1 new-y1 new-x2 new-y2)))
3663 (defun artist-paste (x y)
3664 "Pastes the contents of the copy-buffer at X,Y."
3665 (let ((copy-buf (if artist-interface-with-rect
3666 killed-rectangle
3667 artist-copy-buffer)))
3668 (if (not (null copy-buf))
3669 (while (not (null copy-buf))
3670 (artist-move-to-xy x y)
3671 (artist-replace-string (car copy-buf))
3672 (setq copy-buf (cdr copy-buf))
3673 (setq y (1+ y)))
3674 (message "Nothing to paste"))))
3678 ;; Flood filling
3680 (defun artist-ff-too-far-right (x)
3681 "Determine if the position X is too far to the right."
3682 (cond ((numberp artist-flood-fill-right-border)
3683 (> x artist-flood-fill-right-border))
3684 ((eq artist-flood-fill-right-border 'window-width)
3685 (> x (- (window-width) 2)))
3686 ((eq artist-flood-fill-right-border 'fill-column)
3687 (> x fill-column))
3688 (t (error "Invalid value for `artist-flood-fill-right-border'"))))
3690 (defun artist-ff-get-rightmost-from-xy (x y)
3691 "Find the rightmost position in this run, starting at X, Y."
3692 (save-excursion
3693 (let ((char-at-xy (artist-get-char-at-xy-conv x y))
3694 (last-x x))
3695 (setq x (1+ x))
3696 (while (and (not (artist-ff-too-far-right x))
3697 (= char-at-xy (artist-get-char-at-xy-conv x y)))
3698 (setq last-x x)
3699 (setq x (1+ x)))
3700 last-x)))
3702 (defun artist-ff-is-topmost-line (x y)
3703 "Determine whether the position X,Y is on the topmost line or not."
3704 (= y 0))
3706 (defun artist-ff-is-bottommost-line (x y)
3707 "Determine whether the position X,Y is on the bottommost line or not."
3708 (save-excursion
3709 (goto-char (point-max))
3710 (beginning-of-line)
3711 (let ((last-line (artist-current-line)))
3712 (if (= (point) (point-max))
3714 ;; Last line is empty, don't paint on it, report previous line
3715 ;; as last line
3716 (>= y (- last-line 1))
3717 (>= y last-line)))))
3719 (defun artist-flood-fill (x1 y1)
3720 "Flood-fill starting at X1, Y1. Fill with the char in `artist-fill-char'."
3721 (let ((stack nil)
3722 (input-queue nil)
3723 ;; We are flood-filling the area that has this character.
3724 (c (artist-get-char-at-xy-conv x1 y1))
3725 (artist-fill-char (if artist-fill-char-set
3726 artist-fill-char
3727 artist-default-fill-char)))
3729 ;; Fill only if the fill-char is not the same as the character whose
3730 ;; area we are about to fill, or, in other words, don't fill if we
3731 ;; needn't.
3732 (if (not (= c artist-fill-char))
3733 (push (artist-new-coord x1 y1) stack))
3735 (while (not (null stack))
3736 (let* ((coord (pop stack))
3737 (x (artist-coord-get-x coord))
3738 (y (artist-coord-get-y coord))
3740 ;; Here we keep track of the leftmost and rightmost position
3741 ;; for this run
3742 (x-leftmost 0)
3743 (x-rightmost 0)
3744 (last-x 0)
3746 ;; Remember if line above and below are accessible
3747 ;; Lines below the last one, and prior to the first-one
3748 ;; are not accessible.
3749 (lines-above nil)
3750 (lines-below nil)
3752 ;; Remember char for position on line above and below, so we
3753 ;; can find the rightmost positions on the runs.
3754 (last-c-above -1)
3755 (last-c-below -1))
3757 (setq x-rightmost (artist-ff-get-rightmost-from-xy x y))
3758 (setq lines-above (not (artist-ff-is-topmost-line x y)))
3759 (setq lines-below (not (artist-ff-is-bottommost-line x y)))
3760 (setq last-x x-rightmost)
3761 (setq x x-rightmost)
3763 ;; Search line above, push rightmost positions of runs for that line
3764 (while (and (>= x 0) (= c (artist-get-char-at-xy-conv x y)))
3765 (if lines-above
3766 (let ((c-above (artist-get-char-at-xy-conv x (- y 1))))
3767 (if (and (= c-above c) (/= c-above last-c-above))
3768 (push (artist-new-coord x (- y 1)) stack))
3769 (setq last-c-above c-above)))
3770 (setq last-x x)
3771 (setq x (- x 1)))
3773 ;; Remember the left-most position on this run
3774 (setq x-leftmost last-x)
3776 ;; Search line below, push rightmost positions of runs for that line
3777 (setq x x-rightmost)
3778 (while (>= x x-leftmost)
3779 (if lines-below
3780 (let ((c-below (artist-get-char-at-xy-conv x (1+ y))))
3781 (if (and (= c-below c) (/= c-below last-c-below))
3782 (push (artist-new-coord x (1+ y)) stack))
3783 (setq last-c-below c-below)))
3784 (setq x (- x 1)))
3786 (artist-move-to-xy x-leftmost y)
3787 (artist-replace-chars artist-fill-char (1+ (- x-rightmost x-leftmost)))
3789 ;; If we are to show incrementally, we have to remove any pending
3790 ;; input from the input queue, because processing of pending input
3791 ;; always has priority over display updates (although this input
3792 ;; won't be processed until we are done). Later on we will queue
3793 ;; the input on the input queue again.
3794 (if artist-flood-fill-show-incrementally
3795 (progn
3796 (if (input-pending-p)
3797 (discard-input))
3798 (artist-update-display)))))))
3801 ;; Accessors to arrow-points
3804 (defun artist-make-arrow-point (x y direction &optional state)
3805 "Create an arrow point at X, Y for a line in direction DIRECTION.
3806 Optional argument STATE can be used to set state (default is nil)."
3807 (save-excursion
3808 (let* ((arrow-point (make-vector 4 0))
3809 (arrow-marker (make-marker)))
3810 (artist-move-to-xy x y)
3811 (set-marker arrow-marker (point))
3812 (aset arrow-point 0 arrow-marker)
3813 (aset arrow-point 1 (artist-get-char-at-xy x y))
3814 (aset arrow-point 2 direction)
3815 (aset arrow-point 3 state)
3816 arrow-point)))
3818 (defsubst artist-arrow-point-get-marker (arrow-point)
3819 "Retrieve the marker component of an ARROW-POINT."
3820 (aref arrow-point 0))
3822 (defsubst artist-arrow-point-get-orig-char (arrow-point)
3823 "Retrieve the orig char component of an ARROW-POINT."
3824 (aref arrow-point 1))
3826 (defsubst artist-arrow-point-get-direction (arrow-point)
3827 "Retrieve the direction component of an ARROW-POINT."
3828 (aref arrow-point 2))
3830 (defsubst artist-arrow-point-get-state (arrow-point)
3831 "Retrieve the state component of an ARROW-POINT."
3832 (aref arrow-point 3))
3834 (defsubst artist-arrow-point-set-state (arrow-point new-state)
3835 "Set the state component of an ARROW-POINT to NEW-STATE."
3836 (aset arrow-point 3 new-state))
3839 (defun artist-clear-arrow-points ()
3840 "Clear current endpoints."
3841 (setq artist-arrow-point-1 nil)
3842 (setq artist-arrow-point-2 nil))
3844 (defun artist-set-arrow-points-for-poly (point-list)
3845 "Generic function for setting arrow-points for poly-shapes from POINT-LIST."
3846 (let* ((ep1 (elt point-list 0))
3847 (ep2 (elt point-list 1))
3848 (x1 (artist-endpoint-get-x ep1))
3849 (y1 (artist-endpoint-get-y ep1))
3850 (x2 (artist-endpoint-get-x ep2))
3851 (y2 (artist-endpoint-get-y ep2))
3852 (dir1 (artist-find-direction x2 y2 x1 y1))
3853 (epn (artist-last point-list))
3854 (epn-1 (artist-last point-list 2))
3855 (xn (artist-endpoint-get-x epn))
3856 (yn (artist-endpoint-get-y epn))
3857 (xn-1 (artist-endpoint-get-x epn-1))
3858 (yn-1 (artist-endpoint-get-y epn-1))
3859 (dirn (artist-find-direction xn-1 yn-1 xn yn)))
3860 (setq artist-arrow-point-1 (artist-make-arrow-point x1 y1 dir1))
3861 (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn))))
3864 (defun artist-set-arrow-points-for-2points (shape x1 y1 x2 y2)
3865 "Generic function for setting arrow-points for 2-point shapes.
3866 The 2-point shape SHAPE is drawn from X1, Y1 to X2, Y2."
3867 (let* ((endpoint1 (artist-2point-get-endpoint1 shape))
3868 (endpoint2 (artist-2point-get-endpoint2 shape))
3869 (x1 (artist-endpoint-get-x endpoint1))
3870 (y1 (artist-endpoint-get-y endpoint1))
3871 (x2 (artist-endpoint-get-x endpoint2))
3872 (y2 (artist-endpoint-get-y endpoint2)))
3873 (setq artist-arrow-point-1
3874 (artist-make-arrow-point x1 y1
3875 (artist-find-direction x2 y2 x1 y1)))
3876 (setq artist-arrow-point-2
3877 (artist-make-arrow-point x2 y2
3878 (artist-find-direction x1 y1 x2 y2)))))
3882 ;; Common routine for drawing/undrawing shapes based
3883 ;; on the draw-how
3886 (defun artist-key-undraw-continously (x y)
3887 "Undraw current continous shape with point at X, Y."
3888 ;; No undraw-info for continous shapes
3889 nil)
3891 (defun artist-key-undraw-poly (x y)
3892 "Undraw current poly shape with point at X, Y."
3893 (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
3894 (x1 (artist-endpoint-get-x artist-key-endpoint1))
3895 (y1 (artist-endpoint-get-y artist-key-endpoint1)))
3896 (artist-funcall undraw-fn artist-key-shape)))
3898 (defun artist-key-undraw-1point (x y)
3899 "Undraw current 1-point shape at X, Y."
3900 ;; No undraw-info for 1-point shapes
3901 nil)
3903 (defun artist-key-undraw-2points (x y)
3904 "Undraw current 2-point shape at X, Y."
3905 (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
3906 (x1 (artist-endpoint-get-x artist-key-endpoint1))
3907 (y1 (artist-endpoint-get-y artist-key-endpoint1)))
3908 (artist-funcall undraw-fn artist-key-shape)))
3910 (defun artist-key-undraw-common ()
3911 "Common routine undrawing current shape."
3912 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
3913 (col (artist-current-column))
3914 (row (artist-current-line)))
3916 ;; Depending on what we are currently drawing, call other routines
3917 ;; that knows how to do the job
3919 (cond ((eq draw-how 'artist-do-continously)
3920 (artist-key-undraw-continously col row))
3921 ((eq draw-how 'artist-do-poly)
3922 (artist-key-undraw-poly col row))
3923 ((and (numberp draw-how) (= draw-how 1))
3924 (artist-key-undraw-1point col row))
3925 ((and (numberp draw-how) (= draw-how 2))
3926 (artist-key-undraw-2points col row))
3927 (t (message "Undrawing \"%s\"s is not yet implemented" draw-how)))
3929 ;; Now restore the old position
3931 (artist-move-to-xy col row)))
3935 ;; Implementation note: This really should honor the interval-fn entry
3936 ;; in the master table, `artist-mt', which would mean leaving a timer
3937 ;; that calls `draw-fn' every now and then. That timer would then have
3938 ;; to be cancelled and reinstalled whenever the user moves the cursor.
3939 ;; This could be done, but what if the user suddenly switches to another
3940 ;; drawing mode, or even kills the buffer! In the mouse case, it is much
3941 ;; simpler: when at the end of `artist-mouse-draw-continously', the
3942 ;; user has released the button, so the timer will always be cancelled
3943 ;; at that point.
3944 (defun artist-key-draw-continously (x y)
3945 "Draws current continous shape at X,Y."
3946 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
3947 (setq artist-key-shape (artist-funcall draw-fn x y))))
3949 (defun artist-key-draw-poly (x y)
3950 "Draws current poly-point shape with nth point at X,Y."
3951 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
3952 (x1 (artist-endpoint-get-x artist-key-endpoint1))
3953 (y1 (artist-endpoint-get-y artist-key-endpoint1)))
3954 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y))))
3956 (defun artist-key-draw-1point (x y)
3957 "Draws current 1-point shape at X,Y."
3958 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
3959 (setq artist-key-shape (artist-funcall draw-fn x y))))
3962 (defun artist-key-draw-2points (x y)
3963 "Draws current 2-point shape at X,Y."
3964 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
3965 (x1 (artist-endpoint-get-x artist-key-endpoint1))
3966 (y1 (artist-endpoint-get-y artist-key-endpoint1)))
3967 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y))))
3969 (defun artist-key-draw-common ()
3970 "Common routine for drawing current shape."
3971 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
3972 (col (artist-current-column))
3973 (row (artist-current-line)))
3975 ;; Depending on what we are currently drawing, call other routines
3976 ;; that knows how to do the job
3978 (cond ((eq draw-how 'artist-do-continously)
3979 (artist-key-draw-continously col row))
3980 ((eq draw-how 'artist-do-poly)
3981 (artist-key-draw-poly col row))
3982 ((and (numberp draw-how) (= draw-how 1))
3983 (artist-key-draw-1point col row))
3984 ((and (numberp draw-how) (= draw-how 2))
3985 (artist-key-draw-2points col row))
3986 (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
3988 ;; Now restore the old position
3990 (artist-move-to-xy col row)))
3995 ;; Functions related to trimming line-endings
3996 ;; The region between the topmost and bottommost visited line is
3997 ;; called a draw-region.
4000 (defun artist-draw-region-reset ()
4001 "Reset the current draw-region."
4002 (setq artist-draw-region-max-y 0)
4003 (setq artist-draw-region-min-y 1000000))
4005 (defun artist-draw-region-trim-line-endings (min-y max-y)
4006 "Trim lines in current draw-region from MIN-Y to MAX-Y.
4007 Trimming here means removing white space at end of a line."
4008 ;; Safetyc check: switch min-y and max-y if if max-y is smaller
4009 (if (< max-y min-y)
4010 (let ((tmp min-y))
4011 (setq min-y max-y)
4012 (setq max-y tmp)))
4013 (save-excursion
4014 (let ((curr-y min-y))
4015 (while (<= curr-y max-y)
4016 (artist-move-to-xy 0 curr-y)
4017 (end-of-line)
4018 (delete-horizontal-space)
4019 (setq curr-y (1+ curr-y))))))
4022 ;; Drawing shapes by using keys
4025 (defun artist-key-do-continously-continously (x y)
4026 "Update current continous shape at X,Y."
4027 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
4028 (artist-funcall draw-fn x y)))
4031 (defun artist-key-do-continously-poly (x y)
4032 "Update current poly-point shape with nth point at X,Y."
4033 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4034 (undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
4035 (x1 (artist-endpoint-get-x artist-key-endpoint1))
4036 (y1 (artist-endpoint-get-y artist-key-endpoint1))
4037 (x2 x)
4038 (y2 y))
4039 ;; If not rubber-banding, then move the 2
4040 ;; Otherwise re-draw the shape to the new position
4042 (if (not artist-rubber-banding)
4043 (progn
4044 (artist-no-rb-unset-point2)
4045 (artist-no-rb-set-point2 x y))
4046 (progn
4047 (artist-funcall undraw-fn artist-key-shape)
4048 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))))
4051 (defun artist-key-do-continously-1point (x y)
4052 "Update current 1-point shape at X,Y."
4053 ;; Nothing to do continously for operations
4054 ;; where we have only one input point
4055 nil)
4057 (defun artist-key-do-continously-2points (x y)
4058 "Update current 2-point shape with 2nd point at X,Y."
4059 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4060 (undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
4061 (x1 (artist-endpoint-get-x artist-key-endpoint1))
4062 (y1 (artist-endpoint-get-y artist-key-endpoint1))
4063 (x2 x)
4064 (y2 y))
4065 ;; If not rubber-banding, then move the 2
4066 ;; Otherwise re-draw the shape to the new position
4068 (if (not artist-rubber-banding)
4069 (progn
4070 (artist-no-rb-unset-point2)
4071 (artist-no-rb-set-point2 x y))
4072 (progn
4073 (artist-funcall undraw-fn artist-key-shape)
4074 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))))
4077 (defun artist-key-do-continously-common ()
4078 "Common routine for updating current shape."
4079 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
4080 (col (artist-current-column))
4081 (row (artist-current-line)))
4083 ;; Depending on what we are currently drawing, call other routines
4084 ;; that knows how to do the job
4086 (cond ((eq draw-how 'artist-do-continously)
4087 (artist-key-do-continously-continously col row))
4088 ((eq draw-how 'artist-do-poly)
4089 (artist-key-do-continously-poly col row))
4090 ((and (numberp draw-how) (= draw-how 1))
4091 (artist-key-do-continously-1point col row))
4092 ((and (numberp draw-how) (= draw-how 2))
4093 (artist-key-do-continously-2points col row))
4094 (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
4096 ;; Now restore the old position
4098 (artist-move-to-xy col row)))
4101 (defun artist-key-set-point-continously (x y)
4102 "Set point for current continous shape at X,Y."
4103 ;; Maybe set arrow-points for continous shapes
4104 (let ((arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
4105 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go))
4106 (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
4107 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
4108 (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go)))
4110 (if (not artist-key-is-drawing)
4111 ;; *** We are about to begin drawing
4112 (progn
4113 (artist-funcall init-fn x y))
4115 ;; *** We are about to stop drawing
4116 (progn
4118 (artist-funcall prep-fill-fn x y)
4119 (if (artist-funcall arrow-pred)
4120 (artist-funcall arrow-set-fn x y)
4121 (artist-clear-arrow-points))
4122 (artist-funcall exit-fn x y))))
4124 ;; Toggle the is-drawing flag
4125 (setq artist-key-is-drawing (not artist-key-is-drawing)))
4129 (defun artist-key-set-point-poly (x y &optional this-is-last-point)
4130 "Set point for current poly-point shape at X,Y.
4131 If optional argument THIS-IS-LAST-POINT is non-nil, this point is the last."
4132 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4133 (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
4134 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
4135 (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
4136 (fill-pred (artist-go-get-fill-pred-from-symbol artist-curr-go))
4137 (fill-fn (artist-go-get-fill-fn-from-symbol artist-curr-go))
4138 (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
4139 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
4141 (if (not artist-key-is-drawing)
4143 ;; *** We were not drawing ==> set first point
4144 (progn
4146 (artist-funcall init-fn x y)
4148 ;; If not rubber-banding, set first point.
4149 ;; Otherwise, draw the shape from x,y to x,y
4150 (if (not artist-rubber-banding)
4151 (artist-no-rb-set-point1 x y)
4152 (setq artist-key-shape (artist-funcall draw-fn x y x y)))
4154 ;; Set first endpoint
4155 (setq artist-key-endpoint1 (artist-make-endpoint x y))
4157 ;; Set point-list to contain start point
4158 (setq artist-key-poly-point-list (list (artist-make-endpoint x y)))
4160 ;; Since we are not ready, set the arrow-points to nil
4161 (artist-clear-arrow-points)
4163 ;; Change state to drawing
4164 (setq artist-key-is-drawing t)
4166 ;; Feedback
4167 (message (substitute-command-keys
4168 (concat "First point set. "
4169 "Set next with \\[artist-key-set-point], "
4170 "set last with C-u \\[artist-key-set-point]"))))
4173 ;; *** We were drawing ==> we are about to set nth point
4174 ;; (last point if the argument this-is-last-point is non-nil)
4176 (let ((x1 (artist-endpoint-get-x artist-key-endpoint1))
4177 (y1 (artist-endpoint-get-y artist-key-endpoint1))
4178 (x2 x)
4179 (y2 y))
4181 ;; If not rubber-banding, undraw the 1's and 2's, then
4182 ;; draw the shape (if we were rubber-banding, then the
4183 ;; shape is already drawn in artist-key-do-continously-2points.)
4185 (if (not artist-rubber-banding)
4186 (progn
4187 (artist-no-rb-unset-points)
4188 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))
4190 ;; Set x2 and y2 from shape's second point
4191 ;; (which might be different from the mouse's second point,
4192 ;; if, for example, we are drawing a straight line)
4194 (if (not (null artist-key-shape))
4195 (let ((endpoint2 (artist-2point-get-endpoint2 artist-key-shape)))
4196 (setq x2 (artist-endpoint-get-x endpoint2))
4197 (setq y2 (artist-endpoint-get-y endpoint2))))
4199 ;; Add the endpoint to the list of poly-points
4200 (setq artist-key-poly-point-list
4201 (append artist-key-poly-point-list
4202 (list (artist-make-endpoint x2 y2))))
4204 ;; Now do handle the case when this is the last point,
4205 ;; and the case when this point isn't the last
4207 (if (not this-is-last-point)
4208 ;; ** This is not the last point
4209 (progn
4210 ;; Start drawing a new 2-point-shape from last endpoint.
4212 ;; First set the start-point
4213 (setq x1 x2)
4214 (setq y1 y2)
4215 (setq artist-key-endpoint1 (artist-make-endpoint x1 y1))
4217 ;; If we are not rubber-banding, then place the '1
4218 ;; Otherwise, draw the shape from x1,y1 to x1,y1
4219 (if (not artist-rubber-banding)
4220 (artist-no-rb-set-point1 x1 y1)
4221 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x1 y1)))
4223 ;; Feedback
4224 (message "Point set"))
4226 ;; ** This is the last point
4227 (progn
4229 (artist-funcall prep-fill-fn artist-key-poly-point-list)
4231 ;; Maybe fill
4232 (if (artist-funcall fill-pred)
4233 (artist-funcall fill-fn artist-key-shape
4234 artist-key-poly-point-list))
4236 ;; Set the arrow-points
4237 (if (artist-funcall arrow-pred)
4238 (artist-funcall arrow-set-fn artist-key-poly-point-list)
4239 (artist-clear-arrow-points))
4241 (artist-funcall exit-fn artist-key-poly-point-list)
4243 ;; Change state to not drawing
4244 (setq artist-key-shape nil)
4245 (setq artist-key-endpoint1 nil)
4246 (setq artist-key-is-drawing nil)))))))
4249 (defun artist-key-set-point-1point (x y)
4250 "Set point for current 1-point shape at X,Y."
4251 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4252 (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
4253 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
4254 (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
4255 (draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4256 (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
4257 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
4258 (artist-funcall init-fn x y)
4259 (artist-funcall draw-fn x y)
4260 (artist-funcall prep-fill-fn x y)
4261 (if (artist-funcall arrow-pred)
4262 (artist-funcall arrow-set-fn x y)
4263 (artist-clear-arrow-points))
4264 (artist-funcall exit-fn x y))
4265 (setq artist-key-shape nil)
4266 (setq artist-key-is-drawing nil))
4269 (defun artist-key-set-point-2points (x y)
4270 "Set first or second point in current 2-point shape at X,Y."
4271 (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
4272 (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
4273 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
4274 (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
4275 (fill-pred (artist-go-get-fill-pred-from-symbol artist-curr-go))
4276 (fill-fn (artist-go-get-fill-fn-from-symbol artist-curr-go))
4277 (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
4278 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
4279 (if (not artist-key-is-drawing)
4281 ;; *** We were not drawing ==> set first point
4282 (progn
4284 (artist-funcall init-fn x y)
4286 ;; If not rubber-banding, set first point.
4287 ;; Otherwise, draw the shape from x,y to x,y
4288 (if (not artist-rubber-banding)
4289 (artist-no-rb-set-point1 x y)
4290 (setq artist-key-shape (artist-funcall draw-fn x y x y)))
4292 ;; Set first endpoint
4293 (setq artist-key-endpoint1 (artist-make-endpoint x y))
4295 ;; Since we are not ready, clear the arrow-points
4296 (artist-clear-arrow-points)
4298 ;; Change state to drawing
4299 (setq artist-key-is-drawing t))
4301 ;; *** We were drawing ==> we are about to set 2nd point
4302 ;; and end the drawing operation
4304 (let ((x1 (artist-endpoint-get-x artist-key-endpoint1))
4305 (y1 (artist-endpoint-get-y artist-key-endpoint1))
4306 (x2 x)
4307 (y2 y))
4309 ;; If not rubber-banding, undraw the 1's and 2's, then
4310 ;; draw the shape (if we were rubber-banding, then the
4311 ;; shape is already drawn in artist-key-do-continously-2points.)
4313 (if (not artist-rubber-banding)
4314 (progn
4315 (artist-no-rb-unset-points)
4316 (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))
4318 (artist-funcall prep-fill-fn artist-key-shape x1 y1 x2 y2)
4320 ;; Maybe fill
4322 (if (artist-funcall fill-pred)
4323 (artist-funcall fill-fn artist-key-shape x1 y1 x2 y2))
4325 ;; Maybe set the arrow-points
4327 (if (artist-funcall arrow-pred)
4328 (artist-funcall arrow-set-fn artist-key-shape x1 y1 x2 y2)
4329 (artist-clear-arrow-points))
4331 (artist-funcall exit-fn artist-key-shape x1 y1 x2 y2)
4333 ;; Change state to not drawing
4334 (setq artist-key-is-drawing nil)))))
4337 (defun artist-key-set-point-common (arg)
4338 "Common routine for setting point in current shape.
4339 With non-nil ARG, set the last point."
4340 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
4341 (col (artist-current-column))
4342 (row (artist-current-line))
4343 (was-drawing artist-key-is-drawing))
4345 ;; First, if we are about to draw, then reset the draw-region
4346 (if (not artist-key-is-drawing)
4347 (artist-draw-region-reset))
4349 ;; Depending on what we are currently drawing, call other routines
4350 ;; that knows how to do the job
4352 (cond ((eq draw-how 'artist-do-continously)
4353 (artist-key-set-point-continously col row)
4354 ;; Do this now, otherwise nothing will happen until we move.
4355 (artist-key-do-continously-continously col row))
4356 ((eq draw-how 'artist-do-poly)
4357 (artist-key-set-point-poly col row arg))
4358 ((and (numberp draw-how) (= draw-how 1))
4359 (artist-key-set-point-1point col row))
4360 ((and (numberp draw-how) (= draw-how 2))
4361 (artist-key-set-point-2points col row))
4362 (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
4364 ;; Maybe trim line endings
4365 (if (and artist-trim-line-endings
4366 was-drawing
4367 (not artist-key-is-drawing))
4368 (artist-draw-region-trim-line-endings artist-draw-region-min-y
4369 artist-draw-region-max-y))
4371 ;; Now restore the old position
4373 (artist-move-to-xy col row)
4374 (artist-mode-line-show-curr-operation artist-key-is-drawing)))
4377 ;; Key navigation
4380 (defun artist-previous-line (&optional n)
4381 "Move cursor up optional N lines (default is 1), updating current shape.
4382 If N is negative, move cursor down."
4383 (interactive "p")
4384 (let ((col (artist-current-column)))
4385 (if (not artist-key-is-drawing)
4386 (progn
4387 (previous-line n)
4388 (move-to-column col t))
4389 (previous-line n)
4390 (move-to-column col t)
4391 (artist-key-do-continously-common))))
4394 (defun artist-next-line (&optional n)
4395 "Move cursor down optional N lines (default is 1), updating current shape.
4396 If N is negative, move cursor up."
4397 (interactive "p")
4398 (let ((col (artist-current-column)))
4399 (if (not artist-key-is-drawing)
4400 (progn
4401 (next-line n)
4402 (move-to-column col t))
4403 (next-line n)
4404 (move-to-column col t)
4405 (artist-key-do-continously-common))))
4407 (defun artist-backward-char (&optional n)
4408 "Move cursor backward optional N chars (default is 1), updating curr shape.
4409 If N is negative, move forward."
4410 (interactive "p")
4411 (if (> n 0)
4412 (artist-forward-char (- n))
4413 (artist-forward-char n)))
4415 (defun artist-forward-char (&optional n)
4416 "Move cursor forward optional N chars (default is 1), updating curr shape.
4417 If N is negative, move backward."
4418 (interactive "p")
4419 (let* ((step-x (if (>= n 0) 1 -1))
4420 (distance (abs n))
4421 (curr-col (artist-current-column))
4422 (new-col (max 0 (+ curr-col (* distance step-x)))))
4423 (if (not artist-key-is-drawing)
4424 (move-to-column new-col t)
4425 (move-to-column new-col t)
4426 (artist-key-do-continously-common))))
4429 (defun artist-key-set-point (&optional arg)
4430 "Set a point for the current shape. With optional ARG, set the last point."
4431 (interactive "P")
4432 (artist-key-set-point-common arg))
4435 (defun artist-select-fill-char (c)
4436 "Set current fill character to be C."
4437 (interactive "cType fill char (type RET to turn off): ")
4438 (cond ((eq c ?\r) (setq artist-fill-char-set nil)
4439 (message "Fill cancelled"))
4440 (t (setq artist-fill-char-set t)
4441 (setq artist-fill-char c)
4442 (message "Fill set to \"%c\"" c))))
4445 (defun artist-select-line-char (c)
4446 "Set current line character to be C."
4447 (interactive "cType line char (type RET to turn off): ")
4448 (cond ((eq c ?\r) (setq artist-line-char-set nil)
4449 (message "Normal lines"))
4450 (t (setq artist-line-char-set t)
4451 (setq artist-line-char c)
4452 (message "Line drawn with \"%c\"" c)))
4453 (if artist-key-is-drawing
4454 (artist-key-do-continously-common)))
4457 (defun artist-select-erase-char (c)
4458 "Set current erase character to be C."
4459 (interactive "cType char to use when erasing (type RET for normal): ")
4460 (cond ((eq c ?\r) (setq artist-erase-char ?\ )
4461 (message "Normal erasing"))
4462 (t (setq artist-erase-char c)
4463 (message "Erasing with \"%c\"" c)))
4464 (if artist-key-is-drawing
4465 (artist-key-do-continously-common)))
4467 (defun artist-charlist-to-string (char-list)
4468 "Convert a list of characters, CHAR-LIST, to a string."
4469 (let ((result ""))
4470 (while (not (null char-list))
4471 (setq result (concat result (char-to-string (car char-list))))
4472 (setq char-list (cdr char-list)))
4473 result))
4475 (defun artist-string-to-charlist (str)
4476 "Convert a string, STR, to list of characters."
4477 (append str nil))
4479 (defun artist-select-spray-chars (chars initial-char)
4480 "Set current spray characters to be CHARS, starting with INITIAL-CHAR."
4481 ;; This huge unreadable `interactive'-clause does the following
4482 ;; 1. Asks for a string of spray-characters
4483 ;; 2. Asks for the initial character (default is the first),
4484 ;; and loops if the answer is not a char within the string in 1.
4485 (interactive
4486 (let* ((str (read-string "Select spray-can characters, lightest first: "
4487 (artist-charlist-to-string artist-spray-chars)))
4488 (char-list (artist-string-to-charlist str))
4489 (initial (let* ((err-msg "")
4490 (ok nil)
4491 (first-char-as-str (char-to-string (car char-list)))
4492 (first-s) (first-c))
4493 (while (not ok)
4494 (setq first-s
4495 (read-string
4496 (format (concat "%sSelect initial-character, "
4497 "one of \"%s\" (%s): ")
4498 err-msg str first-char-as-str)))
4499 (if (equal first-s "")
4500 (setq first-s first-char-as-str))
4501 (setq first-c (car (artist-string-to-charlist first-s)))
4502 (setq ok (not (null (member first-c char-list))))
4503 (if (not ok)
4504 (setq err-msg (format
4505 "Not in spray-chars: \"%s\". "
4506 (char-to-string first-c)))))
4507 first-c)))
4508 (list char-list initial)))
4509 (setq artist-spray-chars chars)
4510 (setq artist-spray-new-char initial-char)
4511 (message "Spray-chars set to \"%s\", initial: \"%s\""
4512 (artist-charlist-to-string chars) (char-to-string initial-char)))
4515 (defun artist-select-operation (op-str)
4516 "Select drawing operation OP-STR."
4517 (interactive (list (completing-read "Select operation: "
4518 artist-key-compl-table)))
4519 (let* ((op-symbol (artist-mt-get-symbol-from-keyword op-str))
4520 (draw-how (if op-symbol
4521 (artist-go-get-draw-how-from-symbol op-symbol)
4522 nil)))
4523 ;; First check that the string was valid
4524 (if (null op-symbol)
4525 (error "Unknown drawing method: %s" op-str))
4527 ;; Second, check that we are not about to switch to a different
4528 ;; kind of shape (do that only if we are drawing with keys;
4529 ;; otherwise this function cannot get called).
4530 (if (and artist-key-is-drawing
4531 (not (equal artist-key-draw-how draw-how)))
4532 (error "Cannot switch to a different kind of shape while drawing"))
4534 ;; If we were drawing, undraw the shape
4535 (if (and artist-key-is-drawing
4536 artist-rubber-banding)
4537 (artist-key-undraw-common))
4539 ;; Set the current operation and draw-how
4540 (setq artist-curr-go op-symbol)
4541 (setq artist-key-draw-how draw-how)
4543 ;; If we were drawing, redraw the shape (but don't if shape
4544 ;; is drawn by setting only one point)
4545 (if (and artist-key-is-drawing
4546 artist-rubber-banding
4547 (not (eq artist-key-draw-how 1)))
4548 (artist-key-draw-common)))
4550 ;; Feedback
4551 (artist-mode-line-show-curr-operation artist-key-is-drawing))
4554 (defun artist-toggle-rubber-banding (&optional state)
4555 "Toggle rubber-banding.
4556 If optional argument STATE is positive, turn rubber-banding on."
4557 (interactive)
4558 (if artist-key-is-drawing
4559 (error "Cannot toggle rubber-banding while drawing"))
4560 (if (setq artist-rubber-banding
4561 (if (null state) (not artist-rubber-banding)
4562 (> (prefix-numeric-value state) 0)))
4563 (message "Rubber-banding is now on")
4564 (message "Rubber-banding is now off")))
4567 (defun artist-toggle-trim-line-endings (&optional state)
4568 "Toggle trimming of line-endings.
4569 If optional argument STATE is positive, turn trimming on."
4570 (interactive)
4571 (if (setq artist-trim-line-endings
4572 (if (null state) (not artist-trim-line-endings)
4573 (> (prefix-numeric-value state) 0)))
4574 (message "Trimming is now on")
4575 (message "Trimming is now off")))
4578 (defun artist-toggle-borderless-shapes (&optional state)
4579 "Toggle borders of shapes.
4580 If optional argument STATE is positive, turn borders on."
4581 (interactive)
4582 (if (setq artist-borderless-shapes
4583 (if (null state) (not artist-borderless-shapes)
4584 (> (prefix-numeric-value state) 0)))
4585 (message "Borders are now off")
4586 (message "Borders are now on")))
4589 (defun artist-toggle-first-arrow ()
4590 "Toggle first arrow for shape, if possible."
4591 (interactive)
4592 (save-excursion
4593 (if (not (null artist-arrow-point-1))
4594 (let* ((arrow-point artist-arrow-point-1)
4595 (arrow-state (artist-arrow-point-get-state arrow-point))
4596 (arrow-marker (artist-arrow-point-get-marker arrow-point))
4597 (direction (artist-arrow-point-get-direction arrow-point))
4598 (orig-char (artist-arrow-point-get-orig-char arrow-point))
4599 (arrow-char (aref artist-arrows direction))
4600 (new-state (not arrow-state)))
4602 (goto-char (marker-position arrow-marker))
4604 (if new-state
4605 (if arrow-char
4606 (artist-replace-char arrow-char))
4607 (artist-replace-char orig-char))
4609 (artist-arrow-point-set-state artist-arrow-point-1 new-state)))))
4611 (defun artist-toggle-second-arrow ()
4612 "Toggle second arrow for shape, if possible."
4613 (interactive)
4614 (save-excursion
4615 (if (not (null artist-arrow-point-2))
4616 (let* ((arrow-point artist-arrow-point-2)
4617 (arrow-state (artist-arrow-point-get-state arrow-point))
4618 (arrow-marker (artist-arrow-point-get-marker arrow-point))
4619 (direction (artist-arrow-point-get-direction arrow-point))
4620 (orig-char (artist-arrow-point-get-orig-char arrow-point))
4621 (arrow-char (aref artist-arrows direction))
4622 (new-state (not arrow-state)))
4624 (goto-char (marker-position arrow-marker))
4626 (if new-state
4627 (if arrow-char
4628 (artist-replace-char arrow-char))
4629 (artist-replace-char orig-char))
4631 (artist-arrow-point-set-state artist-arrow-point-2 new-state)))))
4634 (defun artist-select-op-line ()
4635 "Select drawing lines."
4636 (interactive)
4637 (artist-select-operation "line"))
4639 (defun artist-select-op-straight-line ()
4640 "Select drawing straight lines."
4641 (interactive)
4642 (artist-select-operation "straight line"))
4644 (defun artist-select-op-rectangle ()
4645 "Select drawing rectangles."
4646 (interactive)
4647 (artist-select-operation "rectangle"))
4649 (defun artist-select-op-square ()
4650 "Select drawing squares."
4651 (interactive)
4652 (artist-select-operation "square"))
4654 (defun artist-select-op-poly-line ()
4655 "Select drawing poly-lines."
4656 (interactive)
4657 (artist-select-operation "poly-line"))
4659 (defun artist-select-op-straight-poly-line ()
4660 "Select drawing straight poly-lines."
4661 (interactive)
4662 (artist-select-operation "straight poly-line"))
4664 (defun artist-select-op-ellipse ()
4665 "Select drawing ellipses."
4666 (interactive)
4667 (artist-select-operation "ellipse"))
4669 (defun artist-select-op-circle ()
4670 "Select drawing circles."
4671 (interactive)
4672 (artist-select-operation "circle"))
4674 (defun artist-select-op-text-see-thru ()
4675 "Select rendering text (see thru)."
4676 (interactive)
4677 (artist-select-operation "text see-thru"))
4679 (defun artist-select-op-text-overwrite ()
4680 "Select rendering text (overwrite)."
4681 (interactive)
4682 (artist-select-operation "text overwrite"))
4684 (defun artist-select-op-spray-can ()
4685 "Select spraying."
4686 (interactive)
4687 (artist-select-operation "spray-can"))
4689 (defun artist-select-op-spray-set-size ()
4690 "Select setting size for spraying."
4691 (interactive)
4692 (artist-select-operation "spray set size"))
4694 (defun artist-select-op-erase-char ()
4695 "Select erasing characters."
4696 (interactive)
4697 (artist-select-operation "erase char"))
4699 (defun artist-select-op-erase-rectangle ()
4700 "Select erasing rectangles."
4701 (interactive)
4702 (artist-select-operation "erase rectangle"))
4704 (defun artist-select-op-vaporize-line ()
4705 "Select vaporizing single lines."
4706 (interactive)
4707 (artist-select-operation "vaporize line"))
4709 (defun artist-select-op-vaporize-lines ()
4710 "Select vaporizing connected lines."
4711 (interactive)
4712 (artist-select-operation "vaporize lines"))
4714 (defun artist-select-op-cut-rectangle ()
4715 "Select cutting rectangles."
4716 (interactive)
4717 (artist-select-operation "cut rectangle"))
4719 (defun artist-select-op-cut-square ()
4720 "Select cutting squares."
4721 (interactive)
4722 (artist-select-operation "cut square"))
4724 (defun artist-select-op-copy-rectangle ()
4725 "Select copying rectangles."
4726 (interactive)
4727 (artist-select-operation "copy rectangle"))
4729 (defun artist-select-op-copy-square ()
4730 "Select copying squares."
4731 (interactive)
4732 (artist-select-operation "cut square"))
4734 (defun artist-select-op-paste ()
4735 "Select pasting."
4736 (interactive)
4737 (artist-select-operation "paste"))
4739 (defun artist-select-op-flood-fill ()
4740 "Select flood-filling."
4741 (interactive)
4742 (artist-select-operation "flood-fill"))
4745 ;; Drawing lines by using mouse
4746 ;; Mouse button actions
4749 (defun artist-update-pointer-shape ()
4750 "Perform the update of the X Windows pointer shape."
4751 (set-mouse-color nil))
4753 (defun artist-set-pointer-shape (new-pointer-shape)
4754 "Set the shape of the X Windows pointer to NEW-POINTER-SHAPE."
4755 (setq x-pointer-shape new-pointer-shape)
4756 (artist-update-pointer-shape))
4758 (defsubst artist-event-is-shifted (ev)
4759 "Check whether the shift-key is pressed in event EV."
4760 (memq 'shift (event-modifiers ev)))
4762 (defun artist-do-nothing ()
4763 "Function that does nothing."
4764 (interactive))
4766 (defun artist-down-mouse-1 (ev)
4767 "Perform drawing action for event EV."
4768 (interactive "@e")
4769 (let* ((real (artist-go-get-symbol-shift
4770 artist-curr-go (artist-event-is-shifted ev)))
4771 (draw-how (artist-go-get-draw-how-from-symbol real))
4772 ;; Remember original values for draw-region-min-y and max-y
4773 ;; in case we are interrupting a key-draw operation.
4774 (orig-draw-region-min-y artist-draw-region-min-y)
4775 (orig-draw-region-max-y artist-draw-region-max-y)
4776 (orig-pointer-shape (if (eq window-system 'x) x-pointer-shape nil))
4777 (echo-keystrokes 10000) ; a lot of seconds
4778 ;; Remember original binding for the button-up event to this
4779 ;; button-down event.
4780 (key (let* ((basic (event-basic-type ev))
4781 (unshifted basic)
4782 (shifted (make-symbol (concat "S-" (symbol-name basic)))))
4783 (if (artist-event-is-shifted ev)
4784 (make-vector 1 shifted)
4785 (make-vector 1 unshifted))))
4786 (orig-button-up-binding (lookup-key (current-global-map) key)))
4788 (unwind-protect
4789 (progn
4790 (if (eq window-system 'x)
4791 (artist-set-pointer-shape artist-pointer-shape))
4793 ;; Redefine the button-up binding temporarily (the original
4794 ;; binding is restored in the unwind-forms below). This is to
4795 ;; avoid the phenomenon outlined in this scenario:
4797 ;; 1. A routine which reads something from the mini-buffer (such
4798 ;; as the text renderer) is called from below.
4799 ;; 2. Meanwhile, the users releases the mouse button.
4800 ;; 3. As a (funny :-) coincidence, the binding for the
4801 ;; button-up event is often mouse-set-point, so Emacs
4802 ;; sets the point to where the button was released, which is
4803 ;; in the buffer where the user wants to place the text.
4804 ;; 4. The user types C-x o (or uses the mouse once again)
4805 ;; until he reaches the mini-buffer which is still prompting
4806 ;; for some text to render.
4808 ;; To do this foolproof, all local and minor-mode maps should
4809 ;; be searched and temporarily changed as well, since they
4810 ;; too might have some binding for the button-up event,
4811 ;; but I hope dealing with the global map will suffice.
4812 (define-key (current-global-map) key 'artist-do-nothing)
4814 (artist-draw-region-reset)
4816 (artist-mode-line-show-curr-operation t)
4818 (cond ((eq draw-how 'artist-do-continously)
4819 (artist-mouse-draw-continously ev))
4820 ((eq draw-how 'artist-do-poly)
4821 (artist-mouse-draw-poly ev))
4822 ((and (numberp draw-how) (= draw-how 1))
4823 (artist-mouse-draw-1point ev))
4824 ((and (numberp draw-how) (= draw-how 2))
4825 (artist-mouse-draw-2points ev))
4826 (t (message "Drawing \"%s\"s is not yet implemented"
4827 draw-how)))
4829 (if artist-trim-line-endings
4830 (artist-draw-region-trim-line-endings artist-draw-region-min-y
4831 artist-draw-region-max-y))
4832 (setq artist-draw-region-min-y orig-draw-region-min-y)
4833 (setq artist-draw-region-max-y orig-draw-region-max-y))
4835 ; This is protected
4836 (if (eq window-system 'x)
4837 (artist-set-pointer-shape orig-pointer-shape))
4839 (if orig-button-up-binding
4840 (define-key (current-global-map) key orig-button-up-binding))
4842 (artist-mode-line-show-curr-operation artist-key-is-drawing))))
4845 (defun artist-mouse-choose-operation (ev op)
4846 "Choose operation for event EV and operation OP."
4847 (interactive
4848 (progn
4849 (select-window (posn-window (event-start last-input-event)))
4850 (list last-input-event
4851 (x-popup-menu last-nonmenu-event artist-popup-menu-table))))
4853 (let ((draw-fn (artist-go-get-draw-fn-from-symbol (car op)))
4854 (set-fn (artist-fc-get-fn-from-symbol (car op))))
4855 (cond
4857 ;; *** It was a draw-function
4858 ((not (listp draw-fn))
4859 (let* ((unshifted (artist-go-get-symbol-shift (car op) nil))
4860 (shifted (artist-go-get-symbol-shift (car op) t))
4861 (shift-state (artist-event-is-shifted ev))
4862 (selected-op (if shift-state shifted unshifted))
4863 (keyword (artist-go-get-keyword-from-symbol selected-op)))
4864 (artist-select-operation keyword)))
4866 ;; *** It was a set/unset function
4867 ((not (listp set-fn))
4868 (call-interactively set-fn)))))
4871 (defun artist-down-mouse-3 (ev)
4872 "Erase character or rectangle, depending on event EV."
4873 (interactive "@e")
4874 (let ((artist-curr-go 'erase-char))
4875 (artist-down-mouse-1 ev))
4876 ;; Restore mode-line
4877 (artist-mode-line-show-curr-operation artist-key-is-drawing))
4881 ;; Mouse routines
4884 (defsubst artist-shift-has-changed (shift-state ev)
4885 "From the last SHIFT-STATE and EV, determine if the shift-state has changed."
4886 ;; This one simply doesn't work.
4888 ;; There seems to be no way to tell whether the user has pressed shift
4889 ;; while dragging the cursor around when we are in a track-mouse
4890 ;; form. Calling (event-modifiers ev) yields nil :-( Neither is the
4891 ;; (event-basic-type ev) of any help (it is simply `mouse-movement').
4893 ;; So this doesn't work:
4894 ;; (cond ((and shift-state (not (artist-event-is-shifted ev))) t)
4895 ;; ((and (not shift-state) (artist-event-is-shifted ev)) t)
4896 ;; (t nil))
4897 nil)
4899 (defun artist-coord-win-to-buf (coord)
4900 "Convert a window-relative coordinate COORD to a buffer-relative coordinate."
4901 (let ((window-x (car coord))
4902 (window-y (cdr coord))
4903 (window-start-x (window-hscroll))
4904 (window-start-y (save-excursion (goto-char (window-start))
4905 (artist-current-line))))
4906 (cons (+ window-x window-start-x)
4907 (+ window-y window-start-y))))
4910 (defun artist-mouse-draw-continously (ev)
4911 "Generic function for shapes that requires 1 point as input.
4912 Operation is done continously while the mouse button is hold down.
4913 The event, EV, is the mouse event."
4914 (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
4915 (shifted (artist-go-get-symbol-shift artist-curr-go t))
4916 (shift-state (artist-event-is-shifted ev))
4917 (op (if shift-state shifted unshifted))
4918 (draw-how (artist-go-get-draw-how-from-symbol op))
4919 (init-fn (artist-go-get-init-fn-from-symbol op))
4920 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
4921 (exit-fn (artist-go-get-exit-fn-from-symbol op))
4922 (draw-fn (artist-go-get-draw-fn-from-symbol op))
4923 (interval-fn (artist-go-get-interval-fn-from-symbol op))
4924 (interval (artist-funcall interval-fn))
4925 (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
4926 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
4927 (ev-start (event-start ev))
4928 (initial-win (posn-window ev-start))
4929 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
4930 (x1 (car ev-start-pos))
4931 (y1 (cdr ev-start-pos))
4932 (shape)
4933 (timer))
4934 (select-window (posn-window ev-start))
4935 (artist-funcall init-fn x1 y1)
4936 (if (not artist-rubber-banding)
4937 (artist-no-rb-set-point1 x1 y1))
4938 (track-mouse
4939 (while (or (mouse-movement-p ev)
4940 (member 'down (event-modifiers ev)))
4941 (setq ev-start-pos (artist-coord-win-to-buf
4942 (posn-col-row (event-start ev))))
4943 (setq x1 (car ev-start-pos))
4944 (setq y1 (cdr ev-start-pos))
4946 ;; Cancel previous timer
4947 (if timer
4948 (cancel-timer timer))
4950 (if (not (eq initial-win (posn-window (event-start ev))))
4951 ;; If we moved outside the window, do nothing
4954 ;; Still in same window:
4956 ;; Check if user presses or releases shift key
4957 (if (artist-shift-has-changed shift-state ev)
4959 ;; First check that the draw-how is the same as we
4960 ;; already have. Otherwise, ignore the changed shift-state.
4961 (if (not (eq draw-how
4962 (artist-go-get-draw-how-from-symbol
4963 (if (not shift-state) shifted unshifted))))
4964 (message "Cannot switch to shifted operation")
4966 ;; progn is "implicit" since this is the else-part
4967 (setq shift-state (not shift-state))
4968 (setq op (if shift-state shifted unshifted))
4969 (setq draw-how (artist-go-get-draw-how-from-symbol op))
4970 (setq draw-fn (artist-go-get-draw-fn-from-symbol op))))
4972 ;; Draw the new shape
4973 (setq shape (artist-funcall draw-fn x1 y1))
4974 (artist-move-to-xy x1 y1)
4976 ;; Start the timer to call `draw-fn' repeatedly every
4977 ;; `interval' second
4978 (if (and interval draw-fn)
4979 (setq timer (run-at-time interval interval draw-fn x1 y1))))
4981 ;; Read next event
4982 (setq ev (read-event))))
4984 ;; Cancel any timers
4985 (if timer
4986 (cancel-timer timer))
4988 (artist-funcall prep-fill-fn x1 y1)
4990 (if (artist-funcall arrow-pred)
4991 (artist-funcall arrow-set-fn x1 y1)
4992 (artist-clear-arrow-points))
4994 (artist-funcall exit-fn x1 y1)
4995 (artist-move-to-xy x1 y1)))
4999 (defun artist-mouse-draw-poly (ev)
5000 "Generic function for shapes requiring several points as input.
5001 The event, EV, is the mouse event."
5002 (interactive "@e")
5003 (message "Mouse-1: set new point, mouse-2: set last point")
5004 (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
5005 (shifted (artist-go-get-symbol-shift artist-curr-go t))
5006 (shift-state (artist-event-is-shifted ev))
5007 (op (if shift-state shifted unshifted))
5008 (draw-how (artist-go-get-draw-how-from-symbol op))
5009 (init-fn (artist-go-get-init-fn-from-symbol op))
5010 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
5011 (exit-fn (artist-go-get-exit-fn-from-symbol op))
5012 (draw-fn (artist-go-get-draw-fn-from-symbol op))
5013 (undraw-fn (artist-go-get-undraw-fn-from-symbol op))
5014 (fill-pred (artist-go-get-fill-pred-from-symbol op))
5015 (fill-fn (artist-go-get-fill-fn-from-symbol op))
5016 (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
5017 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
5018 (ev-start (event-start ev))
5019 (initial-win (posn-window ev-start))
5020 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
5021 (x1-last (car ev-start-pos))
5022 (y1-last (cdr ev-start-pos))
5023 (x2 x1-last)
5024 (y2 y1-last)
5025 (is-down t)
5026 (shape nil)
5027 (point-list nil)
5028 (done nil))
5029 (select-window (posn-window ev-start))
5030 (artist-funcall init-fn x1-last y1-last)
5031 (if (not artist-rubber-banding)
5032 (artist-no-rb-set-point1 x1-last y1-last))
5033 (track-mouse
5034 (while (not done)
5035 ;; decide what to do
5036 (cond
5038 ;; *** Mouse button is released.
5039 ((and is-down
5040 (or (member 'click (event-modifiers ev))
5041 (member 'drag (event-modifiers ev))))
5042 ;; First, if not rubber-banding, draw the line.
5044 (if (not artist-rubber-banding)
5045 (progn
5046 (artist-no-rb-unset-points)
5047 (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))))
5049 ;; Set the second point to the shape's second point
5050 ;; (which might be different from the mouse's second point,
5051 ;; if, for example, we are drawing a straight line)
5053 (if (not (null shape))
5054 (let ((endpoint2 (artist-2point-get-endpoint2 shape)))
5055 (setq x1-last (artist-endpoint-get-x endpoint2))
5056 (setq y1-last (artist-endpoint-get-y endpoint2))))
5057 (setq point-list (cons (artist-make-endpoint x1-last y1-last)
5058 point-list))
5059 (setq shape nil)
5060 (setq is-down nil))
5062 ;; *** Mouse button 2 or 3 down
5063 ((and (member 'down (event-modifiers ev))
5064 (or (equal (event-basic-type ev) 'mouse-2)
5065 (equal (event-basic-type ev) 'mouse-3)))
5066 ;; Ignore
5067 nil)
5069 ;; *** Mouse button 2 or 3 released
5070 ((and (or (member 'click (event-modifiers ev))
5071 (member 'drag (event-modifiers ev)))
5072 (or (equal (event-basic-type ev) 'mouse-2)
5073 (equal (event-basic-type ev) 'mouse-3)))
5075 ;; This means the end of our poly-line drawing-session.
5077 (setq done t))
5079 ;; *** Mouse button 1 went down
5080 ((and (not is-down)
5081 (member 'down (event-modifiers ev))
5082 (equal (event-basic-type ev) 'mouse-1))
5083 ;; Check whether the (possibly new, that depends on if shift
5084 ;; has been pressed or released) symbol has the same draw-how
5085 ;; information as the previous had. If it hasn't, we can't
5086 ;; proceed.
5088 (if (not (eq draw-how
5089 (artist-go-get-draw-how-from-symbol
5090 (if (not shift-state) shifted unshifted))))
5091 (message "Cannot switch operation")
5092 (progn
5093 ;; Decide operation
5095 (setq unshifted
5096 (artist-go-get-symbol-shift artist-curr-go nil)
5097 shifted
5098 (artist-go-get-symbol-shift artist-curr-go t)
5099 shift-state (artist-event-is-shifted ev)
5100 op (if shift-state shifted unshifted)
5101 draw-how (artist-go-get-draw-how-from-symbol op)
5102 draw-fn (artist-go-get-draw-fn-from-symbol op)
5103 undraw-fn (artist-go-get-undraw-fn-from-symbol op)
5104 fill-pred (artist-go-get-fill-pred-from-symbol op)
5105 fill-fn (artist-go-get-fill-fn-from-symbol op))
5107 ;; Draw shape from last place to this place
5109 ;; set x2 and y2
5111 (setq ev-start-pos (artist-coord-win-to-buf
5112 (posn-col-row (event-start ev))))
5113 (setq x2 (car ev-start-pos))
5114 (setq y2 (cdr ev-start-pos))
5116 ;; Draw the new shape (if not rubber-banding, place both marks)
5118 (if artist-rubber-banding
5119 (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))
5120 (progn
5121 (artist-no-rb-set-point1 x1-last y1-last)
5122 (artist-no-rb-set-point2 x2 y2)))
5124 ;; Show new operation in mode-line
5125 (let ((artist-curr-go op))
5126 (artist-mode-line-show-curr-operation t))))
5128 (setq is-down t))
5131 ;; *** Mouse moved, button is down and we are still in orig window
5132 ((and (mouse-movement-p ev)
5133 is-down
5134 (eq initial-win (posn-window (event-start ev))))
5135 ;; Draw shape from last place to this place
5137 ;; set x2 and y2
5138 (setq ev-start-pos (artist-coord-win-to-buf
5139 (posn-col-row (event-start ev))))
5140 (setq x2 (car ev-start-pos))
5141 (setq y2 (cdr ev-start-pos))
5143 ;; First undraw last shape
5144 ;; (unset last point if not rubberbanding)
5146 (artist-funcall undraw-fn shape)
5148 ;; Draw the new shape (if not rubberbanding, set 2nd mark)
5150 (if artist-rubber-banding
5151 (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))
5152 (progn
5153 (artist-no-rb-unset-point2)
5154 (artist-no-rb-set-point2 x2 y2)))
5155 ;; Move cursor
5156 (artist-move-to-xy x2 y2))
5158 ;; *** Mouse moved, button is down but we are NOT in orig window
5159 ((and (mouse-movement-p ev)
5160 is-down
5161 (not (eq initial-win (posn-window (event-start ev)))))
5162 ;; Ignore
5163 nil)
5166 ;; *** Moving mouse while mouse button is not down
5167 ((and (mouse-movement-p ev) (not is-down))
5168 ;; don't do anything.
5169 nil)
5172 ;; *** Mouse button 1 went down, first time
5173 ((and is-down
5174 (member 'down (event-modifiers ev))
5175 (equal (event-basic-type ev) 'mouse-1))
5176 ;; don't do anything
5177 nil)
5180 ;; *** Another event
5182 ;; End drawing
5184 (setq done t)))
5186 ;; Read next event (only if we should not stop)
5187 (if (not done)
5188 (setq ev (read-event)))))
5190 ;; Reverse point-list (last points are cond'ed first)
5191 (setq point-list (reverse point-list))
5193 (artist-funcall prep-fill-fn point-list)
5195 ;; Maybe fill
5196 (if (artist-funcall fill-pred)
5197 (artist-funcall fill-fn point-list))
5199 ;; Maybe set arrow points
5200 (if (and point-list (artist-funcall arrow-pred))
5201 (artist-funcall arrow-set-fn point-list)
5202 (artist-clear-arrow-points))
5204 (artist-funcall exit-fn point-list)
5205 (artist-move-to-xy x2 y2)))
5208 (defun artist-mouse-draw-1point (ev)
5209 "Generic function for shapes requiring only 1 point as input.
5210 Operation is done once. The event, EV, is the mouse event."
5211 (interactive "@e")
5212 (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
5213 (shifted (artist-go-get-symbol-shift artist-curr-go t))
5214 (shift-state (artist-event-is-shifted ev))
5215 (op (if shift-state shifted unshifted))
5216 (draw-how (artist-go-get-draw-how-from-symbol op))
5217 (init-fn (artist-go-get-init-fn-from-symbol op))
5218 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
5219 (exit-fn (artist-go-get-exit-fn-from-symbol op))
5220 (draw-fn (artist-go-get-draw-fn-from-symbol op))
5221 (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
5222 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
5223 (ev-start (event-start ev))
5224 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
5225 (x1 (car ev-start-pos))
5226 (y1 (cdr ev-start-pos)))
5227 (select-window (posn-window ev-start))
5228 (artist-funcall init-fn x1 y1)
5229 (artist-funcall draw-fn x1 y1)
5230 (artist-funcall prep-fill-fn x1 y1)
5231 (if (artist-funcall arrow-pred)
5232 (artist-funcall arrow-set-fn x1 y1)
5233 (artist-clear-arrow-points))
5234 (artist-funcall exit-fn x1 y1)
5235 (artist-move-to-xy x1 y1)))
5238 (defun artist-mouse-draw-2points (ev)
5239 "Generic function for shapes requiring 2 points as input.
5240 The event, EV, is the mouse event."
5241 (interactive "@e")
5242 (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
5243 (shifted (artist-go-get-symbol-shift artist-curr-go t))
5244 (shift-state (artist-event-is-shifted ev))
5245 (op (if shift-state shifted unshifted))
5246 (draw-how (artist-go-get-draw-how-from-symbol op))
5247 (init-fn (artist-go-get-init-fn-from-symbol op))
5248 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
5249 (exit-fn (artist-go-get-exit-fn-from-symbol op))
5250 (draw-fn (artist-go-get-draw-fn-from-symbol op))
5251 (undraw-fn (artist-go-get-undraw-fn-from-symbol op))
5252 (fill-pred (artist-go-get-fill-pred-from-symbol op))
5253 (fill-fn (artist-go-get-fill-fn-from-symbol op))
5254 (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
5255 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
5256 (ev-start (event-start ev))
5257 (initial-win (posn-window ev-start))
5258 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
5259 (x1 (car ev-start-pos))
5260 (y1 (cdr ev-start-pos))
5261 (x2)
5262 (y2)
5263 (shape))
5264 (select-window (posn-window ev-start))
5265 (artist-funcall init-fn x1 y1)
5266 (if (not artist-rubber-banding)
5267 (artist-no-rb-set-point1 x1 y1))
5268 (track-mouse
5269 (while (or (mouse-movement-p ev)
5270 (member 'down (event-modifiers ev)))
5271 (setq ev-start-pos (artist-coord-win-to-buf
5272 (posn-col-row (event-start ev))))
5273 (setq x2 (car ev-start-pos))
5274 (setq y2 (cdr ev-start-pos))
5276 (if (not (eq initial-win (posn-window (event-start ev))))
5277 ;; If we moved outside the window, do nothing
5280 ;; Still in same window:
5282 ;; First undraw last shape (unset last point if not rubberbanding)
5283 (if artist-rubber-banding
5284 (artist-funcall undraw-fn shape)
5285 (artist-no-rb-unset-point2))
5287 ;; Check if user presses or releases shift key
5288 (if (artist-shift-has-changed shift-state ev)
5290 ;; First check that the draw-how is the same as we
5291 ;; already have. Otherwise, ignore the changed shift-state.
5292 (if (not (eq draw-how
5293 (artist-go-get-draw-how-from-symbol
5294 (if (not shift-state) shifted unshifted))))
5295 (message "Cannot switch to shifted operation")
5297 (message "Switching")
5298 ;; progn is "implicit" since this is the else-part
5299 (setq shift-state (not shift-state))
5300 (setq op (if shift-state shifted unshifted))
5301 (setq draw-how (artist-go-get-draw-how-from-symbol op))
5302 (setq draw-fn (artist-go-get-draw-fn-from-symbol op))
5303 (setq undraw-fn (artist-go-get-undraw-fn-from-symbol op))
5304 (setq fill-pred (artist-go-get-fill-pred-from-symbol op))
5305 (setq fill-fn (artist-go-get-fill-fn-from-symbol op))))
5307 ;; Draw the new shape
5308 (if artist-rubber-banding
5309 (setq shape (artist-funcall draw-fn x1 y1 x2 y2))
5310 (artist-no-rb-set-point2 x2 y2))
5311 ;; Move cursor
5312 (artist-move-to-xy x2 y2))
5315 ;; Read next event
5316 (setq ev (read-event))))
5318 ;; If we are not rubber-banding (that is, we were moving around the `2')
5319 ;; draw the shape
5320 (if (not artist-rubber-banding)
5321 (progn
5322 (artist-no-rb-unset-points)
5323 (setq shape (artist-funcall draw-fn x1 y1 x2 y2))))
5325 (artist-funcall prep-fill-fn shape x1 y1 x2 y2)
5327 ;; Maybe fill
5328 (if (artist-funcall fill-pred)
5329 (artist-funcall fill-fn shape x1 y1 x2 y2))
5331 ;; Maybe set arrow-points
5332 (if (artist-funcall arrow-pred)
5333 (artist-funcall arrow-set-fn shape x1 y1 x2 y2)
5334 (artist-clear-arrow-points))
5336 (artist-funcall exit-fn shape x1 y1 x2 y2)
5337 (artist-move-to-xy x2 y2)))
5341 ;; Bug-report-submitting
5343 (defun artist-submit-bug-report ()
5344 "Submit via mail a bug report on Artist."
5345 (interactive)
5346 (require 'reporter)
5347 (if (y-or-n-p "Do you want to submit a bug report on Artist? ")
5348 (let ((to artist-maintainer-address)
5349 (vars '(window-system
5350 window-system-version
5352 artist-rubber-banding
5353 artist-interface-with-rect
5354 artist-aspect-ratio
5355 ;; Now the internal ones
5356 artist-curr-go
5357 artist-key-poly-point-list
5358 artist-key-shape
5359 artist-key-draw-how
5360 artist-arrow-point-1
5361 artist-arrow-point-2)))
5362 ;; Remove those variables from vars that are not bound
5363 (mapcar
5364 (function
5365 (lambda (x)
5366 (if (not (and (boundp x) (symbol-value x)))
5367 (setq vars (delq x vars))))) vars)
5368 (reporter-submit-bug-report
5369 artist-maintainer-address
5370 (concat "artist.el " artist-version)
5371 vars
5372 nil nil
5373 (concat "Hello Tomas,\n\n"
5374 "I have a nice bug report on Artist for you! Here it is:")))))
5378 ;; Now provide this minor mode
5381 (provide 'artist)
5384 ;;; About adding drawing modes
5385 ;;; --------------------------
5387 ;; If you are going to add a new drawing mode, read the following
5388 ;; sketchy outlines to get started a bit easier.
5390 ;; 1. If your new drawing mode falls into one of the following
5391 ;; categories, goto point 2, otherwise goto point 3.
5393 ;; - Modes where the shapes are drawn continously, as long as
5394 ;; the mouse button is held down (continous modes).
5395 ;; Example: the erase-char mode, the pen and pen-line modes.
5397 ;; - Modes where the shape is made up of from 2 points to an
5398 ;; arbitrary number of points (poly-point modes).
5399 ;; Example: the poly-line mode
5401 ;; - Modes where the shape is made up of 2 points (2-point
5402 ;; modes).
5403 ;; Example: lines, rectangles
5405 ;; - Modes where the shape is made up of 1 point (1-point
5406 ;; modes). This mode differs from the continous modes in
5407 ;; that the shape is drawn only once when the mouse button
5408 ;; is pressed.
5409 ;; Examples: paste, a flood-fill, vaporize modes
5412 ;; 2. To make it easier and more flexible to program new drawing
5413 ;; modes, you might choose to specify
5414 ;; init-fn: a function to be called at the very beginning
5415 ;; of the drawing phase,
5416 ;; prep-fill-fn: a function to be called before filling,
5417 ;; arrow-set-fn: a function for setting arrows, to be called
5418 ;; after filling, and
5419 ;; exit-fn: a function to be called at the very end of
5420 ;; the drawing phase.
5421 ;; For each of the cases below, the arguments given to the init-fn,
5422 ;; prep-fill-fn, arrow-set-fn and exit-fn are stated.
5424 ;; If your mode matches the continous mode or the 1-point mode:
5426 ;; a. Create a draw-function that draws your shape. Your function
5427 ;; must take x and y as arguments. The return value is not
5428 ;; used.
5430 ;; b. Add your mode to the master table, `artist-mt'.
5432 ;; init-fn: x y
5433 ;; prep-fill-fn: x y
5434 ;; arrow-set-fn: x y
5435 ;; exit-fn: x y
5437 ;; If your mode matches the 2-point mode:
5439 ;; a. Create one draw-function that draws your shape and one
5440 ;; undraw-function that undraws it.
5442 ;; The draw-function must take x1, y1, x2 and y2 as
5443 ;; arguments. It must return a list with three elements:
5444 ;; Endpoint1: a vector [x1 y1]
5445 ;; Endpoint2: a vector [x2 y2]
5446 ;; Shapeinfo: all info necessary for your undraw-function to
5447 ;; be able to undraw the shape
5448 ;; Use the artist-endpoint-* accessors to create and inspect
5449 ;; the endpoints.
5451 ;; If applicable, you must be able to draw your shape without
5452 ;; borders if the `artist-borderless-shapes' is non-nil.
5453 ;; See `artist-draw-rect' for an example.
5455 ;; The undraw-function must take one argument: the list created
5456 ;; by your draw-function. The return value is not used.
5458 ;; b. If you want to provide a fill-function, then create a
5459 ;; function that takes 5 arguments: the list created by your
5460 ;; draw-function, x1, y1, x2 and y2. The return value is not
5461 ;; used.
5463 ;; c. Add your mode to the master table, `artist-mt'.
5465 ;; init-fn: x1 y1
5466 ;; prep-fill-fn: shape x1 y1 x2 y2
5467 ;; arrow-set-fn: shape x1 y1 x2 y2
5468 ;; exit-fn: shape x1 y1 x2 y2
5470 ;; If your mode matches the poly-point mode:
5472 ;; a. Create one draw-function that draws your shape and one
5473 ;; undraw-function that undraws it. The draw- and
5474 ;; undraw-functions are used to draw/undraw a segment of
5475 ;; your poly-point mode between 2 points. The draw- and
5476 ;; undraw-functions are then really 2-point mode functions.
5477 ;; They must take the same arguments and return the same
5478 ;; values as those of the 2-point mode.
5480 ;; If applicable, you must be able to draw your shape without
5481 ;; borders if the `artist-borderless-shapes' is non-nil.
5482 ;; See `artist-draw-rect' for an example.
5484 ;; b. If you want to provide a fill-function, then create a
5485 ;; function that takes 1 argument: a list of points where each
5486 ;; point is a vector, [x, y].
5488 ;; c. Add your mode to the master table, `artist-mt'.
5490 ;; init-fn: x1 y1
5491 ;; prep-fill-fn: point-list
5492 ;; arrow-set-fn: point-list
5493 ;; exit-fn: point-list
5495 ;; The arrow-set-fn must set the variables `artist-arrow-point-1'
5496 ;; and `artist-arrow-point-2'. If your mode does not take arrows,
5497 ;; you must set the variables to nil. Use the accessors
5498 ;; artist-arrow-point-* to create and inspect arrow-points.
5501 ;; 3. If your mode doesn't match any of the categories, you are facing
5502 ;; a bit more work, and I cannot be as detailed as above. Here is a
5503 ;; brief outline of what you have to do:
5505 ;; a. Decide on a name for your type of mode. Let's assume that
5506 ;; you decided on `xxx'. Then you should use the draw-how
5507 ;; symbol artist-do-xxx.
5509 ;; b. Create a function artist-mouse-draw-xxx for drawing with
5510 ;; mouse. It should be called from `artist-down-mouse-1'.
5512 ;; The all coordinates must be converted from window-relative
5513 ;; to buffer relative before saved or handed over to
5514 ;; any other function. Converting is done with
5515 ;; the function `artist-coord-win-to-buf'.
5517 ;; It must take care to the `artist-rubber-banding' variable
5518 ;; and perform rubber-banding accordingly. Use the
5519 ;; artist-no-rb-* functions if not rubber-banding.
5521 ;; If applicable, you must be able to draw your shape without
5522 ;; borders if the `artist-borderless-shapes' is non-nil.
5523 ;; See `artist-draw-rect' for an example.
5525 ;; You must call the init-fn, the prep-fill-fn, arrow-set-fn
5526 ;; and the exit-fn at the apropriate points.
5528 ;; When artist-mouse-draw-xxx ends, the shape for your mode
5529 ;; must be completely drawn.
5531 ;; c. Create functions for drawing with keys:
5533 ;; - artist-key-set-point-xxx for setting a point in the
5534 ;; mode, to be called from `artist-key-set-point-common'.
5536 ;; - artist-key-do-continously-xxx to be called from
5537 ;; `artist-key-do-continously-common' whenever the user
5538 ;; moves around.
5540 ;; As for the artist-mouse-draw-xxx, these two functions must
5541 ;; take care to do rubber-banding, borderless shapes and to
5542 ;; set arrows.
5544 ;; These functions should set the variable `artist-key-shape'
5545 ;; to the shape drawn.
5547 ;; d. Create artist-key-draw-xxx and artist-key-undraw-xxx for
5548 ;; drawing and undrawing. These are needed when the user
5549 ;; switches operation to draw another shape of the same type
5550 ;; of drawing mode.
5552 ;; You should provide these functions. You might think that
5553 ;; only you is using your type of mode, so noone will be able
5554 ;; to switch to another operation of the same type of mode,
5555 ;; but someone else might base a new drawing mode upon your
5556 ;; work.
5558 ;; You must call the init-fn, the prep-fill-fn, arrow-set-fn
5559 ;; and the exit-fn at the apropriate points.
5561 ;; e. Add your new mode to the master table, `artist-mt'.
5564 ;; Happy hacking! Please let me hear if you add any drawing modes!
5565 ;; Don't hesitate to ask me any questions.
5568 ;;; arch-tag: 3e63b881-aaaa-4b83-a072-220d4661a8a3
5569 ;;; artist.el ends here