home *** CD-ROM | disk | FTP | other *** search
/ Hot Shareware 35 / hot35.iso / ficheros / 9TXT / ZE32V270.ZIP / WORDCOMP.ZM_ / WORDCOMP.ZM
Text File  |  1997-10-04  |  7KB  |  269 lines

  1. /*************************************************************************
  2.  
  3.         Name: WordComplete
  4.  
  5.  Description: This macro provides a  word completion search feature.  To
  6.               use this macro either run it directly or install it to the
  7.               Macros menu using the Options Macros menu item. To use the
  8.               macro just run it within or at the end of the current word
  9.               that is to be completed. The following navigation keys are
  10.               also supported:
  11.  
  12.                 ESC        - cancel the completion
  13.                 Enter      - to accept the completed word
  14.                 ArrowUp    - search for previous occurrence
  15.                 ArrowDown  - search for next occurrence
  16.  
  17.               NOTE: Note that this macro does a case insensitive search. 
  18.  
  19.       Author: Brander, Bertel K. 
  20.  
  21.      Contact: bertel@post4.tele.dk
  22.  
  23. **************************************************************************/
  24.  
  25. int WordComplete()
  26. {
  27.   int    a;
  28.   int    b;
  29.   int    search_x;
  30.   int    search_y;
  31.   int    end;
  32.   char   ch;
  33.   string org_txt;
  34.   string new_txt;
  35.  
  36.   int sResult = is_document();
  37.  
  38.   // make sure it is a document window
  39.   if (is_document() == 0)
  40.   {
  41.      message("This macro only works for document files!");
  42.      beep();
  43.      return;
  44.   }
  45.  
  46.   key_left      = 37;
  47.   key_up        = 38;
  48.   key_right     = 39;
  49.   key_down      = 40;
  50.   key_escape    = 27;
  51.   key_backspace = 8;
  52.   key_enter     = 13;
  53.  
  54.   ch    = 0;
  55.   end   = 0;
  56.   found = 0;
  57.  
  58.   MoveLineLeft();
  59.   org_txt = "$W";
  60.   if(strlen(org_txt) == 0)
  61.   {
  62.     message("No word found to complete!");
  63.     beep();
  64.   }
  65.   else
  66.   {
  67.     MarkWordCurrent();
  68.     MarkDelete();
  69.  
  70.     cursor_save();            // save the start position of the word to be completed
  71.     search_save();            // save search engine settings
  72.     search_update(0);         // disable the search screen updates
  73.  
  74.     SearchCaseReset();        // no case sensitive search
  75.     SearchWordReset();        // no whole word search
  76.     SearchRegexpReset();      // no regex search
  77.                               
  78.     search_x = 0;             // start at the start of document
  79.     search_y = 0;
  80.  
  81.     set_find_text(org_txt);
  82.     
  83.     set_line_pos(search_y);
  84.     set_column_pos(search_x);
  85.  
  86.     while(found == 0)
  87.     {
  88.       found = SearchNext();
  89.  
  90.       if(found == 1)
  91.       { // A match is found
  92.         new_txt = "$W";
  93.         MoveLineRight();
  94.         if (stricmp(org_txt, new_txt) == 0)
  95.         {
  96.           // false alarm
  97.           found = 0;
  98.         }
  99.         else
  100.         {
  101.           get_column_pos(search_x);
  102.           get_line_pos(search_y);
  103.           cursor_restore();
  104.           put_string(new_txt);
  105.         }
  106.       }
  107.       else
  108.       {
  109.         message("No match found.");
  110.         cursor_restore();
  111.         put_string(org_txt);
  112.         end = -1;
  113.         found = 1;
  114.       }
  115.     }
  116.  
  117.     while(end == 0)
  118.     {
  119.       // gets ascii keys and extended keys
  120.       ch = getch(sExtended);
  121.       if(ch != -1) 
  122.       { // Is a key hit
  123.         if(ch == key_enter)
  124.         { // We are done
  125.           end = 1;
  126.         }
  127.         else if(ch == key_escape)
  128.         { // Put in the old word
  129.           cursor_restore();
  130.           MarkWordCurrent();
  131.           MarkDelete();
  132.           cursor_restore();
  133.           put_string(org_txt);
  134.           end = 1;
  135.         }
  136.         else if((ch == 0) && (sExtended == key_down))
  137.         { // Find the next match
  138.           set_line_pos(search_y);
  139.           set_column_pos(search_x);
  140.           a = 0;
  141.           b = 0;
  142.  
  143.           while(a == 0)
  144.           {
  145.             found = SearchNext();
  146.             if(found == 0)
  147.             { // No new match is found
  148.               a = 1;
  149.               b = 1;
  150.             }
  151.             else
  152.             {
  153.               // make sure it is a new word
  154.               a = stricmp(org_txt, "$W");
  155.  
  156.               if (a)
  157.               {
  158.                 a = stricmp(new_txt, "$W");
  159.               }
  160.             }
  161.           }
  162.           if(b == 0)
  163.           { // A match is found
  164.             if(found == 0)
  165.             { // The word where we started, search again
  166.               get_column_pos(search_x);
  167.               get_line_pos(search_y);
  168.               found = SearchNext();
  169.               if(found == 0)
  170.               {
  171.                 message("No match found.");
  172.                 MoveWordNext();
  173.               }
  174.             }
  175.             if(found == 1)
  176.             { // Ok, insert new word
  177.               new_txt = "$W";
  178.  
  179.               get_column_pos(search_x);
  180.               get_line_pos(search_y);
  181.               cursor_restore();
  182.               // Remove the old word
  183.               MoveLineRight();
  184.               MarkWordCurrent();
  185.               MarkDelete();
  186.               put_string(new_txt);
  187.             }
  188.           }
  189.           else
  190.           {
  191.             message("No match found.");
  192.             cursor_restore();
  193.             MoveWordNext();
  194.           }
  195.         }
  196.         else if((ch == 0) && (sExtended == key_up))
  197.         { // Find the previous match
  198.           set_line_pos(search_y);
  199.           set_column_pos(search_x);
  200.           a = 0;
  201.           b = 0;
  202.  
  203.           while(a == 0)
  204.           {
  205.             found = SearchPrevious();
  206.             if(found == 0)
  207.             { // No new match is found
  208.               a = 1;
  209.               b = 1;
  210.             }
  211.             else
  212.             {
  213.               // make sure it is a new word
  214.               a = stricmp(org_txt, "$W");
  215.  
  216.               if (a)
  217.               {
  218.                 a = stricmp(new_txt, "$W");
  219.               }
  220.             }
  221.           }
  222.           if(b == 0)
  223.           { // A match is found
  224.             if(found == 0)
  225.             { // The word where we started, search again
  226.               found = SearchPrevious();
  227.               if(found == 0)
  228.               {
  229.                 message("No match found.");
  230.                 MoveWordNext();
  231.               }
  232.             }
  233.             if(found == 1)
  234.             { // Ok, insert new word
  235.               new_txt = "$W";
  236.               get_column_pos(search_x);
  237.               get_line_pos(search_y);
  238.               cursor_restore();
  239.               // Remove the old word
  240.               MoveLineRight();
  241.               MarkWordCurrent();
  242.               MarkDelete();
  243.               put_string(new_txt);
  244.             }
  245.           }
  246.           else
  247.           {
  248.             message("No match found.");
  249.             cursor_restore();
  250.             MoveWordNext();
  251.           }
  252.         }
  253.         else if (ch != 0)
  254.         {
  255.           // any other key and just beep
  256.           beep();
  257.           message("Currently running the Word Completion macro. Use the ESC key to cancel.");
  258.         }
  259.       }
  260.     }
  261.  
  262.     search_restore();  // restore the search engine
  263.     search_update(1);  // restore the search screen updating
  264.  
  265.     if(end != -1) message("Word completion ended.");
  266.   }
  267. }
  268.  
  269.