commit de27856787c352519320f213681df6c680a3d446 from: Omar Polo date: Wed Jul 21 13:43:33 2021 UTC move help to its own file in order to do so, we also have to export some internal variables from ui (the width and height of the window). Not the best possible outcome, but are used only in recompute_help, and can be dropped later. Also, move wrap_page to wrap.c. commit - 7ee780fa9ce0c6d6f26912c134489517d47a26c8 commit + de27856787c352519320f213681df6c680a3d446 blob - 4f505881be945dff15d8145da221318c9cf16798 blob + 9f846385379521570ddf7ba5da8b00282de04d17 --- Makefile.am +++ Makefile.am @@ -15,6 +15,7 @@ telescope_SOURCES = cmd.c \ gemtext.c \ gen-emoji-matcher.sh \ gencmd.awk \ + help.c \ hist.c \ keymap.c \ mime.c \ blob - /dev/null blob + 3678f6a8d826dcfd4cca64216876d8285deb85ca (mode 644) --- /dev/null +++ help.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2021 Omar Polo + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "compat.h" + +#include +#include +#include +#include + +#include "telescope.h" +#include "ui.h" + +static void +emit_help_item(char *prfx, void *fn) +{ + struct line *l; + struct cmd *cmd; + + for (cmd = cmds; cmd->cmd != NULL; ++cmd) { + if (fn == cmd->fn) + break; + } + assert(cmd != NULL); + + if ((l = calloc(1, sizeof(*l))) == NULL) + abort(); + + l->type = LINE_TEXT; + l->alt = NULL; + + asprintf(&l->line, "%s %s", prfx, cmd->cmd); + + if (TAILQ_EMPTY(&helpwin.page.head)) + TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines); + else + TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines); +} + +static void +rec_compute_help(struct kmap *keymap, char *prfx, size_t len) +{ + struct keymap *k; + char p[32]; + const char *kn; + + TAILQ_FOREACH(k, &keymap->m, keymaps) { + strlcpy(p, prfx, sizeof(p)); + if (*p != '\0') + strlcat(p, " ", sizeof(p)); + if (k->meta) + strlcat(p, "M-", sizeof(p)); + if ((kn = unkbd(k->key)) != NULL) + strlcat(p, kn, sizeof(p)); + else + strlcat(p, ui_keyname(k->key), sizeof(p)); + + if (k->fn == NULL) + rec_compute_help(&k->map, p, sizeof(p)); + else + emit_help_item(p, k->fn); + } +} + +void +recompute_help(void) +{ + char p[32] = { 0 }; + + erase_buffer(&helpwin); + rec_compute_help(current_map, p, sizeof(p)); + wrap_page(&helpwin, help_cols); +} blob - 38aa01a79427614007b2c917e1bb1c9e6497906b blob + f5e9f458306a7c724e0d03e581549a55a1a16801 --- telescope.h +++ telescope.h @@ -294,6 +294,9 @@ int load_certs(struct ohash*); /* gemini.c */ int client_main(void); + +/* help.c */ +void recompute_help(void); /* hist.c */ void hist_clear_forward(struct histhead*, struct hist*); @@ -349,5 +352,6 @@ void empty_vlist(struct buffer*); int wrap_one(struct buffer *, const char *, struct line *, size_t); int wrap_text(struct buffer*, const char*, struct line*, size_t); int hardwrap_text(struct buffer*, struct line*, size_t); +int wrap_page(struct buffer *, int width); #endif /* TELESCOPE_H */ blob - a19c00c0a5a2a2fb9a7165c4304f360df71c55a9 blob + 6ab32c5924d9f7bf150c9b278655300438d409b0 --- ui.c +++ ui.c @@ -56,7 +56,6 @@ static void handle_clear_echoarea(int, short, void*) static void handle_resize(int, short, void*); static void handle_resize_nodelay(int, short, void*); static void rearrange_windows(void); -static int wrap_page(struct buffer*, int); static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **); static void print_vline(int, int, WINDOW*, struct vline*); static void redraw_tabline(void); @@ -70,9 +69,6 @@ static void do_redraw_minibuffer(void); static void do_redraw_minibuffer_compl(void); static void place_cursor(int); static void redraw_tab(struct tab*); -static void emit_help_item(char*, void*); -static void rec_compute_help(struct kmap*, char*, size_t); -static void recompute_help(void); static void update_loading_anim(int, short, void*); static void stop_loading_anim(struct tab*); @@ -96,8 +92,9 @@ static WINDOW *tabline, *body, *modeline, *echoarea, * int body_lines, body_cols; static WINDOW *help; -static struct buffer helpwin; -static int help_lines, help_cols; +/* not static so we can see them from help.c */ +struct buffer helpwin; +int help_lines, help_cols; static int side_window; @@ -366,89 +363,7 @@ rearrange_windows(void) wrap_page(¤t_tab->buffer, body_cols); redraw_tab(current_tab); } - -static int -wrap_page(struct buffer *buffer, int width) -{ - struct line *l; - const struct line *top_orig, *orig; - struct vline *vl; - int pre_width; - const char *prfx; - - top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent; - orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent; - buffer->top_line = NULL; - buffer->current_line = NULL; - - buffer->force_redraw = 1; - buffer->curs_y = 0; - buffer->line_off = 0; - - empty_vlist(buffer); - - TAILQ_FOREACH(l, &buffer->page.head, lines) { - prfx = line_prefixes[l->type].prfx1; - switch (l->type) { - case LINE_TEXT: - case LINE_LINK: - case LINE_TITLE_1: - case LINE_TITLE_2: - case LINE_TITLE_3: - case LINE_ITEM: - case LINE_QUOTE: - case LINE_PRE_START: - case LINE_PRE_END: - wrap_text(buffer, prfx, l, MIN(fill_column, width)); - break; - case LINE_PRE_CONTENT: - if (olivetti_mode) - pre_width = MIN(fill_column, width); - else - pre_width = width; - hardwrap_text(buffer, l, pre_width); - break; - case LINE_COMPL: - case LINE_COMPL_CURRENT: - wrap_one(buffer, prfx, l, width); - break; - } - - if (top_orig == l && buffer->top_line == NULL) { - buffer->line_off = buffer->line_max-1; - buffer->top_line = TAILQ_LAST(&buffer->head, vhead); - - while (1) { - vl = TAILQ_PREV(buffer->top_line, vhead, vlines); - if (vl == NULL || vl->parent != orig) - break; - buffer->top_line = vl; - buffer->line_off--; - } - } - - if (orig == l && buffer->current_line == NULL) { - buffer->current_line = TAILQ_LAST(&buffer->head, vhead); - - while (1) { - vl = TAILQ_PREV(buffer->current_line, vhead, vlines); - if (vl == NULL || vl->parent != orig) - break; - buffer->current_line = vl; - } - } - } - - if (buffer->current_line == NULL) - buffer->current_line = TAILQ_FIRST(&buffer->head); - - if (buffer->top_line == NULL) - buffer->top_line = buffer->current_line; - - return 1; -} - static void line_prefix_and_text(struct vline *vl, char *buf, size_t len, const char **prfx_ret, const char **text_ret) @@ -787,7 +702,7 @@ static inline char trust_status_char(enum trust_state ts) { switch (ts) { - case TS_UNKNOWN: return 'u'; + case TS_UNKNOWN: return '-'; case TS_UNTRUSTED: return '!'; case TS_TEMP_TRUSTED: return '!'; case TS_TRUSTED: return 'v'; @@ -977,69 +892,7 @@ redraw_tab(struct tab *tab) dprintf(1, "\033]0;%s - Telescope\a", current_tab->buffer.page.title); } - -static void -emit_help_item(char *prfx, void *fn) -{ - struct line *l; - struct cmd *cmd; - for (cmd = cmds; cmd->cmd != NULL; ++cmd) { - if (fn == cmd->fn) - break; - } - assert(cmd != NULL); - - if ((l = calloc(1, sizeof(*l))) == NULL) - abort(); - - l->type = LINE_TEXT; - l->alt = NULL; - - asprintf(&l->line, "%s %s", prfx, cmd->cmd); - - if (TAILQ_EMPTY(&helpwin.page.head)) - TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines); - else - TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines); -} - -static void -rec_compute_help(struct kmap *keymap, char *prfx, size_t len) -{ - struct keymap *k; - char p[32]; - const char *kn; - - TAILQ_FOREACH(k, &keymap->m, keymaps) { - strlcpy(p, prfx, sizeof(p)); - if (*p != '\0') - strlcat(p, " ", sizeof(p)); - if (k->meta) - strlcat(p, "M-", sizeof(p)); - if ((kn = unkbd(k->key)) != NULL) - strlcat(p, kn, sizeof(p)); - else - strlcat(p, keyname(k->key), sizeof(p)); - - if (k->fn == NULL) - rec_compute_help(&k->map, p, sizeof(p)); - else - emit_help_item(p, k->fn); - } -} - -static void -recompute_help(void) -{ - char p[32] = { 0 }; - - empty_vlist(&helpwin); - empty_linelist(&helpwin); - rec_compute_help(current_map, p, sizeof(p)); - wrap_page(&helpwin, help_cols); -} - void vmessage(const char *fmt, va_list ap) { blob - 0882a7aac655e9ee23595b1a2ef1cf8db1286f51 blob + d9724d853f21354625b8f284d0a8e1e6d3c9c713 --- ui.h +++ ui.h @@ -100,6 +100,9 @@ extern struct histhead eecmd_history, extern struct tab *current_tab; +extern struct buffer helpwin; +extern int help_lines, help_cols; + void save_excursion(struct excursion *, struct buffer *); void restore_excursion(struct excursion *, struct buffer *); void global_key_unbound(void); blob - 4ec33c07dfb40be69a31c609b11a1aa3866f0476 blob + 8efca8f5c54eefa09a2c2db55e1f3f4acefa85c8 --- wrap.c +++ wrap.c @@ -231,4 +231,86 @@ hardwrap_text(struct buffer *buffer, struct line *l, s } return push_line(buffer, l, start, line - start, cont); +} + +int +wrap_page(struct buffer *buffer, int width) +{ + struct line *l; + const struct line *top_orig, *orig; + struct vline *vl; + int pre_width; + const char *prfx; + + top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent; + orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent; + + buffer->top_line = NULL; + buffer->current_line = NULL; + + buffer->force_redraw = 1; + buffer->curs_y = 0; + buffer->line_off = 0; + + empty_vlist(buffer); + + TAILQ_FOREACH(l, &buffer->page.head, lines) { + prfx = line_prefixes[l->type].prfx1; + switch (l->type) { + case LINE_TEXT: + case LINE_LINK: + case LINE_TITLE_1: + case LINE_TITLE_2: + case LINE_TITLE_3: + case LINE_ITEM: + case LINE_QUOTE: + case LINE_PRE_START: + case LINE_PRE_END: + wrap_text(buffer, prfx, l, MIN(fill_column, width)); + break; + case LINE_PRE_CONTENT: + if (olivetti_mode) + pre_width = MIN(fill_column, width); + else + pre_width = width; + hardwrap_text(buffer, l, pre_width); + break; + case LINE_COMPL: + case LINE_COMPL_CURRENT: + wrap_one(buffer, prfx, l, width); + break; + } + + if (top_orig == l && buffer->top_line == NULL) { + buffer->line_off = buffer->line_max-1; + buffer->top_line = TAILQ_LAST(&buffer->head, vhead); + + while (1) { + vl = TAILQ_PREV(buffer->top_line, vhead, vlines); + if (vl == NULL || vl->parent != orig) + break; + buffer->top_line = vl; + buffer->line_off--; + } + } + + if (orig == l && buffer->current_line == NULL) { + buffer->current_line = TAILQ_LAST(&buffer->head, vhead); + + while (1) { + vl = TAILQ_PREV(buffer->current_line, vhead, vlines); + if (vl == NULL || vl->parent != orig) + break; + buffer->current_line = vl; + } + } + } + + if (buffer->current_line == NULL) + buffer->current_line = TAILQ_FIRST(&buffer->head); + + if (buffer->top_line == NULL) + buffer->top_line = buffer->current_line; + + return 1; }