1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-29 14:26:39 +02:00

Add "replacement" argument for string_remove_color in plugin API

This commit is contained in:
Sebastien Helleu
2009-03-18 18:02:48 +01:00
parent 5f243ddd86
commit a394ed5d6f
17 changed files with 160 additions and 87 deletions
+47
View File
@@ -1099,6 +1099,53 @@ if (str)
</para>
</section>
<section id="secPluginCApi_weechat_string_remove_color">
<title>weechat_string_remove_color</title>
<para>
Prototype:
<programlisting>
char *weechat_string_remove_color (const char *string, const char *replacement);
</programlisting>
</para>
<para>
Remove WeeChat colors from a string.
</para>
<para>
Arguments:
<itemizedlist>
<listitem>
<para>
<option>string</option>: string
</para>
</listitem>
<listitem>
<para>
<option>replacement</option>: 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
</para>
</listitem>
</itemizedlist>
</para>
<para>
Return value: string without color.
</para>
<note>
<para>
Result has to be free by a call to "free" after use.
</para>
</note>
<para>
Example:
<screen>
char *str1 = weechat_string_remove_color (my_string1, NULL); /* remove colors and related chars */
char *str2 = weechat_string_remove_color (my_string2, "?"); /* replace color codes by "?" */
</screen>
</para>
</section>
</section>
<!-- ==============================[ UTF-8 ]============================= -->
+2 -3
View File
@@ -1372,10 +1372,9 @@ hook_print_exec (struct t_gui_buffer *buffer, struct t_gui_line *line)
if (!line->message || !line->message[0])
return;
prefix_no_color = (line->prefix) ?
(char *)gui_color_decode ((unsigned char *)line->prefix) : NULL;
prefix_no_color = (line->prefix) ? gui_color_decode (line->prefix, NULL) : NULL;
message_no_color = (char *)gui_color_decode ((unsigned char *)line->message);
message_no_color = gui_color_decode (line->message, NULL);
if (!message_no_color)
{
free (prefix_no_color);
-10
View File
@@ -1309,13 +1309,3 @@ string_format_size (unsigned long size)
return strdup (str_size);
}
/*
* string_remove_color: remove WeeChat color codes in string
*/
char *
string_remove_color (const char *string)
{
return (char *)gui_color_decode ((unsigned char *)string);
}
-1
View File
@@ -56,6 +56,5 @@ extern char *string_iconv_from_internal (const char *charset,
const char *string);
extern void string_iconv_fprintf (FILE *file, const char *data, ...);
extern char *string_format_size (unsigned long size);
extern char *string_remove_color (const char *string);
#endif /* wee-string.h */
+1 -1
View File
@@ -51,7 +51,7 @@
#define WEECHAT_COPYRIGHT_DATE "(c) 2003-2009"
#define WEECHAT_WEBSITE "http://weechat.flashtux.org"
#define WEECHAT_WEBSITE "http://weechat.flashtux.org/"
/* log file */
+1 -1
View File
@@ -597,7 +597,7 @@ gui_chat_display_time_and_prefix (struct t_gui_window *window,
prefix_highlighted = NULL;
if (line->highlight)
{
prefix_highlighted = (char *)gui_color_decode ((unsigned char *)line->prefix);
prefix_highlighted = gui_color_decode (line->prefix, NULL);
if (!simulate)
{
gui_window_set_weechat_color (GUI_WINDOW_OBJECTS(window)->win_chat,
+1 -1
View File
@@ -1699,7 +1699,7 @@ gui_buffer_dump_hexa (struct t_gui_buffer *buffer)
{
/* display line without colors */
message_without_colors = (ptr_line->message) ?
(char *)gui_color_decode ((unsigned char *)ptr_line->message) : NULL;
gui_color_decode (ptr_line->message, NULL) : NULL;
log_printf ("");
log_printf (" line %d: %s",
num_line,
+6 -6
View File
@@ -507,7 +507,7 @@ gui_chat_line_search (struct t_gui_line *line, const char *text,
if (line->prefix)
{
prefix = (char *)gui_color_decode ((unsigned char *)line->prefix);
prefix = gui_color_decode (line->prefix, NULL);
if (prefix)
{
if ((case_sensitive && (strstr (prefix, text)))
@@ -519,7 +519,7 @@ gui_chat_line_search (struct t_gui_line *line, const char *text,
if (!rc)
{
message = (char *)gui_color_decode ((unsigned char *)line->message);
message = gui_color_decode (line->message, NULL);
if (message)
{
if ((case_sensitive && (strstr (message, text)))
@@ -555,7 +555,7 @@ gui_chat_line_match_regex (struct t_gui_line *line, regex_t *regex_prefix,
if (line->prefix)
{
prefix = (char *)gui_color_decode ((unsigned char *)line->prefix);
prefix = gui_color_decode (line->prefix, NULL);
if (!prefix
|| (regex_prefix && (regexec (regex_prefix, prefix, 0, NULL, 0) != 0)))
match_prefix = 0;
@@ -568,7 +568,7 @@ gui_chat_line_match_regex (struct t_gui_line *line, regex_t *regex_prefix,
if (line->message)
{
message = (char *)gui_color_decode ((unsigned char *)line->message);
message = gui_color_decode (line->message, NULL);
if (!message
|| (regex_message && (regexec (regex_message, message, 0, NULL, 0) != 0)))
match_message = 0;
@@ -654,7 +654,7 @@ gui_chat_line_has_highlight (struct t_gui_buffer *buffer,
}
/* remove color codes from line message */
msg_no_color = (char *)gui_color_decode ((unsigned char *)line->message);
msg_no_color = gui_color_decode (line->message, NULL);
if (!msg_no_color)
return 0;
@@ -803,7 +803,7 @@ gui_chat_build_string_prefix_message (struct t_gui_line *line)
if (string)
{
string_without_colors = (char *)gui_color_decode ((unsigned char *)string);
string_without_colors = gui_color_decode (string, NULL);
if (string_without_colors)
{
free (string);
+70 -41
View File
@@ -227,12 +227,18 @@ gui_color_get_custom (const char *color_name)
/*
* gui_color_decode: parses a message and remove WeeChat color codes
* 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
* After use, string returned has to be free()
*/
unsigned char *
gui_color_decode (const unsigned char *string)
char *
gui_color_decode (const char *string, const char *replacement)
{
const unsigned char *ptr_string;
unsigned char *out;
int out_length, out_pos, length;
@@ -244,65 +250,88 @@ gui_color_decode (const unsigned char *string)
if (!out)
return NULL;
ptr_string = (unsigned char *)string;
out_pos = 0;
while (string && string[0] && (out_pos < out_length - 1))
while (ptr_string && ptr_string[0] && (out_pos < out_length - 1))
{
switch (string[0])
switch (ptr_string[0])
{
case GUI_COLOR_COLOR_CHAR:
string++;
switch (string[0])
ptr_string++;
if (replacement && replacement[0])
{
case GUI_COLOR_FG_CHAR:
case GUI_COLOR_BG_CHAR:
if (string[1] && string[2])
string += 3;
break;
case GUI_COLOR_FG_BG_CHAR:
if (string[1] && string[2] && (string[3] == ',')
&& string[4] && string[5])
string += 6;
break;
case GUI_COLOR_BAR_CHAR:
string++;
switch (string[0])
{
case GUI_COLOR_BAR_FG_CHAR:
case GUI_COLOR_BAR_BG_CHAR:
case GUI_COLOR_BAR_DELIM_CHAR:
case GUI_COLOR_BAR_START_INPUT_CHAR:
case GUI_COLOR_BAR_MOVE_CURSOR_CHAR:
string++;
break;
}
break;
default:
if (isdigit (string[0]) && isdigit (string[1]))
string += 2;
break;
out[out_pos] = replacement[0];
out_pos++;
}
else
{
switch (ptr_string[0])
{
case GUI_COLOR_FG_CHAR:
case GUI_COLOR_BG_CHAR:
if (ptr_string[1] && ptr_string[2])
ptr_string += 3;
break;
case GUI_COLOR_FG_BG_CHAR:
if (ptr_string[1] && ptr_string[2] && (ptr_string[3] == ',')
&& ptr_string[4] && ptr_string[5])
ptr_string += 6;
break;
case GUI_COLOR_BAR_CHAR:
ptr_string++;
switch (ptr_string[0])
{
case GUI_COLOR_BAR_FG_CHAR:
case GUI_COLOR_BAR_BG_CHAR:
case GUI_COLOR_BAR_DELIM_CHAR:
case GUI_COLOR_BAR_START_INPUT_CHAR:
case GUI_COLOR_BAR_MOVE_CURSOR_CHAR:
ptr_string++;
break;
}
break;
default:
if (isdigit (ptr_string[0]) && isdigit (ptr_string[1]))
ptr_string += 2;
break;
}
}
break;
case GUI_COLOR_SET_WEECHAT_CHAR:
case GUI_COLOR_REMOVE_WEECHAT_CHAR:
string++;
if (string[0])
string++;
ptr_string++;
if (replacement && replacement[0])
{
out[out_pos] = replacement[0];
out_pos++;
}
else
{
if (ptr_string[0])
ptr_string++;
}
break;
case GUI_COLOR_RESET_CHAR:
string++;
ptr_string++;
if (replacement && replacement[0])
{
out[out_pos] = replacement[0];
out_pos++;
}
break;
default:
length = utf8_char_size ((char *)string);
length = utf8_char_size ((char *)ptr_string);
if (length == 0)
length = 1;
memcpy (out + out_pos, string, length);
memcpy (out + out_pos, ptr_string, length);
out_pos += length;
string += length;
ptr_string += length;
break;
}
}
out[out_pos] = '\0';
return out;
return (char *)out;
}
/*
+1 -1
View File
@@ -126,7 +126,7 @@ extern struct t_gui_color *gui_color[];
extern int gui_color_search_config_int (const char *color_name);
extern const char *gui_color_get_custom (const char *color_name);
extern unsigned char *gui_color_decode (const unsigned char *string);
extern char *gui_color_decode (const char *string, const char *replacement);
extern void gui_color_free (struct t_gui_color *color);
/* color functions (GUI dependent) */
+2 -1
View File
@@ -48,6 +48,7 @@
#include "../gui/gui-bar-item.h"
#include "../gui/gui-buffer.h"
#include "../gui/gui-chat.h"
#include "../gui/gui-color.h"
#include "../gui/gui-nicklist.h"
#include "../gui/gui-window.h"
#include "plugin.h"
@@ -412,7 +413,7 @@ plugin_load (const char *filename)
new_plugin->string_split_command = &string_split_command;
new_plugin->string_free_splitted_command = &string_free_splitted_command;
new_plugin->string_format_size = &string_format_size;
new_plugin->string_remove_color = &string_remove_color;
new_plugin->string_remove_color = &gui_color_decode;
new_plugin->utf8_has_8bits = &utf8_has_8bits;
new_plugin->utf8_is_valid = &utf8_is_valid;
+5 -4
View File
@@ -371,7 +371,7 @@ weechat_lua_api_ngettext (lua_State *L)
static int
weechat_lua_api_string_remove_color (lua_State *L)
{
const char *string;
const char *string, *replacement;
char *result;
int n;
@@ -388,15 +388,16 @@ weechat_lua_api_string_remove_color (lua_State *L)
n = lua_gettop (lua_current_interpreter);
if (n < 1)
if (n < 2)
{
WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("string_remove_color");
LUA_RETURN_EMPTY;
}
string = lua_tostring (lua_current_interpreter, -1);
string = lua_tostring (lua_current_interpreter, -2);
replacement = lua_tostring (lua_current_interpreter, -1);
result = weechat_string_remove_color (string);
result = weechat_string_remove_color (string, replacement);
LUA_RETURN_STRING_FREE(result);
}
+4 -3
View File
@@ -316,7 +316,7 @@ static XS (XS_weechat_api_ngettext)
static XS (XS_weechat_api_string_remove_color)
{
char *result, *string;
char *result, *string, *replacement;
dXSARGS;
/* make C compiler happy */
@@ -328,14 +328,15 @@ static XS (XS_weechat_api_string_remove_color)
PERL_RETURN_EMPTY;
}
if (items < 1)
if (items < 2)
{
WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("string_remove_color");
PERL_RETURN_EMPTY;
}
string = SvPV (ST (0), PL_na);
result = weechat_string_remove_color (string);
replacement = SvPV (ST (1), PL_na);
result = weechat_string_remove_color (string, replacement);
PERL_RETURN_STRING_FREE(result);
}
@@ -324,7 +324,7 @@ weechat_python_api_ngettext (PyObject *self, PyObject *args)
static PyObject *
weechat_python_api_string_remove_color (PyObject *self, PyObject *args)
{
char *string, *result;
char *string, *replacement, *result;
PyObject *object;
/* make C compiler happy */
@@ -337,14 +337,15 @@ weechat_python_api_string_remove_color (PyObject *self, PyObject *args)
}
string = NULL;
replacement = NULL;
if (!PyArg_ParseTuple (args, "s", &string))
if (!PyArg_ParseTuple (args, "ss", &string, &replacement))
{
WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("string_remove_color");
PYTHON_RETURN_EMPTY;
}
result = weechat_string_remove_color (string);
result = weechat_string_remove_color (string, replacement);
PYTHON_RETURN_STRING_FREE(result);
}
+9 -5
View File
@@ -372,9 +372,10 @@ weechat_ruby_api_ngettext (VALUE class, VALUE single, VALUE plural,
*/
static VALUE
weechat_ruby_api_string_remove_color (VALUE class, VALUE string)
weechat_ruby_api_string_remove_color (VALUE class, VALUE string,
VALUE replacement)
{
char *c_string, *result;
char *c_string, *c_replacement, *result;
VALUE return_value;
/* make C compiler happy */
@@ -387,18 +388,21 @@ weechat_ruby_api_string_remove_color (VALUE class, VALUE string)
}
c_string = NULL;
c_replacement = NULL;
if (NIL_P (string))
if (NIL_P (string) || NIL_P (replacement))
{
WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("string_remove_color");
RUBY_RETURN_EMPTY;
}
Check_Type (string, T_STRING);
Check_Type (replacement, T_STRING);
c_string = STR2CSTR (string);
c_replacement = STR2CSTR (replacement);
result = weechat_string_remove_color (c_string);
result = weechat_string_remove_color (c_string, c_replacement);
RUBY_RETURN_STRING_FREE(result);
}
@@ -6580,7 +6584,7 @@ weechat_ruby_api_init (VALUE ruby_mWeechat)
rb_define_module_function (ruby_mWeechat, "iconv_from_internal", &weechat_ruby_api_iconv_from_internal, 2);
rb_define_module_function (ruby_mWeechat, "gettext", &weechat_ruby_api_gettext, 1);
rb_define_module_function (ruby_mWeechat, "ngettext", &weechat_ruby_api_ngettext, 3);
rb_define_module_function (ruby_mWeechat, "string_remove_color", &weechat_ruby_api_string_remove_color, 1);
rb_define_module_function (ruby_mWeechat, "string_remove_color", &weechat_ruby_api_string_remove_color, 2);
rb_define_module_function (ruby_mWeechat, "mkdir_home", &weechat_ruby_api_mkdir_home, 2);
rb_define_module_function (ruby_mWeechat, "mkdir", &weechat_ruby_api_mkdir, 2);
rb_define_module_function (ruby_mWeechat, "mkdir_parents", &weechat_ruby_api_mkdir_parents, 2);
+4 -3
View File
@@ -443,7 +443,7 @@ weechat_tcl_api_string_remove_color (ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
Tcl_Obj* objp;
char *result, *string;
char *result, *replacement, *string;
int i;
/* make C compiler happy */
@@ -455,14 +455,15 @@ weechat_tcl_api_string_remove_color (ClientData clientData, Tcl_Interp *interp,
TCL_RETURN_EMPTY;
}
if (objc < 2)
if (objc < 3)
{
WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("string_remove_color");
TCL_RETURN_EMPTY;
}
string = Tcl_GetStringFromObj (objv[1], &i);
result = weechat_string_remove_color (string);
replacement = Tcl_GetStringFromObj (objv[2], &i);
result = weechat_string_remove_color (string, replacement);
TCL_RETURN_STRING_FREE(result);
}
+3 -3
View File
@@ -167,7 +167,7 @@ struct t_weechat_plugin
char **(*string_split_command) (const char *command, char separator);
void (*string_free_splitted_command) (char **splitted_command);
char *(*string_format_size) (unsigned long size);
char *(*string_remove_color) (const char *string);
char *(*string_remove_color) (const char *string, const char *replacement);
/* UTF-8 strings */
int (*utf8_has_8bits) (const char *string);
@@ -694,8 +694,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
weechat_plugin->string_free_splitted_command(__splitted_command)
#define weechat_string_format_size(__size) \
weechat_plugin->string_format_size(__size)
#define weechat_string_remove_color(__string) \
weechat_plugin->string_remove_color(__string)
#define weechat_string_remove_color(__string, __replacement) \
weechat_plugin->string_remove_color(__string, __replacement)
/* UTF-8 strings */
#define weechat_utf8_has_8bits(__string) \