reimplement vi's 'b' command

This commit is contained in:
2025-07-20 17:36:33 +01:00
parent 33474fb5ae
commit 146953c25d
2 changed files with 52 additions and 6 deletions

View File

@@ -9,11 +9,11 @@ struct
val alphaToSpace: Word8.word = 0w4 val alphaToSpace: Word8.word = 0w4
val punctToSpace: Word8.word = 0w5 val punctToSpace: Word8.word = 0w5
val alphaToPunct: Word8.word = 0w6 val spaceToAlpha: Word8.word = 0w6
val punctToAlpha: Word8.word = 0w7 val spaceToPunct: Word8.word = 0w7
val spaceToAlpha: Word8.word = 0w8 val alphaToPunct: Word8.word = 0w8
val spaceToPunct: Word8.word = 0w9 val punctToAlpha: Word8.word = 0w9
fun makeStart i = fun makeStart i =
let let
@@ -60,13 +60,21 @@ struct
val alphaToSpaceTable = startSpaceTable val alphaToSpaceTable = startSpaceTable
val punctToSpaceTable = startSpaceTable val punctToSpaceTable = startSpaceTable
val spaceToAlphaTable = startAlphaTable
val spaceToPunctTable = startPunctTable
val tables = val tables =
#[ startTable #[ startTable
, startAlphaTable , startAlphaTable
, startSpaceTable , startSpaceTable
, startPunctTable , startPunctTable
, alphaToSpaceTable , alphaToSpaceTable
, punctToSpaceTable , punctToSpaceTable
, spaceToAlphaTable
, spaceToPunctTable
] ]
fun next (currentState, chr) = fun next (currentState, chr) =
@@ -139,8 +147,47 @@ struct
end end
end) 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)
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)
(* w *) (* w *)
val startOfNextWord = StartOfNextWord.next val startOfNextWord = StartOfNextWord.next
(* ge *) (* ge *)
val endOfPrevWord = EndOfPrevWord.prev val endOfPrevWord = EndOfPrevWord.prev
(* b *)
val startOfCurrentWord = StartOfCurrentWord.prev
end end

View File

@@ -974,8 +974,7 @@ struct
end end
(* equivalent of vi's 'b' command *) (* equivalent of vi's 'b' command *)
fun prevWord (lineGap, cursorIdx) = val prevWord = ViWordDfa.startOfCurrentWord
toPrevWord (lineGap, cursorIdx, helpPrevWord)
(* equivalent of vi's 'B' command *) (* equivalent of vi's 'B' command *)
val prevWORD = ViWORDDfa.startOfCurrentWORD val prevWORD = ViWORDDfa.startOfCurrentWORD