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