diff --git a/fcore/bin-search.sml b/fcore/bin-search.sml index 3b576e7..94bff73 100644 --- a/fcore/bin-search.sml +++ b/fcore/bin-search.sml @@ -3,7 +3,7 @@ struct local fun reverseLinearSearch (findNum, idx, vec) = if idx < 0 then - idx + ~1 else let val curVal = Vector.sub (vec, idx) @@ -38,7 +38,7 @@ struct local fun forwardLinearSearch (findNum, idx, vec) = if idx = Vector.length vec then - idx + ~1 else let val curVal = Vector.sub (vec, idx) diff --git a/fcore/cursor.sml b/fcore/cursor.sml index 2ef10b4..07e695b 100644 --- a/fcore/cursor.sml +++ b/fcore/cursor.sml @@ -29,7 +29,13 @@ struct val linePos = BinSearch.equalOrLess (strPos - 1, lhd) val lineIdx = Vector.sub (lhd, linePos) in - absIdx - strPos + lineIdx + 1 + if linePos = ~1 then + (* no previous line in lhd *) + helpVi0 (absIdx - strPos, stl, ltl) + else + let val lineIdx = Vector.sub (lhd, linePos) + in absIdx - strPos + lineIdx + 1 + end end else helpVi0 (absIdx - strPos, stl, ltl) @@ -224,6 +230,7 @@ struct * because we know lnHd definitely contains * a lineIdx less or equal to strIdx *) let + (* todo: what if BinSearch doesn't find anything? *) val lnIdx = BinSearch.equalOrLess (strIdx, lnHd) val lnIdx = Vector.sub (lnHd, lnIdx) in diff --git a/fcore/search-list.sml b/fcore/search-list.sml index 84e9ee9..f28e960 100644 --- a/fcore/search-list.sml +++ b/fcore/search-list.sml @@ -262,7 +262,7 @@ struct else let val pos = BinSearch.equalOrMore (cursorIdx + 1, searchList) - val pos = if pos < Vector.length searchList then pos else 0 + val pos = if pos = ~1 then 0 else pos val count = count - 1 in loopNextMatch (pos, searchList, count) @@ -286,7 +286,7 @@ struct else let val pos = BinSearch.equalOrLess (cursorIdx - 1, searchList) - val pos = if pos < 0 then Vector.length searchList - 1 else pos + val pos = if pos = ~1 then Vector.length searchList - 1 else pos val count = count - 1 in loopPrevMatch (pos, searchList, count) diff --git a/fcore/text-builder/normal-mode-text-builder.sml b/fcore/text-builder/normal-mode-text-builder.sml index 9c913fa..a237747 100644 --- a/fcore/text-builder/normal-mode-text-builder.sml +++ b/fcore/text-builder/normal-mode-text-builder.sml @@ -29,7 +29,10 @@ struct Utils.getRelativeLineStartFromRightHead (startLine, curLine, lhd) (* get absolute idx of line *) val absIdx = curIdx + strPos + val searchPos = BinSearch.equalOrMore (absIdx, searchList) + val searchPos = + if searchPos = ~1 then Vector.length searchList else searchPos val env = Utils.initEnv ( windowWidth diff --git a/fcore/text-builder/text-builder-with-cursor.sml b/fcore/text-builder/text-builder-with-cursor.sml index c4cd4ce..45751c8 100644 --- a/fcore/text-builder/text-builder-with-cursor.sml +++ b/fcore/text-builder/text-builder-with-cursor.sml @@ -60,7 +60,7 @@ struct let val searchPos = BinSearch.equalOrMore (pos + 1, #searchList env) in - if searchPos = Vector.length line then + if searchPos = ~1 then (* next line is not in this node *) let val absIdx = absIdx - pos diff --git a/fcore/text-builder/text-builder-with-highlight.sml b/fcore/text-builder/text-builder-with-highlight.sml index fd99f1a..1124237 100644 --- a/fcore/text-builder/text-builder-with-highlight.sml +++ b/fcore/text-builder/text-builder-with-highlight.sml @@ -75,7 +75,7 @@ struct let val searchPos = BinSearch.equalOrMore (pos + 1, #searchList env) in - if searchPos = Vector.length line then + if searchPos = ~1 then (* next line is not in this node *) let val absIdx = absIdx - pos