Word Select Algorithm

I’m a bit perplexed on how to approach this. I need to be able to programmaticly select a “word” from a TextView given SELSTART

But there are multiple scenarios, and I’m not sure how to react to each of them

  • cursor is between two letters - move SELSTART to left until not A-Z, then move SELLENGTH to right until not A-Z…
  • cursor is between two SPACES - move SELSTART to left until not space, then move SELLENGTH to right until not space
  • cursor is between non-LETTER and LETTER - leave SELSTART alone, then move SELLENGTH to right until not A-Z…
  • cursor is between LETTER and non-LETTER- move SELSTART to left until not A-Z, then move SELLENGTH to right until not A-Z…
  • cursor is between two non-LETTER (other than spaces) - do nothing
  • cursor is at start of line - move SELLENGTH until non-letter
  • cursor is at end of line - move SELSTART to left until non-letter

non-LETTER is anything NOT [A-Z] or [0-9]

Can anyone think of any other conditions? or perhaps a simple way to implement these without a boatload of SELECT or IF/THEN’s

Is it correct to rephrase it this way: if the cursor is touching a word, select that word?

yes and no… if the character to the left and/or right of the cursor is a LETTER (or number) then yes
if it is between two spaces, then the run of spaces the cursor is in should be selected

it is when left is non-letter and right is non-letter… what (if anything) should happen
I’ve been playing with a few text editors they seem to do nothing if you are just selecting, but consider somethings a “word” if you are doing a “word delete”, but I can’t grasp the pattern.

I want this app to react the way most people would expect…

In the end I need to

  • Select current word (as described above)
  • Delete current word (ok, select it, then delete)
  • Select or Delete Word - right
  • Select or Delete Word - Left

the last two are strange…
for example
take this text

where | is the cursor
in UltraEdit, doing WORD SELECT does nothing, but DELETE WORD LEFT takes out the first 3 commas, and DELETE WORD RIGHT takes out the right 3. So the non-letters are a “word” sometimes…

Instead of breaking it down to an algorithm for each case, make an algorithm for the activity whose behavior is modified for the case.

So the activity is to move out from a starting position until some type of character blocks it. Since non-letters always block that doesn’t have to be optional. Really the only variant behavior I see is when it’s between spaces. Maybe this sketch explains the idea better…

[code]given SelStart

if character on left and right is a space then
blockingType = letters
travelType = spaces
else
blockingType = spaces
travelType = letters
end

start = selStart
stop = selStart

do
if there’s a character left of start and it’s of travelType then
start = start - 1
else
exit do
end
loop

do
if there’s a character right of stop and it’s of travelType then
stop = stop + 1
else
exit do
end
loop

SelStart = start
SelLength = stop - start[/code]

edit: I didn’t see your second post Dave and think maybe I’m on about something else :stuck_out_tongue:

Dave, where are you implementing this? Perhaps the context will point to a solution.

where? in a TextArea control… if the user hits a particular key, I need it to select “the word”

Got it.

Suggestion: when the user hits that key, start finding words using a regular expression \\b\\w+\\b. When the match surrounds or touches your cursor, use the data to select the word. If the match start point is suddenly beyond your cursor, beep at the user.

(Note that the \\w token includes underscore. If you really don’t want that, it would be \\b[a-z0-9]+\\b.)