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
|
/*
mp_iface.c
Interface.
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 <time.h>
#include "mp_core.h"
#include "mp_conf.h"
#include "mp_video.h"
#include "mp_synhi.h"
#include "mp_lang.h"
#include "mp_func.h"
#include "mp_iface.h"
#include "mp_tags.h"
#include "mp_wordp.h"
/********************
Data
********************/
/* word buffer */
static char _draw_word[128];
/* insert flag */
int _mpi_insert=1;
/* text to search */
static char _mpi_search_text[4096]="";
char * MP_LICENSE=
"\nMinimum Profit " VERSION " - Programmer Text Editor\n\n\
Copyright (C) 1991-2005 Angel Ortega <angel@triptico.com>\n\
\n\
This program is free software; you can redistribute it and/or\n\
modify it under the terms of the GNU General Public License\n\
as published by the Free Software Foundation; either version 2\n\
of the License, or (at your option) any later version.\n\
\n\
This program is distributed in the hope that it will be useful,\n\
but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
See the GNU General Public License for more details.\n\
\n\
You should have received a copy of the GNU General Public License\n\
along with this program; if not, write to the Free Software Foundation,\n\
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\
\n\
Home page: http://www.triptico.com/software/mp.html\n\
Mailing list: mp-subscribe@lists.triptico.com\n";
/* macro */
#ifndef MAX_MACRO_SIZE
#define MAX_MACRO_SIZE 1024
#endif
struct
{
int key;
char * funcname;
} _mpi_macro[MAX_MACRO_SIZE];
int _mpi_macro_index=0;
int _mpi_macro_recording=0;
/* instant position (mouse click or so) */
int _mpi_instant_x=-1;
int _mpi_instant_y=-1;
/* mark column 80 */
int _mpi_mark_column_80=0;
/* exit requested */
int _mpi_exit_requested=0;
/* the template file */
char _mpi_template_file[1024]="~/.mp_templates";
/* move selecting flag */
int mpi_move_selecting=0;
/* experimental ftt code */
static char * _mpi_ftt_key="\x20\x5f\x2f\x7c\x5c\x28\x29\x6f\x2d";
static char * _mpi_ftt_challenge=
"\xDB\xC6\xE5\xE1\xF2\xA0\xF4\xE8\xE5\xA0\xD4\xF2\xE9\xE3\xE5\xF2"
"\xE1\xF4\xEF\xF0\xF3\xDD";
static unsigned char * _mpi_ftt_data=(unsigned char *)
"\x00\x00\x11\x11\x11\x11\x00\x00\x00\x12\x00\x00\x00\x00\x41\x00"
"\x02\x00\x34\x00\x00\x23\x00\x40\x30\x00\x30\x40\x02\x03\x00\x03"
"\x30\x00\x57\x00\x00\x76\x00\x03\x04\x10\x04\x02\x40\x20\x01\x20"
"\x03\x04\x10\x20\x04\x01\x20\x30\x03\x00\x33\x08\x80\x33\x00\x30"
"\x03\x00\x30\x41\x12\x03\x00\x30\x56\x56\x56\x00\x00\x56\x56\x56\xFF";
/* number of lines to move up over current visible line,
to take into account possible start of comments many lines above */
int _mpi_preread_lines=60;
/* monochrome mode */
int mpi_monochrome=0;
/* readline history */
mp_txt * _mpi_history[MPR_LAST + 1];
/* tag target over the cursor */
static char * _mpi_tag_target=NULL;
/* the status line format string */
char _mpi_status_line_f[128]="%m %x,%y [%l] %R%O %s %t";
/* the strftime format string */
char _mpi_strftime_f[128]="%x";
/* break hardlinks flag */
int _mpi_break_hardlinks=0;
/* user-set language */
char _mpi_lang[128]="";
/* move to line on open */
int _mpi_move_to_line=0;
/*******************
Code
*******************/
static char * _mpi_format_status_line(mp_txt * txt)
{
static char str[1024];
char tmp[128];
char * ptr;
int n, m;
ptr=_mpi_status_line_f;
for(n=0;*ptr != '\0' && n < sizeof(str) - 1;ptr++)
{
/* if it's not a format mark, continue */
if(*ptr != '%')
{
str[n++]=*ptr;
continue;
}
ptr++;
switch(*ptr)
{
case 'm':
/* %m: modified flag */
tmp[0]=txt->mod ? '*': '\0';
tmp[1]='\0';
break;
case 'x':
/* %x: X coord */
sprintf(tmp, "%d", txt->x + 1);
break;
case 'y':
/* %y: Y coord */
sprintf(tmp, "%d", txt->y + 1);
break;
case 'l':
/* %l: number of lines */
sprintf(tmp, "%d", txt->lasty + 1);
break;
case 'R':
/* %R: 'recording mode' flag */
tmp[0]=_mpi_macro_recording ? 'R': '\0';
tmp[1]='\0';
break;
case 'O':
/* %O: 'overwrite' text flag */
tmp[0]=_mpi_insert ? '\0' : 'O';
tmp[1]='\0';
break;
case 's':
/* %s: Synhi description */
if(txt->synhi)
strncpy(tmp, _mps_synhi[txt->synhi - 1].type, sizeof(tmp));
else
tmp[0]='\0';
break;
case 'd':
/* %d: date */
{
time_t t;
t=time(NULL);
strftime(tmp, sizeof(tmp) - 1,
_mpi_strftime_f,
localtime(&t));
}
break;
case 't':
/* %t: tag */
if(_mpi_tag_target != NULL)
strncpy(tmp, _mpi_tag_target, sizeof(tmp));
else
tmp[0]='\0';
break;
default:
tmp[0]=*ptr;
tmp[1]='\0';
break;
}
/* transfer */
for(m=0;tmp[m] != '\0' && n < sizeof(str) - 1;m++,n++)
str[n]=tmp[m];
}
str[n]='\0';
return(str);
}
/**
* mpi_color_draw_all - Draws the document window using colors.
* @txt: the text
*
* Draws the document window, with syntax highlighting decorations and
* other colors, if applicable.
*/
void mpi_color_draw_all(mp_txt * txt)
{
int vx,vy;
mp_txt * wrk;
int n,m,r;
int c,wc,color,rcolor;
int wi;
int spcs;
int ry;
int xcursor=0,ycursor=0;
mpv_title(NULL);
mp_adjust(txt,_mpv_x_size,_mpv_y_size - 1);
mp_match_bracket(txt, _mpv_x_size * _mpv_y_size);
vx=txt->vx;
vy=txt->vy;
wrk=mp_get_tmp_txt(txt);
mp_move_bol(wrk);
/* moves up to first visible line */
while(wrk->y > (vy - _mpi_preread_lines))
{
if(! mp_move_up(wrk))
break;
}
_in_comment=-1;
_mpi_tag_target=NULL;
/* line loop */
for(;;)
{
/* column loop */
ry=wrk->y;
/* move this outside the loop to allow
multiline quoted strings (you shouldn't) */
_draw_quoting='\0';
/* end if below the last line */
if(ry >= (vy + _mpv_y_size))
break;
c=mp_get_char(wrk);
/* move drawing cursor to beginning of line */
if(ry >= vy)
mpv_goto(0,ry - vy);
for(m=r=0;;)
{
/* word loop */
wi=0;
spcs=mps_is_sep(c,txt->synhi);
while(wi<sizeof(_draw_word)-1)
{
if(c=='\n' || c=='\0')
break;
_draw_word[wi++]=c;
c=mp_get_char(wrk);
if(spcs != mps_is_sep(c,txt->synhi))
{
if(_draw_word[wi - 1]!='\\')
break;
}
}
_draw_word[wi]='\0';
rcolor=mps_word_color(txt->synhi,_draw_word, m, ry);
if(rcolor == MP_COLOR_NORMAL)
rcolor=mpw_spellcheck_word(_draw_word);
if(c=='\n' || c=='\0')
{
_draw_word[wi++]=' ';
_draw_word[wi]='\0';
}
/* draws the visible chars of the word */
for(wi=0;(wc=_draw_word[wi])!='\0';wi++,m++)
{
color=mps_quoting(wc,rcolor,txt->synhi);
/* if inside selection block... */
if(ry > txt->mby && ry <= txt->mey)
color=MP_COLOR_SELECTED;
if(ry == txt->mby && m >= txt->mbx)
color=MP_COLOR_SELECTED;
if(ry == txt->mey && m >= txt->mex)
color=rcolor;
/* if over the cursor... */
if(m == txt->x && ry == txt->y)
{
if(rcolor == MP_COLOR_LOCAL)
{
/* gets current tag target */
_mpi_tag_target=
_mps_last_tag_target;
}
xcursor=m-vx;
ycursor=ry-vy;
color=MP_COLOR_CURSOR;
}
/* if over the matching bracket... */
if(m == txt->brx && ry == txt->bry)
color=MP_COLOR_BRACKET;
if(_mpi_mark_column_80 && r==80)
color=MP_COLOR_SELECTED;
if(txt->type == MP_TYPE_LIST && ry == txt->y)
color=MP_COLOR_CURSOR;
/* finally draws */
if(wc=='\t')
{
int i;
for(i=MP_REAL_TAB_SIZE(r);i > 0;i--,r++)
{
if(r >= vx && r < (vx+_mpv_x_size))
mpv_char(' ', color);
if(txt->type != MP_TYPE_LIST &&
color != MP_COLOR_SELECTED)
color=MP_COLOR_NORMAL;
}
}
else
{
if(r >= vx && r < (vx+_mpv_x_size))
mpv_char(wc,color);
r++;
}
}
if(c=='\n' || c=='\0')
break;
}
/* spaces to end of line */
if(r < vx)
r=vx;
for(;r < vx+_mpv_x_size;r++)
{
if(_mpi_mark_column_80 && r==80)
mpv_char(' ',MP_COLOR_SELECTED);
else
if(txt->type == MP_TYPE_LIST &&
ry == txt->y)
mpv_char(' ',MP_COLOR_CURSOR);
else
mpv_char(' ',MP_COLOR_NORMAL);
}
/* if last read char is '\0', it's the end */
if(c=='\0')
break;
/* if we are not at the end of the line, move there */
if(c!='\n')
{
mp_move_bol(wrk);
if(! mp_move_down(wrk))
break;
}
}
/* the rest of lines are drawn as blanks */
for(n=ry - vy;n <= _mpv_y_size;n++)
{
mpv_goto(0,n+1);
for(m=0;m < _mpv_x_size;m++)
{
if(_mpi_mark_column_80 && m==80)
mpv_char(' ',MP_COLOR_SELECTED);
else
mpv_char(' ',MP_COLOR_NORMAL);
}
}
mp_end_tmp_txt();
/* scrollbar */
mpv_scrollbar(txt->vy+1,_mpv_y_size,txt->lasty+1);
/* status line */
mpv_status_line(_mpi_format_status_line(txt));
mpv_goto(0,_mpv_y_size);
mpv_cursor(xcursor,ycursor);
mpv_refresh();
}
/**
* mpi_draw_all - Draws the document window.
* @txt: the text
*
* Draws the document window.
*/
void mpi_draw_all(mp_txt * txt)
{
if(txt != NULL)
mpi_color_draw_all(txt);
}
/**
* mpi_move_wheel_up - Moves preferred number of rows up
* @txt: the text
*
* Moves preferred number of rows up.
* Moves a full page if preferred is 0 or greater than a page of rows.
*/
int mpi_move_wheel_up(mp_txt * txt)
{
int n;
if ((_mp_wheel_scroll_rows) > 0 && (_mp_wheel_scroll_rows < _mpv_y_size)) {
for (n = 0; n < _mp_wheel_scroll_rows; n++)
mp_move_up(txt);
} else
mpi_move_page_up(txt);
return(1);
}
/**
* mpi_move_page_up - Moves one page up
* @txt: the text
*
* Moves one page up.
*/
int mpi_move_page_up(mp_txt * txt)
{
int n;
for(n=0;n < _mpv_y_size;n++)
mp_move_up(txt);
return(1);
}
/**
* mpi_move_wheel_down - Moves preferred number of rows down
* @txt: the text
*
* Moves preferred number of rows down.
* Moves a full page if preferred is 0 or greater than a page of rows.
*/
int mpi_move_wheel_down(mp_txt * txt)
{
int n;
if ((_mp_wheel_scroll_rows) > 0 && (_mp_wheel_scroll_rows < _mpv_y_size)) {
for (n = 0; n < _mp_wheel_scroll_rows; n++)
mp_move_down(txt);
} else
mpi_move_page_down(txt);
return(1);
}
/**
* mpi_move_page_down - Moves one page down
* @txt: the text
*
* Moves one page down.
*/
int mpi_move_page_down(mp_txt * txt)
{
int n;
for(n=0;n < _mpv_y_size;n++)
mp_move_down(txt);
return(1);
}
/**
* mpi_goto - Moves to a line by its number
* @txt: the txt
*
* Asks for a line number and moves the cursor there.
*/
int mpi_goto(mp_txt * txt)
{
char * ptr;
int n;
if((ptr=mpv_readline(MPR_GOTO,L("Line to go to: "),NULL))!=NULL)
{
n=atoi(ptr);
mp_move_xy(txt,0,n-1);
}
return(1);
}
/**
* mpi_new - Creates a new text
*
* Creates an empty unnamed text, unless an unnamed text
* already exists; in that case, it's made the current one.
*/
int mpi_new(void)
{
mp_txt * txt;
if((txt=mp_find_txt(L("<unnamed>")))!=NULL)
_mp_active=txt;
else
{
mp_create_txt(L("<unnamed>"));
mps_auto_synhi(_mp_active);
}
return(2);
}
/**
* mpi_open - Loads a file.
* @name: the file name.
* @reopen: if reopening is allowed.
*
* Opens a file and sets it as the current one, or
* otherwise communicates the error to the user.
*/
void mpi_open(char * name, int reopen)
{
mp_txt * txt;
FILE * f;
/* if no name, ask for it */
if(name == NULL &&
(name=mpv_readline(MPR_OPEN,L("Enter file name: "),NULL)) == NULL)
return;
/* if file is already open and don't want to reopen, just select it */
if(!reopen && (txt=mp_find_txt(name)) != NULL)
{
_mp_active=txt;
return;
}
mp_create_txt(name);
if((f=mpv_fopen(name, "r")) != NULL)
{
/* if file is encrypted... */
if(mp_get_encryption_format(f) > 0)
{
char * passwd;
/* ...ask for a password... */
if((passwd=mpv_readline(MPR_PASSWORD,
L("Password:"), NULL)) == NULL ||
passwd[0] == '\0')
{
/* cancelled: close everything */
fclose(f);
mp_delete_txt(_mp_active);
return;
}
/* ...and set it */
mp_set_password(_mp_active, passwd);
}
mp_load_file(_mp_active,f);
fclose(f);
_mp_active->mod=0;
mps_auto_synhi(_mp_active);
}
else
{
#ifdef CLASSIC_BEHAVIOUR
mpv_alert(L("File '%s' not found."), name);
mp_delete_txt(_mp_active);
#else
mp_log("Can't open '%s'... creating new.\n", name);
#endif
}
}
/**
* mpi_save_as - Asks for a name for the text and save.
* @txt: the text
*
* Asks for a name for the text and saves it. If the
* text already has a name, it is replaced.
* On error, communicates it to the user.
*/
int mpi_save_as(mp_txt * txt)
{
char * name;
if(txt->type == MP_TYPE_READ_ONLY) return(0);
if((name=mpv_readline(MPR_SAVE,L("Enter file name: "),
*txt->name == '<' ? NULL : txt->name)) == NULL)
return(0);
mp_name_txt(txt,name);
mps_auto_synhi(txt);
return(mpi_save(txt));
}
/**
* mpi_save - Saves the text.
* @txt: the text
*
* Saves the text. If it already has a name, saves directly
* to disk, otherwise asks for one using mpi_save_as().
*/
int mpi_save(mp_txt * txt)
{
FILE * f;
if(txt->type == MP_TYPE_READ_ONLY) return(0);
if(txt->name[0] == '<')
return(mpi_save_as(txt));
mps_auto_synhi(txt);
if((f=mpv_fopen(txt->name, "w")) != NULL)
{
mp_save_file(txt, f);
fclose(f);
txt->mod=0;
}
else
{
mpv_alert(L("Can't create file '%s'."), txt->name);
mp_name_txt(txt,L("<unnamed>"));
}
return(2);
}
/**
* mpi_sync - Synchronizes the modified texts to disk
*
* Synchonizes the modified texts saving its contents to disk.
*/
void mpi_sync(void)
{
mp_txt * txt;
for(txt=_mp_txts;txt!=NULL;txt=txt->next)
{
if(txt->mod)
{
mpv_status_line(txt->name);
mpi_save(txt);
}
}
}
/**
* mpi_close - Closes the text.
* @txt: the text
*
* Closes the text. If it has changed, asks user if
* file must be saved; if ok, file is saved using mpi_save().
*/
int mpi_close(mp_txt * txt)
{
if(txt->mod)
{
if(mpv_confirm(L("File has changed. Save changes?")))
mpi_save(txt);
}
mp_delete_txt(txt);
return(2);
}
int mpi_history_size(int mode)
{
return(_mpi_history[mode]->lasty);
}
int mpi_history_get(int mode, int index, char * buf, int size)
{
if(index < 0 || index > _mpi_history[mode]->lasty)
return(0);
mp_move_xy(_mpi_history[mode], 0, index);
mp_get_str(_mpi_history[mode], buf, size, '\n');
return(1);
}
void mpi_history_add(int mode, char * str)
{
/* don't add empty strings */
if(*str=='\0') return;
/* never add history to passwords */
if(mode == MPR_PASSWORD) return;
mp_move_eof(_mpi_history[mode]);
mp_move_eol(_mpi_history[mode]);
mp_put_char(_mpi_history[mode], '\n', 1);
mp_put_str(_mpi_history[mode], str, 1);
}
/**
* mpi_exec - Executes a command related to text.
* @txt: the text
* @cmd: the command to execute
*
* Asks for a system command to be executed if none was specified
* in @cmd.
* If the command is preceded by the | (pipe) char, the complete text
* is sent as the command's standard input;
* If the command is preceded by the < char, the standard output is
* displayed in a new read-only buffer. If the character following <
* is another <, then the text between a < > will be used as the buffer
* title.
* If the command is preceded by the @ char, the command is executed
* as a spawned. i.e. the command gets full control of the screen.
* Otherwise, the standard output of the command is written into
* cursor position.
*/
int mpi_exec(mp_txt * txt, char *cmd)
{
int res = -1;
int ret = 0;
/* if no cmd, ask for one */
if (cmd == NULL)
cmd=mpv_readline(MPR_EXEC, L("System command: "), NULL);
/* cancel or empty; return now */
if(cmd == NULL || *cmd == '\0')
return(0);
switch (*cmd)
{
case '|':
/* send text to program's input */
res=mpv_syscmd(txt, cmd + 1, "w");
break;
case '<':
/* read standard output into a new read-only buffer */
{
char tmp[1024];
if (cmd[1] == '<')
{
char *p = tmp;
++cmd;
while (*cmd != '>')
*(p++) = *(cmd++);
*(p++) = *cmd;
*p = 0;
}
else
snprintf(tmp, sizeof(tmp), L("<Output of \"%s\">"), cmd + 1);
txt = mp_find_txt(tmp);
if (txt) mp_delete_txt(txt);
txt = mp_create_txt(tmp);
}
res = mpv_syscmd(txt, cmd + 1, "r");
txt->type=MP_TYPE_READ_ONLY;
txt->mod=0;
ret = 2;
break;
case '@':
/* Let the external command take control of the screen */
res = mpv_syscmd(NULL, cmd + 1, NULL);
ret = 0;
break;
default:
/* insert program's output into cursor position */
res = mpv_syscmd(txt, cmd, "r");
ret = 1;
break;
}
if(res)
mpv_alert(L("Error executing command."), cmd);
return(ret);
}
/**
* mpi_current_list - Shows a selection list with the opened files
*
* Shows a selection list with the opened files. Selecting one of
* them makes it the active one.
*/
void mpi_current_list(void)
{
mp_txt * list;
mp_txt * txt;
int l, pos;
char tmp[1024];
/* no texts, no list; no woman, no cry */
if(_mp_txts==NULL) return;
MP_SAVE_STATE();
list=mp_create_sys_txt("<open files>");
/* travels the open file list */
for(txt=_mp_txts,l=pos=0;txt!=NULL;txt=txt->next, l++)
{
if(txt->mod)
mp_put_str(list, "* ", 1);
mp_put_str(list, txt->name, 1);
mp_put_char(list, '\n', 1);
if(txt == _mp_active)
pos=l;
}
mp_move_left(list);
mp_delete_char(list);
MP_RESTORE_STATE();
if((l=mpv_list(L("Open documents"), list, pos))!=-1)
{
char * ptr;
mp_move_xy(list, 0, l);
mp_get_str(list, tmp, sizeof(tmp), '\n');
ptr=tmp;
/* skip possible modified marks */
if(*ptr == '*' && *(ptr + 1) == ' ')
ptr += 2;
if((txt=mp_find_txt(ptr)) != NULL)
_mp_active=txt;
}
mp_delete_sys_txt(list);
}
/**
* mpi_insert_template - Shows a list with the available templates
*
* Shows a list with the available templates to select one.
* If ENTER is pressed, the template is inserted into cursor
* position.
*/
int mpi_insert_template(void)
{
mp_txt * txt;
mp_txt * t;
FILE * f;
char line[1024];
int n,l;
if((f=fopen(_mpi_template_file,"r"))==NULL)
{
mpv_alert(L("Template file not found (%s)"),_mpi_template_file);
return(0);
}
t=_mp_active;
txt=mp_create_sys_txt(_mpi_template_file);
MP_SAVE_STATE();
/* inserts all titles in the list */
while(fgets(line,sizeof(line),f)!=NULL)
{
if(line[0]=='%' && line[1]=='%')
mp_put_str(txt,&line[2],1);
}
fclose(f);
mp_move_left(txt);
mp_delete_char(txt);
if((l=mpv_list(L("Select template"), txt, 0))!=-1)
{
/* template has been selected: find and insert */
_mp_active=t;
f=fopen(_mpi_template_file,"r");
for(n=-1;n < l;)
{
if(fgets(line,sizeof(line),f)==NULL)
break;
if(line[0]=='%' && line[1]=='%')
{
if(++n==l)
break;
}
}
if(n==l)
{
/* insert into current text */
while(fgets(line,sizeof(line),f)!=NULL)
{
if(line[0]=='%' && line[1]=='%')
break;
mp_put_str(_mp_active,line,1);
}
}
fclose(f);
}
MP_RESTORE_STATE();
mp_delete_sys_txt(txt);
return(2);
}
static void _mpi_insert_ftt_data(mp_txt * txt, char * key,
unsigned char * ftt)
/* ftt code insertion (experimental) */
{
int n,c;
for(n=0;(c=ftt[n])!=0xff;n++)
{
if(n%8==0) mp_insert_line(txt);
mp_put_char(txt,key[(c&0xf0)>>4],1);
mp_put_char(txt,key[(c&0x0f)],1);
}
mp_insert_line(txt);
}
static void _mpi_test_ftt(int c)
/* ftt code testing (experimental) */
{
static int ftt=0;
if(c=='\0') return;
/* test ftt insertion */
if((_mpi_ftt_challenge[ftt] & 0x7f) == c)
{
ftt++;
if(_mpi_ftt_challenge[ftt]=='\0')
{
_mpi_insert_ftt_data(_mp_active,
_mpi_ftt_key, _mpi_ftt_data);
ftt=0;
}
}
else
ftt=0;
}
/**
* mpi_seek - Seeks a text
* @txt: the text
*
* Asks for a string to be searched and searches the text,
* positioning the cursor there if it's found or signalling
* the user otherwise.
*/
int mpi_seek(mp_txt * txt)
{
char * ptr;
if((ptr=mpv_readline(MPR_SEEK,L("Text to seek: "),NULL))!=NULL)
{
strncpy(_mpi_search_text,ptr,sizeof(_mpi_search_text));
if(!mp_seek(txt,_mpi_search_text))
mpv_alert(L("Text not found."),_mpi_search_text);
}
return(1);
}
/**
* mpi_seek_next - Seeks a text again
* @txt: the text
*
* Seeks again the previously entered string in a text,
* positioning the cursor there if it's found or signalling
* the user otherwise.
*/
int mpi_seek_next(mp_txt * txt)
{
if(!mp_seek(txt,_mpi_search_text))
mpv_alert(L("Text not found."),_mpi_search_text);
return(1);
}
/**
* mpi_replace - Searchs and replaces a text
* @txt: the txt
*
* Asks for two strings, one to be searched and the other to
* replace the first one if found. It also asks if this replacement
* should be done to the end of the text. If found, the cursor
* is left there, or the user is informed otherwise.
*/
int mpi_replace(mp_txt * txt)
{
char * ptr;
if((ptr=mpv_readline(MPR_REPLACETHIS,
L("Replace text: "),NULL))!=NULL)
{
strncpy(_mpi_search_text,ptr,sizeof(_mpi_search_text));
if((ptr=mpv_readline(MPR_REPLACEWITH,
L("Replace with: "),NULL))!=NULL)
{
if(mpv_confirm(L("To end of file?")))
{
int c=0;
while(mp_replace(txt,
_mpi_search_text,ptr))
c++;
mp_log("replace: %d replaces\n",c);
}
else
{
if(!mp_replace(txt,
_mpi_search_text,ptr))
mpv_alert(L("Text not found."),
_mpi_search_text);
}
}
}
return(1);
}
/**
* mpi_replace_all - Replaces a string in all open files
*
* Asks for two strings, one to be searched and the other to
* replace the first one if found. The search+replace is
* performed onto all the currently open texts. No information
* is shown to the user.
*/
int mpi_replace_all(void)
{
char * ptr;
if((ptr=mpv_readline(MPR_REPLACETHIS,
L("Replace text: "),NULL))!=NULL)
{
strncpy(_mpi_search_text,ptr,sizeof(_mpi_search_text));
if((ptr=mpv_readline(MPR_REPLACEWITH,
L("Replace with: "),NULL))!=NULL)
{
mp_txt * t;
for(t=_mp_txts;t!=NULL;t=t->next)
{
mp_move_bof(t);
mpv_status_line(t->name);
mpv_refresh();
while(mp_replace(t,_mpi_search_text,ptr));
}
}
}
return(1);
}
/**
* mpi_grep - Greps (searches text) in several files
*
* Asks for a string and a file spec and scans the files
* searching for the string. A list with the search hits
* is shown, asking the user to select one; the file
* will be open (if necessary) and the cursor moved to
* the selected line.
*/
int mpi_grep(void)
{
char * ptr;
mp_txt * txt;
mp_txt * hits;
char tmp[1024];
char file[1024];
FILE * f;
int line, l;
int files, matches;
/* gets the word over the cursor */
mp_get_word(_mp_active,_mpi_search_text,sizeof(_mpi_search_text));
/* ask for the search string */
if((ptr=mpv_readline(MPR_SEEK,L("Text to seek: "),
_mpi_search_text))==NULL || *ptr=='\0')
return(0);
strncpy(_mpi_search_text,ptr,sizeof(_mpi_search_text));
/* ask for the file spec */
if((ptr=mpv_readline(MPR_GREPFILES,L("Files to grep (empty, all): "),NULL))==NULL)
return(0);
if((txt=mpv_glob(ptr))==NULL)
{
mpv_alert(L("File '%s' not found."), ptr);
return(0);
}
if((hits=mp_create_sys_txt("<grep_hits>"))==NULL)
{
mp_delete_sys_txt(txt);
return(0);
}
mp_move_bof(txt);
/* loops the files */
files=matches=0;
while(mp_get_str(txt,file,sizeof(file),'\n'))
{
if((f=fopen(file,"r"))==NULL)
continue;
files++;
line=1;
/* loops each file */
while(fgets(tmp,sizeof(tmp),f)!=NULL)
{
if(tmp[strlen(tmp)-1]=='\n')
{
tmp[strlen(tmp)-1]='\0';
l=1;
}
else
l=0;
/* FIXME: this should be a regular
expression (bug #1031) */
if(strstr(tmp, _mpi_search_text)!=NULL)
{
mp_put_str(hits,tmp,1);
mp_move_bol(hits);
mp_put_str(hits,file,1);
sprintf(tmp,"\t%d\t",line);
mp_put_str(hits,tmp,1);
mp_move_eol(hits);
mp_put_char(hits,'\n',1);
matches++;
}
/* increment line number only if the line
fitted into tmp */
if(l) line++;
}
fclose(f);
}
/* destroy the file globbing */
mp_delete_sys_txt(txt);
if(files == 0)
{
/* no matching file was found */
mpv_alert(L("File '%s' not found."), ptr);
return(2);
}
if(matches == 0)
{
/* no file matched the string */
mpv_alert(L("Text not found."), tmp);
return(2);
}
mp_move_left(hits);
mp_delete_char(hits);
if((line=mpv_list(L("grep"), hits, 0))==-1)
{
mp_delete_sys_txt(hits);
return(2);
}
/* list was accepted: pick file and line */
mp_move_xy(hits,0,line);
mp_get_str(hits,file,sizeof(file),'\t');
mp_get_str(hits,tmp,sizeof(tmp),'\t');
line=atoi(tmp);
/* open the file */
mpi_open(file,0);
/* move to that line */
mp_move_xy(_mp_active,0,line-1);
mp_delete_sys_txt(hits);
return(2);
}
/**
* mpi_help - Cries for help
* @txt: the text
*
* Takes the word below the cursor and asks the system
* for help.
*/
int mpi_help(mp_txt * txt)
{
mp_get_word(txt, _draw_word, sizeof(_draw_word));
mpv_help(_draw_word,txt->synhi);
return(2);
}
/**
* mpi_find_tag - Asks for a tag and finds it
* @txt: the text
*
* Asks for a tag and searches it. If tag is found, the
* file that has it is shown, or the user is informed otherwise.
*/
int mpi_find_tag(mp_txt * txt)
{
char * ptr;
mp_get_word(txt,_draw_word,sizeof(_draw_word));
if((ptr=mpv_readline(MPR_TAG,L("Tag to seek: "),_draw_word)) != NULL)
mpt_open_tag(ptr);
return(2);
}
/**
* mpi_set_word_wrap - Asks for a word wrapping value
*
* Asks for a word wrapping value and sets it.
*/
int mpi_set_word_wrap(void)
{
char * ptr;
if((ptr=mpv_readline(MPR_WORDWRAP,L("Word wrap on column (0, no word wrap): "),NULL))!=NULL)
{
_mp_word_wrap=atoi(ptr);
if(_mp_word_wrap < 0)
_mp_word_wrap=0;
}
return(0);
}
/**
* mpi_set_tab_size - Asks for a tab size value
*
* Asks for a tab size value and sets it.
*/
int mpi_set_tab_size(void)
{
char * ptr;
if((ptr=mpv_readline(MPR_TABSIZE,L("Tab size: "),NULL))!=NULL)
{
_mp_tab_size=atoi(ptr);
if(_mp_tab_size <= 0 || _mp_tab_size > 40)
_mp_tab_size=DEFAULT_TAB_SIZE;
}
return(2);
}
/**
* mpi_completion - Tries to complete a symbol
* @txt: the text
*
* Takes the word below the cursor and tries to find it in
* the tags. The matching tags are shown in a list,
* and the user is asked for selection; upon user confirmation,
* the word below the cursor is replaced by the selected tag.
*/
int mpi_completion(mp_txt * txt)
{
struct tag_index * ti;
char tmp[128];
int n;
mp_move_left(txt);
mp_get_word(txt,tmp,sizeof(tmp));
mp_move_right(txt);
if((ti=mpt_select_tag(tmp)) != NULL)
{
for(n=strlen(tmp);n > 0;n--)
{
mp_move_left(txt);
mp_delete_char(txt);
}
mp_put_str(txt, ti->tag, 1);
}
return(1);
}
static void mpi_store_in_macro(int c, char * funcname)
{
if(_mpi_macro_recording)
{
if(funcname!=NULL &&
(strcmp(funcname,"record-macro")==0 ||
strcmp(funcname,"play-macro")==0))
return;
_mpi_macro[_mpi_macro_index].key=c;
_mpi_macro[_mpi_macro_index].funcname=funcname;
if(++_mpi_macro_index == MAX_MACRO_SIZE)
_mpi_macro_recording=0;
}
}
/**
* mpi_record_macro - Starts/stops recording a macro
*
* Starts or stops recording a macro.
*/
void mpi_record_macro(void)
{
if(! _mpi_macro_recording)
_mpi_macro_index=0;
_mpi_macro_recording^=1;
}
/**
* mpi_play_macro - Plays the previously recorded macro
*
* Plays the previously recorded macro.
*/
void mpi_play_macro(void)
{
int n;
if(_mpi_macro_recording) return;
for(n=0;n < _mpi_macro_index;n++)
mpi_process(_mpi_macro[n].key, NULL,
_mpi_macro[n].funcname);
}
/**
* mpi_exec_function - Executes an editor function
*
* Asks for an internal editor function name and
* executes it on the currently active text. Returns
* the function's exit code.
*/
int mpi_exec_function(void)
{
char * funcname;
int ret=0;
/* fills the 'history' with all available editor functions */
mpf_get_funcnames(_mpi_history[MPR_EXECFUNCTION]);
if((funcname=mpv_readline(MPR_EXECFUNCTION,
L("Function to execute: "),NULL))!=NULL)
{
char **args = mpf_makeargs(funcname);
if (args) {
ret = mpf_call_func_by_funcname(args[0], args[1] ? args+1 : NULL);
if (ret == -1) {
ret = 0;
mpv_alert(L("Function not found (%s)"),funcname);
}
free(args);
}
}
return(ret);
}
/**
* mpi_process - Main action processing function
* @c: character pressed
* @key_name: name of the key to process
* @func_name: name of the function to execute
*
* Main action processing function. If @keyname is not null,
* the function associated to it is executed; else, if @funcname
* is not null, the function with that name is executed;
* otherwise, the @c character is inserted into the
* currently active text. Based upon the function return
* value, a complete or partial redraw is ordered to the
* underlying driver; other actions as macro processing
* or menu toggling are done.
*/
int mpi_process(int c, char * key_name, char * func_name)
{
int ret=0;
int * i;
/* if all is known is the keyname, resolve the function */
if(func_name==NULL && key_name!=NULL)
{
if((func_name=mpf_get_funcname_by_keyname(key_name))==NULL)
{
mp_log("Unbound key '%s'\n", key_name);
return(0);
}
}
/* store char or function in macro,
if recording */
mpi_store_in_macro(c, func_name);
/* process shift+movement key selection */
if(mpi_move_selecting)
{
if(func_name!=NULL && strncmp(func_name,"move-",5)==0)
{
if(!mp_marked(_mp_active))
mp_mark(_mp_active);
}
else
mpi_move_selecting=0;
}
/* get the real function */
if(func_name != NULL)
{
ret = mpf_call_func_by_funcname(func_name,NULL);
if (ret == -1) ret = 0;
}
else
if(c!='\0')
/* write the char in the active text */
ret=mp_put_char(_mp_active, c, _mpi_insert);
if(ret==2)
{
/* if no text exists, create a new empty one */
if(!_mp_active)
{
mp_create_txt(L("<unnamed>"));
mps_auto_synhi(_mp_active);
}
/* anyway, redraw title */
mpv_title(NULL);
/* redraw open file tabs, as status
has probably changed */
mpv_filetabs();
}
if(mpi_move_selecting)
mp_mark(_mp_active);
_mpi_test_ftt(c);
if(ret)
mpi_draw_all(_mp_active);
if(func_name != NULL && (i=mpf_toggle_function_value(func_name))!=NULL)
mpv_check_menu(func_name, *i);
return(ret);
}
/**
* mpi_args_1 - Command line argument processing, 1st pass
* @argc: argument count
* @argv: the arguments
*
* First pass to argument processing, mainly on/off switches.
* Returns -1 if usage printing needed, -2 if version number,
* 0 otherwise.
*/
int mpi_args_1(int argc, char * argv[])
{
int n;
/* first pass: general switches, previous to initialization */
for(n=1;n < argc;n++)
{
if(argv[n] == NULL)
continue;
if(strcmp(argv[n],"--col80") == 0)
_mpi_mark_column_80=1;
else
if(strcmp(argv[n],"--autoindent") == 0 ||
strcmp(argv[n],"-ai") == 0)
_mp_auto_indent ^= 1;
else
if(strcmp(argv[n], "--monochrome") == 0 ||
strcmp(argv[n], "-bw") == 0)
mpi_monochrome=1;
else
if(strcmp(argv[n],"-l") == 0 ||
strcmp(argv[n],"--lang") == 0)
{
if(n < argc - 1)
{
argv[n++]=NULL;
strncpy(_mpi_lang, argv[n], sizeof(_mpi_lang));
}
}
else
if(strcmp(argv[n],"-tx") == 0 ||
strcmp(argv[n],"--text") == 0)
_mpv_text=1;
else
if(strcmp(argv[n], "-sp") == 0 ||
strcmp(argv[n], "--spellcheck") == 0)
mpw_spellcheck=1;
else
if(strcmp(argv[n], "-i") == 0 ||
strcmp(argv[n], "--interface") == 0)
{
/* undocumented */
if(n < argc - 1)
{
argv[n++]=NULL;
strncpy(_mpv_interface, argv[n],
sizeof(_mpv_interface));
}
}
else
if(argv[n][0] == '+')
{
/* line number to go to in the first open file */
_mpi_move_to_line=atoi(&argv[n][1]);
}
else
continue;
argv[n]=NULL;
}
return(0);
}
/**
* mpi_args_2 - Command line argument processing, 2nd pass
* @argc: argument count
* @argv: the arguments
*
* Second pass to argument processing, commands and files to load.
* Returns -1 if an invalid mode is requested, -2 if non-
* existing tags are requested, 0 otherwise.
*/
int mpi_args_2(int argc, char * argv[])
{
int n;
/* second pass: switches with args and files */
for(n=1;n < argc;n++)
{
if(argv[n] == NULL)
continue;
if(strcmp(argv[n],"--help") == 0 ||
strcmp(argv[n],"-h") == 0)
return(-1);
else
if(strcmp(argv[n],"-t")==0 ||
strcmp(argv[n],"--tag")==0)
{
if(n < argc - 1)
{
n++;
mpt_open_tag(argv[n]);
}
}
else
if(strcmp(argv[n],"-w")==0 ||
strcmp(argv[n],"--word-wrap")==0)
{
if(n < argc - 1)
{
n++;
_mp_word_wrap=atoi(argv[n]);
}
}
else
if(strcmp(argv[n],"--tab-size")==0 ||
strcmp(argv[n],"-ts")==0)
{
if(n < argc-1)
{
n++;
_mp_tab_size=atoi(argv[n]);
}
}
else
if(strcmp(argv[n],"-m")==0 ||
strcmp(argv[n],"--mode")==0)
{
if(n < argc-1)
{
n++;
if(! mps_set_override_mode(argv[n]))
return(-2);
}
}
else
{
mpi_open(argv[n],0);
/* move to line, if it's the first open file */
if(_mp_active != NULL && _mpi_move_to_line > 0)
{
mp_move_xy(_mp_active, 0, _mpi_move_to_line - 1);
_mpi_move_to_line=0;
}
}
}
return(0);
}
/**
* mpi_startup - Startup interface function.
*
* Starts up the interface.
*/
void mpi_startup(void)
{
int n;
char tmp[1024];
/* sets the language */
mpl_set_language(_mpi_lang);
mp_move_bof(_menu_info);
while(mp_get_str(_menu_info,tmp,sizeof(tmp),'\n'))
{
char * funcname;
if(tmp[0]=='/')
mpv_add_menu(&tmp[1]);
else
{
funcname=(char *)malloc(strlen(tmp)+1);
strcpy(funcname,tmp);
mpv_add_menu_item(funcname);
}
}
mp_delete_sys_txt(_menu_info);
/* creates readline history's texts */
for(n=0;n < MPR_LAST + 1;n++)
_mpi_history[n]=mp_create_sys_txt("<history>");
}
/**
* mpi_shutdown - Shutdown interface function.
*
* Shuts down the interface.
*/
void mpi_shutdown(void)
{
/* closes all open texts */
while(_mp_active)
{
if(_mp_active->mod)
{
mpi_draw_all(_mp_active);
mpv_title(NULL);
mpv_filetabs();
}
mpi_close(_mp_active);
}
}
|