diff --git a/ChangeLog.adoc b/ChangeLog.adoc index ac187c556..c6f3a46b3 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -21,6 +21,7 @@ New features:: * core: add option `-s` in command `/command` to execute multiple commands separated by semicolons * core: allow case insensitive search of partial buffer name with `(?i)name` in command `/buffer` * core: use function util_strftimeval in evaluation of expression `date:xxx` + * api: use whole replacement string instead of first char in function string_remove_color * api: add functions config_option_get_{string|pointer} and config_{boolean|integer|string|color|enum}_inherited in scripting API * api: add info "plugin_loaded" * api: add support of specifier `%!` for timestamp in function util_strftimeval diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index 557ced193..99e6595c3 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -2308,22 +2308,23 @@ size = weechat.string_color_code_size(weechat.color("yellow,red")) # size == 7 ==== string_remove_color +_Updated in 4.3.0._ + Remove WeeChat colors from a string. Prototype: [source,c] ---- -char *weechat_string_remove_color (const char *string, - const char *replacement); +char *weechat_string_remove_color (const char *string, const char *replacement); ---- Arguments: * _string_: string * _replacement_: if not NULL and not empty, WeeChat color codes are replaced by - first char of this string, otherwise WeeChat color codes and following chars - (if related to color) are removed from string + this string, otherwise WeeChat color codes and following chars (if related + to color) are removed from string Return value: @@ -2339,7 +2340,7 @@ char *str = weechat_string_remove_color (my_string1, NULL); free (str); /* replace color codes by "?" */ -char *str = weechat_string_remove_color (my_string2, "?"); +char *str = weechat_string_remove_color (my_string2, "[color]"); /* ... */ free (str); ---- @@ -2352,7 +2353,7 @@ Script (Python): def string_remove_color(string: str, replacement: str) -> str: ... # example -str = weechat.string_remove_color(my_string, "?") +str = weechat.string_remove_color(my_string, "[color]") ---- ==== string_base_encode diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index 409b00a6d..e3120718b 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -2349,22 +2349,23 @@ size = weechat.string_color_code_size(weechat.color("yellow,red")) # size == 7 ==== string_remove_color +_Mis à jour dans la 4.3.0._ + Supprimer les couleurs WeeChat dans une chaîne. Prototype : [source,c] ---- -char *weechat_string_remove_color (const char *string, - const char *replacement); +char *weechat_string_remove_color (const char *string, const char *replacement); ---- Paramètres : * _string_ : chaîne * _replacement_ : si non NULL et non vide, les couleurs WeeChat sont remplacées - par le premier caractère de cette chaîne, sinon les codes couleurs WeeChat et - les caractères suivants (rattachés à la couleur) sont supprimés de la chaîne + par cette chaîne, sinon les codes couleurs WeeChat et les caractères suivants + (rattachés à la couleur) sont supprimés de la chaîne Valeur de retour : @@ -2381,7 +2382,7 @@ char *str = weechat_string_remove_color (ma_chaine1, NULL); free (str); /* remplace les codes couleur par "?" */ -char *str = weechat_string_remove_color (ma_chaine2, "?"); +char *str = weechat_string_remove_color (ma_chaine2, "[color]"); /* ... */ free (str); ---- @@ -2394,7 +2395,7 @@ Script (Python) : def string_remove_color(string: str, replacement: str) -> str: ... # exemple -str = weechat.string_remove_color(ma_chaine, "?") +str = weechat.string_remove_color(ma_chaine, "[color]") ---- ==== string_base_encode diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index 9c5f837b1..a0058a13e 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -2443,23 +2443,25 @@ size = weechat.string_color_code_size(weechat.color("yellow,red")) # size == 7 ==== string_remove_color +// TRANSLATION MISSING +_Updated in 4.3.0._ + Rimuove i colori di WeeChat da una stringa. Prototipo: [source,c] ---- -char *weechat_string_remove_color (const char *string, - const char *replacement); +char *weechat_string_remove_color (const char *string, const char *replacement); ---- Argomenti: * _string_: stringa -* _replacement_: se non NULL e non vuota, i codici colore di WeeChat sono - sostituiti dal primo carattere di questa stringa, altrimenti i codici colori di - WeeChat ed i caratteri seguenti (se correlate al colore) sono rimossi dalla - stringa +// TRANSLATION MISSING +* _replacement_: if not NULL and not empty, WeeChat color codes are replaced by + this string, otherwise WeeChat color codes and following chars (if related + to color) are removed from string Valore restituito: @@ -2475,7 +2477,7 @@ char *str = weechat_string_remove_color (my_string1, NULL); free (str); /* sostituisce i codici colore con "?" */ -char *str = weechat_string_remove_color (my_string2, "?"); +char *str = weechat_string_remove_color (my_string2, "[color]"); /* ... */ free (str); ---- @@ -2488,7 +2490,7 @@ Script (Python): def string_remove_color(string: str, replacement: str) -> str: ... # esempio -str = weechat.string_remove_color(my_string, "?") +str = weechat.string_remove_color(my_string, "[color]") ---- ==== string_base_encode diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index 3c6ce5562..71c038796 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -2377,22 +2377,24 @@ size = weechat.string_color_code_size(weechat.color("yellow,red")) # size == 7 ==== string_remove_color +_WeeChat バージョン 4.3.0 で更新。_ + 文字列から WeeChat 色コードを削除。 プロトタイプ: [source,c] ---- -char *weechat_string_remove_color (const char *string, - const char *replacement); +char *weechat_string_remove_color (const char *string, const char *replacement); ---- 引数: * _string_: 文字列 -* _replacement_: NULL または空文字列でなければ、WeeChat - 色コードを指定した文字列の 1 文字目で置換、そうでなければ WeeChat - 色コードとそれに続く (色に関連する) 文字を文字列から削除 +// TRANSLATION MISSING +* _replacement_: if not NULL and not empty, WeeChat color codes are replaced by + this string, otherwise WeeChat color codes and following chars (if related + to color) are removed from string 戻り値: @@ -2408,7 +2410,7 @@ char *str = weechat_string_remove_color (my_string1, NULL); free (str); /* replace color codes by "?" */ -char *str = weechat_string_remove_color (my_string2, "?"); +char *str = weechat_string_remove_color (my_string2, "[color]"); /* ... */ free (str); ---- @@ -2421,7 +2423,7 @@ free (str); def string_remove_color(string: str, replacement: str) -> str: ... # 例 -str = weechat.string_remove_color(my_string, "?") +str = weechat.string_remove_color(my_string, "[color]") ---- ==== string_base_encode diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc index 0c522d1bf..6ab324e67 100644 --- a/doc/sr/weechat_plugin_api.sr.adoc +++ b/doc/sr/weechat_plugin_api.sr.adoc @@ -2228,20 +2228,22 @@ size = weechat.string_color_code_size(weechat.color("yellow,red")) # size == 7 ==== string_remove_color +_Ажурирано у верзији 4.3.0._ + Уклања WeeChat боје из стринга. Прототип: [source,c] ---- -char *weechat_string_remove_color (const char *string, - const char *replacement); +char *weechat_string_remove_color (const char *string, const char *replacement); ---- Аргументи: * _string_: стринг -* _replacement_: ако није NULL и није празно, WeeChat кодови боје се замењују првим карактером овог стринга, у супротном се из стринга уклањају WeeChat кодови боја и карактери који следе (ако су у вези боје) +// TRANSLATION MISSING +* _replacement_: if not NULL and not empty, WeeChat color codes are replaced by this string, otherwise WeeChat color codes and following chars (if related to color) are removed from string Повратна вредност: @@ -2257,7 +2259,7 @@ char *str = weechat_string_remove_color (my_string1, NULL); free (str); /* replace color codes by "?" */ -char *str = weechat_string_remove_color (my_string2, "?"); +char *str = weechat_string_remove_color (my_string2, "[color]"); /* ... */ free (str); ---- @@ -2270,7 +2272,7 @@ free (str); def string_remove_color(string: str, replacement: str) -> str: ... # пример -str = weechat.string_remove_color(my_string, "?") +str = weechat.string_remove_color(my_string, "[color]") ---- ==== string_base_encode diff --git a/src/gui/gui-color.c b/src/gui/gui-color.c index 4a6b5ee46..1150b6ff8 100644 --- a/src/gui/gui-color.c +++ b/src/gui/gui-color.c @@ -780,12 +780,8 @@ gui_color_code_size (const char *string) } /* - * Removes WeeChat color codes from a message. - * - * If replacement is not NULL and not empty, it is used to replace color codes - * by first char of replacement (and next chars in string are NOT removed). - * If replacement is NULL or empty, color codes are removed, with following - * chars if they are related to color code. + * Removes WeeChat color codes from a message and optionally replaces them + * by a string. * * Note: result must be freed after use. */ @@ -794,20 +790,18 @@ char * gui_color_decode (const char *string, const char *replacement) { const unsigned char *ptr_string; - unsigned char *out; - int out_length, out_pos, length; + char **out; + int length; if (!string) return NULL; - out_length = (strlen ((char *)string) * 2) + 1; - out = malloc (out_length); + out = string_dyn_alloc (strlen (string) + 1); if (!out) return NULL; ptr_string = (unsigned char *)string; - out_pos = 0; - while (ptr_string && ptr_string[0] && (out_pos < out_length - 1)) + while (ptr_string && ptr_string[0]) { switch (ptr_string[0]) { @@ -945,10 +939,7 @@ gui_color_decode (const char *string, const char *replacement) break; } if (replacement && replacement[0]) - { - out[out_pos] = replacement[0]; - out_pos++; - } + string_dyn_concat (out, replacement, -1); break; case GUI_COLOR_SET_ATTR_CHAR: case GUI_COLOR_REMOVE_ATTR_CHAR: @@ -956,32 +947,24 @@ gui_color_decode (const char *string, const char *replacement) if (ptr_string[0]) ptr_string++; if (replacement && replacement[0]) - { - out[out_pos] = replacement[0]; - out_pos++; - } + string_dyn_concat (out, replacement, -1); break; case GUI_COLOR_RESET_CHAR: ptr_string++; if (replacement && replacement[0]) - { - out[out_pos] = replacement[0]; - out_pos++; - } + string_dyn_concat (out, replacement, -1); break; default: length = utf8_char_size ((char *)ptr_string); if (length == 0) length = 1; - memcpy (out + out_pos, ptr_string, length); - out_pos += length; + string_dyn_concat (out, (const char *)ptr_string, length); ptr_string += length; break; } } - out[out_pos] = '\0'; - return (char *)out; + return string_dyn_free (out, 0); } /* @@ -1317,6 +1300,8 @@ gui_color_encode_ansi (const char *string) return NULL; out = string_dyn_alloc (((strlen (string) * 3) / 2) + 1); + if (!out) + return NULL; ptr_string = (unsigned char *)string; while (ptr_string && ptr_string[0]) diff --git a/src/plugins/python/weechat.pyi b/src/plugins/python/weechat.pyi index c7da88ca9..e3e5c72b4 100644 --- a/src/plugins/python/weechat.pyi +++ b/src/plugins/python/weechat.pyi @@ -229,7 +229,7 @@ def string_remove_color(string: str, replacement: str) -> str: :: # example - str = weechat.string_remove_color(my_string, "?") + str = weechat.string_remove_color(my_string, "[color]") """ ... diff --git a/tests/unit/gui/test-gui-color.cpp b/tests/unit/gui/test-gui-color.cpp index fb51e63b2..6f7972c76 100644 --- a/tests/unit/gui/test-gui-color.cpp +++ b/tests/unit/gui/test-gui-color.cpp @@ -398,7 +398,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("reset")); WEE_CHECK_DECODE("test_reset", string, NULL); WEE_CHECK_DECODE("test_reset", string, ""); - WEE_CHECK_DECODE("test_?reset", string, "?"); + WEE_CHECK_DECODE("test_[color]reset", string, "[color]"); /* resetcolor */ snprintf (string, sizeof (string), @@ -406,7 +406,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("resetcolor")); WEE_CHECK_DECODE("test_resetcolor", string, NULL); WEE_CHECK_DECODE("test_resetcolor", string, ""); - WEE_CHECK_DECODE("test_?resetcolor", string, "?"); + WEE_CHECK_DECODE("test_[color]resetcolor", string, "[color]"); /* emphasis */ snprintf (string, sizeof (string), @@ -414,7 +414,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("emphasis")); WEE_CHECK_DECODE("test_emphasis", string, NULL); WEE_CHECK_DECODE("test_emphasis", string, ""); - WEE_CHECK_DECODE("test_?emphasis", string, "?"); + WEE_CHECK_DECODE("test_[color]emphasis", string, "[color]"); /* blink */ snprintf (string, sizeof (string), @@ -423,7 +423,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("-blink")); WEE_CHECK_DECODE("test_blink_end", string, NULL); WEE_CHECK_DECODE("test_blink_end", string, ""); - WEE_CHECK_DECODE("test_?blink?_end", string, "?"); + WEE_CHECK_DECODE("test_[color]blink[color]_end", string, "[color]"); /* dim */ snprintf (string, sizeof (string), @@ -432,7 +432,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("-dim")); WEE_CHECK_DECODE("test_dim_end", string, NULL); WEE_CHECK_DECODE("test_dim_end", string, ""); - WEE_CHECK_DECODE("test_?dim?_end", string, "?"); + WEE_CHECK_DECODE("test_[color]dim[color]_end", string, "[color]"); /* bold */ snprintf (string, sizeof (string), @@ -441,7 +441,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("-bold")); WEE_CHECK_DECODE("test_bold_end", string, NULL); WEE_CHECK_DECODE("test_bold_end", string, ""); - WEE_CHECK_DECODE("test_?bold?_end", string, "?"); + WEE_CHECK_DECODE("test_[color]bold[color]_end", string, "[color]"); /* reverse */ snprintf (string, sizeof (string), @@ -450,7 +450,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("-reverse")); WEE_CHECK_DECODE("test_reverse_end", string, NULL); WEE_CHECK_DECODE("test_reverse_end", string, ""); - WEE_CHECK_DECODE("test_?reverse?_end", string, "?"); + WEE_CHECK_DECODE("test_[color]reverse[color]_end", string, "[color]"); /* italic */ snprintf (string, sizeof (string), @@ -459,7 +459,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("-italic")); WEE_CHECK_DECODE("test_italic_end", string, NULL); WEE_CHECK_DECODE("test_italic_end", string, ""); - WEE_CHECK_DECODE("test_?italic?_end", string, "?"); + WEE_CHECK_DECODE("test_[color]italic[color]_end", string, "[color]"); /* underline */ snprintf (string, sizeof (string), @@ -468,7 +468,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("-underline")); WEE_CHECK_DECODE("test_underline_end", string, NULL); WEE_CHECK_DECODE("test_underline_end", string, ""); - WEE_CHECK_DECODE("test_?underline?_end", string, "?"); + WEE_CHECK_DECODE("test_[color]underline[color]_end", string, "[color]"); /* bar_fg */ snprintf (string, sizeof (string), @@ -476,7 +476,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("bar_fg")); WEE_CHECK_DECODE("test_bar_fg", string, NULL); WEE_CHECK_DECODE("test_bar_fg", string, ""); - WEE_CHECK_DECODE("test_?bar_fg", string, "?"); + WEE_CHECK_DECODE("test_[color]bar_fg", string, "[color]"); /* bar_delim */ snprintf (string, sizeof (string), @@ -484,7 +484,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("bar_delim")); WEE_CHECK_DECODE("test_bar_delim", string, NULL); WEE_CHECK_DECODE("test_bar_delim", string, ""); - WEE_CHECK_DECODE("test_?bar_delim", string, "?"); + WEE_CHECK_DECODE("test_[color]bar_delim", string, "[color]"); /* bar_bg */ snprintf (string, sizeof (string), @@ -492,7 +492,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("bar_bg")); WEE_CHECK_DECODE("test_bar_bg", string, NULL); WEE_CHECK_DECODE("test_bar_bg", string, ""); - WEE_CHECK_DECODE("test_?bar_bg", string, "?"); + WEE_CHECK_DECODE("test_[color]bar_bg", string, "[color]"); /* fg color */ snprintf (string, sizeof (string), @@ -500,7 +500,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("blue")); WEE_CHECK_DECODE("test_blue", string, NULL); WEE_CHECK_DECODE("test_blue", string, ""); - WEE_CHECK_DECODE("test_?blue", string, "?"); + WEE_CHECK_DECODE("test_[color]blue", string, "[color]"); /* bg color */ snprintf (string, sizeof (string), @@ -508,7 +508,7 @@ TEST(GuiColor, Decode) gui_color_get_custom (",blue")); WEE_CHECK_DECODE("test_blue", string, NULL); WEE_CHECK_DECODE("test_blue", string, ""); - WEE_CHECK_DECODE("test_?blue", string, "?"); + WEE_CHECK_DECODE("test_[color]blue", string, "[color]"); /* fg+bg color */ snprintf (string, sizeof (string), @@ -516,7 +516,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("yellow,blue")); WEE_CHECK_DECODE("test_yellow_blue", string, NULL); WEE_CHECK_DECODE("test_yellow_blue", string, ""); - WEE_CHECK_DECODE("test_?yellow_blue", string, "?"); + WEE_CHECK_DECODE("test_[color]yellow_blue", string, "[color]"); /* fg terminal color */ snprintf (string, sizeof (string), @@ -524,7 +524,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("214")); WEE_CHECK_DECODE("test_214", string, NULL); WEE_CHECK_DECODE("test_214", string, ""); - WEE_CHECK_DECODE("test_?214", string, "?"); + WEE_CHECK_DECODE("test_[color]214", string, "[color]"); /* bg terminal color */ snprintf (string, sizeof (string), @@ -532,7 +532,7 @@ TEST(GuiColor, Decode) gui_color_get_custom (",214")); WEE_CHECK_DECODE("test_,214", string, NULL); WEE_CHECK_DECODE("test_,214", string, ""); - WEE_CHECK_DECODE("test_?,214", string, "?"); + WEE_CHECK_DECODE("test_[color],214", string, "[color]"); /* fg+bg terminal color */ snprintf (string, sizeof (string), @@ -540,7 +540,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("227,240")); WEE_CHECK_DECODE("test_227,240", string, NULL); WEE_CHECK_DECODE("test_227,240", string, ""); - WEE_CHECK_DECODE("test_?227,240", string, "?"); + WEE_CHECK_DECODE("test_[color]227,240", string, "[color]"); /* fg terminal color + bg color */ snprintf (string, sizeof (string), @@ -548,7 +548,7 @@ TEST(GuiColor, Decode) gui_color_get_custom ("227,blue")); WEE_CHECK_DECODE("test_227,blue", string, NULL); WEE_CHECK_DECODE("test_227,blue", string, ""); - WEE_CHECK_DECODE("test_?227,blue", string, "?"); + WEE_CHECK_DECODE("test_[color]227,blue", string, "[color]"); /* WeeChat color */ snprintf (string, sizeof (string), @@ -556,7 +556,7 @@ TEST(GuiColor, Decode) GUI_COLOR(GUI_COLOR_CHAT_HOST)); WEE_CHECK_DECODE("test_option_weechat.color.chat_host", string, NULL); WEE_CHECK_DECODE("test_option_weechat.color.chat_host", string, ""); - WEE_CHECK_DECODE("test_?option_weechat.color.chat_host", string, "?"); + WEE_CHECK_DECODE("test_[color]option_weechat.color.chat_host", string, "[color]"); } /*