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,16 +108,11 @@ struct
end end
end) end)
structure StartOfCurrentWORD = fun startOfCurrentWORD (idx, absIdx, str, tl, currentState, counter) =
MakePrevDfaLoopMinus1
(struct
val startState = startState
fun fStart (idx, absIdx, str, tl, currentState, counter) =
if idx < 0 then if idx < 0 then
case tl of case tl of
str :: tl => str :: tl =>
fStart startOfCurrentWORD
(String.size str - 1, absIdx, str, tl, currentState, counter) (String.size str - 1, absIdx, str, tl, currentState, counter)
| [] => 0 | [] => 0
else else
@@ -129,22 +124,17 @@ struct
if counter - 1 = 0 then if counter - 1 = 0 then
absIdx + 1 absIdx + 1
else else
fStart startOfCurrentWORD
(idx - 1, absIdx - 1, str, tl, startState, counter - 1) (idx - 1, absIdx - 1, str, tl, startState, counter - 1)
else else
fStart (idx - 1, absIdx - 1, str, tl, newState, counter) startOfCurrentWORD (idx - 1, absIdx - 1, str, tl, newState, counter)
end end
end)
structure EndOfCurrentWORD = fun endOfCurrentWORD (idx, absIdx, str, tl, currentState, counter) =
MakeNextDfaLoopPlus1
(struct
val startState = startState
fun fStart (idx, absIdx, str, tl, currentState, counter) =
if idx = String.size str then if idx = String.size str then
case tl of case tl of
str :: tl => fStart (0, absIdx, str, tl, currentState, counter) str :: tl =>
endOfCurrentWORD (0, absIdx, str, tl, currentState, counter)
| [] => Int.max (0, absIdx - 2) | [] => Int.max (0, absIdx - 2)
else else
let let
@@ -155,12 +145,27 @@ struct
if counter - 1 = 0 then if counter - 1 = 0 then
Int.max (0, absIdx - 1) Int.max (0, absIdx - 1)
else else
fStart endOfCurrentWORD
(idx + 1, absIdx + 1, str, tl, startState, counter - 1) (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else else
fStart (idx + 1, absIdx + 1, str, tl, newState, counter) endOfCurrentWORD (idx + 1, absIdx + 1, str, tl, newState, counter)
end end
end)
structure StartOfCurrentWORD =
MakePrevDfaLoopMinus1
(struct val startState = startState val fStart = startOfCurrentWORD end)
structure EndOfCurrentWORD =
MakeNextDfaLoopPlus1
(struct val startState = startState val fStart = endOfCurrentWORD end)
structure StartOfNextWORDStrict =
MakePrevDfaLoop
(struct val startState = startState val fStart = startOfCurrentWORD end)
structure EndOfCurrentWORDStrict =
MakeNextDfaLoop
(struct val startState = startState val fStart = endOfCurrentWORD 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,28 +147,20 @@ struct
end end
end) end)
structure StartOfCurrentWord = fun startOfCurrentWord (idx, absIdx, str, tl, currentState, counter) =
MakePrevDfaLoopMinus1
(struct
val startState = startState
fun fStart (idx, absIdx, str, tl, currentState, counter) =
if idx < 0 then if idx < 0 then
case tl of case tl of
str :: tl => str :: tl =>
fStart startOfCurrentWord
(String.size str - 1, absIdx, str, tl, currentState, counter) (String.size str - 1, absIdx, str, tl, currentState, counter)
| [] => 0 | [] => 0
else else
let let
val chr = String.sub (str, idx) val chr = String.sub (str, idx) handle _ => (print "156\n"; raise Empty)
handle _ => (print "156\n"; raise Empty)
val newState = val newState =
next (currentState, chr) next (currentState, chr)
handle _ => handle _ =>
( print ("158: " ^ Word8.toString currentState ^ "\n") (print ("158: " ^ Word8.toString currentState ^ "\n"); raise Empty)
; raise Empty
)
in in
if if
newState = alphaToSpace orelse newState = punctToSpace newState = alphaToSpace orelse newState = punctToSpace
@@ -177,39 +169,51 @@ struct
if counter - 1 = 0 then if counter - 1 = 0 then
absIdx + 1 absIdx + 1
else else
fStart startOfCurrentWord
(idx - 1, absIdx - 1, str, tl, startState, counter - 1) (idx - 1, absIdx - 1, str, tl, startState, counter - 1)
else else
fStart (idx - 1, absIdx - 1, str, tl, newState, counter) startOfCurrentWord (idx - 1, absIdx - 1, str, tl, newState, counter)
end end
end)
structure EndOfCurrentWord = MakeNextDfaLoopPlus1 ( structure StartOfCurrentWord =
struct MakePrevDfaLoopMinus1
val startState = startState (struct val startState = startState val fStart = startOfCurrentWord end)
fun fStart (idx, absIdx, str, tl, currentState, counter) = structure StartOfCurrentWordStrict =
MakePrevDfaLoop
(struct val startState = startState val fStart = startOfCurrentWord end)
fun endOfCurrentWord (idx, absIdx, str, tl, currentState, counter) =
if idx = String.size str then if idx = String.size str then
case tl of case tl of
str :: tl => str :: tl =>
fStart (0, absIdx, str, tl, currentState, counter) endOfCurrentWord (0, absIdx, str, tl, currentState, counter)
| [] => Int.max (0, absIdx - 2) | [] => Int.max (0, absIdx - 2)
else else
let let
val chr = String.sub (str, idx) val chr = String.sub (str, idx)
val newState = next (currentState, chr) val newState = next (currentState, chr)
in in
if newState = alphaToSpace orelse newState = punctToSpace if
orelse newState = alphaToPunct orelse newState = punctToAlpha then newState = alphaToSpace orelse newState = punctToSpace
orelse newState = alphaToPunct orelse newState = punctToAlpha
then
if counter - 1 = 0 then if counter - 1 = 0 then
absIdx - 1 absIdx - 1
else else
fStart (idx + 1, absIdx + 1, str, tl, startState, counter - 1) endOfCurrentWord
(idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else else
fStart (idx + 1, absIdx + 1, str, tl, newState, counter) endOfCurrentWord (idx + 1, absIdx + 1, str, tl, newState, counter)
end end
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