home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / e / elv17src.zip / VI.C < prev    next >
C/C++ Source or Header  |  1992-12-30  |  23KB  |  844 lines

  1. /* vi.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    Beaverton, OR 97005
  6.  *    kirkenda@cs.pdx.edu
  7.  */
  8.  
  9.  
  10. #include "config.h"
  11. #include "ctype.h"
  12. #include "vi.h"
  13.  
  14.  
  15.  
  16. /* This array describes what each key does */
  17. #define NO_FUNC        (MARK (*)())0
  18.  
  19. #define NO_ARGS        0
  20. #define CURSOR        1
  21. #define CURSOR_CNT_KEY    2
  22. #define CURSOR_MOVED    3
  23. #define CURSOR_EOL    4
  24. #define ZERO        5
  25. #define DIGIT        6
  26. #define CURSOR_TEXT    7
  27. #define KEYWORD        8
  28. #define ARGSMASK    0x0f
  29. #define    C_C_K_REP1    (CURSOR_CNT_KEY | 0x10)
  30. #define C_C_K_CUT    (CURSOR_CNT_KEY | 0x20)
  31. #define C_C_K_MARK    (CURSOR_CNT_KEY | 0x30)
  32. #define C_C_K_CHAR    (CURSOR_CNT_KEY | 0x40)
  33. #ifndef NO_SHOWMODE
  34. static int keymodes[] = {0, WHEN_REP1, WHEN_CUT, WHEN_MARK, WHEN_CHAR};
  35. # define KEYMODE(args) (keymodes[(args) >> 4])
  36. #else
  37. # define KEYMODE(args) 0
  38. #endif
  39.  
  40. static struct keystru
  41. {
  42.     MARK    (*func)();    /* the function to run */
  43.     uchar    args;        /* description of the args needed */
  44. #ifndef NO_VISIBLE
  45.     short    flags;
  46. #else
  47.     uchar    flags;        /* other stuff */
  48. #endif
  49. }
  50.     vikeys[] =
  51. {
  52. /* NUL not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  53. #ifndef NO_EXTENSIONS
  54. /* ^A  find cursor word */    {m_wsrch,    KEYWORD,    MVMT|NREL|VIZ},
  55. #else
  56. /* ^A  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  57. #endif
  58. /* ^B  page backward    */    {m_scroll,    CURSOR,        FRNT|VIZ},
  59. /* ^C  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  60. /* ^D  scroll dn 1/2page*/    {m_scroll,    CURSOR,        NCOL|VIZ},
  61. /* ^E  scroll up    */    {m_scroll,    CURSOR,        NCOL|VIZ},
  62. /* ^F  page forward    */    {m_scroll,    CURSOR,        FRNT|VIZ},
  63. /* ^G  show file status    */    {v_status,    NO_ARGS,     NO_FLAGS},
  64. /* ^H  move left, like h*/    {m_left,    CURSOR,        MVMT|VIZ},
  65. /* ^I  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  66. /* ^J  move down    */    {m_updnto,    CURSOR,        MVMT|LNMD|VIZ|INCL},
  67. /* ^K  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  68. /* ^L  redraw screen    */    {v_redraw,    NO_ARGS,    NO_FLAGS|VIZ},
  69. /* ^M  mv front next ln */    {m_updnto,    CURSOR,        MVMT|FRNT|LNMD|VIZ|INCL},
  70. /* ^N  move down    */    {m_updnto,    CURSOR,        MVMT|LNMD|VIZ|INCL|NCOL},
  71. /* ^O  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  72. /* ^P  move up        */    {m_updnto,    CURSOR,        MVMT|LNMD|VIZ|INCL|NCOL},
  73. /* ^Q  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  74. /* ^R  redraw screen    */    {v_redraw,    NO_ARGS,    NO_FLAGS|VIZ},
  75. /* ^S  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  76. #ifndef NO_TAGSTACK
  77. /* ^T  pop tagstack    */    {v_pop,        CURSOR,        NO_FLAGS},
  78. #else
  79. /* ^T  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  80. #endif
  81. /* ^U  scroll up 1/2page*/    {m_scroll,    CURSOR,        NCOL|VIZ},
  82. /* ^V  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  83. /* ^W  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  84. /* ^X  move to phys col    */    {m_tocol,    CURSOR,        MVMT|NREL|VIZ},
  85. /* ^Y  scroll down    */    {m_scroll,    CURSOR,        NCOL|VIZ},
  86. #ifdef SIGTSTP
  87. /* ^Z  suspend elvis    */    {v_suspend,    NO_ARGS,    NO_FLAGS},
  88. #else
  89. /* ^Z  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  90. #endif
  91. /* ESC not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  92. /* ^\  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  93. /* ^]  keyword is tag    */    {v_tag,        KEYWORD,    NO_FLAGS},
  94. /* ^^  previous file    */    {v_switch,    CURSOR,        FRNT},
  95. /* ^_  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  96. /* SPC move right,like l*/    {m_right,    CURSOR,        MVMT|INCL|VIZ},
  97. /*  !  run thru filter    */    {v_filter,    CURSOR_MOVED,    FRNT|LNMD|INCL|VIZ},
  98. /*  "  select cut buffer*/    {v_selcut,    C_C_K_CUT,    PTMV|VIZ},
  99. #ifndef NO_EXTENSIONS
  100. /*  #  increment number    */    {v_increment,    KEYWORD,    SDOT},
  101. #else
  102. /*  #  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  103. #endif
  104. /*  $  move to rear    */    {m_rear,    CURSOR,        MVMT|INCL|VIZ},
  105. /*  %  move to match    */    {m_match,    CURSOR,        MVMT|INCL|VIZ},
  106. /*  &  repeat subst    */    {v_again,    CURSOR_MOVED,    SDOT|NCOL|LNMD|INCL},
  107. /*  '  move to a mark    */    {m_tomark,    C_C_K_MARK,    MVMT|FRNT|NREL|LNMD|INCL|VIZ},
  108. #ifndef NO_SENTENCE
  109. /*  (  mv back sentence    */    {m_sentence,    CURSOR,        MVMT|VIZ},
  110. /*  )  mv fwd sentence    */    {m_sentence,    CURSOR,        MVMT|VIZ},
  111. #else
  112. /*  (  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  113. /*  )  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  114. #endif
  115. #ifndef NO_ERRLIST
  116. /*  *  errlist        */    {v_errlist,    CURSOR,        FRNT|NREL},
  117. #else
  118. /*  *  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  119. #endif
  120. /*  +  mv front next ln */    {m_updnto,    CURSOR,        MVMT|FRNT|LNMD|VIZ|INCL},
  121. #ifndef NO_CHARSEARCH
  122. /*  ,  reverse [fFtT] cmd*/    {m__ch,        CURSOR,        MVMT|INCL|VIZ},
  123. #else
  124. /*  ,  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  125. #endif
  126. /*  -  mv front prev ln    */    {m_updnto,    CURSOR,        MVMT|FRNT|LNMD|VIZ|INCL},
  127. /*  .  special...    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  128. /*  /  forward search    */    {m_fsrch,    CURSOR_TEXT,    MVMT|NREL|VIZ},
  129. /*  0  part of count?    */    {NO_FUNC,    ZERO,        MVMT|PTMV|VIZ},
  130. /*  1  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  131. /*  2  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  132. /*  3  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  133. /*  4  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  134. /*  5  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  135. /*  6  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  136. /*  7  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  137. /*  8  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  138. /*  9  part of count    */    {NO_FUNC,    DIGIT,        PTMV|VIZ},
  139. /*  :  run single EX cmd*/    {v_1ex,        CURSOR_TEXT,    NO_FLAGS},
  140. #ifndef NO_CHARSEARCH
  141. /*  ;  repeat [fFtT] cmd*/    {m__ch,        CURSOR,        MVMT|INCL|VIZ},
  142. #else
  143. /*  ;  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS|VIZ},
  144. #endif
  145. /*  <  shift text left    */    {v_lshift,    CURSOR_MOVED,    SDOT|FRNT|LNMD|INCL|VIZ},
  146. /*  =  preset filter    */    {v_reformat,    CURSOR_MOVED,    SDOT|FRNT|LNMD|INCL|VIZ},
  147. /*  >  shift text right    */    {v_rshift,    CURSOR_MOVED,    SDOT|FRNT|LNMD|INCL|VIZ},
  148. /*  ?  backward search    */    {m_bsrch,    CURSOR_TEXT,    MVMT|NREL|VIZ},
  149. #ifndef NO_AT
  150. /*  @  execute a cutbuf */    {v_at,        C_C_K_CUT,    NO_FLAGS},
  151. #else
  152. /*  @  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  153. #endif
  154. /*  A  append at EOL    */    {v_insert,    CURSOR,        SDOT},
  155. /*  B  move back Word    */    {m_bword,    CURSOR,        MVMT|VIZ},
  156. /*  C  change to EOL    */    {v_change,    CURSOR_EOL,    SDOT},
  157. /*  D  delete to EOL    */    {v_delete,    CURSOR_EOL,    SDOT},
  158. /*  E  move end of Word    */    {m_eword,    CURSOR,        MVMT|INCL|VIZ},
  159. #ifndef NO_CHARSEARCH
  160. /*  F  move bk to char    */    {m_Fch,        C_C_K_CHAR,    MVMT|INCL|VIZ},
  161. #else
  162. /*  F  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  163. #endif
  164. /*  G  move to line #    */    {m_updnto,    CURSOR,        MVMT|NREL|LNMD|FRNT|INCL|VIZ},
  165. /*  H  move to row    */    {m_row,        CURSOR,        MVMT|LNMD|FRNT|VIZ|INCL},
  166. /*  I  insert at front    */    {v_insert,    CURSOR,        SDOT},
  167. /*  J  join lines    */    {v_join,    CURSOR,        SDOT},
  168. #ifndef NO_EXTENSIONS
  169. /*  K  look up keyword    */    {v_keyword,    KEYWORD,    NO_FLAGS},
  170. #else
  171. /*  K  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  172. #endif
  173. /*  L  move to last row    */    {m_row,        CURSOR,        MVMT|LNMD|FRNT|VIZ|INCL},
  174. /*  M  move to mid row    */    {m_row,        CURSOR,        MVMT|LNMD|FRNT|VIZ|INCL},
  175. /*  N  reverse prev srch*/    {m_nsrch,    CURSOR,        MVMT|NREL|VIZ},
  176. /*  O  insert above line*/    {v_insert,    CURSOR,        SDOT},
  177. /*  P  paste before    */    {v_paste,    CURSOR,        SDOT},
  178. /*  Q  quit to EX mode    */    {v_quit,    NO_ARGS,    NO_FLAGS},
  179. /*  R  overtype        */    {v_overtype,    CURSOR,        SDOT},
  180. /*  S  change line    */    {v_change,    CURSOR_MOVED,    SDOT},
  181. #ifndef NO_CHARSEARCH
  182. /*  T  move bk to char    */    {m_Tch,        C_C_K_CHAR,    MVMT|INCL|VIZ},
  183. #else
  184. /*  T  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  185. #endif
  186. /*  U  undo whole line    */    {v_undoline,    CURSOR,        FRNT},
  187. #ifndef NO_VISIBLE
  188. /*  V  start visible    */    {v_start,    CURSOR,        INCL|LNMD|VIZ},
  189. #else
  190. /*  V  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  191. #endif
  192. /*  W  move forward Word*/    {m_fword,    CURSOR,        MVMT|INCL|NWRP|VIZ},
  193. /*  X  delete to left    */    {v_xchar,    CURSOR,        SDOT},
  194. /*  Y  yank text    */    {v_yank,    CURSOR_MOVED,    NCOL},
  195. /*  Z  save file & exit    */    {v_xit,        CURSOR_CNT_KEY,    NO_FLAGS},
  196. /*  [  move back section*/    {m_paragraph,    CURSOR,        MVMT|LNMD|NREL|VIZ},
  197. #ifndef NO_POPUP
  198. /*  \  pop-up menu    */    {v_popup,    CURSOR_MOVED,    VIZ},
  199. #else
  200. /*  \  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  201. #endif
  202. /*  ]  move fwd section */    {m_paragraph,    CURSOR,        MVMT|LNMD|NREL|VIZ},
  203. /*  ^  move to front    */    {m_front,    CURSOR,        MVMT|VIZ},
  204. /*  _  current line    */    {m_updnto,    CURSOR,        MVMT|LNMD|FRNT|INCL},
  205. /*  `  move to mark    */    {m_tomark,    C_C_K_MARK,    MVMT|NREL|VIZ},
  206. /*  a  append at cursor    */    {v_insert,    CURSOR,        SDOT},
  207. /*  b  move back word    */    {m_bword,    CURSOR,        MVMT|VIZ},
  208. /*  c  change text    */    {v_change,    CURSOR_MOVED,    SDOT|VIZ},
  209. /*  d  delete op    */    {v_delete,    CURSOR_MOVED,    SDOT|VIZ},
  210. /*  e  move end word    */    {m_eword,    CURSOR,        MVMT|INCL|VIZ},
  211. #ifndef NO_CHARSEARCH
  212. /*  f  move fwd for char*/    {m_fch,        C_C_K_CHAR,    MVMT|INCL|VIZ},
  213. #else
  214. /*  f  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  215. #endif
  216. /*  g  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  217. /*  h  move left    */    {m_left,    CURSOR,        MVMT|VIZ},
  218. /*  i  insert at cursor    */    {v_insert,    CURSOR,        SDOT},
  219. /*  j  move down    */    {m_updnto,    CURSOR,        MVMT|NCOL|LNMD|VIZ|INCL},
  220. /*  k  move up        */    {m_updnto,    CURSOR,        MVMT|NCOL|LNMD|VIZ|INCL},
  221. /*  l  move right    */    {m_right,    CURSOR,        MVMT|INCL|VIZ},
  222. /*  m  define a mark    */    {v_mark,    C_C_K_MARK,    NO_FLAGS},
  223. /*  n  repeat prev srch    */    {m_nsrch,    CURSOR,     MVMT|NREL|VIZ},
  224. /*  o  insert below line*/    {v_insert,    CURSOR,        SDOT},
  225. /*  p  paste after    */    {v_paste,    CURSOR,        SDOT},
  226. /*  q  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  227. /*  r  replace chars    */    {v_replace,    C_C_K_REP1,    SDOT},
  228. /*  s  subst N chars    */    {v_subst,    CURSOR,        SDOT},
  229. #ifndef NO_CHARSEARCH
  230. /*  t  move fwd to char    */    {m_tch,        C_C_K_CHAR,    MVMT|INCL|VIZ},
  231. #else
  232. /*  t  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  233. #endif
  234. /*  u  undo        */    {v_undo,    CURSOR,        NO_FLAGS},
  235. #ifndef NO_VISIBLE
  236. /*  v  start visible    */    {v_start,    CURSOR,        INCL|VIZ},
  237. #else
  238. /*  v  not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS},
  239. #endif
  240. /*  w  move fwd word    */    {m_fword,    CURSOR,        MVMT|INCL|NWRP|VIZ},
  241. /*  x  delete character    */    {v_xchar,    CURSOR,        SDOT},
  242. /*  y  yank text    */    {v_yank,    CURSOR_MOVED,    NCOL|VIZ},
  243. /*  z  adjust scrn row    */    {m_z,         CURSOR_CNT_KEY,    NCOL|VIZ},
  244. /*  {  back paragraph    */    {m_paragraph,    CURSOR,        MVMT|VIZ},
  245. /*  |  move to column    */    {m_tocol,    CURSOR,        MVMT|NREL|VIZ},
  246. /*  }  fwd paragraph    */    {m_paragraph,    CURSOR,        MVMT|VIZ},
  247. /*  ~  upper/lowercase    */    {v_ulcase,    CURSOR,        SDOT},
  248. /* DEL not defined    */    {NO_FUNC,    NO_ARGS,    NO_FLAGS}
  249. };
  250.  
  251.  
  252.  
  253. void vi()
  254. {
  255.     REG int            key;    /* keystroke from user */
  256.     long            count;    /* numeric argument to some functions */
  257.     REG struct keystru    *keyptr;/* pointer to vikeys[] element */
  258.     MARK            tcurs;    /* temporary cursor */
  259.     int            prevkey;/* previous key, if d/c/y/</>/! */
  260.     MARK            range;    /* start of range for d/c/y/</>/! */
  261.     char            text[132];
  262.     int            dotkey;    /* last "key" of a change */
  263.     int            dotpkey;/* last "prevkey" of a change */
  264.     int            dotkey2;/* last extra "getkey()" of a change */
  265.     int            dotcnt;    /* last "count" of a change */
  266.     int            firstkey;
  267.     REG int            i;
  268.  
  269.     /* tell the redraw() function to start from scratch */
  270.     redraw(MARK_UNSET, FALSE);
  271.  
  272. #ifdef lint
  273.     /* lint says that "range" might be used before it is set.  This
  274.      * can't really happen due to the way "range" and "prevkey" are used,
  275.      * but lint doesn't know that.  This line is here ONLY to keep lint
  276.      * happy.
  277.      */
  278.     range = 0L;
  279. #endif
  280.  
  281.     /* safeguard against '.' with no previous command */
  282.     dotkey = dotpkey = dotkey2 = dotcnt = 0;
  283.  
  284.     /* go immediately into insert mode, if ":set inputmode" */
  285.     firstkey = 0;
  286. #ifndef NO_EXTENSIONS
  287.     if (*o_inputmode)
  288.     {
  289.         firstkey = 'i';
  290.     }
  291. #endif
  292.  
  293.     /* Repeatedly handle VI commands */
  294.     for (count = 0, prevkey = '\0'; mode == MODE_VI; )
  295.     {
  296.         /* if we've moved off the undoable line, then we can't undo it at all */
  297.         if (markline(cursor) != U_line)
  298.         {
  299.             U_line = 0L;
  300.         }
  301.  
  302.         /* report any changes from the previous command */
  303.         if (rptlines >= *o_report)
  304.         {
  305.             redraw(cursor, FALSE);
  306.             msg("%ld line%s %s", rptlines, (rptlines==1?"":"s"), rptlabel);
  307.         }
  308.         rptlines = 0L;
  309.  
  310.         /* get the next command key.  It must be ASCII */
  311.         if (firstkey)
  312.         {
  313.             key = firstkey;
  314.             firstkey = 0;
  315.         }
  316.         else
  317.         {
  318.             do
  319.             {
  320.                 key = getkey(WHEN_VICMD);
  321.             } while (key < 0 || key > 127);
  322.         }
  323. #ifdef DEBUG2
  324.         debout("\nkey='%c'\n", key);
  325. #endif
  326.  
  327.         /* Convert a doubled-up operator such as "dd" into "d_" */
  328.         if (prevkey && key == prevkey)
  329.         {
  330.             key = '_';
  331.         }
  332.  
  333.         /* look up the structure describing this command */
  334.         keyptr = &vikeys[key];
  335.  
  336.         /* '&' and uppercase operators always act like doubled */
  337.         if (!prevkey && keyptr->args == CURSOR_MOVED
  338.             && (key == '&' || isupper(key)))
  339.         {
  340.             range = cursor;
  341.             prevkey = key;
  342.             key = '_';
  343.             keyptr = &vikeys[key];
  344.         }
  345.  
  346. #ifndef NO_VISIBLE
  347.         /* if we're in the middle of a v/V command, reject commands
  348.          * that aren't operators or movement commands
  349.          */
  350.         if (V_from && !(keyptr->flags & VIZ))
  351.         {
  352.             beep();
  353.             prevkey = 0;
  354.             count = 0;
  355.             continue;
  356.         }
  357. #endif
  358.  
  359.         /* if we're in the middle of a d/c/y/</>/! command, reject
  360.          * anything but movement.
  361.          */
  362.         if (prevkey && !(keyptr->flags & (MVMT|PTMV)))
  363.         {
  364.             beep();
  365.             prevkey = 0;
  366.             count = 0;
  367.             continue;
  368.         }
  369.  
  370.         /* set the "dot" variables, if we're supposed to */
  371.         if (((keyptr->flags & SDOT)
  372.             || (prevkey && vikeys[prevkey].flags & SDOT))
  373. #ifndef NO_VISIBLE
  374.             && !V_from
  375. #endif
  376.         )
  377.         {
  378.             dotkey = key;
  379.             dotpkey = prevkey;
  380.             dotkey2 = '\0';
  381.             dotcnt = count;
  382.  
  383.             /* remember the line before any changes are made */
  384.             if (U_line != markline(cursor))
  385.             {
  386.                 U_line = markline(cursor);
  387.                 strcpy(U_text, fetchline(U_line));
  388.             }
  389.         }
  390.  
  391.         /* if this is "." then set other vars from the "dot" vars */
  392.         if (key == '.')
  393.         {
  394.             key = dotkey;
  395.             keyptr = &vikeys[key];
  396.             prevkey = dotpkey;
  397.             if (prevkey)
  398.             {
  399.                 range = cursor;
  400.             }
  401.             if (count == 0)
  402.             {
  403.                 count = dotcnt;
  404.             }
  405.             doingdot = TRUE;
  406.  
  407.             /* remember the line before any changes are made */
  408.             if (U_line != markline(cursor))
  409.             {
  410.                 U_line = markline(cursor);
  411.                 strcpy(U_text, fetchline(U_line));
  412.             }
  413.         }
  414.         else
  415.         {
  416.             doingdot = FALSE;
  417.         }
  418.  
  419.         /* process the key as a command */
  420.         tcurs = cursor;
  421.         force_flags = NO_FLAGS;
  422.         switch (keyptr->args & ARGSMASK)
  423.         {
  424.           case ZERO:
  425.             if (count == 0)
  426.             {
  427.                 tcurs = cursor & ~(BLKSIZE - 1);
  428.                 break;
  429.             }
  430.             /* else fall through & treat like other digits... */
  431.  
  432.           case DIGIT:
  433.             count = count * 10 + key - '0';
  434.             break;
  435.  
  436.           case KEYWORD:
  437.             /* if not on a keyword, fail */
  438.             pfetch(markline(cursor));
  439.             key = markidx(cursor);
  440.             if (!isalnum(ptext[key]))
  441.             {
  442.                 tcurs = MARK_UNSET;
  443.                 break;
  444.             }
  445.  
  446.             /* find the start of the keyword */
  447.             while (key > 0 && isalnum(ptext[key - 1]))
  448.             {
  449.                 key--;
  450.             }
  451.             tcurs = (cursor & ~(BLKSIZE - 1)) + key;
  452.  
  453.             /* copy it into a buffer, and NUL-terminate it */
  454.             i = 0;
  455.             do
  456.             {
  457.                 text[i++] = ptext[key++];
  458.             } while (isalnum(ptext[key]));
  459.             text[i] = '\0';
  460.  
  461.             /* call the function */
  462.             tcurs = (*keyptr->func)(text, tcurs, count);
  463.             count = 0L;
  464.             break;
  465.  
  466.           case NO_ARGS:
  467.             if (keyptr->func)
  468.             {
  469.                 (*keyptr->func)();
  470.             }
  471.             else
  472.             {
  473.                 beep();
  474.             }
  475.             count = 0L;
  476.             break;
  477.     
  478.           case CURSOR:
  479.             tcurs = (*keyptr->func)(cursor, count, key, prevkey);
  480.             count = 0L;
  481.             break;
  482.  
  483.           case CURSOR_CNT_KEY:
  484.             if (doingdot)
  485.             {
  486.                 tcurs = (*keyptr->func)(cursor, count, dotkey2);
  487.             }
  488.             else
  489.             {
  490.                 /* get a key */
  491.                 i = getkey(KEYMODE(keyptr->args));
  492.                 if (i == '\033') /* ESC */
  493.                 {
  494.                     count = 0;
  495.                     tcurs = MARK_UNSET;
  496.                     break; /* exit from "case CURSOR_CNT_KEY" */
  497.                 }
  498.                 else if (i == ctrl('V'))
  499.                 {
  500.                     i = getkey(0);
  501.                 }
  502.  
  503.                 /* if part of an SDOT command, remember it */
  504.                  if (keyptr->flags & SDOT
  505.                  || (prevkey && vikeys[prevkey].flags & SDOT))
  506.                 {
  507.                     dotkey2 = i;
  508.                 }
  509.  
  510.                 /* do it */
  511.                 tcurs = (*keyptr->func)(cursor, count, i);
  512.             }
  513.             count = 0L;
  514.             break;
  515.     
  516.           case CURSOR_MOVED:
  517. #ifndef NO_VISIBLE
  518.             if (V_from)
  519.             {
  520.                 range = cursor;
  521.                 tcurs = V_from;
  522.                 count = 0L;
  523.                 prevkey = key;
  524.                 key = (V_linemd ? 'V' : 'v');
  525.                 keyptr = &vikeys[key];
  526.             }
  527.             else
  528. #endif
  529.             {
  530.                 prevkey = key;
  531.                 range = cursor;
  532.                 force_flags = LNMD|INCL;
  533.             }
  534.             break;
  535.  
  536.           case CURSOR_EOL:
  537.             prevkey = key;
  538.             /* a zero-length line needs special treatment */
  539.             pfetch(markline(cursor));
  540.             if (plen == 0)
  541.             {
  542.                 /* act on a zero-length section of text */
  543.                 range = tcurs = cursor;
  544.                 key = '0';
  545.             }
  546.             else
  547.             {
  548.                 /* act like CURSOR_MOVED with '$' movement */
  549.                 range = cursor;
  550.                 tcurs = m_rear(cursor, 1L);
  551.                 key = '$';
  552.             }
  553.             count = 0L;
  554.             keyptr = &vikeys[key];
  555.             break;
  556.  
  557.           case CURSOR_TEXT:
  558.               do
  559.               {    
  560.                 text[0] = key;
  561.                 text[1] = '\0';
  562.                 if (doingdot || vgets(key, text + 1, sizeof text - 1) >= 0)
  563.                 {
  564.                     /* reassure user that <CR> was hit */
  565.                     qaddch('\r');
  566.                     refresh();
  567.  
  568.                     /* call the function with the text */
  569.                     tcurs = (*keyptr->func)(cursor, text);
  570.                 }
  571.                 else
  572.                 {
  573.                     if (exwrote || mode == MODE_COLON)
  574.                     {
  575.                         redraw(MARK_UNSET, FALSE);
  576.                     }
  577.                     mode = MODE_VI;
  578.                 }
  579.             } while (mode == MODE_COLON);
  580.             count = 0L;
  581.             break;
  582.         }
  583.  
  584.         /* if that command took us out of vi mode, then exit the loop
  585.          * NOW, without tweaking the cursor or anything.  This is very
  586.          * important when mode == MODE_QUIT.
  587.          */
  588.         if (mode != MODE_VI)
  589.         {
  590.             break;
  591.         }
  592.  
  593.         /* now move the cursor, as appropriate */
  594.         if (prevkey && ((keyptr->flags & MVMT)
  595. #ifndef NO_VISIBLE
  596.                            || V_from
  597. #endif
  598.                 ) && count == 0L)
  599.         {
  600.             /* movements used as targets are less strict */
  601.             tcurs = adjmove(cursor, tcurs, (int)(keyptr->flags | force_flags));
  602.         }
  603.         else if (keyptr->args == CURSOR_MOVED)
  604.         {
  605.             /* the < and > keys have FRNT,
  606.              * but it shouldn't be applied yet
  607.              */
  608.             tcurs = adjmove(cursor, tcurs, FINL);
  609.         }
  610.         else
  611.         {
  612.             tcurs = adjmove(cursor, tcurs, (int)(keyptr->flags | force_flags | FINL));
  613.         }
  614.  
  615.         /* was that the end of a d/c/y/</>/! command? */
  616.         if (prevkey && ((keyptr->flags & MVMT)
  617. #ifndef NO_VISIBLE
  618.                            || V_from
  619. #endif
  620.                 ) && count == 0L)
  621.         {
  622. #ifndef NO_VISIBLE
  623.             /* turn off the hilight */
  624.             V_from = 0L;
  625. #endif
  626.  
  627.             /* if the movement command failed, cancel operation */
  628.             if (tcurs == MARK_UNSET)
  629.             {
  630.                 prevkey = 0;
  631.                 count = 0;
  632.                 continue;
  633.             }
  634.  
  635.             /* make sure range=front and tcurs=rear.  Either way,
  636.              * leave cursor=range since that's where we started.
  637.              */
  638.             cursor = range;
  639.             if (tcurs < range)
  640.             {
  641.                 range = tcurs;
  642.                 tcurs = cursor;
  643.             }
  644.  
  645.             /* The 'w' and 'W' destinations should never take us
  646.              * to the front of a line.  Instead, they should take
  647.              * us only to the end of the preceding line.
  648.              */
  649.             if ((keyptr->flags & NWRP) == NWRP
  650.               && markline(range) < markline(tcurs)
  651.               && (markline(tcurs) > nlines || tcurs == m_front(tcurs, 0L)))
  652.             {
  653.                 tcurs = (tcurs & ~(BLKSIZE - 1)) - BLKSIZE;
  654.                 pfetch(markline(tcurs));
  655.                 tcurs += plen;
  656.             }
  657.  
  658.             /* adjust for line mode & inclusion of last char/line */
  659.             i = (keyptr->flags | vikeys[prevkey].flags);
  660.             switch ((i | force_flags) & (INCL|LNMD))
  661.             {
  662.               case INCL:
  663.                 tcurs++;
  664.                 break;
  665.  
  666.               case INCL|LNMD:
  667.                 tcurs += BLKSIZE;
  668.                 /* fall through... */
  669.  
  670.               case LNMD:
  671.                 range &= ~(BLKSIZE - 1);
  672.                 tcurs &= ~(BLKSIZE - 1);
  673.                 break;
  674.             }
  675.  
  676.             /* run the function */
  677.             tcurs = (*vikeys[prevkey].func)(range, tcurs);
  678.             if (mode == MODE_VI)
  679.             {
  680.                 (void)adjmove(cursor, cursor, FINL);
  681.                 cursor = adjmove(cursor, tcurs, (int)(vikeys[prevkey].flags | FINL));
  682.             }
  683.  
  684.             /* cleanup */
  685.             prevkey = 0;
  686.         }
  687.         else if (!prevkey)
  688.         {
  689.             if (tcurs != MARK_UNSET)
  690.                 cursor = tcurs;
  691.         }
  692.     }
  693. }
  694.  
  695. /* This function adjusts the MARK value that they return; here we make sure
  696.  * it isn't past the end of the line, and that the column hasn't been
  697.  * *accidentally* changed.
  698.  */
  699. MARK adjmove(old, new, flags)
  700.     MARK        old;    /* the cursor position before the command */
  701.     REG MARK    new;    /* the cursor position after the command */
  702.     int        flags;    /* various flags regarding cursor mvmt */
  703. {
  704.     static int    colno;    /* the column number that we want */
  705.     REG char    *text;    /* used to scan through the line's text */
  706.     REG int        i;
  707.  
  708. #ifdef DEBUG2
  709.     debout("adjmove(%ld.%d, %ld.%d, 0x%x)\n", markline(old), markidx(old), markline(new), markidx(new), flags);
  710. #endif
  711. #ifdef DEBUG
  712.     watch();
  713. #endif
  714.  
  715.     /* if the command failed, bag it! */
  716.     if (new == MARK_UNSET)
  717.     {
  718.         if (flags & FINL)
  719.         {
  720.             beep();
  721.             return old;
  722.         }
  723.         return new;
  724.     }
  725.  
  726.     /* if this is a non-relative movement, set the '' mark */
  727.     if (flags & NREL)
  728.     {
  729.         mark[26] = old;
  730.     }
  731.  
  732.     /* make sure it isn't past the end of the file */
  733.     if (markline(new) < 1)
  734.     {
  735.         new = MARK_FIRST;
  736.     }
  737.     else if (markline(new) > nlines)
  738.     {
  739.         if (!(flags & FINL))
  740.         {
  741.             return MARK_EOF;
  742.         }
  743.         new = MARK_LAST;
  744.     }
  745.  
  746.     /* fetch the new line */
  747.     pfetch(markline(new));
  748.  
  749.     /* move to the front, if we're supposed to */
  750.     if (flags & FRNT)
  751.     {
  752.         new = m_front(new, 1L);
  753.     }
  754.  
  755.     /* change the column#, or change the mark to suit the column# */
  756.     if (!(flags & NCOL))
  757.     {
  758.         /* change the column# */
  759.         i = markidx(new);
  760.         if (i == BLKSIZE - 1)
  761.         {
  762.             new &= ~(BLKSIZE - 1);
  763.             if (plen > 0)
  764.             {
  765.                 new += plen - 1;
  766.             }
  767.             colno = BLKSIZE * 8; /* one heck of a big colno */
  768.         }
  769.         else if (plen > 0)
  770.         {
  771.             if (i >= plen)
  772.             {
  773.                 new = (new & ~(BLKSIZE - 1)) + plen - 1;
  774.             }
  775.             colno = idx2col(new, ptext, FALSE);
  776.         }
  777.         else
  778.         {
  779.             new &= ~(BLKSIZE - 1);
  780.             colno = 0;
  781.         }
  782.     }
  783.     else
  784.     {
  785.         /* adjust the mark to get as close as possible to column# */
  786.         for (i = 0, text = ptext; i <= colno && *text; text++)
  787.         {
  788.             if (*text == '\t' && !*o_list)
  789.             {
  790.                 i += *o_tabstop - (i % *o_tabstop);
  791.             }
  792.             else if (UCHAR(*text) < ' ' || *text == 127)
  793.             {
  794.                 i += 2;
  795.             }
  796. #ifndef NO_CHARATTR
  797.             else if (*o_charattr && text[0] == '\\' && text[1] == 'f' && text[2])
  798.             {
  799.                 text += 2; /* plus one more in "for()" stmt */
  800.             }
  801. #endif
  802.             else
  803.             {
  804.                 i++;
  805.             }
  806.         }
  807.         if (text > ptext)
  808.         {
  809.             text--;
  810.         }
  811.         new = (new & ~(BLKSIZE - 1)) + (int)(text - ptext);
  812.     }
  813.  
  814.     return new;
  815. }
  816.  
  817.  
  818. #ifdef DEBUG
  819. watch()
  820. {
  821.     static wasset;
  822.  
  823.     if (*origname)
  824.     {
  825.         wasset = TRUE;
  826.     }
  827.     else if (wasset)
  828.     {
  829.         mode = MODE_EX;
  830.         msg("origname was clobbered");
  831.         endwin();
  832.         abort();
  833.     }
  834.  
  835.     if (wasset && nlines == 0)
  836.     {
  837.         mode = MODE_EX;
  838.         msg("nlines=0");
  839.         endwin();
  840.         abort();
  841.     }
  842. }
  843. #endif
  844.