From db5e7f37310b5a76a66e72598d4f36247fafcfd0 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Thu, 28 Dec 2000 17:04:33 +0000 Subject: [PATCH] emphasis moved to fe-messages. added a few checks so that non-words aren't treated as emphasis git-svn-id: http://svn.irssi.org/repos/irssi/trunk@1022 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/fe-common/core/Makefile.am | 1 + src/fe-common/core/fe-messages.c | 80 +++++++++++++++++++++++++++++++- src/fe-common/core/fe-messages.h | 8 ++++ src/fe-common/core/printtext.c | 66 -------------------------- src/fe-common/irc/dcc/fe-dcc.c | 9 +++- 5 files changed, 96 insertions(+), 68 deletions(-) create mode 100644 src/fe-common/core/fe-messages.h diff --git a/src/fe-common/core/Makefile.am b/src/fe-common/core/Makefile.am index 90a356ce..bb341cf5 100644 --- a/src/fe-common/core/Makefile.am +++ b/src/fe-common/core/Makefile.am @@ -40,6 +40,7 @@ noinst_HEADERS = \ command-history.h \ completion.h \ fe-common-core.h \ + fe-messages.h \ fe-queries.h \ formats.h \ hilight-text.h \ diff --git a/src/fe-common/core/fe-messages.c b/src/fe-common/core/fe-messages.c index 44a03c66..fa431939 100644 --- a/src/fe-common/core/fe-messages.c +++ b/src/fe-common/core/fe-messages.c @@ -35,6 +35,67 @@ #include "ignore.h" #include "printtext.h" +#define ishighalnum(c) ((unsigned char) (c) >= 128 || isalnum(c)) + +/* convert _underlined_ and *bold* words (and phrases) to use real + underlining or bolding */ +char *expand_emphasis(const char *text) +{ + GString *str; + char *ret; + int pos; + + g_return_val_if_fail(text != NULL, NULL); + + str = g_string_new(text); + + for (pos = 0; pos < str->len; pos++) { + char type, *bgn, *end; + + bgn = str->str + pos; + + if (*bgn == '*') + type = 2; /* bold */ + else if (*bgn == '_') + type = 31; /* underlined */ + else + continue; + + /* check that the beginning marker starts a word, and + * that the matching end marker ends a word */ + if ((pos > 0 && isalnum(bgn[-1])) || !ishighalnum(bgn[1])) + continue; + if ((end = strchr(bgn+1, *bgn)) == NULL) + continue; + if (!ishighalnum(end[-1]) || + isalnum(end[1]) || end[1] == type) + continue; + + /* allow only *word* emphasis, not *multiple words* */ + if (!settings_get_bool("emphasis_multiword")) { + char *c; + for (c = bgn+1; c != end; c++) { + if (!isalnum(*c)) + break; + } + if (c != end) continue; + } + + if (settings_get_bool("emphasis_replace")) { + *bgn = *end = type; + pos += (end-bgn); + } else { + g_string_insert_c(str, pos, type); + pos += (end - bgn) + 2; + g_string_insert_c(str, pos++, type); + } + } + + ret = str->str; + g_string_free(str, FALSE); + return ret; +} + static char *get_nickmode(CHANNEL_REC *channel, const char *nick) { NICK_REC *nickrec; @@ -60,7 +121,7 @@ static void sig_message_public(SERVER_REC *server, const char *msg, CHANNEL_REC *chanrec; const char *nickmode; int for_me, print_channel, level; - char *color; + char *color, *freemsg; chanrec = channel_find(server, target); g_return_if_fail(chanrec != NULL); @@ -77,6 +138,11 @@ static void sig_message_public(SERVER_REC *server, const char *msg, level = MSGLEVEL_PUBLIC | (for_me || color != NULL ? MSGLEVEL_HILIGHT : MSGLEVEL_NOHILIGHT); + if (settings_get_bool("emphasis")) + msg = freemsg = expand_emphasis(msg); + else + freemsg = NULL; + nickmode = get_nickmode(chanrec, nick); if (!print_channel) { /* message to active channel in window */ @@ -105,6 +171,7 @@ static void sig_message_public(SERVER_REC *server, const char *msg, } } + g_free_not_null(freemsg); g_free_not_null(color); } @@ -112,11 +179,19 @@ static void sig_message_private(SERVER_REC *server, const char *msg, const char *nick, const char *address) { QUERY_REC *query; + char *freemsg; + + if (settings_get_bool("emphasis")) + msg = freemsg = expand_emphasis(msg); + else + freemsg = NULL; query = query_find(server, nick); printformat(server, nick, MSGLEVEL_MSGS, query == NULL ? IRCTXT_MSG_PRIVATE : IRCTXT_MSG_PRIVATE_QUERY, nick, address, msg); + + g_free_not_null(freemsg); } static void print_own_channel_message(SERVER_REC *server, CHANNEL_REC *channel, @@ -401,6 +476,9 @@ static void sig_message_topic(SERVER_REC *server, const char *channel, void fe_messages_init(void) { + settings_add_bool("lookandfeel", "emphasis", TRUE); + settings_add_bool("lookandfeel", "emphasis_replace", FALSE); + settings_add_bool("lookandfeel", "emphasis_multiword", FALSE); settings_add_bool("lookandfeel", "show_nickmode", TRUE); settings_add_bool("lookandfeel", "show_nickmode_empty", TRUE); settings_add_bool("lookandfeel", "print_active_channel", FALSE); diff --git a/src/fe-common/core/fe-messages.h b/src/fe-common/core/fe-messages.h new file mode 100644 index 00000000..c277840f --- /dev/null +++ b/src/fe-common/core/fe-messages.h @@ -0,0 +1,8 @@ +#ifndef __FE_MESSAGES_H +#define __FE_MESSAGES_H + +/* convert _underlined_ and *bold* words (and phrases) to use real + underlining or bolding */ +char *expand_emphasis(const char *text); + +#endif diff --git a/src/fe-common/core/printtext.c b/src/fe-common/core/printtext.c index 5a4807eb..e631b1e7 100644 --- a/src/fe-common/core/printtext.c +++ b/src/fe-common/core/printtext.c @@ -273,64 +273,6 @@ static void msg_beep_check(SERVER_REC *server, int level) } } -/* convert _underlined_ and *bold* words (and phrases) to use - * mIRC(?)-style emphasis - */ -static char *expand_emphasis(TEXT_DEST_REC *dest, const char *text) -{ - GString *str; - char *ret; - int pos; - - if ((dest->level & (MSGLEVEL_PUBLIC|MSGLEVEL_MSGS|MSGLEVEL_DCCMSGS)) == 0) - return g_strdup(text); - - str = g_string_new(text); - - for (pos = 0; str->str[pos] != '\0'; pos++) { - char type, *bgn, *end; - - bgn = str->str + pos; - - if (*bgn == '*') - type = 2; /* bold */ - else if (*bgn == '_') - type = 31; /* underlined */ - else - continue; - - /* check that the beginning marker starts a word, and - * that the matching end marker ends a word */ - if (pos > 0 && isalnum(bgn[-1])) - continue; - if ((end = strchr(bgn+1, *bgn)) == NULL) - continue; - if (isalnum(*(end + 1)) || *(end + 1) == type) - continue; - - /* allow only *word* emphasis, not *multiple words* */ - if (!settings_get_bool("emphasis_multiword")) { - char *c; - for (c = bgn+1; c != end; c++) { - if (! isalnum(*c)) - break; - } - if (c != end) continue; - } - - if (settings_get_bool("emphasis_replace")) { - *bgn = *end = type; - } else { - g_string_insert_c(str, pos++, type); - g_string_insert_c(str, pos + (end - bgn) + 1, type); - } - } - - ret = str->str; - g_string_free(str, FALSE); - return ret; -} - static void sig_print_text(TEXT_DEST_REC *dest, const char *text) { char *str, *tmp; @@ -349,11 +291,6 @@ static void sig_print_text(TEXT_DEST_REC *dest, const char *text) str = format_add_linestart(text, tmp); g_free_not_null(tmp); - if (settings_get_bool("emphasis")) { - char *tmp = str; - str = expand_emphasis(dest, tmp); - g_free(tmp); - } format_send_to_gui(dest, str); g_free(str); @@ -397,9 +334,6 @@ static void read_settings(void) void printtext_init(void) { settings_add_int("misc", "timestamp_timeout", 0); - settings_add_bool("lookandfeel", "emphasis", TRUE); - settings_add_bool("lookandfeel", "emphasis_replace", FALSE); - settings_add_bool("lookandfeel", "emphasis_multiword", FALSE); sending_print_starting = FALSE; signal_gui_print_text = signal_get_uniq_id("gui print text"); diff --git a/src/fe-common/irc/dcc/fe-dcc.c b/src/fe-common/irc/dcc/fe-dcc.c index bab98124..0bdfd909 100644 --- a/src/fe-common/irc/dcc/fe-dcc.c +++ b/src/fe-common/irc/dcc/fe-dcc.c @@ -37,6 +37,7 @@ #include "module-formats.h" #include "printtext.h" +#include "fe-messages.h" static int autocreate_dccquery; @@ -142,16 +143,22 @@ static void dcc_chat_ctcp(const char *msg, DCC_REC *dcc) static void dcc_chat_msg(DCC_REC *dcc, const char *msg) { - char *sender; + char *sender, *freemsg; g_return_if_fail(dcc != NULL); g_return_if_fail(msg != NULL); + if (settings_get_bool("emphasis")) + msg = freemsg = expand_emphasis(msg); + else + freemsg = NULL; + sender = g_strconcat("=", dcc->nick, NULL); printformat(NULL, sender, MSGLEVEL_DCCMSGS, query_find(NULL, sender) ? IRCTXT_DCC_MSG_QUERY : IRCTXT_DCC_MSG, dcc->nick, msg); g_free(sender); + g_free_not_null(freemsg); } static void dcc_request(DCC_REC *dcc)