File: mp_core.c

package info (click to toggle)
mped 3.3.17-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,288 kB
  • ctags: 1,635
  • sloc: ansic: 13,150; sh: 393; makefile: 232; perl: 66
file content (2619 lines) | stat: -rw-r--r-- 45,530 bytes parent folder | download
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
/*

    mp_core.c

    Text editing engine.

    mp - Programmer Text Editor

    Copyright (C) 1991-2005 Angel Ortega <angel@triptico.com>

    This program 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 2
    of the License, or (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    http://www.triptico.com

*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>

#include "mp_core.h"

#ifdef CONFOPT_PCRE
#include <pcreposix.h>
#endif

#ifdef CONFOPT_SYSTEM_REGEX
#include <regex.h>
#endif

#ifdef CONFOPT_INCLUDED_REGEX
#include "gnu_regex.h"
#endif

/*********************
	  Data
**********************/

/**
 * _mp_txts - Chained list of texts
 *
 * The chained list of texts. This is where all the document
 * texts are stored.
 */
mp_txt * _mp_txts=NULL;

/**
 * _mp_active - Pointer to the active text
 *
 * Pointer to the active text. Assumed to be always non-NULL.
 */
mp_txt * _mp_active=NULL;

/**
 * _mp_clipboard - Pointer to the clipboard
 *
 * Pointer to a text containing the clipboard. Used for
 * copying and pasting.
 */
mp_txt * _mp_clipboard=NULL;

/**
 * _mp_tab_size - Size (in spaces) of a tab character
 *
 * Size in spaces of each tab column position.
 */
int _mp_tab_size=DEFAULT_TAB_SIZE;

/* the word separators */
char _mp_separators[40]=" \r\n:)=!;,-<>()[]|+&\"\t";

/**
 * _mp_word_wrap - Column where the word wrapping is done.
 *
 * Any word crossing the column boundary marked by this
 * variable will be sent to the next line. A value of 0
 * means no word wrapping (so right margin expands forever).
 */
int _mp_word_wrap=0;

/**
 * _mp_wheel_scroll_rows - Number of rows to scroll wheel wheel is rolled.
 *
 * Rolling the wheel scrolls this number of line up or down.
 * A value of 0, will scroll a full page.
 */
int _mp_wheel_scroll_rows=DEFAULT_WHEEL_SCROLL_ROWS;

/**
 * _mp_save_tabs - Tab saving flag.
 *
 * If this flag is set, a string of spaces that crosses
 * a tab column boundary will be saved as a tab. Otherwise,
 * the spaces themselves will be saved.
 */
int _mp_save_tabs=1;

/**
 * _mp_auto_indent - Auto indentation flag.
 *
 * If this flag is set, a new line will have the same spaces
 * at the beginning as the previous one, if any. Otherwise, a
 * new line will start always exactly at the first column.
 */
int _mp_auto_indent=AUTO_INDENT;

/**
 * _mp_case_cmp - Case sensitive compare flag.
 *
 * When this flag is set, searches are made case sensitive.
 */
int _mp_case_cmp=1;

/* LF->CR/LF flag */
int _mp_cr_lf=0;

/* # of chars to indent */
int _mp_indent=0;

/* notify function */
static void mp_notify_stub(char *);
void (* _mp_notify)(char *)=mp_notify_stub;

/* temporal txt */
mp_txt _mp_tmp_txt;

/* the logger */
mp_txt * _mp_log=NULL;

/* use regular expressions in seeks */
int _mp_regex=1;

/* _mp_txts version (incremented on each open/close) */
int _mp_txts_version=0;

/* encryption machine (ARCFOUR algorithm) */

static int _mp_arcfour_i;
static int _mp_arcfour_j;
static unsigned char _mp_arcfour_S[256];

/* encrypted file signature */
static char _mp_crypt1_sig[]="mpcrypt1\n";


/*******************
	Code
********************/


static void * mp_malloc(int size)
{
	return(malloc(size));
}


static void mp_free(void * ptr)
{
	free(ptr);
}


/**
 * _im_alive - The infamous spinning bar
 *
 * Each time it's called, this function returns a string
 * with a frame of an animated spinning bar. Just to
 * show the user the program is alive.
 */
char * _im_alive(void)
{
	static char str[5];
	static int nseq=0;
	char * seq="|/-\\";

	str[0]=seq[nseq++];
	nseq&=3;

	str[1]='\0';

	return(str);
}


/**
 * mp_notify_stub - Dummy notify function
 * @str: message (ignored)
 *
 * _mp_notify points here at startup. The upper level
 * must provide a function (with this prototype), that
 * will be called from within the engine when a message
 * must be notified to the user (str will be that message).
 */
static void mp_notify_stub(char * str)
{
}


/**
 * _mp_error - Bangs an error
 * @s: The error string
 * @line: the code file line number
 *
 * Dumps an error to stdout. Usually called only from
 * an assert-like macro on a probably fatal internal error.
 * It must not happen; this usually means
 * that it will be called when less expected.
 */
void _mp_error(char * s, int line)
{
	fprintf(stderr,"Oops! [%s] [%d] mp_error\n", s, line);
	fflush(stderr);

	mp_log("Oops! [%s] [%d] mp_error\n", s, line);
}


/**
 * mp_create_block - Creates a block
 *
 * Creates a new block of text. Returns the zero filled block
 * or NULL if out of memory or any other horrible event.
 */
static mp_blk * mp_create_block(void)
{
	mp_blk * b;

	if((b=(mp_blk *) mp_malloc(sizeof(mp_blk)))==NULL)
	{
		MP_ERROR("mp_create_block");
		return(NULL);
	}

	memset(b,'\0',sizeof(mp_blk));

	return(b);
}


/**
 * mp_name_txt - Names a text
 * @txt: The text to be named
 * @name: the name to set
 *
 * Assigns @name to a text. This usually is the file name.
 * If name is NULL, the empty string will be set.
 */
void mp_name_txt(mp_txt * txt, char * name)
{
	if(name != NULL)
	{
		if(txt->name != NULL)
			free(txt->name);

		txt->name=strdup(name);
	}

	_mp_txts_version++;
}


/**
 * mp_find_text - Find a text by its name
 * @name: The name of the text to be found
 *
 * Returns a pointer to a text if found, or NULL
 * if there is no text with this name.
 */
mp_txt * mp_find_txt(char * name)
{
	mp_txt * txt;

	for(txt=_mp_txts;txt!=NULL;txt=txt->next)
	{
		if(strcmp(txt->name,name)==0)
			break;
	}

	return(txt);
}


/**
 * mp_create_sys_txt - Creates a new text
 * @name: the name of the new text
 *
 * Returns a new text with its first block, or NULL
 * if out of memory. The new text is NOT appended
 * to the active texts list; this function must only
 * be used to create special-purpose texts (as the
 * clipboard).
 */
