@@ -2821,20 +2821,39 @@ def _alias_delete(self, args: argparse.Namespace) -> None:
2821
2821
2822
2822
@as_subcommand_to ('alias' , 'list' , alias_list_parser , help = alias_delete_help )
2823
2823
def _alias_list (self , args : argparse .Namespace ) -> None :
2824
- """List some or all aliases"""
2824
+ """List some or all aliases as 'alias create' commands """
2825
2825
create_cmd = "alias create"
2826
2826
if args .with_silent :
2827
2827
create_cmd += " --silent"
2828
2828
2829
+ tokens_to_quote = constants .REDIRECTION_TOKENS
2830
+ tokens_to_quote .extend (self .statement_parser .terminators )
2831
+
2829
2832
if args .names :
2830
- for cur_name in utils .remove_duplicates (args .names ):
2831
- if cur_name in self .aliases :
2832
- self .poutput ("{} {} {}" .format (create_cmd , cur_name , self .aliases [cur_name ]))
2833
- else :
2834
- self .perror ("Alias '{}' not found" .format (cur_name ))
2833
+ to_list = utils .remove_duplicates (args .names )
2835
2834
else :
2836
- for cur_alias in sorted (self .aliases , key = self .default_sort_key ):
2837
- self .poutput ("{} {} {}" .format (create_cmd , cur_alias , self .aliases [cur_alias ]))
2835
+ to_list = sorted (self .aliases , key = self .default_sort_key )
2836
+
2837
+ not_found = [] # type: List[str]
2838
+ for name in to_list :
2839
+ if name not in self .aliases :
2840
+ not_found .append (name )
2841
+ continue
2842
+
2843
+ # Quote redirection and terminator tokens for the 'alias create' command
2844
+ tokens = shlex_split (self .aliases [name ])
2845
+ command = tokens [0 ]
2846
+ args = tokens [1 :]
2847
+ utils .quote_specific_tokens (args , tokens_to_quote )
2848
+
2849
+ val = command
2850
+ if args :
2851
+ val += ' ' + ' ' .join (args )
2852
+
2853
+ self .poutput ("{} {} {}" .format (create_cmd , name , val ))
2854
+
2855
+ for name in not_found :
2856
+ self .perror ("Alias '{}' not found" .format (name ))
2838
2857
2839
2858
#############################################################
2840
2859
# Parsers and functions for macro command and subcommands
@@ -3029,20 +3048,39 @@ def _macro_delete(self, args: argparse.Namespace) -> None:
3029
3048
3030
3049
@as_subcommand_to ('macro' , 'list' , macro_list_parser , help = macro_list_help )
3031
3050
def _macro_list (self , args : argparse .Namespace ) -> None :
3032
- """List some or all macros"""
3051
+ """List some or all macros as 'macro create' commands """
3033
3052
create_cmd = "macro create"
3034
3053
if args .with_silent :
3035
3054
create_cmd += " --silent"
3036
3055
3056
+ tokens_to_quote = constants .REDIRECTION_TOKENS
3057
+ tokens_to_quote .extend (self .statement_parser .terminators )
3058
+
3037
3059
if args .names :
3038
- for cur_name in utils .remove_duplicates (args .names ):
3039
- if cur_name in self .macros :
3040
- self .poutput ("{} {} {}" .format (create_cmd , cur_name , self .macros [cur_name ].value ))
3041
- else :
3042
- self .perror ("Macro '{}' not found" .format (cur_name ))
3060
+ to_list = utils .remove_duplicates (args .names )
3043
3061
else :
3044
- for cur_macro in sorted (self .macros , key = self .default_sort_key ):
3045
- self .poutput ("{} {} {}" .format (create_cmd , cur_macro , self .macros [cur_macro ].value ))
3062
+ to_list = sorted (self .macros , key = self .default_sort_key )
3063
+
3064
+ not_found = [] # type: List[str]
3065
+ for name in to_list :
3066
+ if name not in self .macros :
3067
+ not_found .append (name )
3068
+ continue
3069
+
3070
+ # Quote redirection and terminator tokens for the 'macro create' command
3071
+ tokens = shlex_split (self .macros [name ].value )
3072
+ command = tokens [0 ]
3073
+ args = tokens [1 :]
3074
+ utils .quote_specific_tokens (args , tokens_to_quote )
3075
+
3076
+ val = command
3077
+ if args :
3078
+ val += ' ' + ' ' .join (args )
3079
+
3080
+ self .poutput ("{} {} {}" .format (create_cmd , name , val ))
3081
+
3082
+ for name in not_found :
3083
+ self .perror ("Macro '{}' not found" .format (name ))
3046
3084
3047
3085
def complete_help_command (self , text : str , line : str , begidx : int , endidx : int ) -> List [str ]:
3048
3086
"""Completes the command argument of help"""
0 commit comments