@@ -2821,20 +2821,39 @@ def _alias_delete(self, args: argparse.Namespace) -> None:
28212821
28222822 @as_subcommand_to ('alias' , 'list' , alias_list_parser , help = alias_delete_help )
28232823 def _alias_list (self , args : argparse .Namespace ) -> None :
2824- """List some or all aliases"""
2824+ """List some or all aliases as 'alias create' commands """
28252825 create_cmd = "alias create"
28262826 if args .with_silent :
28272827 create_cmd += " --silent"
28282828
2829+ tokens_to_quote = constants .REDIRECTION_TOKENS
2830+ tokens_to_quote .extend (self .statement_parser .terminators )
2831+
28292832 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 )
28352834 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 ))
28382857
28392858 #############################################################
28402859 # Parsers and functions for macro command and subcommands
@@ -3029,20 +3048,39 @@ def _macro_delete(self, args: argparse.Namespace) -> None:
30293048
30303049 @as_subcommand_to ('macro' , 'list' , macro_list_parser , help = macro_list_help )
30313050 def _macro_list (self , args : argparse .Namespace ) -> None :
3032- """List some or all macros"""
3051+ """List some or all macros as 'macro create' commands """
30333052 create_cmd = "macro create"
30343053 if args .with_silent :
30353054 create_cmd += " --silent"
30363055
3056+ tokens_to_quote = constants .REDIRECTION_TOKENS
3057+ tokens_to_quote .extend (self .statement_parser .terminators )
3058+
30373059 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 )
30433061 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 ))
30463084
30473085 def complete_help_command (self , text : str , line : str , begidx : int , endidx : int ) -> List [str ]:
30483086 """Completes the command argument of help"""
0 commit comments