Skip to content

Commit dc522cd

Browse files
committed
Merging in Eduardo's fFrT for c and d changes, along with my tests and fixes.
2 parents 8c13999 + c13ad54 commit dc522cd

File tree

5 files changed

+357
-33
lines changed

5 files changed

+357
-33
lines changed

PSReadLine/KeyBindings.vi.cs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -209,37 +209,45 @@ private static KeyHandler MakeViKeyHandler(Action<ConsoleKeyInfo?, object> actio
209209

210210
private static readonly Dictionary<ConsoleKeyInfo, KeyHandler> _viChordDTable = new Dictionary<ConsoleKeyInfo, KeyHandler>(new ConsoleKeyInfoComparer())
211211
{
212-
{ Keys.D, MakeViKeyHandler( DeleteLine, "DeleteLine") },
213-
{ Keys.Dollar, MakeViKeyHandler( DeleteToEnd, "DeleteToEnd") },
214-
{ Keys.B, MakeViKeyHandler( BackwardDeleteWord, "BackwardDeleteWord") },
215-
{ Keys.ucB, MakeViKeyHandler( ViBackwardDeleteGlob, "ViBackwardDeleteGlob") },
216-
{ Keys.W, MakeViKeyHandler( DeleteWord, "DeleteWord") },
217-
{ Keys.ucW, MakeViKeyHandler( ViDeleteGlob, "ViDeleteGlob") },
218-
{ Keys.E, MakeViKeyHandler( DeleteEndOfWord, "DeleteEndOfWord") },
219-
{ Keys.ucE, MakeViKeyHandler( ViDeleteEndOfGlob, "ViDeleteEndOfGlob") },
220-
{ Keys.H, MakeViKeyHandler( BackwardDeleteChar, "BackwardDeleteChar") },
221-
{ Keys.L, MakeViKeyHandler( DeleteChar, "DeleteChar") },
222-
{ Keys.Space, MakeViKeyHandler( DeleteChar, "DeleteChar") },
223-
{ Keys._0, MakeViKeyHandler( BackwardDeleteLine, "BackwardDeleteLine") },
224-
{ Keys.Uphat, MakeViKeyHandler( DeleteLineToFirstChar,"DeleteLineToFirstChar") },
225-
{ Keys.Percent, MakeViKeyHandler( ViDeleteBrace, "DeleteBrace") },
212+
{ Keys.D, MakeViKeyHandler( DeleteLine, "DeleteLine") },
213+
{ Keys.Dollar, MakeViKeyHandler( DeleteToEnd, "DeleteToEnd") },
214+
{ Keys.B, MakeViKeyHandler( BackwardDeleteWord, "BackwardDeleteWord") },
215+
{ Keys.ucB, MakeViKeyHandler( ViBackwardDeleteGlob, "ViBackwardDeleteGlob") },
216+
{ Keys.W, MakeViKeyHandler( DeleteWord, "DeleteWord") },
217+
{ Keys.ucW, MakeViKeyHandler( ViDeleteGlob, "ViDeleteGlob") },
218+
{ Keys.E, MakeViKeyHandler( DeleteEndOfWord, "DeleteEndOfWord") },
219+
{ Keys.ucE, MakeViKeyHandler( ViDeleteEndOfGlob, "ViDeleteEndOfGlob") },
220+
{ Keys.H, MakeViKeyHandler( BackwardDeleteChar, "BackwardDeleteChar") },
221+
{ Keys.L, MakeViKeyHandler( DeleteChar, "DeleteChar") },
222+
{ Keys.Space, MakeViKeyHandler( DeleteChar, "DeleteChar") },
223+
{ Keys._0, MakeViKeyHandler( BackwardDeleteLine, "BackwardDeleteLine") },
224+
{ Keys.Uphat, MakeViKeyHandler( DeleteLineToFirstChar, "DeleteLineToFirstChar") },
225+
{ Keys.Percent, MakeViKeyHandler( ViDeleteBrace, "DeleteBrace") },
226+
{ Keys.F, MakeViKeyHandler( ViDeleteToChar, "ViDeleteToChar") },
227+
{ Keys.ucF, MakeViKeyHandler( ViDeleteToCharBackward, "ViDeleteToCharBackward") },
228+
{ Keys.T, MakeViKeyHandler( ViDeleteToBeforeChar, "ViDeleteToBeforeChar") },
229+
{ Keys.ucT, MakeViKeyHandler( ViDeleteToBeforeCharBackward, "ViDeleteToBeforeCharBackward") },
226230
};
227231
private static readonly Dictionary<ConsoleKeyInfo, KeyHandler> _viChordCTable = new Dictionary<ConsoleKeyInfo, KeyHandler>(new ConsoleKeyInfoComparer())
228232
{
229-
{ Keys.C, MakeViKeyHandler( ViReplaceLine, "ViReplaceLine") },
230-
{ Keys.Dollar, MakeViKeyHandler( ViReplaceToEnd, "ViReplaceToEnd") },
231-
{ Keys.B, MakeViKeyHandler( ViBackwardReplaceWord, "ViBackwardReplaceWord") },
232-
{ Keys.ucB, MakeViKeyHandler( ViBackwardReplaceGlob, "ViBackwardReplaceGlob") },
233-
{ Keys.W, MakeViKeyHandler( ViReplaceWord, "ViReplaceWord") },
234-
{ Keys.ucW, MakeViKeyHandler( ViReplaceGlob, "ViReplaceGlob") },
235-
{ Keys.E, MakeViKeyHandler( ViReplaceEndOfWord, "ViReplaceEndOfWord") },
236-
{ Keys.ucE, MakeViKeyHandler( ViReplaceEndOfGlob, "ViReplaceEndOfGlob") },
237-
{ Keys.H, MakeViKeyHandler( BackwardReplaceChar, "BackwardReplaceChar") },
238-
{ Keys.L, MakeViKeyHandler( ReplaceChar, "ReplaceChar") },
239-
{ Keys.Space, MakeViKeyHandler( ReplaceChar, "ReplaceChar") },
240-
{ Keys._0, MakeViKeyHandler( ViBackwardReplaceLine, "ViBackwardReplaceLine") },
233+
{ Keys.C, MakeViKeyHandler( ViReplaceLine, "ViReplaceLine") },
234+
{ Keys.Dollar, MakeViKeyHandler( ViReplaceToEnd, "ViReplaceToEnd") },
235+
{ Keys.B, MakeViKeyHandler( ViBackwardReplaceWord, "ViBackwardReplaceWord") },
236+
{ Keys.ucB, MakeViKeyHandler( ViBackwardReplaceGlob, "ViBackwardReplaceGlob") },
237+
{ Keys.W, MakeViKeyHandler( ViReplaceWord, "ViReplaceWord") },
238+
{ Keys.ucW, MakeViKeyHandler( ViReplaceGlob, "ViReplaceGlob") },
239+
{ Keys.E, MakeViKeyHandler( ViReplaceEndOfWord, "ViReplaceEndOfWord") },
240+
{ Keys.ucE, MakeViKeyHandler( ViReplaceEndOfGlob, "ViReplaceEndOfGlob") },
241+
{ Keys.H, MakeViKeyHandler( BackwardReplaceChar, "BackwardReplaceChar") },
242+
{ Keys.L, MakeViKeyHandler( ReplaceChar, "ReplaceChar") },
243+
{ Keys.Space, MakeViKeyHandler( ReplaceChar, "ReplaceChar") },
244+
{ Keys._0, MakeViKeyHandler( ViBackwardReplaceLine, "ViBackwardReplaceLine") },
241245
{ Keys.Uphat, MakeViKeyHandler( ViBackwardReplaceLineToFirstChar, "ViBackwardReplaceLineToFirstChar") },
242-
{ Keys.Percent, MakeViKeyHandler( ViReplaceBrace, "ViReplaceBrace") },
246+
{ Keys.Percent, MakeViKeyHandler( ViReplaceBrace, "ViReplaceBrace") },
247+
{ Keys.F, MakeViKeyHandler( ViReplaceToChar, "ViReplaceToChar") },
248+
{ Keys.ucF, MakeViKeyHandler( ViReplaceToCharBackward, "ViReplaceToCharBackward") },
249+
{ Keys.T, MakeViKeyHandler( ViReplaceToBeforeChar, "ViReplaceToBeforeChar") },
250+
{ Keys.ucT, MakeViKeyHandler( ViReplaceToBeforeCharBackward, "ViReplaceToBeforeCharBackward") },
243251
};
244252
private static readonly Dictionary<ConsoleKeyInfo, KeyHandler> _viChordYTable = new Dictionary<ConsoleKeyInfo, KeyHandler>(new ConsoleKeyInfoComparer())
245253
{

PSReadLine/ReadLine.vi.cs

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ public static void Search(char keyChar, object arg, bool backoff)
7676
Ding();
7777
}
7878

79+
public static bool SearchDelete(char keyChar, object arg, bool backoff, Action<ConsoleKeyInfo?, object> instigator)
80+
{
81+
int qty = (arg is int) ? (int) arg : 1;
82+
83+
for (int i = _singleton._current + 1; i < _singleton._buffer.Length; i++)
84+
{
85+
if (_singleton._buffer[i] == keyChar)
86+
{
87+
qty -= 1;
88+
if (qty == 0)
89+
{
90+
DeleteToEndPoint(arg, backoff ? i : i + 1, instigator);
91+
return true;
92+
}
93+
}
94+
}
95+
Ding();
96+
return false;
97+
}
98+
7999
public static void SearchBackward(char keyChar, object arg, bool backoff)
80100
{
81101
Set(keyChar, isBackward: true, isBackoff: backoff);
@@ -96,6 +116,27 @@ public static void SearchBackward(char keyChar, object arg, bool backoff)
96116
}
97117
Ding();
98118
}
119+
120+
public static bool SearchBackwardDelete(char keyChar, object arg, bool backoff, Action<ConsoleKeyInfo?, object> instigator)
121+
{
122+
Set(keyChar, isBackward: true, isBackoff: backoff);
123+
int qty = (arg is int) ? (int) arg : 1;
124+
125+
for (int i = _singleton._current - 1; i >= 0; i--)
126+
{
127+
if (_singleton._buffer[i] == keyChar)
128+
{
129+
qty -= 1;
130+
if (qty == 0)
131+
{
132+
DeleteBackwardToEndPoint(arg, backoff ? i + 1 : i, instigator);
133+
return true;
134+
}
135+
}
136+
}
137+
Ding();
138+
return false;
139+
}
99140
}
100141

