How to select a word with special characters in LIvecode - livecode

How to select a word with with special characters (eg: usa-uk). the following code will select text but it's doesn't select the words like usa-uk. How I change my code
select the mouseText

If you don't have a huge text in your field you might use:
on mouseMove
if the mouseLoc is within the rect of me then
put the mouseChunk into tChunk
# Check if chars before or after is a "spacing char"
put word 2 of tChunk into tstart
put word 4 of tChunk into tEnd
repeat with i = tStart down to 1 step -1
if matchText(char i of me, "\s") then exit repeat
end repeat
put i+1 into tStart
repeat with i = tEnd to the number of chars of me
if matchText(char i of me, "\s") then exit repeat
end repeat
put i-1 into tEnd
select char tstart to tEnd of me
end if
end mouseMove

How are you selecting? For example, if this was in the field script:
on mousemove
select the mouseText
end mouse move
You would be selecting the text under the cursor. In order to select compound text such as "usa-uk" you would have to group that text fragment. This sets the textStyle of that fragment to "link".
Craig Newman

Related

How do I listen to a specific text string on Mac OS X in a livecode application

I want to create a Mac app similar to Textexpander or Atext. Both these applications allow the user to define snippets along with their respective trigger words. Typing the trigger words in any app, replaces that trigger word with the actual snippet defined.
I presume that the app listens to all strings being typed in any app and when it detects a string matching one of the trigger words defined, it replaces it with the snippet.
Is that how it actually works, or is there some other way?
Make two fields. In field 2 put something like:
time xyz
come ABC
In the script of field 1:
on textChanged
if the last char of me = space then
put the last word of me into temp
if temp is in fld 2 then
repeat for each word tWord in fld 2
put the last word of line lineOffset(temp,fld 2) of fld 2 into the last word of me
exit repeat
end repeat
end if
select after text of me
end if
end textChanged
Now type into fld 1, you know, "Now is the time for all good men to come to the aid of their country". This can be better done with an array, but the concept may be more accessible here.
This is a better handler, since it will not react to the trigger word:
on textChanged
if the last char of me = space then
put the last word of me into stringOfInterest
put fld 2 into dataToSearch
if stringOfInterest is in dataToSearch then
repeat for each line tLine in dataToSearch
if word 1 of tLine = stringOfInterest then
put word 2 of tLine into the last word of me
exit repeat
end if
end repeat
end if
select after text of me
end if
end textChanged

Splitting a string of a multiline textbox by lines

