home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ME22-OS2.ZIP / MACROS.ZIP / SRCHLIST.M < prev    next >
Text File  |  1988-01-01  |  3KB  |  112 lines

  1. /* The srchlist command finds all occurences of a pattern in a file, and */
  2. /* displays all lines which contain that pattern in another window. */
  3.  
  4. init()
  5. {
  6.   assign_key("srchlist", 19);   /* CTRL-S */
  7. }
  8.  
  9.  
  10. srchlist()
  11. {
  12.   string pattern;               /* pattern to search for */
  13.   string foo, line;
  14.   int temp_buf, old_buf;        /* buffer ids */
  15.   int found;
  16.  
  17.   found = 0;                    /* boolean to record if we found any matches */
  18.   old_buf = currbuf();
  19.   temp_buf = create_buffer("$$$list$");   /* create a new buf to hold matches */
  20.  
  21.   /* Go to the beginning of the file in order to start the matching. */
  22.   save_position();
  23.   gobof();
  24.  
  25.   /* Prompt the user for the pattern to search for. */
  26.   pattern = get_tty_str("Pattern : ");
  27.   if (strlen(pattern) < 1)  return;
  28.  
  29.   while (fsearch(pattern))
  30.   {
  31.     found = found + 1;          /* found a match!!! */
  32.     line = sprintf("%d: %s\n", currlinenum(), currline());
  33.  
  34.     /* Insert the matched line (with its line number) in the temp buffer */
  35.     setcurrbuf(temp_buf);
  36.     goeof();
  37.     insert(line);
  38.  
  39.     /* Go back to the file and resume matching at the next line. */
  40.     setcurrbuf(old_buf);
  41.     if (!down())  break;
  42.     gobol();
  43.   }
  44.  
  45.   if (found == 0)
  46.     foo = get_tty_str("Pattern not found");
  47.   else
  48.     interact(temp_buf, old_buf);
  49.  
  50.   /* Get rid of the temp buffer and go back to the file */
  51.   delete_buffer(temp_buf);
  52.   show_buffer(old_buf);
  53.   clear_mark();         /* remove all line marks in the buffer */
  54.   restore_position();
  55. }
  56.  
  57.  
  58. /* This is a nice little touch. The user can move up and down the temp buffer, */
  59. /* and when the user hits <RETURN>, the corresponding line in the original */
  60. /* file will be highlighted. */
  61. interact(new_id, old_id)
  62.   int new_id;
  63.   int old_id;
  64. {
  65.   int c, n;
  66.  
  67.   /* Get rid of the blank last line, and move to the first line. */
  68.   show_buffer(new_id);
  69.   goeof();
  70.   delline();
  71.   gobof();
  72.   show_line(new_id, old_id);
  73.   show_help_message();
  74.  
  75.   while ((c = get_tty_char()) != '\E')          /* <ESC> ends it all */
  76.   {
  77.     if (c == 200)               /* <UP> */
  78.       up();
  79.     else if (c == 208)          /* <DOWN> */
  80.       down();
  81.     else if (c == '\n')         /* We picked a line to view */
  82.       show_line(new_id, old_id);
  83.     show_help_message();
  84.   }
  85. }
  86.  
  87.  
  88. /* This routine goes to the old file, and highlights the line you picked. */
  89. show_line(new_id, old_id)
  90.   int new_id;
  91.   int old_id;
  92. {
  93.   int n;
  94.  
  95.   n = atoi(currline());         /* extract the line number of the line */
  96.   if (n > 0)
  97.   {
  98.     show_buffer(old_id);        /* Go to the original file */
  99.     goline(n);                  /*  and move to the desired line. */
  100.     clear_mark();               /* get rid of old highlighting */
  101.     markline();                 /* Highlight the line */
  102.     refresh();                  /* make sure the cursor line catches up */
  103.     show_buffer(new_id);        /* Go back to the temp window. */
  104.   }
  105. }
  106.  
  107. show_help_message()
  108. {
  109.   message(
  110.   "Use the UP and DOWN keys to navigate, <ENTER> to goto line, <ESC> to exit");
  111. }
  112.