extending selected text backwards from current cursor point

37 views
Skip to first unread message

Todd Ingalls

unread,
Aug 23, 2025, 8:02:53 PMAug 23
to BBEdit Talk
Hello,
I am working on something in an esoteric language and have a Function Pattern that works great 
\\[A-Za-z0-9_ \s]*(?:\s+"[^"]*")?\s*(\[(?:[^\[\]]+|(?1))*\])(?:(\s*=\s*)(?P<function_name>[a-zA-Z0-9_!?]+))?

What I want to be able to do is from wherever my cursor is to search backwards and find the beginning of this function definition and then select all the text from where my cursor was to the beginning of the function.

I can use find previous  with the grep pattern and that will select that function but not the text from where my cursor was. I can also use Reveal Start in Go->Functions to move the cursor to the beginning of the previous function. I just can't seem to figure out how to quite accomplish what I want in AppleScript. Any thoughts would be appreciated

jj

unread,
Aug 24, 2025, 11:27:04 AMAug 24
to BBEdit Talk
Hi Todd,

This applescript seems to do what you want :

tell application "BBEdit"

try

tell first document of front window

set vSelectionEnd to (characterOffset of its window's selection) - 1

set vRegex to "\\\\[A-Za-z0-9_ \\s]*(?:\\s+\"[^\"]*\")?\\s*(\\[(?:[^\\[\\]]+|(?1))*\\])(?:(\\s*=\\s*)(?P<function_name>[a-zA-Z0-9_!?]+))?"

set vOptions to {search mode:grep, wrap around:false, backwards:true}

set vResult to find vRegex searching in it options vOptions

if vResult's found then

set vSelectionStart to characterOffset of vResult's found object

else

error "Pattern not found."

end if

select its characters vSelectionStart thru vSelectionEnd

end tell

on error aMessage number aErrorNumber

if aErrorNumber ≠ -128 then -- Error number -128 (User Cancelled).

display dialog aMessage

else

return

end if

end try

end tell


Successfully tested against those expressions (courtesy of Claude):

\section [main]

\config "default settings" [database] = connect

\path [logs]

\module [auth[user]] = login_handler

\service   [web_server] = nginx

\handler "error pages" [404[custom]] = not_found!

\backup     [files] = backup_job

\system [network[tcp[port]]] = handle_connections?

\data "user info"   [profiles] = user_manager

\temp [cache[memory[buffer]]] = cache_clear!


HTH,

Jean Jourdain

Todd Ingalls

unread,
Aug 24, 2025, 6:45:41 PMAug 24
to bbe...@googlegroups.com
JJ
Works great thank you.

-- 
This is the BBEdit Talk public discussion group. If you have a feature request or believe that the application isn't working correctly, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Mastodon: <https://mastodon.social/@bbedit>
--- 
You received this message because you are subscribed to a topic in the Google Groups "BBEdit Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bbedit/GGeHrjKwjkk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bbedit+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/bbedit/d6ec7d50-afef-4339-a18e-71791f5cb216n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages