diff --git a/CHANGES b/CHANGES index 1cbd47c2..f1cb4730 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,9 @@ +27 April 2009 + +* New command, confirm-before (alias confirm), which asks for confirmation + before executing a command. Bound "&" and "x" by default to confirm-before + "kill-window" and confirm-before "kill-pane", respectively. + 23 April 2009 * Support NEL, yet another way of making newline. Fixes the output from some @@ -1216,7 +1222,7 @@ (including mutt, emacs). No status bar yet and no key remapping or other customisation. -$Id: CHANGES,v 1.275 2009-04-23 21:28:45 nicm Exp $ +$Id: CHANGES,v 1.276 2009-04-27 13:21:15 tcunha Exp $ LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB ms diff --git a/GNUmakefile b/GNUmakefile index b6833c07..01d890df 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,4 +1,4 @@ -# $Id: GNUmakefile,v 1.83 2009-04-21 20:54:18 nicm Exp $ +# $Id: GNUmakefile,v 1.84 2009-04-27 13:21:15 tcunha Exp $ .PHONY: clean @@ -38,7 +38,7 @@ SRCS= tmux.c server.c server-msg.c server-fn.c buffer.c buffer-poll.c status.c \ cmd-up-pane.c cmd-down-pane.c cmd-choose-window.c cmd-choose-session.c \ cmd-suspend-client.c cmd-find-window.c cmd-load-buffer.c \ cmd-copy-buffer.c cmd-break-pane.c cmd-swap-pane.c cmd-next-layout.c \ - cmd-rotate-window.c \ + cmd-rotate-window.c cmd-confirm-before.c \ window-clock.c window-scroll.c window-more.c window-copy.c \ window-choose.c \ options.c options-cmd.c paste.c colour.c utf8.c clock.c \ diff --git a/Makefile b/Makefile index 3a38d2d1..f2e2b328 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.122 2009-04-21 20:54:18 nicm Exp $ +# $Id: Makefile,v 1.123 2009-04-27 13:21:15 tcunha Exp $ .SUFFIXES: .c .o .y .h .PHONY: clean update-index.html upload-index.html @@ -41,7 +41,7 @@ SRCS= tmux.c server.c server-msg.c server-fn.c buffer.c buffer-poll.c status.c \ cmd-up-pane.c cmd-down-pane.c cmd-choose-window.c cmd-choose-session.c \ cmd-suspend-client.c cmd-find-window.c cmd-load-buffer.c \ cmd-copy-buffer.c cmd-break-pane.c cmd-swap-pane.c cmd-next-layout.c \ - cmd-rotate-window.c \ + cmd-rotate-window.c cmd-confirm-before.c \ window-clock.c window-scroll.c window-more.c window-copy.c \ window-choose.c \ options.c options-cmd.c paste.c colour.c utf8.c clock.c \ diff --git a/TODO b/TODO index 2ccfae93..e206211b 100644 --- a/TODO +++ b/TODO @@ -59,8 +59,6 @@ - set-remain-on-exit is a bit of a hack, some way to do it generically? - set-option should be set-session-option and should be overall global options for stuff like mode keys? -- a confirm-before command which asks "Are you sure? (y/n)" before executing - command, eg bind-key "&" confirm-before "kill-window" - clear window title on exit - refer to windows by name etc (duplicates? fnmatch?) - the output code (tty.c) could do with optimisation depending on term diff --git a/cmd-confirm-before.c b/cmd-confirm-before.c new file mode 100644 index 00000000..79b67b10 --- /dev/null +++ b/cmd-confirm-before.c @@ -0,0 +1,217 @@ +/* $Id: cmd-confirm-before.c,v 1.1 2009-04-27 13:21:15 tcunha Exp $ */ + +/* + * Copyright (c) 2009 Tiago Cunha + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "tmux.h" + +/* + * Asks for confirmation before executing a command. + */ + +int cmd_confirm_before_parse(struct cmd *, int, char **, char **); +int cmd_confirm_before_exec(struct cmd *, struct cmd_ctx *); +void cmd_confirm_before_send(struct cmd *, struct buffer *); +void cmd_confirm_before_recv(struct cmd *, struct buffer *); +void cmd_confirm_before_free(struct cmd *); +void cmd_confirm_before_init(struct cmd *, int); +size_t cmd_confirm_before_print(struct cmd *, char *, size_t); +int cmd_confirm_before_callback(void *, const char *); + +struct cmd_confirm_before_data { + char *cmd; +}; + +struct cmd_confirm_before_cdata { + struct client *c; + struct cmd_confirm_before_data data; +}; + +const struct cmd_entry cmd_confirm_before_entry = { + "confirm-before", "confirm", + "command", + 0, + cmd_confirm_before_init, + cmd_confirm_before_parse, + cmd_confirm_before_exec, + cmd_confirm_before_send, + cmd_confirm_before_recv, + cmd_confirm_before_free, + cmd_confirm_before_print +}; + +void +cmd_confirm_before_init(struct cmd *self, int key) +{ + struct cmd_confirm_before_data *data; + + self->data = data = xmalloc(sizeof *data); + data->cmd = NULL; + + switch (key) { + case '&': + data->cmd = xstrdup("kill-window"); + break; + case 'x': + data->cmd = xstrdup("kill-pane"); + break; + } +} + +int +cmd_confirm_before_parse(struct cmd *self, int argc, char **argv, char **cause) +{ + struct cmd_confirm_before_data *data; + int opt; + + self->entry->init(self, 0); + data = self->data; + + while ((opt = getopt(argc, argv, "")) != -1) { + switch (opt) { + default: + goto usage; + } + } + argc -= optind; + argv += optind; + if (argc != 1) + goto usage; + + data->cmd = xstrdup(argv[0]); + return (0); + +usage: + xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage); + + self->entry->free(self); + return (-1); +} + +int +cmd_confirm_before_exec(unused struct cmd *self, struct cmd_ctx *ctx) +{ + struct cmd_confirm_before_cdata *cdata; + struct cmd_confirm_before_data *data = self->data; + char *buf, *cmd, *ptr; + + if (ctx->curclient == NULL) { + ctx->error(ctx, "must be run interactively"); + return (-1); + } + + ptr = xstrdup(data->cmd); + if ((cmd = strtok(ptr, " \t")) == NULL) + cmd = ptr; + xasprintf(&buf, "Confirm '%s'? (y/n) ", cmd); + xfree(ptr); + + cdata = xmalloc(sizeof *cdata); + cdata->data.cmd = xstrdup(data->cmd); + cdata->c = ctx->curclient; + status_prompt_set(cdata->c, buf, cmd_confirm_before_callback, cdata, 0); + + xfree(buf); + return (1); +} + +void +cmd_confirm_before_send(struct cmd *self, struct buffer *b) +{ + struct cmd_confirm_before_data *data = self->data; + + buffer_write(b, data, sizeof *data); + cmd_send_string(b, data->cmd); +} + +void +cmd_confirm_before_recv(struct cmd *self, struct buffer *b) +{ + struct cmd_confirm_before_data *data; + + self->data = data = xmalloc(sizeof *data); + buffer_read(b, data, sizeof *data); + data->cmd = cmd_recv_string(b); +} + +void +cmd_confirm_before_free(struct cmd *self) +{ + struct cmd_confirm_before_data *data = self->data; + + if (data->cmd != NULL) + xfree(data->cmd); + xfree(data); +} + +size_t +cmd_confirm_before_print(struct cmd *self, char *buf, size_t len) +{ + struct cmd_confirm_before_data *data = self->data; + size_t off = 0; + + off += xsnprintf(buf, len, "%s ", self->entry->name); + if (data == NULL) + return (off); + if (off < len && data->cmd != NULL) + off += xsnprintf(buf + off, len - off, "%s", data->cmd); + return (off); +} + +int +cmd_confirm_before_callback(void *data, const char *s) +{ + struct cmd_confirm_before_cdata *cdata = data; + struct client *c = cdata->c; + struct cmd_list *cmdlist; + struct cmd_ctx ctx; + char *cause; + + if (s == NULL || (strcasecmp(s, "y") && strcasecmp(s, "yes"))) + goto out; + + if (cmd_string_parse(cdata->data.cmd, &cmdlist, &cause) != 0) { + if (cause != NULL) { + *cause = toupper((u_char) *cause); + status_message_set(c, cause); + xfree(cause); + } + goto out; + } + + ctx.msgdata = NULL; + ctx.cursession = c->session; + ctx.curclient = c; + + ctx.error = key_bindings_error; + ctx.print = key_bindings_print; + ctx.info = key_bindings_info; + + ctx.cmdclient = NULL; + + cmd_list_exec(cmdlist, &ctx); + cmd_list_free(cmdlist); + +out: + if (cdata->data.cmd != NULL) + xfree(cdata->data.cmd); + xfree(cdata); + + return (0); +} diff --git a/cmd.c b/cmd.c index bde828e7..b8f3f2ec 100644 --- a/cmd.c +++ b/cmd.c @@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.89 2009-04-03 17:21:46 nicm Exp $ */ +/* $Id: cmd.c,v 1.90 2009-04-27 13:21:15 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -33,6 +33,7 @@ const struct cmd_entry *cmd_table[] = { &cmd_choose_window_entry, &cmd_clock_mode_entry, &cmd_command_prompt_entry, + &cmd_confirm_before_entry, &cmd_copy_buffer_entry, &cmd_copy_mode_entry, &cmd_delete_buffer_entry, diff --git a/key-bindings.c b/key-bindings.c index 5ecd3a98..163551b6 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -1,4 +1,4 @@ -/* $Id: key-bindings.c,v 1.66 2009-04-03 17:21:46 nicm Exp $ */ +/* $Id: key-bindings.c,v 1.67 2009-04-27 13:21:15 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -83,7 +83,7 @@ key_bindings_init(void) { '!', 0, &cmd_break_pane_entry }, { '"', 0, &cmd_split_window_entry }, { '#', 0, &cmd_list_buffers_entry }, - { '&', 0, &cmd_kill_window_entry }, + { '&', 0, &cmd_confirm_before_entry }, { ',', 0, &cmd_command_prompt_entry }, { '-', 0, &cmd_delete_buffer_entry }, { '.', 0, &cmd_command_prompt_entry }, @@ -115,7 +115,7 @@ key_bindings_init(void) { 's', 0, &cmd_choose_session_entry }, { 't', 0, &cmd_clock_mode_entry }, { 'w', 0, &cmd_choose_window_entry }, - { 'x', 0, &cmd_kill_pane_entry, }, + { 'x', 0, &cmd_confirm_before_entry }, { '{', 0, &cmd_swap_pane_entry }, { '}', 0, &cmd_swap_pane_entry }, { META, 0, &cmd_send_prefix_entry }, diff --git a/tmux.1 b/tmux.1 index 82071d6a..7ee029d7 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1,4 +1,4 @@ -.\" $Id: tmux.1,v 1.89 2009-04-21 20:06:46 nicm Exp $ +.\" $Id: tmux.1,v 1.90 2009-04-27 13:21:16 tcunha Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -493,6 +493,14 @@ If .Ar template is specified, it is used as the command; any %% in the template will be replaced by what is entered at the prompt. +.It Xo Ic confirm-before +.Ar command +.Xc +.D1 (alias: Ic confirm) +Asks for confirmation before executing +.Ar command . +This command works only from inside +.Nm . .It Xo Ic copy-buffer .Op Fl a Ar src-index .Op Fl b Ar dst-index diff --git a/tmux.h b/tmux.h index a756ebb4..ec9ba670 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.300 2009-04-23 21:09:17 nicm Exp $ */ +/* $Id: tmux.h,v 1.301 2009-04-27 13:21:16 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1181,6 +1181,7 @@ extern const struct cmd_entry cmd_choose_session_entry; extern const struct cmd_entry cmd_choose_window_entry; extern const struct cmd_entry cmd_clock_mode_entry; extern const struct cmd_entry cmd_command_prompt_entry; +extern const struct cmd_entry cmd_confirm_before_entry; extern const struct cmd_entry cmd_copy_buffer_entry; extern const struct cmd_entry cmd_copy_mode_entry; extern const struct cmd_entry cmd_delete_buffer_entry;