mp_txt * mp_create_sys_txt(char * name)
{
	mp_txt * txt;

	if((txt=mp_malloc(sizeof(mp_txt)))!=NULL)
	{
		memset(txt,'\0',sizeof(mp_txt));

		txt->type=MP_TYPE_TEXT;
		txt->sys=1;

		txt->first=txt->cursor=mp_create_block();

		if(txt->first==NULL)
		{
			MP_ERROR("txt->first");
			mp_free(txt);
			txt=NULL;
		}
		else
		{
			mp_modified(txt);
			txt->mod=0;

			/* first block has one char: the EOF (\0) */
			txt->cursor->buf[0]='\0';
			txt->cursor->size=1;
		}

		mp_name_txt(txt,name);
	}
	else
		MP_ERROR("mp_create_txt");

	return(txt);
}


/**
 * mp_create_txt - Creates a new text
 * @name: the name of the new text
 *
 * Creates a new text and links it to the list of
 * loaded texts. Returns a pointer to it or NULL
 * if out of memory. The new text is set as the
 * active one.
 */
mp_txt * mp_create_txt(char * name)
{
	mp_txt * txt;

	if((txt=mp_create_sys_txt(name))==NULL)
		return(NULL);

	/* links to list */
	txt->next=_mp_txts;
	_mp_txts=txt;

	/* txt is not a system txt */
	txt->sys=0;

	/* now it's the active one */
	_mp_active=txt;

	return(txt);
}


/**
 * mp_get_tmp_txt - Gets a temporal text
 * @otxt: the original text
 *
 * Returns a text that is a copy of otxt.
 */
mp_txt * mp_get_tmp_txt(mp_txt * otxt)
{
	mp_txt * dtxt;

	dtxt=&_mp_tmp_txt;

	dtxt->first=otxt->first;
	dtxt->cursor=otxt->cursor;
	dtxt->offset=otxt->offset;
	dtxt->x=otxt->x;
	dtxt->y=otxt->y;
	dtxt->vx=otxt->vx;
	dtxt->vy=otxt->vy;
	dtxt->mbx=otxt->mbx;
	dtxt->mby=otxt->mby;
	dtxt->mex=otxt->mex;
	dtxt->mey=otxt->mey;

	return(dtxt);
}


/**
 * mp_end_tmp_txt - Stop using the temporal text
 *
 * Stop using the temporal copy returned by
 * mp_get_tmp_txt(). Now it's just a dummy.
 */
void mp_end_tmp_txt(void)
{
}


/**
 * mp_delete_sys_txt - Deletes a text
 * @txt: the text to be deleted
 *
 * Destroys a text and its blocks. Only to be
 * used in special-purpose texts (that returned
 * by mp_create_sys_txt()).
 */
void mp_delete_sys_txt(mp_txt * txt)
{
	mp_blk * blk;
	mp_blk * blk2;

	if(txt->name != NULL) free(txt->name);
	if(txt->passwd != NULL) free(txt->passwd);

	for(blk=txt->first;blk!=NULL;blk=blk2)
	{
		blk2=blk->next;
		mp_free(blk);
	}

	mp_free(txt);
}


/**
 * mp_delete_txt - Deletes a text
 * @txt: the text to be deleted
 *
 * Deletes a text and its blocks. The next one
 * in the list (if any) will become the
 * active one.
 */
void mp_delete_txt(mp_txt * txt)
{
	mp_txt * t2;

	/* unlinks it */
	if(_mp_txts==txt)
		_mp_txts=txt->next;
	else
	{
		/* find one whose next is txt */
		for(t2=_mp_txts;t2->next!=txt;
			t2=t2->next);

		t2->next=txt->next;
	}

	/* _mp_active will be the next one */
	if((_mp_active=txt->next)==NULL)
		_mp_active=_mp_txts;

	/* next is nothing */
	txt->next=NULL;

	/* finally destroy, unless it's a system text;
	   if txt->sys is set, it means that txt is
	   a system txt that was temporarily shown
	   so it must be only unchained */
	if(!txt->sys) mp_delete_sys_txt(txt);

	_mp_txts_version++;
}


/**
 * mp_show_sys_txt - Shows an internal (system) text
 * @txt: the system text
 *
 * Links to the 'visible' chain of normal (user) texts
 * a internal (system) text, as the clipboard or the
 * log. This make them be shown as read-only texts.
 */
void mp_show_sys_txt(mp_txt * txt)
{
	mp_txt * t2;

	/* test if already linked */
	for(t2=_mp_txts;t2 != NULL && t2 != txt;t2=t2->next);

	/* link only if not already linked */
	if(t2==NULL)
	{
		/* links to list */
		txt->next=_mp_txts;
		_mp_txts=txt;
	}

	/* now it's the active one */
	_mp_active=txt;

	_mp_txts_version++;

	/* txt is left marked as a system txt */
}


/**
 * mp_empty_txt - Empties a text
 * @txt: the text
 *
 * Empties a text, cleaning all content.
 */
void mp_empty_txt(mp_txt * txt)
{
	mp_move_bof(txt);
	mp_mark(txt);
	mp_move_eof(txt);
	mp_move_eol(txt);
	mp_mark(txt);
	mp_delete_mark(txt);
}


/**
 * mp_next_txt - Select next text
 *
 * Selects the next text as the active one.
 */
void mp_next_txt(void)
{
	if((_mp_active=_mp_active->next) == NULL)
		_mp_active=_mp_txts;
}


/**
 * mp_prev_txt - Select previous text
 *
 * Selects the previous text as the active one.
 */
void mp_prev_txt(void)
{
	mp_txt * txt;

	for(txt=_mp_txts;txt->next && txt->next != _mp_active;txt=txt->next);

	_mp_active=txt;
}


/**
 * mp_change_cursor - set the block where the cursor is
 * @txt: the text to be changed
 * @blk: the block where the cursor is
 *
 * Sets the block where the cursor is over.
 * This function is internal and must not be used.
 */
static int mp_change_cursor(mp_txt * txt, mp_blk * blk)
{
	txt->cursor=blk;

	return(1);
}


/**
 * mp_peek_char - Returns the character over the cursor
 * @txt: the text
 *
 * Returns the character over the cursor without moving it.
 * If the cursor is at EOF, '\0' is returned.
 */
int mp_peek_char(mp_txt * txt)
{
	return(txt->cursor->buf[txt->offset]);
}


/**
 * mp_modified - Marks a text as modified
 * @txt: the text
 *
 * Marks a text as modified.
 */
void mp_modified(mp_txt * txt)
{
	/* unmark block */
	mp_unmark(txt);

	/* forget last search match */
	txt->hbx=-1;

	/* now it's modified */
	txt->mod=1;
}


/**
 * mp_visual_column - Returns the visual column
 * @txt: the text
 *
 * Returns the visual column where the cursor is, taking into
 * account the possible tabs in the line. If there are no
 * tabs in the line, the return value should be equal to txt->x.
 */
int mp_visual_column(mp_txt * txt)
{
	int x,r;

	x=txt->x; r=0;
	mp_move_bol(txt);

	while(txt->x < x)
	{
		if(mp_peek_char(txt)=='\t')
			r+=MP_REAL_TAB_SIZE(r);
		else
			r++;

		mp_move_right(txt);
	}

	return(r);
}


/**
 * mp_adjust - Adjusts the current window pointers.
 * @txt: the text
 * @tx: the current window width
 * @ty: the current window height
 *
 * Adjust the current window pointers (variables vx and vy
 * of the text) to a window of tx width and ty height so
 * that the cursor is visible.
 */
