1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

Merge pull request #13 from ailin-nemui/hilight

Add code to make hilight work with textbuffer changes
This commit is contained in:
ailin-nemui 2021-08-13 17:37:52 +02:00 committed by GitHub
commit 85f0a4c1f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 187 additions and 41 deletions

View File

@ -6,7 +6,7 @@
#define IRSSI_GLOBAL_CONFIG "irssi.conf" /* config file name in /etc/ */
#define IRSSI_HOME_CONFIG "config" /* config file name in ~/.irssi/ */
#define IRSSI_ABI_VERSION 36
#define IRSSI_ABI_VERSION 37
#define DEFAULT_SERVER_ADD_PORT 6667
#define DEFAULT_SERVER_ADD_TLS_PORT 6697

View File

@ -369,7 +369,7 @@ void server_connect_init(SERVER_REC *server)
server->type = module_get_uniq_id("SERVER", 0);
server_ref(server);
server->current_incoming_meta =
g_hash_table_new_full(g_str_hash, (GEqualFunc) g_strcmp0,
g_hash_table_new_full(g_str_hash, (GEqualFunc) g_str_equal,
(GDestroyNotify) i_refstr_release, (GDestroyNotify) g_free);
server->nick = g_strdup(server->connrec->nick);

View File

@ -34,6 +34,7 @@
#include <irssi/src/core/recode.h>
#include <irssi/src/core/utf8.h>
#include <irssi/src/core/misc.h>
#include <irssi/src/core/refstrings.h>
static const char *format_backs = "04261537";
static const char *format_fores = "kbgcrmyw";
@ -46,6 +47,8 @@ static int hide_text_style, hide_server_tags, hide_colors;
static int timestamp_level;
static int timestamp_timeout;
static GHashTable *global_meta;
int format_find_tag(const char *module, const char *tag)
{
FORMAT_REC *formats;
@ -453,6 +456,27 @@ void format_read_arglist(va_list va, FORMAT_REC *format,
}
}
void format_dest_meta_stash(TEXT_DEST_REC *dest, const char *meta_key, const char *meta_value)
{
g_hash_table_replace(dest->meta, i_refstr_intern(meta_key), g_strdup(meta_value));
}
const char *format_dest_meta_stash_find(TEXT_DEST_REC *dest, const char *meta_key)
{
return g_hash_table_lookup(dest->meta, meta_key);
}
void format_dest_meta_clear_all(TEXT_DEST_REC *dest)
{
g_hash_table_remove_all(dest->meta);
}
static void clear_global_meta(WINDOW_REC *window, TEXT_DEST_REC *dest)
{
if (dest != NULL && dest->meta == global_meta)
g_hash_table_remove_all(global_meta);
}
void format_create_dest_tag_meta(TEXT_DEST_REC *dest, void *server, const char *server_tag,
const char *target, int level, WINDOW_REC *window,
GHashTable *meta)
@ -465,7 +489,7 @@ void format_create_dest_tag_meta(TEXT_DEST_REC *dest, void *server, const char *
dest->level = level;
dest->window = window != NULL ? window :
window_find_closest(server, target, level);
dest->meta = meta;
dest->meta = meta != NULL ? meta : global_meta;
}
void format_create_dest_tag(TEXT_DEST_REC *dest, void *server, const char *server_tag,
@ -1705,12 +1729,18 @@ static void read_settings(void)
void formats_init(void)
{
signal_gui_print_text = signal_get_uniq_id("gui print text");
global_meta =
g_hash_table_new_full(g_str_hash, (GEqualFunc) g_str_equal,
(GDestroyNotify) i_refstr_release, (GDestroyNotify) g_free);
read_settings();
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
signal_add_last("gui print text finished", (SIGNAL_FUNC) clear_global_meta);
}
void formats_deinit(void)
{
g_hash_table_destroy(global_meta);
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
signal_remove("gui print text finished", (SIGNAL_FUNC) clear_global_meta);
}

View File

@ -63,10 +63,15 @@ typedef struct _TEXT_DEST_REC {
int hilight_priority;
char *hilight_color;
int flags;
int flags; /* PRINT_FLAG */
GHashTable *meta;
} TEXT_DEST_REC;
typedef struct _LINE_INFO_META_REC {
gint64 server_time;
GHashTable *hash;
} LINE_INFO_META_REC;
#define window_get_theme(window) \
(window != NULL && (window)->theme != NULL ? \
(window)->theme : current_theme)
@ -116,12 +121,16 @@ char *format_get_line_start(THEME_REC *theme, TEXT_DEST_REC *dest, time_t t);
void format_create_dest(TEXT_DEST_REC *dest,
void *server, const char *target,
int level, WINDOW_REC *window);
void format_create_dest_tag(TEXT_DEST_REC *dest, void *server,
const char *server_tag, const char *target,
int level, WINDOW_REC *window);
void format_create_dest_tag(TEXT_DEST_REC *dest, void *server, const char *server_tag,
const char *target, int level, WINDOW_REC *window);
void format_newline(TEXT_DEST_REC *dest);
/* manipulate the meta table of a dest */
void format_dest_meta_stash(TEXT_DEST_REC *dest, const char *meta_key, const char *meta_value);
const char *format_dest_meta_stash_find(TEXT_DEST_REC *dest, const char *meta_key);
void format_dest_meta_clear_all(TEXT_DEST_REC *dest);
/* Return how many characters in `str' must be skipped before `len'
characters of text is skipped. */
int strip_real_length(const char *str, int len,

View File

@ -323,6 +323,71 @@ void hilight_update_text_dest(TEXT_DEST_REC *dest, HILIGHT_REC *rec)
static void hilight_print(int index, HILIGHT_REC *rec);
static void sig_render_line_text(TEXT_DEST_REC *dest, GString *str, LINE_INFO_META_REC *meta)
{
char *color, *tmp, *tmp2;
if (meta == NULL || meta->hash == NULL)
return;
color = g_hash_table_lookup(meta->hash, "hilight-color");
if ((tmp = g_hash_table_lookup(meta->hash, "hilight-line")) != NULL) {
/* hilight whole line */
tmp = strip_codes(str->str);
color = format_string_expand(
color != NULL ? color : settings_get_str("hilight_color"), NULL);
g_string_truncate(str, 0);
g_string_append(str, color);
g_string_append(str, tmp);
g_free(color);
g_free(tmp);
} else if ((tmp = g_hash_table_lookup(meta->hash, "hilight-start")) != NULL &&
(tmp2 = g_hash_table_lookup(meta->hash, "hilight-end")) != NULL) {
/* hilight part of the line */
int hilight_start, hilight_end;
int pos, color_pos, color_len;
char *middle;
GString *str2;
hilight_start = atoi(tmp);
hilight_end = atoi(tmp2);
/* start of the line */
pos = strip_real_length(str->str, hilight_start, NULL, NULL);
str2 = g_string_new_len(str->str, pos);
/* color */
color = format_string_expand(
color != NULL ? color : settings_get_str("hilight_color"), NULL);
g_string_append(str2, color);
g_free(color);
/* middle of the line, stripped */
middle = strip_codes(str->str + pos);
g_string_append_len(str2, middle, hilight_end - hilight_start);
g_free(middle);
/* end of the line */
pos = strip_real_length(str->str, hilight_end, &color_pos, &color_len);
if (color_pos > 0) {
g_string_append_len(str2, str->str + color_pos, color_len);
} else {
/* no colors in line, change back to default */
g_string_append_c(str2, 4);
g_string_append_c(str2, FORMAT_STYLE_DEFAULTS);
}
g_string_append(str2, str->str + pos);
g_string_assign(str, g_string_free(str2, FALSE));
}
}
static void sig_print_text(TEXT_DEST_REC *dest, const char *text,
const char *stripped)
{
@ -372,38 +437,47 @@ static void sig_print_text(TEXT_DEST_REC *dest, const char *text,
char *tmp = strip_codes(text);
newstr = g_strconcat(color, tmp, NULL);
g_free(tmp);
format_dest_meta_stash(dest, "hilight-line", "\001");
} else {
/* hilight part of the line */
GString *tmp;
char *middle;
GString *str;
char *middle, *tmp;
int pos, color_pos, color_len;
/* start of the line */
pos = strip_real_length(text, hilight_start, NULL, NULL);
tmp = g_string_new_len(text, pos);
str = g_string_new_len(text, pos);
/* color */
g_string_append(tmp, color);
g_string_append(str, color);
/* middle of the line, stripped */
middle = strip_codes(text + pos);
g_string_append_len(tmp, middle, hilight_len);
g_string_append_len(str, middle, hilight_len);
g_free(middle);
/* end of the line */
pos = strip_real_length(text, hilight_end,
&color_pos, &color_len);
if (color_pos > 0) {
g_string_append_len(tmp, text + color_pos, color_len);
g_string_append_len(str, text + color_pos, color_len);
} else {
/* no colors in line, change back to default */
g_string_append_c(tmp, 4);
g_string_append_c(tmp, FORMAT_STYLE_DEFAULTS);
g_string_append_c(str, 4);
g_string_append_c(str, FORMAT_STYLE_DEFAULTS);
}
g_string_append(tmp, text + pos);
g_string_append(str, text + pos);
newstr = tmp->str;
g_string_free(tmp, FALSE);
newstr = str->str;
g_string_free(str, FALSE);
format_dest_meta_stash(dest, "hilight-start",
tmp = g_strdup_printf("%d", hilight_start));
g_free(tmp);
format_dest_meta_stash(dest, "hilight-end",
tmp = g_strdup_printf("%d", hilight_end));
g_free(tmp);
}
signal_emit("print text", 3, dest, newstr, stripped);
@ -721,6 +795,7 @@ void hilight_text_init(void)
read_hilight_config();
signal_add_first("print text", (SIGNAL_FUNC) sig_print_text);
signal_add("gui render line text", (SIGNAL_FUNC) sig_render_line_text);
signal_add("setup reread", (SIGNAL_FUNC) read_hilight_config);
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
@ -735,6 +810,7 @@ void hilight_text_deinit(void)
nickmatch_deinit(nickmatch);
signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
signal_remove("gui render line text", (SIGNAL_FUNC) sig_render_line_text);
signal_remove("setup reread", (SIGNAL_FUNC) read_hilight_config);
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);

View File

@ -16,6 +16,7 @@
TEXT_BUFFER_REC *color_buf;
gboolean scrollback_format;
gboolean show_server_time;
int signal_gui_render_line_text;
#if GLIB_CHECK_VERSION(2, 56, 0)
/* nothing */
@ -102,7 +103,7 @@ static void format_rec_set_dest(TEXT_BUFFER_FORMAT_REC *rec, const TEXT_DEST_REC
rec->flags = dest->flags & ~PRINT_FLAG_FORMAT;
}
void textbuffer_meta_rec_free(TEXT_BUFFER_META_REC *rec)
void textbuffer_meta_rec_free(LINE_INFO_META_REC *rec)
{
if (rec == NULL)
return;
@ -113,18 +114,18 @@ void textbuffer_meta_rec_free(TEXT_BUFFER_META_REC *rec)
g_free(rec);
}
static void meta_hash_create(struct _TEXT_BUFFER_META_REC *meta)
static void meta_hash_create(struct _LINE_INFO_META_REC *meta)
{
if (meta->hash == NULL) {
meta->hash = g_hash_table_new_full(g_str_hash, (GEqualFunc) g_strcmp0,
meta->hash = g_hash_table_new_full(g_str_hash, (GEqualFunc) g_str_equal,
(GDestroyNotify) i_refstr_release,
(GDestroyNotify) g_free);
}
}
static TEXT_BUFFER_META_REC *line_meta_create(GHashTable *meta_hash)
static LINE_INFO_META_REC *line_meta_create(GHashTable *meta_hash)
{
struct _TEXT_BUFFER_META_REC *meta;
struct _LINE_INFO_META_REC *meta;
GHashTableIter iter;
const char *key;
const char *val;
@ -132,7 +133,7 @@ static TEXT_BUFFER_META_REC *line_meta_create(GHashTable *meta_hash)
if (meta_hash == NULL || g_hash_table_size(meta_hash) == 0)
return NULL;
meta = g_new0(struct _TEXT_BUFFER_META_REC, 1);
meta = g_new0(struct _LINE_INFO_META_REC, 1);
g_hash_table_iter_init(&iter, meta_hash);
while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &val)) {
@ -362,8 +363,8 @@ char *textbuffer_line_get_text(TEXT_BUFFER_REC *buffer, LINE_REC *line, gboolean
THEME_REC *theme;
int formatnum;
TEXT_BUFFER_FORMAT_REC *format_rec;
TEXT_BUFFER_META_REC *meta;
char *str;
LINE_INFO_META_REC *meta;
char *tmp2;
curr = line;
line = NULL;
@ -396,6 +397,8 @@ char *textbuffer_line_get_text(TEXT_BUFFER_REC *buffer, LINE_REC *line, gboolean
}
if (text != NULL && *text != '\0') {
GString *str;
reference_time = curr->info.time;
if (show_server_time && meta != NULL && meta->server_time != 0) {
current_time = meta->server_time;
@ -403,18 +406,27 @@ char *textbuffer_line_get_text(TEXT_BUFFER_REC *buffer, LINE_REC *line, gboolean
current_time = curr->info.time;
}
str = g_string_new(text);
signal_emit_id(signal_gui_render_line_text, 3, &dest, str, meta);
if (g_strcmp0(text, str->str) == 0) {
g_string_free(str, TRUE);
} else {
g_free(text);
text = g_string_free(str, FALSE);
}
tmp = format_get_level_tag(theme, &dest);
str = !theme->info_eol ? format_add_linestart(text, tmp) :
tmp2 = !theme->info_eol ? format_add_linestart(text, tmp) :
format_add_lineend(text, tmp);
g_free_not_null(tmp);
g_free_not_null(text);
text = str;
text = tmp2;
tmp = format_get_line_start(theme, &dest, current_time);
str = !theme->info_eol ? format_add_linestart(text, tmp) :
tmp2 = !theme->info_eol ? format_add_linestart(text, tmp) :
format_add_lineend(text, tmp);
g_free_not_null(tmp);
g_free_not_null(text);
text = str;
text = tmp2;
/* str = g_strconcat(text, "\n", NULL); */
/* g_free(text); */
@ -447,6 +459,8 @@ static void read_settings(void)
void textbuffer_formats_init(void)
{
signal_gui_render_line_text = signal_get_uniq_id("gui render line text");
settings_add_bool("lookandfeel", "scrollback_format", TRUE);
settings_add_bool("lookandfeel", "show_server_time", FALSE);

View File

@ -2,11 +2,7 @@
#define IRSSI_FE_TEXT_TEXTBUFFER_FORMATS_H
#include <irssi/src/fe-text/textbuffer.h>
typedef struct _TEXT_BUFFER_META_REC {
gint64 server_time;
GHashTable *hash;
} TEXT_BUFFER_META_REC;
#include <irssi/src/fe-common/core/formats.h>
typedef struct _TEXT_BUFFER_FORMAT_REC {
char *module;
@ -22,7 +18,7 @@ typedef struct _TEXT_BUFFER_FORMAT_REC {
} TEXT_BUFFER_FORMAT_REC;
void textbuffer_format_rec_free(TEXT_BUFFER_FORMAT_REC *rec);
void textbuffer_meta_rec_free(TEXT_BUFFER_META_REC *rec);
void textbuffer_meta_rec_free(LINE_INFO_META_REC *rec);
char *textbuffer_line_get_text(TEXT_BUFFER_REC *buffer, LINE_REC *line, gboolean raw);
void textbuffer_formats_init(void);
void textbuffer_formats_deinit(void);

View File

@ -24,7 +24,7 @@ typedef struct {
int level;
time_t time;
char *text;
struct _TEXT_BUFFER_META_REC *meta;
struct _LINE_INFO_META_REC *meta;
struct _TEXT_BUFFER_FORMAT_REC *format;
} LINE_INFO_REC;

View File

@ -107,7 +107,7 @@ textbuffer_line_get_meta(line)
PREINIT:
HV *hv;
LINE_REC *l;
TEXT_BUFFER_META_REC *m;
LINE_INFO_META_REC *m;
GHashTableIter iter;
char *key;
char *val;
@ -123,6 +123,8 @@ PPCODE:
(void) hv_store(hv, key, strlen(key), new_pv(val), 0);
}
}
if (m->server_time) {
(void) hv_store(hv, "server_time", 11, newSViv(m->server_time), 0);
}
}
XPUSHs(sv_2mortal(newRV_noinc((SV *) hv)));

View File

@ -156,3 +156,22 @@ print(dest, str)
char *str
CODE:
printtext_dest(dest, "%s", str);
#*******************************
MODULE = Irssi::UI::Formats PACKAGE = Irssi::UI::TextDest PREFIX = format_dest_
#*******************************
void
format_dest_meta_stash(dest, meta_key, meta_value)
Irssi::UI::TextDest dest
char *meta_key
char *meta_value
char *
format_dest_meta_stash_find(dest, meta_key)
Irssi::UI::TextDest dest
char *meta_key
CODE:
RETVAL = (char *) format_dest_meta_stash_find(dest, meta_key);
OUTPUT:
RETVAL