home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / globals.c < prev    next >
C/C++ Source or Header  |  1998-05-20  |  3KB  |  130 lines

  1. /* ed/vi/ex style global commands, where first the file is scanned for
  2.  *    matching lines, then for each such line, an action is performed.
  3.  *    written for vile: Copyright (c) 1990, 1995 by Paul Fox
  4.  *
  5.  * $Header: /usr/build/vile/vile/RCS/globals.c,v 1.43 1998/05/21 00:37:16 bod Exp $
  6.  *
  7.  */
  8.  
  9. #include    "estruct.h"
  10. #include        "edef.h"
  11. #include    "nefunc.h"
  12.  
  13. static    int    globber (CMD_ARGS, int g_or_v);
  14.  
  15. int
  16. globals(int f, int n)
  17. {
  18.     return globber(f,n,'g');
  19. }
  20.  
  21. int
  22. vglobals(int f, int n)
  23. {
  24.     return globber(f,n,'v');
  25. }
  26.  
  27. /* ARGSUSED */
  28. static int
  29. globber(int f GCC_UNUSED, int n GCC_UNUSED, int g_or_v)
  30. {
  31.     int c, s;
  32.     register LINE *lp;
  33.     register char *fnp;    /* ptr to the name of the cmd to exec */
  34.     char    cmd[NLINE];
  35.     const CMDFUNC *cfp;
  36.     int foundone;
  37.     WINDOW *wp, *sw_wp;
  38.     L_NUM    before;
  39.     int    save_report;
  40.  
  41.     c = kbd_delimiter();
  42.     if (readpattern("global pattern: ", &pat[0], &gregexp, c, FALSE) != TRUE) {
  43.         mlforce("[No pattern.]");
  44.         return FALSE;
  45.     }
  46.  
  47.     /* in some sense, it would be nice to search first, before
  48.                 making the user answer the next question, but the
  49.                 searching delay is too long, and unexpected in the
  50.                 middle of a command.  */
  51.  
  52.     fnp = kbd_engl("action to perform on each matching line: ", cmd);
  53.     /* get the name of, and then the function to execute */
  54.     if (!fnp) {
  55.             mlforce("[No function]");
  56.         return FALSE;
  57.     } else if ((cfp = engl2fnc(fnp)) == 0) {
  58.             return no_such_function(fnp);
  59.     } else if ((cfp->c_flags & GLOBOK) == 0) {
  60.             mlforce("[Function not allowed]");
  61.         return FALSE;
  62.     }
  63.  
  64.  
  65.     /* call the searcher, telling it to do line marking */
  66.     s = fsearch(FALSE,0,TRUE,FALSE);
  67.     if (s != TRUE)
  68.         return s;
  69.  
  70.     calledbefore = FALSE;
  71.  
  72.     if (g_or_v == 'v') {  /* invert the sense of all the matches */
  73.         for_each_line(lp, curbp)
  74.             lflipmark(lp);
  75.     }
  76.     /* loop through the buffer -- we must clear the marks no matter what */
  77.     s = TRUE;
  78.     lp = lforw(buf_head(curbp));
  79.     wp = sw_wp = curwp;
  80.     /* loop until there are no marked lines in the buffer */
  81.     foundone = FALSE;
  82.     before = line_count(curbp);
  83.     save_report = global_g_val(GVAL_REPORT);
  84.     for_ever {
  85.         if (lp == win_head(wp)) {
  86.             /* at the end -- only quit if we found no
  87.                 marks on the last pass through. otherwise,
  88.                 go through again */
  89.             if (foundone)
  90.                 foundone = FALSE;
  91.             else
  92.                 break;
  93.             lsetnotmarked(lp); /* always unmark the header line */
  94.         }
  95.         if (lismarked(lp)) {
  96.             foundone = TRUE;
  97.             lsetnotmarked(lp);
  98.             /* call the function, if there is one, and results
  99.                 have been ok so far */
  100.             if (cfp && s) {
  101.                 if (!calledbefore && (cfp->c_flags & UNDO)) {
  102.                     if (b_val(wp->w_bufp,MDVIEW))
  103.                         return(rdonly());
  104.                     mayneedundo();
  105.                     set_global_g_val(GVAL_REPORT,0);
  106.                 }
  107.                 havemotion = &f_godotplus;
  108.                 wp->w_dot.l = lp;
  109.                 wp->w_dot.o = 0;
  110.                 s = call_cmdfunc(cfp, FALSE, 1);
  111.                 if (curwp != wp) {
  112.                     /* function may have switched on us */
  113.                     sw_wp = curwp;
  114.                     (void)set_curwp(wp);
  115.                 }
  116.                 lp = wp->w_dot.l;
  117.                 havemotion = NULL;
  118.                 calledbefore = TRUE;
  119.             }
  120.         }
  121.         lp = lforw(lp);
  122.     }
  123.     set_global_g_val(GVAL_REPORT,save_report);
  124.     (void)line_report(before);
  125.     /* if it tried to switch, do it now */
  126.     (void)set_curwp(sw_wp);
  127.  
  128.     return s;
  129. }
  130.