void mp_adjust(mp_txt * txt, int tx, int ty)
{
	int r;

	r=mp_visual_column(txt);

	if(r < txt->vx)
		txt->vx=r;

	if(r > (txt->vx + tx - 1))
		txt->vx=r-tx+1;

	if(txt->y < txt->vy)
		txt->vy=txt->y;

	if(txt->y > (txt->vy + ty - 1))
		txt->vy=txt->y-ty+1;
}


/**
 * mp_match_bracket - Matches the bracket over the cursor
 * @txt: the text
 * @max: maximum characters to search (-1, no limit)
 *
 * If the cursor is over an opening bracket (round, square or curly)
 * of over a closing one, this function sets the internal brx and bry
 * of @txt with the position of the twin matching bracket, searching
 * forward or backwards, respectively. A maximum of @max characters
 * will be tested, unless set to -1, where the full text will be scanned.
 */
void mp_match_bracket(mp_txt * txt, int max)
{
	int c,d,s,n;
	char * ob="([{";
	char * cb=")]}";
	char * ptr;
	mp_txt * t;

	/* forgets current one */
	txt->brx=txt->bry=-1;

	if((c=mp_peek_char(txt)) == '\0')
		return;

	if((ptr=strchr(ob, c)) != NULL)
	{
		/* over an open bracket; search forward */
		d=cb[ptr - ob];
		s=1;
	}
	else
	if((ptr=strchr(cb, c)) != NULL)
	{
		/* over a close bracket; search backwards */
		d=ob[ptr - cb];
		s=-1;
	}
	else
		return;

	t=mp_get_tmp_txt(txt);
	n=0;

	while(max != 0)
	{
		if(mp_peek_char(t) == c)
		{
			/* found again; increment */
			n++;
		}
		else
		if(mp_peek_char(t) == d)
		{
			/* found the inverse; decrement */
			if(--n == 0)
			{
				/* out of levels; store and break */
				txt->brx=t->x;
				txt->bry=t->y;

				break;
			}
		}

		/* move */
		if(s == -1)
		{
			if(!mp_move_left(t))
				break;
		}
		else
		{
			if(!mp_move_right(t))
				break;
		}

		max--;
	}

	mp_end_tmp_txt();
}


/**
 * mp_set_notify - Sets the notify function.
 * @func: the new notify function.
 *
 * The notify function is called from inside file loading
 * and searching routines to inform the user about what is
 * happening, sending a message in a character string.
 * This function changes the current (dummy) one.
 */
void mp_set_notify(void (* func)(char *))
{
	_mp_notify=func;
}


/**
 * mp_debug_hook - Dummy debug hook function.
 *
 * This function does nothing. It is just defined to
 * be inserted as a breakpoint for the debugger.
 */
void mp_debug_hook(void)
{
}


/* movement functions */

/**
 * mp_is_sep - separator test
 * @c: the char to test
 *
 * Returns 1 if c is a word separator.
 */
int mp_is_sep(char c)
{
	if(strchr(_mp_separators, c)==NULL)
		return(0);
	else
		return(1);
}


/**
 * mp_recalc_x - Recalculates txt->x
 * @txt: the text
 *
 * Recalculates the value of the x cursor position.
 * Only called internally.
 */
void mp_recalc_x(mp_txt * txt)
{
	int offset;
	mp_blk * blk;
	int x;

	offset=txt->offset;
	blk=txt->cursor;
	x=0;

	for(;;)
	{
		offset--;

		/* if block underflow... */
		if(offset==-1)
		{
			/* it is the first block; stop */
			if(blk->last==NULL)
				break;

			/* travel back the block chain */
			blk=blk->last;

			offset=blk->size-1;
		}

		if(blk->buf[offset]=='\n')
			break;

		x++;
	}

	txt->x=x;
}


/**
 * mp_move_right - Moves cursor one char to the right
 * @txt: the text
 *
 * Moves the cursor one char to the right. Returns 0 if
 * the movement could not be done (i.e., at EOF). If
 * cursor moves over the end of line, it moves to
 * the beginning of the next line.
 */
int mp_move_right(mp_txt * txt)
{
	char c;

	c=mp_peek_char(txt);

	if(txt->offset==txt->cursor->size-1)
	{
		/* is it the last char of file? */
		if(txt->cursor->next==NULL)
		{
			txt->lasty=txt->y;
			return(0);
		}

		/* movement forces a block change */
		mp_change_cursor(txt, txt->cursor->next);

		txt->offset=0;
	}
	else
		txt->offset++;

	/* if char was a '\n', move forward to next line */
	if(c=='\n')
	{
		txt->x=0;
		txt->y++;
	}
	else
		txt->x++;

	return(1);
}


/**
 * mp_move_left - Moves cursor one char to the right
 * @txt: the text
 *
 * Moves the cursor one char to the left. Returns 0 if
 * the movement could not be done (i.e., at BOF). If it
 * moves to the left of the first column, moves to the
 * end of the previous line.
 */
int mp_move_left(mp_txt * txt)
{
	if(txt->offset==0)
	{
		/* is it the beginning of the text? */
		if(txt->cursor->last==NULL)
			return(0);

		/* block change */
		mp_change_cursor(txt, txt->cursor->last);

		txt->offset=txt->cursor->size;
	}

	txt->offset--;

	/* if over '\n', the line has changed */
	if(mp_peek_char(txt)=='\n')
	{
		/* integrity check: if a \n was reached and x is
		   not 0, something strange had happened */
		if(txt->x!=0)
			MP_ERROR("txt->x!=0");

		/* recalcs x to find previous \n */
		mp_recalc_x(txt);

		/* move up */
		txt->y--;
	}
	else
		txt->x--;

	return(1);
}


/**
 * mp_move_to_visual_column - Tries to recover a column position
 * @txt: the text
 * @r: the position
 *
 * Tries to move to the @r visual column position, taking
 * tabs into account.
 */
void mp_move_to_visual_column(mp_txt * txt, int r)
{
	int n,c;

	for(n=0;n < r;)
	{
		c=mp_peek_char(txt);

		if(c=='\n') break;
		if(! mp_move_right(txt)) break;

		if(c=='\t')
			n+=MP_REAL_TAB_SIZE(n);
		else
			n++;
	}
}


/**
 * mp_move_down - Moves cursor one line down
 * @txt: the text
 *
 * Moves the cursor one line down. Returns 0 if
 * the movement could not be done (i.e., at EOF). The
 * x position is preserved if possible.
 */
int mp_move_down(mp_txt * txt)
{
	int x,y,r;

	/* save current coords */
	x=txt->x;
	y=txt->y;
	r=mp_visual_column(txt);

	/* move to end of line */
	mp_move_eol(txt);

	/* try to move beyond it */
	if(! mp_move_right(txt))
	{
		/* no more lines; invalidate move */
		mp_move_xy(txt, x, y);

		return(0);
	}

	/* try to recover previous column */
	mp_move_to_visual_column(txt, r);

	return(1);
}


/**
 * mp_move_bol - Move to the beginning of the line.
 * @txt: the text
 *
 * Moves to the beginning of the current line.
 */
