Skip to content

Commit 8b15294

Browse files
committed
Merge pull request #309 from PowerShell/master
Merge vi mode and other stuff
2 parents f807c77 + 2c780b9 commit 8b15294

33 files changed

+6786
-127
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ ClientBin
105105
*.dbmdl
106106
Generated_Code #added for RIA/Silverlight projects
107107
*.bak*
108+
PSReadLine/CopyDLL.cmd
108109

109110
# Backup & report files from converting an old project file to a newer
110111
# Visual Studio version. Backup files are not needed, because we have git ;-)

PSReadLine/BasicEditing.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ public static void BackwardDeleteLine(ConsoleKeyInfo? key = null, object arg = n
143143
{
144144
if (_singleton._current > 0)
145145
{
146-
var str = _singleton._buffer.ToString(0, _singleton._current);
147-
_singleton.SaveEditItem(EditItemDelete.Create(str, 0));
146+
_singleton._clipboard = _singleton._buffer.ToString(0, _singleton._current);
147+
_singleton.SaveEditItem(EditItemDelete.Create(_singleton._clipboard, 0));
148148
_singleton._buffer.Remove(0, _singleton._current);
149149
_singleton._current = 0;
150150
_singleton.Render();
@@ -167,17 +167,32 @@ public static void BackwardDeleteChar(ConsoleKeyInfo? key = null, object arg = n
167167

168168
if (_singleton._buffer.Length > 0 && _singleton._current > 0)
169169
{
170-
int startDeleteIndex = _singleton._current - 1;
170+
int qty = (arg is int) ? (int) arg : 1;
171+
qty = Math.Min(qty, _singleton._current);
172+
173+
int startDeleteIndex = _singleton._current - qty;
171174
_singleton.SaveEditItem(
172-
EditItemDelete.Create(new string(_singleton._buffer[startDeleteIndex], 1), startDeleteIndex));
173-
_singleton._buffer.Remove(startDeleteIndex, 1);
174-
_singleton._current--;
175+
EditItemDelete.Create(
176+
_singleton._buffer.ToString(startDeleteIndex, qty),
177+
startDeleteIndex,
178+
BackwardDeleteChar,
179+
arg)
180+
);
181+
_singleton.SaveToClipboard(startDeleteIndex, qty);
182+
_singleton._buffer.Remove(startDeleteIndex, qty);
183+
_singleton._current = startDeleteIndex;
175184
_singleton.Render();
176185
}
186+
else
187+
{
188+
Ding();
189+
}
177190
}
178191

179-
private void DeleteCharImpl(bool orExit)
192+
private void DeleteCharImpl(int qty, bool orExit)
180193
{
194+
qty = Math.Min(qty, _singleton._buffer.Length + 1 + ViEndOfLineFactor - _singleton._current);
195+
181196
if (_visualSelectionCommandCount > 0)
182197
{
183198
int start, length;
@@ -190,8 +205,13 @@ private void DeleteCharImpl(bool orExit)
190205
{
191206
if (_current < _buffer.Length)
192207
{
193-
SaveEditItem(EditItemDelete.Create(new string(_buffer[_current], 1), _current));
194-
_buffer.Remove(_current, 1);
208+
SaveEditItem(EditItemDelete.Create(_buffer.ToString(_current, qty), _current, DeleteChar, qty));
209+
SaveToClipboard(_current, qty);
210+
_buffer.Remove(_current, qty);
211+
if (_current >= _buffer.Length)
212+
{
213+
_current = Math.Max(0, _buffer.Length - 1);
214+
}
195215
Render();
196216
}
197217
}
@@ -207,7 +227,9 @@ private void DeleteCharImpl(bool orExit)
207227
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
208228
public static void DeleteChar(ConsoleKeyInfo? key = null, object arg = null)
209229
{
210-
_singleton.DeleteCharImpl(orExit: false);
230+
int qty = (arg is int) ? (int)arg : 1;
231+
232+
_singleton.DeleteCharImpl(qty, orExit: false);
211233
}
212234

213235
/// <summary>
@@ -216,7 +238,7 @@ public static void DeleteChar(ConsoleKeyInfo? key = null, object arg = null)
216238
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
217239
public static void DeleteCharOrExit(ConsoleKeyInfo? key = null, object arg = null)
218240
{
219-
_singleton.DeleteCharImpl(orExit: true);
241+
_singleton.DeleteCharImpl(1, orExit: true);
220242
}
221243

222244
private bool AcceptLineImpl(bool validate)

PSReadLine/Cmdlets.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ public enum EditMode
3535
{
3636
Windows,
3737
Emacs,
38-
#if FALSE
3938
Vi,
40-
#endif
4139
}
4240

4341
public enum BellStyle
@@ -47,6 +45,15 @@ public enum BellStyle
4745
Audible
4846
}
4947

48+
#region vi
49+
public enum ViModeStyle
50+
{
51+
None,
52+
Prompt,
53+
Cursor
54+
}
55+
#endregion vi
56+
5057
public enum HistorySaveStyle
5158
{
5259
SaveIncrementally,
@@ -106,7 +113,7 @@ public class PSConsoleReadlineOptions
106113
public const int DefaultCompletionQueryItems = 100;
107114

108115
// Default includes all characters PowerShell treats like a dash - em dash, en dash, horizontal bar
109-
public const string DefaultWordDelimiters = @";:,.[]{}()/\|^&*-=+" + "\u2013\u2014\u2015";
116+
public const string DefaultWordDelimiters = @";:,.[]{}()/\|^&*-=+'""" + "\u2013\u2014\u2015";
110117

111118
/// <summary>
112119
/// When ringing the bell, what should be done?
@@ -225,6 +232,10 @@ internal StringComparison HistoryStringComparison
225232
get { return HistorySearchCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; }
226233
}
227234

235+
#region vi
236+
public ViModeStyle ViModeIndicator { get; set; }
237+
#endregion vi
238+
228239
/// <summary>
229240
/// The path to the saved history.
230241
/// </summary>
@@ -538,6 +549,16 @@ public HistorySaveStyle HistorySaveStyle
538549
[ValidateNotNullOrEmpty]
539550
public string HistorySavePath { get; set; }
540551

552+
#region vi
553+
[Parameter(ParameterSetName = "OptionsSet")]
554+
public ViModeStyle ViModeIndicator
555+
{
556+
get { return _viModeIndicator.GetValueOrDefault(); }
557+
set { _viModeIndicator = value; }
558+
}
559+
internal ViModeStyle? _viModeIndicator;
560+
#endregion vi
561+
541562
[Parameter(ParameterSetName = "ColorSet", Position = 0, Mandatory = true)]
542563
public TokenClassification TokenKind
543564
{

PSReadLine/Completion.vi.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
7+
using System.Management.Automation;
8+
using System.Management.Automation.Runspaces;
9+
using System.Text;
10+
11+
namespace Microsoft.PowerShell
12+
{
13+
public partial class PSConsoleReadLine
14+
{
15+
/// <summary>
16+
/// Ends the current edit group, if needed, and invokes TabCompleteNext.
17+
/// </summary>
18+
public static void ViTabCompleteNext(ConsoleKeyInfo? key = null, object arg = null)
19+
{
20+
if (_singleton._editGroupStart >= 0)
21+
{
22+
_singleton._groupUndoHelper.EndGroup();
23+
}
24+
TabCompleteNext(key, arg);
25+
}
26+
27+
/// <summary>
28+
/// Ends the current edit group, if needed, and invokes TabCompletePrevious.
29+
/// </summary>
30+
public static void ViTabCompletePrevious(ConsoleKeyInfo? key = null, object arg = null)
31+
{
32+
if (_singleton._editGroupStart >= 0)
33+
{
34+
_singleton._groupUndoHelper.EndGroup();
35+
}
36+
TabCompletePrevious(key, arg);
37+
}
38+
}
39+
}

PSReadLine/ConsoleKeyChordConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ private static ConsoleKeyInfo ConvertOneSequence(string sequence)
137137
}
138138
else
139139
{
140-
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, PSReadLineResources.InvalidModifier, token));
140+
throw new ArgumentException(
141+
String.Format(CultureInfo.CurrentCulture, PSReadLineResources.InvalidModifier, token, key));
141142
}
142143
}
143144
}

