home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Shells / zsh-3.0.5-MIHS / src / Src / zle_move.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-03  |  6.6 KB  |  473 lines

  1. /*
  2.  * $Id: zle_move.c,v 2.7 1996/10/15 20:16:35 hzoli Exp $
  3.  *
  4.  * zle_move.c - editor movement
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * Copyright (c) 1992-1996 Paul Falstad
  9.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and to distribute modified versions of this software for any
  14.  * purpose, provided that the above copyright notice and the following
  15.  * two paragraphs appear in all copies of this software.
  16.  *
  17.  * In no event shall Paul Falstad or the Zsh Development Group be liable
  18.  * to any party for direct, indirect, special, incidental, or consequential
  19.  * damages arising out of the use of this software and its documentation,
  20.  * even if Paul Falstad and the Zsh Development Group have been advised of
  21.  * the possibility of such damage.
  22.  *
  23.  * Paul Falstad and the Zsh Development Group specifically disclaim any
  24.  * warranties, including, but not limited to, the implied warranties of
  25.  * merchantability and fitness for a particular purpose.  The software
  26.  * provided hereunder is on an "as is" basis, and Paul Falstad and the
  27.  * Zsh Development Group have no obligation to provide maintenance,
  28.  * support, updates, enhancements, or modifications.
  29.  *
  30.  */
  31.  
  32. #define ZLE
  33. #include "zsh.h"
  34.  
  35. static vimarkcs[27], vimarkline[27];
  36.  
  37. /**/
  38. void
  39. beginningofline(void)
  40. {
  41.     if (zmult < 0) {
  42.     zmult = -zmult;
  43.     endofline();
  44.     return;
  45.     }
  46.     while (zmult--) {
  47.     if (cs == 0)
  48.         return;
  49.     if (line[cs - 1] == '\n')
  50.         if (!--cs)
  51.         return;
  52.     while (cs && line[cs - 1] != '\n')
  53.         cs--;
  54.     }
  55. }
  56.  
  57. /**/
  58. void
  59. endofline(void)
  60. {
  61.     if (zmult < 0) {
  62.     zmult = -zmult;
  63.     beginningofline();
  64.     return;
  65.     }
  66.     while (zmult--) {
  67.     if (cs >= ll) {
  68.         cs = ll;
  69.         return;
  70.     }
  71.     if (line[cs] == '\n')
  72.         if (++cs == ll)
  73.         return;
  74.     while (cs != ll && line[cs] != '\n')
  75.         cs++;
  76.     }
  77. }
  78.  
  79. /**/
  80. void
  81. beginningoflinehist(void)
  82. {
  83.     if (zmult < 0) {
  84.     zmult = -zmult;
  85.     endoflinehist();
  86.     return;
  87.     }
  88.     while (zmult) {
  89.     if (cs == 0)
  90.         break;
  91.     if (line[cs - 1] == '\n')
  92.         if (!--cs)
  93.         break;
  94.     while (cs && line[cs - 1] != '\n')
  95.         cs--;
  96.     zmult--;
  97.     }
  98.     if (zmult) {
  99.     uphistory();
  100.     cs = 0;
  101.     }
  102. }
  103.  
  104. /**/
  105. void
  106. endoflinehist(void)
  107. {
  108.     if (zmult < 0) {
  109.     zmult = -zmult;
  110.     beginningoflinehist();
  111.     return;
  112.     }
  113.     while (zmult) {
  114.     if (cs >= ll) {
  115.         cs = ll;
  116.         break;
  117.     }
  118.     if (line[cs] == '\n')
  119.         if (++cs == ll)
  120.         break;
  121.     while (cs != ll && line[cs] != '\n')
  122.         cs++;
  123.     zmult--;
  124.     }
  125.     if (zmult)
  126.     downhistory();
  127. }
  128.  
  129. /**/
  130. void
  131. forwardchar(void)
  132. {
  133.     cs += zmult;
  134.     if (cs > ll)
  135.     cs = ll;
  136.     if (cs < 0)
  137.     cs = 0;
  138. }
  139.  
  140. /**/
  141. void
  142. backwardchar(void)
  143. {
  144.     cs -= zmult;
  145.     if (cs > ll)
  146.     cs = ll;
  147.     if (cs < 0)
  148.     cs = 0;
  149. }
  150.  
  151. /**/
  152. void
  153. setmarkcommand(void)
  154. {
  155.     mark = cs;
  156. }
  157.  
  158. /**/
  159. void
  160. exchangepointandmark(void)
  161. {
  162.     int x;
  163.  
  164.     x = mark;
  165.     mark = cs;
  166.     cs = x;
  167.     if (cs > ll)
  168.     cs = ll;
  169. }
  170.  
  171. /**/
  172. void
  173. vigotocolumn(void)
  174. {
  175.     int x, y;
  176.  
  177.     if (zmult > 0)
  178.     zmult--;
  179.     findline(&x, &y);
  180.     if (zmult >= 0)
  181.     cs = x + zmult;
  182.     else
  183.     cs = y + zmult;
  184.     if (cs > y)
  185.     cs = y;
  186.     if (cs < x)
  187.     cs = x;
  188. }
  189.  
  190. /**/
  191. void
  192. vimatchbracket(void)
  193. {
  194.     int ocs = cs, dir, ct;
  195.     unsigned char oth, me;
  196.  
  197.   otog:
  198.     if (cs == ll || line[cs] == '\n') {
  199.     feep();
  200.     cs = ocs;
  201.     return;
  202.     }
  203.     switch (me = line[cs]) {
  204.     case '{':
  205.     dir = 1;
  206.     oth = '}';
  207.     break;
  208.     case /*{*/ '}':
  209.     virangeflag = -virangeflag;
  210.     dir = -1;
  211.     oth = '{'; /*}*/
  212.     break;
  213.     case '(':
  214.     dir = 1;
  215.     oth = ')';
  216.     break;
  217.     case ')':
  218.     virangeflag = -virangeflag;
  219.     dir = -1;
  220.     oth = '(';
  221.     break;
  222.     case '[':
  223.     dir = 1;
  224.     oth = ']';
  225.     break;
  226.     case ']':
  227.     virangeflag = -virangeflag;
  228.     dir = -1;
  229.     oth = '[';
  230.     break;
  231.     default:
  232.     cs++;
  233.     goto otog;
  234.     }
  235.     ct = 1;
  236.     while (cs >= 0 && cs < ll && ct) {
  237.     cs += dir;
  238.     if (line[cs] == oth)
  239.         ct--;
  240.     else if (line[cs] == me)
  241.         ct++;
  242.     }
  243.     if (cs < 0 || cs >= ll) {
  244.     feep();
  245.     cs = ocs;
  246.     } else if(dir > 0 && virangeflag)
  247.     cs++;
  248. }
  249.  
  250. /**/
  251. void
  252. viforwardchar(void)
  253. {
  254.     int lim = findeol() - (bindtab == altbindtab);
  255.     if (zmult < 0) {
  256.     zmult = -zmult;
  257.     vibackwardchar();
  258.     return;
  259.     }
  260.     if (cs >= lim) {
  261.     feep();
  262.     return;
  263.     }
  264.     while (zmult-- && cs < lim)
  265.     cs++;
  266. }
  267.  
  268. /**/
  269. void
  270. vibackwardchar(void)
  271. {
  272.     if (zmult < 0) {
  273.     zmult = -zmult;
  274.     viforwardchar();
  275.     return;
  276.     }
  277.     if (cs == findbol()) {
  278.     feep();
  279.     return;
  280.     }
  281.     while (zmult--) {
  282.     cs--;
  283.     if (cs < 0 || line[cs] == '\n') {
  284.         cs++;
  285.         break;
  286.     }
  287.     }
  288. }
  289.  
  290. /**/
  291. void
  292. viendofline(void)
  293. {
  294.     int oldcs = cs;
  295.  
  296.     if (zmult<1) {
  297.     feep();
  298.     return;
  299.     }
  300.     while(zmult--) {
  301.     if (cs > ll) {
  302.         cs = oldcs;
  303.         feep();
  304.         return;
  305.     }
  306.     cs = findeol() + 1;
  307.     }
  308.     cs--;
  309. }
  310.  
  311. /**/
  312. void
  313. vibeginningofline(void)
  314. {
  315.     cs = findbol();
  316. }
  317.  
  318. static int vfindchar, vfinddir, tailadd;
  319.  
  320. /**/
  321. void
  322. vifindnextchar(void)
  323. {
  324.     if ((vfindchar = vigetkey()) != -1) {
  325.     vfinddir = 1;
  326.     tailadd = 0;
  327.     virepeatfind();
  328.     }
  329. }
  330.  
  331. /**/
  332. void
  333. vifindprevchar(void)
  334. {
  335.     if ((vfindchar = vigetkey()) != -1) {
  336.     vfinddir = -1;
  337.     tailadd = 0;
  338.     virepeatfind();
  339.     }
  340. }
  341.  
  342. /**/
  343. void
  344. vifindnextcharskip(void)
  345. {
  346.     if ((vfindchar = vigetkey()) != -1) {
  347.     vfinddir = 1;
  348.     tailadd = -1;
  349.     virepeatfind();
  350.     }
  351. }
  352.  
  353. /**/
  354. void
  355. vifindprevcharskip(void)
  356. {
  357.     if ((vfindchar = vigetkey()) != -1) {
  358.     vfinddir = -1;
  359.     tailadd = 1;
  360.     virepeatfind();
  361.     }
  362. }
  363.  
  364. /**/
  365. void
  366. virepeatfind(void)
  367. {
  368.     int ocs = cs;
  369.  
  370.     if (!vfinddir) {
  371.     feep();
  372.     return;
  373.     }
  374.     if (zmult < 0) {
  375.     zmult = -zmult;
  376.     virevrepeatfind();
  377.     return;
  378.     }
  379.     while (zmult--) {
  380.     do
  381.         cs += vfinddir;
  382.     while (cs >= 0 && cs < ll && line[cs] != vfindchar && line[cs] != '\n');
  383.     if (cs < 0 || cs >= ll || line[cs] == '\n') {
  384.         feep();
  385.         cs = ocs;
  386.         return;
  387.     }
  388.     }
  389.     cs += tailadd;
  390.     if (vfinddir == 1 && virangeflag)
  391.     cs++;
  392. }
  393.  
  394. /**/
  395. void
  396. virevrepeatfind(void)
  397. {
  398.     if (zmult < 0) {
  399.     zmult = -zmult;
  400.     virepeatfind();
  401.     return;
  402.     }
  403.     vfinddir = -vfinddir;
  404.     virepeatfind();
  405.     vfinddir = -vfinddir;
  406. }
  407.  
  408. /**/
  409. void
  410. vifirstnonblank(void)
  411. {
  412.     cs = findbol();
  413.     while (cs != ll && iblank(line[cs]))
  414.     cs++;
  415. }
  416.  
  417. /**/
  418. void
  419. visetmark(void)
  420. {
  421.     int ch;
  422.  
  423.     ch = getkey(0);
  424.     if (ch < 'a' || ch > 'z') {
  425.     feep();
  426.     return;
  427.     }
  428.     ch -= 'a';
  429.     vimarkcs[ch] = cs;
  430.     vimarkline[ch] = histline;
  431. }
  432.  
  433. /**/
  434. void
  435. vigotomark(void)
  436. {
  437.     int ch;
  438.  
  439.     ch = getkey(0);
  440.     if (ch == c)
  441.     ch = 26;
  442.     else {
  443.     if (ch < 'a' || ch > 'z') {
  444.         feep();
  445.         return;
  446.     }
  447.     ch -= 'a';
  448.     }
  449.     if (!vimarkline[ch]) {
  450.     feep();
  451.     return;
  452.     }
  453.     if (curhist != vimarkline[ch]) {
  454.     zmult = vimarkline[ch];
  455.     gotmult = 1;
  456.     lastcmd |= ZLE_ARG;
  457.     vifetchhistory();
  458.     if (histline != vimarkline[ch])
  459.         return;
  460.     }
  461.     cs = vimarkcs[ch];
  462.     if (cs > ll)
  463.     cs = ll;
  464. }
  465.  
  466. /**/
  467. void
  468. vigotomarkline(void)
  469. {
  470.     vigotomark();
  471.     vifirstnonblank();
  472. }
  473.