simplify and rewrite function 'del' in search-list.sml

This commit is contained in:
2024-11-16 08:33:26 +00:00
parent 0ba56bf6d3
commit 436b3e0d92
4 changed files with 104 additions and 63 deletions

View File

@@ -363,9 +363,34 @@ struct
(* text-delete functions *) (* text-delete functions *)
(** equivalent of vi's 'x' command **) (** equivalent of vi's 'x' command **)
fun helpRemoveChr (app: app_type, buffer, cursorIdx, count) = fun helpRemoveChr (app: app_type, buffer, searchList, cursorIdx, count) =
if count = 0 then if count = 0 then
buildTextAndClear (app, buffer, cursorIdx) let
val {windowWidth, windowHeight, startLine, searchString, ...} =
app
(* move LineGap to first line displayed on screen *)
val buffer = LineGap.goToLine (startLine, buffer)
(* get new startLine which may move screen depending on cursor movements *)
val startLine = TextWindow.getStartLine
(buffer, startLine, cursorIdx, windowWidth, windowHeight)
(* move buffer to new startLine as required by TextBuilder.build *)
val buffer = LineGap.goToLine (startLine, buffer)
val drawMsg = TextBuilder.build
( startLine, cursorIdx, buffer
, windowWidth, windowHeight
, searchList, searchString
)
val mode = NORMAL_MODE ""
val newApp = AppWith.onDelete
(app, buffer, cursorIdx, mode, startLine, searchList)
in
(newApp, drawMsg)
end
else else
let let
val buffer = LineGap.goToIdx (cursorIdx, buffer) val buffer = LineGap.goToIdx (cursorIdx, buffer)
@@ -388,14 +413,17 @@ struct
(* vi simply doesn't do anything on 'x' command (* vi simply doesn't do anything on 'x' command
* when cursor is at start of line, and next chr is line break * when cursor is at start of line, and next chr is line break
* so skip to end of loop by passing count of 0 *) * so skip to end of loop by passing count of 0 *)
helpRemoveChr (app, buffer, cursorIdx, 0) helpRemoveChr (app, buffer, searchList, cursorIdx, 0)
else if cursorIsStart then else if cursorIsStart then
helpRemoveChr (app, buffer, cursorIdx, 0) helpRemoveChr (app, buffer, searchList, cursorIdx, 0)
else if nextIsEnd then else if nextIsEnd then
let let
(* delete char at cursor and then decrement cursorIdx by 1 (* delete char at cursor and then decrement cursorIdx by 1
* if cursorIdx is not 0 *) * if cursorIdx is not 0 *)
val buffer = LineGap.delete (cursorIdx, 1, buffer) val buffer = LineGap.delete (cursorIdx, 1, buffer)
val searchList = SearchList.delete (cursorIdx, 1, searchList)
val searchList = SearchList.mapFrom (cursorIdx, ~1, searchList)
val cursorIdx = val cursorIdx =
if if
Cursor.isPrevChrStartOfLine (buffer, cursorIdx) Cursor.isPrevChrStartOfLine (buffer, cursorIdx)
@@ -403,16 +431,20 @@ struct
then cursorIdx then cursorIdx
else cursorIdx - 1 else cursorIdx - 1
in in
helpRemoveChr (app, buffer, cursorIdx, count - 1) helpRemoveChr (app, buffer, searchList, cursorIdx, count - 1)
end end
else else
let val buffer = LineGap.delete (cursorIdx, 1, buffer) let
in helpRemoveChr (app, buffer, cursorIdx, count - 1) val buffer = LineGap.delete (cursorIdx, 1, buffer)
val searchList = SearchList.delete (cursorIdx, 1, searchList)
val searchList = SearchList.mapFrom (cursorIdx, ~1, searchList)
in
helpRemoveChr (app, buffer, searchList, cursorIdx, count - 1)
end end
end end
fun removeChr (app: app_type, count) = fun removeChr (app: app_type, count) =
helpRemoveChr (app, #buffer app, #cursorIdx app, count) helpRemoveChr (app, #buffer app, #searchList app, #cursorIdx app, count)
fun helpDelete (app: app_type, buffer, cursorIdx, otherIdx, count, fMove) = fun helpDelete (app: app_type, buffer, cursorIdx, otherIdx, count, fMove) =
(* As a small optimisation to reduce allocations, (* As a small optimisation to reduce allocations,
@@ -470,8 +502,10 @@ struct
val lastChr = Cursor.viDlr (buffer, cursorIdx) val lastChr = Cursor.viDlr (buffer, cursorIdx)
val length = lastChr - cursorIdx val length = lastChr - cursorIdx
val buffer = LineGap.delete (cursorIdx, length, buffer) val buffer = LineGap.delete (cursorIdx, length, buffer)
(* todo: delete from searchList and map *)
in in
helpRemoveChr (app, buffer, cursorIdx, 1) helpRemoveChr (app, buffer, #searchList app, cursorIdx, 1)
end end
end end

View File

@@ -75,6 +75,31 @@ struct
} }
end end
fun onDelete
(app: app_type, newBuffer, newCursorIdx, newMode, newStartLine, newSearchList) =
let
val
{ mode = _
, buffer = _
, cursorIdx = _
, startLine = _
, searchList = _
, searchString
, windowWidth
, windowHeight
} = app
in
{ mode = newMode
, buffer = newBuffer
, cursorIdx = newCursorIdx
, startLine = newStartLine
, searchList = newSearchList
, searchString = searchString
, windowWidth = windowWidth
, windowHeight = windowHeight
}
end
fun mode (app: app_type, newMode) = fun mode (app: app_type, newMode) =
let let
val val

View File

@@ -8,13 +8,26 @@ sig
val delete: int * int * t -> t val delete: int * int * t -> t
val goToNum: int * t -> t val goToNum: int * t -> t
val mapFromNum: int * int * t -> t val mapFrom: int * int * t -> t
end end
structure SearchList :> SEARCH_LIST = structure SearchList :> SEARCH_LIST =
struct struct
type t = {left: int vector list, right: int vector list} type t = {left: int vector list, right: int vector list}
(* temp function for testing *)
fun printlst (left, right) =
let
val left = List.rev left
val lst = List.concat [left, right]
val v = Vector.concat lst
val _ = print "\nstart print list:\n"
val _ = Vector.map (fn el => print (" " ^ Int.toString el ^ ",")) v
val _ = print "\nend print list\n"
in
()
end
val targetLength = 1024 val targetLength = 1024
val empty: t = {left = [], right = []} val empty: t = {left = [], right = []}
@@ -379,54 +392,25 @@ struct
val rfirst = Vector.sub (rhd, 0) val rfirst = Vector.sub (rhd, 0)
in in
if start < rfirst then if start < rfirst then
(case left of if finish < rfirst then
lhd :: ltl => (case left of
let lhd :: ltl =>
val llast = Vector.sub (lhd, Vector.length lhd - 1) let
in val llast = Vector.sub (lhd, Vector.length lhd - 1)
if in
start < llast if finish = llast then
then delLeftFromHere (start, left, right)
if finish < rfirst then else
(* start < rfirst and start < llast and finish < rfirst moveLeftAndDelete (start, finish, left, right)
* move left and delete *) end
moveLeftAndDelete (start, finish, left, right) | [] =>
else {left = left, right = right})
(* start < rfirst and start < llast and finish >= rfirst else
* in middle; delete from both sides *) (* finish >= rfirst *)
delFromLeftAndRight (start, finish, left, right) delFromLeftAndRight (start, finish, left, right)
else if
start = llast
then
if finish < rfirst then
(* start < rfirst, start = llast, and finish < rfirst
* so just have to delete left from here *)
delLeftFromHere (start, left, right)
else
(* start < rfirst, start = llast, finish >= rfirst
* in middle; delete from both sides *)
delFromLeftAndRight (start, finish, left, right)
else (* start > llast and start < rfirst *) if
finish >= rfirst
then
(* delete right from here *)
delRightFromHere (finish, left, right)
else
(* start < rfirst and finish < rfirst
* so just return *)
{left = left, right = right}
end
| [] =>
(* start < rfirst
* but left is empty.
* All we can do is 1. Delete right or 2. Return *)
if finish < rfirst then {left = left, right = right}
else delRightFromHere (finish, left, right))
else if start = rfirst then else if start = rfirst then
delRightFromHere (finish, left, right) delRightFromHere (finish, left, right)
else else
(* start > rfirst
* move right and then start deleting *)
moveRightAndDelete (start, finish, left, right) moveRightAndDelete (start, finish, left, right)
end end
| [] => | [] =>
@@ -435,13 +419,11 @@ struct
let let
val llast = Vector.sub (lhd, Vector.length lhd - 1) val llast = Vector.sub (lhd, Vector.length lhd - 1)
in in
if start < llast then if finish >= llast then
if finish <= llast then delLeftFromHere (start, left, right)
moveRightAndDelete (start, finish, left, right)
else
delLeftFromHere (finish, left, right)
else else
moveRightAndDelete (start, finish, left, right) (* finish < last *)
moveLeftAndDelete (start, finish, left, right)
end end
| [] => | [] =>
(* left and right are both empty *) (* left and right are both empty *)
@@ -493,7 +475,7 @@ struct
end end
| [] => {left = left, right = right} | [] => {left = left, right = right}
fun mapFromNum (num, mapBy, lst) = fun mapFrom (num, mapBy, lst) =
let let
(* goToNum always places vector where num was found to the right list *) (* goToNum always places vector where num was found to the right list *)
val {left, right} = goToNum (num, lst) val {left, right} = goToNum (num, lst)

BIN
shf

Binary file not shown.