Add 'shf/' from commit 'b6c5a95b664aeb861d7b33ffc9eefe447ba99dd7'

git-subtree-dir: shf
git-subtree-mainline: 401408448f
git-subtree-split: b6c5a95b66
This commit is contained in:
2026-04-24 00:27:49 +01:00
83 changed files with 43952 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,5 @@
structure NormalDelete =
MakeNormalDelete
(struct
fun initMsgs _ = []
end)

View File

@@ -0,0 +1,155 @@
structure NormalFinish =
struct
open AppType
open MailboxType
open DrawMsg
open InputMsg
fun clearMode app =
NormalModeWith.mode (app, NORMAL_MODE "", [])
fun buildTextAndClear
(app: app_type, buffer, cursorIdx, searchList, msgs, bufferModifyTime) =
let
val
{ windowWidth
, windowHeight
, visualScrollColumn = prevScrollColumn
, startLine = prevLineNumber
, ...
} = app
(* calculate new scroll column and start line, if there are any changes *)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val visualScrollColumn =
TextScroll.getScrollColumn
(buffer, cursorIdx, windowWidth, prevScrollColumn)
val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer)
val startLine =
TextScroll.getStartLine
(prevLineNumber, cursorLine, windowHeight, #lineLength buffer)
(* move buffer to new startLine as required by TextBuilder.build *)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
val drawMsg = DRAW_TEXT drawMsg
val msgs = DRAW drawMsg :: msgs
val mode = NORMAL_MODE ""
in
NormalModeWith.bufferAndCursorIdx
( app
, buffer
, cursorIdx
, mode
, startLine
, searchList
, msgs
, bufferModifyTime
, visualScrollColumn
)
end
fun resizeText (app: app_type, newWidth, newHeight) =
let
val
{ buffer
, windowWidth
, windowHeight
, startLine
, cursorIdx
, searchList
, bufferModifyTime
, visualScrollColumn = prevScrollColumn
, ...
} = app
val newBuffer = LineGap.goToIdx (cursorIdx, buffer)
val visualScrollColumn =
TextScroll.getScrollColumn
(newBuffer, cursorIdx, newWidth, prevScrollColumn)
val newBuffer = LineGap.goToLine (startLine, newBuffer)
val lineIdx = TextBuilderUtils.getLineAbsIdxFromBuffer (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, newWidth
, newHeight
, searchList
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
val drawMsg = DRAW_TEXT drawMsg
val msgs = [DRAW drawMsg]
in
NormalModeWith.bufferAndSize
( app
, newBuffer
, newWidth
, newHeight
, searchList
, msgs
, bufferModifyTime
, visualScrollColumn
)
end
fun centreToCursor (app: app_type) =
let
val
{ buffer
, windowWidth
, windowHeight
, startLine = prevLineNumber
, cursorIdx
, searchList
, bufferModifyTime
, visualScrollColumn
, ...
} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer)
val startLine = TextScroll.getLineCentre (cursorLine, windowHeight)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
val drawMsg = DRAW_TEXT drawMsg
val drawMsg = [DRAW drawMsg]
in
NormalModeWith.bufferAndCursorIdx
( app
, buffer
, cursorIdx
, NORMAL_MODE ""
, startLine
, searchList
, drawMsg
, bufferModifyTime
, #visualScrollColumn app
)
end
end

View File

@@ -0,0 +1,203 @@
structure NormalModeWith =
struct
open AppType
fun bufferMsgsAndMode (app: app_type, newBuffer, newMsgs, newMode) =
let
val
{ mode = _
, buffer = _
, msgs = _
, bufferModifyTime
, windowWidth
, windowHeight
, searchList
, visualScrollColumn
, startLine
, cursorIdx
, dfa
} = app
in
{ mode = newMode
, buffer = newBuffer
, msgs = newMsgs
, bufferModifyTime = bufferModifyTime
, windowWidth = windowWidth
, windowHeight = windowHeight
, searchList = searchList
, visualScrollColumn = visualScrollColumn
, startLine = startLine
, cursorIdx = cursorIdx
, dfa = dfa
}
end
fun bufferAndSize
( app: app_type
, newBuffer
, newWidth
, newHeight
, newSearchList
, newMsgs
, newBufferModifyTime
, newVisualScrollColumn
) =
let
val
{ mode
, buffer = _
, bufferModifyTime = _
, windowWidth = _
, windowHeight = _
, searchList = _
, visualScrollColumn = _
, msgs = _
, startLine
, cursorIdx
, dfa
} = app
in
{ mode = mode
, buffer = newBuffer
, bufferModifyTime = newBufferModifyTime
, windowWidth = newWidth
, windowHeight = newHeight
, searchList = newSearchList
, visualScrollColumn = newVisualScrollColumn
, msgs = newMsgs
, startLine = startLine
, cursorIdx = cursorIdx
, dfa = dfa
}
end
fun bufferAndCursorIdx
( app: app_type
, newBuffer
, newCursorIdx
, newMode
, newStartLine
, newSearchList
, newMsgs
, newBufferModifyTime
, newVisualScrollColumn
) =
let
val
{ mode = _
, buffer = _
, bufferModifyTime = _
, cursorIdx = _
, startLine = _
, searchList = _
, visualScrollColumn = _
, msgs = _
, windowWidth
, windowHeight
, dfa
} = app
in
{ mode = newMode
, buffer = newBuffer
, bufferModifyTime = newBufferModifyTime
, cursorIdx = newCursorIdx
, startLine = newStartLine
, searchList = newSearchList
, visualScrollColumn = newVisualScrollColumn
, msgs = newMsgs
, windowWidth = windowWidth
, windowHeight = windowHeight
, dfa = dfa
}
end
fun mode (app: app_type, newMode, newMsgs) =
let
val
{ mode = _
, msgs = _
, buffer
, bufferModifyTime
, searchList
, cursorIdx
, windowWidth
, windowHeight
, startLine
, visualScrollColumn
, dfa
} = app
in
{ mode = newMode
, msgs = newMsgs
, buffer = buffer
, bufferModifyTime = bufferModifyTime
, searchList = searchList
, cursorIdx = cursorIdx
, windowWidth = windowWidth
, windowHeight = windowHeight
, startLine = startLine
, visualScrollColumn = visualScrollColumn
, dfa = dfa
}
end
fun modeAndBuffer (app: app_type, newBuffer, newMode, newMsgs) =
let
val
{ mode = _
, msgs = _
, buffer = _
, bufferModifyTime
, searchList
, cursorIdx
, windowWidth
, windowHeight
, startLine
, visualScrollColumn
, dfa
} = app
in
{ mode = newMode
, msgs = newMsgs
, buffer = newBuffer
, bufferModifyTime = bufferModifyTime
, searchList = searchList
, cursorIdx = cursorIdx
, windowWidth = windowWidth
, windowHeight = windowHeight
, startLine = startLine
, visualScrollColumn = visualScrollColumn
, dfa = dfa
}
end
fun searchList (app: app_type, newSearchList, newBuffer, newBufferModifyTime) =
let
val
{ searchList = _
, buffer = _
, bufferModifyTime
, msgs
, mode
, cursorIdx
, windowWidth
, windowHeight
, startLine
, visualScrollColumn
, dfa
} = app
in
{ searchList = newSearchList
, buffer = newBuffer
, bufferModifyTime = newBufferModifyTime
, msgs = msgs
, mode = mode
, cursorIdx = cursorIdx
, windowWidth = windowWidth
, windowHeight = windowHeight
, startLine = startLine
, visualScrollColumn = visualScrollColumn
, dfa = dfa
}
end
end

View File

@@ -0,0 +1,693 @@
structure NormalMode =
struct
(* parsing functions, deciding what to do while we are in normal mode *)
open AppType
open InputMsg
fun switchToNormalSearchMode (app: app_type, caseSensitive) =
NormalSearchFinish.onSearchChanged
(app, "", PersistentVector.empty, 0, 0, caseSensitive, #buffer app)
fun getNumLength (pos, str) =
if pos = String.size str then
pos
else
let val chr = String.sub (str, pos)
in if Char.isDigit chr then getNumLength (pos + 1, str) else pos
end
fun appendChr (app: app_type, chr, str) =
let
val str = str ^ Char.toString chr
val mode = NORMAL_MODE str
in
NormalModeWith.mode (app, mode, [])
end
local
fun loop (app: app_type, cursorIdx, buffer, searchList, count, time) =
if count = 0 then
NormalDelete.finishAfterDeletingBuffer
(app, #cursorIdx app, buffer, searchList, time, [])
else
let
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val lineStart = Cursor.vi0 (buffer, cursorIdx)
val (buffer, searchList) = SearchList.insert
(lineStart, " ", buffer, searchList, #dfa app)
val buffer = LineGap.goToIdx (lineStart, buffer)
val lineEnd = Cursor.viDlr (buffer, lineStart, 1)
val buffer = LineGap.goToIdx (lineEnd, buffer)
val nextLine = Cursor.viL (buffer, lineEnd, 1)
val count = if lineEnd = nextLine then 0 else count - 1
in
loop (app, nextLine, buffer, searchList, count, time)
end
in
fun indnetLine (app: app_type, count, time) =
let val {buffer, searchList, cursorIdx, ...} = app
in loop (app, cursorIdx, buffer, searchList, count, time)
end
end
local
fun loop (cursorIdx, buffer, searchList, dfa, count) =
if count = 0 then
(buffer, searchList)
else
let
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val lineStart = Cursor.vi0 (buffer, cursorIdx)
val firstNonSpaceChr = Cursor.firstNonSpaceChr (buffer, lineStart)
(* delete from buffer *)
val difference = firstNonSpaceChr - lineStart
val deleteLength = Int.min (difference, 2)
val (buffer, searchList) =
if difference = 0 then
(* can't dedent as there is no leading space *)
(buffer, searchList)
else
SearchList.deleteBufferAndSearchList
(lineStart, deleteLength, buffer, searchList, dfa)
(* get next line to dedent *)
val buffer = LineGap.goToIdx (lineStart, buffer)
val lineEnd = Cursor.viDlr (buffer, lineStart, 1)
val buffer = LineGap.goToIdx (lineEnd, buffer)
val nextLine = Cursor.viL (buffer, lineEnd, 1)
val count = if lineEnd = nextLine then 0 else count - 1
in
loop (nextLine, buffer, searchList, dfa, count)
end
in
fun dedentLine (app: app_type, count, time) =
let
open MailboxType
val {cursorIdx, buffer, searchList, dfa, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val lineStart = Cursor.vi0 (buffer, cursorIdx)
val firstNonSpaceChr = Cursor.firstNonSpaceChr (buffer, lineStart)
(* calculate length to delete *)
val difference = firstNonSpaceChr - lineStart
val deleteLength = Int.min (difference, 2)
(* delete once *)
val (buffer, searchList) =
if deleteLength = 0 then
(buffer, searchList)
else
SearchList.deleteBufferAndSearchList
(lineStart, deleteLength, buffer, searchList, dfa)
(* Calculate nextLine and newCursorIdx.
* The cursorIdx might be past the current line after we dedent.
* If it is, we put the cursorIdx at the last char of the line. *)
val buffer = LineGap.goToIdx (lineStart, buffer)
val lineEnd = Cursor.viDlr (buffer, lineStart, 1)
val buffer = LineGap.goToIdx (lineEnd, buffer)
val nextLine = Cursor.viL (buffer, lineEnd, 1)
val newCursorIdx = Int.min (lineEnd, cursorIdx)
val (buffer, searchList) =
if lineEnd = nextLine then
(* at end of file, so we cannot dedent anymore *)
(buffer, searchList)
else
(* dedent remaining lines specified by count *)
loop (nextLine, buffer, searchList, dfa, count - 1)
val buffer = LineGap.goToStart buffer
in
NormalDelete.finishAfterDeletingBuffer
(app, newCursorIdx, buffer, searchList, time, [])
end
end
fun parseGo (count, app, chrCmd) =
case chrCmd of
#"e" => MoveToEndOfPrevWord.move (app, count)
| #"E" => MoveToEndOfPrevWORD.move (app, count)
| #"g" => NormalMove.moveToStart app
| _ => NormalFinish.clearMode app
fun parseChr (app: app_type, count, chr, str, time) =
case chr of
#"h" => MoveViH.move (app, count)
| #"j" => NormalMove.moveCursorDown (app, count)
| #"k" => NormalMove.moveCursorUp (app, count)
| #"l" => MoveViL.move (app, count)
| #"w" => MoveToNextWord.move (app, count)
| #"W" => MoveToNextWORD.move (app, count)
| #"b" => MoveToPrevWord.move (app, count)
| #"B" => MoveToPrevWORD.move (app, count)
| #"e" => MoveToEndOfWord.move (app, count)
| #"E" => MoveToEndOfWORD.move (app, count)
| #"n" => NormalMove.moveToNextMatch (app, count)
| #"N" => NormalMove.moveToPrevMatch (app, count)
| #"z" => NormalFinish.centreToCursor app
(* can only move to start or end of line once
* so hardcode count as 1 *)
| #"0" =>
(* 0 is a bit of a special case.
* If 0 is pressed without any preceding characters,
* then it should move cursor to the start of the line.
* However, if a number was pressed previously before 0 was,
* then this means user is entering a count.
* In that case, we append 0 to the string. *)
if String.size str > 0 then
let
val lastChr = String.sub (str, String.size str - 1)
in
if Char.isDigit lastChr then
let
val chr = Char.toString chr
val str = str ^ chr
val mode = NORMAL_MODE str
in
NormalModeWith.mode (app, mode, [])
end
else
MoveToStartOfLine.move (app, 1)
end
else
MoveToStartOfLine.move (app, 1)
| #"$" => MoveToEndOfLine.move (app, 1)
| #"^" => NormalMove.firstNonSpaceChr app
| #"G" =>
(* if str has a size larger than 0,
* interpret as "go to line" command;
* else, interpret as a command to move to end *)
if String.size str = 0 then NormalMove.moveToEnd app
else NormalMove.moveToLine (app, count)
| #"%" => NormalMove.moveToMatchingPair app
| #"D" => NormalDelete.deleteToEndOfLine (app, time)
| #"x" => NormalDelete.removeChr (app, count, time)
| #"J" => NormalDelete.removeLineBreaks (app, count, time)
| #"/" => switchToNormalSearchMode (app, false)
| #"?" => switchToNormalSearchMode (app, true)
| #">" => indnetLine (app, count, time)
| #"<" => dedentLine (app, count, time)
(* multi-char commands which can be appended *)
| #"t" => appendChr (app, chr, str)
| #"T" => appendChr (app, chr, str)
| #"y" => appendChr (app, chr, str)
| #"d" => appendChr (app, chr, str)
| #"f" => appendChr (app, chr, str)
| #"F" => appendChr (app, chr, str)
| #"g" => appendChr (app, chr, str)
| #"c" => appendChr (app, chr, str)
| _ =>
(* user may be entering a cmd with more than one chr
* such as "2dw" to delete two word
* so add current chr to mode, and save it in the app state *)
let
val str = if Char.isDigit chr then str ^ Char.toString chr else ""
val mode = NORMAL_MODE str
in
NormalModeWith.mode (app, mode, [])
end
structure ParseDelete =
struct
fun parseDeleteInside (app, chr, time) =
case chr of
#"w" => NormalDelete.deleteInsideWord (app, time)
| #"W" => NormalDelete.deleteInsideWORD (app, time)
| #"(" => NormalDelete.deleteInsidePair (app, #"(", #")", time)
| #")" => NormalDelete.deleteInsidePair (app, #"(", #")", time)
| #"[" => NormalDelete.deleteInsidePair (app, #"[", #"]", time)
| #"]" => NormalDelete.deleteInsidePair (app, #"[", #"]", time)
| #"{" => NormalDelete.deleteInsidePair (app, #"{", #"}", time)
| #"}" => NormalDelete.deleteInsidePair (app, #"{", #"}", time)
| #"<" => NormalDelete.deleteInsidePair (app, #"<", #">", time)
| #">" => NormalDelete.deleteInsidePair (app, #"<", #">", time)
| _ => NormalFinish.clearMode app
fun parseDeleteAround (app, chr, time) =
case chr of
#"w" => NormalDelete.deleteAroundWord (app, time)
| #"W" => NormalDelete.deleteAroundWORD (app, time)
| #"(" => NormalDelete.deleteAroundPair (app, #"(", #")", time)
| #")" => NormalDelete.deleteAroundPair (app, #"(", #")", time)
| #"[" => NormalDelete.deleteAroundPair (app, #"[", #"]", time)
| #"]" => NormalDelete.deleteAroundPair (app, #"[", #"]", time)
| #"{" => NormalDelete.deleteAroundPair (app, #"{", #"}", time)
| #"}" => NormalDelete.deleteAroundPair (app, #"{", #"}", time)
| #"<" => NormalDelete.deleteAroundPair (app, #"<", #">", time)
| #">" => NormalDelete.deleteAroundPair (app, #"<", #">", time)
| _ => NormalFinish.clearMode app
fun parseDeleteTerminal (str, count, app, chrCmd, time) =
case chrCmd of
(* terminal commands: require no input after *)
#"h" => NormalDelete.deleteCharsLeft (app, count, time)
| #"l" => NormalDelete.removeChr (app, count, time)
(* vi's 'j' and 'k' commands move up or down a column
* but 'dj' or 'dk' delete whole lines
* so their implementation differs from
* other cursor motions *)
| #"j" => NormalDelete.deleteLineDown (app, count, time)
| #"k" => NormalDelete.deleteLineUp (app, count, time)
| #"w" => NormalDelete.deleteWord (app, count, time)
| #"W" => NormalDelete.deleteWORD (app, count, time)
| #"b" => NormalDelete.deleteByDfa (app, count, Cursor.prevWord, time)
| #"B" => NormalDelete.deleteByDfa (app, count, Cursor.prevWORD, time)
| #"e" =>
NormalDelete.deleteByDfa (app, count, Cursor.endOfWordForDelete, time)
| #"E" =>
NormalDelete.deleteByDfa (app, count, Cursor.endOfWORDForDelete, time)
| #"0" => NormalDelete.delete (app, 1, Cursor.vi0, time)
| #"$" => NormalDelete.deleteToEndOfLine (app, time)
| #"^" => NormalDelete.deleteToFirstNonSpaceChr (app, time)
| #"d" => NormalDelete.deleteLine (app, count, time)
| #"n" => NormalDelete.deleteToNextMatch (app, count, time)
| #"N" => NormalDelete.deleteToPrevMatch (app, count, time)
| #"%" => NormalDelete.deletePair (app, time)
| #"G" => NormalDelete.deleteToEnd (app, time)
(* non-terminal commands which require appending chr *)
| #"t" => appendChr (app, chrCmd, str)
| #"T" => appendChr (app, chrCmd, str)
| #"f" => appendChr (app, chrCmd, str)
| #"F" => appendChr (app, chrCmd, str)
| #"g" => appendChr (app, chrCmd, str)
| #"i" => appendChr (app, chrCmd, str)
| #"a" => appendChr (app, chrCmd, str)
(* invalid command: reset mode *)
| _ => NormalFinish.clearMode app
fun parseDeleteGo (app, count, chrCmd, time) =
case chrCmd of
#"e" => NormalDelete.deleteToEndOfPrevWord (app, count, time)
| #"E" => NormalDelete.deleteToEndOfPrevWORD (app, count, time)
| #"g" => NormalDelete.deleteToStart (app, time)
| _ => NormalFinish.clearMode app
fun parseDelete (strPos, str, count, app, chrCmd, time) =
if strPos = String.size str - 1 then
parseDeleteTerminal (str, count, app, chrCmd, time)
else
(* have to continue parsing string *)
case String.sub (str, strPos + 1) of
#"t" => NormalDelete.deleteTillNextChr (app, count, chrCmd, time)
| #"T" => NormalDelete.deleteTillPrevChr (app, count, chrCmd, time)
| #"f" => NormalDelete.deleteToNextChr (app, count, chrCmd, time)
| #"F" => NormalDelete.deleteToPrevChr (app, count, chrCmd, time)
| #"g" => parseDeleteGo (app, count, chrCmd, time)
| #"i" => parseDeleteInside (app, chrCmd, time)
| #"a" => parseDeleteAround (app, chrCmd, time)
| _ => NormalFinish.clearMode app
end
structure ParseYankDelete =
struct
fun parseDeleteInside (app, chr, time) =
case chr of
#"w" => NormalYankDelete.deleteInsideWord (app, time)
| #"W" => NormalYankDelete.deleteInsideWORD (app, time)
| #"(" => NormalYankDelete.deleteInsidePair (app, #"(", #")", time)
| #")" => NormalYankDelete.deleteInsidePair (app, #"(", #")", time)
| #"[" => NormalYankDelete.deleteInsidePair (app, #"[", #"]", time)
| #"]" => NormalYankDelete.deleteInsidePair (app, #"[", #"]", time)
| #"{" => NormalYankDelete.deleteInsidePair (app, #"{", #"}", time)
| #"}" => NormalYankDelete.deleteInsidePair (app, #"{", #"}", time)
| #"<" => NormalYankDelete.deleteInsidePair (app, #"<", #">", time)
| #">" => NormalYankDelete.deleteInsidePair (app, #"<", #">", time)
| _ => NormalFinish.clearMode app
fun parseDeleteAround (app, chr, time) =
case chr of
#"w" => NormalYankDelete.deleteAroundWord (app, time)
| #"W" => NormalYankDelete.deleteAroundWORD (app, time)
| #"(" => NormalYankDelete.deleteAroundPair (app, #"(", #")", time)
| #")" => NormalYankDelete.deleteAroundPair (app, #"(", #")", time)
| #"[" => NormalYankDelete.deleteAroundPair (app, #"[", #"]", time)
| #"]" => NormalYankDelete.deleteAroundPair (app, #"[", #"]", time)
| #"{" => NormalYankDelete.deleteAroundPair (app, #"{", #"}", time)
| #"}" => NormalYankDelete.deleteAroundPair (app, #"{", #"}", time)
| #"<" => NormalYankDelete.deleteAroundPair (app, #"<", #">", time)
| #">" => NormalYankDelete.deleteAroundPair (app, #"<", #">", time)
| _ => NormalFinish.clearMode app
fun parseDeleteTerminal (str, count, app, chrCmd, time) =
case chrCmd of
(* terminal commands: require no input after *)
#"h" => NormalYankDelete.deleteCharsLeft (app, count, time)
| #"l" => NormalYankDelete.removeChr (app, count, time)
(* vi's 'j' and 'k' commands move up or down a column
* but 'dj' or 'dk' delete whole lines
* so their implementation differs from
* other cursor motions *)
| #"j" => NormalYankDelete.deleteLineDown (app, count, time)
| #"k" => NormalYankDelete.deleteLineUp (app, count, time)
| #"w" => NormalYankDelete.deleteWord (app, count, time)
| #"W" => NormalYankDelete.deleteWORD (app, count, time)
| #"b" => NormalYankDelete.deleteByDfa (app, count, Cursor.prevWord, time)
| #"B" => NormalYankDelete.deleteByDfa (app, count, Cursor.prevWORD, time)
| #"e" =>
NormalYankDelete.deleteByDfa
(app, count, Cursor.endOfWordForDelete, time)
| #"E" =>
NormalYankDelete.deleteByDfa
(app, count, Cursor.endOfWORDForDelete, time)
| #"0" => NormalYankDelete.delete (app, 1, Cursor.vi0, time)
| #"$" => NormalYankDelete.deleteToEndOfLine (app, time)
| #"^" => NormalYankDelete.deleteToFirstNonSpaceChr (app, time)
| #"d" => NormalYankDelete.deleteLine (app, count, time)
| #"n" => NormalYankDelete.deleteToNextMatch (app, count, time)
| #"N" => NormalYankDelete.deleteToPrevMatch (app, count, time)
| #"%" => NormalYankDelete.deletePair (app, time)
| #"G" => NormalYankDelete.deleteToEnd (app, time)
(* non-terminal commands which require appending chr *)
| #"t" => appendChr (app, chrCmd, str)
| #"T" => appendChr (app, chrCmd, str)
| #"f" => appendChr (app, chrCmd, str)
| #"F" => appendChr (app, chrCmd, str)
| #"g" => appendChr (app, chrCmd, str)
| #"i" => appendChr (app, chrCmd, str)
| #"a" => appendChr (app, chrCmd, str)
(* invalid command: reset mode *)
| _ => NormalFinish.clearMode app
fun parseDeleteGo (app, count, chrCmd, time) =
case chrCmd of
#"e" => NormalYankDelete.deleteToEndOfPrevWord (app, count, time)
| #"E" => NormalYankDelete.deleteToEndOfPrevWORD (app, count, time)
| #"g" => NormalYankDelete.deleteToStart (app, time)
| _ => NormalFinish.clearMode app
fun parseDelete (strPos, str, count, app, chrCmd, time) =
if strPos = String.size str - 1 then
parseDeleteTerminal (str, count, app, chrCmd, time)
else
(* have to continue parsing string *)
case String.sub (str, strPos + 1) of
#"t" => NormalYankDelete.deleteTillNextChr (app, count, chrCmd, time)
| #"T" => NormalYankDelete.deleteTillPrevChr (app, count, chrCmd, time)
| #"f" => NormalYankDelete.deleteToNextChr (app, count, chrCmd, time)
| #"F" => NormalYankDelete.deleteToPrevChr (app, count, chrCmd, time)
| #"g" => parseDeleteGo (app, count, chrCmd, time)
| #"i" => parseDeleteInside (app, chrCmd, time)
| #"a" => parseDeleteAround (app, chrCmd, time)
| _ => NormalFinish.clearMode app
end
structure ParseYank =
struct
fun yankWhenMovingBack (app: app_type, fMove, count) =
let
open DrawMsg
open MailboxType
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val low = fMove (buffer, cursorIdx, count)
val length = cursorIdx - low
val str = LineGap.substring (low, length, buffer)
val msg = YANK str
val mode = NORMAL_MODE ""
in
NormalModeWith.modeAndBuffer (app, buffer, mode, [DRAW msg])
end
fun yankWhenMovingForward (app: app_type, fMove, count) =
let
open DrawMsg
open MailboxType
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val high = fMove (buffer, cursorIdx, count)
val buffer = LineGap.goToIdx (high, buffer)
val length = high - cursorIdx
val str = LineGap.substring (cursorIdx, length, buffer)
val msg = YANK str
val mode = NORMAL_MODE ""
in
NormalModeWith.modeAndBuffer (app, buffer, mode, [DRAW msg])
end
fun parseYankTerminal (str, count, app, chrCmd, time) =
case chrCmd of
#"h" => NormalYank.yankLeft (app, count)
| #"k" => NormalYank.yankLineUp (app, count)
| #"j" => NormalYank.yankLineDown (app, count)
| #"l" => NormalYank.yankRight (app, count)
| #"y" => NormalYank.yankLine (app, count)
| #"0" => NormalYank.yankToStartOfLine app
| #"w" => NormalYank.yankWhenMovingForward (app, Cursor.nextWord, count)
| #"W" => NormalYank.yankWhenMovingForward (app, Cursor.nextWORD, count)
| #"b" => NormalYank.yankWhenMovingBack (app, Cursor.prevWord, count)
| #"B" => NormalYank.yankWhenMovingBack (app, Cursor.prevWORD, count)
| #"e" =>
NormalYank.yankWhenMovingForward
(app, Cursor.endOfWordForDelete, count)
| #"E" =>
NormalYank.yankWhenMovingForward
(app, Cursor.endOfWORDForDelete, count)
| #"$" => NormalYank.yankWhenMovingForward (app, Cursor.viDlr, 1)
| #"^" => NormalYank.yankToFirstNonSpaceChr app
| #"G" => NormalYank.yankToEndOfText app
| #"%" => NormalYank.yankToMatchingPair app
| #"n" => NormalYank.yankToNextMatch (app, count)
| #"N" => NormalYank.yankToPrevMatch (app, count)
| #"x" => NormalYankDelete.removeChr (app, count, time)
(* append non-terminal characters to string *)
| #"d" =>
let (* 'yd' motion, like 'ydw'; meant to be 'yank then delete' *)
in appendChr (app, chrCmd, str)
end
| #"t" => appendChr (app, chrCmd, str)
| #"T" => appendChr (app, chrCmd, str)
| #"f" => appendChr (app, chrCmd, str)
| #"F" => appendChr (app, chrCmd, str)
| #"g" => appendChr (app, chrCmd, str)
| #"i" => appendChr (app, chrCmd, str)
| #"a" => appendChr (app, chrCmd, str)
| _ => NormalFinish.clearMode app
fun parseYankGo (count, app, chrCmd) =
case chrCmd of
#"e" =>
NormalYank.yankWhenMovingBackPlusOne
(app, Cursor.endOfPrevWord, count)
| #"E" =>
NormalYank.yankWhenMovingBackPlusOne
(app, Cursor.endOfPrevWORD, count)
| #"g" => NormalYank.yankToStart app
| _ => NormalFinish.clearMode app
fun parseYankInside (app, chr) =
case chr of
#"w" => NormalYank.yankInsideWord app
| #"W" => NormalYank.yankInsideWORD app
| #"(" => NormalYank.yankInsideChrOpen (app, chr)
| #"[" => NormalYank.yankInsideChrOpen (app, chr)
| #"{" => NormalYank.yankInsideChrOpen (app, chr)
| #"<" => NormalYank.yankInsideChrOpen (app, chr)
| #")" => NormalYank.yankInsideChrClose (app, chr)
| #"]" => NormalYank.yankInsideChrClose (app, chr)
| #"}" => NormalYank.yankInsideChrClose (app, chr)
| #">" => NormalYank.yankInsideChrClose (app, chr)
| _ => NormalFinish.clearMode app
fun parseYankAround (app, chr) =
case chr of
#"(" => NormalYank.yankAroundChrOpen (app, chr)
| #"[" => NormalYank.yankAroundChrOpen (app, chr)
| #"{" => NormalYank.yankAroundChrOpen (app, chr)
| #"<" => NormalYank.yankAroundChrOpen (app, chr)
| #")" => NormalYank.yankAroundChrClose (app, chr)
| #"]" => NormalYank.yankAroundChrClose (app, chr)
| #"}" => NormalYank.yankAroundChrClose (app, chr)
| #">" => NormalYank.yankAroundChrClose (app, chr)
| _ => NormalFinish.clearMode app
fun parseYank (strPos, str, count, app, chrCmd, time) =
if strPos = String.size str - 1 then
parseYankTerminal (str, count, app, chrCmd, time)
else
case String.sub (str, strPos + 1) of
#"t" => NormalYank.yankTillNextChr (app, count, chrCmd)
| #"T" => NormalYank.yankTillPrevChr (app, count, chrCmd)
| #"f" => NormalYank.yankToNextChr (app, count, chrCmd)
| #"F" => NormalYank.yankToPrevChr (app, count, chrCmd)
| #"g" => parseYankGo (count, app, chrCmd)
| #"i" => parseYankInside (app, chrCmd)
| #"a" => parseYankAround (app, chrCmd)
| #"d" =>
ParseYankDelete.parseDelete
(strPos + 1, str, count, app, chrCmd, time)
| _ => NormalFinish.clearMode app
end
(* useful reference as list of non-terminal commands *)
fun parseAfterCount (strPos, str, count, app, chrCmd, time) =
(* we are trying to parse multi-char but non-terminal strings here.
* For example, we don't want to parse 3w which is a terminal commmand
* to go 3 words forwards
* but we do want to parse 3d which is a non-terminal command
* which can be made terminal by adding "w" or "e" at the end.
* *)
case String.sub (str, strPos) of
#"t" => NormalMove.tillNextChr (app, count, chrCmd)
| #"T" => NormalMove.tillPrevChr (app, count, chrCmd)
| #"y" => ParseYank.parseYank (strPos, str, count, app, chrCmd, time)
| #"d" => ParseDelete.parseDelete (strPos, str, count, app, chrCmd, time)
| #"f" => NormalMove.toNextChr (app, count, chrCmd)
| #"F" => NormalMove.toPrevChr (app, count, chrCmd)
| #"g" => (* go *) parseGo (count, app, chrCmd)
| #"c" => (* change *) NormalFinish.clearMode app
| _ =>
(* isn't a non-terminal cmd
* this case should never happen*)
NormalFinish.clearMode app
fun parseNormalModeCommand (app, str, chrCmd, time) =
if String.size str = 0 then
parseChr (app, 1, chrCmd, str, time)
else if String.size str = 1 then
case Int.fromString str of
SOME count => parseChr (app, count, chrCmd, str, time)
| NONE => parseAfterCount (0, str, 1, app, chrCmd, time)
else
let
val numLength = getNumLength (0, str)
val count = String.substring (str, 0, numLength)
val count =
case Int.fromString count of
SOME x => x
| NONE => 1
in
if numLength = String.size str then
(* reached end of str; str only contained numbers *)
parseChr (app, count, chrCmd, str, time)
else
(* continue parsing. *)
parseAfterCount (numLength, str, count, app, chrCmd, time)
end
structure LeftArrow =
struct
fun parseLeftArrowCommand (strPos, str, count, app, time) =
case String.sub (str, strPos) of
#"y" =>
if strPos + 1 = String.size str then
(* terminal command, so simple yank *)
raise Fail "left-arrow-yank unimplemnted"
else
(case String.sub (str, strPos + 1) of
#"d" => NormalYankDelete.deleteCharsLeft (app, count, time)
| _ => NormalFinish.clearMode app)
| #"d" => NormalDelete.deleteCharsLeft (app, count, time)
| _ => NormalFinish.clearMode app
fun parse (app, str, time) =
if String.size str = 0 then
MoveViH.move (app, 1)
else if String.size str = 1 then
case Int.fromString str of
SOME count => MoveViH.move (app, count)
| NONE => parseLeftArrowCommand (0, str, 1, app, time)
else
let
val numLength = getNumLength (0, str)
val count = String.substring (str, 0, numLength)
val count =
case Int.fromString count of
SOME x => x
| NONE => 1
in
if numLength = String.size str then
(* reached end of string; string only contained numbers *)
MoveViH.move (app, count)
else
parseLeftArrowCommand (numLength, str, count, app, time)
end
end
structure RightArrow =
struct
fun parseRightArrowCommand (strPos, str, count, app, time) =
case String.sub (str, strPos) of
#"y" =>
if strPos + 1 = String.size str then
raise Fail "right-arrow-yank unimplemnted"
else
(case String.sub (str, strPos + 1) of
#"d" => NormalYankDelete.removeChr (app, count, time)
| _ => NormalFinish.clearMode app)
| #"d" => NormalDelete.removeChr (app, count, time)
| _ => NormalFinish.clearMode app
fun parse (app, str, time) =
if String.size str = 0 then
MoveViL.move (app, 1)
else if String.size str = 1 then
case Int.fromString str of
SOME count => MoveViL.move (app, count)
| NONE => parseRightArrowCommand (0, str, 1, app, time)
else
let
val numLength = getNumLength (0, str)
val count = String.substring (str, 0, numLength)
val count =
case Int.fromString count of
SOME x => x
| NONE => 1
in
if numLength = String.size str then
(* reached end of string; string only contained numbers *)
MoveViH.move (app, count)
else
parseRightArrowCommand (numLength, str, count, app, time)
end
end
fun update (app, str, msg, time) =
case msg of
CHAR_EVENT chrCmd => parseNormalModeCommand (app, str, chrCmd, time)
| KEY_ESC => NormalFinish.clearMode app
| RESIZE_EVENT (width, height) =>
NormalFinish.resizeText (app, width, height)
| ARROW_RIGHT => RightArrow.parse (app, str, time)
| ARROW_LEFT => LeftArrow.parse (app, str, time)
| ARROW_UP => NormalFinish.clearMode app
| ARROW_DOWN => NormalFinish.clearMode app
| KEY_ENTER => NormalFinish.clearMode app
| KEY_BACKSPACE => NormalFinish.clearMode app
end

View File

@@ -0,0 +1,586 @@
structure NormalMove =
struct
open AppType
fun moveToStart (app: app_type) : AppType.app_type =
let
val
{ buffer
, windowWidth
, windowHeight
, searchList
, bufferModifyTime
, visualScrollColumn
, ...
} = app
val cursorIdx = 0
val startLine = 0
val buffer = LineGap.goToStart buffer
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, 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
, 0
)
end
fun moveToEnd (app: app_type) =
let
val
{ buffer
, windowWidth
, windowHeight
, searchList
, bufferModifyTime
, visualScrollColumn = prevScrollColumn
, startLine = prevLineNumber
, ...
} = app
val buffer = LineGap.goToEnd buffer
val {line = bufferLine, textLength, ...} = buffer
val bufferIdx = Int.max (0, textLength - 1)
val bufferLine = bufferLine - 1
val buffer = LineGap.goToIdx (bufferIdx, buffer)
val bufferIdx =
if Cursor.isOnNewlineAfterChr (buffer, bufferIdx) then
Int.max (0, bufferIdx - 1)
else
bufferIdx
val buffer = LineGap.goToIdx (bufferIdx, buffer)
val visualScrollColumn =
TextScroll.getScrollColumn
(buffer, bufferIdx, windowWidth, prevScrollColumn)
val bufferLine =
TextScroll.getStartLine
(prevLineNumber, bufferLine, windowHeight, #lineLength buffer)
val buffer = LineGap.goToLine (bufferLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( bufferLine
, bufferIdx
, buffer
, windowWidth
, windowHeight
, searchList
, 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
, bufferIdx
, mode
, bufferLine
, searchList
, drawMsg
, bufferModifyTime
, visualScrollColumn
)
end
fun finishMoveCursorUpDown
(app: app_type, newCursorLineNumber, buffer, column, lineIdx) =
let
val
{ windowWidth
, windowHeight
, visualScrollColumn = prevScrollColumn
, startLine = prevLineNumber
, searchList
, bufferModifyTime
, ...
} = app
val buffer = LineGap.goToIdx (lineIdx, buffer)
val endOfLineIdx = Cursor.viDlr (buffer, lineIdx, 1)
val endOfLineIdx =
if endOfLineIdx >= #textLength buffer - 1 then
Int.max (0, #textLength buffer - 1)
else
endOfLineIdx
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
, #lineLength buffer
)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, 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 moveCursorUp (app: app_type, count) =
let
val {cursorIdx, buffer, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val startOfLine = Cursor.vi0 (buffer, cursorIdx)
in
if Cursor.isCursorAtStartOfLine (buffer, cursorIdx) then
let
val cursorLineNumber = LineGap.idxToLineNumber (cursorIdx + 1, buffer)
val newCursorLineNumber = Int.max (0, cursorLineNumber - count)
val buffer = LineGap.goToLine (newCursorLineNumber, buffer)
val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer)
val lineIdx =
if Cursor.isPrevChrStartOfLine (buffer, lineIdx) then lineIdx
else lineIdx - 1
val buffer = LineGap.goToIdx (lineIdx, buffer)
val lineIdx = Cursor.vi0 (buffer, lineIdx)
val lineIdx = Int.max (0, lineIdx)
in
finishMoveCursorUpDown (app, newCursorLineNumber, buffer, 0, lineIdx)
end
else
let
val cursorLineNumber = LineGap.idxToLineNumber (cursorIdx, buffer)
val newCursorLineNumber = Int.max (cursorLineNumber - count, 0)
val buffer = LineGap.goToLine (newCursorLineNumber, buffer)
val lineIdx =
LineGap.lineNumberToIdx (newCursorLineNumber, buffer) + 1
val column = cursorIdx - startOfLine
val column = if newCursorLineNumber = 0 then column - 1 else column
in
finishMoveCursorUpDown
(app, newCursorLineNumber, buffer, column, lineIdx)
end
end
fun moveCursorDown (app: app_type, count) =
let
val
{ windowWidth
, windowHeight
, cursorIdx
, buffer
, startLine = prevLineNumber
, searchList
, 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
in
if Cursor.isCursorAtStartOfLine (buffer, cursorIdx) then
let
val cursorLineNumber = LineGap.idxToLineNumber (cursorIdx + 1, buffer)
val newCursorLineNumber = cursorLineNumber + count
val buffer = LineGap.goToLine (newCursorLineNumber, buffer)
val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer)
val lineIdx =
if Cursor.isPrevChrStartOfLine (buffer, lineIdx) then lineIdx
else lineIdx - 1
val buffer = LineGap.goToIdx (lineIdx, buffer)
val lineIdx = Cursor.vi0 (buffer, lineIdx)
val lineIdx =
if lineIdx >= #textLength buffer - 1 then
Int.max (0, #textLength buffer - 1)
else
lineIdx
in
finishMoveCursorUpDown (app, newCursorLineNumber, buffer, 0, lineIdx)
end
else
let
val cursorLineNumber = LineGap.idxToLineNumber (cursorIdx, buffer)
val newCursorLineNumber = cursorLineNumber + count
val buffer = LineGap.goToLine (newCursorLineNumber, buffer)
val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer)
val buffer = LineGap.goToIdx (lineIdx, buffer)
in
if lineIdx >= #textLength buffer - 1 then
(* we reached last line *)
let
val lineIdx = Int.max (#textLength buffer - 1, 0)
val buffer = LineGap.goToIdx (lineIdx, buffer)
val lineIdx =
if Cursor.isOnNewlineAfterChr (buffer, lineIdx) then lineIdx - 1
else lineIdx
val startOfLine = Cursor.vi0 (buffer, lineIdx)
in
if cursorIdx >= startOfLine then
(* we are already on last line so don't move *)
NormalFinish.buildTextAndClear
(app, buffer, cursorIdx, searchList, [], bufferModifyTime)
else
finishMoveCursorUpDown
(app, newCursorLineNumber, buffer, column, startOfLine)
end
else
let
val lineIdx =
if lineIdx >= #textLength buffer - 2 then
Int.max (0, #textLength buffer - 2)
else
lineIdx
val buffer = LineGap.goToIdx (lineIdx, buffer)
val lineIdx =
if Cursor.isOnNewlineAfterChr (buffer, lineIdx) then lineIdx + 1
else lineIdx
in
finishMoveCursorUpDown
(app, newCursorLineNumber, buffer, column, lineIdx)
end
end
end
fun moveToLine (app: app_type, reqLine) =
let
val reqLine = reqLine - 1
in
if reqLine = 0 then
moveToStart app
else
let
val
{ windowWidth
, windowHeight
, buffer
, startLine = prevLineNumber
, searchList
, bufferModifyTime
, visualScrollColumn = prevScrollColumn
, ...
} = app
val buffer = LineGap.goToLine (reqLine, buffer)
(* get idx of first chr after linebreak *)
val cursorIdx = LineGap.lineNumberToIdx (reqLine, buffer)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
(* we got the line start idx, but we want to move to the index
* after it, where the first character of the line is.
* Unless the next character is a line break,
* in which case we want to stay at the current idx. *)
val cursorIdx =
if Cursor.isNextChrEndOfLine (buffer, cursorIdx) then cursorIdx
else cursorIdx + 1
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val visualScrollColumn =
TextScroll.getScrollColumn
(buffer, cursorIdx, windowWidth, prevScrollColumn)
val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer)
val startLine =
TextScroll.getStartLine
(prevLineNumber, cursorLine, windowHeight, #lineLength buffer)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, 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
end
fun moveToMatchingPair (app: app_type) =
let
val
{ buffer
, cursorIdx
, windowWidth
, windowHeight
, startLine = prevLineNumber
, searchList
, bufferModifyTime
, visualScrollColumn = prevScrollColumn
, ...
} = app
(* move LineGap and buffer to start of line *)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val cursorIdx = Cursor.nextPairChr (buffer, cursorIdx)
in
if cursorIdx = ~1 then
NormalFinish.clearMode app
else
let
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val cursorIdx = Cursor.matchPair (buffer, cursorIdx)
in
if cursorIdx = ~1 then
NormalFinish.clearMode app
else
let
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val visualScrollColumn =
TextScroll.getScrollColumn
(buffer, cursorIdx, windowWidth, prevScrollColumn)
val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer)
val startLine =
TextScroll.getStartLine
(prevLineNumber, cursorLine, windowHeight, #lineLength buffer)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
val drawMsg = DrawMsg.DRAW_TEXT drawMsg
val drawMsg = [MailboxType.DRAW drawMsg]
in
NormalModeWith.bufferAndCursorIdx
( app
, buffer
, cursorIdx
, NORMAL_MODE ""
, startLine
, searchList
, drawMsg
, bufferModifyTime
, visualScrollColumn
)
end
end
end
fun firstNonSpaceChr (app: app_type) =
let
val
{ buffer
, cursorIdx
, windowWidth
, windowHeight
, startLine
, searchList
, bufferModifyTime
, ...
} = app
(* move LineGap and buffer to start of line *)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val cursorIdx = Cursor.vi0 (buffer, cursorIdx)
(* move cursorIdx to first character on line *)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val cursorIdx = Cursor.firstNonSpaceChr (buffer, cursorIdx)
in
NormalFinish.buildTextAndClear
(app, buffer, cursorIdx, searchList, [], bufferModifyTime)
end
fun helpMoveToChr (app: app_type, buffer, cursorIdx, count, fMove, chr) =
if count = 0 then
NormalFinish.buildTextAndClear
(app, buffer, cursorIdx, #searchList app, [], #bufferModifyTime app)
else
let
(* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx = fMove (buffer, cursorIdx, chr)
val newCount = if cursorIdx = newCursorIdx then 0 else count - 1
in
helpMoveToChr (app, buffer, newCursorIdx, newCount, fMove, chr)
end
fun moveToChr (app: app_type, count, fMove, chr) =
let val {cursorIdx, buffer, ...} = app
in helpMoveToChr (app, buffer, cursorIdx, count, fMove, chr)
end
fun moveToNextMatch (app: app_type, count) =
let
val
{ cursorIdx
, searchList
, buffer
, bufferModifyTime
, visualScrollColumn
, ...
} = app
val newCursorIdx =
PersistentVector.nextMatch (cursorIdx, searchList, count)
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
NormalFinish.buildTextAndClear
(app, buffer, newCursorIdx, searchList, [], bufferModifyTime)
end
fun moveToPrevMatch (app: app_type, count) =
let
val {cursorIdx, searchList, buffer, bufferModifyTime, ...} = app
val newCursorIdx =
PersistentVector.prevMatch (cursorIdx, searchList, count)
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
NormalFinish.buildTextAndClear
(app, buffer, newCursorIdx, searchList, [], bufferModifyTime)
end
fun toNextChr (app: app_type, count, chr) =
let
val {cursorIdx, buffer, searchList, bufferModifyTime, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx =
Cursor.toNextChr (buffer, cursorIdx, {findChr = chr, count = count})
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
NormalFinish.buildTextAndClear
(app, buffer, newCursorIdx, searchList, [], bufferModifyTime)
end
fun tillNextChr (app: app_type, count, chr) =
let
val {cursorIdx, buffer, searchList, bufferModifyTime, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx =
Cursor.toNextChr (buffer, cursorIdx, {findChr = chr, count = count})
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
NormalFinish.buildTextAndClear
(app, buffer, newCursorIdx - 1, searchList, [], bufferModifyTime)
end
fun toPrevChr (app: app_type, count, chr) =
let
val {cursorIdx, buffer, searchList, bufferModifyTime, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx =
Cursor.toPrevChr (buffer, cursorIdx, {findChr = chr, count = count})
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
NormalFinish.buildTextAndClear
(app, buffer, newCursorIdx, searchList, [], bufferModifyTime)
end
fun tillPrevChr (app: app_type, count, chr) =
let
val {cursorIdx, buffer, searchList, bufferModifyTime, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx =
Cursor.toPrevChr (buffer, cursorIdx, {findChr = chr, count = count})
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
NormalFinish.buildTextAndClear
(app, buffer, newCursorIdx + 1, searchList, [], bufferModifyTime)
end
end

View File

@@ -0,0 +1,156 @@
structure NormalSearchFinish =
struct
open AppType
open DrawMsg
fun onSearchChanged
( app: app_type
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
, buffer
) =
let
val
{ buffer
, cursorIdx
, startLine = prevLineNumber
, windowWidth
, windowHeight
, visualScrollColumn
, ...
} = app
val searchScrollColumn =
TextScroll.getScrollColumnFromString
(searchCursorIdx, windowWidth, searchScrollColumn)
val mode = NORMAL_SEARCH_MODE
{ searchString = searchString
, tempSearchList = tempSearchList
, searchCursorIdx = searchCursorIdx
, searchScrollColumn = searchScrollColumn
, caseSensitive = caseSensitive
}
val floatWindowWidth = Real32.fromInt windowWidth
val floatWindowHeight = Real32.fromInt windowHeight
val searchStringPosY = windowHeight - TextConstants.ySpace - 5
val initialTextAcc = SearchBar.build
( searchString
, 5
, searchStringPosY
, windowWidth
, floatWindowWidth
, floatWindowHeight
, searchCursorIdx
, searchScrollColumn
, caseSensitive
)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer)
val startLine =
TextScroll.getStartLine
(prevLineNumber, cursorLine, windowHeight, #lineLength buffer)
val buffer = LineGap.goToLine (startLine, buffer)
val remainingWindowHeight = windowHeight - (TextConstants.ySpace * 2)
val drawMsg = NormalModeTextBuilder.startBuild
( startLine
, cursorIdx
, buffer
, windowWidth
, remainingWindowHeight
, floatWindowWidth
, floatWindowHeight
, tempSearchList
, visualScrollColumn
, initialTextAcc
)
val drawMsg = Vector.concat drawMsg
val drawMsg = DrawMsg.DRAW_TEXT drawMsg
val msgs = [MailboxType.DRAW drawMsg]
in
NormalSearchModeWith.changeTempSearchString
(app, buffer, startLine, mode, msgs)
end
fun resize
( app: app_type
, newWindowWidth
, newWindowHeight
, searchString
, searchCursorIdx
, tempSearchList
, searchScrollColumn
, caseSensitive
) =
let
val
{buffer, cursorIdx, startLine = prevLineNumber, visualScrollColumn, ...} =
app
val floatWindowWidth = Real32.fromInt newWindowWidth
val floatWindowHeight = Real32.fromInt newWindowHeight
val searchScrollColumn =
TextScroll.getScrollColumnFromString
(searchCursorIdx, newWindowWidth, searchScrollColumn)
val mode = NORMAL_SEARCH_MODE
{ searchString = searchString
, tempSearchList = tempSearchList
, searchCursorIdx = searchCursorIdx
, searchScrollColumn = searchScrollColumn
, caseSensitive = caseSensitive
}
val searchStringPosY = newWindowHeight - TextConstants.ySpace - 5
val initialTextAcc = SearchBar.build
( searchString
, 5
, searchStringPosY
, newWindowWidth
, floatWindowWidth
, floatWindowHeight
, searchCursorIdx
, searchScrollColumn
, caseSensitive
)
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val cursorLine = LineGap.idxToLineNumber (cursorIdx, buffer)
val startLine =
TextScroll.getStartLine
(prevLineNumber, cursorLine, newWindowHeight, #lineLength buffer)
val buffer = LineGap.goToLine (startLine, buffer)
val remainingWindowHeight = newWindowHeight - (TextConstants.ySpace * 2)
val drawMsg = NormalModeTextBuilder.startBuild
( startLine
, cursorIdx
, buffer
, newWindowWidth
, remainingWindowHeight
, floatWindowWidth
, floatWindowHeight
, tempSearchList
, visualScrollColumn
, initialTextAcc
)
val drawMsg = Vector.concat drawMsg
val drawMsg = DrawMsg.DRAW_TEXT drawMsg
val msgs = [MailboxType.DRAW drawMsg]
in
NormalSearchModeWith.bufferAndSize
(app, mode, buffer, newWindowWidth, newWindowHeight, msgs)
end
end

View File

@@ -0,0 +1,140 @@
structure NormalSearchModeWith =
struct
open AppType
fun returnToNormalMode
( app: app_type
, newBuffer
, newSearchList
, newStartLine
, newMode
, newDfa
, newMsgs
) =
let
val
{ mode = _
, buffer = _
, searchList = _
, startLine = _
, msgs = _
, dfa = _
, bufferModifyTime
, windowWidth
, windowHeight
, cursorIdx
, visualScrollColumn
} = app
in
{ mode = newMode
, buffer = newBuffer
, searchList = newSearchList
, startLine = newStartLine
, dfa = newDfa
, bufferModifyTime = bufferModifyTime
, msgs = newMsgs
, windowWidth = windowWidth
, windowHeight = windowHeight
, cursorIdx = cursorIdx
, visualScrollColumn = visualScrollColumn
}
end
fun changeTempSearchString
(app: app_type, newBuffer, newStartLine, newMode, newMsgs) =
let
val
{ mode = _
, buffer = _
, searchList
, startLine = _
, msgs = _
, bufferModifyTime
, windowWidth
, windowHeight
, cursorIdx
, visualScrollColumn
, dfa
} = app
in
{ mode = newMode
, buffer = newBuffer
, startLine = newStartLine
, msgs = newMsgs
, searchList = searchList
, bufferModifyTime = bufferModifyTime
, windowWidth = windowWidth
, windowHeight = windowHeight
, cursorIdx = cursorIdx
, visualScrollColumn = visualScrollColumn
, dfa = dfa
}
end
fun searchList (app: app_type, newSearchList) =
let
val
{ mode
, buffer
, searchList = _
, startLine
, msgs
, bufferModifyTime
, windowWidth
, windowHeight
, cursorIdx
, visualScrollColumn
, dfa
} = app
in
{ mode = mode
, searchList = newSearchList
, buffer = buffer
, startLine = startLine
, msgs = msgs
, bufferModifyTime = bufferModifyTime
, windowWidth = windowWidth
, windowHeight = windowHeight
, cursorIdx = cursorIdx
, visualScrollColumn = visualScrollColumn
, dfa = dfa
}
end
fun bufferAndSize
( app: app_type
, newMode
, newBuffer
, newWindowWidth
, newWindowHeight
, newMsgs
) =
let
val
{ mode = _
, windowWidth = _
, windowHeight = _
, msgs = _
, buffer = _
, searchList
, startLine
, bufferModifyTime
, cursorIdx
, visualScrollColumn
, dfa
} = app
in
{ mode = newMode
, buffer = newBuffer
, windowWidth = newWindowWidth
, windowHeight = newWindowHeight
, msgs = newMsgs
, searchList = searchList
, startLine = startLine
, bufferModifyTime = bufferModifyTime
, cursorIdx = cursorIdx
, visualScrollColumn = visualScrollColumn
, dfa = dfa
}
end
end

View File

@@ -0,0 +1,274 @@
structure NormalSearchMode =
struct
open AppType
open InputMsg
open MailboxType
fun buildTempSearchList (searchString, buffer, cursorIdx, caseSensitive) =
let
val dfa =
if caseSensitive then CaseSensitiveDfa.fromString searchString
else CaseInsensitiveDfa.fromString searchString
in
SearchList.buildRange (buffer, cursorIdx + 1111, dfa)
end
fun addChr
( app: app_type
, searchString
, searchCursorIdx
, searchScrollColumn
, caseSensitive
, chr
) =
let
val {cursorIdx, buffer, ...} = app
val c = String.implode [chr]
val searchString =
if searchCursorIdx = String.size searchString then
searchString ^ c
else
let
val sub1 = Substring.extract (searchString, 0, SOME searchCursorIdx)
val sub2 = Substring.full c
val sub3 = Substring.extract (searchString, searchCursorIdx, NONE)
in
Substring.concat [sub1, sub2, sub3]
end
val searchCursorIdx = searchCursorIdx + 1
val buffer = LineGap.goToIdx (cursorIdx - 1111, buffer)
val (buffer, tempSearchList) =
buildTempSearchList (searchString, buffer, cursorIdx, caseSensitive)
in
NormalSearchFinish.onSearchChanged
( app
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
, buffer
)
end
(* return to normal mode, keeping the same searchString and searchList
* from before entering this mode. *)
fun exitToNormalMode (app: app_type) =
let
val {buffer, cursorIdx, searchList, bufferModifyTime, ...} = app
in
NormalFinish.buildTextAndClear
(app, buffer, cursorIdx, searchList, [], bufferModifyTime)
end
(* save search string and searchList and return to normal mode *)
fun saveSearch (app: app_type, searchString, caseSensitive, time) =
let
val
{ buffer
, cursorIdx
, windowWidth
, windowHeight
, startLine
, visualScrollColumn
, ...
} = app
val dfa =
if caseSensitive then CaseSensitiveDfa.fromString searchString
else CaseInsensitiveDfa.fromString searchString
val buffer = LineGap.goToStart buffer
val (buffer, searchList) = SearchList.build (buffer, dfa)
(* move LineGap to first line displayed on screen *)
val buffer = LineGap.goToLine (startLine, buffer)
(* move buffer to new startLine as required by TextBuilder.build *)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = NormalModeTextBuilder.build
( startLine
, cursorIdx
, buffer
, windowWidth
, windowHeight
, searchList
, visualScrollColumn
)
val drawMsg = Vector.concat drawMsg
val drawMsg = DrawMsg.DRAW_TEXT drawMsg
val msgs = [DRAW drawMsg]
val mode = NORMAL_MODE ""
in
NormalSearchModeWith.returnToNormalMode
(app, buffer, searchList, startLine, mode, dfa, msgs)
end
fun backspace
( app: app_type
, searchString
, tempSearchList
, searchScrollColumn
, searchCursorIdx
, caseSensitive
) =
if searchCursorIdx = 0 then
app
else
let
val searchString =
if searchCursorIdx = String.size searchString then
String.substring (searchString, 0, String.size searchString - 1)
else
let
val sub1 = Substring.extract
(searchString, 0, SOME (searchCursorIdx - 1))
val sub2 = Substring.extract (searchString, searchCursorIdx, SOME
(String.size searchString - searchCursorIdx))
in
Substring.concat [sub1, sub2]
end
val searchCursorIdx = searchCursorIdx - 1
val {cursorIdx, buffer, ...} = app
val buffer = LineGap.goToIdx (cursorIdx - 1111, buffer)
val (buffer, tempSearchList) =
buildTempSearchList (searchString, buffer, cursorIdx, caseSensitive)
in
NormalSearchFinish.onSearchChanged
( app
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
, buffer
)
end
fun moveLeft
( app
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
) =
if searchCursorIdx = 0 then
app
else
let
val searchCursorIdx = Int.max (0, searchCursorIdx - 1)
in
NormalSearchFinish.onSearchChanged
( app
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
, #buffer app
)
end
fun moveRight
( app
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
) =
if searchCursorIdx = String.size searchString then
app
else
let
val searchCursorIdx =
Int.min (searchCursorIdx + 1, String.size searchString)
in
NormalSearchFinish.onSearchChanged
( app
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
, #buffer app
)
end
fun update
( app
, { searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
}
, msg
, time
) =
case msg of
CHAR_EVENT chr =>
addChr
( app
, searchString
, searchCursorIdx
, searchScrollColumn
, caseSensitive
, chr
)
| KEY_BACKSPACE =>
backspace
( app
, searchString
, tempSearchList
, searchScrollColumn
, searchCursorIdx
, caseSensitive
)
| KEY_ESC => exitToNormalMode app
| KEY_ENTER => saveSearch (app, searchString, caseSensitive, time)
| ARROW_LEFT =>
moveLeft
( app
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
)
| ARROW_RIGHT =>
moveRight
( app
, searchString
, tempSearchList
, searchCursorIdx
, searchScrollColumn
, caseSensitive
)
| RESIZE_EVENT (width, height) =>
NormalSearchFinish.resize
( app
, width
, height
, searchString
, searchCursorIdx
, tempSearchList
, searchScrollColumn
, caseSensitive
)
(* In Vim's search mode, the up and down arrows can be used
* to scroll through the search history.
* I don't find this feature too useful as it is often easier to type
* the whole search string again, so I'm leaving it unimplemented
* until/unless I find that I wish this functionality was there
* while using the program. *)
| ARROW_UP => app
| ARROW_DOWN => app
end

View File

@@ -0,0 +1,14 @@
structure NormalYankDelete =
MakeNormalDelete
(struct
open DrawMsg
open MailboxType
fun initMsgs (low, length, buffer) =
let
val str = LineGap.substring (low, length + 1, buffer)
val msg = YANK str
in
[DRAW msg]
end
end)

View File

@@ -0,0 +1,534 @@
structure NormalYank =
struct
open AppType
open DrawMsg
open MailboxType
fun finish (app, buffer, yankedString) =
let
val msgs = [DRAW (YANK yankedString)]
val mode = NORMAL_MODE ""
in
NormalModeWith.modeAndBuffer (app, buffer, mode, msgs)
end
fun yankLeft (app: app_type, count) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val min = Cursor.vi0 (buffer, cursorIdx)
val low = Cursor.viH (buffer, cursorIdx, count)
val low = Int.max (min, low)
val length = cursorIdx - low
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
fun yankRight (app: app_type, count) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val endOfLineIdx = Cursor.viDlr (buffer, cursorIdx, 1) + 1
val high = Cursor.viL (buffer, cursorIdx, count)
val high = Int.min (high, endOfLineIdx)
val length = high - cursorIdx
val buffer = LineGap.goToIdx (high, buffer)
val str = LineGap.substring (cursorIdx, length, buffer)
in
finish (app, buffer, str)
end
fun yankLineUp (app: app_type, count) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val cursorLineNumber =
if Cursor.isNextChrEndOfLine (buffer, cursorIdx) then
LineGap.idxToLineNumber (cursorIdx + 1, buffer)
else
LineGap.idxToLineNumber (cursorIdx, buffer)
val newCursorLineNumber = Int.max (cursorLineNumber - count, 0)
in
if cursorLineNumber = 0 then
NormalFinish.clearMode app
else if newCursorLineNumber = 0 then
let
val endOfLine = Cursor.viDlr (buffer, cursorIdx, 1)
val buffer = LineGap.goToIdx (endOfLine, buffer)
val endOfLine =
if Cursor.isCursorAtStartOfLine (buffer, endOfLine) then
endOfLine + 1
else
endOfLine + 2
val buffer = LineGap.goToIdx (endOfLine, buffer)
val str = LineGap.substring (0, endOfLine, buffer)
in
finish (app, buffer, str)
end
else
let
val endOfLine = Cursor.viDlr (buffer, cursorIdx, 1)
val buffer = LineGap.goToIdx (endOfLine, buffer)
val endsOnNewline = Cursor.isCursorAtStartOfLine (buffer, endOfLine)
val endOfLine = if endsOnNewline then endOfLine else endOfLine + 1
val newCursorLineNumber =
if endsOnNewline andalso endOfLine = #textLength buffer - 1 then
newCursorLineNumber - 1
else
newCursorLineNumber
val buffer = LineGap.goToLine (newCursorLineNumber, buffer)
val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer)
val length = endOfLine - lineIdx
val buffer = LineGap.goToIdx (endOfLine, buffer)
val str = LineGap.substring (lineIdx + 1, length, buffer)
in
finish (app, buffer, str)
end
end
fun yankLineDown (app: app_type, count) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val startIdx = Cursor.vi0 (buffer, cursorIdx)
val buffer = LineGap.goToIdx (startIdx, buffer)
val startLine =
if Cursor.isCursorAtStartOfLine (buffer, startIdx) then
LineGap.idxToLineNumber (startIdx, buffer)
else
LineGap.idxToLineNumber (startIdx + 1, buffer)
val endLine = startLine + count + 1
val buffer = LineGap.goToLine (endLine, buffer)
val endLineIdx = LineGap.lineNumberToIdx (endLine, buffer)
val buffer = LineGap.goToIdx (endLineIdx - 1, buffer)
(* get "real" endLine by not considering newline after non-newline *)
val endLine =
if Cursor.isOnNewlineAfterChr (buffer, endLineIdx - 1) then
LineGap.idxToLineNumber (endLineIdx - 1, buffer)
else
LineGap.idxToLineNumber (endLineIdx, buffer)
in
if endLineIdx = #textLength buffer andalso endLine = startLine then
(* cursor is already on last line so don't yank *)
NormalFinish.clearMode app
else
let
val endLineIdx = endLineIdx + 1
val length = endLineIdx - startIdx
(* perform the actual yank *)
val buffer = LineGap.goToIdx (endLineIdx, buffer)
val str = LineGap.substring (startIdx, length, buffer)
in
finish (app, buffer, str)
end
end
fun yankLine (app: app_type, count) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val low = Cursor.vi0 (buffer, cursorIdx)
val buffer = LineGap.goToIdx (low, buffer)
val high = Cursor.viDlrForDelete (buffer, low, count)
val buffer = LineGap.goToIdx (high, buffer)
val length = high - low
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
fun yankToStartOfLine (app: app_type) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val low = Cursor.vi0 (buffer, cursorIdx)
val length = cursorIdx - low
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
fun yankWhenMovingBack (app: app_type, fMove, count) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val low = fMove (buffer, cursorIdx, count)
val length = cursorIdx - low
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
fun yankWhenMovingBackPlusOne (app: app_type, fMove, count) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val low = fMove (buffer, cursorIdx, count)
val length = (cursorIdx + 1) - low
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
fun yankWhenMovingForward (app: app_type, fMove, count) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val high = fMove (buffer, cursorIdx, count)
val beforeHigh = high - 1
val buffer = LineGap.goToIdx (beforeHigh, buffer)
val high =
if Cursor.isOnNewlineAfterChr (buffer, beforeHigh) then beforeHigh
else high
val length = high - cursorIdx
val str = LineGap.substring (cursorIdx, length, buffer)
in
finish (app, buffer, str)
end
fun yankToFirstNonSpaceChr (app: app_type) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val otherIdx = Cursor.vi0 (buffer, cursorIdx)
val buffer = LineGap.goToIdx (otherIdx, buffer)
val otherIdx = Cursor.firstNonSpaceChr (buffer, otherIdx)
in
if cursorIdx > otherIdx then
(* yanking backwards from cursorIdx *)
let
val length = cursorIdx - otherIdx + 1
val buffer = LineGap.goToIdx (otherIdx, buffer)
val str = LineGap.substring (otherIdx, length, buffer)
in
finish (app, buffer, str)
end
else if cursorIdx < otherIdx then
(* yanking forward from cursorIdx *)
let
val length = otherIdx - cursorIdx
val str = LineGap.substring (cursorIdx, length, buffer)
in
finish (app, buffer, str)
end
else
NormalFinish.clearMode app
end
fun yankToEndOfText (app: app_type) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToEnd buffer
val {rightStrings, idx, ...} = buffer
val finishIdx = Int.max (0, idx - 1)
val length = finishIdx - cursorIdx
val str = LineGap.substring (cursorIdx, length, buffer)
in
finish (app, buffer, str)
end
fun yankToMatchingPair (app: app_type) =
let
val {buffer, cursorIdx, ...} = app
val otherIdx = Cursor.matchPair (buffer, cursorIdx)
in
if cursorIdx = otherIdx then
NormalFinish.clearMode app
else
let
val low = Int.min (cursorIdx, otherIdx)
val high = Int.max (cursorIdx, otherIdx)
val length = high - low + 1
val buffer = LineGap.goToIdx (high, buffer)
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
end
fun yankToNextMatch (app: app_type, count) =
let
val {cursorIdx, searchList, buffer, ...} = app
val high = PersistentVector.nextMatch (cursorIdx, searchList, count)
in
if high = ~1 orelse high <= cursorIdx then
NormalFinish.clearMode app
else
let
val length = high - cursorIdx
val buffer = LineGap.goToIdx (high, buffer)
val str = LineGap.substring (cursorIdx, length, buffer)
in
finish (app, buffer, str)
end
end
fun yankToPrevMatch (app: app_type, count) =
let
val {cursorIdx, searchList, buffer, ...} = app
val low = PersistentVector.prevMatch (cursorIdx, searchList, count)
in
if low = ~1 orelse low >= cursorIdx then
NormalFinish.clearMode app
else
let
val length = cursorIdx - low
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
end
fun helpYankToChr
(app: app_type, buffer, cursorIdx, otherIdx, count, fMove, fInc, chr) =
if count = 0 then
let
val low = Int.min (cursorIdx, otherIdx)
val high = Int.max (cursorIdx, otherIdx)
val length = high - low
val buffer = LineGap.goToIdx (high, buffer)
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
else
let
val buffer = LineGap.goToIdx (otherIdx, buffer)
val newOtherIdx = fMove (buffer, otherIdx, chr)
val newCount = if newOtherIdx = otherIdx then 0 else count - 1
val newOtherIdx = fInc (newOtherIdx, 1)
in
helpYankToChr
(app, buffer, cursorIdx, newOtherIdx, newCount, fMove, fInc, chr)
end
fun yankToNextChr (app: app_type, count, chr) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx =
Cursor.toNextChr (buffer, cursorIdx, {findChr = chr, count = count})
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
let
val length = newCursorIdx - cursorIdx + 1
val buffer = LineGap.goToIdx (newCursorIdx, buffer)
val str = LineGap.substring (cursorIdx, length, buffer)
in
finish (app, buffer, str)
end
end
fun yankTillNextChr (app: app_type, count, chr) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx =
Cursor.toNextChr (buffer, cursorIdx, {findChr = chr, count = count})
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
let
val length = newCursorIdx - cursorIdx
val buffer = LineGap.goToIdx (newCursorIdx, buffer)
val str = LineGap.substring (cursorIdx, length, buffer)
in
finish (app, buffer, str)
end
end
fun yankToPrevChr (app: app_type, count, chr) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx =
Cursor.toPrevChr (buffer, cursorIdx, {findChr = chr, count = count})
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
let
val length = cursorIdx - newCursorIdx
val str = LineGap.substring (newCursorIdx, length, buffer)
in
finish (app, buffer, str)
end
end
fun yankTillPrevChr (app: app_type, count, chr) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val newCursorIdx =
Cursor.toPrevChr (buffer, cursorIdx, {findChr = chr, count = count})
in
if newCursorIdx = ~1 then
NormalFinish.clearMode app
else
let
val newCursorIdx = newCursorIdx + 1
val length = cursorIdx - newCursorIdx
val str = LineGap.substring (newCursorIdx, length, buffer)
in
finish (app, buffer, str)
end
end
fun yankToStart (app: app_type) =
let
val {cursorIdx, buffer, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val high = Cursor.viDlrForDelete (buffer, cursorIdx, 1)
val buffer = LineGap.goToIdx (high, buffer)
val str = LineGap.substring (0, high, buffer)
in
finish (app, buffer, str)
end
fun yankInsideWord (app: app_type) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val low = Cursor.prevWordStrict (buffer, cursorIdx, 1)
val high = Cursor.endOfWordStrict (buffer, cursorIdx, 1)
val high = high + 1
val buffer = LineGap.goToIdx (high, buffer)
val length = high - low
val str = LineGap.substring (low, length, buffer)
in
if str = "\n" then NormalFinish.clearMode app
else finish (app, buffer, str)
end
fun yankInsideWORD (app: app_type) =
let
val {buffer, cursorIdx, ...} = app
val buffer = LineGap.goToIdx (cursorIdx, buffer)
val low = Cursor.prevWORDStrict (buffer, cursorIdx, 1)
val high = Cursor.endOfWORDStrict (buffer, cursorIdx, 1)
val high = high + 1
val buffer = LineGap.goToIdx (high, buffer)
val length = high - low
val str = LineGap.substring (low, length, buffer)
in
if str = "\n" then NormalFinish.clearMode app
else finish (app, buffer, str)
end
fun finishAfterYankInside (app: app_type, low, high, buffer) =
let
val length = high - low
val str = LineGap.substring (low, length, buffer)
in
finish (app, buffer, str)
end
fun yankInsideChrOpen (app: app_type, chr) =
let
val {cursorIdx, buffer, ...} = app
val start = cursorIdx + 1
val buffer = LineGap.goToIdx (start, buffer)
val low = Cursor.toPrevChr (buffer, start, {findChr = chr, count = 1})
val buffer = LineGap.goToIdx (low, buffer)
val high = Cursor.matchPair (buffer, low)
val buffer = LineGap.goToIdx (high, buffer)
val low = low + 1
in
if low = high then NormalFinish.clearMode app
else finishAfterYankInside (app, low, high, buffer)
end
fun yankInsideChrClose (app: app_type, chr) =
let
val {cursorIdx, buffer, ...} = app
val start = Int.max (cursorIdx - 1, 0)
val buffer = LineGap.goToIdx (start, buffer)
val high = Cursor.toNextChr (buffer, start, {findChr = chr, count = 1})
val buffer = LineGap.goToIdx (high, buffer)
val low = Cursor.matchPair (buffer, high) + 1
in
if low = high then NormalFinish.clearMode app
else finishAfterYankInside (app, low, high, buffer)
end
fun yankAroundChrOpen (app: app_type, chr) =
let
val {cursorIdx, buffer, ...} = app
val start = cursorIdx + 1
val buffer = LineGap.goToIdx (start, buffer)
val low = Cursor.toPrevChr (buffer, start, {findChr = chr, count = 1})
val buffer = LineGap.goToIdx (low, buffer)
val high = Cursor.matchPair (buffer, low) + 1
val buffer = LineGap.goToIdx (high, buffer)
val low = low
in
if low = high then NormalFinish.clearMode app
else finishAfterYankInside (app, low, high, buffer)
end
fun yankAroundChrClose (app: app_type, chr) =
let
val {cursorIdx, buffer, ...} = app
val start = Int.max (cursorIdx - 1, 0)
val buffer = LineGap.goToIdx (start, buffer)
val high = Cursor.toNextChr (buffer, start, {findChr = chr, count = 1})
val buffer = LineGap.goToIdx (high, buffer)
val low = Cursor.matchPair (buffer, high)
val high = high + 1
in
if low = high then NormalFinish.clearMode app
else finishAfterYankInside (app, low, high, buffer)
end
end