Copy current Tcl procedure to the clipboard

Ask questions about how to create a script or swap scripts with other users.
Post Reply
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Copy current Tcl procedure to the clipboard

Post by jgodfrey »

Tcl is a very dynamic language. For example, you can paste an updated copy of a procedure into a running application and have access to the change immediately - without restarting the application. During development and debugging, I tend to use this feature often as it greatly speeds up the entire test cycle (start app, test, change proc, test, change proc, ..., close app).

So, to further speed up the process, here's a TextEd script that will select the current Tcl procedure (the insertion cursor is expected to be within the procedure body when the script is executed) and copy its contents to the clipboard - ready for pasting into a running Tcl application.

Thanks to Rickard for providing some new scripting functionality to make this possible.

***
Edit: I know that Document.GetMethodAtCursor(true) is designed to do just this, but it currently doesn't work consistently for me (likely due to my "in progress" syntax definition file). So, for now, I'll use a more brute-force method of proc selection...
***
Jeff

Code: Select all

#language C++Script

// SelectTclProc.cpp

// Jeff Godfrey, 28-Aug-2011

// Find and select an entire Tcl procedure.  The insertion cursor must be
// within the body of the target proc when this script is executed.

{
   int i, j, braceCount;
   WideString currLine;
   int procStart = -1;
   int procEnd = -1;
   int xPos = Document.CursorX;
   int yPos = Document.CursorY;

   // Search backwards from the cursor position to find "proc" in col 1
   // (of a trimmed line).
   for (i = ypos; i >= 0; i--)
   {
      currLine = Trim(Document.Lines(i));
      if (ScriptUtils.WPosAt("proc", currLine, 1))
      {
         procStart = i;
         break;
      }
   }

   // Couldn't find "proc".  Issue message and exit.
   if (procStart < 0)
   {
      ShowMessage("Can't find start of proc - exiting...");
      exit;
   }

   // Search down from "proc" to find final matching brace
   for (i = procStart; i < Document.LineCount; i++)
   {
      currLine = Document.Lines(i);
      for (j = 1; j <= Length(currLine); j++)
      {
         if (ScriptUtils.WPosAt("{", currLine, j)) { Inc(braceCount); }
         else if (ScriptUtils.WPosAt("}", currLine, j)) { Dec(braceCount); }
      }
      if (braceCount == 0)
      {
         procEnd = i;
         break;
      }
   }

   // Couldn't find matching braces.  Issue message and exit.
   if (procEnd < 0)
   {
      ShowMessage("Can't find end of proc (mismatched braces?) - exiting...");
      exit;
   }

   // Remember the current column select mode
   boolean wasInColMode = Document.IsColumnModeActive();

   // If we're in column select mode, turn it off temporarily
   if (wasInColMode) { Document.SetColumnMode(false); }

   // Select the range of lines
   Document.SetSelection(0, procStart, Length(currLine), procEnd);

   // Copy the selection to the clipboard
   Document.CopyToClipboard();

   // Tell the User we succeeded
   ShowMessage("The selected procedure has been copied to clipboard.");

   // Reset column selection mode if necessary.
   if (wasInColMode) { Document.SetColumnMode(true); }

   // Clear the selection
   Document.SetSelection(xPos, yPos, xPos, yPos);

   // Return the cursor to its original position
   Document.CursorX = xPos;
   Document.CursorY = yPos;
}
Post Reply