home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / JED / JED097-1.TAR / jed / lib / srchmisc.sl < prev    next >
Encoding:
Text File  |  1994-12-12  |  2.4 KB  |  110 lines

  1. %  These routines are common to both regular expression searches and ordinary
  2. %  searches.
  3.  
  4. define mark_next_nchars (n, dir)
  5. {
  6.    ERROR_BLOCK {pop_mark(0)}
  7.    push_mark(); call ("set_mark_cmd");
  8.    go_right(n);
  9.    if (dir < 0) exchange_point_and_mark ();
  10.    update(1);
  11.    ungetkey(getkey());
  12.    EXECUTE_ERROR_BLOCK;
  13. }
  14.  
  15.  
  16. % The search function is to return: 0 if non-match found or the length of the 
  17. % item matched.
  18. % search_fun takes the pattern to search for and returns the length of the 
  19. % pattern matched.
  20. % rep_fun returns the length of characters replaced.
  21.  
  22. define replace_with_query (search_fun, pat, rep, query, rep_fun)
  23. {
  24.    variable n, prompt, doit, err, ch, pat_len;
  25.    variable last;
  26.    variable rep_len = -1;
  27.  
  28.    ERROR_BLOCK 
  29.      {
  30.     if (rep_len != -1) pop_mark (0);
  31.      }
  32.    
  33.    prompt =  Sprintf ("Replace '%s' with '%s'? (y/n/!/+/q/h)", pat, rep, 2);
  34.    
  35.    while (pat_len = search_fun (pat), pat_len >= 0)
  36.      {
  37.     !if (query)
  38.       {
  39.          () = rep_fun (rep, pat_len); 
  40.          continue;
  41.       }
  42.  
  43.     do 
  44.       {
  45.          message(prompt);
  46.          mark_next_nchars (pat_len, -1);
  47.          
  48.          ch = getkey ();
  49.          if (ch == 'r')
  50.            {
  51.           recenter (window_info('r') / 2);
  52.            }
  53.          
  54.       } while (ch == 'r');
  55.     
  56.     switch(ch)
  57.       { case 'u' and (rep_len >= 0):
  58.          pop_mark (1); push_spot ();
  59.          () = rep_fun (last, rep_len);
  60.          pop_spot ();
  61.          rep_len = -1;
  62.       }   
  63.       { case 'y' :
  64.          if (rep_len != -1) pop_mark (0);
  65.          push_spot(); push_mark ();
  66.          go_right (pat_len); last = bufsubstr ();
  67.          pop_spot (); push_mark ();
  68.          rep_len = rep_fun (rep, pat_len);
  69.       }
  70.       { case 'n' : go_right(1);}
  71.       { case '+' : () = rep_fun (rep, pat_len); 
  72.                    break;
  73.       }
  74.       { case '!' :
  75.          do 
  76.            {
  77.           () = rep_fun (rep, pat_len); 
  78.            }
  79.          while (search_fun (pat) > 0);
  80.       }
  81.           { case 'q' : break; }
  82.           {  pop();
  83.          flush ("y:replace, n:skip, !:replace all, u: undo last, +:replace then quit, q:quit");
  84.          () = input_pending (30); 
  85.       }
  86.      }
  87.    EXECUTE_ERROR_BLOCK;
  88. }
  89.  
  90.  
  91. define search_maybe_again (fun, str, dir)
  92. {
  93.    variable ch, len;
  94.    
  95.    while (len = fun (str, dir), len >= 0)
  96.      {    
  97.     if (EXECUTING_MACRO or DEFINING_MACRO) return 1;
  98.     message ("Press RET to continue searching.");
  99.     mark_next_nchars (len, -1);
  100.     ch = getkey ();
  101.     if (ch != '\r')
  102.       {
  103.          ungetkey (ch);
  104.          return 1;
  105.       }
  106.     if (dir > 0) go_right (1);
  107.      }
  108.    return 0;
  109. }
  110.