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

  1. //--------------------------------------------------------------------------
  2. // Object Scripting
  3. // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
  4. //
  5. // CODELIB.SPP: Code Library. Displays libraries of code snippets you can
  6. //   insert in the current buffer. You can also edit code library data
  7. //   files, and create library entries from selected text. You can create
  8. //   as many code libraries as you want.
  9. //
  10. // USE: Set values in codelib.cfg. The default values provide five libraries
  11. //   (general, script, C++, OWL, and Win32). Run script. Select an entry
  12. //   to insert it in the current buffer, or select another library. To add
  13. //   the currently selected text to a library, or to edit the current
  14. //   library's data file, select the corresponding commands.
  15. //
  16. // FILES: CODE*.DAT, CODELIB.CFG, MSG.SPP, FILE.SPP, MISC.SPP
  17. //
  18. // NOTES: All code*.* files must reside in the same directory.
  19. //--------------------------------------------------------------------------
  20. print typeid(module());
  21.  
  22. //
  23. // IDE imports.
  24. //
  25. import IDE;
  26. import scriptEngine;
  27. import editor;
  28.  
  29. //
  30. // Load support module(s).
  31. //
  32. if (!scriptEngine.IsLoaded("msg")) scriptEngine.Load("msg");
  33. if (!scriptEngine.IsLoaded("file")) scriptEngine.Load("file");
  34. if (!scriptEngine.IsLoaded("misc")) scriptEngine.Load("misc");
  35.  
  36. //
  37. // Constants for the list windows.
  38. //
  39. #define LIST_X   100
  40. #define LIST_Y   10
  41. #define LIST_H   350   
  42. #define LIST_W   525
  43.  
  44. //
  45. // Constants for the list window command entries text.
  46. //
  47. #define EDIT_ITEM_TXT  "(Edit data file for this library)"
  48. #define ADD_ITEM_TXT   "(Create entry for this library from selected text)"
  49.  
  50. sTmp      = new String();  // Temporary string object.
  51. Msg       = new TMsg();    // Message object.
  52. Libraries = new array[];   // Array of library objects.
  53.  
  54. EntryMarker;  // String used to separate library entries.
  55. NumEntries;   // Number of items in the list window that are library entries.
  56. CurLib;       // Index of the current library.
  57.  
  58. ModuleDir = GetModuleDir(typeid(module()));  // Directory of this script.
  59.  
  60. //
  61. // Create a list window to display the code libraries.
  62. //
  63. LibWnd = new ListWindow(LIST_X, LIST_Y, LIST_H, LIST_W, NULL,
  64.                         FALSE, FALSE, NULL);
  65.  
  66. codelib()
  67. {
  68.   // Check for a buffer.
  69.   //
  70.   if (!editor.TopView) {
  71.     Msg.Info("You need an open edit buffer to use the code library.");
  72.     return;
  73.   }
  74.  
  75.   //
  76.   // Load configuration values. Dynamically build key names to accommodate
  77.   // variable number of libraries.
  78.   //
  79.   declare CFGFile = new TConfigFile(ModuleDir + "\\codelib.cfg");
  80.   EntryMarker = CFGFile.GetValue("EntryMarker", "<*>");
  81.   declare i = 0;
  82.   declare descr;
  83.   while (descr = CFGFile.GetValue("Lib" + (i + 1), "")) {
  84.     Libraries[i] = new TLibrary(descr, CFGFile.GetValue("DAT" + (i + 1), ""));
  85.     i++;
  86.   }
  87.   CFGFile.Close();
  88.  
  89.   // Display the first library.
  90.   //
  91.   DisplayLib(0);
  92. }
  93.  
  94. //
  95. // class TLibrary
  96. // ~~~~~ ~~~~~~~~
  97. // TLibrary encapsulates library information.
  98. //
  99. class TLibrary(descr, dataFile) {
  100.   declare Descr    = descr;
  101.   declare DataFile = dataFile;
  102. }
  103.  
  104. //
  105. // Displays a library.
  106. //
  107. DisplayLib(index)
  108. {
  109.   CurLib = index;
  110.   declare description = Libraries[index].Descr;
  111.   declare dataFile    = Libraries[index].DataFile;
  112.  
  113.   LibWnd.Execute();
  114.   LibWnd.Clear();
  115.   LibWnd.Caption = description;
  116.   LibWnd.Hidden = TRUE;
  117.  
  118.   // Get the library's entry descriptions.
  119.   //
  120.   declare file = new TFlatFile(ModuleDir + "\\" + dataFile);
  121.   if (!file.IsValid()) {
  122.     Msg.Error("The data file for this library is not valid.");
  123.     return;
  124.   }
  125.   declare lines = file.GetLines(EntryMarker);
  126.   file.Close();
  127.  
  128.   // Add the entry descriptions, first removing entry markers.
  129.   //
  130.   sTmp.Text = EntryMarker;
  131.   declare markerLen = sTmp.Length;
  132.   declare output;
  133.   iterate (output; lines) {
  134.     sTmp.Text = output;
  135.     sTmp = sTmp.SubString(markerLen);
  136.     LibWnd.Add(sTmp.Text, LibWnd.Count);
  137.   }
  138.   NumEntries = LibWnd.Count;
  139.  
  140.   // Add items for the library entries.
  141.   //
  142.   declare lib;
  143.   iterate (lib; Libraries) {
  144.     LibWnd.Add("-> " + lib.Descr, LibWnd.Count);
  145.   }
  146.  
  147.   // Add items for editing data files and adding entries.
  148.   //
  149.   LibWnd.Add(EDIT_ITEM_TXT, LibWnd.Count);
  150.   LibWnd.Add(ADD_ITEM_TXT, LibWnd.Count);
  151.  
  152.   IDE.KeyboardManager.SendKeys("{VK_HOME}", TRUE);
  153.   LibWnd.Hidden = FALSE;
  154. }
  155.  
  156. //
  157. // Handle item selection.
  158. //
  159. on LibWnd:>Accept()
  160. {
  161.   declare curView = editor.TopView;
  162.  
  163.   // The selection may be another library, not a code entry.
  164.   //
  165.   sTmp.Text = .GetString(.CurrentIndex);
  166.   if (sTmp.Index("->")) {
  167.     DisplayLib(.CurrentIndex - NumEntries);
  168.   }
  169.  
  170.   // The selection may be the "edit" entry.
  171.   //
  172.   else if (sTmp.Text == EDIT_ITEM_TXT) {
  173.     declare dataFile = Libraries[CurLib].DataFile;
  174.     if (!IDE.FileOpen(ModuleDir + "\\" + dataFile, "EditText")) {
  175.       Msg.Error("Could not open the library's data file.");
  176.     }
  177.     .Close();
  178.   }
  179.  
  180.   // The selection may be the "add" entry.
  181.   //
  182.   else if (sTmp.Text == ADD_ITEM_TXT) {
  183.     declare description;
  184.     description = IDE.SimpleDialog("Enter a description of the code.", NULL);
  185.     if (description) {
  186.       .Close();
  187.       if (curView.Block.IsValid) {
  188.         declare text = EntryMarker + description + "\n\r" +
  189.                        curView.Block.Text;
  190.         declare dataFile = new TFlatFile(ModuleDir + "\\" +
  191.                                          Libraries[CurLib].DataFile);
  192.         dataFile.Append(text);
  193.         dataFile.Close(TRUE);
  194.       }
  195.       else {
  196.         Msg.Warn("The selection you want to add is not valid.");
  197.       }
  198.     }
  199.   }
  200.  
  201.   // The selection is a code library entry.
  202.   //
  203.   else {
  204.  
  205.     // Verify there still is an open buffer.
  206.     //
  207.     if (!initialized(editor.TopView)) {
  208.       Msg.Info("You need an open edit buffer for the library entry.");
  209.       .Close();
  210.       return;
  211.     }
  212.  
  213.     InsertEntry(sTmp.Text);
  214.     .Close();
  215.     pass();
  216.   } 
  217. }
  218.  
  219. InsertEntry(entry)
  220. {
  221.   // Get the code snippet.
  222.   //
  223.   declare file = new TFlatFile(ModuleDir + "\\" + Libraries[CurLib].DataFile);
  224.   declare code = file.GetSandwichedText(entry, EntryMarker, TRUE);
  225.   if (code == NULL) {
  226.     Msg.Error("Could not get the code snippet.");
  227.   }
  228.   else {
  229.  
  230.     // Remove leading newline and any trailing marker.
  231.     //
  232.     sTmp.Text = code;
  233.     sTmp = sTmp.Trim(TRUE);
  234.     declare position;
  235.     if (position = sTmp.Index(EntryMarker)) {
  236.       sTmp = sTmp.SubString(0, position - 1);
  237.     }
  238.  
  239.     // Insert the code in the current buffer, and select and center it.
  240.     //
  241.     declare edPos = editor.TopView.Position;
  242.     declare edBlk = editor.TopView.Block;
  243.     declare row = edPos.Row;
  244.     edPos.InsertText(sTmp.Text);
  245.     edBlk.End();
  246.     edPos.Move(row, 1);
  247.     edBlk.Begin();
  248.     editor.TopView.Center(row, 1);
  249.   }
  250. }