home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------
- // Object Scripting
- // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
- //
- // CODELIB.SPP: Code Library. Displays libraries of code snippets you can
- // insert in the current buffer. You can also edit code library data
- // files, and create library entries from selected text. You can create
- // as many code libraries as you want.
- //
- // USE: Set values in codelib.cfg. The default values provide five libraries
- // (general, script, C++, OWL, and Win32). Run script. Select an entry
- // to insert it in the current buffer, or select another library. To add
- // the currently selected text to a library, or to edit the current
- // library's data file, select the corresponding commands.
- //
- // FILES: CODE*.DAT, CODELIB.CFG, MSG.SPP, FILE.SPP, MISC.SPP
- //
- // NOTES: All code*.* files must reside in the same directory.
- //--------------------------------------------------------------------------
- print typeid(module());
-
- //
- // IDE imports.
- //
- import IDE;
- import scriptEngine;
- import editor;
-
- //
- // Load support module(s).
- //
- if (!scriptEngine.IsLoaded("msg")) scriptEngine.Load("msg");
- if (!scriptEngine.IsLoaded("file")) scriptEngine.Load("file");
- if (!scriptEngine.IsLoaded("misc")) scriptEngine.Load("misc");
-
- //
- // Constants for the list windows.
- //
- #define LIST_X 100
- #define LIST_Y 10
- #define LIST_H 350
- #define LIST_W 525
-
- //
- // Constants for the list window command entries text.
- //
- #define EDIT_ITEM_TXT "(Edit data file for this library)"
- #define ADD_ITEM_TXT "(Create entry for this library from selected text)"
-
- sTmp = new String(); // Temporary string object.
- Msg = new TMsg(); // Message object.
- Libraries = new array[]; // Array of library objects.
-
- EntryMarker; // String used to separate library entries.
- NumEntries; // Number of items in the list window that are library entries.
- CurLib; // Index of the current library.
-
- ModuleDir = GetModuleDir(typeid(module())); // Directory of this script.
-
- //
- // Create a list window to display the code libraries.
- //
- LibWnd = new ListWindow(LIST_X, LIST_Y, LIST_H, LIST_W, NULL,
- FALSE, FALSE, NULL);
-
- codelib()
- {
- // Check for a buffer.
- //
- if (!editor.TopView) {
- Msg.Info("You need an open edit buffer to use the code library.");
- return;
- }
-
- //
- // Load configuration values. Dynamically build key names to accommodate
- // variable number of libraries.
- //
- declare CFGFile = new TConfigFile(ModuleDir + "\\codelib.cfg");
- EntryMarker = CFGFile.GetValue("EntryMarker", "<*>");
- declare i = 0;
- declare descr;
- while (descr = CFGFile.GetValue("Lib" + (i + 1), "")) {
- Libraries[i] = new TLibrary(descr, CFGFile.GetValue("DAT" + (i + 1), ""));
- i++;
- }
- CFGFile.Close();
-
- // Display the first library.
- //
- DisplayLib(0);
- }
-
- //
- // class TLibrary
- // ~~~~~ ~~~~~~~~
- // TLibrary encapsulates library information.
- //
- class TLibrary(descr, dataFile) {
- declare Descr = descr;
- declare DataFile = dataFile;
- }
-
- //
- // Displays a library.
- //
- DisplayLib(index)
- {
- CurLib = index;
- declare description = Libraries[index].Descr;
- declare dataFile = Libraries[index].DataFile;
-
- LibWnd.Execute();
- LibWnd.Clear();
- LibWnd.Caption = description;
- LibWnd.Hidden = TRUE;
-
- // Get the library's entry descriptions.
- //
- declare file = new TFlatFile(ModuleDir + "\\" + dataFile);
- if (!file.IsValid()) {
- Msg.Error("The data file for this library is not valid.");
- return;
- }
- declare lines = file.GetLines(EntryMarker);
- file.Close();
-
- // Add the entry descriptions, first removing entry markers.
- //
- sTmp.Text = EntryMarker;
- declare markerLen = sTmp.Length;
- declare output;
- iterate (output; lines) {
- sTmp.Text = output;
- sTmp = sTmp.SubString(markerLen);
- LibWnd.Add(sTmp.Text, LibWnd.Count);
- }
- NumEntries = LibWnd.Count;
-
- // Add items for the library entries.
- //
- declare lib;
- iterate (lib; Libraries) {
- LibWnd.Add("-> " + lib.Descr, LibWnd.Count);
- }
-
- // Add items for editing data files and adding entries.
- //
- LibWnd.Add(EDIT_ITEM_TXT, LibWnd.Count);
- LibWnd.Add(ADD_ITEM_TXT, LibWnd.Count);
-
- IDE.KeyboardManager.SendKeys("{VK_HOME}", TRUE);
- LibWnd.Hidden = FALSE;
- }
-
- //
- // Handle item selection.
- //
- on LibWnd:>Accept()
- {
- declare curView = editor.TopView;
-
- // The selection may be another library, not a code entry.
- //
- sTmp.Text = .GetString(.CurrentIndex);
- if (sTmp.Index("->")) {
- DisplayLib(.CurrentIndex - NumEntries);
- }
-
- // The selection may be the "edit" entry.
- //
- else if (sTmp.Text == EDIT_ITEM_TXT) {
- declare dataFile = Libraries[CurLib].DataFile;
- if (!IDE.FileOpen(ModuleDir + "\\" + dataFile, "EditText")) {
- Msg.Error("Could not open the library's data file.");
- }
- .Close();
- }
-
- // The selection may be the "add" entry.
- //
- else if (sTmp.Text == ADD_ITEM_TXT) {
- declare description;
- description = IDE.SimpleDialog("Enter a description of the code.", NULL);
- if (description) {
- .Close();
- if (curView.Block.IsValid) {
- declare text = EntryMarker + description + "\n\r" +
- curView.Block.Text;
- declare dataFile = new TFlatFile(ModuleDir + "\\" +
- Libraries[CurLib].DataFile);
- dataFile.Append(text);
- dataFile.Close(TRUE);
- }
- else {
- Msg.Warn("The selection you want to add is not valid.");
- }
- }
- }
-
- // The selection is a code library entry.
- //
- else {
-
- // Verify there still is an open buffer.
- //
- if (!initialized(editor.TopView)) {
- Msg.Info("You need an open edit buffer for the library entry.");
- .Close();
- return;
- }
-
- InsertEntry(sTmp.Text);
- .Close();
- pass();
- }
- }
-
- InsertEntry(entry)
- {
- // Get the code snippet.
- //
- declare file = new TFlatFile(ModuleDir + "\\" + Libraries[CurLib].DataFile);
- declare code = file.GetSandwichedText(entry, EntryMarker, TRUE);
- if (code == NULL) {
- Msg.Error("Could not get the code snippet.");
- }
- else {
-
- // Remove leading newline and any trailing marker.
- //
- sTmp.Text = code;
- sTmp = sTmp.Trim(TRUE);
- declare position;
- if (position = sTmp.Index(EntryMarker)) {
- sTmp = sTmp.SubString(0, position - 1);
- }
-
- // Insert the code in the current buffer, and select and center it.
- //
- declare edPos = editor.TopView.Position;
- declare edBlk = editor.TopView.Block;
- declare row = edPos.Row;
- edPos.InsertText(sTmp.Text);
- edBlk.End();
- edPos.Move(row, 1);
- edBlk.Begin();
- editor.TopView.Center(row, 1);
- }
- }