@@ -52,6 +52,12 @@ public enum ViModeStyle
52
52
Prompt ,
53
53
Cursor
54
54
}
55
+
56
+ public enum ViMode
57
+ {
58
+ Insert ,
59
+ Command
60
+ }
55
61
#endregion vi
56
62
57
63
public enum HistorySaveStyle
@@ -590,15 +596,48 @@ protected override void EndProcessing()
590
596
}
591
597
}
592
598
593
- [ Cmdlet ( "Set" , "PSReadlineKeyHandler" , HelpUri = "http://go.microsoft.com/fwlink/?LinkId=528810" ) ]
594
- public class SetPSReadlineKeyHandlerCommand : PSCmdlet , IDynamicParameters
599
+ public class ChangePSReadlineKeyHandlerCommand : PSCmdlet
595
600
{
596
601
[ Parameter ( Position = 0 , Mandatory = true ) ]
597
602
[ Alias ( "Key" ) ]
598
603
[ ValidateNotNullOrEmpty ]
599
604
[ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
600
605
public string [ ] Chord { get ; set ; }
601
606
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 . Insert ) // default if -ViMode not specified
627
+ return PSConsoleReadLine . UseViInsertModeTables ( ) ;
628
+ else if ( ViMode == ViMode . Command )
629
+ return PSConsoleReadLine . UseViCommandModeTables ( ) ;
630
+ else
631
+ System . Diagnostics . Debug . Fail ( "unexpected ViMode" ) ;
632
+ }
633
+
634
+ return null ;
635
+ }
636
+ }
637
+
638
+ [ Cmdlet ( "Set" , "PSReadlineKeyHandler" , HelpUri = "http://go.microsoft.com/fwlink/?LinkId=528810" ) ]
639
+ public class SetPSReadlineKeyHandlerCommand : ChangePSReadlineKeyHandlerCommand , IDynamicParameters
640
+ {
602
641
[ Parameter ( Position = 1 , Mandatory = true , ParameterSetName = "ScriptBlock" ) ]
603
642
[ ValidateNotNull ]
604
643
public ScriptBlock ScriptBlock { get ; set ; }
@@ -616,18 +655,21 @@ public class SetPSReadlineKeyHandlerCommand : PSCmdlet, IDynamicParameters
616
655
[ ExcludeFromCodeCoverage ]
617
656
protected override void EndProcessing ( )
618
657
{
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
658
+ using ( UseRequestedDispatchTables ( ) )
629
659
{
630
- PSConsoleReadLine . SetKeyHandler ( Chord , ScriptBlock , BriefDescription , Description ) ;
660
+ if ( ParameterSetName . Equals ( FunctionParameterSet ) )
661
+ {
662
+ var function = ( string ) _dynamicParameters . Value [ FunctionParameter ] . Value ;
663
+ var keyHandler = ( Action < ConsoleKeyInfo ? , object > )
664
+ Delegate . CreateDelegate ( typeof ( Action < ConsoleKeyInfo ? , object > ) ,
665
+ typeof ( PSConsoleReadLine ) . GetMethod ( function ) ) ;
666
+ BriefDescription = function ;
667
+ PSConsoleReadLine . SetKeyHandler ( Chord , keyHandler , BriefDescription , Description ) ;
668
+ }
669
+ else
670
+ {
671
+ PSConsoleReadLine . SetKeyHandler ( Chord , ScriptBlock , BriefDescription , Description ) ;
672
+ }
631
673
}
632
674
}
633
675
@@ -713,18 +755,15 @@ protected override void EndProcessing()
713
755
}
714
756
715
757
[ Cmdlet ( "Remove" , "PSReadlineKeyHandler" , HelpUri = "http://go.microsoft.com/fwlink/?LinkId=528809" ) ]
716
- public class RemoveKeyHandlerCommand : PSCmdlet
758
+ public class RemoveKeyHandlerCommand : ChangePSReadlineKeyHandlerCommand
717
759
{
718
- [ Parameter ( Position = 0 , Mandatory = true ) ]
719
- [ Alias ( "Key" ) ]
720
- [ ValidateNotNullOrEmpty ]
721
- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
722
- public string [ ] Chord { get ; set ; }
723
-
724
760
[ ExcludeFromCodeCoverage ]
725
761
protected override void EndProcessing ( )
726
762
{
727
- PSConsoleReadLine . RemoveKeyHandler ( Chord ) ;
763
+ using ( UseRequestedDispatchTables ( ) )
764
+ {
765
+ PSConsoleReadLine . RemoveKeyHandler ( Chord ) ;
766
+ }
728
767
}
729
768
}
730
769
0 commit comments