potter@tove.cs.umd.edu (Richard Potter) (01/29/91)
subject: the cleanest way to index into fields w/ text cursor What is the best way to get the location of the text cursor in a field? What is the best way to use a variable to index into a field? Background: I needed a HyperCard script to delete characters before the cursor back to but not including the first non-blank character. After browsing through the HyperTalk manual this is what I came up with. on functionKey fkeyv if fkeyv=5 then put the selectedchunk into cursorloc put (word 5 to 8 of cursorloc) into thefield put (word 2 of cursorloc) into cursorindex put (cursorindex-1) into cursorindex repeat while " " = (the value of ("char " & cursorindex & thefield)) do "put " & quote & quote & " into " & "char " & cursorindex & thefield put (cursorindex-1) into cursorindex end repeat end if end functionKey This script goes in the stack script. You click the cursor in any field and press F5 to start the script. I am using HyperCard Version 1.2. This script works, but I am wondering if there is a more direct way of doing this. This is my first script that manipulates fields. In particular, the line: put the selectedchunk into cursorloc seems a cludgy way to get the location of the cursor. Also, the line: do "put " & quote & quote & " into " & "char " & cursorindex & thefield seems a very hard way just to delete the character at the position designated by 'cursorindex'. Is there a way to avoid using 'do'? thanks in advance, --Richard
jk3t+@andrew.cmu.edu (Jonathan King) (01/31/91)
potter@tove.cs.umd.edu (Richard Potter) writes: > I needed a HyperCard script to delete characters before the cursor back to > but not including the first non-blank character. Here, I assume you mean "first non-blank character before the insertion point". I've tidied up you solution below by commenting out the bad lines putting a + in front of my lines. on functionKey fkeyv if fkeyv=5 then put the selectedchunk into cursorloc put (word 5 to 8 of cursorloc) into thefield -- put (word 2 of cursorloc) into cursorindex -- put (cursorindex-1) into cursorindex + put ((word 2 of cursorloc) -1) into startcursor + put startcursor into cursorindex repeat while " " = (the value of ("char " & cursorindex & thefield)) -- do "put " & quote & quote & " into " & "char " & cursorindex & thefield -- kill the previous line; no reason to have this in the loop... put (cursorindex-1) into cursorindex end repeat + delete ("char"&&cursorindex&&"to"&&startcursors&&thefield) + select ("char"&&cursorindex&&"to"&&(cursorindex-1)&&thefield) -- restores insertion point to the field if delete doesn't...I forget + else pass functionKey --don't lock out other function keys! end if end functionKey My changes should help by taking the space deletions out of your loop, using the command "delete" rather than the horrible kluge you used, and unblocking the other function keys. > This script works, but I am wondering if there is a more direct way > of doing this. This is my first script that manipulates fields. Text handling is still a bear in HC 2.0. I *think* the fixes I made use the correct level of quoting/evaluation, but I don't have time to check it right now. I haven't done this kind of thing in a couple months. I've written a number of fairly general text-handling commands that I'll give to the world as soon as I tidy them up a bit. > In particular, the line: > put the selectedchunk into cursorloc > seems a cludgy way to get the location of the cursor. Actually, it's even weirder than that, since the selectedchunk might actually not be just the insertion point location. But by subtracting 1 from (word 2 of the selectedchunk), you are guaranteed to get the location at the front of the selection, which is what I assumed you wanted in this case. > Also, the line: > do "put " & quote & quote & " into " & "char " & cursorindex & thefield > seems a very hard way just to delete the character at the position > designated by 'cursorindex'. Is there a way to avoid using 'do'? My solution was to use the 'delete' command. In my own stacks I use the home-rolled command deletebackwardchar, which in turn uses 'delete' Hope this helps... jking