@@ -52,6 +52,12 @@ public enum ViModeStyle
5252 Prompt ,
5353 Cursor
5454 }
55+
56+ public enum ViMode
57+ {
58+ Insert ,
59+ Command
60+ }
5561 #endregion vi
5662
5763 public enum HistorySaveStyle
@@ -590,15 +596,46 @@ protected override void EndProcessing()
590596 }
591597 }
592598
593- [ Cmdlet ( "Set" , "PSReadlineKeyHandler" , HelpUri = "http://go.microsoft.com/fwlink/?LinkId=528810" ) ]
594- public class SetPSReadlineKeyHandlerCommand : PSCmdlet , IDynamicParameters
599+ public class ChangePSReadlineKeyHandlerCommandBase : PSCmdlet
595600 {
596601 [ Parameter ( Position = 0 , Mandatory = true ) ]
597602 [ Alias ( "Key" ) ]
598603 [ ValidateNotNullOrEmpty ]
599604 [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
600605 public string [ ] Chord { get ; set ; }
601606
607+ [ Parameter ]
608+ public ViMode ViMode { get ; set ; }
609+
610+ [ ExcludeFromCodeCoverage ]
611+ protected IDisposable UseRequestedDispatchTables ( )
612+ {
613+ bool inViMode = PSConsoleReadLine . GetOptions ( ) . EditMode == EditMode . Vi ;
614+ bool viModeParamPresent = MyInvocation . BoundParameters . ContainsKey ( "ViMode" ) ;
615+
616+ if ( inViMode || viModeParamPresent )
617+ {
618+ if ( ! inViMode )
619+ {
620+ // "-ViMode" must have been specified explicitly. Well, okay... we can
621+ // modify the Vi tables... but isn't that an odd thing to do from
622+ // not-vi mode?
623+ WriteWarning ( PSReadLineResources . NotInViMode ) ;
624+ }
625+
626+ if ( ViMode == ViMode . Command )
627+ return PSConsoleReadLine . UseViCommandModeTables ( ) ;
628+ else // default if -ViMode not specified, invalid, or "Insert"
629+ return PSConsoleReadLine . UseViInsertModeTables ( ) ;
630+ }
631+
632+ return null ;
633+ }
634+ }
635+
636+ [ Cmdlet ( "Set" , "PSReadlineKeyHandler" , HelpUri = "http://go.microsoft.com/fwlink/?LinkId=528810" ) ]
637+ public class SetPSReadlineKeyHandlerCommand : ChangePSReadlineKeyHandlerCommandBase , IDynamicParameters
638+ {
602639 [ Parameter ( Position = 1 , Mandatory = true , ParameterSetName = "ScriptBlock" ) ]
603640 [ ValidateNotNull ]
604641 public ScriptBlock ScriptBlock { get ; set ; }
@@ -616,18 +653,21 @@ public class SetPSReadlineKeyHandlerCommand : PSCmdlet, IDynamicParameters
616653 [ ExcludeFromCodeCoverage ]
617654 protected override void EndProcessing ( )
618655 {
619- if ( ParameterSetName . Equals ( FunctionParameterSet ) )
620- {
621- var function = ( string ) _dynamicParameters . Value [ FunctionParameter ] . Value ;
622- var keyHandler = ( Action < ConsoleKeyInfo ? , object > )
623- Delegate . CreateDelegate ( typeof ( Action < ConsoleKeyInfo ? , object > ) ,
624- typeof ( PSConsoleReadLine ) . GetMethod ( function ) ) ;
625- BriefDescription = function ;
626- PSConsoleReadLine . SetKeyHandler ( Chord , keyHandler , BriefDescription , Description ) ;
627- }
628- else
656+ using ( UseRequestedDispatchTables ( ) )
629657 {
630- PSConsoleReadLine . SetKeyHandler ( Chord , ScriptBlock , BriefDescription , Description ) ;
658+ if ( ParameterSetName . Equals ( FunctionParameterSet ) )
659+ {
660+ var function = ( string ) _dynamicParameters . Value [ FunctionParameter ] . Value ;
661+ var keyHandler = ( Action < ConsoleKeyInfo ? , object > )
662+ Delegate . CreateDelegate ( typeof ( Action < ConsoleKeyInfo ? , object > ) ,
663+ typeof ( PSConsoleReadLine ) . GetMethod ( function ) ) ;
664+ BriefDescription = function ;
665+ PSConsoleReadLine . SetKeyHandler ( Chord , keyHandler , BriefDescription , Description ) ;
666+ }
667+ else
668+ {
669+ PSConsoleReadLine . SetKeyHandler ( Chord , ScriptBlock , BriefDescription , Description ) ;
670+ }
631671 }
632672 }
633673
@@ -713,18 +753,15 @@ protected override void EndProcessing()
713753 }
714754
715755 [ Cmdlet ( "Remove" , "PSReadlineKeyHandler" , HelpUri = "http://go.microsoft.com/fwlink/?LinkId=528809" ) ]
716- public class RemoveKeyHandlerCommand : PSCmdlet
756+ public class RemoveKeyHandlerCommand : ChangePSReadlineKeyHandlerCommandBase
717757 {
718- [ Parameter ( Position = 0 , Mandatory = true ) ]
719- [ Alias ( "Key" ) ]
720- [ ValidateNotNullOrEmpty ]
721- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
722- public string [ ] Chord { get ; set ; }
723-
724758 [ ExcludeFromCodeCoverage ]
725759 protected override void EndProcessing ( )
726760 {
727- PSConsoleReadLine . RemoveKeyHandler ( Chord ) ;
761+ using ( UseRequestedDispatchTables ( ) )
762+ {
763+ PSConsoleReadLine . RemoveKeyHandler ( Chord ) ;
764+ }
728765 }
729766 }
730767
0 commit comments