int mp_move_bol(mp_txt * txt)
{
	while(txt->x > 0)
	{
		if(! mp_move_left(txt))
		{
			MP_ERROR("txt->x!=0 at BOF");
			break;
		}
	}

	return(1);
}


/**
 * mp_move_eol - Move to the end of the line.
 * @txt: the text
 *
 * Moves to the end of the current line.
 */
int mp_move_eol(mp_txt * txt)
{
	while(mp_peek_char(txt)!='\n')
	{
		if(! mp_move_right(txt))
			break;
	}

	return(1);
}


/**
 * mp_move_up - Moves cursor one line up
 * @txt: the text
 *
 * Moves the cursor one line up. Returns 0 if
 * the movement could not be done (i.e., at BOF). The
 * x position is preserved if possible.
 */
int mp_move_up(mp_txt * txt)
{
	int ret=1;
	int x;

	x=mp_visual_column(txt);

	mp_move_bol(txt);

	if(! mp_move_left(txt))
		ret=0;

	mp_move_bol(txt);

	/* recover x position if possible */
	mp_move_to_visual_column(txt, x);

	return(ret);
}


/**
 * mp_move_bof - Moves to beginning of file
 * @txt: the text
 *
 * Moves to the beginning of the file.
 */
int mp_move_bof(mp_txt * txt)
{
	mp_change_cursor(txt, txt->first);
	txt->x=0;
	txt->y=0;
	txt->offset=0;

	return(1);
}


/**
 * mp_move_eof - Moves to end of file
 * @txt: the text
 *
 * Moves to the last line of the file. If the last line doesn't
 * end in a new line, the cursor may not actually be at the
 * end of file; use mp_move_eol() after it to be sure.
 */
int mp_move_eof(mp_txt * txt)
{
	while(mp_move_down(txt));

	return(1);
}


/**
 * mp_move_word_right - Moves a word to the right.
 * @txt: the text
 *
 * Moves the cursor one word to the right. Returns 0 if
 * the movement could not be done (i.e., at EOF).
 */
int mp_move_word_right(mp_txt * txt)
{
	while(! mp_is_sep(mp_peek_char(txt)))
	{
		if(! mp_move_right(txt))
			return(0);
	}

	while(mp_is_sep(mp_peek_char(txt)))
	{
		if(! mp_move_right(txt))
			return(0);
	}

	return(1);
}


/**
 * mp_move_word_left - Moves a word to the left.
 * @txt: the text
 *
 * Moves the cursor one word to the left. Returns 0 if
 * the movement could not be done (i.e., at BOF).
 */
int mp_move_word_left(mp_txt * txt)
{
	while(! mp_is_sep(mp_peek_char(txt)))
	{
		if(! mp_move_left(txt))
			return(0);
	}

	while(mp_is_sep(mp_peek_char(txt)))
	{
		if(! mp_move_left(txt))
			return(0);
	}

	return(1);
}


/**
 * mp_move_xy - Moves to x, y position
 * @txt: the text
 * @x: the new x position
 * @y: the new y position
 *
 * Sets the cursor position of text to x, y. Returns 0
 * if the movement cannot be done.
 */
int mp_move_xy(mp_txt * txt, int x, int y)
{
	if(y != txt->y)
	{
		mp_move_bof(txt);

		while(txt->y < y)
		{
			if(! mp_move_down(txt))
				return(0);
		}
	}
	else
		mp_move_bol(txt);

	while(txt->x < x && txt->y==y)
	{
		if(! mp_move_right(txt))
			return(0);
	}

	if(txt->y > y)
		mp_move_left(txt);

	return(1);
}


/**
 * mp_get_char - Gets the char at cursor and advances
 * @txt: the text
 *
 * Returns the char over the cursor and advances to the right.
 * Returns '\0' at EOF.
 */
int mp_get_char(mp_txt * txt)
{
	int c;

	c=mp_peek_char(txt);
	mp_move_right(txt);

	return(c);
}


/**
 * mp_get_str - Gets a string from a text until a delimiter
 * @txt: the text
 * @str: the buffer where the string is get
 * @maxsize: maximum size of buffer
 * @delim: the delimiter to be found
 *
 * Gets a string from a text until the delimiter is found
 * or the maximum size of buffer is reached.
 * Returns the number of chars written into @str.
 */
int mp_get_str(mp_txt * txt, char * str, int maxsize, int delim)
{
	int n,c;

	for(n=0;n < maxsize;n++)
	{
		c=mp_peek_char(txt);
		mp_move_right(txt);

		if(c==delim || c=='\0') break;

		str[n]=c;
	}

	str[n]='\0';

	return(n);
}


/**
 * mp_get_word - Gets the word over the cursor.
 * @txt: the text
 * @buf: the buffer to hold the word
 * @size: maximum size of the buffer
 *
 * Gets the word over the cursor and writes it over
 * the buffer, using the global word separators.
 */
void mp_get_word(mp_txt * txt, char * buf, int size)
{
	mp_txt * ctxt;
	char c;
	int n;

	ctxt=mp_get_tmp_txt(txt);

	/* moves back while not a separator */
	while(! mp_is_sep(mp_peek_char(ctxt)) &&
		mp_move_left(ctxt));

	if(mp_is_sep(mp_peek_char(ctxt)))
		mp_move_right(ctxt);

	/* writes in buffer while not a separator */
	for(n=0;n < size - 1;n++)
	{
		c=mp_get_char(ctxt);

		if(c=='\0' || mp_is_sep(c))
			break;

		buf[n]=c;
	}

	buf[n]='\0';

	mp_end_tmp_txt();
}


/* edit functions */

/**
 * mp_save_state - Saves/restores current state
 * @state: state flag
 *
 * Saves (if @state is > 0) or restores (@state < 0) current state
 * of word-wrapping and auto-indenting.
 */
void mp_save_state(int state)
{
	static int ww=0;
	static int ai=0;

	if(state > 0)
	{
		ww=_mp_word_wrap;
		ai=_mp_auto_indent;
		_mp_word_wrap=_mp_auto_indent=0;
	}
	else
	{
		_mp_word_wrap=ww;
		_mp_auto_indent=ai;
	}
}


/**
 * mp_insert_char - Inserts a char in cursor position
 * @txt: the text
 * @c: the char
 *
 * Inserts a character, moving forward the cursor position.
 * Returns 0 if character could not be inserted
 * (readonly text, errors, etc).
 */
