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