home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / YADME10.LHA / YADME10 / src / cmd3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  10.7 KB  |  557 lines

  1.  
  2. /*
  3.  * CMD3.C
  4.  *
  5.  *      (C)Copyright 1988 by Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  SETFONT
  8.  *  IGNORECASE
  9.  *  SET
  10.  *  SETENV
  11.  *  UNSET
  12.  *  UNSETENV
  13.  *  CD
  14.  *  SAVECONFIG
  15.  *  FGPEN
  16.  *  TPEN
  17.  *  BGPEN
  18.  *  TITLE
  19.  *  JUSTIFY
  20.  *  UNJUSTIFY
  21.  *  MODIFIED
  22.  *  UNDELINE
  23.  */
  24.  
  25. #include "defs.h"
  26. #include <reqtools/reqtools.h>
  27.  
  28. #define nomemory()  { memoryfail = 1; }
  29.  
  30. /*
  31.  *  SETFONT font size
  32.  */
  33.  
  34. void do_setfont(void)
  35. {
  36.     FONT *font = (FONT *)GetFont(av[1], (short)atoi(av[2]));
  37.     ED *ep = Ep;
  38.     if (font) {
  39.         text_sync();
  40.         if (ep->Font)
  41.             CloseFont(ep->Font);
  42.         ep->Font = font;
  43.         SetFont(ep->Win->RPort, font);
  44.         SetRast(ep->Win->RPort, 0);
  45.         RefreshWindowFrame(ep->Win);
  46.         set_window_params();
  47.         text_redisplay();
  48.     } else {
  49.         title("Unable to find font");
  50.     }
  51. }
  52.  
  53. void do_ignorecase(void)
  54. {
  55.     ED *ep = Ep;
  56.  
  57.     if (av[1][0]) {
  58.         switch(av[1][1] & 0x1F) {
  59.         case 'n'&0x1F:
  60.             ep->IgnoreCase = 1;
  61.             break;
  62.         case 'f'&0x1F:
  63.             ep->IgnoreCase = 0;
  64.             break;
  65.         case 'o'&0x1F:
  66.             ep->IgnoreCase = 1 - ep->IgnoreCase;
  67.             break;
  68.         }
  69.         if (ep->IgnoreCase)
  70.             title("Case InSensitive");
  71.         else
  72.             title("Case Sensitive");
  73.     }
  74. }
  75.  
  76. /*
  77.  *  av[1]
  78.  */
  79.  
  80. void do_cd(void)
  81. {
  82.     BPTR oldlock;
  83.     BPTR lock;
  84.  
  85.     oldlock = CurrentDir((BPTR)Ep->dirlock);
  86.     if (lock = Lock(av[1], SHARED_LOCK)) {
  87.         UnLock(CurrentDir(oldlock));
  88.         Ep->dirlock = (long)lock;
  89.     } else {
  90.         CurrentDir(oldlock);
  91.         Abortcommand = 1;
  92.         title("Unable to CD");
  93.     }
  94. }
  95.  
  96. /*
  97.  *  VARIABLE SUPPORT!
  98.  */
  99.  
  100. #define VARS    struct _VARS
  101. VARS {
  102.     MNODE   Node;
  103.     char    *Name;
  104.     char    *Str;
  105. };
  106.  
  107. static MLIST SList = { (MNODE *)&SList.mlh_Tail, NULL, (MNODE *)&SList.mlh_Head };
  108.  
  109. void do_set(void)
  110. {
  111.     VARS *v;
  112.     void do_unset();
  113.  
  114.     do_unset();
  115.     if (v = malloc(sizeof(VARS))) {
  116.         if (v->Name = malloc(strlen(av[1])+1)) {
  117.             if (v->Str = malloc(strlen(av[2])+1)) {
  118.                 AddHead((LIST *)&SList, (NODE *)v);
  119.                 strcpy(v->Name, av[1]);
  120.                 strcpy(v->Str , av[2]);
  121.                 return;
  122.             }
  123.             free(v->Name);
  124.         }
  125.         free(v);
  126.     }
  127.     nomemory();
  128. }
  129.  
  130. void do_setenv(void)
  131. {
  132.     SetDEnv(av[1], av[2]);
  133. }
  134.  
  135. void do_unset(void)
  136. {
  137.     VARS *v;
  138.  
  139.     for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
  140.         if (strcmp(v->Name, av[1]) == 0) {
  141.             Remove((NODE *)v);
  142.             free(v);
  143.             free(v->Name);
  144.             free(v->Str);
  145.             break;
  146.         }
  147.     }
  148. }
  149.  
  150. void do_unsetenv(void)
  151. {
  152.     char *ptr = (char *)av[1];
  153.     char *tmp = malloc(4+strlen(ptr)+1);
  154.  
  155.     if (tmp) {
  156.         strcpy(tmp, "ENV:");
  157.         strcat(tmp, ptr);
  158.         mountrequest(0);
  159.         DeleteFile(tmp);
  160.         mountrequest(1);
  161.         free(tmp);
  162.     }
  163. }
  164.  
  165. /*
  166.  *  Search (1) internal list, (2) enviroment, (3) macros.  The variable
  167.  *  is allocated with malloc().  NULL if not found.  ENV: need not exist.
  168.  */
  169.  
  170. char *getvar(char *find)
  171. {
  172.     char *str = NULL;
  173.     {
  174.         VARS *v;
  175.  
  176.         for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
  177.             if (strcmp(v->Name, find) == 0) {
  178.                 if (str = malloc(strlen(v->Str)+1)) {
  179.                     strcpy(str, v->Str);
  180.                     return(str);
  181.                 }
  182.             }
  183.         }
  184.     }
  185.  
  186.     mountrequest(0);
  187.     str = (char *)GetDEnv(find);
  188.     mountrequest(1);
  189.     if (str)
  190.         return(str);
  191.  
  192.     if ((str = keyspectomacro(find)) || (str = menutomacro(find))) {
  193.         char *ptr = malloc(strlen(str)+1);
  194.         if (ptr) {
  195.             strcpy(ptr, str);
  196.             return(ptr);
  197.         }
  198.     }
  199.     return(NULL);
  200. }
  201.  
  202. void do_col(void)
  203. {
  204.     int col;
  205.  
  206.     {
  207.         char *ptr = av[1];
  208.  
  209.         switch(*ptr) {
  210.         case '+':
  211.             col = text_colno() + atoi(ptr + 1);
  212.             if (col > 254)
  213.                 col = 254;
  214.             break;
  215.         case '-':
  216.             col = text_colno() + atoi(ptr);
  217.             if (col < 0)
  218.                 col = 0;
  219.             break;
  220.         default:
  221.             col = atoi(ptr) - 1;
  222.             break;
  223.         }
  224.     }
  225.     if (col > 254 || col < 0) {
  226.         Abortcommand = 1;
  227.         return;
  228.     }
  229.     while (Clen < col)
  230.         Current[Clen++] = ' ';
  231.     Current[Clen] = 0;
  232.     Ep->Column = col;
  233.     if (Ep->Column - Ep->Topcolumn >= Columns || Ep->Column < Ep->Topcolumn)
  234.         text_sync();
  235. }
  236.  
  237. void do_saveconfig(void)
  238. {
  239.     ED *ep = Ep;
  240.     FILE *fi;
  241.  
  242.     if (ep->iconmode == 0) {
  243.         WIN *win = ep->Win;
  244.         ep->Winx      = win->LeftEdge;
  245.         ep->Winy      = win->TopEdge;
  246.         ep->Winwidth  = win->Width;
  247.         ep->Winheight = win->Height;
  248.     }
  249.  
  250.     if (fi = fopen("ENVARC:dme.config", "w")) {
  251.         fwrite(&ep->BeginConfig, (char *)&ep->EndConfig - (char *)&ep->BeginConfig, 1, fi);
  252.         fclose(fi);
  253.     }
  254. }
  255.  
  256. void loadconfig(ED *ep)
  257. {
  258.     FILE *fi;
  259.     char buf[10];
  260.  
  261.     if (fi = fopen("ENVARC:dme.config", "r")) {
  262.         fread(&ep->BeginConfig, (char *)&ep->EndConfig - (char *)&ep->BeginConfig, 1, fi);
  263.         fclose(fi);
  264.     }
  265. }
  266.  
  267. /* Now all color set routines redraw the text */
  268.  
  269. void do_fgpen(void)
  270. {
  271.     ED *ep = Ep;
  272.  
  273.     ep->FGPen = atoi(av[1]);
  274.     text_redisplay();
  275. }
  276.  
  277. void do_tpen(void)
  278. {
  279.     ED *ep = Ep;
  280.  
  281.     ep->TPen = atoi(av[1]);
  282.     text_redisplay();
  283. }
  284.  
  285. void do_bgpen(void)
  286. {
  287.     ED *ep = Ep;
  288.  
  289.     ep->BGPen = atoi(av[1]);
  290.     text_redisplay();
  291. }
  292.  
  293. void do_hgpen(void)
  294. {
  295.     ED *ep = Ep;
  296.  
  297.     ep->HGPen = atoi(av[1]);
  298.     text_redisplay();
  299. }
  300.  
  301. /* New: 06/94 Karl Lukas */
  302.  
  303. void do_hgbgpen(void)
  304. {
  305.     ED *ep = Ep;
  306.  
  307.     ep->HGBGPen = atoi(av[1]);
  308.     text_redisplay();
  309. }
  310.  
  311. /*
  312.  *  Commands submitted by Markus Wenzel
  313.  */
  314.  
  315. void do_undeline(void)
  316. {
  317.    do_insline();
  318.    text_load();
  319.    strcpy(Current, Deline);
  320.    text_sync();
  321.    text_displayseg(Ep->Line - Ep->Topline, 1);
  322. }
  323.  
  324.  
  325. void do_modified(void)
  326. {
  327.     register ED *ep = Ep;
  328.  
  329.     if (av[1][0]) {
  330.         switch(av[1][1] & 0x1F) {
  331.         case 'n' & 0x1F:
  332.             ep->Modified = 1;
  333.             break;
  334.         case 'f' & 0x1F:
  335.             ep->Modified = 0;
  336.             break;
  337.         case 'o' & 0x1F:
  338.             ep->Modified = ep->Modified ? 0 : 1;
  339.             break;
  340.         }
  341.     }
  342. }
  343.  
  344.  
  345. void do_unjustify(void)
  346. {
  347.     short i, j, waswhite = FALSE;
  348.     ubyte c;
  349.  
  350.  
  351.     for (i = 0; Current[i] == ' '; i++);
  352.     for (j = i; Current[i]; i++) {
  353.         c = Current[j] = Current[i];
  354.         if (c != ' ' || !waswhite)
  355.             j++;
  356.         waswhite = (c == ' ');
  357.  
  358.     }
  359.     Current[j] = 0;
  360.  
  361.     if (i != j) {
  362.         text_sync();
  363.         text_redisplaycurrline();
  364.     }
  365. }
  366.  
  367.  
  368. void do_justify(void)
  369. {
  370.     ED *ep = Ep;
  371.     short firstnb, lastnb, i, n, fill, c, sp;
  372.     short changed = FALSE;
  373.  
  374.  
  375.     switch(av[1][0]) {
  376.     case 'c':
  377.         break;
  378.     case 'f':
  379.         firstnb = firstns(Current);
  380.         lastnb = lastns(Current);
  381.         if (firstnb < lastnb && ep->Margin < 255) {
  382.             n = 0;
  383.             i = firstnb;
  384.             while (i <= lastnb) {
  385.                 while ((c = Current[i]) && c != ' ')
  386.                     i++;
  387.                 if (i <= lastnb) {
  388.                     n++;
  389.                     while (Current[i] == ' ')
  390.                         i++;
  391.                 }
  392.             }
  393.             fill = ep->Margin - lastnb - 1;
  394.             i = firstnb;
  395.             Current[lastnb + 1] = 0;
  396.             if (n > 0 && fill > 0)
  397.                 changed = TRUE;
  398.             while (n > 0 && fill > 0 && Current[i]) {
  399.                 while ((c = Current[i]) && c != ' ')
  400.                     i++;
  401.                 sp = fill / n;
  402.                 movmem(&Current[i], &Current[i + sp], strlen(&Current[i]) + 1);
  403.                 memset(&Current[i], ' ', sp);
  404.                 while (Current[i] == ' ')
  405.                     i++;
  406.                 fill -= sp;
  407.                 n--;
  408.             }
  409.         }
  410.         break;
  411.     default:
  412.         break;
  413.     }
  414.  
  415.     if (changed) {
  416.         text_sync();
  417.         text_redisplaycurrline();
  418.     }
  419. }
  420.  
  421.  
  422. void do_title(void)
  423. {
  424.     static ubyte wtitle[256];
  425.  
  426.     strncpy(wtitle, av[1], 255);
  427.     wtitle[255] = 0;
  428.     title(wtitle);
  429. }
  430.  
  431. void do_space(void)
  432. {
  433.     ED *ep = Ep;
  434.     int insmode = ep->Insertmode;
  435.  
  436.     ep->Insertmode = 1;
  437.     text_write(" ");
  438.     ep->Insertmode = insmode;
  439. }
  440.  
  441. /* MMW 1.42 */
  442. void do_sizewindow(void)
  443. {
  444.     WIN *win = Ep->Win;
  445.     struct NewWindow nw;
  446.     int mdx, mdy;
  447.  
  448.     GeometryToNW(av[1], &nw);
  449.  
  450.     if (nw.LeftEdge + nw.Width <= win->WScreen->Width &&
  451.         nw.TopEdge + nw.Height <= win->WScreen->Height &&
  452.         nw.Width >= win->MinWidth &&
  453.         nw.Height >= win->MinHeight) {
  454.  
  455.         mdx = nw.LeftEdge - win->LeftEdge;
  456.         mdy = nw.TopEdge - win->TopEdge;
  457.         if (mdx > 0)
  458.             mdx = 0;
  459.         if (mdy > 0)
  460.             mdy = 0;
  461.  
  462.         MoveWindow(win, mdx, mdy);
  463.         SizeWindow(win, nw.Width - win->Width, nw.Height - win->Height);
  464.         MoveWindow(win, nw.LeftEdge - win->LeftEdge, nw.TopEdge - win->TopEdge);
  465.     }
  466. }
  467.  
  468.  
  469. void do_reqparcol(void)
  470. {
  471.    struct TagItem taglist[3];
  472.    ULONG number = Ep->WWCol;
  473.  
  474.    if (! ReqToolsBase)
  475.    {
  476.       title("No reqtools.library");
  477.       return;
  478.    }
  479.    taglist[0].ti_Tag = RTGL_Min;
  480.    taglist[0].ti_Data = 0;
  481.    taglist[1].ti_Tag = RTGL_Max;
  482.    taglist[1].ti_Data = Ep->Margin - 1;
  483.    taglist[2].ti_Tag = TAG_DONE;
  484.    taglist[2].ti_Data = 0;
  485.  
  486.    if (rtGetLongA(&number, "Left margin", NULL, taglist))
  487.       Ep->WWCol = number;
  488. }
  489.  
  490. void do_reqmargin(void)
  491. {
  492.    TagItem taglist[3];
  493.    ULONG number = Ep->Margin;
  494.  
  495.    if (! ReqToolsBase)
  496.    {
  497.       title("No reqtools.library");
  498.       return;
  499.    }
  500.    taglist[0].ti_Tag = RTGL_Min;
  501.    taglist[0].ti_Data = Ep->WWCol + 1;
  502.    taglist[1].ti_Tag = RTGL_Max;
  503.    taglist[1].ti_Data = 255;
  504.    taglist[2].ti_Tag = TAG_DONE;
  505.    taglist[2].ti_Data = 0;
  506.  
  507.    if (rtGetLongA(&number, "Right margin", NULL, taglist))
  508.       Ep->Margin = number;
  509. }
  510.  
  511. void do_reqgoto(void)
  512. {
  513.    TagItem taglist[3];
  514.    ULONG number = Ep->Line + 1;
  515.    char buf[10];
  516.    if (! ReqToolsBase)
  517.    {
  518.       title("No reqtools.library");
  519.       return;
  520.    }
  521.    taglist[0].ti_Tag = RTGL_Min;
  522.    taglist[0].ti_Data = 0;
  523.    taglist[1].ti_Tag = RTGL_Max;
  524.    taglist[1].ti_Data = Ep->Lines - 1;
  525.    taglist[2].ti_Tag = TAG_DONE;
  526.    taglist[2].ti_Data = 0;
  527.  
  528.    if (rtGetLongA(&number, "Goto line", NULL, taglist))
  529.    {
  530.       sprintf(buf, "%ld", number);
  531.       av[1] = buf;
  532.       do_goto();
  533.    }
  534. }
  535.  
  536. void do_reqtabstop(void)
  537. {
  538.    TagItem taglist[3];
  539.    ULONG number = Ep->Tabstop;
  540.  
  541.    if (! ReqToolsBase)
  542.    {
  543.       title("No reqtools.library");
  544.       return;
  545.    }
  546.    taglist[0].ti_Tag = RTGL_Min;
  547.    taglist[0].ti_Data = 1;
  548.    taglist[1].ti_Tag = RTGL_Max;
  549.    taglist[1].ti_Data = 255;
  550.    taglist[2].ti_Tag = TAG_DONE;
  551.    taglist[2].ti_Data = 0;
  552.  
  553.    if (rtGetLongA(&number, "Set tabwidth", NULL, taglist))
  554.       Ep->Tabstop = number;
  555. }
  556.  
  557.