2011-08-26 10:53:16 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2011-08-26 10:53:16 +00:00
|
|
|
*
|
|
|
|
* 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 <sys/types.h>
|
2014-12-09 19:23:35 +00:00
|
|
|
#include <sys/wait.h>
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2018-06-27 17:36:10 +00:00
|
|
|
#include <ctype.h>
|
2013-10-10 11:50:20 +00:00
|
|
|
#include <errno.h>
|
2017-05-29 15:43:48 +00:00
|
|
|
#include <fnmatch.h>
|
2015-10-25 22:29:17 +00:00
|
|
|
#include <libgen.h>
|
2011-08-26 10:53:16 +00:00
|
|
|
#include <stdarg.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2011-08-26 10:53:16 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build a list of key-value pairs and use them to expand #{key} entries in a
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
struct format_entry;
|
|
|
|
typedef void (*format_cb)(struct format_tree *, struct format_entry *);
|
|
|
|
|
2016-10-10 21:29:23 +00:00
|
|
|
static char *format_job_get(struct format_tree *, const char *);
|
|
|
|
static void format_job_timer(int, short, void *);
|
|
|
|
|
|
|
|
static char *format_find(struct format_tree *, const char *, int);
|
|
|
|
static void format_add_cb(struct format_tree *, const char *, format_cb);
|
|
|
|
static void format_add_tv(struct format_tree *, const char *,
|
|
|
|
struct timeval *);
|
|
|
|
static int format_replace(struct format_tree *, const char *, size_t,
|
|
|
|
char **, size_t *, size_t *);
|
|
|
|
|
|
|
|
static void format_defaults_session(struct format_tree *,
|
|
|
|
struct session *);
|
|
|
|
static void format_defaults_client(struct format_tree *, struct client *);
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
static void format_defaults_winlink(struct format_tree *, struct winlink *);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
/* Entry in format job tree. */
|
|
|
|
struct format_job {
|
2017-05-01 12:20:55 +00:00
|
|
|
struct client *client;
|
2017-02-03 11:57:27 +00:00
|
|
|
u_int tag;
|
2015-05-27 13:28:04 +00:00
|
|
|
const char *cmd;
|
2016-11-17 10:06:08 +00:00
|
|
|
const char *expanded;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
time_t last;
|
|
|
|
char *out;
|
2017-04-20 09:20:22 +00:00
|
|
|
int updated;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
struct job *job;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
RB_ENTRY(format_job) entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Format job tree. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static struct event format_job_event;
|
|
|
|
static int format_job_cmp(struct format_job *, struct format_job *);
|
2016-10-11 13:45:47 +00:00
|
|
|
static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
|
2016-10-10 21:29:23 +00:00
|
|
|
RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
/* Format job tree comparison function. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2015-05-27 13:28:04 +00:00
|
|
|
format_job_cmp(struct format_job *fj1, struct format_job *fj2)
|
|
|
|
{
|
2017-02-03 11:57:27 +00:00
|
|
|
if (fj1->tag < fj2->tag)
|
|
|
|
return (-1);
|
|
|
|
if (fj1->tag > fj2->tag)
|
|
|
|
return (1);
|
2015-05-27 13:28:04 +00:00
|
|
|
return (strcmp(fj1->cmd, fj2->cmd));
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
/* Format modifiers. */
|
|
|
|
#define FORMAT_TIMESTRING 0x1
|
|
|
|
#define FORMAT_BASENAME 0x2
|
|
|
|
#define FORMAT_DIRNAME 0x4
|
2015-11-18 14:13:55 +00:00
|
|
|
#define FORMAT_SUBSTITUTE 0x8
|
2018-08-26 09:28:42 +00:00
|
|
|
#define FORMAT_QUOTE 0x10
|
2015-10-25 22:29:17 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
/* Entry in format tree. */
|
|
|
|
struct format_entry {
|
2015-08-28 16:46:40 +00:00
|
|
|
char *key;
|
|
|
|
char *value;
|
2015-10-25 22:29:17 +00:00
|
|
|
time_t t;
|
2015-08-28 16:46:40 +00:00
|
|
|
format_cb cb;
|
|
|
|
RB_ENTRY(format_entry) entry;
|
2014-12-02 23:19:45 +00:00
|
|
|
};
|
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
/* Format entry tree. */
|
2014-12-02 23:19:45 +00:00
|
|
|
struct format_tree {
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
struct client *c;
|
2015-08-28 16:46:40 +00:00
|
|
|
struct session *s;
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct window *w;
|
2015-08-28 16:46:40 +00:00
|
|
|
struct window_pane *wp;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
struct client *client;
|
2017-02-03 11:57:27 +00:00
|
|
|
u_int tag;
|
2015-09-14 10:25:52 +00:00
|
|
|
int flags;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
RB_HEAD(format_entry_tree, format_entry) tree;
|
|
|
|
};
|
2016-10-10 21:29:23 +00:00
|
|
|
static int format_entry_cmp(struct format_entry *, struct format_entry *);
|
|
|
|
RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
/* Format entry tree comparison function. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2015-05-27 13:28:04 +00:00
|
|
|
format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
|
|
|
return (strcmp(fe1->key, fe2->key));
|
|
|
|
}
|
|
|
|
|
2013-05-31 19:46:42 +00:00
|
|
|
/* Single-character uppercase aliases. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static const char *format_upper[] = {
|
2011-08-26 10:53:16 +00:00
|
|
|
NULL, /* A */
|
|
|
|
NULL, /* B */
|
|
|
|
NULL, /* C */
|
|
|
|
"pane_id", /* D */
|
|
|
|
NULL, /* E */
|
|
|
|
"window_flags", /* F */
|
|
|
|
NULL, /* G */
|
|
|
|
"host", /* H */
|
|
|
|
"window_index", /* I */
|
|
|
|
NULL, /* J */
|
|
|
|
NULL, /* K */
|
|
|
|
NULL, /* L */
|
|
|
|
NULL, /* M */
|
|
|
|
NULL, /* N */
|
|
|
|
NULL, /* O */
|
|
|
|
"pane_index", /* P */
|
|
|
|
NULL, /* Q */
|
|
|
|
NULL, /* R */
|
|
|
|
"session_name", /* S */
|
|
|
|
"pane_title", /* T */
|
|
|
|
NULL, /* U */
|
|
|
|
NULL, /* V */
|
|
|
|
"window_name", /* W */
|
|
|
|
NULL, /* X */
|
|
|
|
NULL, /* Y */
|
|
|
|
NULL /* Z */
|
|
|
|
};
|
|
|
|
|
2013-05-31 19:46:42 +00:00
|
|
|
/* Single-character lowercase aliases. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static const char *format_lower[] = {
|
2013-05-31 19:46:42 +00:00
|
|
|
NULL, /* a */
|
|
|
|
NULL, /* b */
|
|
|
|
NULL, /* c */
|
|
|
|
NULL, /* d */
|
|
|
|
NULL, /* e */
|
|
|
|
NULL, /* f */
|
|
|
|
NULL, /* g */
|
|
|
|
"host_short", /* h */
|
|
|
|
NULL, /* i */
|
|
|
|
NULL, /* j */
|
|
|
|
NULL, /* k */
|
|
|
|
NULL, /* l */
|
|
|
|
NULL, /* m */
|
|
|
|
NULL, /* n */
|
|
|
|
NULL, /* o */
|
|
|
|
NULL, /* p */
|
|
|
|
NULL, /* q */
|
|
|
|
NULL, /* r */
|
|
|
|
NULL, /* s */
|
|
|
|
NULL, /* t */
|
|
|
|
NULL, /* u */
|
|
|
|
NULL, /* v */
|
|
|
|
NULL, /* w */
|
|
|
|
NULL, /* x */
|
|
|
|
NULL, /* y */
|
|
|
|
NULL /* z */
|
|
|
|
};
|
|
|
|
|
2017-04-20 09:20:22 +00:00
|
|
|
/* Format job update callback. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2017-04-20 09:20:22 +00:00
|
|
|
format_job_update(struct job *job)
|
|
|
|
{
|
2018-08-23 15:45:05 +00:00
|
|
|
struct format_job *fj = job_get_data(job);
|
|
|
|
struct evbuffer *evb = job_get_event(job)->input;
|
2018-01-18 14:28:11 +00:00
|
|
|
char *line = NULL, *next;
|
2017-04-20 09:20:22 +00:00
|
|
|
time_t t;
|
|
|
|
|
2018-01-18 14:28:11 +00:00
|
|
|
while ((next = evbuffer_readline(evb)) != NULL) {
|
|
|
|
free(line);
|
|
|
|
line = next;
|
|
|
|
}
|
|
|
|
if (line == NULL)
|
2017-04-20 09:20:22 +00:00
|
|
|
return;
|
|
|
|
fj->updated = 1;
|
|
|
|
|
|
|
|
free(fj->out);
|
|
|
|
fj->out = line;
|
|
|
|
|
2017-05-12 13:27:57 +00:00
|
|
|
log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
|
2017-04-20 09:20:22 +00:00
|
|
|
|
2017-05-31 17:56:48 +00:00
|
|
|
t = time(NULL);
|
2017-04-20 09:20:22 +00:00
|
|
|
if (fj->status && fj->last != t) {
|
2017-05-12 13:27:57 +00:00
|
|
|
if (fj->client != NULL)
|
|
|
|
server_status_client(fj->client);
|
2017-04-20 09:20:22 +00:00
|
|
|
fj->last = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Format job complete callback. */
|
|
|
|
static void
|
|
|
|
format_job_complete(struct job *job)
|
2015-05-27 13:28:04 +00:00
|
|
|
{
|
2018-08-23 15:45:05 +00:00
|
|
|
struct format_job *fj = job_get_data(job);
|
|
|
|
struct evbuffer *evb = job_get_event(job)->input;
|
2015-05-27 13:28:04 +00:00
|
|
|
char *line, *buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
fj->job = NULL;
|
|
|
|
|
|
|
|
buf = NULL;
|
2018-08-23 15:45:05 +00:00
|
|
|
if ((line = evbuffer_readline(evb)) == NULL) {
|
|
|
|
len = EVBUFFER_LENGTH(evb);
|
2015-05-27 13:28:04 +00:00
|
|
|
buf = xmalloc(len + 1);
|
|
|
|
if (len != 0)
|
2018-08-23 15:45:05 +00:00
|
|
|
memcpy(buf, EVBUFFER_DATA(evb), len);
|
2015-05-27 13:28:04 +00:00
|
|
|
buf[len] = '\0';
|
|
|
|
} else
|
|
|
|
buf = line;
|
2017-04-20 09:20:22 +00:00
|
|
|
|
2017-05-12 13:27:57 +00:00
|
|
|
log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
|
|
|
|
|
2017-04-20 09:20:22 +00:00
|
|
|
if (*buf != '\0' || !fj->updated) {
|
|
|
|
free(fj->out);
|
|
|
|
fj->out = buf;
|
|
|
|
} else
|
|
|
|
free(buf);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
if (fj->status) {
|
2017-05-01 12:20:55 +00:00
|
|
|
if (fj->client != NULL)
|
|
|
|
server_status_client(fj->client);
|
2015-05-27 13:28:04 +00:00
|
|
|
fj->status = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a job. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static char *
|
2015-05-27 13:28:04 +00:00
|
|
|
format_job_get(struct format_tree *ft, const char *cmd)
|
|
|
|
{
|
2017-05-01 12:20:55 +00:00
|
|
|
struct format_job_tree *jobs;
|
2016-11-17 10:06:08 +00:00
|
|
|
struct format_job fj0, *fj;
|
|
|
|
time_t t;
|
|
|
|
char *expanded;
|
|
|
|
int force;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
if (ft->client == NULL)
|
|
|
|
jobs = &format_jobs;
|
|
|
|
else if (ft->client->jobs != NULL)
|
|
|
|
jobs = ft->client->jobs;
|
|
|
|
else {
|
|
|
|
jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
|
|
|
|
RB_INIT(jobs);
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:57:27 +00:00
|
|
|
fj0.tag = ft->tag;
|
2015-05-27 13:28:04 +00:00
|
|
|
fj0.cmd = cmd;
|
2017-05-01 12:20:55 +00:00
|
|
|
if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
|
2015-05-27 13:28:04 +00:00
|
|
|
fj = xcalloc(1, sizeof *fj);
|
2017-05-01 12:20:55 +00:00
|
|
|
fj->client = ft->client;
|
2017-02-03 11:57:27 +00:00
|
|
|
fj->tag = ft->tag;
|
2015-05-27 13:28:04 +00:00
|
|
|
fj->cmd = xstrdup(cmd);
|
2016-11-17 10:06:08 +00:00
|
|
|
fj->expanded = NULL;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
|
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
RB_INSERT(format_job_tree, jobs, fj);
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 10:06:08 +00:00
|
|
|
expanded = format_expand(ft, cmd);
|
|
|
|
if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
|
|
|
|
free((void *)fj->expanded);
|
|
|
|
fj->expanded = xstrdup(expanded);
|
|
|
|
force = 1;
|
|
|
|
} else
|
|
|
|
force = (ft->flags & FORMAT_FORCE);
|
|
|
|
|
2015-09-14 10:25:52 +00:00
|
|
|
t = time(NULL);
|
2016-11-17 10:06:08 +00:00
|
|
|
if (fj->job == NULL && (force || fj->last != t)) {
|
2018-09-27 07:43:18 +00:00
|
|
|
fj->job = job_run(expanded, NULL,
|
|
|
|
server_client_get_cwd(ft->client, NULL), format_job_update,
|
2018-03-08 08:09:10 +00:00
|
|
|
format_job_complete, NULL, fj, JOB_NOWAIT);
|
2015-05-27 13:28:04 +00:00
|
|
|
if (fj->job == NULL) {
|
|
|
|
free(fj->out);
|
|
|
|
xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
|
|
|
|
}
|
2015-09-14 10:25:52 +00:00
|
|
|
fj->last = t;
|
2017-05-12 22:43:15 +00:00
|
|
|
fj->updated = 0;
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
2015-09-14 10:25:52 +00:00
|
|
|
|
|
|
|
if (ft->flags & FORMAT_STATUS)
|
|
|
|
fj->status = 1;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
2016-11-17 10:06:08 +00:00
|
|
|
free(expanded);
|
2015-10-25 08:59:26 +00:00
|
|
|
return (format_expand(ft, fj->out));
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove old jobs. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2017-05-01 12:20:55 +00:00
|
|
|
format_job_tidy(struct format_job_tree *jobs, int force)
|
2015-05-27 13:28:04 +00:00
|
|
|
{
|
|
|
|
struct format_job *fj, *fj1;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
now = time(NULL);
|
2017-05-01 12:20:55 +00:00
|
|
|
RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
|
|
|
|
if (!force && (fj->last > now || now - fj->last < 3600))
|
2015-05-27 13:28:04 +00:00
|
|
|
continue;
|
2017-05-01 12:20:55 +00:00
|
|
|
RB_REMOVE(format_job_tree, jobs, fj);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
2015-08-28 11:38:27 +00:00
|
|
|
log_debug("%s: %s", __func__, fj->cmd);
|
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
if (fj->job != NULL)
|
|
|
|
job_free(fj->job);
|
|
|
|
|
2016-11-17 10:06:08 +00:00
|
|
|
free((void *)fj->expanded);
|
2015-08-28 12:16:28 +00:00
|
|
|
free((void *)fj->cmd);
|
2015-05-27 13:28:04 +00:00
|
|
|
free(fj->out);
|
|
|
|
|
|
|
|
free(fj);
|
|
|
|
}
|
2017-05-01 12:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove old jobs for client. */
|
|
|
|
void
|
|
|
|
format_lost_client(struct client *c)
|
|
|
|
{
|
|
|
|
if (c->jobs != NULL)
|
|
|
|
format_job_tidy(c->jobs, 1);
|
|
|
|
free(c->jobs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove old jobs periodically. */
|
|
|
|
static void
|
|
|
|
format_job_timer(__unused int fd, __unused short events, __unused void *arg)
|
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
struct timeval tv = { .tv_sec = 60 };
|
|
|
|
|
|
|
|
format_job_tidy(&format_jobs, 0);
|
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->jobs != NULL)
|
|
|
|
format_job_tidy(c->jobs, 0);
|
|
|
|
}
|
2015-08-28 11:38:27 +00:00
|
|
|
|
|
|
|
evtimer_del(&format_job_event);
|
|
|
|
evtimer_add(&format_job_event, &tv);
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Callback for host. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
char host[HOST_NAME_MAX + 1];
|
|
|
|
|
|
|
|
if (gethostname(host, sizeof host) != 0)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
else
|
|
|
|
fe->value = xstrdup(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for host_short. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
char host[HOST_NAME_MAX + 1], *cp;
|
|
|
|
|
|
|
|
if (gethostname(host, sizeof host) != 0)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
else {
|
|
|
|
if ((cp = strchr(host, '.')) != NULL)
|
|
|
|
*cp = '\0';
|
|
|
|
fe->value = xstrdup(host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pid. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
xasprintf(&fe->value, "%ld", (long)getpid());
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for session_alerts. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 17:01:42 +00:00
|
|
|
format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct winlink *wl;
|
2017-05-05 11:59:47 +00:00
|
|
|
char alerts[1024], tmp[16];
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*alerts = '\0';
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
|
|
|
if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
|
|
|
|
continue;
|
|
|
|
xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
|
|
|
|
|
|
|
|
if (*alerts != '\0')
|
|
|
|
strlcat(alerts, ",", sizeof alerts);
|
|
|
|
strlcat(alerts, tmp, sizeof alerts);
|
|
|
|
if (wl->flags & WINLINK_ACTIVITY)
|
|
|
|
strlcat(alerts, "#", sizeof alerts);
|
|
|
|
if (wl->flags & WINLINK_BELL)
|
|
|
|
strlcat(alerts, "!", sizeof alerts);
|
|
|
|
if (wl->flags & WINLINK_SILENCE)
|
|
|
|
strlcat(alerts, "~", sizeof alerts);
|
|
|
|
}
|
|
|
|
fe->value = xstrdup(alerts);
|
|
|
|
}
|
|
|
|
|
2017-05-05 11:59:47 +00:00
|
|
|
/* Callback for session_stack. */
|
|
|
|
static void
|
|
|
|
format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct winlink *wl;
|
|
|
|
char result[1024], tmp[16];
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
xsnprintf(result, sizeof result, "%u", s->curw->idx);
|
|
|
|
TAILQ_FOREACH(wl, &s->lastw, sentry) {
|
|
|
|
xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
|
|
|
|
|
|
|
|
if (*result != '\0')
|
|
|
|
strlcat(result, ",", sizeof result);
|
|
|
|
strlcat(result, tmp, sizeof result);
|
|
|
|
}
|
|
|
|
fe->value = xstrdup(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_stack_index. */
|
|
|
|
static void
|
|
|
|
format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct session *s = ft->wl->session;
|
|
|
|
struct winlink *wl;
|
|
|
|
u_int idx;
|
|
|
|
|
|
|
|
idx = 0;
|
|
|
|
TAILQ_FOREACH(wl, &s->lastw, sentry) {
|
|
|
|
idx++;
|
|
|
|
if (wl == ft->wl)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (wl != NULL)
|
|
|
|
xasprintf(&fe->value, "%u", idx);
|
|
|
|
else
|
|
|
|
fe->value = xstrdup("0");
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for window_layout. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 17:01:42 +00:00
|
|
|
format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window *w = ft->w;
|
|
|
|
|
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (w->saved_layout_root != NULL)
|
|
|
|
fe->value = layout_dump(w->saved_layout_root);
|
|
|
|
else
|
|
|
|
fe->value = layout_dump(w->layout_root);
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:00:26 +00:00
|
|
|
/* Callback for window_visible_layout. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-13 10:00:26 +00:00
|
|
|
format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window *w = ft->w;
|
|
|
|
|
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fe->value = layout_dump(w->layout_root);
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Callback for pane_start_command. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 16:46:40 +00:00
|
|
|
format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fe->value = cmd_stringify_argv(wp->argc, wp->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_current_command. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 16:46:40 +00:00
|
|
|
format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cmd = get_proc_name(wp->fd, wp->tty);
|
|
|
|
if (cmd == NULL || *cmd == '\0') {
|
|
|
|
free(cmd);
|
|
|
|
cmd = cmd_stringify_argv(wp->argc, wp->argv);
|
|
|
|
if (cmd == NULL || *cmd == '\0') {
|
|
|
|
free(cmd);
|
|
|
|
cmd = xstrdup(wp->shell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fe->value = parse_window_name(cmd);
|
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for history_bytes. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 17:01:42 +00:00
|
|
|
format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct grid *gd;
|
|
|
|
struct grid_line *gl;
|
|
|
|
unsigned long long size;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
gd = wp->base.grid;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
for (i = 0; i < gd->hsize; i++) {
|
2018-07-04 09:44:07 +00:00
|
|
|
gl = grid_get_line(gd, i);
|
2015-08-28 17:01:42 +00:00
|
|
|
size += gl->cellsize * sizeof *gl->celldata;
|
2015-11-13 08:09:28 +00:00
|
|
|
size += gl->extdsize * sizeof *gl->extddata;
|
2015-08-28 17:01:42 +00:00
|
|
|
}
|
2018-07-04 09:44:07 +00:00
|
|
|
size += gd->hsize * sizeof *gl;
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
xasprintf(&fe->value, "%llu", size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_tabs. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 17:01:42 +00:00
|
|
|
format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
u_int i;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer = evbuffer_new();
|
2018-11-19 13:35:40 +00:00
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
2015-08-28 17:01:42 +00:00
|
|
|
for (i = 0; i < wp->base.grid->sx; i++) {
|
|
|
|
if (!bit_test(wp->base.tabs, i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%u", i);
|
|
|
|
}
|
2017-11-02 18:27:35 +00:00
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
|
|
|
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
|
|
|
|
evbuffer_free(buffer);
|
|
|
|
}
|
|
|
|
|
2017-11-02 18:52:05 +00:00
|
|
|
/* Callback for session_group_list. */
|
2017-11-02 18:27:35 +00:00
|
|
|
static void
|
2017-11-02 18:52:05 +00:00
|
|
|
format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
|
2017-11-02 18:27:35 +00:00
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct session_group *sg;
|
|
|
|
struct session *loop;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
sg = session_group_contains(s);
|
|
|
|
if (sg == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer = evbuffer_new();
|
2018-11-19 13:35:40 +00:00
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
2017-11-02 18:27:35 +00:00
|
|
|
TAILQ_FOREACH(loop, &sg->sessions, gentry) {
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%s", loop->name);
|
|
|
|
}
|
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
|
|
|
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2015-08-28 17:01:42 +00:00
|
|
|
evbuffer_free(buffer);
|
|
|
|
}
|
|
|
|
|
2016-10-16 19:36:37 +00:00
|
|
|
/* Merge a format tree. */
|
|
|
|
static void
|
|
|
|
format_merge(struct format_tree *ft, struct format_tree *from)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
|
|
|
|
|
|
|
RB_FOREACH(fe, format_entry_tree, &from->tree) {
|
|
|
|
if (fe->value != NULL)
|
|
|
|
format_add(ft, fe->key, "%s", fe->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Create a new tree. */
|
|
|
|
struct format_tree *
|
2017-05-01 12:20:55 +00:00
|
|
|
format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
|
|
|
struct format_tree *ft;
|
|
|
|
|
2015-08-28 11:38:27 +00:00
|
|
|
if (!event_initialized(&format_job_event)) {
|
|
|
|
evtimer_set(&format_job_event, format_job_timer, NULL);
|
|
|
|
format_job_timer(-1, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
ft = xcalloc(1, sizeof *ft);
|
|
|
|
RB_INIT(&ft->tree);
|
2017-02-03 11:57:27 +00:00
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
if (c != NULL) {
|
|
|
|
ft->client = c;
|
|
|
|
ft->client->references++;
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:57:27 +00:00
|
|
|
ft->tag = tag;
|
2015-09-14 10:25:52 +00:00
|
|
|
ft->flags = flags;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
format_add_cb(ft, "host", format_cb_host);
|
|
|
|
format_add_cb(ft, "host_short", format_cb_host_short);
|
|
|
|
format_add_cb(ft, "pid", format_cb_pid);
|
2015-11-24 21:52:06 +00:00
|
|
|
format_add(ft, "socket_path", "%s", socket_path);
|
|
|
|
format_add_tv(ft, "start_time", &start_time);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2017-04-21 14:01:19 +00:00
|
|
|
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);
|
|
|
|
}
|
2015-12-11 12:27:36 +00:00
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
return (ft);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free a tree. */
|
|
|
|
void
|
|
|
|
format_free(struct format_tree *ft)
|
|
|
|
{
|
2014-12-02 23:19:45 +00:00
|
|
|
struct format_entry *fe, *fe1;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
|
|
|
|
RB_REMOVE(format_entry_tree, &ft->tree, fe);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(fe->value);
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
if (ft->client != NULL)
|
|
|
|
server_client_unref(ft->client);
|
2013-05-31 19:46:42 +00:00
|
|
|
free(ft);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a key-value pair. */
|
|
|
|
void
|
|
|
|
format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
2013-10-10 11:47:52 +00:00
|
|
|
struct format_entry *fe_now;
|
2011-08-26 10:53:16 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fe = xmalloc(sizeof *fe);
|
|
|
|
fe->key = xstrdup(key);
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
|
|
|
|
if (fe_now != NULL) {
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
|
|
|
free(fe_now->value);
|
|
|
|
fe = fe_now;
|
|
|
|
}
|
|
|
|
|
|
|
|
fe->cb = NULL;
|
2015-10-25 22:29:17 +00:00
|
|
|
fe->t = 0;
|
2015-08-28 16:46:40 +00:00
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&fe->value, fmt, ap);
|
|
|
|
va_end(ap);
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
/* Add a key and time. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
|
|
|
struct format_entry *fe_now;
|
|
|
|
|
|
|
|
fe = xmalloc(sizeof *fe);
|
|
|
|
fe->key = xstrdup(key);
|
|
|
|
|
|
|
|
fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
|
|
|
|
if (fe_now != NULL) {
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
|
|
|
free(fe_now->value);
|
|
|
|
fe = fe_now;
|
|
|
|
}
|
|
|
|
|
|
|
|
fe->cb = NULL;
|
|
|
|
fe->t = tv->tv_sec;
|
|
|
|
|
|
|
|
fe->value = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Add a key and function. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 16:46:40 +00:00
|
|
|
format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
|
|
|
struct format_entry *fe_now;
|
|
|
|
|
|
|
|
fe = xmalloc(sizeof *fe);
|
|
|
|
fe->key = xstrdup(key);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
|
2013-10-10 11:47:52 +00:00
|
|
|
if (fe_now != NULL) {
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
2015-08-28 16:46:40 +00:00
|
|
|
free(fe_now->value);
|
|
|
|
fe = fe_now;
|
2013-10-10 11:47:52 +00:00
|
|
|
}
|
2015-08-28 16:46:40 +00:00
|
|
|
|
|
|
|
fe->cb = cb;
|
2015-10-25 22:29:17 +00:00
|
|
|
fe->t = 0;
|
2015-08-28 16:46:40 +00:00
|
|
|
|
|
|
|
fe->value = NULL;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 09:28:42 +00:00
|
|
|
/* Quote special characters in string. */
|
|
|
|
static char *
|
|
|
|
format_quote(const char *s)
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
char *out, *at;
|
|
|
|
|
|
|
|
at = out = xmalloc(strlen(s) * 2 + 1);
|
|
|
|
for (cp = s; *cp != '\0'; cp++) {
|
|
|
|
if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
|
|
|
|
*at++ = '\\';
|
|
|
|
*at++ = *cp;
|
|
|
|
}
|
|
|
|
*at = '\0';
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Find a format entry. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static char *
|
2015-10-25 22:29:17 +00:00
|
|
|
format_find(struct format_tree *ft, const char *key, int modifiers)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
|
|
|
struct format_entry *fe, fe_find;
|
2015-08-28 10:06:52 +00:00
|
|
|
struct environ_entry *envent;
|
2015-10-25 22:29:17 +00:00
|
|
|
static char s[64];
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2015-10-25 22:29:17 +00:00
|
|
|
const char *found;
|
2017-01-15 20:48:41 +00:00
|
|
|
int idx;
|
2015-10-25 22:29:17 +00:00
|
|
|
char *copy, *saved;
|
|
|
|
|
|
|
|
if (~modifiers & FORMAT_TIMESTRING) {
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(global_options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o == NULL && ft->w != NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(ft->w->options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o == NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(global_w_options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o == NULL && ft->s != NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(ft->s->options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o == NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(global_s_options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o != NULL) {
|
2017-01-30 21:41:17 +00:00
|
|
|
found = options_tostring(o, idx, 1);
|
2017-01-15 20:48:41 +00:00
|
|
|
goto found;
|
2014-12-02 23:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
found = NULL;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
|
|
|
fe_find.key = (char *) key;
|
2015-05-27 13:28:04 +00:00
|
|
|
fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
|
2015-08-28 16:46:40 +00:00
|
|
|
if (fe != NULL) {
|
2015-10-25 22:29:17 +00:00
|
|
|
if (modifiers & FORMAT_TIMESTRING) {
|
|
|
|
if (fe->t == 0)
|
|
|
|
return (NULL);
|
|
|
|
ctime_r(&fe->t, s);
|
|
|
|
s[strcspn(s, "\n")] = '\0';
|
|
|
|
found = s;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
if (fe->t != 0) {
|
|
|
|
xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
|
|
|
|
found = s;
|
|
|
|
goto found;
|
|
|
|
}
|
2017-11-02 18:43:51 +00:00
|
|
|
if (fe->value == NULL && fe->cb != NULL) {
|
2015-08-28 16:46:40 +00:00
|
|
|
fe->cb(ft, fe);
|
2017-11-02 18:43:51 +00:00
|
|
|
if (fe->value == NULL)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
}
|
2015-10-25 22:29:17 +00:00
|
|
|
found = fe->value;
|
|
|
|
goto found;
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
2015-08-28 10:06:52 +00:00
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
if (~modifiers & FORMAT_TIMESTRING) {
|
|
|
|
envent = NULL;
|
|
|
|
if (ft->s != NULL)
|
2015-10-28 09:51:55 +00:00
|
|
|
envent = environ_find(ft->s->environ, key);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (envent == NULL)
|
2015-10-28 09:51:55 +00:00
|
|
|
envent = environ_find(global_environ, key);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (envent != NULL) {
|
|
|
|
found = envent->value;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 10:06:52 +00:00
|
|
|
|
|
|
|
return (NULL);
|
2015-10-25 22:29:17 +00:00
|
|
|
|
|
|
|
found:
|
2015-10-27 09:18:06 +00:00
|
|
|
if (found == NULL)
|
|
|
|
return (NULL);
|
2015-10-25 22:29:17 +00:00
|
|
|
copy = xstrdup(found);
|
|
|
|
if (modifiers & FORMAT_BASENAME) {
|
|
|
|
saved = copy;
|
|
|
|
copy = xstrdup(basename(saved));
|
|
|
|
free(saved);
|
|
|
|
}
|
|
|
|
if (modifiers & FORMAT_DIRNAME) {
|
|
|
|
saved = copy;
|
|
|
|
copy = xstrdup(dirname(saved));
|
|
|
|
free(saved);
|
|
|
|
}
|
2018-08-26 09:28:42 +00:00
|
|
|
if (modifiers & FORMAT_QUOTE) {
|
|
|
|
saved = copy;
|
|
|
|
copy = xstrdup(format_quote(saved));
|
|
|
|
free(saved);
|
|
|
|
}
|
2015-10-25 22:29:17 +00:00
|
|
|
return (copy);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 08:49:12 +00:00
|
|
|
/* Skip until end. */
|
|
|
|
static const char *
|
|
|
|
format_skip(const char *s, char end)
|
2017-01-09 21:03:25 +00:00
|
|
|
{
|
|
|
|
int brackets = 0;
|
|
|
|
|
|
|
|
for (; *s != '\0'; s++) {
|
2018-05-22 08:49:12 +00:00
|
|
|
if (*s == '#' && s[1] == '{')
|
2017-01-09 21:03:25 +00:00
|
|
|
brackets++;
|
2018-05-22 08:49:12 +00:00
|
|
|
if (*s == '#' && strchr(",#{}", s[1]) != NULL) {
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-09 21:03:25 +00:00
|
|
|
if (*s == '}')
|
|
|
|
brackets--;
|
2018-05-22 08:49:12 +00:00
|
|
|
if (*s == end && brackets == 0)
|
2017-01-09 21:03:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*s == '\0')
|
|
|
|
return (NULL);
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return left and right alternatives separated by commas. */
|
|
|
|
static int
|
|
|
|
format_choose(char *s, char **left, char **right)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
2018-05-22 08:49:12 +00:00
|
|
|
cp = (char *)format_skip(s, ',');
|
2017-01-09 21:03:25 +00:00
|
|
|
if (cp == NULL)
|
|
|
|
return (-1);
|
|
|
|
*cp = '\0';
|
|
|
|
|
|
|
|
*left = s;
|
|
|
|
*right = cp + 1;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this true? */
|
Rewrite of choose mode, both to simplify and tidy the code and to add
some modern features.
Now the common code is in mode-tree.c, which provides an API used by the
three modes now separated into window-{buffer,client,tree}.c. Buffer
mode shows buffers, client mode clients and tree mode a tree of
sessions, windows and panes.
Each mode has a common set of key bindings plus a few that are specific
to the mode. Other changes are:
- each mode has a preview pane: for buffers this is the buffer content
(very useful), for others it is a preview of the pane;
- items may be sorted in different ways ('O' key);
- multiple items may be tagged and an operation applied to all of them
(for example, to delete multiple buffers at once);
- in tree mode a command may be run on the selected item (session,
window, pane) or on tagged items (key ':');
- displayed items may be filtered in tree mode by using a format (this
is used to implement find-window) (key 'f');
- the custom format (-F) for the display is no longer available;
- shortcut keys change from 0-9, a-z, A-Z which was always a bit weird
with keys used for other uses to 0-9, M-a to M-z.
Now that the code is simpler, other improvements will come later.
Primary key bindings for each mode are documented under the commands in
the man page (choose-buffer, choose-client, choose-tree).
Parts written by Thomas Adam.
2017-05-30 21:44:59 +00:00
|
|
|
int
|
2017-01-09 21:03:25 +00:00
|
|
|
format_true(const char *s)
|
|
|
|
{
|
|
|
|
if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2017-05-29 18:06:34 +00:00
|
|
|
/* Replace a key. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2013-10-10 11:50:20 +00:00
|
|
|
format_replace(struct format_tree *ft, const char *key, size_t keylen,
|
|
|
|
char **buf, size_t *len, size_t *off)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2017-05-29 18:06:34 +00:00
|
|
|
struct window_pane *wp = ft->wp;
|
2018-06-27 17:36:10 +00:00
|
|
|
char *copy, *copy0, *endptr, *ptr, *found, *new, sep;
|
2017-05-29 18:06:34 +00:00
|
|
|
char *value, *from = NULL, *to = NULL, *left, *right;
|
|
|
|
size_t valuelen, newlen, fromlen, tolen, used;
|
|
|
|
long limit = 0;
|
|
|
|
int modifiers = 0, compare = 0, search = 0;
|
2018-05-22 08:49:12 +00:00
|
|
|
int literal = 0;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
|
|
|
/* Make a copy of the key. */
|
2013-10-10 11:50:20 +00:00
|
|
|
copy0 = copy = xmalloc(keylen + 1);
|
2011-08-26 10:53:16 +00:00
|
|
|
memcpy(copy, key, keylen);
|
|
|
|
copy[keylen] = '\0';
|
|
|
|
|
2013-10-10 11:50:20 +00:00
|
|
|
/* Is there a length limit or whatnot? */
|
2015-10-25 22:29:17 +00:00
|
|
|
switch (copy[0]) {
|
2018-05-22 08:49:12 +00:00
|
|
|
case 'l':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
literal = 1;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
2017-05-29 15:43:48 +00:00
|
|
|
case 'm':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
compare = -2;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
2017-05-29 18:06:34 +00:00
|
|
|
case 'C':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
search = 1;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
if (copy[1] != '|' || copy[2] != ':')
|
|
|
|
break;
|
|
|
|
compare = -3;
|
|
|
|
copy += 3;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
if (copy[1] != '&' || copy[2] != ':')
|
|
|
|
break;
|
|
|
|
compare = -4;
|
|
|
|
copy += 3;
|
|
|
|
break;
|
2017-01-09 21:03:25 +00:00
|
|
|
case '!':
|
|
|
|
if (copy[1] == '=' && copy[2] == ':') {
|
|
|
|
compare = -1;
|
|
|
|
copy += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2015-10-25 22:29:17 +00:00
|
|
|
case '=':
|
2017-01-09 21:03:25 +00:00
|
|
|
if (copy[1] == '=' && copy[2] == ':') {
|
|
|
|
compare = 1;
|
|
|
|
copy += 3;
|
|
|
|
break;
|
|
|
|
}
|
2015-10-25 22:29:17 +00:00
|
|
|
errno = 0;
|
2016-01-31 09:54:46 +00:00
|
|
|
limit = strtol(copy + 1, &endptr, 10);
|
|
|
|
if (errno == ERANGE && (limit == LONG_MIN || limit == LONG_MAX))
|
2015-10-25 22:29:17 +00:00
|
|
|
break;
|
|
|
|
if (*endptr != ':')
|
|
|
|
break;
|
|
|
|
copy = endptr + 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
modifiers |= FORMAT_BASENAME;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
modifiers |= FORMAT_DIRNAME;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
modifiers |= FORMAT_TIMESTRING;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
2018-08-26 09:28:42 +00:00
|
|
|
case 'q':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
modifiers |= FORMAT_QUOTE;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
2015-11-18 14:13:55 +00:00
|
|
|
case 's':
|
2018-06-27 17:36:10 +00:00
|
|
|
sep = copy[1];
|
|
|
|
if (sep == ':' || !ispunct((u_char)sep))
|
2015-11-18 14:13:55 +00:00
|
|
|
break;
|
|
|
|
from = copy + 2;
|
2018-06-27 17:36:10 +00:00
|
|
|
for (copy = from; *copy != '\0' && *copy != sep; copy++)
|
2015-11-18 14:13:55 +00:00
|
|
|
/* nothing */;
|
2018-06-27 17:36:10 +00:00
|
|
|
if (copy[0] != sep || copy == from) {
|
2015-11-18 14:13:55 +00:00
|
|
|
copy = copy0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
copy[0] = '\0';
|
|
|
|
to = copy + 1;
|
2018-06-27 17:36:10 +00:00
|
|
|
for (copy = to; *copy != '\0' && *copy != sep; copy++)
|
2015-11-18 14:13:55 +00:00
|
|
|
/* nothing */;
|
2018-06-27 17:36:10 +00:00
|
|
|
if (copy[0] != sep || copy[1] != ':') {
|
2015-11-18 14:13:55 +00:00
|
|
|
copy = copy0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
copy[0] = '\0';
|
|
|
|
|
|
|
|
modifiers |= FORMAT_SUBSTITUTE;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
2013-10-10 11:50:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 08:49:12 +00:00
|
|
|
/* Is this a literal string? */
|
|
|
|
if (literal) {
|
|
|
|
value = xstrdup(copy);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2017-01-09 21:03:25 +00:00
|
|
|
/* Is this a comparison or a conditional? */
|
2017-05-29 18:06:34 +00:00
|
|
|
if (search) {
|
|
|
|
/* Search in pane. */
|
|
|
|
if (wp == NULL)
|
|
|
|
value = xstrdup("0");
|
|
|
|
else
|
|
|
|
xasprintf(&value, "%u", window_pane_search(wp, copy));
|
|
|
|
} else if (compare != 0) {
|
2017-01-09 21:03:25 +00:00
|
|
|
/* Comparison: compare comma-separated left and right. */
|
|
|
|
if (format_choose(copy, &left, &right) != 0)
|
|
|
|
goto fail;
|
|
|
|
left = format_expand(ft, left);
|
|
|
|
right = format_expand(ft, right);
|
2017-05-29 18:06:34 +00:00
|
|
|
if (compare == -3 &&
|
|
|
|
(format_true(left) || format_true(right)))
|
|
|
|
value = xstrdup("1");
|
|
|
|
else if (compare == -4 &&
|
|
|
|
(format_true(left) && format_true(right)))
|
|
|
|
value = xstrdup("1");
|
|
|
|
else if (compare == 1 && strcmp(left, right) == 0)
|
2017-01-09 21:03:25 +00:00
|
|
|
value = xstrdup("1");
|
|
|
|
else if (compare == -1 && strcmp(left, right) != 0)
|
|
|
|
value = xstrdup("1");
|
2017-05-29 15:43:48 +00:00
|
|
|
else if (compare == -2 && fnmatch(left, right, 0) == 0)
|
|
|
|
value = xstrdup("1");
|
2017-01-09 21:03:25 +00:00
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
|
|
|
free(right);
|
|
|
|
free(left);
|
|
|
|
} else if (*copy == '?') {
|
|
|
|
/* Conditional: check first and choose second or third. */
|
2018-05-22 08:49:12 +00:00
|
|
|
ptr = (char *)format_skip(copy, ',');
|
2011-08-26 10:53:16 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
goto fail;
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2015-11-18 14:13:55 +00:00
|
|
|
found = format_find(ft, copy + 1, modifiers);
|
2018-05-29 09:10:30 +00:00
|
|
|
if (found == NULL) {
|
|
|
|
/*
|
|
|
|
* If the conditional not found, try to expand it. If
|
|
|
|
* the expansion doesn't have any effect, then assume
|
|
|
|
* false.
|
|
|
|
*/
|
2017-02-07 18:06:42 +00:00
|
|
|
found = format_expand(ft, copy + 1);
|
2018-05-29 09:10:30 +00:00
|
|
|
if (strcmp(found, copy + 1) == 0) {
|
|
|
|
free(found);
|
|
|
|
found = xstrdup("");
|
|
|
|
}
|
|
|
|
}
|
2018-08-27 11:03:34 +00:00
|
|
|
if (format_choose(ptr + 1, &left, &right) != 0) {
|
|
|
|
free(found);
|
2015-10-27 09:28:31 +00:00
|
|
|
goto fail;
|
2018-08-27 11:03:34 +00:00
|
|
|
}
|
2015-10-27 09:28:31 +00:00
|
|
|
|
2017-01-09 21:03:25 +00:00
|
|
|
if (format_true(found))
|
|
|
|
value = format_expand(ft, left);
|
|
|
|
else
|
|
|
|
value = format_expand(ft, right);
|
2015-11-18 14:13:55 +00:00
|
|
|
free(found);
|
2011-08-26 10:53:16 +00:00
|
|
|
} else {
|
2017-01-09 21:03:25 +00:00
|
|
|
/* Neither: look up directly. */
|
2015-11-18 14:13:55 +00:00
|
|
|
value = format_find(ft, copy, modifiers);
|
2011-08-26 10:53:16 +00:00
|
|
|
if (value == NULL)
|
2015-11-18 14:13:55 +00:00
|
|
|
value = xstrdup("");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform substitution if any. */
|
|
|
|
if (modifiers & FORMAT_SUBSTITUTE) {
|
|
|
|
fromlen = strlen(from);
|
|
|
|
tolen = strlen(to);
|
|
|
|
|
|
|
|
newlen = strlen(value) + 1;
|
|
|
|
copy = new = xmalloc(newlen);
|
|
|
|
for (ptr = value; *ptr != '\0'; /* nothing */) {
|
|
|
|
if (strncmp(ptr, from, fromlen) != 0) {
|
|
|
|
*new++ = *ptr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
used = new - copy;
|
|
|
|
|
|
|
|
newlen += tolen;
|
|
|
|
copy = xrealloc(copy, newlen);
|
|
|
|
|
|
|
|
new = copy + used;
|
|
|
|
memcpy(new, to, tolen);
|
|
|
|
|
|
|
|
new += tolen;
|
|
|
|
ptr += fromlen;
|
|
|
|
}
|
|
|
|
*new = '\0';
|
|
|
|
free(value);
|
|
|
|
value = copy;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 11:50:20 +00:00
|
|
|
/* Truncate the value if needed. */
|
2016-01-31 09:54:46 +00:00
|
|
|
if (limit > 0) {
|
2015-11-18 14:13:55 +00:00
|
|
|
new = utf8_trimcstr(value, limit);
|
|
|
|
free(value);
|
|
|
|
value = new;
|
2016-01-31 09:54:46 +00:00
|
|
|
} else if (limit < 0) {
|
|
|
|
new = utf8_rtrimcstr(value, -limit);
|
|
|
|
free(value);
|
|
|
|
value = new;
|
2014-04-17 15:37:55 +00:00
|
|
|
}
|
2013-10-10 11:50:20 +00:00
|
|
|
|
2018-05-22 08:49:12 +00:00
|
|
|
done:
|
2018-05-29 09:10:30 +00:00
|
|
|
/* Expand the buffer and copy in the value. */
|
2015-11-18 14:13:55 +00:00
|
|
|
valuelen = strlen(value);
|
2011-08-26 10:53:16 +00:00
|
|
|
while (*len - *off < valuelen + 1) {
|
2014-10-08 17:35:58 +00:00
|
|
|
*buf = xreallocarray(*buf, 2, *len);
|
2011-08-26 10:53:16 +00:00
|
|
|
*len *= 2;
|
|
|
|
}
|
|
|
|
memcpy(*buf + *off, value, valuelen);
|
|
|
|
*off += valuelen;
|
|
|
|
|
2015-11-18 14:13:55 +00:00
|
|
|
free(value);
|
2013-10-10 11:50:20 +00:00
|
|
|
free(copy0);
|
2011-08-26 10:53:16 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
fail:
|
2013-10-10 11:50:20 +00:00
|
|
|
free(copy0);
|
2011-08-26 10:53:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2015-02-06 17:11:39 +00:00
|
|
|
/* Expand keys in a template, passing through strftime first. */
|
|
|
|
char *
|
|
|
|
format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
|
|
|
|
{
|
|
|
|
struct tm *tm;
|
2016-05-23 20:39:26 +00:00
|
|
|
char s[2048];
|
2015-02-06 17:11:39 +00:00
|
|
|
|
2015-05-20 06:39:02 +00:00
|
|
|
if (fmt == NULL || *fmt == '\0')
|
2015-02-06 17:11:39 +00:00
|
|
|
return (xstrdup(""));
|
|
|
|
|
|
|
|
tm = localtime(&t);
|
|
|
|
|
2016-05-23 20:39:26 +00:00
|
|
|
if (strftime(s, sizeof s, fmt, tm) == 0)
|
|
|
|
return (xstrdup(""));
|
2015-02-06 17:11:39 +00:00
|
|
|
|
2016-05-23 20:39:26 +00:00
|
|
|
return (format_expand(ft, s));
|
2015-02-06 17:11:39 +00:00
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Expand keys in a template. */
|
|
|
|
char *
|
|
|
|
format_expand(struct format_tree *ft, const char *fmt)
|
|
|
|
{
|
2018-02-20 10:43:46 +00:00
|
|
|
char *buf, *out, *name;
|
2015-08-28 16:46:40 +00:00
|
|
|
const char *ptr, *s, *saved = fmt;
|
2015-10-25 08:59:26 +00:00
|
|
|
size_t off, len, n, outlen;
|
2013-10-10 11:50:36 +00:00
|
|
|
int ch, brackets;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-02-06 17:11:39 +00:00
|
|
|
if (fmt == NULL)
|
|
|
|
return (xstrdup(""));
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
len = 64;
|
|
|
|
buf = xmalloc(len);
|
|
|
|
off = 0;
|
|
|
|
|
|
|
|
while (*fmt != '\0') {
|
|
|
|
if (*fmt != '#') {
|
|
|
|
while (len - off < 2) {
|
2014-10-08 17:35:58 +00:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2011-08-26 10:53:16 +00:00
|
|
|
len *= 2;
|
|
|
|
}
|
|
|
|
buf[off++] = *fmt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
ch = (u_char) *fmt++;
|
|
|
|
switch (ch) {
|
2015-05-27 13:28:04 +00:00
|
|
|
case '(':
|
|
|
|
brackets = 1;
|
|
|
|
for (ptr = fmt; *ptr != '\0'; ptr++) {
|
|
|
|
if (*ptr == '(')
|
|
|
|
brackets++;
|
|
|
|
if (*ptr == ')' && --brackets == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*ptr != ')' || brackets != 0)
|
|
|
|
break;
|
|
|
|
n = ptr - fmt;
|
|
|
|
|
2017-01-09 21:03:25 +00:00
|
|
|
if (ft->flags & FORMAT_NOJOBS)
|
|
|
|
out = xstrdup("");
|
2018-02-20 10:43:46 +00:00
|
|
|
else {
|
|
|
|
name = xstrndup(fmt, n);
|
|
|
|
out = format_job_get(ft, name);
|
|
|
|
free(name);
|
|
|
|
}
|
2015-10-25 08:59:26 +00:00
|
|
|
outlen = strlen(out);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
2015-10-25 08:59:26 +00:00
|
|
|
while (len - off < outlen + 1) {
|
2015-05-27 13:28:04 +00:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
|
|
|
len *= 2;
|
|
|
|
}
|
2015-10-25 08:59:26 +00:00
|
|
|
memcpy(buf + off, out, outlen);
|
|
|
|
off += outlen;
|
|
|
|
|
|
|
|
free(out);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
fmt += n + 1;
|
|
|
|
continue;
|
2011-08-26 10:53:16 +00:00
|
|
|
case '{':
|
2018-05-22 08:49:12 +00:00
|
|
|
ptr = format_skip(fmt - 2, '}');
|
|
|
|
if (ptr == NULL)
|
2011-08-26 10:53:16 +00:00
|
|
|
break;
|
|
|
|
n = ptr - fmt;
|
|
|
|
|
|
|
|
if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
|
|
|
|
break;
|
|
|
|
fmt += n + 1;
|
|
|
|
continue;
|
2018-05-22 08:49:12 +00:00
|
|
|
case '}':
|
2013-11-24 11:29:09 +00:00
|
|
|
case '#':
|
2018-05-22 08:49:12 +00:00
|
|
|
case ',':
|
2013-11-24 11:29:09 +00:00
|
|
|
while (len - off < 2) {
|
2014-10-08 17:35:58 +00:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2013-11-24 11:29:09 +00:00
|
|
|
len *= 2;
|
|
|
|
}
|
2018-05-22 08:49:12 +00:00
|
|
|
buf[off++] = ch;
|
2013-11-24 11:29:09 +00:00
|
|
|
continue;
|
2011-08-26 10:53:16 +00:00
|
|
|
default:
|
2013-05-31 19:46:42 +00:00
|
|
|
s = NULL;
|
|
|
|
if (ch >= 'A' && ch <= 'Z')
|
|
|
|
s = format_upper[ch - 'A'];
|
|
|
|
else if (ch >= 'a' && ch <= 'z')
|
|
|
|
s = format_lower[ch - 'a'];
|
|
|
|
if (s == NULL) {
|
|
|
|
while (len - off < 3) {
|
2014-10-08 17:35:58 +00:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2013-05-31 19:46:42 +00:00
|
|
|
len *= 2;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
2013-05-31 19:46:42 +00:00
|
|
|
buf[off++] = '#';
|
|
|
|
buf[off++] = ch;
|
|
|
|
continue;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
2013-05-31 19:46:42 +00:00
|
|
|
n = strlen(s);
|
|
|
|
if (format_replace(ft, s, n, &buf, &len, &off) != 0)
|
|
|
|
break;
|
2011-08-26 10:53:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[off] = '\0';
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
log_debug("format '%s' -> '%s'", saved, buf);
|
2011-08-26 10:53:16 +00:00
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
2017-03-08 13:36:12 +00:00
|
|
|
/* Expand a single string. */
|
|
|
|
char *
|
|
|
|
format_single(struct cmdq_item *item, const char *fmt, struct client *c,
|
|
|
|
struct session *s, struct winlink *wl, struct window_pane *wp)
|
|
|
|
{
|
|
|
|
struct format_tree *ft;
|
|
|
|
char *expanded;
|
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
if (item != NULL)
|
|
|
|
ft = format_create(item->client, item, FORMAT_NONE, 0);
|
|
|
|
else
|
|
|
|
ft = format_create(NULL, item, FORMAT_NONE, 0);
|
2017-03-08 13:36:12 +00:00
|
|
|
format_defaults(ft, c, s, wl, wp);
|
|
|
|
|
|
|
|
expanded = format_expand(ft, fmt);
|
|
|
|
format_free(ft);
|
|
|
|
return (expanded);
|
|
|
|
}
|
|
|
|
|
2015-02-05 10:29:43 +00:00
|
|
|
/* Set defaults for any of arguments that are not NULL. */
|
|
|
|
void
|
|
|
|
format_defaults(struct format_tree *ft, struct client *c, struct session *s,
|
|
|
|
struct winlink *wl, struct window_pane *wp)
|
|
|
|
{
|
2018-04-18 14:35:37 +00:00
|
|
|
if (c != NULL && s != NULL && c->session != s)
|
|
|
|
log_debug("%s: session does not match", __func__);
|
|
|
|
|
2017-08-09 11:43:45 +00:00
|
|
|
format_add(ft, "session_format", "%d", s != NULL);
|
|
|
|
format_add(ft, "window_format", "%d", wl != NULL);
|
|
|
|
format_add(ft, "pane_format", "%d", wp != NULL);
|
|
|
|
|
2015-02-05 10:29:43 +00:00
|
|
|
if (s == NULL && c != NULL)
|
|
|
|
s = c->session;
|
|
|
|
if (wl == NULL && s != NULL)
|
|
|
|
wl = s->curw;
|
|
|
|
if (wp == NULL && wl != NULL)
|
|
|
|
wp = wl->window->active;
|
|
|
|
|
|
|
|
if (c != NULL)
|
|
|
|
format_defaults_client(ft, c);
|
|
|
|
if (s != NULL)
|
|
|
|
format_defaults_session(ft, s);
|
2017-04-20 09:43:45 +00:00
|
|
|
if (wl != NULL)
|
|
|
|
format_defaults_winlink(ft, wl);
|
2015-02-05 10:29:43 +00:00
|
|
|
if (wp != NULL)
|
|
|
|
format_defaults_pane(ft, wp);
|
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Set default format keys for a session. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_session(struct format_tree *ft, struct session *s)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
ft->s = s;
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
format_add(ft, "session_name", "%s", s->name);
|
|
|
|
format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
|
2013-03-25 11:40:54 +00:00
|
|
|
format_add(ft, "session_id", "$%u", s->id);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2017-02-09 15:04:53 +00:00
|
|
|
sg = session_group_contains(s);
|
2011-08-26 10:53:16 +00:00
|
|
|
format_add(ft, "session_grouped", "%d", sg != NULL);
|
2017-11-02 18:27:35 +00:00
|
|
|
if (sg != NULL) {
|
2017-02-09 15:04:53 +00:00
|
|
|
format_add(ft, "session_group", "%s", sg->name);
|
2017-11-02 18:27:35 +00:00
|
|
|
format_add(ft, "session_group_size", "%u",
|
|
|
|
session_group_count (sg));
|
2017-11-02 18:52:05 +00:00
|
|
|
format_add_cb(ft, "session_group_list",
|
|
|
|
format_cb_session_group_list);
|
2017-11-02 18:27:35 +00:00
|
|
|
}
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(ft, "session_created", &s->creation_time);
|
|
|
|
format_add_tv(ft, "session_last_attached", &s->last_attached_time);
|
|
|
|
format_add_tv(ft, "session_activity", &s->activity_time);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2014-03-31 21:37:55 +00:00
|
|
|
format_add(ft, "session_attached", "%u", s->attached);
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "session_many_attached", "%d", s->attached > 1);
|
2015-05-12 15:29:29 +00:00
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
format_add_cb(ft, "session_alerts", format_cb_session_alerts);
|
2017-05-05 11:59:47 +00:00
|
|
|
format_add_cb(ft, "session_stack", format_cb_session_stack);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2011-10-23 01:12:46 +00:00
|
|
|
/* Set default format keys for a client. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_client(struct format_tree *ft, struct client *c)
|
2011-10-23 01:12:46 +00:00
|
|
|
{
|
2013-03-22 10:32:36 +00:00
|
|
|
struct session *s;
|
2015-12-12 18:32:24 +00:00
|
|
|
const char *name;
|
2017-01-11 16:09:57 +00:00
|
|
|
struct tty *tty = &c->tty;
|
|
|
|
const char *types[] = TTY_TYPES;
|
2011-10-23 01:12:46 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->s == NULL)
|
|
|
|
ft->s = c->session;
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
ft->c = c;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2017-04-05 10:49:46 +00:00
|
|
|
format_add(ft, "client_name", "%s", c->name);
|
2015-06-14 10:07:44 +00:00
|
|
|
format_add(ft, "client_pid", "%ld", (long) c->pid);
|
2017-01-11 16:09:57 +00:00
|
|
|
format_add(ft, "client_height", "%u", tty->sy);
|
|
|
|
format_add(ft, "client_width", "%u", tty->sx);
|
2017-04-05 10:49:46 +00:00
|
|
|
format_add(ft, "client_tty", "%s", c->ttyname);
|
2015-07-13 15:37:26 +00:00
|
|
|
format_add(ft, "client_control_mode", "%d",
|
|
|
|
!!(c->flags & CLIENT_CONTROL));
|
2011-10-23 01:12:46 +00:00
|
|
|
|
2017-01-11 16:09:57 +00:00
|
|
|
if (tty->term_name != NULL)
|
|
|
|
format_add(ft, "client_termname", "%s", tty->term_name);
|
|
|
|
if (tty->term_name != NULL)
|
|
|
|
format_add(ft, "client_termtype", "%s", types[tty->term_type]);
|
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(ft, "client_created", &c->creation_time);
|
|
|
|
format_add_tv(ft, "client_activity", &c->activity_time);
|
2017-04-19 06:52:27 +00:00
|
|
|
|
|
|
|
format_add(ft, "client_written", "%zu", c->written);
|
|
|
|
format_add(ft, "client_discarded", "%zu", c->discarded);
|
2011-10-23 01:12:46 +00:00
|
|
|
|
2015-12-12 18:32:24 +00:00
|
|
|
name = server_client_get_key_table(c);
|
|
|
|
if (strcmp(c->keytable->name, name) == 0)
|
2015-04-20 15:34:56 +00:00
|
|
|
format_add(ft, "client_prefix", "%d", 0);
|
|
|
|
else
|
|
|
|
format_add(ft, "client_prefix", "%d", 1);
|
|
|
|
format_add(ft, "client_key_table", "%s", c->keytable->name);
|
2013-03-21 16:14:09 +00:00
|
|
|
|
2017-01-11 16:09:57 +00:00
|
|
|
if (tty->flags & TTY_UTF8)
|
2011-10-23 01:12:46 +00:00
|
|
|
format_add(ft, "client_utf8", "%d", 1);
|
|
|
|
else
|
|
|
|
format_add(ft, "client_utf8", "%d", 0);
|
|
|
|
|
|
|
|
if (c->flags & CLIENT_READONLY)
|
|
|
|
format_add(ft, "client_readonly", "%d", 1);
|
|
|
|
else
|
|
|
|
format_add(ft, "client_readonly", "%d", 0);
|
2013-03-22 10:32:36 +00:00
|
|
|
|
|
|
|
s = c->session;
|
|
|
|
if (s != NULL)
|
|
|
|
format_add(ft, "client_session", "%s", s->name);
|
|
|
|
s = c->last_session;
|
|
|
|
if (s != NULL && session_alive(s))
|
|
|
|
format_add(ft, "client_last_session", "%s", s->name);
|
2011-10-23 01:12:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 12:04:01 +00:00
|
|
|
/* Set default format keys for a window. */
|
2011-08-26 10:53:16 +00:00
|
|
|
void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_window(struct format_tree *ft, struct window *w)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2014-12-02 23:19:45 +00:00
|
|
|
ft->w = w;
|
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(ft, "window_activity", &w->activity_time);
|
2012-01-30 09:39:34 +00:00
|
|
|
format_add(ft, "window_id", "@%u", w->id);
|
2011-08-26 10:53:16 +00:00
|
|
|
format_add(ft, "window_name", "%s", w->name);
|
|
|
|
format_add(ft, "window_width", "%u", w->sx);
|
|
|
|
format_add(ft, "window_height", "%u", w->sy);
|
2015-08-28 17:01:42 +00:00
|
|
|
format_add_cb(ft, "window_layout", format_cb_window_layout);
|
2015-11-13 10:00:26 +00:00
|
|
|
format_add_cb(ft, "window_visible_layout",
|
|
|
|
format_cb_window_visible_layout);
|
2012-05-22 11:35:37 +00:00
|
|
|
format_add(ft, "window_panes", "%u", window_count_panes(w));
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_zoomed_flag", "%d",
|
2014-12-01 14:30:18 +00:00
|
|
|
!!(w->flags & WINDOW_ZOOMED));
|
2013-10-10 12:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default format keys for a winlink. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2017-04-20 09:43:45 +00:00
|
|
|
format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
|
2013-10-10 12:04:01 +00:00
|
|
|
{
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
struct client *c = ft->c;
|
2017-04-20 09:43:45 +00:00
|
|
|
struct session *s = wl->session;
|
2013-10-10 12:04:01 +00:00
|
|
|
struct window *w = wl->window;
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
int flag;
|
|
|
|
u_int ox, oy, sx, sy;
|
2013-10-10 12:04:01 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->w == NULL)
|
|
|
|
ft->w = wl->window;
|
2017-05-05 11:59:47 +00:00
|
|
|
ft->wl = wl;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_window(ft, w);
|
2013-10-10 12:04:01 +00:00
|
|
|
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
if (c != NULL) {
|
|
|
|
flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
|
|
|
|
format_add(ft, "window_bigger", "%d", flag);
|
|
|
|
if (flag) {
|
|
|
|
format_add(ft, "window_offset_x", "%u", ox);
|
|
|
|
format_add(ft, "window_offset_y", "%u", oy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 12:04:01 +00:00
|
|
|
format_add(ft, "window_index", "%d", wl->idx);
|
2017-05-05 11:59:47 +00:00
|
|
|
format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
|
2017-04-20 09:43:45 +00:00
|
|
|
format_add(ft, "window_flags", "%s", window_printable_flags(wl));
|
2013-10-10 12:04:01 +00:00
|
|
|
format_add(ft, "window_active", "%d", wl == s->curw);
|
|
|
|
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_bell_flag", "%d",
|
2013-10-10 11:47:52 +00:00
|
|
|
!!(wl->flags & WINLINK_BELL));
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_activity_flag", "%d",
|
2013-10-10 11:47:52 +00:00
|
|
|
!!(wl->flags & WINLINK_ACTIVITY));
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_silence_flag", "%d",
|
2013-10-10 11:47:52 +00:00
|
|
|
!!(wl->flags & WINLINK_SILENCE));
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_last_flag", "%d",
|
2014-09-08 14:29:05 +00:00
|
|
|
!!(wl == TAILQ_FIRST(&s->lastw)));
|
2015-05-06 08:35:39 +00:00
|
|
|
format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default format keys for a window pane. */
|
|
|
|
void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2018-08-18 16:14:03 +00:00
|
|
|
struct window *w = wp->window;
|
2015-08-28 17:01:42 +00:00
|
|
|
struct grid *gd = wp->base.grid;
|
2017-10-12 11:32:27 +00:00
|
|
|
int status = wp->status;
|
2015-08-28 17:01:42 +00:00
|
|
|
u_int idx;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->w == NULL)
|
2018-08-18 16:14:03 +00:00
|
|
|
ft->w = w;
|
2015-08-28 16:46:40 +00:00
|
|
|
ft->wp = wp;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2013-03-22 16:03:35 +00:00
|
|
|
format_add(ft, "history_size", "%u", gd->hsize);
|
|
|
|
format_add(ft, "history_limit", "%u", gd->hlimit);
|
2015-08-28 17:01:42 +00:00
|
|
|
format_add_cb(ft, "history_bytes", format_cb_history_bytes);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2011-11-15 23:21:52 +00:00
|
|
|
if (window_pane_index(wp, &idx) != 0)
|
|
|
|
fatalx("index not found");
|
2013-03-22 16:03:35 +00:00
|
|
|
format_add(ft, "pane_index", "%u", idx);
|
2011-11-15 23:21:52 +00:00
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
format_add(ft, "pane_width", "%u", wp->sx);
|
|
|
|
format_add(ft, "pane_height", "%u", wp->sy);
|
|
|
|
format_add(ft, "pane_title", "%s", wp->base.title);
|
|
|
|
format_add(ft, "pane_id", "%%%u", wp->id);
|
2018-08-18 16:14:03 +00:00
|
|
|
format_add(ft, "pane_active", "%d", wp == w->active);
|
2014-10-25 08:47:04 +00:00
|
|
|
format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
|
2017-07-07 14:39:45 +00:00
|
|
|
format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
|
2013-03-22 16:03:35 +00:00
|
|
|
|
2017-10-12 11:32:27 +00:00
|
|
|
if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
|
2014-12-09 19:23:35 +00:00
|
|
|
format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
|
|
|
|
format_add(ft, "pane_dead", "%d", wp->fd == -1);
|
|
|
|
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
format_add(ft, "pane_left", "%u", wp->xoff);
|
|
|
|
format_add(ft, "pane_top", "%u", wp->yoff);
|
|
|
|
format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
|
|
|
|
format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
|
|
|
|
format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
|
|
|
|
format_add(ft, "pane_at_top", "%d", wp->yoff == 0);
|
|
|
|
format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
|
|
|
|
format_add(ft, "pane_at_bottom", "%d", wp->yoff + wp->sy == w->sy);
|
2014-05-27 12:49:36 +00:00
|
|
|
|
2013-03-22 16:03:35 +00:00
|
|
|
format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
|
2017-05-07 22:27:57 +00:00
|
|
|
if (wp->mode != NULL)
|
|
|
|
format_add(ft, "pane_mode", "%s", wp->mode->name);
|
|
|
|
|
2013-07-05 15:27:14 +00:00
|
|
|
format_add(ft, "pane_synchronized", "%d",
|
2018-08-18 16:14:03 +00:00
|
|
|
!!options_get_number(w->options, "synchronize-panes"));
|
2017-05-12 10:45:38 +00:00
|
|
|
if (wp->searchstr != NULL)
|
|
|
|
format_add(ft, "pane_search_string", "%s", wp->searchstr);
|
2013-03-22 16:03:35 +00:00
|
|
|
|
2015-06-10 12:56:04 +00:00
|
|
|
format_add(ft, "pane_tty", "%s", wp->tty);
|
2013-03-22 16:03:35 +00:00
|
|
|
format_add(ft, "pane_pid", "%ld", (long) wp->pid);
|
2015-08-28 16:46:40 +00:00
|
|
|
format_add_cb(ft, "pane_start_command", format_cb_start_command);
|
|
|
|
format_add_cb(ft, "pane_current_command", format_cb_current_command);
|
2013-03-22 16:03:35 +00:00
|
|
|
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "cursor_x", "%u", wp->base.cx);
|
|
|
|
format_add(ft, "cursor_y", "%u", wp->base.cy);
|
|
|
|
format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
|
|
|
|
format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
|
2013-03-22 16:03:35 +00:00
|
|
|
|
2017-08-02 11:10:48 +00:00
|
|
|
window_copy_add_formats(wp, ft);
|
2015-10-23 16:02:21 +00:00
|
|
|
|
2013-03-22 16:03:35 +00:00
|
|
|
format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
|
|
|
|
format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
|
2013-03-22 16:03:35 +00:00
|
|
|
|
|
|
|
format_add(ft, "cursor_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_CURSOR));
|
|
|
|
format_add(ft, "insert_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_INSERT));
|
|
|
|
format_add(ft, "keypad_cursor_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_KCURSOR));
|
|
|
|
format_add(ft, "keypad_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_KKEYPAD));
|
|
|
|
format_add(ft, "wrap_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_WRAP));
|
|
|
|
|
2015-04-21 15:18:38 +00:00
|
|
|
format_add(ft, "mouse_any_flag", "%d",
|
2017-02-01 09:55:07 +00:00
|
|
|
!!(wp->base.mode & ALL_MOUSE_MODES));
|
2013-03-22 16:03:35 +00:00
|
|
|
format_add(ft, "mouse_standard_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_MOUSE_STANDARD));
|
|
|
|
format_add(ft, "mouse_button_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_MOUSE_BUTTON));
|
2017-02-01 09:55:07 +00:00
|
|
|
format_add(ft, "mouse_all_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_MOUSE_ALL));
|
2013-03-24 09:29:40 +00:00
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2013-03-24 09:29:40 +00:00
|
|
|
/* Set default format keys for paste buffer. */
|
2012-05-22 11:35:37 +00:00
|
|
|
void
|
2015-11-12 11:09:11 +00:00
|
|
|
format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
|
2012-05-22 11:35:37 +00:00
|
|
|
{
|
2017-08-09 11:43:45 +00:00
|
|
|
struct timeval tv;
|
|
|
|
size_t size;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
timerclear(&tv);
|
|
|
|
tv.tv_sec = paste_buffer_created(pb);
|
|
|
|
paste_buffer_data(pb, &size);
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2017-08-09 11:43:45 +00:00
|
|
|
format_add(ft, "buffer_size", "%zu", size);
|
2015-08-29 09:25:00 +00:00
|
|
|
format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
|
2017-08-09 11:43:45 +00:00
|
|
|
format_add_tv(ft, "buffer_created", &tv);
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2015-11-12 11:09:11 +00:00
|
|
|
s = paste_make_sample(pb);
|
2014-04-02 18:12:18 +00:00
|
|
|
format_add(ft, "buffer_sample", "%s", s);
|
|
|
|
free(s);
|
2012-05-22 11:35:37 +00:00
|
|
|
}
|