From 6f5150a943425d7d5d65ae443f956931fcb82d0b Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Fri, 17 Jul 2009 18:45:08 +0000 Subject: [PATCH] - New command display-message (alias display) to display a message in the status line (bound to "i" and displays the current window and time by default). The same substitutions are applied as for status-left/right. - Add support for including the window index (#I), pane index (#P) and window name (#W) in the message, and status-left or status-right. - Bump protocol version. From Tiago Cunha, thanks! --- Makefile | 3 +- cmd-display-message.c | 65 +++++++++++++++++++++++++++++++++++++++++++ cmd.c | 1 + key-bindings.c | 1 + status.c | 21 ++++++++++++-- tmux.1 | 12 ++++++++ tmux.h | 5 +++- window.c | 15 ++++++++++ 8 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 cmd-display-message.c diff --git a/Makefile b/Makefile index ef628faa..6f6644a0 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,8 @@ SRCS= attributes.c buffer-poll.c buffer.c cfg.c client-fn.c \ cmd-split-window.c cmd-start-server.c cmd-string.c cmd-if-shell.c \ cmd-suspend-client.c cmd-swap-pane.c cmd-swap-window.c \ cmd-switch-client.c cmd-unbind-key.c cmd-unlink-window.c \ - cmd-up-pane.c cmd.c colour.c grid-view.c grid.c input-keys.c \ + cmd-up-pane.c cmd-display-message.c cmd.c \ + colour.c grid-view.c grid.c input-keys.c \ input.c key-bindings.c key-string.c layout-manual.c layout.c log.c \ mode-key.c names.c options-cmd.c options.c paste.c procname.c \ resize.c screen-redraw.c screen-write.c screen.c server-fn.c \ diff --git a/cmd-display-message.c b/cmd-display-message.c new file mode 100644 index 00000000..332b6e28 --- /dev/null +++ b/cmd-display-message.c @@ -0,0 +1,65 @@ +/* $OpenBSD$ */ + +/* + * 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" + +/* + * Displays a message in the status line. + */ + +int cmd_display_message_exec(struct cmd *, struct cmd_ctx *); + +const struct cmd_entry cmd_display_message_entry = { + "display-message", "display", + CMD_TARGET_CLIENT_USAGE " [message]", + CMD_ARG01, 0, + cmd_target_init, + cmd_target_parse, + cmd_display_message_exec, + cmd_target_send, + cmd_target_recv, + cmd_target_free, + cmd_target_print +}; + +int +cmd_display_message_exec(struct cmd *self, struct cmd_ctx *ctx) +{ + struct cmd_target_data *data = self->data; + struct client *c; + const char *template; + char *msg; + + if ((c = cmd_find_client(ctx, data->target)) == NULL) + return (-1); + + if (data->arg == NULL) + template = "[#S] #I:#W, current pane #P - (%H:%M %d-%b-%y)"; + else + template = data->arg; + + msg = status_replace(c->session, template, time(NULL)); + status_message_set(c, "%s", msg); + xfree(msg); + + return (0); +} diff --git a/cmd.c b/cmd.c index f2466216..30553287 100644 --- a/cmd.c +++ b/cmd.c @@ -41,6 +41,7 @@ const struct cmd_entry *cmd_table[] = { &cmd_copy_mode_entry, &cmd_delete_buffer_entry, &cmd_detach_client_entry, + &cmd_display_message_entry, &cmd_down_pane_entry, &cmd_find_window_entry, &cmd_has_session_entry, diff --git a/key-bindings.c b/key-bindings.c index 56b1d58b..c298c79d 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -119,6 +119,7 @@ key_bindings_init(void) { 'c', 0, &cmd_new_window_entry }, { 'd', 0, &cmd_detach_client_entry }, { 'f', 0, &cmd_command_prompt_entry }, + { 'i', 0, &cmd_display_message_entry }, { 'l', 0, &cmd_last_window_entry }, { 'n', 0, &cmd_next_window_entry }, { 'o', 0, &cmd_down_pane_entry }, diff --git a/status.c b/status.c index 32effe5d..c5190b54 100644 --- a/status.c +++ b/status.c @@ -29,7 +29,6 @@ #include "tmux.h" -char *status_replace(struct session *, char *, time_t); char *status_replace_popen(char **); size_t status_width(struct winlink *); char *status_print(struct session *, struct winlink *, struct grid_cell *); @@ -275,7 +274,7 @@ out: } char * -status_replace(struct session *s, char *fmt, time_t t) +status_replace(struct session *s, const char *fmt, time_t t) { struct winlink *wl = s->curw; static char out[BUFSIZ]; @@ -323,6 +322,20 @@ status_replace(struct session *s, char *fmt, time_t t) ptr = tmp; } /* FALLTHROUGH */ + case 'I': + if (ptr == NULL) { + xsnprintf(tmp, sizeof tmp, "%d", wl->idx); + ptr = tmp; + } + /* FALLTHROUGH */ + case 'P': + if (ptr == NULL) { + xsnprintf(tmp, sizeof tmp, "%u", + window_pane_index(wl->window, + wl->window->active)); + ptr = tmp; + } + /* FALLTHOUGH */ case 'S': if (ptr == NULL) ptr = s->name; @@ -330,6 +343,10 @@ status_replace(struct session *s, char *fmt, time_t t) case 'T': if (ptr == NULL) ptr = wl->window->active->base.title; + /* FALLTHROUGH */ + case 'W': + if (ptr == NULL) + ptr = wl->window->name; len = strlen(ptr); if ((size_t) n < len) len = n; diff --git a/tmux.1 b/tmux.1 index dcc30a25..9b2f344c 100644 --- a/tmux.1 +++ b/tmux.1 @@ -717,6 +717,15 @@ or the top buffer if not specified. .D1 (alias: Ic detach ) Detach the current client if bound to a key, or the specified client with .Fl t . +.It Xo Ic display-message +.Op Fl t Ar target-client +.Op Ar message +.Xc +.D1 (alias: Ic display ) +Display a message (see the +.Ic status-left +option below) +in the status line. .It Xo Ic down-pane .Op Fl p Ar pane-index .Op Fl t Ar target-window @@ -1262,8 +1271,11 @@ may contain any of the following special character pairs: .It Sy "Character pair" Ta Sy "Replaced with" .It Li "#(command)" Ta "First line of command's output" .It Li "#H" Ta "Hostname of local host" +.It Li "#I" Ta "Current window index" +.It Li "#P" Ta "Current pane index" .It Li "#S" Ta "Session name" .It Li "#T" Ta "Current window title" +.It Li "#W" Ta "Current window name" .It Li "##" Ta "A literal" Ql # .El .Pp diff --git a/tmux.h b/tmux.h index 60455df1..2a0412ba 100644 --- a/tmux.h +++ b/tmux.h @@ -19,7 +19,7 @@ #ifndef TMUX_H #define TMUX_H -#define PROTOCOL_VERSION -13 +#define PROTOCOL_VERSION -14 #include #include @@ -1110,6 +1110,7 @@ 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; extern const struct cmd_entry cmd_detach_client_entry; +extern const struct cmd_entry cmd_display_message_entry; extern const struct cmd_entry cmd_down_pane_entry; extern const struct cmd_entry cmd_find_window_entry; extern const struct cmd_entry cmd_has_session_entry; @@ -1286,6 +1287,7 @@ int server_unlock(const char *); /* status.c */ int status_redraw(struct client *); +char *status_replace(struct session *, const char *, time_t); void printflike2 status_message_set(struct client *, const char *, ...); void status_message_clear(struct client *); int status_message_redraw(struct client *); @@ -1447,6 +1449,7 @@ struct window_pane *window_add_pane(struct window *, int, const char *, const char *, const char **, u_int, char **); void window_remove_pane(struct window *, struct window_pane *); struct window_pane *window_pane_at_index(struct window *, u_int); +u_int window_pane_index(struct window *, struct window_pane *); u_int window_count_panes(struct window *); void window_destroy_panes(struct window *); struct window_pane *window_pane_create(struct window *, u_int, u_int, u_int); diff --git a/window.c b/window.c index 7823d2f5..81b4d34e 100644 --- a/window.c +++ b/window.c @@ -379,6 +379,21 @@ window_pane_at_index(struct window *w, u_int idx) return (NULL); } +u_int +window_pane_index(struct window *w, struct window_pane *wp) +{ + struct window_pane *wq; + u_int n; + + n = 0; + TAILQ_FOREACH(wq, &w->panes, entry) { + if (wp == wq) + break; + n++; + } + return (n); +} + u_int window_count_panes(struct window *w) {