I want to split the text of a textbox after a specific amount of visible lines.
I've found some codes that "allows that", but all of them consider the lines by the "vbCrLf" parameter, but i want to split using the visible lines of a multiline textbox.
To make it more clear to understand, consider a multiline textbox with the following text:
"The history of textbooks dates back to civilizations of ancient history. For example, Ancient Greeks wrote texts intended for education. The modern textbook has its roots in the standardization made possible by the printing press. Johannes Gutenberg himself may have printed editions of Ars Minor, a schoolbook on Latin grammar by Aelius Donatus. Early textbooks were used by tutors and teachers, who used the books as instructional aids (e.g., alphabet books), as well as individuals who taught themselves."
When i use the Textbox.Linecount function it returns the number 6 because the textbox shows six lines (which depends on the size of the control), but if i use a function like strText = Split(TextBox.Text, vbCrLf) it will return 1, because there is only one vbCrLf. But i need to split the text into two textbox considering the visible lines of the control, something like what happens in page breaks of MS Word.
For a better visual explanation, please look at the attached image.
Example
Firstly, I'm not convinced there is a robust and elegant way to do this, but it was fun to experiment and it might be useful to you.
The following will split the contents of TextBoxInput into TextBoxPage1 and TextBoxPage2 breaking on the line number specified by PAGED_TEXT_BOX_LINES.
It uses the textbox itself to detect natural line breaks and thus implicitly caters for the size of the textbox, the font, etc.
The desired line count is hard coded as a constant - not doing this would require an alternative of calculating the line height of the textbox (requiring calculations based on the font metrics and the textbox's internal line-leading size).
It only handles two "pages". But the concept could be extended simply by repeating the process based on the remainder of text that ends up in TextBoxPage2.
Private Sub CommandButton1_Click()
Const PAGED_TEXT_BOX_LINES As Integer = 5
Dim text As String
Dim i As Long
Dim textLength As Long
Dim curLine As Integer
text = TextBoxInput.text
textLength = Len(text)
TextBoxPage1.SetFocus
'add characters of the input string until the first page textbox
' exceeds maximum line count
For i = 1 To textLength
TextBoxPage1.text = Mid$(text, 1, i)
If TextBoxPage1.LineCount > PAGED_TEXT_BOX_LINES Then
'retreat cursor until we reach previous line, so we can
' detect the word that wrapped
curLine = TextBoxPage1.curLine
Do While TextBoxPage1.curLine = curLine
TextBoxPage1.SelStart = TextBoxPage1.SelStart - 1
Loop
'the remaining text after the SelStart is what
' wrapped, so stop page 1 after SelStart
TextBoxPage1.text = Mid$(text, 1, TextBoxPage1.SelStart)
TextBoxPage2.text = Trim$(Mid$(text, TextBoxPage1.SelStart + 1))
Exit For
End If
Next i
End Sub

Change word after quotation in vim?

I know you can use cw to change the word and ciw to change the inner word, but what i'm trying to do is change the word after the character.
For example I have this
this.option('test');
Now my cursor is at the first quote (') and I want to change the word test. If I press cw it also deletes the first quote my cursor is on. I'm on the other hand looking for a command that imitates the a mode (where it inserts after the cursor), so in my case deletes the word after cursor and puts it in insert mode?
cw is not "change the word", it's "change to next word".
With the cursor on either of the single quotes, you can use ci' to "change between single quotes".
With the cursor on the first single quote, you could also do:
wciw move to next word then change inner word
wcw move to next word then change to next word
wct' move to next word then change until next single quote
wce move to next word then change to end of the word
lciw move to next character then change inner word
lcw move to next character then change to next word
lct' move to next character then change until next single quote
lce move to next word then change to end of the word
<Right>ciw move to next character then change inner word
<Right>cw move to next character then change to next word
<Right>ct' move to next character then change until next single quote
<Right>ce move to next word then change to end of the word
See :help navigation.
If your cursor is at ( ' ) than you can use
ci'
This will delete the text and sets you in insert mode to make the change.
So it's change inside the '.

Tkinter selecting text by index

I am implementing a search function in python with Tkinter and would like to select the first match it comes to. I have seen many examples with creating a tag_config to highlight the background of the indexed range, however I would like to select the text (the same way one would by clicking at the first index, then shift clicking the last index).
Thus far I have got both the start and end index of the area I need to select, I just don't know the command to "select" the text with that information.
My current code (that uses a highlight approach) is:
def search_command():
word = askstring("Search", "Enter word to search")
length = len(str(word))
pos = textPad.search(word, '1.0', stopindex=END)
row, col = pos.split('.')
endlen = int(col) + length
end = row + '.' + str(endlen)
textPad.tag_add("found", pos, end)
The "found" tag just highlights the background of the text rather than selecting it.
Any help with finding the correct function would be greatly appreciated.
The selection is defined by the "sel" tag. Apply that tag to the range of text you want selected:
textPad.tag_add("sel", pos, end)

How to manipulate the position of character(s) in a string in LiveCode

So, I have a very specific problem. I have a random word which I have already generated. I also have a separate string that uses the length of that character, 'n' to repeat "_ " n times to field wordDisplay. These elements of the program work. So, if the random word was "meme", the wordDisplay, on openCard would show "_ _ _ _ ".
Now, I have an input button "a". If this button is clicked, it will check for "a" in the randomword variable and if it is found, it will replace the "_ " in the wordDisplay with "a". So, if the randomword was "name", then before you click "a", the wordDisplay shows "_ _ _ _ ". After you click "a", wordDisplay will show "_ a _ _ ". However, if there is more than one "a", it will reveal all of them.
It is also worth knowing that I plan to have the program do something if there is no letter found.
Being a beginner to LiveCode, this all seems very confusing to me and I would really appreciate someone to share their knowledge on how I would do something like this. Thanks!
There are several ways to do this, but I wouldn't try to do everything in a single field. Unless you use a monospaced font, the text's width will shift each time you swap characters and underscores.
I would suggest using a separate field (or button or graphic) to display each character of the word (named something like letter1, letter2, letter3...), and place a line graphic under each field (named line1, line2, line3...). Create a number of controls that matches the length of your worst-case word, and hide the unneeded controls each time a new word is selected.
By laying the controls out this way, you're guaranteed a relatively consistent alignment regardless of what font is used, or how the card is scaled.
Now you can simply toggle the visibility of each "letter" field and the visibility of each "underscore" line as needed, by looping through the set of controls. Something sort of like:
[ the secret word is 'aardvark' ]
command checkForCharacterMatch pUserChar
local noMatch = true
repeat with N = 1 to length(theSecretWord)
put long id of field ("letter" & N) into theLetter
put long id of graphic ("line" & N) into theLine
if (the text of theLetter = pUserChar) then
show theLetter
hide theLine
put false into noMatch
end if
end repeat
if noMatch then
-- DO PENALTY STUFF HERE
end if
end checkForCharacterMatch
In reality, you don't even need to hide the lines, but this follows your original approach.

Resources