101142
/// <summary>
@@ -241,11 +282,17 @@ public static void DeleteWord(ConsoleKeyInfo? key = null, object arg = null)
241282
Ding();
242283
return;
243284
}
285+
286+
DeleteToEndPoint(arg, endPoint, DeleteWord);
287+
}
288+
289+
private static void DeleteToEndPoint(object arg, int endPoint, Action<ConsoleKeyInfo?, object> instigator)
290+
{
244291
_singleton.SaveToClipboard(_singleton._current, endPoint - _singleton._current);
245292
_singleton.SaveEditItem(EditItemDelete.Create(
246293
_singleton._clipboard,
247294
_singleton._current,
248-
DeleteWord,
295+
instigator,
249296
arg
250297
));
251298
_singleton._buffer.Remove(_singleton._current, endPoint - _singleton._current);
@@ -256,6 +303,22 @@ public static void DeleteWord(ConsoleKeyInfo? key = null, object arg = null)
256303
_singleton.Render();
257304
}
258305

306+
private static void DeleteBackwardToEndPoint(object arg, int endPoint, Action<ConsoleKeyInfo?, object> instigator)
307+
{
308+
int deleteLength = _singleton._current - endPoint + 1;
309+
310+
_singleton.SaveToClipboard(endPoint, deleteLength);
311+
_singleton.SaveEditItem(EditItemDelete.Create(
312+
_singleton._clipboard,
313+
endPoint,
314+
instigator,
315+
arg
316+
));
317+
_singleton._buffer.Remove(endPoint, deleteLength);
318+
_singleton._current = endPoint;
319+
_singleton.Render();
320+
}
321+
259322
/// <summary>
260323
/// Delete the next glob (white space delimited word).
261324
/// </summary>
@@ -343,6 +406,77 @@ public static void ViDeleteEndOfGlob(ConsoleKeyInfo? key = null, object arg = nu
343406
_singleton.Render();
344407
}
345408

