From ab83e004752875742ccfd8632b10455a8875a475 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 15 Jul 2016 13:38:55 -0700 Subject: [PATCH 1/4] Refactor switch into SetDefaultBindings(EditMode) --- .../KeyBindings.cs | 19 +++++++++++++++++++ .../Options.cs | 13 +------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.PSReadLine/KeyBindings.cs b/src/Microsoft.PowerShell.PSReadLine/KeyBindings.cs index aae97e503..b543aef8a 100644 --- a/src/Microsoft.PowerShell.PSReadLine/KeyBindings.cs +++ b/src/Microsoft.PowerShell.PSReadLine/KeyBindings.cs @@ -114,6 +114,25 @@ namespace Microsoft.PowerShell private Dictionary _dispatchTable; private Dictionary> _chordDispatchTable; + /// + /// Helper to set bindings based on EditMode + /// + void SetDefaultBindings(EditMode editMode) + { + switch (editMode) + { + case EditMode.Emacs: + SetDefaultEmacsBindings(); + break; + case EditMode.Vi: + SetDefaultViBindings(); + break; + case EditMode.Windows: + SetDefaultWindowsBindings(); + break; + } + } + void SetDefaultWindowsBindings() { _dispatchTable = new Dictionary(new ConsoleKeyInfoComparer()) diff --git a/src/Microsoft.PowerShell.PSReadLine/Options.cs b/src/Microsoft.PowerShell.PSReadLine/Options.cs index d8162b743..b927282e2 100644 --- a/src/Microsoft.PowerShell.PSReadLine/Options.cs +++ b/src/Microsoft.PowerShell.PSReadLine/Options.cs @@ -96,18 +96,7 @@ namespace Microsoft.PowerShell // Switching/resetting modes - clear out chord dispatch table _chordDispatchTable.Clear(); - switch (options._editMode) - { - case EditMode.Emacs: - SetDefaultEmacsBindings(); - break; - case EditMode.Vi: - SetDefaultViBindings(); - break; - case EditMode.Windows: - SetDefaultWindowsBindings(); - break; - } + SetDefaultBindings(Options.EditMode); } if (options._showToolTips.HasValue) { From b3ef98036d4ca52f94c8d80b1343a064cf8a7ede Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 15 Jul 2016 13:39:29 -0700 Subject: [PATCH 2/4] Set bindings based on options This is instead of hard-coding a call to SetDefaultWindowsBindings(). --- src/Microsoft.PowerShell.PSReadLine/ReadLine.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.PSReadLine/ReadLine.cs b/src/Microsoft.PowerShell.PSReadLine/ReadLine.cs index c2848f054..75654e21d 100644 --- a/src/Microsoft.PowerShell.PSReadLine/ReadLine.cs +++ b/src/Microsoft.PowerShell.PSReadLine/ReadLine.cs @@ -485,8 +485,6 @@ namespace Microsoft.PowerShell _console = new ConhostConsole(); #endif - SetDefaultWindowsBindings(); - _buffer = new StringBuilder(8 * 1024); _statusBuffer = new StringBuilder(256); _savedCurrentLine = new HistoryItem(); @@ -516,6 +514,7 @@ namespace Microsoft.PowerShell hostName = "PSReadline"; } _options = new PSConsoleReadlineOptions(hostName); + SetDefaultBindings(_options.EditMode); } private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics) From c6758603ae71509bb2d7e865fbeb445220198d7b Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 15 Jul 2016 13:40:16 -0700 Subject: [PATCH 3/4] Set DefaultEditMode to Emacs on Linux / OS X --- src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs b/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs index 514eed8ed..fbd0c867c 100644 --- a/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs +++ b/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs @@ -92,7 +92,12 @@ namespace Microsoft.PowerShell public const ConsoleColor DefaultEmphasisForegroundColor = ConsoleColor.Cyan; public const ConsoleColor DefaultErrorForegroundColor = ConsoleColor.Red; - public const EditMode DefaultEditMode = EditMode.Windows; + public const EditMode DefaultEditMode = +#if LINUX + EditMode.Emacs; +#else + EditMode.Windows; +#endif public const string DefaultContinuationPrompt = ">> "; From b745d75f1b9828dcd0a850d6726d0d46edfbf989 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 15 Jul 2016 13:43:29 -0700 Subject: [PATCH 4/4] Test PSReadLine's DefaultEditMode and bindings --- .../powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 b/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 index 7821fca3e..0173b9169 100644 --- a/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 +++ b/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 @@ -13,6 +13,16 @@ Describe "PSReadLine" { $module.Version | Should Be "1.2" } + It "Should use Emacs Bindings on Linux and OS X" -skip:$IsWindows { + (Get-PSReadLineOption).EditMode | Should Be Emacs + (Get-PSReadlineKeyHandler | where { $_.Key -eq "Ctrl+A" }).Function | Should Be BeginningOfLine + } + + It "Should use Windows Bindings on Windows" -skip:(-not $IsWindows) { + (Get-PSReadLineOption).EditMode | Should Be Windows + (Get-PSReadlineKeyHandler | where { $_.Key -eq "Ctrl+A" }).Function | Should Be SelectAll + } + It "Should set the edit mode" { Set-PSReadlineOption -EditMode Windows (Get-PSReadlineKeyHandler | where { $_.Key -eq "Ctrl+A" }).Function | Should Be SelectAll