a little refactoring to make implementation of word and WORD selection (viW, ciW, diW) easier

This commit is contained in:
2025-07-21 09:40:32 +01:00
parent 51c9090adf
commit a8ee1d5d37
2 changed files with 135 additions and 105 deletions

View File

@@ -108,59 +108,64 @@ struct
end
end)
fun startOfCurrentWORD (idx, absIdx, str, tl, currentState, counter) =
if idx < 0 then
case tl of
str :: tl =>
startOfCurrentWORD
(String.size str - 1, absIdx, str, tl, currentState, counter)
| [] => 0
else
let
val chr = String.sub (str, idx)
val newState = next (currentState, chr)
in
if newState = spaceAfterNonBlankState then
if counter - 1 = 0 then
absIdx + 1
else
startOfCurrentWORD
(idx - 1, absIdx - 1, str, tl, startState, counter - 1)
else
startOfCurrentWORD (idx - 1, absIdx - 1, str, tl, newState, counter)
end
fun endOfCurrentWORD (idx, absIdx, str, tl, currentState, counter) =
if idx = String.size str then
case tl of
str :: tl =>
endOfCurrentWORD (0, absIdx, str, tl, currentState, counter)
| [] => Int.max (0, absIdx - 2)
else
let
val chr = String.sub (str, idx)
val newState = next (currentState, chr)
in
if newState = spaceAfterNonBlankState then
if counter - 1 = 0 then
Int.max (0, absIdx - 1)
else
endOfCurrentWORD
(idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else
endOfCurrentWORD (idx + 1, absIdx + 1, str, tl, newState, counter)
end
structure StartOfCurrentWORD =
MakePrevDfaLoopMinus1
(struct
val startState = startState
fun fStart (idx, absIdx, str, tl, currentState, counter) =
if idx < 0 then
case tl of
str :: tl =>
fStart
(String.size str - 1, absIdx, str, tl, currentState, counter)
| [] => 0
else
let
val chr = String.sub (str, idx)
val newState = next (currentState, chr)
in
if newState = spaceAfterNonBlankState then
if counter - 1 = 0 then
absIdx + 1
else
fStart
(idx - 1, absIdx - 1, str, tl, startState, counter - 1)
else
fStart (idx - 1, absIdx - 1, str, tl, newState, counter)
end
end)
(struct val startState = startState val fStart = startOfCurrentWORD end)
structure EndOfCurrentWORD =
MakeNextDfaLoopPlus1
(struct
val startState = startState
(struct val startState = startState val fStart = endOfCurrentWORD end)
fun fStart (idx, absIdx, str, tl, currentState, counter) =
if idx = String.size str then
case tl of
str :: tl => fStart (0, absIdx, str, tl, currentState, counter)
| [] => Int.max (0, absIdx - 2)
else
let
val chr = String.sub (str, idx)
val newState = next (currentState, chr)
in
if newState = spaceAfterNonBlankState then
if counter - 1 = 0 then
Int.max (0, absIdx - 1)
else
fStart
(idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else
fStart (idx + 1, absIdx + 1, str, tl, newState, counter)
end
end)
structure StartOfNextWORDStrict =
MakePrevDfaLoop
(struct val startState = startState val fStart = startOfCurrentWORD end)
structure EndOfCurrentWORDStrict =
MakeNextDfaLoop
(struct val startState = startState val fStart = endOfCurrentWORD end)
(* W *)
val startOfNextWORD = StartOfNextWORD.next
@@ -170,4 +175,20 @@ struct
val startOfCurrentWORD = StartOfCurrentWORD.prev
(* E *)
val endOfCurrentWORD = EndOfCurrentWORD.next
(* functions to strictly get the start and end of the current word.
* Problem: We want to support Vi motions like viW (selects a single word),
* as well as ciW (change one WORD) and diW (delete one WORD).
*
* The 'startOfCurrentWORD' and 'endOfCurrentWORD' functions do this
* (representing the vi 'B' and 'E' commands respectively),
* except that 'B' goes to the previous WORD if the cursor is on the first
* character of the current WORD, and 'E' goes to the next WORD if the cursor
* is on the last character of the current WORD.
*
* What is meant by "strict" is that these below functions always stay
* within the current WORD, not making the two exceptions mentioned above.
*)
val startOfCurrentWORDStrict = StartOfNextWORDStrict.prev
val endOfCurrentWORDStrict = EndOfCurrentWORDStrict.next
end