2024-10-17 02:57:26 +01:00
|
|
|
structure Cursor =
|
|
|
|
|
struct
|
2024-11-26 21:57:02 +00:00
|
|
|
(* returns absolute index of previous line break in string *)
|
|
|
|
|
fun helpVi0 (absIdx, stl, ltl) =
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
if Vector.length lhd > 0 then
|
|
|
|
|
let
|
|
|
|
|
val startAbsIdx = absIdx - String.size shd
|
|
|
|
|
val lineIdx = Vector.sub (lhd, Vector.length lhd - 1)
|
|
|
|
|
in
|
|
|
|
|
(* found lineIdx.
|
|
|
|
|
* Need to make sure we follow cursor-on-linebreak rule:
|
|
|
|
|
* If line break is preceded by non-line break char,
|
|
|
|
|
* then increment by 1.
|
|
|
|
|
* *)
|
2025-03-22 05:18:25 +00:00
|
|
|
startAbsIdx + lineIdx + 1
|
2024-11-26 21:57:02 +00:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
helpVi0 (absIdx - String.size shd, stl, ltl)
|
|
|
|
|
| (_, _) => 0
|
|
|
|
|
|
|
|
|
|
fun startVi0 (strPos, shd, lhd, absIdx, stl, ltl) =
|
|
|
|
|
if String.sub (shd, strPos) = #"\n" then
|
|
|
|
|
absIdx
|
2025-03-22 05:18:25 +00:00
|
|
|
else if Vector.length lhd > 0 then
|
|
|
|
|
if Vector.sub (lhd, 0) < strPos then
|
|
|
|
|
let
|
|
|
|
|
val linePos = BinSearch.equalOrLess (strPos - 1, lhd)
|
|
|
|
|
val lineIdx = Vector.sub (lhd, linePos)
|
|
|
|
|
in
|
|
|
|
|
absIdx - strPos + lineIdx + 1
|
|
|
|
|
end
|
2024-11-26 21:57:02 +00:00
|
|
|
else
|
|
|
|
|
helpVi0 (absIdx - strPos, stl, ltl)
|
2025-03-22 05:18:25 +00:00
|
|
|
else
|
|
|
|
|
helpVi0 (absIdx - strPos, stl, ltl)
|
2024-10-22 07:06:16 +01:00
|
|
|
|
2025-08-04 03:23:27 +01:00
|
|
|
structure Vi0 = MakeIfCharFolderPrev (struct val fStart = startVi0 end)
|
|
|
|
|
|
|
|
|
|
val vi0 = Vi0.foldPrev
|
2024-10-22 07:06:16 +01:00
|
|
|
|
2024-10-25 15:25:39 +01:00
|
|
|
fun helpViDlr (strPos, str, absIdx, strTl, lineTl) =
|
2024-10-22 07:06:16 +01:00
|
|
|
if strPos = String.size str then
|
2024-10-25 15:25:39 +01:00
|
|
|
case (strTl, lineTl) of
|
2024-10-25 15:47:39 +01:00
|
|
|
(shd :: stl, lhd :: ltl) => helpViDlr (0, shd, absIdx, stl, ltl)
|
|
|
|
|
| (_, _) => absIdx - 1
|
2024-10-22 07:06:16 +01:00
|
|
|
else
|
|
|
|
|
case String.sub (str, strPos) of
|
2024-10-25 15:47:39 +01:00
|
|
|
#"\n" => absIdx - 1
|
|
|
|
|
| _ => helpViDlr (strPos + 1, str, absIdx + 1, strTl, lineTl)
|
2024-10-22 07:06:16 +01:00
|
|
|
|
2024-10-25 15:25:39 +01:00
|
|
|
fun viDlr (lineGap: LineGap.t, cursorIdx) =
|
2024-10-22 07:06:16 +01:00
|
|
|
let
|
|
|
|
|
val
|
|
|
|
|
{rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} =
|
|
|
|
|
lineGap
|
|
|
|
|
in
|
|
|
|
|
case (rightStrings, rightLines) of
|
|
|
|
|
(strHd :: strTl, lnHd :: lnTl) =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
if strIdx < String.size strHd then
|
|
|
|
|
if String.sub (strHd, strIdx) <> #"\n" then
|
|
|
|
|
(* not in double linebreak *)
|
2024-10-25 15:47:39 +01:00
|
|
|
helpViDlr (strIdx + 1, strHd, cursorIdx + 1, strTl, lnTl)
|
2025-03-22 05:18:25 +00:00
|
|
|
else (* check if we are in double linebreak *) if strIdx - 1 >= 0 then
|
|
|
|
|
if String.sub (strHd, strIdx - 1) = #"\n" then
|
|
|
|
|
(* we are in double linebreak, so do nothing *)
|
|
|
|
|
cursorIdx
|
2024-10-22 07:06:16 +01:00
|
|
|
else
|
2025-03-22 05:18:25 +00:00
|
|
|
(* not in double linebreak, so iterate *)
|
|
|
|
|
helpViDlr (strIdx + 1, strHd, cursorIdx + 1, strTl, lnTl)
|
|
|
|
|
else
|
|
|
|
|
(* check if double linebreak in strTl *)
|
|
|
|
|
(case strTl of
|
|
|
|
|
nestStrHd :: _ =>
|
|
|
|
|
if String.sub (nestStrHd, 0) = #"\n" then
|
|
|
|
|
cursorIdx
|
|
|
|
|
else
|
|
|
|
|
helpViDlr (strIdx + 1, strHd, cursorIdx + 1, strTl, lnTl)
|
|
|
|
|
| [] => cursorIdx)
|
2024-10-22 07:06:16 +01:00
|
|
|
else
|
|
|
|
|
(* strIdx must be in the strTl *)
|
|
|
|
|
(case (strTl, lnTl) of
|
|
|
|
|
(nestStrHd :: nestStrTl, nestLnHd :: nestLnTl) =>
|
|
|
|
|
let
|
|
|
|
|
val strIdx = strIdx - String.size strHd
|
|
|
|
|
in
|
2024-10-25 15:47:39 +01:00
|
|
|
helpViDlr
|
|
|
|
|
( strIdx + 1
|
|
|
|
|
, nestStrHd
|
|
|
|
|
, cursorIdx + 1
|
|
|
|
|
, nestStrTl
|
|
|
|
|
, nestLnTl
|
|
|
|
|
)
|
2024-10-22 07:06:16 +01:00
|
|
|
end
|
|
|
|
|
| (_, _) => cursorIdx)
|
|
|
|
|
end
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => (* nowhere to go, so return cursorIdx *) cursorIdx
|
2024-10-22 07:06:16 +01:00
|
|
|
end
|
|
|
|
|
|
2024-11-05 13:14:52 +00:00
|
|
|
fun helpViL (strIdx, hd, cursorIdx, tl) =
|
|
|
|
|
if String.sub (hd, strIdx) = #"\n" then
|
|
|
|
|
(* if line break, go to next idx, no matter what chr is there *)
|
|
|
|
|
cursorIdx + 1
|
2025-03-22 05:18:25 +00:00
|
|
|
else if strIdx + 1 < String.size hd then
|
|
|
|
|
(* next chr is in hd *)
|
|
|
|
|
if String.sub (hd, strIdx + 1) = #"\n" then
|
|
|
|
|
(* if non-line break followed by line break, go to chr + 2 *)
|
|
|
|
|
cursorIdx + 2
|
2024-11-05 13:14:52 +00:00
|
|
|
else
|
2025-03-22 05:18:25 +00:00
|
|
|
cursorIdx + 1
|
|
|
|
|
else
|
|
|
|
|
(case tl of
|
|
|
|
|
tlhd :: _ =>
|
|
|
|
|
if String.sub (tlhd, 0) = #"\n" then
|
|
|
|
|
(* non-line break followed by line break *)
|
|
|
|
|
cursorIdx + 2
|
|
|
|
|
else
|
|
|
|
|
(* non-line break followed by non-line break *)
|
|
|
|
|
cursorIdx + 1
|
|
|
|
|
| [] => cursorIdx)
|
2024-11-05 13:14:52 +00:00
|
|
|
|
2024-10-17 08:40:28 +01:00
|
|
|
(* Prerequisite: lineGap is moved to requested idx first
|
|
|
|
|
* todo: check if we are in a \r\n pair, but this is not a priority *)
|
|
|
|
|
fun viL (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
|
|
|
|
in
|
|
|
|
|
case rightStrings of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
2024-11-05 13:14:52 +00:00
|
|
|
if strIdx < String.size hd then
|
|
|
|
|
(* strIdx is in hd *)
|
|
|
|
|
helpViL (strIdx, hd, cursorIdx, tl)
|
2024-10-17 08:40:28 +01:00
|
|
|
else
|
2024-11-05 13:14:52 +00:00
|
|
|
(* strIdx is in tl *)
|
2024-10-17 08:40:28 +01:00
|
|
|
(case tl of
|
2025-03-22 05:18:25 +00:00
|
|
|
tlhd :: tltl =>
|
|
|
|
|
let val strIdx = strIdx - String.size hd
|
|
|
|
|
in helpViL (strIdx, tlhd, cursorIdx, tltl)
|
|
|
|
|
end
|
|
|
|
|
| [] => cursorIdx)
|
2024-10-17 08:40:28 +01:00
|
|
|
end
|
|
|
|
|
| [] =>
|
|
|
|
|
(* return original cursorIdx if there is nothing to the right *)
|
|
|
|
|
cursorIdx
|
|
|
|
|
end
|
2024-10-18 00:40:43 +01:00
|
|
|
|
2024-11-05 19:44:49 +00:00
|
|
|
fun helpViH (strIdx, hd, cursorIdx, leftStrings) =
|
|
|
|
|
if strIdx > 0 then
|
|
|
|
|
(* bounds check: can access prev char in hd *)
|
|
|
|
|
if String.sub (hd, strIdx - 1) = #"\n" then
|
|
|
|
|
(* prev char is line break *)
|
|
|
|
|
if strIdx - 1 > 0 then
|
|
|
|
|
(* bounds check: can access two chars back in hd *)
|
|
|
|
|
if String.sub (hd, strIdx - 2) = #"\n" then
|
|
|
|
|
(* line break followed by line break
|
|
|
|
|
* so it is fine to decrement by 1 *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
else
|
|
|
|
|
(* non-line break followed by line break
|
|
|
|
|
* so we have to decrement by two,
|
|
|
|
|
* skipping over line break *)
|
2025-03-22 05:18:25 +00:00
|
|
|
cursorIdx - 2
|
2024-11-05 19:44:49 +00:00
|
|
|
else
|
|
|
|
|
(* need to check two chars back in leftStrings *)
|
|
|
|
|
(case leftStrings of
|
2025-03-22 05:18:25 +00:00
|
|
|
lhd :: ltl =>
|
|
|
|
|
if String.sub (lhd, String.size lhd - 1) = #"\n" then
|
|
|
|
|
(* double line break *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
else
|
|
|
|
|
(* non-line break precedes line break *)
|
|
|
|
|
cursorIdx - 2
|
|
|
|
|
| [] => cursorIdx - 1)
|
2024-11-05 19:44:49 +00:00
|
|
|
else
|
|
|
|
|
(* prev char is not line break so we can decrement by 1 *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
else
|
|
|
|
|
(* prev char is in leftStrings *)
|
|
|
|
|
(case leftStrings of
|
2025-03-22 05:18:25 +00:00
|
|
|
lhd :: ltl =>
|
|
|
|
|
if String.sub (lhd, String.size lhd - 1) = #"\n" then
|
|
|
|
|
(* one line break *)
|
|
|
|
|
if String.size lhd > 1 then
|
|
|
|
|
(* bounds check: prev-prev chr is in lhd *)
|
|
|
|
|
if String.sub (lhd, String.size lhd - 2) = #"\n" then
|
|
|
|
|
(* double line break *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
else
|
|
|
|
|
(* non-line break precedes line break *)
|
|
|
|
|
cursorIdx - 2
|
|
|
|
|
else
|
|
|
|
|
(* prev-prev chr is in ltl *)
|
|
|
|
|
(case ltl of
|
|
|
|
|
ltlhd :: _ =>
|
|
|
|
|
if String.sub (ltlhd, String.size ltlhd - 1) = #"\n" then
|
|
|
|
|
(* double line break *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
else
|
|
|
|
|
(* non-line break precedes line break *)
|
|
|
|
|
cursorIdx - 2
|
|
|
|
|
| [] => cursorIdx - 1)
|
|
|
|
|
else
|
|
|
|
|
(* no line break *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
| [] => 0)
|
2024-11-05 19:44:49 +00:00
|
|
|
|
2024-11-05 19:50:36 +00:00
|
|
|
(* Prerequisite: lineGap is moved to requested idx first. *)
|
2024-10-18 00:40:43 +01:00
|
|
|
fun viH (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, leftStrings, idx = bufferIdx, ...} = lineGap
|
|
|
|
|
in
|
|
|
|
|
case rightStrings of
|
2024-11-05 19:50:36 +00:00
|
|
|
hd :: tl =>
|
2024-10-18 00:40:43 +01:00
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
2024-11-05 19:44:49 +00:00
|
|
|
if strIdx < String.size hd then
|
2024-11-05 19:50:36 +00:00
|
|
|
(* strIdx in hd *)
|
2024-11-05 19:44:49 +00:00
|
|
|
helpViH (strIdx, hd, cursorIdx, leftStrings)
|
2024-10-18 00:40:43 +01:00
|
|
|
else
|
2024-11-05 19:50:36 +00:00
|
|
|
(* strIdx in tl *)
|
|
|
|
|
(case tl of
|
2025-03-22 05:18:25 +00:00
|
|
|
tlhd :: tltl =>
|
|
|
|
|
let val strIdx = strIdx - String.size hd
|
|
|
|
|
in helpViH (strIdx, tlhd, cursorIdx, hd :: leftStrings)
|
|
|
|
|
end
|
|
|
|
|
| [] => cursorIdx)
|
2024-10-18 00:40:43 +01:00
|
|
|
end
|
|
|
|
|
| [] => cursorIdx
|
|
|
|
|
end
|
2024-10-19 07:03:32 +01:00
|
|
|
|
|
|
|
|
fun helpGetCursorColumn (distanceFromLine, strList, lineList) =
|
|
|
|
|
case (strList, lineList) of
|
|
|
|
|
(strHd :: strTl, lnHd :: lnTl) =>
|
|
|
|
|
if Vector.length lnHd = 0 then
|
|
|
|
|
(* lnHd is empty, so line is not here *)
|
|
|
|
|
helpGetCursorColumn
|
|
|
|
|
(distanceFromLine + String.size strHd, strTl, lnTl)
|
|
|
|
|
else
|
|
|
|
|
(* lnHd is not empty, meaning last lineIdx is closest linebreak *)
|
|
|
|
|
let
|
|
|
|
|
val lineIdx = Vector.sub (lnHd, Vector.length lnHd - 1)
|
|
|
|
|
(* number of chars after the lineIdx *)
|
|
|
|
|
val idxAfterLn = String.size strHd - lineIdx
|
|
|
|
|
in
|
|
|
|
|
distanceFromLine + idxAfterLn - 1
|
|
|
|
|
end
|
|
|
|
|
| (_, _) => distanceFromLine
|
|
|
|
|
|
2024-11-07 20:51:02 +00:00
|
|
|
fun helpGetCursorColumnLeft (leftStrings, leftLines, cursorIdx) =
|
|
|
|
|
case (leftStrings, leftLines) of
|
|
|
|
|
(lshd :: lstl, llhd :: lltl) =>
|
|
|
|
|
let
|
|
|
|
|
val cursorIdx = cursorIdx - String.size lshd
|
|
|
|
|
in
|
|
|
|
|
if Vector.length llhd > 0 then
|
2025-03-22 05:18:25 +00:00
|
|
|
let val lnIdx = Vector.sub (llhd, Vector.length llhd - 1)
|
|
|
|
|
in lnIdx + cursorIdx
|
2024-11-07 20:51:02 +00:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
helpGetCursorColumnLeft (lstl, lltl, cursorIdx)
|
|
|
|
|
end
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => Int.max (cursorIdx, 0)
|
2024-11-07 20:51:02 +00:00
|
|
|
|
|
|
|
|
fun getCursorColumn (strIdx, strHd, lnHd, leftStrings, leftLines, cursorIdx) =
|
|
|
|
|
if Vector.length lnHd > 0 then
|
2024-10-19 23:04:26 +01:00
|
|
|
let
|
2024-11-07 20:51:02 +00:00
|
|
|
val firstLn = Vector.sub (lnHd, 0)
|
2024-10-19 23:04:26 +01:00
|
|
|
in
|
2024-11-07 20:51:02 +00:00
|
|
|
if firstLn > strIdx then
|
|
|
|
|
(* search left strings/lines *)
|
2024-10-19 23:04:26 +01:00
|
|
|
let
|
2025-03-22 05:18:25 +00:00
|
|
|
val lineIdx =
|
2024-11-07 20:51:02 +00:00
|
|
|
helpGetCursorColumnLeft
|
|
|
|
|
(leftStrings, leftLines, cursorIdx - strIdx)
|
2024-10-19 23:04:26 +01:00
|
|
|
in
|
2025-03-22 05:18:25 +00:00
|
|
|
if lineIdx = 0 then cursorIdx else cursorIdx - lineIdx - 1
|
2024-10-19 23:04:26 +01:00
|
|
|
end
|
2024-11-07 20:51:02 +00:00
|
|
|
else if firstLn < strIdx then
|
|
|
|
|
(* binary search in here
|
|
|
|
|
* because we know lnHd definitely contains
|
|
|
|
|
* a lineIdx less or equal to strIdx *)
|
2024-10-19 07:03:32 +01:00
|
|
|
let
|
2024-11-11 13:23:37 +00:00
|
|
|
val lnIdx = BinSearch.equalOrLess (strIdx, lnHd)
|
2024-11-07 20:51:02 +00:00
|
|
|
val lnIdx = Vector.sub (lnHd, lnIdx)
|
2024-10-19 07:03:32 +01:00
|
|
|
in
|
2025-03-22 05:18:25 +00:00
|
|
|
if lnIdx < strIdx then strIdx - lnIdx - 1
|
|
|
|
|
else (* firstLn = strIdx *) 0
|
2024-10-19 07:03:32 +01:00
|
|
|
end
|
2024-11-07 20:51:02 +00:00
|
|
|
else
|
|
|
|
|
(* firstLn = strIdx
|
|
|
|
|
* meaning that we are already at a line break
|
|
|
|
|
* and that the column is 0 *)
|
2025-03-22 05:18:25 +00:00
|
|
|
0
|
2024-11-07 20:51:02 +00:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
2025-03-22 05:18:25 +00:00
|
|
|
val lineIdx =
|
|
|
|
|
helpGetCursorColumnLeft (leftStrings, leftLines, cursorIdx - strIdx)
|
2024-11-07 20:51:02 +00:00
|
|
|
in
|
2025-03-22 05:18:25 +00:00
|
|
|
if lineIdx = 0 then cursorIdx else cursorIdx - lineIdx - 1
|
2024-11-07 20:51:02 +00:00
|
|
|
end
|
2024-10-20 21:30:06 +01:00
|
|
|
|
2024-10-25 15:25:39 +01:00
|
|
|
fun helpViJ
|
2025-03-22 05:18:25 +00:00
|
|
|
( strPos
|
|
|
|
|
, str
|
|
|
|
|
, absIdx
|
|
|
|
|
, lineColumn
|
|
|
|
|
, preferredColumn
|
|
|
|
|
, hasPassedLine
|
|
|
|
|
, strTl
|
|
|
|
|
, lineTl
|
|
|
|
|
, prevIsLn
|
2024-10-20 21:30:06 +01:00
|
|
|
) =
|
2024-10-25 15:47:39 +01:00
|
|
|
if strPos = String.size str then
|
|
|
|
|
case (strTl, lineTl) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
(* todo: possibly check if we have passed line,
|
|
|
|
|
* and if so, if there are any line breaks in the lineHd
|
|
|
|
|
* which we could use to skip searching part of the string.
|
|
|
|
|
* However, this will likely have worse cache locality
|
|
|
|
|
* as we switch to searching in string to searcing in line vector
|
|
|
|
|
* so perhaps not. *)
|
2024-10-25 15:25:39 +01:00
|
|
|
helpViJ
|
2025-03-22 05:18:25 +00:00
|
|
|
( 0
|
|
|
|
|
, shd
|
|
|
|
|
, absIdx
|
|
|
|
|
, lineColumn
|
|
|
|
|
, preferredColumn
|
|
|
|
|
, hasPassedLine
|
|
|
|
|
, stl
|
|
|
|
|
, ltl
|
|
|
|
|
, prevIsLn
|
2024-10-25 15:25:39 +01:00
|
|
|
)
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => (* empty, so return end of previous string *) absIdx - 1
|
2024-10-25 15:47:39 +01:00
|
|
|
else
|
|
|
|
|
case String.sub (str, strPos) of
|
|
|
|
|
#"\n" =>
|
2024-10-25 21:27:58 +01:00
|
|
|
if hasPassedLine then
|
2024-10-25 15:47:39 +01:00
|
|
|
(* reached end of line twice,
|
2024-11-05 20:25:08 +00:00
|
|
|
* but line has fewer chars than preferredColumn *)
|
|
|
|
|
if prevIsLn then
|
|
|
|
|
(* line break is preceded by linebreak *)
|
|
|
|
|
absIdx
|
2024-10-25 15:47:39 +01:00
|
|
|
else
|
2024-11-05 20:25:08 +00:00
|
|
|
(* line break is preceded by graphical chr
|
|
|
|
|
* so go to graphical chr *)
|
|
|
|
|
absIdx - 1
|
2025-03-22 05:18:25 +00:00
|
|
|
else
|
2024-10-25 15:47:39 +01:00
|
|
|
(* reached end of line once;
|
2024-11-05 20:25:08 +00:00
|
|
|
* continue iterationg *)
|
2025-03-22 05:18:25 +00:00
|
|
|
helpViJ
|
|
|
|
|
( strPos + 1
|
|
|
|
|
, str
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, 0
|
|
|
|
|
, preferredColumn
|
|
|
|
|
, true
|
|
|
|
|
, strTl
|
|
|
|
|
, lineTl
|
|
|
|
|
, true
|
|
|
|
|
)
|
2024-10-25 15:47:39 +01:00
|
|
|
| _ =>
|
|
|
|
|
if lineColumn = preferredColumn andalso hasPassedLine then
|
|
|
|
|
(* we're at the preferredColumn so return absIdx *)
|
|
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
(* we're not in the preferred column, so keep iterating *)
|
|
|
|
|
helpViJ
|
2025-03-22 05:18:25 +00:00
|
|
|
( strPos + 1
|
|
|
|
|
, str
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, lineColumn + 1
|
|
|
|
|
, preferredColumn
|
|
|
|
|
, hasPassedLine
|
|
|
|
|
, strTl
|
|
|
|
|
, lineTl
|
|
|
|
|
, false
|
2024-10-25 15:47:39 +01:00
|
|
|
)
|
2024-10-20 21:30:06 +01:00
|
|
|
|
|
|
|
|
fun viJ (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val
|
|
|
|
|
{rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} =
|
|
|
|
|
lineGap
|
|
|
|
|
in
|
|
|
|
|
case (rightStrings, rightLines) of
|
|
|
|
|
(strHd :: strTl, lnHd :: lnTl) =>
|
|
|
|
|
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 *)
|
2024-11-07 12:59:03 +00:00
|
|
|
let
|
2025-03-22 05:18:25 +00:00
|
|
|
val lineColumn = getCursorColumn
|
|
|
|
|
(strIdx, strHd, lnHd, leftStrings, leftLines, cursorIdx)
|
2024-11-07 12:59:03 +00:00
|
|
|
in
|
|
|
|
|
helpViJ
|
2025-03-22 05:18:25 +00:00
|
|
|
( strIdx
|
|
|
|
|
, strHd
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, lineColumn
|
|
|
|
|
, lineColumn
|
|
|
|
|
, false
|
|
|
|
|
, strTl
|
|
|
|
|
, lnTl
|
|
|
|
|
, false
|
2024-11-07 12:59:03 +00:00
|
|
|
)
|
|
|
|
|
end
|
2024-10-20 21:30:06 +01:00
|
|
|
else
|
|
|
|
|
(* strIdx must be in the strTl *)
|
|
|
|
|
(case (strTl, lnTl) of
|
|
|
|
|
(nestStrHd :: nestStrTl, nestLnHd :: nestLnTl) =>
|
|
|
|
|
let
|
|
|
|
|
val strIdx = strIdx - String.size strHd
|
2024-11-07 12:59:03 +00:00
|
|
|
val leftStrings = strHd :: leftStrings
|
|
|
|
|
val leftLines = lnHd :: leftLines
|
2025-03-22 05:18:25 +00:00
|
|
|
val lineColumn = getCursorColumn
|
|
|
|
|
( strIdx
|
|
|
|
|
, nestStrHd
|
|
|
|
|
, nestLnHd
|
|
|
|
|
, leftStrings
|
|
|
|
|
, leftLines
|
|
|
|
|
, cursorIdx
|
|
|
|
|
)
|
2024-10-20 21:30:06 +01:00
|
|
|
in
|
2024-11-05 20:25:08 +00:00
|
|
|
helpViJ
|
2025-03-22 05:18:25 +00:00
|
|
|
( strIdx
|
|
|
|
|
, nestStrHd
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, lineColumn
|
|
|
|
|
, lineColumn
|
|
|
|
|
, false
|
|
|
|
|
, nestStrTl
|
|
|
|
|
, nestLnTl
|
|
|
|
|
, false
|
2024-11-05 20:25:08 +00:00
|
|
|
)
|
2024-10-20 21:30:06 +01:00
|
|
|
end
|
|
|
|
|
| (_, _) => cursorIdx)
|
|
|
|
|
end
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => (* nowhere to go rightward, so return cursorIdx *) cursorIdx
|
2024-10-20 21:30:06 +01:00
|
|
|
end
|
2024-10-21 01:34:54 +01:00
|
|
|
|
2024-10-25 15:25:39 +01:00
|
|
|
fun helpViK
|
2025-03-22 05:18:25 +00:00
|
|
|
( strPos
|
|
|
|
|
, str
|
|
|
|
|
, absIdx
|
|
|
|
|
, lineColumn
|
|
|
|
|
, preferredColumn
|
|
|
|
|
, hasPassedLine
|
|
|
|
|
, strTl
|
|
|
|
|
, lineHd
|
|
|
|
|
, lineTl
|
2024-10-21 01:34:54 +01:00
|
|
|
) =
|
2024-10-25 15:47:39 +01:00
|
|
|
if strPos < 0 then
|
|
|
|
|
case (strTl, lineTl) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
helpViK
|
2025-03-22 05:18:25 +00:00
|
|
|
( String.size shd - 1
|
|
|
|
|
, shd
|
|
|
|
|
, absIdx
|
|
|
|
|
, lineColumn
|
|
|
|
|
, preferredColumn
|
|
|
|
|
, hasPassedLine
|
|
|
|
|
, stl
|
|
|
|
|
, lhd
|
|
|
|
|
, ltl
|
2024-10-25 15:47:39 +01:00
|
|
|
)
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => (* empty, so return start of previous string *) absIdx + 1
|
2024-10-25 15:47:39 +01:00
|
|
|
else
|
|
|
|
|
case String.sub (str, strPos) of
|
|
|
|
|
#"\n" =>
|
|
|
|
|
if hasPassedLine then
|
2024-11-05 21:42:56 +00:00
|
|
|
(* reached line break twice,
|
2024-10-25 15:47:39 +01:00
|
|
|
* but line has fewer chars than preferredColumn
|
2024-11-05 21:42:56 +00:00
|
|
|
* so go back to chr immediately after this second line break *)
|
2025-03-22 05:18:25 +00:00
|
|
|
absIdx + 1
|
|
|
|
|
else
|
2024-10-25 15:47:39 +01:00
|
|
|
(* reached start of line once;
|
|
|
|
|
* have to check if this is a double linebreak,
|
2025-03-22 05:18:25 +00:00
|
|
|
* and return idx of second linebreak if so *)
|
2024-11-05 21:42:56 +00:00
|
|
|
let
|
|
|
|
|
(* have to calculate column of current line
|
|
|
|
|
* so we know which line to stop searching at *)
|
2025-03-22 05:18:25 +00:00
|
|
|
val lineColumn = getCursorColumn
|
|
|
|
|
(strPos - 1, str, lineHd, strTl, lineTl, absIdx - 1)
|
2024-11-05 21:42:56 +00:00
|
|
|
in
|
|
|
|
|
helpViK
|
2025-03-22 05:18:25 +00:00
|
|
|
( strPos - 1
|
|
|
|
|
, str
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, lineColumn
|
|
|
|
|
, preferredColumn
|
|
|
|
|
, true
|
|
|
|
|
, strTl
|
|
|
|
|
, lineHd
|
|
|
|
|
, lineTl
|
2024-11-05 21:42:56 +00:00
|
|
|
)
|
|
|
|
|
end
|
2024-10-25 15:47:39 +01:00
|
|
|
| _ =>
|
|
|
|
|
if lineColumn <= preferredColumn andalso hasPassedLine then
|
|
|
|
|
(* We're at or before the preferredColumn so return absIdx
|
|
|
|
|
* context: current line may have fewer columns
|
|
|
|
|
* than our preferred column value.
|
|
|
|
|
* If this is the case, we want to check
|
|
|
|
|
* "is lineColumn equal to or before preferredColumn?". *)
|
|
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
(* we're not in the preferred column, so keep iterating *)
|
|
|
|
|
helpViK
|
2025-03-22 05:18:25 +00:00
|
|
|
( strPos - 1
|
|
|
|
|
, str
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, lineColumn - 1
|
|
|
|
|
, preferredColumn
|
|
|
|
|
, hasPassedLine
|
|
|
|
|
, strTl
|
|
|
|
|
, lineHd
|
|
|
|
|
, lineTl
|
2024-10-25 15:47:39 +01:00
|
|
|
)
|
2024-10-21 01:34:54 +01:00
|
|
|
|
2024-11-07 20:51:02 +00:00
|
|
|
fun startViK (strIdx, shd, cursorIdx, leftStrings, lhd, leftLines) =
|
2024-11-05 21:42:56 +00:00
|
|
|
if String.sub (shd, strIdx) = #"\n" then
|
2024-11-07 20:51:02 +00:00
|
|
|
(* ? -> ? -> \n *)
|
2024-11-06 11:52:40 +00:00
|
|
|
if strIdx > 0 then
|
|
|
|
|
(* strIdx - 1 is in shd *)
|
|
|
|
|
if String.sub (shd, strIdx - 1) = #"\n" then
|
2024-11-07 20:51:02 +00:00
|
|
|
(* ? -> \n -> \n *)
|
|
|
|
|
if strIdx > 1 then
|
|
|
|
|
(* strIdx - 2 is in shd *)
|
|
|
|
|
if String.sub (shd, strIdx - 2) = #"\n" then
|
|
|
|
|
(* \n -> \n -> \n
|
|
|
|
|
* so it is safe to decrement cursorIdx by 1 *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
else
|
|
|
|
|
(* graphical-chr -> \n -> \n
|
|
|
|
|
* so go to beginning of line,
|
|
|
|
|
* starting from graphical-chr *)
|
2024-11-26 21:57:02 +00:00
|
|
|
startVi0
|
|
|
|
|
(strIdx - 2, shd, lhd, cursorIdx - 2, leftStrings, leftLines)
|
2024-11-07 20:51:02 +00:00
|
|
|
else
|
|
|
|
|
(* strIdx - 2 is in leftStrings *)
|
|
|
|
|
case (leftStrings, leftLines) of
|
|
|
|
|
(lshd :: lstl, llhd :: lltl) =>
|
|
|
|
|
if String.sub (lshd, String.size lshd - 1) = #"\n" then
|
2025-03-22 05:18:25 +00:00
|
|
|
(* \n -> \n -> \n
|
|
|
|
|
* so it is safe to decrement cursorIdx by 1 *)
|
|
|
|
|
cursorIdx - 1
|
2024-11-07 20:51:02 +00:00
|
|
|
else
|
2025-03-22 05:18:25 +00:00
|
|
|
(* graphical-chr -> \n -> \n
|
|
|
|
|
* so go to beginning of line,
|
|
|
|
|
* starting from graphical-chr *)
|
|
|
|
|
startVi0
|
|
|
|
|
( String.size lshd - 1
|
|
|
|
|
, lshd
|
|
|
|
|
, llhd
|
|
|
|
|
, cursorIdx - 2
|
|
|
|
|
, lstl
|
|
|
|
|
, lltl
|
|
|
|
|
)
|
2024-11-07 20:51:02 +00:00
|
|
|
| (_, _) =>
|
|
|
|
|
(* nothing to the left, so we are at start of buffer *)
|
|
|
|
|
0
|
2024-11-06 11:52:40 +00:00
|
|
|
else
|
2024-11-07 20:51:02 +00:00
|
|
|
(* ? -> graphical-chr -> \n
|
|
|
|
|
* Don't expect this case to happen
|
|
|
|
|
* but if it does, go to start of line. *)
|
2025-03-22 05:18:25 +00:00
|
|
|
startVi0 (strIdx - 1, shd, lhd, cursorIdx - 1, leftStrings, leftLines)
|
2024-11-07 20:51:02 +00:00
|
|
|
else
|
2024-11-06 11:52:40 +00:00
|
|
|
(* strIdx - 1 is in leftStrings *)
|
2024-11-05 21:42:56 +00:00
|
|
|
case (leftStrings, leftLines) of
|
|
|
|
|
(lshd :: lstl, llhd :: lltl) =>
|
2024-11-06 11:52:40 +00:00
|
|
|
if String.sub (lshd, String.size lshd - 1) = #"\n" then
|
2024-11-07 20:51:02 +00:00
|
|
|
(* ? -> \n -> \n *)
|
|
|
|
|
if String.size lshd > 1 then
|
|
|
|
|
(* cursorIdx - 2 is in this string *)
|
|
|
|
|
if String.sub (lshd, String.size lshd - 2) = #"\n" then
|
|
|
|
|
(* \n -> \n -> \n *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
else
|
|
|
|
|
(* graphical-chr -> \n -> \n *)
|
2024-11-26 21:57:02 +00:00
|
|
|
startVi0
|
2025-03-22 05:18:25 +00:00
|
|
|
( String.size lshd - 2
|
|
|
|
|
, lshd
|
|
|
|
|
, llhd
|
|
|
|
|
, cursorIdx - 2
|
|
|
|
|
, lstl
|
|
|
|
|
, lltl
|
|
|
|
|
)
|
2024-11-07 20:51:02 +00:00
|
|
|
else
|
|
|
|
|
(* cursorIdx - 2 is in lstl *)
|
|
|
|
|
(case (lstl, lltl) of
|
2025-03-22 05:18:25 +00:00
|
|
|
(stlhd :: stltl, ltlhd :: lltl) =>
|
|
|
|
|
if String.sub (stlhd, String.size stlhd - 1) = #"\n" then
|
|
|
|
|
(* \n -> \n -> \n *)
|
|
|
|
|
cursorIdx - 1
|
|
|
|
|
else
|
|
|
|
|
(* graphical-chr -> \n -> \n *)
|
|
|
|
|
startVi0
|
|
|
|
|
( String.size stlhd - 1
|
|
|
|
|
, stlhd
|
|
|
|
|
, ltlhd
|
|
|
|
|
, cursorIdx - 2
|
|
|
|
|
, lstl
|
|
|
|
|
, lltl
|
|
|
|
|
)
|
|
|
|
|
| (_, _) => 0)
|
2024-11-06 11:52:40 +00:00
|
|
|
else
|
2024-11-07 20:51:02 +00:00
|
|
|
(* ? -> graphical-chr -> \n *)
|
2024-11-26 21:57:02 +00:00
|
|
|
startVi0
|
2025-03-22 05:18:25 +00:00
|
|
|
( String.size lshd - 1
|
|
|
|
|
, lshd
|
|
|
|
|
, llhd
|
|
|
|
|
, cursorIdx - 1
|
|
|
|
|
, leftStrings
|
|
|
|
|
, leftLines
|
|
|
|
|
)
|
|
|
|
|
| (_, _) => (* leftStrings is empty so go to start of buffer *) 0
|
2024-11-05 21:42:56 +00:00
|
|
|
else
|
2024-11-07 20:51:02 +00:00
|
|
|
(* ? -> ? -> graphical-chr
|
|
|
|
|
* Normal case where we call startViK. *)
|
2024-11-05 21:42:56 +00:00
|
|
|
let
|
2025-03-22 05:18:25 +00:00
|
|
|
val lineColumn = getCursorColumn
|
|
|
|
|
(strIdx, shd, lhd, leftStrings, leftLines, cursorIdx)
|
2024-11-05 21:42:56 +00:00
|
|
|
in
|
|
|
|
|
helpViK
|
2025-03-22 05:18:25 +00:00
|
|
|
( strIdx
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, lineColumn
|
|
|
|
|
, lineColumn
|
|
|
|
|
, false
|
|
|
|
|
, leftStrings
|
|
|
|
|
, lhd
|
|
|
|
|
, leftLines
|
2024-11-05 21:42:56 +00:00
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-21 01:34:54 +01:00
|
|
|
fun viK (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val
|
|
|
|
|
{rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} =
|
|
|
|
|
lineGap
|
|
|
|
|
in
|
|
|
|
|
case (rightStrings, rightLines) of
|
2024-11-07 20:51:02 +00:00
|
|
|
(shd :: stl, lhd :: ltl) =>
|
2024-10-21 01:34:54 +01:00
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
2024-11-07 20:51:02 +00:00
|
|
|
if strIdx < String.size shd then
|
2025-03-22 05:18:25 +00:00
|
|
|
startViK (strIdx, shd, cursorIdx, leftStrings, lhd, leftLines)
|
2024-11-07 20:51:02 +00:00
|
|
|
else
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
|
|
|
|
let
|
|
|
|
|
val strIdx = strIdx - String.size shd
|
|
|
|
|
val leftStrings = shd :: leftStrings
|
|
|
|
|
val leftLines = lhd :: leftLines
|
|
|
|
|
in
|
|
|
|
|
startViK
|
|
|
|
|
(strIdx, stlhd, cursorIdx, leftStrings, ltlhd, leftLines)
|
|
|
|
|
end
|
|
|
|
|
| (_, _) => cursorIdx
|
2024-10-21 01:34:54 +01:00
|
|
|
end
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => (* nowhere to go rightward, so return cursorIdx *) cursorIdx
|
2024-10-21 01:34:54 +01:00
|
|
|
end
|
2024-10-23 04:31:34 +01:00
|
|
|
|
2024-11-05 12:38:01 +00:00
|
|
|
(* equivalent of vi's 'w' command *)
|
2025-07-20 16:04:48 +01:00
|
|
|
val nextWord = ViWordDfa.startOfNextWord
|
2024-10-25 21:27:58 +01:00
|
|
|
|
2024-11-05 12:38:01 +00:00
|
|
|
(* equivalent of vi's 'W' command *)
|
2025-07-20 09:56:32 +01:00
|
|
|
val nextWORD = ViWORDDfa.startOfNextWORD
|
2024-10-25 21:27:58 +01:00
|
|
|
|
|
|
|
|
(* equivalent of vi's 'b' command *)
|
2025-07-20 17:36:33 +01:00
|
|
|
val prevWord = ViWordDfa.startOfCurrentWord
|
2024-10-25 21:27:58 +01:00
|
|
|
|
|
|
|
|
(* equivalent of vi's 'B' command *)
|
2025-07-20 13:09:21 +01:00
|
|
|
val prevWORD = ViWORDDfa.startOfCurrentWORD
|
2024-10-25 21:27:58 +01:00
|
|
|
|
2024-10-28 06:52:58 +00:00
|
|
|
(* equivalent of vi's 'ge' command *)
|
2025-07-20 16:55:42 +01:00
|
|
|
val endOfPrevWord = ViWordDfa.endOfPrevWord
|
2024-10-28 06:52:58 +00:00
|
|
|
|
|
|
|
|
(* equivalent of vi's 'gE' command *)
|
2025-07-20 09:56:32 +01:00
|
|
|
val endOfPrevWORD = ViWORDDfa.endOfPrevWORD
|
2024-10-28 06:52:58 +00:00
|
|
|
|
2024-10-25 21:27:58 +01:00
|
|
|
(* equivalent of vi's `e` command *)
|
2025-07-20 17:48:45 +01:00
|
|
|
val endOfWord = ViWordDfa.endOfCurrentWord
|
2025-08-03 17:35:33 +01:00
|
|
|
val endOfWordForDelete = ViWordDfa.endOfCurrentWordForDelete
|
2024-10-25 21:27:58 +01:00
|
|
|
|
|
|
|
|
(* equivalent of vi's `E` command *)
|
2025-07-20 14:00:59 +01:00
|
|
|
val endOfWORD = ViWORDDfa.endOfCurrentWORD
|
2025-08-03 17:35:33 +01:00
|
|
|
val endOfWORDForDelete = ViWORDDfa.endOfCurrentWORDForDelete
|
2024-10-25 21:44:46 +01:00
|
|
|
|
2024-10-26 21:21:33 +01:00
|
|
|
fun helpFirstNonSpaceChr (strPos, str, absIdx, stl, ltl) =
|
2025-03-22 05:18:25 +00:00
|
|
|
if strPos = String.size str then
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
helpFirstNonSpaceChr (0, shd, absIdx, stl, ltl)
|
|
|
|
|
| (_, _) => absIdx - 1
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, strPos)
|
|
|
|
|
in
|
|
|
|
|
if chr = #" " then
|
|
|
|
|
helpFirstNonSpaceChr (strPos + 1, str, absIdx + 1, stl, ltl)
|
|
|
|
|
else
|
|
|
|
|
absIdx
|
|
|
|
|
end
|
2024-10-25 21:44:46 +01:00
|
|
|
|
2024-10-26 21:21:33 +01:00
|
|
|
fun startFirstNonSpaceChr (shd, strIdx, absIdx, stl, ltl) =
|
2025-03-22 05:18:25 +00:00
|
|
|
if strIdx < String.size shd then
|
|
|
|
|
helpFirstNonSpaceChr (strIdx, shd, absIdx, stl, ltl)
|
2024-10-26 21:21:33 +01:00
|
|
|
else
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpFirstNonSpaceChr (0, stlhd, absIdx, stltl, ltltl)
|
|
|
|
|
| (_, _) => (* tl is empty; just return absIdx *) absIdx
|
2024-10-25 21:44:46 +01:00
|
|
|
|
|
|
|
|
(* Prerequisite:
|
|
|
|
|
* LineGap has been moved to start of line (provided with vi0). *)
|
|
|
|
|
fun firstNonSpaceChr (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, rightLines, idx = bufferIdx, ...} = lineGap
|
|
|
|
|
in
|
|
|
|
|
case (rightStrings, rightLines) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
if strIdx < String.size shd then
|
|
|
|
|
(* strIdx is in this string *)
|
2025-03-22 05:18:25 +00:00
|
|
|
startFirstNonSpaceChr (shd, strIdx, cursorIdx, stl, ltl)
|
2024-10-25 21:44:46 +01:00
|
|
|
else
|
|
|
|
|
(* strIdx is in tl *)
|
|
|
|
|
(case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
2025-03-22 05:18:25 +00:00
|
|
|
let
|
2024-10-25 21:44:46 +01:00
|
|
|
val strIdx = strIdx - String.size shd
|
2025-03-22 05:18:25 +00:00
|
|
|
in
|
|
|
|
|
startFirstNonSpaceChr
|
2024-10-25 21:44:46 +01:00
|
|
|
(stlhd, strIdx, cursorIdx, stltl, ltltl)
|
|
|
|
|
end
|
|
|
|
|
| (_, _) => cursorIdx)
|
|
|
|
|
end
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => cursorIdx
|
2024-10-25 21:44:46 +01:00
|
|
|
end
|
2024-10-26 10:47:45 +01:00
|
|
|
|
2024-10-26 16:20:24 +01:00
|
|
|
fun helpToNextChr (strPos, str, absIdx, stl, ltl, origIdx, findChr) =
|
2024-10-26 10:47:45 +01:00
|
|
|
if strPos = String.size str then
|
|
|
|
|
case (stl, ltl) of
|
2025-03-22 05:18:25 +00:00
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
helpToNextChr (0, shd, absIdx, stl, ltl, origIdx, findChr)
|
|
|
|
|
| (_, _) => origIdx
|
|
|
|
|
else if String.sub (str, strPos) = findChr then
|
|
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
helpToNextChr (strPos + 1, str, absIdx + 1, stl, ltl, origIdx, findChr)
|
2024-10-26 10:47:45 +01:00
|
|
|
|
2024-10-26 16:20:24 +01:00
|
|
|
fun startToNextChr (shd, strIdx, absIdx, stl, ltl, findChr) =
|
2024-10-26 10:47:45 +01:00
|
|
|
(* we want to start iterating from next char after strIdx *)
|
|
|
|
|
if strIdx - 1 < String.size shd then
|
2025-03-22 05:18:25 +00:00
|
|
|
helpToNextChr (strIdx + 1, shd, absIdx + 1, stl, ltl, absIdx, findChr)
|
2024-10-26 10:47:45 +01:00
|
|
|
else
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpToNextChr (0, stlhd, absIdx + 1, stltl, ltltl, absIdx, findChr)
|
|
|
|
|
| (_, _) => (* tl is empty; just return absIdx *) absIdx
|
2024-10-26 10:47:45 +01:00
|
|
|
|
2025-03-22 05:18:25 +00:00
|
|
|
fun helpTillNextChr
|
|
|
|
|
(strPos, str, absIdx, stl, ltl, origIdx, findChr, lastNonLine, lastLine) =
|
|
|
|
|
if strPos = String.size str then
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
helpTillNextChr
|
|
|
|
|
(0, shd, absIdx, stl, ltl, origIdx, findChr, lastNonLine, lastLine)
|
|
|
|
|
| (_, _) => origIdx
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, strPos)
|
|
|
|
|
in
|
|
|
|
|
if chr = findChr then
|
|
|
|
|
if lastLine = lastNonLine + 1 then
|
|
|
|
|
(* graphical-chr -> \n
|
|
|
|
|
* so return graphical-chr *)
|
|
|
|
|
lastNonLine
|
2024-11-09 05:35:32 +00:00
|
|
|
else
|
2025-03-22 05:18:25 +00:00
|
|
|
Int.max (lastLine, lastNonLine)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val lastLine = if chr = #"\n" then absIdx else lastLine
|
|
|
|
|
val lastNonLine = if chr = #"\n" then lastNonLine else absIdx
|
|
|
|
|
in
|
|
|
|
|
helpTillNextChr
|
|
|
|
|
( strPos + 1
|
|
|
|
|
, str
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, stl
|
|
|
|
|
, ltl
|
|
|
|
|
, origIdx
|
|
|
|
|
, findChr
|
|
|
|
|
, lastNonLine
|
|
|
|
|
, lastLine
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-10-26 10:47:45 +01:00
|
|
|
|
2024-10-26 16:20:24 +01:00
|
|
|
fun startTillNextChr (shd, strIdx, absIdx, stl, ltl, findChr) =
|
|
|
|
|
(* we want to start iterating from next char after strIdx *)
|
2024-11-09 05:35:32 +00:00
|
|
|
if strIdx + 1 < String.size shd then
|
2025-03-22 05:18:25 +00:00
|
|
|
helpTillNextChr
|
2024-11-09 05:35:32 +00:00
|
|
|
(strIdx + 1, shd, absIdx + 1, stl, ltl, absIdx, findChr, absIdx, absIdx)
|
2024-10-26 16:20:24 +01:00
|
|
|
else
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
|
|
|
|
helpTillNextChr
|
2025-03-22 05:18:25 +00:00
|
|
|
( 0
|
|
|
|
|
, stlhd
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, stltl
|
|
|
|
|
, ltltl
|
|
|
|
|
, absIdx
|
|
|
|
|
, findChr
|
|
|
|
|
, absIdx
|
|
|
|
|
, absIdx
|
|
|
|
|
)
|
|
|
|
|
| (_, _) => (* tl is empty; just return absIdx *) absIdx
|
2024-10-26 10:47:45 +01:00
|
|
|
|
2024-10-26 22:11:52 +01:00
|
|
|
fun nextChr (lineGap: LineGap.t, cursorIdx, chr, fStart) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, rightLines, idx = bufferIdx, ...} = lineGap
|
|
|
|
|
in
|
|
|
|
|
case (rightStrings, rightLines) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
if strIdx < String.size shd then
|
|
|
|
|
(* strIdx is in this string *)
|
2025-03-22 05:18:25 +00:00
|
|
|
fStart (shd, strIdx, cursorIdx, stl, ltl, chr)
|
2024-10-26 22:11:52 +01:00
|
|
|
else
|
|
|
|
|
(* strIdx is in tl *)
|
|
|
|
|
(case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
2025-03-22 05:18:25 +00:00
|
|
|
let val strIdx = strIdx - String.size shd
|
|
|
|
|
in fStart (stlhd, strIdx, cursorIdx, stltl, ltltl, chr)
|
2024-10-26 22:11:52 +01:00
|
|
|
end
|
|
|
|
|
| (_, _) => cursorIdx)
|
|
|
|
|
end
|
|
|
|
|
| (_, _) => cursorIdx
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-26 21:56:11 +01:00
|
|
|
fun tillNextChr (lineGap, cursorIdx, chr) =
|
|
|
|
|
nextChr (lineGap, cursorIdx, chr, startTillNextChr)
|
|
|
|
|
|
|
|
|
|
fun toNextChr (lineGap, cursorIdx, chr) =
|
|
|
|
|
nextChr (lineGap, cursorIdx, chr, startToNextChr)
|
2024-10-26 21:21:33 +01:00
|
|
|
|
|
|
|
|
fun helpToPrevChr (strPos, str, absIdx, stl, ltl, origIdx, findChr) =
|
2024-10-26 21:56:11 +01:00
|
|
|
if strPos < 0 then
|
2024-10-26 21:21:33 +01:00
|
|
|
case (stl, ltl) of
|
2025-03-22 05:18:25 +00:00
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
helpToPrevChr
|
2024-10-26 21:21:33 +01:00
|
|
|
(String.size shd - 1, shd, absIdx, stl, ltl, origIdx, findChr)
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => origIdx
|
|
|
|
|
else if String.sub (str, strPos) = findChr then
|
|
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
helpToPrevChr (strPos - 1, str, absIdx - 1, stl, ltl, origIdx, findChr)
|
2024-10-26 21:21:33 +01:00
|
|
|
|
|
|
|
|
fun startToPrevChr (shd, strIdx, absIdx, stl, ltl, findChr) =
|
|
|
|
|
(* we want to start iterating from Prev char after strIdx *)
|
|
|
|
|
if strIdx > 0 then
|
2025-03-22 05:18:25 +00:00
|
|
|
helpToPrevChr (strIdx - 1, shd, absIdx - 1, stl, ltl, absIdx, findChr)
|
2024-10-26 21:21:33 +01:00
|
|
|
else
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
|
|
|
|
helpToPrevChr
|
2025-03-22 05:18:25 +00:00
|
|
|
( String.size stlhd - 1
|
|
|
|
|
, stlhd
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, stltl
|
|
|
|
|
, ltltl
|
|
|
|
|
, absIdx
|
|
|
|
|
, findChr
|
|
|
|
|
)
|
|
|
|
|
| (_, _) => (* tl is empty; return 0 for lineGap start *) 0
|
|
|
|
|
|
|
|
|
|
fun helpTillPrevChr
|
|
|
|
|
( strPos
|
|
|
|
|
, str
|
|
|
|
|
, absIdx
|
|
|
|
|
, stl
|
|
|
|
|
, ltl
|
|
|
|
|
, origIdx
|
|
|
|
|
, findChr
|
|
|
|
|
, lastNonLine
|
|
|
|
|
, lastLine
|
|
|
|
|
, lastValid
|
2024-11-09 06:33:32 +00:00
|
|
|
) =
|
2025-03-22 05:18:25 +00:00
|
|
|
if strPos < 0 then
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
helpTillPrevChr
|
|
|
|
|
( String.size shd - 1
|
|
|
|
|
, shd
|
|
|
|
|
, absIdx
|
|
|
|
|
, stl
|
|
|
|
|
, ltl
|
|
|
|
|
, origIdx
|
|
|
|
|
, findChr
|
|
|
|
|
, lastNonLine
|
|
|
|
|
, lastLine
|
|
|
|
|
, lastValid
|
|
|
|
|
)
|
|
|
|
|
| (_, _) => origIdx
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, strPos)
|
|
|
|
|
in
|
|
|
|
|
if chr = findChr then
|
|
|
|
|
if lastLine = lastNonLine then lastNonLine
|
|
|
|
|
else if absIdx + 1 = lastLine then lastValid + 1
|
|
|
|
|
else Int.min (lastLine, lastNonLine)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val lastLine = if chr = #"\n" then absIdx else lastLine
|
|
|
|
|
val lastNonLine = if chr = #"\n" then lastNonLine else absIdx
|
|
|
|
|
|
|
|
|
|
(* There is a slightly tricky edge case
|
|
|
|
|
* which is the reason the lastValid variable.
|
|
|
|
|
* Say we have a string "a\n\n\nbcd"
|
|
|
|
|
* and we type "Ta" with the cursor at the end.
|
|
|
|
|
* We want the cursor to go to the second line break
|
|
|
|
|
* because (graphical-chr -> \n) should not be selectable.
|
|
|
|
|
* However, with only lastLine and lastNonLine variables,
|
|
|
|
|
* we only have information about the most recent \n
|
|
|
|
|
* and the most recent graphical-chr.
|
|
|
|
|
* This means we don't have information about the case
|
|
|
|
|
* where a graphical-chr is followed by multiple '\n's.
|
|
|
|
|
* The lastValid variable keeps track of this information
|
|
|
|
|
* so we can use it to provide the expected behaviour.
|
|
|
|
|
* *)
|
|
|
|
|
val lastValid =
|
|
|
|
|
if lastLine = lastNonLine + 1 then lastLine + 1
|
|
|
|
|
else Int.min (lastLine, lastNonLine)
|
|
|
|
|
in
|
|
|
|
|
helpTillPrevChr
|
|
|
|
|
( strPos - 1
|
|
|
|
|
, str
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, stl
|
|
|
|
|
, ltl
|
|
|
|
|
, origIdx
|
|
|
|
|
, findChr
|
|
|
|
|
, lastNonLine
|
|
|
|
|
, lastLine
|
|
|
|
|
, lastValid
|
2024-11-09 06:33:32 +00:00
|
|
|
)
|
2025-03-22 05:18:25 +00:00
|
|
|
end
|
|
|
|
|
end
|
2024-10-26 22:11:52 +01:00
|
|
|
|
|
|
|
|
fun startTillPrevChr (shd, strIdx, absIdx, stl, ltl, findChr) =
|
|
|
|
|
(* we want to start iterating from Prev char after strIdx *)
|
|
|
|
|
if strIdx > 0 then
|
2025-03-22 05:18:25 +00:00
|
|
|
helpTillPrevChr
|
|
|
|
|
( strIdx - 1
|
|
|
|
|
, shd
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, stl
|
|
|
|
|
, ltl
|
|
|
|
|
, absIdx
|
|
|
|
|
, findChr
|
|
|
|
|
, absIdx
|
|
|
|
|
, absIdx
|
|
|
|
|
, absIdx
|
2024-11-09 06:33:32 +00:00
|
|
|
)
|
2024-10-26 22:11:52 +01:00
|
|
|
else
|
|
|
|
|
case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
|
|
|
|
helpTillPrevChr
|
2025-03-22 05:18:25 +00:00
|
|
|
( String.size stlhd - 1
|
|
|
|
|
, stlhd
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, stltl
|
|
|
|
|
, ltltl
|
|
|
|
|
, absIdx
|
|
|
|
|
, findChr
|
|
|
|
|
, absIdx
|
|
|
|
|
, absIdx
|
|
|
|
|
, absIdx
|
2024-10-26 22:11:52 +01:00
|
|
|
)
|
2025-03-22 05:18:25 +00:00
|
|
|
| (_, _) => (* tl is empty; return 0 for lineGap start *) 0
|
2024-10-26 22:11:52 +01:00
|
|
|
|
|
|
|
|
fun prevChr (lineGap: LineGap.t, cursorIdx, chr, fStart) =
|
2024-10-26 21:21:33 +01:00
|
|
|
let
|
2025-03-22 05:18:25 +00:00
|
|
|
val
|
|
|
|
|
{rightStrings, rightLines, idx = bufferIdx, leftStrings, leftLines, ...} =
|
|
|
|
|
lineGap
|
2024-10-26 21:21:33 +01:00
|
|
|
in
|
|
|
|
|
case (rightStrings, rightLines) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
if strIdx < String.size shd then
|
|
|
|
|
(* strIdx is in this string *)
|
2025-03-22 05:18:25 +00:00
|
|
|
fStart (shd, strIdx, cursorIdx, leftStrings, leftLines, chr)
|
2024-10-26 21:21:33 +01:00
|
|
|
else
|
|
|
|
|
(* strIdx is in tl *)
|
|
|
|
|
(case (stl, ltl) of
|
|
|
|
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
2025-03-22 05:18:25 +00:00
|
|
|
let
|
2024-10-26 21:21:33 +01:00
|
|
|
val strIdx = strIdx - String.size shd
|
|
|
|
|
val leftStrings = shd :: leftStrings
|
|
|
|
|
val leftLines = lhd :: leftLines
|
2025-03-22 05:18:25 +00:00
|
|
|
in
|
2024-10-26 22:11:52 +01:00
|
|
|
fStart
|
2024-10-26 21:21:33 +01:00
|
|
|
(shd, strIdx, cursorIdx, leftStrings, leftLines, chr)
|
2024-10-26 16:20:24 +01:00
|
|
|
end
|
|
|
|
|
| (_, _) => cursorIdx)
|
|
|
|
|
end
|
|
|
|
|
| (_, _) => cursorIdx
|
|
|
|
|
end
|
2024-10-26 22:11:52 +01:00
|
|
|
|
|
|
|
|
fun toPrevChr (lineGap, cursorIdx, chr) =
|
|
|
|
|
prevChr (lineGap, cursorIdx, chr, startToPrevChr)
|
|
|
|
|
|
|
|
|
|
fun tillPrevChr (lineGap, cursorIdx, chr) =
|
|
|
|
|
prevChr (lineGap, cursorIdx, chr, startTillPrevChr)
|
2024-10-31 11:07:52 +00:00
|
|
|
|
2025-03-22 05:18:25 +00:00
|
|
|
fun helpMatchPairNext
|
|
|
|
|
(strPos, str, absIdx, stl, origIdx, openChr, openNum, closeChr, closeNum) =
|
|
|
|
|
if strPos = String.size str then
|
|
|
|
|
case stl of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
helpMatchPairNext
|
|
|
|
|
(0, hd, absIdx, tl, origIdx, openChr, openNum, closeChr, closeNum)
|
|
|
|
|
| [] => origIdx
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, strPos)
|
|
|
|
|
val openNum = if chr = openChr then openNum + 1 else openNum
|
|
|
|
|
val closeNum = if chr = closeChr then closeNum + 1 else closeNum
|
|
|
|
|
in
|
|
|
|
|
if openNum = closeNum then
|
|
|
|
|
absIdx
|
|
|
|
|
else
|
|
|
|
|
helpMatchPairNext
|
|
|
|
|
( strPos + 1
|
|
|
|
|
, str
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, stl
|
|
|
|
|
, origIdx
|
|
|
|
|
, openChr
|
|
|
|
|
, openNum
|
|
|
|
|
, closeChr
|
|
|
|
|
, closeNum
|
|
|
|
|
)
|
|
|
|
|
end
|
2024-10-31 11:07:52 +00:00
|
|
|
|
2025-03-22 05:18:25 +00:00
|
|
|
fun helpMatchPairPrev
|
|
|
|
|
(strPos, str, absIdx, stl, origIdx, openChr, openNum, closeChr, closeNum) =
|
|
|
|
|
if strPos < 0 then
|
|
|
|
|
case stl of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
helpMatchPairPrev
|
|
|
|
|
( String.size hd - 1
|
|
|
|
|
, hd
|
|
|
|
|
, absIdx
|
|
|
|
|
, tl
|
|
|
|
|
, origIdx
|
|
|
|
|
, openChr
|
|
|
|
|
, openNum
|
|
|
|
|
, closeChr
|
|
|
|
|
, closeNum
|
|
|
|
|
)
|
|
|
|
|
| [] => origIdx
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, strPos)
|
|
|
|
|
val openNum = if chr = openChr then openNum + 1 else openNum
|
|
|
|
|
val closeNum = if chr = closeChr then closeNum + 1 else closeNum
|
|
|
|
|
in
|
|
|
|
|
if openNum = closeNum then
|
|
|
|
|
absIdx
|
2024-10-31 11:07:52 +00:00
|
|
|
else
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairPrev
|
|
|
|
|
( strPos - 1
|
|
|
|
|
, str
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, stl
|
|
|
|
|
, origIdx
|
|
|
|
|
, openChr
|
|
|
|
|
, openNum
|
|
|
|
|
, closeChr
|
|
|
|
|
, closeNum
|
|
|
|
|
)
|
|
|
|
|
end
|
2024-10-31 11:07:52 +00:00
|
|
|
|
2024-10-31 11:50:18 +00:00
|
|
|
fun startMatchPair (strIdx, shd, leftStrings, rightStrings, cursorIdx) =
|
|
|
|
|
case String.sub (shd, strIdx) of
|
|
|
|
|
#"(" =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairNext
|
|
|
|
|
( strIdx + 1
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx + 1
|
|
|
|
|
, rightStrings
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, #"("
|
|
|
|
|
, 1
|
|
|
|
|
, #")"
|
|
|
|
|
, 0
|
|
|
|
|
)
|
2024-10-31 11:50:18 +00:00
|
|
|
| #")" =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairPrev
|
|
|
|
|
( strIdx - 1
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx - 1
|
|
|
|
|
, leftStrings
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, #"("
|
|
|
|
|
, 0
|
|
|
|
|
, #")"
|
|
|
|
|
, 1
|
|
|
|
|
)
|
2024-10-31 11:50:18 +00:00
|
|
|
| #"[" =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairNext
|
|
|
|
|
( strIdx + 1
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx + 1
|
|
|
|
|
, rightStrings
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, #"["
|
|
|
|
|
, 1
|
|
|
|
|
, #"]"
|
|
|
|
|
, 0
|
|
|
|
|
)
|
2024-10-31 11:50:18 +00:00
|
|
|
| #"]" =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairPrev
|
|
|
|
|
( strIdx - 1
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx - 1
|
|
|
|
|
, leftStrings
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, #"["
|
|
|
|
|
, 0
|
|
|
|
|
, #"]"
|
|
|
|
|
, 1
|
|
|
|
|
)
|
2024-10-31 11:50:18 +00:00
|
|
|
| #"{" =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairNext
|
|
|
|
|
( strIdx + 1
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx + 1
|
|
|
|
|
, rightStrings
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, #"{"
|
|
|
|
|
, 1
|
|
|
|
|
, #"}"
|
|
|
|
|
, 0
|
|
|
|
|
)
|
2024-10-31 11:50:18 +00:00
|
|
|
| #"}" =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairPrev
|
|
|
|
|
( strIdx - 1
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx - 1
|
|
|
|
|
, leftStrings
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, #"{"
|
|
|
|
|
, 0
|
|
|
|
|
, #"}"
|
|
|
|
|
, 1
|
|
|
|
|
)
|
2024-10-31 11:50:18 +00:00
|
|
|
| #"<" =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairNext
|
|
|
|
|
( strIdx + 1
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx + 1
|
|
|
|
|
, rightStrings
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, #"<"
|
|
|
|
|
, 1
|
|
|
|
|
, #">"
|
|
|
|
|
, 0
|
|
|
|
|
)
|
2024-10-31 11:50:18 +00:00
|
|
|
| #">" =>
|
2025-03-22 05:18:25 +00:00
|
|
|
helpMatchPairPrev
|
|
|
|
|
( strIdx - 1
|
|
|
|
|
, shd
|
|
|
|
|
, cursorIdx - 1
|
|
|
|
|
, leftStrings
|
|
|
|
|
, cursorIdx
|
|
|
|
|
, #"<"
|
|
|
|
|
, 0
|
|
|
|
|
, #">"
|
|
|
|
|
, 1
|
|
|
|
|
)
|
2024-10-31 11:50:18 +00:00
|
|
|
| _ => cursorIdx
|
2024-10-31 11:19:10 +00:00
|
|
|
|
2024-10-31 11:07:52 +00:00
|
|
|
fun matchPair (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, idx = bufferIdx, leftStrings, ...} = lineGap
|
|
|
|
|
in
|
|
|
|
|
case rightStrings of
|
|
|
|
|
shd :: stl =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
if strIdx < String.size shd then
|
|
|
|
|
(* strIdx is in this string *)
|
2024-10-31 11:50:18 +00:00
|
|
|
startMatchPair (strIdx, shd, leftStrings, stl, cursorIdx)
|
2024-10-31 11:07:52 +00:00
|
|
|
else
|
|
|
|
|
(* strIdx is in tl *)
|
|
|
|
|
(case stl of
|
|
|
|
|
stlhd :: stltl =>
|
2025-03-22 05:18:25 +00:00
|
|
|
let
|
2024-10-31 11:07:52 +00:00
|
|
|
val strIdx = strIdx - String.size shd
|
|
|
|
|
val leftStrings = shd :: leftStrings
|
2025-03-22 05:18:25 +00:00
|
|
|
in
|
|
|
|
|
startMatchPair
|
|
|
|
|
(strIdx, stlhd, leftStrings, stltl, cursorIdx)
|
2024-10-31 11:07:52 +00:00
|
|
|
end
|
|
|
|
|
| [] => cursorIdx)
|
|
|
|
|
end
|
|
|
|
|
| [] => cursorIdx
|
|
|
|
|
end
|
2024-11-02 13:59:55 +00:00
|
|
|
|
|
|
|
|
(* Prerequisite: move lineGap to reqLine *)
|
|
|
|
|
fun getLineStartIdx (lineGap: LineGap.t, reqLine) =
|
|
|
|
|
let
|
2025-03-22 05:18:25 +00:00
|
|
|
val {rightStrings, rightLines, line = bufferLine, idx = bufferIdx, ...} =
|
|
|
|
|
lineGap
|
2024-11-02 13:59:55 +00:00
|
|
|
in
|
2024-11-04 02:46:02 +00:00
|
|
|
case (rightStrings, rightLines) of
|
|
|
|
|
(shd :: stl, lhd :: ltl) =>
|
2024-11-02 13:59:55 +00:00
|
|
|
(* reqLine exists in lineGap, so retrieve it *)
|
|
|
|
|
let
|
|
|
|
|
val relativeLine = reqLine - bufferLine - 1
|
2024-11-04 02:46:02 +00:00
|
|
|
val lineIdx = Vector.sub (lhd, relativeLine)
|
2024-11-02 13:59:55 +00:00
|
|
|
in
|
2024-11-04 02:46:02 +00:00
|
|
|
if lineIdx = String.size shd - 1 andalso List.null stl then
|
2024-11-08 20:43:36 +00:00
|
|
|
(* if is end of buffer, return last idx in buffer; else,
|
2024-11-04 02:46:02 +00:00
|
|
|
* increment by 1 as we want to go to first char after line break *)
|
|
|
|
|
bufferIdx + lineIdx
|
|
|
|
|
else
|
|
|
|
|
bufferIdx + lineIdx + 1
|
2024-11-02 13:59:55 +00:00
|
|
|
end
|
2024-11-04 02:46:02 +00:00
|
|
|
| (_, _) =>
|
2024-11-02 13:59:55 +00:00
|
|
|
(* reqLine does not exist in lineGap, so just go to start of last line *)
|
|
|
|
|
let
|
2024-11-04 02:46:02 +00:00
|
|
|
val {leftStrings, leftLines, ...} = lineGap
|
2024-11-02 13:59:55 +00:00
|
|
|
in
|
2024-11-04 02:46:02 +00:00
|
|
|
(case (leftStrings, leftLines) of
|
2025-03-22 05:18:25 +00:00
|
|
|
(lshd :: lstl, llhd :: lltl) =>
|
|
|
|
|
let
|
|
|
|
|
val result = startVi0
|
|
|
|
|
( String.size lshd - 1
|
|
|
|
|
, lshd
|
|
|
|
|
, llhd
|
|
|
|
|
, bufferIdx - 1
|
|
|
|
|
, lstl
|
|
|
|
|
, lltl
|
|
|
|
|
)
|
|
|
|
|
in
|
|
|
|
|
if result = bufferIdx then bufferIdx - 1 else result
|
|
|
|
|
end
|
|
|
|
|
| (_, _) => 0)
|
2024-11-02 13:59:55 +00:00
|
|
|
end
|
|
|
|
|
end
|
2024-11-05 12:38:01 +00:00
|
|
|
|
|
|
|
|
(* Prerequisite: lineGap is moved to cursorIdx *)
|
|
|
|
|
fun isCursorAtStartOfLine (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
|
|
|
|
in
|
|
|
|
|
case rightStrings of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
if strIdx < String.size hd then
|
|
|
|
|
(* chr is in hd *)
|
|
|
|
|
String.sub (hd, strIdx) = #"\n"
|
|
|
|
|
else
|
|
|
|
|
(* chr is in tl *)
|
|
|
|
|
(case tl of
|
2025-03-22 05:18:25 +00:00
|
|
|
tlhd :: _ =>
|
|
|
|
|
let val strIdx = strIdx - String.size hd
|
|
|
|
|
in String.sub (tlhd, strIdx) = #"\n"
|
|
|
|
|
end
|
|
|
|
|
| [] => true)
|
2024-11-05 12:38:01 +00:00
|
|
|
end
|
|
|
|
|
| [] => true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
(* Prerequisite: lineGap is moved to cursorIdx *)
|
|
|
|
|
fun isPrevChrStartOfLine (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, idx = bufferIdx, leftStrings, ...} = lineGap
|
|
|
|
|
in
|
|
|
|
|
case rightStrings of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
if strIdx > 0 then
|
|
|
|
|
(* prev chr is in hd *)
|
|
|
|
|
String.sub (hd, strIdx - 1) = #"\n"
|
|
|
|
|
else
|
|
|
|
|
(* prev chr if in leftStrings *)
|
|
|
|
|
(case leftStrings of
|
2025-03-22 05:18:25 +00:00
|
|
|
lhd :: _ => String.sub (lhd, String.size lhd - 1) = #"\n"
|
|
|
|
|
| [] =>
|
|
|
|
|
(* cursorIdx = 0 which means we are at start of file/line *)
|
|
|
|
|
true)
|
2024-11-05 12:38:01 +00:00
|
|
|
end
|
2025-03-22 05:18:25 +00:00
|
|
|
| [] => true
|
2024-11-05 12:38:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun helpIsNextChrEndOfLine (strIdx, hd, tl) =
|
|
|
|
|
if strIdx + 1 < String.size hd then
|
|
|
|
|
(* next chr is in this string *)
|
|
|
|
|
String.sub (hd, strIdx + 1) = #"\n"
|
|
|
|
|
else
|
|
|
|
|
(* next chr, if it exists, is in tl *)
|
|
|
|
|
(case tl of
|
2025-03-22 05:18:25 +00:00
|
|
|
tlhd :: _ => String.sub (tlhd, 0) = #"\n"
|
|
|
|
|
| [] => true)
|
2024-11-05 12:38:01 +00:00
|
|
|
|
|
|
|
|
(* Prerequisite: lineGap is moved to cursorIdx *)
|
|
|
|
|
fun isNextChrEndOfLine (lineGap: LineGap.t, cursorIdx) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
|
|
|
|
in
|
|
|
|
|
case rightStrings of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
let
|
|
|
|
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
|
|
|
|
if strIdx < String.size hd then
|
|
|
|
|
helpIsNextChrEndOfLine (strIdx, hd, tl)
|
|
|
|
|
else
|
|
|
|
|
(* strIdx is in tl *)
|
|
|
|
|
(case tl of
|
2025-03-22 05:18:25 +00:00
|
|
|
tlhd :: tltl =>
|
|
|
|
|
helpIsNextChrEndOfLine (strIdx - String.size hd, tlhd, tltl)
|
|
|
|
|
| [] =>
|
|
|
|
|
(* strIdx is at end of lineGap
|
|
|
|
|
* which also means at end of line *)
|
2024-11-05 12:38:01 +00:00
|
|
|
true)
|
|
|
|
|
end
|
|
|
|
|
| [] => true
|
|
|
|
|
end
|
2024-11-08 18:47:26 +00:00
|
|
|
|
|
|
|
|
(* Prerequisite: lineGap is moved to cursorIdx *)
|
2024-11-08 20:43:36 +00:00
|
|
|
fun clipIdx (lineGap: LineGap.t, cursorIdx) =
|
2024-11-08 18:47:26 +00:00
|
|
|
let
|
|
|
|
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
|
|
|
|
in
|
2024-11-08 20:43:36 +00:00
|
|
|
(* We are trying to check if cursorIdx is within the buffer. *)
|
2024-11-08 18:47:26 +00:00
|
|
|
case rightStrings of
|
2025-03-22 05:18:25 +00:00
|
|
|
_ :: _ :: _ =>
|
2024-11-08 20:43:36 +00:00
|
|
|
(* if there is a string after the hd,
|
|
|
|
|
* we are definitely in a valid idx and should return it *)
|
|
|
|
|
cursorIdx
|
|
|
|
|
| [hd] =>
|
2024-11-08 18:47:26 +00:00
|
|
|
let
|
|
|
|
|
val strIdx = cursorIdx - bufferIdx
|
|
|
|
|
in
|
2024-11-08 20:43:36 +00:00
|
|
|
if strIdx < String.size hd - 1 then
|
|
|
|
|
(* if we are before the last char in the string.
|
|
|
|
|
* Unix file endings always have \n at the end
|
|
|
|
|
* but we do not want cursor to ever go to end
|
|
|
|
|
* as vi also does not go to the very end.
|
|
|
|
|
* This is why we check strIdx is before the last char.
|
|
|
|
|
* *)
|
2024-11-08 18:47:26 +00:00
|
|
|
cursorIdx
|
|
|
|
|
else
|
2024-11-08 20:43:36 +00:00
|
|
|
(* if end of buffer - 2 is greater than 0, then that;
|
|
|
|
|
* else, 0 *)
|
|
|
|
|
Int.max (bufferIdx + String.size hd - 2, 0)
|
2024-11-08 18:47:26 +00:00
|
|
|
end
|
2025-03-22 05:18:25 +00:00
|
|
|
| [] =>
|
2024-11-08 20:43:36 +00:00
|
|
|
(* if end of buffer - 2 is greater than 0, then that;
|
|
|
|
|
* else, 0 *)
|
|
|
|
|
Int.max (bufferIdx - 2, 0)
|
2024-11-08 18:47:26 +00:00
|
|
|
end
|
2024-10-17 02:57:26 +01:00
|
|
|
end
|