409+
/// <summary>
410+
/// Deletes until given character
411+
/// </summary>
412+
/// <param name="key"></param>
413+
/// <param name="arg"></param>
414+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
415+
private static void ViDeleteToChar(ConsoleKeyInfo? key = null, object arg = null)
416+
{
417+
var keyChar = ReadKey().KeyChar;
418+
ViDeleteToChar(keyChar, key, arg);
419+
}
420+
421+
private static void ViDeleteToChar(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
422+
{
423+
ViCharacterSearcher.Set(keyChar, isBackward: false, isBackoff: false);
424+
ViCharacterSearcher.SearchDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViDeleteToChar(keyChar, _key, _arg));
425+
}
426+
427+
/// <summary>
428+
/// Deletes until given character
429+
/// </summary>
430+
/// <param name="key"></param>
431+
/// <param name="arg"></param>
432+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
433+
private static void ViDeleteToCharBackward(ConsoleKeyInfo? key = null, object arg = null)
434+
{
435+
var keyChar = ReadKey().KeyChar;
436+
ViDeleteToCharBack(keyChar, key, arg);
437+
}
438+
439+
private static void ViDeleteToCharBack(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
440+
{
441+
ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViDeleteToCharBack(keyChar, _key, _arg));
442+
}
443+
444+
/// <summary>
445+
/// Deletes until given character
446+
/// </summary>
447+
/// <param name="key"></param>
448+
/// <param name="arg"></param>
449+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
450+
private static void ViDeleteToBeforeChar(ConsoleKeyInfo? key = null, object arg = null)
451+
{
452+
var keyChar = ReadKey().KeyChar;
453+
ViDeleteToBeforeChar(keyChar, key, arg);
454+
}
455+
456+
private static void ViDeleteToBeforeChar(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
457+
{
458+
ViCharacterSearcher.Set(keyChar, isBackward: false, isBackoff: true);
459+
ViCharacterSearcher.SearchDelete(keyChar, arg, backoff: true, instigator: (_key, _arg) => ViDeleteToBeforeChar(keyChar, _key, _arg));
460+
}
461+
462+
/// <summary>
463+
/// Deletes until given character
464+
/// </summary>
465+
/// <param name="key"></param>
466+
/// <param name="arg"></param>
467+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
468+
private static void ViDeleteToBeforeCharBackward(ConsoleKeyInfo? key = null, object arg = null)
469+
{
470+
var keyChar = ReadKey().KeyChar;
471+
ViDeleteToBeforeCharBack(keyChar, key, arg);
472+
}
473+
474+
private static void ViDeleteToBeforeCharBack(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
475+
{
476+
ViCharacterSearcher.Set(keyChar, isBackward: true, isBackoff: true);
477+
ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: true, instigator: (_key, _arg) => ViDeleteToBeforeCharBack(keyChar, _key, _arg));
478+
}
479+
346480
/// <summary>
347481
/// Ring the bell.
348482
/// </summary>

PSReadLine/Replace.vi.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,102 @@ private static void ReplaceCharInPlace(ConsoleKeyInfo? key, object arg)
245245
Ding();
246246
}
247247
}
248+
249+
/// <summary>
250+
/// Deletes until given character
251+
/// </summary>
252+
/// <param name="key"></param>
253+
/// <param name="arg"></param>
254+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
255+
private static void ViReplaceToChar(ConsoleKeyInfo? key = null, object arg = null)
256+
{
257+
var keyChar = ReadKey().KeyChar;
258+
ViReplaceToChar(keyChar, key, arg);
259+
}
260+
261+
private static void ViReplaceToChar(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
262+
{
263+
bool shouldAppend = _singleton._current > 0;
264+
265+
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
266+
ViCharacterSearcher.Set(keyChar, isBackward: false, isBackoff: false);
267+
if (ViCharacterSearcher.SearchDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViReplaceToChar(keyChar, _key, _arg)))
268+
{
269+
if (shouldAppend)
270+
{
271+
ViInsertWithAppend(key, arg);
272+
}
273+
else
274+
{
275+
ViInsertMode(key, arg);
276+
}
277+
}
278+
}
279+
280+
/// <summary>
281+
/// Replaces until given character
282+
/// </summary>
283+
/// <param name="key"></param>
284+
/// <param name="arg"></param>
285+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
286+
private static void ViReplaceToCharBackward(ConsoleKeyInfo? key = null, object arg = null)
287+
{
288+
var keyChar = ReadKey().KeyChar;
289+
ViReplaceToCharBack(keyChar, key, arg);
290+
}
291+
292+
private static void ViReplaceToCharBack(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
293+
{
294+
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
295+
if (ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViReplaceToCharBack(keyChar, _key, _arg)))
296+
{
297+
ViInsertMode(key, arg);
298+
}
299+
}
300+
301+
/// <summary>
302+
/// Replaces until given character
303+
/// </summary>
304+
/// <param name="key"></param>
305+
/// <param name="arg"></param>
306+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
307+
private static void ViReplaceToBeforeChar(ConsoleKeyInfo? key = null, object arg = null)
308+
{
309+
var keyChar = ReadKey().KeyChar;
310+
ViReplaceToBeforeChar(keyChar, key, arg);
311+
}
312+
313+
private static void ViReplaceToBeforeChar(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
314+
{
315+
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
316+
ViCharacterSearcher.Set(keyChar, isBackward: false, isBackoff: true);
317+
if (ViCharacterSearcher.SearchDelete(keyChar, arg, backoff: true, instigator: (_key, _arg) => ViReplaceToBeforeChar(keyChar, _key, _arg)))
318+
{
319+
ViInsertMode(key, arg);
320+
}
321+
}
322+
323+
/// <summary>
324+
/// Replaces until given character
325+
/// </summary>
326+
/// <param name="key"></param>
327+
/// <param name="arg"></param>
328+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
329+
private static void ViReplaceToBeforeCharBackward(ConsoleKeyInfo? key = null, object arg = null)
330+
{
331+
var keyChar = ReadKey().KeyChar;
332+
ViReplaceToBeforeCharBack(keyChar, key, arg);
333+
}
334+
335+
private static void ViReplaceToBeforeCharBack(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
336+
{
337+
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
338+
if (ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: true, instigator: (_key, _arg) => ViReplaceToBeforeCharBack(keyChar, _key, _arg)))
339+
{
340+
ViInsertMode(key, arg);
341+
}
342+
}
343+
344+
248345
}
249346
}

0 commit comments

Comments
 (0)