Page 1 of 1
Apply visual formatting to unformatted code
Posted: Thu Dec 19, 2024 3:54 pm
by EMR
Hello all!
I am a (very) longtime reader of the forum (helpful!), but this is my first time posting a question.
Imagine that I paste an unformatted script in a regular field of my stack. Is there some way to colorize and indent this so it appears like it would when working in the Code Editor (but without having to manually select and copy the script in the Code Editor)?
In other words, can I put an unformatted script in a field and then click a button to paste a color/indent-formatted version into a second field?
I tried to set the script of an object using the unformatted code hoping that the Code Editor would format it, but could not find a way to use a script to copy a formatted version.
Thanks for any suggestions!
Eric R
Re: Apply visual formatting to unformatted code
Posted: Thu Dec 19, 2024 4:24 pm
by Klaus
Hi Eric,
there is an undocumented feature of the engine that LC uses to colorize the scripts in the script editor.
Here the script in a mouseup handler, I had copied a script into that field (field 1) without formatting:
Code: Select all
on mouseUp
_internal script colorize char 1 to (the number of chars of field 1) of field 1
end mouseUp
Tested and works.
Best
Klaus
P.S.
I'm not sure if this will also work in a standalone.
Re: Apply visual formatting to unformatted code
Posted: Thu Dec 19, 2024 4:53 pm
by EMR
Thank you, Klaus!! Very helpful (and impressively fast reply).
Is there an analogous function for indentation?
Re: Apply visual formatting to unformatted code
Posted: Thu Dec 19, 2024 5:32 pm
by Klaus
Hi Eric,
EMR wrote: ↑Thu Dec 19, 2024 4:53 pm
Thank you, Klaus!! Very helpful (and impressively fast reply).
my pleasure and I kind of live here.
EMR wrote: ↑Thu Dec 19, 2024 4:53 pm
Is there an analogous function for indentation?
there is, however that is not a one-liner but a very long script which relies on many other handlers
buried deep in the multiple LC IDE scripts and behaviors.
Best
Klaus
Re: Apply visual formatting to unformatted code
Posted: Thu Dec 19, 2024 7:13 pm
by bn
EMR wrote: ↑Thu Dec 19, 2024 4:53 pm
Is there an analogous function for indentation?
Hi Eric,
try this
Code: Select all
on mouseUp
local tOrigText, tFormattedText
put the text of field "fInput" into tOrigText
send "scriptFormatSnippet tOrigText" to stack "com.livecode.scripteditor.behavior.editorcommon"
put the result into tFormattedText
put tFormattedText into field "fRes"
end mouseUp
tested with partial scripts and complete handlers. Adapt after testing to your needs
Kind regards
Bernd
Re: Apply visual formatting to unformatted code
Posted: Thu Dec 19, 2024 7:14 pm
by EMR
Thanks again everyone!
Re: Apply visual formatting to unformatted code
Posted: Thu Dec 19, 2024 7:16 pm
by Klaus
Ah, yes, Bernd is correct!
There are so many IDE stacks and scripts and behaviours, so I missed this one, sorry.
Re: Apply visual formatting to unformatted code
Posted: Fri Dec 20, 2024 10:46 am
by richmond62
This is marvellous.
I wonder how many other undocumented features of the engine there are, and if anyone has, err, documented them.
How, Klaus, did you discover that feature?
Re: Apply visual formatting to unformatted code
Posted: Fri Dec 20, 2024 10:54 am
by Klaus
I think this came up on the mailing list many, many years ago and I have a wonderful memory.

Re: Apply visual formatting to unformatted code
Posted: Fri Dec 20, 2024 11:12 am
by richmond62
Good that you have that memory.
But that is like asking which came first: the hen or the egg.
Because, someone, somewhere, somewhen must have somehow had access to the innards of the engine to find that information out.
Unless, of course, LiveCode have their own, internal documentation.
Re: Apply visual formatting to unformatted code
Posted: Fri Dec 20, 2024 4:09 pm
by SWEdeAndy
So, for the sake of completeness, and future readers, here's an all-in-one appliance of it:
Code: Select all
command formatAsScript pOutputFieldId,pSourceText
## pOutputFieldId has to be the long id of the output field
## pSourceText can optionally be used if the code to be formatted is not already in the output field
if pSourceText is empty then put the text of pOutputFieldId into pSourceText
dispatch "scriptFormatSnippet" to stack "com.livecode.scripteditor.behavior.editorcommon" with pSourceText
set the text of pOutputFieldId to the result
dispatch "revSEColorizeField" to stack "revseutilities" with pOutputFieldId,1,length(pOutputFieldId)
end formatAsScript
on mouseUp
## Example with the input and output field being the same
formatAsScript the long id of field "myOutputField"
## Example with the input source being another field than the output field
formatAsScript the long id of field "myOutputField",fld "myUnformattedCode"
end mouseUp
Re: Apply visual formatting to unformatted code
Posted: Fri Dec 20, 2024 5:49 pm
by FourthWorld
richmond62 wrote: ↑Fri Dec 20, 2024 11:12 am
Because, someone, somewhere, somewhen must have somehow had access to the innards of the engine to find that information out.
Most of us who've wanted to do things the IDE does learned how it does then by reading the scripts.
That's one of the things I've loved about xTalks with scripted IDEs (SuperCard, Gain Momentum, MetaCard/RuneRev/LC) over those with IDE elements compiled into the engine (HyperCard, Oracle Media Objects, Spinnaker Plus): the ability to dive in as deep as you want to learn and tweak to your heart's content.