home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / z / zsh220.zip / zsh2.2 / src / zle_vi.c < prev    next >
C/C++ Source or Header  |  1992-05-07  |  5KB  |  311 lines

  1. /*
  2.  *
  3.  * zle_vi.c - vi-specific functions
  4.  *
  5.  * This file is part of zsh, the Z shell.
  6.  *
  7.  * This software is Copyright 1992 by Paul Falstad
  8.  *
  9.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  10.  * use this software as long as: there is no monetary profit gained
  11.  * specifically from the use or reproduction of this software, it is not
  12.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  13.  * included prominently in any copy made. 
  14.  *
  15.  * The author make no claims as to the fitness or correctness of this software
  16.  * for any use whatsoever, and it is provided as is. Any use of this software
  17.  * is at the user's own risk. 
  18.  *
  19.  */
  20.  
  21. #define ZLE
  22. #include "zsh.h"
  23.  
  24.  
  25. static void startvichange(im)
  26. int im;
  27. {
  28.     insmode = im;
  29.     if (vichgbuf) free(vichgbuf);
  30.     vichgbuf = zalloc(vichgbufsz = 16);
  31.     vichgbuf[0] = c;
  32.     vichgbufptr = 1;
  33.     vichgflag = 1;
  34.     viinsbegin = cs;
  35. }
  36.  
  37. static void startvitext(im)
  38. {
  39.     startvichange(im);
  40.     bindtab = mainbindtab;
  41.     undoing = 0;
  42. }
  43.  
  44. int vigetkey() /**/
  45. {
  46. int ch;
  47.  
  48.     if ((ch = getkey(0)) == -1)
  49.         return 0;
  50.     if (ch == 22)
  51.         {
  52.         if ((ch = getkey(0)) == -1)
  53.             return 0;
  54.         return ch;
  55.         }
  56.     else if (ch == 27)
  57.         return 0;
  58.     return ch;
  59. }
  60.  
  61. int getvirange(wf) /**/
  62. int wf;
  63. {
  64. int k2,t0,startline,endline;
  65.  
  66.     startline = findbol();
  67.     endline = findeol();
  68.     for (;;) {
  69.         k2 = getkeycmd();
  70.         if (k2 == -1) {
  71.             feep();
  72.             return -1;
  73.         }
  74.         if (zlecmds[k2].flags & ZLE_ARG)
  75.             (*zlecmds[k2].func)();
  76.         else
  77.             break;
  78.     }
  79.     if (k2 == bindk) {
  80.         findline(&cs,&t0);
  81.         return (t0 == ll) ? t0 : t0+1;
  82.     }
  83.     if (!(zlecmds[k2].flags & ZLE_MOVEMENT)) {
  84.         feep();
  85.         return -1;
  86.     }
  87.     t0 = cs;
  88.  
  89.     virangeflag = 1;
  90.     wordflag = wf;
  91.     (*zlecmds[k2].func)();
  92.     wordflag = virangeflag = 0;
  93.     if (cs == t0) {
  94.         feep();
  95.         return -1;
  96.     }
  97.     if (startline != findbol()) {
  98.         if (zlecmds[k2].flags & ZLE_LINEMOVE) {
  99.             if (cs < t0) {
  100.                 cs = startline;
  101.                 t0 = findeol()+1;
  102.             } else {
  103.                 t0 = startline;
  104.                 cs = findeol()+1;
  105.             }
  106.         } else {
  107.             if (cs < startline) cs = startline;
  108.             else if (cs >= endline) cs = endline-1;
  109.         }
  110.     }
  111.     if (cs > t0) {
  112.         k2 = cs;
  113.         cs = t0;
  114.         t0 = k2;
  115.     }
  116.     return t0;
  117. }
  118.  
  119. void viaddnext() /**/
  120. {
  121.     if (cs != ll)
  122.         cs++;
  123.     startvitext(1);
  124. }
  125.  
  126. void viaddeol() /**/
  127. {
  128.     cs = findeol();
  129.     startvitext(1);
  130. }
  131.  
  132. void viinsert() /**/
  133. {
  134.     startvitext(1);
  135. }
  136.  
  137. void viinsertbol() /**/
  138. {
  139.     cs = findbol();
  140.     startvitext(1);
  141. }
  142.  
  143. void videlete() /**/
  144. {
  145. int c2;
  146.  
  147.     startvichange(1);
  148.     if ((c2 = getvirange(0)) == -1)
  149.         { vichgflag = 0; return; }
  150.     forekill(c2-cs,0);
  151.     vichgflag = 0;
  152. }
  153.  
  154. void vichange() /**/
  155. {
  156. int c2;
  157.  
  158.     startvichange(1);
  159.     if ((c2 = getvirange(1)) == -1)
  160.         { vichgflag = 0; return; }
  161.     forekill(c2-cs,0);
  162.     bindtab = mainbindtab;
  163.     undoing = 0;
  164. }
  165.  
  166. void visubstitute() /**/
  167. {
  168.     if (mult < 0) return;
  169.     if (findeol()-cs < mult) mult = findeol()-cs;
  170.     if (mult) {
  171.         foredel(mult);
  172.         startvitext(1);
  173.     }
  174. }
  175.  
  176. void vichangeeol() /**/
  177. {
  178.     killline();
  179.     startvitext(1);
  180. }
  181.  
  182. void vichangewholeline() /**/
  183. {
  184. int cq;
  185.  
  186.     findline(&cs,&cq);
  187.     foredel(cq-cs);
  188.     startvitext(1);
  189. }
  190.  
  191. void viyank() /**/
  192. {
  193. int c2;
  194.  
  195.     if ((c2 = getvirange(0)) == -1) return;
  196.     cut(cs,c2-cs,0);
  197. }
  198.  
  199. void viyankeol() /**/
  200. {
  201. int x = findeol();
  202.  
  203.     if (x == cs)
  204.         feep();
  205.     else
  206.         cut(cs,x-cs,0);
  207. }
  208.  
  209. void vireplace() /**/
  210. {
  211.     startvitext(0);
  212. }
  213.  
  214. void vireplacechars() /**/
  215. {
  216. int ch;
  217.  
  218.     if (mult < 0) return;
  219.     if (mult+cs > ll) {
  220.         feep();
  221.         return;
  222.     }
  223.     startvichange(1);
  224.     if (ch = vigetkey()) while (mult--) line[cs++] = ch;
  225.     vichgflag = 0;
  226.     cs--;
  227. }
  228.  
  229. void vicmdmode() /**/
  230. {
  231.     bindtab = altbindtab;
  232.     if (cs) cs--;
  233.     undoing = 1;
  234.     if (vichgflag) vichgflag = 0;
  235. }
  236.  
  237. void viopenlinebelow() /**/
  238. {
  239.     cs = findeol();
  240.     spaceinline(1);
  241.     line[cs++] = '\n';
  242.     startvitext(1);
  243. }
  244.  
  245. void viopenlineabove() /**/
  246. {
  247.     cs = findbol();
  248.     spaceinline(1);
  249.     line[cs] = '\n';
  250.     startvitext(1);
  251. }
  252.  
  253. void vioperswapcase() /**/
  254. {
  255. int c2;
  256.  
  257.     if ((c2 = getvirange(0)) == -1)
  258.         return;
  259.     while (cs < c2)
  260.         {
  261.         int ch = line[cs];
  262.  
  263.         if (islower(ch))
  264.             ch = tuupper(ch);
  265.         else if (isupper(ch))
  266.             ch = tulower(ch);
  267.         line[cs++] = ch;
  268.         }
  269. }
  270.  
  271. void virepeatchange() /**/
  272. {
  273.     if (!vichgbuf || bindtab == mainbindtab || vichgflag) feep();
  274.     else ungetkeys(vichgbuf,vichgbufptr);
  275. }
  276.  
  277. void viindent() /**/
  278. {
  279. int c2,endcs,t0,rmult;
  280.  
  281.     if (mult < 0) { mult = -mult; viunindent(); return; }
  282.     rmult = mult;
  283.     if ((c2 = getvirange(0)) == -1)
  284.         return;
  285.     if (cs != findbol()) { feep(); return; }
  286.     endcs = cs+rmult;
  287.     while (cs < c2) {
  288.         spaceinline(rmult);
  289.         for (t0 = 0; t0 != rmult; t0++) line[cs++] = '\t';
  290.         cs = findeol()+1;
  291.     }
  292.     cs = endcs;
  293. }
  294.  
  295. void viunindent() /**/
  296. {
  297. int c2,endcs,t0,rmult;
  298.  
  299.     rmult = mult;
  300.     if (mult < 0) { mult = -mult; viindent(); return; }
  301.     if ((c2 = getvirange(0)) == -1)
  302.         return;
  303.     if (cs != findbol()) { feep(); return; }
  304.     endcs = cs;
  305.     while (cs < c2) {
  306.         for (t0 = 0; t0 != rmult && line[cs] == '\t'; t0++) foredel(1);
  307.         cs = findeol()+1;
  308.     }
  309.     cs = endcs;
  310. }
  311.