home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / SCRPTEXM.PAK / APIEXP.SPP < prev    next >
Encoding:
Text File  |  1997-05-06  |  5.6 KB  |  218 lines

  1. //--------------------------------------------------------------------------
  2. // Object Scripting
  3. // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
  4. //
  5. // APIEXP.SPP: API Expander. Expands current word in editor to the matching
  6. //   Windows API or C RTL signiture. Provides selection list if seed string
  7. //   has multiple matches. If the match is an RTL member, API Expander can
  8. //   almost always indicate if the corresponding header file needs to be
  9. //   included in the source file.
  10. //
  11. // USE: Run with cursor on or just to the right of the word to expand.
  12. //   Matching is case-sensitive.
  13. //
  14. // FILES: APIEXP.DAT, MSG.SPP, FILE.SPP, MISC.SPP
  15. //
  16. // NOTES: All apiexp.* files must reside in the same directory. The DAT file
  17. //   does not currently contain every Windows API.
  18. //--------------------------------------------------------------------------
  19. print typeid(module());
  20.  
  21. //
  22. // IDE imports.
  23. //
  24. import IDE;
  25. import scriptEngine;
  26. import editor;
  27.  
  28. //
  29. // Load support module(s).
  30. //
  31. if (!scriptEngine.IsLoaded("msg")) scriptEngine.Load("msg");
  32. if (!scriptEngine.IsLoaded("file")) scriptEngine.Load("file");
  33. if (!scriptEngine.IsLoaded("misc")) scriptEngine.Load("misc");
  34.  
  35. Msg = new TMsg();  // Message object.
  36.  
  37. //
  38. // Create an expander object.
  39. //
  40. Expander = new TExpander(GetModuleDir(typeid(module())) + "\\apiexp.dat", TRUE);
  41.  
  42. apiexp()
  43. {
  44.   // Execute the expander object to expand the current word.
  45.   //
  46.   Expander.Expand();
  47. }
  48.  
  49. //
  50. // class TExpander
  51. // ~~~~~ ~~~~~~~~~
  52. // TExpander encapsulates word-expansion functionality.
  53. //
  54. class TExpander(dataFile, caseSensitive) {
  55.   declare Matches;
  56.   declare CaseSensitive = caseSensitive;
  57.   declare CurView;
  58.   declare EdPos;
  59.   declare EdBlk;
  60.  
  61.   // Instantiate a list object for selection of multiple matches.
  62.   //
  63.   declare SelList = new ListWindow(50, 50, 200, 550,
  64.                                    "Multiple matches found.", FALSE, FALSE,
  65.                                    NULL);
  66.  
  67.   // Instantiate a file object for the data file and make sure it exists.
  68.   //
  69.   declare File = new TFlatFile(dataFile);
  70.   if (!File.IsValid()) {
  71.     Msg.Error("The data file is not valid.");
  72.   }
  73.  
  74.   //
  75.   // Returns the word the cursor is on, or just to the right of. 
  76.   // Leaves seed as the current block with the cursor at the beginning.
  77.   //
  78.   GetSeed()
  79.   {
  80.     EdPos.MoveCursor(SKIP_NONWHITE | SKIP_RIGHT);
  81.     EdBlk.End();
  82.     EdPos.MoveCursor(SKIP_LEFT);
  83.     EdBlk.Begin();
  84.     return EdBlk.Text;    
  85.   }
  86.  
  87.   //
  88.   // Expands the word the cursor is on, or just to the right of. 
  89.   //
  90.   Expand()
  91.   {
  92.     // Validate the buffer.
  93.     //
  94.     if (!editor.TopView.IsValid) {
  95.       Msg.Info("You must have an open buffer for expansion.");
  96.       return;
  97.     }
  98.  
  99.     // Assign edit objects.
  100.     //
  101.     CurView = editor.TopView;
  102.     EdPos = CurView.Position;
  103.     EdBlk = CurView.Block;
  104.  
  105.     // Get the seed word.
  106.     //
  107.     declare seed = GetSeed();
  108.     if (seed == "") {
  109.       Msg.Warn("There is no seed word to expand.");
  110.       return;
  111.     }
  112.  
  113.     // Get the matches. Always search at the beginning of a data line.
  114.     //
  115.     File.EdPos.Move(1, 1);
  116.     declare matches = 0;
  117.     Matches = new array[];
  118.     declare sLine = new String();
  119.     while (File.EdPos.Search("^" + seed, CaseSensitive, TRUE, NULL, IDE_RE)) {
  120.       File.EdPos.MoveBOL();
  121.       sLine.Text = File.EdPos.Read();
  122.       sLine = sLine.Trim();
  123.       Matches[matches] = sLine.Text;
  124.       File.EdPos.MoveEOL();
  125.       matches++;
  126.     }
  127.  
  128.     // Respond to number of matches.
  129.     //
  130.     switch (matches) {
  131.       case 0:
  132.         Msg.Info("No expansion match.");
  133.         break;
  134.  
  135.       case 1:  // Single match.
  136.         InsertMatch(sLine.Text);
  137.         break;
  138.  
  139.       default:  // Multiple matches.
  140.         BuildSelectionList();
  141.     }
  142.   }
  143.  
  144.   //
  145.   // Inserts the match in the buffer. Performs header checking if the match
  146.   // is an RTL item.
  147.   //
  148.   InsertMatch(line)
  149.   {
  150.     // If match is from the RTL, remove the header file info.
  151.     //
  152.     declare sLine = new String(line);
  153.     declare position = sLine.Index(".h");
  154.     declare headerFile;
  155.     if (position) {
  156.       headerFile = sLine.SubString(sLine.Index(";")).Text;
  157.       sLine = sLine.SubString(0, sLine.Index(";"));
  158.     }
  159.  
  160.     // Insert the match, positioning the caret at the open parens.
  161.     //
  162.     EdBlk.Delete();
  163.  
  164.     EdPos.InsertText(sLine.Text);
  165.     declare openParens = sLine.Index("(");
  166.     EdPos.MoveRelative(NULL, openParens - sLine.Length);
  167.  
  168.     // If RTL item, attempt to see if the needed header is included.
  169.     //
  170.     if (position) {
  171.       declare edBuf = new EditBuffer(CurView.Buffer.FullName, TRUE, TRUE);
  172.       edBuf.Position.Move(1, 1);
  173.  
  174.       if (!edBuf.Position.Search("include*" + headerFile, FALSE, TRUE,
  175.                                  SEARCH_FORWARD, BRIEF_RE)) {
  176.         Msg.Info("You need to include " + headerFile);
  177.       }
  178.       edBuf.Destroy();
  179.     }
  180.   }
  181.  
  182.   //
  183.   // Run the multiple matches list. 
  184.   //
  185.   BuildSelectionList()
  186.   {
  187.     SelList.Hidden = TRUE;
  188.     SelList.Execute();
  189.     SelList.Clear();
  190.     declare match;
  191.     iterate (match ; Matches) {
  192.       SelList.Add(match, SelList.Count);
  193.     }
  194.     IDE.KeyboardManager.SendKeys("{VK_HOME}", TRUE);
  195.     SelList.Hidden = FALSE;
  196.   }
  197. }
  198.  
  199. //
  200. // Handle selection of multiple matches.
  201. //
  202. on Expander.SelList:>Accept()
  203. {
  204.   //
  205.   // Get the text, and clear and close the window. Leaving the list up
  206.   // interferes with the insertion.
  207.   //
  208.   declare expansionText = .GetString(.CurrentIndex);
  209.   .Close();
  210.  
  211.   // Insert the selected match.
  212.   //
  213.   Expander.InsertMatch(expansionText);
  214. }
  215.  
  216.  
  217.  
  218.