PSReadLine/ConsoleLib.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,64 @@ IntPtr templateFileWin32Handle
105105

106106
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
107107
internal static extern bool GetCurrentConsoleFontEx(IntPtr consoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFO_EX consoleFontInfo);
108+
109+
[StructLayout(LayoutKind.Sequential)]
110+
internal struct COLORREF
111+
{
112+
internal uint ColorDWORD;
113+
114+
internal uint R
115+
{
116+
get { return ColorDWORD & 0xff; }
117+
}
118+
119+
internal uint G
120+
{
121+
get { return (ColorDWORD >> 8) & 0xff; }
122+
}
123+
124+
internal uint B
125+
{
126+
get { return (ColorDWORD >> 16) & 0xff; }
127+
}
128+
}
129+
130+
[StructLayout(LayoutKind.Sequential)]
131+
internal struct CONSOLE_SCREEN_BUFFER_INFO_EX
132+
{
133+
internal int cbSize;
134+
internal COORD dwSize;
135+
internal COORD dwCursorPosition;
136+
internal ushort wAttributes;
137+
internal SMALL_RECT srWindow;
138+
internal COORD dwMaximumWindowSize;
139+
internal ushort wPopupAttributes;
140+
internal bool bFullscreenSupported;
141+
internal COLORREF Black;
142+
internal COLORREF DarkBlue;
143+
internal COLORREF DarkGreen;
144+
internal COLORREF DarkCyan;
145+
internal COLORREF DarkRed;
146+
internal COLORREF DarkMagenta;
147+
internal COLORREF DarkYellow;
148+
internal COLORREF Gray;
149+
internal COLORREF DarkGray;
150+
internal COLORREF Blue;
151+
internal COLORREF Green;
152+
internal COLORREF Cyan;
153+
internal COLORREF Red;
154+
internal COLORREF Magenta;
155+
internal COLORREF Yellow;
156+
internal COLORREF White;
157+
}
158+
159+
[DllImport("kernel32.dll", SetLastError = true)]
160+
internal static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput,
161+
ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);
162+
163+
[DllImport("kernel32.dll", SetLastError = true)]
164+
internal static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput,
165+
ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);
108166
}
109167

110168
public delegate bool BreakHandler(ConsoleBreakSignal ConsoleBreakSignal);

PSReadLine/History.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@ private void UpdateFromHistory(bool moveCursor)
280280
_buffer.Append(line);
281281
if (moveCursor)
282282
{
283-
_current = _buffer.Length;
283+
_current = _options.EditMode == EditMode.Vi ? 0 : Math.Max(0, _buffer.Length + ViEndOfLineFactor);
284284
}
285285
else if (_current > _buffer.Length)
286286
{
287-
_current = _buffer.Length;
287+
_current = Math.Max(0, _buffer.Length + ViEndOfLineFactor);
288288
}
289289
Render();
290290
}

0 commit comments

Comments
 (0)