forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKillYank.cs
More file actions
565 lines (514 loc) · 21.6 KB
/
KillYank.cs
File metadata and controls
565 lines (514 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation.Language;
#if !CORECLR // TODO: clipboard
using System.Windows.Forms;
#endif
namespace Microsoft.PowerShell
{
public partial class PSConsoleReadLine
{
// Yank/Kill state
private List<string> _killRing;
private int _killIndex;
private int _killCommandCount;
private int _yankCommandCount;
private int _yankStartPoint;
private int _yankLastArgCommandCount;
class YankLastArgState
{
internal int argument;
internal int historyIndex;
internal int historyIncrement;
internal int startPoint = -1;
}
private YankLastArgState _yankLastArgState;
private int _visualSelectionCommandCount;
/// <summary>
/// Mark the current location of the cursor for use in a subsequent editing command.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SetMark(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton._mark = _singleton._current;
}
/// <summary>
/// The cursor is placed at the location of the mark and the mark is moved
/// to the location of the cursor.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void ExchangePointAndMark(ConsoleKeyInfo? key = null, object arg = null)
{
var tmp = _singleton._mark;
_singleton._mark = _singleton._current;
_singleton._current = tmp;
_singleton.PlaceCursor();
}
/// <summary>
/// The contents of the kill ring are cleared.
/// </summary>
public static void ClearKillRing()
{
if (_singleton._killRing != null)
{
_singleton._killRing.Clear();
}
_singleton._killIndex = -1; // So first add indexes 0.
}
private void Kill(int start, int length, bool prepend)
{
if (length > 0)
{
var killText = _buffer.ToString(start, length);
SaveEditItem(EditItemDelete.Create(killText, start));
_buffer.Remove(start, length);
_current = start;
Render();
if (_killCommandCount > 0)
{
if (prepend)
{
_killRing[_killIndex] = killText + _killRing[_killIndex];
}
else
{
_killRing[_killIndex] += killText;
}
}
else
{
if (_killRing.Count < Options.MaximumKillRingCount)
{
_killRing.Add(killText);
_killIndex = _killRing.Count - 1;
}
else
{
_killIndex += 1;
if (_killIndex == _killRing.Count)
{
_killIndex = 0;
}
_killRing[_killIndex] = killText;
}
}
}
_killCommandCount += 1;
}
/// <summary>
/// Clear the input from the cursor to the end of the input. The cleared text is placed
/// in the kill ring.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void KillLine(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.Kill(_singleton._current, _singleton._buffer.Length - _singleton._current, false);
}
/// <summary>
/// Clear the input from the start of the input to the cursor. The cleared text is placed
/// in the kill ring.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void BackwardKillLine(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.Kill(0, _singleton._current, true);
}
/// <summary>
/// Clear the input from the cursor to the end of the current word. If the cursor
/// is between words, the input is cleared from the cursor to the end of the next word.
/// The cleared text is placed in the kill ring.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void KillWord(ConsoleKeyInfo? key = null, object arg = null)
{
int i = _singleton.FindForwardWordPoint(_singleton.Options.WordDelimiters);
_singleton.Kill(_singleton._current, i - _singleton._current, false);
}
/// <summary>
/// Clear the input from the cursor to the end of the current word. If the cursor
/// is between words, the input is cleared from the cursor to the end of the next word.
/// The cleared text is placed in the kill ring.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void ShellKillWord(ConsoleKeyInfo? key = null, object arg = null)
{
var token = _singleton.FindToken(_singleton._current, FindTokenMode.CurrentOrNext);
var end = (token.Kind == TokenKind.EndOfInput)
? _singleton._buffer.Length
: token.Extent.EndOffset;
_singleton.Kill(_singleton._current, end - _singleton._current, false);
}
/// <summary>
/// Clear the input from the start of the current word to the cursor. If the cursor
/// is between words, the input is cleared from the start of the previous word to the
/// cursor. The cleared text is placed in the kill ring.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void BackwardKillWord(ConsoleKeyInfo? key = null, object arg = null)
{
int i = _singleton.FindBackwardWordPoint(_singleton.Options.WordDelimiters);
_singleton.Kill(i, _singleton._current - i, true);
}
/// <summary>
/// Clear the input from the start of the current word to the cursor. If the cursor
/// is between words, the input is cleared from the start of the previous word to the
/// cursor. The cleared text is placed in the kill ring.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void UnixWordRubout(ConsoleKeyInfo? key = null, object arg = null)
{
int i = _singleton.FindBackwardWordPoint("");
_singleton.Kill(i, _singleton._current - i, true);
}
/// <summary>
/// Clear the input from the start of the current word to the cursor. If the cursor
/// is between words, the input is cleared from the start of the previous word to the
/// cursor. The cleared text is placed in the kill ring.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void ShellBackwardKillWord(ConsoleKeyInfo? key = null, object arg = null)
{
var token = _singleton.FindToken(_singleton._current, FindTokenMode.Previous);
var start = token == null
? 0
: token.Extent.StartOffset;
_singleton.Kill(start, _singleton._current - start, true);
}
/// <summary>
/// Kill the text between the cursor and the mark.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void KillRegion(ConsoleKeyInfo? key = null, object arg = null)
{
int start, length;
_singleton.GetRegion(out start, out length);
_singleton.Kill(start, length, true);
}
private void YankImpl()
{
if (_killRing.Count == 0)
return;
// Starting a yank session, yank the last thing killed and
// remember where we started.
_mark = _yankStartPoint = _current;
Insert(_killRing[_killIndex]);
_yankCommandCount += 1;
}
/// <summary>
/// Add the most recently killed text to the input.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void Yank(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.YankImpl();
}
private void YankPopImpl()
{
if (_yankCommandCount == 0)
return;
_killIndex -= 1;
if (_killIndex < 0)
{
_killIndex = _killRing.Count - 1;
}
var yankText = _killRing[_killIndex];
Replace(_yankStartPoint, _current - _yankStartPoint, yankText);
_yankCommandCount += 1;
}
/// <summary>
/// If the previous operation was Yank or YankPop, replace the previously yanked
/// text with the next killed text from the kill ring.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void YankPop(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.YankPopImpl();
}
void YankArgImpl(YankLastArgState yankLastArgState)
{
if (yankLastArgState.historyIndex < 0 || yankLastArgState.historyIndex >= _history.Count)
{
Ding();
return;
}
Token[] tokens;
ParseError[] errors;
var buffer = _history[yankLastArgState.historyIndex];
Parser.ParseInput(buffer._line, out tokens, out errors);
int arg = (yankLastArgState.argument < 0)
? tokens.Length + yankLastArgState.argument - 1
: yankLastArgState.argument;
if (arg < 0 || arg >= tokens.Length)
{
Ding();
return;
}
var argText = tokens[arg].Text;
if (yankLastArgState.startPoint < 0)
{
yankLastArgState.startPoint = _current;
Insert(argText);
}
else
{
Replace(yankLastArgState.startPoint, _current - yankLastArgState.startPoint, argText);
}
}
/// <summary>
/// Yank the first argument (after the command) from the previous history line.
/// With an argument, yank the nth argument (starting from 0), if the argument
/// is negative, start from the last argument.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void YankNthArg(ConsoleKeyInfo? key = null, object arg = null)
{
var yankLastArgState = new YankLastArgState
{
argument = (arg is int) ? (int)arg : 1,
historyIndex = _singleton._currentHistoryIndex - 1,
};
_singleton.YankArgImpl(yankLastArgState);
}
/// <summary>
/// Yank the last argument from the previous history line. With an argument,
/// the first time it is invoked, behaves just like YankNthArg. If invoked
/// multiple times, instead it iterates through history and arg sets the direction
/// (negative reverses the direction.)
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void YankLastArg(ConsoleKeyInfo? key = null, object arg = null)
{
if (arg != null && !(arg is int))
{
Ding();
return;
}
_singleton._yankLastArgCommandCount += 1;
if (_singleton._yankLastArgCommandCount == 1)
{
_singleton._yankLastArgState = new YankLastArgState
{
argument = (arg != null) ? (int)arg : -1,
historyIncrement = -1,
historyIndex = _singleton._currentHistoryIndex - 1
};
_singleton.YankArgImpl(_singleton._yankLastArgState);
return;
}
var yankLastArgState = _singleton._yankLastArgState;
if (arg != null)
{
if ((int)arg < 0)
{
yankLastArgState.historyIncrement = -yankLastArgState.historyIncrement;
}
}
yankLastArgState.historyIndex += yankLastArgState.historyIncrement;
// Don't increment more than 1 out of range so it's quick to get back to being in range.
if (yankLastArgState.historyIndex < 0)
{
Ding();
yankLastArgState.historyIndex = 0;
}
else if (yankLastArgState.historyIndex >= _singleton._history.Count)
{
Ding();
yankLastArgState.historyIndex = _singleton._history.Count - 1;
}
else
{
_singleton.YankArgImpl(yankLastArgState);
}
}
private void VisualSelectionCommon(Action action)
{
if (_singleton._visualSelectionCommandCount == 0)
{
SetMark();
}
_singleton._visualSelectionCommandCount += 1;
action();
_singleton.Render();
}
/// <summary>
/// Adjust the current selection to include the previous character
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectBackwardChar(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => BackwardChar(key, arg));
}
/// <summary>
/// Adjust the current selection to include the next character
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectForwardChar(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => ForwardChar(key, arg));
}
/// <summary>
/// Adjust the current selection to include the previous word
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectBackwardWord(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => BackwardWord(key, arg));
}
/// <summary>
/// Adjust the current selection to include the next word
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectNextWord(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => NextWord(key, arg));
}
/// <summary>
/// Adjust the current selection to include the next word using ForwardWord
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectForwardWord(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => ForwardWord(key, arg));
}
/// <summary>
/// Adjust the current selection to include the next word using ShellForwardWord
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectShellForwardWord(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => ShellForwardWord(key, arg));
}
/// <summary>
/// Adjust the current selection to include the next word using ShellNextWord
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectShellNextWord(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => ShellNextWord(key, arg));
}
/// <summary>
/// Adjust the current selection to include the previous word using ShellBackwardWord
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectShellBackwardWord(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => ShellBackwardWord(key, arg));
}
/// <summary>
/// Select the entire line
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectAll(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton._visualSelectionCommandCount += 1;
_singleton._mark = 0;
_singleton._current = _singleton._buffer.Length;
_singleton.Render();
}
/// <summary>
/// Adjust the current selection to include from the cursor to the end of the line
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectLine(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => EndOfLine(key, arg));
}
/// <summary>
/// Adjust the current selection to include from the cursor to the start of the line
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void SelectBackwardsLine(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.VisualSelectionCommon(() => BeginningOfLine(key, arg));
}
/// <summary>
/// Paste text from the system clipboard.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void Paste(ConsoleKeyInfo? key = null, object arg = null)
{
#if !CORECLR // TODO: clipboard
string textToPaste = null;
ExecuteOnSTAThread(() => {
if (Clipboard.ContainsText())
{
textToPaste = Clipboard.GetText();
}
});
if (textToPaste != null)
{
textToPaste = textToPaste.Replace("\r", "");
if (_singleton._visualSelectionCommandCount > 0)
{
int start, length;
_singleton.GetRegion(out start, out length);
Replace(start, length, textToPaste);
}
else
{
Insert(textToPaste);
}
}
#endif
}
/// <summary>
/// Copy selected region to the system clipboard. If no region is selected, copy the whole line.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void Copy(ConsoleKeyInfo? key = null, object arg = null)
{
#if !CORECLR // TODO: clipboard
string textToSet;
if (_singleton._visualSelectionCommandCount > 0)
{
int start, length;
_singleton.GetRegion(out start, out length);
textToSet = _singleton._buffer.ToString(start, length);
}
else
{
textToSet = _singleton._buffer.ToString();
}
if (!string.IsNullOrEmpty(textToSet))
{
ExecuteOnSTAThread(() => Clipboard.SetText(textToSet));
}
#endif
}
/// <summary>
/// If text is selected, copy to the clipboard, otherwise cancel the line.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void CopyOrCancelLine(ConsoleKeyInfo? key = null, object arg = null)
{
if (_singleton._visualSelectionCommandCount > 0)
{
Copy(key, arg);
}
else
{
CancelLine(key, arg);
}
}
/// <summary>
/// Delete selected region placing deleted text in the system clipboard.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public static void Cut(ConsoleKeyInfo? key = null, object arg = null)
{
if (_singleton._visualSelectionCommandCount > 0)
{
int start, length;
_singleton.GetRegion(out start, out length);
#if !CORECLR // TODO: clipboard
ExecuteOnSTAThread(() => Clipboard.SetText(_singleton._buffer.ToString(start, length)));
#endif
Delete(start, length);
}
}
}
}