From 6cc441f34a31b4ad9f0c922abb5348394ca51552 Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 10:07:50 +0200 Subject: [PATCH 01/12] Introduce set_screen_char_color() and reduce code redundancy. --- src/document/html/renderer.c | 41 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index cc8a066f0..2aa668bad 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -137,11 +137,21 @@ void put_chars(struct html_context *, unsigned char *, int); #define ALIGN_SPACES(x, o, n) mem_align_alloc(x, o, n, unsigned char, SPACES_GRANULARITY) +static inline void +set_screen_char_color(struct screen_char *schar, + color_T bgcolor, color_T fgcolor, + enum color_flags color_flags, + enum color_mode color_mode) +{ + struct color_pair colors = INIT_COLOR_PAIR(bgcolor, fgcolor); + + set_term_color(schar, &colors, color_flags, color_mode); +} + static int realloc_line(struct html_context *html_context, struct document *document, int y, int length) { - struct color_pair colors = INIT_COLOR_PAIR(par_format.bgcolor, 0x0); struct screen_char *pos, *end; struct line *line; @@ -162,7 +172,8 @@ realloc_line(struct html_context *html_context, struct document *document, end = &line->chars[length]; end->data = ' '; end->attr = 0; - set_term_color(end, &colors, 0, document->options.color_mode); + set_screen_char_color(end, par_format.bgcolor, 0x0, + 0, document->options.color_mode); for (pos = &line->chars[line->length]; pos < end; pos++) { copy_screen_chars(pos, end, 1); @@ -219,7 +230,6 @@ static inline void clear_hchars(struct html_context *html_context, int x, int y, int width) { struct part *part; - struct color_pair colors = INIT_COLOR_PAIR(par_format.bgcolor, 0x0); struct screen_char *pos, *end; assert(html_context); @@ -240,7 +250,8 @@ clear_hchars(struct html_context *html_context, int x, int y, int width) end = pos + width - 1; end->data = ' '; end->attr = 0; - set_term_color(end, &colors, 0, part->document->options.color_mode); + set_screen_char_color(end, par_format.bgcolor, 0x0, + 0, part->document->options.color_mode); while (pos < end) copy_screen_chars(pos++, end, 1); @@ -254,10 +265,7 @@ get_frame_char(struct html_context *html_context, struct part *part, int x, int y, unsigned char data, color_T bgcolor, color_T fgcolor) { - struct color_pair colors = INIT_COLOR_PAIR(bgcolor, fgcolor); struct screen_char *template; - static enum color_flags color_flags; - static enum color_mode color_mode; assert(html_context); if_assert_failed return NULL; @@ -274,11 +282,9 @@ get_frame_char(struct html_context *html_context, struct part *part, template = &POS(x, y); template->data = data; template->attr = SCREEN_ATTR_FRAME; - - color_mode = part->document->options.color_mode; - color_flags = part->document->options.color_flags; - - set_term_color(template, &colors, color_flags, color_mode); + set_screen_char_color(template, bgcolor, fgcolor, + part->document->options.color_flags, + part->document->options.color_mode); return template; } @@ -356,14 +362,9 @@ get_format_screen_char(struct html_context *html_context, schar_cache.attr |= SCREEN_ATTR_UNDERLINE; } - { - struct color_pair colors = INIT_COLOR_PAIR(format.style.bg, - format.style.fg); - - set_term_color(&schar_cache, &colors, - html_context->options->color_flags, - html_context->options->color_mode); - } + set_screen_char_color(&schar_cache, format.style.bg, format.style.fg, + html_context->options->color_flags, + html_context->options->color_mode); if (html_context->options->display_subs) { if (format.style.attr & AT_SUBSCRIPT) { From 11564e255c8713d274c89719e73409fdc30b1f8d Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 10:09:19 +0200 Subject: [PATCH 02/12] Trim trailing whitespaces. --- src/document/html/renderer.c | 4 ++-- src/terminal/tab.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 2aa668bad..9f8c28534 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -147,7 +147,7 @@ set_screen_char_color(struct screen_char *schar, set_term_color(schar, &colors, color_flags, color_mode); } - + static int realloc_line(struct html_context *html_context, struct document *document, int y, int length) @@ -365,7 +365,7 @@ get_format_screen_char(struct html_context *html_context, set_screen_char_color(&schar_cache, format.style.bg, format.style.fg, html_context->options->color_flags, html_context->options->color_mode); - + if (html_context->options->display_subs) { if (format.style.attr & AT_SUBSCRIPT) { if (format.style.attr & AT_UPDATE_SUB) { diff --git a/src/terminal/tab.c b/src/terminal/tab.c index 8d6c1466f..60382097a 100644 --- a/src/terminal/tab.c +++ b/src/terminal/tab.c @@ -166,7 +166,7 @@ really_close_tab(struct session *ses) switch_to_tab(term, term->current_tab - 1, num_tabs - 1); } - + delete_window(ses->tab); } From f28df73348e08389f8907b03c90d00028c436865 Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 10:21:09 +0200 Subject: [PATCH 03/12] Fix a missing space. --- configure.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.in b/configure.in index c0931fa7e..cabba3dfd 100644 --- a/configure.in +++ b/configure.in @@ -1066,7 +1066,7 @@ EL_ARG_DEPEND(CONFIG_SYSMOUSE, sysmouse, [ --disable-sysmouse disable BSD sysmouse support]) EL_ARG_ENABLE(CONFIG_88_COLORS, 88-colors, [88 colors], - [ --enable-88-colors enable 88 color support]) + [ --enable-88-colors enable 88 color support]) EL_ARG_ENABLE(CONFIG_256_COLORS, 256-colors, [256 colors], [ --enable-256-colors enable 256 color support]) From ee476c6eb6b1323146eda9e9d2d6a253b20a595d Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 11:13:10 +0200 Subject: [PATCH 04/12] shift_chars(): let it use alloca() if possible (--fastmem mode only). --- src/document/html/renderer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 9f8c28534..91587f067 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -615,14 +615,14 @@ shift_chars(struct html_context *html_context, int y, int shift) len = LEN(y); - a = mem_alloc(len * sizeof(*a)); + a = fmem_alloc(len * sizeof(*a)); if (!a) return; copy_screen_chars(a, &POS(0, y), len); clear_hchars(html_context, 0, y, shift); copy_chars(html_context, shift, y, len, a); - mem_free(a); + fmem_free(a); move_links(html_context, 0, y, shift, y); } From ca27359131b0e0c6820ce6bd66d3133523bf61eb Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 11:20:13 +0200 Subject: [PATCH 05/12] Cast void * to struct tag *, instead the opposite. --- src/document/html/renderer.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 91587f067..5b6771eb3 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -1052,7 +1052,7 @@ html_special_tag(struct document *document, unsigned char *t, int x, int y) tag->y = y; memcpy(tag->name, t, tag_len + 1); add_to_list(document->tags, tag); - if ((void *) renderer_context.last_tag_for_newline == &document->tags) + if (renderer_context.last_tag_for_newline == (struct tag *) &document->tags) renderer_context.last_tag_for_newline = tag; } } @@ -1310,7 +1310,7 @@ put_chars(struct html_context *html_context, unsigned char *chars, int charslen) * non-whitespace content. */ if (html_is_preformatted() || html_has_non_space_chars(chars, charslen)) { - renderer_context.last_tag_for_newline = (void *) &part->document->tags; + renderer_context.last_tag_for_newline = (struct tag *) &part->document->tags; } int_lower_bound(&part->box.height, part->cy + 1); @@ -1418,7 +1418,7 @@ line_break(struct html_context *html_context) if (part->cx > 0) align_line(html_context, part->cy, 1); for (tag = renderer_context.last_tag_for_newline; - tag && (void *) tag != &part->document->tags; + tag && tag != (struct tag *) &part->document->tags; tag = tag->prev) { tag->x = X(0); tag->y = Y(part->cy + 1); @@ -1818,12 +1818,12 @@ format_html_part(struct html_context *html_context, } renderer_context.last_link_to_move = document->nlinks; - renderer_context.last_tag_to_move = (void *) &document->tags; - renderer_context.last_tag_for_newline = (void *) &document->tags; + renderer_context.last_tag_to_move = (struct tag *) &document->tags; + renderer_context.last_tag_for_newline = (struct tag *) &document->tags; } else { renderer_context.last_link_to_move = 0; - renderer_context.last_tag_to_move = NULL; - renderer_context.last_tag_for_newline = NULL; + renderer_context.last_tag_to_move = (struct tag *) NULL; + renderer_context.last_tag_for_newline = (struct tag *) NULL; } html_context->margin = margin; From d5e61107113ffa9f65d7cf11288799367268364f Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 11:21:02 +0200 Subject: [PATCH 06/12] html_special_tag(): reduce indentation. --- src/document/html/renderer.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 5b6771eb3..b0e833d39 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -1047,14 +1047,14 @@ html_special_tag(struct document *document, unsigned char *t, int x, int y) tag_len = strlen(t); /* One byte is reserved for name in struct tag. */ tag = mem_alloc(sizeof(*tag) + tag_len); - if (tag) { - tag->x = x; - tag->y = y; - memcpy(tag->name, t, tag_len + 1); - add_to_list(document->tags, tag); - if (renderer_context.last_tag_for_newline == (struct tag *) &document->tags) - renderer_context.last_tag_for_newline = tag; - } + if (!tag) return; + + tag->x = x; + tag->y = y; + memcpy(tag->name, t, tag_len + 1); + add_to_list(document->tags, tag); + if (renderer_context.last_tag_for_newline == (struct tag *) &document->tags) + renderer_context.last_tag_for_newline = tag; } From 23f7fc1b7859efd540ba3ca899501e3567794767 Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 11:43:42 +0200 Subject: [PATCH 07/12] Move code from new_link() to new init_link_event_hooks(). Make code cleaner, and reduce indentation. --- src/document/html/renderer.c | 57 +++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index b0e833d39..9ee6b7764 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -916,6 +916,38 @@ align_line(struct html_context *html_context, int y, int last) shift_chars(html_context, y, shift); } +static inline void +init_link_event_hooks(struct html_context *html_context, struct link *link) +{ + link->event_hooks = mem_calloc(1, sizeof(*link->event_hooks)); + if (!link->event_hooks) return; + +#define add_evhook(list_, type_, src_) \ + do { \ + struct script_event_hook *evhook; \ + \ + if (!src_) break; \ + \ + evhook = mem_calloc(1, sizeof(*evhook)); \ + if (!evhook) break; \ + \ + evhook->type = type_; \ + evhook->src = stracpy(src_); \ + add_to_list(*(list_), evhook); \ + } while (0) + + init_list(*link->event_hooks); + add_evhook(link->event_hooks, SEVHOOK_ONCLICK, format.onclick); + add_evhook(link->event_hooks, SEVHOOK_ONDBLCLICK, format.ondblclick); + add_evhook(link->event_hooks, SEVHOOK_ONMOUSEOVER, format.onmouseover); + add_evhook(link->event_hooks, SEVHOOK_ONHOVER, format.onhover); + add_evhook(link->event_hooks, SEVHOOK_ONFOCUS, format.onfocus); + add_evhook(link->event_hooks, SEVHOOK_ONMOUSEOUT, format.onmouseout); + add_evhook(link->event_hooks, SEVHOOK_ONBLUR, format.onblur); + +#undef add_evhook +} + static struct link * new_link(struct html_context *html_context, unsigned char *name, int namelen) { @@ -1009,28 +1041,7 @@ new_link(struct html_context *html_context, unsigned char *name, int namelen) link->color.foreground = link_is_textinput(link) ? format.style.fg : format.clink; - link->event_hooks = mem_calloc(1, sizeof(*link->event_hooks)); - if (link->event_hooks) { - init_list(*link->event_hooks); -#define add_evhook(list_, type_, src_) \ -do { \ - struct script_event_hook *evhook = mem_calloc(1, sizeof(*evhook)); \ - \ - if (evhook) { \ - evhook->type = type_; \ - evhook->src = stracpy(src_); \ - add_to_list(*list_, evhook); \ - } \ -} while (0) - if (format.onclick) add_evhook(link->event_hooks, SEVHOOK_ONCLICK, format.onclick); - if (format.ondblclick) add_evhook(link->event_hooks, SEVHOOK_ONDBLCLICK, format.ondblclick); - if (format.onmouseover) add_evhook(link->event_hooks, SEVHOOK_ONMOUSEOVER, format.onmouseover); - if (format.onhover) add_evhook(link->event_hooks, SEVHOOK_ONHOVER, format.onhover); - if (format.onfocus) add_evhook(link->event_hooks, SEVHOOK_ONFOCUS, format.onfocus); - if (format.onmouseout) add_evhook(link->event_hooks, SEVHOOK_ONMOUSEOUT, format.onmouseout); - if (format.onblur) add_evhook(link->event_hooks, SEVHOOK_ONBLUR, format.onblur); -#undef add_evhook - } + init_link_event_hooks(html_context, link); return link; } @@ -1048,7 +1059,7 @@ html_special_tag(struct document *document, unsigned char *t, int x, int y) /* One byte is reserved for name in struct tag. */ tag = mem_alloc(sizeof(*tag) + tag_len); if (!tag) return; - + tag->x = x; tag->y = y; memcpy(tag->name, t, tag_len + 1); From c17940044cf91ccb7fa0346f87da08e02c6d0bb1 Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 17:17:25 +0200 Subject: [PATCH 08/12] Move table cache stuff outside local renderer_context. --- src/document/html/renderer.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 9ee6b7764..077598cc2 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -90,11 +90,11 @@ struct table_cache_entry { #define MAX_TABLE_CACHE_ENTRIES 16384 /* Global variables */ +static int table_cache_entries; +static struct hash *table_cache; + struct renderer_context { - int table_cache_entries; - struct hash *table_cache; - int last_link_to_move; struct tag *last_tag_to_move; /* All tags between document->tags and this tag (inclusive) should @@ -1750,20 +1750,20 @@ html_special(struct html_context *html_context, enum html_special_type c, ...) void free_table_cache(void) { - if (renderer_context.table_cache) { + if (table_cache) { struct hash_item *item; int i; /* We do not free key here. */ - foreach_hash_item (item, *renderer_context.table_cache, i) { + foreach_hash_item (item, *table_cache, i) { mem_free_if(item->value); } - free_hash(renderer_context.table_cache); + free_hash(table_cache); } - renderer_context.table_cache = NULL; - renderer_context.table_cache_entries = 0; + table_cache = NULL; + table_cache_entries = 0; } struct part * @@ -1783,8 +1783,8 @@ format_html_part(struct html_context *html_context, struct table_cache_entry *tce; /* Hash creation if needed. */ - if (!renderer_context.table_cache) { - renderer_context.table_cache = init_hash(8, &strhash); + if (!table_cache) { + table_cache = init_hash(8, &strhash); } else if (!document) { /* Search for cached entry. */ struct table_cache_entry_key key; @@ -1802,7 +1802,7 @@ format_html_part(struct html_context *html_context, key.x = x; key.link_num = link_num; - item = get_hash_item(renderer_context.table_cache, + item = get_hash_item(table_cache, (unsigned char *) &key, sizeof(key)); if (item) { /* We found it in cache, so just copy and return. */ @@ -1880,8 +1880,8 @@ ret: renderer_context.empty_format = ef; if (html_context->table_level > 1 && !document - && renderer_context.table_cache - && renderer_context.table_cache_entries < MAX_TABLE_CACHE_ENTRIES) { + && table_cache + && table_cache_entries < MAX_TABLE_CACHE_ENTRIES) { /* Create a new entry. */ /* Clear memory to prevent bad key comparaison due to alignment * of key fields. */ @@ -1899,12 +1899,12 @@ ret: tce->key.link_num = link_num; copy_struct(&tce->part, part); - if (!add_hash_item(renderer_context.table_cache, + if (!add_hash_item(table_cache, (unsigned char *) &tce->key, sizeof(tce->key), tce)) { mem_free(tce); } else { - renderer_context.table_cache_entries++; + table_cache_entries++; } } From e47f8068d9f7fbbd357f27217c4cd3b1a564beb6 Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 17:21:17 +0200 Subject: [PATCH 09/12] Declare variable in inner block. --- src/document/html/renderer.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 077598cc2..400c2ece1 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -1780,8 +1780,7 @@ format_html_part(struct html_context *html_context, /*struct tag *ltn = last_tag_for_newline;*/ int lm = html_context->margin; int ef = renderer_context.empty_format; - struct table_cache_entry *tce; - + /* Hash creation if needed. */ if (!table_cache) { table_cache = init_hash(8, &strhash); @@ -1885,7 +1884,7 @@ ret: /* Create a new entry. */ /* Clear memory to prevent bad key comparaison due to alignment * of key fields. */ - tce = mem_calloc(1, sizeof(*tce)); + struct table_cache_entry *tce = mem_calloc(1, sizeof(*tce)); /* A goto is used here to prevent a test or code * redundancy. */ if (!tce) goto end; From 2f8ed27eab3b27f5b789e8cc394f59180c2913ab Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 17:22:15 +0200 Subject: [PATCH 10/12] No need to initialize @end and @start. --- src/document/html/renderer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 400c2ece1..9594f7325 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -1918,8 +1918,8 @@ render_html_document(struct cache_entry *cached, struct document *document, { struct html_context *html_context; struct part *part; - unsigned char *start = NULL; - unsigned char *end = NULL; + unsigned char *start; + unsigned char *end; struct string title; struct string head; int i; From d54f76bec87ba1c6b9c60dc27525275c4aaebe83 Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 17:23:18 +0200 Subject: [PATCH 11/12] Re-order code in a more logical fashion. --- src/document/html/renderer.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 9594f7325..71c427d4a 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -1929,20 +1929,19 @@ render_html_document(struct cache_entry *cached, struct document *document, if (!init_string(&head)) return; - renderer_context.g_ctrl_num = 0; - renderer_context.cached = cached; + if (cached->head) add_to_string(&head, cached->head); start = buffer->source; end = buffer->source + buffer->length; - if (cached->head) add_to_string(&head, cached->head); - html_context = init_html_parser(cached->uri, &document->options, start, end, &head, &title, put_chars_conv, line_break, html_special); if (!html_context) return; + renderer_context.g_ctrl_num = 0; + renderer_context.cached = cached; renderer_context.convert_table = get_convert_table(head.source, document->options.cp, document->options.assume_cp, From 956484cf572347b7c693fce307e57d250a774309 Mon Sep 17 00:00:00 2001 From: Laurent MONIN Date: Thu, 20 Oct 2005 17:26:22 +0200 Subject: [PATCH 12/12] Drop dead commented code and tidy up. --- src/document/html/renderer.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 71c427d4a..2bab69b37 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -1777,10 +1777,9 @@ format_html_part(struct html_context *html_context, struct html_element *html_state; int llm = renderer_context.last_link_to_move; struct tag *ltm = renderer_context.last_tag_to_move; - /*struct tag *ltn = last_tag_for_newline;*/ - int lm = html_context->margin; int ef = renderer_context.empty_format; - + int lm = html_context->margin; + /* Hash creation if needed. */ if (!table_cache) { table_cache = init_hash(8, &strhash); @@ -1874,10 +1873,10 @@ format_html_part(struct html_context *html_context, ret: renderer_context.last_link_to_move = llm; renderer_context.last_tag_to_move = ltm; - /* renderer_context.last_tag_for_newline = ltn; */ - html_context->margin = lm; renderer_context.empty_format = ef; + html_context->margin = lm; + if (html_context->table_level > 1 && !document && table_cache && table_cache_entries < MAX_TABLE_CACHE_ENTRIES) {