Skip to content

Commit

Permalink
Add "replacement" argument for string_remove_color in plugin API
Browse files Browse the repository at this point in the history
  • Loading branch information
flashcode committed Mar 18, 2009
1 parent 5f243dd commit a394ed5
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 87 deletions.
47 changes: 47 additions & 0 deletions doc/en/dev/plugin_c_api.en.xml
Expand Up @@ -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 ]============================= -->
Expand Down
5 changes: 2 additions & 3 deletions src/core/wee-hook.c
Expand Up @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions src/core/wee-string.c
Expand Up @@ -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 change: 0 additions & 1 deletion src/core/wee-string.h
Expand Up @@ -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 */
2 changes: 1 addition & 1 deletion src/core/weechat.h
Expand Up @@ -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 */

Expand Down
2 changes: 1 addition & 1 deletion src/gui/curses/gui-curses-chat.c
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/gui/gui-buffer.c
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions src/gui/gui-chat.c
Expand Up @@ -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)))
Expand All @@ -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)))
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
111 changes: 70 additions & 41 deletions src/gui/gui-color.c
Expand Up @@ -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;

Expand All @@ -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;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/gui/gui-color.h
Expand Up @@ -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) */
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/plugin.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/scripts/lua/weechat-lua-api.c
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/scripts/perl/weechat-perl-api.c
Expand Up @@ -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 */
Expand All @@ -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);
}
Expand Down

0 comments on commit a394ed5

Please sign in to comment.