home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 411b.lha / dme_1.42 / src.LZH / src / cmd3.c < prev    next >
C/C++ Source or Header  |  1990-08-20  |  7KB  |  427 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.  *  BGPEN
  17.  *  TITLE
  18.  *  JUSTIFY
  19.  *  UNJUSTIFY
  20.  *  MODIFIED
  21.  *  UNDELINE
  22.  */
  23.  
  24. #include "defs.h"
  25. #include <libraries/dos.h>
  26.  
  27. #define nomemory()  { memoryfail = 1; }
  28.  
  29. /*
  30.  *  SETFONT font size
  31.  */
  32.  
  33. void
  34. do_setfont()
  35. {
  36.     FONT *font = (FONT *)GetFont(av[1], (short)atoi(av[2]));
  37.     ED *ep = Ep;
  38.     if (font) {
  39.     if (ep->Font)
  40.         CloseFont(ep->Font);
  41.     ep->Font = font;
  42.     SetFont(ep->Win->RPort, font);
  43.     SetRast(ep->Win->RPort, 0);
  44.     RefreshWindowFrame(ep->Win);
  45.     set_window_params();
  46.     text_redisplay();
  47.     } else {
  48.     title("Unable to find font");
  49.     }
  50. }
  51.  
  52. void
  53. do_ignorecase()
  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
  81. do_cd()
  82. {
  83.     BPTR oldlock;
  84.     BPTR lock;
  85.  
  86.     oldlock = CurrentDir((BPTR)Ep->dirlock);
  87.     if (lock = Lock(av[1], SHARED_LOCK)) {
  88.     UnLock(CurrentDir(oldlock));
  89.     Ep->dirlock = (long)lock;
  90.     } else {
  91.     CurrentDir(oldlock);
  92.     Abortcommand = 1;
  93.     title("Unable to CD");
  94.     }
  95. }
  96.  
  97. /*
  98.  *  VARIABLE SUPPORT!
  99.  */
  100.  
  101. #define VARS    struct _VARS
  102. VARS {
  103.     MNODE   Node;
  104.     char    *Name;
  105.     char    *Str;
  106. };
  107.  
  108. static MLIST SList = { (MNODE *)&SList.mlh_Tail, NULL, (MNODE *)&SList.mlh_Head };
  109.  
  110. void
  111. do_set()
  112. {
  113.     VARS *v;
  114.     void do_unset();
  115.  
  116.     do_unset();
  117.     if (v = malloc(sizeof(VARS))) {
  118.     if (v->Name = malloc(strlen(av[1])+1)) {
  119.         if (v->Str = malloc(strlen(av[2])+1)) {
  120.         AddHead((LIST *)&SList, (NODE *)v);
  121.         strcpy(v->Name, av[1]);
  122.         strcpy(v->Str , av[2]);
  123.         return;
  124.         }
  125.         free(v->Name);
  126.     }
  127.     free(v);
  128.     }
  129.     nomemory();
  130. }
  131.  
  132. void
  133. do_setenv()
  134. {
  135.     SetDEnv(av[1], av[2]);
  136. }
  137.  
  138. void
  139. do_unset()
  140. {
  141.     VARS *v;
  142.  
  143.     for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
  144.     if (strcmp(v->Name, av[1]) == 0) {
  145.         Remove((NODE *)v);
  146.         free(v);
  147.         free(v->Name);
  148.         free(v->Str);
  149.         break;
  150.     }
  151.     }
  152. }
  153.  
  154. void
  155. do_unsetenv()
  156. {
  157.     char *ptr = (char *)av[1];
  158.     char *tmp = malloc(4+strlen(ptr)+1);
  159.  
  160.     if (tmp) {
  161.     strcpy(tmp, "ENV:");
  162.     strcat(tmp, ptr);
  163.     mountrequest(0);
  164.     DeleteFile(tmp);
  165.     mountrequest(1);
  166.     free(tmp);
  167.     }
  168. }
  169.  
  170. /*
  171.  *  Search (1) internal list, (2) enviroment, (3) macros.  The variable
  172.  *  is allocated with malloc().  NULL if not found.  ENV: need not exist.
  173.  */
  174.  
  175. char *
  176. getvar(find)
  177. char *find;
  178. {
  179.     char *str = NULL;
  180.     {
  181.     VARS *v;
  182.  
  183.     for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
  184.         if (strcmp(v->Name, find) == 0) {
  185.         if (str = malloc(strlen(v->Str)+1)) {
  186.             strcpy(str, v->Str);
  187.             return(str);
  188.         }
  189.         }
  190.     }
  191.     }
  192.  
  193.     mountrequest(0);
  194.     str = (char *)GetDEnv(find);
  195.     mountrequest(1);
  196.     if (str)
  197.     return(str);
  198.  
  199.     if ((str = keyspectomacro(find)) || (str = menutomacro(find))) {
  200.     char *ptr = malloc(strlen(str)+1);
  201.     if (ptr) {
  202.         strcpy(ptr, str);
  203.         return(ptr);
  204.     }
  205.     }
  206.     return(NULL);
  207. }
  208.  
  209. void
  210. do_col()
  211. {
  212.     int col;
  213.  
  214.     {
  215.     char *ptr = av[1];
  216.  
  217.     switch(*ptr) {
  218.     case '+':
  219.         col = text_colno() + atoi(ptr + 1);
  220.         if (col > 254)
  221.         col = 254;
  222.         break;
  223.     case '-':
  224.         col = text_colno() + atoi(ptr);
  225.         if (col < 0)
  226.         col = 0;
  227.         break;
  228.     default:
  229.         col = atoi(ptr) - 1;
  230.         break;
  231.     }
  232.     }
  233.     if (col > 254 || col < 0) {
  234.     Abortcommand = 1;
  235.     return;
  236.     }
  237.     while (Clen < col)
  238.     Current[Clen++] = ' ';
  239.     Current[Clen] = 0;
  240.     Ep->Column = col;
  241.     if (Ep->Column - Ep->Topcolumn >= Columns || Ep->Column < Ep->Topcolumn)
  242.     text_sync();
  243. }
  244.  
  245. void
  246. do_saveconfig()
  247. {
  248.     ED *ep = Ep;
  249.     FILE *fi;
  250.  
  251.     if (ep->iconmode == 0) {
  252.     WIN *win = ep->Win;
  253.     ep->Winx      = win->LeftEdge;
  254.     ep->Winy      = win->TopEdge;
  255.     ep->Winwidth  = win->Width;
  256.     ep->Winheight = win->Height;
  257.     }
  258.  
  259.     if (fi = fopen("s:dme.config", "w")) {
  260.     fwrite(&ep->BeginConfig, (char *)&ep->EndConfig - (char *)&ep->BeginConfig, 1, fi);
  261.     fclose(fi);
  262.     }
  263. }
  264.  
  265. void
  266. loadconfig(ep)
  267. ED *ep;
  268. {
  269.     FILE *fi;
  270.  
  271.     if (fi = fopen("s:dme.config", "r")) {
  272.     fread(&ep->BeginConfig, (char *)&ep->EndConfig - (char *)&ep->BeginConfig, 1, fi);
  273.     fclose(fi);
  274.     }
  275. }
  276.  
  277. void
  278. do_fgpen()
  279. {
  280.     ED *ep = Ep;
  281.  
  282.     ep->FGPen = atoi(av[1]);
  283. }
  284.  
  285. void
  286. do_bgpen()
  287. {
  288.     ED *ep = Ep;
  289.  
  290.     ep->BGPen = atoi(av[1]);
  291. }
  292.  
  293. void
  294. do_hgpen()
  295. {
  296.     ED *ep = Ep;
  297.  
  298.     ep->HGPen = atoi(av[1]);
  299. }
  300.  
  301. /*
  302.  *  Commands submitted by Markus Wenzel
  303.  */
  304.  
  305. void
  306. do_undeline()
  307. {
  308.    do_insline();
  309.    text_load();
  310.    strcpy(Current, Deline);
  311.    text_sync();
  312.    text_displayseg(Ep->Line - Ep->Topline, 1);
  313. }
  314.  
  315.  
  316. void
  317. do_modified()
  318. {
  319.     register ED *ep = Ep;
  320.  
  321.     if (av[1][0]) {
  322.     switch(av[1][1] & 0x1F) {
  323.     case 'n' & 0x1F:
  324.         ep->Modified = 1;
  325.         break;
  326.     case 'f' & 0x1F:
  327.         ep->Modified = 0;
  328.         break;
  329.     case 'o' & 0x1F:
  330.         ep->Modified = ep->Modified ? 0 : 1;
  331.         break;
  332.     }
  333.     }
  334. }
  335.  
  336.  
  337. void
  338. do_unjustify()
  339. {
  340.     short i, j, waswhite = FALSE;
  341.     ubyte c;
  342.  
  343.  
  344.     for (i = 0; Current[i] == ' '; i++);
  345.     for (j = i; Current[i]; i++) {
  346.     c = Current[j] = Current[i];
  347.     if (c != ' ' || !waswhite)
  348.         j++;
  349.     waswhite = (c == ' ');
  350.  
  351.     }
  352.     Current[j] = 0;
  353.  
  354.     if (i != j) {
  355.     text_sync();
  356.     text_redisplaycurrline();
  357.     }
  358. }
  359.  
  360.  
  361. void
  362. do_justify()
  363. {
  364.     ED *ep = Ep;
  365.     short firstnb, lastnb, i, n, fill, c, sp;
  366.     short changed = FALSE;
  367.  
  368.  
  369.     switch(av[1][0]) {
  370.     case 'c':
  371.     break;
  372.     case 'f':
  373.     firstnb = firstns(Current);
  374.     lastnb = lastns(Current);
  375.     if (firstnb < lastnb && ep->Margin < 255) {
  376.         n = 0;
  377.         i = firstnb;
  378.         while (i <= lastnb) {
  379.         while ((c = Current[i]) && c != ' ')
  380.             i++;
  381.         if (i <= lastnb) {
  382.             n++;
  383.             while (Current[i] == ' ')
  384.             i++;
  385.         }
  386.         }
  387.         fill = ep->Margin - lastnb - 1;
  388.         i = firstnb;
  389.         Current[lastnb + 1] = 0;
  390.         if (n > 0 && fill > 0)
  391.         changed = TRUE;
  392.         while (n > 0 && fill > 0 && Current[i]) {
  393.         while ((c = Current[i]) && c != ' ')
  394.             i++;
  395.         sp = fill / n;
  396.         movmem(&Current[i], &Current[i + sp], strlen(&Current[i]) + 1);
  397.         memset(&Current[i], ' ', sp);
  398.         while (Current[i] == ' ')
  399.             i++;
  400.         fill -= sp;
  401.         n--;
  402.         }
  403.     }
  404.     break;
  405.     default:
  406.     break;
  407.     }
  408.  
  409.     if (changed) {
  410.     text_sync();
  411.     text_redisplaycurrline();
  412.     }
  413. }
  414.  
  415.  
  416. void
  417. do_title()
  418. {
  419.     static ubyte wtitle[256];
  420.  
  421.     strncpy(wtitle, av[1], 255);
  422.     wtitle[255] = 0;
  423.     title(wtitle);
  424. }
  425.  
  426.  
  427.