4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
32 char *status_redraw_get_left(
33 struct client
*, time_t, int, struct grid_cell
*, size_t *);
34 char *status_redraw_get_right(
35 struct client
*, time_t, int, struct grid_cell
*, size_t *);
36 char *status_find_job(struct client
*, char **);
37 void status_job_free(void *);
38 void status_job_callback(struct job
*);
40 struct client
*, struct winlink
*, time_t, struct grid_cell
*);
41 void status_replace1(struct client
*, struct session
*, struct winlink
*,
42 struct window_pane
*, char **, char **, char *, size_t, int);
43 void status_message_callback(int, short, void *);
45 const char *status_prompt_up_history(u_int
*);
46 const char *status_prompt_down_history(u_int
*);
47 void status_prompt_add_history(const char *);
48 char *status_prompt_complete(const char *);
50 /* Status prompt history. */
51 ARRAY_DECL(, char *) status_prompt_history
= ARRAY_INITIALIZER
;
53 /* Status output tree. */
54 RB_GENERATE(status_out_tree
, status_out
, entry
, status_out_cmp
);
56 /* Output tree comparison function. */
58 status_out_cmp(struct status_out
*so1
, struct status_out
*so2
)
60 return (strcmp(so1
->cmd
, so2
->cmd
));
63 /* Get screen line of status line. -1 means off. */
65 status_at_line(struct client
*c
)
67 struct session
*s
= c
->session
;
69 if (!options_get_number(&s
->options
, "status"))
72 if (options_get_number(&s
->options
, "status-position") == 0)
74 return (c
->tty
.sy
- 1);
77 /* Retrieve options for left string. */
79 status_redraw_get_left(struct client
*c
,
80 time_t t
, int utf8flag
, struct grid_cell
*gc
, size_t *size
)
82 struct session
*s
= c
->session
;
87 fg
= options_get_number(&s
->options
, "status-left-fg");
89 colour_set_fg(gc
, fg
);
90 bg
= options_get_number(&s
->options
, "status-left-bg");
92 colour_set_bg(gc
, bg
);
93 attr
= options_get_number(&s
->options
, "status-left-attr");
97 left
= status_replace(c
, NULL
,
98 NULL
, NULL
, options_get_string(&s
->options
, "status-left"), t
, 1);
100 *size
= options_get_number(&s
->options
, "status-left-length");
101 leftlen
= screen_write_cstrlen(utf8flag
, "%s", left
);
107 /* Retrieve options for right string. */
109 status_redraw_get_right(struct client
*c
,
110 time_t t
, int utf8flag
, struct grid_cell
*gc
, size_t *size
)
112 struct session
*s
= c
->session
;
117 fg
= options_get_number(&s
->options
, "status-right-fg");
119 colour_set_fg(gc
, fg
);
120 bg
= options_get_number(&s
->options
, "status-right-bg");
122 colour_set_bg(gc
, bg
);
123 attr
= options_get_number(&s
->options
, "status-right-attr");
127 right
= status_replace(c
, NULL
,
128 NULL
, NULL
, options_get_string(&s
->options
, "status-right"), t
, 1);
130 *size
= options_get_number(&s
->options
, "status-right-length");
131 rightlen
= screen_write_cstrlen(utf8flag
, "%s", right
);
132 if (rightlen
< *size
)
137 /* Set window at window list position. */
139 status_set_window_at(struct client
*c
, u_int x
)
141 struct session
*s
= c
->session
;
145 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
146 if (x
< wl
->status_width
&&
147 session_select(s
, wl
->idx
) == 0) {
148 server_redraw_session(s
);
150 x
-= wl
->status_width
+ 1;
154 /* Draw status for client on the last lines of given context. */
156 status_redraw(struct client
*c
)
158 struct screen_write_ctx ctx
;
159 struct session
*s
= c
->session
;
161 struct screen old_status
, window_list
;
162 struct grid_cell stdgc
, lgc
, rgc
, gc
;
165 u_int offset
, needed
;
166 u_int wlstart
, wlwidth
, wlavailable
, wloffset
, wlsize
;
168 int larrow
, rarrow
, utf8flag
;
170 /* No status line? */
171 if (c
->tty
.sy
== 0 || !options_get_number(&s
->options
, "status"))
176 /* Update status timer. */
177 if (gettimeofday(&c
->status_timer
, NULL
) != 0)
178 fatal("gettimeofday failed");
179 t
= c
->status_timer
.tv_sec
;
181 /* Set up default colour. */
182 memcpy(&stdgc
, &grid_default_cell
, sizeof gc
);
183 colour_set_fg(&stdgc
, options_get_number(&s
->options
, "status-fg"));
184 colour_set_bg(&stdgc
, options_get_number(&s
->options
, "status-bg"));
185 stdgc
.attr
|= options_get_number(&s
->options
, "status-attr");
187 /* Create the target screen. */
188 memcpy(&old_status
, &c
->status
, sizeof old_status
);
189 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
190 screen_write_start(&ctx
, NULL
, &c
->status
);
191 for (offset
= 0; offset
< c
->tty
.sx
; offset
++)
192 screen_write_putc(&ctx
, &stdgc
, ' ');
193 screen_write_stop(&ctx
);
195 /* If the height is one line, blank status line. */
199 /* Get UTF-8 flag. */
200 utf8flag
= options_get_number(&s
->options
, "status-utf8");
202 /* Work out left and right strings. */
203 memcpy(&lgc
, &stdgc
, sizeof lgc
);
204 left
= status_redraw_get_left(c
, t
, utf8flag
, &lgc
, &llen
);
205 memcpy(&rgc
, &stdgc
, sizeof rgc
);
206 right
= status_redraw_get_right(c
, t
, utf8flag
, &rgc
, &rlen
);
209 * Figure out how much space we have for the window list. If there
210 * isn't enough space, just show a blank status line.
217 if (c
->tty
.sx
== 0 || c
->tty
.sx
<= needed
)
219 wlavailable
= c
->tty
.sx
- needed
;
221 /* Calculate the total size needed for the window list. */
222 wlstart
= wloffset
= wlwidth
= 0;
223 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
224 if (wl
->status_text
!= NULL
)
225 xfree(wl
->status_text
);
226 memcpy(&wl
->status_cell
, &stdgc
, sizeof wl
->status_cell
);
227 wl
->status_text
= status_print(c
, wl
, t
, &wl
->status_cell
);
229 screen_write_cstrlen(utf8flag
, "%s", wl
->status_text
);
233 wlwidth
+= wl
->status_width
+ 1;
236 /* Create a new screen for the window list. */
237 screen_init(&window_list
, wlwidth
, 1, 0);
239 /* And draw the window list into it. */
240 screen_write_start(&ctx
, NULL
, &window_list
);
241 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
242 screen_write_cnputs(&ctx
,
243 -1, &wl
->status_cell
, utf8flag
, "%s", wl
->status_text
);
244 screen_write_putc(&ctx
, &stdgc
, ' ');
246 screen_write_stop(&ctx
);
248 /* If there is enough space for the total width, skip to draw now. */
249 if (wlwidth
<= wlavailable
)
252 /* Find size of current window text. */
253 wlsize
= s
->curw
->status_width
;
256 * If the current window is already on screen, good to draw from the
257 * start and just leave off the end.
259 if (wloffset
+ wlsize
< wlavailable
) {
260 if (wlavailable
> 0) {
264 wlwidth
= wlavailable
;
267 * Work out how many characters we need to omit from the
268 * start. There are wlavailable characters to fill, and
269 * wloffset + wlsize must be the last. So, the start character
270 * is wloffset + wlsize - wlavailable.
272 if (wlavailable
> 0) {
277 wlstart
= wloffset
+ wlsize
- wlavailable
;
278 if (wlavailable
> 0 && wlwidth
> wlstart
+ wlavailable
+ 1) {
283 wlwidth
= wlavailable
;
286 /* Bail if anything is now too small too. */
287 if (wlwidth
== 0 || wlavailable
== 0) {
288 screen_free(&window_list
);
293 * Now the start position is known, work out the state of the left and
297 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
298 if (wl
->flags
& WINLINK_ALERTFLAGS
&&
299 larrow
== 1 && offset
< wlstart
)
302 offset
+= wl
->status_width
;
304 if (wl
->flags
& WINLINK_ALERTFLAGS
&&
305 rarrow
== 1 && offset
> wlstart
+ wlwidth
)
311 screen_write_start(&ctx
, NULL
, &c
->status
);
313 /* Draw the left string and arrow. */
314 screen_write_cursormove(&ctx
, 0, 0);
316 screen_write_cnputs(&ctx
, llen
, &lgc
, utf8flag
, "%s", left
);
317 screen_write_putc(&ctx
, &stdgc
, ' ');
320 memcpy(&gc
, &stdgc
, sizeof gc
);
322 gc
.attr
^= GRID_ATTR_REVERSE
;
323 screen_write_putc(&ctx
, &gc
, '<');
326 /* Draw the right string and arrow. */
328 screen_write_cursormove(&ctx
, c
->tty
.sx
- rlen
- 2, 0);
329 memcpy(&gc
, &stdgc
, sizeof gc
);
331 gc
.attr
^= GRID_ATTR_REVERSE
;
332 screen_write_putc(&ctx
, &gc
, '>');
334 screen_write_cursormove(&ctx
, c
->tty
.sx
- rlen
- 1, 0);
336 screen_write_putc(&ctx
, &stdgc
, ' ');
337 screen_write_cnputs(&ctx
, rlen
, &rgc
, utf8flag
, "%s", right
);
340 /* Figure out the offset for the window list. */
345 if (wlwidth
< wlavailable
) {
346 switch (options_get_number(&s
->options
, "status-justify")) {
347 case 1: /* centered */
348 wloffset
+= (wlavailable
- wlwidth
) / 2;
351 wloffset
+= (wlavailable
- wlwidth
);
358 /* Copy the window list. */
359 s
->wlmouse
= -wloffset
+ wlstart
;
360 screen_write_cursormove(&ctx
, wloffset
, 0);
361 screen_write_copy(&ctx
, &window_list
, wlstart
, 0, wlwidth
, 1);
362 screen_free(&window_list
);
364 screen_write_stop(&ctx
);
372 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
373 screen_free(&old_status
);
376 screen_free(&old_status
);
380 /* Replace a single special sequence (prefixed by #). */
382 status_replace1(struct client
*c
, struct session
*s
, struct winlink
*wl
,
383 struct window_pane
*wp
, char **iptr
, char **optr
, char *out
,
384 size_t outsize
, int jobsflag
)
386 char ch
, tmp
[256], *ptr
, *endptr
, *freeptr
;
396 wp
= wl
->window
->active
;
399 limit
= strtol(*iptr
, &endptr
, 10);
400 if ((limit
== 0 && errno
!= EINVAL
) ||
401 (limit
== LONG_MIN
&& errno
!= ERANGE
) ||
402 (limit
== LONG_MAX
&& errno
!= ERANGE
) ||
410 switch (*(*iptr
)++) {
416 if ((ptr
= status_find_job(c
, iptr
)) == NULL
)
420 xsnprintf(tmp
, sizeof tmp
, "%%%u", wp
->id
);
424 if (gethostname(tmp
, sizeof tmp
) != 0)
425 fatal("gethostname failed");
429 if (gethostname(tmp
, sizeof tmp
) != 0)
430 fatal("gethostname failed");
431 if ((ptr
= strchr(tmp
, '.')) != NULL
)
436 xsnprintf(tmp
, sizeof tmp
, "%d", wl
->idx
);
440 if (window_pane_index(wp
, &idx
) != 0)
441 fatalx("index not found");
443 tmp
, sizeof tmp
, "%u", idx
);
450 ptr
= wp
->base
.title
;
453 ptr
= wl
->window
->name
;
456 ptr
= window_printable_flags(s
, wl
);
461 * Embedded style, handled at display time. Leave present and
462 * skip input until ].
474 ptrlen
= strlen(ptr
);
475 if ((size_t) limit
< ptrlen
)
478 if (*optr
+ ptrlen
>= out
+ outsize
- 1)
480 while (ptrlen
> 0 && *ptr
!= '\0') {
493 (*iptr
)--; /* include ch */
494 while (**iptr
!= ch
&& **iptr
!= '\0') {
495 if (*optr
>= out
+ outsize
- 1)
497 *(*optr
)++ = *(*iptr
)++;
501 /* Replace special sequences in fmt. */
503 status_replace(struct client
*c
, struct session
*s
, struct winlink
*wl
,
504 struct window_pane
*wp
, const char *fmt
, time_t t
, int jobsflag
)
506 static char out
[BUFSIZ
];
507 char in
[BUFSIZ
], ch
, *iptr
, *optr
;
510 len
= strftime(in
, sizeof in
, fmt
, localtime(&t
));
516 while (*iptr
!= '\0') {
517 if (optr
>= out
+ (sizeof out
) - 1)
521 if (ch
!= '#' || *iptr
== '\0') {
526 c
, s
, wl
, wp
, &iptr
, &optr
, out
, sizeof out
, jobsflag
);
530 return (xstrdup(out
));
533 /* Figure out job name and get its result, starting it off if necessary. */
535 status_find_job(struct client
*c
, char **iptr
)
537 struct status_out
*so
, so_find
;
544 if (**iptr
== ')') { /* no command given */
549 cmd
= xmalloc(strlen(*iptr
) + 1);
553 for (; **iptr
!= '\0'; (*iptr
)++) {
554 if (!lastesc
&& **iptr
== ')')
555 break; /* unescaped ) is the end */
556 if (!lastesc
&& **iptr
== '\\') {
558 continue; /* skip \ if not escaped */
563 if (**iptr
== '\0') /* no terminating ) */ {
567 (*iptr
)++; /* skip final ) */
570 /* First try in the new tree. */
572 so
= RB_FIND(status_out_tree
, &c
->status_new
, &so_find
);
573 if (so
!= NULL
&& so
->out
!= NULL
) {
578 /* If not found at all, start the job and add to the tree. */
580 job_run(cmd
, status_job_callback
, status_job_free
, c
);
583 so
= xmalloc(sizeof *so
);
584 so
->cmd
= xstrdup(cmd
);
586 RB_INSERT(status_out_tree
, &c
->status_new
, so
);
589 /* Lookup in the old tree. */
591 so
= RB_FIND(status_out_tree
, &c
->status_old
, &so_find
);
600 status_free_jobs(struct status_out_tree
*sotree
)
602 struct status_out
*so
, *so_next
;
604 so_next
= RB_MIN(status_out_tree
, sotree
);
605 while (so_next
!= NULL
) {
607 so_next
= RB_NEXT(status_out_tree
, sotree
, so
);
609 RB_REMOVE(status_out_tree
, sotree
, so
);
617 /* Update jobs on status interval. */
619 status_update_jobs(struct client
*c
)
621 /* Free the old tree. */
622 status_free_jobs(&c
->status_old
);
624 /* Move the new to old. */
625 memcpy(&c
->status_old
, &c
->status_new
, sizeof c
->status_old
);
626 RB_INIT(&c
->status_new
);
629 /* Free status job. */
631 status_job_free(void *data
)
633 struct client
*c
= data
;
638 /* Job has finished: save its result. */
640 status_job_callback(struct job
*job
)
642 struct client
*c
= job
->data
;
643 struct status_out
*so
, so_find
;
647 if (c
->flags
& CLIENT_DEAD
)
650 so_find
.cmd
= job
->cmd
;
651 so
= RB_FIND(status_out_tree
, &c
->status_new
, &so_find
);
652 if (so
== NULL
|| so
->out
!= NULL
)
656 if ((line
= evbuffer_readline(job
->event
->input
)) == NULL
) {
657 len
= EVBUFFER_LENGTH(job
->event
->input
);
658 buf
= xmalloc(len
+ 1);
660 memcpy(buf
, EVBUFFER_DATA(job
->event
->input
), len
);
666 server_status_client(c
);
669 /* Return winlink status line entry and adjust gc as necessary. */
672 struct client
*c
, struct winlink
*wl
, time_t t
, struct grid_cell
*gc
)
674 struct options
*oo
= &wl
->window
->options
;
675 struct session
*s
= c
->session
;
680 fg
= options_get_number(oo
, "window-status-fg");
682 colour_set_fg(gc
, fg
);
683 bg
= options_get_number(oo
, "window-status-bg");
685 colour_set_bg(gc
, bg
);
686 attr
= options_get_number(oo
, "window-status-attr");
689 fmt
= options_get_string(oo
, "window-status-format");
691 fg
= options_get_number(oo
, "window-status-current-fg");
693 colour_set_fg(gc
, fg
);
694 bg
= options_get_number(oo
, "window-status-current-bg");
696 colour_set_bg(gc
, bg
);
697 attr
= options_get_number(oo
, "window-status-current-attr");
700 fmt
= options_get_string(oo
, "window-status-current-format");
703 if (wl
->flags
& WINLINK_BELL
) {
704 fg
= options_get_number(oo
, "window-status-bell-fg");
706 colour_set_fg(gc
, fg
);
707 bg
= options_get_number(oo
, "window-status-bell-bg");
709 colour_set_bg(gc
, bg
);
710 attr
= options_get_number(oo
, "window-status-bell-attr");
713 } else if (wl
->flags
& WINLINK_CONTENT
) {
714 fg
= options_get_number(oo
, "window-status-content-fg");
716 colour_set_fg(gc
, fg
);
717 bg
= options_get_number(oo
, "window-status-content-bg");
719 colour_set_bg(gc
, bg
);
720 attr
= options_get_number(oo
, "window-status-content-attr");
723 } else if (wl
->flags
& (WINLINK_ACTIVITY
|WINLINK_SILENCE
)) {
724 fg
= options_get_number(oo
, "window-status-activity-fg");
726 colour_set_fg(gc
, fg
);
727 bg
= options_get_number(oo
, "window-status-activity-bg");
729 colour_set_bg(gc
, bg
);
730 attr
= options_get_number(oo
, "window-status-activity-attr");
735 text
= status_replace(c
, NULL
, wl
, NULL
, fmt
, t
, 1);
739 /* Set a status line message. */
741 status_message_set(struct client
*c
, const char *fmt
, ...)
744 struct session
*s
= c
->session
;
745 struct message_entry
*msg
;
750 status_prompt_clear(c
);
751 status_message_clear(c
);
754 xvasprintf(&c
->message_string
, fmt
, ap
);
757 ARRAY_EXPAND(&c
->message_log
, 1);
758 msg
= &ARRAY_LAST(&c
->message_log
);
759 msg
->msg_time
= time(NULL
);
760 msg
->msg
= xstrdup(c
->message_string
);
765 limit
= options_get_number(&s
->options
, "message-limit");
766 if (ARRAY_LENGTH(&c
->message_log
) > limit
) {
767 limit
= ARRAY_LENGTH(&c
->message_log
) - limit
;
768 for (i
= 0; i
< limit
; i
++) {
769 msg
= &ARRAY_FIRST(&c
->message_log
);
771 ARRAY_REMOVE(&c
->message_log
, 0);
775 delay
= options_get_number(&c
->session
->options
, "display-time");
776 tv
.tv_sec
= delay
/ 1000;
777 tv
.tv_usec
= (delay
% 1000) * 1000L;
779 evtimer_del(&c
->message_timer
);
780 evtimer_set(&c
->message_timer
, status_message_callback
, c
);
781 evtimer_add(&c
->message_timer
, &tv
);
783 c
->tty
.flags
|= (TTY_NOCURSOR
|TTY_FREEZE
);
784 c
->flags
|= CLIENT_STATUS
;
787 /* Clear status line message. */
789 status_message_clear(struct client
*c
)
791 if (c
->message_string
== NULL
)
794 xfree(c
->message_string
);
795 c
->message_string
= NULL
;
797 c
->tty
.flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
);
798 c
->flags
|= CLIENT_REDRAW
; /* screen was frozen and may have changed */
800 screen_reinit(&c
->status
);
803 /* Clear status line message after timer expires. */
806 status_message_callback(unused
int fd
, unused
short event
, void *data
)
808 struct client
*c
= data
;
810 status_message_clear(c
);
813 /* Draw client message on status line of present else on last line. */
815 status_message_redraw(struct client
*c
)
817 struct screen_write_ctx ctx
;
818 struct session
*s
= c
->session
;
819 struct screen old_status
;
824 if (c
->tty
.sx
== 0 || c
->tty
.sy
== 0)
826 memcpy(&old_status
, &c
->status
, sizeof old_status
);
827 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
829 utf8flag
= options_get_number(&s
->options
, "status-utf8");
831 len
= screen_write_strlen(utf8flag
, "%s", c
->message_string
);
835 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
836 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-fg"));
837 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-bg"));
838 gc
.attr
|= options_get_number(&s
->options
, "message-attr");
840 screen_write_start(&ctx
, NULL
, &c
->status
);
842 screen_write_cursormove(&ctx
, 0, 0);
843 screen_write_nputs(&ctx
, len
, &gc
, utf8flag
, "%s", c
->message_string
);
844 for (; len
< c
->tty
.sx
; len
++)
845 screen_write_putc(&ctx
, &gc
, ' ');
847 screen_write_stop(&ctx
);
849 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
850 screen_free(&old_status
);
853 screen_free(&old_status
);
857 /* Enable status line prompt. */
859 status_prompt_set(struct client
*c
, const char *msg
, const char *input
,
860 int (*callbackfn
)(void *, const char *), void (*freefn
)(void *),
861 void *data
, int flags
)
865 status_message_clear(c
);
866 status_prompt_clear(c
);
868 c
->prompt_string
= status_replace(c
, NULL
, NULL
, NULL
, msg
,
873 c
->prompt_buffer
= status_replace(c
, NULL
, NULL
, NULL
, input
,
875 c
->prompt_index
= strlen(c
->prompt_buffer
);
877 c
->prompt_callbackfn
= callbackfn
;
878 c
->prompt_freefn
= freefn
;
879 c
->prompt_data
= data
;
881 c
->prompt_hindex
= 0;
883 c
->prompt_flags
= flags
;
885 keys
= options_get_number(&c
->session
->options
, "status-keys");
886 if (keys
== MODEKEY_EMACS
)
887 mode_key_init(&c
->prompt_mdata
, &mode_key_tree_emacs_edit
);
889 mode_key_init(&c
->prompt_mdata
, &mode_key_tree_vi_edit
);
891 c
->tty
.flags
|= (TTY_NOCURSOR
|TTY_FREEZE
);
892 c
->flags
|= CLIENT_STATUS
;
895 /* Remove status line prompt. */
897 status_prompt_clear(struct client
*c
)
899 if (c
->prompt_string
== NULL
)
902 if (c
->prompt_freefn
!= NULL
&& c
->prompt_data
!= NULL
)
903 c
->prompt_freefn(c
->prompt_data
);
905 xfree(c
->prompt_string
);
906 c
->prompt_string
= NULL
;
908 xfree(c
->prompt_buffer
);
909 c
->prompt_buffer
= NULL
;
911 c
->tty
.flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
);
912 c
->flags
|= CLIENT_REDRAW
; /* screen was frozen and may have changed */
914 screen_reinit(&c
->status
);
917 /* Update status line prompt with a new prompt string. */
919 status_prompt_update(struct client
*c
, const char *msg
, const char *input
)
921 xfree(c
->prompt_string
);
922 c
->prompt_string
= status_replace(c
, NULL
, NULL
, NULL
, msg
,
925 xfree(c
->prompt_buffer
);
928 c
->prompt_buffer
= status_replace(c
, NULL
, NULL
, NULL
, input
,
930 c
->prompt_index
= strlen(c
->prompt_buffer
);
932 c
->prompt_hindex
= 0;
934 c
->flags
|= CLIENT_STATUS
;
937 /* Draw client prompt on status line of present else on last line. */
939 status_prompt_redraw(struct client
*c
)
941 struct screen_write_ctx ctx
;
942 struct session
*s
= c
->session
;
943 struct screen old_status
;
944 size_t i
, size
, left
, len
, off
;
945 struct grid_cell gc
, *gcp
;
948 if (c
->tty
.sx
== 0 || c
->tty
.sy
== 0)
950 memcpy(&old_status
, &c
->status
, sizeof old_status
);
951 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
953 utf8flag
= options_get_number(&s
->options
, "status-utf8");
955 len
= screen_write_strlen(utf8flag
, "%s", c
->prompt_string
);
960 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
961 /* Change colours for command mode. */
962 if (c
->prompt_mdata
.mode
== 1) {
963 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-command-fg"));
964 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-command-bg"));
965 gc
.attr
|= options_get_number(&s
->options
, "message-command-attr");
967 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-fg"));
968 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-bg"));
969 gc
.attr
|= options_get_number(&s
->options
, "message-attr");
972 screen_write_start(&ctx
, NULL
, &c
->status
);
974 screen_write_cursormove(&ctx
, 0, 0);
975 screen_write_nputs(&ctx
, len
, &gc
, utf8flag
, "%s", c
->prompt_string
);
977 left
= c
->tty
.sx
- len
;
979 size
= screen_write_strlen(utf8flag
, "%s", c
->prompt_buffer
);
980 if (c
->prompt_index
>= left
) {
981 off
= c
->prompt_index
- left
+ 1;
982 if (c
->prompt_index
== size
)
987 &ctx
, left
, &gc
, utf8flag
, "%s", c
->prompt_buffer
+ off
);
989 for (i
= len
+ size
; i
< c
->tty
.sx
; i
++)
990 screen_write_putc(&ctx
, &gc
, ' ');
993 screen_write_stop(&ctx
);
995 /* Apply fake cursor. */
996 off
= len
+ c
->prompt_index
- off
;
997 gcp
= grid_view_get_cell(c
->status
.grid
, off
, 0);
998 gcp
->attr
^= GRID_ATTR_REVERSE
;
1000 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
1001 screen_free(&old_status
);
1004 screen_free(&old_status
);
1008 /* Handle keys in prompt. */
1010 status_prompt_key(struct client
*c
, int key
)
1012 struct session
*sess
= c
->session
;
1013 struct options
*oo
= &sess
->options
;
1014 struct paste_buffer
*pb
;
1015 char *s
, *first
, *last
, word
[64], swapc
;
1016 const char *histstr
;
1017 const char *wsep
= NULL
;
1019 size_t size
, n
, off
, idx
;
1021 size
= strlen(c
->prompt_buffer
);
1022 switch (mode_key_lookup(&c
->prompt_mdata
, key
)) {
1023 case MODEKEYEDIT_CURSORLEFT
:
1024 if (c
->prompt_index
> 0) {
1026 c
->flags
|= CLIENT_STATUS
;
1029 case MODEKEYEDIT_SWITCHMODE
:
1030 c
->flags
|= CLIENT_STATUS
;
1032 case MODEKEYEDIT_SWITCHMODEAPPEND
:
1033 c
->flags
|= CLIENT_STATUS
;
1035 case MODEKEYEDIT_CURSORRIGHT
:
1036 if (c
->prompt_index
< size
) {
1038 c
->flags
|= CLIENT_STATUS
;
1041 case MODEKEYEDIT_STARTOFLINE
:
1042 if (c
->prompt_index
!= 0) {
1043 c
->prompt_index
= 0;
1044 c
->flags
|= CLIENT_STATUS
;
1047 case MODEKEYEDIT_ENDOFLINE
:
1048 if (c
->prompt_index
!= size
) {
1049 c
->prompt_index
= size
;
1050 c
->flags
|= CLIENT_STATUS
;
1053 case MODEKEYEDIT_COMPLETE
:
1054 if (*c
->prompt_buffer
== '\0')
1057 idx
= c
->prompt_index
;
1061 /* Find the word we are in. */
1062 first
= c
->prompt_buffer
+ idx
;
1063 while (first
> c
->prompt_buffer
&& *first
!= ' ')
1065 while (*first
== ' ')
1067 last
= c
->prompt_buffer
+ idx
;
1068 while (*last
!= '\0' && *last
!= ' ')
1070 while (*last
== ' ')
1074 if (last
<= first
||
1075 ((size_t) (last
- first
)) > (sizeof word
) - 1)
1077 memcpy(word
, first
, last
- first
);
1078 word
[last
- first
] = '\0';
1080 /* And try to complete it. */
1081 if ((s
= status_prompt_complete(word
)) == NULL
)
1084 /* Trim out word. */
1085 n
= size
- (last
- c
->prompt_buffer
) + 1; /* with \0 */
1086 memmove(first
, last
, n
);
1087 size
-= last
- first
;
1089 /* Insert the new word. */
1091 off
= first
- c
->prompt_buffer
;
1092 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ 1);
1093 first
= c
->prompt_buffer
+ off
;
1094 memmove(first
+ strlen(s
), first
, n
);
1095 memcpy(first
, s
, strlen(s
));
1097 c
->prompt_index
= (first
- c
->prompt_buffer
) + strlen(s
);
1100 c
->flags
|= CLIENT_STATUS
;
1102 case MODEKEYEDIT_BACKSPACE
:
1103 if (c
->prompt_index
!= 0) {
1104 if (c
->prompt_index
== size
)
1105 c
->prompt_buffer
[--c
->prompt_index
] = '\0';
1107 memmove(c
->prompt_buffer
+ c
->prompt_index
- 1,
1108 c
->prompt_buffer
+ c
->prompt_index
,
1109 size
+ 1 - c
->prompt_index
);
1112 c
->flags
|= CLIENT_STATUS
;
1115 case MODEKEYEDIT_DELETE
:
1116 if (c
->prompt_index
!= size
) {
1117 memmove(c
->prompt_buffer
+ c
->prompt_index
,
1118 c
->prompt_buffer
+ c
->prompt_index
+ 1,
1119 size
+ 1 - c
->prompt_index
);
1120 c
->flags
|= CLIENT_STATUS
;
1123 case MODEKEYEDIT_DELETELINE
:
1124 *c
->prompt_buffer
= '\0';
1125 c
->prompt_index
= 0;
1126 c
->flags
|= CLIENT_STATUS
;
1128 case MODEKEYEDIT_DELETETOENDOFLINE
:
1129 if (c
->prompt_index
< size
) {
1130 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1131 c
->flags
|= CLIENT_STATUS
;
1134 case MODEKEYEDIT_DELETEWORD
:
1135 wsep
= options_get_string(oo
, "word-separators");
1136 idx
= c
->prompt_index
;
1138 /* Find a non-separator. */
1141 if (!strchr(wsep
, c
->prompt_buffer
[idx
]))
1145 /* Find the separator at the beginning of the word. */
1148 if (strchr(wsep
, c
->prompt_buffer
[idx
])) {
1149 /* Go back to the word. */
1155 memmove(c
->prompt_buffer
+ idx
,
1156 c
->prompt_buffer
+ c
->prompt_index
,
1157 size
+ 1 - c
->prompt_index
);
1158 memset(c
->prompt_buffer
+ size
- (c
->prompt_index
- idx
),
1159 '\0', c
->prompt_index
- idx
);
1160 c
->prompt_index
= idx
;
1161 c
->flags
|= CLIENT_STATUS
;
1163 case MODEKEYEDIT_NEXTSPACE
:
1166 case MODEKEYEDIT_NEXTWORD
:
1168 wsep
= options_get_string(oo
, "word-separators");
1170 /* Find a separator. */
1171 while (c
->prompt_index
!= size
) {
1173 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1177 /* Find the word right after the separation. */
1178 while (c
->prompt_index
!= size
) {
1180 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1184 c
->flags
|= CLIENT_STATUS
;
1186 case MODEKEYEDIT_NEXTSPACEEND
:
1189 case MODEKEYEDIT_NEXTWORDEND
:
1191 wsep
= options_get_string(oo
, "word-separators");
1194 while (c
->prompt_index
!= size
) {
1196 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1200 /* Find the separator at the end of the word. */
1201 while (c
->prompt_index
!= size
) {
1203 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1207 c
->flags
|= CLIENT_STATUS
;
1209 case MODEKEYEDIT_PREVIOUSSPACE
:
1212 case MODEKEYEDIT_PREVIOUSWORD
:
1214 wsep
= options_get_string(oo
, "word-separators");
1216 /* Find a non-separator. */
1217 while (c
->prompt_index
!= 0) {
1219 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1223 /* Find the separator at the beginning of the word. */
1224 while (c
->prompt_index
!= 0) {
1226 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
])) {
1227 /* Go back to the word. */
1233 c
->flags
|= CLIENT_STATUS
;
1235 case MODEKEYEDIT_HISTORYUP
:
1236 histstr
= status_prompt_up_history(&c
->prompt_hindex
);
1237 if (histstr
== NULL
)
1239 xfree(c
->prompt_buffer
);
1240 c
->prompt_buffer
= xstrdup(histstr
);
1241 c
->prompt_index
= strlen(c
->prompt_buffer
);
1242 c
->flags
|= CLIENT_STATUS
;
1244 case MODEKEYEDIT_HISTORYDOWN
:
1245 histstr
= status_prompt_down_history(&c
->prompt_hindex
);
1246 if (histstr
== NULL
)
1248 xfree(c
->prompt_buffer
);
1249 c
->prompt_buffer
= xstrdup(histstr
);
1250 c
->prompt_index
= strlen(c
->prompt_buffer
);
1251 c
->flags
|= CLIENT_STATUS
;
1253 case MODEKEYEDIT_PASTE
:
1254 if ((pb
= paste_get_top(&global_buffers
)) == NULL
)
1256 for (n
= 0; n
< pb
->size
; n
++) {
1257 ch
= (u_char
) pb
->data
[n
];
1258 if (ch
< 32 || ch
== 127)
1262 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ n
+ 1);
1263 if (c
->prompt_index
== size
) {
1264 memcpy(c
->prompt_buffer
+ c
->prompt_index
, pb
->data
, n
);
1265 c
->prompt_index
+= n
;
1266 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1268 memmove(c
->prompt_buffer
+ c
->prompt_index
+ n
,
1269 c
->prompt_buffer
+ c
->prompt_index
,
1270 size
+ 1 - c
->prompt_index
);
1271 memcpy(c
->prompt_buffer
+ c
->prompt_index
, pb
->data
, n
);
1272 c
->prompt_index
+= n
;
1275 c
->flags
|= CLIENT_STATUS
;
1277 case MODEKEYEDIT_TRANSPOSECHARS
:
1278 idx
= c
->prompt_index
;
1282 swapc
= c
->prompt_buffer
[idx
- 2];
1283 c
->prompt_buffer
[idx
- 2] = c
->prompt_buffer
[idx
- 1];
1284 c
->prompt_buffer
[idx
- 1] = swapc
;
1285 c
->prompt_index
= idx
;
1286 c
->flags
|= CLIENT_STATUS
;
1289 case MODEKEYEDIT_ENTER
:
1290 if (*c
->prompt_buffer
!= '\0')
1291 status_prompt_add_history(c
->prompt_buffer
);
1292 if (c
->prompt_callbackfn(c
->prompt_data
, c
->prompt_buffer
) == 0)
1293 status_prompt_clear(c
);
1295 case MODEKEYEDIT_CANCEL
:
1296 if (c
->prompt_callbackfn(c
->prompt_data
, NULL
) == 0)
1297 status_prompt_clear(c
);
1300 if ((key
& 0xff00) != 0 || key
< 32 || key
== 127)
1302 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ 2);
1304 if (c
->prompt_index
== size
) {
1305 c
->prompt_buffer
[c
->prompt_index
++] = key
;
1306 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1308 memmove(c
->prompt_buffer
+ c
->prompt_index
+ 1,
1309 c
->prompt_buffer
+ c
->prompt_index
,
1310 size
+ 1 - c
->prompt_index
);
1311 c
->prompt_buffer
[c
->prompt_index
++] = key
;
1314 if (c
->prompt_flags
& PROMPT_SINGLE
) {
1315 if (c
->prompt_callbackfn(
1316 c
->prompt_data
, c
->prompt_buffer
) == 0)
1317 status_prompt_clear(c
);
1320 c
->flags
|= CLIENT_STATUS
;
1327 /* Get previous line from the history. */
1329 status_prompt_up_history(u_int
*idx
)
1334 * History runs from 0 to size - 1.
1336 * Index is from 0 to size. Zero is empty.
1339 size
= ARRAY_LENGTH(&status_prompt_history
);
1340 if (size
== 0 || *idx
== size
)
1343 return (ARRAY_ITEM(&status_prompt_history
, size
- *idx
));
1346 /* Get next line from the history. */
1348 status_prompt_down_history(u_int
*idx
)
1352 size
= ARRAY_LENGTH(&status_prompt_history
);
1353 if (size
== 0 || *idx
== 0)
1358 return (ARRAY_ITEM(&status_prompt_history
, size
- *idx
));
1361 /* Add line to the history. */
1363 status_prompt_add_history(const char *line
)
1367 size
= ARRAY_LENGTH(&status_prompt_history
);
1368 if (size
> 0 && strcmp(ARRAY_LAST(&status_prompt_history
), line
) == 0)
1371 if (size
== PROMPT_HISTORY
) {
1372 xfree(ARRAY_FIRST(&status_prompt_history
));
1373 ARRAY_REMOVE(&status_prompt_history
, 0);
1376 ARRAY_ADD(&status_prompt_history
, xstrdup(line
));
1379 /* Complete word. */
1381 status_prompt_complete(const char *s
)
1383 const struct cmd_entry
**cmdent
;
1384 const struct options_table_entry
*oe
;
1385 ARRAY_DECL(, const char *) list
;
1393 /* First, build a list of all the possible matches. */
1395 for (cmdent
= cmd_table
; *cmdent
!= NULL
; cmdent
++) {
1396 if (strncmp((*cmdent
)->name
, s
, strlen(s
)) == 0)
1397 ARRAY_ADD(&list
, (*cmdent
)->name
);
1399 for (oe
= server_options_table
; oe
->name
!= NULL
; oe
++) {
1400 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1401 ARRAY_ADD(&list
, oe
->name
);
1403 for (oe
= session_options_table
; oe
->name
!= NULL
; oe
++) {
1404 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1405 ARRAY_ADD(&list
, oe
->name
);
1407 for (oe
= window_options_table
; oe
->name
!= NULL
; oe
++) {
1408 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1409 ARRAY_ADD(&list
, oe
->name
);
1412 /* If none, bail now. */
1413 if (ARRAY_LENGTH(&list
) == 0) {
1418 /* If an exact match, return it, with a trailing space. */
1419 if (ARRAY_LENGTH(&list
) == 1) {
1420 xasprintf(&s2
, "%s ", ARRAY_FIRST(&list
));
1425 /* Now loop through the list and find the longest common prefix. */
1426 prefix
= xstrdup(ARRAY_FIRST(&list
));
1427 for (i
= 1; i
< ARRAY_LENGTH(&list
); i
++) {
1428 s
= ARRAY_ITEM(&list
, i
);
1431 if (j
> strlen(prefix
))
1433 for (; j
> 0; j
--) {
1434 if (prefix
[j
- 1] != s
[j
- 1])
1435 prefix
[j
- 1] = '\0';