int mp_insert_char(mp_txt * txt, int c)
{
	char o;
	int offset;
	char * buf;
	int size;
	mp_blk * blk;
	mp_blk * nblk;
	int insertar;

	if(txt->type != MP_TYPE_TEXT)
		return(0);

	/* if c is '\n', increment total line count */
	if(c=='\n')
		txt->lasty++;

	buf=txt->cursor->buf;
	size=txt->cursor->size;
	offset=txt->offset;

	/* this should be optimized using memcpy */
	for(;offset<size;offset++)
	{
		o=buf[offset];
		buf[offset]=c;
		c=o;
	}

	/* if no room in block... */
	if(size==BLK_SIZE)
	{
		insertar=1;

		/* take next block */
		blk=txt->cursor->next;

		if(blk!=NULL)
		{
			/* if there is room in next block,
			   no need to insert a new one */
			if(blk->size!=BLK_SIZE)
				insertar=0;
		}

		if(insertar)
		{
			if((nblk=mp_create_block())==NULL)
			{
				MP_ERROR("mp_create_block");
				return(0);
			}

			/* next block points to the new one */
			txt->cursor->next=nblk;

			if(blk!=NULL)
				blk->last=nblk;

			nblk->last=txt->cursor;
			nblk->next=blk;

			blk=nblk;
		}

		/* go on inserting */
		buf=blk->buf;
		size=blk->size;
		offset=0;

		for(;offset < size;offset++)
		{
			o=buf[offset];
			buf[offset]=c;
			c=o;
		}

		buf[offset]=c;
		blk->size++;
	}
	else
	{
		txt->cursor->size++;
		buf[offset]=c;
	}

	/* move forward */
	mp_move_right(txt);

	/* mark as modified */
	mp_modified(txt);

	return(1);
}


/**
 * mp_insert_tab - Inserts a tab
 * @txt: the text
 *
 * Inserts a tab, moving forward the cursor position.
 * Returns 0 if character could not be inserted
 * (readonly text, errors, etc).
 */
int mp_insert_tab(mp_txt * txt)
{
	if(txt->type != MP_TYPE_TEXT)
		return(0);

	mp_insert_char(txt, '\t');

	return(1);
}


/**
 * mp_insert_line - Inserts a new line
 * @txt: the text
 *
 * Inserts a new line, taking account of indentations
 * and unmarking the (possible) block.
 */
int mp_insert_line(mp_txt * txt)
{
	int nspcs;
	int c;
	char tmp[1024];

	if(txt->type != MP_TYPE_TEXT)
		return(0);

	/* if not auto-indenting, insert and go */
	if(! _mp_auto_indent)
	{
		mp_insert_char(txt, '\n');

		for(c=0;c < _mp_indent;c++)
			mp_insert_char(txt, ' ');

		return(1);
	}

	c=mp_peek_char(txt);

	/* if inserting before a line with content, just insert it */
	if(c!='\n' && c!='\0')
		return(mp_insert_char(txt, '\n'));

	/* auto-indenting */

	/* count the blanks in current line */
	mp_move_bol(txt);

	for(nspcs=0;nspcs < sizeof(tmp)-1;nspcs++)
	{
		c=mp_get_char(txt);

		if(c != ' ' && c != '\t')
			break;

		tmp[nspcs]=c;
	}

	tmp[nspcs]='\0';

	/* if last char is '\n' or '\0', means that there's nothing
	   but spaces in current line; so insert the new line BEFORE them */
	if(c=='\n' || c=='\0')
	{
		if(c=='\n')
			mp_move_left(txt);

		mp_move_bol(txt);

		mp_insert_char(txt, '\n');

		mp_move_eol(txt);
	}
	else
	{
		/* if not only spaces, create the new line and add
		   the blanks counted before */
		mp_move_eol(txt);

		mp_insert_char(txt, '\n');

		mp_put_str(txt, tmp, 1);
	}

	return(1);
}


/**
 * mp_over_char - Overwrites a char.
 * @arg: the text
 * @c: the char
 *
 * Overwrites current char, moving forward the cursor position.
 * Returns 0 if character could not be written
 * (readonly text, errors, etc).
 */
int mp_over_char(mp_txt * txt, char c)
{
	int o;

	if(txt->type != MP_TYPE_TEXT)
		return(0);

	/* if a '\n' or over a '\n', insertion is needed */
	if((o=mp_peek_char(txt)) == '\n' || o == '\0' || c == '\n')
		return(mp_insert_char(txt, c));
	else
	{
		/* otherwise just substitute */
		txt->cursor->buf[txt->offset]=c;

		mp_move_right(txt);
	}

	mp_modified(txt);

	return(1);
}


/**
 * mp_put_char - Writes a char.
 * @arg: the text
 * @c: the char
 * @insert: current insert status
 *
 * Inserts or overwrites a character, depending of the
 * insert status. Also manages word wrapping,
 * tabs and newlines. Main character writing function.
 * Returns 0 if character could not be put
 * (readonly text, errors, etc).
 */
int mp_put_char(mp_txt * txt, int c, int insert)
{
	int ret;

	if(txt->type != MP_TYPE_TEXT)
		return(0);

	/* do not write '\0's */
	if(c=='\0')
		c='_';

	/* if wordwrapping... */
	if(_mp_word_wrap)
	{
		/* ... and it's on the limit... */
		if(txt->x>=_mp_word_wrap)
		{
			/* moves back until the space to destroy it */
			do
			{
				if(! mp_move_left(txt))
					break;
			}
			while(mp_peek_char(txt)!=' ');

			mp_delete_char(txt);
			mp_insert_line(txt);
			mp_move_eol(txt);
		}
	}

	if(c=='\t')
		ret=mp_insert_tab(txt);
	else
	if(c=='\n')
		ret=mp_insert_line(txt);
	else
	if(insert)
		ret=mp_insert_char(txt, c);
	else
		ret=mp_over_char(txt, c);

	return(ret);
}


/**
 * mp_put_str - Writes a string.
 * @txt: the text
 * @str: the string
 * @insert: current insert status
 *
 * Writes a string, calling mp_put_char() for each character.
 * Returns 0 if any character could not be put
 * (readonly text, errors, etc).
 */
int mp_put_str(mp_txt * txt, char * str, int insert)
{
	int n,ret=1;

	for(n=0;str[n] && (ret=mp_put_char(txt,str[n],insert));n++);

	return(ret);
}


/**
 * mp_put_strf - Writes a string with formatting
 * @txt: the text
 * @fmt: the format string
 *
 * Writes a string into @txt, using @fmt as a
 * formatting printf()-like string.
 */
int mp_put_strf(mp_txt * txt, char * fmt, ...)
{
	char buf[4096];
	va_list argptr;

	va_start(argptr, fmt);
	vsprintf(buf, fmt, argptr);
	va_end(argptr);

	return(mp_put_str(txt, buf, 1));
}


/**
 * mp_delete_char - Deletes a char.
 * @txt: the text
 *
 * Deletes the character in cursor position.
 * Returns 0 if character could not be deleted
 * (readonly text, errors, etc).
 */
