just formatting cursor.sml

This commit is contained in:
2024-10-25 15:47:39 +01:00
parent b79a8696f5
commit c2ea6e5e04

View File

@@ -4,16 +4,12 @@ struct
if strPos < 0 then
case (strTl, lineTl) of
(shd :: stl, lhd :: ltl) =>
helpVi0
(String.size shd - 1, shd, absIdx, stl, ltl)
| (_, _) =>
0
helpVi0 (String.size shd - 1, shd, absIdx, stl, ltl)
| (_, _) => 0
else
case String.sub (str, strPos) of
#"\n" =>
absIdx + 1
| _ =>
helpVi0 (strPos - 1, str, absIdx - 1, strTl, lineTl)
#"\n" => absIdx + 1
| _ => helpVi0 (strPos - 1, str, absIdx - 1, strTl, lineTl)
fun vi0 (lineGap: LineGap.t, cursorIdx) =
let
@@ -35,10 +31,7 @@ struct
cursorIdx
else
(* not at newline so start iterating *)
helpVi0
( strIdx - 1, strHd, cursorIdx - 1
, strTl, lnTl
)
helpVi0 (strIdx - 1, strHd, cursorIdx - 1, strTl, lnTl)
else
(* strIdx must be in the strTl *)
(case (strTl, lnTl) of
@@ -52,8 +45,11 @@ struct
else
(* not in linebreak *)
helpVi0
( strIdx - 1, nestStrHd, cursorIdx - 1
, strHd :: leftStrings, lnHd :: leftLines
( strIdx - 1
, nestStrHd
, cursorIdx - 1
, strHd :: leftStrings
, lnHd :: leftLines
)
end
| (_, _) => cursorIdx)
@@ -66,17 +62,12 @@ struct
fun helpViDlr (strPos, str, absIdx, strTl, lineTl) =
if strPos = String.size str then
case (strTl, lineTl) of
(shd :: stl, lhd :: ltl) =>
helpViDlr
(0, shd, absIdx, stl, ltl)
| (_, _) =>
absIdx - 1
(shd :: stl, lhd :: ltl) => helpViDlr (0, shd, absIdx, stl, ltl)
| (_, _) => absIdx - 1
else
case String.sub (str, strPos) of
#"\n" =>
absIdx - 1
| _ =>
helpViDlr (strPos + 1, str, absIdx + 1, strTl, lineTl)
#"\n" => absIdx - 1
| _ => helpViDlr (strPos + 1, str, absIdx + 1, strTl, lineTl)
fun viDlr (lineGap: LineGap.t, cursorIdx) =
let
@@ -93,8 +84,7 @@ struct
if strIdx < String.size strHd then
if String.sub (strHd, strIdx) <> #"\n" then
(* not in double linebreak *)
helpViDlr
(strIdx + 1, strHd, cursorIdx + 1, strTl, lnTl)
helpViDlr (strIdx + 1, strHd, cursorIdx + 1, strTl, lnTl)
else
(* check if we are in double linebreak *)
if strIdx - 1 >= 0 then
@@ -103,8 +93,7 @@ struct
cursorIdx
else
(* not in double linebreak, so iterate *)
helpViDlr
(strIdx + 1, strHd, cursorIdx + 1 , strTl, lnTl)
helpViDlr (strIdx + 1, strHd, cursorIdx + 1, strTl, lnTl)
else
(* check if double linebreak in strTl *)
(case strTl of
@@ -112,8 +101,7 @@ struct
if String.sub (nestStrHd, 0) = #"\n" then
cursorIdx
else
helpViDlr
(strIdx + 1, strHd, cursorIdx + 1, strTl, lnTl)
helpViDlr (strIdx + 1, strHd, cursorIdx + 1, strTl, lnTl)
| [] => cursorIdx)
else
(* strIdx must be in the strTl *)
@@ -123,7 +111,12 @@ struct
val strIdx = strIdx - String.size strHd
in
helpViDlr
(strIdx + 1, nestStrHd, cursorIdx + 1, nestStrTl, nestLnTl)
( strIdx + 1
, nestStrHd
, cursorIdx + 1
, nestStrTl
, nestLnTl
)
end
| (_, _) => cursorIdx)
end
@@ -246,7 +239,8 @@ struct
(* no chars before this idx; have to check leftStrings *)
(case leftStrings of
lHd :: lTl =>
(* if there is another string after current head, we can increment cursorIdx
(* if there is another string after current head,
* we can increment cursorIdx
* however, first we need to check if next char is \n. *)
let
val lastChr = String.sub (lHd, String.size lHd - 1)
@@ -575,7 +569,7 @@ struct
helpViJ
( strIdx + 1, nestStrHd, cursorIdx + 1
, lineColumn, lineColumn, false
, nestStrTl, nestStrTl
, nestStrTl, nestLnTl
)
end
end
@@ -605,9 +599,7 @@ struct
else
case String.sub (str, strPos) of
#"\n" =>
if
hasPassedLine
then
if hasPassedLine then
(* reached end of line twice,
* but line has fewer chars than preferredColumn
* note: if in double \n\n linebreak,
@@ -725,15 +717,20 @@ struct
if String.sub (lStrHd, String.size lStrHd - 1) = #"\n" then
(* in double linebreak *)
helpVi0
(String.size lStrHd - 2, lStrHd, cursorIdx - 2, lStrTl, lLnTl)
( String.size lStrHd - 2, lStrHd, cursorIdx - 2
, lStrTl, lLnTl
)
else
(* in single linebreak *)
helpVi0
(strIdx - 1, strHd, cursorIdx - 1, leftStrings, leftLines)
( strIdx - 1, strHd, cursorIdx - 1
, leftStrings, leftLines
)
| (_, _) =>
helpViK
( strIdx - 1, strHd, cursorIdx - 1
, 0, 0, true, leftStrings, lnHd, leftLines
, 0, 0, true
, leftStrings, lnHd, leftLines
))
else
(* not at newline
@@ -776,9 +773,7 @@ struct
)
else
(* must check strHd for second linebreak *)
if
String.sub (strHd, String.size strHd - 1) = #"\n"
then
if String.sub (strHd, String.size strHd - 1) = #"\n" then
(* is in double linebreak *)
helpVi0
( String.size strHd - 2, nestStrHd, cursorIdx - 2
@@ -798,7 +793,9 @@ struct
helpViK
( strIdx - 1, nestStrHd, cursorIdx - 1
, lineColumn, lineColumn, false
, strHd :: leftStrings, nestLnHd, lnHd :: leftLines
, strHd :: leftStrings
, nestLnHd
, lnHd :: leftLines
)
end
end
@@ -811,26 +808,17 @@ struct
fun isNextChrSpace (strPos, str, strTl) =
if strPos + 1 < String.size str then
let
val chr = String.sub (str, strPos + 1)
in
Char.isSpace chr
let val chr = String.sub (str, strPos + 1)
in Char.isSpace chr
end
else
case strTl of
hd :: _ =>
let
val chr = String.sub (hd, 0)
in
Char.isSpace chr
end
hd :: _ => let val chr = String.sub (hd, 0) in Char.isSpace chr end
| [] => false
fun notIsNextChrSpace (strPos, str, strTl) =
let
val isSpace = isNextChrSpace (strPos, str, strTl)
in
not isSpace
let val isSpace = isNextChrSpace (strPos, str, strTl)
in not isSpace
end
fun isNextChrNonBlank (strPos, str, strTl) =
@@ -838,9 +826,7 @@ struct
let
val chr = String.sub (str, strPos + 1)
val isNotBlank =
Char.isSpace chr
orelse Char.isAlphaNum chr
orelse chr = #"_"
Char.isSpace chr orelse Char.isAlphaNum chr orelse chr = #"_"
in
not isNotBlank
end
@@ -850,9 +836,7 @@ struct
let
val chr = String.sub (hd, 0)
val isNotBlank =
Char.isSpace chr
orelse Char.isAlphaNum chr
orelse chr = #"_"
Char.isSpace chr orelse Char.isAlphaNum chr orelse chr = #"_"
in
not isNotBlank
end
@@ -860,59 +844,45 @@ struct
fun isNextChrAlphaNum (strPos, str, stl) =
if strPos + 1 < String.size str then
let
val chr = String.sub (str, strPos + 1)
in
Char.isAlphaNum chr orelse chr = #"_"
let val chr = String.sub (str, strPos + 1)
in Char.isAlphaNum chr orelse chr = #"_"
end
else
case stl of
hd :: _ =>
let
val chr = String.sub (str, 0)
in
Char.isAlphaNum chr orelse chr = #"_"
let val chr = String.sub (str, 0)
in Char.isAlphaNum chr orelse chr = #"_"
end
| [] => false
fun isPrevChrSpace (strPos, str, strTl) =
if strPos > 0 then
let
val prevChr = String.sub (str, strPos - 1)
in
Char.isSpace prevChr
let val prevChr = String.sub (str, strPos - 1)
in Char.isSpace prevChr
end
else
case strTl of
hd :: _ =>
let
val prevChr = String.sub (hd, String.size hd - 1)
in
Char.isSpace prevChr
let val prevChr = String.sub (hd, String.size hd - 1)
in Char.isSpace prevChr
end
| [] => false
fun notIsPrevChrSpace (strPos, str, strTl) =
let
val isSpace = isPrevChrSpace (strPos, str, strTl)
in
not isSpace
let val isSpace = isPrevChrSpace (strPos, str, strTl)
in not isSpace
end
fun isPrevChrAlphaNum (strPos, str, strTl) =
if strPos > 0 then
let
val chr = String.sub (str, strPos - 1)
in
Char.isAlphaNum chr orelse chr = #"_"
let val chr = String.sub (str, strPos - 1)
in Char.isAlphaNum chr orelse chr = #"_"
end
else
case strTl of
hd :: _ =>
let
val chr = String.sub (hd, String.size hd - 1)
in
Char.isAlphaNum chr orelse chr = #"_"
let val chr = String.sub (hd, String.size hd - 1)
in Char.isAlphaNum chr orelse chr = #"_"
end
| [] => false
@@ -921,9 +891,7 @@ struct
let
val chr = String.sub (str, strPos - 1)
val isNotBlank =
Char.isSpace chr
orelse Char.isAlphaNum chr
orelse chr = #"_"
Char.isSpace chr orelse Char.isAlphaNum chr orelse chr = #"_"
in
not isNotBlank
end
@@ -933,9 +901,7 @@ struct
let
val chr = String.sub (hd, String.size hd - 1)
val isNotBlank =
Char.isSpace chr
orelse Char.isAlphaNum chr
orelse chr = #"_"
Char.isSpace chr orelse Char.isAlphaNum chr orelse chr = #"_"
in
not isNotBlank
end
@@ -955,30 +921,24 @@ struct
val chr = String.sub (str, strPos)
in
if Char.isAlphaNum chr orelse chr = #"_" then
if isNextChrNonBlank (strPos, str, strTl) then
absIdx + 1
else
helpNextWord
(strPos + 1, str, absIdx + 1, strTl, lineTl)
if isNextChrNonBlank (strPos, str, strTl) then absIdx + 1
else helpNextWord (strPos + 1, str, absIdx + 1, strTl, lineTl)
else if Char.isSpace chr then
if notIsNextChrSpace (strPos, str, strTl) then
absIdx + 1
else
(* nothing to do on space, except keep iterating *)
helpNextWord
(strPos + 1, str, absIdx + 1, strTl, lineTl)
helpNextWord (strPos + 1, str, absIdx + 1, strTl, lineTl)
else
(* chr is NON_BLANK. *)
if isNextChrAlphaNum (strPos, str, strTl) then
absIdx + 1
else
helpNextWord
(strPos + 1, str, absIdx + 1, strTl, lineTl)
helpNextWord (strPos + 1, str, absIdx + 1, strTl, lineTl)
end
fun startNextWord (shd, strIdx, absIdx, stl, ltl) =
helpNextWord
(strIdx, shd, absIdx, stl, ltl)
helpNextWord (strIdx, shd, absIdx, stl, ltl)
fun nextWord (lineGap: LineGap.t, cursorIdx) =
let
@@ -997,11 +957,8 @@ struct
(* strIdx is in tl *)
(case (stl, ltl) of
(stlhd :: stltl, ltlhd :: ltltl) =>
let
val strIdx = strIdx - String.size shd
in
startNextWord
(stlhd, strIdx, cursorIdx, stltl, ltltl)
let val strIdx = strIdx - String.size shd
in startNextWord (stlhd, strIdx, cursorIdx, stltl, ltltl)
end
| (_, _) => cursorIdx)
end
@@ -1012,8 +969,7 @@ struct
if strPos < 0 then
case (strTl, lineTl) of
(shd :: stl, lhd :: ltl) =>
helpPrevWord
(String.size shd - 1, shd, absIdx, stl, ltl)
helpPrevWord (String.size shd - 1, shd, absIdx, stl, ltl)
| (_, _) =>
(* reached start of lineGap;
* return 0 which is start idx *)
@@ -1022,24 +978,25 @@ struct
let
val chr = String.sub (str, strPos)
in
if Char.isAlphaNum chr orelse chr = #"_" then
if
Char.isAlphaNum chr orelse chr = #"_"
then
if isPrevChrSpace (strPos, str, strTl)
orelse isPrevChrNonBlank (strPos, str, strTl) then
absIdx
else
helpPrevWord
(strPos - 1, str, absIdx - 1, strTl, lineTl)
else if Char.isSpace chr then
helpPrevWord
(strPos - 1, str, absIdx - 1, strTl, lineTl)
orelse isPrevChrNonBlank (strPos, str, strTl)
then absIdx
else helpPrevWord (strPos - 1, str, absIdx - 1, strTl, lineTl)
else if
Char.isSpace chr
then
helpPrevWord (strPos - 1, str, absIdx - 1, strTl, lineTl)
else
(* is NON_BLANK *)
if isPrevChrSpace (strPos, str, strTl)
orelse isPrevChrAlphaNum (strPos, str, strTl) then
orelse isPrevChrAlphaNum (strPos, str, strTl)
then
absIdx
else
helpPrevWord
(strPos - 1, str, absIdx - 1, strTl, lineTl)
helpPrevWord (strPos - 1, str, absIdx - 1, strTl, lineTl)
end
fun startPrevWord (shd, strIdx, absIdx, stl, ltl) =
@@ -1047,16 +1004,12 @@ struct
* and ignore the character the cursor is at
* so check previous character *)
if strIdx > 0 then
helpPrevWord
(strIdx - 1, shd, absIdx - 1, stl, ltl)
helpPrevWord (strIdx - 1, shd, absIdx - 1, stl, ltl)
else
case (stl, ltl) of
(stlhd :: stltl, ltlhd :: ltltl) =>
let
val prevIdx = String.size stlhd - 1
in
helpPrevWord
(prevIdx, stlhd, absIdx - 1, stltl, ltltl)
let val prevIdx = String.size stlhd - 1
in helpPrevWord (prevIdx, stlhd, absIdx - 1, stltl, ltltl)
end
| (_, _) =>
(* tl is empty; just return idx 0 *)
@@ -1065,7 +1018,9 @@ struct
(* equivalent of vi's `b` command *)
fun prevWord (lineGap: LineGap.t, cursorIdx) =
let
val {rightStrings, rightLines, leftStrings, leftLines, idx = bufferIdx, ...} = lineGap
val
{rightStrings, rightLines, leftStrings, leftLines, idx = bufferIdx, ...} =
lineGap
in
case (rightStrings, rightLines) of
(shd :: stl, lhd :: ltl) =>
@@ -1075,8 +1030,7 @@ struct
in
if strIdx < String.size shd then
(* strIdx is in this string *)
startPrevWord
(shd, strIdx, cursorIdx, leftStrings, leftLines)
startPrevWord (shd, strIdx, cursorIdx, leftStrings, leftLines)
else
(* strIdx is in tl *)
(case (stl, ltl) of
@@ -1097,44 +1051,41 @@ struct
fun helpEndOfWord (strPos, str, absIdx, stl, ltl) =
if strPos = String.size str then
case (stl, ltl) of
(shd :: stl, lhd :: ltl) =>
helpEndOfWord (0, shd, absIdx, stl, ltl)
| (_, _) =>
absIdx - 1
(shd :: stl, lhd :: ltl) => helpEndOfWord (0, shd, absIdx, stl, ltl)
| (_, _) => absIdx - 1
else
let
val chr = String.sub (str, strPos)
in
if Char.isAlphaNum chr orelse chr = #"_" then
if
Char.isAlphaNum chr orelse chr = #"_"
then
if isNextChrSpace (strPos, str, stl)
orelse isNextChrNonBlank (strPos, str, stl) then
absIdx
else
helpEndOfWord
(strPos + 1, str, absIdx + 1, stl, ltl)
else if Char.isSpace chr then
helpEndOfWord
(strPos + 1, str, absIdx + 1, stl, ltl)
orelse isNextChrNonBlank (strPos, str, stl)
then absIdx
else helpEndOfWord (strPos + 1, str, absIdx + 1, stl, ltl)
else if
Char.isSpace chr
then
helpEndOfWord (strPos + 1, str, absIdx + 1, stl, ltl)
else
(* is NON_BLANK *)
if isNextChrSpace (strPos, str, stl)
orelse isNextChrAlphaNum (strPos, str, stl) then
orelse isNextChrAlphaNum (strPos, str, stl)
then
absIdx
else
helpEndOfWord
(strPos + 1, str, absIdx + 1, stl, ltl)
helpEndOfWord (strPos + 1, str, absIdx + 1, stl, ltl)
end
fun startEndOfWord (shd, strIdx, absIdx, stl, ltl) =
(* we want to start iterating from next char after strIdx *)
if strIdx - 1 < String.size shd then
helpEndOfWord
(strIdx + 1, shd, absIdx + 1, stl, ltl)
helpEndOfWord (strIdx + 1, shd, absIdx + 1, stl, ltl)
else
case (stl, ltl) of
(stlhd :: stltl, ltlhd :: ltltl) =>
helpEndOfWord
(0, stlhd, absIdx + 1, stltl, ltltl)
helpEndOfWord (0, stlhd, absIdx + 1, stltl, ltltl)
| (_, _) =>
(* tl is empty; just return absIdx *)
absIdx
@@ -1142,7 +1093,9 @@ struct
(* equivalent of vi's `e` command *)
fun endOfWord (lineGap: LineGap.t, cursorIdx) =
let
val {rightStrings, rightLines, leftStrings, leftLines, idx = bufferIdx, ...} = lineGap
val
{rightStrings, rightLines, leftStrings, leftLines, idx = bufferIdx, ...} =
lineGap
in
case (rightStrings, rightLines) of
(shd :: stl, lhd :: ltl) =>
@@ -1151,17 +1104,13 @@ struct
in
if strIdx < String.size shd then
(* strIdx is in this string *)
startEndOfWord
(shd, strIdx, cursorIdx, rightStrings, rightLines)
startEndOfWord (shd, strIdx, cursorIdx, rightStrings, rightLines)
else
(* strIdx is in tl *)
(case (stl, ltl) of
(stlhd :: stltl, ltlhd :: ltltl) =>
let
val strIdx = strIdx - String.size shd
in
startEndOfWord
(stlhd, strIdx, cursorIdx, stltl, ltltl)
let val strIdx = strIdx - String.size shd
in startEndOfWord (stlhd, strIdx, cursorIdx, stltl, ltltl)
end
| (_, _) => cursorIdx)
end