From 3a429ca6c38e0547e45626486463c8a8d9b7cec5 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Fri, 2 Oct 2009 09:56:07 +0200 Subject: [PATCH] New expansions for alias arguments ($n, $-m, $n-, $n-m, $*, $~) (patch #6917) --- ChangeLog | 4 +- doc/en/autogen/user/alias_commands.txt | 12 +- doc/fr/autogen/user/alias_commands.txt | 12 +- po/cs.po | 16 ++- po/de.po | 16 ++- po/es.po | 16 ++- po/fr.po | 33 +++-- po/hu.po | 16 ++- po/pl.po | 16 ++- po/ru.po | 16 ++- po/weechat.pot | 16 ++- src/plugins/alias/alias.c | 188 ++++++++++++++++++------- 12 files changed, 259 insertions(+), 102 deletions(-) diff --git a/ChangeLog b/ChangeLog index e96497e1f..8601cb66a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,7 @@ WeeChat ChangeLog ================= FlashCode -v0.3.1-dev, 2009-09-30 +v0.3.1-dev, 2009-10-02 Version 0.3.1 (under dev!) @@ -12,6 +12,8 @@ Version 0.3.1 (under dev!) * core: fix plural form in translation files (bug #27430) * core: fix terminal title bug: do not reset it when option weechat.look.set_title is off (bug #27399) +* alias: new expansions for alias arguments ($n, $-m, $n-, $n-m, $*, $~) + (patch #6917) * alias: allow use of wildcards for /alias list (patch #6925) * alias: allow /unalias to remove multiple aliases (patch #6926) * alias: fix bug with arguments (bug #27440) diff --git a/doc/en/autogen/user/alias_commands.txt b/doc/en/autogen/user/alias_commands.txt index 58d3497db..b4a0f1322 100644 --- a/doc/en/autogen/user/alias_commands.txt +++ b/doc/en/autogen/user/alias_commands.txt @@ -9,7 +9,15 @@ Without argument, this command lists all defined alias. - Note: in command, special variables $1, $2,..,$9 are replaced by arguments given by user, and $* is replaced by all arguments. - Variables $nick, $channel and $server are replaced by current nick/channel/server. + Note: in command, special variables are replaced: + $n: argument 'n' (between 1 and 9) + $-m: arguments from 1 to 'm' + $n-: arguments from 'n' to last + $n-m: arguments from 'n' to 'm' + $*: all arguments + $~: last argument + $nick: current nick + $channel: current channel + $server: current server ........................................ diff --git a/doc/fr/autogen/user/alias_commands.txt b/doc/fr/autogen/user/alias_commands.txt index 1428dbf34..68b22467e 100644 --- a/doc/fr/autogen/user/alias_commands.txt +++ b/doc/fr/autogen/user/alias_commands.txt @@ -9,7 +9,15 @@ Sans paramètre, cette commande liste tous les alias définis. - Note: dans la commande, les variables spéciales $1, $2,..,$9 sont remplacés par les paramètres donnés par l'utilisateur, et $* est remplacé par tous les paramètres. - Les variables $nick, $channel et $server sont remplacées par le pseudo/canal/serveur courant. + Note: dans la commande, les variables spéciales sont remplacées : + $n: paramètre 'n' (entre 1 et 9) + $-m: paramètres de 1 à 'm' + $n-: paramètres de 'n' au dernier + $n-m: paramètres de 'n' à 'm' + $*: tous les paramètres + $~: le dernier paramètre + $nick: pseudo courant + $channel: canal courant + $server: serveur courant ........................................ diff --git a/po/cs.po b/po/cs.po index 75c3b1686..5787f8d24 100644 --- a/po/cs.po +++ b/po/cs.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: WeeChat 0.3.1-dev\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2009-09-30 17:50+0200\n" +"POT-Creation-Date: 2009-10-02 00:08+0200\n" "PO-Revision-Date: 2009-09-14 10:56+0200\n" "Last-Translator: Jiri Golembiovsky \n" "Language-Team: weechat-dev \n" @@ -2354,10 +2354,16 @@ msgid "" "\n" "Without argument, this command lists all defined alias.\n" "\n" -"Note: in command, special variables $1, $2,..,$9 are replaced by arguments " -"given by user, and $* is replaced by all arguments.\n" -"Variables $nick, $channel and $server are replaced by current nick/channel/" -"server." +"Note: in command, special variables are replaced:\n" +" $n: argument 'n' (between 1 and 9)\n" +" $-m: arguments from 1 to 'm'\n" +" $n-: arguments from 'n' to last\n" +" $n-m: arguments from 'n' to 'm'\n" +" $*: all arguments\n" +" $~: last argument\n" +" $nick: current nick\n" +" $channel: current channel\n" +" $server: current server" msgstr "" "jméno_aliasu: jméno aliasu\n" " příkaz: jméno příkazu (více příkazů se rozděluje středníky)\n" diff --git a/po/de.po b/po/de.po index 6c41a7a28..801be28db 100644 --- a/po/de.po +++ b/po/de.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: WeeChat 0.3.1-dev\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2009-09-30 17:50+0200\n" +"POT-Creation-Date: 2009-10-02 00:08+0200\n" "PO-Revision-Date: 2009-09-20 13:58+0200\n" "Last-Translator: Thomas Schuetz \n" "Language-Team: weechat-dev \n" @@ -2230,10 +2230,16 @@ msgid "" "\n" "Without argument, this command lists all defined alias.\n" "\n" -"Note: in command, special variables $1, $2,..,$9 are replaced by arguments " -"given by user, and $* is replaced by all arguments.\n" -"Variables $nick, $channel and $server are replaced by current nick/channel/" -"server." +"Note: in command, special variables are replaced:\n" +" $n: argument 'n' (between 1 and 9)\n" +" $-m: arguments from 1 to 'm'\n" +" $n-: arguments from 'n' to last\n" +" $n-m: arguments from 'n' to 'm'\n" +" $*: all arguments\n" +" $~: last argument\n" +" $nick: current nick\n" +" $channel: current channel\n" +" $server: current server" msgstr "" "Aliasname: Name des Alias\n" " Befehl: Befehlsname (WeeChat- oder IRC-Befehl ohne führenden '/', mehrere " diff --git a/po/es.po b/po/es.po index 94117fd95..13e60661f 100644 --- a/po/es.po +++ b/po/es.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: WeeChat 0.3.1-dev\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2009-09-30 17:50+0200\n" +"POT-Creation-Date: 2009-10-02 00:08+0200\n" "PO-Revision-Date: 2009-09-06 21:25+0200\n" "Last-Translator: Elián Hanisch \n" "Language-Team: weechat-dev \n" @@ -2182,10 +2182,16 @@ msgid "" "\n" "Without argument, this command lists all defined alias.\n" "\n" -"Note: in command, special variables $1, $2,..,$9 are replaced by arguments " -"given by user, and $* is replaced by all arguments.\n" -"Variables $nick, $channel and $server are replaced by current nick/channel/" -"server." +"Note: in command, special variables are replaced:\n" +" $n: argument 'n' (between 1 and 9)\n" +" $-m: arguments from 1 to 'm'\n" +" $n-: arguments from 'n' to last\n" +" $n-m: arguments from 'n' to 'm'\n" +" $*: all arguments\n" +" $~: last argument\n" +" $nick: current nick\n" +" $channel: current channel\n" +" $server: current server" msgstr "" "nombre_alias: nombre del seudónimo\n" " comando: nombre del comando (comando WeeChat o IRC, sin el primer '/')\n" diff --git a/po/fr.po b/po/fr.po index 81d634d3c..6a83cb17e 100644 --- a/po/fr.po +++ b/po/fr.po @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: WeeChat 0.3.1-dev\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2009-09-30 17:50+0200\n" -"PO-Revision-Date: 2009-09-30 17:50+0200\n" +"POT-Creation-Date: 2009-10-02 00:08+0200\n" +"PO-Revision-Date: 2009-10-02 00:09+0200\n" "Last-Translator: FlashCode \n" "Language-Team: weechat-dev \n" "MIME-Version: 1.0\n" @@ -2409,10 +2409,16 @@ msgid "" "\n" "Without argument, this command lists all defined alias.\n" "\n" -"Note: in command, special variables $1, $2,..,$9 are replaced by arguments " -"given by user, and $* is replaced by all arguments.\n" -"Variables $nick, $channel and $server are replaced by current nick/channel/" -"server." +"Note: in command, special variables are replaced:\n" +" $n: argument 'n' (between 1 and 9)\n" +" $-m: arguments from 1 to 'm'\n" +" $n-: arguments from 'n' to last\n" +" $n-m: arguments from 'n' to 'm'\n" +" $*: all arguments\n" +" $~: last argument\n" +" $nick: current nick\n" +" $channel: current channel\n" +" $server: current server" msgstr "" " nom_alias: nom de l'alias (peut démarrer ou se terminer par \"*\" pour une " "liste d'alias)\n" @@ -2422,11 +2428,16 @@ msgstr "" "\n" "Sans paramètre, cette commande liste tous les alias définis.\n" "\n" -"Note: dans la commande, les variables spéciales $1, $2,..,$9 sont remplacés " -"par les paramètres donnés par l'utilisateur, et $* est remplacé par tous les " -"paramètres.\n" -"Les variables $nick, $channel et $server sont remplacées par le pseudo/canal/" -"serveur courant." +"Note: dans la commande, les variables spéciales sont remplacées :\n" +" $n: paramètre 'n' (entre 1 et 9)\n" +" $-m: paramètres de 1 à 'm'\n" +" $n-: paramètres de 'n' au dernier\n" +" $n-m: paramètres de 'n' à 'm'\n" +" $*: tous les paramètres\n" +" $~: le dernier paramètre\n" +" $nick: pseudo courant\n" +" $channel: canal courant\n" +" $server: serveur courant" msgid "remove aliases" msgstr "supprimer des alias" diff --git a/po/hu.po b/po/hu.po index 0d53efab9..90bb622d1 100644 --- a/po/hu.po +++ b/po/hu.po @@ -12,7 +12,7 @@ msgid "" msgstr "" "Project-Id-Version: WeeChat 0.3.1-dev\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2009-09-30 17:50+0200\n" +"POT-Creation-Date: 2009-10-02 00:08+0200\n" "PO-Revision-Date: 2009-09-20 13:51+0200\n" "Last-Translator: Andras Voroskoi \n" "Language-Team: weechat-dev \n" @@ -2300,10 +2300,16 @@ msgid "" "\n" "Without argument, this command lists all defined alias.\n" "\n" -"Note: in command, special variables $1, $2,..,$9 are replaced by arguments " -"given by user, and $* is replaced by all arguments.\n" -"Variables $nick, $channel and $server are replaced by current nick/channel/" -"server." +"Note: in command, special variables are replaced:\n" +" $n: argument 'n' (between 1 and 9)\n" +" $-m: arguments from 1 to 'm'\n" +" $n-: arguments from 'n' to last\n" +" $n-m: arguments from 'n' to 'm'\n" +" $*: all arguments\n" +" $~: last argument\n" +" $nick: current nick\n" +" $channel: current channel\n" +" $server: current server" msgstr "" " alias_név: az alias neve\n" " parancs: parancs neve (WeeChat vagy IRC parancs, több parancs " diff --git a/po/pl.po b/po/pl.po index 353b29e75..d86b22616 100644 --- a/po/pl.po +++ b/po/pl.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: WeeChat 0.3.1-dev\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2009-09-30 17:50+0200\n" +"POT-Creation-Date: 2009-10-02 00:08+0200\n" "PO-Revision-Date: 2009-09-06 21:25+0200\n" "Last-Translator: Krzysztof Korościk \n" "Language-Team: Polish\n" @@ -2388,10 +2388,16 @@ msgid "" "\n" "Without argument, this command lists all defined alias.\n" "\n" -"Note: in command, special variables $1, $2,..,$9 are replaced by arguments " -"given by user, and $* is replaced by all arguments.\n" -"Variables $nick, $channel and $server are replaced by current nick/channel/" -"server." +"Note: in command, special variables are replaced:\n" +" $n: argument 'n' (between 1 and 9)\n" +" $-m: arguments from 1 to 'm'\n" +" $n-: arguments from 'n' to last\n" +" $n-m: arguments from 'n' to 'm'\n" +" $*: all arguments\n" +" $~: last argument\n" +" $nick: current nick\n" +" $channel: current channel\n" +" $server: current server" msgstr "" "nazwa_aliasu: nazwa aliasu\n" " komenda: nazwa komendy (wiele komend można oddzielić za pomocą średnika)\n" diff --git a/po/ru.po b/po/ru.po index 9fb9f64f5..4ce05e833 100644 --- a/po/ru.po +++ b/po/ru.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: WeeChat 0.3.1-dev\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2009-09-30 17:50+0200\n" +"POT-Creation-Date: 2009-10-02 00:08+0200\n" "PO-Revision-Date: 2009-09-20 13:50+0200\n" "Last-Translator: Pavel Shevchuk \n" "Language-Team: weechat-dev \n" @@ -2313,10 +2313,16 @@ msgid "" "\n" "Without argument, this command lists all defined alias.\n" "\n" -"Note: in command, special variables $1, $2,..,$9 are replaced by arguments " -"given by user, and $* is replaced by all arguments.\n" -"Variables $nick, $channel and $server are replaced by current nick/channel/" -"server." +"Note: in command, special variables are replaced:\n" +" $n: argument 'n' (between 1 and 9)\n" +" $-m: arguments from 1 to 'm'\n" +" $n-: arguments from 'n' to last\n" +" $n-m: arguments from 'n' to 'm'\n" +" $*: all arguments\n" +" $~: last argument\n" +" $nick: current nick\n" +" $channel: current channel\n" +" $server: current server" msgstr "" "сокращение: название сокращения\n" " команда: название команды (команда WeeChat или IRC, несколько команд " diff --git a/po/weechat.pot b/po/weechat.pot index f9e6783fd..19950a1e1 100644 --- a/po/weechat.pot +++ b/po/weechat.pot @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2009-09-30 17:50+0200\n" +"POT-Creation-Date: 2009-10-02 00:08+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1968,10 +1968,16 @@ msgid "" "\n" "Without argument, this command lists all defined alias.\n" "\n" -"Note: in command, special variables $1, $2,..,$9 are replaced by arguments " -"given by user, and $* is replaced by all arguments.\n" -"Variables $nick, $channel and $server are replaced by current nick/channel/" -"server." +"Note: in command, special variables are replaced:\n" +" $n: argument 'n' (between 1 and 9)\n" +" $-m: arguments from 1 to 'm'\n" +" $n-: arguments from 'n' to last\n" +" $n-m: arguments from 'n' to 'm'\n" +" $*: all arguments\n" +" $~: last argument\n" +" $nick: current nick\n" +" $channel: current channel\n" +" $server: current server" msgstr "" msgid "remove aliases" diff --git a/src/plugins/alias/alias.c b/src/plugins/alias/alias.c index cfe71282b..6224145e6 100644 --- a/src/plugins/alias/alias.c +++ b/src/plugins/alias/alias.c @@ -34,6 +34,8 @@ WEECHAT_PLUGIN_AUTHOR("FlashCode "); WEECHAT_PLUGIN_VERSION(WEECHAT_VERSION); WEECHAT_PLUGIN_LICENSE("GPL3"); +#define ALIAS_IS_ARG_NUMBER(number) ((number >= '1') && (number <= '9')) + struct t_weechat_plugin *weechat_alias_plugin = NULL; struct t_config_file *alias_config_file = NULL; @@ -87,12 +89,11 @@ alias_search (const char *alias_name) } /* - * alias_add_word: add word to string and increment length - * This function should NOT be called directly. + * alias_string_add_word: add word to string and increment length */ void -alias_add_word (char **alias, int *length, const char *word) +alias_string_add_word (char **alias, int *length, const char *word) { int length_word; @@ -117,15 +118,61 @@ alias_add_word (char **alias, int *length, const char *word) } /* - * alias_replace_args: replace arguments ($1, $2, .. or $*) in alias arguments + * alias_string_add_word_range: add word (in range) to string and increment + * length + */ + +void +alias_string_add_word_range (char **alias, int *length, const char *start, + const char *end) +{ + char *word; + + word = weechat_strndup (start, end - start); + if (word) + { + alias_string_add_word (alias, length, word); + free (word); + } +} + +/* + * alias_string_add_arguments: add some arguments to string and increment + * length + */ + +void +alias_string_add_arguments (char **alias, int *length, char **argv, int start, + int end) +{ + int i; + + for (i = start; i <= end; i++) + { + if (i != start) + alias_string_add_word (alias, length, " "); + alias_string_add_word (alias, length, argv[i]); + } +} + +/* + * alias_replace_args: replace arguments in alias + * arguments are: + * $n argument n + * $-m arguments from 1 to m + * $n- arguments from n to last + * $n-m arguments from n to m + * $* all arguments + * $~ last argument + * with n and m in 1..9 */ char * alias_replace_args (const char *alias_args, const char *user_args) { - char **argv, *res, *word; + char **argv, *res; const char *start, *pos; - int argc, length_res, args_count; + int n, m, argc, length_res, args_count, offset; argv = weechat_string_split (user_args, " ", 0, 0, &argc); @@ -136,17 +183,13 @@ alias_replace_args (const char *alias_args, const char *user_args) pos = start; while (pos && pos[0]) { + offset = 0; + if ((pos[0] == '\\') && (pos[1] == '$')) { - word = weechat_strndup (start, pos - start); - if (word) - { - alias_add_word (&res, &length_res, word); - free (word); - } - alias_add_word (&res, &length_res, "$"); - start = pos + 2; - pos = start; + offset = 2; + alias_string_add_word_range (&res, &length_res, start, pos); + alias_string_add_word (&res, &length_res, "$"); } else { @@ -154,50 +197,87 @@ alias_replace_args (const char *alias_args, const char *user_args) { if (pos[1] == '*') { + /* replace with all arguments */ args_count++; + offset = 2; if (pos > start) - { - word = weechat_strndup (start, pos - start); - if (word) - { - alias_add_word (&res, &length_res, word); - free (word); - } - } - alias_add_word (&res, &length_res, user_args); - start = pos + 2; - pos = start; + alias_string_add_word_range (&res, &length_res, start, pos); + alias_string_add_word (&res, &length_res, user_args); } - else + else if (pos[1] == '~') { - if ((pos[1] >= '1') && (pos[1] <= '9')) + /* replace with last argument */ + args_count++; + offset = 2; + if (pos > start) + alias_string_add_word_range (&res, &length_res, start, pos); + if (argc > 0) + alias_string_add_word (&res, &length_res, argv[argc - 1]); + } + else if ((pos[1] == '-') && ALIAS_IS_ARG_NUMBER(pos[2])) + { + /* replace with arguments 1 to m */ + args_count++; + offset = 3; + if (pos > start) + alias_string_add_word_range (&res, &length_res, start, pos); + if (pos[2] - '1' < argc) + m = pos[2] - '1'; + else + m = argc - 1; + alias_string_add_arguments (&res, &length_res, argv, 0, m); + } + else if (ALIAS_IS_ARG_NUMBER(pos[1])) + { + args_count++; + n = pos[1] - '1'; + if (pos > start) + alias_string_add_word_range (&res, &length_res, start, pos); + if (pos[2] != '-') { - args_count++; - if (pos > start) - { - word = weechat_strndup (start, pos - start); - if (word) - { - alias_add_word (&res, &length_res, word); - free (word); - } - } - if (pos[1] - '1' < argc) - alias_add_word (&res, &length_res, argv[pos[1] - '1']); - start = pos + 2; - pos = start; + /* replace with argument n */ + offset = 2; + if (n < argc) + alias_string_add_word (&res, &length_res, argv[n]); } else - pos++; + { + if (ALIAS_IS_ARG_NUMBER(pos[3])) + { + /* replace with arguments n to m */ + offset = 4; + if (pos[3] - '1' < argc) + m = pos[3] - '1'; + else + m = argc - 1; + } + else + { + /* replace with arguments n to last */ + offset = 3; + m = argc - 1; + } + if (n < argc) + { + alias_string_add_arguments (&res, &length_res, + argv, n, m); + } + } } } - else - pos++; } + + if (offset != 0) + { + pos += offset; + start = pos; + } + else + pos++; } - if (start < pos) - alias_add_word (&res, &length_res, start); + if (pos > start) + alias_string_add_word (&res, &length_res, start); if (argv) weechat_string_free_split (argv); @@ -942,10 +1022,16 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) "Without argument, this command lists all " "defined alias.\n\n" "Note: in command, special variables " - "$1, $2,..,$9 are replaced by arguments given " - "by user, and $* is replaced by all arguments.\n" - "Variables $nick, $channel and $server are " - "replaced by current nick/channel/server."), + "are replaced:\n" + " $n: argument 'n' (between 1 and 9)\n" + " $-m: arguments from 1 to 'm'\n" + " $n-: arguments from 'n' to last\n" + " $n-m: arguments from 'n' to 'm'\n" + " $*: all arguments\n" + " $~: last argument\n" + " $nick: current nick\n" + " $channel: current channel\n" + " $server: current server"), "%(alias) %(commands)", &alias_command_cb, NULL);