int mp_delete_char(mp_txt * txt)
{
	mp_blk * blk;
	mp_blk * dblk;
	int n;

	if(txt->type != MP_TYPE_TEXT)
		return(0);

	/* you cannot delete the EOF */
	if(mp_peek_char(txt)=='\0')
		return(0);

	/* if a '\n' is being deleted, decrement line count */
	if(mp_peek_char(txt)=='\n')
	{
		if(txt->lasty)
			txt->lasty--;
	}

	if(txt->cursor->size > 1)
	{
		txt->cursor->size--;

		/* compress block by moving back */
		/* this must be optimized with memcpy */
		for(n=txt->offset;n<txt->cursor->size;n++)
			txt->cursor->buf[n]=txt->cursor->buf[n+1];

		/* move to next block if deleted char was
		   the last of the block */
		if(txt->offset==txt->cursor->size)
		{
			mp_change_cursor(txt, txt->cursor->next);
			txt->offset=0;
		}
	}
	else
	{
		/* it was the last char of the block: destroy it */

		/* save previous block */
		blk=txt->cursor->last;
		dblk=txt->cursor;

		if(txt->cursor->next==NULL)
		{
			/* inconsistency check: last block cannot have
			   a char that is not EOF */
			MP_ERROR("last block char must be EOF");
			return(0);
		}

		/* move cursor to next block */
		mp_change_cursor(txt, txt->cursor->next);
		txt->offset=0;

		txt->cursor->last=blk;

		if(blk != NULL)
			blk->next=txt->cursor;

		if(txt->first==dblk)
			txt->first=txt->cursor;

		mp_free(dblk);
	}

	mp_modified(txt);

	return(1);
}


/**
 * mp_delete_line - Deletes current line.
 * @txt: the text
 *
 * Deletes current line.
 * Returns 0 if the line could not be deleted
 * (readonly text, errors, etc).
 */
int mp_delete_line(mp_txt * txt)
{
	char c;

	if(txt->type != MP_TYPE_TEXT)
		return(0);

	mp_move_bol(txt);

	/* deletes until '\n' inclusive */
	for(;;)
	{
		c=mp_peek_char(txt);

		if(! mp_delete_char(txt))
			break;

		if(c=='\n')
			break;
	}

	return(1);
}


/**
 * mp_log - Logs to Minimum Profit's internal log
 * @fmt: printf() like format string
 * @...: variable list of arguments
 *
 * Writes a formatted string to the internal log txt.
 */
void mp_log(char * fmt, ...)
{
	char buf[4096];
	va_list argptr;

	/* create log txt if it doesn't exist */
	if(_mp_log == NULL)
		_mp_log=mp_create_sys_txt("<log>");

	_mp_log->type=MP_TYPE_TEXT;

	mp_move_eof(_mp_log);
	mp_move_eol(_mp_log);

	va_start(argptr, fmt);
	vsprintf(buf, fmt, argptr);
	va_end(argptr);

	mp_put_str(_mp_log, buf, 1);

	_mp_log->type=MP_TYPE_READ_ONLY;
	_mp_log->mod=0;
}

static int _mp_xstrcmp(const void *a,const void *b) { 
  return strcmp(*((char **)a),*((char **)b));
}
static int _mp_xstrcase(const void *a,const void *b) { 
  return strcasecmp(*((char **)a),*((char **)b));
}

/**
 * mp_sort - Sorts a txt buffer
 * @txt: text to sort
 *
 * Sorts a buffer.  If the mark is defined it will sort only the
 * contents of the txt inside the mark
 */
void mp_sort(mp_txt *txt)
{
  int start = 0;
  int end = txt->lasty;
  char **ptrs;
  int i,j;
  
  if (mp_marked(txt)) {
    start = txt->mby;
    end =txt->mey;
  }
  /* Allocate temp buffers... */
  ptrs = (char **)malloc(sizeof(char *)*(end-start+1));
  if (!ptrs) return;
  for (j=0,i=start; i <= end ; i++,j++) {
    int l;
    mp_move_xy(txt,0,i);
    mp_move_eol(txt);
    ptrs[j] = (char *)malloc(l = txt->x+1);
    if (!ptrs[j]) {
      for (l=0;l < j; l++) free(ptrs[l]);
      free(ptrs);
      return;
    }
    mp_move_bol(txt);
    mp_get_str(txt,ptrs[j],l,'\n');
  }
  qsort(ptrs,end-start+1,sizeof(char *), _mp_case_cmp ? _mp_xstrcmp :_mp_xstrcase);

  for (j=0,i=start; i <= end; i++,j++) {
    mp_move_xy(txt,0,i);
    mp_delete_line(txt);
    mp_put_str(txt,ptrs[j],1);
    mp_put_char(txt,'\n',1);
    free(ptrs[j]);
  }
  free(ptrs);
}

/* block, cut and paste routines */

/**
 * mp_unmark - Unmarks the selection block.
 * @txt: the text
 *
 * Unmarks the selection block.
 */
void mp_unmark(mp_txt * txt)
{
	if(txt->mbx!=-1)
		txt->mbx=txt->mby=txt->mex=txt->mey=-1;
}


/**
 * mp_marked - Tests if selection block is marked.
 * @txt: the text
 *
 * Returns 1 if a selection block is marked.
 */
int mp_marked(mp_txt * txt)
{
	if(txt->mbx==-1 || txt->mex==-1)
		return(0);

	return(1);
}


/**
 * mp_mark - Marks the beginning or end of the selection block.
 * @txt: the text
 *
 * Marks the current cursor position as the beginning or the end
 * of the selection block. Both boundaries can be reselected.
 */
void mp_mark(mp_txt * txt)
{
	int before;

	if(!mp_marked(txt))
	{
		txt->mbx=txt->mex=txt->x;
		txt->mby=txt->mey=txt->y;

		return;
	}

	if(txt->y < txt->mby)
		before=1;
	else
	if(txt->y > txt->mey)
		before=0;
	else
	{
		if(txt->x < txt->mbx)
			before=1;
		else
			before=0;
	}

	if(before)
	{
		txt->mbx=txt->x;
		txt->mby=txt->y;
	}
	else
	{
		txt->mex=txt->x;
		txt->mey=txt->y;
	}
}


void mp_lock_clipboard(int lock)
{
	if(lock)
	{
		/* make clipboard writable */
		_mp_clipboard->type=MP_TYPE_TEXT;

		/* deletes previous clipboard */
		mp_empty_txt(_mp_clipboard);
	}
	else
	{
		/* clipboard is back to read-only */
		_mp_clipboard->type=MP_TYPE_READ_ONLY;
		_mp_clipboard->mod=0;
	}
}


/**
 * mp_copy_mark - Copies the selection block into the clipboard
 * @txt: the text
 *
 * Copies the selection block into the internal clipboard.
 * Returns 0 if the selection block is not marked.
 */
int mp_copy_mark(mp_txt * txt)
{
	int x,y;
	int c;
	mp_txt * ctxt;

	if(! mp_marked(txt))
		return(0);

	mp_lock_clipboard(1);

	ctxt=mp_get_tmp_txt(txt);

	/* move to beginning of block */
	mp_move_xy(ctxt, txt->mbx, txt->mby);

	/* read until the end */
	for(x=txt->mex,y=txt->mey;ctxt->y<=y;)
	{
		c=mp_get_char(ctxt);

		if(c=='\0')
		{
			MP_ERROR("unexpected EOF");
			break;
		}

		mp_insert_char(_mp_clipboard, c);

		if(ctxt->y==y && ctxt->x==x)
			break;
	}

	mp_end_tmp_txt();

	mp_lock_clipboard(0);

	return(1);
}


/**
 * mp_paste_mark - Pastes the clipboard into cursor position.
 * @txt: the text
 *
 * Pastes the clipboard into cursor position.
 * Returns 0 if text is read-only or if there is no
 * clipboard to paste.
 */
