all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 930c39c82ccc905938fa25167215df943ddf9671 194966 bytes (raw)
name: lisp/textmodes/artist.el 	 # note: path name is non-authoritative(*)

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
 
;;; artist.el --- draw ascii graphics with your mouse

;; Copyright (C) 2000-2015 Free Software Foundation, Inc.

;; Author:       Tomas Abrahamsson <tab@lysator.liu.se>
;; Maintainer:   Tomas Abrahamsson <tab@lysator.liu.se>
;; Keywords:     mouse
;; Version:	 1.2.6
;; Release-date: 6-Aug-2004
;; Location:     http://www.lysator.liu.se/~tab/artist/

;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
;; file on 19/3/2008, and the maintainer agreed that when a bug is filed in
;; the Emacs bug reporting system against this file, a copy of the bug
;; report be sent to the maintainer's email address.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; What is artist?
;; ---------------
;;
;; Artist is an Emacs lisp package that allows you to draw lines,
;; rectangles and ellipses by using your mouse and/or keyboard.  The
;; shapes are made up with the ascii characters |, -, / and \.
;;
;; Features are:
;;
;; * Intersecting: When a `|' intersects with a `-', a `+' is
;;   drawn, like this:    |        \ /
;;                      --+--       X
;;                        |        / \
;;
;; * Rubber-banding: When drawing lines you can interactively see the
;;   result while holding the mouse button down and moving the mouse.  If
;;   your machine is not fast enough (a 386 is a bit to slow, but a
;;   pentium is well enough), you can turn this feature off.  You will
;;   then see 1's and 2's which mark the 1st and 2nd endpoint of the line
;;   you are drawing.
;;
;; * Drawing operations: The following drawing operations are implemented:
;;
;;     lines                    straight-lines
;;     rectangles               squares
;;     poly-lines               straight poly-lines
;;     ellipses                 circles
;;     text (see-thru)          text (overwrite)
;;     spray-can                setting size for spraying
;;     vaporize line            vaporize lines
;;     erase characters         erase rectangles
;;
;;   Straight lines are lines that go horizontally, vertically or
;;   diagonally.  Plain lines go in any direction.  The operations in
;;   the right column are accessed by holding down the shift key while
;;   drawing.
;;
;;   It is possible to vaporize (erase) entire lines and connected lines
;;   (rectangles for example) as long as the lines being vaporized are
;;   straight and connected at their endpoints.  Vaporizing is inspired
;;   by the drawrect package by Jari Aalto <jari.aalto@poboxes.com>.
;;
;; * Flood-filling: You can fill any area with a certain character by
;;   flood-filling.
;;
;; * Cut copy and paste: You can cut, copy and paste rectangular
;;   regions.  Artist also interfaces with the rect package (this can be
;;   turned off if it causes you any trouble) so anything you cut in
;;   artist can be yanked with C-x r y and vice versa.
;;
;; * Drawing with keys: Everything you can do with the mouse, you can
;;   also do without the mouse.
;;
;; * Arrows: After having drawn a (straight) line or a (straight)
;;   poly-line, you can set arrows on the line-ends by typing < or >.
;;
;; * Aspect-ratio: You can set the variable artist-aspect-ratio to
;;   reflect the height-width ratio for the font you are using.  Squares
;;   and circles are then drawn square/round.  Note, that once your
;;   ascii-file is shown with font with a different height-width ratio,
;;   the squares won't be square and the circles won't be round.
;;
;; * Picture mode compatibility: Artist is picture mode compatible (this
;;   can be turned off).
;;
;; See the documentation for the function artist-mode for a detailed
;; description on how to use artist.
;;
;;
;; What about adding my own drawing modes?
;; ---------------------------------------
;;
;; See the short guide at the end of this file.
;; If you add a new drawing mode, send it to me, and I would gladly
;; include in the next release!

;;; Installation:

