refactor some code to use looping in DFA (which is faster than looping outside of the data structure)
This commit is contained in:
@@ -472,6 +472,27 @@ struct
|
|||||||
fun delete (app: app_type, count, fMove) =
|
fun delete (app: app_type, count, fMove) =
|
||||||
helpDelete (app, #buffer app, #cursorIdx app, #cursorIdx app, count, fMove)
|
helpDelete (app, #buffer app, #cursorIdx app, #cursorIdx app, count, fMove)
|
||||||
|
|
||||||
|
fun deleteByDfa (app: app_type, count, fMove) =
|
||||||
|
let
|
||||||
|
val {buffer, cursorIdx, searchList, searchString, ...} = app
|
||||||
|
|
||||||
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
|
val otherIdx = fMove (buffer, cursorIdx, count)
|
||||||
|
|
||||||
|
val low = Int.min (cursorIdx, otherIdx)
|
||||||
|
val high = Int.max (cursorIdx, otherIdx)
|
||||||
|
val length = high - low
|
||||||
|
|
||||||
|
val buffer = LineGap.delete (low, length, buffer)
|
||||||
|
|
||||||
|
val (buffer, searchList) = deleteSearchList
|
||||||
|
(low, length, searchString, searchList, buffer)
|
||||||
|
|
||||||
|
val buffer = LineGap.goToIdx (low, buffer)
|
||||||
|
in
|
||||||
|
Finish.buildTextAndClear (app, buffer, low, searchList)
|
||||||
|
end
|
||||||
|
|
||||||
fun deleteToEndOfLine (app: app_type) =
|
fun deleteToEndOfLine (app: app_type) =
|
||||||
let
|
let
|
||||||
val {buffer, cursorIdx, ...} = app
|
val {buffer, cursorIdx, ...} = app
|
||||||
@@ -780,12 +801,16 @@ struct
|
|||||||
* other cursor motions *)
|
* other cursor motions *)
|
||||||
| #"j" => deleteLine (app, count + 1)
|
| #"j" => deleteLine (app, count + 1)
|
||||||
| #"k" => deleteLineBack (app, count)
|
| #"k" => deleteLineBack (app, count)
|
||||||
| #"w" => delete (app, count, Cursor.nextWord)
|
| #"w" => deleteByDfa (app, count, Cursor.nextWord)
|
||||||
| #"W" => delete (app, count, Cursor.nextWORD)
|
| #"W" => deleteByDfa (app, count, Cursor.nextWORD)
|
||||||
| #"b" => delete (app, count, Cursor.prevWord)
|
| #"b" => deleteByDfa (app, count, Cursor.prevWord)
|
||||||
| #"B" => delete (app, count, Cursor.prevWORD)
|
| #"B" => deleteByDfa (app, count, Cursor.prevWORD)
|
||||||
| #"e" => delete (app, count, Cursor.endOfWordPlusOne)
|
|
||||||
| #"E" => delete (app, count, Cursor.endOfWORDPlusOne)
|
(* todo: fix as 'plusOne' needs reimplementing
|
||||||
|
| #"e" => deleteByDfa (app, count, Cursor.endOfWordPlusOne)
|
||||||
|
| #"E" => deleteByDfa (app, count, Cursor.endOfWORDPlusOne)
|
||||||
|
*)
|
||||||
|
|
||||||
| #"0" => delete (app, 1, Cursor.vi0)
|
| #"0" => delete (app, 1, Cursor.vi0)
|
||||||
| #"$" => deleteToEndOfLine app
|
| #"$" => deleteToEndOfLine app
|
||||||
| #"^" => deleteToFirstNonSpaceChr app
|
| #"^" => deleteToFirstNonSpaceChr app
|
||||||
@@ -835,8 +860,8 @@ struct
|
|||||||
(case newCmd of
|
(case newCmd of
|
||||||
CHAR_EVENT chr =>
|
CHAR_EVENT chr =>
|
||||||
(case chr of
|
(case chr of
|
||||||
#"e" => delete (app, count, Cursor.endOfPrevWord)
|
#"e" => deleteByDfa (app, count, Cursor.endOfPrevWord)
|
||||||
| #"E" => delete (app, count, Cursor.endOfPrevWORD)
|
| #"E" => deleteByDfa (app, count, Cursor.endOfPrevWORD)
|
||||||
| #"g" => deleteToStart app
|
| #"g" => deleteToStart app
|
||||||
| _ => clearMode app)
|
| _ => clearMode app)
|
||||||
| KEY_ESC => clearMode app
|
| KEY_ESC => clearMode app
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ end
|
|||||||
|
|
||||||
functor MakeNextDfaLoop(M: MAKE_DFA_LOOP) =
|
functor MakeNextDfaLoop(M: MAKE_DFA_LOOP) =
|
||||||
struct
|
struct
|
||||||
fun next (lineGap: LineGap.t, cursorIdx) =
|
fun next (lineGap: LineGap.t, cursorIdx, count) =
|
||||||
let
|
let
|
||||||
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
||||||
(* convert absolute cursorIdx to idx relative to hd string *)
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||||
@@ -16,13 +16,13 @@ struct
|
|||||||
shd :: stl =>
|
shd :: stl =>
|
||||||
if strIdx < String.size shd then
|
if strIdx < String.size shd then
|
||||||
(* strIdx is in this string *)
|
(* strIdx is in this string *)
|
||||||
M.fStart (strIdx, cursorIdx, shd, stl, M.startState, 1)
|
M.fStart (strIdx, cursorIdx, shd, stl, M.startState, count)
|
||||||
else
|
else
|
||||||
(* strIdx is in tl *)
|
(* strIdx is in tl *)
|
||||||
(case stl of
|
(case stl of
|
||||||
stlhd :: stltl =>
|
stlhd :: stltl =>
|
||||||
let val strIdx = strIdx - String.size shd
|
let val strIdx = strIdx - String.size shd
|
||||||
in M.fStart (strIdx, cursorIdx, stlhd, stltl, M.startState, 1)
|
in M.fStart (strIdx, cursorIdx, stlhd, stltl, M.startState, count)
|
||||||
end
|
end
|
||||||
| _ => cursorIdx)
|
| _ => cursorIdx)
|
||||||
| [] => cursorIdx
|
| [] => cursorIdx
|
||||||
@@ -31,7 +31,7 @@ end
|
|||||||
|
|
||||||
functor MakeNextDfaLoopPlus1(M: MAKE_DFA_LOOP) =
|
functor MakeNextDfaLoopPlus1(M: MAKE_DFA_LOOP) =
|
||||||
struct
|
struct
|
||||||
fun next (lineGap: LineGap.t, cursorIdx) =
|
fun next (lineGap: LineGap.t, cursorIdx, count) =
|
||||||
let
|
let
|
||||||
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
||||||
(* convert absolute cursorIdx to idx relative to hd string *)
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||||
@@ -42,13 +42,13 @@ struct
|
|||||||
shd :: stl =>
|
shd :: stl =>
|
||||||
if strIdx < String.size shd then
|
if strIdx < String.size shd then
|
||||||
(* strIdx is in this string *)
|
(* strIdx is in this string *)
|
||||||
M.fStart (strIdx, absIdx, shd, stl, M.startState, 1)
|
M.fStart (strIdx, absIdx, shd, stl, M.startState, count)
|
||||||
else
|
else
|
||||||
(* strIdx is in tl *)
|
(* strIdx is in tl *)
|
||||||
(case stl of
|
(case stl of
|
||||||
stlhd :: stltl =>
|
stlhd :: stltl =>
|
||||||
let val strIdx = strIdx - String.size shd
|
let val strIdx = strIdx - String.size shd
|
||||||
in M.fStart (strIdx, absIdx, stlhd, stltl, M.startState, 1)
|
in M.fStart (strIdx, absIdx, stlhd, stltl, M.startState, count)
|
||||||
end
|
end
|
||||||
| _ => cursorIdx)
|
| _ => cursorIdx)
|
||||||
| [] => cursorIdx
|
| [] => cursorIdx
|
||||||
@@ -57,7 +57,7 @@ end
|
|||||||
|
|
||||||
functor MakePrevDfaLoop(M: MAKE_DFA_LOOP) =
|
functor MakePrevDfaLoop(M: MAKE_DFA_LOOP) =
|
||||||
struct
|
struct
|
||||||
fun prev (lineGap: LineGap.t, cursorIdx) =
|
fun prev (lineGap: LineGap.t, cursorIdx, count) =
|
||||||
let
|
let
|
||||||
val {rightStrings, leftStrings, idx = bufferIdx, ...} = lineGap
|
val {rightStrings, leftStrings, idx = bufferIdx, ...} = lineGap
|
||||||
(* convert absolute cursorIdx to idx relative to hd string *)
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||||
@@ -67,7 +67,7 @@ struct
|
|||||||
shd :: stl =>
|
shd :: stl =>
|
||||||
if strIdx < String.size shd then
|
if strIdx < String.size shd then
|
||||||
(* strIdx is in this string *)
|
(* strIdx is in this string *)
|
||||||
M.fStart (strIdx, cursorIdx, shd, leftStrings, M.startState, 1)
|
M.fStart (strIdx, cursorIdx, shd, leftStrings, M.startState, count)
|
||||||
else
|
else
|
||||||
(* strIdx is in tl *)
|
(* strIdx is in tl *)
|
||||||
(case stl of
|
(case stl of
|
||||||
@@ -77,7 +77,7 @@ struct
|
|||||||
val leftStrings = shd :: leftStrings
|
val leftStrings = shd :: leftStrings
|
||||||
in
|
in
|
||||||
M.fStart
|
M.fStart
|
||||||
(strIdx, cursorIdx, stlhd, leftStrings, M.startState, 1)
|
(strIdx, cursorIdx, stlhd, leftStrings, M.startState, count)
|
||||||
end
|
end
|
||||||
| [] => cursorIdx)
|
| [] => cursorIdx)
|
||||||
| [] => cursorIdx
|
| [] => cursorIdx
|
||||||
@@ -86,7 +86,7 @@ end
|
|||||||
|
|
||||||
functor MakePrevDfaLoopMinus1(M: MAKE_DFA_LOOP) =
|
functor MakePrevDfaLoopMinus1(M: MAKE_DFA_LOOP) =
|
||||||
struct
|
struct
|
||||||
fun prev (lineGap: LineGap.t, cursorIdx) =
|
fun prev (lineGap: LineGap.t, cursorIdx, count) =
|
||||||
let
|
let
|
||||||
val {idx = bufferIdx, leftStrings, ...} = lineGap
|
val {idx = bufferIdx, leftStrings, ...} = lineGap
|
||||||
val strIdx = cursorIdx - bufferIdx - 1
|
val strIdx = cursorIdx - bufferIdx - 1
|
||||||
@@ -95,12 +95,12 @@ struct
|
|||||||
if strIdx < 0 then
|
if strIdx < 0 then
|
||||||
case leftStrings of
|
case leftStrings of
|
||||||
lhd :: ltl =>
|
lhd :: ltl =>
|
||||||
M.fStart (String.size lhd - 1, absIdx, lhd, ltl, M.startState, 1)
|
M.fStart (String.size lhd - 1, absIdx, lhd, ltl, M.startState, count)
|
||||||
| [] => 0
|
| [] => 0
|
||||||
else
|
else
|
||||||
case #rightStrings lineGap of
|
case #rightStrings lineGap of
|
||||||
rhd :: _ =>
|
rhd :: _ =>
|
||||||
M.fStart (strIdx, absIdx, rhd, leftStrings, M.startState, 1)
|
M.fStart (strIdx, absIdx, rhd, leftStrings, M.startState, count)
|
||||||
| [] => Int.max (0, cursorIdx - 2)
|
| [] => Int.max (0, cursorIdx - 2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -737,10 +737,12 @@ struct
|
|||||||
* but 'de' deletes up to and including last char of word
|
* but 'de' deletes up to and including last char of word
|
||||||
* So we need to increment by one for deletion. *)
|
* So we need to increment by one for deletion. *)
|
||||||
fun endOfWordPlusOne (lineGap, cursorIdx) =
|
fun endOfWordPlusOne (lineGap, cursorIdx) =
|
||||||
endOfWord (lineGap, cursorIdx) + 1
|
(* todo: fix *)
|
||||||
|
endOfWord (lineGap, cursorIdx, 1) + 1
|
||||||
|
|
||||||
fun endOfWORDPlusOne (lineGap, cursorIdx) =
|
fun endOfWORDPlusOne (lineGap, cursorIdx) =
|
||||||
endOfWORD (lineGap, cursorIdx) + 1
|
(* todo: fix *)
|
||||||
|
endOfWORD (lineGap, cursorIdx, 1) + 1
|
||||||
|
|
||||||
fun helpFirstNonSpaceChr (strPos, str, absIdx, stl, ltl) =
|
fun helpFirstNonSpaceChr (strPos, str, absIdx, stl, ltl) =
|
||||||
if strPos = String.size str then
|
if strPos = String.size str then
|
||||||
|
|||||||
@@ -42,19 +42,42 @@ structure MoveViJ = MakeMove (struct val fMove = Cursor.viJ end)
|
|||||||
structure MoveViK = MakeMove (struct val fMove = Cursor.viK end)
|
structure MoveViK = MakeMove (struct val fMove = Cursor.viK end)
|
||||||
structure MoveViL = MakeMove (struct val fMove = Cursor.viL end)
|
structure MoveViL = MakeMove (struct val fMove = Cursor.viL end)
|
||||||
|
|
||||||
structure MoveToNextWord = MakeMove (struct val fMove = Cursor.nextWord end)
|
|
||||||
structure MoveToNextWORD = MakeMove (struct val fMove = Cursor.nextWORD end)
|
|
||||||
|
|
||||||
structure MoveToEndOfWord = MakeMove (struct val fMove = Cursor.endOfWord end)
|
|
||||||
structure MoveToEndOfWORD = MakeMove (struct val fMove = Cursor.endOfWORD end)
|
|
||||||
|
|
||||||
structure MoveToPrevWord = MakeMove (struct val fMove = Cursor.prevWord end)
|
|
||||||
structure MoveToPrevWORD = MakeMove (struct val fMove = Cursor.prevWORD end)
|
|
||||||
|
|
||||||
structure MoveToEndOfPrevWord =
|
|
||||||
MakeMove (struct val fMove = Cursor.endOfPrevWord end)
|
|
||||||
structure MoveToEndOfPrevWORD =
|
|
||||||
MakeMove (struct val fMove = Cursor.endOfPrevWORD end)
|
|
||||||
|
|
||||||
structure MoveToStartOfLine = MakeMove (struct val fMove = Cursor.vi0 end)
|
structure MoveToStartOfLine = MakeMove (struct val fMove = Cursor.vi0 end)
|
||||||
structure MoveToEndOfLine = MakeMove (struct val fMove = Cursor.viDlr end)
|
structure MoveToEndOfLine = MakeMove (struct val fMove = Cursor.viDlr end)
|
||||||
|
|
||||||
|
signature DFA_MOVE =
|
||||||
|
sig
|
||||||
|
val fMove: LineGap.t * int * int -> int
|
||||||
|
end
|
||||||
|
|
||||||
|
signature MAKE_DFA_MOVE =
|
||||||
|
sig
|
||||||
|
val move: AppType.app_type * int -> AppType.app_type
|
||||||
|
end
|
||||||
|
|
||||||
|
functor MakeDfaMove(Fn: DFA_MOVE): MAKE_DFA_MOVE =
|
||||||
|
struct
|
||||||
|
fun move (app: AppType.app_type, count) =
|
||||||
|
let
|
||||||
|
val {buffer, cursorIdx, ...} = app
|
||||||
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
|
val cursorIdx = Fn.fMove (buffer, cursorIdx, count)
|
||||||
|
in
|
||||||
|
Finish.buildTextAndClear (app, buffer, cursorIdx, #searchList app)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
structure MoveToNextWord = MakeDfaMove (struct val fMove = Cursor.nextWord end)
|
||||||
|
structure MoveToNextWORD = MakeDfaMove (struct val fMove = Cursor.nextWORD end)
|
||||||
|
|
||||||
|
structure MoveToEndOfWord = MakeDfaMove (struct val fMove = Cursor.endOfWord end)
|
||||||
|
structure MoveToEndOfWORD = MakeDfaMove (struct val fMove = Cursor.endOfWORD end)
|
||||||
|
|
||||||
|
structure MoveToPrevWord = MakeDfaMove (struct val fMove = Cursor.prevWord end)
|
||||||
|
structure MoveToPrevWORD = MakeDfaMove (struct val fMove = Cursor.prevWORD end)
|
||||||
|
|
||||||
|
structure MoveToEndOfPrevWord =
|
||||||
|
MakeDfaMove (struct val fMove = Cursor.endOfPrevWord end)
|
||||||
|
structure MoveToEndOfPrevWORD =
|
||||||
|
MakeDfaMove (struct val fMove = Cursor.endOfPrevWORD end)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user