From 4206bcc10ef936a34eabb32f94a58a6cdd4ec907 Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 14 Mar 2019 21:41:30 +0000 Subject: [PATCH 01/18] Add format flags for start and end window. --- format.c | 5 +++++ tmux.1 | 2 ++ 2 files changed, 7 insertions(+) diff --git a/format.c b/format.c index 530e2403..17e54996 100644 --- a/format.c +++ b/format.c @@ -1797,6 +1797,11 @@ format_defaults_winlink(struct format_tree *ft, struct winlink *wl) format_add(ft, "window_flags", "%s", window_printable_flags(wl)); format_add(ft, "window_active", "%d", wl == s->curw); + format_add(ft, "window_start_flag", "%d", + !!(wl == RB_MIN(winlinks, &s->windows))); + format_add(ft, "window_end_flag", "%d", + !!(wl == RB_MAX(winlinks, &s->windows))); + format_add(ft, "window_bell_flag", "%d", !!(wl->flags & WINLINK_BELL)); format_add(ft, "window_activity_flag", "%d", diff --git a/tmux.1 b/tmux.1 index 12da91ec..91c60f8e 100644 --- a/tmux.1 +++ b/tmux.1 @@ -3875,6 +3875,7 @@ The following variables are available, where appropriate: .It Li "window_activity_flag" Ta "" Ta "1 if window has activity" .It Li "window_active" Ta "" Ta "1 if window active" .It Li "window_bell_flag" Ta "" Ta "1 if window has bell" +.It Li "window_end_flag" Ta "" Ta "1 if window has the highest index" .It Li "window_flags" Ta "#F" Ta "Window flags" .It Li "window_format" Ta "" Ta "1 if format is for a window (not assuming the current)" .It Li "window_height" Ta "" Ta "Height of window" @@ -3887,6 +3888,7 @@ The following variables are available, where appropriate: .It Li "window_panes" Ta "" Ta "Number of panes in window" .It Li "window_silence_flag" Ta "" Ta "1 if window has silence alert" .It Li "window_stack_index" Ta "" Ta "Index in session most recent stack" +.It Li "window_start_flag" Ta "" Ta "1 if window has the lowest index" .It Li "window_visible_layout" Ta "" Ta "Window layout description, respecting zoomed window panes" .It Li "window_width" Ta "" Ta "Width of window" .It Li "window_zoomed_flag" Ta "" Ta "1 if window is zoomed" From bace79a5715932f093d4c17db5d49af8e6594916 Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 14 Mar 2019 21:46:08 +0000 Subject: [PATCH 02/18] Remove some unnecessary temporary variables and be much less strict about spacing in style_parse. --- style.c | 65 ++++++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/style.c b/style.c index af4affc8..293c70f1 100644 --- a/style.c +++ b/style.c @@ -39,24 +39,24 @@ static struct style style_default = { int style_parse(struct style *sy, const struct grid_cell *base, const char *in) { - struct style saved; - const char delimiters[] = " ,"; - char tmp[32]; - int value, fg, bg, attr, flags; - size_t end; + struct style saved; + const char delimiters[] = " ,"; + char tmp[32]; + int value; + size_t end; if (*in == '\0') return (0); - if (strchr(delimiters, in[strlen(in) - 1]) != NULL) - return (-1); style_copy(&saved, sy); - fg = sy->gc.fg; - bg = sy->gc.bg; - attr = sy->gc.attr; - flags = sy->gc.flags; - do { + while (*in != '\0' && strchr(delimiters, *in) != NULL) { + in++; + end--; + } + if (*in == '\0') + break; + end = strcspn(in, delimiters); if (end > (sizeof tmp) - 1) goto error; @@ -64,45 +64,40 @@ style_parse(struct style *sy, const struct grid_cell *base, const char *in) tmp[end] = '\0'; if (strcasecmp(tmp, "default") == 0) { - fg = base->fg; - bg = base->bg; - attr = base->attr; - flags = base->flags; + sy->gc.fg = base->fg; + sy->gc.bg = base->bg; + sy->gc.attr = base->attr; + sy->gc.flags = base->flags; } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { if ((value = colour_fromstring(tmp + 3)) == -1) goto error; if (*in == 'f' || *in == 'F') { if (value != 8) - fg = value; + sy->gc.fg = value; else - fg = base->fg; + sy->gc.fg = base->fg; } else if (*in == 'b' || *in == 'B') { if (value != 8) - bg = value; + sy->gc.bg = value; else - bg = base->bg; + sy->gc.bg = base->bg; } else goto error; } else if (strcasecmp(tmp, "none") == 0) - attr = 0; + sy->gc.attr = 0; else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) { if ((value = attributes_fromstring(tmp + 2)) == -1) goto error; - attr &= ~value; + sy->gc.attr &= ~value; } else { if ((value = attributes_fromstring(tmp)) == -1) goto error; - attr |= value; + sy->gc.attr |= value; } in += end + strspn(in + end, delimiters); } while (*in != '\0'); - sy->gc.fg = fg; - sy->gc.bg = bg; - sy->gc.attr = attr; - sy->gc.flags = flags; - return (0); error: @@ -122,18 +117,18 @@ style_tostring(struct style *sy) *s = '\0'; if (gc->fg != 8) { - off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", - comma, colour_tostring(gc->fg)); + off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma, + colour_tostring(gc->fg)); comma = ","; } if (gc->bg != 8) { - off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", - comma, colour_tostring(gc->bg)); + off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma, + colour_tostring(gc->bg)); comma = ","; } if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) { - xsnprintf(s + off, sizeof s - off, "%s%s", - comma, attributes_tostring(gc->attr)); + xsnprintf(s + off, sizeof s - off, "%s%s", comma, + attributes_tostring(gc->attr)); comma = ","; } @@ -174,7 +169,7 @@ style_apply_update(struct grid_cell *gc, struct options *oo, const char *name) void style_set(struct style *sy, const struct grid_cell *gc) { - memset(sy, 0, sizeof *sy); + memcpy(sy, &style_default, sizeof *sy); memcpy(&sy->gc, gc, sizeof sy->gc); } From 10d60faba5d06db707a752be47dbb12abd4b8168 Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 14 Mar 2019 23:14:27 +0000 Subject: [PATCH 03/18] Store the time in the format tree rather than passing it around. --- cmd-display-message.c | 2 +- cmd-pipe-pane.c | 2 +- format.c | 11 ++++----- server-client.c | 2 +- status.c | 55 +++++++++++++++++-------------------------- tmux.h | 2 +- 6 files changed, 31 insertions(+), 43 deletions(-) diff --git a/cmd-display-message.c b/cmd-display-message.c index 6dd210ac..c55cf1e2 100644 --- a/cmd-display-message.c +++ b/cmd-display-message.c @@ -86,7 +86,7 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) ft = format_create(item->client, item, FORMAT_NONE, 0); format_defaults(ft, target_c, s, wl, wp); - msg = format_expand_time(ft, template, 0); + msg = format_expand_time(ft, template); if (args_has(self->args, 'p')) cmdq_print(item, "%s", msg); else if (c != NULL) diff --git a/cmd-pipe-pane.c b/cmd-pipe-pane.c index c0d2939f..084eafe5 100644 --- a/cmd-pipe-pane.c +++ b/cmd-pipe-pane.c @@ -110,7 +110,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item) /* Expand the command. */ ft = format_create(item->client, item, FORMAT_NONE, 0); format_defaults(ft, c, s, wl, wp); - cmd = format_expand_time(ft, args->argv[0], 0); + cmd = format_expand_time(ft, args->argv[0]); format_free(ft); /* Fork the child. */ diff --git a/format.c b/format.c index 17e54996..369c0a6d 100644 --- a/format.c +++ b/format.c @@ -121,6 +121,7 @@ struct format_tree { struct client *client; u_int tag; int flags; + time_t time; RB_HEAD(format_entry_tree, format_entry) tree; }; @@ -682,6 +683,7 @@ format_create(struct client *c, struct cmdq_item *item, int tag, int flags) ft->tag = tag; ft->flags = flags; + ft->time = time(NULL); format_add_cb(ft, "host", format_cb_host); format_add_cb(ft, "host_short", format_cb_host_short); @@ -1437,7 +1439,7 @@ done: value = new; } else if (modifiers & FORMAT_EXPANDTIME) { - new = format_expand_time(ft, value, 0); + new = format_expand_time(ft, value); free(value); value = new; } @@ -1483,7 +1485,7 @@ fail: /* Expand keys in a template, passing through strftime first. */ char * -format_expand_time(struct format_tree *ft, const char *fmt, time_t t) +format_expand_time(struct format_tree *ft, const char *fmt) { struct tm *tm; char s[2048]; @@ -1491,10 +1493,7 @@ format_expand_time(struct format_tree *ft, const char *fmt, time_t t) if (fmt == NULL || *fmt == '\0') return (xstrdup("")); - if (t == 0) - t = time(NULL); - tm = localtime(&t); - + tm = localtime(&ft->time); if (strftime(s, sizeof s, fmt, tm) == 0) return (xstrdup("")); diff --git a/server-client.c b/server-client.c index 5bd77c84..ba24034b 100644 --- a/server-client.c +++ b/server-client.c @@ -1537,7 +1537,7 @@ server_client_set_title(struct client *c) ft = format_create(c, NULL, FORMAT_NONE, 0); format_defaults(ft, c, NULL, NULL, NULL); - title = format_expand_time(ft, template, 0); + title = format_expand_time(ft, template); if (c->title == NULL || strcmp(title, c->title) != 0) { free(c->title); c->title = xstrdup(title); diff --git a/status.c b/status.c index 4da72866..bd639d79 100644 --- a/status.c +++ b/status.c @@ -29,14 +29,14 @@ #include "tmux.h" -static char *status_redraw_get_left(struct client *, time_t, - struct grid_cell *, size_t *); -static char *status_redraw_get_right(struct client *, time_t, - struct grid_cell *, size_t *); -static char *status_print(struct client *, struct winlink *, time_t, +static char *status_redraw_get_left(struct client *, struct grid_cell *, + size_t *); +static char *status_redraw_get_right(struct client *, struct grid_cell *, + size_t *); +static char *status_print(struct client *, struct winlink *, struct grid_cell *); -static char *status_replace(struct client *, struct winlink *, const char *, - time_t); +static char *status_replace(struct client *, struct winlink *, + const char *); static void status_message_callback(int, short, void *); static void status_timer_callback(int, short, void *); @@ -232,8 +232,7 @@ status_line_size(struct client *c) /* Retrieve options for left string. */ static char * -status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc, - size_t *size) +status_redraw_get_left(struct client *c, struct grid_cell *gc, size_t *size) { struct session *s = c->session; const char *template; @@ -243,7 +242,7 @@ status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc, style_apply_update(gc, s->options, "status-left-style"); template = options_get_string(s->options, "status-left"); - left = status_replace(c, NULL, template, t); + left = status_replace(c, NULL, template); *size = options_get_number(s->options, "status-left-length"); leftlen = screen_write_cstrlen("%s", left); @@ -254,8 +253,7 @@ status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc, /* Retrieve options for right string. */ static char * -status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc, - size_t *size) +status_redraw_get_right(struct client *c, struct grid_cell *gc, size_t *size) { struct session *s = c->session; const char *template; @@ -265,7 +263,7 @@ status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc, style_apply_update(gc, s->options, "status-right-style"); template = options_get_string(s->options, "status-right"); - right = status_replace(c, NULL, template, t); + right = status_replace(c, NULL, template); *size = options_get_number(s->options, "status-right-length"); rightlen = screen_write_cstrlen("%s", right); @@ -308,7 +306,6 @@ status_redraw(struct client *c) struct screen old_status, window_list; struct grid_cell stdgc, lgc, rgc, gc; struct options *oo; - time_t t; char *left, *right; const char *sep; u_int offset, needed, lines; @@ -330,9 +327,6 @@ status_redraw(struct client *c) left = right = NULL; larrow = rarrow = 0; - /* Store current time. */ - t = time(NULL); - /* Set up default colour. */ style_apply(&stdgc, s->options, "status-style"); @@ -350,9 +344,9 @@ status_redraw(struct client *c) /* Work out left and right strings. */ memcpy(&lgc, &stdgc, sizeof lgc); - left = status_redraw_get_left(c, t, &lgc, &llen); + left = status_redraw_get_left(c, &lgc, &llen); memcpy(&rgc, &stdgc, sizeof rgc); - right = status_redraw_get_right(c, t, &rgc, &rlen); + right = status_redraw_get_right(c, &rgc, &rlen); /* * Figure out how much space we have for the window list. If there @@ -372,7 +366,7 @@ status_redraw(struct client *c) RB_FOREACH(wl, winlinks, &s->windows) { free(wl->status_text); memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell); - wl->status_text = status_print(c, wl, t, &wl->status_cell); + wl->status_text = status_print(c, wl, &wl->status_cell); wl->status_width = screen_write_cstrlen("%s", wl->status_text); if (wl == s->curw) @@ -531,7 +525,7 @@ out: /* Replace special sequences in fmt. */ static char * -status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t) +status_replace(struct client *c, struct winlink *wl, const char *fmt) { struct format_tree *ft; char *expanded; @@ -550,7 +544,7 @@ status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t) ft = format_create(c, NULL, tag, FORMAT_STATUS); format_defaults(ft, c, NULL, wl, NULL); - expanded = format_expand_time(ft, fmt, t); + expanded = format_expand_time(ft, fmt); format_free(ft); return (expanded); @@ -558,8 +552,7 @@ status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t) /* Return winlink status line entry and adjust gc as necessary. */ static char * -status_print(struct client *c, struct winlink *wl, time_t t, - struct grid_cell *gc) +status_print(struct client *c, struct winlink *wl, struct grid_cell *gc) { struct options *oo = wl->window->options; struct session *s = c->session; @@ -580,7 +573,7 @@ status_print(struct client *c, struct winlink *wl, time_t t, else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE)) style_apply_update(gc, oo, "window-status-activity-style"); - text = status_replace(c, wl, fmt, t); + text = status_replace(c, wl, fmt); return (text); } @@ -698,19 +691,17 @@ status_prompt_set(struct client *c, const char *msg, const char *input, prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags) { struct format_tree *ft; - time_t t; char *tmp, *cp; ft = format_create(c, NULL, FORMAT_NONE, 0); format_defaults(ft, c, NULL, NULL, NULL); - t = time(NULL); if (input == NULL) input = ""; if (flags & PROMPT_NOFORMAT) tmp = xstrdup(input); else - tmp = format_expand_time(ft, input, t); + tmp = format_expand_time(ft, input); status_message_clear(c); status_prompt_clear(c); @@ -722,7 +713,7 @@ status_prompt_set(struct client *c, const char *msg, const char *input, screen_init(&c->status.status, c->tty.sx, 1, 0); } - c->prompt_string = format_expand_time(ft, msg, t); + c->prompt_string = format_expand_time(ft, msg); c->prompt_buffer = utf8_fromcstr(tmp); c->prompt_index = utf8_strlen(c->prompt_buffer); @@ -780,17 +771,15 @@ void status_prompt_update(struct client *c, const char *msg, const char *input) { struct format_tree *ft; - time_t t; char *tmp; ft = format_create(c, NULL, FORMAT_NONE, 0); format_defaults(ft, c, NULL, NULL, NULL); - t = time(NULL); - tmp = format_expand_time(ft, input, t); + tmp = format_expand_time(ft, input); free(c->prompt_string); - c->prompt_string = format_expand_time(ft, msg, t); + c->prompt_string = format_expand_time(ft, msg); free(c->prompt_buffer); c->prompt_buffer = utf8_fromcstr(tmp); diff --git a/tmux.h b/tmux.h index 87873bf6..95e59e36 100644 --- a/tmux.h +++ b/tmux.h @@ -1585,7 +1585,7 @@ struct format_tree *format_create(struct client *, struct cmdq_item *, int, void format_free(struct format_tree *); void printflike(3, 4) format_add(struct format_tree *, const char *, const char *, ...); -char *format_expand_time(struct format_tree *, const char *, time_t); +char *format_expand_time(struct format_tree *, const char *); char *format_expand(struct format_tree *, const char *); char *format_single(struct cmdq_item *, const char *, struct client *, struct session *, struct winlink *, From 25e2e227913095c79d3c043da84db254c51f9c8c Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 14 Mar 2019 23:34:41 +0000 Subject: [PATCH 04/18] Add a limit on how far format_expand can recurse. --- format.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/format.c b/format.c index 369c0a6d..48224861 100644 --- a/format.c +++ b/format.c @@ -100,6 +100,9 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2) #define FORMAT_WINDOWS 0x100 #define FORMAT_PANES 0x200 +/* Limit on recursion. */ +#define FORMAT_LOOP_LIMIT 10 + /* Entry in format tree. */ struct format_entry { char *key; @@ -122,6 +125,7 @@ struct format_tree { u_int tag; int flags; time_t time; + u_int loop; RB_HEAD(format_entry_tree, format_entry) tree; }; @@ -1512,6 +1516,10 @@ format_expand(struct format_tree *ft, const char *fmt) if (fmt == NULL) return (xstrdup("")); + if (ft->loop == FORMAT_LOOP_LIMIT) + return (xstrdup("")); + ft->loop++; + len = 64; buf = xmalloc(len); off = 0; @@ -1606,6 +1614,8 @@ format_expand(struct format_tree *ft, const char *fmt) buf[off] = '\0'; log_debug("%s: '%s' -> '%s'", __func__, saved, buf); + + ft->loop--; return (buf); } From 27578815dace7addeaaf15227aa102705046f843 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 15 Mar 2019 10:04:13 +0000 Subject: [PATCH 05/18] Add a -v flag to display-message to show verbose messages as the format is parsed, this gives the user a way to debug problems with formats rather than just being confronted with (for example) a blank status line. --- cmd-display-message.c | 11 ++- format.c | 190 +++++++++++++++++++++++++++++++----------- tmux.1 | 6 +- tmux.h | 1 + 4 files changed, 154 insertions(+), 54 deletions(-) diff --git a/cmd-display-message.c b/cmd-display-message.c index c55cf1e2..07cd12a8 100644 --- a/cmd-display-message.c +++ b/cmd-display-message.c @@ -39,8 +39,8 @@ const struct cmd_entry cmd_display_message_entry = { .name = "display-message", .alias = "display", - .args = { "c:pt:F:", 0, 1 }, - .usage = "[-p] [-c target-client] [-F format] " + .args = { "c:pt:F:v", 0, 1 }, + .usage = "[-pv] [-c target-client] [-F format] " CMD_TARGET_PANE_USAGE " [message]", .target = { 't', CMD_FIND_PANE, 0 }, @@ -60,6 +60,7 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) const char *template; char *msg; struct format_tree *ft; + int flags; if (args_has(args, 'F') && args->argc != 0) { cmdq_error(item, "only one of -F or argument must be given"); @@ -83,7 +84,11 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) target_c = c; else target_c = cmd_find_best_client(s); - ft = format_create(item->client, item, FORMAT_NONE, 0); + if (args_has(self->args, 'v')) + flags = FORMAT_VERBOSE; + else + flags = 0; + ft = format_create(item->client, item, FORMAT_NONE, flags); format_defaults(ft, target_c, s, wl, wp); msg = format_expand_time(ft, template); diff --git a/format.c b/format.c index 48224861..d15faefc 100644 --- a/format.c +++ b/format.c @@ -208,6 +208,36 @@ static const char *format_lower[] = { NULL /* z */ }; +/* Is logging enabled? */ +static inline int +format_logging(struct format_tree *ft) +{ + return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE)); +} + +/* Log a message if verbose. */ +static void printflike(3, 4) +format_log1(struct format_tree *ft, const char *from, const char *fmt, ...) +{ + va_list ap; + char *s; + static const char spaces[] = " "; + + if (!format_logging(ft)) + return; + + va_start(ap, fmt); + vasprintf(&s, fmt, ap); + va_end(ap); + + log_debug("%s: %s", from, s); + if (ft->item != NULL) + cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s); + + free(s); +} +#define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__) + /* Format job update callback. */ static void format_job_update(struct job *job) @@ -1153,6 +1183,7 @@ format_loop_sessions(struct format_tree *ft, const char *fmt) valuelen = 1; RB_FOREACH(s, sessions, &sessions) { + format_log(ft, "session loop: $%u", s->id); expanded = format_single(item, fmt, ft->c, ft->s, NULL, NULL); valuelen += strlen(expanded); @@ -1174,8 +1205,10 @@ format_loop_windows(struct format_tree *ft, const char *fmt) size_t valuelen; struct winlink *wl; - if (ft->s == NULL) + if (ft->s == NULL) { + format_log(ft, "window loop but no session"); return (NULL); + } if (format_choose(ft, fmt, &all, &active, 0) != 0) { all = xstrdup(fmt); @@ -1186,6 +1219,7 @@ format_loop_windows(struct format_tree *ft, const char *fmt) valuelen = 1; RB_FOREACH(wl, winlinks, &ft->s->windows) { + format_log(ft, "window loop: %u @%u", wl->idx, wl->window->id); if (active != NULL && wl == ft->s->curw) use = active; else @@ -1214,8 +1248,10 @@ format_loop_panes(struct format_tree *ft, const char *fmt) size_t valuelen; struct window_pane *wp; - if (ft->w == NULL) + if (ft->w == NULL) { + format_log(ft, "pane loop but no window"); return (NULL); + } if (format_choose(ft, fmt, &all, &active, 0) != 0) { all = xstrdup(fmt); @@ -1226,6 +1262,7 @@ format_loop_panes(struct format_tree *ft, const char *fmt) valuelen = 1; TAILQ_FOREACH(wp, &ft->w->panes, entry) { + format_log(ft, "pane loop: %%%u", wp->id); if (active != NULL && wp == ft->w->active) use = active; else @@ -1254,25 +1291,27 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, const char *errptr, *copy, *cp; char *copy0, *condition, *found, *new; char *value, *left, *right; - char tmp[64]; size_t valuelen; int modifiers = 0, limit = 0; struct format_modifier *list, *fm, *cmp = NULL, *search = NULL; struct format_modifier *sub = NULL; u_int i, count; + int j; /* Make a copy of the key. */ copy = copy0 = xstrndup(key, keylen); - log_debug("%s: format = '%s'", __func__, copy); /* Process modifier list. */ list = format_build_modifiers(ft, ©, &count); for (i = 0; i < count; i++) { - xsnprintf(tmp, sizeof tmp, "%s: modifier %u", __func__, i); - log_debug("%s = %s", tmp, list[i].modifier); - cmd_log_argv(list[i].argc, list[i].argv, tmp); - fm = &list[i]; + if (format_logging(ft)) { + format_log(ft, "modifier %u is %s", i, fm->modifier); + for (j = 0; j < fm->argc; j++) { + format_log(ft, "modifier %u argument %d: %s", i, + j, fm->argv[j]); + } + } if (fm->size == 1) { switch (fm->modifier[0]) { case 'm': @@ -1355,14 +1394,22 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, goto fail; } else if (search != NULL) { /* Search in pane. */ - if (wp == NULL) + if (wp == NULL) { + format_log(ft, "search '%s' but no pane", copy); value = xstrdup("0"); - else + } else { + format_log(ft, "search '%s' pane %%%u", copy, wp->id); xasprintf(&value, "%u", window_pane_search(wp, copy)); + } } else if (cmp != NULL) { /* Comparison of left and right. */ - if (format_choose(ft, copy, &left, &right, 1) != 0) + if (format_choose(ft, copy, &left, &right, 1) != 0) { + format_log(ft, "compare %s syntax error: %s", + cmp->modifier, copy); goto fail; + } + format_log(ft, "compare %s left is: %s", cmp->modifier, left); + format_log(ft, "compare %s right is: %s", cmp->modifier, right); if (strcmp(cmp->modifier, "||") == 0) { if (format_true(left) || format_true(right)) @@ -1397,9 +1444,12 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, } else if (*copy == '?') { /* Conditional: check first and choose second or third. */ cp = format_skip(copy + 1, ","); - if (cp == NULL) + if (cp == NULL) { + format_log(ft, "condition syntax error: %s", copy + 1); goto fail; + } condition = xstrndup(copy + 1, cp - (copy + 1)); + format_log(ft, "condition is: %s", condition); found = format_find(ft, condition, modifiers); if (found == NULL) { @@ -1412,27 +1462,42 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, if (strcmp(found, condition) == 0) { free(found); found = xstrdup(""); + format_log(ft, "condition '%s' found: %s", + condition, found); + } else { + format_log(ft, + "condition '%s' not found; assuming false", + condition); } - } - free(condition); + } else + format_log(ft, "condition '%s' found", condition); if (format_choose(ft, cp + 1, &left, &right, 0) != 0) { + format_log(ft, "condition '%s' syntax error: %s", + condition, cp + 1); free(found); goto fail; } - if (format_true(found)) + if (format_true(found)) { + format_log(ft, "condition '%s' is true", condition); value = format_expand(ft, left); - else + } else { + format_log(ft, "condition '%s' is false", condition); value = format_expand(ft, right); + } free(right); free(left); + free(condition); free(found); } else { /* Neither: look up directly. */ value = format_find(ft, copy, modifiers); - if (value == NULL) + if (value == NULL) { + format_log(ft, "format '%s' not found", copy); value = xstrdup(""); + } else + format_log(ft, "format '%s' found: %s", copy, value); } done: @@ -1451,6 +1516,8 @@ done: /* Perform substitution if any. */ if (sub != NULL) { new = format_substitute(value, sub->argv[0], sub->argv[1]); + format_log(ft, "substituted '%s' to '%s: %s", sub->argv[0], + sub->argv[1], new); free(value); value = new; } @@ -1458,16 +1525,17 @@ done: /* Truncate the value if needed. */ if (limit > 0) { new = utf8_trimcstr(value, limit); + format_log(ft, "applied length limit %d: %s", limit, new); free(value); value = new; } else if (limit < 0) { new = utf8_rtrimcstr(value, -limit); + format_log(ft, "applied length limit %d: %s", limit, new); free(value); value = new; } /* Expand the buffer and copy in the value. */ - log_debug("%s: '%s' -> '%s'", __func__, copy0, value); valuelen = strlen(value); while (*len - *off < valuelen + 1) { *buf = xreallocarray(*buf, 2, *len); @@ -1476,50 +1544,51 @@ done: memcpy(*buf + *off, value, valuelen); *off += valuelen; + format_log(ft, "replaced '%s' with '%s'", copy0, value); free(value); + format_free_modifiers(list, count); free(copy0); return (0); fail: + format_log(ft, "failed %s", copy0); format_free_modifiers(list, count); free(copy0); return (-1); } -/* Expand keys in a template, passing through strftime first. */ -char * -format_expand_time(struct format_tree *ft, const char *fmt) -{ - struct tm *tm; - char s[2048]; - - if (fmt == NULL || *fmt == '\0') - return (xstrdup("")); - - tm = localtime(&ft->time); - if (strftime(s, sizeof s, fmt, tm) == 0) - return (xstrdup("")); - - return (format_expand(ft, s)); -} - /* Expand keys in a template. */ -char * -format_expand(struct format_tree *ft, const char *fmt) +static char * +format_expand1(struct format_tree *ft, const char *fmt, int time) { char *buf, *out, *name; - const char *ptr, *s, *saved = fmt; + const char *ptr, *s; size_t off, len, n, outlen; int ch, brackets; + struct tm *tm; + char expanded[8192]; - if (fmt == NULL) + if (fmt == NULL || *fmt == '\0') return (xstrdup("")); if (ft->loop == FORMAT_LOOP_LIMIT) return (xstrdup("")); ft->loop++; + format_log(ft, "expanding format: %s", fmt); + + if (time) { + tm = localtime(&ft->time); + if (strftime(expanded, sizeof expanded, fmt, tm) == 0) { + format_log(ft, "format is too long"); + return (xstrdup("")); + } + if (format_logging(ft) && strcmp(expanded, fmt) != 0) + format_log(ft, "after time expanded: %s", expanded); + fmt = expanded; + } + len = 64; buf = xmalloc(len); off = 0; @@ -1535,7 +1604,7 @@ format_expand(struct format_tree *ft, const char *fmt) } fmt++; - ch = (u_char) *fmt++; + ch = (u_char)*fmt++; switch (ch) { case '(': brackets = 1; @@ -1549,15 +1618,19 @@ format_expand(struct format_tree *ft, const char *fmt) break; n = ptr - fmt; - if (ft->flags & FORMAT_NOJOBS) - out = xstrdup(""); - else { - name = xstrndup(fmt, n); - out = format_job_get(ft, name); - free(name); - } - outlen = strlen(out); + name = xstrndup(fmt, n); + format_log(ft, "found #(): %s", name); + if (ft->flags & FORMAT_NOJOBS) { + out = xstrdup(""); + format_log(ft, "#() is disabled"); + } else { + out = format_job_get(ft, name); + format_log(ft, "#() result: %s", out); + } + free(name); + + outlen = strlen(out); while (len - off < outlen + 1) { buf = xreallocarray(buf, 2, len); len *= 2; @@ -1575,6 +1648,7 @@ format_expand(struct format_tree *ft, const char *fmt) break; n = ptr - fmt; + format_log(ft, "found #{}: %.*s", (int)n, fmt); if (format_replace(ft, fmt, n, &buf, &len, &off) != 0) break; fmt += n + 1; @@ -1582,6 +1656,7 @@ format_expand(struct format_tree *ft, const char *fmt) case '}': case '#': case ',': + format_log(ft, "found #%c", ch); while (len - off < 2) { buf = xreallocarray(buf, 2, len); len *= 2; @@ -1604,6 +1679,7 @@ format_expand(struct format_tree *ft, const char *fmt) continue; } n = strlen(s); + format_log(ft, "found #%c: %s", ch, s); if (format_replace(ft, s, n, &buf, &len, &off) != 0) break; continue; @@ -1613,12 +1689,26 @@ format_expand(struct format_tree *ft, const char *fmt) } buf[off] = '\0'; - log_debug("%s: '%s' -> '%s'", __func__, saved, buf); - + format_log(ft, "result is: %s", buf); ft->loop--; + return (buf); } +/* Expand keys in a template, passing through strftime first. */ +char * +format_expand_time(struct format_tree *ft, const char *fmt) +{ + return (format_expand1(ft, fmt, 1)); +} + +/* Expand keys in a template. */ +char * +format_expand(struct format_tree *ft, const char *fmt) +{ + return (format_expand1(ft, fmt, 0)); +} + /* Expand a single string. */ char * format_single(struct cmdq_item *item, const char *fmt, struct client *c, diff --git a/tmux.1 b/tmux.1 index 91c60f8e..4a4b18fa 100644 --- a/tmux.1 +++ b/tmux.1 @@ -4174,7 +4174,7 @@ option. This command works only from inside .Nm . .It Xo Ic display-message -.Op Fl p +.Op Fl pv .Op Fl c Ar target-client .Op Fl t Ar target-pane .Op Ar message @@ -4196,6 +4196,10 @@ if .Fl t is given, otherwise the active pane for the session attached to .Ar target-client . +.Pp +With +.Fl v , +verbose logging is printed as the format is parsed. .El .Sh BUFFERS .Nm diff --git a/tmux.h b/tmux.h index 95e59e36..d6264357 100644 --- a/tmux.h +++ b/tmux.h @@ -1575,6 +1575,7 @@ char *paste_make_sample(struct paste_buffer *); #define FORMAT_STATUS 0x1 #define FORMAT_FORCE 0x2 #define FORMAT_NOJOBS 0x4 +#define FORMAT_VERBOSE 0x8 #define FORMAT_NONE 0 #define FORMAT_PANE 0x80000000U #define FORMAT_WINDOW 0x40000000U From 672c49d5129e130c21a019f989594f02af99e967 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 15 Mar 2019 10:07:24 +0000 Subject: [PATCH 06/18] The pane and window loops need to pass the window and pane tags when they build their format. --- format.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/format.c b/format.c index d15faefc..93c11651 100644 --- a/format.c +++ b/format.c @@ -132,7 +132,7 @@ struct format_tree { static int format_entry_cmp(struct format_entry *, struct format_entry *); RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); -/* Format modifiers. */ +/* Format modifier. */ struct format_modifier { char modifier[3]; u_int size; @@ -1174,7 +1174,9 @@ format_substitute(const char *source, const char *from, const char *to) static char * format_loop_sessions(struct format_tree *ft, const char *fmt) { + struct client *c = ft->client; struct cmdq_item *item = ft->item; + struct format_tree *nft; char *expanded, *value; size_t valuelen; struct session *s; @@ -1184,7 +1186,10 @@ format_loop_sessions(struct format_tree *ft, const char *fmt) RB_FOREACH(s, sessions, &sessions) { format_log(ft, "session loop: $%u", s->id); - expanded = format_single(item, fmt, ft->c, ft->s, NULL, NULL); + nft = format_create(c, item, FORMAT_NONE, ft->flags); + format_defaults(nft, ft->c, s, NULL, NULL); + expanded = format_expand(nft, fmt); + format_free(nft); valuelen += strlen(expanded); value = xrealloc(value, valuelen); @@ -1200,10 +1205,13 @@ format_loop_sessions(struct format_tree *ft, const char *fmt) static char * format_loop_windows(struct format_tree *ft, const char *fmt) { + struct client *c = ft->client; struct cmdq_item *item = ft->item; + struct format_tree *nft; char *all, *active, *use, *expanded, *value; size_t valuelen; struct winlink *wl; + struct window *w; if (ft->s == NULL) { format_log(ft, "window loop but no session"); @@ -1219,12 +1227,16 @@ format_loop_windows(struct format_tree *ft, const char *fmt) valuelen = 1; RB_FOREACH(wl, winlinks, &ft->s->windows) { - format_log(ft, "window loop: %u @%u", wl->idx, wl->window->id); + w = wl->window; + format_log(ft, "window loop: %u @%u", wl->idx, w->id); if (active != NULL && wl == ft->s->curw) use = active; else use = all; - expanded = format_single(item, use, ft->c, ft->s, wl, NULL); + nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags); + format_defaults(nft, ft->c, ft->s, wl, NULL); + expanded = format_expand(nft, use); + format_free(nft); valuelen += strlen(expanded); value = xrealloc(value, valuelen); @@ -1243,7 +1255,9 @@ format_loop_windows(struct format_tree *ft, const char *fmt) static char * format_loop_panes(struct format_tree *ft, const char *fmt) { + struct client *c = ft->client; struct cmdq_item *item = ft->item; + struct format_tree *nft; char *all, *active, *use, *expanded, *value; size_t valuelen; struct window_pane *wp; @@ -1267,7 +1281,10 @@ format_loop_panes(struct format_tree *ft, const char *fmt) use = active; else use = all; - expanded = format_single(item, use, ft->c, ft->s, ft->wl, wp); + nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags); + format_defaults(nft, ft->c, ft->s, ft->wl, wp); + expanded = format_expand(nft, use); + format_free(nft); valuelen += strlen(expanded); value = xrealloc(value, valuelen); From f6d34f066cd872a981196027477a749f58849a02 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 15 Mar 2019 10:22:57 +0000 Subject: [PATCH 07/18] Only print format logging when the flag is set, even if also sending to log_debug. --- format.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/format.c b/format.c index 93c11651..d1df2b54 100644 --- a/format.c +++ b/format.c @@ -231,7 +231,7 @@ format_log1(struct format_tree *ft, const char *from, const char *fmt, ...) va_end(ap); log_debug("%s: %s", from, s); - if (ft->item != NULL) + if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE)) cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s); free(s); From 2d71bef0ca3d0c31975f70118f632fe30393b059 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 15 Mar 2019 10:48:05 +0000 Subject: [PATCH 08/18] Remove unused member of struct client. --- tmux.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/tmux.h b/tmux.h index d6264357..eb55a588 100644 --- a/tmux.h +++ b/tmux.h @@ -1434,8 +1434,6 @@ struct client { struct session *session; struct session *last_session; - int wlmouse; - int references; void *pan_window; From 85044a634bbcd660ae4b7c9ff4aed7e3891af5d4 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 15 Mar 2019 14:46:58 +0000 Subject: [PATCH 09/18] Move status line free into its own function. --- server-client.c | 8 +------- status.c | 18 +++++++++++++++++- tmux.h | 1 + 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/server-client.c b/server-client.c index ba24034b..b436267d 100644 --- a/server-client.c +++ b/server-client.c @@ -281,13 +281,7 @@ server_client_lost(struct client *c) if (c->stderr_data != c->stdout_data) evbuffer_free(c->stderr_data); - if (event_initialized(&c->status.timer)) - evtimer_del(&c->status.timer); - screen_free(&c->status.status); - if (c->status.old_status != NULL) { - screen_free(c->status.old_status); - free(c->status.old_status); - } + status_free(c); free(c->title); free((void *)c->cwd); diff --git a/status.c b/status.c index bd639d79..65579a26 100644 --- a/status.c +++ b/status.c @@ -296,7 +296,23 @@ status_get_window_at(struct client *c, u_int x) return (NULL); } -/* Draw status for client on the last lines of given context. */ +/* Free status line. */ +void +status_free(struct client *c) +{ + struct status_line *sl = &c->status; + + if (event_initialized(&sl->timer)) + evtimer_del(&sl->timer); + + screen_free(&sl->status); + if (sl->old_status != NULL) { + screen_free(sl->old_status); + free(sl->old_status); + } +} + +/* Draw status line for client. */ int status_redraw(struct client *c) { diff --git a/tmux.h b/tmux.h index eb55a588..26f11e23 100644 --- a/tmux.h +++ b/tmux.h @@ -1970,6 +1970,7 @@ void status_update_saved(struct session *); int status_at_line(struct client *); u_int status_line_size(struct client *); struct window *status_get_window_at(struct client *, u_int); +void status_free(struct client *); int status_redraw(struct client *); void printflike(2, 3) status_message_set(struct client *, const char *, ...); void status_message_clear(struct client *); From 33595a255f8af5d368a1897d09cb23171aa302ef Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 15 Mar 2019 15:02:25 +0000 Subject: [PATCH 10/18] Copy recursion counter into new formats when looping. --- format.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/format.c b/format.c index d1df2b54..bcc31d3c 100644 --- a/format.c +++ b/format.c @@ -1187,6 +1187,7 @@ format_loop_sessions(struct format_tree *ft, const char *fmt) RB_FOREACH(s, sessions, &sessions) { format_log(ft, "session loop: $%u", s->id); nft = format_create(c, item, FORMAT_NONE, ft->flags); + nft->loop = ft->loop; format_defaults(nft, ft->c, s, NULL, NULL); expanded = format_expand(nft, fmt); format_free(nft); @@ -1234,6 +1235,7 @@ format_loop_windows(struct format_tree *ft, const char *fmt) else use = all; nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags); + nft->loop = ft->loop; format_defaults(nft, ft->c, ft->s, wl, NULL); expanded = format_expand(nft, use); format_free(nft); @@ -1282,6 +1284,7 @@ format_loop_panes(struct format_tree *ft, const char *fmt) else use = all; nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags); + nft->loop = ft->loop; format_defaults(nft, ft->c, ft->s, ft->wl, wp); expanded = format_expand(nft, use); format_free(nft); From 1d306e926a9f0cc6ab93f30857a8ca6d145ede83 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 15 Mar 2019 15:20:00 +0000 Subject: [PATCH 11/18] Add a : to make error messages clearer. --- cmd-find.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd-find.c b/cmd-find.c index 2711c26b..2fa53b9c 100644 --- a/cmd-find.c +++ b/cmd-find.c @@ -1264,17 +1264,17 @@ found: no_session: if (~flags & CMD_FIND_QUIET) - cmdq_error(item, "can't find session %s", session); + cmdq_error(item, "can't find session: %s", session); goto error; no_window: if (~flags & CMD_FIND_QUIET) - cmdq_error(item, "can't find window %s", window); + cmdq_error(item, "can't find window: %s", window); goto error; no_pane: if (~flags & CMD_FIND_QUIET) - cmdq_error(item, "can't find pane %s", pane); + cmdq_error(item, "can't find pane: %s", pane); goto error; } @@ -1344,7 +1344,7 @@ cmd_find_client(struct cmdq_item *item, const char *target, int quiet) /* If no client found, report an error. */ if (c == NULL && !quiet) - cmdq_error(item, "can't find client %s", copy); + cmdq_error(item, "can't find client: %s", copy); free(copy); log_debug("%s: target %s, return %p", __func__, target, c); From e8b33af780d8b9622de422cbcd4f82a47ccd0c6e Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 15 Mar 2019 21:54:47 +0000 Subject: [PATCH 12/18] Add a way to set individual defaults for an array option. --- options.c | 13 +++++++++---- tmux.h | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/options.c b/options.c index 08d9275f..9ea5c118 100644 --- a/options.c +++ b/options.c @@ -169,12 +169,17 @@ options_empty(struct options *oo, const struct options_table_entry *oe) struct options_entry * options_default(struct options *oo, const struct options_table_entry *oe) { - struct options_entry *o; + struct options_entry *o; + u_int i; o = options_empty(oo, oe); - if (oe->type == OPTIONS_TABLE_ARRAY) - options_array_assign(o, oe->default_str); - else if (oe->type == OPTIONS_TABLE_STRING) + if (oe->type == OPTIONS_TABLE_ARRAY) { + if (oe->default_arr != NULL) { + for (i = 0; oe->default_arr[i] != NULL; i++) + options_array_set(o, i, oe->default_arr[i], 0); + } else + options_array_assign(o, oe->default_str); + } else if (oe->type == OPTIONS_TABLE_STRING) o->string = xstrdup(oe->default_str); else if (oe->type == OPTIONS_TABLE_STYLE) { style_set(&o->style, &grid_default_cell); diff --git a/tmux.h b/tmux.h index 26f11e23..4b16eb83 100644 --- a/tmux.h +++ b/tmux.h @@ -1497,6 +1497,7 @@ struct options_table_entry { const char *default_str; long long default_num; + const char **default_arr; const char *separator; const char *style; From b4f5b99e4b3c6b266e53c6c4ff748320a197011d Mon Sep 17 00:00:00 2001 From: nicm Date: Sat, 16 Mar 2019 17:14:07 +0000 Subject: [PATCH 13/18] Tidy and rename some bits of status line code. --- cmd-set-option.c | 2 +- resize.c | 2 +- screen-redraw.c | 2 +- server-client.c | 2 +- session.c | 2 +- status.c | 111 +++++++++++++++++++++++++++-------------------- tmux.h | 7 +-- window-client.c | 6 +-- 8 files changed, 77 insertions(+), 57 deletions(-) diff --git a/cmd-set-option.c b/cmd-set-option.c index c4b82004..a55472ca 100644 --- a/cmd-set-option.c +++ b/cmd-set-option.c @@ -264,7 +264,7 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) layout_fix_panes(w); } RB_FOREACH(s, sessions, &sessions) - status_update_saved(s); + status_update_cache(s); /* * Update sizes and redraw. May not always be necessary but do it diff --git a/resize.c b/resize.c index 3726298f..868ddac8 100644 --- a/resize.c +++ b/resize.c @@ -162,7 +162,7 @@ recalculate_sizes(void) */ RB_FOREACH(s, sessions, &sessions) { s->attached = 0; - status_update_saved(s); + status_update_cache(s); } /* diff --git a/screen-redraw.c b/screen-redraw.c index 0c537630..e8a62ea5 100644 --- a/screen-redraw.c +++ b/screen-redraw.c @@ -569,7 +569,7 @@ screen_redraw_draw_status(struct screen_redraw_ctx *ctx) struct client *c = ctx->c; struct window *w = c->session->curw->window; struct tty *tty = &c->tty; - struct screen *s = &c->status.status; + struct screen *s = &c->status.screen; u_int i, y; log_debug("%s: %s @%u", __func__, c->name, w->id); diff --git a/server-client.c b/server-client.c index b436267d..fe79163f 100644 --- a/server-client.c +++ b/server-client.c @@ -204,7 +204,7 @@ server_client_create(int fd) c->tty.sx = 80; c->tty.sy = 24; - screen_init(&c->status.status, c->tty.sx, 1, 0); + status_init(c); c->message_string = NULL; TAILQ_INIT(&c->message_log); diff --git a/session.c b/session.c index 061ff499..43ca321a 100644 --- a/session.c +++ b/session.c @@ -136,7 +136,7 @@ session_create(const char *prefix, const char *name, int argc, char **argv, s->options = oo; s->hooks = hooks_create(global_hooks); - status_update_saved(s); + status_update_cache(s); s->tio = NULL; if (tio != NULL) { diff --git a/status.c b/status.c index 65579a26..3065415d 100644 --- a/status.c +++ b/status.c @@ -194,7 +194,7 @@ status_timer_start_all(void) /* Update status cache. */ void -status_update_saved(struct session *s) +status_update_cache(struct session *s) { if (!options_get_number(s->options, "status")) s->statusat = -1; @@ -296,6 +296,15 @@ status_get_window_at(struct client *c, u_int x) return (NULL); } +/* Initialize status line. */ +void +status_init(struct client *c) +{ + struct status_line *sl = &c->status; + + screen_init(&sl->screen, c->tty.sx, 1, 0); +} + /* Free status line. */ void status_free(struct client *c) @@ -305,10 +314,31 @@ status_free(struct client *c) if (event_initialized(&sl->timer)) evtimer_del(&sl->timer); - screen_free(&sl->status); - if (sl->old_status != NULL) { - screen_free(sl->old_status); - free(sl->old_status); + screen_free(&sl->screen); + if (sl->old_screen != NULL) { + screen_free(sl->old_screen); + free(sl->old_screen); + } +} + +/* Save as old status line. */ +static void +status_save_old(struct status_line *sl) +{ + if (sl->old_screen == NULL) { + sl->old_screen = xmalloc(sizeof *sl->old_screen); + memcpy(sl->old_screen, &sl->screen, sizeof *sl->old_screen); + } +} + +/* Free old status line. */ +static void +status_free_old(struct status_line *sl) +{ + if (sl->old_screen != NULL) { + screen_free(sl->old_screen); + free(sl->old_screen); + sl->old_screen = NULL; } } @@ -316,10 +346,11 @@ status_free(struct client *c) int status_redraw(struct client *c) { + struct status_line *sl = &c->status; struct screen_write_ctx ctx; struct session *s = c->session; struct winlink *wl; - struct screen old_status, window_list; + struct screen old_screen, window_list; struct grid_cell stdgc, lgc, rgc, gc; struct options *oo; char *left, *right; @@ -330,11 +361,7 @@ status_redraw(struct client *c) int larrow, rarrow; /* Delete the saved status line, if any. */ - if (c->status.old_status != NULL) { - screen_free(c->status.old_status); - free(c->status.old_status); - c->status.old_status = NULL; - } + status_free_old(sl); /* No status line? */ lines = status_line_size(c); @@ -347,9 +374,9 @@ status_redraw(struct client *c) style_apply(&stdgc, s->options, "status-style"); /* Create the target screen. */ - memcpy(&old_status, &c->status.status, sizeof old_status); - screen_init(&c->status.status, c->tty.sx, lines, 0); - screen_write_start(&ctx, NULL, &c->status.status); + memcpy(&old_screen, &sl->screen, sizeof old_screen); + screen_init(&sl->screen, c->tty.sx, lines, 0); + screen_write_start(&ctx, NULL, &sl->screen); for (offset = 0; offset < lines * c->tty.sx; offset++) screen_write_putc(&ctx, &stdgc, ' '); screen_write_stop(&ctx); @@ -472,7 +499,7 @@ status_redraw(struct client *c) draw: /* Begin drawing. */ - screen_write_start(&ctx, NULL, &c->status.status); + screen_write_start(&ctx, NULL, &sl->screen); /* Draw the left string and arrow. */ screen_write_cursormove(&ctx, 0, 0, 0); @@ -516,14 +543,14 @@ draw: wloffset++; /* Copy the window list. */ - c->status.window_list_offset = -wloffset + wlstart; + sl->window_list_offset = -wloffset + wlstart; screen_write_cursormove(&ctx, wloffset, 0, 0); screen_write_fast_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1); screen_free(&window_list); /* Save left and right size. */ - c->status.left_size = llen; - c->status.right_size = rlen; + sl->left_size = llen; + sl->right_size = rlen; screen_write_stop(&ctx); @@ -531,11 +558,11 @@ out: free(left); free(right); - if (grid_compare(c->status.status.grid, old_status.grid) == 0) { - screen_free(&old_status); + if (grid_compare(sl->screen.grid, old_screen.grid) == 0) { + screen_free(&old_screen); return (0); } - screen_free(&old_status); + screen_free(&old_screen); return (1); } @@ -603,12 +630,8 @@ status_message_set(struct client *c, const char *fmt, ...) status_message_clear(c); - if (c->status.old_status == NULL) { - c->status.old_status = xmalloc(sizeof *c->status.old_status); - memcpy(c->status.old_status, &c->status.status, - sizeof *c->status.old_status); - screen_init(&c->status.status, c->tty.sx, 1, 0); - } + status_save_old(&c->status); + screen_init(&c->status.screen, c->tty.sx, 1, 0); va_start(ap, fmt); xvasprintf(&c->message_string, fmt, ap); @@ -645,7 +668,7 @@ status_message_clear(struct client *c) c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */ - screen_reinit(&c->status.status); + screen_reinit(&c->status.screen); } /* Clear status line message after timer expires. */ @@ -670,14 +693,14 @@ status_message_redraw(struct client *c) if (c->tty.sx == 0 || c->tty.sy == 0) return (0); - memcpy(&old_status, &c->status.status, sizeof old_status); + memcpy(&old_status, &c->status.screen, sizeof old_status); lines = status_line_size(c); if (lines <= 1) { lines = 1; - screen_init(&c->status.status, c->tty.sx, 1, 0); + screen_init(&c->status.screen, c->tty.sx, 1, 0); } else - screen_init(&c->status.status, c->tty.sx, lines, 0); + screen_init(&c->status.screen, c->tty.sx, lines, 0); len = screen_write_strlen("%s", c->message_string); if (len > c->tty.sx) @@ -685,7 +708,7 @@ status_message_redraw(struct client *c) style_apply(&gc, s->options, "message-style"); - screen_write_start(&ctx, NULL, &c->status.status); + screen_write_start(&ctx, NULL, &c->status.screen); screen_write_cursormove(&ctx, 0, 0, 0); for (offset = 0; offset < lines * c->tty.sx; offset++) screen_write_putc(&ctx, &gc, ' '); @@ -693,7 +716,7 @@ status_message_redraw(struct client *c) screen_write_nputs(&ctx, len, &gc, "%s", c->message_string); screen_write_stop(&ctx); - if (grid_compare(c->status.status.grid, old_status.grid) == 0) { + if (grid_compare(c->status.screen.grid, old_status.grid) == 0) { screen_free(&old_status); return (0); } @@ -722,12 +745,8 @@ status_prompt_set(struct client *c, const char *msg, const char *input, status_message_clear(c); status_prompt_clear(c); - if (c->status.old_status == NULL) { - c->status.old_status = xmalloc(sizeof *c->status.old_status); - memcpy(c->status.old_status, &c->status.status, - sizeof *c->status.old_status); - screen_init(&c->status.status, c->tty.sx, 1, 0); - } + status_save_old(&c->status); + screen_init(&c->status.screen, c->tty.sx, 1, 0); c->prompt_string = format_expand_time(ft, msg); @@ -779,7 +798,7 @@ status_prompt_clear(struct client *c) c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */ - screen_reinit(&c->status.status); + screen_reinit(&c->status.screen); } /* Update status line prompt with a new prompt string. */ @@ -822,14 +841,14 @@ status_prompt_redraw(struct client *c) if (c->tty.sx == 0 || c->tty.sy == 0) return (0); - memcpy(&old_status, &c->status.status, sizeof old_status); + memcpy(&old_status, &c->status.screen, sizeof old_status); lines = status_line_size(c); if (lines <= 1) { lines = 1; - screen_init(&c->status.status, c->tty.sx, 1, 0); + screen_init(&c->status.screen, c->tty.sx, 1, 0); } else - screen_init(&c->status.status, c->tty.sx, lines, 0); + screen_init(&c->status.screen, c->tty.sx, lines, 0); if (c->prompt_mode == PROMPT_COMMAND) style_apply(&gc, s->options, "message-command-style"); @@ -843,7 +862,7 @@ status_prompt_redraw(struct client *c) if (start > c->tty.sx) start = c->tty.sx; - screen_write_start(&ctx, NULL, &c->status.status); + screen_write_start(&ctx, NULL, &c->status.screen); screen_write_cursormove(&ctx, 0, 0, 0); for (offset = 0; offset < lines * c->tty.sx; offset++) screen_write_putc(&ctx, &gc, ' '); @@ -889,14 +908,14 @@ status_prompt_redraw(struct client *c) screen_write_cell(&ctx, &cursorgc); } } - if (c->status.status.cx < screen_size_x(&c->status.status) && + if (c->status.screen.cx < screen_size_x(&c->status.screen) && c->prompt_index >= i) screen_write_putc(&ctx, &cursorgc, ' '); finished: screen_write_stop(&ctx); - if (grid_compare(c->status.status.grid, old_status.grid) == 0) { + if (grid_compare(c->status.screen.grid, old_status.grid) == 0) { screen_free(&old_status); return (0); } diff --git a/tmux.h b/tmux.h index 4b16eb83..3b0f7d5b 100644 --- a/tmux.h +++ b/tmux.h @@ -1314,8 +1314,8 @@ struct cmd_entry { struct status_line { struct event timer; - struct screen status; - struct screen *old_status; + struct screen screen; + struct screen *old_screen; int window_list_offset; @@ -1967,10 +1967,11 @@ void server_unzoom_window(struct window *); /* status.c */ void status_timer_start(struct client *); void status_timer_start_all(void); -void status_update_saved(struct session *); +void status_update_cache(struct session *); int status_at_line(struct client *); u_int status_line_size(struct client *); struct window *status_get_window_at(struct client *, u_int); +void status_init(struct client *); void status_free(struct client *); int status_redraw(struct client *); void printflike(2, 3) status_message_set(struct client *, const char *, ...); diff --git a/window-client.c b/window-client.c index f86d8676..2e977184 100644 --- a/window-client.c +++ b/window-client.c @@ -229,10 +229,10 @@ window_client_draw(__unused void *modedata, void *itemdata, screen_write_hline(ctx, sx, 0, 0); screen_write_cursormove(ctx, cx, cy + sy - 1, 0); - if (c->status.old_status != NULL) - screen_write_fast_copy(ctx, c->status.old_status, 0, 0, sx, 1); + if (c->status.old_screen != NULL) + screen_write_fast_copy(ctx, c->status.old_screen, 0, 0, sx, 1); else - screen_write_fast_copy(ctx, &c->status.status, 0, 0, sx, 1); + screen_write_fast_copy(ctx, &c->status.screen, 0, 0, sx, 1); } static struct screen * From 818fda03638e5001209886ddcd12b401c23dffae Mon Sep 17 00:00:00 2001 From: nicm Date: Sat, 16 Mar 2019 17:53:55 +0000 Subject: [PATCH 14/18] Give status_save_old the client so it can do the reinit too. --- status.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/status.c b/status.c index 3065415d..fc9fbe3f 100644 --- a/status.c +++ b/status.c @@ -323,18 +323,23 @@ status_free(struct client *c) /* Save as old status line. */ static void -status_save_old(struct status_line *sl) +status_save_old(struct client *c) { + struct status_line *sl = &c->status; + if (sl->old_screen == NULL) { sl->old_screen = xmalloc(sizeof *sl->old_screen); memcpy(sl->old_screen, &sl->screen, sizeof *sl->old_screen); + screen_init(&c->status.screen, c->tty.sx, 1, 0); } } /* Free old status line. */ static void -status_free_old(struct status_line *sl) +status_free_old(struct client *c) { + struct status_line *sl = &c->status; + if (sl->old_screen != NULL) { screen_free(sl->old_screen); free(sl->old_screen); @@ -361,7 +366,7 @@ status_redraw(struct client *c) int larrow, rarrow; /* Delete the saved status line, if any. */ - status_free_old(sl); + status_free_old(c); /* No status line? */ lines = status_line_size(c); @@ -629,9 +634,7 @@ status_message_set(struct client *c, const char *fmt, ...) int delay; status_message_clear(c); - - status_save_old(&c->status); - screen_init(&c->status.screen, c->tty.sx, 1, 0); + status_save_old(c); va_start(ap, fmt); xvasprintf(&c->message_string, fmt, ap); @@ -744,9 +747,7 @@ status_prompt_set(struct client *c, const char *msg, const char *input, status_message_clear(c); status_prompt_clear(c); - - status_save_old(&c->status); - screen_init(&c->status.screen, c->tty.sx, 1, 0); + status_save_old(c); c->prompt_string = format_expand_time(ft, msg); From b588b1729ad9668a60d402d549506e56d72f1919 Mon Sep 17 00:00:00 2001 From: nicm Date: Sat, 16 Mar 2019 19:12:13 +0000 Subject: [PATCH 15/18] Use a pointer for the active screen in the status line instead of copying them around all the time. --- screen-redraw.c | 2 +- status.c | 139 ++++++++++++++++++++++++------------------------ tmux.h | 3 +- window-client.c | 5 +- 4 files changed, 73 insertions(+), 76 deletions(-) diff --git a/screen-redraw.c b/screen-redraw.c index e8a62ea5..9880433d 100644 --- a/screen-redraw.c +++ b/screen-redraw.c @@ -569,7 +569,7 @@ screen_redraw_draw_status(struct screen_redraw_ctx *ctx) struct client *c = ctx->c; struct window *w = c->session->curw->window; struct tty *tty = &c->tty; - struct screen *s = &c->status.screen; + struct screen *s = c->status.active; u_int i, y; log_debug("%s: %s @%u", __func__, c->name, w->id); diff --git a/status.c b/status.c index fc9fbe3f..5a6a4466 100644 --- a/status.c +++ b/status.c @@ -296,6 +296,32 @@ status_get_window_at(struct client *c, u_int x) return (NULL); } +/* Save old status line. */ +static void +status_push_screen(struct client *c) +{ + struct status_line *sl = &c->status; + + if (sl->active == &sl->screen) { + sl->active = xmalloc(sizeof *sl->active); + screen_init(sl->active, c->tty.sx, status_line_size(c), 0); + } + sl->references++; +} + +/* Restore old status line. */ +static void +status_pop_screen(struct client *c) +{ + struct status_line *sl = &c->status; + + if (--sl->references == 0) { + screen_free(sl->active); + free(sl->active); + sl->active = &sl->screen; + } +} + /* Initialize status line. */ void status_init(struct client *c) @@ -303,6 +329,7 @@ status_init(struct client *c) struct status_line *sl = &c->status; screen_init(&sl->screen, c->tty.sx, 1, 0); + sl->active = &sl->screen; } /* Free status line. */ @@ -314,37 +341,11 @@ status_free(struct client *c) if (event_initialized(&sl->timer)) evtimer_del(&sl->timer); + if (sl->active != &sl->screen) { + screen_free(sl->active); + free(sl->active); + } screen_free(&sl->screen); - if (sl->old_screen != NULL) { - screen_free(sl->old_screen); - free(sl->old_screen); - } -} - -/* Save as old status line. */ -static void -status_save_old(struct client *c) -{ - struct status_line *sl = &c->status; - - if (sl->old_screen == NULL) { - sl->old_screen = xmalloc(sizeof *sl->old_screen); - memcpy(sl->old_screen, &sl->screen, sizeof *sl->old_screen); - screen_init(&c->status.screen, c->tty.sx, 1, 0); - } -} - -/* Free old status line. */ -static void -status_free_old(struct client *c) -{ - struct status_line *sl = &c->status; - - if (sl->old_screen != NULL) { - screen_free(sl->old_screen); - free(sl->old_screen); - sl->old_screen = NULL; - } } /* Draw status line for client. */ @@ -365,8 +366,9 @@ status_redraw(struct client *c) size_t llen, rlen, seplen; int larrow, rarrow; - /* Delete the saved status line, if any. */ - status_free_old(c); + /* Shouldn't get here if not the active screen. */ + if (sl->active != &sl->screen) + fatalx("not the active screen"); /* No status line? */ lines = status_line_size(c); @@ -379,9 +381,9 @@ status_redraw(struct client *c) style_apply(&stdgc, s->options, "status-style"); /* Create the target screen. */ - memcpy(&old_screen, &sl->screen, sizeof old_screen); - screen_init(&sl->screen, c->tty.sx, lines, 0); - screen_write_start(&ctx, NULL, &sl->screen); + memcpy(&old_screen, sl->active, sizeof old_screen); + screen_init(sl->active, c->tty.sx, lines, 0); + screen_write_start(&ctx, NULL, sl->active); for (offset = 0; offset < lines * c->tty.sx; offset++) screen_write_putc(&ctx, &stdgc, ' '); screen_write_stop(&ctx); @@ -504,7 +506,7 @@ status_redraw(struct client *c) draw: /* Begin drawing. */ - screen_write_start(&ctx, NULL, &sl->screen); + screen_write_start(&ctx, NULL, sl->active); /* Draw the left string and arrow. */ screen_write_cursormove(&ctx, 0, 0, 0); @@ -563,7 +565,7 @@ out: free(left); free(right); - if (grid_compare(sl->screen.grid, old_screen.grid) == 0) { + if (grid_compare(sl->active->grid, old_screen.grid) == 0) { screen_free(&old_screen); return (0); } @@ -634,7 +636,7 @@ status_message_set(struct client *c, const char *fmt, ...) int delay; status_message_clear(c); - status_save_old(c); + status_push_screen(c); va_start(ap, fmt); xvasprintf(&c->message_string, fmt, ap); @@ -671,7 +673,7 @@ status_message_clear(struct client *c) c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */ - screen_reinit(&c->status.screen); + status_pop_screen(c); } /* Clear status line message after timer expires. */ @@ -687,23 +689,22 @@ status_message_callback(__unused int fd, __unused short event, void *data) int status_message_redraw(struct client *c) { - struct screen_write_ctx ctx; - struct session *s = c->session; - struct screen old_status; - size_t len; - struct grid_cell gc; - u_int lines, offset; + struct status_line *sl = &c->status; + struct screen_write_ctx ctx; + struct session *s = c->session; + struct screen old_screen; + size_t len; + u_int lines, offset; + struct grid_cell gc; if (c->tty.sx == 0 || c->tty.sy == 0) return (0); - memcpy(&old_status, &c->status.screen, sizeof old_status); + memcpy(&old_screen, sl->active, sizeof old_screen); lines = status_line_size(c); - if (lines <= 1) { + if (lines <= 1) lines = 1; - screen_init(&c->status.screen, c->tty.sx, 1, 0); - } else - screen_init(&c->status.screen, c->tty.sx, lines, 0); + screen_init(sl->active, c->tty.sx, 1, 0); len = screen_write_strlen("%s", c->message_string); if (len > c->tty.sx) @@ -711,7 +712,7 @@ status_message_redraw(struct client *c) style_apply(&gc, s->options, "message-style"); - screen_write_start(&ctx, NULL, &c->status.screen); + screen_write_start(&ctx, NULL, sl->active); screen_write_cursormove(&ctx, 0, 0, 0); for (offset = 0; offset < lines * c->tty.sx; offset++) screen_write_putc(&ctx, &gc, ' '); @@ -719,11 +720,11 @@ status_message_redraw(struct client *c) screen_write_nputs(&ctx, len, &gc, "%s", c->message_string); screen_write_stop(&ctx); - if (grid_compare(c->status.screen.grid, old_status.grid) == 0) { - screen_free(&old_status); + if (grid_compare(sl->active->grid, old_screen.grid) == 0) { + screen_free(&old_screen); return (0); } - screen_free(&old_status); + screen_free(&old_screen); return (1); } @@ -747,7 +748,7 @@ status_prompt_set(struct client *c, const char *msg, const char *input, status_message_clear(c); status_prompt_clear(c); - status_save_old(c); + status_push_screen(c); c->prompt_string = format_expand_time(ft, msg); @@ -799,7 +800,7 @@ status_prompt_clear(struct client *c) c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */ - screen_reinit(&c->status.screen); + status_pop_screen(c); } /* Update status line prompt with a new prompt string. */ @@ -833,23 +834,22 @@ status_prompt_update(struct client *c, const char *msg, const char *input) int status_prompt_redraw(struct client *c) { + struct status_line *sl = &c->status; struct screen_write_ctx ctx; struct session *s = c->session; - struct screen old_status; - u_int i, offset, left, start, pcursor, pwidth, width; - u_int lines; + struct screen old_screen; + u_int i, lines, offset, left, start, width; + u_int pcursor, pwidth; struct grid_cell gc, cursorgc; if (c->tty.sx == 0 || c->tty.sy == 0) return (0); - memcpy(&old_status, &c->status.screen, sizeof old_status); + memcpy(&old_screen, sl->active, sizeof old_screen); lines = status_line_size(c); - if (lines <= 1) { + if (lines <= 1) lines = 1; - screen_init(&c->status.screen, c->tty.sx, 1, 0); - } else - screen_init(&c->status.screen, c->tty.sx, lines, 0); + screen_init(sl->active, c->tty.sx, lines, 0); if (c->prompt_mode == PROMPT_COMMAND) style_apply(&gc, s->options, "message-command-style"); @@ -863,7 +863,7 @@ status_prompt_redraw(struct client *c) if (start > c->tty.sx) start = c->tty.sx; - screen_write_start(&ctx, NULL, &c->status.screen); + screen_write_start(&ctx, NULL, sl->active); screen_write_cursormove(&ctx, 0, 0, 0); for (offset = 0; offset < lines * c->tty.sx; offset++) screen_write_putc(&ctx, &gc, ' '); @@ -909,18 +909,17 @@ status_prompt_redraw(struct client *c) screen_write_cell(&ctx, &cursorgc); } } - if (c->status.screen.cx < screen_size_x(&c->status.screen) && - c->prompt_index >= i) + if (sl->active->cx < screen_size_x(sl->active) && c->prompt_index >= i) screen_write_putc(&ctx, &cursorgc, ' '); finished: screen_write_stop(&ctx); - if (grid_compare(c->status.screen.grid, old_status.grid) == 0) { - screen_free(&old_status); + if (grid_compare(sl->active->grid, old_screen.grid) == 0) { + screen_free(&old_screen); return (0); } - screen_free(&old_status); + screen_free(&old_screen); return (1); } diff --git a/tmux.h b/tmux.h index 3b0f7d5b..2b083e44 100644 --- a/tmux.h +++ b/tmux.h @@ -1315,7 +1315,8 @@ struct status_line { struct event timer; struct screen screen; - struct screen *old_screen; + struct screen *active; + int references; int window_list_offset; diff --git a/window-client.c b/window-client.c index 2e977184..db1edbb3 100644 --- a/window-client.c +++ b/window-client.c @@ -229,10 +229,7 @@ window_client_draw(__unused void *modedata, void *itemdata, screen_write_hline(ctx, sx, 0, 0); screen_write_cursormove(ctx, cx, cy + sy - 1, 0); - if (c->status.old_screen != NULL) - screen_write_fast_copy(ctx, c->status.old_screen, 0, 0, sx, 1); - else - screen_write_fast_copy(ctx, &c->status.screen, 0, 0, sx, 1); + screen_write_fast_copy(ctx, &c->status.screen, 0, 0, sx, 1); } static struct screen * From bd3332b21137ca7c1bd57b26df9482e72b0e909c Mon Sep 17 00:00:00 2001 From: nicm Date: Sun, 17 Mar 2019 19:33:12 +0000 Subject: [PATCH 16/18] Break description of styles into its own section. --- tmux.1 | 227 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 115 insertions(+), 112 deletions(-) diff --git a/tmux.1 b/tmux.1 index 4a4b18fa..ce036018 100644 --- a/tmux.1 +++ b/tmux.1 @@ -2750,82 +2750,19 @@ The default is to run with .Fl np . .It Ic message-command-style Ar style -Set status line message command style, where -.Ar style -is a comma-separated list of characteristics to be specified. -.Pp -The style format is shared by many options and may be: -.Ql bg=colour -to set the background colour, -.Ql fg=colour -to set the foreground colour, and a list of attributes as specified below. -.Pp -The colour is one of: -.Ic black , -.Ic red , -.Ic green , -.Ic yellow , -.Ic blue , -.Ic magenta , -.Ic cyan , -.Ic white , -aixterm bright variants (if supported: -.Ic brightred , -.Ic brightgreen , -and so on), -.Ic colour0 -to -.Ic colour255 -from the 256-colour set, -.Ic default -for the default colour (inherited from another option in the case of some options, for example -.Ic window-status-style -inherits from -.Ic status-style ) , -.Ic terminal -for the terminal default colour, or a hexadecimal RGB string such as -.Ql #ffffff . -.Pp -The attributes is either -.Ic none -or a comma-delimited list of one or more of: -.Ic bright -(or -.Ic bold ) , -.Ic dim , -.Ic underscore , -.Ic blink , -.Ic reverse , -.Ic hidden , -.Ic italics , -.Ic strikethrough , -.Ic double-underscore -.Ic curly-underscore -.Ic dotted-underscore -or -.Ic dashed-underscore -to turn an attribute on, or an attribute prefixed with -.Ql no -to turn one off. -.Pp -Examples are: -.Bd -literal -offset indent -fg=yellow,bold,underscore,blink -bg=black,fg=default,noreverse -.Ed -.Pp -With the -.Fl a -flag to the -.Ic set-option -command the new style is added otherwise the existing style is replaced. +Set status line message command style. +For how to specify +.Ar style , +see the +.Sx STYLES +section. .It Ic message-style Ar style Set status line message style. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .It Xo Ic mouse .Op Ic on | off .Xc @@ -2932,17 +2869,12 @@ Display (by default the session name) to the left of the status line. .Ar string will be passed through -.Xr strftime 3 -and formats (see -.Sx FORMATS ) -will be expanded. -It may also contain the special character sequence #[] to change the colour -or attributes, for example -.Ql #[fg=red,bright] -to set a bright red foreground. -See the -.Ic message-command-style -option for a description of colours and attributes. +.Xr strftime 3 . +Also see the +.Sx FORMATS +and +.Sx STYLES +sections. .Pp For details on how the names and titles can be set see the .Sx "NAMES AND TITLES" @@ -2966,8 +2898,8 @@ Set the style of the left part of the status line. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .It Xo Ic status-position .Op Ic top | bottom .Xc @@ -2994,15 +2926,15 @@ Set the style of the right part of the status line. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .It Ic status-style Ar style Set status line style. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .It Ic update-environment[] Ar variable Set list of environment variables to be copied into the session environment when a new session is created or an existing session is attached. @@ -3186,8 +3118,8 @@ Set window modes style. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .Pp .It Xo Ic monitor-activity .Op Ic on | off @@ -3235,8 +3167,8 @@ Set the pane border style for the currently active pane. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. Attributes are ignored. .Pp .It Ic pane-base-index Ar index @@ -3257,8 +3189,8 @@ Set the pane border style for panes aside from the active pane. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. Attributes are ignored. .Pp .It Xo Ic remain-on-exit @@ -3281,24 +3213,24 @@ Set the style for the window's active pane. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .Pp .It Ic window-status-activity-style Ar style Set status line style for windows with an activity alert. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .Pp .It Ic window-status-bell-style Ar style Set status line style for windows with a bell alert. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .Pp .It Ic window-status-current-format Ar string Like @@ -3310,24 +3242,24 @@ Set status line style for the currently active window. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .Pp .It Ic window-status-format Ar string Set the format in which the window is displayed in the status line window list. See the -.Ar status-left -option for details of special character sequences available. -The default is -.Ql #I:#W#F . +.Sx FORMATS +and +.Sx STYLES +sections. .Pp .It Ic window-status-last-style Ar style Set status line style for the last active window. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .Pp .It Ic window-status-separator Ar string Sets the separator drawn between windows in the status line. @@ -3338,16 +3270,16 @@ Set status line style for a single window. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .Pp .It Ic window-style Ar style Set the default window style. For how to specify .Ar style , see the -.Ic message-command-style -option. +.Sx STYLES +section. .Pp .It Xo Ic wrap-search .Op Ic on | off @@ -3894,6 +3826,77 @@ The following variables are available, where appropriate: .It Li "window_zoomed_flag" Ta "" Ta "1 if window is zoomed" .It Li "wrap_flag" Ta "" Ta "Pane wrap flag" .El +.Sh STYLES +.Nm +offers various options to specify the colour and attributes of aspects of the +interface, for example +.Ic status-style +for the status line. +In addition, embedded styles may be specified in format options, such as +.Ic status-left-format , +by enclosing them in +.Ql #[ +and +.Ql ] . +.Pp +A style may be the single term +.Ql default +to specify the default style (which may inherit from another option) or a space +separated list of the following: +.Bl -tag -width Ds +.It Ic fg=colour +Set the foreground colour. +The colour is one of: +.Ic black , +.Ic red , +.Ic green , +.Ic yellow , +.Ic blue , +.Ic magenta , +.Ic cyan , +.Ic white ; +if supported the bright variants +.Ic brightred , +.Ic brightgreen , +.Ic brightyellow ; +.Ic colour0 +to +.Ic colour255 +from the 256-colour set; +.Ic default +for the default colour; +.Ic terminal +for the terminal default colour; or a hexadecimal RGB string such as +.Ql #ffffff . +.It Ic bg=colour +Set the background colour. +.It Ic none +Set no attributes (turn off any active attributes). +.It Xo Ic bright (or +.Ic bold ) +.Ic dim , +.Ic underscore , +.Ic blink , +.Ic reverse , +.Ic hidden , +.Ic italics , +.Ic strikethrough , +.Ic double-underscore , +.Ic curly-underscore , +.Ic dotted-underscore , +.Ic dashed-underscore +.Xc +Set an attribute. +Any of the attributes may be prefixed with +.Ql no +to unset. +.El +.Pp +Examples are: +.Bd -literal -offset indent +fg=yellow,bold,underscore,blink +bg=black,fg=default,noreverse +.Ed .Sh NAMES AND TITLES .Nm distinguishes between names and titles. From d2d43987d0f35af2bc012f1260fdb81c851fe390 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 18 Mar 2019 09:46:42 +0000 Subject: [PATCH 17/18] With force, kill previous job before starting new. Fixes problem reported by Scott Mcdermott in GitHub issue 1627. --- format.c | 4 +++- status.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/format.c b/format.c index bcc31d3c..aa0a8aa2 100644 --- a/format.c +++ b/format.c @@ -346,7 +346,9 @@ format_job_get(struct format_tree *ft, const char *cmd) force = (ft->flags & FORMAT_FORCE); t = time(NULL); - if (fj->job == NULL && (force || fj->last != t)) { + if (force && fj->job != NULL) + job_free(fj->job); + if (force || (fj->job == NULL && fj->last != t)) { fj->job = job_run(expanded, NULL, server_client_get_cwd(ft->client, NULL), format_job_update, format_job_complete, NULL, fj, JOB_NOWAIT); diff --git a/status.c b/status.c index 5a6a4466..4a2eb19d 100644 --- a/status.c +++ b/status.c @@ -704,7 +704,7 @@ status_message_redraw(struct client *c) lines = status_line_size(c); if (lines <= 1) lines = 1; - screen_init(sl->active, c->tty.sx, 1, 0); + screen_init(sl->active, c->tty.sx, lines, 0); len = screen_write_strlen("%s", c->message_string); if (len > c->tty.sx) From ce6be7afd4d10b542f9cce8634d6bdd81754f775 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 18 Mar 2019 11:58:40 +0000 Subject: [PATCH 18/18] Make array options a sparse tree instead of an array of char * and remove the size limit. --- cmd-set-option.c | 10 ++- cmd-show-options.c | 32 ++++----- cmd.c | 38 +++++------ environ.c | 20 +++--- options.c | 162 +++++++++++++++++++++++++++++++-------------- status.c | 19 ++++-- tmux.h | 7 +- tty-keys.c | 11 +-- tty-term.c | 15 +++-- 9 files changed, 197 insertions(+), 117 deletions(-) diff --git a/cmd-set-option.c b/cmd-set-option.c index a55472ca..ddbfc334 100644 --- a/cmd-set-option.c +++ b/cmd-set-option.c @@ -163,11 +163,9 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) parent = options_get(oo, name); /* Check that array options and indexes match up. */ - if (idx != -1) { - if (*name == '@' || options_array_size(parent, NULL) == -1) { - cmdq_error(item, "not an array: %s", argument); - goto fail; - } + if (idx != -1 && (*name == '@' || !options_isarray(parent))) { + cmdq_error(item, "not an array: %s", argument); + goto fail; } /* With -o, check this option is not already set. */ @@ -209,7 +207,7 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) goto fail; } options_set_string(oo, name, append, "%s", value); - } else if (idx == -1 && options_array_size(parent, NULL) == -1) { + } else if (idx == -1 && !options_isarray(parent)) { error = cmd_set_option_set(self, item, oo, parent, value); if (error != 0) goto fail; diff --git a/cmd-show-options.c b/cmd-show-options.c index f043beca..872f0c45 100644 --- a/cmd-show-options.c +++ b/cmd-show-options.c @@ -89,20 +89,20 @@ static void cmd_show_options_print(struct cmd *self, struct cmdq_item *item, struct options_entry *o, int idx) { - const char *name; - const char *value; - char *tmp, *escaped; - u_int size, i; + struct options_array_item *a; + const char *name, *value; + char *tmp, *escaped; if (idx != -1) { xasprintf(&tmp, "%s[%d]", options_name(o), idx); name = tmp; } else { - if (options_array_size(o, &size) != -1) { - for (i = 0; i < size; i++) { - if (options_array_get(o, i) == NULL) - continue; - cmd_show_options_print(self, item, o, i); + if (options_isarray(o)) { + a = options_array_first(o); + while (a != NULL) { + idx = options_array_item_index(a); + cmd_show_options_print(self, item, o, idx); + a = options_array_next(a); } return; } @@ -165,9 +165,10 @@ static enum cmd_retval cmd_show_options_all(struct cmd *self, struct cmdq_item *item, struct options *oo) { - struct options_entry *o; + struct options_entry *o; const struct options_table_entry *oe; - u_int size, idx; + struct options_array_item *a; + u_int idx; o = options_first(oo); while (o != NULL) { @@ -176,13 +177,14 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, o = options_next(o); continue; } - if (options_array_size(o, &size) == -1) + if (!options_isarray(o)) cmd_show_options_print(self, item, o, -1); else { - for (idx = 0; idx < size; idx++) { - if (options_array_get(o, idx) == NULL) - continue; + a = options_array_first(o); + while (a != NULL) { + idx = options_array_item_index(a); cmd_show_options_print(self, item, o, idx); + a = options_array_next(a); } } o = options_next(o); diff --git a/cmd.c b/cmd.c index 9226e6c2..eace4bbd 100644 --- a/cmd.c +++ b/cmd.c @@ -319,31 +319,31 @@ cmd_stringify_argv(int argc, char **argv) static int cmd_try_alias(int *argc, char ***argv) { - struct options_entry *o; - int old_argc = *argc, new_argc; - char **old_argv = *argv, **new_argv; - u_int size, idx; - int i; - size_t wanted; - const char *s, *cp = NULL; + struct options_entry *o; + struct options_array_item *a; + int old_argc = *argc, new_argc, i; + char **old_argv = *argv, **new_argv; + size_t wanted; + const char *s, *cp = NULL; o = options_get_only(global_options, "command-alias"); - if (o == NULL || options_array_size(o, &size) == -1 || size == 0) + if (o == NULL) return (-1); - wanted = strlen(old_argv[0]); - for (idx = 0; idx < size; idx++) { - s = options_array_get(o, idx); - if (s == NULL) - continue; - cp = strchr(s, '='); - if (cp == NULL || (size_t)(cp - s) != wanted) - continue; - if (strncmp(old_argv[0], s, wanted) == 0) - break; + a = options_array_first(o); + while (a != NULL) { + s = options_array_item_value(a); + if (s != NULL) { + cp = strchr(s, '='); + if (cp != NULL && + (size_t)(cp - s) == wanted && + strncmp(old_argv[0], s, wanted) == 0) + break; + } + a = options_array_next(a); } - if (idx == size) + if (a == NULL) return (-1); if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0) diff --git a/environ.c b/environ.c index 29191ee5..2764e027 100644 --- a/environ.c +++ b/environ.c @@ -174,22 +174,26 @@ environ_unset(struct environ *env, const char *name) void environ_update(struct options *oo, struct environ *src, struct environ *dst) { - struct environ_entry *envent; - struct options_entry *o; - u_int size, idx; - const char *value; + struct environ_entry *envent; + struct options_entry *o; + struct options_array_item *a; + const char *value; o = options_get(oo, "update-environment"); - if (o == NULL || options_array_size(o, &size) == -1) + if (o == NULL) return; - for (idx = 0; idx < size; idx++) { - value = options_array_get(o, idx); - if (value == NULL) + a = options_array_first(o); + while (a != NULL) { + value = options_array_item_value(a); + if (value == NULL) { + a = options_array_next(a); continue; + } if ((envent = environ_find(src, value)) == NULL) environ_clear(dst, value); else environ_set(dst, envent->name, "%s", envent->value); + a = options_array_next(a); } } diff --git a/options.c b/options.c index 9ea5c118..6a7c37d6 100644 --- a/options.c +++ b/options.c @@ -30,6 +30,23 @@ * a red-black tree. */ +struct options_array_item { + u_int index; + char *value; + RB_ENTRY(options_array_item) entry; +}; +RB_HEAD(options_array, options_array_item); +static int +options_array_cmp(struct options_array_item *a1, struct options_array_item *a2) +{ + if (a1->index < a2->index) + return (-1); + if (a1->index > a2->index) + return (1); + return (0); +} +RB_GENERATE_STATIC(options_array, options_array_item, entry, options_array_cmp); + struct options_entry { struct options *owner; @@ -40,10 +57,7 @@ struct options_entry { char *string; long long number; struct style style; - struct { - const char **array; - u_int arraysize; - }; + struct options_array array; }; RB_ENTRY(options_entry) entry; @@ -56,8 +70,6 @@ struct options { static struct options_entry *options_add(struct options *, const char *); -#define OPTIONS_ARRAY_LIMIT 1000 - #define OPTIONS_IS_STRING(o) \ ((o)->tableentry == NULL || \ (o)->tableentry->type == OPTIONS_TABLE_STRING) @@ -163,6 +175,9 @@ options_empty(struct options *oo, const struct options_table_entry *oe) o = options_add(oo, oe->name); o->tableentry = oe; + if (oe->type == OPTIONS_TABLE_ARRAY) + RB_INIT(&o->array); + return (o); } @@ -210,15 +225,11 @@ void options_remove(struct options_entry *o) { struct options *oo = o->owner; - u_int i; if (OPTIONS_IS_STRING(o)) - free((void *)o->string); - else if (OPTIONS_IS_ARRAY(o)) { - for (i = 0; i < o->arraysize; i++) - free((void *)o->array[i]); - free(o->array); - } + free(o->string); + else if (OPTIONS_IS_ARRAY(o)) + options_array_clear(o); RB_REMOVE(options_tree, &oo->tree, o); free(o); @@ -236,62 +247,79 @@ options_table_entry(struct options_entry *o) return (o->tableentry); } +static struct options_array_item * +options_array_item(struct options_entry *o, u_int idx) +{ + struct options_array_item a; + + a.index = idx; + return (RB_FIND(options_array, &o->array, &a)); +} + +static void +options_array_free(struct options_entry *o, struct options_array_item *a) +{ + free(a->value); + RB_REMOVE(options_array, &o->array, a); + free(a); +} + void options_array_clear(struct options_entry *o) { - if (OPTIONS_IS_ARRAY(o)) - o->arraysize = 0; + struct options_array_item *a, *a1; + + if (!OPTIONS_IS_ARRAY(o)) + return; + + RB_FOREACH_SAFE(a, options_array, &o->array, a1) + options_array_free(o, a); } const char * options_array_get(struct options_entry *o, u_int idx) { + struct options_array_item *a; + if (!OPTIONS_IS_ARRAY(o)) return (NULL); - if (idx >= o->arraysize) + a = options_array_item(o, idx); + if (a == NULL) return (NULL); - return (o->array[idx]); + return (a->value); } int options_array_set(struct options_entry *o, u_int idx, const char *value, int append) { - char *new; - u_int i; + struct options_array_item *a; + char *new; if (!OPTIONS_IS_ARRAY(o)) return (-1); - if (idx >= OPTIONS_ARRAY_LIMIT) - return (-1); - if (idx >= o->arraysize) { - o->array = xreallocarray(o->array, idx + 1, sizeof *o->array); - for (i = o->arraysize; i < idx + 1; i++) - o->array[i] = NULL; - o->arraysize = idx + 1; + a = options_array_item(o, idx); + if (value == NULL) { + if (a != NULL) + options_array_free(o, a); + return (0); } - new = NULL; - if (value != NULL) { - if (o->array[idx] != NULL && append) - xasprintf(&new, "%s%s", o->array[idx], value); + if (a == NULL) { + a = xcalloc(1, sizeof *a); + a->index = idx; + a->value = xstrdup(value); + RB_INSERT(options_array, &o->array, a); + } else { + free(a->value); + if (a != NULL && append) + xasprintf(&new, "%s%s", a->value, value); else new = xstrdup(value); + a->value = new; } - free((void *)o->array[idx]); - o->array[idx] = new; - return (0); -} - -int -options_array_size(struct options_entry *o, u_int *size) -{ - if (!OPTIONS_IS_ARRAY(o)) - return (-1); - if (size != NULL) - *size = o->arraysize; return (0); } @@ -310,37 +338,69 @@ options_array_assign(struct options_entry *o, const char *s) while ((next = strsep(&string, separator)) != NULL) { if (*next == '\0') continue; - for (i = 0; i < OPTIONS_ARRAY_LIMIT; i++) { - if (i >= o->arraysize || o->array[i] == NULL) + for (i = 0; i < UINT_MAX; i++) { + if (options_array_item(o, i) == NULL) break; } - if (i == OPTIONS_ARRAY_LIMIT) + if (i == UINT_MAX) break; options_array_set(o, i, next, 0); } free(copy); } +struct options_array_item * +options_array_first(struct options_entry *o) +{ + if (!OPTIONS_IS_ARRAY(o)) + return (NULL); + return (RB_MIN(options_array, &o->array)); +} + +struct options_array_item * +options_array_next(struct options_array_item *a) +{ + return (RB_NEXT(options_array, &o->array, a)); +} + +u_int +options_array_item_index(struct options_array_item *a) +{ + return (a->index); +} + +const char * +options_array_item_value(struct options_array_item *a) +{ + return (a->value); +} + +int +options_isarray(struct options_entry *o) +{ + return (OPTIONS_IS_ARRAY(o)); +} + int options_isstring(struct options_entry *o) { - if (o->tableentry == NULL) - return (1); return (OPTIONS_IS_STRING(o) || OPTIONS_IS_ARRAY(o)); } const char * options_tostring(struct options_entry *o, int idx, int numeric) { - static char s[1024]; - const char *tmp; + static char s[1024]; + const char *tmp; + struct options_array_item *a; if (OPTIONS_IS_ARRAY(o)) { if (idx == -1) return (NULL); - if ((u_int)idx >= o->arraysize || o->array[idx] == NULL) + a = options_array_item(o, idx); + if (a == NULL) return (""); - return (o->array[idx]); + return (a->value); } if (OPTIONS_IS_STYLE(o)) return (style_tostring(&o->style)); diff --git a/status.c b/status.c index 4a2eb19d..de6d2716 100644 --- a/status.c +++ b/status.c @@ -1510,9 +1510,10 @@ status_prompt_complete_list(u_int *size, const char *s) const char **layout, *value, *cp; const struct cmd_entry **cmdent; const struct options_table_entry *oe; - u_int items, idx; + u_int idx; size_t slen = strlen(s), valuelen; struct options_entry *o; + struct options_array_item *a; const char *layouts[] = { "even-horizontal", "even-vertical", "main-horizontal", "main-vertical", "tiled", NULL @@ -1538,16 +1539,22 @@ status_prompt_complete_list(u_int *size, const char *s) } } o = options_get_only(global_options, "command-alias"); - if (o != NULL && options_array_size(o, &items) != -1) { - for (idx = 0; idx < items; idx++) { - value = options_array_get(o, idx); + if (o != NULL) { + a = options_array_first(o); + while (a != NULL) { + value = options_array_item_value(a);; if (value == NULL || (cp = strchr(value, '=')) == NULL) - continue; + goto next; + valuelen = cp - value; if (slen > valuelen || strncmp(value, s, slen) != 0) - continue; + goto next; + list = xreallocarray(list, (*size) + 1, sizeof *list); list[(*size)++] = xstrndup(value, valuelen); + + next: + a = options_array_next(a); } } for (idx = 0; idx < (*size); idx++) diff --git a/tmux.h b/tmux.h index 2b083e44..a1ed1b46 100644 --- a/tmux.h +++ b/tmux.h @@ -50,6 +50,7 @@ struct mode_tree_data; struct mouse_event; struct options; struct options_entry; +struct options_array_item; struct session; struct tmuxpeer; struct tmuxproc; @@ -1642,8 +1643,12 @@ void options_array_clear(struct options_entry *); const char *options_array_get(struct options_entry *, u_int); int options_array_set(struct options_entry *, u_int, const char *, int); -int options_array_size(struct options_entry *, u_int *); void options_array_assign(struct options_entry *, const char *); +struct options_array_item *options_array_first(struct options_entry *); +struct options_array_item *options_array_next(struct options_array_item *); +u_int options_array_item_index(struct options_array_item *); +const char *options_array_item_value(struct options_array_item *); +int options_isarray(struct options_entry *); int options_isstring(struct options_entry *); const char *options_tostring(struct options_entry *, int, int); char *options_parse(const char *, int *); diff --git a/tty-keys.c b/tty-keys.c index 3eb8d428..da84077b 100644 --- a/tty-keys.c +++ b/tty-keys.c @@ -398,9 +398,10 @@ tty_keys_build(struct tty *tty) { const struct tty_default_key_raw *tdkr; const struct tty_default_key_code *tdkc; - u_int i, size; + u_int i; const char *s, *value; struct options_entry *o; + struct options_array_item *a; if (tty->key_tree != NULL) tty_keys_free(tty); @@ -423,11 +424,13 @@ tty_keys_build(struct tty *tty) } o = options_get(global_options, "user-keys"); - if (o != NULL && options_array_size(o, &size) != -1) { - for (i = 0; i < size; i++) { - value = options_array_get(o, i); + if (o != NULL) { + a = options_array_first(o); + while (a != NULL) { + value = options_array_item_value(a); if (value != NULL) tty_keys_add(tty, value, KEYC_USER + i); + a = options_array_next(a); } } } diff --git a/tty-term.c b/tty-term.c index 7df9a7d1..9e0bb63d 100644 --- a/tty-term.c +++ b/tty-term.c @@ -416,7 +416,8 @@ tty_term_find(char *name, int fd, char **cause) const struct tty_term_code_entry *ent; struct tty_code *code; struct options_entry *o; - u_int size, i; + struct options_array_item *a; + u_int i; int n, error; const char *s, *acs; @@ -491,12 +492,12 @@ tty_term_find(char *name, int fd, char **cause) /* Apply terminal overrides. */ o = options_get_only(global_options, "terminal-overrides"); - if (options_array_size(o, &size) != -1) { - for (i = 0; i < size; i++) { - s = options_array_get(o, i); - if (s != NULL) - tty_term_override(term, s); - } + a = options_array_first(o); + while (a != NULL) { + s = options_array_item_value(a); + if (s != NULL) + tty_term_override(term, s); + a = options_array_next(a); } /* Delete curses data. */