summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/en/weechat_plugin_api.en.txt16
-rw-r--r--doc/fr/weechat_plugin_api.fr.txt18
-rw-r--r--doc/it/weechat_plugin_api.it.txt16
-rw-r--r--doc/ja/weechat_plugin_api.ja.txt16
-rw-r--r--src/core/wee-string.c43
-rw-r--r--src/core/wee-string.h4
-rw-r--r--src/plugins/trigger/trigger-callback.c3
-rw-r--r--src/plugins/weechat-plugin.h12
8 files changed, 103 insertions, 25 deletions
diff --git a/doc/en/weechat_plugin_api.en.txt b/doc/en/weechat_plugin_api.en.txt
index 91631663d..a57636d20 100644
--- a/doc/en/weechat_plugin_api.en.txt
+++ b/doc/en/weechat_plugin_api.en.txt
@@ -1268,14 +1268,17 @@ This function is not available in scripting API.
_WeeChat ≥ 0.4.4._
-Replace text in a string using a regular expression and replacement text.
+Replace text in a string using a regular expression, replacement text and
+optional callback.
Prototype:
[source,C]
----
char *weechat_string_replace_regex (const char *string, void *regex,
- const char *replace, const char reference_char);
+ const char *replace, const char reference_char,
+ char *(*callback)(void *data, const char *text),
+ void *callback_data);
----
Arguments:
@@ -1291,6 +1294,13 @@ Arguments:
** `$.*N`: match `N` (can be `+` or `0` to `99`), with all chars replaced by `*`
(the `*` char can be any char between space (32) and `~` (126))
* 'reference_char': the char used for reference to match (commonly '$')
+* 'callback': an optional callback called for each reference in 'replace'
+ (except for matches replaced by a char); the callback must return:
+** newly allocated string: it is used as replacement text (it is freed after
+ use)
+** NULL: the text received in callback is used as replacement text (without
+ changes)
+* 'callback_data': pointer given to callback when it is called
Return value:
@@ -1307,7 +1317,7 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
REG_EXTENDED) == 0)
{
string = weechat_string_replace_regex ("date: 2014-02-14", &my_regex,
- "$3/$2/$1", '$');
+ "$3/$2/$1", '$', NULL, NULL);
/* string == "date: 14/02/2014" */
if (string)
free (string);
diff --git a/doc/fr/weechat_plugin_api.fr.txt b/doc/fr/weechat_plugin_api.fr.txt
index 60cca1461..20ecc200a 100644
--- a/doc/fr/weechat_plugin_api.fr.txt
+++ b/doc/fr/weechat_plugin_api.fr.txt
@@ -1286,15 +1286,17 @@ Cette fonction n'est pas disponible dans l'API script.
_WeeChat ≥ 0.4.4._
-Remplacer du texte dans une chaîne en utilisant une expression régulière et du
-texte de remplacement.
+Remplacer du texte dans une chaîne en utilisant une expression régulière, du
+texte de remplacement et un "callback" optionnel.
Prototype :
[source,C]
----
char *weechat_string_replace_regex (const char *string, void *regex,
- const char *replace, const char reference_char);
+ const char *replace, const char reference_char,
+ char *(*callback)(void *data, const char *text),
+ void *callback_data);
----
Paramètres :
@@ -1314,6 +1316,14 @@ Paramètres :
caractère entre l'espace (32) et `~` (126))
* 'reference_char' : le caractère utilisé pour les références aux
correspondances (en général '$')
+* 'callback' : un "callback" optionnel appelé pour chaque référence dans
+ 'replace' (sauf pour les correspondances remplacées par un caractère); le
+ "callback" doit retourner :
+** une chaîne nouvellement allouée : elle est utilisée en texte de remplacement
+ (elle est libérée après utilisation)
+** NULL : le texte reçu dans le "callback" est utilisé comme texte de
+ remplacement (sans changement)
+* 'callback_data' : pointeur donné au "callback" lorsqu'il est appelé
Valeur de retour :
@@ -1330,7 +1340,7 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
REG_EXTENDED) == 0)
{
string = weechat_string_replace_regex ("date: 2014-02-14", &my_regex,
- "$3/$2/$1", '$');
+ "$3/$2/$1", '$', NULL, NULL);
/* string == "date: 14/02/2014" */
if (string)
free (string);
diff --git a/doc/it/weechat_plugin_api.it.txt b/doc/it/weechat_plugin_api.it.txt
index 62ae887e6..eaccf8cc0 100644
--- a/doc/it/weechat_plugin_api.it.txt
+++ b/doc/it/weechat_plugin_api.it.txt
@@ -1302,14 +1302,17 @@ Questa funzione non è disponibile nelle API per lo scripting.
_WeeChat ≥ 0.4.4._
// TRANSLATION MISSING
-Replace text in a string using a regular expression and replacement text.
+Replace text in a string using a regular expression, replacement text and
+optional callback.
Prototipo:
[source,C]
----
char *weechat_string_replace_regex (const char *string, void *regex,
- const char *replace, const char reference_char);
+ const char *replace, const char reference_char,
+ char *(*callback)(void *data, const char *text),
+ void *callback_data);
----
Argomenti:
@@ -1326,6 +1329,13 @@ Argomenti:
** `$.*N`: match `N` (can be `+` or `0` to `99`), with all chars replaced by `*`
(the `*` char can be any char between space (32) and `~` (126))
* 'reference_char': the char used for reference to match (commonly '$')
+* 'callback': an optional callback called for each reference in 'replace'
+ (except for matches replaced by a char); the callback must return:
+** newly allocated string: it is used as replacement text (it is freed after
+ use)
+** NULL: the text received in callback is used as replacement text (without
+ changes)
+* 'callback_data': pointer given to callback when it is called
Valore restituito:
@@ -1343,7 +1353,7 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
REG_EXTENDED) == 0)
{
string = weechat_string_replace_regex ("date: 2014-02-14", &my_regex,
- "$3/$2/$1", '$');
+ "$3/$2/$1", '$', NULL, NULL);
/* string == "date: 14/02/2014" */
if (string)
free (string);
diff --git a/doc/ja/weechat_plugin_api.ja.txt b/doc/ja/weechat_plugin_api.ja.txt
index 42032beef..136950763 100644
--- a/doc/ja/weechat_plugin_api.ja.txt
+++ b/doc/ja/weechat_plugin_api.ja.txt
@@ -1269,14 +1269,17 @@ free (str);
_WeeChat バージョン 0.4.4 以上で利用可。_
// TRANSLATION MISSING
-Replace text in a string using a regular expression and replacement text.
+Replace text in a string using a regular expression, replacement text and
+optional callback.
プロトタイプ:
[source,C]
----
char *weechat_string_replace_regex (const char *string, void *regex,
- const char *replace, const char reference_char);
+ const char *replace, const char reference_char,
+ char *(*callback)(void *data, const char *text),
+ void *callback_data);
----
引数:
@@ -1293,6 +1296,13 @@ char *weechat_string_replace_regex (const char *string, void *regex,
** `$.*N`: match `N` (can be `+` or `0` to `99`), with all chars replaced by `*`
(the `*` char can be any char between space (32) and `~` (126))
* 'reference_char': the char used for reference to match (commonly '$')
+* 'callback': an optional callback called for each reference in 'replace'
+ (except for matches replaced by a char); the callback must return:
+** newly allocated string: it is used as replacement text (it is freed after
+ use)
+** NULL: the text received in callback is used as replacement text (without
+ changes)
+* 'callback_data': pointer given to callback when it is called
戻り値:
@@ -1310,7 +1320,7 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
REG_EXTENDED) == 0)
{
string = weechat_string_replace_regex ("date: 2014-02-14", &my_regex,
- "$3/$2/$1", '$');
+ "$3/$2/$1", '$', NULL, NULL);
/* string == "date: 14/02/2014" */
if (string)
free (string);
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index ff3d143e9..e3a8f1184 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -1154,7 +1154,9 @@ string_replace (const char *string, const char *search, const char *replace)
char *
string_replace_regex_get_replace (const char *string, regmatch_t *regex_match,
int last_match, const char *replace,
- const char reference_char)
+ const char reference_char,
+ char *(*callback)(void *data, const char *text),
+ void *callback_data)
{
int length, length_current, length_add, match;
const char *ptr_replace, *ptr_add;
@@ -1208,8 +1210,26 @@ string_replace_regex_get_replace (const char *string, regmatch_t *regex_match,
}
if (regex_match[match].rm_so >= 0)
{
- ptr_add = string + regex_match[match].rm_so;
- length_add = regex_match[match].rm_eo - regex_match[match].rm_so;
+ if (callback)
+ {
+ temp = string_strndup (string + regex_match[match].rm_so,
+ regex_match[match].rm_eo - regex_match[match].rm_so);
+ if (temp)
+ {
+ modified_replace = (*callback) (callback_data, temp);
+ if (modified_replace)
+ {
+ ptr_add = modified_replace;
+ length_add = strlen (modified_replace);
+ }
+ free (temp);
+ }
+ }
+ if (!ptr_add)
+ {
+ ptr_add = string + regex_match[match].rm_so;
+ length_add = regex_match[match].rm_eo - regex_match[match].rm_so;
+ }
}
}
else if ((ptr_replace[1] == '.')
@@ -1308,6 +1328,11 @@ string_replace_regex_get_replace (const char *string, regmatch_t *regex_match,
* (the char '*' can be replaced by any char between space (32)
* and '~' (126))
*
+ * If the callback is not NULL, it is called for every reference to a match
+ * (except for matches replaced by a char).
+ * If not NULL, the string returned by the callback (which must have been newly
+ * allocated) is used and freed after use.
+ *
* Examples:
*
* string | regex | replace | result
@@ -1322,7 +1347,9 @@ string_replace_regex_get_replace (const char *string, regmatch_t *regex_match,
char *
string_replace_regex (const char *string, void *regex, const char *replace,
- const char reference_char)
+ const char reference_char,
+ char *(*callback)(void *data, const char *text),
+ void *callback_data)
{
char *result, *result2, *str_replace;
int length, length_replace, start_offset, i, rc, end, last_match;
@@ -1373,9 +1400,13 @@ string_replace_regex (const char *string, void *regex, const char *replace,
/* check if the regex matched the end of string */
end = !result[regex_match[0].rm_eo];
- str_replace = string_replace_regex_get_replace (result, regex_match,
+ str_replace = string_replace_regex_get_replace (result,
+ regex_match,
last_match,
- replace, reference_char);
+ replace,
+ reference_char,
+ callback,
+ callback_data);
length_replace = (str_replace) ? strlen (str_replace) : 0;
length = regex_match[0].rm_so + length_replace +
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index 1ae8cb9b2..bf34cb2a3 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -59,7 +59,9 @@ extern int string_has_highlight_regex_compiled (const char *string,
extern int string_has_highlight_regex (const char *string, const char *regex);
extern char *string_replace_regex (const char *string, void *regex,
const char *replace,
- const char reference_char);
+ const char reference_char,
+ char *(*callback)(void *data, const char *text),
+ void *callback_data);
extern char **string_split (const char *string, const char *separators,
int keep_eol, int num_items_max, int *num_items);
extern char **string_split_shared (const char *string, const char *separators,
diff --git a/src/plugins/trigger/trigger-callback.c b/src/plugins/trigger/trigger-callback.c
index d9d82101e..00a941b20 100644
--- a/src/plugins/trigger/trigger-callback.c
+++ b/src/plugins/trigger/trigger-callback.c
@@ -213,7 +213,8 @@ trigger_callback_replace_regex (struct t_trigger *trigger,
value = weechat_string_replace_regex (ptr_value,
trigger->regex[i].regex,
replace_eval,
- '$');
+ '$',
+ NULL, NULL);
if (value)
{
/* display debug info on trigger buffer */
diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h
index 6bfb54bb8..9f3d219d0 100644
--- a/src/plugins/weechat-plugin.h
+++ b/src/plugins/weechat-plugin.h
@@ -57,7 +57,7 @@ struct timeval;
* please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01.
*/
-#define WEECHAT_PLUGIN_API_VERSION "20140311-01"
+#define WEECHAT_PLUGIN_API_VERSION "20140313-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -250,7 +250,9 @@ struct t_weechat_plugin
int (*string_has_highlight_regex) (const char *string, const char *regex);
char *(*string_replace_regex) (const char *string, void *regex,
const char *replace,
- const char reference_char);
+ const char reference_char,
+ char *(*callback)(void *data, const char *text),
+ void *callback_data);
char **(*string_split) (const char *string, const char *separators,
int keep_eol, int num_items_max, int *num_items);
char **(*string_split_shell) (const char *string, int *num_items);
@@ -1017,9 +1019,11 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
#define weechat_string_has_highlight_regex(__string, __regex) \
weechat_plugin->string_has_highlight_regex(__string, __regex)
#define weechat_string_replace_regex(__string, __regex, __replace, \
- __reference_char) \
+ __reference_char, __callback, \
+ __callback_data) \
weechat_plugin->string_replace_regex(__string, __regex, __replace, \
- __reference_char)
+ __reference_char, __callback, \
+ __callback_data)
#define weechat_string_split(__string, __separator, __eol, __max, \
__num_items) \
weechat_plugin->string_split(__string, __separator, __eol, \