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

  1. //--------------------------------------------------------------------------
  2. // Object Scripting
  3. // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
  4. //
  5. // SRCHALL.SPP: Search All. Searches and replaces across files in the
  6. //   current project.
  7. //
  8. // USE: Run script. Invoke SearchAll(topNode) to search and
  9. //   ReplaceAll(topNode) to search and replace. Parameter topNode optionally
  10. //   specifies a node from which to begin the search. Searches include C,
  11. //   CPP, SPP and TXT files. Search/replaces allow the specification of a
  12. //   file mask.
  13. //
  14. // FILES: SEARCH.DLL, MISC.SPP, FOREACH.SPP
  15. //
  16. // NOTES: Search.dll must be available for loading. It is shipped in the
  17. //   BCW bin directory.
  18. //--------------------------------------------------------------------------
  19. print typeid(module());
  20.  
  21. //
  22. // IDE imports.
  23. //
  24. import IDE;
  25. import editor;
  26. import scriptEngine;
  27.  
  28. //
  29. // Load support modules.
  30. //
  31. if (!scriptEngine.IsLoaded("foreach")) scriptEngine.Load("foreach");
  32.  
  33. //
  34. // class TSearchRep
  35. // ~~~~~ ~~~~~~~~~~
  36. // Used to pass parameters to replace.
  37. //
  38. class TSearchRep(search, replace)  {
  39.   declare searchFor = new String(search);
  40.   declare replaceWith = new String(replace);
  41. }
  42.  
  43. //
  44. // Get a handle to the current module. Used in passing "global" functions.
  45. //
  46. Mod = module();
  47.  
  48. TopMessage = NULL;  // Message database ID of parent message.
  49.  
  50. //
  51. // Import a custom dialog function from srchrep.dll.
  52. //
  53. import "search.dll" {
  54.   int  searchReplaceDialog  (int, char *, char *, char *);
  55. }
  56.  
  57. //
  58. // Import needed Windows APIs.
  59. //
  60. import "user32.dll" {
  61.     int GetActiveWindow();
  62. }
  63.  
  64. Search(fileName, parentName, searchFor)
  65. {
  66.   if (TopMessage == NULL) {
  67.     TopMessage = IDE.MessageCreate("SearchA&ll", "transfer", INFORMATION, 0,
  68.                                    "Searching nodes for '" +
  69.                                    searchFor + "'", 0, 0, NULL, 0, 0);
  70.   }
  71.  
  72.   declare del = 0;
  73.  
  74.   if (searchFor != "") {
  75.     declare newEdView;
  76.     declare pos;
  77.  
  78.     if (editor.TopView.Buffer.FileName == fileName) {
  79.       newEdView = editor.TopView;
  80.       pos = newEdView.Position;
  81.       pos.Save();
  82.       pos.Move(1, 1);
  83.     }
  84.     else {
  85.       newEdView = new EditBuffer(fileName, TRUE);
  86.       del = 1;
  87.       pos = newEdView.Position;
  88.       pos.Move(1, 1);
  89.     }
  90.  
  91.     if (pos.Search(searchFor, FALSE, FALSE) == TRUE) {
  92.       do {
  93.         IDE.MessageCreate("SearchA&ll", "", INFORMATION, TopMessage,
  94.                           fileName, pos.Row, pos.Column, "Found", 0, 0);
  95.       } while (pos.SearchAgain());
  96.     }
  97.     if (del) {
  98.       newEdView.Destroy();
  99.     }
  100.     else {
  101.       pos.Restore();
  102.     }
  103.   }
  104.  
  105.   return OK;
  106. }
  107.  
  108. Replace(fileName, parentName, searchReplace)
  109. {
  110.   declare searchFor;
  111.   declare replaceWith;
  112.   declare del = 0;
  113.  
  114.   if (initialized(searchReplace)) {
  115.     searchFor = searchReplace.searchFor;
  116.     replaceWith = searchReplace.replaceWith;
  117.  
  118.     if (TopMessage == NULL) {
  119.       TopMessage = IDE.MessageCreate("R&eplaceAll", "transfer", INFORMATION,
  120.                                      0, "Searching nodes for '" +
  121.                                      searchFor.Text +
  122.                                      "', replacing with '" +
  123.                                      replaceWith.Text + "'",
  124.                                      0, 0, NULL, 0, 0);
  125.     }
  126.  
  127.     if (searchFor.Length != 0 && replaceWith.Length != 0) {
  128.       declare newEdBuffer;
  129.       declare pos;
  130.  
  131.       if (editor.TopView.Buffer.FileName == fileName) {
  132.         pos = editor.TopView.Position;
  133.         pos.Save();
  134.         pos.Move(1, 1);
  135.       }
  136.       else {
  137.         newEdBuffer = new EditBuffer(fileName, TRUE);
  138.         del = 1;
  139.         pos = newEdBuffer.Position;
  140.         pos.Move(1, 1);
  141.       }
  142.  
  143.       if (pos.Replace(searchFor.Text, replaceWith.Text, FALSE, FALSE) != 0) {
  144.         do {
  145.           IDE.MessageCreate("R&eplaceAll", "", INFORMATION, TopMessage,
  146.                             fileName, pos.Row, pos.Column, "Replaced", 0, 0);
  147.         } while (pos.ReplaceAgain());
  148.       }
  149.       if (del) {
  150.         if (newEdBuffer.IsModified) {
  151.           newEdBuffer.Save();
  152.         }
  153.         newEdBuffer.Destroy();
  154.       }
  155.       else {
  156.         pos.Restore();
  157.       }
  158.     }
  159.   }
  160.   else {
  161.     return 0;
  162.   }
  163.  
  164.   return OK;
  165. }
  166.  
  167. SearchAll(node)
  168. {
  169.   declare searchFor;
  170.   searchFor = IDE.SimpleDialog("Search For:", "");
  171.  
  172.   if (searchFor != "") {
  173.     if (initialized(node)) {
  174.       declare prjNode = new ProjectNode(node);
  175.       TraverseProject(node, Mod:>Search, "*.cpp;*.spp;*.txt",
  176.                       searchFor);
  177.     }
  178.     else {
  179.       TraverseProject(NULL, Mod:>Search, "*.cpp;*.spp;*.txt",
  180.                       searchFor);
  181.     }
  182.   }
  183.   
  184.   TopMessage = NULL;
  185.  
  186.   // Show the results.
  187.   //
  188.   IDE.ViewMessage("SearchAll");
  189. }
  190.  
  191. ReplaceAll(node)
  192. {
  193.   declare searchFor;
  194.   declare replaceWith;
  195.   declare mask = "*.cpp;*.spp;*.txt";
  196.   declare retVal = searchReplaceDialog(GetActiveWindow(), searchFor,
  197.                                        replaceWith, mask);
  198.  
  199.   if (retVal) {
  200.     declare str = new TSearchRep(searchFor, replaceWith);
  201.     if (initialized(node)) {
  202.       declare prjNode = new ProjectNode(node);
  203.       TraverseProject(node, Mod:>Replace, mask, str);
  204.     }
  205.     else {
  206.       TraverseProject(NULL, Mod:>Replace, mask, str);
  207.     }
  208.   }
  209.  
  210.   TopMessage = NULL;
  211.  
  212.   // Show the results.
  213.   //
  214.   IDE.ViewMessage("ReplaceAll");
  215. }
  216.  
  217.