Apply visual formatting to unformatted code

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
EMR
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4
Joined: Wed Jun 14, 2006 12:24 pm

Apply visual formatting to unformatted code

Post by EMR » Thu Dec 19, 2024 3:54 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Apply visual formatting to unformatted code

Post by Klaus » Thu Dec 19, 2024 4:24 pm

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.

EMR
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4
Joined: Wed Jun 14, 2006 12:24 pm

Re: Apply visual formatting to unformatted code

Post by EMR » Thu Dec 19, 2024 4:53 pm

Thank you, Klaus!! Very helpful (and impressively fast reply).

Is there an analogous function for indentation?

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Apply visual formatting to unformatted code

Post by Klaus » Thu Dec 19, 2024 5:32 pm

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. :-D :D :D
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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Apply visual formatting to unformatted code

Post by bn » Thu Dec 19, 2024 7:13 pm

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

EMR
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4
Joined: Wed Jun 14, 2006 12:24 pm

Re: Apply visual formatting to unformatted code

Post by EMR » Thu Dec 19, 2024 7:14 pm

Thanks again everyone!

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Apply visual formatting to unformatted code

Post by Klaus » Thu Dec 19, 2024 7:16 pm

Ah, yes, Bernd is correct!
There are so many IDE stacks and scripts and behaviours, so I missed this one, sorry.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Apply visual formatting to unformatted code

Post by richmond62 » Fri Dec 20, 2024 10:46 am

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?

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Apply visual formatting to unformatted code

Post by Klaus » Fri Dec 20, 2024 10:54 am

I think this came up on the mailing list many, many years ago and I have a wonderful memory. :-)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Apply visual formatting to unformatted code

Post by richmond62 » Fri Dec 20, 2024 11:12 am

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.

SWEdeAndy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 324
Joined: Sat Aug 16, 2008 9:48 am
Contact:

Re: Apply visual formatting to unformatted code

Post by SWEdeAndy » Fri Dec 20, 2024 4:09 pm

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
Andreas Bergendal
Independent app and system developer
Free LC dev tools: https://github.com/wheninspace
(WIS_WebDeployHelper, WIS_ScriptDependencies, WIS_BrowserAnimation)
WhenInSpace: https://wheninspace.com

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Apply visual formatting to unformatted code

Post by FourthWorld » Fri Dec 20, 2024 5:49 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply