Menu

[26e832]: / joe / uedit.c  Maximize  Restore  History

Download this file

2610 lines (2376 with data), 55.6 kB

   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
/*
* Basic user edit functions
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
/***************/
/* Global options */
int pgamnt = -1; /* No. of PgUp/PgDn lines to keep */
/*
* Move cursor to beginning of line
*/
int u_goto_bol(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.hex) {
pbkwd(bw->cursor,bw->cursor->byte%16);
} else {
p_goto_bol(bw->cursor);
}
return 0;
}
/*
* Move cursor to first non-whitespace character, unless it is
* already there, in which case move it to beginning of line
*/
int uhome(W *w, int k)
{
BW *bw;
P *p;
WIND_BW(bw, w);
if (bw->o.hex) {
return u_goto_bol(w, 0);
}
p = pdup(bw->cursor, "uhome");
if (bw->o.indentfirst) {
if ((bw->o.smarthome) && (piscol(p) > pisindent(p))) {
p_goto_bol(p);
while (joe_isblank(p->b->o.charmap,brc(p)))
pgetc(p);
} else
p_goto_bol(p);
} else {
if (bw->o.smarthome && piscol(p)==0 && pisindent(p)) {
while (joe_isblank(p->b->o.charmap,brc(p)))
pgetc(p);
} else
p_goto_bol(p);
}
pset(bw->cursor, p);
prm(p);
return 0;
}
/*
* Move cursor to end of line
*/
int u_goto_eol(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.hex) {
if (bw->cursor->byte + 15 - bw->cursor->byte%16 > bw->b->eof->byte)
pset(bw->cursor,bw->b->eof);
else
pfwrd(bw->cursor, 15 - bw->cursor->byte%16);
} else
p_goto_eol(bw->cursor);
return 0;
}
/*
* Move cursor to beginning of file
*/
int u_goto_bof(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
p_goto_bof(bw->cursor);
return 0;
}
/*
* Move cursor to end of file
*/
int u_goto_eof(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->b->vt && bw->b->pid) {
pset(bw->cursor, bw->b->vt->vtcur);
} else {
p_goto_eof(bw->cursor);
}
return 0;
}
/*
* Move cursor left
*/
int u_goto_left(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.hex) {
if (prgetb(bw->cursor) != NO_MORE_DATA) {
return 0;
} else {
return -1;
}
}
if (bw->o.picture) {
if (bw->cursor->xcol) {
--bw->cursor->xcol;
pcol(bw->cursor,bw->cursor->xcol);
return 0;
} else
return -1;
} else {
/* Have to do ECHKXCOL here because of picture mode */
if (bw->cursor->xcol != piscol(bw->cursor)) {
bw->cursor->xcol = piscol(bw->cursor);
return 0;
} else if (prgetc(bw->cursor) != NO_MORE_DATA) {
bw->cursor->xcol = piscol(bw->cursor);
return 0;
} else {
return -1;
}
}
}
/*
* Move cursor right
*/
int u_goto_right(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.hex) {
if (pgetb(bw->cursor) != NO_MORE_DATA) {
return 0;
} else {
return -1;
}
}
if (bw->o.picture) {
++bw->cursor->xcol;
pcol(bw->cursor,bw->cursor->xcol);
return 0;
} else {
int rtn;
if (pgetc(bw->cursor) != NO_MORE_DATA) {
bw->cursor->xcol = piscol(bw->cursor);
rtn = 0;
} else {
rtn = -1;
}
/* Have to do EFIXXCOL here because of picture mode */
if (bw->cursor->xcol != piscol(bw->cursor))
bw->cursor->xcol = piscol(bw->cursor);
return rtn;
}
}
/*
* Move cursor to beginning of previous word or if there isn't
* previous word then go to beginning of the file
*
* WORD is a sequence non-white-space characters
*/
static int p_goto_prev(P *ptr)
{
P *p = pdup(ptr, "p_goto_prev");
struct charmap *map=ptr->b->o.charmap;
int c = prgetc(p);
if (joe_isalnum_(map,c)) {
while (joe_isalnum_(map,(c=prgetc(p))))
/* Do nothing */;
if (c != NO_MORE_DATA)
pgetc(p);
} else if (joe_isspace(map,c) || joe_ispunct(map,c)) {
while ((c=prgetc(p)), (joe_isspace(map,c) || joe_ispunct(map,c)))
/* Do nothing */;
while(joe_isalnum_(map,(c=prgetc(p))))
/* Do nothing */;
if (c != NO_MORE_DATA)
pgetc(p);
}
pset(ptr, p);
prm(p);
return 0;
}
int u_goto_prev(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
return p_goto_prev(bw->cursor);
}
/*
* Move cursor to end of next word or if there isn't
* next word then go to end of the file
*
* WORD is a sequence non-white-space characters
*/
static int p_goto_next(P *ptr)
{
P *p = pdup(ptr, "p_goto_next");
struct charmap *map=ptr->b->o.charmap;
int c = brch(p);
int rtn = -1;
if (joe_isalnum_(map,c)) {
rtn = 0;
while (joe_isalnum_(map,(c = brch(p))))
pgetc(p);
} else if (joe_isspace(map,c) || joe_ispunct(map,c)) {
while (joe_isspace(map, (c = brch(p))) || joe_ispunct(map,c))
pgetc(p);
while (joe_isalnum_(map,(c = brch(p)))) {
rtn = 0;
pgetc(p);
}
} else
pgetc(p);
pset(ptr, p);
prm(p);
return rtn;
}
int u_goto_next(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
return p_goto_next(bw->cursor);
}
static P *pboi(P *p)
{
p_goto_bol(p);
while (joe_isblank(p->b->o.charmap,brc(p)))
pgetc(p);
return p;
}
static int pisedge(P *p)
{
P *q;
int c;
if (pisbol(p))
return -1;
if (piseol(p))
return 1;
q = pdup(p, "pisedge");
pboi(q);
if (q->byte == p->byte)
goto left;
if (joe_isblank(p->b->o.charmap,(c = brc(p)))) {
pset(q, p);
if (joe_isblank(p->b->o.charmap,prgetc(q)))
goto no;
if (c == '\t')
goto right;
pset(q, p);
pgetc(q);
if (pgetc(q) == ' ')
goto right;
goto no;
} else {
pset(q, p);
c = prgetc(q);
if (c == '\t')
goto left;
if (c != ' ')
goto no;
if (prgetc(q) == ' ')
goto left;
goto no;
}
right:prm(q);
return 1;
left:prm(q);
return -1;
no:prm(q);
return 0;
}
int upedge(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (prgetc(bw->cursor) == NO_MORE_DATA)
return -1;
while (pisedge(bw->cursor) != -1)
prgetc(bw->cursor);
return 0;
}
int unedge(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (pgetc(bw->cursor) == NO_MORE_DATA)
return -1;
while (pisedge(bw->cursor) != 1)
pgetc(bw->cursor);
return 0;
}
/* Move cursor to matching delimiter */
/*
* begin end
*
* module endmodule
*
* function endfunction
*
* <word </word
*
* if elif else fi
*
* do done
*
* case esac, endcase
*
* #if #ifdef #ifndef #elseif #else #endif
*
* `ifdef `ifndef `else `endif
*
*/
/* A delimiter set list is a : separated list of delimiter sets.
A delimiter set is two or more groups of matching delimiters.
A group is a list of equivalent delimiters separated with |.
For example, here is a delimiter set list, with three sets:
"case|casex|casez=endcase:begin=end:if=elif=else=fi:
In the first delimiter set: "case," "casex" and "casez" all match with
"endcase." In the third set: "if" matches with "elif," which matches with
"else," which finally matches with "fi".
The search goes forward if the delimiter matches any words of any group
but the last of the set. If the delimiter matches a word in the last
group, the search goes backward to the first delimiter.
Delimiter sets lists are now UTF-8.
*/
/* Return pointer to first matching set in delimiter set list. Returns NULL
if no matches were found. */
static const char *next_set(const char *set)
{
while (*set && *set!=':')
++set;
if (*set==':')
++set;
return set;
}
static const char *next_group(const char *group)
{
while (*group && *group!='=' && *group!=':')
++group;
if (*group=='=')
++group;
return group;
}
static const char *next_word(const char *word)
{
while (*word && *word!='|' && *word!='=' && *word!=':')
++word;
if (*word=='|')
++word;
return word;
}
static int match_word(const char *word,const int *s)
{
while (*s && *word && utf8_decode_fwrd(&word, NULL) == *s++);
if (!*s && (!*word || *word=='|' || *word=='=' || *word==':'))
return 1;
else
return 0;
}
static int is_in_group(const char *group,const int *s)
{
while (*group && *group!='=' && *group!=':') {
if (match_word(group, s))
return 1;
else
group = next_word(group);
}
return 0;
}
static int is_in_any_group(const char *group,const int *s)
{
while (*group && *group!=':') {
if (match_word(group, s))
return 1;
else {
group = next_word(group);
if (*group == '=')
++group;
}
}
return 0;
}
static const char *find_last_group(const char *group)
{
const char *s;
for (s = group; *s && *s!=':'; s=next_group(s))
group = s;
return group;
}
#define MAX_WORD_SIZE 255
/* Search for matching delimiter: ignore things in comments or strings */
static int tomatch_char_or_word(BW *bw,int word_delimiter,int c,int f,const char *set,const char *group,int backward)
{
P *p = pdup(bw->cursor, "tomatch_char_or_word");
P *q = pdup(p, "tomatch_char_or_word");
const char *last_of_set = "";
int buf[MAX_WORD_SIZE+1];
int len;
int query_highlighter = bw->o.highlighter_context && bw->o.syntax && bw->db;
int initial_context = 0;
int col = 0;
int cnt = 0; /* No. levels of delimiters we're in */
int d;
off_t sod = 0; /* Start of delimiter */
if (word_delimiter) {
if (backward) {
last_of_set = find_last_group(set);
p_goto_next(p);
p_goto_prev(p);
} else {
last_of_set = find_last_group(group);
p_goto_next(p);
}
pset(q, p);
}
if (query_highlighter) {
col = -1;
do {
d = prgetc(q);
++col;
} while (d != NO_MORE_DATA && d != '\n');
if (d != NO_MORE_DATA)
pgetc(q);
parse(bw->o.syntax, q, lattr_get(bw->db, bw->o.syntax, q, q->line),bw->o.charmap);
initial_context = attr_buf[col] & CONTEXT_MASK;
}
if (backward) {
/* Backward search */
while ((d = prgetc(p)) != NO_MORE_DATA) {
int peek;
int peek1;
if (query_highlighter && d == '\n'){
pset(q, p);
col = -1;
do {
d = prgetc(q);
++col;
} while (d != NO_MORE_DATA && d != '\n');
if (d != NO_MORE_DATA)
pgetc(q);
parse(bw->o.syntax, q, lattr_get(bw->db, bw->o.syntax, q, q->line), bw->o.charmap);
continue;
}
peek = prgetc(p);
peek1 = 0;
if(peek != NO_MORE_DATA) {
peek1 = prgetc(p);
if (peek1 != NO_MORE_DATA)
pgetc(p);
pgetc(p);
}
--col;
if (query_highlighter
&& (attr_buf[col] & (CONTEXT_COMMENT | CONTEXT_STRING))
&& (attr_buf[col] & CONTEXT_MASK) != initial_context) {
/* Ignore */
} else if (!query_highlighter
&& (bw->o.cpp_comment || bw->o.pound_comment ||
bw->o.semi_comment || bw->o.tex_comment || bw->o.vhdl_comment) && d == '\n') {
int cc;
pset(q, p);
p_goto_bol(q);
while((cc = pgetc(q)) != '\n') {
if (cc == '\\') {
if (pgetc(q) == '\n')
break;
} else if (bw->o.pound_comment && cc == '$' && brch(q)=='#') {
pgetc(q);
} else if(!bw->o.no_double_quoted && cc=='"') {
while ((cc = pgetc(q)) != '\n')
if (cc == '"') break;
else if (cc == '\\') if ((cc = pgetc(q)) == '\n') break;
if (cc == '\n')
break;
} else if (bw->o.single_quoted && cc == '\'') {
while((cc = pgetc(q)) != '\n')
if (cc == '\'') break;
else if (cc == '\\') if ((cc = pgetc(q)) == '\n') break;
if (cc == '\n')
break;
} else if (bw->o.cpp_comment && cc == '/') {
if (brch(q)=='/') {
prgetc(q);
pset(p,q);
break;
}
} else if (bw->o.vhdl_comment && cc == '-') {
if (brch(q)=='-') {
prgetc(q);
pset(p,q);
break;
}
} else if (bw->o.pound_comment && cc == '#') {
pset(p,q);
break;
} else if (bw->o.semi_comment && cc == ';') {
pset(p,q);
break;
} else if (bw->o.tex_comment && cc == '%') {
pset(p,q);
break;
}
}
} else if (peek == '\\' && peek1!='\\') {
/* Ignore */
} else if (!query_highlighter && !bw->o.no_double_quoted && d == '"') {
while((d = prgetc(p)) != NO_MORE_DATA) {
if (d == '"') {
d = prgetc(p);
if (d != '\\') {
if (d != NO_MORE_DATA)
pgetc(p);
break;
}
}
}
} else if (!query_highlighter && bw->o.single_quoted && d == '\'' && c != '\'' && c != '`') {
while((d = prgetc(p)) != NO_MORE_DATA)
if (d == '\'') {
d = prgetc(p);
if (d != '\\') {
if (d != NO_MORE_DATA)
pgetc(p);
break;
}
}
} else if (!query_highlighter && bw->o.c_comment && d == '/') {
d = prgetc(p);
if (d == '*') {
d = prgetc(p);
do {
do {
if (d == '*') break;
} while ((d = prgetc(p)) != NO_MORE_DATA);
d = prgetc(p);
} while (d != NO_MORE_DATA && d != '/');
} else if (d != NO_MORE_DATA)
pgetc(p);
} else if (word_delimiter) {
if (joe_isalnum_(p->b->o.charmap, d)) {
int x;
int flg=0;
P *r;
len=0;
while (joe_isalnum_(p->b->o.charmap, d)) {
if(len!=MAX_WORD_SIZE)
buf[len++] = d;
d=prgetc(p);
--col;
}
/* ifdef hack */
r = pdup(p, "tomatch_char_or_word");
while (d ==' ' || d=='\t')
d=prgetc(r);
/* VHDL hack */
if ((d=='d' || d=='D') && bw->o.vhdl_comment) {
d=prgetc(r);
if(d=='n' || d=='N') {
d=prgetc(r);
if(d=='e' || d=='E') {
d=prgetc(r);
if(d==' ' || d=='\t' || d=='\n' || d==NO_MORE_DATA)
flg=1;
}
}
}
prm(r);
if (d == utf8_decode_string(set))
buf[len++] = d;
if(d!=NO_MORE_DATA)
pgetc(p);
++col;
buf[len]=0;
for(x=0;x!=len/2;++x) {
int e = buf[x];
buf[x] = buf[len-x-1];
buf[len-x-1] = e;
}
if (is_in_group(last_of_set,buf)) {
++cnt;
} else if(is_in_group(set,buf) && !flg && !cnt--) {
pset(bw->cursor,p);
prm(q);
prm(p);
return 0;
}
}
} else if (d == c) {
++cnt;
} else if (d == f && !cnt--) {
pset(bw->cursor, p);
prm(q);
prm(p);
return 0;
}
}
} else {
/* Forward search */
while ((sod = p->byte), ((d = pgetc(p)) != NO_MORE_DATA)) {
if (query_highlighter && d == '\n') {
parse(bw->o.syntax, q, lattr_get(bw->db, bw->o.syntax, q, q->line), bw->o.charmap);
col = 0;
continue;
}
if (query_highlighter
&& (attr_buf[col] & (CONTEXT_COMMENT | CONTEXT_STRING))
&& (attr_buf[col] & CONTEXT_MASK) != initial_context) {
/* Ignore */
} else if (d == '\\') {
if (!(query_highlighter && brch(p) == '\n')) {
pgetc(p);
++col;
}
} else if (!query_highlighter && !bw->o.no_double_quoted && d == '"') {
while ((d = pgetc(p)) != NO_MORE_DATA)
if (d == '"') break;
else if (d == '\\') pgetc(p);
} else if (!query_highlighter && bw->o.single_quoted && d == '\'' && c != '\'' && c != '`') {
while((d = pgetc(p)) != NO_MORE_DATA)
if (d == '\'') break;
else if (d == '\\') pgetc(p);
} else if (!query_highlighter && d == '$' && brch(p)=='#' && bw->o.pound_comment) {
pgetc(p);
} else if (!query_highlighter
&& ((bw->o.pound_comment && d == '#') ||
(bw->o.semi_comment && d == ';') ||
(bw->o.tex_comment && d == '%') ||
(bw->o.vhdl_comment && d == '-' && brch(p) == '-') ||
(bw->o.cpp_comment && d == '/' && brch(p) == '/'))) {
while ((d = pgetc(p)) != NO_MORE_DATA)
if (d == '\n')
break;
} else if (!query_highlighter && bw->o.c_comment && d == '/' && brch(p) == '*') {
pgetc(p);
d = pgetc(p);
do {
do {
if (d == '*') break;
} while ((d = pgetc(p)) != NO_MORE_DATA);
d = pgetc(p);
} while (d != NO_MORE_DATA && d != '/');
} else if (word_delimiter) {
int set0 = utf8_decode_string(set);
if (d == set0) {
/* ifdef hack */
len = 0;
if (!joe_isalnum_(p->b->o.charmap, d)) { /* If it's a # in #ifdef, allow spaces after it */
sod = p->byte;
while ((d = pgetc(p))!=NO_MORE_DATA) {
++col;
if (d!=' ' && d!='\t')
break;
sod = p->byte;
}
buf[0] = set0;
len=1;
}
if (joe_isalnum_(p->b->o.charmap, d))
goto doit;
if (d!=NO_MORE_DATA) {
prgetc(p);
--col;
}
} else if (joe_isalpha_(p->b->o.charmap, d)) {
len=0;
doit:
while (joe_isalnum_(p->b->o.charmap, d)) {
if(len!=MAX_WORD_SIZE)
buf[len++] = d;
d=pgetc(p);
++col;
}
if (d!=NO_MORE_DATA) {
prgetc(p);
--col;
}
buf[len]=0;
if (is_in_group(set,buf)) {
++cnt;
} else if (cnt==0) {
if (is_in_any_group(group,buf)) {
pgoto(p, sod);
pset(bw->cursor,p);
prm(q);
prm(p);
return 0;
}
} else if(is_in_group(last_of_set,buf)) {
/* VHDL hack */
if (bw->o.vhdl_comment && (match_word("end", buf) || !match_word("END", buf)))
while((d=pgetc(p))!=NO_MORE_DATA) {
++col;
if (d==';' || d=='\n') {
prgetc(p);
--col;
break;
}
}
--cnt;
}
}
} else if (d == c) {
++cnt;
} else if (d == f && !--cnt) {
prgetc(p);
pset(bw->cursor, p);
prm(q);
prm(p);
return 0;
}
++col;
}
}
prm(q);
prm(p);
return -1;
}
static int tomatch_char(BW *bw,int c,int f,int dir)
{
return tomatch_char_or_word(bw, 0, c, f, 0, 0, dir == -1);
}
static int tomatch_word(BW *bw,const char *set,const char *group)
{
return tomatch_char_or_word(bw, 1, 0, 0, set, group, !*group || *group==':');
}
/* Return true if <foo /> */
static int xml_startend(P *p)
{
int c, d=0;
p=pdup(p, "xml_startend");
while((c=pgetc(p)) != NO_MORE_DATA) {
if(d=='/' && c=='>') {
prm(p);
return 1;
} else if(c=='>')
break;
d=c;
}
prm(p);
return 0;
}
static int tomatch_xml(BW *bw,int *word,int dir)
{
if (dir== -1) {
/* Backward search */
P *p=pdup(bw->cursor, "tomatch_xml");
int c;
int buf[MAX_WORD_SIZE+1];
int len;
int cnt = 1;
p_goto_next(p);
p_goto_prev(p);
while ((c=prgetc(p)) != NO_MORE_DATA) {
if (joe_isalnum_(p->b->o.charmap, c) || c == '.' || c == ':' || c == '-') {
int x;
len=0;
while (joe_isalnum_(p->b->o.charmap, c) || c=='.' || c==':' || c == '-') {
if(len!=MAX_WORD_SIZE)
buf[len++] = c;
c=prgetc(p);
}
if(c!=NO_MORE_DATA)
c = pgetc(p);
buf[len]=0;
for(x=0;x!=len/2;++x) {
int d = buf[x];
buf[x] = buf[len-x-1];
buf[len-x-1] = d;
}
if (!Zcmp(word,buf) && !xml_startend(p)) {
if (c=='<') {
if (!--cnt) {
pset(bw->cursor,p);
prm(p);
return 0;
}
}
else if (c=='/') {
++cnt;
}
}
}
}
prm(p);
return -1;
} else {
/* Forward search */
P *p=pdup(bw->cursor, "tomatch_xml");
int c;
int buf[MAX_WORD_SIZE+1];
int len;
int cnt = 1;
off_t sod = 0;
while ((c=pgetc(p)) != NO_MORE_DATA) {
if (c == '<') {
int e = 1;
sod = p->byte;
c = pgetc(p);
if (c=='/') {
sod = p->byte;
e = 0;
c = pgetc(p);
}
if (joe_isalpha_(p->b->o.charmap, c) || c==':' || c=='-' || c=='.') {
len=0;
while (joe_isalnum_(p->b->o.charmap, c) || c==':' || c=='-' || c=='.') {
if(len!=MAX_WORD_SIZE)
buf[len++]=c;
c=pgetc(p);
}
if (c!=NO_MORE_DATA)
prgetc(p);
buf[len]=0;
if (!Zcmp(word,buf) && !xml_startend(p)) {
if (e) {
++cnt;
}
else if (!--cnt) {
pgoto(p, sod);
pset(bw->cursor,p);
prm(p);
return 0;
}
}
} else if (c!=NO_MORE_DATA) {
prgetc(p);
}
}
}
prm(p);
return -1;
}
}
static void get_xml_name(P *p,int *buf)
{
int c;
int len=0;
p=pdup(p, "get_xml_name");
c=pgetc(p);
while (joe_isalnum_(p->b->o.charmap, c) || c==':' || c=='-' || c=='.') {
if(len!=MAX_WORD_SIZE)
buf[len++]=c;
c=pgetc(p);
}
buf[len]=0;
prm(p);
}
static void get_delim_name(P *q,int *buf)
{
int c;
int len=0;
P *p=pdup(q, "get_delim_name");
while ((c=prgetc(p))!=NO_MORE_DATA)
if (c!=' ' && c!='\t')
break;
prm(p);
/* preprocessor directive hack */
if (c=='#' || c=='`')
buf[len++]=c;
p=pdup(q, "get_delim_name");
c=pgetc(p);
while (joe_isalnum_(p->b->o.charmap, c)) {
if(len!=MAX_WORD_SIZE)
buf[len++]=c;
c=pgetc(p);
}
buf[len]=0;
prm(p);
}
int utomatch(W *w, int k)
{
int d;
int c, /* Character under cursor */
f, /* Character to find */
dir; /* 1 to search forward, -1 to search backward */
BW *bw;
WIND_BW(bw, w);
c = brch(bw->cursor);
/* Check for word delimiters */
if (joe_isalnum_(bw->cursor->b->o.charmap, c)) {
P *p;
int buf[MAX_WORD_SIZE+1];
char utf8_buf[MAX_WORD_SIZE * 6 + 1]; /* Possibly UTF-8 version of buf */
int buf1[MAX_WORD_SIZE+1];
const char *list = bw->b->o.text_delimiters;
const char *set;
const char *group;
const char *word;
int flg=0;
p=pdup(bw->cursor, "utomatch");
p_goto_next(p);
p_goto_prev(p);
get_delim_name(p,buf);
get_xml_name(p,buf1);
c=prgetc(p);
if (c=='<')
flg = 1;
else if (c=='/') {
c=prgetc(p);
if (c=='<')
flg = -1;
}
prm(p);
if (flg) {
return tomatch_xml(bw, buf1, flg);
}
for (set = list; set && *set; set = next_set(set)) {
for (group = set; *group && *group!='=' && *group!=':'; group=next_group(group)) {
for (word = group; *word && *word!='|' && *word!='=' && *word!=':'; word=next_word(word)) {
if (match_word(word, buf)) {
return tomatch_word(bw, set, next_group(word));
}
}
}
}
/* We don't know the word, so start a search */
if (bw->b->o.charmap->type) {
Ztoutf8(utf8_buf, SIZEOF(utf8_buf), buf);
} else {
Ztoz(utf8_buf, SIZEOF(utf8_buf), buf);
}
return dofirst(bw, 0, 0, utf8_buf);
}
switch (c) {
case '/':
dir = 1;
pgetc(bw->cursor);
f = brch(bw->cursor);
prgetc(bw->cursor);
if(f=='*') f = '/';
else {
dir = -1;
f = prgetc(bw->cursor);
if (f!=NO_MORE_DATA)
pgetc(bw->cursor);
if(f=='*') f = '/';
else
return -1;
}
break;
case '*':
dir = -1;
pgetc(bw->cursor);
f = brch(bw->cursor);
prgetc(bw->cursor);
if(f=='/') f = '*';
else {
dir = 1;
f = prgetc(bw->cursor);
if (f!=NO_MORE_DATA)
pgetc(bw->cursor);
if(f=='/') f = '*';
else
return -1;
}
break;
case '(':
f = ')';
dir = 1;
break;
case '[':
f = ']';
dir = 1;
break;
case '{':
f = '}';
dir = 1;
break;
case '`':
f = '\'';
dir = 1;
break;
case '<':
f = '>';
dir = 1;
break;
case ')':
f = '(';
dir = -1;
break;
case ']':
f = '[';
dir = -1;
break;
case '}':
f = '{';
dir = -1;
break;
case '\'':
f = '`';
dir = -1;
break;
case '>':
f = '<';
dir = -1;
break;
default:
return -1;
}
/* Search for matching C comment */
if (f == '/' || f == '*') {
P *p = pdup(bw->cursor, "utomatch");
if (dir == 1) {
d = pgetc(p);
do {
do {
if (d == '*') break;
} while ((d = pgetc(p)) != NO_MORE_DATA);
d = pgetc(p);
} while (d != NO_MORE_DATA && d != '/');
if (d == '/') {
if (f == '*') {
prgetc(p);
}
pset(bw->cursor,p);
prgetc(bw->cursor);
}
} else {
d = prgetc(p);
do {
do {
if (d == '*') break;
} while ((d = prgetc(p)) != NO_MORE_DATA);
d = prgetc(p);
} while (d != NO_MORE_DATA && d != '/');
if (d == '/') {
if (f == '*') {
pgetc(p);
}
pset(bw->cursor,p);
}
}
prm(p);
if (d == NO_MORE_DATA)
return -1;
else
return 0;
}
return tomatch_char(bw, c, f, dir);
}
/* Move cursor up */
int uuparw(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.hex) {
if (bw->cursor->byte<16)
return -1;
else {
pbkwd(bw->cursor, 16);
return 0;
}
}
if (bw->cursor->line) {
pprevl(bw->cursor);
pcol(bw->cursor, bw->cursor->xcol);
return 0;
} else
return -1;
}
/* Move cursor down */
int udnarw(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.hex) {
if (bw->cursor->byte+16 <= bw->b->eof->byte) {
pfwrd(bw->cursor, 16);
return 0;
} else if (bw->cursor->byte != bw->b->eof->byte) {
pset(bw->cursor, bw->b->eof);
return 0;
} else {
return -1;
}
}
if (bw->cursor->line != bw->b->eof->line) {
pnextl(bw->cursor);
pcol(bw->cursor, bw->cursor->xcol);
return 0;
} else if(bw->o.picture) {
p_goto_eol(bw->cursor);
binsc(bw->cursor,'\n');
pgetc(bw->cursor);
pcol(bw->cursor, bw->cursor->xcol);
return 0;
} else
return -1;
}
/* Move cursor to top of window */
int utos(W *w, int k)
{
off_t col;
BW *bw;
WIND_BW(bw, w);
col = bw->cursor->xcol;
pset(bw->cursor, bw->top);
pcol(bw->cursor, col);
bw->cursor->xcol = col;
return 0;
}
/* Move cursor to bottom of window */
int ubos(W *w, int k)
{
BW *bw;
off_t col;
WIND_BW(bw, w);
col = bw->cursor->xcol;
pline(bw->cursor, bw->top->line + bw->h - 1);
pcol(bw->cursor, col);
bw->cursor->xcol = col;
return 0;
}
/* Scroll buffer window up n lines
* If beginning of file is close, scrolls as much as it can
* If beginning of file is on-screen, cursor jumps to beginning of file
*
* If flg is set: cursor stays fixed relative to screen edge
* If flg is clr: cursor stays fixed on the buffer line
*/
void scrup(BW *bw, ptrdiff_t n, int flg)
{
ptrdiff_t scrollamnt = 0;
ptrdiff_t cursoramnt = 0;
ptrdiff_t x;
/* Decide number of lines we're really going to scroll */
if (bw->o.hex) {
if (bw->top->byte/16 >= n)
scrollamnt = cursoramnt = n;
else if (bw->top->byte/16)
scrollamnt = cursoramnt = (ptrdiff_t)(bw->top->byte/16);
else if (flg)
cursoramnt = (ptrdiff_t)(bw->cursor->byte/16);
else if (bw->cursor->byte/16 >= n)
cursoramnt = n;
} else {
if (bw->top->line >= n)
scrollamnt = cursoramnt = n;
else if (bw->top->line)
scrollamnt = cursoramnt = (ptrdiff_t)bw->top->line;
else if (flg)
cursoramnt = (ptrdiff_t)bw->cursor->line;
else if (bw->cursor->line >= n)
cursoramnt = n;
}
if (bw->o.hex) {
/* Move top-of-window pointer */
pbkwd(bw->top,scrollamnt*16);
/* Move cursor */
pbkwd(bw->cursor,cursoramnt*16);
/* If window is on the screen, give (buffered) scrolling command */
if (bw->parent->y != -1)
nscrldn(bw->parent->t->t, bw->y, bw->y + bw->h, scrollamnt);
} else {
/* Move top-of-window pointer */
for (x = 0; x != scrollamnt; ++x)
pprevl(bw->top);
p_goto_bol(bw->top);
/* Move cursor */
for (x = 0; x != cursoramnt; ++x)
pprevl(bw->cursor);
p_goto_bol(bw->cursor);
pcol(bw->cursor, bw->cursor->xcol);
/* If window is on the screen, give (buffered) scrolling command */
if (bw->parent->y != -1)
nscrldn(bw->parent->t->t, bw->y, bw->y + bw->h, scrollamnt);
}
}
/* Scroll buffer window down n lines
* If end of file is close, scrolls as much as possible
* If end of file is on-screen, cursor jumps to end of file
*
* If flg is set: cursor stays fixed relative to screen edge
* If flg is clr: cursor stays fixed on the buffer line
*/
void scrdn(BW *bw, ptrdiff_t n, int flg)
{
ptrdiff_t scrollamnt = 0;
ptrdiff_t cursoramnt = 0;
ptrdiff_t x;
/* How much we're really going to scroll... */
if (bw->o.hex) {
if (bw->top->b->eof->byte/16 < bw->top->byte/16 + bw->h) {
cursoramnt = (ptrdiff_t)(bw->top->b->eof->byte/16 - bw->cursor->byte/16);
if (!flg && cursoramnt > n)
cursoramnt = n;
} else if (bw->top->b->eof->byte/16 - (bw->top->byte/16 + bw->h) >= n)
cursoramnt = scrollamnt = n;
else
cursoramnt = scrollamnt = (ptrdiff_t)(bw->top->b->eof->byte/16 - (bw->top->byte/16 + bw->h) + 1);
} else {
if (bw->top->b->eof->line < bw->top->line + bw->h) {
cursoramnt = (ptrdiff_t)(bw->top->b->eof->line - bw->cursor->line);
if (!flg && cursoramnt > n)
cursoramnt = n;
} else if (bw->top->b->eof->line - (bw->top->line + bw->h) >= n)
cursoramnt = scrollamnt = n;
else
cursoramnt = scrollamnt = (ptrdiff_t)(bw->top->b->eof->line - (bw->top->line + bw->h) + 1);
}
if (bw->o.hex) {
/* Move top-of-window pointer */
pfwrd(bw->top,16*scrollamnt);
/* Move cursor */
pfwrd(bw->cursor,16*cursoramnt);
/* If window is on screen, give (buffered) scrolling command to terminal */
if (bw->parent->y != -1)
nscrlup(bw->parent->t->t, bw->y, bw->y + bw->h, scrollamnt);
} else {
/* Move top-of-window pointer */
for (x = 0; x != scrollamnt; ++x)
pnextl(bw->top);
/* Move cursor */
for (x = 0; x != cursoramnt; ++x)
pnextl(bw->cursor);
pcol(bw->cursor, bw->cursor->xcol);
/* If window is on screen, give (buffered) scrolling command to terminal */
if (bw->parent->y != -1)
nscrlup(bw->parent->t->t, bw->y, bw->y + bw->h, scrollamnt);
}
}
/* Page up */
int upgup(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (menu_above) {
if (w->link.prev->watom == &watommenu && w->link.prev->win == w) {
return umpgup(w->link.prev, 0);
}
} else {
if (w->link.next->watom == &watommenu && w->link.next->win == w) {
return umpgup(w->link.next, 0);
}
}
bw = (BW *) bw->parent->main->object;
if (bw->o.hex ? bw->cursor->byte < 16 : !bw->cursor->line)
return -1;
if (pgamnt < 0)
scrup(bw, bw->h / 2 + bw->h % 2, 1);
else if (pgamnt < bw->h)
scrup(bw, bw->h - pgamnt, 1);
else
scrup(bw, 1, 1);
return 0;
}
/* Page down */
int upgdn(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (menu_above) {
if (w->link.prev->watom == &watommenu && w->link.prev->win == w) {
return umpgdn(w->link.prev, 0);
}
} else {
if (w->link.next->watom == &watommenu && w->link.next->win == w) {
return umpgdn(w->link.next, 0);
}
}
bw = (BW *)bw->parent->main->object;
if (bw->o.hex ? bw->cursor->byte/16 == bw->b->eof->byte/16 : bw->cursor->line == bw->b->eof->line)
return -1;
if (pgamnt < 0)
scrdn(bw, bw->h / 2 + bw->h % 2, 1);
else if (pgamnt < bw->h)
scrdn(bw, bw->h - pgamnt, 1);
else
scrdn(bw, 1, 1);
return 0;
}
/* Scroll by a single line. The cursor moves with the scroll */
int uupslide(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.hex ? bw->top->byte/16 : bw->top->line) {
if (bw->o.hex ? bw->top->byte/16 + bw->h -1 != bw->cursor->byte/16 : bw->top->line + bw->h - 1 != bw->cursor->line)
udnarw(w, 0);
scrup(bw, 1, 0);
return 0;
} else
/* was return -1; */
return uuparw(w, 0);
}
int udnslide(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.hex ? bw->top->line/16 + bw->h <= bw->top->b->eof->byte/16 : bw->top->line + bw->h <= bw->top->b->eof->line) {
if (bw->o.hex ? bw->top->byte/16 != bw->cursor->byte/16 : bw->top->line != bw->cursor->line)
uuparw(w, 0);
scrdn(bw, 1, 0);
return 0;
} else
/* was return -1; */
return udnarw(w, 0);
}
/* Move cursor to specified line number */
static B *linehist = NULL; /* History of previously entered line numbers */
static int doline(W *w, char *s, void *object, int *notify)
{
BW *bw;
off_t num;
WIND_BW(bw, w);
num = (off_t)calc(bw, s, 1);
if (notify)
*notify = 1;
vsrm(s);
if (num >= 1 && !merr) {
int tmp = opt_mid;
if (num > bw->b->eof->line)
num = bw->b->eof->line + 1;
pline(bw->cursor, num - 1), bw->cursor->xcol = piscol(bw->cursor);
opt_mid = 1;
dofollows();
opt_mid = tmp;
return 0;
} else {
if (merr)
msgnw(bw->parent, merr);
else
msgnw(bw->parent, joe_gettext(_("Invalid line number")));
return -1;
}
}
int uline(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (wmkpw(bw->parent, joe_gettext(_("Go to line (%{abort} to abort): ")), &linehist, doline, NULL, NULL, math_cmplt, NULL, NULL, utf8_map, 0))
return 0;
else
return -1;
}
/* Move cursor to specified column number */
static B *colhist = NULL; /* History of previously entered column numbers */
static int docol(W *w, char *s, void *object, int *notify)
{
BW *bw;
off_t num;
WIND_BW(bw, w);
num = (off_t)calc(bw, s, 1);
if (notify)
*notify = 1;
vsrm(s);
if (num >= 1 && !merr) {
int tmp = opt_mid;
pcol(bw->cursor, num - 1), bw->cursor->xcol = piscol(bw->cursor);
opt_mid = 1;
dofollows();
opt_mid = tmp;
return 0;
} else {
if (merr)
msgnw(bw->parent, merr);
else
msgnw(bw->parent, joe_gettext(_("Invalid column number")));
return -1;
}
}
int ucol(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (wmkpw(bw->parent, joe_gettext(_("Go to column (%{abort} to abort): ")), &colhist, docol, NULL, NULL, math_cmplt, NULL, NULL, utf8_map, 0))
return 0;
else
return -1;
}
/* Move cursor to specified byte number */
static B *bytehist = NULL; /* History of previously entered byte numbers */
static int dobyte(W *w, char *s, void *object, int *notify)
{
BW *bw;
off_t num;
WIND_BW(bw, w);
num = (off_t)calc(bw, s, 1);
if (notify)
*notify = 1;
vsrm(s);
if (num >= 0 && !merr) {
int tmp = opt_mid;
pgoto(bw->cursor, num), bw->cursor->xcol = piscol(bw->cursor);
opt_mid = 1;
dofollows();
opt_mid = tmp;
return 0;
} else {
if (merr)
msgnw(bw->parent, merr);
else
msgnw(bw->parent, joe_gettext(_("Invalid byte number")));
return -1;
}
}
int ubyte(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (wmkpw(bw->parent, joe_gettext(_("Go to byte (%{abort} to abort): ")), &bytehist, dobyte, NULL, NULL, math_cmplt, NULL, NULL, utf8_map, 0))
return 0;
else
return -1;
}
/* Delete character under cursor
* or write ^D to process if we're at end of file in a shell window
*/
int udelch(W *w, int k)
{
BW *bw;
P *p;
WIND_BW(bw, w);
if (piseof(bw->cursor))
return -1;
pgetc(p = pdup(bw->cursor, "udelch"));
bdel(bw->cursor, p);
prm(p);
return 0;
}
/* Backspace */
int ubacks(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
/* Don't backspace when at beginning of line in prompt windows */
if (bw->parent->watom->what == TYPETW || !pisbol(bw->cursor)) {
int c;
off_t indent;
off_t col;
off_t indwid;
off_t wid;
/* Degenerate into ltarw for overtype mode */
if (bw->o.overtype) {
return u_goto_left(bw->parent, 0);
}
if (pisbof(bw->cursor))
return -1;
/* Indentation point of this line */
indent = pisindent(bw->cursor);
/* Column position of cursor */
col = piscol(bw->cursor);
/* Indentation step in columns */
if (bw->o.indentc=='\t')
wid = bw->o.tab;
else
wid = 1;
indwid = (bw->o.istep*wid);
/* Smart backspace when: cursor is at indentation point, indentation point
is a multiple of indentation width, we're not at beginning of line,
'smarthome' option is enabled, and indentation is purely made out of
indent characters (or purify indents is enabled). */
/* Ignore purify for backspace */
if (col == indent && (col%indwid)==0 && col!=0 && bw->o.smartbacks && bw->o.autoindent) {
P *p;
/* Delete all indentation */
p = pdup(bw->cursor, "ubacks");
p_goto_bol(p);
bdel(p,bw->cursor);
prm(p);
/* Indent to new position */
pfill(bw->cursor,col-indwid,bw->o.indentc);
} else if (col<indent && bw->o.smartbacks && !pisbol(bw->cursor)) {
/* We're before indent point: delete indwid worth of space but do not
cross line boundary. We could probably replace the above with this. */
off_t cw=0;
P *p = pdup(bw->cursor, "ubacks");
do {
c = prgetc(bw->cursor);
if(c=='\t') cw += bw->o.tab;
else cw += 1;
bdel(bw->cursor, p);
} while(!pisbol(bw->cursor) && cw<indwid);
prm(p);
} else {
/* Regular backspace */
P *p = pdup(bw->cursor, "ubacks");
if ((c = prgetc(bw->cursor)) != NO_MORE_DATA)
if (!bw->o.overtype || c == '\t' || pisbol(p) || piseol(p))
bdel(bw->cursor, p);
prm(p);
}
return 0;
} else
return -1;
}
/*
* Delete sequence of characters (alphabetic, numeric) or (white-space)
* if cursor is on the white-space it will delete all white-spaces
* until alphanumeric character
* if cursor is on the alphanumeric it will delete all alphanumeric
* characters until character that is not alphanumeric
*/
int u_word_delete(W *w, int k)
{
BW *bw;
P *p;
struct charmap *map;
int c;
WIND_BW(bw, w);
p = pdup(bw->cursor, "u_word_delete");
map=bw->b->o.charmap;
c = brch(p);
if (joe_isalnum_(map,c))
while (joe_isalnum_(map,(c = brch(p))))
pgetc(p);
else if (joe_isspace(map,c))
while (joe_isspace(map,(c = brch(p))))
pgetc(p);
else
pgetc(p);
if (p->byte == bw->cursor->byte) {
prm(p);
return -1;
}
bdel(bw->cursor, p);
prm(p);
return 0;
}
/* Delete from cursor to beginning of word it's in or immediately after,
* to start of whitespace, or a single character
*/
int ubackw(W *w, int k)
{
BW *bw;
P *p;
int c;
struct charmap *map;
WIND_BW(bw, w);
p = pdup(bw->cursor, "ubackw");
c = prgetc(bw->cursor);
map=bw->b->o.charmap;
if (joe_isalnum_(map,c)) {
while (joe_isalnum_(map,(c = prgetc(bw->cursor))))
/* do nothing */;
if (c != NO_MORE_DATA)
pgetc(bw->cursor);
} else if (joe_isspace(map,c)) {
while (joe_isspace(map,(c = prgetc(bw->cursor))))
/* do nothing */;
if (c != NO_MORE_DATA)
pgetc(bw->cursor);
}
if (bw->cursor->byte == p->byte) {
prm(p);
return -1;
}
bdel(bw->cursor, p);
prm(p);
return 0;
}
/* Delete from cursor to end of line, or if there's nothing to delete,
* delete the line-break
*/
int udelel(W *w,int k)
{
BW *bw;
P *p;
WIND_BW(bw, w);
p = p_goto_eol(pdup(bw->cursor, "udelel"));
if (bw->cursor->byte == p->byte) {
prm(p);
return udelch(w, 0);
} else
bdel(bw->cursor, p);
prm(p);
return 0;
}
/* Delete to beginning of line, or if there's nothing to delete,
* delete the line-break
*/
int udelbl(W *w, int k)
{
BW *bw;
P *p;
WIND_BW(bw, w);
p = p_goto_bol(pdup(bw->cursor, "udelbl"));
if (p->byte == bw->cursor->byte) {
prm(p);
return ubacks(w, 8); /* The 8 goes to the process if we're at EOF of shell window */
} else
bdel(p, bw->cursor);
prm(p);
return 0;
}
/* Delete entire line */
int udelln(W *w, int k)
{
BW *bw;
P *p;
WIND_BW(bw, w);
p = pdup(bw->cursor, "udelln");
p_goto_bol(bw->cursor);
pnextl(p);
if (bw->cursor->byte == p->byte) {
prm(p);
return -1;
}
bdel(bw->cursor, p);
prm(p);
return 0;
}
/* Insert a space */
int uinsc(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
binsc(bw->cursor, ' ');
return 0;
}
/* Move p backwards to first non-blank line and return its indentation */
static off_t find_indent(P *p)
{
int x;
for (x=0; x != 10; ++x) {
if (!pprevl(p)) return -1;
p_goto_bol(p);
if (!pisblank(p)) break;
}
if (x==10)
return -1;
else
return pisindent(p);
}
/* Type a character into the buffer (deal with left margin, overtype mode and
* word-wrap), if cursor is at end of shell window buffer, just send character
* to process.
*/
static int utypebw_raw(BW *bw, int k, int no_decode)
{
/* Character map of buffer */
struct charmap *map = bw->b->o.charmap;
/* Send data to shell window */
if ((bw->b->pid && !bw->b->vt && piseof(bw->cursor)) ||
( bw->b->pid && bw->b->vt && bw->cursor->byte == bw->b->vt->vtcur->byte)) {
if (locale_map->type) {
char buf[8];
ptrdiff_t len = utf8_encode(buf, k);
joe_write(bw->b->out, buf, len);
} else {
if (!no_decode) {
k = from_uni(locale_map, k);
}
if (k != -1) {
char c = TO_CHAR_OK(k);
joe_write(bw->b->out, &c, 1);
}
}
return 0;
}
/* Hex mode overtype needs to preserve file size */
if (bw->o.hex && bw->o.overtype) {
char buf[8];
ptrdiff_t x;
ptrdiff_t len;
if (map->type) {
len = utf8_encode(buf, k);
} else {
if (!no_decode)
k = from_uni(map, k);
if (k == -1)
return 1;
buf[0] = TO_CHAR_OK(k);
len = 1;
}
binsm(bw->cursor, buf, len);
for (x = 0; x != len; ++x)
pgetb(bw->cursor);
while (len--) {
P *p;
if (piseof(bw->cursor))
return 0;
p = pdup(bw->cursor, "utypebw_raw");
pgetb(p);
bdel(bw->cursor, p);
prm(p);
}
return 0;
}
if (k == '\t' && bw->o.overtype && !piseol(bw->cursor) && !no_decode) { /* TAB in overtype mode is supposed to be just cursor motion */
off_t col = bw->cursor->xcol; /* Current cursor column */
col = col + bw->o.tab - (col%bw->o.tab);/* Move to next tab stop */
pcol(bw->cursor,col); /* Try to position cursor there */
if (!bw->o.picture && piseol(bw->cursor) && piscol(bw->cursor)<col) { /* We moved past end of line, insert a tab (unless in picture mode) */
if (bw->o.spaces)
pfill(bw->cursor,col,' ');
else
pfill(bw->cursor,col,'\t');
}
bw->cursor->xcol = col; /* Put cursor there even if we can't really go there */
} else if (k == '\t' && bw->o.smartbacks && bw->o.autoindent && pisindent(bw->cursor)>=piscol(bw->cursor) && !no_decode) {
P *p = pdup(bw->cursor, "utypebw_raw");
off_t n = find_indent(p);
if (n != -1 && pisindent(bw->cursor)==piscol(bw->cursor) && n > pisindent(bw->cursor)) {
if (!pisbol(bw->cursor))
udelbl(bw->parent, 0);
while (joe_isspace(map,(k = pgetc(p))) && k != '\n') {
binsc(bw->cursor, k);
pgetc(bw->cursor);
}
} else {
int x;
for (x=0;x<bw->o.istep;++x) {
binsc(bw->cursor,bw->o.indentc);
pgetc(bw->cursor);
}
}
bw->cursor->xcol = piscol(bw->cursor);
prm (p);
} else if (k == '\t' && bw->o.spaces && !no_decode) {
off_t n;
if (bw->o.picture)
n = bw->cursor->xcol;
else
n = piscol(bw->cursor);
n = bw->o.tab - n % bw->o.tab;
while (n--)
utypebw(bw, ' ');
} else {
int upd;
int simple;
ptrdiff_t x;
/* Picture mode */
if (bw->o.picture && bw->cursor->xcol!=piscol(bw->cursor))
pfill(bw->cursor,bw->cursor->xcol,' '); /* Why no tabs? */
upd = bw->parent->t->t->updtab[bw->y + bw->cursor->line - bw->top->line];
simple = 1;
if (pisblank(bw->cursor))
while (piscol(bw->cursor) < bw->o.lmargin) {
binsc(bw->cursor, ' ');
pgetc(bw->cursor);
}
if (!no_decode) {
if(!map->type) {
/* Convert to byte code */
k = from_uni(map, k);
}
}
binsc(bw->cursor, k);
/* We need x position before we move cursor */
x = piscol(bw->cursor) - bw->offset;
pgetc(bw->cursor);
/* Tabs are weird here... */
if (bw->o.overtype && !piseol(bw->cursor) && k != '\t')
udelch(bw->parent, 0);
/* Not sure if we're in right position for wordwrap when we're in overtype mode */
if (bw->o.wordwrap && piscol(bw->cursor) > bw->o.rmargin && !joe_isblank(map,k)) {
wrapword(bw, bw->cursor, bw->o.lmargin, bw->o.french, 0, NULL);
simple = 0;
}
bw->cursor->xcol = piscol(bw->cursor);
#ifndef __MSDOS__
if (x < 0 || x >= bw->w)
simple = 0;
if (bw->cursor->line < bw->top->line || bw->cursor->line >= bw->top->line + bw->h)
simple = 0;
if (simple && bw->parent->t->t->sary[bw->y + bw->cursor->line - bw->top->line])
simple = 0;
if (cclass_lookup(cclass_combining, k))
simple = 0;
if (simple && k != '\t' && k != '\n' && !curmacro) {
int atr;
SCRN *t = bw->parent->t->t;
ptrdiff_t y = bw->y + TO_DIFF_OK(bw->cursor->line - bw->top->line);
int (*screen)[COMPOSE] = t->scrn + y * t->co;
int *attr = t->attr + y * t->co;
x += bw->x;
atr = BG_COLOR(bg_text);
if (!upd && piseol(bw->cursor) && !bw->o.highlight)
t->updtab[y] = 0;
if (markb &&
markk &&
markb->b == bw->b &&
markk->b == bw->b &&
((!square && bw->cursor->byte >= markb->byte && bw->cursor->byte < markk->byte) ||
( square && bw->cursor->line >= markb->line && bw->cursor->line <= markk->line && piscol(bw->cursor) >= markb->xcol && piscol(bw->cursor) < markk->xcol)))
atr |= INVERSE;
outatr(bw->b->o.charmap, t, screen + x, attr + x, x, y, k, atr);
}
#endif
}
return 0;
}
int utypebw(BW *bw, int k)
{
return utypebw_raw(bw, k, 0);
}
int utypew(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
return utypebw(bw, k);
}
/* Quoting */
static B *unicodehist = NULL; /* History of previously entered unicode characters */
static int dounicode(W *w, char *s, void *object, int *notify)
{
BW *bw;
int num;
WIND_BW(bw, w);
num = zhtoi(s);
if (notify)
*notify = 1;
vsrm(s);
utypebw_raw(bw, num, 1);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
}
int quotestate;
int quoteval;
static int doquote(W *w, int c, void *object, int *notify)
{
BW *bw;
char buf[40];
WIND_BW(bw, w);
/*
if (c < 0 || c >= 256) {
nungetc(c);
return -1;
}
*/
switch (quotestate) {
case 0:
if (c >= '0' && c <= '9') {
quoteval = c - '0';
quotestate = 1;
joe_snprintf_1(buf, SIZEOF(buf), "ASCII %c--", c);
if (!mkqwna(bw->parent, sz(buf), doquote, NULL, NULL, notify))
return -1;
else
return 0;
} else if (c == 'x' || c == 'X') {
if (bw->b->o.charmap->type) {
if (!wmkpw(bw->parent, joe_gettext(_("Unicode (ISO-10646) character in hex (%{abort} to abort): ")), &unicodehist, dounicode,
NULL, NULL, NULL, NULL, NULL, locale_map, 0))
return 0;
else
return -1;
} else {
quotestate = 3;
if (!mkqwna(bw->parent, sc("ASCII 0x--"), doquote, NULL, NULL, notify))
return -1;
else
return 0;
}
} else if (c == 'o' || c == 'O') {
quotestate = 5;
if (!mkqwna(bw->parent, sc("ASCII 0---"), doquote, NULL, NULL, notify))
return -1;
else
return 0;
} else {
if ((c >= 0x40 && c <= 0x5F) || (c >= 'a' && c <= 'z'))
c &= 0x1F;
if (c == '?')
c = 127;
utypebw_raw(bw, c, 1);
bw->cursor->xcol = piscol(bw->cursor);
}
break;
case 1:
if (c >= '0' && c <= '9') {
joe_snprintf_2(buf, SIZEOF(buf), "ASCII %c%c-", quoteval + '0', c);
quoteval = quoteval * 10 + c - '0';
quotestate = 2;
if (!mkqwna(bw->parent, sz(buf), doquote, NULL, NULL, notify))
return -1;
else
return 0;
}
break;
case 2:
if (c >= '0' && c <= '9') {
quoteval = quoteval * 10 + c - '0';
utypebw_raw(bw, quoteval, 1);
bw->cursor->xcol = piscol(bw->cursor);
}
break;
case 3:
if (c >= '0' && c <= '9') {
joe_snprintf_1(buf, SIZEOF(buf), "ASCII 0x%c-", c);
quoteval = c - '0';
quotestate = 4;
if (!mkqwna(bw->parent, sz(buf), doquote, NULL, NULL, notify))
return -1;
else
return 0;
} else if (c >= 'a' && c <= 'f') {
joe_snprintf_1(buf, SIZEOF(buf), "ASCII 0x%c-", c + 'A' - 'a');
quoteval = c - 'a' + 10;
quotestate = 4;
if (!mkqwna(bw->parent, sz(buf), doquote, NULL, NULL, notify))
return -1;
else
return 0;
} else if (c >= 'A' && c <= 'F') {
joe_snprintf_1(buf, SIZEOF(buf), "ASCII 0x%c-", c);
quoteval = c - 'A' + 10;
quotestate = 4;
if (!mkqwna(bw->parent, sz(buf), doquote, NULL, NULL, notify))
return -1;
else
return 0;
}
break;
case 4:
if (c >= '0' && c <= '9') {
quoteval = quoteval * 16 + c - '0';
utypebw_raw(bw, quoteval, 1);
bw->cursor->xcol = piscol(bw->cursor);
} else if (c >= 'a' && c <= 'f') {
quoteval = quoteval * 16 + c - 'a' + 10;
utypebw_raw(bw, quoteval, 1);
bw->cursor->xcol = piscol(bw->cursor);
} else if (c >= 'A' && c <= 'F') {
quoteval = quoteval * 16 + c - 'A' + 10;
utypebw_raw(bw, quoteval, 1);
bw->cursor->xcol = piscol(bw->cursor);
}
break;
case 5:
if (c >= '0' && c <= '7') {
joe_snprintf_1(buf, SIZEOF(buf), "ASCII 0%c--", c);
quoteval = c - '0';
quotestate = 6;
if (!mkqwna(bw->parent, sz(buf), doquote, NULL, NULL, notify))
return -1;
else
return 0;
}
break;
case 6:
if (c >= '0' && c <= '7') {
joe_snprintf_2(buf, SIZEOF(buf), "ASCII 0%c%c-", quoteval + '0', c);
quoteval = quoteval * 8 + c - '0';
quotestate = 7;
if (!mkqwna(bw->parent, sz(buf), doquote, NULL, NULL, notify))
return -1;
else
return 0;
}
break;
case 7:
if (c >= '0' && c <= '7') {
quoteval = quoteval * 8 + c - '0';
utypebw_raw(bw, quoteval, 1);
bw->cursor->xcol = piscol(bw->cursor);
}
break;
}
if (notify)
*notify = 1;
return 0;
}
int uquote(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
quotestate = 0;
if (mkqwna(bw->parent, sz(joe_gettext(_("Ctrl- (or 0-9 for dec. ascii, x for hex, or o for octal)"))), doquote, NULL, NULL, NULL))
return 0;
else
return -1;
}
static int doquote9(W *w, int c, void *object, int *notify)
{
BW *bw;
WIND_BW(bw, w);
if (notify)
*notify = 1;
if ((c >= 0x40 && c <= 0x5F) || (c >= 'a' && c <= 'z'))
c &= 0x1F;
if (c == '?')
c = 127;
if (c >= 0 && c <= 127)
c |= 128;
utypebw_raw(bw, c, 1);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
}
static int doquote8(W *w, int c, void *object, int *notify)
{
BW *bw;
WIND_BW(bw, w);
if (c == '`') {
if (mkqwna(bw->parent, sc("Meta-Ctrl-"), doquote9, NULL, NULL, notify))
return 0;
else
return -1;
}
if (notify)
*notify = 1;
if (c >= 0 && c <= 127)
c |= 128;
utypebw_raw(bw, c, 1);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
}
int uquote8(W *w, int k)
{
if (mkqwna(w, sc("Meta-"), doquote8, NULL, NULL, NULL))
return 0;
else
return -1;
}
static int doctrl(W *w, int c, void *object, int *notify)
{
BW *bw;
int org;
WIND_BW(bw, w);
org = bw->o.overtype;
if (notify)
*notify = 1;
bw->o.overtype = 0;
if ((bw->parent->huh == srchstr || bw->parent->huh == replstr) && c == '\n') {
utypebw(bw, '\\');
utypebw(bw, 'n');
} else
utypebw_raw(bw, c, 1);
bw->o.overtype = org;
bw->cursor->xcol = piscol(bw->cursor);
return 0;
}
int uctrl(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (mkqwna(bw->parent, sz(joe_gettext(_("Quote"))), doctrl, NULL, NULL, NULL))
return 0;
else
return -1;
}
/* User hit Return. Deal with autoindent.
*/
int rtntw(W *w)
{
BW *bw;
WIND_BW(bw, w);
if (bw->o.overtype) {
p_goto_eol(bw->cursor);
if (piseof(bw->cursor))
binsc(bw->cursor, '\n');
pgetc(bw->cursor);
bw->cursor->xcol = piscol(bw->cursor);
} else {
P *p = pdup(bw->cursor, "rtntw");
int c;
binsc(bw->cursor, '\n'), pgetc(bw->cursor);
/* Suppress autoindent if we're on a space or tab... */
if (bw->o.autoindent /* && (brch(bw->cursor)!=' ' && brch(bw->cursor)!='\t')*/) {
p_goto_bol(p);
while (joe_isspace(bw->b->o.charmap,(c = pgetc(p))) && c != '\n') {
binsc(bw->cursor, c);
pgetc(bw->cursor);
}
}
prm(p);
bw->cursor->xcol = piscol(bw->cursor);
}
return 0;
}
/* Open a line */
int uopen(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
binsc(bw->cursor,'\n');
if (bw->o.autoindent && (brch(bw->cursor)!=' ' && brch(bw->cursor)!='\t')) {
P *p = pdup(bw->cursor, "uopen");
P *q = pdup(p, "uopen");
int c;
pgetc(q);
p_goto_bol(p);
while (joe_isspace(bw->b->o.charmap,(c = pgetc(p))) && c != '\n') {
binsc(q, c);
pgetc(q);
}
prm(p); prm(q);
}
return 0;
}
/* Set book-mark */
static int dosetmark(W *w, int c, void *object, int *notify)
{
BW *bw;
WIND_BW(bw, w);
if (notify)
*notify = 1;
if (c >= '0' && c <= ':') {
pdupown(bw->cursor, bw->b->marks + c - '0', "dosetmark");
poffline(bw->b->marks[c - '0']);
if (c!=':') {
joe_snprintf_1(msgbuf, JOE_MSGBUFSIZE, joe_gettext(_("Mark %d set")), c - '0');
msgnw(bw->parent, msgbuf);
}
return 0;
} else {
nungetc(c);
return -1;
}
}
int usetmark(W *w, int c)
{
if (c >= '0' && c <= ':')
return dosetmark(w, c, NULL, NULL);
else if (mkqwna(w, sz(joe_gettext(_("Set mark (0-9):"))), dosetmark, NULL, NULL, NULL))
return 0;
else
return -1;
}
/* Goto book-mark */
static int dogomark(W *w, int c, void *object, int *notify)
{
BW *bw;
WIND_BW(bw, w);
if (notify)
*notify = 1;
if (c >= '0' && c <= ':')
if (bw->b->marks[c - '0']) {
pset(bw->cursor, bw->b->marks[c - '0']);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
} else {
joe_snprintf_1(msgbuf, JOE_MSGBUFSIZE, joe_gettext(_("Mark %d not set")), c - '0');
msgnw(bw->parent, msgbuf);
return -1;
} else {
nungetc(c);
return -1;
}
}
int ugomark(W *w, int c)
{
if (c >= '0' && c <= '9')
return dogomark(w, c, NULL, NULL);
else if (mkqwna(w, sz(joe_gettext(_("Goto bookmark (0-9):"))), dogomark, NULL, NULL, NULL))
return 0;
else
return -1;
}
/* Goto next instance of character */
static int dobkwdc;
static int dofwrdc(W *w, int k, void *object, int *notify)
{
BW *bw;
int c;
P *q;
WIND_BW(bw, w);
if (notify)
*notify = 1;
if (k < 0 || k >= 256) {
nungetc(k);
return -1;
}
q = pdup(bw->cursor, "dofwrdc");
if (dobkwdc) {
while ((c = prgetc(q)) != NO_MORE_DATA)
if (c == k)
break;
} else {
while ((c = pgetc(q)) != NO_MORE_DATA)
if (c == k)
break;
}
if (c == NO_MORE_DATA) {
msgnw(bw->parent, joe_gettext(_("Not found")));
prm(q);
return -1;
} else {
pset(bw->cursor, q);
bw->cursor->xcol = piscol(bw->cursor);
prm(q);
return 0;
}
}
int ufwrdc(W *w, int k)
{
dobkwdc = 0;
if (k >= 0 && k < 256)
return dofwrdc(w, k, NULL, NULL);
else if (mkqw(w, sz(joe_gettext(_("Forward to char: "))), dofwrdc, NULL, NULL, NULL))
return 0;
else
return -1;
}
int ubkwdc(W *w, int k)
{
dobkwdc = 1;
if (k >= 0 && k < 256)
return dofwrdc(w, k, NULL, NULL);
else if (mkqw(w, sz(joe_gettext(_("Backward to char: "))), dofwrdc, NULL, NULL, NULL))
return 0;
else
return -1;
}
/* Display a message */
static int domsg(W *w, char *s, void *object, int *notify)
{
if (notify)
*notify = 1;
zlcpy(msgbuf, SIZEOF(msgbuf), s);
vsrm(s);
msgnw(w, msgbuf);
return 0;
}
int umsg(W *w, int k)
{
if (wmkpw(w, joe_gettext(_("Message (%{abort} to abort): ")), NULL, domsg, NULL, NULL, NULL, NULL, NULL, locale_map, 0))
return 0;
else
return -1;
}
/* Insert text */
static int dotxt(W *w, char *s, void *object, int *notify)
{
char fill;
char *str;
BW *bw;
WIND_BW(bw, w);
if (notify)
*notify = 1;
if (s[0] == '`') {
str = vsmk(1024);
fill = ' ';
str = stagen(str, bw, &s[1], fill);
vsrm(s);
s = str;
}
if (s) {
const char *t = s;
ptrdiff_t len = sLEN(s);
while (len) {
int c;
if (bw->b->o.charmap->type)
c = utf8_decode_fwrd(&t, &len);
else {
c = *(const unsigned char *)t++;
--len;
}
if (c >= 0)
utypebw_raw(bw, c, 1);
}
vsrm(s);
}
return 0;
}
int utxt(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (wmkpw(w, joe_gettext(_("Insert (%{abort} to abort): ")), NULL, dotxt, NULL, NULL, utypebw, NULL, NULL, bw->b->o.charmap, 0))
return 0;
else
return -1;
}
/* Insert current file name */
int uname_joe(W *w, int k)
{
const char *s;
s=((BW *)w->main->object)->b->name;
if (!s || !*s)
return -1;
while (*s)
if (utypew(w,*(const unsigned char *)s++))
return -1;
return 0;
}
/* Insert until non-base64 character received */
int upaste(W *w, int k)
{
int c;
int accu = 0;
int count;
int tmp_ww;
int tmp_ai;
BW *bw;
WIND_BW(bw, w);
tmp_ww = bw->o.wordwrap;
tmp_ai = bw->o.autoindent;
bw->o.wordwrap = 0;
bw->o.autoindent = 0;
count = 0;
/* We have to wait for the second ';' */
while ((c = ttgetch()) != -1)
if (c == ';')
break;
if (c == -1)
goto bye;
while ((c = ttgetch()) != -1) {
if (c >= 'A' && c <= 'Z')
c = c - 'A';
else if (c >= 'a' && c <= 'z')
c = c - 'a' + 26;
else if (c >= '0' && c <= '9')
c = c - '0' + 52;
else if (c == '+')
c = 62;
else if (c == '/')
c = 63;
else if (c == '=')
continue;
else
break;
switch (count) {
case 0:
accu = c;
count = 6;
break;
case 2:
accu = (accu << 6) + c;
if (accu == 13)
rtntw(bw->parent);
else
utypebw(bw, accu);
count = 0;
break;
case 4:
accu = (accu << 4) + (c >> 2);
if (accu == 13)
rtntw(bw->parent);
else
utypebw(bw, accu);
accu = (c & 0x3);
count = 2;
break;
case 6:
accu = (accu << 2) + (c >> 4);
if (accu == 13)
rtntw(bw->parent);
else
utypebw(bw, accu);
accu = (c & 0xF);
count = 4;
break;
}
}
/* Terminator is ESC \ */
if (c == 033) {
ttgetch();
}
bye:
bw->o.wordwrap = tmp_ww;
bw->o.autoindent = tmp_ai;
return 0;
}
/* Bracketed paste */
int saved_ww;
int saved_ai;
int saved_sp;
int ubrpaste(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
saved_ww = bw->o.wordwrap;
saved_ai = bw->o.autoindent;
saved_sp = bw->o.spaces;
bw->o.wordwrap = bw->o.autoindent = bw->o.spaces = 0;
return 0;
}
int ubrpaste_done(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
bw->o.wordwrap = saved_ww;
bw->o.autoindent = saved_ai;
bw->o.spaces = saved_sp;
return 0;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.