int mp_paste_mark(mp_txt * txt)
{
	int c;

	if(txt->type != MP_TYPE_TEXT)
		return(0);

	if(_mp_clipboard==NULL)
		return(0);

	mp_move_bof(_mp_clipboard);

	while((c=mp_get_char(_mp_clipboard)))
		mp_put_char(txt, c, 1);

	return(1);
}


/**
 * mp_delete_mark - Deletes the marked selection block.
 * @txt: the text
 *
 * Deletes the marked selection block. It is done
 * by deleting char by char and this is slow; an
 * optimized version (releasing entire blocks) should
 * be written.
 * Returns 0 if text is read-only or there is no
 * marked selection block.
 */
int mp_delete_mark(mp_txt * txt)
{
	long l;

	if(txt->type != MP_TYPE_TEXT)
		return(0);

	if(! mp_marked(txt))
		return(0);

	mp_move_xy(txt, txt->mex, txt->mey);

	for(l=0;txt->y!=txt->mby ||
	    (txt->y==txt->mby && txt->x>txt->mbx);l++)
	{
		if(! mp_move_left(txt))
		{
			MP_ERROR("Unexpected EOF");
			return(0);
		}
	}

	for(;l;l--)
	{
		if(! mp_delete_char(txt))
		{
			MP_ERROR("Unexpected EOF");
			return(0);
		}
	}

	return(1);
}


/**
 * mp_reformat_paragraph - Reformats a paragraph using word-wrapping
 * @txt: the text
 *
 * Cuts current paragraph (i.e. a block of text delimited by two blank
 * lines), and reformats it using word-wrapping. If word-wrapping is not
 * active, nothing is done.
 */
void mp_reformat_paragraph(mp_txt * txt)
{
	int c;

	/* if not word-wrapping, do nothing */
	if(!_mp_word_wrap)
		return;

	/* unmark */
	mp_unmark(txt);
	mp_move_bol(txt);

	/* move until an empty line or bof */
	while(mp_move_up(txt))
	{
		if(mp_peek_char(txt) == '\n')
		{
			mp_move_down(txt);
			break;
		}
	}

	/* mark start of block to be cut */
	mp_mark(txt);

	/* move down until an empty line of eof */
	while(mp_move_down(txt))
	{
		if(mp_peek_char(txt) == '\n')
		{
			mp_move_left(txt);
			break;
		}
	}

	/* mark end of block to be cut */
	mp_mark(txt);

	/* cut block */
	mp_copy_mark(txt);
	mp_delete_mark(txt);

	/* replace from the clipboard all \n to ' ' */
	_mp_clipboard->type=MP_TYPE_TEXT;
	mp_move_bof(_mp_clipboard);

	while((c=mp_peek_char(_mp_clipboard)) != '\0')
	{
		if(c == '\n')
		{
			mp_delete_char(_mp_clipboard);
			mp_insert_char(_mp_clipboard, ' ');
		}

		mp_move_right(_mp_clipboard);
	}

	/* unlocks the clipboard */
	mp_lock_clipboard(0);

	/* paste now, letting word-wrapping format everything */
	mp_paste_mark(txt);
}


/* seek and replace functions */

/**
 * mp_seek_plain - Seeks the string as plain text.
 * @txt: the text
 * @str: the string
 *
 * Seeks the string into text. If found, the cursor is moved there.
 * The string must be found as-is (i.e., it does not contain any
 * special characters). Returns 1 if the string is found.
 */
int mp_seek_plain(mp_txt * txt, char * str)
{
	int c,t,n,l;
	int found;
	int x,y,hbx,hby;

	x=txt->x;
	y=txt->y;
	n=found=0;
	hbx=hby=-1;

	l=strlen(str);

	c=mp_get_char(txt);

	while(c != '\0')
	{
		if(_mp_case_cmp)
			t=(c == str[n]);
		else
			t=(toupper(c) == toupper(str[n]));

		if(t)
		{
			if(n==0)
			{
				hbx=txt->x - 1;
				hby=txt->y;
			}

			if(++n == l)
			{
				found=1;
				break;
			}

			c=mp_get_char(txt);
		}
		else
		{
			if(n)
				n=0;
			else
				c=mp_get_char(txt);
		}
	}

	if(!found)
		mp_move_xy(txt,x,y);
	else
	{
		/* store search hit */
		txt->hbx=hbx; txt->hby=hby;
		txt->hex=txt->x; txt->hey=txt->y;
	}

	return(found);
}


/**
 * mp_seek_regex - Seeks the string as a regular expression.
 * @txt: the text
 * @str: the string
 *
 * Seeks the string into text. If found, the cursor is moved there.
 * The string must be a GNU regular expression.
 * Returns 1 if the string is found.
 */
int mp_seek_regex(mp_txt * txt, char * str)
{
	int ret=0;

#ifndef CONFOPT_NO_REGEX

	int err;
	regex_t r;
	regmatch_t m;
	char line[4096];
	int x, y, t;

	/* compiles the expression */
	if((err=regcomp(&r, str, REG_EXTENDED|
		(_mp_case_cmp ? 0: REG_ICASE))))
	{
		regerror(err, &r, line, sizeof(line));

		mp_log("regex error: %s\n", line);
		return(0);
	}

	x=txt->x; y=txt->y;

	mp_move_right(txt);

	while(mp_peek_char(txt) != '\0')
	{
		t=txt->x;

		/* reads a line */
		mp_get_str(txt, line, sizeof(line), '\n');

		/* executes the regex */
		if(regexec(&r, line, 1, &m, (t > 0 ? REG_NOTBOL : 0))==0)
		{
			/* found! */
			ret=1;

			/* store search hit */
			x=txt->hbx=m.rm_so + t;
			txt->hex=m.rm_eo + t;
			y=txt->hby=txt->hey=txt->y - 1;

			break;
		}
	}

	regfree(&r);

	/* final (or initial if not found) position */
	mp_move_xy(txt, x, y);

#endif /* CONFOPT_NO_REGEX */

	return(ret);
}


/**
 * mp_seek - Seeks the string.
 * @txt: the text
 * @str: the string
 *
 * Seeks the string into text. If found, the cursor is moved there.
 * Returns 1 if the string is found.
 */
int mp_seek(mp_txt * txt, char * str)
{
#ifndef CONFOPT_NO_REGEX
	if(_mp_regex)
		return(mp_seek_regex(txt, str));
#endif

	return(mp_seek_plain(txt, str));
}


/**
 * mp_mark_match - Marks (selects) the last successful match
 * @txt: the text
 *
 * Marks (as a block) the last successful match.
 */
int mp_mark_match(mp_txt * txt)
{
	if(txt->hbx==-1) return(0);

	mp_unmark(txt);
	mp_move_xy(txt, txt->hbx, txt->hby);
	mp_mark(txt);
	mp_move_xy(txt, txt->hex, txt->hey);
	mp_mark(txt);

	return(1);
}


/**
 * mp_replace - Seeks and replaces a string.
 * @txt: the text
 * @src: the string to be searched.
 * @des: the string to replace src.
 *
 * Seeks a string and, if it is found, replaces it with another.
 * Returns 0 if the text is read-only or the string is not found.
 */
