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
-
glenn9
- Posts: 234
- Joined: Wed Jan 15, 2020 10:45 pm
Post
by glenn9 » Thu Mar 10, 2022 10:16 pm
Dear All,
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:
Code: Select all
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.
Grateful for any hints.
Many thanks,
Glenn
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Thu Mar 10, 2022 11:40 pm
Hi.
Make a field on a new card. Put this into the field script:
Code: Select all
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
Not too much pain, eh?
Craig
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Thu Mar 10, 2022 11:49 pm
Hmmm.
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:
Code: Select all
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
-
glenn9
- Posts: 234
- Joined: Wed Jan 15, 2020 10:45 pm
Post
by glenn9 » Fri Mar 11, 2022 8:46 am
Thanks Craig, brilliant, that works perfectly!
(I just need to work out to understand why
Code: Select all
...
put the number of words of char 1 to (word 2 of the selectedChunk) of me into currentWord
...
does the trick... my headache to sort out...!
Thank you again,
Glenn
-
glenn9
- Posts: 234
- Joined: Wed Jan 15, 2020 10:45 pm
Post
by glenn9 » Fri Mar 11, 2022 9:00 am
dunbarx wrote: ↑Thu Mar 10, 2022 11:49 pm
Hmmm.
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:
Code: Select all
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,
Code: Select all
...
put the number of words of char 1 to (word 2 of the selectedChunk) of me into currentWord
...
does
always return "1"? - and therefore is it in effect a 'function' to identify what the currently typed word is??
Glenn
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Fri Mar 11, 2022 2:42 pm
Glenn.
Look at "the selectedChunk". From the dictionary:
The return value reports the selected text: the startChar is the first character of the selection, and the endChar is the last character.
When you have a blinking cursor in a field, the "selection" is the point where that cursor lives. Word 2 is the "start character".
So It counts the number of words from char 1 to the "start character", and that is the "current" word. It tracks the word the cursor lives in.
Craig
-
glenn9
- Posts: 234
- Joined: Wed Jan 15, 2020 10:45 pm
Post
by glenn9 » Fri Mar 11, 2022 3:43 pm
Aha, I think I now get it...
the script identifies the number of chars from 1 to the insertion point (cursor position) with this:
Code: Select all
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...
Hopefully my understanding is now correct....
Thank you again Craig,
Glenn
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Fri Mar 11, 2022 5:39 pm
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.
Craig
-
jmburnod
- VIP Livecode Opensource Backer

- Posts: 2729
- Joined: Sat Dec 22, 2007 5:35 pm
-
Contact:
Post
by jmburnod » Fri Mar 11, 2022 7:02 pm
hi Glenn,
What Craig said
I think you might use functions when you often need a same data (string or integer) like the number of a word in a string.
Code: Select all
function getNumWords pStart,pEnd -- return num of words of a string
return the num of words of char pStart to pEnd of fld "notes"
end getNumWords
All the best
Jean-Marc
https://alternatic.ch