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