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

  1. /*
  2.  * This file contains the command processing functions for the commands
  3.  * that take motion operators.
  4.  * written for vile by Paul Fox, (c)1990
  5.  *
  6.  * $Log: opers.c,v $
  7.  * Revision 1.23  1992/11/19  09:15:37  foxharp
  8.  * be sure to turn doingopcmd off if the motion fails or is aborted.
  9.  * also, allow null regions -- i don't know why i didn't used to allow them
  10.  *
  11.  * Revision 1.22  1992/07/16  22:18:54  foxharp
  12.  * ins() takes an argument -- whether or not to playback, usually FALSE
  13.  *
  14.  * Revision 1.21  1992/05/25  21:07:48  foxharp
  15.  * extern func declarations moved to header
  16.  *
  17.  * Revision 1.20  1992/05/16  12:00:31  pgf
  18.  * prototypes/ansi/void-int stuff/microsoftC
  19.  *
  20.  * Revision 1.19  1992/01/06  23:09:33  pgf
  21.  * error message if bad function specified for motion
  22.  *
  23.  * Revision 1.18  1992/01/05  00:06:13  pgf
  24.  * split mlwrite into mlwrite/mlprompt/mlforce to make errors visible more
  25.  * often.  also normalized message appearance somewhat.
  26.  *
  27.  * Revision 1.17  1992/01/03  23:26:48  pgf
  28.  * make the "pre-operator" position, pre_op_dot, a global so that it can
  29.  * be adjust slightly by some motions -- paragraph motions in particular
  30.  *
  31.  * Revision 1.16  1991/11/03  17:46:30  pgf
  32.  * removed f,n args from all region functions -- they don't use them,
  33.  * since they're no longer directly called by the user
  34.  *
  35.  * Revision 1.15  1991/11/01  14:38:00  pgf
  36.  * saber cleanup
  37.  *
  38.  * Revision 1.14  1991/10/29  14:35:29  pgf
  39.  * implemented the & commands: substagain
  40.  *
  41.  * Revision 1.13  1991/09/10  00:51:05  pgf
  42.  * only restore dot with swapmark if the buffer hasn't switched on us
  43.  *
  44.  * Revision 1.12  1991/08/13  02:50:52  pgf
  45.  * fixed chgreg case of butting against top of buffer
  46.  *
  47.  * Revision 1.11  1991/08/07  12:35:07  pgf
  48.  * added RCS log messages
  49.  *
  50.  * revision 1.10
  51.  * date: 1991/07/19 17:23:06;
  52.  * added status return to chgreg()
  53.  * 
  54.  * revision 1.9
  55.  * date: 1991/07/19 17:16:41;
  56.  * fix null pointer de-ref
  57.  * 
  58.  * revision 1.8
  59.  * date: 1991/06/25 19:53:04;
  60.  * massive data structure restructure
  61.  * 
  62.  * revision 1.7
  63.  * date: 1991/06/16 17:36:47;
  64.  * added entab, detab, and trim operator routines
  65.  * 
  66.  * revision 1.6
  67.  * date: 1991/06/15 09:11:23;
  68.  * hardened chgreg[ion] against motions that don't succeed, leaving
  69.  * the mark unset, so swapmark fails
  70.  * 
  71.  * revision 1.5
  72.  * date: 1991/06/03 10:26:34;
  73.  * cleanup, and
  74.  * pulled some code out of execute() into here
  75.  * 
  76.  * revision 1.4
  77.  * date: 1991/05/31 11:38:43;
  78.  * syntax error from bad merge
  79.  * 
  80.  * revision 1.3
  81.  * date: 1991/05/31 11:17:31;
  82.  * lot of cleanup, some new operators -- now use godotplus() instead of
  83.  * stutterfunc().  much better
  84.  * 
  85.  * revision 1.2
  86.  * date: 1991/04/04 09:39:56;
  87.  * added operfilter (!) command
  88.  * 
  89.  * revision 1.1
  90.  * date: 1990/09/21 10:25:52;
  91.  * initial vile RCS revision
  92.  */
  93.  
  94. #include    "estruct.h"
  95. #include        "edef.h"
  96. #ifndef NULL
  97. #define NULL 0
  98. #endif
  99.  
  100. extern CMDFUNC f_godotplus;
  101. MARK pre_op_dot;
  102.  
  103. /* For the "operator" commands -- the following command is a motion, or
  104.  *  the operator itself is repeated.  All operate on regions.
  105.  */
  106. int
  107. operator(f,n,fn,str)
  108. int f,n;
  109. int (*fn)();
  110. char *str;
  111. {
  112.     int c;
  113.     int this1key;
  114.     int status;
  115.     CMDFUNC *cfp;            /* function to execute */
  116.     char tok[NSTRING];        /* command incoming */
  117.     BUFFER *ourbp;
  118.  
  119.     doingopcmd = TRUE;
  120.  
  121.     pre_op_dot = DOT;
  122.     ourbp = curbp;
  123.  
  124.     if (havemotion != NULL) {
  125.         cfp = havemotion;
  126.         havemotion = NULL;
  127.     } else {
  128.         mlwrite("%s operation pending...",str);
  129.         update(FALSE);
  130.  
  131.         /* get the next command from the keyboard */
  132.         /* or a command line, as approp. */
  133.         if (clexec) {
  134.             macarg(tok);    /* get the next token */
  135.             if (!strcmp(tok,"lines"))
  136.                 cfp = &f_godotplus;
  137.             else
  138.                 cfp = engl2fnc(tok);
  139.         } else {
  140.             this1key = last1key;
  141.             c = kbd_seq();
  142.  
  143.             /* allow second chance for entering counts */
  144.             if (f == FALSE) {
  145.                 do_num_proc(&c,&f,&n);
  146.                 do_rept_arg_proc(&c,&f,&n);
  147.             }
  148.  
  149.             if (this1key == last1key)
  150.                 cfp = &f_godotplus;
  151.             else
  152.                 cfp = kcod2fnc(c);
  153.  
  154.         }
  155.         if (cfp)
  156.             mlerase();
  157.         else
  158.             mlforce("[No such function]");
  159.     }
  160.     if (!cfp) {
  161.         doingopcmd = FALSE;
  162.         return FALSE;
  163.     }
  164.  
  165.     if ((cfp->c_flags & MOTION) == 0) {
  166.         TTbeep();
  167.         doingopcmd = FALSE;
  168.         return(ABORT);
  169.     }
  170.  
  171.     /* motion is interpreted as affecting full lines */
  172.     if (cfp->c_flags & FL)
  173.         fulllineregions = TRUE;
  174.  
  175.     /* and execute the motion */
  176.     status = execute(cfp, f, n);
  177.  
  178.     if (status != TRUE 
  179. #ifdef BEFORE
  180.     ||
  181.        ( samepoint(pre_op_dot, DOT) && fulllineregions == FALSE)
  182. #endif
  183.         ) {
  184.         doingopcmd = FALSE;
  185.         fulllineregions = FALSE;
  186.         return FALSE;
  187.     }
  188.  
  189.     opcmd = 0;
  190.  
  191.     MK = pre_op_dot;
  192.  
  193.     /* we've successfully set up a region */
  194.     if (!fn) { /* be defensive */
  195.         mlforce("BUG -- null func pointer in operator");
  196.         status = FALSE;
  197.     } else {
  198.         status = (fn)();
  199.     }
  200.  
  201.     if (ourbp == curbp) /* in case the func switched buffers on us */
  202.         swapmark();
  203.  
  204.     if (fulllineregions) {
  205.         fulllineregions = FALSE;
  206.         firstnonwhite(f,n);
  207.     }
  208.  
  209.     doingopcmd = FALSE;
  210.     return status;
  211. }
  212.  
  213. int
  214. operdel(f,n)
  215. int f,n;
  216. {
  217.     opcmd = OPDEL;
  218.     return operator(f,n,killregion,"Delete");
  219. }
  220.  
  221. int
  222. operlinedel(f,n)
  223. int f,n;
  224. {
  225.     fulllineregions = TRUE;
  226.     opcmd = OPDEL;
  227.     return operator(f,n,killregion,"Delete of full lines");
  228. }
  229.  
  230. int
  231. chgreg()
  232. {
  233.     killregion();
  234.     if (fulllineregions) {
  235.         if (backline(FALSE,1) == TRUE) /* returns FALSE at top of buffer */
  236.             return opendown(TRUE,1);
  237.         else
  238.             return openup(TRUE,1);
  239.     }
  240.     return ins(FALSE);
  241. }
  242.  
  243. int
  244. operchg(f,n)
  245. int f,n;
  246. {
  247.     int s;
  248.  
  249.     opcmd = OPOTHER;
  250.     s = operator(f,n,chgreg,"Change");
  251.     if (s == TRUE) swapmark();
  252.     return s;
  253. }
  254.  
  255. int
  256. operlinechg(f,n)
  257. int f,n;
  258. {
  259.     int s;
  260.  
  261.     fulllineregions = TRUE;
  262.     opcmd = OPOTHER;
  263.     s = operator(f,n,chgreg,"Change of full lines");
  264.     if (s == TRUE) swapmark();
  265.     return s;
  266. }
  267.  
  268. int
  269. operyank(f,n)
  270. int f,n;
  271. {
  272.     opcmd = OPOTHER;
  273.     return operator(f,n,yankregion,"Yank");
  274. }
  275.  
  276. int
  277. operlineyank(f,n)
  278. int f,n;
  279. {
  280.     fulllineregions = TRUE;
  281.     opcmd = OPOTHER;
  282.     return operator(f,n,yankregion,"Yank of full lines");
  283. }
  284.  
  285. int
  286. operflip(f,n)
  287. int f,n;
  288. {
  289.     opcmd = OPOTHER;
  290.     return operator(f,n,flipregion,"Flip case");
  291. }
  292.  
  293. int
  294. operupper(f,n)
  295. int f,n;
  296. {
  297.     opcmd = OPOTHER;
  298.     return operator(f,n,upperregion,"Upper case");
  299. }
  300.  
  301. int
  302. operlower(f,n)
  303. int f,n;
  304. {
  305.     opcmd = OPOTHER;
  306.     return operator(f,n,lowerregion,"Lower case");
  307. }
  308.  
  309.  
  310. int
  311. operlshift(f,n)
  312. int f,n;
  313. {
  314.     fulllineregions = TRUE;
  315.     opcmd = OPOTHER;
  316.     return operator(f,n,shiftlregion,"Left shift");
  317. }
  318.  
  319. int
  320. operrshift(f,n)
  321. int f,n;
  322. {
  323.     fulllineregions = TRUE;
  324.     opcmd = OPOTHER;
  325.     return operator(f,n,shiftrregion,"Right shift");
  326. }
  327.  
  328. int
  329. operwrite(f,n)
  330. int f,n;
  331. {
  332.         register int    s;
  333.         static char fname[NFILEN];
  334.     if (ukb != 0) {
  335.             if ((s=mlreply("Write to file: ", fname, NFILEN)) != TRUE)
  336.                     return s;
  337.         return kwrite(fname,TRUE);
  338.     } else {
  339.         opcmd = OPOTHER;
  340.         return operator(f,n,writeregion,"File write");
  341.     }
  342. }
  343.  
  344. int
  345. operformat(f,n)
  346. int f,n;
  347. {
  348.     fulllineregions = TRUE;
  349.     opcmd = OPOTHER;
  350.     return operator(f,n,formatregion,"Format");
  351. }
  352.  
  353. int
  354. operfilter(f,n)
  355. int f,n;
  356. {
  357.     fulllineregions = TRUE;
  358.     opcmd = OPOTHER;
  359.     return operator(f,n,filterregion,"Filter");
  360. }
  361.  
  362.  
  363. int
  364. operprint(f,n)
  365. int f,n;
  366. {
  367.     fulllineregions = TRUE;
  368.     opcmd = OPOTHER;
  369.     return operator(f,n,plineregion,"Line print");
  370. }
  371.  
  372. int
  373. operlist(f,n)
  374. int f,n;
  375. {
  376.     fulllineregions = TRUE;
  377.     opcmd = OPOTHER;
  378.     return operator(f,n,llineregion,"Line list");
  379. }
  380.  
  381. int
  382. opersubst(f,n)
  383. int f,n;
  384. {
  385.     fulllineregions = TRUE;
  386.     opcmd = OPOTHER;
  387.     return operator(f,n,substregion,"Substitute");
  388. }
  389.  
  390. int
  391. opersubstagain(f,n)
  392. int f,n;
  393. {
  394.     fulllineregions = TRUE;
  395.     opcmd = OPOTHER;
  396.     return operator(f,n,subst_again_region,"Substitute-again");
  397. }
  398.  
  399. int
  400. operentab(f,n)
  401. int f,n;
  402. {
  403.     fulllineregions = TRUE;
  404.     opcmd = OPOTHER;
  405.     return operator(f,n,entab_region,"Spaces-->Tabs");
  406. }
  407.  
  408. int
  409. operdetab(f,n)
  410. int f,n;
  411. {
  412.     fulllineregions = TRUE;
  413.     opcmd = OPOTHER;
  414.     return operator(f,n,detab_region,"Tabs-->Spaces");
  415. }
  416.  
  417. int
  418. opertrim(f,n)
  419. int f,n;
  420. {
  421.     fulllineregions = TRUE;
  422.     opcmd = OPOTHER;
  423.     return operator(f,n,trim_region,"Trim whitespace");
  424. }
  425.