/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
/*                                                                     */
/*        P�l Hedne 1990                                               */
/*                                                                     */
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
_init (...)
{
  int match_key,goto;
  global match_key,goto;

       match_key=0;
/*  if (!inq_macro("latex")) load_macro ("latex");*/
  load_macro ("latex");
/*   if (!inq_macro("latexh")) load_macro ("latexh");*/
  load_macro ("latexh");
       assign_to_key ("<Ctrl-F4>", "find_match");
       assign_to_key ("<Shift-F4>", "disp_match");
}

/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
extern gen_help,
       _reg_exp,                                       //      Regular expressions on/off.
               _block_search;                          //      Block search on/off.

find_match (...)
{
       int line,col,line1,col1,match,ind,length,maxlength,
      buf_key,nwrite,dum,key_save,key_next;
  string text,label,pattern,fname,ext,bname,sdum;


/*      pause_on_error (1);  */


  maxlength=30;
  pattern="";
       sprintf (text, "Global search %sfor: ", _reg_exp ? "" : "(RE off) ");
  if (get_parm (0, pattern, text, NULL,pattern))
     {
     key_save=inq_buffer();
     inq_names(fname,ext,bname);
     bname=upper(bname);
     do
     {
       sprintf (text,"%s: Search for %s ",bname,pattern);
        message(text);
       inq_position (line, col);
        top_of_buffer();

       sprintf (text,"Buffer %s",bname);
        match_key=create_buffer(text,NULL,1);
        nwrite=0;
        match=1;
       while (match > 0)
        {
                               match=search_fwd(pattern,_reg_exp,NULL,_block_search);
           if (match > 0)
                         {
              buf_key=inq_buffer();
               inq_position(line1,col1);
               move_abs(line1,1);
                          text = read();
              text=trim(text);
              length=strlen(text);
              label=text;
               while ((ind=search_string(";",text,dum,0,0)) > 0)
                  text=substr(label,1,ind-1)+":"+substr(label,ind+1,length);
              if (length>maxlength) maxlength=length;
              while (strlen(text)<maxlength) text=text+" ";
              set_buffer(match_key);
              insert("Line %d: %s  ;go_to_line %c%d%c%d%c\n",line1,text,34,line1,34,col1,34);
              set_buffer(buf_key);
              ++nwrite;
                        move_abs(line1+1,1);
             }
        }
        if (nwrite>0)
           {
            message(" ");
            label=" ";
            text=gen_help(label,match_key);
            if (goto == 1)
              {
               attach_buffer(buf_key);
               break;
              }
           }
/*         else
           {
            text="No matches in file. Press return.";
            get_parm (0, sdum, text);
           }*/
        text="Continue with next buffer ? (Y/N)";
        get_parm (0, sdum, text, 1);
        sdum=upper(sdum);
        if (substr(sdum,1,1) != "Y")
           {
            key_next=key_save;
            set_buffer(key_next);
            move_abs (line, col);
           }
        else
           {
            key_next=next_buffer();
            set_buffer(key_next);
            inq_names(fname,ext,bname);
            delete_buffer(match_key);
            if (key_next == key_save) message("No more buffers.");
           }
/*            pause_on_error (0);  */
     }
     while (key_next != key_save);
  }
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
go_to_line(...)
{
  int line,col;

       if (get_parm(0,line) && get_parm(1,col))
     {
      goto=1;
      move_abs(line,col);
     }
  else
    goto=0;
}
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
disp_match (...)
{
  string fname;
  fname=" ";
  if (match_key>0) fname=gen_help(fname,match_key);
}