Sync OpenBSD patchset 1119:

Switch all of the various choose- and list- commands over to the format
infrastructure, from Thomas Adam.
pull/1/head
Tiago Cunha 2012-05-22 21:03:25 +00:00
parent baafc17a1e
commit 5cc4961fd2
16 changed files with 211 additions and 112 deletions

View File

@ -93,9 +93,10 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
server_status_session_group(s);
if (args_has(args, 'P')) {
template = "#{session_name}:#{window_index}";
if (args_has(args, 'F'))
template = args_get(args, 'F');
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_PANE_INFO_TEMPLATE;
ft = format_create();
if ((c = cmd_find_client(ctx, NULL)) != NULL)
format_client(ft, c);

View File

@ -33,8 +33,8 @@ void cmd_choose_buffer_free(void *);
const struct cmd_entry cmd_choose_buffer_entry = {
"choose-buffer", NULL,
"t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [template]",
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
NULL,
NULL,
@ -53,14 +53,19 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
struct cmd_choose_buffer_data *cdata;
struct winlink *wl;
struct paste_buffer *pb;
struct format_tree *ft;
u_int idx;
char *tmp;
char *line;
const char *template;
if (ctx->curclient == NULL) {
ctx->error(ctx, "must be run interactively");
return (-1);
}
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_BUFFER_LIST_TEMPLATE;
if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
return (-1);
@ -72,10 +77,15 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
idx = 0;
while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) {
tmp = paste_print(pb, 50);
window_choose_add(wl->window->active, idx - 1,
"%u: %zu bytes: \"%s\"", idx - 1, pb->size, tmp);
xfree(tmp);
ft = format_create();
format_add(ft, "line", "%u", idx - 1);
format_paste_buffer(ft, pb);
line = format_expand(ft, template);
window_choose_add(wl->window->active, idx - 1, "%s", line);
xfree(line);
format_free(ft);
}
cdata = xmalloc(sizeof *cdata);

View File

@ -33,8 +33,8 @@ void cmd_choose_client_free(void *);
const struct cmd_entry cmd_choose_client_entry = {
"choose-client", NULL,
"t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [template]",
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
NULL,
NULL,
@ -51,8 +51,11 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
struct cmd_choose_client_data *cdata;
struct format_tree *ft;
struct winlink *wl;
struct client *c;
char *line;
const char *template;
u_int i, idx, cur;
if (ctx->curclient == NULL) {
@ -66,6 +69,9 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
return (0);
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_CLIENT_TEMPLATE;
cur = idx = 0;
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
c = ARRAY_ITEM(&clients, i);
@ -75,12 +81,16 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
cur = idx;
idx++;
window_choose_add(wl->window->active, i,
"%s: %s [%ux%u %s]%s%s", c->tty.path,
c->session->name, c->tty.sx, c->tty.sy,
c->tty.termname,
c->tty.flags & TTY_UTF8 ? " (utf8)" : "",
c->flags & CLIENT_READONLY ? " (ro)" : "");
ft = format_create();
format_add(ft, "line", "%u", i);
format_session(ft, c->session);
format_client(ft, c);
line = format_expand(ft, template);
window_choose_add(wl->window->active, i, "%s", line);
xfree(line);
format_free(ft);
}
cdata = xmalloc(sizeof *cdata);

View File

@ -33,8 +33,8 @@ void cmd_choose_session_free(void *);
const struct cmd_entry cmd_choose_session_entry = {
"choose-session", NULL,
"t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [template]",
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
NULL,
NULL,
@ -53,9 +53,10 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
struct cmd_choose_session_data *cdata;
struct winlink *wl;
struct session *s;
struct session_group *sg;
u_int idx, sgidx, cur;
char tmp[64];
struct format_tree *ft;
const char *template;
char *line;
u_int idx, cur;
if (ctx->curclient == NULL) {
ctx->error(ctx, "must be run interactively");
@ -68,24 +69,24 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
return (0);
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_SESSION_TEMPLATE;
cur = idx = 0;
RB_FOREACH(s, sessions, &sessions) {
if (s == ctx->curclient->session)
cur = idx;
idx++;
sg = session_group_find(s);
if (sg == NULL)
*tmp = '\0';
else {
sgidx = session_group_index(sg);
xsnprintf(tmp, sizeof tmp, " (group %u)", sgidx);
}
ft = format_create();
format_add(ft, "line", "%u", idx);
format_session(ft, s);
window_choose_add(wl->window->active, s->idx,
"%s: %u windows [%ux%u]%s%s", s->name,
winlink_count(&s->windows), s->sx, s->sy,
tmp, s->flags & SESSION_UNATTACHED ? "" : " (attached)");
line = format_expand(ft, template);
window_choose_add(wl->window->active, s->idx, "%s", line);
xfree(line);
format_free(ft);
}
cdata = xmalloc(sizeof *cdata);

View File

@ -33,8 +33,8 @@ void cmd_choose_window_free(void *);
const struct cmd_entry cmd_choose_window_entry = {
"choose-window", NULL,
"t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [template]",
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
NULL,
NULL,
@ -54,10 +54,10 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
struct cmd_choose_window_data *cdata;
struct session *s;
struct winlink *wl, *wm;
struct window *w;
struct format_tree *ft;
const char *template;
char *line;
u_int idx, cur;
char *flags, *title;
const char *left, *right;
if (ctx->curclient == NULL) {
ctx->error(ctx, "must be run interactively");
@ -71,30 +71,25 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
return (0);
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_WINDOW_TEMPLATE;
cur = idx = 0;
RB_FOREACH(wm, winlinks, &s->windows) {
w = wm->window;
if (wm == s->curw)
cur = idx;
idx++;
flags = window_printable_flags(s, wm);
title = w->active->screen->title;
if (wm == wl)
title = w->active->base.title;
left = " \"";
right = "\"";
if (*title == '\0')
left = right = "";
ft = format_create();
format_add(ft, "line", "%u", idx);
format_session(ft, s);
format_winlink(ft, s, wm);
window_choose_add(wl->window->active,
wm->idx, "%3d: %s%s [%ux%u] (%u panes%s)%s%s%s",
wm->idx, w->name, flags, w->sx, w->sy, window_count_panes(w),
w->active->fd == -1 ? ", dead" : "",
left, title, right);
line = format_expand(ft, template);
window_choose_add(wl->window->active, idx, "%s", line);
xfree(flags);
xfree(line);
format_free(ft);
}
cdata = xmalloc(sizeof *cdata);

View File

@ -75,7 +75,7 @@ cmd_display_message_exec(struct cmd *self, struct cmd_ctx *ctx)
if (args->argc != 0)
template = args->argv[0];
if (template == NULL)
template = "[#S] #I:#W, current pane #P - (%H:%M %d-%b-%y)";
template = DEFAULT_DISPLAY_MESSAGE_TEMPLATE;
ft = format_create();
format_client(ft, c);

View File

@ -46,8 +46,8 @@ void cmd_find_window_free(void *);
const struct cmd_entry cmd_find_window_entry = {
"find-window", "findw",
"CNt:T", 1, 4,
"[-CNT] " CMD_TARGET_WINDOW_USAGE " match-string",
"F:CNt:T", 1, 4,
"[-CNT] [-F format] " CMD_TARGET_WINDOW_USAGE " match-string",
0,
NULL,
NULL,
@ -85,11 +85,13 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
struct cmd_find_window_data *cdata;
struct session *s;
struct winlink *wl, *wm;
struct window *w;
struct window_pane *wp;
struct format_tree *ft;
ARRAY_DECL(, u_int) list_idx;
ARRAY_DECL(, char *) list_ctx;
char *str, *sres, *sctx, *searchstr;
char *find_line;
const char *template;
u_int i, line, match_flags;
if (ctx->curclient == NULL) {
@ -101,6 +103,9 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
return (-1);
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_FIND_WINDOW_TEMPLATE;
match_flags = cmd_find_window_match_flags(args);
str = args->argv[0];
@ -167,13 +172,20 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
for (i = 0; i < ARRAY_LENGTH(&list_idx); i++) {
wm = winlink_find_by_index(
&s->windows, ARRAY_ITEM(&list_idx, i));
w = wm->window;
sctx = ARRAY_ITEM(&list_ctx, i);
window_choose_add(wl->window->active,
wm->idx, "%3d: %s [%ux%u] (%u panes) %s", wm->idx, w->name,
w->sx, w->sy, window_count_panes(w), sctx);
xfree(sctx);
ft = format_create();
format_add(ft, "line", "%u", i);
format_add(ft, "window_find_matches", "%s",
ARRAY_ITEM(&list_ctx, i));
format_session(ft, s);
format_winlink(ft, s, wm);
find_line = format_expand(ft, template);
window_choose_add(wl->window->active, wm->idx, "%s", find_line);
xfree(find_line);
format_free(ft);
}
cdata = xmalloc(sizeof *cdata);

View File

@ -30,8 +30,8 @@ int cmd_list_buffers_exec(struct cmd *, struct cmd_ctx *);
const struct cmd_entry cmd_list_buffers_entry = {
"list-buffers", "lsb",
"", 0, 0,
"",
"F:", 0, 0,
"[-F format]",
0,
NULL,
NULL,
@ -42,16 +42,27 @@ const struct cmd_entry cmd_list_buffers_entry = {
int
cmd_list_buffers_exec(unused struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
struct paste_buffer *pb;
struct format_tree *ft;
u_int idx;
char *tmp;
char *line;
const char *template;
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_BUFFER_LIST_TEMPLATE;
idx = 0;
while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) {
tmp = paste_print(pb, 50);
ctx->print(ctx,
"%u: %zu bytes: \"%s\"", idx - 1, pb->size, tmp);
xfree(tmp);
ft = format_create();
format_add(ft, "line", "%u", idx - 1);
format_paste_buffer(ft, pb);
line = format_expand(ft, template);
ctx->print(ctx, "%s", line);
xfree(line);
format_free(ft);
}
return (0);

View File

@ -58,13 +58,8 @@ cmd_list_clients_exec(struct cmd *self, struct cmd_ctx *ctx)
} else
s = NULL;
template = args_get(args, 'F');
if (template == NULL) {
template = "#{client_tty}: #{session_name} "
"[#{client_width}x#{client_height} #{client_termname}]"
"#{?client_utf8, (utf8),}"
"#{?client_readonly, (ro),}";
}
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_CLIENT_TEMPLATE;
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
c = ARRAY_ITEM(&clients, i);

View File

@ -49,14 +49,8 @@ cmd_list_sessions_exec(struct cmd *self, struct cmd_ctx *ctx)
const char *template;
char *line;
template = args_get(args, 'F');
if (template == NULL) {
template = "#{session_name}: #{session_windows} windows "
"(created #{session_created_string}) [#{session_width}x"
"#{session_height}]#{?session_grouped, (group ,}"
"#{session_group}#{?session_grouped,),}"
"#{?session_attached, (attached),}";
}
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_SESSION_TEMPLATE;
n = 0;
RB_FOREACH(s, sessions, &sessions) {

View File

@ -34,7 +34,7 @@ void cmd_list_windows_session(
const struct cmd_entry cmd_list_windows_entry = {
"list-windows", "lsw",
"aF:t:", 0, 0,
"F:at:", 0, 0,
"[-a] [-F format] " CMD_TARGET_SESSION_USAGE,
0,
NULL,
@ -84,18 +84,10 @@ cmd_list_windows_session(
if (template == NULL) {
switch (type) {
case 0:
template = "#{window_index}: "
"#{window_name} "
"[#{window_width}x#{window_height}] "
"[layout #{window_layout}] #{window_id}"
"#{?window_active, (active),}";
template = DEFAULT_WINDOW_TEMPLATE;
break;
case 1:
template = "#{session_name}:#{window_index}: "
"#{window_name} "
"[#{window_width}x#{window_height}] "
"[layout #{window_layout}] #{window_id}"
"#{?window_active, (active),}";
template = "#{session_name}:" DEFAULT_WINDOW_TEMPLATE;
break;
}
}

View File

@ -122,15 +122,15 @@ cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx)
server_status_session_group(s);
if (args_has(args, 'P')) {
template = "#{session_name}:#{window_index}";
if (args_has(args, 'F'))
template = args_get(args, 'F');
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_PANE_INFO_TEMPLATE;
ft = format_create();
if ((c = cmd_find_client(ctx, NULL)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
format_window_pane(ft, wl->window->active);
cp = format_expand(ft, template);
ctx->print(ctx, "%s", cp);

View File

@ -138,9 +138,8 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx)
environ_free(&env);
if (args_has(args, 'P')) {
template = "#{session_name}:#{window_index}.#{pane_index}";
if (args_has(args, 'F'))
template = args_get(args, 'F');
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_PANE_INFO_TEMPLATE;
ft = format_create();
if ((c = cmd_find_client(ctx, NULL)) != NULL)

View File

@ -349,6 +349,7 @@ format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
format_add(ft, "window_flags", "%s", flags);
format_add(ft, "window_layout", "%s", layout);
format_add(ft, "window_active", "%d", wl == s->curw);
format_add(ft, "window_panes", "%u", window_count_panes(w));
xfree(flags);
xfree(layout);
@ -393,3 +394,14 @@ format_window_pane(struct format_tree *ft, struct window_pane *wp)
format_add(ft, "pane_pid", "%ld", (long) wp->pid);
format_add(ft, "pane_tty", "%s", wp->tty);
}
void
format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
{
char *pb_print = paste_print(pb, 50);
format_add(ft, "buffer_size", "%zu", pb->size);
format_add(ft, "buffer_sample", "%s", pb_print);
xfree(pb_print);
}

51
tmux.1
View File

@ -1037,6 +1037,7 @@ visible pane and negative numbers are lines in the history.
The default is to capture only the visible contents of the pane.
.It Xo
.Ic choose-client
.Op Fl F Ar format
.Op Fl t Ar target-window
.Op Ar template
.Xc
@ -1052,10 +1053,16 @@ and the result executed as a command.
If
.Ar template
is not given, "detach-client -t '%%'" is used.
For the meaning of the
.Fl F
flag, see the
.Sx FORMATS
section.
This command works only from inside
.Nm .
.It Xo
.Ic choose-session
.Op Fl F Ar format
.Op Fl t Ar target-window
.Op Ar template
.Xc
@ -1069,10 +1076,16 @@ and the result executed as a command.
If
.Ar template
is not given, "switch-client -t '%%'" is used.
For the meaning of the
.Fl F
flag, see the
.Sx FORMATS
section.
This command works only from inside
.Nm .
.It Xo
.Ic choose-window
.Op Fl F Ar format
.Op Fl t Ar target-window
.Op Ar template
.Xc
@ -1086,6 +1099,11 @@ and the result executed as a command.
If
.Ar template
is not given, "select-window -t '%%'" is used.
For the meaning of the
.Fl F
flag, see the
.Sx FORMATS
section.
This command works only from inside
.Nm .
.It Ic display-panes Op Fl t Ar target-client
@ -1105,6 +1123,7 @@ to
keys.
.It Xo Ic find-window
.Op Fl CNT
.Op Fl F Ar format
.Op Fl t Ar target-window
.Ar match-string
.Xc
@ -1125,6 +1144,11 @@ The default is
.Fl CNT .
If only one window is matched, it'll be automatically selected,
otherwise a choice list is shown.
For the meaning of the
.Fl F
flag, see the
.Sx FORMATS
section.
This command only works from inside
.Nm .
.It Xo Ic join-pane
@ -2765,13 +2789,7 @@ or the global window options if
is used.
.El
.Sh FORMATS
The
.Ic list-clients ,
.Ic list-sessions ,
.Ic list-windows
and
.Ic list-panes
commands accept the
Certain commands accept the
.Fl F
flag with a
.Ar format
@ -2804,6 +2822,8 @@ if it is unattached.
The following variables are available, where appropriate:
.Bl -column "session_created_string" "Replaced with" -offset indent
.It Sy "Variable name" Ta Sy "Replaced with"
.It Li "buffer_sample" Ta "First 50 characters from the specified buffer"
.It Li "buffer_size" Ta "Size of the specified buffer in bytes"
.It Li "client_activity" Ta "Integer time client last had activity"
.It Li "client_activity_string" Ta "String time client last had activity"
.It Li "client_created" Ta "Integer time client created"
@ -2838,11 +2858,13 @@ The following variables are available, where appropriate:
.It Li "session_width" Ta "Width of session"
.It Li "session_windows" Ta "Number of windows in session"
.It Li "window_active" Ta "1 if window active"
.It Li "window_find_matches" Ta "Matched data from the find-window command if available"
.It Li "window_flags" Ta "Window flags"
.It Li "window_height" Ta "Height of window"
.It Li "window_index" Ta "Index of window"
.It Li "window_layout" Ta "Window layout description"
.It Li "window_name" Ta "Name of window"
.It Li "window_panes" Ta "Number of panes in window"
.It Li "window_width" Ta "Width of window"
.El
.Sh NAMES AND TITLES
@ -3149,6 +3171,7 @@ The buffer commands are as follows:
.Bl -tag -width Ds
.It Xo
.Ic choose-buffer
.Op Fl F Ar format
.Op Fl t Ar target-window
.Op Ar template
.Xc
@ -3162,6 +3185,11 @@ and the result executed as a command.
If
.Ar template
is not given, "paste-buffer -b '%%'" is used.
For the meaning of the
.Fl F
flag, see the
.Sx FORMATS
section.
This command works only from inside
.Nm .
.It Ic clear-history Op Fl t Ar target-pane
@ -3172,9 +3200,16 @@ Remove and free the history for the specified pane.
Delete the buffer at
.Ar buffer-index ,
or the top buffer if not specified.
.It Ic list-buffers
.It Xo Ic list-buffers
.Op Fl F Ar format
.Xc
.D1 (alias: Ic lsb )
List the global buffers.
For the meaning of the
.Fl F
flag, see the
.Sx FORMATS
section.
.It Xo Ic load-buffer
.Op Fl b Ar buffer-index
.Ar path

32
tmux.h
View File

@ -88,6 +88,37 @@ extern char **environ;
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
/* Default format templates. */
#define DEFAULT_BUFFER_LIST_TEMPLATE \
"#{line}: #{buffer_size} bytes: \"#{buffer_sample}\""
#define DEFAULT_CLIENT_TEMPLATE \
"#{client_tty}: #{session_name} " \
"[#client_width}x#{client_height} #{client_termname}]" \
"{?client_utf8, (utf8),} #{?client_readonly, (ro),}"
#define DEFAULT_DISPLAY_MESSAGE_TEMPLATE \
"[#{session_name}] #{window_index}:" \
"#{window_name}, current pane #{pane_index} " \
"- (%H:%M %d-%b-%y)"
#define DEFAULT_FIND_WINDOW_TEMPLATE \
"#{window_index}: #{window_name} " \
"[#{window_width}x#{window_height}] " \
"(#{window_panes} panes) #{window_find_matches}"
#define DEFAULT_SESSION_TEMPLATE \
"#{session_name}: #{session_windows} windows " \
"(created #{session_created_string}) " \
"[#{session_width}x#{session_height}]" \
"#{?session_grouped, (group ,}" \
"#{session_group}#{?session_grouped,),}" \
"#{?session_attached, (attached),}"
#define DEFAULT_WINDOW_TEMPLATE \
"#{window_index}: #{window_name}#{window_flags} " \
"(#{window_panes} panes) " \
"[#{window_width}x#{window_height}] " \
"[layout #{window_layout}] #{window_id}" \
"#{?window_active, (active),}"
#define DEFAULT_PANE_INFO_TEMPLATE \
"#{session_name}:#{window_index}.#{pane_index}"
/* Bell option values. */
#define BELL_NONE 0
#define BELL_ANY 1
@ -1402,6 +1433,7 @@ void format_client(struct format_tree *, struct client *);
void format_winlink(
struct format_tree *, struct session *, struct winlink *);
void format_window_pane(struct format_tree *, struct window_pane *);
void format_paste_buffer(struct format_tree *, struct paste_buffer *);
/* mode-key.c */
extern const struct mode_key_table mode_key_tables[];