simplify viK and especially startViK in cursor.sml, by only checking (strIdx - 1) and (strIdx - 2)

This commit is contained in:
2024-11-06 19:47:14 +00:00
parent 6b9cf8dbc2
commit 9f0982ad91
2 changed files with 13 additions and 81 deletions

View File

@@ -559,34 +559,14 @@ struct
fun startViK (lg, strIdx, shd, cursorIdx, leftStrings, lhd, leftLines) =
if String.sub (shd, strIdx) = #"\n" then
(* ? -> \n *)
if strIdx > 0 then
(* strIdx - 1 is in shd *)
if String.sub (shd, strIdx - 1) = #"\n" then
(* strIdx - 1 is \n *)
if strIdx - 1 > 0 then
(* strIdx - 2 is in shd *)
if String.sub (shd, strIdx - 2) = #"\n" then
(* \n -> \n *)
cursorIdx - 1
else
(* graphical-chr -> \n *)
helpVi0
(strIdx - 2, shd, cursorIdx - 2, leftStrings, leftLines)
else
(* strIdx - 2 is in leftStrings *)
case (leftStrings, leftLines) of
(lshd :: lstl, llhd :: lltl) =>
if String.sub (lshd, String.size lshd - 1) = #"\n" then
(* \n -> \n *)
cursorIdx - 1
else
(* graphical-chr -> \n *)
helpVi0
(String.size lshd - 2, lshd, cursorIdx - 2, lstl, lltl)
| (_, _) =>
cursorIdx - 1
(* \n -> \n *)
cursorIdx
else
(* strIdx - 1 is not \n *)
(* non-graphical-chr -> \n *)
let
val lineColumn = getCursorColumn (lg, cursorIdx)
in
@@ -601,49 +581,18 @@ struct
case (leftStrings, leftLines) of
(lshd :: lstl, llhd :: lltl) =>
if String.sub (lshd, String.size lshd - 1) = #"\n" then
(* ? -> \n *)
if String.size lshd > 1 then
(* if strIdx - 2 is in lshd *)
if String.sub (lshd, String.size lshd - 2) = #"\n" then
(* \n -> \n *)
cursorIdx - 1
else
(* graphical-chr -> \n *)
helpVi0
(String.size lshd - 2, lshd, cursorIdx - 2, lstl, lltl)
else
(* strIdx - 2 is in lstl *)
(case (lstl, lltl) of
(stlhd :: stltl, ltlhd :: ltltl) =>
if String.sub (stlhd, String.size stlhd - 1) = #"\n" then
(* \n -> \n *)
cursorIdx - 1
else
(* graphical-chr -> \n *)
helpVi0
( String.size stlhd - 2, stlhd
, cursorIdx - 2, stltl, ltltl
)
| (_, _) =>
cursorIdx - 1)
(* \n -> \n *)
cursorIdx
else
(* does not start with \n
* so start viK normally *)
let
val lineColumn = getCursorColumn (lg, cursorIdx)
in
helpViK
( strIdx, shd, cursorIdx
, lineColumn, lineColumn, false
, leftStrings, lhd, leftLines
)
end
(* graphical-chr -> \n *)
helpVi0
(String.size lshd - 2, lshd, cursorIdx - 2, lstl, lltl)
| (_, _) => cursorIdx
else
(* strIdx does not start with \n
* so start viK normally*)
let
val lineColumn = getCursorColumn (lg, cursorIdx)
val lineColumn = getCursorColumn (lg, cursorIdx) + 1
in
helpViK
( strIdx, shd, cursorIdx
@@ -659,30 +608,13 @@ struct
lineGap
in
case (rightStrings, rightLines) of
(strHd :: strTl, lnHd :: lnTl) =>
(strHd :: _, lnHd :: _) =>
let
(* convert absolute cursorIdx to idx relative to hd string *)
val strIdx = cursorIdx - bufferIdx
in
if strIdx < String.size strHd then
(* strIdx is in this string *)
startViK
(lineGap, strIdx, strHd, cursorIdx, leftStrings, lnHd, leftLines)
else
(* strIdx must be in the strTl *)
(case (strTl, lnTl) of
(nestStrHd :: nestStrTl, nestLnHd :: nestLnTl) =>
let
val strIdx = strIdx - String.size strHd
val leftStrings = strHd :: leftStrings
val leftLines = lnHd :: leftLines
in
startViK
( lineGap, strIdx, nestStrHd, cursorIdx
, leftStrings, nestLnHd, leftLines
)
end
| (_, _) => cursorIdx)
startViK
(lineGap, strIdx - 1, strHd, cursorIdx - 1, leftStrings, lnHd, leftLines)
end
| (_, _) =>
(* nowhere to go rightward, so return cursorIdx *)

BIN
shf

Binary file not shown.