From 311dad8c28c8c1a842beb3dbb1757064b9d83b2e Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 21 Apr 2017 13:15:43 +0000 Subject: [PATCH 1/4] Do not run the config file in the context of the first client, instead use no client like we did before. This means commands like new-session won't try to attach if they are in the config file. --- cfg.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/cfg.c b/cfg.c index 5ad9916a..93d0f8f6 100644 --- a/cfg.c +++ b/cfg.c @@ -32,7 +32,6 @@ static char *cfg_file; int cfg_finished; static char **cfg_causes; static u_int cfg_ncauses; -struct client *cfg_client; static enum cmd_retval cfg_done(__unused struct cmdq_item *item, __unused void *data) @@ -44,8 +43,6 @@ cfg_done(__unused struct cmdq_item *item, __unused void *data) if (!RB_EMPTY(&sessions)) cfg_show_causes(RB_MIN(sessions, &sessions)); - if (cfg_client != NULL) - server_client_unref(cfg_client); return (CMD_RETURN_NORMAL); } @@ -62,20 +59,22 @@ start_cfg(void) const char *home; int quiet = 0; - cfg_client = TAILQ_FIRST(&clients); - if (cfg_client != NULL) - cfg_client->references++; + /* + * Note that the configuration files are loaded without a client, so + * NULL is passed into load_cfg() which means that commands run in the + * global queue and item->client is NULL for all commands. + */ - load_cfg(TMUX_CONF, cfg_client, NULL, 1); + load_cfg(TMUX_CONF, NULL, NULL, 1); if (cfg_file == NULL && (home = find_home()) != NULL) { xasprintf(&cfg_file, "%s/.tmux.conf", home); quiet = 1; } if (cfg_file != NULL) - load_cfg(cfg_file, cfg_client, NULL, quiet); + load_cfg(cfg_file, NULL, NULL, quiet); - cmdq_append(cfg_client, cmdq_get_callback(cfg_done, NULL)); + cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL)); } int From bba588752f8085da13edcacd71101055bd617303 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 21 Apr 2017 14:01:19 +0000 Subject: [PATCH 2/4] Store state shared between multiple commands in the queue in a shared structure. --- cmd-attach-session.c | 2 +- cmd-copy-mode.c | 5 +++-- cmd-find.c | 6 +++--- cmd-if-shell.c | 5 +++-- cmd-list-keys.c | 4 ++-- cmd-new-session.c | 2 +- cmd-queue.c | 30 ++++++++++++++++++------------ cmd-resize-pane.c | 5 +++-- cmd-send-keys.c | 2 +- cmd-switch-client.c | 2 +- format.c | 10 ++++++---- key-bindings.c | 8 +++++--- server-client.c | 5 +++-- tmux.h | 25 ++++++++++++++++++------- 14 files changed, 68 insertions(+), 43 deletions(-) diff --git a/cmd-attach-session.c b/cmd-attach-session.c index 079b22f3..684fc3f1 100644 --- a/cmd-attach-session.c +++ b/cmd-attach-session.c @@ -92,7 +92,7 @@ cmd_attach_session(struct cmdq_item *item, int dflag, int rflag, environ_update(s->options, c->environ, s->environ); c->session = s; - if (!item->repeat) + if (~item->shared->flags & CMDQ_SHARED_REPEAT) server_client_set_key_table(c, NULL); status_timer_start(c); notify_client("client-session-changed", c); diff --git a/cmd-copy-mode.c b/cmd-copy-mode.c index 448e7b9c..35c41a3a 100644 --- a/cmd-copy-mode.c +++ b/cmd-copy-mode.c @@ -56,12 +56,13 @@ static enum cmd_retval cmd_copy_mode_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; + struct cmdq_shared *shared = item->shared; struct client *c = item->client; struct session *s; struct window_pane *wp = item->state.tflag.wp; if (args_has(args, 'M')) { - if ((wp = cmd_mouse_pane(&item->mouse, &s, NULL)) == NULL) + if ((wp = cmd_mouse_pane(&shared->mouse, &s, NULL)) == NULL) return (CMD_RETURN_NORMAL); if (c == NULL || c->session != s) return (CMD_RETURN_NORMAL); @@ -80,7 +81,7 @@ cmd_copy_mode_exec(struct cmd *self, struct cmdq_item *item) if (args_has(args, 'M')) { if (wp->mode != NULL && wp->mode != &window_copy_mode) return (CMD_RETURN_NORMAL); - window_copy_start_drag(c, &item->mouse); + window_copy_start_drag(c, &shared->mouse); } if (wp->mode == &window_copy_mode && args_has(self->args, 'u')) window_copy_pageup(wp, 0); diff --git a/cmd-find.c b/cmd-find.c index 877af492..ae1ee835 100644 --- a/cmd-find.c +++ b/cmd-find.c @@ -998,8 +998,8 @@ cmd_find_target(struct cmd_find_state *fs, struct cmd_find_state *current, if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) { fs->current = &marked_pane; log_debug("%s: current is marked pane", __func__); - } else if (cmd_find_valid_state(&item->current)) { - fs->current = &item->current; + } else if (cmd_find_valid_state(&item->shared->current)) { + fs->current = &item->shared->current; log_debug("%s: current is from queue", __func__); } else { fs->current = current; @@ -1015,7 +1015,7 @@ cmd_find_target(struct cmd_find_state *fs, struct cmd_find_state *current, /* Mouse target is a plain = or {mouse}. */ if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) { - m = &item->mouse; + m = &item->shared->mouse; switch (type) { case CMD_FIND_PANE: fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl); diff --git a/cmd-if-shell.c b/cmd-if-shell.c index 0b1fe7f5..fafb8cee 100644 --- a/cmd-if-shell.c +++ b/cmd-if-shell.c @@ -65,6 +65,7 @@ static enum cmd_retval cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; + struct cmdq_shared *shared = item->shared; struct cmd_if_shell_data *cdata; char *shellcmd, *cmd, *cause; struct cmd_list *cmdlist; @@ -100,7 +101,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) } return (CMD_RETURN_ERROR); } - new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0); + new_item = cmdq_get_command(cmdlist, NULL, &shared->mouse, 0); cmdq_insert_after(item, new_item); cmd_list_free(cmdlist); return (CMD_RETURN_NORMAL); @@ -125,7 +126,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) cdata->item = item; else cdata->item = NULL; - memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse); + memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse); job_run(shellcmd, s, cwd, NULL, cmd_if_shell_callback, cmd_if_shell_free, cdata); diff --git a/cmd-list-keys.c b/cmd-list-keys.c index ebc221e5..a46e05f5 100644 --- a/cmd-list-keys.c +++ b/cmd-list-keys.c @@ -81,7 +81,7 @@ cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item) RB_FOREACH(bd, key_bindings, &table->key_bindings) { key = key_string_lookup_key(bd->key); - if (bd->can_repeat) + if (bd->flags & KEY_BINDING_REPEAT) repeat = 1; width = utf8_cstrwidth(table->name); @@ -101,7 +101,7 @@ cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item) if (!repeat) r = ""; - else if (bd->can_repeat) + else if (bd->flags & KEY_BINDING_REPEAT) r = "-r "; else r = " "; diff --git a/cmd-new-session.c b/cmd-new-session.c index a5b197b0..0808b60c 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -297,7 +297,7 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item) } else if (c->session != NULL) c->last_session = c->session; c->session = s; - if (!item->repeat) + if (~item->shared->flags & CMDQ_SHARED_REPEAT) server_client_set_key_table(c, NULL); status_timer_start(c); notify_client("client-session-changed", c); diff --git a/cmd-queue.c b/cmd-queue.c index 8e3b0bf3..56745c38 100644 --- a/cmd-queue.c +++ b/cmd-queue.c @@ -102,8 +102,11 @@ cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item) static void cmdq_remove(struct cmdq_item *item) { - if (item->formats != NULL) - format_free(item->formats); + if (item->shared != NULL && --item->shared->references == 0) { + if (item->shared->formats != NULL) + format_free(item->shared->formats); + free(item->shared); + } if (item->client != NULL) server_client_unref(item->client); @@ -150,6 +153,13 @@ cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current, struct cmd *cmd; u_int group = cmdq_next_group(); char *tmp; + struct cmdq_shared *shared; + + shared = xcalloc(1, sizeof *shared); + if (current != NULL) + cmd_find_copy_state(&shared->current, current); + if (m != NULL) + memcpy(&shared->mouse, m, sizeof shared->mouse); TAILQ_FOREACH(cmd, &cmdlist->list, qentry) { xasprintf(&tmp, "command[%s]", cmd->entry->name); @@ -161,13 +171,11 @@ cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current, item->group = group; item->flags = flags; + item->shared = shared; item->cmdlist = cmdlist; item->cmd = cmd; - if (current != NULL) - cmd_find_copy_state(&item->current, current); - if (m != NULL) - memcpy(&item->mouse, m, sizeof item->mouse); + shared->references++; cmdlist->references++; if (first == NULL) @@ -258,19 +266,17 @@ cmdq_fire_callback(struct cmdq_item *item) void cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...) { + struct cmdq_shared *shared = item->shared; va_list ap; - struct cmdq_item *loop; char *value; va_start(ap, fmt); xvasprintf(&value, fmt, ap); va_end(ap); - for (loop = item; loop != NULL; loop = item->next) { - if (loop->formats == NULL) - loop->formats = format_create(NULL, FORMAT_NONE, 0); - format_add(loop->formats, key, "%s", value); - } + if (shared->formats == NULL) + shared->formats = format_create(NULL, FORMAT_NONE, 0); + format_add(shared->formats, key, "%s", value); free(value); } diff --git a/cmd-resize-pane.c b/cmd-resize-pane.c index 37bfcece..b9755c1f 100644 --- a/cmd-resize-pane.c +++ b/cmd-resize-pane.c @@ -49,6 +49,7 @@ static enum cmd_retval cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; + struct cmdq_shared *shared = item->shared; struct window_pane *wp = item->state.tflag.wp; struct winlink *wl = item->state.tflag.wl; struct window *w = wl->window; @@ -60,12 +61,12 @@ cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item) int x, y; if (args_has(args, 'M')) { - if (cmd_mouse_window(&item->mouse, &s) == NULL) + if (cmd_mouse_window(&shared->mouse, &s) == NULL) return (CMD_RETURN_NORMAL); if (c == NULL || c->session != s) return (CMD_RETURN_NORMAL); c->tty.mouse_drag_update = cmd_resize_pane_mouse_update; - cmd_resize_pane_mouse_update(c, &item->mouse); + cmd_resize_pane_mouse_update(c, &shared->mouse); return (CMD_RETURN_NORMAL); } diff --git a/cmd-send-keys.c b/cmd-send-keys.c index ae861894..d6c958d5 100644 --- a/cmd-send-keys.c +++ b/cmd-send-keys.c @@ -62,7 +62,7 @@ cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item) struct client *c = item->state.c; struct window_pane *wp = item->state.tflag.wp; struct session *s = item->state.tflag.s; - struct mouse_event *m = &item->mouse; + struct mouse_event *m = &item->shared->mouse; struct utf8_data *ud, *uc; wchar_t wc; int i, literal; diff --git a/cmd-switch-client.c b/cmd-switch-client.c index 5bcdbb78..06388d44 100644 --- a/cmd-switch-client.c +++ b/cmd-switch-client.c @@ -108,7 +108,7 @@ cmd_switch_client_exec(struct cmd *self, struct cmdq_item *item) if (c->session != NULL && c->session != s) c->last_session = c->session; c->session = s; - if (!item->repeat) + if (~item->shared->flags & CMDQ_SHARED_REPEAT) server_client_set_key_table(c, NULL); status_timer_start(c); session_update_activity(s, NULL); diff --git a/format.c b/format.c index b173df4c..4f5d4b9c 100644 --- a/format.c +++ b/format.c @@ -554,10 +554,12 @@ format_create(struct cmdq_item *item, int tag, int flags) format_add(ft, "socket_path", "%s", socket_path); format_add_tv(ft, "start_time", &start_time); - if (item != NULL && item->cmd != NULL) - format_add(ft, "command", "%s", item->cmd->entry->name); - if (item != NULL && item->formats != NULL) - format_merge(ft, item->formats); + if (item != NULL) { + if (item->cmd != NULL) + format_add(ft, "command", "%s", item->cmd->entry->name); + if (item->shared != NULL && item->shared->formats != NULL) + format_merge(ft, item->shared->formats); + } return (ft); } diff --git a/key-bindings.c b/key-bindings.c index 37ff7fe7..ae7afb71 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -84,7 +84,7 @@ key_bindings_unref_table(struct key_table *table) } void -key_bindings_add(const char *name, key_code key, int can_repeat, +key_bindings_add(const char *name, key_code key, int repeat, struct cmd_list *cmdlist) { struct key_table *table; @@ -104,7 +104,8 @@ key_bindings_add(const char *name, key_code key, int can_repeat, bd->key = key; RB_INSERT(key_bindings, &table->key_bindings, bd); - bd->can_repeat = can_repeat; + if (repeat) + bd->flags |= KEY_BINDING_REPEAT; bd->cmdlist = cmdlist; } @@ -415,7 +416,8 @@ key_bindings_dispatch(struct key_binding *bd, struct client *c, cmdq_append(c, cmdq_get_callback(key_bindings_read_only, NULL)); else { item = cmdq_get_command(bd->cmdlist, fs, m, 0); - item->repeat = bd->can_repeat; + if (bd->flags & KEY_BINDING_REPEAT) + item->shared->flags |= CMDQ_SHARED_REPEAT; cmdq_append(c, item); } } diff --git a/server-client.c b/server-client.c index 41135c00..93d4bffc 100644 --- a/server-client.c +++ b/server-client.c @@ -908,7 +908,8 @@ retry: * non-repeating binding was found, stop repeating and try * again in the root table. */ - if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) { + if ((c->flags & CLIENT_REPEAT) && + (~bd->flags & KEY_BINDING_REPEAT)) { server_client_set_key_table(c, NULL); c->flags &= ~CLIENT_REPEAT; server_status_client(c); @@ -926,7 +927,7 @@ retry: * the client back to the root table. */ xtimeout = options_get_number(s->options, "repeat-time"); - if (xtimeout != 0 && bd->can_repeat) { + if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) { c->flags |= CLIENT_REPEAT; tv.tv_sec = xtimeout / 1000; diff --git a/tmux.h b/tmux.h index 901f5a8d..382efb40 100644 --- a/tmux.h +++ b/tmux.h @@ -41,6 +41,7 @@ extern char **environ; struct args; struct client; struct cmdq_item; +struct cmdq_subitem; struct cmdq_list; struct environ; struct input_ctx; @@ -1204,6 +1205,19 @@ enum cmdq_type { CMDQ_CALLBACK, }; +/* Command queue item shared state. */ +struct cmdq_shared { + int references; + + int flags; +#define CMDQ_SHARED_REPEAT 0x1 + + struct format_tree *formats; + + struct mouse_event mouse; + struct cmd_find_state current; +}; + /* Command queue item. */ typedef enum cmd_retval (*cmdq_cb) (struct cmdq_item *, void *); struct cmdq_item { @@ -1219,25 +1233,20 @@ struct cmdq_item { u_int number; time_t time; - struct format_tree *formats; - int flags; #define CMDQ_FIRED 0x1 #define CMDQ_WAITING 0x2 #define CMDQ_NOHOOKS 0x4 + struct cmdq_shared *shared; struct cmd_list *cmdlist; struct cmd *cmd; - int repeat; cmdq_cb cb; void *data; - struct cmd_find_state current; struct cmd_state state; - struct mouse_event mouse; - TAILQ_ENTRY(cmdq_item) entry; }; TAILQ_HEAD(cmdq_list, cmdq_item); @@ -1394,7 +1403,9 @@ TAILQ_HEAD(clients, client); struct key_binding { key_code key; struct cmd_list *cmdlist; - int can_repeat; + + int flags; +#define KEY_BINDING_REPEAT 0x1 RB_ENTRY(key_binding) entry; }; From 428be9803cab5121bc8ccad5ecbcce509c57b3a1 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 21 Apr 2017 14:04:54 +0000 Subject: [PATCH 3/4] History needs to be loaded after config parsing is done - now that commands are queued, that's in cfg_done not after start_cfg finishes. --- cfg.c | 2 ++ server.c | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cfg.c b/cfg.c index 93d0f8f6..f98d5f0b 100644 --- a/cfg.c +++ b/cfg.c @@ -43,6 +43,8 @@ cfg_done(__unused struct cmdq_item *item, __unused void *data) if (!RB_EMPTY(&sessions)) cfg_show_causes(RB_MIN(sessions, &sessions)); + status_prompt_load_history(); + return (CMD_RETURN_NORMAL); } diff --git a/server.c b/server.c index 70f1a76f..71c0e25a 100644 --- a/server.c +++ b/server.c @@ -176,8 +176,6 @@ server_start(struct event_base *base, int lockfd, char *lockfile) start_cfg(); - status_prompt_load_history(); - server_add_accept(0); proc_loop(server_proc, server_loop); From c799425069d02675474cda312fac1457829052fb Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 21 Apr 2017 14:09:44 +0000 Subject: [PATCH 4/4] More unnecessary arguments now winlink points back to session. --- alerts.c | 6 +++--- cmd-find.c | 5 ++--- cmd-new-window.c | 2 +- notify.c | 6 +++--- tmux.h | 4 ++-- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/alerts.c b/alerts.c index 43108ed4..708a24e2 100644 --- a/alerts.c +++ b/alerts.c @@ -178,7 +178,7 @@ alerts_check_bell(struct window *w) s = wl->session; if (s->curw != wl) { wl->flags |= WINLINK_BELL; - notify_winlink("alert-bell", s, wl); + notify_winlink("alert-bell", wl); } if (s->flags & SESSION_ALERTED) @@ -239,7 +239,7 @@ alerts_check_activity(struct window *w) continue; wl->flags |= WINLINK_ACTIVITY; - notify_winlink("alert-activity", s, wl); + notify_winlink("alert-activity", wl); if (s->flags & SESSION_ALERTED) continue; @@ -275,7 +275,7 @@ alerts_check_silence(struct window *w) if (s->curw == wl) continue; wl->flags |= WINLINK_SILENCE; - notify_winlink("alert-silence", s, wl); + notify_winlink("alert-silence", wl); if (s->flags & SESSION_ALERTED) continue; diff --git a/cmd-find.c b/cmd-find.c index ae1ee835..68c15f15 100644 --- a/cmd-find.c +++ b/cmd-find.c @@ -895,12 +895,11 @@ cmd_find_from_session(struct cmd_find_state *fs, struct session *s) /* Find state from a winlink. */ int -cmd_find_from_winlink(struct cmd_find_state *fs, struct session *s, - struct winlink *wl) +cmd_find_from_winlink(struct cmd_find_state *fs, struct winlink *wl) { cmd_find_clear_state(fs, NULL, 0); - fs->s = s; + fs->s = wl->session; fs->wl = wl; fs->w = wl->window; fs->wp = wl->window->active; diff --git a/cmd-new-window.c b/cmd-new-window.c index c10a4aaf..e99a8089 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -147,7 +147,7 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item) if (to_free != NULL) free((void *)to_free); - cmd_find_from_winlink(&fs, s, wl); + cmd_find_from_winlink(&fs, wl); hooks_insert(s->hooks, item, &fs, "after-new-window"); return (CMD_RETURN_NORMAL); diff --git a/notify.c b/notify.c index bf984211..446e71e7 100644 --- a/notify.c +++ b/notify.c @@ -184,12 +184,12 @@ notify_session(const char *name, struct session *s) } void -notify_winlink(const char *name, struct session *s, struct winlink *wl) +notify_winlink(const char *name, struct winlink *wl) { struct cmd_find_state fs; - cmd_find_from_winlink(&fs, s, wl); - notify_add(name, &fs, NULL, s, wl->window, NULL); + cmd_find_from_winlink(&fs, wl); + notify_add(name, &fs, NULL, wl->session, wl->window, NULL); } void diff --git a/tmux.h b/tmux.h index 382efb40..5b33b497 100644 --- a/tmux.h +++ b/tmux.h @@ -1563,7 +1563,7 @@ void printflike(4, 5) hooks_insert(struct hooks *, struct cmdq_item *, void notify_input(struct window_pane *, struct evbuffer *); void notify_client(const char *, struct client *); void notify_session(const char *, struct session *); -void notify_winlink(const char *, struct session *, struct winlink *); +void notify_winlink(const char *, struct winlink *); void notify_session_window(const char *, struct session *, struct window *); void notify_window(const char *, struct window *); void notify_pane(const char *, struct window_pane *); @@ -1747,7 +1747,7 @@ void cmd_find_log_state(const char *, struct cmd_find_state *); int cmd_find_from_session(struct cmd_find_state *, struct session *); int cmd_find_from_winlink(struct cmd_find_state *, - struct session *, struct winlink *); + struct winlink *); int cmd_find_from_session_window(struct cmd_find_state *, struct session *, struct window *); int cmd_find_from_window(struct cmd_find_state *, struct window *);