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!
Is it possible to put text in a field at a position other than that defined by the keywords 'before', 'after' or 'into'.
The background is that I'm trying to create a 'synonym' function, eg if I type in 'lidsa' then i would like the 'lidsa' to be replaced by 'Lorem ipsum dolor sit amet' at the cursor position.
At the moment I'm solving it with this workaround which of course works only for the last word in the field:
on keyup
if the last word of fld"Notes" is "lidsa" then
replace the last word of fld"Notes" with "Lorem ipsum dolor sit amet" in fld"Notes"
-- above will replace the text but will put the cursor unhelpfully at the beginning of the field!
-- the following code will put the cursor at the end of the field so I can continue typing!
set the vScroll of fld"Notes" to the formattedHeight of fld"Notes"
put " " after fld"Notes"
end keyup
end if
What I'm trying to achieve is to be able to type 'lidsa' at any position in the field and have it replaced by the longer text.
on textChanged
put the number of words of char 1 to (word 2 of the selectedChunk) of me into currentWord
if word currentWord of me = "lidsa" then
put "Lorem ipsum dolor sit amet" into word currentWord of me
put space after me
select after text of me
end if
end textChanged
I just noticed that you might be typing at a randomly selected place in a body of text. So there are many ways one might insinuate oneself into a body of text. Does this do a better job? Again, in the field script:
on textChanged
put the number of words of char 1 to (word 2 of the selectedChunk) of me into currentWord
if word currentWord of me contains "lidsa" then -- NOTE CHANGE FROM "=" TO "CONTAINS"
put "Lorem ipsum dolor sit amet" into word currentWord of me
put space after me
select after text of me
end if
end textChanged
I just noticed that you might be typing at a randomly selected place in a body of text. So there are many ways one might insinuate oneself into a body of text. Does this do a better job? Again, in the field script:
on textChanged
put the number of words of char 1 to (word 2 of the selectedChunk) of me into currentWord
if word currentWord of me contains "lidsa" then -- NOTE CHANGE FROM "=" TO "CONTAINS"
put "Lorem ipsum dolor sit amet" into word currentWord of me
put space after me
select after text of me
end if
end textChanged
Hi Craig,
After looking to see what happens with your code in debug mode,
put the number of words of char 1 to (word 2 of the selectedChunk)
and then puts the number of words within that character range into a variable (currentWord)
and then through the keyword 'word' applied to the variable
it can then check if the defined text is contained within any of the words in the defined character range.... and if so carries out the actions following the 'then' keyword...
it can then check if the defined text is contained within any of the words in the defined character range
My handler only looks at word "currentWord", which is where the cursor is, not that the "defined text" is somewhere in the block of text delimited by that cursor. It would, I think, do no good were that the case, because where then would the new text go? And how did that defined text get passed by in the first place.
But you do get the gist of the handler. The cute part is that "the number of words..." thingie tells you what number word you are presently at.