reimplement vi's 'j' motion, and remove the original function for this motion which was in cursor.sml

This commit is contained in:
2025-09-19 22:33:37 +01:00
parent 2c88341c37
commit 859860b19f
4 changed files with 90 additions and 145 deletions

View File

@@ -132,147 +132,6 @@ struct
if lineIdx = 0 then cursorIdx else cursorIdx - lineIdx - 1
end
fun helpViJ
( strPos
, str
, absIdx
, lineColumn
, preferredColumn
, hasPassedLine
, strTl
, lineTl
, prevIsLn
) =
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. *)
helpViJ
( 0
, shd
, absIdx
, lineColumn
, preferredColumn
, hasPassedLine
, stl
, ltl
, prevIsLn
)
| (_, _) => (* empty, so return end of previous string *) absIdx - 1
else
case String.sub (str, strPos) of
#"\n" =>
if hasPassedLine then
(* reached end of line twice,
* but line has fewer chars than preferredColumn *)
if prevIsLn then
(* line break is preceded by linebreak *)
absIdx
else
(* line break is preceded by graphical chr
* so go to graphical chr *)
absIdx - 1
else
(* reached end of line once;
* continue iterationg *)
helpViJ
( strPos + 1
, str
, absIdx + 1
, 0
, preferredColumn
, true
, strTl
, lineTl
, true
)
| _ =>
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
( strPos + 1
, str
, absIdx + 1
, lineColumn + 1
, preferredColumn
, hasPassedLine
, strTl
, lineTl
, false
)
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 *)
let
val lineColumn = getCursorColumn
(strIdx, strHd, lnHd, leftStrings, leftLines, cursorIdx)
in
helpViJ
( strIdx
, strHd
, cursorIdx
, lineColumn
, lineColumn
, false
, strTl
, lnTl
, false
)
end
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
val lineColumn = getCursorColumn
( strIdx
, nestStrHd
, nestLnHd
, leftStrings
, leftLines
, cursorIdx
)
in
helpViJ
( strIdx
, nestStrHd
, cursorIdx
, lineColumn
, lineColumn
, false
, nestStrTl
, nestLnTl
, false
)
end
| (_, _) => cursorIdx)
end
| (_, _) => (* nowhere to go rightward, so return cursorIdx *) cursorIdx
end
(* equivalent of vi's 'w' command *)
val nextWord = ViWordDfa.startOfNextWord

View File

@@ -42,8 +42,6 @@ struct
end
end
structure MoveViJ = MakeMove (struct val fMove = Cursor.viJ end)
structure MoveToStartOfLine = MakeMove (struct val fMove = Cursor.vi0 end)
signature DFA_MOVE =

View File

@@ -147,7 +147,7 @@ struct
fun parseChr (app: app_type, count, chr, str, time) =
case chr of
#"h" => MoveViH.move (app, count)
| #"j" => MoveViJ.move (app, count)
| #"j" => NormalMove.moveCursorDown (app, count)
| #"k" => NormalMove.moveCursorUp (app, count)
| #"l" => MoveViL.move (app, count)
| #"w" => MoveToNextWord.move (app, count)

View File

@@ -132,6 +132,7 @@ struct
else
LineGap.idxToLineNumber (cursorIdx, buffer)
val newCursorLineNumber = Int.max (cursorLineNumber - count, 0)
val column = if newCursorLineNumber = 0 then column - 1 else column
val buffer = LineGap.goToLine (newCursorLineNumber, buffer)
val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer)
@@ -143,7 +144,6 @@ struct
val buffer = LineGap.goToIdx (lineIdx, buffer)
val endOfLineIdx = Cursor.viDlr (buffer, lineIdx, 1)
val column = if newCursorLineNumber = 0 then column - 1 else column
val cursorIdx = Int.min (endOfLineIdx, lineIdx + column)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
@@ -188,6 +188,94 @@ struct
)
end
fun moveCursorDown (app: app_type, count) =
let
val
{ windowWidth
, windowHeight
, cursorIdx
, buffer
, startLine = prevLineNumber
, searchList
, searchString
, bufferModifyTime
, visualScrollColumn = prevScrollColumn
, ...
} = app
(* calculate new idx to move to *)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val startOfLine = Cursor.vi0 (buffer, cursorIdx)
val column = cursorIdx - startOfLine
val cursorLineNumber =
if Cursor.isNextChrEndOfLine (buffer, cursorIdx) then
LineGap.idxToLineNumber (cursorIdx + 1, buffer)
else
LineGap.idxToLineNumber (cursorIdx, buffer)
val newCursorLineNumber = cursorLineNumber + count
val newCursorLineNumber =
if cursorLineNumber >= #lineLength buffer - 2 then
Int.max (0, #lineLength buffer - 2)
else
newCursorLineNumber
val buffer = LineGap.goToLine (newCursorLineNumber, buffer)
val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer)
val buffer = LineGap.goToIdx (lineIdx, buffer)
val lineIdx =
if Cursor.isNextChrEndOfLine (buffer, lineIdx) then lineIdx
else lineIdx + 1
val buffer = LineGap.goToIdx (lineIdx, buffer)
val endOfLineIdx = Cursor.viDlr (buffer, lineIdx, 1)
val cursorIdx = Int.min (endOfLineIdx, lineIdx + column)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
(* create draw message *)
val visualScrollColumn =
TextScroll.getScrollColumn
(buffer, cursorIdx, windowWidth, prevScrollColumn)
val startLine =
TextScroll.getStartLine
(prevLineNumber, newCursorLineNumber, windowHeight)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, searchString
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
val drawMsg = DrawMsg.DRAW_TEXT drawMsg
val drawMsg = [MailboxType.DRAW drawMsg]
val mode = NORMAL_MODE ""
in
NormalModeWith.bufferAndCursorIdx
( app
, buffer
, cursorIdx
, mode
, startLine
, searchList
, drawMsg
, bufferModifyTime
, visualScrollColumn
)
end
fun moveToLine (app: app_type, reqLine) =
let
val reqLine = reqLine - 1