home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / VILE327.ZIP / VILE327.TAR / vile3.27 / modes.c < prev    next >
C/C++ Source or Header  |  1992-12-14  |  10KB  |  450 lines

  1. /* 
  2.  *
  3.  *    modes.c
  4.  *
  5.  * Maintain and list the editor modes and value sets.
  6.  *
  7.  * Original code probably by Dan Lawrence or Dave Conroy for MicroEMACS.
  8.  * Major extensions for vile by Paul Fox, 1991
  9.  *
  10.  *    $Log: modes.c,v $
  11.  * Revision 1.4  1992/12/04  09:14:36  foxharp
  12.  * deleted unused assigns
  13.  *
  14.  * Revision 1.3  1992/08/20  23:40:48  foxharp
  15.  * typo fixes -- thanks, eric
  16.  *
  17.  * Revision 1.2  1992/07/13  20:03:54  foxharp
  18.  * the "terse" variable is now a boolean mode
  19.  *
  20.  * Revision 1.1  1992/05/29  09:38:33  foxharp
  21.  * Initial revision
  22.  *
  23.  *
  24.  *
  25.  */
  26.  
  27. #include    <stdio.h>
  28. #include    "estruct.h"
  29. #include    "edef.h"
  30.  
  31. /* ARGSUSED */
  32. int
  33. listmodes(f, n)
  34. int f,n;
  35. {
  36.     register WINDOW *wp = curwp;
  37.     register int s;
  38.  
  39.     s = liststuff("[Settings]",makemodelist,0,(char *)wp);
  40.     /* back to the buffer whose modes we just listed */
  41.     swbuffer(wp->w_bufp);
  42.     return s;
  43. }
  44.  
  45.  
  46. /* list the current modes into the current buffer */
  47. /* ARGSUSED */
  48. void
  49. makemodelist(dum1,ptr)
  50. int dum1;
  51. char *ptr;
  52. {
  53.     register WINDOW *localwp = (WINDOW *)ptr;
  54.     register BUFFER *localbp = localwp->w_bufp;
  55.     bprintf("--- \"%s\" settings, if different than globals %*P\n",
  56.             localbp->b_bname, term.t_ncol-1, '-');
  57.     listvalueset(b_valuenames, localbp->b_values.bv, global_b_values.bv);
  58.     bputc('\n');
  59.     listvalueset(w_valuenames, localwp->w_values.wv, global_w_values.wv);
  60.     bprintf("--- Global settings %*P\n", term.t_ncol-1, '-');
  61.     listvalueset(b_valuenames, global_b_values.bv, NULL);
  62.     bputc('\n');
  63.     listvalueset(w_valuenames, global_w_values.wv, NULL);
  64. #if LAZY
  65.     lsprintf(line," lazy filename matching is %s",
  66.                     (othmode & OTH_LAZY) ? "on":"off");
  67.     addline(blistp,line,-1);
  68. #endif
  69. }
  70.  
  71. /* listvalueset: print each value in the array according to type,
  72.     along with its name, until a NULL name is encountered.  Only print
  73.     if the value in the two arrays differs, or the second array is nil */
  74. int
  75. listvalueset(names, values, globvalues)
  76. struct VALNAMES *names;
  77. struct VAL *values, *globvalues;
  78. {
  79.     register int j, perline;
  80.     perline = 3;
  81.     j = 0;
  82.     while(names->name != NULL) {
  83.         switch(names->type) {
  84.         case VALTYPE_BOOL:
  85.             if (!globvalues || values->vp->i != globvalues->v.i) {
  86.                 bprintf("%s%s%*P", values->vp->i ? "":"no",
  87.                     names->name, 26, ' ');
  88.                 j++;
  89.             }
  90.             break;
  91.         case VALTYPE_INT:
  92.             if (!globvalues || values->vp->i != globvalues->v.i) {
  93.                 bprintf("%s=%d%*P", names->name,
  94.                     values->vp->i, 26, ' ');
  95.                 j++;
  96.             }
  97.             break;
  98.         case VALTYPE_STRING:
  99.             perline = 1;
  100.             if (j && j >= perline) {
  101.                 bputc('\n');
  102.                 j = 0;
  103.             }
  104.             if (!globvalues || ( values->vp->p && globvalues->v.p &&
  105.                 (strcmp( values->vp->p, globvalues->v.p)))) {
  106.                 bprintf("%s=%s%*P", names->name,
  107.                     values->vp->p ? values->vp->p : "",
  108.                     26, ' ');
  109.                 j++;
  110.             }
  111.             break;
  112.         case VALTYPE_REGEX:
  113.             if (!globvalues || (values->vp->r->pat && globvalues->v.r->pat &&
  114.                 (strcmp( values->vp->r->pat, globvalues->v.r->pat)))) {
  115.                 bprintf("%s=%s%*P", names->name,
  116.                     values->vp->r->pat ? values->vp->r->pat : "",
  117.                     26, ' ');
  118.                 j++;
  119.             }
  120.             break;
  121.         default:
  122.             mlforce("BUG: bad type %s %d",names->name,names->type);
  123.             return FALSE;
  124.         }
  125.         if (j && j >= perline) {
  126.             bputc('\n');
  127.             j = 0;
  128.         }
  129.         names++;
  130.         values++;
  131.         if (globvalues) globvalues++;
  132.     }
  133.     if (j % 3 != 0)
  134.         bputc('\n');
  135.     return TRUE;
  136. }
  137. /*
  138.  * Set tab size
  139.  */
  140. int
  141. settab(f, n)
  142. int f,n;
  143. {
  144.     register WINDOW *wp;
  145.     int val;
  146.     char *whichtabs;
  147.     if (b_val(curbp, MDCMOD)) {
  148.         val = VAL_C_TAB;
  149.         whichtabs = "C-t";
  150.     } else {
  151.         val = VAL_TAB;
  152.         whichtabs = "T";
  153.     }
  154.     if (f && n >= 1) {
  155.         make_local_b_val(curbp,val);
  156.         set_b_val(curbp,val,n);
  157.         curtabval = n;
  158.         for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
  159.             if (wp->w_bufp == curbp) wp->w_flag |= WFHARD;
  160.     } else if (f) {
  161.         mlforce("[Illegal tabstop value]");
  162.         TTbeep();
  163.         return FALSE;
  164.     }
  165.     if (!global_b_val(MDTERSE) || !f)
  166.         mlwrite("[%sabs are %d columns apart, using %s value.]", whichtabs,
  167.             curtabval, is_global_b_val(curbp,val)?"global":"local" );
  168.     return TRUE;
  169. }
  170.  
  171. /*
  172.  * Set fill column to n.
  173.  */
  174. int
  175. setfillcol(f, n)
  176. int f,n;
  177. {
  178.     if (f && n >= 1) {
  179.         make_local_b_val(curbp,VAL_FILL);
  180.         set_b_val(curbp,VAL_FILL,n);
  181.     } else if (f) {
  182.         mlforce("[Illegal fill-column value]");
  183.         TTbeep();
  184.         return FALSE;
  185.     }
  186.     if (!global_b_val(MDTERSE) || !f)
  187.         mlwrite("[Fill column is %d, and is %s]", b_val(curbp,VAL_FILL),
  188.             is_global_b_val(curbp,VAL_FILL) ? "global" : "local" );
  189.     return(TRUE);
  190. }
  191.  
  192.  
  193. /* ARGSUSED */
  194. int
  195. setmode(f, n)    /* prompt and set an editor mode */
  196. int f, n;    /* default and argument */
  197. {
  198.     return adjustmode(TRUE, FALSE);
  199. }
  200.  
  201. /* ARGSUSED */
  202. int
  203. delmode(f, n)    /* prompt and delete an editor mode */
  204. int f, n;    /* default and argument */
  205. {
  206.     return adjustmode(FALSE, FALSE);
  207. }
  208.  
  209. /* ARGSUSED */
  210. int
  211. setgmode(f, n)    /* prompt and set a global editor mode */
  212. int f, n;    /* default and argument */
  213. {
  214.     return adjustmode(TRUE, TRUE);
  215. }
  216.  
  217. /* ARGSUSED */
  218. int
  219. delgmode(f, n)    /* prompt and delete a global editor mode */
  220. int f, n;    /* default and argument */
  221. {
  222.     return adjustmode(FALSE, TRUE);
  223. }
  224.  
  225.  
  226. int
  227. adjustmode(kind, global)    /* change the editor mode status */
  228. int kind;    /* true = set,        false = delete */
  229. int global; /* true = global flag,    false = current buffer flag */
  230. {
  231.     register char *scan;        /* scanning pointer to convert prompt */
  232.     register int i;         /* loop index */
  233.     register s;        /* error return on input */
  234. #if COLOR
  235.     register int uflag;     /* was modename uppercase?    */
  236. #endif
  237.     char prompt[50];    /* string to prompt user with */
  238.     static char cbuf[NPAT];     /* buffer to receive mode name into */
  239.  
  240.     /* build the proper prompt string */
  241.     if (global)
  242.         strcpy(prompt,"Global value: ");
  243.     else
  244.         strcpy(prompt,"Local value: ");
  245.  
  246.  
  247.     /* prompt the user and get an answer */
  248.  
  249.     s = mlreply(prompt, cbuf, NPAT - 1);
  250.     if (s != TRUE)
  251.         return(s);
  252.  
  253.     /* make it lowercase */
  254.  
  255.     scan = cbuf;
  256. #if COLOR
  257.     uflag = isupper(*scan);
  258. #endif
  259.     while (*scan != '\0' && *scan != '=') {
  260.         if (isupper(*scan))
  261.             *scan = tolower(*scan);
  262.         scan++;
  263.     }
  264.  
  265.     if (scan == cbuf) { /* no string */
  266.         s = FALSE;
  267.         goto errout;
  268.     }
  269.  
  270.     if (!strcmp(cbuf,"all")) {
  271.         return listmodes(FALSE,1);
  272.     }
  273.  
  274.     /* colors should become another kind of value, i.e. VAL_TYPE_COLOR,
  275.         and then this could be handled generically in adjvalueset() */
  276.     /* test it first against the colors we know */
  277.     for (i=0; i<NCOLORS; i++) {
  278.         if (strcmp(cbuf, cname[i]) == 0) {
  279.             /* finding the match, we set the color */
  280. #if COLOR
  281.             if (uflag)
  282.                 if (global)
  283.                     gfcolor = i;
  284.                 else if (curwp)
  285.                     set_w_val(curwp, WVAL_FCOLOR, i);
  286.             else
  287.                 if (global)
  288.                     gbcolor = i;
  289.                 else if (curwp)
  290.                     set_w_val(curwp, WVAL_BCOLOR, i);
  291.  
  292.             if (curwp)
  293.                 curwp->w_flag |= WFCOLR;
  294. #endif
  295.             mlerase();
  296.             return(TRUE);
  297.         }
  298.     }
  299.  
  300.     s = adjvalueset(cbuf, kind, b_valuenames,
  301.         global ? global_b_values.bv : curbp->b_values.bv );
  302.     if (s == TRUE)
  303.         goto success;
  304.     s = adjvalueset(cbuf, kind, w_valuenames,
  305.         global ? global_w_values.wv : curwp->w_values.wv );
  306.     if (s == TRUE)
  307.         goto success;
  308.     if (s == FALSE) {
  309.     errout:
  310.         mlforce("[Not a legal set option: \"%s\"]", cbuf);
  311.     }
  312.     return s;
  313.  
  314. success:
  315.     if (global) {
  316.         register WINDOW *wp;
  317.         for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
  318.             wp->w_flag |= WFHARD|WFMODE;
  319.     } else {
  320.         curwp->w_flag |= WFHARD|WFMODE;
  321.     }
  322.  
  323.     /* if the settings are up, redisplay them */
  324.     if (bfind("[Settings]", NO_CREAT, BFSCRTCH))
  325.         listmodes(FALSE,1);
  326.  
  327.     refresh(FALSE,1);
  328.     mlerase();    /* erase the junk */
  329.  
  330.     return TRUE;
  331. }
  332.  
  333. int
  334. adjvalueset(cp, kind, names, values)
  335. char *cp;
  336. int kind;
  337. struct VALNAMES *names;
  338. register struct VAL *values;
  339. {
  340.     char respbuf[NFILEN];
  341.     char *rp;
  342.     char *equp;
  343.     int rplen = 0;
  344.     int no;
  345.     int nval, s;
  346.  
  347.     no = 0;
  348.     if (strncmp(cp, "no", 2) == 0) {
  349.         kind = !kind;
  350.         no = 2;
  351.     }
  352.     equp = rp = strchr(cp, '=');
  353.     if (rp) {
  354.         *rp = '\0';
  355.         rp++;
  356.         rplen = strlen(rp);
  357.         if (!rplen)
  358.             goto err;
  359.     }
  360.     while(names->name != NULL) {
  361.         if (strncmp(cp + no, names->name, strlen(cp) - no) == 0)
  362.             break;
  363.         if (strncmp(cp + no, names->shortname, strlen(cp) - no) == 0)
  364.             break;
  365.         names++;
  366.         values++;
  367.     }
  368.     if (names->name == NULL) {
  369.     err:    if (equp)
  370.             *equp = '=';
  371.         return FALSE;
  372.     }
  373.  
  374.     /* we matched a name -- get the value */
  375.     switch(names->type) {
  376.     case VALTYPE_BOOL:
  377.         if (rp) {
  378.             if (!strncmp(rp,"no",rplen) ||
  379.                     !strncmp(rp,"false",rplen))
  380.                 kind = FALSE;
  381.             else if (!strncmp(rp,"yes",rplen) ||
  382.                     !strncmp(rp,"true",rplen))
  383.                 kind = TRUE;
  384.             else
  385.                 goto err;
  386.         }
  387.         values->vp = &(values->v);
  388.         values->vp->i = kind;
  389.         break;
  390.  
  391.     case VALTYPE_INT:
  392.     case VALTYPE_STRING:
  393.     case VALTYPE_REGEX:
  394.         if (no) goto err;
  395.         if (!rp) {
  396.             respbuf[0] = '\0';
  397.             if (names->type != VALTYPE_REGEX)
  398.                 s = mlreply("New value: ", respbuf, NFILEN - 1);
  399.             else
  400.                 s = kbd_string("New pattern: ", respbuf,
  401.                     NFILEN - 1, '\n', NO_EXPAND, FALSE);
  402.             if (s != TRUE) {
  403.                 if (equp)
  404.                     *equp = '=';
  405.                 return s;
  406.             }
  407.             rp = respbuf;
  408.             rplen = strlen(rp);
  409.             if (!rplen)
  410.                 goto err;
  411.         }
  412.         values->vp = &(values->v);
  413.         switch (names->type) {
  414.         case VALTYPE_INT:
  415.             nval = 0;
  416.             while (isdigit(*rp))
  417.                 nval = (nval * 10) + (*rp++ - '0');
  418.             values->vp->i = nval;
  419.             break;
  420.         case VALTYPE_STRING:
  421.             if (values->vp->p)
  422.                 free(values->vp->p);
  423.             values->vp->p = strmalloc(rp);
  424.             break;
  425.         case VALTYPE_REGEX:
  426.             if (!values->vp->r) {
  427.                 values->vp->r = (struct regexval *)malloc(
  428.                                             sizeof (struct regexval));
  429.             } else {
  430.                 if (values->vp->r->pat)
  431.                     free(values->vp->r->pat);
  432.                 if (values->vp->r->reg)
  433.                     free((char *)values->vp->r->reg);
  434.             }
  435.             values->vp->r->pat = strmalloc(rp);
  436.             values->vp->r->reg = regcomp(values->vp->r->pat, TRUE);
  437.             break;
  438.         }
  439.         break;
  440.  
  441.     default:
  442.         mlforce("BUG: bad type %s %d",names->name,names->type);
  443.         if (equp)
  444.             *equp = '=';
  445.         return FALSE;
  446.     }
  447.     return TRUE;
  448. }
  449.  
  450.