Two questions about trev
Richard Gaskin
ambassador at fourthworld.com
Tue Sep 15 10:37:42 EDT 2009
Tereza Snyder wrote:
> Some satisfaction might be obtained if, by clicking a parenthesis with
> a modifier key pressed, one could select all the text between it and
> its partner. Similarly, click the keyword "if" and all text to the
> matching "end if" is selected and vice-versa. Same for repeat and
> switch.
I like the way you think, Tereza. That strikes a perfect balance,
providing the clarity Mark Wieder was looking for while keeping the
script displayed as the whole block of text it is. I'll admit I have a
few long handlers of my own where this will be very helpful. :)
I just added the code below to my own script editor, and I really like
it. Thanks for the suggestion.
Jerry, I found that as sloppy as this code is it performs reasonably
well as long as I store the top of the graphic "ControlSel" (for
"Control Stucture Selection") in a property so I can update it during
scrolling without having to recalculate the rect on the fly.
The code assumes you have a graphic named "ControlSel" below your
transparent editor field (since my editor is a fork of MC's my editor
field is named simply "Editor Field"). Here I use a color slightly
darker than the light gray of the background color of the stack, but
brighter colors may look good too.
If one of the BBEdit users here could send me a screenshot of how that
app displays this that would be very helpful. It would also be good to
know how they handle knowing what closure means for different languages
(prefs settings?).
ShowControlSel is called from a CommandKeyDown handler (I used Cmd-J
simply because it was available, but if you use a different one it would
be good to know so I can change mine for consistency before I share it).
When invoked it'll draw the "ControlSel" graphic at the bounds of
the relevant block, i.e. if a handler it'll surround the whole handler,
if a switch it'll do the switch, if "if" it'll do until the "end".
The ControlStructureClosureLine function is used in a few places in my
editor, handy for finding the line offset where a handler or control
structure closes. It's way sloppy in style (cleaning it up is on my
to-do list after a long list of client features), but seems to work okay
where I'm using it. With "if", "switch", and "repeat" it should account
for nested control structures (please let me know if it fails to catch any).
You're welcome to use any of this that's helpful:
on ShowControlSel
hide grc "ControlSel"
put word 2 of the selectedLine into tLineNum
put line tLineNum of fld "Editor Field" into tLine
put ControlStructureClosureLine(tLineNum) into tClosingLineNum
--
put ((tLineNum-1) * the effective textHeight of \
fld "Editor Field") + the top of fld "Editor Field" \
+ 2 into tTop
put (tClosingLineNum * the effective textHeight of \
fld "Editor Field") + the top of fld "Editor Field" \
+ 6 into tBottom -- 6 added for borders and margins
set the uTop of grc "ControlSel" to tTop -- used for updating during
scroll
set the rect of grc "ControlSel" to \
the left of fld "Editor Field", \
tTop - the vScroll of fld "Editor Field", \
the right of fld "Editor Field", \
tBottom- the vScroll of fld "Editor Field"
show grc "ControlSel"
end ShowControlSel
function ControlStructureClosureLine tLineNum
put line tLineNum to (the number of lines of fld "Editor Field") \
of fld "Editor Field" into tScript
put line 1 of tScript into tLine
put word 1 of tLine into tToken
if tToken is among the words of "if switch repeat" then
put 0 into i
put 0 into tNestedCount
repeat for each line tLine in tScript
add 1 to i
if word 1 of tLine is tToken then
add 1 to tNestedCount
end if
--
if word 1 of tLine is "end" AND word 2 of tLine is tToken then
subtract 1 from tNestedCount
if tNestedCount = 0 then
return i+tLineNum-1
end if
end if
end repeat
--
-- If we got here we may have an "if" closed by "else" so:
if tToken is "if" then
put 0 into i
repeat for each line tLine in tScript
add 1 to i
if word 1 of tLine = "else" then
return i+tLineNum-1
end if
end repeat
--
-- If we got this far perhaps the "if" dangles "then" on the next
line:
put 0 into i
repeat for each line tLine in tScript
add 1 to i
if word 1 of tLine = "then" then
return i+tLineNum-1
end if
end repeat
end if
--
else
-- Get whole handler?
if tToken is among the words of "on private command function
getProp setProp" then
if word 1 of tLine is "private" then put word 3 of tScript into
tToken
else put word 2 of tLine into tToken
put 0 into i
repeat for each line tLine in tScript
add 1 to i
if word 1 of tLine = "end" AND word 2 of tLine is tToken then
return i+tLineNum-1
end if
end repeat
end if
end if -- closable token
return tLineNum
end ControlStructureClosureLine
--
Richard Gaskin
Fourth World
Revolution training and consulting: http://www.fourthworld.com
Webzine for Rev developers: http://www.revjournal.com
More information about the use-livecode
mailing list