make 'de', 'dE', 'dj' and 'dk' commands work similarly to vi (details are in comments)

This commit is contained in:
2024-11-14 08:57:31 +00:00
parent cae9f19787
commit f279dc0937
3 changed files with 47 additions and 4 deletions

View File

@@ -450,6 +450,36 @@ struct
helpDeleteLine (app, buffer, cursorIdx, cursorIdx, count) helpDeleteLine (app, buffer, cursorIdx, cursorIdx, count)
end end
fun helpDeleteLineBack (app, buffer, low, high, count) =
if count = 0 then
let
val low = Int.max (low, 0)
val length = high - low
val buffer = LineGap.delete (low, length, buffer)
val buffer = LineGap.goToIdx (low, buffer)
in
buildTextAndClear (app, buffer, low)
end
else
let
val buffer = LineGap.goToIdx (low, buffer)
val low = Cursor.viH (buffer, low)
val buffer = LineGap.goToIdx (low, buffer)
val low = Cursor.vi0 (buffer, low)
val newCount = if low = 0 then 0 else count - 1
in
helpDeleteLineBack (app, buffer, low, high, newCount)
end
fun deleteLineBack (app: app_type, count) =
let
val {buffer, cursorIdx, ...} = app
val low = Cursor.vi0 (buffer, cursorIdx)
val high = Cursor.viDlr (buffer, cursorIdx) + 1
in
helpDeleteLineBack (app, buffer, low, high, count)
end
fun deleteToFirstNonSpaceChr (app: app_type) = fun deleteToFirstNonSpaceChr (app: app_type) =
let let
val {buffer, cursorIdx, windowWidth, windowHeight, startLine, ...} = app val {buffer, cursorIdx, windowWidth, windowHeight, startLine, ...} = app
@@ -615,15 +645,19 @@ struct
(case chr of (case chr of
(* terminal commands: require no input after *) (* terminal commands: require no input after *)
#"h" => delete (app, count, Cursor.viH) #"h" => delete (app, count, Cursor.viH)
| #"j" => delete (app, count, Cursor.viJ)
| #"k" => delete (app, count, Cursor.viK)
| #"l" => delete (app, count, Cursor.viL) | #"l" => delete (app, count, Cursor.viL)
(* 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" => deleteLine (app, count + 1)
| #"k" => deleteLineBack (app, count)
| #"w" => delete (app, count, Cursor.nextWord) | #"w" => delete (app, count, Cursor.nextWord)
| #"W" => delete (app, count, Cursor.nextWORD) | #"W" => delete (app, count, Cursor.nextWORD)
| #"b" => delete (app, count, Cursor.prevWord) | #"b" => delete (app, count, Cursor.prevWord)
| #"B" => delete (app, count, Cursor.prevWORD) | #"B" => delete (app, count, Cursor.prevWORD)
| #"e" => delete (app, count, Cursor.endOfWord) | #"e" => delete (app, count, Cursor.endOfWordPlusOne)
| #"E" => delete (app, count, Cursor.endOfWORD) | #"E" => delete (app, count, Cursor.endOfWORDPlusOne)
| #"0" => delete (app, 1, Cursor.vi0) | #"0" => delete (app, 1, Cursor.vi0)
| #"$" => deleteToEndOfLine app | #"$" => deleteToEndOfLine app
| #"^" => deleteToFirstNonSpaceChr app | #"^" => deleteToFirstNonSpaceChr app

View File

@@ -1135,6 +1135,15 @@ struct
fun endOfWORD (lineGap, cursorIdx) = fun endOfWORD (lineGap, cursorIdx) =
toEndOfWord (lineGap, cursorIdx, helpEndOfWORD) toEndOfWord (lineGap, cursorIdx, helpEndOfWORD)
(* vi's 'e' command takes user last char in word
* but 'de' deletes up to and including last char of word
* So we need to increment by one for deletion. *)
fun endOfWordPlusOne (lineGap, cursorIdx) =
endOfWord (lineGap, cursorIdx) + 1
fun endOfWORDPlusOne (lineGap, cursorIdx) =
endOfWORD (lineGap, cursorIdx) + 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
case (stl, ltl) of case (stl, ltl) of

BIN
shf

Binary file not shown.