Get a handler from a script

Alex Tweedly alex at
Tue Mar 21 09:20:13 EST 2006

Dick Kriesel wrote:

>On 3/21/06 2:37 AM, "Alex Tweedly" <alex at> wrote:
>>I suspect you have a different idea for what handler 'a' will do - in
>>fact, it *always" ask the time.
>You suspected correctly, Alex.  Thanks for finding another mistake.  That
>example only shows that the list of tokens is insufficient to reproduce the
>_appearance_ of the handler.
>Despite that weak example, the point was that the list of tokens is
>insufficient to reproduce the _semantics_ of the handler, and therefore not
>useful to "get a handler from a script."  Here's a better-chosen example of
>two handlers with different semantics but the same tokens for the bodies of
>the handlers:
But the interesting thing is that if you do something like
   token K1 to K2 of someScript
then it gives you the complete text between the char position of token 
K1 and the char position of token K2

I've uploaded a stack to revonline (username alextweedly, name 
FindHandler, category programming) which does this.
Moderately tested .... but not thoroughly. (btw - only for handlers - 
extension to functions, setprop, etc.. is easy)

The essence of the (short) version is

> function findhandler pScript, pHandler
>     put 0 into tStart
>     put 0 into tEnd
>     put 0 into i
>     put token 1 to -1 of pScript into pScript
>     repeat for each token t in pScript
>         add 1 to i
>         if t = "on" then
>             if token i+1 of pScript = pHandler then
>                 put i into tStart
>             end if
>         end if
>         if t = "end" then
>             if token i+1 of pScript = pHandler then
>                 put i into tEnd
>                 if tStart > 0 then exit repeat
>             end if
>         end if
>     end repeat
>     if tStart = 0 or tEnd = 0 then  return ""
>     -- to simply return the handler text
>     return token tStart to tEnd+1 of pScript
> end findhandler
Note that this fails to return any closing, trailing comment. For 
example, if the last line had been

>    end findhandler    -- end of function to do something

it would not have included the comment.

The stack includes another version which returns the line numbers of the 
start and end lines of the handler, and therefore allows you to include 
such trailing comments. It does not handle trailing, multi-line, block 
comments :-( but I'd regard a format like


> end findhandler   /* some comment
>                               in here */

as particularly bad style.

NB This is still only a 99.99% solution. It will fail in the case where 
there is a multi-line block comment which includes a complete, EXACT 
copy of the handler being sought, and which precedes the handler in 
question. Since it must be an exact copy, then if you use this to 
duplicate the handler somewhere else, it will give the correct result 
anyway :-)

Alex Tweedly

-------------- next part --------------
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.2.6/286 - Release Date: 20/03/2006

More information about the Use-livecode mailing list