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