;; To use artist, put this in your .emacs:
;;
;;    (autoload 'artist-mode "artist" "Enter artist-mode" t)


;;; Requirements:

;; Artist requires Emacs 19.28 or higher.
;;
;; Artist requires the `rect' package (which comes with Emacs) to be
;; loadable, unless the variable `artist-interface-with-rect' is set
;; to nil.
;;
;; Artist also requires the Picture mode (which also comes with Emacs)
;; to be loadable, unless the variable `artist-picture-compatibility'
;; is set to nil.

;;; Known bugs:

;; The shifted operations are not available when drawing with the mouse
;; in Emacs 19.29 and 19.30.
;;
;; It is not possible to change between shifted and unshifted operation
;; while drawing with the mouse. (See the comment in the function
;; artist-shift-has-changed for further details.)


;;; ChangeLog:

;; 1.2.6	6-Aug-2004
;; New:		Coerced with the artist.el that's in Emacs-21.3.
;;              (minor editorial changes)
;;
;; 1.2.5	4-Aug-2004
;; New:		Added tool selection via the mouse-wheel
;;		Function provided by Andreas Leue <al@sphenon.de>
;;
;; 1.2.4	25-Oct-2001
;; Bugfix:	Some operations (the edit menu) got hidden
;; Bugfix:      The first arrow for poly-lines was always pointing
;;              to the right
;; Changed:	Updated with changes made for Emacs 21.1
;;
;; 1.2.3	20-Nov-2000
;; Bugfix:	Autoload cookie corrected
;;
;; 1.2.2	19-Nov-2000
;; Changed:	More documentation fixes.
;; Bugfix:	The arrow characters (`artist-arrows'), which
;;              got wrong in 1.1, are now corrected.
;;
;; 1.2.1	15-Nov-2000
;; New:		Documentation fixes.
;; Bugfix:	Sets next-line-add-newlines to t while in artist-mode.
;;		Drawing with keys was confusing without this fix, if
;;		next-line-add-newlines was set to nil.
;;		Thanks to Tatsuo Furukawa <tatsuo@kobe.hp.com> for this.
;;
;; 1.2		22-Oct-2000
;; New:		Updated to work with Emacs 21
;;
;; 1.1		15-Aug-2000
;; Bugfix:	Cursor follows mouse pointer more closely.
;; New:		Works with Emacs 20.x
;; New:		Variables are customizable
;;
;; 1.1-beta1    21-Apr-1998
;; New:		Spray-can (Utterly useless, I believe, but it was fun
;;		to implement :-) after an idea by Karl-Johan Karlsson
;;		<kj@lysator.liu.se>.
;; New:		Freehand drawing (with pen).
;; New:		Vaporizing lines.
;; New:		Text-rendering using figlet.
;; New:		Picture mode compatibility.
;; Changed:	All Artist keys now uses the prefix C-c C-a not to conflict
;;		with Picture mode.
;; Bugfix:	No longer leaves traces of lines when rubberbanding
;;              if the buffer auto-scrolls.
;; Bugfix:	Infinite loop sometimes when rubberbanding was turned
;;		off.
;;
;; 1.0          01-Mar-1998
;; First official release.

;;; Code:

;; Variables

(defconst artist-version "1.2.6")
(defconst artist-maintainer-address "tab@lysator.liu.se, bug-gnu-emacs@gnu.org")

(defvar x-pointer-crosshair)

;; User options
;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

(defgroup artist nil
  "Customization of the Artist mode."
  :group 'mouse)

(defgroup artist-text nil
  "Customization of the text rendering."
  :group 'artist)

(defcustom artist-rubber-banding t
  "Interactively do rubber-banding when non-nil."
  :group 'artist
  :type 'boolean)

(defcustom artist-first-char ?1
  "Character to set at first point when not rubber-banding."
  :group 'artist
  :type 'character)

(defcustom artist-second-char ?2
  "Character to set at second point when not rubber-banding."
  :group 'artist
  :type 'character)

(defcustom artist-interface-with-rect t
  "Whether to interface with the rect package or not.

Interfacing to the rect package means that the Copy and Paste operations
will use the rectangle buffer when accessing the copied area.  This means
that you can insert a rectangle which is copied using the artist package
and vice versa.

If this causes any problem for you (for example, if the implementation of
the rectangle package changes), you can set this variable to nil, and the
artist package will use its own copy buffer."
  :group 'artist
  :type 'boolean)

(defvar artist-arrows [ ?> nil ?v ?L ?< nil ?^ nil ]
  ;; This is a defvar, not a defcustom, since the custom
  ;; package shows vectors of characters as a vector of integers,
  ;; which is confusing
  "A vector of characters to use as arrows.

The vector is 8 elements long and contains a character for each
direction, or nil if there is no suitable character to use for arrow
in that direction.

The directions are as follows:

			  5  6  7
			   \\ | /
			 4 - * - 0
			   / | \\
			  3  2  1")

(defcustom artist-aspect-ratio 1
  "Defines the character height-to-width aspect ratio.
This is used when drawing squares and circles."
  :group 'artist
  :type 'number)

(defcustom artist-trim-line-endings t
  "Whether or not to remove white-space at end of lines.

If non-nil, line-endings are trimmed (that is, extraneous white-space
at the end of the line is removed) when the shape is drawn."
  :group 'artist
  :type 'boolean)


(defcustom artist-flood-fill-right-border 'window-width
  "Right edge definition, used when flood-filling.

When flood-filling, if the area is not closed off to the right, then
flood-filling will fill no more to the right than specified by this
variable.  This limit is called the fill-border."
  :group 'artist
  :type '(choice (const :tag "limited to window" window-width)
		 (const :tag "limited to value of `fill-column'" fill-column)))

(defcustom artist-flood-fill-show-incrementally t
  "Whether or not to incrementally update display when flood-filling.

If non-nil, incrementally update display when flood-filling.
If set to non-nil, this currently implies discarding any input events
during the flood-fill."
  :group 'artist
  :type 'boolean)


(defcustom artist-ellipse-right-char ?\)
  "Character to use at the rightmost position when drawing narrow ellipses.

In this figure, it is the right parenthesis (the ``)'' character):
             -----
            (     )
             -----"
  :group 'artist
  :type 'character)


(defcustom artist-ellipse-left-char ?\(
  "Character to use at the leftmost position when drawing narrow ellipses.

In this figure, it is the left parenthesis (the ``('' character):
             -----
            (     )
             -----"
  :group 'artist
  :type 'character)

(defcustom artist-picture-compatibility t
  "Whether or not picture mode compatibility is on."
  :group 'artist
  :type 'boolean)




(defcustom artist-vaporize-fuzziness 1
  "How to vaporize lines that are cut off.

Accept this many characters cutting off a line and still treat
it as one line.
Example:
 If `artist-vaporize-fuzziness' is 2, then those will be recognized as
 lines from A to B (provided you start vaporizing them at the ``*''):
                         /
            A----*------/-----------B
                      \\/
            A----*----/\\------------B
                     /  \\

 but this one won't, since it is cut off by more than 2 characters:
                      \\/ /
            A----*----/\\/----------B
                     / /\\
 (in fact, only the left part [between the A and the leftmost ``/''
 crossing the line] will be vaporized)."
  :group 'artist
  :type 'integer)


(defvar artist-pointer-shape (if (eq window-system 'x) x-pointer-crosshair nil)
  "If in X Windows, use this pointer shape while drawing with the mouse.")


(defcustom artist-text-renderer-function 'artist-figlet
  "Function for doing text rendering."
  :group 'artist-text
  :type 'symbol)
(defvaralias 'artist-text-renderer 'artist-text-renderer-function)


(defcustom artist-figlet-program "figlet"
  "Program to run for `figlet'."
  :group 'artist-text
  :type 'string)


(defcustom artist-figlet-default-font "standard"
  "Default font for `figlet'."
  :group 'artist-text
  :type 'string)


(defcustom artist-figlet-list-fonts-command
  ;; list files ending with *.flf in any directory printed by the
  ;; ``figlet -I2'' command. I think this will not produce more than
  ;; one directory, but it never hurts to be on the safe side...
  "for dir in `figlet -I2`; do cd $dir; ls *.flf; done"
  "Command to run to get list of available fonts."
  :group 'artist-text
  :type 'string)


(defcustom artist-spray-interval 0.2
  "Number of seconds between repeated spraying."
  :group 'artist
  :type 'number)


(defcustom artist-spray-radius 4
  "Size of the area for spraying."
  :group 'artist
  :type 'integer)


(defvar artist-spray-chars '(?\s ?. ?- ?+ ?m ?% ?* ?#)
  ;; This is a defvar, not a defcustom, since the custom
  ;; package shows lists of characters as a lists of integers,
  ;; which is confusing
  "Characters (``color'') to use when spraying.
They should be ordered from the ``lightest'' to the ``heaviest''
since spraying replaces a light character with the next heavier one.")


(defvar artist-spray-new-char ?.
  "Initial character to use when spraying.
This character is used if spraying upon a character that is not in
`artist-spray-chars'.  The character defined by this variable should
be in `artist-spray-chars', or spraying will behave strangely.")


;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;; End of user options


;; Internal variables
;;
(defvar artist-mode nil
  "Non-nil to enable `artist-mode' and nil to disable.")
(make-variable-buffer-local 'artist-mode)

(defvar artist-mode-name " Artist"
  "Name of Artist mode beginning with a space (appears in the mode-line).")

(defvar artist-curr-go 'pen-line
  "Current selected graphics operation.")
(make-variable-buffer-local 'artist-curr-go)

(defvar artist-line-char-set nil
  "Boolean to tell whether user has set some char to use when drawing lines.")
(make-variable-buffer-local 'artist-line-char-set)

(defvar artist-line-char nil
  "Char to use when drawing lines.")
(make-variable-buffer-local 'artist-line-char)

(defvar artist-fill-char-set nil
  "Boolean to tell whether user has set some char to use when filling.")
(make-variable-buffer-local 'artist-fill-char-set)

(defvar artist-fill-char nil
  "Char to use when filling.")
(make-variable-buffer-local 'artist-fill-char)

(defvar artist-erase-char ?\s
  "Char to use when erasing.")
(make-variable-buffer-local 'artist-erase-char)

(defvar artist-default-fill-char ?.
  "Char to use when a fill-char is required but none is set.")
(make-variable-buffer-local 'artist-default-fill-char)

; This variable is not buffer local
(defvar artist-copy-buffer nil
  "Copy buffer.")

(defvar artist-draw-region-min-y 0
  "Line-number for top-most visited line for draw operation.")
(make-variable-buffer-local 'artist-draw-region-min-y)

(defvar artist-draw-region-max-y 0
  "Line-number for bottom-most visited line for draw operation.")
(make-variable-buffer-local 'artist-draw-region-max-y)

(defvar artist-borderless-shapes nil
  "When non-nil, draw shapes without border.
The fill char is used instead, if it is set.")
(make-variable-buffer-local 'artist-borderless-shapes)

(defvar artist-prev-next-op-alist nil
  "Assoc list for looking up next and/or previous draw operation.
The structure is as follows:  (OP . (PREV-OP . NEXT-OP))
where the elements are as follows:
* OP is an atom: the KEY-SYMBOL in the `artist-mt' structure
* PREV-OP and NEXT-OP are strings: the KEYWORD in the `artist-mt' structure

This variable is initialized by the `artist-make-prev-next-op-alist' function.")

(eval-when-compile
  ;; Make rect available at compile-time
  (require 'rect)			; for interfacing with rect
  (require 'reporter)			; the bug-reporting tool
  (require 'picture))			; picture mode compatibility

(if artist-interface-with-rect
    (require 'rect))

(require 'reporter)

(if artist-picture-compatibility
    (require 'picture))

;; Variables that are made local in artist-mode-init
(defvar artist-key-is-drawing nil)
(defvar artist-key-endpoint1 nil)
(defvar artist-key-poly-point-list nil)
(defvar artist-key-shape nil)
(defvar artist-key-draw-how nil)
(defvar artist-popup-menu-table nil)
(defvar artist-key-compl-table nil)
(defvar artist-rb-save-data nil)
(defvar artist-arrow-point-1 nil)
(defvar artist-arrow-point-2 nil)
\f
(defvar artist-menu-map
  (let ((map (make-sparse-keymap)))
    (define-key map [spray-chars]
      '(menu-item "Characters for Spray" artist-select-spray-chars
		  :help "Choose characters for sprayed by the spray-can"))
    (define-key map [borders]
      '(menu-item "Draw Shape Borders" artist-toggle-borderless-shapes
		  :help "Toggle whether shapes are drawn with borders"
		  :button (:toggle . (not artist-borderless-shapes))))
    (define-key map [trimming]
      '(menu-item "Trim Line Endings" artist-toggle-trim-line-endings
		  :help "Toggle trimming of line-endings"
		  :button (:toggle . artist-trim-line-endings)))
    (define-key map [rubber-band]
      '(menu-item "Rubber-banding" artist-toggle-rubber-banding
		  :help "Toggle rubber-banding"
		  :button (:toggle . artist-rubber-banding)))
    (define-key map [set-erase]
      '(menu-item "Character to Erase..." artist-select-erase-char
		  :help "Choose a specific character to erase"))
    (define-key map [set-line]
      '(menu-item "Character for Line..." artist-select-line-char
		  :help "Choose the character to insert when drawing lines"))
    (define-key map [set-fill]
      '(menu-item "Character for Fill..." artist-select-fill-char
		  :help "Choose the character to insert when filling in shapes"))
    (define-key map [artist-separator] '(menu-item "--"))
    (dolist (op '(("Vaporize" artist-select-op-vaporize-lines vaporize-lines)
		  ("Erase" artist-select-op-erase-rectangle erase-rect)
		  ("Spray-can" artist-select-op-spray-set-size spray-get-size)
		  ("Text" artist-select-op-text-overwrite text-ovwrt)
		  ("Ellipse" artist-select-op-circle circle)
		  ("Poly-line" artist-select-op-straight-poly-line spolyline)
		  ("Square" artist-select-op-square square)
		  ("Rectangle" artist-select-op-rectangle rectangle)
    		  ("Line" artist-select-op-straight-line s-line)
    		  ("Pen" artist-select-op-pen-line pen-line)))
      (define-key map (vector (nth 2 op))
    	`(menu-item ,(nth 0 op)
    		    ,(nth 1 op)
    		    :help ,(format "Draw using the %s style" (nth 0 op))
    		    :button (:radio . (eq artist-curr-go ',(nth 2 op))))))
    map))

(defvar artist-mode-map
  (let ((map (make-sparse-keymap)))
    (setq artist-mode-map (make-sparse-keymap))
    (define-key map [down-mouse-1] 'artist-down-mouse-1)
    (define-key map [S-down-mouse-1] 'artist-down-mouse-1)
    (define-key map [down-mouse-2] 'artist-mouse-choose-operation)
    (define-key map [S-down-mouse-2] 'artist-mouse-choose-operation)
    (define-key map [down-mouse-3] 'artist-down-mouse-3)
    (define-key map [S-down-mouse-3] 'artist-down-mouse-3)
    (define-key map [C-mouse-4] 'artist-select-prev-op-in-list)
    (define-key map [C-mouse-5] 'artist-select-next-op-in-list)
    (define-key map "\r" 'artist-key-set-point) ; return
    (define-key map [up] 'artist-previous-line)
    (define-key map "\C-p" 'artist-previous-line)
    (define-key map [down] 'artist-next-line)
    (define-key map "\C-n" 'artist-next-line)
    (define-key map [left] 'artist-backward-char)
    (define-key map "\C-b" 'artist-backward-char)
    (define-key map [right] 'artist-forward-char)
    (define-key map "\C-f" 'artist-forward-char)
    (define-key map "<" 'artist-toggle-first-arrow)
    (define-key map ">" 'artist-toggle-second-arrow)
    (define-key map "\C-c\C-a\C-e" 'artist-select-erase-char)
    (define-key map "\C-c\C-a\C-f" 'artist-select-fill-char)
    (define-key map "\C-c\C-a\C-l" 'artist-select-line-char)
    (define-key map "\C-c\C-a\C-o" 'artist-select-operation)
    (define-key map "\C-c\C-a\C-r" 'artist-toggle-rubber-banding)
    (define-key map "\C-c\C-a\C-t" 'artist-toggle-trim-line-endings)
    (define-key map "\C-c\C-a\C-s" 'artist-toggle-borderless-shapes)
    (define-key map "\C-c\C-c"     'artist-mode-off)
    (define-key map "\C-c\C-al"    'artist-select-op-line)
    (define-key map "\C-c\C-aL"    'artist-select-op-straight-line)
    (define-key map "\C-c\C-ar"    'artist-select-op-rectangle)
    (define-key map "\C-c\C-aR"    'artist-select-op-square)
    (define-key map "\C-c\C-as"    'artist-select-op-square)
    (define-key map "\C-c\C-ap"    'artist-select-op-poly-line)
    (define-key map "\C-c\C-aP"    'artist-select-op-straight-poly-line)
    (define-key map "\C-c\C-ae"    'artist-select-op-ellipse)
    (define-key map "\C-c\C-ac"    'artist-select-op-circle)
    (define-key map "\C-c\C-at"    'artist-select-op-text-see-thru)
    (define-key map "\C-c\C-aT"    'artist-select-op-text-overwrite)
    (define-key map "\C-c\C-aS"    'artist-select-op-spray-can)
    (define-key map "\C-c\C-az"    'artist-select-op-spray-set-size)
    (define-key map "\C-c\C-a\C-d" 'artist-select-op-erase-char)
    (define-key map "\C-c\C-aE"    'artist-select-op-erase-rectangle)
    (define-key map "\C-c\C-av"    'artist-select-op-vaporize-line)
    (define-key map "\C-c\C-aV"    'artist-select-op-vaporize-lines)
    (define-key map "\C-c\C-a\C-k" 'artist-select-op-cut-rectangle)
    (define-key map "\C-c\C-a\M-w" 'artist-select-op-copy-rectangle)
    (define-key map "\C-c\C-a\C-y" 'artist-select-op-paste)
    (define-key map "\C-c\C-af"    'artist-select-op-flood-fill)
    (define-key map "\C-c\C-a\C-b" 'artist-submit-bug-report)
    (define-key map [menu-bar artist] (cons "Artist" artist-menu-map))
    map)
  "Keymap for `artist-minor-mode'.")

(defvar artist-replacement-table (make-vector 256 0)
  "Replacement table for `artist-replace-char'.")


;;;
;;; The table of graphic operations
;;;
(defvar artist-mt
  ;; Implementation note: Maybe this should be done using a structure
  ;; in the cl package?
  ;;
  '(
    (menu
     ("Drawing"
      ((function-call
	( "Undo"		do-undo		undo))

       (separator )
       (graphics-operation
	("Pen" (("Pen" pen-char "pen-c"
		 artist-no-arrows nil
		 nil nil nil
		 artist-do-continously
		 artist-pen
		 (nil))
		("Pen Line" pen-line "pen-l"
		 artist-arrows artist-pen-set-arrow-points
		 artist-pen-reset-last-xy nil nil
		 artist-do-continously
		 artist-pen-line
		 (nil)))))

       (graphics-operation
	("Line" (("line" line "line"
		  artist-arrows artist-set-arrow-points-for-2points
		  nil nil nil
		  2
		  artist-draw-line
		  (artist-undraw-line
		   artist-nil nil))
		 ("straight line" s-line "sline"
		  artist-arrows artist-set-arrow-points-for-2points
		  nil nil nil
		  2
		  artist-draw-sline
		  (artist-undraw-sline
		   artist-nil nil)))))

       (graphics-operation
	("Rectangle" (("rectangle" rect "rect"
		       artist-no-arrows nil
		       nil nil nil
		       2
		       artist-draw-rect
		       (artist-undraw-rect
			artist-t-if-fill-char-set artist-fill-rect))
		      ("square" square "square"
		       artist-no-arrows nil
		       nil nil nil
		       2
		       artist-draw-square
		       (artist-undraw-square
			artist-t-if-fill-char-set artist-fill-square)))))

       (graphics-operation
	("Poly-line" (("poly-line" polyline "poly"
		       artist-arrows artist-set-arrow-points-for-poly
		       nil nil nil
		       artist-do-poly
		       artist-draw-line
		       (artist-undraw-line
			artist-nil nil))
		      ("straight poly-line" spolyline "s-poly"
		       artist-arrows artist-set-arrow-points-for-poly
		       nil nil nil
		       artist-do-poly
		       artist-draw-sline
		       (artist-undraw-sline
			artist-nil nil)))))

       (graphics-operation
	("Ellipse" (("ellipse" ellipse "ellipse"
		     artist-no-arrows nil
		     nil nil nil
		     2
		     artist-draw-ellipse
		     (artist-undraw-ellipse
		      artist-t-if-fill-char-set artist-fill-ellipse))
		    ("circle" circle "circle"
		     artist-no-arrows nil
		     nil nil nil
		     2
		     artist-draw-circle
		     (artist-undraw-circle
		      artist-t-if-fill-char-set artist-fill-circle)))))

       (graphics-operation
	("Text" (("text see-thru" text-thru "text-thru"
		   artist-no-arrows nil
		   nil nil nil
		   1
		   artist-text-see-thru
		   nil)
		  ("text overwrite" text-ovwrt "text-ovwrt"
		   artist-no-arrows nil
		   nil nil nil
		   1
		   artist-text-overwrite
		   nil))))

       (graphics-operation
	("Spray-can" (("spray-can" spray-can "spray-can"
		       artist-no-arrows nil
		       nil nil nil
		       artist-do-continously
		       artist-spray
		       (artist-spray-get-interval))
		      ("spray set size" spray-get-size "spray-size"
		       artist-no-arrows nil
		       nil artist-spray-clear-circle artist-spray-set-radius
		       2
		       artist-draw-circle
		       (artist-undraw-circle
			artist-nil nil)))))

       (graphics-operation
	("Erase" (("erase char" erase-char "erase-c"
		   artist-no-arrows nil
		   nil nil nil
		   artist-do-continously
		   artist-erase-char
		   (nil))
		  ("erase rectangle" erase-rect "erase-r"
		   artist-no-arrows nil
		   nil nil nil
		   2
		   artist-draw-rect
		   (artist-undraw-rect
		    artist-t artist-erase-rect)))))

       (graphics-operation
	("Vaporize" (("vaporize line" vaporize-line "vaporize-1"
		   artist-no-arrows nil
		   nil nil nil
		   1
		   artist-vaporize-line
		   nil)
		  ("vaporize lines" vaporize-lines "vaporize-n"
		   artist-no-arrows nil
		   nil nil nil
		   1
		   artist-vaporize-lines
		   nil)))))))

    (menu
     ("Edit"
      ((graphics-operation
	("Cut" (("cut rectangle" cut-r "cut-r"
		 artist-no-arrows nil
		 nil nil nil
		 2
		 artist-draw-rect
		 (artist-undraw-rect
		  artist-t artist-cut-rect))
		("cut square" cut-s "cut-s"
		 artist-no-arrows nil
		 nil nil nil
		 2
		 artist-draw-square
		 (artist-undraw-square
		  artist-t artist-cut-square)))))

       (graphics-operation
	("Copy" (("copy rectangle" copy-r "copy-r"
		  artist-no-arrows nil
		  nil nil nil
		  2
		  artist-draw-rect
		  (artist-undraw-rect
		   artist-t artist-copy-rect))
		 ("copy square" copy-s "copy-s"
		  artist-no-arrows nil
		  nil nil nil
		  2
		  artist-draw-square
		  (artist-undraw-square
		   artist-t artist-copy-square)))))

       (graphics-operation
	("Paste" (("paste" paste "paste"
		   artist-no-arrows nil
		   nil nil nil
		   1
		   artist-paste
		   nil)
		  ("paste" paste "paste"
		   artist-no-arrows nil
		   nil nil nil
		   1
		   artist-paste
		   nil))))

       (graphics-operation
	("Flood-fill" (("flood-fill" flood-fill "flood"
			artist-no-arrows nil
			nil nil nil
			1
			artist-flood-fill
			nil)
		       ("flood-fill" flood-fill "flood"
			artist-no-arrows nil
			nil nil nil
			1
			artist-flood-fill
			nil)))))))

    (menu
     ("Settings"
      ((function-call
	("Set Fill"	set-fill	artist-select-fill-char))

       (function-call
	("Set Line"	set-line	artist-select-line-char))

       (function-call
	("Set Erase"	set-erase	artist-select-erase-char))

       (function-call
	("Rubber-banding" rubber-band	artist-toggle-rubber-banding))

       (function-call
	("Trimming"	trimming	artist-toggle-trim-line-endings))

       (function-call
	("Borders"	borders		artist-toggle-borderless-shapes))

       (function-call
	("Spray-chars"	spray-chars	artist-select-spray-chars)))))

  ) ;; end of list

  "Master Table for `artist-mode'.
This table is primarily a table over the different graphics operations
available in Artist mode, but it also holds layout information for the
popup menu.

The master table is a list of table elements.  The elements of this table
have the layout

  (TAG INFO-PART)

There are three kinds of TAG:

  `menu'                -- a sub-menu
  `separator'           -- produce a separator in the popup menu
  `function-call'       -- call a function
  `graphics-operation'  -- a graphics operation

The layout of the INFO-PART for `menu' is

  (TITLE ((TAG-1 INFO-PART-1) (TAG-2 INFO-PART-2) ...))

TITLE is the title of the submenu; this is followed by a list of
menu items, each on the general form (TAG INFO-PART).


The layout of the INFO-PART for `separator' is empty and not used.


This is the layout of the INFO-PART for `function-call':

  (KEYWORD SYMBOL FN)

KEYWORD is a string naming the operation, and appears in the popup menu.
SYMBOL is the symbol for the operations.
FN is the function performing the operation.  This function
  is called with no arguments.  Its return value is ignored.


The layout of the INFO-PART for `graphics-operation' is

  (TITLE (UNSHIFTED SHIFTED))

TITLE is the title that appears in the popup menu.  UNSHIFTED
and SHIFTED specify for unshifted and shifted operation.  Both
have the form

  (KEYWORD KEY-SYMBOL MODE-LINE ARROW-PRED ARROW-SET-FN
   INIT-FN PREP-FILL-FN EXIT-FN DRAW-HOW DRAW-FN EXTRA-DRAW-INFO)

KEYWORD is a string specifying the name of the shape to draw.
  This is used when selecting drawing operation.
KEY-SYMBOL is the key which is used when looking up members
  through the functions `artist-go-get-MEMBER-from-symbol'
  and `artist-fc-get-MEMBER-from-symbol'.
MODE-LINE is a string that appears in the mode-line when drawing
  the shape.
ARROW-PRED is a function that is called to find out if the shape
  can have arrows.  The function is called with no arguments and
  must return nil or t.
ARROW-SET-FN is a function that is called to set arrow end-points.
  Arguments and return values for this function are described below.
INIT-FN is, if non-nil, a function that is called when the first
  point of the shape is set.  Arguments and return values for
  this function are described below.
PREP-FILL-FN is, if non-nil, a function that is called after
  the last point is set, but before the filling is done.
  Arguments and return values for this function are described below.
EXIT-FN is, if non-nil, a function that is called after filling
  is done.  Arguments and return values for this function are
  described below.
DRAW-HOW defines the kind of shape.  The kinds of shapes are:
  `artist-do-continously'  -- Do drawing operation continuously,
                              as long as the mouse button is held down.
  `artist-do-poly'         -- Do drawing operation many times.
  1                        -- Do drawing operation only once.
  2                        -- The drawing operation requires two points.
DRAW-FN is the function to call for drawing.  Arguments and
  return values for this function are described below.
EXTRA-DRAW-INFO the layout of this depends on the value of DRAW-HOW:
  If DRAW-HOW is `artist-do-continously':

    (INTERVAL-FN)

    INTERVAL-FN is, if non-nil, a function to call for getting
      an interval between repeated calls to the DRAW-FN.
      This function is called with no arguments and must
      return a number, the interval in seconds.
      If nil, calls to DRAW-FN are done only when the mouse
      or cursor is moved.

  If DRAW-HOW is either `artist-do-poly' or 2:

    (UNDRAW-FN FILL-PRED FILL-FN)

    UNDRAW-FN is a function to call for undrawing the shape.
      Arguments and return values for this function are
      described below.
    FILL-PRED is a function that is called to find out if the shape
      can have arrows.  The function must take no arguments and
      return nil or t.
    FILL-FN  is a function to call for filling the shape.
      Arguments and return values for this function are
      described below.

  If DRAW-HOW is 1:

    ()

Note! All symbols and keywords (both in the `function-call' INFO-PART
      as well as in the `graphics-operation' INFO-PART) must be unique.

The following table describe function arguments and return value
for different functions and DRAW-HOWs.

If DRAW-HOW is either `artist-do-continously' or 1:

  INIT-FN       X Y ==> ignored
  PREP-FILL-FN  X Y ==> ignored
  EXIT-FN       X Y ==> ignored
  ARROW-SET-FN  X Y ==> ignored
  DRAW-FN       X Y ==> ignored

If DRAW-HOW is 2:

  INIT-FN       X1 Y1  ==> ignored
  PREP-FILL-FN  X1 Y1 X2 Y2 ==> ignored
  EXIT-FN       X1 Y1 X2 Y2 ==> ignored
  ARROW-SET-FN  X1 Y1 X2 Y2 ==> ignored
  DRAW-FN       X1 Y1 X2 Y2 ==> (ENDPOINT-1 ENDPOINT-2 SHAPE)
  UNDRAW-FN     (ENDPOINT-1 ENDPOINT-2 SHAPE) ==> ignored
  FILL-FN	 (ENDPOINT-1 ENDPOINT-2 SHAPE) X1 Y1 X2 Y2 ==> ignored

  ENDPOINT-1 and ENDPOINT-2 are endpoints which are created with
  `artist-make-endpoint'
  SHAPE is an opaque structure, created by the DRAW-FN and intended
  to be used only by the UNDRAW-FN.

If DRAW-HOW is `artist-do-poly':

  INIT-FN       X1 Y1
  PREP-FILL-FN  POINT-LIST
  ARROW-SET-FN  POINT-LIST
  EXIT-FN       POINT-LIST
  DRAW-FN       X-LAST Y-LAST X-NEW Y-NEW ==> (ENDPOINT-1 ENDPOINT-2 SHAPE)
  UNDRAW-FN     (ENDPOINT-1 ENDPOINT-2 SHAPE)
  FILL-FN       POINT-LIST

  ENDPOINT-1 and ENDPOINT-2 are endpoints which are created with
  `artist-make-endpoint'.
  SHAPE is an opaque structure, created by the DRAW-FN and intended
  to be used only by the UNDRAW-FN.
  POINT-LIST is a list of vectors [X Y].")


;;
;; Accessors for the master table
;;

(defun artist-mt-get-tag (element)
  "Retrieve the tag component from the master table ELEMENT."
  (elt element 0))

(defun artist-mt-get-info-part (element)
  "Retrieve the info part component from the master table ELEMENT."
  (elt element 1))

;; For the 'graphics-operation info-parts
;;
(defsubst artist-go-get-desc (info-part)
  "Retrieve the description component from a graphics operation INFO-PART."
  (elt info-part 0))

(defsubst artist-go-get-unshifted (info-part)
  "Retrieve the unshifted info from a graphics operation INFO-PART."
  (elt (elt info-part 1) 0))

(defsubst artist-go-get-shifted (info-part)
  "Retrieve the shifted info from a graphics operation INFO-PART."
  (elt (elt info-part 1) 1))

(defsubst artist-go-get-keyword (info-variant-part)
  "Retrieve the keyword component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 0))

(defsubst artist-go-get-symbol (info-variant-part)
  "Retrieve the symbol component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 1))

(defsubst artist-go-get-mode-line (info-variant-part)
  "Retrieve the mode line component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 2))

(defsubst artist-go-get-arrow-pred (info-variant-part)
  "Retrieve the arrow predicate component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 3))

(defsubst artist-go-get-arrow-set-fn (info-variant-part)
  "Retrieve the arrow set component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 4))

(defsubst artist-go-get-init-fn (info-variant-part)
  "Retrieve the init function component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 5))

(defsubst artist-go-get-prep-fill-fn (info-variant-part)
  "Retrieve the fill preparation function component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 6))

(defsubst artist-go-get-exit-fn (info-variant-part)
  "Retrieve the exit component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 7))

(defsubst artist-go-get-draw-how (info-variant-part)
  "Retrieve the draw how component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 8))

(defsubst artist-go-get-draw-fn (info-variant-part)
  "Retrieve the draw function component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  (elt info-variant-part 9))

(defsubst artist-go-get-undraw-fn (info-variant-part)
  "Retrieve the undraw function component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
This interval function component is available only if the `draw-how'
component is other than `artist-do-continously' or 1."
  (elt (elt info-variant-part 10) 0))

(defsubst artist-go-get-interval-fn (info-variant-part)
  "Retrieve the interval function component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
This interval function component is available only if the `draw-how'
component is `artist-do-continously'."
  (elt (elt info-variant-part 10) 0))

(defsubst artist-go-get-fill-pred (info-variant-part)
  "Retrieve the fill predicate component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
This interval function component is available only if the `draw-how'
component is other than `artist-do-continously' or 1."
  (elt (elt info-variant-part 10) 1))

(defsubst artist-go-get-fill-fn (info-variant-part)
  "Retrieve the fill function component from an INFO-VARIANT-PART.
An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
This interval function component is available only if the `draw-how'
component is other than `artist-do-continously' or 1."
  (elt (elt info-variant-part 10) 2))

;; For the 'function-call info-parts
;;
(defsubst artist-fc-get-keyword (info-part)
  "Retrieve the keyword component from a graphics operation INFO-PART."
  (elt info-part 0))

(defsubst artist-fc-get-symbol (info-part)
  "Retrieve the symbol component from a graphics operation INFO-PART."
  (elt info-part 1))

(defsubst artist-fc-get-fn (info-part)
  "Retrieve the function component from a graphics operation INFO-PART."
  (elt info-part 2))

;; For the 'menu info-parts
;;
(defsubst artist-mn-get-title (info-part)
  "Retrieve the title component from a graphics operation INFO-PART."
  (elt info-part 0))

(defsubst artist-mn-get-items (info-part)
  "Retrieve the items component from a graphics operation INFO-PART."
  (elt info-part 1))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; mouse wheel cyclic operation selection

(defun artist-get-last-non-nil-op (op-list &optional last-non-nil)
  "Find the last non-nil draw operation in OP-LIST.
Optional LAST-NON-NIL will be returned if OP-LIST is nil."
  (if op-list
      (artist-get-last-non-nil-op (cdr op-list)
				  (or (car (car op-list)) last-non-nil))
    last-non-nil))

(defun artist-get-first-non-nil-op (op-list)
  "Find the first non-nil draw operation in OP-LIST."
  (or (car (car op-list)) (artist-get-first-non-nil-op (cdr op-list))))

(defun artist-is-in-op-list-p (op op-list)
  "Check whether OP is in OP-LIST."
  (and op-list
       (or (and (car (car op-list)) (string= op (car (car op-list))))
	   (artist-is-in-op-list-p op (cdr op-list)))))

(defun artist-make-prev-next-op-alist (op-list
				       &optional
				       last-non-nil-arg first-non-nil-arg
				       prev-entry prev-op-arg)
  "Build an assoc-list of OP-LIST.
The arguments LAST-NON-NIL-ARG, FIRST-NON-NIL-ARG, PREV-ENTRY and
PREV-OP-ARG are used when invoked recursively during the build-up."
  (let* ((last-non-nil  (or last-non-nil-arg
			    (artist-get-last-non-nil-op
			     artist-key-compl-table)))
         (first-non-nil (or first-non-nil-arg
			    (artist-get-first-non-nil-op
			     artist-key-compl-table)))
         (prev-op       (or prev-op-arg last-non-nil))
         (op            (car (car op-list)))
         (opsym         (artist-mt-get-symbol-from-keyword op))
         (entry         (cons opsym (cons prev-op nil))))
    (if (or (and op-list (not op))
	    (artist-is-in-op-list-p op (cdr op-list)))
        (artist-make-prev-next-op-alist (cdr op-list)
					last-non-nil first-non-nil
					prev-entry prev-op)
      (if prev-entry (setcdr (cdr prev-entry) op))
      (if op-list
          (cons entry (artist-make-prev-next-op-alist
		       (cdr op-list)
		       last-non-nil first-non-nil
		       entry op))
        (progn (setcdr (cdr prev-entry) first-non-nil) nil)))))

(defun artist-select-next-op-in-list ()
  "Cyclically select next drawing mode operation."
  (interactive)
  (let ((next-op (cdr (cdr (assoc artist-curr-go artist-prev-next-op-alist)))))
    (artist-select-operation next-op)
    (message "%s" next-op)))

(defun artist-select-prev-op-in-list ()
  "Cyclically select previous drawing mode operation."
  (interactive)
  (let ((prev-op (car (cdr (assoc artist-curr-go artist-prev-next-op-alist)))))
    (artist-select-operation prev-op)
    (message "%s" prev-op)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; ---------------------------------
;;; The artist-mode
;;; ---------------------------------

;;;###autoload
(define-minor-mode artist-mode
  "Toggle Artist mode.
With argument ARG, turn Artist mode on if ARG is positive.
Artist lets you draw lines, squares, rectangles and poly-lines,
ellipses and circles with your mouse and/or keyboard.

How to quit Artist mode

 Type \\[artist-mode-off] to quit artist-mode.


How to submit a bug report

 Type \\[artist-submit-bug-report] to submit a bug report.


Drawing with the mouse:

 mouse-2
 shift mouse-2	Pops up a menu where you can select what to draw with
		mouse-1, and where you can do some settings (described
		below).

 mouse-1
 shift mouse-1	Draws lines, rectangles or poly-lines, erases, cuts, copies
		or pastes:

		Operation	Not shifted		  Shifted
		--------------------------------------------------------------
                Pen             fill-char at point        line from last point
                                                          to new point
		--------------------------------------------------------------
		Line		Line in any direction	  Straight line
		--------------------------------------------------------------
		Rectangle	Rectangle		  Square
		--------------------------------------------------------------
		Poly-line	Poly-line in any dir	  Straight poly-lines
		--------------------------------------------------------------
		Ellipses	Ellipses		  Circles
		--------------------------------------------------------------
		Text		Text (see thru)		  Text (overwrite)
		--------------------------------------------------------------
		Spray-can	Spray-can		  Set size for spray
		--------------------------------------------------------------
		Erase		Erase character		  Erase rectangle
		--------------------------------------------------------------
		Vaporize	Erase single line	  Erase connected
							  lines
		--------------------------------------------------------------
		Cut		Cut rectangle		  Cut square
		--------------------------------------------------------------
		Copy		Copy rectangle		  Copy square
		--------------------------------------------------------------
		Paste		Paste			  Paste
		--------------------------------------------------------------
		Flood-fill	Flood-fill		  Flood-fill
		--------------------------------------------------------------

		* Straight lines can only go horizontally, vertically
		  or diagonally.

		* Poly-lines are drawn while holding mouse-1 down.  When you
		  release the button, the point is set.  If you want a segment
		  to be straight, hold down shift before pressing the
		  mouse-1 button.  Click mouse-2 or mouse-3 to stop drawing
		  poly-lines.

		* See thru for text means that text already in the buffer
		  will be visible through blanks in the text rendered, while
		  overwrite means the opposite.

		* Vaporizing connected lines only vaporizes lines whose
		  _endpoints_ are connected.  See also the variable
		  `artist-vaporize-fuzziness'.

		* Cut copies, then clears the rectangle/square.

		* When drawing lines or poly-lines, you can set arrows.
		  See below under ``Arrows'' for more info.

		* The mode line shows the currently selected drawing operation.
		  In addition, if it has an asterisk (*) at the end, you
		  are currently drawing something.

		* Be patient when flood-filling -- large areas take quite
		  some time to fill.


 mouse-3	Erases character under pointer
 shift mouse-3	Erases rectangle


Settings

 Set fill	Sets the character used when filling rectangles/squares

 Set line	Sets the character used when drawing lines

 Erase char	Sets the character used when erasing

 Rubber-banding	Toggles rubber-banding

 Trimming	Toggles trimming of line-endings (that is: when the shape
		is drawn, extraneous white-space at end of lines is removed)

 Borders        Toggles the drawing of line borders around filled shapes


Drawing with keys

 \\[artist-key-set-point]		Does one of the following:
		For lines/rectangles/squares: sets the first/second endpoint
		For poly-lines: sets a point (use C-u \\[artist-key-set-point] to set last point)
		When erase characters: toggles erasing
		When cutting/copying: Sets first/last endpoint of rect/square
		When pasting: Pastes

 \\[artist-select-operation]	Selects what to draw

 Move around with \\[artist-next-line], \\[artist-previous-line], \\[artist-forward-char] and \\[artist-backward-char].

 \\[artist-select-fill-char]	Sets the character to use when filling
 \\[artist-select-line-char]	Sets the character to use when drawing
 \\[artist-select-erase-char]	Sets the character to use when erasing
 \\[artist-toggle-rubber-banding]	Toggles rubber-banding
 \\[artist-toggle-trim-line-endings]	Toggles trimming of line-endings
 \\[artist-toggle-borderless-shapes]	Toggles borders on drawn shapes


Arrows

 \\[artist-toggle-first-arrow]		Sets/unsets an arrow at the beginning
		of the line/poly-line

 \\[artist-toggle-second-arrow]		Sets/unsets an arrow at the end
		of the line/poly-line


Selecting operation

 There are some keys for quickly selecting drawing operations:

 \\[artist-select-op-line]	Selects drawing lines
 \\[artist-select-op-straight-line]	Selects drawing straight lines
 \\[artist-select-op-rectangle]	Selects drawing rectangles
 \\[artist-select-op-square]	Selects drawing squares
 \\[artist-select-op-poly-line]	Selects drawing poly-lines
 \\[artist-select-op-straight-poly-line]	Selects drawing straight poly-lines
 \\[artist-select-op-ellipse]	Selects drawing ellipses
 \\[artist-select-op-circle]	Selects drawing circles
 \\[artist-select-op-text-see-thru]	Selects rendering text (see thru)
 \\[artist-select-op-text-overwrite]	Selects rendering text (overwrite)
 \\[artist-select-op-spray-can]	Spray with spray-can
 \\[artist-select-op-spray-set-size]	Set size for the spray-can
 \\[artist-select-op-erase-char]	Selects erasing characters
 \\[artist-select-op-erase-rectangle]	Selects erasing rectangles
 \\[artist-select-op-vaporize-line]	Selects vaporizing single lines
 \\[artist-select-op-vaporize-lines]	Selects vaporizing connected lines
 \\[artist-select-op-cut-rectangle]	Selects cutting rectangles
 \\[artist-select-op-copy-rectangle]	Selects copying rectangles
 \\[artist-select-op-paste]	Selects pasting
 \\[artist-select-op-flood-fill]	Selects flood-filling


Variables

 This is a brief overview of the different variables.  For more info,
 see the documentation for the variables (type \\[describe-variable] <variable> RET).

 artist-rubber-banding		Interactively do rubber-banding or not
 artist-first-char		What to set at first/second point...
 artist-second-char		...when not rubber-banding
 artist-interface-with-rect	If cut/copy/paste should interface with rect
 artist-arrows			The arrows to use when drawing arrows
 artist-aspect-ratio		Character height-to-width for squares
 artist-trim-line-endings	Trimming of line endings
 artist-flood-fill-right-border	Right border when flood-filling
 artist-flood-fill-show-incrementally	Update display while filling
 artist-pointer-shape		Pointer shape to use while drawing
 artist-ellipse-left-char	Character to use for narrow ellipses
 artist-ellipse-right-char	Character to use for narrow ellipses
 artist-borderless-shapes       If shapes should have borders
 artist-picture-compatibility   Whether or not to be picture mode compatible
 artist-vaporize-fuzziness      Tolerance when recognizing lines
 artist-spray-interval          Seconds between repeated sprayings
 artist-spray-radius            Size of the spray-area
 artist-spray-chars             The spray-``color''
 artist-spray-new-chars         Initial spray-``color''

Hooks

 Turning the mode on or off runs `artist-mode-hook'.


Keymap summary

\\{artist-mode-map}"
  :init-value nil :group 'artist :lighter artist-mode-name
  :keymap artist-mode-map
  (cond ((null artist-mode)
	 ;; Turn mode off
	 (artist-mode-exit))
	(t
	 ;; Turn mode on
	 (artist-mode-init))))

;; Init and exit
(defun artist-mode-init ()
  "Init Artist mode.  This will call the hook `artist-mode-hook'."
  ;; Set up a conversion table for mapping tabs and new-lines to spaces.
  ;; the last case, 0, is for the last position in buffer/region, where
  ;; the `following-char' function returns 0.
  (let ((i 0))
    (while (< i 256)
      (aset artist-replacement-table i i)
      (setq i (1+ i))))
  (aset artist-replacement-table ?\n ?\s)
  (aset artist-replacement-table ?\t ?\s)
  (aset artist-replacement-table 0 ?\s)
  ;; More setup
  (make-local-variable 'artist-key-is-drawing)
  (make-local-variable 'artist-key-endpoint1)
  (make-local-variable 'artist-key-poly-point-list)
  (make-local-variable 'artist-key-shape)
  (make-local-variable 'artist-key-draw-how)
  (make-local-variable 'artist-popup-menu-table)
  (make-local-variable 'artist-key-compl-table)
  (make-local-variable 'artist-prev-next-op-alist)
  (make-local-variable 'artist-rb-save-data)
  (make-local-variable 'artist-arrow-point-1)
  (make-local-variable 'artist-arrow-point-2)
  (setq artist-key-is-drawing nil)
  (setq artist-key-endpoint1 nil)
  (setq artist-key-poly-point-list nil)
  (setq artist-key-shape nil)
  (setq artist-popup-menu-table (artist-compute-popup-menu-table artist-mt))
  (setq artist-key-compl-table (artist-compute-key-compl-table artist-mt))
  (setq artist-prev-next-op-alist
	(artist-make-prev-next-op-alist artist-key-compl-table))
  (setq artist-rb-save-data (make-vector 7 0))
  (setq artist-arrow-point-1 nil)
  (setq artist-arrow-point-2 nil)
  (make-local-variable 'next-line-add-newlines)
  (setq next-line-add-newlines t)
  (setq artist-key-draw-how
	(artist-go-get-draw-how-from-symbol artist-curr-go))
  (if (and artist-picture-compatibility (not (eq major-mode 'picture-mode)))
      (progn
	(picture-mode)
	(message "")))
  (artist-mode-line-show-curr-operation artist-key-is-drawing))

(declare-function picture-mode-exit "picture" (&optional nostrip))

(defun artist-mode-exit ()
  "Exit Artist mode.  This will call the hook `artist-mode-hook'."
  (if (and artist-picture-compatibility (eq major-mode 'picture-mode))
      (picture-mode-exit))
  (kill-local-variable 'next-line-add-newlines))

(defun artist-mode-off ()
  "Turn Artist mode off."
  (interactive)
  (artist-mode -1)
  (force-mode-line-update))

;;
;; General routines
;;

(defun artist-update-display ()
  "Repaint the display."
  (sit-for 0))

(defun artist-mode-line-show-curr-operation (is-drawing)
  "Show current operation in mode-line.  If IS-DRAWING, show that."
  (let ((mtext (concat artist-mode-name "/"
		       (artist-go-get-mode-line-from-symbol artist-curr-go)
		       (if is-drawing "/*" ""))))
    (setcdr (assq 'artist-mode minor-mode-alist) (list mtext)))
  (force-mode-line-update))


(defun artist-t-if-fill-char-set ()
  "Return the value of the variable `artist-fill-char-set'."
  artist-fill-char-set)

(defun artist-t ()
  "Always return t."
  t)

(defun artist-nil ()
  "Always return nil."
  nil)

(defun artist-arrows ()
  "Say yes to arrows!"
  t)

(defun artist-no-arrows ()
  "Say no to arrows!"
  nil)

;;
;; Auxiliary init-routines
;;

;
; Computing the table for the x-popup-menu from the master table
;

(defun artist-compute-popup-menu-table (menu-table)
  "Create a menu from MENU-TABLE data.
The returned value is suitable for the `x-popup-menu' function."
  (cons "Artist menu"
	(artist-compute-popup-menu-table-sub menu-table)))

(defun artist-compute-popup-menu-table-sub (menu-table)
  "Compute operation table suitable for `x-popup-menu' from MENU-TABLE."
  (mapcar
   (lambda (element)
     (let ((element-tag (artist-mt-get-tag element)))
       (cond ((eq element-tag 'graphics-operation)
	      (let* ((info-part (artist-mt-get-info-part element))
		     (descr     (artist-go-get-desc info-part))
		     (unshifted (artist-go-get-unshifted info-part))
		     (symbol    (artist-go-get-symbol unshifted)))
		(list descr symbol)))

	     ((eq element-tag 'function-call)
	      (let* ((info-part (artist-mt-get-info-part element))
		     (keyword   (artist-fc-get-keyword info-part))
		     (symbol    (artist-fc-get-symbol info-part)))
		(list keyword symbol)))

	     ((eq element-tag 'separator)
	      '("" ""))

	     ((eq element-tag 'menu)
	      (let* ((info-part (artist-mt-get-info-part element))
		     (title     (artist-mn-get-title info-part))
		     (items     (artist-mn-get-items info-part)))
		(cons title (artist-compute-popup-menu-table-sub items))))

	     (t
	      (error "Internal error: unknown element-tag: \"%s\""
		     element-tag)))))
   menu-table))

;
; Computing the completion table from the master table
;

(defun artist-compute-key-compl-table (menu-table)
  "Compute completion table from MENU-TABLE, suitable for `completing-read'."
  (apply
   'nconc
   (remq nil
    (mapcar
     (lambda (element)
       (let ((element-tag (artist-mt-get-tag element)))
	 (cond ((eq element-tag 'graphics-operation)
		(let* ((info-part     (artist-mt-get-info-part element))
		       (unshifted     (artist-go-get-unshifted info-part))
		       (shifted       (artist-go-get-shifted info-part))
		       (unshifted-kwd (artist-go-get-keyword unshifted))
		       (shifted-kwd   (artist-go-get-keyword shifted)))
		  (list (list unshifted-kwd) (list shifted-kwd))))
	       ((eq element-tag 'menu)
		(let* ((info-part     (artist-mt-get-info-part element))
		       (items         (artist-mn-get-items info-part)))
		  (artist-compute-key-compl-table items)))
	       (t
		nil))))
     menu-table))))


;
; Retrieving  a symbol (graphics operation or function-call) from a keyword
;

(defun artist-mt-get-symbol-from-keyword (kwd)
  "Search master table for keyword KWD and return its symbol."
  (artist-mt-get-symbol-from-keyword-sub artist-mt kwd))

(defun artist-mt-get-symbol-from-keyword-sub (table kwd)
  "Search TABLE for keyword KWD and return its symbol."
  (catch 'found
    (mapc
     (lambda (element)
       (let ((element-tag (artist-mt-get-tag element)))
	 (cond ((eq element-tag 'graphics-operation)
		(let* ((info-part     (artist-mt-get-info-part element))
		       (unshifted     (artist-go-get-unshifted info-part))
		       (shifted       (artist-go-get-shifted info-part))
		       (unshifted-kwd (artist-go-get-keyword unshifted))
		       (shifted-kwd   (artist-go-get-keyword shifted))
		       (unshifted-sym (artist-go-get-symbol unshifted))
		       (shifted-sym   (artist-go-get-symbol shifted)))
		  (if (string-equal kwd unshifted-kwd)
		      (throw 'found unshifted-sym))
		  (if (string-equal kwd shifted-kwd)
		      (throw 'found shifted-sym))))

	       ((eq element-tag 'function-call)
		(let* ((info-part (artist-mt-get-info-part element))
		       (keyword   (artist-fc-get-keyword info-part))
		       (symbol    (artist-fc-get-symbol info-part)))
		  (if (string-equal kwd keyword)
		      (throw 'found symbol))))
	       ((eq element-tag 'menu)
		(let* ((info-part     (artist-mt-get-info-part element))
		       (items         (artist-mn-get-items info-part))
		       (answer        (artist-mt-get-symbol-from-keyword-sub
				       items kwd)))
		  (if answer (throw 'found answer))))
	       (t
		nil))))
     table)
    nil))


;
; Retrieving info from a graphics operation symbol
;

(defun artist-go-retrieve-from-symbol (symbol retrieve-fn)
  "Search the master table for a graphics operation SYMBOL.
Calls RETRIEVE-FN to retrieve information from that symbol's
info-variant-part."
  (artist-go-retrieve-from-symbol-sub artist-mt symbol retrieve-fn))

(defun artist-go-retrieve-from-symbol-sub (table symbol retrieve-fn)
  "Search the TABLE for a graphics operation SYMBOL.
Calls RETRIEVE-FN to retrieve information from that symbol's
info-variant-part."
  (catch 'found
    (mapc
     (lambda (element)
       (let ((element-tag (artist-mt-get-tag element)))
	 (cond ((eq element-tag 'graphics-operation)
		(let* ((info-part     (artist-mt-get-info-part element))
		       (unshifted     (artist-go-get-unshifted info-part))
		       (shifted       (artist-go-get-shifted info-part))
		       (unshifted-sym (artist-go-get-symbol unshifted))
		       (shifted-sym   (artist-go-get-symbol shifted))
		       (variant-part  (cond
				       ((eq unshifted-sym symbol) unshifted)
				       ((eq shifted-sym symbol) shifted)
				       (t nil))))
		  (if variant-part	; if found do:
		      (throw 'found (funcall retrieve-fn variant-part)))))

	       ((eq element-tag 'menu)
		(let* ((info-part     (artist-mt-get-info-part element))
		       (items         (artist-mn-get-items info-part))
		       (answer        (artist-go-retrieve-from-symbol-sub
				       items symbol retrieve-fn)))
		  (if answer (throw 'found answer)))))))

     table)
    nil))

(defun artist-go-get-keyword-from-symbol (symbol)
  "Search the master table, get keyword from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-keyword))

(defun artist-go-get-mode-line-from-symbol (symbol)
  "Search the master table, get mode-line from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-mode-line))

(defun artist-go-get-arrow-pred-from-symbol (symbol)
  "Search the master table, get arrow-pred from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-pred))

(defun artist-go-get-arrow-set-fn-from-symbol (symbol)
  "Search the master table, get arrow-set-fn from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-set-fn))

(defun artist-go-get-init-fn-from-symbol (symbol)
  "Search the master table, get init-fn from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-init-fn))

(defun artist-go-get-prep-fill-fn-from-symbol (symbol)
  "Search the master table, get prep-fill-fn from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-prep-fill-fn))

(defun artist-go-get-exit-fn-from-symbol (symbol)
  "Search the master table, get exit-fn from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-exit-fn))

(defun artist-go-get-draw-fn-from-symbol (symbol)
  "Search the master table, get draw-fn from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-fn))

(defun artist-go-get-draw-how-from-symbol (symbol)
  "Search the master table, get draw-how from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-how))

(defun artist-go-get-undraw-fn-from-symbol (symbol)
  "Search the master table, get undraw-fn from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-undraw-fn))

(defun artist-go-get-interval-fn-from-symbol (symbol)
  "Search the master table, get interval-fn from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-interval-fn))

(defun artist-go-get-fill-pred-from-symbol (symbol)
  "Search the master table, get fill-pred from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-pred))

(defun artist-go-get-fill-fn-from-symbol (symbol)
  "Search the master table, get fill-fn from a graphics operation SYMBOL."
  (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-fn))

(defun artist-go-get-symbol-shift (symbol is-shifted)
  "Search for (shifted or unshifted) graphics operation SYMBOL.
If IS-SHIFTED is non-nil, return the shifted symbol,
otherwise the unshifted symbol."
  (artist-go-get-symbol-shift-sub artist-mt symbol is-shifted))

(defun artist-go-get-symbol-shift-sub (table symbol is-shifted)
  "Search TABLE for (shifted or unshifted) graphics SYMBOL.
If IS-SHIFTED is non-nil, return the shifted symbol,
otherwise the unshifted symbol."
  (catch 'found
    (mapc
     (lambda (element)
       (let ((element-tag (artist-mt-get-tag element)))
	 (cond ((eq element-tag 'graphics-operation)
		(let* ((info-part       (artist-mt-get-info-part element))
		       (unshift-variant (artist-go-get-unshifted info-part))
		       (shift-variant   (artist-go-get-shifted info-part))
		       (unshift-sym     (artist-go-get-symbol unshift-variant))
		       (shift-sym       (artist-go-get-symbol shift-variant)))
		  (if (or (eq symbol unshift-sym) (eq symbol shift-sym))
		      (throw 'found (if is-shifted shift-sym unshift-sym)))))

	       ((eq element-tag 'menu)
		(let* ((info-part     (artist-mt-get-info-part element))
		       (items         (artist-mn-get-items info-part))
		       (answer        (artist-go-get-symbol-shift-sub
				       items symbol is-shifted)))
		  (if answer (throw 'found answer)))))))

     table)
    nil))

;
; Retrieving info from a function-call symbol
;

(defun artist-fc-retrieve-from-symbol (symbol retrieve-fn)
  "Search the master table for a function call SYMBOL.
Calls RETRIEVE-FN to retrieve information from that symbol's
info-variant-part."
  (artist-fc-retrieve-from-symbol-sub artist-mt symbol retrieve-fn))

(defun artist-fc-retrieve-from-symbol-sub (table symbol retrieve-fn)
  "Search TABLE for a function-call SYMBOL.
Calls RETRIEVE-FN to retrieve information from that symbol's
info-variant-part."
  (catch 'found
    (mapc
     (lambda (element)
       (let ((element-tag (artist-mt-get-tag element)))
	 (cond ((eq element-tag 'function-call)
		(let* ((info-part (artist-mt-get-info-part element))
		       (fc-symbol (artist-fc-get-symbol info-part)))
		  (if (eq fc-symbol symbol)
		      (throw 'found (funcall retrieve-fn info-part)))))

	       ((eq element-tag 'menu)
		(let* ((info-part     (artist-mt-get-info-part element))
		       (items         (artist-mn-get-items info-part))
		       (answer        (artist-fc-retrieve-from-symbol-sub
				       items symbol retrieve-fn)))
		  (if answer (throw 'found answer)))))))

     table)
    nil))

(defun artist-fc-get-fn-from-symbol (symbol)
  "Search the master table to get function from a function call SYMBOL."
  (artist-fc-retrieve-from-symbol symbol 'artist-fc-get-fn))


;;
;; Utilities
;;

;; Macro that won't funcall the function if it is nil.
;;
(defmacro artist-funcall (fn &rest args)
  "Call function FN with ARGS, if FN is not nil."
  `(if ,fn (funcall ,fn ,@args)))

(defun artist-uniq (l)
  "Remove consecutive duplicates in list L.  Comparison is done with `equal'."
  (cond ((null l) nil)
	((null (cdr l)) l)		; only one element in list
	((equal (car l) (car (cdr l))) (artist-uniq (cdr l))) ; first 2 equal
	(t (cons (car l) (artist-uniq (cdr l)))))) ; first 2 are different

(defun artist-string-split (str r)
  "Split string STR at occurrences of regexp R, returning a list of strings."
  (let ((res nil)
	(start 0)
	(match-pos 0))
    (while (setq match-pos (string-match r str start))
      (setq res (cons (copy-sequence (substring str start match-pos)) res))
      (setq start (match-end 0)))
    (if (null res)
	(list str)
      (if (< (match-end 0) (- (length str) 1))
	  (setq res (cons (substring str (match-end 0) (length str)) res)))
      (reverse res))))

(defun artist-string-to-file (str file-name)
  "Write string STR to file FILE-NAME."
  (write-region str 'end-is-ignored file-name nil 'no-message))

(defun artist-file-to-string (file-name)
  "Read from file FILE-NAME into a string."
  (save-excursion
    (let ((tmp-buffer (get-buffer-create (concat "*artist-" file-name "*"))))
      (set-buffer tmp-buffer)
      (goto-char (point-min))
      (insert-file-contents file-name nil nil nil t)
      (let ((str (copy-sequence (buffer-substring (point-min)
						  (point-max)))))
	(kill-buffer tmp-buffer)
	str))))

(defun artist-clear-buffer (buf)
  "Clear contents of buffer BUF."
  (with-current-buffer buf
    (goto-char (point-min))
    (delete-char (- (point-max) (point-min)) nil)))


(defun artist-system (program stdin &optional program-args)
  "Run PROGRAM synchronously with the contents of string STDIN to stdin.
Optional args PROGRAM-ARGS are arguments to PROGRAM.
Return a list (RETURN-CODE STDOUT STDERR)."
  (save-excursion
    (let* ((tmp-stdin-file-name (if stdin
				    (make-temp-file "artist-stdin.")
				  nil))
	   (tmp-stdout-buffer (get-buffer-create
			       (concat "*artist-" program "*")))
	   (tmp-stderr-file-name (make-temp-file "artist-stdout."))
	   (binary-process-input nil)	; for msdos
	   (binary-process-output nil))

      ;; Prepare stdin
      (if stdin (artist-string-to-file stdin tmp-stdin-file-name))

      ;; Clear the buffer
      (artist-clear-buffer tmp-stdout-buffer)

      ;; Start the program
      (unwind-protect
	  (let ((res (if program-args
			 (apply 'call-process
				program
				tmp-stdin-file-name
				(list tmp-stdout-buffer
				      tmp-stderr-file-name)
				nil
				(if (stringp program-args)
				    (list program-args)
				  program-args))
		       (apply 'call-process
			      program
			      tmp-stdin-file-name
			      (list tmp-stdout-buffer
				    tmp-stderr-file-name)
			      nil))))

	    ;; the return value
	    (list res
		  (with-current-buffer tmp-stdout-buffer
                    (buffer-substring (point-min) (point-max)))
		  (artist-file-to-string tmp-stderr-file-name)))

	;; Unwind: remove temporary files and buffers
	(if (and stdin (file-exists-p tmp-stdin-file-name))
	    (delete-file tmp-stdin-file-name))
	(if (file-exists-p tmp-stderr-file-name)
	    (delete-file tmp-stderr-file-name))
	(if (memq tmp-stdout-buffer (buffer-list))
	    (kill-buffer tmp-stdout-buffer))))))

;; Routines that deal with the buffer
;;
;; artist-current-line			get line number (top of buffer is 0)
;;
;; artist-move-to-xy			move to (x,y) (0,0) is beg-of-buffer
;;
;; artist-get-char-at-xy		get char in at (x,y)
;;
;; artist-replace-char			overwrite (replace) char at point
;; artist-replace-chars			overwrite (replace) chars at point
;;

(defsubst artist-current-column ()
  "Return point's current column."
  (current-column))

(defsubst artist-current-line ()
  "Return point's current line, buffer-relative.  Top of buffer is 0."
  (+ (count-lines 1 (point))
     (if (= (current-column) 0) 1 0)
     -1))

(defsubst artist-move-to-xy (x y)
  "Move to column X, at row Y from the top of buffer.  Top line is 0."
  ;;
  ;; Q: Why do we do forward-line twice?
  ;; A: The documentation for forward-line says
  ;;
  ;;        "... Returns the count of lines left to move. ... With
  ;;        positive N, a non-empty line at the end counts as one
  ;;        line successfully moved (for the return value)."
  ;;
  ;;    This means that if we are trying to move forward past the end
  ;;    of the buffer, and that last line happened to be longer than
  ;;    the current column, then we end up at the end of that last
  ;;    line, and forward-line returns one less than we actually
  ;;    wanted to move.
  ;;
  ;;	Example: In the figure below, the `X' is the very last
  ;;		 character in the buffer ("a non-empty line at the
  ;;             end"). Suppose point is at P. Then (forward-line 1)
  ;;             returns 0 and puts point after the `X'.
  ;;
  ;;			--------top of buffer--------
  ;;
  ;;			     P      X
  ;;			-------bottom of buffer------
  ;;
  ;;    But, if we are at the end of buffer when trying to move
  ;;    forward, then forward-line will return the (for us) correct
  ;;    value, which is good, because we will come to the end of the
  ;;    buffer by the first forward-line. The second forward-line
  ;;    will then get us where we really wanted to go.
  ;;
  ;;    If we are not moving past the end of the buffer, then the
  ;;    second forward-line will return 0.
  ;;
  ;; Q: What happens if we are moving upwards?
  ;; A: That will work good. insert-char won't insert a negative
  ;;    number of chars, and forward-line will fail silently if we are
  ;;    moving past the beginning of the buffer.
  ;;
  (forward-line (- y (artist-current-line)))
  (insert-char ?\n (forward-line (- y (artist-current-line))))
  (move-to-column (max x 0) t)
  (let ((curr-y (artist-current-line)))
    (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y))
    (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y))))

(defsubst artist-get-char-at-xy (x y)
  "Return the character found at column X, row Y.
Also updates the variables `artist-draw-min-y' and `artist-draw-max-y'."
  (artist-move-to-xy x y)
  (let ((curr-y (artist-current-line)))
    (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y))
    (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y)))
  (following-char))


(defsubst artist-get-replacement-char (c)
  "Retrieve a replacement for character C from `artist-replacement-table'.
The replacement is used to convert tabs and new-lines to spaces."
  ;; Characters may be outside the range of the `artist-replacement-table',
  ;; for example if they are Unicode code points >= 256.
  ;; Check so we don't attempt to access the array out of its bounds,
  ;; assuming no such character needs to be replaced.
  (if (< c (length artist-replacement-table))
      (aref artist-replacement-table c)
    c))

(defun artist-get-char-at-xy-conv (x y)
  "Retrieve the character at X, Y, converting tabs and new-lines to spaces."
  (save-excursion
    (artist-get-replacement-char (artist-get-char-at-xy x y))))


(defun artist-replace-char (new-char)
  "Replace the character at point with NEW-CHAR."
  (let ((overwrite-mode 'overwrite-mode-textual)
	(fill-column 32765)		; Large :-)
	(blink-matching-paren nil))
    (setq last-command-event (artist-get-replacement-char new-char))
    (self-insert-command 1)))

(defun artist-replace-chars (new-char count)
  "Replace characters at point with NEW-CHAR.  COUNT chars are replaced."
  ;; Check that the variable exists first. The doc says it was added in 19.23.
  (if (and (and (boundp 'emacs-major-version) (= emacs-major-version 20))
	   (and (boundp 'emacs-minor-version) (<= emacs-minor-version 3)))
      ;; This is a bug workaround for Emacs 20, versions up to 20.3:
      ;; The self-insert-command doesn't care about the overwrite-mode,
      ;; so the insertion is done in the same way as in picture mode.
      ;; This seems to be a little bit slower.
      (let* ((replaced-c (artist-get-replacement-char new-char))
	     (replaced-s (make-string count replaced-c)))
	(artist-move-to-xy (+ (artist-current-column) count)
			   (artist-current-line))
	(delete-char (- count))
	(insert replaced-s))
    ;; In emacs-19, the self-insert-command works better
    (let ((overwrite-mode 'overwrite-mode-textual)
	  (fill-column 32765)		; Large :-)
	  (blink-matching-paren nil))
      (setq last-command-event (artist-get-replacement-char new-char))
      (self-insert-command count))))

(defsubst artist-replace-string (string &optional see-thru)
  "Replace contents at point with STRING.
With optional argument SEE-THRU set to non-nil, text in the buffer
``shines thru'' blanks in the STRING."
  (let ((char-list (append string nil))	; convert the string to a list
	(overwrite-mode 'overwrite-mode-textual)
	(fill-column 32765)		; Large :-)
	(blink-matching-paren nil))
    (while char-list
      (let ((c (car char-list)))
	(if (and see-thru (= (artist-get-replacement-char c) ?\s))
	    (artist-move-to-xy (1+ (artist-current-column))
			       (artist-current-line))
	  (artist-replace-char c)))
      (setq char-list (cdr char-list)))))

;;
;; Routines for setting and unsetting points
;; Used when not rubber-banding
;;
(defun artist-no-rb-unset-point1 ()
  "Unsets point 1 when not rubber-banding."
  (let ((x-now (artist-current-column))
	(y-now (artist-current-line))
	(x (aref artist-rb-save-data 0))
	(y (aref artist-rb-save-data 1)))
    (artist-move-to-xy x y)
    (artist-replace-char (aref artist-rb-save-data 2))
    (artist-move-to-xy x-now y-now)))

(defun artist-no-rb-set-point1 (x y)
  "Set point 1 at X, Y when not rubber-banding."
  (let ((x-now (artist-current-column))
	(y-now (artist-current-line)))
    (aset artist-rb-save-data 0 x)
    (aset artist-rb-save-data 1 y)
    (aset artist-rb-save-data 2 (artist-get-char-at-xy x y))
    (artist-move-to-xy x y)
    (artist-replace-char artist-first-char)
    (artist-move-to-xy x-now y-now)
    (aset artist-rb-save-data 6 0)))

(defun artist-no-rb-unset-point2 ()
  "This function unsets point 2 when not rubber-banding."
  (if (= (aref artist-rb-save-data 6) 1)
      (let ((x-now (artist-current-column))
	    (y-now (artist-current-line))
	    (x (aref artist-rb-save-data 3))
	    (y (aref artist-rb-save-data 4)))
	(artist-move-to-xy x y)
	(artist-replace-char (aref artist-rb-save-data 5))
	(artist-move-to-xy x-now y-now))))

(defun artist-no-rb-set-point2 (x y)
  "Set point 2 at X, Y when not rubber-banding."
  (let ((x-now (artist-current-column))
	(y-now (artist-current-line)))
    (aset artist-rb-save-data 3 x)
    (aset artist-rb-save-data 4 y)
    (aset artist-rb-save-data 5 (artist-get-char-at-xy x y))
    (artist-move-to-xy x y)
    (artist-replace-char artist-second-char)
    (artist-move-to-xy x-now y-now)
    (aset artist-rb-save-data 6 1)))

(defun artist-no-rb-unset-points ()
  "This function unsets point 1 and 2 when not rubber-banding."
  (artist-no-rb-unset-point1)
  (artist-no-rb-unset-point2))


;; artist-intersection-char
;;
;; Note: If changing this, see the notes for artist-unintersection-char
;;       and artist-vaporize-lines
;;
(defun artist-intersection-char (new-c old-c)
  "Calculates intersection character when drawing a NEW-C on top of an OLD-C.
Return character according to this scheme:

		OLD-C	NEW-C		return
		 -	 |		   +
		 |	 -		   +
		 +	 |		   +
		 +	 -		   +
		 \\	 /		   X
		 /	 \\		   X
		 X	 /		   X
		 X	 \\		   X
		other combinations	   NEW-C"

  (cond ((and (= old-c ?- )  (= new-c ?| ))  ?+ )
	((and (= old-c ?| )  (= new-c ?- ))  ?+ )
	((and (= old-c ?+ )  (= new-c ?- ))  ?+ )
	((and (= old-c ?+ )  (= new-c ?| ))  ?+ )
	((and (= old-c ?\\ ) (= new-c ?/ ))  ?X )
	((and (= old-c ?/ )  (= new-c ?\\ )) ?X )
	((and (= old-c ?X )  (= new-c ?/ ))  ?X )
	((and (= old-c ?X )  (= new-c ?\\ )) ?X )
	(t new-c)))

;; artist-unintersection-char
;;
;; Note: If changing this, see the note for artist-vaporize-lines
;;
(defun artist-unintersection-char (line-c buffer-c)
  "Restore character to before intersection when removing LINE-C from BUFFER-C.
Return character according to this scheme:

		LINE-C	BUFFER-C	return
		 -	 +		   |
		 |	 +		   -
		 \\	 X		   /
		 /	 X		   \\
		other combinations	   `artist-erase-char'."

  (cond ((and (= line-c ?- )  (= buffer-c ?+ ))  ?|  )
	((and (= line-c ?| )  (= buffer-c ?+ ))  ?-  )
	((and (= line-c ?\\ ) (= buffer-c ?X ))  ?/  )
	((and (= line-c ?/ )  (= buffer-c ?X ))  ?\\ )
	((= line-c buffer-c) artist-erase-char)
	(t buffer-c)))


;; Computing the line-char to use
;; for use with borderless shapes
;;
(defsubst artist-compute-line-char ()
  "Compute which character to use for lines, if any.
Return value is either nil for the default characters that make up lines, or
a character chosen depending on the variables `artist-borderless-shapes',
`artist-fill-char-set', `artist-fill-char' and
`artist-line-char-set' and `artist-line-char'."
  (if (and artist-borderless-shapes artist-fill-char-set)
      artist-fill-char
    (if artist-line-char-set
	artist-line-char
      nil)))


;; Things for drawing horizontal, vertical and diagonal (straight) lines.
;;
;; A line here is a vector:
;; [ start-x start-y length direction saved-char-1 saved-char-2 ... ]
;; directions start with 0 at the x-axis and counts anti clockwise.
;;
(defvar artist-direction-info
  ;;   x  y char
  [ [  1  0 ?- ]			; direction 0
    [  1  1 ?\\ ]			; direction 1
    [  0  1 ?| ]			; direction 2
    [ -1  1 ?/ ]			; direction 3
    [ -1  0 ?- ]			; direction 4
    [ -1 -1 ?\\ ]			; direction 5
    [  0 -1 ?| ]			; direction 6
    [  1 -1 ?/ ] ]			; direction 7
  "Table used for stepping x and y coordinates in a specific direction.
This table is also used for determining which char to use for that direction.")

(defsubst artist-direction-step-x (direction)
  "Return the x-step for DIRECTION from the `artist-direction-info' table."
  (aref (aref artist-direction-info direction) 0))

(defsubst artist-direction-step-y (direction)
  "Return the y-step for DIRECTION from the `artist-direction-info' table."
  (aref (aref artist-direction-info direction) 1))

(defun artist-direction-char (direction)
  "Return the character for DIRECTION from the `artist-direction-info' table."
  (aref (aref artist-direction-info direction) 2))

;; artist-find-direction
;;
;;
;;
(defun artist-find-direction (x1 y1 x2 y2)
  "Find the direction from point X1,Y1 to X2,Y2.
Returns a DIRECTION, a number 0--7, coded as follows:

			 5  6  7
			  \\ | /
			4 - * - 0
			  / | \\
			 3  2  1"
  (let ((delta-x (- x2 x1))
	(delta-y (- y2 y1)))
    (cond ((>= delta-x (* 2 (abs delta-y))) 0)
	  ((>= delta-y (* 2 (abs delta-x))) 2)
 	  ((>= (- delta-x) (* 2 (abs delta-y))) 4)
  	  ((>= (- delta-y) (* 2 (abs delta-x))) 6)
   	  ((and (>= delta-x 0) (>= delta-y 0)) 1)
    	  ((and (<= delta-x 0) (>= delta-y 0)) 3)
     	  ((and (<= delta-x 0) (<= delta-y 0)) 5)
      	  ((and (>= delta-x 0) (<= delta-y 0)) 7))))

(defun artist-straight-calculate-length (direction x1 y1 x2 y2)
  "Calculate length for a straight line in DIRECTION from X1,Y1 to X2,Y2."
  (cond ((or (= direction 7)
	     (= direction 0)
	     (= direction 1)) (1+ (- x2 x1)))
	((or (= direction 3)
	     (= direction 4)
	     (= direction 5)) (1+ (- x1 x2)))
	(t (1+ (abs (- y2 y1))))))

(defun artist-sline (x1 y1 x2 y2)
  "Create a straight line from X1,Y1 to X2,Y2."
  (let* ((direction (artist-find-direction x1 y1 x2 y2))
	 (length (artist-straight-calculate-length direction x1 y1 x2 y2))
	 (line (make-vector (+ length 4) x1)))
    ;; not needed:
    ;; (aset line 0 x1)
    ;; because we set all elements to x1
    (aset line 1 y1)
    (aset line 2 length)
    (aset line 3 direction)
    line))

(defun artist-save-chars-under-sline (line)
  "Save characters under a LINE."
  (let ((x (aref line 0))
       	(y (aref line 1))
  	(length (+ (aref line 2) 4))
   	(direction (aref line 3))
    	(i 4))
    (while (< i length)
      (aset line i (artist-get-char-at-xy x y))
      (setq x (+ x (artist-direction-step-x direction)))
      (setq y (+ y (artist-direction-step-y direction)))
      (setq i (1+ i))))
    line)



;; Things for drawing lines in all directions.
;; The line drawing engine is the eight-point algorithm.
;;
;; A line is here a list of (x y saved-char new-char)s.
;;
(defvar artist-octant-info
  ;; Initial	Step in		Step in
  ;; coeffs	x and y		x and y
  ;; for	if q >= 0	if g < 0
  ;; dfdx,dfdy
  [ [  2  1	 1  0		 1  1 ]		; 1st octant
    [  1  2	 1  1		 0  1 ]		; 2nd octant
    [ -1  2	 0  1		-1  1 ]		; 3rd octant
    [ -2  1	-1  1		-1  0 ]		; 4th octant
    [ -2 -1	-1  0		-1 -1 ]		; 5th octant
    [ -1 -2	-1 -1		 0 -1 ]		; 6th octant
    [  1 -2	 0 -1		 1 -1 ]		; 7th octant
    [  2 -1	 1 -1		 1  0 ] ]	; 8th octant
  "Table used by line drawing algorithm (eight point).")

;; Primitives for the artist-octant-info.
;; Decrease octant by 1 since elt counts from 0 and octant counts from 1.
;;
(defsubst artist-get-dfdx-init-coeff (octant)
  "Retrieve dfdx component for OCTANT."
  (aref (aref artist-octant-info (- octant 1)) 0))

(defsubst artist-get-dfdy-init-coeff (octant)
  "Retrieve dfdy component for OCTANT."
  (aref (aref artist-octant-info (- octant 1)) 1))

(defsubst artist-get-x-step-q>=0 (octant)
  "Retrieve x-step component for OCTANT when q >= 0."
  (aref (aref artist-octant-info (- octant 1)) 2))

(defsubst artist-get-y-step-q>=0 (octant)
  "Retrieve y-step component for OCTANT when q >= 0."
  (aref (aref artist-octant-info (- octant 1)) 3))

(defsubst artist-get-x-step-q<0 (octant)
  "Retrieve x-step component for OCTANT for q < 0."
  (aref (aref artist-octant-info (- octant 1)) 4))

(defsubst artist-get-y-step-q<0 (octant)
  "Retrieve y-step component for OCTANT for q < 0."
  (aref (aref artist-octant-info (- octant 1)) 5))


;; Find octant from x1 y1 x2 y2 coordinates.
;;
(defun artist-find-octant (x1 y1 x2 y2)
  "Find octant for a line from X1,Y1 to X2,Y2.
Octant are numbered 1--8, anti-clockwise as:

		 \\3|2/
		 4\\|/1
		---+---
		 5/|\\8
		 /6|7\\"

  (if (<= x1 x2)				; quadrant 1 or 4
      (if (<= y1 y2)				; quadrant 1, octant 1 or 2
	  (if (>= (- x2 x1) (- y2 y1))
	      1
	    2)
	(if (>= (- x2 x1) (- (- y2 y1)))	; quadrant 4, octant 7 or 8
	    8
	  7))
    (if (<= y1 y2)				; quadrant 2 or 3
	(if (>= (- (- x2 x1)) (- y2 y1))	; quadrant 2, octant 3 or 4
	    4
	  3)
      (if (>= (- (- x2 x1)) (- (- y2 y1)))	; quadrant 3, octant 5 or 6
	  5
	6))))

;; Some inline functions for creating, setting and reading
;; members of a coordinate
;;

(defsubst artist-new-coord (x y &optional new-char)
  "Create a new coordinate at X,Y for use in a line.
Optional argument NEW-CHAR can be used for setting the new-char component
in the coord."
  (let ((coord (make-vector 4 x)))
    (aset coord 1 y)
    (aset coord 3 new-char)
    coord))

(defsubst artist-coord-get-x (coord)
  "Retrieve the x component of a COORD."
  (aref coord 0))

(defsubst artist-coord-get-y (coord)
  "Retrieve the y component of a COORD."
  (aref coord 1))

(defsubst artist-coord-set-x (coord new-x)
  "Set the x component of a COORD to NEW-X."
  (aset coord 0 new-x)
  coord)

(defsubst artist-coord-set-y (coord new-y)
  "Set the y component of a COORD to NEW-Y."
  (aset coord 1 new-y)
  coord)

(defsubst artist-coord-get-saved-char (coord)
  "Retrieve the saved char component of a COORD."
  (aref coord 2))

(defsubst artist-coord-get-new-char (coord)
  "Retrieve the new char component of a COORD."
  (aref coord 3))

(defsubst artist-coord-add-saved-char (coord saved-char)
  "Set the saved char component of a COORD to SAVED-CHAR."
  (aset coord 2 saved-char)
  coord)

(defsubst artist-coord-add-new-char (coord new-char)
  "Set the new char component of a COORD to NEW-CHAR."
  (aset coord 3 new-char)
  coord)

(defsubst artist-coord-set-new-char (coord new-char)
  "Set the new char component of a COORD to NEW-CHAR."
  (aset coord 3 new-char)
  coord)


;; Pretend we are plotting a pixel. Instead we just list it
;;
(defmacro artist-put-pixel (point-list x y)
  "In POINT-LIST, store a ``pixel'' at coord X,Y."
  `(setq ,point-list
	 (append ,point-list (list (artist-new-coord ,x ,y)))))

;; Calculate list of points using eight point algorithm
;; return a list of coords
;;
(defun artist-eight-point (x1 y1 x2 y2)
  "Run the eight-point algorithm to get a list of coords from X1,Y1 to X2,Y2."
  (let* ((point-list nil)
	 (octant (artist-find-octant x1 y1 x2 y2))
	 (dfdx-coeff (artist-get-dfdx-init-coeff octant))
	 (dfdy-coeff (artist-get-dfdy-init-coeff octant))
	 (x-step-q>=0 (artist-get-x-step-q>=0 octant))
	 (y-step-q>=0 (artist-get-y-step-q>=0 octant))
	 (x-step-q<0 (artist-get-x-step-q<0 octant))
	 (y-step-q<0 (artist-get-y-step-q<0 octant))
	 (dfdx (- (- y2 y1)))
	 (dfdy (- x2 x1))
	 (x x1)
	 (y y1)
	 (f 0)
	 (q (+ (* 2 f)
	       (* dfdx-coeff dfdx)
	       (* dfdy-coeff dfdy))))
    (artist-put-pixel point-list x y)
    (while (or (not (eq x x2)) (not (eq y y2)))
      (if (>= q 0)
	  (progn
	    (setq x (+ x x-step-q>=0))
	    (setq y (+ y y-step-q>=0))
	    (setq f (+ f (* x-step-q>=0 dfdx) (* y-step-q>=0 dfdy))))
	(progn
	  (setq x (+ x x-step-q<0))
	  (setq y (+ y y-step-q<0))
	  (setq f (+ f (* x-step-q<0 dfdx) (* y-step-q<0 dfdy)))))
      (setq q (+ (* 2 f) (* dfdx-coeff dfdx) (* dfdy-coeff dfdy)))
      (artist-put-pixel point-list x y))
    point-list))

;; artist-save-chars-under-point-list
;; Remembers the chars that were there before we did draw the line.
;; Returns point-list.
;;
(defun artist-save-chars-under-point-list (point-list)
  "Save characters originally under POINT-LIST."
  (mapcar
   (lambda (coord)
     (artist-coord-add-saved-char
      coord
      (artist-get-char-at-xy (artist-coord-get-x coord)
			     (artist-coord-get-y coord))))
   point-list))

;; artist-calculate-new-char, artist-calculate-new-chars
;; Calculates which char to insert depending on direction of point-list.
;;
;; Depending on new-coord's position relative to last-coord one of the
;; following chars are returned: \ | / - o, as indicated by this:
;;
;;			\ | /
;;			- o -
;;			/ | \
;;
;; artist-calculate-new-char  works on one coordinate, returns char.
;; artist-calculate-new-chars works on a point-list, returns point-list.
;;
(defun artist-calculate-new-char (last-coord new-coord)
  "Return a line-char to use when moving from LAST-COORD to NEW-COORD."
  (let ((last-x (artist-coord-get-x last-coord))
	(last-y (artist-coord-get-y last-coord))
	(new-x (artist-coord-get-x new-coord))
	(new-y (artist-coord-get-y new-coord)))
    (cond ((> new-x last-x) (cond ((< new-y last-y) ?/ )
				  ((> new-y last-y) ?\\ )
				  (t ?- )))
	  ((< new-x last-x) (cond ((< new-y last-y) ?\\ )
				  ((> new-y last-y) ?/ )
				  (t ?- )))
	  ((eq new-y last-y) ?o)
	  (t ?| ))))

(defun artist-calculate-new-chars (point-list)
  "Return a list of coords with line-chars calculated.  Input: POINT-LIST."
  (if (null (cdr point-list))
      (list (artist-coord-add-new-char (car point-list) ?o ))
    (let ((last-coord (car point-list)))
      (cons (artist-coord-add-new-char
	     (car point-list)
	     (artist-calculate-new-char (car (cdr point-list))
					(car point-list)))
	    (mapcar
	     (lambda (this-coord)
	       (prog1
		   (artist-coord-add-new-char
		    this-coord
		    (artist-calculate-new-char last-coord this-coord))
		 (setq last-coord this-coord)))
	     (cdr point-list))))))

;; artist-modify-new-chars
;; Replaces some characters with some other characters.
;;
;; artist-modify-new-chars works on a point-list, returns point-list.
;;
(defun artist-modify-new-chars (point-list)
  "Replace intersecting characters in POINT-LIST.
This function returns a point-list."
  (mapcar
   (lambda (coord)
     (let* ((new-c (artist-coord-get-new-char coord))
	    (saved-c (artist-coord-get-saved-char coord))
	    (modified-c (artist-intersection-char new-c saved-c)))
       (artist-coord-set-new-char coord modified-c)))
   point-list))


;;
;; functions for accessing endpoints and elements in object requiring
;; 2 endpoints
;;

(defun artist-make-endpoint (x y)
  "Create an endpoint at X, Y."
  (let ((new-endpoint (make-vector 2 x)))
    (aset new-endpoint 1 y)
    new-endpoint))

(defun artist-endpoint-get-x (endpoint)
  "Retrieve the x component of an ENDPOINT."
  (aref endpoint 0))

(defun artist-endpoint-get-y (endpoint)
  "Retrieve the y component of an ENDPOINT."
  (aref endpoint 1))

(defun artist-make-2point-object (endpoint1 endpoint2 shapeinfo)
  "Create a 2-point object of ENDPOINT1, ENDPOINT2 and SHAPEINFO."
  (list endpoint1 endpoint2 shapeinfo))

(defun artist-2point-get-endpoint1 (obj)
  "Retrieve the first endpoint of a 2-point object OBJ."
  (elt obj 0))

(defun artist-2point-get-endpoint2 (obj)
  "Retrieve the second endpoint of a 2-point object OBJ."
  (elt obj 1))

(defun artist-2point-get-shapeinfo (obj)
  "Retrieve the shapeinfo component of a 2-point object OBJ."
  (elt obj 2))


;;
;; Drawing and undrawing lines (any direction)
;;

(defun artist-draw-line (x1 y1 x2 y2)
  "Draw a line from X1, Y1 to X2, Y2.

Output is a line, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).

END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
SHAPE-INFO is a list of vectors [X Y SAVED-CHAR NEW-CHAR]."
  (let ((endpoint1 (artist-make-endpoint x1 y1))
	(endpoint2 (artist-make-endpoint x2 y2)))
    (artist-make-2point-object
     endpoint1
     endpoint2
     (mapcar
      (lambda (coord)
	(artist-move-to-xy (artist-coord-get-x coord)
			   (artist-coord-get-y coord))
	(if artist-line-char-set
	    (artist-replace-char artist-line-char)
	  (artist-replace-char (artist-coord-get-new-char coord)))
	coord)
      (artist-modify-new-chars
       (artist-calculate-new-chars
	(artist-save-chars-under-point-list
	 (artist-eight-point x1 y1 x2 y2))))))))

(defun artist-undraw-line (line)
  "Undraw LINE."
  (mapcar
   (lambda (coord)
     (artist-move-to-xy (artist-coord-get-x coord)
			(artist-coord-get-y coord))
     (artist-replace-char (artist-coord-get-saved-char coord))
     coord)
   (artist-2point-get-shapeinfo line)))

;;
;; Drawing and undrawing straight lines
;;

(defun artist-draw-sline (x1 y1 x2 y2)
  "Draw a straight line from X1, Y1 to X2, Y2.
Straight lines are vertical, horizontal or diagonal lines.
They are faster to draw and most often they are what you need
when drawing a simple image.

Output is a straight line, which is a list on the form
\(END-POINT-1 END-POINT-2 SHAPE-INFO).

END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
SHAPE-INFO is a vector [START-X START-Y LENGTH-OF-LINE DIRECTION
                        ORIGINAL-CHAR-1 ORIGINAL-CHAR-2 ... ]."
  (let* ((line (artist-save-chars-under-sline (artist-sline x1 y1 x2 y2)))
	 (x (aref line 0))
	 (y (aref line 1))
	 (length (+ (aref line 2) 4))
	 (direction (aref line 3))
	 (line-char (artist-direction-char direction))
	 (i 4)
	 (endpoint1 (artist-make-endpoint x y))
	 (endpoint2 nil))
    (while (< i length)
      (artist-move-to-xy x y)
      (if artist-line-char-set
	  (artist-replace-char artist-line-char)
	(artist-replace-char (artist-intersection-char
			      line-char
			      (aref line i))))
      (if (not (< (1+ i) length))
	  ;; This is the last element. Set the second endpoint
	  (setq endpoint2 (artist-make-endpoint x y)))
      (setq x (+ x (artist-direction-step-x direction)))
      (setq y (+ y (artist-direction-step-y direction)))
      (setq i (1+ i)))
    (artist-make-2point-object endpoint1 endpoint2 line)))


(defun artist-undraw-sline (line)
  "Undraw a straight line LINE."
  (if line
      (let* ((shape-info (artist-2point-get-shapeinfo line))
	     (x (aref shape-info 0))
	     (y (aref shape-info 1))
	     (length (+ (aref shape-info 2) 4))
	     (direction (aref shape-info 3))
	     (i 4))
	(while (< i length)
	  (artist-move-to-xy x y)
	  (artist-replace-char (aref shape-info i))
	  (setq x (+ x (artist-direction-step-x direction)))
	  (setq y (+ y (artist-direction-step-y direction)))
	  (setq i (1+ i))))))


;;
;; Drawing and undrawing rectangles and squares
;;

(defun artist-draw-rect (x1 y1 x2 y2)
  "Draw a rectangle with corners at X1, Y1 and X2, Y2.

Output is a rectangle, which is a list on the form
\(END-POINT-1 END-POINT-2 SHAPE-INFO).

END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
SHAPE-INFO is a list of four straight lines."
  (let* ((artist-line-char (artist-compute-line-char))
         (artist-line-char-set artist-line-char)
         (line1 (artist-draw-sline x1 y1 x2 y1))
	 (line2 (artist-draw-sline x2 y1 x2 y2))
	 (line3 (artist-draw-sline x2 y2 x1 y2))
	 (line4 (artist-draw-sline x1 y2 x1 y1))
	 (endpoint1 (artist-make-endpoint x1 y1))
	 (endpoint2 (artist-make-endpoint x2 y2)))
    (artist-make-2point-object endpoint1
			       endpoint2
			       (list line1 line2 line3 line4))))

(defun artist-undraw-rect (rectangle)
  "Undraw RECTANGLE."
  (if rectangle
      (let ((shape-info (artist-2point-get-shapeinfo rectangle)))
	(artist-undraw-sline (elt shape-info 3))
	(artist-undraw-sline (elt shape-info 2))
	(artist-undraw-sline (elt shape-info 1))
	(artist-undraw-sline (elt shape-info 0)))))


(defun artist-rect-corners-squarify (x1 y1 x2 y2)
  "Compute square corners from rectangle corners at X1, Y1 and X2, Y2.
The square's first corner will be X1, Y1.  The position of the second
corner depends on which of X2 and Y2 is most far away from X1, Y1."
  (let* ((delta-x      (- x2 x1))
	 (delta-y      (- y2 y1))
	 (delta-x-sign (if (< delta-x 0) -1 1))
	 (delta-y-sign (if (< delta-y 0) -1 1))
	 (new-x2)	; set below
	 (new-y2))	; set below

    ;; Check which of x2 and y2 is most distant
    ;; take care to the aspect ratio
    (if (> (abs delta-x) (abs delta-y))

	;; *** x2 more distant than y2 (with care taken to aspect ratio)
	(progn
	  (setq new-x2 x2)
	  (setq new-y2 (+ y1 (round (/ (* (abs delta-x) delta-y-sign)
				       artist-aspect-ratio)))))

      ;; *** y2 more distant than x2  (with care taken to aspect ratio)
      (progn
	(setq new-x2 (round (+ x1 (* (* (abs delta-y) delta-x-sign)
				     artist-aspect-ratio))))
	(setq new-y2 y2)))

    ;; Return this
    (list x1 y1 new-x2 new-y2)))


(defun artist-draw-square (x1 y1 x2 y2)
  "Draw a square with corners at X1, Y1 and X2, Y2.

Output is a square, which is a list on the form
\(END-POINT-1 END-POINT-2 SHAPE-INFO).

END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
SHAPE-INFO is a list of four straight lines."
  (let* ((artist-line-char (artist-compute-line-char))
         (artist-line-char-set artist-line-char)
         (square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
	 (new-x1 (elt square-corners 0))
	 (new-y1 (elt square-corners 1))
	 (new-x2 (elt square-corners 2))
	 (new-y2 (elt square-corners 3))
	 (endpoint1 (artist-make-endpoint new-x1 new-y1))
	 (endpoint2 (artist-make-endpoint new-x2 new-y2))
	 (line1 (artist-draw-sline new-x1 new-y1 new-x2 new-y1))
	 (line2 (artist-draw-sline new-x2 new-y1 new-x2 new-y2))
	 (line3 (artist-draw-sline new-x2 new-y2 new-x1 new-y2))
	 (line4 (artist-draw-sline new-x1 new-y2 new-x1 new-y1)))
    (artist-make-2point-object endpoint1
			       endpoint2
			       (list line1 line2 line3 line4))))

(defun artist-undraw-square (square)
  "Undraw SQUARE."
  (if square
      (let ((shape-info (artist-2point-get-shapeinfo square)))
	(artist-undraw-sline (elt shape-info 3))
	(artist-undraw-sline (elt shape-info 2))
	(artist-undraw-sline (elt shape-info 1))
	(artist-undraw-sline (elt shape-info 0)))))

;;
;; Filling rectangles and squares
;;

(defun artist-fill-rect (rect x1 y1 x2 y2)
  "Fill rectangle RECT from X1,Y1 to X2,Y2."
  (let ((x (1+ (min x1 x2)))
	(y (1+ (min y1 y2)))
	(x-max (max x1 x2))
	(y-max (max y1 y2)))
    (let ((w (- x-max x)))
      (while (< y y-max)
	(artist-move-to-xy x y)
	(artist-replace-chars artist-fill-char w)
	(setq y (1+ y))))))

(defun artist-fill-square (square x1 y1 x2 y2)
  "Fill a SQUARE from X1,Y1 to X2,Y2."
  (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
	 (new-x1 (elt square-corners 0))
	 (new-y1 (elt square-corners 1))
	 (new-x2 (elt square-corners 2))
	 (new-y2 (elt square-corners 3))
	 (x (1+ (min new-x1 new-x2)))
	 (y (1+ (min new-y1 new-y2)))
	 (x-max (max new-x1 new-x2))
	 (y-max (max new-y1 new-y2))
	 (w (- x-max x)))
    (while (< y y-max)
      (artist-move-to-xy x y)
      (artist-replace-chars artist-fill-char w)
      (setq y (1+ y)))))


;;
;; Pen drawing
;;

(defun artist-pen (x1 y1)
  "Draw a character at X1, Y1.
The character is replaced with the character in `artist-fill-char'."
  (artist-move-to-xy x1 y1)
  (artist-replace-char (if artist-line-char-set
			   artist-line-char
			 (if artist-fill-char-set
			     artist-fill-char
			   artist-default-fill-char))))


(defun artist-pen-line (x1 y1)
  "Draw a line from last pen position to X1, Y1.
The character is replaced with the character in `artist-fill-char'.
This will store all points in `artist-key-poly-point-list' in reversed
order (I assume it is faster to cons to the beginning of the list than
to append to the end of the list, when doing free-hand drawing)."
  (let ((artist-line-char (if artist-line-char-set
			      artist-line-char
			    (if artist-fill-char-set
				artist-fill-char
			      artist-default-fill-char))))

    ;; Draw line from last point to this
    (let ((x-last (car (car artist-key-poly-point-list)))
	  (y-last (cdr (car artist-key-poly-point-list))))
      (artist-move-to-xy x-last y-last)
      (artist-replace-char artist-line-char)
      (artist-draw-line x-last y-last x1 y1))

    ;; Update the point-list
    (setq artist-key-poly-point-list
	  (cons (cons x1 y1) artist-key-poly-point-list))))

(defun artist-pen-reset-last-xy (x1 y1)
  "Reset the last x and y points to X1, Y1 when doing pen-drawing."
  (artist-clear-arrow-points)
  (setq artist-key-poly-point-list (list (cons x1 y1))))


(defun artist-pen-set-arrow-points (x1 y1)
  "Set arrow points for pen drawing using X1, Y1.
Also, the `artist-key-poly-point-list' is reversed."

  (setq artist-key-poly-point-list
	(artist-uniq artist-key-poly-point-list))

  (if (>= (length artist-key-poly-point-list) 2)

      ;; Only set arrow-points if the point-list has two or more entries
      (let ((xn   (car (car artist-key-poly-point-list)))
	    (yn   (cdr (car artist-key-poly-point-list)))
	    (xn-1 (car (car (cdr artist-key-poly-point-list))))
	    (yn-1 (cdr (car (cdr artist-key-poly-point-list))))
	    (dirn))			; direction for point n
	(setq artist-key-poly-point-list (reverse artist-key-poly-point-list))
	(let ((x0 (car (car artist-key-poly-point-list)))
	      (y0 (cdr (car artist-key-poly-point-list)))
	      (x1 (car (car (cdr artist-key-poly-point-list))))
	      (y1 (cdr (car (cdr artist-key-poly-point-list))))
	      (dir0))			; direction for point 0
	  (setq dir0 (artist-find-direction x1 y1 x0 y0))
	  (setq dirn (artist-find-direction xn-1 yn-1 xn yn))
	  (setq artist-arrow-point-1 (artist-make-arrow-point x0 y0 dir0))
	  (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn))))))


;;
;; Text rendering
;;
(defun artist-figlet-run (text font extra-args)
  "Run figlet rendering TEXT using FONT.
EXTRA-ARGS for figlet, for the command line, may be specified."
  (let* ((figlet-args (cond ((and font extra-args)
			     (cons (concat "-f" font)
				   (artist-string-split extra-args "[ \t]+")))
			    (font (concat "-f" font))
			    (extra-args
			     (artist-string-split extra-args "[ \t]+"))
			    (t nil)))
	 (figlet-output (artist-system artist-figlet-program text figlet-args))
	 (exit-code (elt figlet-output 0))
	 (stdout    (elt figlet-output 1))
	 (stderr    (elt figlet-output 2)))
    (if (not (= exit-code 0))
	(error "Failed to render font: %s (%d)" stderr exit-code))
    stdout))

(defun artist-figlet-get-font-list ()
  "Read fonts in with the shell command.
Returns a list of strings."
  (let* ((cmd-interpreter "/bin/sh")
	 (ls-cmd          artist-figlet-list-fonts-command)
	 (result          (artist-system cmd-interpreter ls-cmd nil))
	 (exit-code       (elt result 0))
	 (stdout          (elt result 1))
	 (stderr          (elt result 2)))
    (if (not (= exit-code 0))
	(error "Failed to read available fonts: %s (%d)" stderr exit-code))
    (artist-string-split stdout ".flf\n")))

(defun artist-figlet-choose-font ()
  "Read any extra arguments for figlet."
  (interactive)
  (let* ((avail-fonts  (artist-figlet-get-font-list))
	 (font (completing-read (concat "Select font (default "
					artist-figlet-default-font
					"): ")
				(mapcar
				 (lambda (font) (cons font font))
				 avail-fonts))))
    (if (string= font "") artist-figlet-default-font font)))

(defun artist-figlet-get-extra-args ()
  "Read any extra arguments for figlet."
  (let ((extra-args (read-string "Extra args to figlet: ")))
    (if (string= extra-args "")
	nil
      extra-args)))

(defun artist-figlet (text)
  "Render TEXT using figlet."
  (let* ((figlet-font (artist-figlet-choose-font))
	 (figlet-extra-args (artist-figlet-get-extra-args)))
    (artist-figlet-run text figlet-font figlet-extra-args)))


(defun artist-text-insert-common (x y text see-thru)
  "At position X, Y, insert text TEXT.
If SEE-THRU is non-nil, then blanks in TEXT do not replace text
in the buffer."
  (let* ((string-list (artist-string-split text "\n"))
	 (i 0)
	 (len (length string-list)))
    (while (< i len)
      (artist-move-to-xy x (+ y i))
      (artist-replace-string (car string-list) see-thru)
      (setq string-list (cdr string-list))
      (setq i (1+ i)))))

(defun artist-text-insert-see-thru (x y text)
  "At position X, Y, insert text TEXT.
Let text already in buffer shine thru the TEXT inserted."
  (artist-text-insert-common x y text t))

(defun artist-text-insert-overwrite (x y text)
  "At position X, Y, insert text TEXT.
Let blanks in TEXT overwrite any text already in the buffer."
  (artist-text-insert-common x y text nil))

(defun artist-text-see-thru (x y)
  "Prompt for text to render, render it at X,Y.
This is done by calling the function specified by
`artist-text-renderer-function', which must return a list of strings,
to be inserted in the buffer.

Text already in the buffer ``shines thru'' blanks in the rendered text."
  (let* ((input-text (read-string "Type text to render: "))
	 (rendered-text (artist-funcall artist-text-renderer-function input-text)))
    (artist-text-insert-see-thru x y rendered-text)))


(defun artist-text-overwrite (x y)
  "Prompt for text to render, render it at X,Y.
This is done by calling the function specified by
`artist-text-renderer-function', which must return a list of strings,
to be inserted in the buffer.

Blanks in the rendered text overwrite any text in the buffer."
  (let* ((input-text (read-string "Type text to render: "))
	 (rendered-text (artist-funcall artist-text-renderer-function input-text)))
    (artist-text-insert-overwrite x y rendered-text)))

;;
;; Spraying
;;

(defun artist-spray-get-interval ()
  "Retrieve the interval for repeated spray."
  artist-spray-interval)

(defun artist-spray-random-points (n radius)
  "Generate N random points within a radius of RADIUS.
Returns a list of points.  Each point is on the form (X1 . Y1)."
  (let ((points))
    (while (> n 0)
      (let* ((angle (* (random 359) (/ float-pi 180)))
	     (dist  (random radius))
	     (point (cons (round (* dist (cos angle)))
			  (round (* dist (sin angle))))))
	(setq points (cons point points)))
      (setq n (- n 1)))
    points))

(defun artist-spray (x1 y1)
  "Spray at X1, Y1."
  (let* ((num-points (* artist-spray-radius artist-spray-radius))
	 (spray-points (artist-spray-random-points num-points
						   artist-spray-radius)))
    (while spray-points
      ;; Replace one spray point
      (let* ((point  (car spray-points))
	     (x      (+ x1 (car point)))
	     (y      (+ y1 (cdr point)))
	     (buf-c  (artist-get-char-at-xy-conv x y))
	     (this-c (memq buf-c artist-spray-chars))
	     (next-c (cond ((null this-c) artist-spray-new-char)
			   ((null (cdr this-c)) (car this-c))
			   (t (car (cdr this-c))))))
	(artist-move-to-xy x y)
	(artist-replace-char next-c))

      ;; Step to next spray point
      (setq spray-points (cdr spray-points)))))

(defun artist-spray-clear-circle (circle x1 y1 x2 y2)
  "Clear circle CIRCLE at X1, Y1 through X2, Y2."
  (artist-undraw-circle circle))

(defun artist-spray-set-radius (circle x1 y1 x2 y2)
  "Set spray radius from CIRCLE at X1, Y1 through X2, Y2."
  (let ((dx (- x2 x1))
	(dy (- y2 y1)))
    (setq artist-spray-radius (round (sqrt (+ (* dx dx) (* dy dy)))))
    (if (= 0 artist-spray-radius)
	(setq artist-spray-radius 1))))

;;
;; Erasing
;;

(defun artist-erase-char (x1 y1)
  "Erase a character at X1, Y1.
The character is replaced with the character in `artist-erase-char'."
  (artist-move-to-xy x1 y1)
  (artist-replace-char artist-erase-char))

(defun artist-erase-rect (rect x1 y1 x2 y2)
  "Erase rectangle RECT from X1, Y1, X2, Y2."
  (let ((artist-line-char-set t)
	(artist-fill-char-set t)
	(artist-line-char artist-erase-char)
	(artist-fill-char artist-erase-char))
    (artist-draw-rect x1 y1 x2 y2)
    (artist-fill-rect rect x1 y1 x2 y2)))


;;
;; Vaporizing (erasing) line and lines
;;


(defun artist-vap-find-endpoint (x1 y1 step-x step-y accept-set reject-set)
  "Find one endpoint for line through X1, Y1.
The endpoint is searched for in the direction defined by STEP-X, STEP-Y,
accepting characters in the list ACCEPT-SET, stopping immediately
when finding characters in the list REJECT-SET.  Fuzziness, that is
the number of consecutive characters not in ACCEPT-SET to allow as
part of the line, is determined by the variable `artist-vaporize-fuzziness'.
An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  (let ((x x1)
	(y y1)
	(x-last x1)
	(y-last y1)
	(done nil))
    (while (not done)
      (let ((c (artist-get-char-at-xy-conv x y)))
	(cond ((memq c reject-set)
	       (setq done t))

	      ;; We found a character we are accepting as part of the line.
	      ;; Update position
	      ((memq c accept-set)
	       (setq x-last x
		     y-last y
		     x (+ x step-x)
		     y (+ y step-y))
	       (if (or (< x 0) (< y 0))	;stop at the edge
		   (setq done t)))

	      ;; We found a character we are not accepting as part of
	      ;; the line Search `artist-vaporize-fuzziness'
	      ;; characters away from this position in the same
	      ;; direction to see if there are any characters in the
	      ;; accept-set. If not, we have found the endpoint.
	      (t
	       (let ((fuzziness artist-vaporize-fuzziness)
		     (x-tmp x)
		     (y-tmp y))

		 ;; while we have more fuzziness left and we have not
		 ;; found a character accepted as a line, move
		 ;; forward!
		 (while (and (> fuzziness 0) (not (memq c accept-set)))
		   (setq x-tmp (+ x-tmp step-x))
		   (setq y-tmp (+ y-tmp step-y))
		   (setq c (artist-get-char-at-xy-conv x-tmp y-tmp))
		   (setq fuzziness (- fuzziness 1)))
		 (if (memq c accept-set)

		     ;; The line continues on the other side of the
		     ;; not-accepted character.
		     (setq x x-tmp
			   y y-tmp)

		   ;; Else: We couldn't find any line on the other side.
		   ;; That means we are done searching for the endpoint.
		   (setq done t)))))))
    (cons x-last y-last)))


(defun artist-vap-find-endpoints-horiz (x y)
  "Find endpoints for a horizontal line through X, Y.
An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  (list (artist-vap-find-endpoint x y  1 0 '(?- ?+) '(?\s))
	(artist-vap-find-endpoint x y -1 0 '(?- ?+) '(?\s))))

(defun artist-vap-find-endpoints-vert (x y)
  "Find endpoints for a vertical line through X, Y.
An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  (list (artist-vap-find-endpoint x y 0  1 '(?| ?+) '(?\s))
	(artist-vap-find-endpoint x y 0 -1 '(?| ?+) '(?\s))))

(defun artist-vap-find-endpoints-swne (x y)
  "Find endpoints for a diagonal line (made by /'s) through X, Y.
An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  (list (artist-vap-find-endpoint x y  1 -1 '(?/ ?X) '(?\s))
	(artist-vap-find-endpoint x y -1  1 '(?/ ?X) '(?\s))))

(defun artist-vap-find-endpoints-nwse (x y)
  "Find endpoints for a diagonal line (made by \\'s) through X, Y.
An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  (list (artist-vap-find-endpoint x y  1  1 '(?\\ ?X) '(?\s))
	(artist-vap-find-endpoint x y -1 -1 '(?\\ ?X) '(?\s))))


(defun artist-vap-find-endpoints (x y)
  "Given a point X1, Y1, return a list of endpoints of lines through X, Y.
An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  (if artist-line-char-set
      nil
    (let ((c (artist-get-char-at-xy-conv x y)))
      (cond ((eq c ?-)  (artist-vap-find-endpoints-horiz x y))
	    ((eq c ?|)  (artist-vap-find-endpoints-vert x y))
	    ((eq c ?/)  (artist-vap-find-endpoints-swne x y))
	    ((eq c ?\\) (artist-vap-find-endpoints-nwse x y))
	    ((eq c ?+)  (append (artist-vap-find-endpoints-horiz x y)
				(artist-vap-find-endpoints-vert x y)))
	    ((eq c ?X)  (append (artist-vap-find-endpoints-swne x y)
				(artist-vap-find-endpoints-nwse x y)))

	    ;; We don't know how to find directions when we are on
	    ;; another character
	    (t nil)))))


(defun artist-vap-group-in-pairs (l)
  "Group elements in list L in pairs."
  (cond ((null l) nil)
	((null (cdr l)) l)		; unevent number of elements in list
	(t (append (list (list (car l) (car (cdr l))))
		   (artist-vap-group-in-pairs (cdr (cdr l)))))))

(defun artist-vaporize-by-endpoints (endpoint1 endpoint2)
  "Given ENDPOINT1 and ENDPOINT2, vaporize the line between them.
An endpoint is a pair (X . Y)."
  (let* ((x1 (car endpoint1))
	 (y1 (cdr endpoint1))
	 (x2 (car endpoint2))
	 (y2 (cdr endpoint2))
	 (dir (artist-find-direction x1 y1 x2 y2))
	 (x-step (aref [1 1 0 -1 -1 -1 0 1] dir))
	 (y-step (aref [0 1 1 1 0 -1 -1 -1] dir))
	 (line-c (aref [?- ?\\ ?| ?/ ?- ?\\ ?| ?/] dir))
	 (line-len (elt (list (abs (- x2 x1))
			      (abs (- x2 x1))
			      (abs (- y2 y1))
			      (abs (- y2 y1))
			      (abs (- x1 x2))
			      (abs (- x1 x2))
			      (abs (- y1 y2))
			      (abs (- y1 y2)))
			dir))
	 (x x1)
	 (y y1))
    (while (>= line-len 0)
      (let* ((buffer-c (artist-get-char-at-xy-conv x y))
	     (new-c (artist-unintersection-char line-c buffer-c)))
	(artist-move-to-xy x y)
	(artist-replace-char new-c))
      (setq x (+ x x-step)
	    y (+ y y-step)
	    line-len (- line-len 1)))))


(defun artist-vaporize-line (x1 y1)
  "Vaporize (erase) the straight line through X1, Y1.
Do this by replacing the characters that forms the line with
`artist-erase-char'.  Output is a list of endpoints for lines through
X1, Y1.  An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  (let ((endpoints (artist-vap-find-endpoints x1 y1)))
    (mapc
     (lambda (endpoints)
       (let ((ep1 (car endpoints))
	     (ep2 (car (cdr endpoints))))
	 (artist-vaporize-by-endpoints ep1 ep2)))
     (artist-vap-group-in-pairs endpoints))
    endpoints))


;; Implementation note: This depends on artist-vaporize-line doing
;; unintersections of intersecting lines.
;;
;; Example:
;;   Suppose the buffer looks like this and that we start vaporizing
;;   lines at (3,0) (at the ``*'').
;;
;;          0123456
;;         0+--*--+
;;         1|     |
;;         2|     |
;;         3+-----+
;;
;;   We will then push (0,0) and (6,0) on the stack, and vaporize the
;;   topmost horizontal line:
;;
;;          0123456
;;         0|     |
;;         1|     |
;;         2|     |
;;         3+-----+
;;
;;   We will then pop (0,0) and remove the left-most vertical line while
;;   pushing the lower left corner (0,3) on the stack, and so on until
;;   the entire rectangle is vaporized.
;;
;;   Now, What if the `+' in the upper left and upper right corners,
;;   had not been changed to `|' but to spaces instead? We would
;;   have failed when popping (0,0) and vaporizing that line because
;;   we wouldn't find any line at (0,0):
;;
;;          0123456
;;         0
;;         1|     |
;;         2|     |
;;         3+-----+
;;
;;   That's why we depend on artist-vaporize-line doing unintersecting
;;   of crossing lines. There are alternative ways to handle this
;;   if it becomes too much a trouble.
;;
(defun artist-vaporize-lines (x1 y1)
  "Vaporize lines reachable from point X1, Y1."
  (let ((ep-stack nil))
    (mapc
     (lambda (ep) (push ep ep-stack))
     (artist-vap-find-endpoints x1 y1))
    (while (not (null ep-stack))
      (let* ((vaporize-point (pop ep-stack))
	     (new-endpoints (artist-vaporize-line (car vaporize-point)
						  (cdr vaporize-point))))
	(mapc
	 (lambda (endpoint) (push endpoint ep-stack))
	 new-endpoints)))))


;;
;; Circles and ellipses
;;
(defun artist-ellipse-generate-quadrant (x-radius y-radius)
  "Create a point-list for first quadrant.
Points go from (X-RADIUS, 0) to (0, Y-RADIUS).
Quadrant is generated around origin."
  (let* ((rx2 (* x-radius x-radius))
	 (ry2 (* y-radius y-radius))
	 (2rx2 (* 2 rx2))
	 (2ry2 (* 2 ry2))
	 (p)
	 (x 0)
	 (y y-radius)
	 (px 0)
	 (py (* 2rx2 y))
	 (point-list nil))
    (artist-put-pixel point-list x y)
    (setq p (round (+ ry2 (- (* rx2 y-radius)) (* 0.25 rx2))))
    (while (< px py)
      (setq x (1+ x)
	    px (+ px 2ry2))
      (if (< p 0)
	  (setq p (+ p ry2 px))
	(setq y (- y 1)
	      py (- py 2rx2)
	      p (+ p ry2 px (- py))))
      (artist-put-pixel point-list x y))
    (setq p (round (+ (* ry2 (+ x 0.5) (+ x 0.5))
		      (* rx2 (- y 1) (- y 1))
		      (- (* rx2 ry2)))))
    (while (> y 0)
      (setq y (- y 1)
	    py (- py 2rx2))
      (if (> p 0)
	  (setq p (+ p rx2 (- py)))
	(setq x (1+ x)
	      px (+ px 2ry2)
	      p (+ p rx2 (- py) px)))
      (artist-put-pixel point-list x y))
    point-list))

(defsubst artist-new-fill-item (x y width)
  "Create a new item at X, Y, with WIDTH.
This is for use in fill-info in ellipses and circles."
  (let ((new-item (make-vector 3 x)))
    (aset new-item 1 y)
    (aset new-item 2 width)
    new-item))

(defsubst artist-fill-item-get-x (fill-item)
  "Retrieve the x component of a FILL-ITEM."
  (aref fill-item 0))

(defsubst artist-fill-item-set-x (fill-item new-x)
  "Set the x component of a FILL-ITEM to NEW-X."
  (aset fill-item 0 new-x)
  fill-item)

(defsubst artist-fill-item-get-y (fill-item)
  "Retrieve the y component of a FILL-ITEM."
  (aref fill-item 1))

(defsubst artist-fill-item-set-y (fill-item new-y)
  "Set the y component of a FILL-ITEM to NEW-Y."
  (aset fill-item 1 new-y)
  fill-item)

(defsubst artist-fill-item-get-width (fill-item)
  "Retrieve the width component of a FILL-ITEM."
  (aref fill-item 2))

(defsubst artist-fill-item-set-width (fill-item new-width)
  "Set the width component of a FILL-ITEM to NEW-WIDTH."
  (aset fill-item 2 new-width)
  fill-item)


(defun artist-ellipse-point-list-add-center (x-center y-center point-list)
  "Add offsets X-CENTER and Y-CENTER to coordinates in POINT-LIST."
  (mapcar
   (lambda (p)
     (artist-coord-set-x p (+ x-center (artist-coord-get-x p)))
     (artist-coord-set-y p (+ y-center (artist-coord-get-y p))))
   point-list))


(defun artist-ellipse-fill-info-add-center (x-center y-center fill-info)
  "Add offsets X-CENTER and Y-CENTER to fill-items in FILL-INFO."
  (mapcar
   (lambda (p)
     (artist-fill-item-set-x p (+ x-center (artist-fill-item-get-x p)))
     (artist-fill-item-set-y p (+ y-center (artist-fill-item-get-y p))))
   fill-info))

(defun artist-ellipse-remove-0-fills (fill-info)
  "Remove fill-infos from FILL-INFO that fills a zero-width field."
  (cond ((null fill-info)
	 nil)
	((= 0 (artist-fill-item-get-width (car fill-info)))
	 (artist-ellipse-remove-0-fills (cdr fill-info)))
	(t
	 (append (list (car fill-info))
		 (artist-ellipse-remove-0-fills (cdr fill-info))))))


(defun artist-ellipse-compute-fill-info (point-list)
  "Compute fill info for ellipse around 0,0 from POINT-LIST.
The POINT-LIST is expected to cover the first quadrant."
  (let ((first-half nil)
	(both-halves nil)
	(last-y nil))

    ;; Create first half (the lower one (since y grows downwards)) from
    ;; the first quadrant.
    (mapc
     (lambda (coord)
       (let* ((x         (artist-coord-get-x coord))
	      (y         (artist-coord-get-y coord))
	      (width     (max (- (* 2 x) 1) 0))
	      (left-edge (- x width)))
	 (if (or (null last-y) (not (= y last-y)))
	     ;; This was either the first time,
	     ;; or it was the first time on a new line
	     (setq first-half
		   (append first-half
			   ;; Fill info item starts at left-edge on line y
			   (list (artist-new-fill-item left-edge y width)))))
	 (setq last-y y)))
     point-list)

    ;; Create the other half by mirroring the first half.
    (setq both-halves
	  (append first-half
		  (mapcar
		   (lambda (i)
		     (artist-new-fill-item (artist-fill-item-get-x i)
					   (- (artist-fill-item-get-y i))
					   (artist-fill-item-get-width i)))
		   ;; The cdr below is so we don't include fill-info for
		   ;;; the middle line twice
		   (cdr (reverse first-half)))))
    (artist-ellipse-remove-0-fills both-halves)))


(defun artist-ellipse-mirror-quadrant (point-list)
  "Mirror a POINT-LIST describing first quadrant to create a complete ellipse."
  (let ((right-half nil)
	(left-half nil))

    ;; First, if last char in that quadrant is `/', then replace it with `)'
    ;; This way we avoids things
    ;;                      ---------                        ---------
    ;;                     /         \                      /         \
    ;; that look like:    \           /  instead we get:   (           )
    ;;                     \         /                      \         /
    ;;                      ---------                        ---------
    (let ((last-coord (car (last point-list))))
      (if (= (artist-coord-get-new-char last-coord) ?/)
	  (artist-coord-set-new-char last-coord artist-ellipse-right-char)))

    ;; Create the other part of the right half by mirroring the first part
    (setq right-half
	  (append
	   point-list
	   (mapcar
	    (lambda (coord)
	      (let ((c (artist-coord-get-new-char coord)))
		(artist-new-coord (artist-coord-get-x coord)
				  (- (artist-coord-get-y coord))
				  (cond ((= c ?/) ?\\)
					((= c ?\\) ?/)
					(t c)))))
	    ;; The cdr below is so we don't draw the middle right char twice
	    (cdr (reverse point-list)))))

    ;; Create the left half by mirroring the right half.
    (setq left-half
	  (mapcar
	   (lambda (coord)
	     (let ((c (artist-coord-get-new-char coord)))
	       (artist-new-coord (- (artist-coord-get-x coord))
				 (artist-coord-get-y coord)
				 (cond ((= c ?/)  ?\\)
				       ((= c ?\\) ?/)
				       ((= c artist-ellipse-right-char)
					artist-ellipse-left-char)
				       (t c)))))
	   ;; The cdr and butlast below is so we don't draw the middle top
	   ;; and middle bottom char twice.
	   (butlast (cdr (reverse right-half)))))
    (append right-half left-half)))


(defun artist-draw-ellipse-general (x1 y1 x-radius y-radius)
  "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS.

Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).

END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].

POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE].

Ellipses with zero Y-RADIUS are not drawn correctly."
  (let* ((point-list   (artist-ellipse-generate-quadrant x-radius y-radius))
	 (fill-info    (artist-ellipse-compute-fill-info point-list))
	 (shape-info   (make-vector 2 0)))

    (setq point-list (artist-calculate-new-chars point-list))
    (setq point-list (artist-ellipse-mirror-quadrant point-list))
    (setq point-list (artist-ellipse-point-list-add-center x1 y1 point-list))
    (setq fill-info (artist-ellipse-fill-info-add-center x1 y1 fill-info))

    ;; Draw the ellipse
    (setq point-list
	  (mapcar
	   (lambda (coord)
	     (artist-move-to-xy (artist-coord-get-x coord)
				(artist-coord-get-y coord))
	     (if artist-line-char-set
		 (artist-replace-char artist-line-char)
	       (artist-replace-char (artist-coord-get-new-char coord)))
	     coord)
	   (artist-modify-new-chars
	     (artist-save-chars-under-point-list point-list))))

    (aset shape-info 0 point-list)
    (aset shape-info 1 fill-info)
    (artist-make-2point-object (artist-make-endpoint x1 y1)
			       (artist-make-endpoint x-radius y-radius)
			       shape-info)))

(defun artist-draw-ellipse-with-0-height (x1 y1 x-radius y-radius)
  "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS.

Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).

END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].

POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE].

The Y-RADIUS must be 0, but the X-RADIUS must not be 0."
  (let ((point-list nil)
	(width      (max (- (abs (* 2 x-radius)) 1)))
	(left-edge  (1+ (- x1 (abs x-radius))))
	(line-char  (if artist-line-char-set artist-line-char ?-))
	(i          0)
	(point-list nil)
	(fill-info  nil)
	(shape-info (make-vector 2 0)))
    (while (< i width)
      (let* ((line-x (+ left-edge i))
	     (line-y y1)
	     (new-coord (artist-new-coord line-x line-y)))
	(artist-coord-add-saved-char new-coord
				     (artist-get-char-at-xy line-x line-y))
	(artist-move-to-xy line-x line-y)
	(artist-replace-char line-char)
	(setq point-list (append point-list (list new-coord)))
	(setq i (1+ i))))
    (aset shape-info 0 point-list)
    (aset shape-info 1 fill-info)
    (artist-make-2point-object (artist-make-endpoint x1 y1)
			       (artist-make-endpoint x-radius y-radius)
			       shape-info)))

(defun artist-draw-ellipse (x1 y1 x2 y2)
  "Draw an ellipse with center at X1, Y1 and point X2,Y2.

Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).

END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].

POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]."
  (let* ((artist-line-char (artist-compute-line-char))
         (artist-line-char-set artist-line-char)
         (width (abs (- x2 x1)))
	 (height (abs (- y2 y1)))
	 ;;
	 ;; When we draw our ellipse, we want it to go through the cursor
	 ;; position, but since x1,y1, x2,y2 marks the corners of one
	 ;; of the quadrants, we have to enlarge the ellipse a bit.
	 ;; Ok, so then why by sqrt(2)?
	 ;; It comes from the equation for the ellipse (where a is the
	 ;; x-radius and b is the y-radius):
	 ;;	f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0
	 ;; and the fact that we want the enlarged ellipse to have the
	 ;; same proportions as the smaller square, therefore we have:
	 ;;     a/b = x/y
	 ;; Solving this yields a-in-larger-ellipse = a-in-smaller * sqrt(2)
	 (x-radius (round (* width (sqrt 2))))
	 (y-radius (round (* height (sqrt 2))))
	 (x x1)
	 (y y1))
    (if (and (= y1 y2) (not (= x1 x2)))
	(artist-draw-ellipse-with-0-height x y x-radius y-radius)
      (artist-draw-ellipse-general x y x-radius y-radius))))


(defun artist-undraw-ellipse (ellipse)
  "Undraw ELLIPSE."
  (if ellipse
      (let ((point-list (aref (artist-2point-get-shapeinfo ellipse) 0)))
	(mapcar
	 (lambda (coord)
	   (artist-move-to-xy (artist-coord-get-x coord)
			      (artist-coord-get-y coord))
	   (artist-replace-char (artist-coord-get-saved-char coord))
	   coord)
	 point-list))))


(defun artist-draw-circle (x1 y1 x2 y2)
  "Draw a circle with center at X1, Y1 and point X2,Y2.

Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).

END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].

POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]."
  (let* ((artist-line-char (artist-compute-line-char))
         (artist-line-char-set artist-line-char)
         (width (abs (- x2 x1)))
	 (height (abs (- y2 y1)))
	 ;; When drawing our circle, we want it to through the cursor
	 ;; just as when drawing the ellipse, but we have to take
	 ;; care for the aspect-ratio.
	 ;; The equation for the ellipse  (where a is the x-radius and
	 ;; b is the y-radius):
	 ;;	f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0
	 ;; together with the relationship
	 ;;     a = aspect-ratio * b
	 ;; gives
	 ;; a = sqrt( x^2 + (aspect-ratio * y)^2 ) and
	 ;; b = a / aspect-ratio
	 (x-radius (round (sqrt (+ (* width width)
				   (* (* artist-aspect-ratio height)
				      (* artist-aspect-ratio height))))))
	 (y-radius (round (/ x-radius artist-aspect-ratio))))
    (artist-draw-ellipse-general x1 y1 x-radius y-radius)))

(defalias 'artist-undraw-circle 'artist-undraw-ellipse)


;
; Filling ellipses
;
(defun artist-fill-ellipse (ellipse x y x-radius y-radius)
  "Fill an ELLIPSE centered at X,Y with radius X-RADIUS and Y-RADIUS."
  (let ((fill-info (aref (artist-2point-get-shapeinfo ellipse) 1)))
    (mapcar
     (lambda (fill-item)
       (artist-move-to-xy (artist-fill-item-get-x fill-item)
			  (artist-fill-item-get-y fill-item))
       (artist-replace-chars artist-fill-char
			    (artist-fill-item-get-width fill-item))
       fill-item)
     fill-info)))

(defalias 'artist-fill-circle 'artist-fill-ellipse)


;;
;; Cutting, copying and pasting rectangles and squares
;; (filling functions)
;;

(defun artist-cut-rect (rect x1 y1 x2 y2)
  "Copy rectangle RECT drawn from X1, Y1 to X2, Y2, then clear it."
  (artist-undraw-rect rect)
  (artist-copy-generic x1 y1 x2 y2)
  (artist-erase-rect rect x1 y1 x2 y2))

(defun artist-cut-square (square x1 y1 x2 y2)
  "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (made square), then clears it."
  (artist-undraw-square square)
  (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
	 (new-x1 (elt square-corners 0))
	 (new-y1 (elt square-corners 1))
	 (new-x2 (elt square-corners 2))
	 (new-y2 (elt square-corners 3)))
    (artist-copy-generic new-x1 new-y1 new-x2 new-y2)
    (artist-erase-rect square new-x1 new-y1 new-x2 new-y2)))


(defun artist-get-buffer-contents-at-xy (x y width)
  "Retrieve contents from the buffer at X, Y.  WIDTH characters are returned."
  (artist-move-to-xy x y)
  (let ((here (point))
	(there (save-excursion (artist-move-to-xy (+ x width) y) (point))))
    (untabify here there)
    (setq there (save-excursion (artist-move-to-xy (+ x width) y) (point)))
    (buffer-substring here there)))


(defun artist-copy-generic (x1 y1 x2 y2)
  "Copy a rectangular area with corners at X1, Y1 and X2, Y2.
Output is a copy buffer, a list of strings, representing the
original contents of that area in the buffer."
  (let* ((x    	(min x1 x2))
	 (y    	(min y1 y2))
	 (x-max (max x1 x2))
	 (y-max (max y1 y2))
	 (w     (+ (- x-max x) 1))
	 (l     nil))
    (while (<= y y-max)
      (setq l (cons (artist-get-buffer-contents-at-xy x y w) l))
      (setq y (1+ y)))
    (if artist-interface-with-rect
	(setq killed-rectangle (reverse l))
      (setq artist-copy-buffer (reverse l)))))


(defun artist-copy-rect (rect x1 y1 x2 y2)
  "Copy rectangle RECT drawn from X1, Y1 to X2, Y2."
  (artist-undraw-rect rect)
  (artist-copy-generic x1 y1 x2 y2))

(defun artist-copy-square (square x1 y1 x2 y2)
  "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (but made square)."
  (artist-undraw-square square)
  (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
	 (new-x1 (elt square-corners 0))
	 (new-y1 (elt square-corners 1))
	 (new-x2 (elt square-corners 2))
	 (new-y2 (elt square-corners 3)))
    (artist-copy-generic new-x1 new-y1 new-x2 new-y2)))

(defun artist-paste (x y)
  "Paste the contents of the copy-buffer at X,Y."
  (let ((copy-buf (if artist-interface-with-rect
		      killed-rectangle
		    artist-copy-buffer)))
    (if (not (null copy-buf))
	(while (not (null copy-buf))
	  (artist-move-to-xy x y)
	  (artist-replace-string (car copy-buf))
	  (setq copy-buf (cdr copy-buf))
	  (setq y (1+ y)))
      (message "Nothing to paste"))))


;;
;; Flood filling
;;
(defun artist-ff-too-far-right (x)
  "Determine if the position X is too far to the right."
  (cond ((numberp artist-flood-fill-right-border)
	 (> x artist-flood-fill-right-border))
	((eq artist-flood-fill-right-border 'window-width)
	 (> x (- (window-width) 2)))
	((eq artist-flood-fill-right-border 'fill-column)
	 (> x fill-column))
	(t (error "Invalid value for `artist-flood-fill-right-border'"))))

(defun artist-ff-get-rightmost-from-xy (x y)
  "Find the rightmost position in this run, starting at X, Y."
  (save-excursion
    (let ((char-at-xy (artist-get-char-at-xy-conv x y))
	  (last-x x))
      (setq x (1+ x))
      (while (and (not (artist-ff-too-far-right x))
		  (= char-at-xy (artist-get-char-at-xy-conv x y)))
	(setq last-x x)
	(setq x (1+ x)))
      last-x)))

(defun artist-ff-is-topmost-line (x y)
  "Determine whether the position X,Y is on the topmost line or not."
  (= y 0))

(defun artist-ff-is-bottommost-line (x y)
  "Determine whether the position X,Y is on the bottommost line or not."
  (save-excursion
    (goto-char (point-max))
    (beginning-of-line)
    (let ((last-line (artist-current-line)))
      (if (= (point) (point-max))

	  ;; Last line is empty, don't paint on it, report previous line
	  ;; as last line
	  (>= y (- last-line 1))
        (>= y last-line)))))

(defun artist-flood-fill (x1 y1)
  "Flood-fill starting at X1, Y1.  Fill with the char in `artist-fill-char'."
  (let ((stack nil)
	(input-queue nil)
	;; We are flood-filling the area that has this character.
	(c     (artist-get-char-at-xy-conv x1 y1))
        (artist-fill-char (if artist-fill-char-set
                              artist-fill-char
                            artist-default-fill-char)))

    ;; Fill only if the fill-char is not the same as the character whose
    ;; area we are about to fill, or, in other words, don't fill if we
    ;; needn't.
    (if (not (= c artist-fill-char))
	(push (artist-new-coord x1 y1) stack))

    (while (not (null stack))
      (let* ((coord (pop stack))
	     (x (artist-coord-get-x coord))
	     (y (artist-coord-get-y coord))

	     ;; Here we keep track of the leftmost and rightmost position
	     ;; for this run
	     (x-leftmost 0)
	     (x-rightmost 0)
	     (last-x 0)

	     ;; Remember if line above and below are accessible
	     ;; Lines below the last one, and prior to the first-one
	     ;; are not accessible.
	     (lines-above nil)
	     (lines-below nil)

	     ;; Remember char for position on line above and below, so we
	     ;; can find the rightmost positions on the runs.
	     (last-c-above -1)
	     (last-c-below -1))

	(setq x-rightmost (artist-ff-get-rightmost-from-xy x y))
	(setq lines-above (not (artist-ff-is-topmost-line x y)))
	(setq lines-below (not (artist-ff-is-bottommost-line x y)))
	(setq last-x x-rightmost)
	(setq x x-rightmost)

	;; Search line above, push rightmost positions of runs for that line
	(while (and (>= x 0) (= c (artist-get-char-at-xy-conv x y)))
	  (if lines-above
	      (let ((c-above (artist-get-char-at-xy-conv x (- y 1))))
		(if (and (= c-above c) (/= c-above last-c-above))
		    (push (artist-new-coord x (- y 1)) stack))
		(setq last-c-above c-above)))
	  (setq last-x x)
	  (setq x (- x 1)))

	;; Remember the left-most position on this run
	(setq x-leftmost last-x)

	;; Search line below, push rightmost positions of runs for that line
	(setq x x-rightmost)
	(while (>= x x-leftmost)
	  (if lines-below
	      (let ((c-below (artist-get-char-at-xy-conv x (1+ y))))
		(if (and (= c-below c) (/= c-below last-c-below))
		    (push (artist-new-coord x (1+ y)) stack))
		(setq last-c-below c-below)))
	  (setq x (- x 1)))

	(artist-move-to-xy x-leftmost y)
	(artist-replace-chars artist-fill-char (1+ (- x-rightmost x-leftmost)))

	;; If we are to show incrementally, we have to remove any pending
	;; input from the input queue, because processing of pending input
	;; always has priority over display updates (although this input
	;; won't be processed until we are done). Later on we will queue
	;; the input on the input queue again.
	(if artist-flood-fill-show-incrementally
	    (progn
	      (if (input-pending-p)
		  (discard-input))
	      (artist-update-display)))))))

;;
;; Accessors to arrow-points
;;

(defun artist-make-arrow-point (x y direction &optional state)
  "Create an arrow point at X, Y for a line in direction DIRECTION.
Optional argument STATE can be used to set state (default is nil)."
  (save-excursion
    (let* ((arrow-point  (make-vector 4 0))
	   (arrow-marker (make-marker)))
      (artist-move-to-xy x y)
      (set-marker arrow-marker (point))
      (aset arrow-point 0 arrow-marker)
      (aset arrow-point 1 (artist-get-char-at-xy x y))
      (aset arrow-point 2 direction)
      (aset arrow-point 3 state)
      arrow-point)))

(defsubst artist-arrow-point-get-marker (arrow-point)
  "Retrieve the marker component of an ARROW-POINT."
  (aref arrow-point 0))

(defsubst artist-arrow-point-get-orig-char (arrow-point)
  "Retrieve the orig char component of an ARROW-POINT."
  (aref arrow-point 1))

(defsubst artist-arrow-point-get-direction (arrow-point)
  "Retrieve the direction component of an ARROW-POINT."
  (aref arrow-point 2))

(defsubst artist-arrow-point-get-state (arrow-point)
  "Retrieve the state component of an ARROW-POINT."
  (aref arrow-point 3))

(defsubst artist-arrow-point-set-state (arrow-point new-state)
  "Set the state component of an ARROW-POINT to NEW-STATE."
  (aset arrow-point 3 new-state))


(defun artist-clear-arrow-points ()
  "Clear current endpoints."
  (setq artist-arrow-point-1 nil)
  (setq artist-arrow-point-2 nil))

(defun artist-set-arrow-points-for-poly (point-list)
  "Generic function for setting arrow-points for poly-shapes from POINT-LIST."
  (let* ((ep1   (elt point-list 0))
	 (ep2   (elt point-list 1))
	 (x1    (artist-endpoint-get-x ep1))
	 (y1    (artist-endpoint-get-y ep1))
	 (x2    (artist-endpoint-get-x ep2))
	 (y2    (artist-endpoint-get-y ep2))
	 (dir1  (artist-find-direction x2 y2 x1 y1))
	 (epn   (car (last point-list)))
	 (epn-1 (car (last point-list 2)))
	 (xn    (artist-endpoint-get-x epn))
	 (yn    (artist-endpoint-get-y epn))
	 (xn-1  (artist-endpoint-get-x epn-1))
	 (yn-1  (artist-endpoint-get-y epn-1))
	 (dirn  (artist-find-direction xn-1 yn-1 xn yn)))
    (setq artist-arrow-point-1 (artist-make-arrow-point x1 y1 dir1))
    (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn))))


(defun artist-set-arrow-points-for-2points (shape x1 y1 x2 y2)
  "Generic function for setting arrow-points for 2-point shapes.
The 2-point shape SHAPE is drawn from X1, Y1 to X2, Y2."
  (let* ((endpoint1 (artist-2point-get-endpoint1 shape))
	 (endpoint2 (artist-2point-get-endpoint2 shape))
	 (x1        (artist-endpoint-get-x endpoint1))
	 (y1        (artist-endpoint-get-y endpoint1))
	 (x2        (artist-endpoint-get-x endpoint2))
	 (y2        (artist-endpoint-get-y endpoint2)))
    (setq artist-arrow-point-1
	  (artist-make-arrow-point x1 y1
				   (artist-find-direction x2 y2 x1 y1)))
    (setq artist-arrow-point-2
	  (artist-make-arrow-point x2 y2
				   (artist-find-direction x1 y1 x2 y2)))))


;;
;; Common routine for drawing/undrawing shapes based
;; on the draw-how
;;

(defun artist-key-undraw-continously (x y)
  "Undraw current continuous shape with point at X, Y."
  ;; No undraw-info for continuous shapes
  nil)

(defun artist-key-undraw-poly (x y)
  "Undraw current poly shape with point at X, Y."
  (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
	(x1        (artist-endpoint-get-x artist-key-endpoint1))
	(y1        (artist-endpoint-get-y artist-key-endpoint1)))
    (artist-funcall undraw-fn artist-key-shape)))

(defun artist-key-undraw-1point (x y)
  "Undraw current 1-point shape at X, Y."
  ;; No undraw-info for 1-point shapes
  nil)

(defun artist-key-undraw-2points (x y)
  "Undraw current 2-point shape at X, Y."
  (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
	(x1        (artist-endpoint-get-x artist-key-endpoint1))
	(y1        (artist-endpoint-get-y artist-key-endpoint1)))
    (artist-funcall undraw-fn artist-key-shape)))

(defun artist-key-undraw-common ()
  "Common routine undrawing current shape."
 (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
       (col      (artist-current-column))
       (row      (artist-current-line)))

    ;; Depending on what we are currently drawing, call other routines
    ;; that knows how to do the job
    ;;
    (cond ((eq draw-how 'artist-do-continously)
	   (artist-key-undraw-continously col row))
	  ((eq draw-how 'artist-do-poly)
	   (artist-key-undraw-poly col row))
	  ((and (numberp draw-how) (= draw-how 1))
	   (artist-key-undraw-1point col row))
	  ((and (numberp draw-how) (= draw-how 2))
	   (artist-key-undraw-2points col row))
	  (t (message "Undrawing \"%s\"s is not yet implemented" draw-how)))

    ;; Now restore the old position
    ;;
    (artist-move-to-xy col row)))



;; Implementation note: This really should honor the interval-fn entry
;; in the master table, `artist-mt', which would mean leaving a timer
;; that calls `draw-fn' every now and then. That timer would then have
;; to be canceled and reinstalled whenever the user moves the cursor.
;; This could be done, but what if the user suddenly switches to another
;; drawing mode, or even kills the buffer! In the mouse case, it is much
;; simpler: when at the end of `artist-mouse-draw-continously', the
;; user has released the button, so the timer will always be canceled
;; at that point.
(defun artist-key-draw-continously (x y)
  "Draw current continuous shape at X,Y."
  (let ((draw-fn   (artist-go-get-draw-fn-from-symbol artist-curr-go)))
    (setq artist-key-shape (artist-funcall draw-fn x y))))

(defun artist-key-draw-poly (x y)
  "Draw current poly-point shape with nth point at X,Y."
  (let ((draw-fn   (artist-go-get-draw-fn-from-symbol artist-curr-go))
	(x1        (artist-endpoint-get-x artist-key-endpoint1))
	(y1        (artist-endpoint-get-y artist-key-endpoint1)))
    (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y))))

(defun artist-key-draw-1point (x y)
  "Draw current 1-point shape at X,Y."
  (let ((draw-fn   (artist-go-get-draw-fn-from-symbol artist-curr-go)))
    (setq artist-key-shape (artist-funcall draw-fn x y))))


(defun artist-key-draw-2points (x y)
  "Draw current 2-point shape at X,Y."
  (let ((draw-fn   (artist-go-get-draw-fn-from-symbol artist-curr-go))
	(x1        (artist-endpoint-get-x artist-key-endpoint1))
	(y1        (artist-endpoint-get-y artist-key-endpoint1)))
    (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y))))

(defun artist-key-draw-common ()
  "Common routine for drawing current shape."
  (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
	(col      (artist-current-column))
	(row      (artist-current-line)))

    ;; Depending on what we are currently drawing, call other routines
    ;; that knows how to do the job
    ;;
    (cond ((eq draw-how 'artist-do-continously)
	   (artist-key-draw-continously col row))
	  ((eq draw-how 'artist-do-poly)
	   (artist-key-draw-poly col row))
	  ((and (numberp draw-how) (= draw-how 1))
	   (artist-key-draw-1point col row))
	  ((and (numberp draw-how) (= draw-how 2))
	   (artist-key-draw-2points col row))
	  (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))

    ;; Now restore the old position
    ;;
    (artist-move-to-xy col row)))



;;
;; Functions related to trimming line-endings
;; The region between the topmost and bottommost visited line is
;; called a draw-region.
;;

(defun artist-draw-region-reset ()
  "Reset the current draw-region."
  (setq artist-draw-region-max-y 0)
  (setq artist-draw-region-min-y 1000000))

(defun artist-draw-region-trim-line-endings (min-y max-y)
  "Trim lines in current draw-region from MIN-Y to MAX-Y.
Trimming here means removing white space at end of a line."
  ;; Safety check: switch min-y and max-y if max-y is smaller
  (if (< max-y min-y)
      (let ((tmp min-y))
	(setq min-y max-y)
	(setq max-y tmp)))
  (save-excursion
    (let ((curr-y min-y))
      (while (<= curr-y max-y)
	(artist-move-to-xy 0 curr-y)
	(end-of-line)
	(delete-horizontal-space)
	(setq curr-y (1+ curr-y))))))

;;
;; Drawing shapes by using keys
;;

(defun artist-key-do-continously-continously (x y)
  "Update current continuous shape at X,Y."
  (let ((draw-fn   (artist-go-get-draw-fn-from-symbol artist-curr-go)))
    (artist-funcall draw-fn x y)))


(defun artist-key-do-continously-poly (x y)
  "Update current poly-point shape with nth point at X,Y."
  (let ((draw-fn   (artist-go-get-draw-fn-from-symbol artist-curr-go))
	(undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
	(x1        (artist-endpoint-get-x artist-key-endpoint1))
	(y1        (artist-endpoint-get-y artist-key-endpoint1))
	(x2        x)
	(y2        y))
    ;; If not rubber-banding, then move the 2
    ;; Otherwise re-draw the shape to the new position
    ;;
    (if (not artist-rubber-banding)
	(progn
	  (artist-no-rb-unset-point2)
	  (artist-no-rb-set-point2 x y))
      (progn
	(artist-funcall undraw-fn artist-key-shape)
	(setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))))


(defun artist-key-do-continously-1point (x y)
  "Update current 1-point shape at X,Y."
  ;; Nothing to do continuously for operations
  ;; where we have only one input point
  nil)

(defun artist-key-do-continously-2points (x y)
  "Update current 2-point shape with 2nd point at X,Y."
  (let ((draw-fn   (artist-go-get-draw-fn-from-symbol artist-curr-go))
	(undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
	(x1        (artist-endpoint-get-x artist-key-endpoint1))
	(y1        (artist-endpoint-get-y artist-key-endpoint1))
	(x2        x)
	(y2        y))
    ;; If not rubber-banding, then move the 2
    ;; Otherwise re-draw the shape to the new position
    ;;
    (if (not artist-rubber-banding)
	(progn
	  (artist-no-rb-unset-point2)
	  (artist-no-rb-set-point2 x y))
      (progn
	(artist-funcall undraw-fn artist-key-shape)
	(setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))))


(defun artist-key-do-continously-common ()
  "Common routine for updating current shape."
  (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
	(col      (artist-current-column))
	(row      (artist-current-line)))

    ;; Depending on what we are currently drawing, call other routines
    ;; that knows how to do the job
    ;;
    (cond ((eq draw-how 'artist-do-continously)
	   (artist-key-do-continously-continously col row))
	  ((eq draw-how 'artist-do-poly)
	   (artist-key-do-continously-poly col row))
	  ((and (numberp draw-how) (= draw-how 1))
	   (artist-key-do-continously-1point col row))
	  ((and (numberp draw-how) (= draw-how 2))
	   (artist-key-do-continously-2points col row))
	  (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))

    ;; Now restore the old position
    ;;
    (artist-move-to-xy col row)))


(defun artist-key-set-point-continously (x y)
  "Set point for current continuous shape at X,Y."
  ;; Maybe set arrow-points for continuous shapes
  (let ((arrow-pred   (artist-go-get-arrow-pred-from-symbol artist-curr-go))
	(arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go))
	(init-fn      (artist-go-get-init-fn-from-symbol artist-curr-go))
	(prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
	(exit-fn      (artist-go-get-exit-fn-from-symbol artist-curr-go)))

    (if (not artist-key-is-drawing)
	;; *** We are about to begin drawing
	(progn
	  (artist-funcall init-fn x y))

      ;; *** We are about to stop drawing
      (progn

	(artist-funcall prep-fill-fn x y)
	(if (artist-funcall arrow-pred)
	    (artist-funcall arrow-set-fn x y)
	  (artist-clear-arrow-points))
	(artist-funcall exit-fn x y))))

  ;; Toggle the is-drawing flag
  (setq artist-key-is-drawing (not artist-key-is-drawing)))



(defun artist-key-set-point-poly (x y &optional this-is-last-point)
  "Set point for current poly-point shape at X,Y.
If optional argument THIS-IS-LAST-POINT is non-nil, this point is the last."
  (let ((draw-fn      (artist-go-get-draw-fn-from-symbol artist-curr-go))
	(init-fn      (artist-go-get-init-fn-from-symbol artist-curr-go))
	(prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
	(exit-fn      (artist-go-get-exit-fn-from-symbol artist-curr-go))
	(fill-pred    (artist-go-get-fill-pred-from-symbol artist-curr-go))
	(fill-fn      (artist-go-get-fill-fn-from-symbol artist-curr-go))
	(arrow-pred   (artist-go-get-arrow-pred-from-symbol artist-curr-go))
	(arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))

    (if (not artist-key-is-drawing)

	;; *** We were not drawing ==> set first point
	(progn

	  (artist-funcall init-fn x y)

	  ;; If not rubber-banding, set first point.
	  ;; Otherwise, draw the shape from x,y to x,y
	  (if (not artist-rubber-banding)
	      (artist-no-rb-set-point1 x y)
	    (setq artist-key-shape (artist-funcall draw-fn x y x y)))

	  ;; Set first endpoint
	  (setq artist-key-endpoint1 (artist-make-endpoint x y))

	  ;; Set point-list to contain start point
	  (setq artist-key-poly-point-list (list (artist-make-endpoint x y)))

	  ;; Since we are not ready, set the arrow-points to nil
	  (artist-clear-arrow-points)

	  ;; Change state to drawing
	  (setq artist-key-is-drawing t)

	  ;; Feedback
	  (message "%s" (substitute-command-keys
		    (concat "First point set. "
			    "Set next with \\[artist-key-set-point], "
			    "set last with C-u \\[artist-key-set-point]"))))


      ;; *** We were drawing ==> we are about to set nth point
      ;;     (last point if the argument this-is-last-point is non-nil)
      ;;
      (let ((x1 (artist-endpoint-get-x artist-key-endpoint1))
	    (y1 (artist-endpoint-get-y artist-key-endpoint1))
	    (x2 x)
	    (y2 y))

	;; If not rubber-banding, undraw the 1's and 2's, then
	;; draw the shape (if we were rubber-banding, then the
	;; shape is already drawn in artist-key-do-continously-2points.)
	;;
	(if (not artist-rubber-banding)
	    (progn
	      (artist-no-rb-unset-points)
	      (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))

	;; Set x2 and y2 from shape's second point
	;; (which might be different from the mouse's second point,
	;; if, for example, we are drawing a straight line)
	;;
	(if (not (null artist-key-shape))
	    (let ((endpoint2 (artist-2point-get-endpoint2 artist-key-shape)))
	      (setq x2 (artist-endpoint-get-x endpoint2))
	      (setq y2 (artist-endpoint-get-y endpoint2))))

	;; Add the endpoint to the list of poly-points
	(setq artist-key-poly-point-list
	      (append artist-key-poly-point-list
		      (list (artist-make-endpoint x2 y2))))

	;; Now do handle the case when this is the last point,
	;; and the case when this point isn't the last
	;;
	(if (not this-is-last-point)
	    ;; ** This is not the last point
	    (progn
	      ;; Start drawing a new 2-point-shape from last endpoint.

	      ;; First set the start-point
	      (setq x1 x2)
	      (setq y1 y2)
	      (setq artist-key-endpoint1 (artist-make-endpoint x1 y1))

	      ;; If we are not rubber-banding, then place the '1
	      ;; Otherwise, draw the shape from x1,y1 to x1,y1
	      (if (not artist-rubber-banding)
		  (artist-no-rb-set-point1 x1 y1)
		(setq artist-key-shape (artist-funcall draw-fn x1 y1 x1 y1)))

	      ;; Feedback
	      (message "Point set"))

	  ;; ** This is the last point
	  (progn

	    (artist-funcall prep-fill-fn artist-key-poly-point-list)

	    ;; Maybe fill
	    (if (artist-funcall fill-pred)
		(artist-funcall fill-fn artist-key-shape
				artist-key-poly-point-list))

	    ;; Set the arrow-points
	    (if (artist-funcall arrow-pred)
		(artist-funcall arrow-set-fn artist-key-poly-point-list)
	      (artist-clear-arrow-points))

	    (artist-funcall exit-fn artist-key-poly-point-list)

	    ;; Change state to not drawing
	    (setq artist-key-shape nil)
	    (setq artist-key-endpoint1 nil)
	    (setq artist-key-is-drawing nil)))))))


(defun artist-key-set-point-1point (x y)
  "Set point for current 1-point shape at X,Y."
  (let ((draw-fn      (artist-go-get-draw-fn-from-symbol artist-curr-go))
	(init-fn      (artist-go-get-init-fn-from-symbol artist-curr-go))
	(prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
	(exit-fn      (artist-go-get-exit-fn-from-symbol artist-curr-go))
	(draw-fn      (artist-go-get-draw-fn-from-symbol artist-curr-go))
	(arrow-pred   (artist-go-get-arrow-pred-from-symbol artist-curr-go))
	(arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
    (artist-funcall init-fn x y)
    (artist-funcall draw-fn x y)
    (artist-funcall prep-fill-fn x y)
    (if (artist-funcall arrow-pred)
	(artist-funcall arrow-set-fn x y)
      (artist-clear-arrow-points))
    (artist-funcall exit-fn x y))
  (setq artist-key-shape nil)
  (setq artist-key-is-drawing nil))


(defun artist-key-set-point-2points (x y)
  "Set first or second point in current 2-point shape at X,Y."
  (let ((draw-fn      (artist-go-get-draw-fn-from-symbol artist-curr-go))
	(init-fn      (artist-go-get-init-fn-from-symbol artist-curr-go))
	(prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
	(exit-fn      (artist-go-get-exit-fn-from-symbol artist-curr-go))
	(fill-pred    (artist-go-get-fill-pred-from-symbol artist-curr-go))
	(fill-fn      (artist-go-get-fill-fn-from-symbol artist-curr-go))
	(arrow-pred   (artist-go-get-arrow-pred-from-symbol artist-curr-go))
	(arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
    (if (not artist-key-is-drawing)

	;; *** We were not drawing ==> set first point
	(progn

	  (artist-funcall init-fn x y)

	  ;; If not rubber-banding, set first point.
	  ;; Otherwise, draw the shape from x,y to x,y
	  (if (not artist-rubber-banding)
	      (artist-no-rb-set-point1 x y)
	    (setq artist-key-shape (artist-funcall draw-fn x y x y)))

	  ;; Set first endpoint
	  (setq artist-key-endpoint1 (artist-make-endpoint x y))

	  ;; Since we are not ready, clear the arrow-points
	  (artist-clear-arrow-points)

	  ;; Change state to drawing
	  (setq artist-key-is-drawing t))

      ;; *** We were drawing ==> we are about to set 2nd point
      ;;     and end the drawing operation

      (let ((x1 (artist-endpoint-get-x artist-key-endpoint1))
	    (y1 (artist-endpoint-get-y artist-key-endpoint1))
	    (x2 x)
	    (y2 y))

	;; If not rubber-banding, undraw the 1's and 2's, then
	;; draw the shape (if we were rubber-banding, then the
	;; shape is already drawn in artist-key-do-continously-2points.)
	;;
	(if (not artist-rubber-banding)
	    (progn
	      (artist-no-rb-unset-points)
	      (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))

	(artist-funcall prep-fill-fn artist-key-shape x1 y1 x2 y2)

	;; Maybe fill
	;;
	(if (artist-funcall fill-pred)
	    (artist-funcall fill-fn artist-key-shape x1 y1 x2 y2))

	;; Maybe set the arrow-points
	;;
	(if (artist-funcall arrow-pred)
	    (artist-funcall arrow-set-fn artist-key-shape x1 y1 x2 y2)
	  (artist-clear-arrow-points))

	(artist-funcall exit-fn artist-key-shape x1 y1 x2 y2)

	;; Change state to not drawing
	(setq artist-key-is-drawing nil)))))


(defun artist-key-set-point-common (arg)
  "Common routine for setting point in current shape.
With non-nil ARG, set the last point."
  (let ((draw-how    (artist-go-get-draw-how-from-symbol artist-curr-go))
	(col         (artist-current-column))
	(row         (artist-current-line))
	(was-drawing artist-key-is-drawing))

    ;; First, if we are about to draw, then reset the draw-region
    (if (not artist-key-is-drawing)
	(artist-draw-region-reset))

    ;; Depending on what we are currently drawing, call other routines
    ;; that knows how to do the job
    ;;
    (cond ((eq draw-how 'artist-do-continously)
	   (artist-key-set-point-continously col row)
	   ;; Do this now, otherwise nothing will happen until we move.
	   (artist-key-do-continously-continously col row))
	  ((eq draw-how 'artist-do-poly)
	   (artist-key-set-point-poly col row arg))
	  ((and (numberp draw-how) (= draw-how 1))
	   (artist-key-set-point-1point col row))
	  ((and (numberp draw-how) (= draw-how 2))
	   (artist-key-set-point-2points col row))
	  (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))

    ;; Maybe trim line endings
    (if (and artist-trim-line-endings
	     was-drawing
	     (not artist-key-is-drawing))
	(artist-draw-region-trim-line-endings artist-draw-region-min-y
					      artist-draw-region-max-y))

    ;; Now restore the old position
    ;;
    (artist-move-to-xy col row)
    (artist-mode-line-show-curr-operation artist-key-is-drawing)))

;;
;; Key navigation
;;

(defun artist-previous-line (&optional n)
  "Move cursor up N lines (default is 1), updating current shape.
If N is negative, move cursor down."
  (interactive "p")
  (let ((col (artist-current-column)))
    (forward-line (- n))
    (move-to-column col t))
  (when artist-key-is-drawing
    (artist-key-do-continously-common)))


(defun artist-next-line (&optional n)
  "Move cursor down N lines (default is 1), updating current shape.
If N is negative, move cursor up."
  (interactive "p")
  (let ((col (artist-current-column)))
    (forward-line n)
    (move-to-column col t))
  (when artist-key-is-drawing
    (artist-key-do-continously-common)))

(defun artist-backward-char (&optional n)
  "Move cursor backward N chars (default is 1), updating current shape.
If N is negative, move forward."
  (interactive "p")
  (if (> n 0)
      (artist-forward-char (- n))
    (artist-forward-char n)))

(defun artist-forward-char (&optional n)
  "Move cursor forward N chars (default is 1), updating current shape.
If N is negative, move backward."
  (interactive "p")
  (let* ((step-x   (if (>= n 0) 1 -1))
	 (distance (abs n))
	 (curr-col (artist-current-column))
	 (new-col  (max 0 (+ curr-col (* distance step-x)))))
    (if (not artist-key-is-drawing)
	  (move-to-column new-col t)
      (move-to-column new-col t)
      (artist-key-do-continously-common))))


(defun artist-key-set-point (&optional arg)
  "Set a point for the current shape.  With optional ARG, set the last point."
  (interactive "P")
  (artist-key-set-point-common arg))


(defun artist-select-fill-char (c)
  "Set current fill character to be C."
  (interactive "cType fill char (type RET to turn off): ")
  (cond ((eq c ?\r) (setq artist-fill-char-set nil)
		    (message "Fill canceled"))
	(t	    (setq artist-fill-char-set t)
		    (setq artist-fill-char c)
		    (message "Fill set to \"%c\"" c))))


(defun artist-select-line-char (c)
  "Set current line character to be C."
  (interactive "cType line char (type RET to turn off): ")
  (cond ((eq c ?\r) (setq artist-line-char-set nil)
		    (message "Normal lines"))
	(t	    (setq artist-line-char-set t)
		    (setq artist-line-char c)
		    (message "Line drawn with \"%c\"" c)))
  (if artist-key-is-drawing
      (artist-key-do-continously-common)))


(defun artist-select-erase-char (c)
  "Set current erase character to be C."
  (interactive "cType char to use when erasing (type RET for normal): ")
  (cond ((eq c ?\r) (setq artist-erase-char ?\s)
		    (message "Normal erasing"))
	(t	    (setq artist-erase-char c)
		    (message "Erasing with \"%c\"" c)))
  (if artist-key-is-drawing
      (artist-key-do-continously-common)))

(defun artist-charlist-to-string (char-list)
  "Convert a list of characters, CHAR-LIST, to a string."
  (concat char-list))

(defun artist-string-to-charlist (str)
  "Convert a string, STR, to list of characters."
  (append str nil))

(defun artist-select-spray-chars (chars initial-char)
  "Set current spray characters to be CHARS, starting with INITIAL-CHAR."
  ;; This huge unreadable `interactive'-clause does the following
  ;; 1. Asks for a string of spray-characters
  ;; 2. Asks for the initial character (default is the first),
  ;;    and loops if the answer is not a char within the string in 1.
  (interactive
   (let* ((str  (read-string "Select spray-can characters, lightest first: "
			     (artist-charlist-to-string artist-spray-chars)))
	  (char-list (artist-string-to-charlist str))
	  (initial (let* ((err-msg "")
			  (ok nil)
			  (first-char-as-str (char-to-string (car char-list)))
			  (first-s) (first-c))
		     (while (not ok)
		       (setq first-s
			     (read-string
			      (format (concat "%sSelect initial-character, "
					      "one of \"%s\" (%s): ")
				      err-msg str first-char-as-str)))
		       (if (equal first-s "")
			   (setq first-s first-char-as-str))
		       (setq first-c (car (artist-string-to-charlist first-s)))
		       (setq ok (not (null (member first-c char-list))))
		       (if (not ok)
			   (setq err-msg (format
					  "Not in spray-chars: \"%s\". "
					  (char-to-string first-c)))))
		     first-c)))
     (list char-list initial)))
  (setq artist-spray-chars chars)
  (setq artist-spray-new-char initial-char)
  (message "Spray-chars set to \"%s\", initial: \"%s\""
	   (artist-charlist-to-string chars) (char-to-string initial-char)))


(defun artist-select-operation (op-str)
  "Select drawing operation OP-STR."
  (interactive (list (completing-read "Select operation: "
				      artist-key-compl-table)))
  (let* ((op-symbol (artist-mt-get-symbol-from-keyword op-str))
	 (draw-how  (if op-symbol
			(artist-go-get-draw-how-from-symbol op-symbol)
		      nil)))
    ;; First check that the string was valid
    (if (null op-symbol)
	(error "Unknown drawing method: %s" op-str))

    ;; Second, check that we are not about to switch to a different
    ;; kind of shape (do that only if we are drawing with keys;
    ;; otherwise this function cannot get called).
    (if (and artist-key-is-drawing
	     (not (equal artist-key-draw-how draw-how)))
	(error "Cannot switch to a different kind of shape while drawing"))

    ;; If we were drawing, undraw the shape
    (if (and artist-key-is-drawing
	     artist-rubber-banding)
	(artist-key-undraw-common))

    ;; Set the current operation and draw-how
    (setq artist-curr-go op-symbol)
    (setq artist-key-draw-how draw-how)

    ;; If we were drawing, redraw the shape (but don't if shape
    ;; is drawn by setting only one point)
    (if (and artist-key-is-drawing
	     artist-rubber-banding
	     (not (eq artist-key-draw-how 1)))
	(artist-key-draw-common)))

  ;; Feedback
  (artist-mode-line-show-curr-operation artist-key-is-drawing))


(defun artist-toggle-rubber-banding (&optional state)
  "Toggle rubber-banding.
If optional argument STATE is positive, turn rubber-banding on."
  (interactive)
  (if artist-key-is-drawing
      (error "Cannot toggle rubber-banding while drawing"))
  (if (setq artist-rubber-banding
	    (if (null state) (not artist-rubber-banding)
	      (> (prefix-numeric-value state) 0)))
      (message "Rubber-banding is now on")
    (message "Rubber-banding is now off")))


(defun artist-toggle-trim-line-endings (&optional state)
  "Toggle trimming of line-endings.
If optional argument STATE is positive, turn trimming on."
  (interactive)
  (if (setq artist-trim-line-endings
	    (if (null state) (not artist-trim-line-endings)
	      (> (prefix-numeric-value state) 0)))
      (message "Trimming is now on")
    (message "Trimming is now off")))


(defun artist-toggle-borderless-shapes (&optional state)
  "Toggle borders of shapes.
If optional argument STATE is positive, turn borders on."
  (interactive)
  (if (setq artist-borderless-shapes
	    (if (null state) (not artist-borderless-shapes)
	      (> (prefix-numeric-value state) 0)))
      (message "Borders are now off")
    (message "Borders are now on")))


(defun artist-toggle-first-arrow ()
  "Toggle first arrow for shape, if possible."
  (interactive)
  (save-excursion
    (if (not (null artist-arrow-point-1))
	(let* ((arrow-point  artist-arrow-point-1)
	       (arrow-state  (artist-arrow-point-get-state arrow-point))
	       (arrow-marker (artist-arrow-point-get-marker arrow-point))
	       (direction    (artist-arrow-point-get-direction arrow-point))
	       (orig-char    (artist-arrow-point-get-orig-char arrow-point))
	       (arrow-char   (aref artist-arrows direction))
	       (new-state    (not arrow-state)))

	  (goto-char (marker-position arrow-marker))

	  (if new-state
	      (if arrow-char
		  (artist-replace-char arrow-char))
	    (artist-replace-char orig-char))

	  (artist-arrow-point-set-state artist-arrow-point-1 new-state)))))

(defun artist-toggle-second-arrow ()
  "Toggle second arrow for shape, if possible."
  (interactive)
  (save-excursion
    (if (not (null artist-arrow-point-2))
	(let* ((arrow-point  artist-arrow-point-2)
	       (arrow-state  (artist-arrow-point-get-state arrow-point))
	       (arrow-marker (artist-arrow-point-get-marker arrow-point))
	       (direction    (artist-arrow-point-get-direction arrow-point))
	       (orig-char    (artist-arrow-point-get-orig-char arrow-point))
	       (arrow-char   (aref artist-arrows direction))
	       (new-state    (not arrow-state)))

	  (goto-char (marker-position arrow-marker))

	  (if new-state
	      (if arrow-char
		  (artist-replace-char arrow-char))
	    (artist-replace-char orig-char))

	  (artist-arrow-point-set-state artist-arrow-point-2 new-state)))))

(defun artist-select-op-pen-line ()
  "Select drawing pen lines."
  (interactive)
  (artist-select-operation "Pen Line"))

(defun artist-select-op-line ()
  "Select drawing lines."
  (interactive)
  (artist-select-operation "line"))

(defun artist-select-op-straight-line ()
  "Select drawing straight lines."
  (interactive)
  (artist-select-operation "straight line"))

(defun artist-select-op-rectangle ()
  "Select drawing rectangles."
  (interactive)
  (artist-select-operation "rectangle"))

(defun artist-select-op-square ()
  "Select drawing squares."
  (interactive)
  (artist-select-operation "square"))

(defun artist-select-op-poly-line ()
  "Select drawing poly-lines."
  (interactive)
  (artist-select-operation "poly-line"))

(defun artist-select-op-straight-poly-line ()
  "Select drawing straight poly-lines."
  (interactive)
  (artist-select-operation "straight poly-line"))

(defun artist-select-op-ellipse ()
  "Select drawing ellipses."
  (interactive)
  (artist-select-operation "ellipse"))

(defun artist-select-op-circle ()
  "Select drawing circles."
  (interactive)
  (artist-select-operation "circle"))

(defun artist-select-op-text-see-thru ()
  "Select rendering text (see thru)."
  (interactive)
  (artist-select-operation "text see-thru"))

(defun artist-select-op-text-overwrite ()
  "Select rendering text (overwrite)."
  (interactive)
  (artist-select-operation "text overwrite"))

(defun artist-select-op-spray-can ()
  "Select spraying."
  (interactive)
  (artist-select-operation "spray-can"))

(defun artist-select-op-spray-set-size ()
  "Select setting size for spraying."
  (interactive)
  (artist-select-operation "spray set size"))

(defun artist-select-op-erase-char ()
  "Select erasing characters."
  (interactive)
  (artist-select-operation "erase char"))

(defun artist-select-op-erase-rectangle ()
  "Select erasing rectangles."
  (interactive)
  (artist-select-operation "erase rectangle"))

(defun artist-select-op-vaporize-line ()
  "Select vaporizing single lines."
  (interactive)
  (artist-select-operation "vaporize line"))

(defun artist-select-op-vaporize-lines ()
  "Select vaporizing connected lines."
  (interactive)
  (artist-select-operation "vaporize lines"))

(defun artist-select-op-cut-rectangle ()
  "Select cutting rectangles."
  (interactive)
  (artist-select-operation "cut rectangle"))

(defun artist-select-op-cut-square ()
  "Select cutting squares."
  (interactive)
  (artist-select-operation "cut square"))

(defun artist-select-op-copy-rectangle ()
  "Select copying rectangles."
  (interactive)
  (artist-select-operation "copy rectangle"))

(defun artist-select-op-copy-square ()
  "Select copying squares."
  (interactive)
  (artist-select-operation "cut square"))

(defun artist-select-op-paste ()
  "Select pasting."
  (interactive)
  (artist-select-operation "paste"))

(defun artist-select-op-flood-fill ()
  "Select flood-filling."
  (interactive)
  (artist-select-operation "flood-fill"))


;; Drawing lines by using mouse
;; Mouse button actions
;;

(defun artist-update-pointer-shape ()
  "Perform the update of the X Windows pointer shape."
  (set-mouse-color nil))

(defvar x-pointer-shape)

(defun artist-set-pointer-shape (new-pointer-shape)
  "Set the shape of the X Windows pointer to NEW-POINTER-SHAPE."
  (setq x-pointer-shape new-pointer-shape)
  (artist-update-pointer-shape))

(defsubst artist-event-is-shifted (ev)
  "Check whether the shift-key is pressed in event EV."
  (memq 'shift (event-modifiers ev)))

(defun artist-do-nothing ()
  "Function that does nothing."
  (interactive))

(defun artist-compute-up-event-key (ev)
  "Compute the corresponding up key sequence for event EV."
 (let* ((basic (event-basic-type ev))
	(unshifted basic)
	(shifted (make-symbol (concat "S-" (symbol-name basic)))))
   (if (artist-event-is-shifted ev)
       (make-vector 1 shifted)
     (make-vector 1 unshifted))))

(defun artist-down-mouse-1 (ev)
  "Perform drawing action for event EV."
  (interactive "@e")
  (let* ((real (artist-go-get-symbol-shift
		artist-curr-go (artist-event-is-shifted ev)))
	 (draw-how (artist-go-get-draw-how-from-symbol real))
	 ;; Remember original values for draw-region-min-y and max-y
	 ;; in case we are interrupting a key-draw operation.
	 (orig-draw-region-min-y artist-draw-region-min-y)
	 (orig-draw-region-max-y artist-draw-region-max-y)
	 (orig-pointer-shape (if (eq window-system 'x) x-pointer-shape nil))
	 (echoq-keystrokes 10000)	; a lot of seconds
	 ;; Remember original binding for the button-up event to this
	 ;; button-down event.
	 (key (artist-compute-up-event-key ev))
	 (orig-button-up-binding (lookup-key (current-global-map) key)))

    (unwind-protect
	(progn
	  (if (eq window-system 'x)
	      (artist-set-pointer-shape artist-pointer-shape))

	  ;; Redefine the button-up binding temporarily (the original
	  ;; binding is restored in the unwind-forms below). This is to
	  ;; avoid the phenomenon outlined in this scenario:
	  ;;
	  ;; 1. A routine which reads something from the mini-buffer (such
	  ;;    as the text renderer) is called from below.
	  ;; 2. Meanwhile, the users releases the mouse button.
	  ;; 3. As a (funny :-) coincidence, the binding for the
	  ;;    button-up event is often mouse-set-point, so Emacs
	  ;;    sets the point to where the button was released, which is
	  ;;    in the buffer where the user wants to place the text.
	  ;; 4. The user types C-x o (or uses the mouse once again)
	  ;;    until he reaches the mini-buffer which is still prompting
	  ;;    for some text to render.
	  ;;
	  ;; To do this foolproof, all local and minor-mode maps should
	  ;; be searched and temporarily changed as well, since they
	  ;; too might have some binding for the button-up event,
	  ;; but I hope dealing with the global map will suffice.
	  (define-key (current-global-map) key 'artist-do-nothing)

	  (artist-draw-region-reset)

	  (artist-mode-line-show-curr-operation t)

	  (cond ((eq draw-how 'artist-do-continously)
		 (artist-mouse-draw-continously ev))
		((eq draw-how 'artist-do-poly)
		 (artist-mouse-draw-poly ev))
		((and (numberp draw-how) (= draw-how 1))
		 (artist-mouse-draw-1point ev))
		((and (numberp draw-how) (= draw-how 2))
		 (artist-mouse-draw-2points ev))
		(t (message "Drawing \"%s\"s is not yet implemented"
			    draw-how)))

	  (if artist-trim-line-endings
	      (artist-draw-region-trim-line-endings artist-draw-region-min-y
						    artist-draw-region-max-y))
	  (setq artist-draw-region-min-y orig-draw-region-min-y)
	  (setq artist-draw-region-max-y orig-draw-region-max-y))

      ; This is protected
      (if (eq window-system 'x)
	  (artist-set-pointer-shape orig-pointer-shape))

      (if orig-button-up-binding
	  (define-key (current-global-map) key orig-button-up-binding))

      (artist-mode-line-show-curr-operation artist-key-is-drawing))))


(defun artist-mouse-choose-operation (ev op)
  "Choose operation for event EV and operation OP."
  (interactive
   (progn
     (select-window (posn-window (event-start last-input-event)))
     (list last-input-event
	   (if (display-popup-menus-p)
	       (x-popup-menu last-nonmenu-event artist-popup-menu-table)
	     'no-popup-menus))))

  (if (eq op 'no-popup-menus)
      ;; No popup menus. Call `tmm-prompt' instead, but with the
      ;; up-mouse-button, if any, temporarily disabled, otherwise
      ;; it'll interfere.
      (let* ((key (artist-compute-up-event-key ev))
	     (orig-button-up-binding (lookup-key (current-global-map) key)))
	(unwind-protect
	    (define-key (current-global-map) key 'artist-do-nothing)
	    (setq op (tmm-prompt artist-popup-menu-table))
	  (if orig-button-up-binding
	      (define-key (current-global-map) key orig-button-up-binding)))))

  (let ((draw-fn (artist-go-get-draw-fn-from-symbol (car op)))
	(set-fn (artist-fc-get-fn-from-symbol (car op))))
    (cond

     ;; *** It was a draw-function
     ((not (listp draw-fn))
      (let* ((unshifted    (artist-go-get-symbol-shift (car op) nil))
	     (shifted      (artist-go-get-symbol-shift (car op) t))
	     (shift-state  (artist-event-is-shifted ev))
	     (selected-op  (if shift-state shifted unshifted))
	     (keyword      (artist-go-get-keyword-from-symbol selected-op)))
	(artist-select-operation keyword)))

     ;; *** It was a set/unset function
     ((not (listp set-fn))
      (call-interactively set-fn)))))


(defun artist-down-mouse-3 (ev)
  "Erase character or rectangle, depending on event EV."
  (interactive "@e")
  (let ((artist-curr-go 'erase-char))
    (artist-down-mouse-1 ev))
  ;; Restore mode-line
  (artist-mode-line-show-curr-operation artist-key-is-drawing))


;;
;; Mouse routines
;;

(defsubst artist-shift-has-changed (shift-state ev)
  "From the last SHIFT-STATE and EV, determine if the shift-state has changed."
  ;; This one simply doesn't work.
  ;;
  ;; There seems to be no way to tell whether the user has pressed shift
  ;; while dragging the cursor around when we are in a track-mouse
  ;; form. Calling (event-modifiers ev) yields nil :-( Neither is the
  ;; (event-basic-type ev) of any help (it is simply `mouse-movement').
  ;;
  ;; So this doesn't work:
  ;; (cond ((and shift-state (not (artist-event-is-shifted ev))) t)
  ;;       ((and (not shift-state) (artist-event-is-shifted ev)) t)
  ;;       (t nil))
  nil)

(defun artist-coord-win-to-buf (coord)
  "Convert a window-relative coordinate COORD to a buffer-relative coordinate."
  (let ((window-x       (car coord))
	(window-y       (cdr coord))
	(window-start-x (window-hscroll))
	(window-start-y (save-excursion (goto-char (window-start))
					(artist-current-line))))
      (cons (+ window-x window-start-x)
	    (+ window-y window-start-y))))


(defun artist-mouse-draw-continously (ev)
  "Generic function for shapes that require 1 point as input.
Operation is done continuously while the mouse button is hold down.
The event, EV, is the mouse event."
  (let* ((unshifted    (artist-go-get-symbol-shift artist-curr-go nil))
	 (shifted      (artist-go-get-symbol-shift artist-curr-go t))
	 (shift-state  (artist-event-is-shifted ev))
	 (op           (if shift-state shifted unshifted))
	 (draw-how     (artist-go-get-draw-how-from-symbol op))
	 (init-fn      (artist-go-get-init-fn-from-symbol op))
	 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
	 (exit-fn      (artist-go-get-exit-fn-from-symbol op))
	 (draw-fn      (artist-go-get-draw-fn-from-symbol op))
	 (interval-fn  (artist-go-get-interval-fn-from-symbol op))
	 (interval     (artist-funcall interval-fn))
	 (arrow-pred   (artist-go-get-arrow-pred-from-symbol op))
	 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
	 (ev-start     (event-start ev))
	 (initial-win  (posn-window ev-start))
	 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
	 (x1           (car ev-start-pos))
	 (y1           (cdr ev-start-pos))
	 (shape)
	 (timer))
    (select-window (posn-window ev-start))
    (artist-funcall init-fn x1 y1)
    (if (not artist-rubber-banding)
	(artist-no-rb-set-point1 x1 y1))
    (unwind-protect
        (track-mouse
          (while (or (mouse-movement-p ev)
                     (member 'down (event-modifiers ev)))
            (setq ev-start-pos (artist-coord-win-to-buf
                                (posn-col-row (event-start ev))))
            (setq x1 (car ev-start-pos))
            (setq y1 (cdr ev-start-pos))

            ;; Cancel previous timer
            (if timer
                (cancel-timer timer))

            (if (not (eq initial-win (posn-window (event-start ev))))
                ;; If we moved outside the window, do nothing
                nil

              ;; Still in same window:
              ;;
              ;; Check if user presses or releases shift key
              (if (artist-shift-has-changed shift-state ev)

                  ;; First check that the draw-how is the same as we
                  ;; already have. Otherwise, ignore the changed shift-state.
                  (if (not (eq draw-how
                               (artist-go-get-draw-how-from-symbol
                                (if (not shift-state) shifted unshifted))))
                      (message "Cannot switch to shifted operation")

                    ;; progn is "implicit" since this is the else-part
                    (setq shift-state (not shift-state))
                    (setq op          (if shift-state shifted unshifted))
                    (setq draw-how    (artist-go-get-draw-how-from-symbol op))
                    (setq draw-fn     (artist-go-get-draw-fn-from-symbol op))))

              ;; Draw the new shape
              (setq shape (artist-funcall draw-fn x1 y1))
              (artist-move-to-xy x1 y1)

              ;; Start the timer to call `draw-fn' repeatedly every
              ;; `interval' second
              (if (and interval draw-fn)
                  (setq timer (run-at-time interval interval draw-fn x1 y1))))

            ;; Read next event
            (setq ev (read-event))))
      ;; Cleanup: get rid of any active timer.
      (if timer
          (cancel-timer timer)))
    ;; Cancel any timers
    (if timer
	(cancel-timer timer))

    (artist-funcall prep-fill-fn x1 y1)

    (if (artist-funcall arrow-pred)
	(artist-funcall arrow-set-fn x1 y1)
      (artist-clear-arrow-points))

    (artist-funcall exit-fn x1 y1)
    (artist-move-to-xy x1 y1)))



(defun artist-mouse-draw-poly (ev)
  "Generic function for shapes requiring several points as input.
The event, EV, is the mouse event."
  (interactive "@e")
  (message "Mouse-1: set new point, mouse-2: set last point")
  (let* ((unshifted    (artist-go-get-symbol-shift artist-curr-go nil))
	 (shifted      (artist-go-get-symbol-shift artist-curr-go t))
	 (shift-state  (artist-event-is-shifted ev))
	 (op           (if shift-state shifted unshifted))
	 (draw-how     (artist-go-get-draw-how-from-symbol op))
	 (init-fn      (artist-go-get-init-fn-from-symbol op))
	 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
	 (exit-fn      (artist-go-get-exit-fn-from-symbol op))
	 (draw-fn      (artist-go-get-draw-fn-from-symbol op))
	 (undraw-fn    (artist-go-get-undraw-fn-from-symbol op))
	 (fill-pred    (artist-go-get-fill-pred-from-symbol op))
	 (fill-fn      (artist-go-get-fill-fn-from-symbol op))
	 (arrow-pred   (artist-go-get-arrow-pred-from-symbol op))
	 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
	 (ev-start     (event-start ev))
	 (initial-win  (posn-window ev-start))
	 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
	 (x1-last      (car ev-start-pos))
	 (y1-last      (cdr ev-start-pos))
	 (x2           x1-last)
	 (y2           y1-last)
	 (is-down      t)
	 (shape        nil)
	 (point-list   nil)
	 (done         nil))
    (select-window (posn-window ev-start))
    (artist-funcall init-fn x1-last y1-last)
    (if (not artist-rubber-banding)
	(artist-no-rb-set-point1 x1-last y1-last))
    (track-mouse
      (while (not done)
	;; decide what to do
	(cond

	 ;; *** Mouse button is released.
	 ((and is-down
	       (or (member 'click (event-modifiers ev))
		   (member 'drag (event-modifiers ev))))
	  ;; First, if not rubber-banding, draw the line.
	  ;;
	  (if (not artist-rubber-banding)
	      (progn
		(artist-no-rb-unset-points)
		(setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))))

	  ;; Set the second point to the shape's second point
	  ;; (which might be different from the mouse's second point,
	  ;; if, for example, we are drawing a straight line)
	  ;;
	  (if (not (null shape))
	      (let ((endpoint2 (artist-2point-get-endpoint2 shape)))
		(setq x1-last (artist-endpoint-get-x endpoint2))
		(setq y1-last (artist-endpoint-get-y endpoint2))))
	  (setq point-list (cons (artist-make-endpoint x1-last y1-last)
				 point-list))
	  (setq shape nil)
	  (setq is-down nil))

	 ;; *** Mouse button 2 or 3 down
	 ((and (member 'down (event-modifiers ev))
	       (or (equal (event-basic-type ev) 'mouse-2)
		   (equal (event-basic-type ev) 'mouse-3)))
	  ;; Ignore
	  nil)

	 ;; *** Mouse button 2 or 3 released
	 ((and (or (member 'click (event-modifiers ev))
		   (member 'drag (event-modifiers ev)))
	       (or (equal (event-basic-type ev) 'mouse-2)
		   (equal (event-basic-type ev) 'mouse-3)))

	  ;; This means the end of our poly-line drawing-session.
	  ;;
	  (setq done t))

	 ;; *** Mouse button 1 went down
	 ((and (not is-down)
	       (member 'down (event-modifiers ev))
	       (equal (event-basic-type ev) 'mouse-1))
	  ;; Check whether the (possibly new, that depends on if shift
	  ;; has been pressed or released) symbol has the same draw-how
	  ;; information as the previous had. If it hasn't, we can't
	  ;; proceed.
	  ;;
	  (if (not (eq draw-how
		       (artist-go-get-draw-how-from-symbol
			(if (not shift-state) shifted unshifted))))
	      (message "Cannot switch operation")
	    (progn
	      ;; Decide operation
	      ;;
	      (setq unshifted
		    (artist-go-get-symbol-shift artist-curr-go nil)
		    shifted
		    (artist-go-get-symbol-shift artist-curr-go t)
		    shift-state  (artist-event-is-shifted ev)
		    op           (if shift-state shifted unshifted)
		    draw-how     (artist-go-get-draw-how-from-symbol op)
		    draw-fn      (artist-go-get-draw-fn-from-symbol op)
		    undraw-fn    (artist-go-get-undraw-fn-from-symbol op)
		    fill-pred    (artist-go-get-fill-pred-from-symbol op)
		    fill-fn      (artist-go-get-fill-fn-from-symbol op))

	      ;; Draw shape from last place to this place

	      ;; set x2 and y2
	      ;;
	      (setq ev-start-pos (artist-coord-win-to-buf
				  (posn-col-row (event-start ev))))
	      (setq x2 (car ev-start-pos))
	      (setq y2 (cdr ev-start-pos))

	      ;; Draw the new shape (if not rubber-banding, place both marks)
	      ;;
	      (if artist-rubber-banding
		  (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))
		(progn
		  (artist-no-rb-set-point1 x1-last y1-last)
		  (artist-no-rb-set-point2 x2 y2)))

	      ;; Show new operation in mode-line
	      (let ((artist-curr-go op))
		(artist-mode-line-show-curr-operation t))))

	  (setq is-down t))


	 ;; *** Mouse moved, button is down and we are still in orig window
	 ((and (mouse-movement-p ev)
	       is-down
	       (eq initial-win (posn-window (event-start ev))))
	  ;; Draw shape from last place to this place
	  ;;
	  ;; set x2 and y2
	  (setq ev-start-pos (artist-coord-win-to-buf
			      (posn-col-row (event-start ev))))
	  (setq x2 (car ev-start-pos))
	  (setq y2 (cdr ev-start-pos))

	  ;; First undraw last shape
	  ;; (unset last point if not rubberbanding)
	  ;;
	  (artist-funcall undraw-fn shape)

	  ;; Draw the new shape (if not rubberbanding, set 2nd mark)
	  ;;
	  (if artist-rubber-banding
	      (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))
	    (progn
	      (artist-no-rb-unset-point2)
	      (artist-no-rb-set-point2 x2 y2)))
	  ;; Move cursor
	  (artist-move-to-xy x2 y2))

	 ;; *** Mouse moved, button is down but we are NOT in orig window
	 ((and (mouse-movement-p ev)
	       is-down
	       (not (eq initial-win (posn-window (event-start ev)))))
	  ;; Ignore
	  nil)


	 ;; *** Moving mouse while mouse button is not down
	 ((and (mouse-movement-p ev) (not is-down))
	  ;; don't do anything.
	  nil)


	 ;; *** Mouse button 1 went down, first time
	 ((and is-down
	       (member 'down (event-modifiers ev))
	       (equal (event-basic-type ev) 'mouse-1))
	  ;; don't do anything
	  nil)


	 ;; *** Another event
	 (t
	  ;; End drawing
	  ;;
	  (setq done t)))

	;; Read next event (only if we should not stop)
	(if (not done)
	    (setq ev (read-event)))))

    ;; Reverse point-list (last points are cond'ed first)
    (setq point-list (reverse point-list))

    (artist-funcall prep-fill-fn point-list)

    ;; Maybe fill
    (if (artist-funcall fill-pred)
	(artist-funcall fill-fn point-list))

    ;; Maybe set arrow points
    (if (and point-list (artist-funcall arrow-pred))
	(artist-funcall arrow-set-fn point-list)
      (artist-clear-arrow-points))

    (artist-funcall exit-fn point-list)
    (artist-move-to-xy x2 y2)))


(defun artist-mouse-draw-1point (ev)
  "Generic function for shapes requiring only 1 point as input.
Operation is done once.  The event, EV, is the mouse event."
  (interactive "@e")
  (let* ((unshifted    (artist-go-get-symbol-shift artist-curr-go nil))
	 (shifted      (artist-go-get-symbol-shift artist-curr-go t))
	 (shift-state  (artist-event-is-shifted ev))
	 (op           (if shift-state shifted unshifted))
	 (draw-how     (artist-go-get-draw-how-from-symbol op))
	 (init-fn      (artist-go-get-init-fn-from-symbol op))
	 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
	 (exit-fn      (artist-go-get-exit-fn-from-symbol op))
	 (draw-fn      (artist-go-get-draw-fn-from-symbol op))
	 (arrow-pred   (artist-go-get-arrow-pred-from-symbol op))
	 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
	 (ev-start     (event-start ev))
	 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
	 (x1           (car ev-start-pos))
	 (y1           (cdr  ev-start-pos)))
    (select-window (posn-window ev-start))
    (artist-funcall init-fn x1 y1)
    (artist-funcall draw-fn x1 y1)
    (artist-funcall prep-fill-fn x1 y1)
    (if (artist-funcall arrow-pred)
	(artist-funcall arrow-set-fn x1 y1)
      (artist-clear-arrow-points))
    (artist-funcall exit-fn x1 y1)
    (artist-move-to-xy x1 y1)))


(defun artist-mouse-draw-2points (ev)
  "Generic function for shapes requiring 2 points as input.
The event, EV, is the mouse event."
  (interactive "@e")
  (let* ((unshifted    (artist-go-get-symbol-shift artist-curr-go nil))
	 (shifted      (artist-go-get-symbol-shift artist-curr-go t))
	 (shift-state  (artist-event-is-shifted ev))
	 (op           (if shift-state shifted unshifted))
	 (draw-how     (artist-go-get-draw-how-from-symbol op))
	 (init-fn      (artist-go-get-init-fn-from-symbol op))
	 (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
	 (exit-fn      (artist-go-get-exit-fn-from-symbol op))
	 (draw-fn      (artist-go-get-draw-fn-from-symbol op))
	 (undraw-fn    (artist-go-get-undraw-fn-from-symbol op))
	 (fill-pred    (artist-go-get-fill-pred-from-symbol op))
	 (fill-fn      (artist-go-get-fill-fn-from-symbol op))
	 (arrow-pred   (artist-go-get-arrow-pred-from-symbol op))
	 (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
	 (ev-start     (event-start ev))
	 (initial-win  (posn-window ev-start))
	 (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
	 (x1           (car ev-start-pos))
	 (y1           (cdr ev-start-pos))
	 (x2)
	 (y2)
	 (shape))
    (select-window (posn-window ev-start))
    (artist-funcall init-fn x1 y1)
    (if (not artist-rubber-banding)
	(artist-no-rb-set-point1 x1 y1))
    (track-mouse
      (while (or (mouse-movement-p ev)
		 (member 'down (event-modifiers ev)))
	(setq ev-start-pos (artist-coord-win-to-buf
			    (posn-col-row (event-start ev))))
	(setq x2 (car ev-start-pos))
	(setq y2 (cdr ev-start-pos))

	(if (not (eq initial-win (posn-window (event-start ev))))
	    ;; If we moved outside the window, do nothing
	    nil

	  ;; Still in same window:
	  ;;
	  ;; First undraw last shape (unset last point if not rubberbanding)
	  (if artist-rubber-banding
	      (artist-funcall undraw-fn shape)
	    (artist-no-rb-unset-point2))

	  ;; Check if user presses or releases shift key
	  (if (artist-shift-has-changed shift-state ev)

	      ;; First check that the draw-how is the same as we
	      ;; already have. Otherwise, ignore the changed shift-state.
	      (if (not (eq draw-how
			   (artist-go-get-draw-how-from-symbol
			    (if (not shift-state) shifted unshifted))))
		  (message "Cannot switch to shifted operation")

		(message "Switching")
		;; progn is "implicit" since this is the else-part
		(setq shift-state (not shift-state))
		(setq op          (if shift-state shifted unshifted))
		(setq draw-how    (artist-go-get-draw-how-from-symbol op))
		(setq draw-fn     (artist-go-get-draw-fn-from-symbol op))
		(setq undraw-fn   (artist-go-get-undraw-fn-from-symbol op))
		(setq fill-pred   (artist-go-get-fill-pred-from-symbol op))
		(setq fill-fn     (artist-go-get-fill-fn-from-symbol op))))

	  ;; Draw the new shape
	  (if artist-rubber-banding
	      (setq shape (artist-funcall draw-fn x1 y1 x2 y2))
	    (artist-no-rb-set-point2 x2 y2))
	  ;; Move cursor
	  (artist-move-to-xy x2 y2))


	;; Read next event
	(setq ev (read-event))))

    ;; If we are not rubber-banding (that is, we were moving around the `2')
    ;; draw the shape
    (if (not artist-rubber-banding)
	(progn
	  (artist-no-rb-unset-points)
	  (setq shape (artist-funcall draw-fn x1 y1 x2 y2))))

    (artist-funcall prep-fill-fn shape x1 y1 x2 y2)

    ;; Maybe fill
    (if (artist-funcall fill-pred)
	(artist-funcall fill-fn shape x1 y1 x2 y2))

    ;; Maybe set arrow-points
    (if (artist-funcall arrow-pred)
	(artist-funcall arrow-set-fn shape x1 y1 x2 y2)
      (artist-clear-arrow-points))

    (artist-funcall exit-fn shape x1 y1 x2 y2)
    (artist-move-to-xy x2 y2)))


;;
;; Bug-report-submitting
;;
(defun artist-submit-bug-report ()
  "Submit via mail a bug report on Artist."
  (interactive)
  (require 'reporter)
  (if (y-or-n-p "Do you want to submit a bug report on Artist? ")
      (let ((to   artist-maintainer-address)
	    (vars '(window-system
		    window-system-version
		    ;;
		    artist-rubber-banding
		    artist-interface-with-rect
		    artist-aspect-ratio
		    ;; Now the internal ones
		    artist-curr-go
		    artist-key-poly-point-list
		    artist-key-shape
		    artist-key-draw-how
		    artist-arrow-point-1
		    artist-arrow-point-2)))
	;; Remove those variables from vars that are not bound
	(mapc
	 (function
	  (lambda (x)
	    (if (not (and (boundp x) (symbol-value x)))
		(setq vars (delq x vars))))) vars)
	(reporter-submit-bug-report
	 artist-maintainer-address
	 (concat "artist.el " artist-version)
	 vars
	 nil nil
	 (concat "Hello Tomas,\n\n"
		 "I have a nice bug report on Artist for you! Here it is:")))))


;;
;; Now provide this minor mode
;;

(provide 'artist)


;;; About adding drawing modes
;;; --------------------------

;; If you are going to add a new drawing mode, read the following
;; sketchy outlines to get started a bit easier.
;;
;; 1.   If your new drawing mode falls into one of the following
;;      categories, goto point 2, otherwise goto point 3.
;;
;;         - Modes where the shapes are drawn continuously, as long as
;;           the mouse button is held down (continuous modes).
;;           Example: the erase-char mode, the pen and pen-line modes.
;;
;;         - Modes where the shape is made up of from 2 points to an
;;           arbitrary number of points (poly-point modes).
;;           Example: the poly-line mode
;;
;;         - Modes where the shape is made up of 2 points (2-point
;;           modes).
;;           Example: lines, rectangles
;;
;;         - Modes where the shape is made up of 1 point (1-point
;;           modes). This mode differs from the continuous modes in
;;           that the shape is drawn only once when the mouse button
;;           is pressed.
;;           Examples: paste, a flood-fill, vaporize modes
;;
;;
;; 2. To make it easier and more flexible to program new drawing
;;    modes, you might choose to specify
;;      init-fn:      a function to be called at the very beginning
;;                    of the drawing phase,
;;      prep-fill-fn: a function to be called before filling,
;;      arrow-set-fn: a function for setting arrows, to be called
;;                    after filling, and
;;      exit-fn:      a function to be called at the very end of
;;                    the drawing phase.
;;    For each of the cases below, the arguments given to the init-fn,
;;    prep-fill-fn, arrow-set-fn and exit-fn are stated.
;;
;;    If your mode matches the continuous mode or the 1-point mode:
;;
;;      a. Create a draw-function that draws your shape. Your function
;;         must take x and y as arguments. The return value is not
;;         used.
;;
;;      b. Add your mode to the master table, `artist-mt'.
;;
;;	init-fn:      x y
;;	prep-fill-fn: x y
;;	arrow-set-fn: x y
;;	exit-fn:      x y
;;
;;    If your mode matches the 2-point mode:
;;
;;      a. Create one draw-function that draws your shape and one
;;         undraw-function that undraws it.
;;
;;         The draw-function must take x1, y1, x2 and y2 as
;;         arguments. It must return a list with three elements:
;;           Endpoint1: a vector [x1 y1]
;;           Endpoint2: a vector [x2 y2]
;;           Shapeinfo: all info necessary for your undraw-function to
;;                      be able to undraw the shape
;;         Use the artist-endpoint-* accessors to create and inspect
;;         the endpoints.
;;
;;         If applicable, you must be able to draw your shape without
;;         borders if the `artist-borderless-shapes' is non-nil.
;;         See `artist-draw-rect' for an example.
;;
;;         The undraw-function must take one argument: the list created
;;         by your draw-function. The return value is not used.
;;
;;      b. If you want to provide a fill-function, then create a
;;         function that takes 5 arguments: the list created by your
;;         draw-function, x1, y1, x2 and y2. The return value is not
;;         used.
;;
;;      c. Add your mode to the master table, `artist-mt'.
;;
;;	init-fn:      x1 y1
;;	prep-fill-fn: shape x1 y1 x2 y2
;;	arrow-set-fn: shape x1 y1 x2 y2
;;	exit-fn:      shape x1 y1 x2 y2
;;
;;    If your mode matches the poly-point mode:
;;
;;      a. Create one draw-function that draws your shape and one
;;         undraw-function that undraws it. The draw- and
;;         undraw-functions are used to draw/undraw a segment of
;;         your poly-point mode between 2 points. The draw- and
;;         undraw-functions are then really 2-point mode functions.
;;         They must take the same arguments and return the same
;;         values as those of the 2-point mode.
;;
;;         If applicable, you must be able to draw your shape without
;;         borders if the `artist-borderless-shapes' is non-nil.
;;         See `artist-draw-rect' for an example.
;;
;;      b. If you want to provide a fill-function, then create a
;;         function that takes 1 argument: a list of points where each
;;         point is a vector, [x, y].
;;
;;      c. Add your mode to the master table, `artist-mt'.
;;
;;	init-fn:      x1 y1
;;	prep-fill-fn: point-list
;;	arrow-set-fn: point-list
;;	exit-fn:      point-list
;;
;;    The arrow-set-fn must set the variables `artist-arrow-point-1'
;;    and `artist-arrow-point-2'. If your mode does not take arrows,
;;    you must set the variables to nil. Use the accessors
;;    artist-arrow-point-* to create and inspect arrow-points.
;;
;;
;; 3. If your mode doesn't match any of the categories, you are facing
;;    a bit more work, and I cannot be as detailed as above. Here is a
;;    brief outline of what you have to do:
;;
;;      a. Decide on a name for your type of mode. Let's assume that
;;         you decided on `xxx'. Then you should use the draw-how
;;         symbol artist-do-xxx.
;;
;;      b. Create a function artist-mouse-draw-xxx for drawing with
;;         mouse. It should be called from `artist-down-mouse-1'.
;;
;;         The all coordinates must be converted from window-relative
;;         to buffer relative before saved or handed over to
;;         any other function. Converting is done with
;;         the function `artist-coord-win-to-buf'.
;;
;;         It must take care to the `artist-rubber-banding' variable
;;         and perform rubber-banding accordingly. Use the
;;         artist-no-rb-* functions if not rubber-banding.
;;
;;         If applicable, you must be able to draw your shape without
;;         borders if the `artist-borderless-shapes' is non-nil.
;;         See `artist-draw-rect' for an example.
;;
;;         You must call the init-fn, the prep-fill-fn, arrow-set-fn
;;         and the exit-fn at the appropriate points.
;;
;;         When artist-mouse-draw-xxx ends, the shape for your mode
;;         must be completely drawn.
;;
;;      c. Create functions for drawing with keys:
;;
;;         - artist-key-set-point-xxx for setting a point in the
;;           mode, to be called from `artist-key-set-point-common'.
;;
;;         - artist-key-do-continuously-xxx to be called from
;;           `artist-key-do-continuously-common' whenever the user
;;           moves around.
;;
;;         As for the artist-mouse-draw-xxx, these two functions must
;;         take care to do rubber-banding, borderless shapes and to
;;         set arrows.
;;
;;         These functions should set the variable `artist-key-shape'
;;         to the shape drawn.
;;
;;      d. Create artist-key-draw-xxx and artist-key-undraw-xxx for
;;         drawing and undrawing. These are needed when the user
;;         switches operation to draw another shape of the same type
;;         of drawing mode.
;;
;;         You should provide these functions. You might think that
;;         only you is using your type of mode, so no one will be able
;;         to switch to another operation of the same type of mode,
;;         but someone else might base a new drawing mode upon your
;;         work.
;;
;;         You must call the init-fn, the prep-fill-fn, arrow-set-fn
;;         and the exit-fn at the appropriate points.
;;
;;      e. Add your new mode to the master table, `artist-mt'.
;;
;;
;; Happy hacking! Please let me hear if you add any drawing modes!
;; Don't hesitate to ask me any questions.


;;; artist.el ends here

debug log:

solving 930c39c ...
found 930c39c in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.