Page 2 of 2
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 3:32 pm
by dunbarx
Hi.
You really have been working hard on this. Simon, we need to give this guy a break.
Put this into the field script:
Code: Select all
on keydown var
find var in me
if the result <> "" then
select char 0 of me
find var in me
end if
end keydown
Please think about the "if" clause. It is a kluge. Also, I do not have your text, so I do not know what the significance of the "@" char is there, but the unadorned "find" command limits itself to the beginnings of words, which is what I think you wanted originally. Anyway, see if this gets you closer.
Craig
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 4:16 pm
by jacque
Is this what you want to do?
http://forums.livecode.com/viewtopic.ph ... et#p112167
This reads a key press and finds the first matching line that begins with that letter. You will need to modify it to use the optional "skip" parameter to find the next instance of a repeated letter. You can do that by putting the last line number found into the amount to skip.
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 6:54 pm
by dunbarx
Jacque.
I started another thread on the "Talking LC" forum that deals strictly with "find". If you think about it, using the "offset" function is rolling your own index.
Craig
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 7:21 pm
by jacque
Maybe I misunderstood, but it sounded to me like the OP wanted to select lines based on a keypress that matched the first letter of each line in a field. But you've been in this thread longer than I have.
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 9:41 pm
by Simon
Simon, we need to give this guy a break.
Fine by me.
sphere has put good effort in
Simon
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 9:54 pm
by dunbarx
Jacque.
He did indeed. Using other functions than "find" are just fine. It is what I meant by rolling your own "index".
I was trying to making the "find" command itself work, the way I thought it ought to.
Craig
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 11:17 pm
by sphere
Hi Jacque
Maybe I misunderstood, but it sounded to me like the OP wanted to select lines based on a keypress that matched the first letter of each line in a field. But you've been in this thread longer than I have.
, that is a bit what i'm trying to do, i indeed saw it a few times and read it thru but thought that it would'nt fit.
Also i tried it tonight but can't get it to work. Thanks anyway
So i'm back to my code at the previous page of this thread.
Hello Craig,
i tried your code above, edit: i did not look good enough.
I added a few lines form your code to mine, so now at least the useless pressing of a key when the last line is found is gone:
Code: Select all
on keydown var # this works-almost ok
#set the dontWrap of me to false
repeat for 1 time
set the itemDelimiter to "@"
#set the lineDelimiter to "@"
#put var
find normal var in me
if the result is empty then select the foundLine
if the result is the last item of var then exit keydown
if the result <> "" then
select char 0 of me
find var in me
next repeat
pass keyDown
end if
end repeat
end keydown
the field i'm searching thru contains only email addresses:
a@blabla.com
aaa@blabla.com
bb@blabla.com
c@blabla.com
cccc@blabla.com
etcetera...
that's why i want to delimit at the @ so that only is searched before @
Also i noticed that still finds the c from com or the b from blabla after the @
Thanks for helping me to figure this out.
thank you too Simon !
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 11:35 pm
by jacque
sphere wrote:Also i noticed that still finds the c from com or the b from blabla after the @
!
That's pretty much why I think the find command won't work. To only find the character at the front of the line, you need to do a find for a cr plus the letter. But if you do that, the first line in the field will never be a match because there is no cr before it. Using lineoffset, you can add an artificial cr to the front of a variable and that fixes the problem -- but the find command can't search a variable, so that trick won't work there.
In this case, since you want to find sequential lines that begin with the same letter, you'll need to add the optional "skip" parameter which tells the offset where to start looking. The skip parameter would be the last found position, which you could get from the hilitedline of the field.
I actually started to write you a script for this, but I got hung up when I saw that the list field wasn't receiving key presses for some reason. But if you want to see what I had up until that point, I can post it.
Re: How to hilite a line of field by pushing first letter of
Posted: Tue Nov 04, 2014 11:51 pm
by sphere
Hi Jacque,
i also looked into the offset thingy but i'm a bit to unexperienced with it.
But i can try to test some things, with the skip and the hilited position as you tell.
Yes please post if you don't mind, i can learn from it.
Thanks a lot for your help!
Re: How to hilite a line of field by pushing first letter of
Posted: Wed Nov 05, 2014 1:49 am
by bn
Hi,
maybe this helps
Code: Select all
on keyDown pKey
put me into tData
put 0 into tCounter
put the number of lines of me into tMyLines
put the effective textHeight of me into tTextHeight
put the height of me into tMyHeight
repeat for each line aLine in tData
add 1 to tCounter
if char 1 of aLine = pKey then
lock screen
select line tCounter of me
set the scroll of me to tCounter * tTextHeight - tTextHeight
exit keyDown
end if
end repeat
beep -- not found
end keyDown
this works on a list field that has focus, also on a locked field that is focusable and has focus.
Kind regards
Bernd
Re: How to hilite a line of field by pushing first letter of
Posted: Wed Nov 05, 2014 4:58 am
by dunbarx
Now I see what the "@" was doing in there.
Jacque, the regular find command only searches the beginnings of words, so early on, when I thought Sphere's list was something like:
alfred
arnold
betty
brunhilde
The little handler at the top of this page works fine. Not sure what you meant by needing a CR.
Anyway, with a list like:
alfred@alfred
arnold@arnold
betty@betty
brunhilde@brunhilde
"Find" thinks that the ampersand is a word delimiter, as good as a space; subsequent finds treat the text following that char just like a new word. Did you know this? I was surprised. Anyway, that simple fact alone makes my handler useless in a list of email addresses. Are there other chars that do that? If nobody knows, I will have to write a gadget to find out.
Craig
Re: How to hilite a line of field by pushing first letter of
Posted: Wed Nov 05, 2014 7:41 am
by jacque
Yes please post if you don't mind, i can learn from it.
Okay, this works in one direction. That is, it will select sequential lines that start with the same letter, but only in a downward direction. It doesn't loop around to the first line with that letter, but with some more tinkering it could. If the user types a letter that doesn't start any line, the selection does not change.
Code: Select all
on keydown pKey
if char 1 of line (the hilitedline of me) of me = pKey then
put the hilitedline of me into tSkip
else
put 0 into tSkip
end if
put lineoffset(cr & pKey,cr & me,tSkip) + tSkip into tLineNum
if tLineNum > 0 then
set the hilitedline of me to tLineNum
end if
end keydown
Put the handler into a list field script. The field must have focus, as Bernd said, or key presses will not be caught. That's probably where I went wrong before.
The first "if" clause checks to see if the hilited line of the field starts with the requested character in pKey. If so,the character has already been found once so it puts that line number into the amount of text to skip when doing the search. If there is no match line, the skip amount is set to zero and no lines will be skipped.
The next line in the handler finds the lineoffset for a carriage return concatenated with the requested character ("cr & pKey"). The reason the carriage return is included is to be sure that we are only finding the first character in a line; that character will always be preceded by a CR from the previous line so we only look for that. If we looked only for the character itself, we'd find any matching character anywhere in the line. (That's why your script was finding the "c" after the @ in each email address.)
There's one problem with looking for a CR plus a character; there will be no CR before the first line of the field. That means you can never get a match for the first line. But because lineOffset can search through a variable, we can add an extra CR before the variable's text to be sure that even the first line can be matched. That's why the offset uses "cr & me" in the second parameter of the lineOffset function.
If there is a match, lineoffset will return the number of the line after the skip amount. For example, if we are skipping 6 lines and the match is on line 7, lineOffset will return 1. We need to add that 1 to the original skip amount to get the real line number. The lineoffset number (1) is added to the skip amount (6) to get line 7.
Finally, we check to see if the line number is greater than zero. If so, we set the hilitedline of the field to that number (i.e., 7.) If it is zero, there is no match and nothing changes.
Edit: There's another way to find matches for more than one character so that the user can type the first few letters of a line and the match will be found. "a" will match "
a@xx.com" and "aa" will match "
aa@xx.com". But that's more complicated.
Re: How to hilite a line of field by pushing first letter of
Posted: Wed Nov 05, 2014 7:54 am
by jacque
dunbarx wrote:"Find" thinks that the ampersand is a word delimiter, as good as a space; subsequent finds treat the text following that char just like a new word. Did you know this?
I don't remember ever noticing it before, but I don't use "find" much any more. The symbol doesn't delimit words, but apparently it delimits the "find" results. I'm not sure if that's to be expected or not. Logically it makes sense, syntactically it doesn't. I wonder if it should be changed.
Re: How to hilite a line of field by pushing first letter of
Posted: Wed Nov 05, 2014 8:42 am
by sphere
It is good to see that an apparently simple looking request is getting a lot of thought. Someone just asked me, hey can you try to add this? If i just push a,b or c it jumps to the line who are starting with it. It's a lot of work and testing before you get it done. Even more if some commands are working different then expected. But it's fun.
Thank you all, and will test both Jacque's and Bernd's code to see what fits best for me. and let you know what i've done.
Re: How to hilite a line of field by pushing first letter of
Posted: Wed Nov 05, 2014 1:41 pm
by sphere
Thank you all for this learning path
I took the code of Jacque, because it seemed that the code of Bernd did not go to the beginning again. I don't know why.
Only thing i added was to put the found line in a global so i can use it to send the email to. That's working.
It's ok for now that it does not jump to the top of a few lines with the same character, but it does jump to the above lying other character when that one is pushed.
So for now i'm am very pleased with this!
Thanks again!