int mp_replace(mp_txt * txt, char * src, char * des)
{
	if(txt->type != MP_TYPE_TEXT)
		return(0);

	if(! mp_seek(txt, src))
		return(0);

	mp_mark_match(txt);
	mp_delete_mark(txt);
	mp_put_str(txt,des,1);

	return(1);
}


/* password protecting and encryption */

/**
 * mp_set_password - Assigns a password to a text
 * @txt: the text
 * @passwd: the password
 *
 * Assigns a password to a text. Next time the text is loaded or saved,
 * the password will be used to en/decrypt it.
 */
void mp_set_password(mp_txt * txt, char * passwd)
{
	/* don't set passwords to system texts */
	if(txt->sys)
		return;

	if(txt->passwd != NULL)
		free(txt->passwd);

	txt->passwd=(unsigned char *) strdup(passwd);
}


static int _arcfour_byte(void)
/* returns next ARCFOUR byte */
{
	int i, j, t;
	unsigned char * S;

	i=_mp_arcfour_i; j=_mp_arcfour_j; S=_mp_arcfour_S;

	i=(i + 1) & 0xff;
	j=(j + S[i]) & 0xff;

	_mp_arcfour_i=i; _mp_arcfour_j=j;

	t=S[i]; S[i]=S[j]; S[j]=t;

	return(S[(S[i] + S[j]) & 0xff]);
}


static void _arcfour_init(unsigned char * key)
/* starts an ARCFOUR encryption machine */
{
	int i, j, t;
	int k;

	k=strlen((char *)key);

	/* initial values */
	for(i=0;i < 256;i++)
		_mp_arcfour_S[i]=i;

	_mp_arcfour_i=_mp_arcfour_j=0;

	/* scramble */
	for(i=j=0;i < 256;i++)
	{
		t=_mp_arcfour_S[i];

		j=(j + t + key[i % k]) & 0xff;

		_mp_arcfour_S[i]=_mp_arcfour_S[j];
		_mp_arcfour_S[j]=t;
	}

	/* discard 256 bytes (as recommended in many sources) */
	for(i=0;i < 256;i++)
		_arcfour_byte();
}


static int _encrypt_char(int c)
{
	c ^= _arcfour_byte();

	return(c);
}


static int _decrypt_char(int c)
{
	c ^= _arcfour_byte();

	return(c);
}


/* file operations */

/**
 * mp_get_encryption_format - Tests if file is encrypted
 * @f: the file stream
 *
 * Returns the encryption format of the open file @f. 0 means an unencrypted
 * one (ordinary text file). In any case, the file pointer is left to the
 * start of the actual file data (i.e., after any possible signatures).
 * By now, only types 0 (unencrypted) or 1 (ARCFOUR algorithm encryption)
 * are implemented.
 */
int mp_get_encryption_format(FILE * f)
{
	char tmp[sizeof(_mp_crypt1_sig)];

	/* if seeking to current position fails, f is
	   probably stdin, so fail */
	if(fseek(f, 0, SEEK_CUR) == -1)
		return(0);

	if(fread(tmp, 1, sizeof(tmp), f) == sizeof(tmp))
	{
		/* test signature */
		if(strncmp(tmp, _mp_crypt1_sig, sizeof(tmp)) == 0)
			return(1);
	}

	/* no; rewind and start from the beginning */
	fseek(f, 0, SEEK_SET);

	return(0);
}


/**
 * mp_load_file - Loads a file into the text.
 * @txt: the text
 * @f: the file
 *
 * Loads a file into the text.
 * Returns 0 if some character could not be put
 * (readonly text, errors, etc).
 */
int mp_load_file(mp_txt * txt, FILE * f)
{
	char tmp[30];
	int c;
	int ret;
	long bytes;

	ret=0;
	bytes=0;

	/* if txt has a password, init the encryption machine */
	if(txt->passwd != NULL)
		_arcfour_init(txt->passwd);

	MP_SAVE_STATE();

	while((c=fgetc(f)) != EOF)
	{
		/* decrypt if needed */
		if(txt->passwd != NULL)
			c=_decrypt_char(c);

		if(c=='\r' || c=='\x1a')
			continue;

		/* special case: '\b' deletes previous char */
		if(c=='\b')
		{
			mp_move_left(txt);
			mp_delete_char(txt);
		}
		else
		if(! mp_put_char(txt, c, 1))
		{
			ret=-2;
			break;
		}

		bytes++;

		if((bytes % 50000) == 0)
		{
			sprintf(tmp,"%ld bytes",bytes);
			_mp_notify(tmp);
		}
	}

	/* try to move forward the end of file, to force
	   a line number recalculation */
	mp_move_right(txt);

	sprintf(tmp,"%ld bytes",bytes);
	_mp_notify(tmp);
	mp_log("mp_load_file: %d bytes\n",bytes);

	mp_move_bof(txt);

	MP_RESTORE_STATE();

	return(ret);
}


/**
 * mp_save_file - Writes the text into file
 * @txt: the text
 * @f: the file
 *
 * Writes the text into the file, previously opened
 * for writing.
 * Returns always 1 (no error checking is done).
 */
int mp_save_file(mp_txt * txt, FILE * f)
{
	char tmp[30];
	int c, i, n;
	mp_txt * ctxt;
	long bytes;

	/* if txt has a password, init the encryption machine */
	if(txt->passwd != NULL)
	{
		_arcfour_init(txt->passwd);

		/* write the signature */
		fwrite(_mp_crypt1_sig, 1, sizeof(_mp_crypt1_sig), f);
	}

	ctxt=mp_get_tmp_txt(txt);

	mp_move_bof(ctxt);

	bytes=0;

	for(;;)
	{
		if((c=mp_get_char(ctxt)) == '\0')
			break;

		i=0;

		if(c == '\n' && _mp_cr_lf)
			tmp[i++]='\r';

		tmp[i++]=c;

		/* FIXME: _mp_save_tabs should be treated here;
		   by now, disabling _mp_save_tabs has no effect
		   (bug #1030) */

		/* transfer */
		for(n=0;n < i;n++)
		{
			c=tmp[n];

			/* encrypt if needed */
			if(txt->passwd != NULL)
				c=_encrypt_char(c);

			fputc(c,f);

			bytes++;
		}

		if((bytes % 50000) == 0)
		{
			sprintf(tmp,"%ld bytes",bytes);
			_mp_notify(tmp);
		}
	}

	sprintf(tmp,"%ld bytes",bytes);
	_mp_notify(tmp);
	mp_log("mp_save_file: %d bytes\n",bytes);

	mp_end_tmp_txt();

	return(1);
}


/**
 * mp_startup - Starts up the mp core.
 *
 * Starts up the mp core. By now, it does nothing.
 * Returns 1 is succesful.
 */
int mp_startup(void)
{
	mp_log("Minimum Profit version " VERSION " (" __DATE__ " " __TIME__ ")\n");
	mp_log("Block size: %d\n",BLK_SIZE);

	_mp_clipboard=mp_create_sys_txt("<clipboard>");

	return(1);
}


/**
 * mp_shutdown - Shuts down the mp core.
 *
 * Shuts down the mp core. By now, it does nothing.
 */
void mp_shutdown(void)
{
}