2011-07-03 23:52:50 +02:00
|
|
|
/* $Id$ */
|
2007-10-01 16:53:29 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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>
|
2008-08-28 19:45:30 +02:00
|
|
|
#include <sys/time.h>
|
2007-10-01 16:53:29 +02:00
|
|
|
|
2008-12-05 21:04:06 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2007-10-01 16:53:29 +02:00
|
|
|
#include <stdarg.h>
|
2008-12-05 21:04:06 +01:00
|
|
|
#include <stdlib.h>
|
2008-06-04 07:40:35 +02:00
|
|
|
#include <string.h>
|
2008-09-25 22:33:53 +02:00
|
|
|
#include <time.h>
|
2009-01-16 00:42:21 +01:00
|
|
|
#include <unistd.h>
|
2007-10-01 16:53:29 +02:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
char *status_redraw_get_left(
|
|
|
|
struct client *, time_t, int, struct grid_cell *, size_t *);
|
|
|
|
char *status_redraw_get_right(
|
|
|
|
struct client *, time_t, int, struct grid_cell *, size_t *);
|
2011-02-15 16:20:03 +01:00
|
|
|
char *status_find_job(struct client *, char **);
|
|
|
|
void status_job_free(void *);
|
2009-10-12 01:55:26 +02:00
|
|
|
void status_job_callback(struct job *);
|
2009-11-19 23:35:10 +01:00
|
|
|
char *status_print(
|
2009-12-04 23:14:47 +01:00
|
|
|
struct client *, struct winlink *, time_t, struct grid_cell *);
|
2011-04-07 00:21:02 +02:00
|
|
|
void status_replace1(struct client *, struct session *, struct winlink *,
|
|
|
|
struct window_pane *, char **, char **, char *, size_t, int);
|
2009-11-09 00:11:23 +01:00
|
|
|
void status_message_callback(int, short, void *);
|
2008-06-18 18:34:07 +02:00
|
|
|
|
2010-12-11 17:15:02 +01:00
|
|
|
const char *status_prompt_up_history(u_int *);
|
|
|
|
const char *status_prompt_down_history(u_int *);
|
|
|
|
void status_prompt_add_history(const char *);
|
2009-01-06 18:04:56 +01:00
|
|
|
char *status_prompt_complete(const char *);
|
2009-01-06 16:37:15 +01:00
|
|
|
|
2010-12-11 17:15:02 +01:00
|
|
|
/* Status prompt history. */
|
|
|
|
ARRAY_DECL(, char *) status_prompt_history = ARRAY_INITIALIZER;
|
|
|
|
|
2011-02-15 16:20:03 +01:00
|
|
|
/* Status output tree. */
|
|
|
|
RB_GENERATE(status_out_tree, status_out, entry, status_out_cmp);
|
|
|
|
|
|
|
|
/* Output tree comparison function. */
|
|
|
|
int
|
|
|
|
status_out_cmp(struct status_out *so1, struct status_out *so2)
|
|
|
|
{
|
|
|
|
return (strcmp(so1->cmd, so2->cmd));
|
|
|
|
}
|
|
|
|
|
2012-01-29 13:57:01 +01:00
|
|
|
/* Get screen line of status line. -1 means off. */
|
|
|
|
int
|
|
|
|
status_at_line(struct client *c)
|
|
|
|
{
|
|
|
|
struct session *s = c->session;
|
|
|
|
|
|
|
|
if (!options_get_number(&s->options, "status"))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (options_get_number(&s->options, "status-position") == 0)
|
|
|
|
return (0);
|
|
|
|
return (c->tty.sy - 1);
|
|
|
|
}
|
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Retrieve options for left string. */
|
|
|
|
char *
|
|
|
|
status_redraw_get_left(struct client *c,
|
|
|
|
time_t t, int utf8flag, struct grid_cell *gc, size_t *size)
|
|
|
|
{
|
|
|
|
struct session *s = c->session;
|
|
|
|
char *left;
|
|
|
|
u_char fg, bg, attr;
|
|
|
|
size_t leftlen;
|
|
|
|
|
|
|
|
fg = options_get_number(&s->options, "status-left-fg");
|
|
|
|
if (fg != 8)
|
|
|
|
colour_set_fg(gc, fg);
|
|
|
|
bg = options_get_number(&s->options, "status-left-bg");
|
|
|
|
if (bg != 8)
|
|
|
|
colour_set_bg(gc, bg);
|
|
|
|
attr = options_get_number(&s->options, "status-left-attr");
|
|
|
|
if (attr != 0)
|
|
|
|
gc->attr = attr;
|
|
|
|
|
2011-04-07 00:21:02 +02:00
|
|
|
left = status_replace(c, NULL,
|
|
|
|
NULL, NULL, options_get_string(&s->options, "status-left"), t, 1);
|
2009-11-19 23:37:04 +01:00
|
|
|
|
|
|
|
*size = options_get_number(&s->options, "status-left-length");
|
|
|
|
leftlen = screen_write_cstrlen(utf8flag, "%s", left);
|
|
|
|
if (leftlen < *size)
|
|
|
|
*size = leftlen;
|
|
|
|
return (left);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve options for right string. */
|
|
|
|
char *
|
|
|
|
status_redraw_get_right(struct client *c,
|
|
|
|
time_t t, int utf8flag, struct grid_cell *gc, size_t *size)
|
|
|
|
{
|
|
|
|
struct session *s = c->session;
|
|
|
|
char *right;
|
|
|
|
u_char fg, bg, attr;
|
|
|
|
size_t rightlen;
|
|
|
|
|
|
|
|
fg = options_get_number(&s->options, "status-right-fg");
|
|
|
|
if (fg != 8)
|
|
|
|
colour_set_fg(gc, fg);
|
|
|
|
bg = options_get_number(&s->options, "status-right-bg");
|
|
|
|
if (bg != 8)
|
|
|
|
colour_set_bg(gc, bg);
|
|
|
|
attr = options_get_number(&s->options, "status-right-attr");
|
|
|
|
if (attr != 0)
|
|
|
|
gc->attr = attr;
|
|
|
|
|
2011-04-07 00:21:02 +02:00
|
|
|
right = status_replace(c, NULL,
|
|
|
|
NULL, NULL, options_get_string(&s->options, "status-right"), t, 1);
|
2009-11-19 23:37:04 +01:00
|
|
|
|
|
|
|
*size = options_get_number(&s->options, "status-right-length");
|
|
|
|
rightlen = screen_write_cstrlen(utf8flag, "%s", right);
|
|
|
|
if (rightlen < *size)
|
|
|
|
*size = rightlen;
|
|
|
|
return (right);
|
|
|
|
}
|
|
|
|
|
2011-04-18 23:07:58 +02:00
|
|
|
/* Set window at window list position. */
|
|
|
|
void
|
|
|
|
status_set_window_at(struct client *c, u_int x)
|
|
|
|
{
|
|
|
|
struct session *s = c->session;
|
|
|
|
struct winlink *wl;
|
|
|
|
|
|
|
|
x += s->wlmouse;
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
|
|
|
if (x < wl->status_width &&
|
|
|
|
session_select(s, wl->idx) == 0) {
|
|
|
|
server_redraw_session(s);
|
|
|
|
}
|
|
|
|
x -= wl->status_width + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-14 18:47:20 +02:00
|
|
|
/* Draw status for client on the last lines of given context. */
|
2009-02-10 01:18:06 +01:00
|
|
|
int
|
2008-06-14 18:47:20 +02:00
|
|
|
status_redraw(struct client *c)
|
2007-10-01 16:53:29 +02:00
|
|
|
{
|
2009-11-19 23:37:04 +01:00
|
|
|
struct screen_write_ctx ctx;
|
|
|
|
struct session *s = c->session;
|
|
|
|
struct winlink *wl;
|
|
|
|
struct screen old_status, window_list;
|
|
|
|
struct grid_cell stdgc, lgc, rgc, gc;
|
|
|
|
time_t t;
|
|
|
|
char *left, *right;
|
|
|
|
u_int offset, needed;
|
|
|
|
u_int wlstart, wlwidth, wlavailable, wloffset, wlsize;
|
|
|
|
size_t llen, rlen;
|
|
|
|
int larrow, rarrow, utf8flag;
|
2008-12-05 21:04:06 +01:00
|
|
|
|
2009-11-22 01:09:42 +01:00
|
|
|
/* No status line? */
|
2009-07-15 19:43:45 +02:00
|
|
|
if (c->tty.sy == 0 || !options_get_number(&s->options, "status"))
|
|
|
|
return (1);
|
2009-11-19 23:37:04 +01:00
|
|
|
left = right = NULL;
|
2009-07-15 19:43:45 +02:00
|
|
|
larrow = rarrow = 0;
|
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Update status timer. */
|
2008-08-28 19:45:30 +02:00
|
|
|
if (gettimeofday(&c->status_timer, NULL) != 0)
|
2009-09-21 00:11:27 +02:00
|
|
|
fatal("gettimeofday failed");
|
2009-11-19 23:37:04 +01:00
|
|
|
t = c->status_timer.tv_sec;
|
|
|
|
|
|
|
|
/* Set up default colour. */
|
2009-01-30 01:24:49 +01:00
|
|
|
memcpy(&stdgc, &grid_default_cell, sizeof gc);
|
2009-09-11 16:13:52 +02:00
|
|
|
colour_set_fg(&stdgc, options_get_number(&s->options, "status-fg"));
|
|
|
|
colour_set_bg(&stdgc, options_get_number(&s->options, "status-bg"));
|
2009-01-30 01:24:49 +01:00
|
|
|
stdgc.attr |= options_get_number(&s->options, "status-attr");
|
2008-06-18 18:34:07 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Create the target screen. */
|
|
|
|
memcpy(&old_status, &c->status, sizeof old_status);
|
|
|
|
screen_init(&c->status, c->tty.sx, 1, 0);
|
|
|
|
screen_write_start(&ctx, NULL, &c->status);
|
|
|
|
for (offset = 0; offset < c->tty.sx; offset++)
|
|
|
|
screen_write_putc(&ctx, &stdgc, ' ');
|
|
|
|
screen_write_stop(&ctx);
|
|
|
|
|
|
|
|
/* If the height is one line, blank status line. */
|
|
|
|
if (c->tty.sy <= 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Get UTF-8 flag. */
|
2009-06-25 17:48:25 +02:00
|
|
|
utf8flag = options_get_number(&s->options, "status-utf8");
|
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Work out left and right strings. */
|
|
|
|
memcpy(&lgc, &stdgc, sizeof lgc);
|
|
|
|
left = status_redraw_get_left(c, t, utf8flag, &lgc, &llen);
|
|
|
|
memcpy(&rgc, &stdgc, sizeof rgc);
|
|
|
|
right = status_redraw_get_right(c, t, utf8flag, &rgc, &rlen);
|
2008-06-18 18:34:07 +02:00
|
|
|
|
|
|
|
/*
|
2009-11-19 23:37:04 +01:00
|
|
|
* Figure out how much space we have for the window list. If there
|
|
|
|
* isn't enough space, just show a blank status line.
|
2008-06-18 18:34:07 +02:00
|
|
|
*/
|
2009-12-04 23:14:47 +01:00
|
|
|
needed = 0;
|
2008-06-18 18:34:07 +02:00
|
|
|
if (llen != 0)
|
2009-11-19 23:37:04 +01:00
|
|
|
needed += llen + 1;
|
2008-06-18 18:34:07 +02:00
|
|
|
if (rlen != 0)
|
2009-11-19 23:37:04 +01:00
|
|
|
needed += rlen + 1;
|
|
|
|
if (c->tty.sx == 0 || c->tty.sx <= needed)
|
|
|
|
goto out;
|
|
|
|
wlavailable = c->tty.sx - needed;
|
2008-06-18 18:34:07 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Calculate the total size needed for the window list. */
|
|
|
|
wlstart = wloffset = wlwidth = 0;
|
2008-06-18 18:34:07 +02:00
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
2009-11-19 23:37:04 +01:00
|
|
|
if (wl->status_text != NULL)
|
|
|
|
xfree(wl->status_text);
|
|
|
|
memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
|
|
|
|
wl->status_text = status_print(c, wl, t, &wl->status_cell);
|
2009-12-04 23:14:47 +01:00
|
|
|
wl->status_width =
|
2009-11-19 23:37:04 +01:00
|
|
|
screen_write_cstrlen(utf8flag, "%s", wl->status_text);
|
|
|
|
|
2008-06-18 18:34:07 +02:00
|
|
|
if (wl == s->curw)
|
2009-11-19 23:37:04 +01:00
|
|
|
wloffset = wlwidth;
|
|
|
|
wlwidth += wl->status_width + 1;
|
2008-06-18 18:34:07 +02:00
|
|
|
}
|
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Create a new screen for the window list. */
|
|
|
|
screen_init(&window_list, wlwidth, 1, 0);
|
|
|
|
|
|
|
|
/* And draw the window list into it. */
|
|
|
|
screen_write_start(&ctx, NULL, &window_list);
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
2009-12-04 23:14:47 +01:00
|
|
|
screen_write_cnputs(&ctx,
|
2009-11-19 23:37:04 +01:00
|
|
|
-1, &wl->status_cell, utf8flag, "%s", wl->status_text);
|
|
|
|
screen_write_putc(&ctx, &stdgc, ' ');
|
|
|
|
}
|
|
|
|
screen_write_stop(&ctx);
|
|
|
|
|
|
|
|
/* If there is enough space for the total width, skip to draw now. */
|
|
|
|
if (wlwidth <= wlavailable)
|
2008-06-18 18:34:07 +02:00
|
|
|
goto draw;
|
2008-06-04 07:40:35 +02:00
|
|
|
|
2008-06-18 18:34:07 +02:00
|
|
|
/* Find size of current window text. */
|
2009-11-19 23:37:04 +01:00
|
|
|
wlsize = s->curw->status_width;
|
2008-06-18 18:34:07 +02:00
|
|
|
|
|
|
|
/*
|
2009-11-19 23:37:04 +01:00
|
|
|
* If the current window is already on screen, good to draw from the
|
2008-06-18 18:34:07 +02:00
|
|
|
* start and just leave off the end.
|
|
|
|
*/
|
2009-11-19 23:37:04 +01:00
|
|
|
if (wloffset + wlsize < wlavailable) {
|
|
|
|
if (wlavailable > 0) {
|
2008-06-18 19:28:17 +02:00
|
|
|
rarrow = 1;
|
2009-11-19 23:37:04 +01:00
|
|
|
wlavailable--;
|
2008-06-18 19:28:17 +02:00
|
|
|
}
|
2009-11-19 23:37:04 +01:00
|
|
|
wlwidth = wlavailable;
|
2008-06-18 19:14:02 +02:00
|
|
|
} else {
|
2009-11-19 23:37:04 +01:00
|
|
|
/*
|
|
|
|
* Work out how many characters we need to omit from the
|
|
|
|
* start. There are wlavailable characters to fill, and
|
|
|
|
* wloffset + wlsize must be the last. So, the start character
|
|
|
|
* is wloffset + wlsize - wlavailable.
|
|
|
|
*/
|
|
|
|
if (wlavailable > 0) {
|
|
|
|
larrow = 1;
|
|
|
|
wlavailable--;
|
|
|
|
}
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
wlstart = wloffset + wlsize - wlavailable;
|
|
|
|
if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
|
|
|
|
rarrow = 1;
|
|
|
|
wlstart++;
|
|
|
|
wlavailable--;
|
|
|
|
}
|
|
|
|
wlwidth = wlavailable;
|
2008-06-18 19:14:02 +02:00
|
|
|
}
|
2008-06-18 18:34:07 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Bail if anything is now too small too. */
|
|
|
|
if (wlwidth == 0 || wlavailable == 0) {
|
|
|
|
screen_free(&window_list);
|
|
|
|
goto out;
|
2009-07-20 18:01:07 +02:00
|
|
|
}
|
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/*
|
|
|
|
* Now the start position is known, work out the state of the left and
|
|
|
|
* right arrows.
|
|
|
|
*/
|
2008-06-18 18:34:07 +02:00
|
|
|
offset = 0;
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
2010-06-23 01:26:18 +02:00
|
|
|
if (wl->flags & WINLINK_ALERTFLAGS &&
|
|
|
|
larrow == 1 && offset < wlstart)
|
|
|
|
larrow = -1;
|
2008-06-18 19:14:02 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
offset += wl->status_width;
|
2008-06-18 18:34:07 +02:00
|
|
|
|
2010-06-23 01:26:18 +02:00
|
|
|
if (wl->flags & WINLINK_ALERTFLAGS &&
|
|
|
|
rarrow == 1 && offset > wlstart + wlwidth)
|
|
|
|
rarrow = -1;
|
2007-10-01 16:53:29 +02:00
|
|
|
}
|
2008-06-18 18:34:07 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
draw:
|
2009-12-04 23:14:47 +01:00
|
|
|
/* Begin drawing. */
|
2009-11-19 23:37:04 +01:00
|
|
|
screen_write_start(&ctx, NULL, &c->status);
|
2008-06-18 19:14:02 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Draw the left string and arrow. */
|
|
|
|
screen_write_cursormove(&ctx, 0, 0);
|
|
|
|
if (llen != 0) {
|
|
|
|
screen_write_cnputs(&ctx, llen, &lgc, utf8flag, "%s", left);
|
2009-06-26 17:34:12 +02:00
|
|
|
screen_write_putc(&ctx, &stdgc, ' ');
|
2008-06-18 19:14:02 +02:00
|
|
|
}
|
|
|
|
if (larrow != 0) {
|
2009-01-30 01:24:49 +01:00
|
|
|
memcpy(&gc, &stdgc, sizeof gc);
|
2008-09-25 22:08:57 +02:00
|
|
|
if (larrow == -1)
|
2009-01-30 01:24:49 +01:00
|
|
|
gc.attr ^= GRID_ATTR_REVERSE;
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_putc(&ctx, &gc, '<');
|
2008-06-19 00:21:51 +02:00
|
|
|
}
|
2009-11-19 23:37:04 +01:00
|
|
|
|
|
|
|
/* Draw the right string and arrow. */
|
2008-06-18 19:14:02 +02:00
|
|
|
if (rarrow != 0) {
|
2009-11-19 23:37:04 +01:00
|
|
|
screen_write_cursormove(&ctx, c->tty.sx - rlen - 2, 0);
|
2009-01-30 01:24:49 +01:00
|
|
|
memcpy(&gc, &stdgc, sizeof gc);
|
2008-09-25 22:08:57 +02:00
|
|
|
if (rarrow == -1)
|
2009-01-30 01:24:49 +01:00
|
|
|
gc.attr ^= GRID_ATTR_REVERSE;
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_putc(&ctx, &gc, '>');
|
2009-11-19 23:37:04 +01:00
|
|
|
} else
|
|
|
|
screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0);
|
|
|
|
if (rlen != 0) {
|
|
|
|
screen_write_putc(&ctx, &stdgc, ' ');
|
|
|
|
screen_write_cnputs(&ctx, rlen, &rgc, utf8flag, "%s", right);
|
2008-06-18 19:14:02 +02:00
|
|
|
}
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Figure out the offset for the window list. */
|
2010-01-28 23:46:44 +01:00
|
|
|
if (llen != 0)
|
|
|
|
wloffset = llen + 1;
|
|
|
|
else
|
|
|
|
wloffset = 0;
|
2009-11-19 23:37:04 +01:00
|
|
|
if (wlwidth < wlavailable) {
|
|
|
|
switch (options_get_number(&s->options, "status-justify")) {
|
|
|
|
case 1: /* centered */
|
2010-01-28 23:46:44 +01:00
|
|
|
wloffset += (wlavailable - wlwidth) / 2;
|
2009-11-19 23:37:04 +01:00
|
|
|
break;
|
|
|
|
case 2: /* right */
|
2010-01-28 23:46:44 +01:00
|
|
|
wloffset += (wlavailable - wlwidth);
|
2009-11-19 23:37:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (larrow != 0)
|
|
|
|
wloffset++;
|
2008-06-18 18:34:07 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
/* Copy the window list. */
|
2011-04-18 23:07:58 +02:00
|
|
|
s->wlmouse = -wloffset + wlstart;
|
2009-11-19 23:37:04 +01:00
|
|
|
screen_write_cursormove(&ctx, wloffset, 0);
|
|
|
|
screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
|
2009-12-04 23:14:47 +01:00
|
|
|
screen_free(&window_list);
|
2008-06-19 20:27:55 +02:00
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_stop(&ctx);
|
2008-06-19 20:27:55 +02:00
|
|
|
|
2009-11-19 23:37:04 +01:00
|
|
|
out:
|
2008-12-05 21:04:06 +01:00
|
|
|
if (left != NULL)
|
|
|
|
xfree(left);
|
|
|
|
if (right != NULL)
|
|
|
|
xfree(right);
|
2009-02-10 01:18:06 +01:00
|
|
|
|
|
|
|
if (grid_compare(c->status.grid, old_status.grid) == 0) {
|
|
|
|
screen_free(&old_status);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
screen_free(&old_status);
|
|
|
|
return (1);
|
2008-12-05 21:04:06 +01:00
|
|
|
}
|
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Replace a single special sequence (prefixed by #). */
|
|
|
|
void
|
2011-04-07 00:21:02 +02:00
|
|
|
status_replace1(struct client *c, struct session *s, struct winlink *wl,
|
|
|
|
struct window_pane *wp, char **iptr, char **optr, char *out,
|
|
|
|
size_t outsize, int jobsflag)
|
2008-12-05 21:04:06 +01:00
|
|
|
{
|
2011-04-07 00:21:02 +02:00
|
|
|
char ch, tmp[256], *ptr, *endptr, *freeptr;
|
|
|
|
size_t ptrlen;
|
|
|
|
long limit;
|
2011-11-25 14:30:45 +01:00
|
|
|
u_int idx;
|
2009-11-19 23:30:39 +01:00
|
|
|
|
2011-04-07 00:21:02 +02:00
|
|
|
if (s == NULL)
|
|
|
|
s = c->session;
|
2009-11-19 23:35:10 +01:00
|
|
|
if (wl == NULL)
|
|
|
|
wl = s->curw;
|
2011-04-07 00:21:02 +02:00
|
|
|
if (wp == NULL)
|
|
|
|
wp = wl->window->active;
|
2009-11-19 23:35:10 +01:00
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
errno = 0;
|
|
|
|
limit = strtol(*iptr, &endptr, 10);
|
|
|
|
if ((limit == 0 && errno != EINVAL) ||
|
|
|
|
(limit == LONG_MIN && errno != ERANGE) ||
|
2009-12-04 23:14:47 +01:00
|
|
|
(limit == LONG_MAX && errno != ERANGE) ||
|
2009-11-19 23:30:39 +01:00
|
|
|
limit != 0)
|
|
|
|
*iptr = endptr;
|
|
|
|
if (limit <= 0)
|
|
|
|
limit = LONG_MAX;
|
|
|
|
|
|
|
|
freeptr = NULL;
|
|
|
|
|
|
|
|
switch (*(*iptr)++) {
|
|
|
|
case '(':
|
|
|
|
if (!jobsflag) {
|
|
|
|
ch = ')';
|
|
|
|
goto skip_to;
|
|
|
|
}
|
2011-02-15 16:20:03 +01:00
|
|
|
if ((ptr = status_find_job(c, iptr)) == NULL)
|
2009-11-19 23:30:39 +01:00
|
|
|
return;
|
|
|
|
goto do_replace;
|
2011-04-07 00:21:02 +02:00
|
|
|
case 'D':
|
|
|
|
xsnprintf(tmp, sizeof tmp, "%%%u", wp->id);
|
|
|
|
ptr = tmp;
|
|
|
|
goto do_replace;
|
2009-11-19 23:30:39 +01:00
|
|
|
case 'H':
|
|
|
|
if (gethostname(tmp, sizeof tmp) != 0)
|
|
|
|
fatal("gethostname failed");
|
|
|
|
ptr = tmp;
|
|
|
|
goto do_replace;
|
2011-04-25 22:35:22 +02:00
|
|
|
case 'h':
|
|
|
|
if (gethostname(tmp, sizeof tmp) != 0)
|
|
|
|
fatal("gethostname failed");
|
|
|
|
if ((ptr = strchr(tmp, '.')) != NULL)
|
|
|
|
*ptr = '\0';
|
|
|
|
ptr = tmp;
|
|
|
|
goto do_replace;
|
2009-11-19 23:30:39 +01:00
|
|
|
case 'I':
|
|
|
|
xsnprintf(tmp, sizeof tmp, "%d", wl->idx);
|
|
|
|
ptr = tmp;
|
|
|
|
goto do_replace;
|
|
|
|
case 'P':
|
2011-11-25 14:30:45 +01:00
|
|
|
if (window_pane_index(wp, &idx) != 0)
|
|
|
|
fatalx("index not found");
|
2011-04-07 00:21:02 +02:00
|
|
|
xsnprintf(
|
2011-11-25 14:30:45 +01:00
|
|
|
tmp, sizeof tmp, "%u", idx);
|
2009-11-19 23:30:39 +01:00
|
|
|
ptr = tmp;
|
|
|
|
goto do_replace;
|
|
|
|
case 'S':
|
|
|
|
ptr = s->name;
|
|
|
|
goto do_replace;
|
|
|
|
case 'T':
|
2011-04-07 00:21:02 +02:00
|
|
|
ptr = wp->base.title;
|
2009-11-19 23:30:39 +01:00
|
|
|
goto do_replace;
|
|
|
|
case 'W':
|
|
|
|
ptr = wl->window->name;
|
|
|
|
goto do_replace;
|
2009-11-19 23:35:10 +01:00
|
|
|
case 'F':
|
2011-01-07 17:55:40 +01:00
|
|
|
ptr = window_printable_flags(s, wl);
|
|
|
|
freeptr = ptr;
|
2009-11-19 23:35:10 +01:00
|
|
|
goto do_replace;
|
2009-11-19 23:30:39 +01:00
|
|
|
case '[':
|
2009-12-04 23:14:47 +01:00
|
|
|
/*
|
2009-11-19 23:30:39 +01:00
|
|
|
* Embedded style, handled at display time. Leave present and
|
|
|
|
* skip input until ].
|
|
|
|
*/
|
|
|
|
ch = ']';
|
|
|
|
goto skip_to;
|
|
|
|
case '#':
|
2009-11-22 01:09:42 +01:00
|
|
|
*(*optr)++ = '#';
|
2009-11-19 23:30:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
do_replace:
|
|
|
|
ptrlen = strlen(ptr);
|
|
|
|
if ((size_t) limit < ptrlen)
|
|
|
|
ptrlen = limit;
|
|
|
|
|
|
|
|
if (*optr + ptrlen >= out + outsize - 1)
|
2012-01-29 13:51:43 +01:00
|
|
|
goto out;
|
2009-11-19 23:30:39 +01:00
|
|
|
while (ptrlen > 0 && *ptr != '\0') {
|
|
|
|
*(*optr)++ = *ptr++;
|
|
|
|
ptrlen--;
|
|
|
|
}
|
|
|
|
|
2012-01-29 13:51:43 +01:00
|
|
|
out:
|
2009-11-19 23:30:39 +01:00
|
|
|
if (freeptr != NULL)
|
|
|
|
xfree(freeptr);
|
|
|
|
return;
|
|
|
|
|
|
|
|
skip_to:
|
|
|
|
*(*optr)++ = '#';
|
|
|
|
|
|
|
|
(*iptr)--; /* include ch */
|
|
|
|
while (**iptr != ch && **iptr != '\0') {
|
|
|
|
if (*optr >= out + outsize - 1)
|
|
|
|
break;
|
|
|
|
*(*optr)++ = *(*iptr)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace special sequences in fmt. */
|
|
|
|
char *
|
2011-04-07 00:21:02 +02:00
|
|
|
status_replace(struct client *c, struct session *s, struct winlink *wl,
|
|
|
|
struct window_pane *wp, const char *fmt, time_t t, int jobsflag)
|
2009-11-19 23:30:39 +01:00
|
|
|
{
|
|
|
|
static char out[BUFSIZ];
|
|
|
|
char in[BUFSIZ], ch, *iptr, *optr;
|
2012-01-29 13:52:16 +01:00
|
|
|
size_t len;
|
2009-11-19 23:35:10 +01:00
|
|
|
|
2012-01-29 13:52:16 +01:00
|
|
|
len = strftime(in, sizeof in, fmt, localtime(&t));
|
|
|
|
in[len] = '\0';
|
2008-12-05 21:04:06 +01:00
|
|
|
|
|
|
|
iptr = in;
|
|
|
|
optr = out;
|
|
|
|
|
|
|
|
while (*iptr != '\0') {
|
|
|
|
if (optr >= out + (sizeof out) - 1)
|
|
|
|
break;
|
2009-11-19 23:30:39 +01:00
|
|
|
ch = *iptr++;
|
|
|
|
|
2011-01-07 15:32:26 +01:00
|
|
|
if (ch != '#' || *iptr == '\0') {
|
2008-12-05 21:04:06 +01:00
|
|
|
*optr++ = ch;
|
2009-11-19 23:30:39 +01:00
|
|
|
continue;
|
2008-12-05 21:04:06 +01:00
|
|
|
}
|
2011-04-07 00:21:02 +02:00
|
|
|
status_replace1(
|
|
|
|
c, s, wl, wp, &iptr, &optr, out, sizeof out, jobsflag);
|
2008-12-05 21:04:06 +01:00
|
|
|
}
|
|
|
|
*optr = '\0';
|
|
|
|
|
|
|
|
return (xstrdup(out));
|
2008-06-18 18:34:07 +02:00
|
|
|
}
|
2007-11-27 20:23:34 +01:00
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Figure out job name and get its result, starting it off if necessary. */
|
2009-01-30 00:35:14 +01:00
|
|
|
char *
|
2011-02-15 16:20:03 +01:00
|
|
|
status_find_job(struct client *c, char **iptr)
|
2009-01-30 00:35:14 +01:00
|
|
|
{
|
2011-02-15 16:20:03 +01:00
|
|
|
struct status_out *so, so_find;
|
|
|
|
char *cmd;
|
|
|
|
int lastesc;
|
|
|
|
size_t len;
|
2009-01-30 00:35:14 +01:00
|
|
|
|
2009-02-01 19:14:49 +01:00
|
|
|
if (**iptr == '\0')
|
2009-01-30 00:35:14 +01:00
|
|
|
return (NULL);
|
2009-02-01 19:14:49 +01:00
|
|
|
if (**iptr == ')') { /* no command given */
|
|
|
|
(*iptr)++;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = xmalloc(strlen(*iptr) + 1);
|
|
|
|
len = 0;
|
2009-01-30 00:35:14 +01:00
|
|
|
|
2009-02-01 19:14:49 +01:00
|
|
|
lastesc = 0;
|
|
|
|
for (; **iptr != '\0'; (*iptr)++) {
|
|
|
|
if (!lastesc && **iptr == ')')
|
|
|
|
break; /* unescaped ) is the end */
|
|
|
|
if (!lastesc && **iptr == '\\') {
|
|
|
|
lastesc = 1;
|
|
|
|
continue; /* skip \ if not escaped */
|
|
|
|
}
|
|
|
|
lastesc = 0;
|
|
|
|
cmd[len++] = **iptr;
|
|
|
|
}
|
2009-10-12 01:55:26 +02:00
|
|
|
if (**iptr == '\0') /* no terminating ) */ {
|
|
|
|
xfree(cmd);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2009-02-01 19:14:49 +01:00
|
|
|
(*iptr)++; /* skip final ) */
|
|
|
|
cmd[len] = '\0';
|
2009-01-30 00:35:14 +01:00
|
|
|
|
2011-02-15 16:20:03 +01:00
|
|
|
/* First try in the new tree. */
|
|
|
|
so_find.cmd = cmd;
|
|
|
|
so = RB_FIND(status_out_tree, &c->status_new, &so_find);
|
2011-08-21 14:39:34 +02:00
|
|
|
if (so != NULL && so->out != NULL) {
|
|
|
|
xfree(cmd);
|
2011-02-15 16:20:03 +01:00
|
|
|
return (so->out);
|
2011-08-21 14:39:34 +02:00
|
|
|
}
|
2011-02-15 16:20:03 +01:00
|
|
|
|
|
|
|
/* If not found at all, start the job and add to the tree. */
|
|
|
|
if (so == NULL) {
|
|
|
|
job_run(cmd, status_job_callback, status_job_free, c);
|
|
|
|
c->references++;
|
|
|
|
|
|
|
|
so = xmalloc(sizeof *so);
|
|
|
|
so->cmd = xstrdup(cmd);
|
|
|
|
so->out = NULL;
|
|
|
|
RB_INSERT(status_out_tree, &c->status_new, so);
|
2009-01-30 00:35:14 +01:00
|
|
|
}
|
2011-02-15 16:20:03 +01:00
|
|
|
|
|
|
|
/* Lookup in the old tree. */
|
|
|
|
so_find.cmd = cmd;
|
|
|
|
so = RB_FIND(status_out_tree, &c->status_old, &so_find);
|
2010-03-10 14:41:13 +01:00
|
|
|
xfree(cmd);
|
2011-02-15 16:20:03 +01:00
|
|
|
if (so != NULL)
|
|
|
|
return (so->out);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free job tree. */
|
|
|
|
void
|
|
|
|
status_free_jobs(struct status_out_tree *sotree)
|
|
|
|
{
|
|
|
|
struct status_out *so, *so_next;
|
|
|
|
|
|
|
|
so_next = RB_MIN(status_out_tree, sotree);
|
|
|
|
while (so_next != NULL) {
|
|
|
|
so = so_next;
|
|
|
|
so_next = RB_NEXT(status_out_tree, sotree, so);
|
|
|
|
|
|
|
|
RB_REMOVE(status_out_tree, sotree, so);
|
|
|
|
if (so->out != NULL)
|
|
|
|
xfree(so->out);
|
|
|
|
xfree(so->cmd);
|
|
|
|
xfree(so);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update jobs on status interval. */
|
|
|
|
void
|
|
|
|
status_update_jobs(struct client *c)
|
|
|
|
{
|
|
|
|
/* Free the old tree. */
|
|
|
|
status_free_jobs(&c->status_old);
|
|
|
|
|
|
|
|
/* Move the new to old. */
|
|
|
|
memcpy(&c->status_old, &c->status_new, sizeof c->status_old);
|
|
|
|
RB_INIT(&c->status_new);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free status job. */
|
|
|
|
void
|
|
|
|
status_job_free(void *data)
|
|
|
|
{
|
|
|
|
struct client *c = data;
|
|
|
|
|
|
|
|
c->references--;
|
2009-10-12 01:55:26 +02:00
|
|
|
}
|
2009-01-30 00:35:14 +01:00
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Job has finished: save its result. */
|
2009-10-12 01:55:26 +02:00
|
|
|
void
|
|
|
|
status_job_callback(struct job *job)
|
|
|
|
{
|
2011-02-15 16:20:03 +01:00
|
|
|
struct client *c = job->data;
|
|
|
|
struct status_out *so, so_find;
|
|
|
|
char *line, *buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (c->flags & CLIENT_DEAD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
so_find.cmd = job->cmd;
|
|
|
|
so = RB_FIND(status_out_tree, &c->status_new, &so_find);
|
|
|
|
if (so == NULL || so->out != NULL)
|
|
|
|
return;
|
2009-10-12 01:55:26 +02:00
|
|
|
|
2009-11-08 23:56:04 +01:00
|
|
|
buf = NULL;
|
|
|
|
if ((line = evbuffer_readline(job->event->input)) == NULL) {
|
|
|
|
len = EVBUFFER_LENGTH(job->event->input);
|
|
|
|
buf = xmalloc(len + 1);
|
|
|
|
if (len != 0)
|
|
|
|
memcpy(buf, EVBUFFER_DATA(job->event->input), len);
|
|
|
|
buf[len] = '\0';
|
2011-02-15 16:20:03 +01:00
|
|
|
} else
|
|
|
|
buf = xstrdup(line);
|
2009-10-12 01:55:26 +02:00
|
|
|
|
2011-02-15 16:20:03 +01:00
|
|
|
so->out = buf;
|
2011-05-05 12:02:36 +02:00
|
|
|
server_status_client(c);
|
2009-01-30 00:35:14 +01:00
|
|
|
}
|
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Return winlink status line entry and adjust gc as necessary. */
|
2008-06-18 18:34:07 +02:00
|
|
|
char *
|
2009-11-19 23:35:10 +01:00
|
|
|
status_print(
|
|
|
|
struct client *c, struct winlink *wl, time_t t, struct grid_cell *gc)
|
2008-06-18 18:34:07 +02:00
|
|
|
{
|
2009-09-11 16:13:52 +02:00
|
|
|
struct options *oo = &wl->window->options;
|
2009-11-19 23:35:10 +01:00
|
|
|
struct session *s = c->session;
|
|
|
|
const char *fmt;
|
|
|
|
char *text;
|
2009-09-11 16:13:52 +02:00
|
|
|
u_char fg, bg, attr;
|
2009-01-30 01:24:49 +01:00
|
|
|
|
2009-09-11 16:13:52 +02:00
|
|
|
fg = options_get_number(oo, "window-status-fg");
|
2009-01-30 01:24:49 +01:00
|
|
|
if (fg != 8)
|
2009-09-11 16:13:52 +02:00
|
|
|
colour_set_fg(gc, fg);
|
|
|
|
bg = options_get_number(oo, "window-status-bg");
|
2009-01-30 01:24:49 +01:00
|
|
|
if (bg != 8)
|
2009-09-11 16:13:52 +02:00
|
|
|
colour_set_bg(gc, bg);
|
|
|
|
attr = options_get_number(oo, "window-status-attr");
|
2009-01-30 01:24:49 +01:00
|
|
|
if (attr != 0)
|
|
|
|
gc->attr = attr;
|
2009-11-19 23:35:10 +01:00
|
|
|
fmt = options_get_string(oo, "window-status-format");
|
2009-07-20 17:57:05 +02:00
|
|
|
if (wl == s->curw) {
|
2009-09-11 16:13:52 +02:00
|
|
|
fg = options_get_number(oo, "window-status-current-fg");
|
2009-07-20 17:57:05 +02:00
|
|
|
if (fg != 8)
|
2009-09-11 16:13:52 +02:00
|
|
|
colour_set_fg(gc, fg);
|
|
|
|
bg = options_get_number(oo, "window-status-current-bg");
|
2009-07-20 17:57:05 +02:00
|
|
|
if (bg != 8)
|
2009-09-11 16:13:52 +02:00
|
|
|
colour_set_bg(gc, bg);
|
|
|
|
attr = options_get_number(oo, "window-status-current-attr");
|
2009-07-20 17:57:05 +02:00
|
|
|
if (attr != 0)
|
|
|
|
gc->attr = attr;
|
2009-11-19 23:35:10 +01:00
|
|
|
fmt = options_get_string(oo, "window-status-current-format");
|
2009-07-20 17:57:05 +02:00
|
|
|
}
|
2008-06-18 18:34:07 +02:00
|
|
|
|
2012-01-20 22:20:35 +01:00
|
|
|
if (wl->flags & WINLINK_BELL) {
|
|
|
|
fg = options_get_number(oo, "window-status-bell-fg");
|
2010-06-06 01:56:29 +02:00
|
|
|
if (fg != 8)
|
|
|
|
colour_set_fg(gc, fg);
|
2012-01-20 22:20:35 +01:00
|
|
|
bg = options_get_number(oo, "window-status-bell-bg");
|
2010-06-06 01:56:29 +02:00
|
|
|
if (bg != 8)
|
|
|
|
colour_set_bg(gc, bg);
|
2012-01-20 22:20:35 +01:00
|
|
|
attr = options_get_number(oo, "window-status-bell-attr");
|
|
|
|
if (attr != 0)
|
|
|
|
gc->attr = attr;
|
|
|
|
} else if (wl->flags & WINLINK_CONTENT) {
|
|
|
|
fg = options_get_number(oo, "window-status-content-fg");
|
|
|
|
if (fg != 8)
|
|
|
|
colour_set_fg(gc, fg);
|
|
|
|
bg = options_get_number(oo, "window-status-content-bg");
|
|
|
|
if (bg != 8)
|
|
|
|
colour_set_bg(gc, bg);
|
|
|
|
attr = options_get_number(oo, "window-status-content-attr");
|
|
|
|
if (attr != 0)
|
|
|
|
gc->attr = attr;
|
|
|
|
} else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE)) {
|
|
|
|
fg = options_get_number(oo, "window-status-activity-fg");
|
|
|
|
if (fg != 8)
|
|
|
|
colour_set_fg(gc, fg);
|
|
|
|
bg = options_get_number(oo, "window-status-activity-bg");
|
|
|
|
if (bg != 8)
|
|
|
|
colour_set_bg(gc, bg);
|
|
|
|
attr = options_get_number(oo, "window-status-activity-attr");
|
2010-06-06 01:56:29 +02:00
|
|
|
if (attr != 0)
|
|
|
|
gc->attr = attr;
|
|
|
|
}
|
2008-06-04 07:40:35 +02:00
|
|
|
|
2011-04-07 00:21:02 +02:00
|
|
|
text = status_replace(c, NULL, wl, NULL, fmt, t, 1);
|
2008-06-18 18:34:07 +02:00
|
|
|
return (text);
|
2007-10-01 16:53:29 +02:00
|
|
|
}
|
2008-06-19 20:27:55 +02:00
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Set a status line message. */
|
2009-07-15 19:50:11 +02:00
|
|
|
void printflike2
|
|
|
|
status_message_set(struct client *c, const char *fmt, ...)
|
2009-02-13 19:57:55 +01:00
|
|
|
{
|
2009-11-19 23:20:04 +01:00
|
|
|
struct timeval tv;
|
|
|
|
struct session *s = c->session;
|
|
|
|
struct message_entry *msg;
|
|
|
|
va_list ap;
|
|
|
|
int delay;
|
|
|
|
u_int i, limit;
|
2009-02-13 19:57:55 +01:00
|
|
|
|
2009-07-17 11:26:21 +02:00
|
|
|
status_prompt_clear(c);
|
|
|
|
status_message_clear(c);
|
|
|
|
|
2009-09-01 00:30:15 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&c->message_string, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2009-11-19 23:20:04 +01:00
|
|
|
ARRAY_EXPAND(&c->message_log, 1);
|
|
|
|
msg = &ARRAY_LAST(&c->message_log);
|
|
|
|
msg->msg_time = time(NULL);
|
|
|
|
msg->msg = xstrdup(c->message_string);
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
limit = 0;
|
|
|
|
else
|
|
|
|
limit = options_get_number(&s->options, "message-limit");
|
2009-11-22 01:10:39 +01:00
|
|
|
if (ARRAY_LENGTH(&c->message_log) > limit) {
|
|
|
|
limit = ARRAY_LENGTH(&c->message_log) - limit;
|
|
|
|
for (i = 0; i < limit; i++) {
|
|
|
|
msg = &ARRAY_FIRST(&c->message_log);
|
|
|
|
xfree(msg->msg);
|
|
|
|
ARRAY_REMOVE(&c->message_log, 0);
|
|
|
|
}
|
2009-11-19 23:20:04 +01:00
|
|
|
}
|
|
|
|
|
2009-02-13 19:57:55 +01:00
|
|
|
delay = options_get_number(&c->session->options, "display-time");
|
|
|
|
tv.tv_sec = delay / 1000;
|
|
|
|
tv.tv_usec = (delay % 1000) * 1000L;
|
2009-11-19 23:20:04 +01:00
|
|
|
|
2009-11-09 00:11:23 +01:00
|
|
|
evtimer_del(&c->message_timer);
|
|
|
|
evtimer_set(&c->message_timer, status_message_callback, c);
|
|
|
|
evtimer_add(&c->message_timer, &tv);
|
2009-02-13 19:57:55 +01:00
|
|
|
|
|
|
|
c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Clear status line message. */
|
2009-02-13 19:57:55 +01:00
|
|
|
void
|
|
|
|
status_message_clear(struct client *c)
|
|
|
|
{
|
|
|
|
if (c->message_string == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
xfree(c->message_string);
|
|
|
|
c->message_string = NULL;
|
|
|
|
|
|
|
|
c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
|
2009-07-17 11:26:21 +02:00
|
|
|
c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
|
2009-07-15 19:43:45 +02:00
|
|
|
|
|
|
|
screen_reinit(&c->status);
|
2009-02-13 19:57:55 +01:00
|
|
|
}
|
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Clear status line message after timer expires. */
|
2009-11-28 15:50:37 +01:00
|
|
|
/* ARGSUSED */
|
2009-11-09 00:11:23 +01:00
|
|
|
void
|
|
|
|
status_message_callback(unused int fd, unused short event, void *data)
|
|
|
|
{
|
|
|
|
struct client *c = data;
|
|
|
|
|
|
|
|
status_message_clear(c);
|
|
|
|
}
|
|
|
|
|
2008-06-19 20:27:55 +02:00
|
|
|
/* Draw client message on status line of present else on last line. */
|
2009-02-10 01:18:06 +01:00
|
|
|
int
|
2008-06-19 20:27:55 +02:00
|
|
|
status_message_redraw(struct client *c)
|
|
|
|
{
|
2009-01-12 00:31:46 +01:00
|
|
|
struct screen_write_ctx ctx;
|
2008-12-08 17:19:51 +01:00
|
|
|
struct session *s = c->session;
|
2009-02-10 01:18:06 +01:00
|
|
|
struct screen old_status;
|
2009-01-12 00:31:46 +01:00
|
|
|
size_t len;
|
2008-09-25 22:08:57 +02:00
|
|
|
struct grid_cell gc;
|
2009-11-22 01:11:33 +01:00
|
|
|
int utf8flag;
|
2008-06-19 20:27:55 +02:00
|
|
|
|
2009-02-11 18:50:36 +01:00
|
|
|
if (c->tty.sx == 0 || c->tty.sy == 0)
|
2009-02-10 01:18:06 +01:00
|
|
|
return (0);
|
|
|
|
memcpy(&old_status, &c->status, sizeof old_status);
|
2009-02-11 18:50:36 +01:00
|
|
|
screen_init(&c->status, c->tty.sx, 1, 0);
|
2008-06-19 20:27:55 +02:00
|
|
|
|
2009-11-22 01:11:33 +01:00
|
|
|
utf8flag = options_get_number(&s->options, "status-utf8");
|
|
|
|
|
|
|
|
len = screen_write_strlen(utf8flag, "%s", c->message_string);
|
2009-02-11 18:50:36 +01:00
|
|
|
if (len > c->tty.sx)
|
|
|
|
len = c->tty.sx;
|
2008-06-19 20:27:55 +02:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
memcpy(&gc, &grid_default_cell, sizeof gc);
|
2009-09-11 16:13:52 +02:00
|
|
|
colour_set_fg(&gc, options_get_number(&s->options, "message-fg"));
|
|
|
|
colour_set_bg(&gc, options_get_number(&s->options, "message-bg"));
|
Allow status, mode and message attributes to be changed by three new options: status-attr, mode-attr, message-attr. A comma-separataed list is accepted containing: bright, dim, underscore, blink, reverse, hidden, italics, for example: set -g status-attr bright,blink
From Josh Elsasser, thanks!
2009-01-27 21:22:33 +01:00
|
|
|
gc.attr |= options_get_number(&s->options, "message-attr");
|
2008-09-25 22:08:57 +02:00
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_start(&ctx, NULL, &c->status);
|
2008-09-25 22:08:57 +02:00
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_cursormove(&ctx, 0, 0);
|
2009-11-22 01:11:33 +01:00
|
|
|
screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->message_string);
|
2009-02-11 18:50:36 +01:00
|
|
|
for (; len < c->tty.sx; len++)
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_putc(&ctx, &gc, ' ');
|
2008-06-19 20:27:55 +02:00
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_stop(&ctx);
|
2009-02-10 01:18:06 +01:00
|
|
|
|
|
|
|
if (grid_compare(c->status.grid, old_status.grid) == 0) {
|
|
|
|
screen_free(&old_status);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
screen_free(&old_status);
|
|
|
|
return (1);
|
2008-06-19 20:27:55 +02:00
|
|
|
}
|
2008-06-19 21:40:35 +02:00
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Enable status line prompt. */
|
2009-02-13 19:57:55 +01:00
|
|
|
void
|
2011-07-03 23:52:50 +02:00
|
|
|
status_prompt_set(struct client *c, const char *msg, const char *input,
|
2009-07-17 11:26:21 +02:00
|
|
|
int (*callbackfn)(void *, const char *), void (*freefn)(void *),
|
|
|
|
void *data, int flags)
|
2009-02-13 19:57:55 +01:00
|
|
|
{
|
2009-07-29 00:55:59 +02:00
|
|
|
int keys;
|
|
|
|
|
2009-07-17 11:26:21 +02:00
|
|
|
status_message_clear(c);
|
|
|
|
status_prompt_clear(c);
|
|
|
|
|
2011-07-08 10:42:03 +02:00
|
|
|
c->prompt_string = status_replace(c, NULL, NULL, NULL, msg,
|
|
|
|
time(NULL), 0);
|
2009-02-13 19:57:55 +01:00
|
|
|
|
2011-07-08 10:42:03 +02:00
|
|
|
if (input == NULL)
|
|
|
|
input = "";
|
|
|
|
c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input,
|
|
|
|
time(NULL), 0);
|
2011-07-03 23:52:50 +02:00
|
|
|
c->prompt_index = strlen(c->prompt_buffer);
|
2009-02-13 19:57:55 +01:00
|
|
|
|
2009-07-17 11:26:21 +02:00
|
|
|
c->prompt_callbackfn = callbackfn;
|
|
|
|
c->prompt_freefn = freefn;
|
2009-02-13 19:57:55 +01:00
|
|
|
c->prompt_data = data;
|
|
|
|
|
|
|
|
c->prompt_hindex = 0;
|
|
|
|
|
2009-04-27 19:27:36 +02:00
|
|
|
c->prompt_flags = flags;
|
2009-02-13 19:57:55 +01:00
|
|
|
|
2009-07-29 00:55:59 +02:00
|
|
|
keys = options_get_number(&c->session->options, "status-keys");
|
|
|
|
if (keys == MODEKEY_EMACS)
|
2009-07-29 01:11:18 +02:00
|
|
|
mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit);
|
2009-07-29 00:55:59 +02:00
|
|
|
else
|
2009-07-29 01:11:18 +02:00
|
|
|
mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit);
|
2009-02-13 22:39:45 +01:00
|
|
|
|
2009-02-13 19:57:55 +01:00
|
|
|
c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Remove status line prompt. */
|
2009-02-13 19:57:55 +01:00
|
|
|
void
|
|
|
|
status_prompt_clear(struct client *c)
|
|
|
|
{
|
2009-12-04 23:14:47 +01:00
|
|
|
if (c->prompt_string == NULL)
|
2009-02-13 19:57:55 +01:00
|
|
|
return;
|
|
|
|
|
2009-07-17 11:26:21 +02:00
|
|
|
if (c->prompt_freefn != NULL && c->prompt_data != NULL)
|
|
|
|
c->prompt_freefn(c->prompt_data);
|
|
|
|
|
2009-02-13 19:57:55 +01:00
|
|
|
xfree(c->prompt_string);
|
|
|
|
c->prompt_string = NULL;
|
|
|
|
|
|
|
|
xfree(c->prompt_buffer);
|
|
|
|
c->prompt_buffer = NULL;
|
|
|
|
|
|
|
|
c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
|
2009-07-17 11:26:21 +02:00
|
|
|
c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
|
2009-07-15 19:43:45 +02:00
|
|
|
|
|
|
|
screen_reinit(&c->status);
|
2009-02-13 19:57:55 +01:00
|
|
|
}
|
|
|
|
|
2009-11-19 23:30:39 +01:00
|
|
|
/* Update status line prompt with a new prompt string. */
|
2009-08-20 13:51:20 +02:00
|
|
|
void
|
2011-07-03 23:52:50 +02:00
|
|
|
status_prompt_update(struct client *c, const char *msg, const char *input)
|
2009-08-20 13:51:20 +02:00
|
|
|
{
|
|
|
|
xfree(c->prompt_string);
|
2011-07-08 10:42:03 +02:00
|
|
|
c->prompt_string = status_replace(c, NULL, NULL, NULL, msg,
|
|
|
|
time(NULL), 0);
|
2009-08-20 13:51:20 +02:00
|
|
|
|
2011-07-03 23:52:50 +02:00
|
|
|
xfree(c->prompt_buffer);
|
2011-07-08 10:42:03 +02:00
|
|
|
if (input == NULL)
|
|
|
|
input = "";
|
|
|
|
c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input,
|
|
|
|
time(NULL), 0);
|
2011-07-03 23:52:50 +02:00
|
|
|
c->prompt_index = strlen(c->prompt_buffer);
|
2009-08-20 13:51:20 +02:00
|
|
|
|
|
|
|
c->prompt_hindex = 0;
|
|
|
|
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
|
2008-06-19 21:40:35 +02:00
|
|
|
/* Draw client prompt on status line of present else on last line. */
|
2009-02-10 01:18:06 +01:00
|
|
|
int
|
2008-06-19 21:40:35 +02:00
|
|
|
status_prompt_redraw(struct client *c)
|
|
|
|
{
|
2009-11-22 01:11:33 +01:00
|
|
|
struct screen_write_ctx ctx;
|
2008-12-08 17:19:51 +01:00
|
|
|
struct session *s = c->session;
|
2009-02-10 01:18:06 +01:00
|
|
|
struct screen old_status;
|
2009-09-02 02:55:49 +02:00
|
|
|
size_t i, size, left, len, off;
|
2009-11-22 01:11:33 +01:00
|
|
|
struct grid_cell gc, *gcp;
|
|
|
|
int utf8flag;
|
2008-06-19 21:40:35 +02:00
|
|
|
|
2009-02-11 18:50:36 +01:00
|
|
|
if (c->tty.sx == 0 || c->tty.sy == 0)
|
2009-02-10 01:18:06 +01:00
|
|
|
return (0);
|
|
|
|
memcpy(&old_status, &c->status, sizeof old_status);
|
2009-02-11 18:50:36 +01:00
|
|
|
screen_init(&c->status, c->tty.sx, 1, 0);
|
2008-06-19 21:40:35 +02:00
|
|
|
|
2009-11-22 01:11:33 +01:00
|
|
|
utf8flag = options_get_number(&s->options, "status-utf8");
|
|
|
|
|
|
|
|
len = screen_write_strlen(utf8flag, "%s", c->prompt_string);
|
2009-02-11 18:50:36 +01:00
|
|
|
if (len > c->tty.sx)
|
|
|
|
len = c->tty.sx;
|
2009-11-22 01:11:33 +01:00
|
|
|
off = 0;
|
2008-06-19 21:40:35 +02:00
|
|
|
|
2008-09-25 22:08:57 +02:00
|
|
|
memcpy(&gc, &grid_default_cell, sizeof gc);
|
2011-11-10 22:40:17 +01:00
|
|
|
/* Change colours for command mode. */
|
|
|
|
if (c->prompt_mdata.mode == 1) {
|
|
|
|
colour_set_fg(&gc, options_get_number(&s->options, "message-command-fg"));
|
|
|
|
colour_set_bg(&gc, options_get_number(&s->options, "message-command-bg"));
|
|
|
|
gc.attr |= options_get_number(&s->options, "message-command-attr");
|
|
|
|
} else {
|
|
|
|
colour_set_fg(&gc, options_get_number(&s->options, "message-fg"));
|
|
|
|
colour_set_bg(&gc, options_get_number(&s->options, "message-bg"));
|
|
|
|
gc.attr |= options_get_number(&s->options, "message-attr");
|
|
|
|
}
|
2008-09-25 22:08:57 +02:00
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_start(&ctx, NULL, &c->status);
|
2008-06-19 21:40:35 +02:00
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_cursormove(&ctx, 0, 0);
|
2009-11-22 01:11:33 +01:00
|
|
|
screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->prompt_string);
|
2008-06-19 21:40:35 +02:00
|
|
|
|
2009-02-11 18:50:36 +01:00
|
|
|
left = c->tty.sx - len;
|
2008-06-19 21:40:35 +02:00
|
|
|
if (left != 0) {
|
2009-11-22 01:11:33 +01:00
|
|
|
size = screen_write_strlen(utf8flag, "%s", c->prompt_buffer);
|
|
|
|
if (c->prompt_index >= left) {
|
2009-07-29 00:32:15 +02:00
|
|
|
off = c->prompt_index - left + 1;
|
2009-11-22 01:11:33 +01:00
|
|
|
if (c->prompt_index == size)
|
2008-06-19 21:40:35 +02:00
|
|
|
left--;
|
|
|
|
size = left;
|
|
|
|
}
|
2009-11-22 01:11:33 +01:00
|
|
|
screen_write_nputs(
|
|
|
|
&ctx, left, &gc, utf8flag, "%s", c->prompt_buffer + off);
|
|
|
|
|
2009-12-04 23:14:47 +01:00
|
|
|
for (i = len + size; i < c->tty.sx; i++)
|
|
|
|
screen_write_putc(&ctx, &gc, ' ');
|
2009-05-29 07:40:56 +02:00
|
|
|
}
|
2008-06-19 21:40:35 +02:00
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
screen_write_stop(&ctx);
|
2009-02-10 01:18:06 +01:00
|
|
|
|
2009-11-22 01:11:33 +01:00
|
|
|
/* Apply fake cursor. */
|
|
|
|
off = len + c->prompt_index - off;
|
|
|
|
gcp = grid_view_get_cell(c->status.grid, off, 0);
|
|
|
|
gcp->attr ^= GRID_ATTR_REVERSE;
|
|
|
|
|
2009-02-10 01:18:06 +01:00
|
|
|
if (grid_compare(c->status.grid, old_status.grid) == 0) {
|
|
|
|
screen_free(&old_status);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
screen_free(&old_status);
|
|
|
|
return (1);
|
2008-06-19 21:40:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle keys in prompt. */
|
|
|
|
void
|
|
|
|
status_prompt_key(struct client *c, int key)
|
|
|
|
{
|
2011-11-25 14:34:20 +01:00
|
|
|
struct session *sess = c->session;
|
|
|
|
struct options *oo = &sess->options;
|
2009-05-14 21:36:56 +02:00
|
|
|
struct paste_buffer *pb;
|
2011-11-25 14:34:20 +01:00
|
|
|
char *s, *first, *last, word[64], swapc;
|
|
|
|
const char *histstr;
|
2012-01-20 22:17:18 +01:00
|
|
|
const char *wsep = NULL;
|
2009-11-28 15:54:12 +01:00
|
|
|
u_char ch;
|
2009-05-14 21:36:56 +02:00
|
|
|
size_t size, n, off, idx;
|
2008-06-19 21:40:35 +02:00
|
|
|
|
|
|
|
size = strlen(c->prompt_buffer);
|
2009-02-13 22:39:45 +01:00
|
|
|
switch (mode_key_lookup(&c->prompt_mdata, key)) {
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_CURSORLEFT:
|
2008-06-19 21:40:35 +02:00
|
|
|
if (c->prompt_index > 0) {
|
|
|
|
c->prompt_index--;
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
break;
|
2011-11-10 22:40:17 +01:00
|
|
|
case MODEKEYEDIT_SWITCHMODE:
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_SWITCHMODEAPPEND:
|
2011-11-10 22:40:17 +01:00
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
/* FALLTHROUGH */
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_CURSORRIGHT:
|
2008-06-19 21:40:35 +02:00
|
|
|
if (c->prompt_index < size) {
|
|
|
|
c->prompt_index++;
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_STARTOFLINE:
|
2008-06-19 21:40:35 +02:00
|
|
|
if (c->prompt_index != 0) {
|
|
|
|
c->prompt_index = 0;
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_ENDOFLINE:
|
2008-06-19 21:40:35 +02:00
|
|
|
if (c->prompt_index != size) {
|
|
|
|
c->prompt_index = size;
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_COMPLETE:
|
2009-01-06 18:04:56 +01:00
|
|
|
if (*c->prompt_buffer == '\0')
|
2008-06-21 16:11:39 +02:00
|
|
|
break;
|
2009-01-10 02:51:22 +01:00
|
|
|
|
2009-01-06 18:04:56 +01:00
|
|
|
idx = c->prompt_index;
|
|
|
|
if (idx != 0)
|
|
|
|
idx--;
|
|
|
|
|
|
|
|
/* Find the word we are in. */
|
|
|
|
first = c->prompt_buffer + idx;
|
|
|
|
while (first > c->prompt_buffer && *first != ' ')
|
|
|
|
first--;
|
|
|
|
while (*first == ' ')
|
|
|
|
first++;
|
|
|
|
last = c->prompt_buffer + idx;
|
|
|
|
while (*last != '\0' && *last != ' ')
|
|
|
|
last++;
|
|
|
|
while (*last == ' ')
|
|
|
|
last--;
|
|
|
|
if (*last != '\0')
|
|
|
|
last++;
|
2009-01-08 23:28:02 +01:00
|
|
|
if (last <= first ||
|
|
|
|
((size_t) (last - first)) > (sizeof word) - 1)
|
2008-06-21 16:11:39 +02:00
|
|
|
break;
|
2009-01-06 18:04:56 +01:00
|
|
|
memcpy(word, first, last - first);
|
|
|
|
word[last - first] = '\0';
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2009-01-06 18:04:56 +01:00
|
|
|
/* And try to complete it. */
|
|
|
|
if ((s = status_prompt_complete(word)) == NULL)
|
|
|
|
break;
|
2008-06-21 16:11:39 +02:00
|
|
|
|
2009-01-06 18:04:56 +01:00
|
|
|
/* Trim out word. */
|
|
|
|
n = size - (last - c->prompt_buffer) + 1; /* with \0 */
|
|
|
|
memmove(first, last, n);
|
|
|
|
size -= last - first;
|
|
|
|
|
|
|
|
/* Insert the new word. */
|
2009-12-04 23:14:47 +01:00
|
|
|
size += strlen(s);
|
2009-01-06 18:04:56 +01:00
|
|
|
off = first - c->prompt_buffer;
|
2009-12-04 23:14:47 +01:00
|
|
|
c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 1);
|
2009-01-06 18:04:56 +01:00
|
|
|
first = c->prompt_buffer + off;
|
2009-12-04 23:14:47 +01:00
|
|
|
memmove(first + strlen(s), first, n);
|
|
|
|
memcpy(first, s, strlen(s));
|
2009-01-06 18:04:56 +01:00
|
|
|
|
|
|
|
c->prompt_index = (first - c->prompt_buffer) + strlen(s);
|
2009-07-30 23:14:04 +02:00
|
|
|
xfree(s);
|
2008-06-21 16:11:39 +02:00
|
|
|
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_BACKSPACE:
|
2008-06-19 21:40:35 +02:00
|
|
|
if (c->prompt_index != 0) {
|
|
|
|
if (c->prompt_index == size)
|
|
|
|
c->prompt_buffer[--c->prompt_index] = '\0';
|
|
|
|
else {
|
2008-09-26 08:45:28 +02:00
|
|
|
memmove(c->prompt_buffer + c->prompt_index - 1,
|
2008-06-19 21:40:35 +02:00
|
|
|
c->prompt_buffer + c->prompt_index,
|
|
|
|
size + 1 - c->prompt_index);
|
|
|
|
c->prompt_index--;
|
|
|
|
}
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
break;
|
2009-12-04 23:14:47 +01:00
|
|
|
case MODEKEYEDIT_DELETE:
|
2008-06-19 21:40:35 +02:00
|
|
|
if (c->prompt_index != size) {
|
2008-09-26 08:45:28 +02:00
|
|
|
memmove(c->prompt_buffer + c->prompt_index,
|
2008-06-19 21:40:35 +02:00
|
|
|
c->prompt_buffer + c->prompt_index + 1,
|
|
|
|
size + 1 - c->prompt_index);
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
break;
|
2009-08-20 13:22:48 +02:00
|
|
|
case MODEKEYEDIT_DELETELINE:
|
|
|
|
*c->prompt_buffer = '\0';
|
|
|
|
c->prompt_index = 0;
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_DELETETOENDOFLINE:
|
2009-07-29 00:44:38 +02:00
|
|
|
if (c->prompt_index < size) {
|
|
|
|
c->prompt_buffer[c->prompt_index] = '\0';
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
break;
|
2011-11-25 14:34:20 +01:00
|
|
|
case MODEKEYEDIT_DELETEWORD:
|
|
|
|
wsep = options_get_string(oo, "word-separators");
|
|
|
|
idx = c->prompt_index;
|
|
|
|
|
|
|
|
/* Find a non-separator. */
|
|
|
|
while (idx != 0) {
|
|
|
|
idx--;
|
|
|
|
if (!strchr(wsep, c->prompt_buffer[idx]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the separator at the beginning of the word. */
|
|
|
|
while (idx != 0) {
|
|
|
|
idx--;
|
|
|
|
if (strchr(wsep, c->prompt_buffer[idx])) {
|
|
|
|
/* Go back to the word. */
|
|
|
|
idx++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(c->prompt_buffer + idx,
|
|
|
|
c->prompt_buffer + c->prompt_index,
|
|
|
|
size + 1 - c->prompt_index);
|
|
|
|
memset(c->prompt_buffer + size - (c->prompt_index - idx),
|
|
|
|
'\0', c->prompt_index - idx);
|
|
|
|
c->prompt_index = idx;
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2012-01-20 22:17:18 +01:00
|
|
|
case MODEKEYEDIT_NEXTSPACE:
|
|
|
|
wsep = " ";
|
|
|
|
/* FALLTHROUGH */
|
2011-11-25 14:34:20 +01:00
|
|
|
case MODEKEYEDIT_NEXTWORD:
|
2012-01-20 22:17:18 +01:00
|
|
|
if (wsep == NULL)
|
|
|
|
wsep = options_get_string(oo, "word-separators");
|
2011-11-25 14:34:20 +01:00
|
|
|
|
|
|
|
/* Find a separator. */
|
|
|
|
while (c->prompt_index != size) {
|
|
|
|
c->prompt_index++;
|
|
|
|
if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the word right after the separation. */
|
|
|
|
while (c->prompt_index != size) {
|
|
|
|
c->prompt_index++;
|
|
|
|
if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2012-01-20 22:17:18 +01:00
|
|
|
case MODEKEYEDIT_NEXTSPACEEND:
|
|
|
|
wsep = " ";
|
|
|
|
/* FALLTHROUGH */
|
2011-11-25 14:34:20 +01:00
|
|
|
case MODEKEYEDIT_NEXTWORDEND:
|
2012-01-20 22:17:18 +01:00
|
|
|
if (wsep == NULL)
|
|
|
|
wsep = options_get_string(oo, "word-separators");
|
2011-11-25 14:34:20 +01:00
|
|
|
|
|
|
|
/* Find a word. */
|
|
|
|
while (c->prompt_index != size) {
|
|
|
|
c->prompt_index++;
|
|
|
|
if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the separator at the end of the word. */
|
|
|
|
while (c->prompt_index != size) {
|
|
|
|
c->prompt_index++;
|
2011-12-06 19:47:14 +01:00
|
|
|
if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
|
2011-11-25 14:34:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2012-01-20 22:17:18 +01:00
|
|
|
case MODEKEYEDIT_PREVIOUSSPACE:
|
|
|
|
wsep = " ";
|
|
|
|
/* FALLTHROUGH */
|
2011-11-25 14:34:20 +01:00
|
|
|
case MODEKEYEDIT_PREVIOUSWORD:
|
2012-01-20 22:17:18 +01:00
|
|
|
if (wsep == NULL)
|
|
|
|
wsep = options_get_string(oo, "word-separators");
|
2011-11-25 14:34:20 +01:00
|
|
|
|
|
|
|
/* Find a non-separator. */
|
|
|
|
while (c->prompt_index != 0) {
|
|
|
|
c->prompt_index--;
|
|
|
|
if (!strchr(wsep, c->prompt_buffer[c->prompt_index]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the separator at the beginning of the word. */
|
|
|
|
while (c->prompt_index != 0) {
|
|
|
|
c->prompt_index--;
|
|
|
|
if (strchr(wsep, c->prompt_buffer[c->prompt_index])) {
|
|
|
|
/* Go back to the word. */
|
|
|
|
c->prompt_index++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_HISTORYUP:
|
2010-12-11 17:15:02 +01:00
|
|
|
histstr = status_prompt_up_history(&c->prompt_hindex);
|
|
|
|
if (histstr == NULL)
|
2009-01-06 16:37:15 +01:00
|
|
|
break;
|
2011-11-25 14:34:20 +01:00
|
|
|
xfree(c->prompt_buffer);
|
2010-12-11 17:15:02 +01:00
|
|
|
c->prompt_buffer = xstrdup(histstr);
|
2009-01-06 16:37:15 +01:00
|
|
|
c->prompt_index = strlen(c->prompt_buffer);
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_HISTORYDOWN:
|
2010-12-11 17:15:02 +01:00
|
|
|
histstr = status_prompt_down_history(&c->prompt_hindex);
|
|
|
|
if (histstr == NULL)
|
|
|
|
break;
|
2009-01-06 16:37:15 +01:00
|
|
|
xfree(c->prompt_buffer);
|
2010-12-11 17:15:02 +01:00
|
|
|
c->prompt_buffer = xstrdup(histstr);
|
2009-01-06 16:37:15 +01:00
|
|
|
c->prompt_index = strlen(c->prompt_buffer);
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_PASTE:
|
2010-12-30 23:39:49 +01:00
|
|
|
if ((pb = paste_get_top(&global_buffers)) == NULL)
|
2009-05-14 21:36:56 +02:00
|
|
|
break;
|
2009-09-08 01:48:54 +02:00
|
|
|
for (n = 0; n < pb->size; n++) {
|
2009-11-28 15:54:12 +01:00
|
|
|
ch = (u_char) pb->data[n];
|
|
|
|
if (ch < 32 || ch == 127)
|
2009-09-08 01:48:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-05-14 21:36:56 +02:00
|
|
|
|
|
|
|
c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1);
|
|
|
|
if (c->prompt_index == size) {
|
|
|
|
memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
|
|
|
|
c->prompt_index += n;
|
|
|
|
c->prompt_buffer[c->prompt_index] = '\0';
|
|
|
|
} else {
|
|
|
|
memmove(c->prompt_buffer + c->prompt_index + n,
|
|
|
|
c->prompt_buffer + c->prompt_index,
|
|
|
|
size + 1 - c->prompt_index);
|
|
|
|
memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
|
|
|
|
c->prompt_index += n;
|
|
|
|
}
|
|
|
|
|
2009-06-25 18:34:50 +02:00
|
|
|
c->flags |= CLIENT_STATUS;
|
2009-05-14 21:36:56 +02:00
|
|
|
break;
|
2009-09-03 00:45:17 +02:00
|
|
|
case MODEKEYEDIT_TRANSPOSECHARS:
|
|
|
|
idx = c->prompt_index;
|
|
|
|
if (idx < size)
|
|
|
|
idx++;
|
|
|
|
if (idx >= 2) {
|
|
|
|
swapc = c->prompt_buffer[idx - 2];
|
|
|
|
c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1];
|
|
|
|
c->prompt_buffer[idx - 1] = swapc;
|
|
|
|
c->prompt_index = idx;
|
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
}
|
|
|
|
break;
|
2009-12-04 23:14:47 +01:00
|
|
|
case MODEKEYEDIT_ENTER:
|
2009-08-16 21:29:24 +02:00
|
|
|
if (*c->prompt_buffer != '\0')
|
2010-12-11 17:15:02 +01:00
|
|
|
status_prompt_add_history(c->prompt_buffer);
|
2009-08-16 21:29:24 +02:00
|
|
|
if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0)
|
|
|
|
status_prompt_clear(c);
|
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEYEDIT_CANCEL:
|
2009-07-17 11:26:21 +02:00
|
|
|
if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
|
2009-02-13 19:57:55 +01:00
|
|
|
status_prompt_clear(c);
|
2008-06-19 21:40:35 +02:00
|
|
|
break;
|
2009-07-29 00:55:59 +02:00
|
|
|
case MODEKEY_OTHER:
|
2010-03-31 20:24:08 +02:00
|
|
|
if ((key & 0xff00) != 0 || key < 32 || key == 127)
|
2008-06-19 21:40:35 +02:00
|
|
|
break;
|
|
|
|
c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 2);
|
|
|
|
|
|
|
|
if (c->prompt_index == size) {
|
|
|
|
c->prompt_buffer[c->prompt_index++] = key;
|
|
|
|
c->prompt_buffer[c->prompt_index] = '\0';
|
|
|
|
} else {
|
2008-09-26 08:45:28 +02:00
|
|
|
memmove(c->prompt_buffer + c->prompt_index + 1,
|
2008-06-19 21:40:35 +02:00
|
|
|
c->prompt_buffer + c->prompt_index,
|
|
|
|
size + 1 - c->prompt_index);
|
|
|
|
c->prompt_buffer[c->prompt_index++] = key;
|
|
|
|
}
|
|
|
|
|
2009-04-27 19:27:36 +02:00
|
|
|
if (c->prompt_flags & PROMPT_SINGLE) {
|
2009-07-17 11:26:21 +02:00
|
|
|
if (c->prompt_callbackfn(
|
2009-04-27 19:27:36 +02:00
|
|
|
c->prompt_data, c->prompt_buffer) == 0)
|
|
|
|
status_prompt_clear(c);
|
|
|
|
}
|
|
|
|
|
2008-06-19 21:40:35 +02:00
|
|
|
c->flags |= CLIENT_STATUS;
|
|
|
|
break;
|
2009-02-13 22:39:45 +01:00
|
|
|
default:
|
|
|
|
break;
|
2008-06-19 21:40:35 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-06 16:37:15 +01:00
|
|
|
|
2010-12-11 17:15:02 +01:00
|
|
|
/* Get previous line from the history. */
|
|
|
|
const char *
|
|
|
|
status_prompt_up_history(u_int *idx)
|
|
|
|
{
|
|
|
|
u_int size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* History runs from 0 to size - 1.
|
|
|
|
*
|
|
|
|
* Index is from 0 to size. Zero is empty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
size = ARRAY_LENGTH(&status_prompt_history);
|
|
|
|
if (size == 0 || *idx == size)
|
|
|
|
return (NULL);
|
|
|
|
(*idx)++;
|
|
|
|
return (ARRAY_ITEM(&status_prompt_history, size - *idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next line from the history. */
|
|
|
|
const char *
|
|
|
|
status_prompt_down_history(u_int *idx)
|
|
|
|
{
|
|
|
|
u_int size;
|
|
|
|
|
|
|
|
size = ARRAY_LENGTH(&status_prompt_history);
|
|
|
|
if (size == 0 || *idx == 0)
|
|
|
|
return ("");
|
|
|
|
(*idx)--;
|
|
|
|
if (*idx == 0)
|
|
|
|
return ("");
|
|
|
|
return (ARRAY_ITEM(&status_prompt_history, size - *idx));
|
|
|
|
}
|
|
|
|
|
2009-01-06 16:37:15 +01:00
|
|
|
/* Add line to the history. */
|
|
|
|
void
|
2010-12-11 17:15:02 +01:00
|
|
|
status_prompt_add_history(const char *line)
|
2009-01-06 16:37:15 +01:00
|
|
|
{
|
2010-12-11 17:15:02 +01:00
|
|
|
u_int size;
|
|
|
|
|
|
|
|
size = ARRAY_LENGTH(&status_prompt_history);
|
|
|
|
if (size > 0 && strcmp(ARRAY_LAST(&status_prompt_history), line) == 0)
|
2009-01-06 16:37:15 +01:00
|
|
|
return;
|
2009-01-10 02:51:22 +01:00
|
|
|
|
2010-12-11 17:15:02 +01:00
|
|
|
if (size == PROMPT_HISTORY) {
|
|
|
|
xfree(ARRAY_FIRST(&status_prompt_history));
|
|
|
|
ARRAY_REMOVE(&status_prompt_history, 0);
|
2009-01-06 16:37:15 +01:00
|
|
|
}
|
|
|
|
|
2010-12-11 17:15:02 +01:00
|
|
|
ARRAY_ADD(&status_prompt_history, xstrdup(line));
|
2009-01-06 16:37:15 +01:00
|
|
|
}
|
2009-01-06 18:04:56 +01:00
|
|
|
|
|
|
|
/* Complete word. */
|
|
|
|
char *
|
|
|
|
status_prompt_complete(const char *s)
|
|
|
|
{
|
2011-01-04 00:52:38 +01:00
|
|
|
const struct cmd_entry **cmdent;
|
|
|
|
const struct options_table_entry *oe;
|
|
|
|
ARRAY_DECL(, const char *) list;
|
|
|
|
char *prefix, *s2;
|
|
|
|
u_int i;
|
|
|
|
size_t j;
|
2009-01-06 18:04:56 +01:00
|
|
|
|
|
|
|
if (*s == '\0')
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* First, build a list of all the possible matches. */
|
|
|
|
ARRAY_INIT(&list);
|
|
|
|
for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
|
|
|
|
if (strncmp((*cmdent)->name, s, strlen(s)) == 0)
|
|
|
|
ARRAY_ADD(&list, (*cmdent)->name);
|
|
|
|
}
|
2011-01-04 00:52:38 +01:00
|
|
|
for (oe = server_options_table; oe->name != NULL; oe++) {
|
|
|
|
if (strncmp(oe->name, s, strlen(s)) == 0)
|
|
|
|
ARRAY_ADD(&list, oe->name);
|
2009-12-16 02:10:36 +01:00
|
|
|
}
|
2011-01-04 00:52:38 +01:00
|
|
|
for (oe = session_options_table; oe->name != NULL; oe++) {
|
|
|
|
if (strncmp(oe->name, s, strlen(s)) == 0)
|
|
|
|
ARRAY_ADD(&list, oe->name);
|
2009-01-06 18:04:56 +01:00
|
|
|
}
|
2011-01-04 00:52:38 +01:00
|
|
|
for (oe = window_options_table; oe->name != NULL; oe++) {
|
|
|
|
if (strncmp(oe->name, s, strlen(s)) == 0)
|
|
|
|
ARRAY_ADD(&list, oe->name);
|
2009-01-06 18:04:56 +01:00
|
|
|
}
|
2009-01-10 02:51:22 +01:00
|
|
|
|
2009-01-06 18:04:56 +01:00
|
|
|
/* If none, bail now. */
|
|
|
|
if (ARRAY_LENGTH(&list) == 0) {
|
|
|
|
ARRAY_FREE(&list);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2009-01-10 02:51:22 +01:00
|
|
|
|
2009-01-06 18:04:56 +01:00
|
|
|
/* If an exact match, return it, with a trailing space. */
|
|
|
|
if (ARRAY_LENGTH(&list) == 1) {
|
|
|
|
xasprintf(&s2, "%s ", ARRAY_FIRST(&list));
|
|
|
|
ARRAY_FREE(&list);
|
|
|
|
return (s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now loop through the list and find the longest common prefix. */
|
|
|
|
prefix = xstrdup(ARRAY_FIRST(&list));
|
|
|
|
for (i = 1; i < ARRAY_LENGTH(&list); i++) {
|
|
|
|
s = ARRAY_ITEM(&list, i);
|
|
|
|
|
|
|
|
j = strlen(s);
|
|
|
|
if (j > strlen(prefix))
|
|
|
|
j = strlen(prefix);
|
|
|
|
for (; j > 0; j--) {
|
|
|
|
if (prefix[j - 1] != s[j - 1])
|
|
|
|
prefix[j - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ARRAY_FREE(&list);
|
|
|
|
return (prefix);
|
|
|
|
}
|