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
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 = structure StartOfCurrentWORD =
MakePrevDfaLoopMinus1 MakePrevDfaLoopMinus1
(struct (struct val startState = startState val fStart = startOfCurrentWORD end)
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)
structure EndOfCurrentWORD = structure EndOfCurrentWORD =
MakeNextDfaLoopPlus1 MakeNextDfaLoopPlus1
(struct (struct val startState = startState val fStart = endOfCurrentWORD end)
val startState = startState
fun fStart (idx, absIdx, str, tl, currentState, counter) = structure StartOfNextWORDStrict =
if idx = String.size str then MakePrevDfaLoop
case tl of (struct val startState = startState val fStart = startOfCurrentWORD end)
str :: tl => fStart (0, absIdx, str, tl, currentState, counter)
| [] => Int.max (0, absIdx - 2) structure EndOfCurrentWORDStrict =
else MakeNextDfaLoop
let (struct val startState = startState val fStart = endOfCurrentWORD end)
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)
(* W *) (* W *)
val startOfNextWORD = StartOfNextWORD.next val startOfNextWORD = StartOfNextWORD.next
@@ -170,4 +175,20 @@ struct
val startOfCurrentWORD = StartOfCurrentWORD.prev val startOfCurrentWORD = StartOfCurrentWORD.prev
(* E *) (* E *)
val endOfCurrentWORD = EndOfCurrentWORD.next 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 end

View File

@@ -147,69 +147,73 @@ struct
end end
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) handle _ => (print "156\n"; raise Empty)
val newState =
next (currentState, chr)
handle _ =>
(print ("158: " ^ Word8.toString currentState ^ "\n"); raise Empty)
in
if
newState = alphaToSpace orelse newState = punctToSpace
orelse newState = alphaToPunct orelse newState = punctToAlpha
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
structure StartOfCurrentWord = structure StartOfCurrentWord =
MakePrevDfaLoopMinus1 MakePrevDfaLoopMinus1
(struct (struct val startState = startState val fStart = startOfCurrentWord end)
val startState = startState
fun fStart (idx, absIdx, str, tl, currentState, counter) = structure StartOfCurrentWordStrict =
if idx < 0 then MakePrevDfaLoop
case tl of (struct val startState = startState val fStart = startOfCurrentWord end)
str :: tl =>
fStart
(String.size str - 1, absIdx, str, tl, currentState, counter)
| [] => 0
else
let
val chr = String.sub (str, idx)
handle _ => (print "156\n"; raise Empty)
val newState =
next (currentState, chr)
handle _ =>
( print ("158: " ^ Word8.toString currentState ^ "\n")
; raise Empty
)
in
if
newState = alphaToSpace orelse newState = punctToSpace
orelse newState = alphaToPunct orelse newState = punctToAlpha
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)
structure EndOfCurrentWord = MakeNextDfaLoopPlus1 ( fun endOfCurrentWord (idx, absIdx, str, tl, currentState, counter) =
struct if idx = String.size str then
val startState = startState case tl of
str :: tl =>
fun fStart (idx, absIdx, str, tl, currentState, counter) = endOfCurrentWord (0, absIdx, str, tl, currentState, counter)
if idx = String.size str then | [] => Int.max (0, absIdx - 2)
case tl of else
str :: tl => let
fStart (0, absIdx, str, tl, currentState, counter) val chr = String.sub (str, idx)
| [] => Int.max (0, absIdx - 2) val newState = next (currentState, chr)
else in
let if
val chr = String.sub (str, idx) newState = alphaToSpace orelse newState = punctToSpace
val newState = next (currentState, chr) orelse newState = alphaToPunct orelse newState = punctToAlpha
in then
if newState = alphaToSpace orelse newState = punctToSpace if counter - 1 = 0 then
orelse newState = alphaToPunct orelse newState = punctToAlpha then absIdx - 1
if counter - 1 = 0 then
absIdx - 1
else
fStart (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else else
fStart (idx + 1, absIdx + 1, str, tl, newState, counter) endOfCurrentWord
end (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
end else
) endOfCurrentWord (idx + 1, absIdx + 1, str, tl, newState, counter)
end
structure EndOfCurrentWord =
MakeNextDfaLoopPlus1
(struct val startState = startState val fStart = endOfCurrentWord end)
structure EndOfCurrentWordStrict =
MakeNextDfaLoop
(struct val startState = startState val fStart = endOfCurrentWord end)
(* w *) (* w *)
val startOfNextWord = StartOfNextWord.next val startOfNextWord = StartOfNextWord.next
@@ -219,4 +223,9 @@ struct
val startOfCurrentWord = StartOfCurrentWord.prev val startOfCurrentWord = StartOfCurrentWord.prev
(* e *) (* e *)
val endOfCurrentWord = EndOfCurrentWord.next val endOfCurrentWord = EndOfCurrentWord.next
(* the meaning of "Strict" and the utility of these two functions
* is described in vi-WORD-dfa.sml *)
val startOfCurrentWordStrict = StartOfCurrentWordStrict.prev
val endOfCurrentWordStrict = EndOfCurrentWordStrict.next
end end