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

  1. #include    "estruct.h"
  2. #include        "edef.h"
  3. #include        <stdio.h>
  4.  
  5. /* ed/vi/ex style global commands, where first the file is scanned for
  6.  *    matching lines, then for each such line, an action is performed.
  7.  *    written for vile by Paul Fox, (c)1990
  8.  *
  9.  * $Log: globals.c,v $
  10.  * Revision 1.13  1992/12/04  09:12:25  foxharp
  11.  * deleted unused assigns
  12.  *
  13.  * Revision 1.12  1992/05/16  12:00:31  pgf
  14.  * prototypes/ansi/void-int stuff/microsoftC
  15.  *
  16.  * Revision 1.11  1992/03/07  10:21:29  pgf
  17.  * arg mismatch on fsearch()
  18.  *
  19.  * Revision 1.10  1992/01/05  00:06:13  pgf
  20.  * split mlwrite into mlwrite/mlprompt/mlforce to make errors visible more
  21.  * often.  also normalized message appearance somewhat.
  22.  *
  23.  * Revision 1.9  1991/11/01  14:38:00  pgf
  24.  * saber cleanup
  25.  *
  26.  * Revision 1.8  1991/09/10  00:52:55  pgf
  27.  * be careful to not rely on curbp during global ops, since some commands, like
  28.  * print (pregion) change buffers
  29.  *
  30.  * Revision 1.7  1991/08/07  12:35:07  pgf
  31.  * added RCS log messages
  32.  *
  33.  * revision 1.6
  34.  * date: 1991/08/06 15:22:03;
  35.  *  global/local values
  36.  * 
  37.  * revision 1.5
  38.  * date: 1991/06/27 18:43:40;
  39.  * fixed infinite loop if a global referenced '^' or '$' as the search string
  40.  * 
  41.  * revision 1.4
  42.  * date: 1991/06/25 19:52:44;
  43.  * massive data structure restructure
  44.  * 
  45.  * revision 1.3
  46.  * date: 1991/05/31 11:07:32;
  47.  * clean up globals routine, so it doesn't need or provide extra args
  48.  * 
  49.  * revision 1.2
  50.  * date: 1991/04/22 09:02:42;
  51.  * removed non-portable initialization
  52.  * 
  53.  * revision 1.1
  54.  * date: 1990/09/21 10:25:21;
  55.  * initial vile RCS revision
  56. */
  57. #if GLOBALS
  58.  
  59. int
  60. globals(f,n)
  61. int f,n;
  62. {
  63.     return globber(f,n,'g');
  64. }
  65.  
  66. int
  67. vglobals(f,n)
  68. int f,n;
  69. {
  70. #ifdef SOMEDAY
  71.     return globber(f,n,'v');
  72. #else
  73.     return unimpl(f,n);
  74. #endif
  75. }
  76.  
  77. /* ARGSUSED */
  78. int
  79. globber(f, n, g_or_v)
  80. int f, n, g_or_v;
  81. {
  82.     int c, s;
  83.     register LINE *lp;
  84.     register char *fnp;    /* ptr to the name of the cmd to exec */
  85.     char *kbd_engl();
  86.     CMDFUNC *engl2fnc();
  87.     CMDFUNC *cfp;
  88.     int foundone;
  89.     WINDOW *wp;
  90.     extern CMDFUNC f_godotplus;
  91.     
  92.     c = '\n';
  93.     if (isnamedcmd) {
  94.         c = tpeekc();
  95.         if (c < 0) {
  96.             c = '\n';
  97.         } else {
  98.             if (ispunct(c)) {
  99.                 (void)kbd_key();
  100.             }
  101.         }
  102.     }
  103.     if (readpattern("global pattern: ", &pat[0], &gregexp, c, FALSE) != TRUE) {
  104.         mlforce("[No pattern.]");
  105.         return FALSE;
  106.     }
  107.  
  108.     /* in some sense, it would be nice to search first, before
  109.                 making the user answer the next question, but the
  110.                 searching delay is too long, and unexpected in the
  111.                 middle of a command.  */
  112.  
  113.     mlprompt("action to perform on each matching line: ");
  114.     /* and now get the name of, and then the function to execute */
  115.     fnp = kbd_engl();
  116.     if (!fnp || !fnp[0]) {
  117.             mlforce("[No function]");
  118.         return FALSE;
  119.     } else if (!(cfp = engl2fnc(fnp))) {
  120.             mlforce("[No such function]");
  121.         return FALSE;
  122.     } else if ((cfp->c_flags & GLOBOK) == 0) {
  123.             mlforce("[Function not allowed]");
  124.         return FALSE;
  125.     }
  126.     
  127.     
  128.     /* call the searcher, telling it to do line marking */
  129.     s = fsearch(FALSE,0,TRUE,FALSE);
  130.     if (s != TRUE)
  131.         return s;
  132.     
  133.     calledbefore = FALSE;
  134.     
  135.     /* loop through the buffer -- we must clear the marks no matter what */
  136.     s = TRUE;
  137.     lp = lforw(curbp->b_line.l);
  138.     wp = curwp;
  139.     /* loop until there are no marked lines in the buffer */
  140.     foundone = FALSE;
  141.     for(;;) {
  142.         if (lp == wp->w_bufp->b_line.l) {
  143.             /* at the end -- only quit if we found no 
  144.                 marks on the last pass through. otherwise,
  145.                 go through again */
  146.             if (foundone)
  147.                 foundone = FALSE;
  148.             else
  149.                 break;
  150.             lsetnotmarked(lp); /* always unmark the header line */
  151.         }
  152.         if (lismarked(lp)) {
  153.             foundone = TRUE;
  154.             lsetnotmarked(lp);
  155.             /* call the function, if there is one, and results
  156.                 have been ok so far */
  157.             if (cfp && s) {
  158.                 if (!calledbefore && (cfp->c_flags & UNDO)) {
  159.                     if (b_val(wp->w_bufp,MDVIEW))
  160.                         return(rdonly());
  161.                     mayneedundo();
  162.                 }
  163.                 havemotion = &f_godotplus;
  164.                 wp->w_dot.l = lp;
  165.                 wp->w_dot.o = 0;
  166.                 s = (cfp->c_func)(FALSE, 1, NULL, NULL);
  167.                 /* function may have switched on us */
  168.                 swbuffer(wp->w_bufp);
  169.                 lp = wp->w_dot.l;
  170.                 havemotion = NULL;
  171.                 calledbefore = TRUE;
  172.             }
  173.         }
  174.         lp = lforw(lp);
  175.     }
  176.  
  177.  
  178.     return s;
  179. }
  180.  
  181. #else
  182. globalhello() { }
  183. #endif
  184.