Find QB function or sub based on cursor location

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

Find QB function or sub based on cursor location

Post by jgodfrey »

In an attempt to assist with this topic:

http://www.rjsoftware.se/Forum/viewtopi ... =11&t=1981

Here's a script that will find a QB function or subroutine named like the word that's currently selected or near the cursor. Since I'm not a QB programmer it may require some additional seasoning, but I think it works as described in the referenced post.

Jeff

Code: Select all

#language C++Script

// Locate the QB FUNCTION or SUB named like the word that's currently
// selected or near the cursor.

// Jeff Godfrey, Sep-2011

WideString GetSelection()
{
   // If there is no selection, create one
   if (Document.SelLength == 0)
   {
      int col = Document.CursorX;
      WideString thisLine = Document.Lines(Document.CursorY);
      // Word is to our right
      if (ScriptUtils.WPosAt(" ", thisLine, col)) { Document.CursorWordRight(true); }
      // Word is to our left
      else if (ScriptUtils.WPosAt(" ", thisLine, col + 1)) { Document.CursorWordLeft(true); }
      // We're within a word
      else { Document.CursorWordLeft(false); Document.CursorWordRight(true); }
   }
   return Trim(Document.SelText()); 
}

// Search through the file looking for either:
// FUNCTION <selection> 
// SUB <selection>
boolean FindMatchingCodeBlock(WideString selection)
{
   for (int i = 0; i < Document.LineCount; i++)
   {
      WideString currLine = Trim(Document.Lines(i));
      if ((ScriptUtils.WPosAt("FUNCTION " + selection, currLine, 1)) ||
          (ScriptUtils.WPosAt("SUB " + selection, currLine, 1)))
      {
         Document.CursorX = 0;
         Document.CursorY = i;
         return true;
      }   
   }
   return false;
}

{
   // Get the selection
   WideString selection = GetSelection();
   
   // If we were unable to get the selection, get out
   if (selection == "") 
   {
      ShowMessage("Unable to determine selection - Exiting...");
      exit;
   }
   
   // Find the FUNCTION or SUB matching our selection
   if (!FindMatchingCodeBlock(selection))
   {
     ShowMessage("Unable to find code block named " + selection);
   }
}


User avatar
rjbill
Posts: 877
Joined: 13 Jun 2011 06:36

Re: Find QB function or sub based on cursor location

Post by rjbill »

Thanks for doing that.

It should work for QB64 programs, and other Quick BASIC-like programming languages.
(I suppose there could be times when SUB or FUNCTION don't start at the first character)

And it won't be hard to make it find NEXT and PREVIOUS routines, too.
RJTE version 16.16 (Beta 2) - 64-bit
Win 10 Pro 64-bit 8 GB RAM Intel Core i7-6700 3.40 GHz SCSI Hard Drive 1 TB

Note: The signature is dynamic, not static,
so it may not show the correct version above
that was in use at the time of the post.
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Re: Find QB function or sub based on cursor location

Post by jgodfrey »

rjbill wrote:(I suppose there could be times when SUB or FUNCTION don't start at the first character)
That isn't a problem, as all leading and trailing spaces are removed from the current line before the search is performed. So, as long as SUB or FUNCTION is the first non-whitespace char (which I assume it has to be), it should work fine.
rjbill wrote:And it won't be hard to make it find NEXT and PREVIOUS routines, too.
You're just looking for the next or previous SUB or FUNCTION, unrelated to the any specific SUB or FUNCTION, right? In that case, that seems like a different (and easier) problem, but a separate script. I'd just search up or down from the current cursor location for whatever make sense...

Jeff
User avatar
rjbill
Posts: 877
Joined: 13 Jun 2011 06:36

Re: Find QB function or sub based on cursor location

Post by rjbill »

I'm thinking I want to assign these functions to a 'command prefix key',
probably F11. So you would press F11, and then another key to execute
the appropriate function, like:

UpArrow = Find Previous Sub/Function
DownArrow = Find Next Sub/Function
F11 = Execute Previous Find (so you can F11-Up, F11-F11, F11-F11, ...)
F12 = Find Sub/Function under cursor or selection
ESC = Cancel F11 'mode'

So I would need to have a "Master Function" assigned to F11,
and have it call the other functions based on the second keypress.

Rather than assign each function to its own command key.

Is that hard to do?
RJTE version 16.16 (Beta 2) - 64-bit
Win 10 Pro 64-bit 8 GB RAM Intel Core i7-6700 3.40 GHz SCSI Hard Drive 1 TB

Note: The signature is dynamic, not static,
so it may not show the correct version above
that was in use at the time of the post.
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Re: Find QB function or sub based on cursor location

Post by jgodfrey »

rjbill wrote:So I would need to have a "Master Function" assigned to F11,
and have it call the other functions based on the second keypress.

Rather than assign each function to its own command key.

Is that hard to do?
I'm not aware of a way to do that via scripting right now, though maybe Rickard has some tricks up his sleeve. If you could somehow read keystrokes via a script, you could have F11 start the master script and then have it (the script) react appropriately to the various keystrokes. However, I'm not sure that's possible right now.

With that said though, if it were me, I'd probably prefer some separate key assignments for the 3 functions. For instance, I might do something like this:
  • Ctrl+Shift+Up - find previous function or sub
  • Ctrl+Shift+Down - find next function or sub
  • Ctrl+Shift+Right - find function or sub beneath cursor
That way, they're all related and very similar (Ctrl+Shift+<SomeArrow>), but easy to trigger separately.

Jeff
Post Reply