home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / zsh / Source / src / zle_utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-07  |  4.8 KB  |  257 lines

  1.  
  2. /*
  3.  *
  4.  * zle_utils.c - miscellaneous line editor utilities
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * This software is Copyright 1992 by Paul Falstad
  9.  *
  10.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  11.  * use this software as long as: there is no monetary profit gained
  12.  * specifically from the use or reproduction of this software, it is not
  13.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  14.  * included prominently in any copy made.
  15.  *
  16.  * The author make no claims as to the fitness or correctness of this software
  17.  * for any use whatsoever, and it is provided as is. Any use of this software
  18.  * is at the user's own risk.
  19.  *
  20.  */
  21.  
  22. #define ZLE
  23. #include "zsh.h"
  24.  
  25. /* make sure that the line buffer has at least sz chars */
  26.  
  27. void sizeline(sz)        /**/
  28. int sz;
  29. {
  30.     while (sz > linesz)
  31.     line = (unsigned char *)realloc(line, (linesz *= 4) + 1);
  32. }
  33.  
  34. /* insert space for ct chars at cursor position */
  35.  
  36. void spaceinline(ct)        /**/
  37. int ct;
  38. {
  39.     int i;
  40.  
  41.     while (ct + ll > linesz)
  42.     line = (unsigned char *)realloc(line, (linesz *= 4) + 1);
  43.     for (i = ll; i >= cs; i--)
  44.     line[i + ct] = line[i];
  45.     ll += ct;
  46.     line[ll] = '\0';
  47. }
  48.  
  49. void backkill(ct, dir)        /**/
  50. int ct;
  51. int dir;
  52. {
  53.     int i = (cs -= ct);
  54.  
  55.     cut(i, ct, dir);
  56.     while ((line[i] = line[i + ct]))
  57.     i++;
  58.     ll -= ct;
  59. }
  60.  
  61. void forekill(ct, dir)        /**/
  62. int ct;
  63. int dir;
  64. {
  65.     int i = cs;
  66.  
  67.     cut(i, ct, dir);
  68.     while ((line[i] = line[i + ct]))
  69.     i++;
  70.     ll -= ct;
  71. }
  72.  
  73. extern owrite;
  74.  
  75. void cut(i, ct, dir)        /**/
  76. int i;
  77. int ct;
  78. int dir;
  79. {
  80.     if (vibufspec) {
  81.     if (owrite || !vibuf[vibufspec]) {
  82.         if (vibuf[vibufspec])
  83.         free(vibuf[vibufspec]);
  84.         vibuf[vibufspec] = (char *)zalloc(ct + 1);
  85.         ztrncpy(vibuf[vibufspec], UTOSCP(line + i), ct);
  86.     } else {
  87.         int len = strlen(vibuf[vibufspec]);
  88.  
  89.         vibuf[vibufspec] = realloc(vibuf[vibufspec], ct + len);
  90.         ztrncpy(vibuf[vibufspec] + len, UTOSCP(line + i), ct);
  91.     }
  92.     vibufspec = 0;
  93.     return;
  94.     }
  95.     if (!cutbuf)
  96.     cutbuf = ztrdup("");
  97.     else if (!(lastcmd & ZLE_KILL)) {
  98.     kringnum = (kringnum + 1) & (KRINGCT - 1);
  99.     if (kring[kringnum])
  100.         free(kring[kringnum]);
  101.     kring[kringnum] = cutbuf;
  102.     cutbuf = ztrdup("");
  103.     }
  104.     if (dir) {
  105.     char *s = (char *)zalloc(strlen(cutbuf) + ct + 1);
  106.  
  107.     strncpy(s, (char *)line + i, ct);
  108.     strcpy(s + ct, cutbuf);
  109.     free(cutbuf);
  110.     cutbuf = s;
  111.     } else {
  112.     int x;
  113.  
  114.     cutbuf = realloc(cutbuf, (x = strlen(cutbuf)) + ct + 1);
  115.     ztrncpy(cutbuf + x, UTOSCP(line + i), ct);
  116.     }
  117. }
  118.  
  119. void backdel(ct)        /**/
  120. int ct;
  121. {
  122.     int i = (cs -= ct);
  123.  
  124.     while ((line[i] = line[i + ct]))
  125.     i++;
  126.     ll -= ct;
  127. }
  128.  
  129. void foredel(ct)        /**/
  130. int ct;
  131. {
  132.     int i = cs;
  133.  
  134.     while ((line[i] = line[i + ct]))
  135.     i++;
  136.     ll -= ct;
  137. }
  138.  
  139. void setline(s)            /**/
  140. char *s;
  141. {
  142.     sizeline(strlen(s));
  143.     strcpy((char *)line, s);
  144.     cs = ll = strlen(s);
  145.     if (cs && bindtab == altbindtab)
  146.     cs--;
  147. }
  148.  
  149. void sethistline(s)        /**/
  150. unsigned char *s;
  151. {
  152.     setline(UTOSCP(s));
  153.     for (s = line; *s; s++)
  154.     if (*s == STOUC(HISTSPACE))
  155.         *s = ' ';
  156. }
  157.  
  158. int findbol()
  159. {                /**/
  160.     int x = cs;
  161.  
  162.     while (x > 0 && line[x - 1] != '\n')
  163.     x--;
  164.     return x;
  165. }
  166.  
  167. int findeol()
  168. {                /**/
  169.     int x = cs;
  170.  
  171.     while (x != ll && line[x] != '\n')
  172.     x++;
  173.     return x;
  174. }
  175.  
  176. void findline(a, b)        /**/
  177. int *a;
  178. int *b;
  179. {
  180.     *a = findbol();
  181.     *b = findeol();
  182. }
  183.  
  184. static int lastlinelen;
  185.  
  186. void initundo()
  187. {                /**/
  188.     int t0;
  189.  
  190.     for (t0 = 0; t0 != UNDOCT; t0++)
  191.     undos[t0].change = NULL;
  192.     undoct = 0;
  193.     lastline = (unsigned char *)zalloc(lastlinelen = (ll + 1 < 32) ? 32 : ll + 1);
  194.     strcpy((char *)lastline, (char *)line);
  195.     lastcs = cs;
  196. }
  197.  
  198. void addundo()
  199. {                /**/
  200.     int pf, sf;
  201.     unsigned char *s, *s2, *t, *t2;
  202.     struct undoent *ue;
  203.  
  204.     for (s = line, t = lastline; *s && *s == *t; s++, t++);
  205.     if (!*s && !*t)
  206.     return;
  207.     pf = s - line;
  208.     for (s2 = (unsigned char *)line + strlen((char *)line),
  209.      t2 = lastline + strlen((char *)lastline);
  210.      s2 > s && t > t2 && s2[-1] == t2[-1]; s2--, t2--);
  211.     sf = strlen((char *)s2);
  212.     ue = undos + (undoct = (UNDOCT - 1) & (undoct + 1));
  213.     ue->pref = pf;
  214.     ue->suff = sf;
  215.     ue->len = t2 - t;
  216.     ue->cs = lastcs;
  217.     strncpy(ue->change = (char *)halloc(ue->len), (char *)t, ue->len);
  218.     while (ll + 1 > lastlinelen) {
  219.     free(lastline);
  220.     lastline = (unsigned char *)zalloc(lastlinelen *= 2);
  221.     }
  222.     strcpy((char *)lastline, (char *)line);
  223.     lastcs = cs;
  224. }
  225.  
  226. int hstrncmp(s, t, len)        /**/
  227. char *s;
  228. char *t;
  229. int len;
  230. {
  231.     while (len && *s && (*s == *t || (*s == ' ' && *t == HISTSPACE) ||
  232.              (*s == HISTSPACE && *t == ' ')))
  233.     s++, t++, len--;
  234.     return len;
  235. }
  236.  
  237. int hstrcmp(s, t)        /**/
  238. char *s;
  239. char *t;
  240. {
  241.     while (*s && (*s == *t || (*s == ' ' && *t == HISTSPACE) ||
  242.           (*s == HISTSPACE && *t == ' ')))
  243.     s++, t++;
  244.     return !(*s == '\0' && *t == '\0');
  245. }
  246.  
  247. char *hstrnstr(s, t, len)    /**/
  248. char *s;
  249. char *t;
  250. int len;
  251. {
  252.     for (; *s; s++)
  253.     if (!hstrncmp(t, s, len))
  254.         return s;
  255.     return NULL;
  256. }
  257.