diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common/utf8.c | 50 | ||||
-rw-r--r-- | src/common/utf8.h | 11 | ||||
-rw-r--r-- | src/irc/irc-recv.c | 26 |
3 files changed, 70 insertions, 17 deletions
diff --git a/src/common/utf8.c b/src/common/utf8.c index abcef47cf..176fee797 100644 --- a/src/common/utf8.c +++ b/src/common/utf8.c @@ -26,16 +26,6 @@ #include <stdlib.h> #include <string.h> -#ifndef __USE_XOPEN -#define __USE_XOPEN -#endif - -#if defined(__OpenBSD__) -#include <utf8/wchar.h> -#else -#include <wchar.h> -#endif - #include "weechat.h" #include "utf8.h" #include "util.h" @@ -394,3 +384,43 @@ utf8_pos (char *string, int real_pos) } return count; } + +/* + * utf8_get_wc: get wide char from string (first char) + */ + +wint_t +utf8_get_wc (char *string) +{ + int char_size; + wint_t result; + + if (!string || !string[0]) + return WEOF; + + char_size = utf8_char_size (string); + switch (char_size) + { + case 1: + result = (wint_t)string[0]; + break; + case 2: + result = ((wint_t)((unsigned char)string[0])) << 8 + | ((wint_t)((unsigned char)string[1])); + break; + case 3: + result = ((wint_t)((unsigned char)string[0])) << 16 + | ((wint_t)((unsigned char)string[1])) << 8 + | ((wint_t)((unsigned char)string[2])); + break; + case 4: + result = ((wint_t)((unsigned char)string[0])) << 24 + | ((wint_t)((unsigned char)string[1])) << 16 + | ((wint_t)((unsigned char)string[2])) << 8 + | ((wint_t)((unsigned char)string[3])); + break; + default: + result = WEOF; + } + return result; +} diff --git a/src/common/utf8.h b/src/common/utf8.h index 3a35bec69..caf7b7b10 100644 --- a/src/common/utf8.h +++ b/src/common/utf8.h @@ -20,6 +20,16 @@ #ifndef __WEECHAT_UTF8_H #define __WEECHAT_UTF8_H 1 +#ifndef __USE_XOPEN +#define __USE_XOPEN +#endif + +#if defined(__OpenBSD__) +#include <utf8/wchar.h> +#else +#include <wchar.h> +#endif + extern int local_utf8; extern void utf8_init (); @@ -35,5 +45,6 @@ extern int utf8_width_screen (char *); extern char *utf8_add_offset (char *, int); extern int utf8_real_pos (char *, int); extern int utf8_pos (char *, int); +extern wint_t utf8_get_wc (char *); #endif /* utf8.h */ diff --git a/src/irc/irc-recv.c b/src/irc/irc-recv.c index 0c115de87..03960d788 100644 --- a/src/irc/irc-recv.c +++ b/src/irc/irc-recv.c @@ -29,6 +29,7 @@ #include <stdio.h> #include <string.h> #include <ctype.h> +#include <wctype.h> #include <sys/time.h> #include <time.h> #include <sys/utsname.h> @@ -39,6 +40,7 @@ #include "../common/alias.h" #include "../common/command.h" #include "../common/hotlist.h" +#include "../common/utf8.h" #include "../common/util.h" #include "../common/weeconfig.h" #include "../gui/gui.h" @@ -57,9 +59,14 @@ int command_ignored, command_force_highlight; */ int -irc_recv_is_word_char (char c) +irc_recv_is_word_char (char *str) { - if (isalnum (c)) + wint_t c = utf8_get_wc (str); + + if (c == WEOF) + return 0; + + if (iswalnum (c)) return 1; switch (c) @@ -113,10 +120,12 @@ irc_recv_is_highlight (char *message, char *nick) match = strstr (message, nick); if (match) { - match_pre = match - 1; + match_pre = utf8_prev_char (message, match); + if (!match_pre) + match_pre = match - 1; match_post = match + strlen(nick); - startswith = ((match == message) || (!irc_recv_is_word_char (match_pre[0]))); - endswith = ((!match_post[0]) || (!irc_recv_is_word_char (match_post[0]))); + startswith = ((match == message) || (!irc_recv_is_word_char (match_pre))); + endswith = ((!match_post[0]) || (!irc_recv_is_word_char (match_post))); if (startswith && endswith) return 1; } @@ -188,9 +197,12 @@ irc_recv_is_highlight (char *message, char *nick) while ((match = strstr (msg_pos, pos)) != NULL) { match_pre = match - 1; + match_pre = utf8_prev_char (msg, match); + if (!match_pre) + match_pre = match - 1; match_post = match + length; - startswith = ((match == msg) || (!irc_recv_is_word_char (match_pre[0]))); - endswith = ((!match_post[0]) || (!irc_recv_is_word_char (match_post[0]))); + startswith = ((match == msg) || (!irc_recv_is_word_char (match_pre))); + endswith = ((!match_post[0]) || (!irc_recv_is_word_char (match_post))); if ((wildcard_start && wildcard_end) || (!wildcard_start && !wildcard_end && startswith && endswith) || |