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
*, char **, char **, char *, size_t, int);
42 void status_message_callback(int, short, void *);
44 const char *status_prompt_up_history(u_int
*);
45 const char *status_prompt_down_history(u_int
*);
46 void status_prompt_add_history(const char *);
47 char *status_prompt_complete(const char *);
49 /* Status prompt history. */
50 ARRAY_DECL(, char *) status_prompt_history
= ARRAY_INITIALIZER
;
52 /* Status output tree. */
53 RB_GENERATE(status_out_tree
, status_out
, entry
, status_out_cmp
);
55 /* Output tree comparison function. */
57 status_out_cmp(struct status_out
*so1
, struct status_out
*so2
)
59 return (strcmp(so1
->cmd
, so2
->cmd
));
62 /* Get screen line of status line. -1 means off. */
64 status_at_line(struct client
*c
)
66 struct session
*s
= c
->session
;
68 if (!options_get_number(&s
->options
, "status"))
71 if (options_get_number(&s
->options
, "status-position") == 0)
73 return (c
->tty
.sy
- 1);
76 /* Retrieve options for left string. */
78 status_redraw_get_left(struct client
*c
,
79 time_t t
, int utf8flag
, struct grid_cell
*gc
, size_t *size
)
81 struct session
*s
= c
->session
;
86 fg
= options_get_number(&s
->options
, "status-left-fg");
88 colour_set_fg(gc
, fg
);
89 bg
= options_get_number(&s
->options
, "status-left-bg");
91 colour_set_bg(gc
, bg
);
92 attr
= options_get_number(&s
->options
, "status-left-attr");
96 left
= status_replace(c
, NULL
,
97 NULL
, NULL
, options_get_string(&s
->options
, "status-left"), t
, 1);
99 *size
= options_get_number(&s
->options
, "status-left-length");
100 leftlen
= screen_write_cstrlen(utf8flag
, "%s", left
);
106 /* Retrieve options for right string. */
108 status_redraw_get_right(struct client
*c
,
109 time_t t
, int utf8flag
, struct grid_cell
*gc
, size_t *size
)
111 struct session
*s
= c
->session
;
116 fg
= options_get_number(&s
->options
, "status-right-fg");
118 colour_set_fg(gc
, fg
);
119 bg
= options_get_number(&s
->options
, "status-right-bg");
121 colour_set_bg(gc
, bg
);
122 attr
= options_get_number(&s
->options
, "status-right-attr");
126 right
= status_replace(c
, NULL
,
127 NULL
, NULL
, options_get_string(&s
->options
, "status-right"), t
, 1);
129 *size
= options_get_number(&s
->options
, "status-right-length");
130 rightlen
= screen_write_cstrlen(utf8flag
, "%s", right
);
131 if (rightlen
< *size
)
136 /* Set window at window list position. */
138 status_set_window_at(struct client
*c
, u_int x
)
140 struct session
*s
= c
->session
;
144 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
145 if (x
< wl
->status_width
&& session_select(s
, wl
->idx
) == 0)
146 server_redraw_session(s
);
147 x
-= wl
->status_width
+ 1;
151 /* Draw status for client on the last lines of given context. */
153 status_redraw(struct client
*c
)
155 struct screen_write_ctx ctx
;
156 struct session
*s
= c
->session
;
158 struct screen old_status
, window_list
;
159 struct grid_cell stdgc
, lgc
, rgc
, gc
;
162 char *left
, *right
, *sep
;
163 u_int offset
, needed
;
164 u_int wlstart
, wlwidth
, wlavailable
, wloffset
, wlsize
;
165 size_t llen
, rlen
, seplen
;
166 int larrow
, rarrow
, utf8flag
;
168 /* No status line? */
169 if (c
->tty
.sy
== 0 || !options_get_number(&s
->options
, "status"))
174 /* Update status timer. */
175 if (gettimeofday(&c
->status_timer
, NULL
) != 0)
176 fatal("gettimeofday failed");
177 t
= c
->status_timer
.tv_sec
;
179 /* Set up default colour. */
180 memcpy(&stdgc
, &grid_default_cell
, sizeof gc
);
181 colour_set_fg(&stdgc
, options_get_number(&s
->options
, "status-fg"));
182 colour_set_bg(&stdgc
, options_get_number(&s
->options
, "status-bg"));
183 stdgc
.attr
|= options_get_number(&s
->options
, "status-attr");
185 /* Create the target screen. */
186 memcpy(&old_status
, &c
->status
, sizeof old_status
);
187 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
188 screen_write_start(&ctx
, NULL
, &c
->status
);
189 for (offset
= 0; offset
< c
->tty
.sx
; offset
++)
190 screen_write_putc(&ctx
, &stdgc
, ' ');
191 screen_write_stop(&ctx
);
193 /* If the height is one line, blank status line. */
197 /* Get UTF-8 flag. */
198 utf8flag
= options_get_number(&s
->options
, "status-utf8");
200 /* Work out left and right strings. */
201 memcpy(&lgc
, &stdgc
, sizeof lgc
);
202 left
= status_redraw_get_left(c
, t
, utf8flag
, &lgc
, &llen
);
203 memcpy(&rgc
, &stdgc
, sizeof rgc
);
204 right
= status_redraw_get_right(c
, t
, utf8flag
, &rgc
, &rlen
);
207 * Figure out how much space we have for the window list. If there
208 * isn't enough space, just show a blank status line.
215 if (c
->tty
.sx
== 0 || c
->tty
.sx
<= needed
)
217 wlavailable
= c
->tty
.sx
- needed
;
219 /* Calculate the total size needed for the window list. */
220 wlstart
= wloffset
= wlwidth
= 0;
221 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
222 free(wl
->status_text
);
223 memcpy(&wl
->status_cell
, &stdgc
, sizeof wl
->status_cell
);
224 wl
->status_text
= status_print(c
, wl
, t
, &wl
->status_cell
);
226 screen_write_cstrlen(utf8flag
, "%s", wl
->status_text
);
231 oo
= &wl
->window
->options
;
232 sep
= options_get_string(oo
, "window-status-separator");
233 seplen
= screen_write_strlen(utf8flag
, "%s", sep
);
234 wlwidth
+= wl
->status_width
+ seplen
;
237 /* Create a new screen for the window list. */
238 screen_init(&window_list
, wlwidth
, 1, 0);
240 /* And draw the window list into it. */
241 screen_write_start(&ctx
, NULL
, &window_list
);
242 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
243 screen_write_cnputs(&ctx
,
244 -1, &wl
->status_cell
, utf8flag
, "%s", wl
->status_text
);
246 oo
= &wl
->window
->options
;
247 sep
= options_get_string(oo
, "window-status-separator");
248 screen_write_nputs(&ctx
, -1, &stdgc
, utf8flag
, "%s", sep
);
250 screen_write_stop(&ctx
);
252 /* If there is enough space for the total width, skip to draw now. */
253 if (wlwidth
<= wlavailable
)
256 /* Find size of current window text. */
257 wlsize
= s
->curw
->status_width
;
260 * If the current window is already on screen, good to draw from the
261 * start and just leave off the end.
263 if (wloffset
+ wlsize
< wlavailable
) {
264 if (wlavailable
> 0) {
268 wlwidth
= wlavailable
;
271 * Work out how many characters we need to omit from the
272 * start. There are wlavailable characters to fill, and
273 * wloffset + wlsize must be the last. So, the start character
274 * is wloffset + wlsize - wlavailable.
276 if (wlavailable
> 0) {
281 wlstart
= wloffset
+ wlsize
- wlavailable
;
282 if (wlavailable
> 0 && wlwidth
> wlstart
+ wlavailable
+ 1) {
287 wlwidth
= wlavailable
;
290 /* Bail if anything is now too small too. */
291 if (wlwidth
== 0 || wlavailable
== 0) {
292 screen_free(&window_list
);
297 * Now the start position is known, work out the state of the left and
301 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
302 if (wl
->flags
& WINLINK_ALERTFLAGS
&&
303 larrow
== 1 && offset
< wlstart
)
306 offset
+= wl
->status_width
;
308 if (wl
->flags
& WINLINK_ALERTFLAGS
&&
309 rarrow
== 1 && offset
> wlstart
+ wlwidth
)
315 screen_write_start(&ctx
, NULL
, &c
->status
);
317 /* Draw the left string and arrow. */
318 screen_write_cursormove(&ctx
, 0, 0);
320 screen_write_cnputs(&ctx
, llen
, &lgc
, utf8flag
, "%s", left
);
321 screen_write_putc(&ctx
, &stdgc
, ' ');
324 memcpy(&gc
, &stdgc
, sizeof gc
);
326 gc
.attr
^= GRID_ATTR_REVERSE
;
327 screen_write_putc(&ctx
, &gc
, '<');
330 /* Draw the right string and arrow. */
332 screen_write_cursormove(&ctx
, c
->tty
.sx
- rlen
- 2, 0);
333 memcpy(&gc
, &stdgc
, sizeof gc
);
335 gc
.attr
^= GRID_ATTR_REVERSE
;
336 screen_write_putc(&ctx
, &gc
, '>');
338 screen_write_cursormove(&ctx
, c
->tty
.sx
- rlen
- 1, 0);
340 screen_write_putc(&ctx
, &stdgc
, ' ');
341 screen_write_cnputs(&ctx
, rlen
, &rgc
, utf8flag
, "%s", right
);
344 /* Figure out the offset for the window list. */
349 if (wlwidth
< wlavailable
) {
350 switch (options_get_number(&s
->options
, "status-justify")) {
351 case 1: /* centered */
352 wloffset
+= (wlavailable
- wlwidth
) / 2;
355 wloffset
+= (wlavailable
- wlwidth
);
362 /* Copy the window list. */
363 c
->wlmouse
= -wloffset
+ wlstart
;
364 screen_write_cursormove(&ctx
, wloffset
, 0);
365 screen_write_copy(&ctx
, &window_list
, wlstart
, 0, wlwidth
, 1);
366 screen_free(&window_list
);
368 screen_write_stop(&ctx
);
374 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
375 screen_free(&old_status
);
378 screen_free(&old_status
);
382 /* Replace a single special sequence (prefixed by #). */
384 status_replace1(struct client
*c
, char **iptr
, char **optr
, char *out
,
385 size_t outsize
, int jobsflag
)
387 char ch
, tmp
[256], *ptr
, *endptr
;
392 limit
= strtol(*iptr
, &endptr
, 10);
393 if ((limit
== 0 && errno
!= EINVAL
) ||
394 (limit
== LONG_MIN
&& errno
!= ERANGE
) ||
395 (limit
== LONG_MAX
&& errno
!= ERANGE
) ||
401 switch (*(*iptr
)++) {
407 if ((ptr
= status_find_job(c
, iptr
)) == NULL
)
412 * Embedded style, handled at display time. Leave present and
413 * skip input until ].
424 xsnprintf(tmp
, sizeof tmp
, "#%c", *(*iptr
- 1));
432 ptrlen
= strlen(ptr
);
433 if ((size_t) limit
< ptrlen
)
436 if (*optr
+ ptrlen
>= out
+ outsize
- 1)
438 while (ptrlen
> 0 && *ptr
!= '\0') {
448 (*iptr
)--; /* include ch */
449 while (**iptr
!= ch
&& **iptr
!= '\0') {
450 if (*optr
>= out
+ outsize
- 1)
452 *(*optr
)++ = *(*iptr
)++;
456 /* Replace special sequences in fmt. */
458 status_replace(struct client
*c
, struct session
*s
, struct winlink
*wl
,
459 struct window_pane
*wp
, const char *fmt
, time_t t
, int jobsflag
)
461 static char out
[BUFSIZ
];
462 char in
[BUFSIZ
], ch
, *iptr
, *optr
, *expanded
;
464 struct format_tree
*ft
;
467 return (xstrdup(""));
474 wp
= wl
->window
->active
;
476 len
= strftime(in
, sizeof in
, fmt
, localtime(&t
));
482 while (*iptr
!= '\0') {
483 if (optr
>= out
+ (sizeof out
) - 1)
487 if (ch
!= '#' || *iptr
== '\0') {
491 status_replace1(c
, &iptr
, &optr
, out
, sizeof out
, jobsflag
);
495 ft
= format_create();
496 format_client(ft
, c
);
497 format_session(ft
, s
);
498 format_winlink(ft
, s
, wl
);
499 format_window_pane(ft
, wp
);
500 expanded
= format_expand(ft
, out
);
505 /* Figure out job name and get its result, starting it off if necessary. */
507 status_find_job(struct client
*c
, char **iptr
)
509 struct status_out
*so
, so_find
;
516 if (**iptr
== ')') { /* no command given */
521 cmd
= xmalloc(strlen(*iptr
) + 1);
525 for (; **iptr
!= '\0'; (*iptr
)++) {
526 if (!lastesc
&& **iptr
== ')')
527 break; /* unescaped ) is the end */
528 if (!lastesc
&& **iptr
== '\\') {
530 continue; /* skip \ if not escaped */
535 if (**iptr
== '\0') /* no terminating ) */ {
539 (*iptr
)++; /* skip final ) */
542 /* First try in the new tree. */
544 so
= RB_FIND(status_out_tree
, &c
->status_new
, &so_find
);
545 if (so
!= NULL
&& so
->out
!= NULL
) {
550 /* If not found at all, start the job and add to the tree. */
552 job_run(cmd
, NULL
, status_job_callback
, status_job_free
, c
);
555 so
= xmalloc(sizeof *so
);
556 so
->cmd
= xstrdup(cmd
);
558 RB_INSERT(status_out_tree
, &c
->status_new
, so
);
561 /* Lookup in the old tree. */
563 so
= RB_FIND(status_out_tree
, &c
->status_old
, &so_find
);
572 status_free_jobs(struct status_out_tree
*sotree
)
574 struct status_out
*so
, *so_next
;
576 so_next
= RB_MIN(status_out_tree
, sotree
);
577 while (so_next
!= NULL
) {
579 so_next
= RB_NEXT(status_out_tree
, sotree
, so
);
581 RB_REMOVE(status_out_tree
, sotree
, so
);
588 /* Update jobs on status interval. */
590 status_update_jobs(struct client
*c
)
592 /* Free the old tree. */
593 status_free_jobs(&c
->status_old
);
595 /* Move the new to old. */
596 memcpy(&c
->status_old
, &c
->status_new
, sizeof c
->status_old
);
597 RB_INIT(&c
->status_new
);
600 /* Free status job. */
602 status_job_free(void *data
)
604 struct client
*c
= data
;
609 /* Job has finished: save its result. */
611 status_job_callback(struct job
*job
)
613 struct client
*c
= job
->data
;
614 struct status_out
*so
, so_find
;
618 if (c
->flags
& CLIENT_DEAD
)
621 so_find
.cmd
= job
->cmd
;
622 so
= RB_FIND(status_out_tree
, &c
->status_new
, &so_find
);
623 if (so
== NULL
|| so
->out
!= NULL
)
627 if ((line
= evbuffer_readline(job
->event
->input
)) == NULL
) {
628 len
= EVBUFFER_LENGTH(job
->event
->input
);
629 buf
= xmalloc(len
+ 1);
631 memcpy(buf
, EVBUFFER_DATA(job
->event
->input
), len
);
637 server_status_client(c
);
640 /* Return winlink status line entry and adjust gc as necessary. */
643 struct client
*c
, struct winlink
*wl
, time_t t
, struct grid_cell
*gc
)
645 struct options
*oo
= &wl
->window
->options
;
646 struct session
*s
= c
->session
;
651 fg
= options_get_number(oo
, "window-status-fg");
653 colour_set_fg(gc
, fg
);
654 bg
= options_get_number(oo
, "window-status-bg");
656 colour_set_bg(gc
, bg
);
657 attr
= options_get_number(oo
, "window-status-attr");
660 fmt
= options_get_string(oo
, "window-status-format");
662 fg
= options_get_number(oo
, "window-status-current-fg");
664 colour_set_fg(gc
, fg
);
665 bg
= options_get_number(oo
, "window-status-current-bg");
667 colour_set_bg(gc
, bg
);
668 attr
= options_get_number(oo
, "window-status-current-attr");
671 fmt
= options_get_string(oo
, "window-status-current-format");
673 if (wl
== TAILQ_FIRST(&s
->lastw
)) {
674 fg
= options_get_number(oo
, "window-status-last-fg");
676 colour_set_fg(gc
, fg
);
677 bg
= options_get_number(oo
, "window-status-last-bg");
679 colour_set_bg(gc
, bg
);
680 attr
= options_get_number(oo
, "window-status-last-attr");
685 if (wl
->flags
& WINLINK_BELL
) {
686 fg
= options_get_number(oo
, "window-status-bell-fg");
688 colour_set_fg(gc
, fg
);
689 bg
= options_get_number(oo
, "window-status-bell-bg");
691 colour_set_bg(gc
, bg
);
692 attr
= options_get_number(oo
, "window-status-bell-attr");
695 } else if (wl
->flags
& WINLINK_CONTENT
) {
696 fg
= options_get_number(oo
, "window-status-content-fg");
698 colour_set_fg(gc
, fg
);
699 bg
= options_get_number(oo
, "window-status-content-bg");
701 colour_set_bg(gc
, bg
);
702 attr
= options_get_number(oo
, "window-status-content-attr");
705 } else if (wl
->flags
& (WINLINK_ACTIVITY
|WINLINK_SILENCE
)) {
706 fg
= options_get_number(oo
, "window-status-activity-fg");
708 colour_set_fg(gc
, fg
);
709 bg
= options_get_number(oo
, "window-status-activity-bg");
711 colour_set_bg(gc
, bg
);
712 attr
= options_get_number(oo
, "window-status-activity-attr");
717 text
= status_replace(c
, NULL
, wl
, NULL
, fmt
, t
, 1);
721 /* Set a status line message. */
723 status_message_set(struct client
*c
, const char *fmt
, ...)
726 struct session
*s
= c
->session
;
727 struct message_entry
*msg
;
732 status_prompt_clear(c
);
733 status_message_clear(c
);
736 xvasprintf(&c
->message_string
, fmt
, ap
);
739 ARRAY_EXPAND(&c
->message_log
, 1);
740 msg
= &ARRAY_LAST(&c
->message_log
);
741 msg
->msg_time
= time(NULL
);
742 msg
->msg
= xstrdup(c
->message_string
);
747 limit
= options_get_number(&s
->options
, "message-limit");
748 if (ARRAY_LENGTH(&c
->message_log
) > limit
) {
749 limit
= ARRAY_LENGTH(&c
->message_log
) - limit
;
750 for (i
= 0; i
< limit
; i
++) {
751 msg
= &ARRAY_FIRST(&c
->message_log
);
753 ARRAY_REMOVE(&c
->message_log
, 0);
757 delay
= options_get_number(&c
->session
->options
, "display-time");
758 tv
.tv_sec
= delay
/ 1000;
759 tv
.tv_usec
= (delay
% 1000) * 1000L;
761 if (event_initialized (&c
->message_timer
))
762 evtimer_del(&c
->message_timer
);
763 evtimer_set(&c
->message_timer
, status_message_callback
, c
);
764 evtimer_add(&c
->message_timer
, &tv
);
766 c
->tty
.flags
|= (TTY_NOCURSOR
|TTY_FREEZE
);
767 c
->flags
|= CLIENT_STATUS
;
770 /* Clear status line message. */
772 status_message_clear(struct client
*c
)
774 if (c
->message_string
== NULL
)
777 free(c
->message_string
);
778 c
->message_string
= NULL
;
780 c
->tty
.flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
);
781 c
->flags
|= CLIENT_REDRAW
; /* screen was frozen and may have changed */
783 screen_reinit(&c
->status
);
786 /* Clear status line message after timer expires. */
788 status_message_callback(unused
int fd
, unused
short event
, void *data
)
790 struct client
*c
= data
;
792 status_message_clear(c
);
795 /* Draw client message on status line of present else on last line. */
797 status_message_redraw(struct client
*c
)
799 struct screen_write_ctx ctx
;
800 struct session
*s
= c
->session
;
801 struct screen old_status
;
806 if (c
->tty
.sx
== 0 || c
->tty
.sy
== 0)
808 memcpy(&old_status
, &c
->status
, sizeof old_status
);
809 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
811 utf8flag
= options_get_number(&s
->options
, "status-utf8");
813 len
= screen_write_strlen(utf8flag
, "%s", c
->message_string
);
817 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
818 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-fg"));
819 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-bg"));
820 gc
.attr
|= options_get_number(&s
->options
, "message-attr");
822 screen_write_start(&ctx
, NULL
, &c
->status
);
824 screen_write_cursormove(&ctx
, 0, 0);
825 screen_write_nputs(&ctx
, len
, &gc
, utf8flag
, "%s", c
->message_string
);
826 for (; len
< c
->tty
.sx
; len
++)
827 screen_write_putc(&ctx
, &gc
, ' ');
829 screen_write_stop(&ctx
);
831 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
832 screen_free(&old_status
);
835 screen_free(&old_status
);
839 /* Enable status line prompt. */
841 status_prompt_set(struct client
*c
, const char *msg
, const char *input
,
842 int (*callbackfn
)(void *, const char *), void (*freefn
)(void *),
843 void *data
, int flags
)
847 status_message_clear(c
);
848 status_prompt_clear(c
);
850 c
->prompt_string
= status_replace(c
, NULL
, NULL
, NULL
, msg
,
853 c
->prompt_buffer
= status_replace(c
, NULL
, NULL
, NULL
, input
,
855 c
->prompt_index
= strlen(c
->prompt_buffer
);
857 c
->prompt_callbackfn
= callbackfn
;
858 c
->prompt_freefn
= freefn
;
859 c
->prompt_data
= data
;
861 c
->prompt_hindex
= 0;
863 c
->prompt_flags
= flags
;
865 keys
= options_get_number(&c
->session
->options
, "status-keys");
866 if (keys
== MODEKEY_EMACS
)
867 mode_key_init(&c
->prompt_mdata
, &mode_key_tree_emacs_edit
);
869 mode_key_init(&c
->prompt_mdata
, &mode_key_tree_vi_edit
);
871 c
->tty
.flags
|= (TTY_NOCURSOR
|TTY_FREEZE
);
872 c
->flags
|= CLIENT_STATUS
;
875 /* Remove status line prompt. */
877 status_prompt_clear(struct client
*c
)
879 if (c
->prompt_string
== NULL
)
882 if (c
->prompt_freefn
!= NULL
&& c
->prompt_data
!= NULL
)
883 c
->prompt_freefn(c
->prompt_data
);
885 free(c
->prompt_string
);
886 c
->prompt_string
= NULL
;
888 free(c
->prompt_buffer
);
889 c
->prompt_buffer
= NULL
;
891 c
->tty
.flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
);
892 c
->flags
|= CLIENT_REDRAW
; /* screen was frozen and may have changed */
894 screen_reinit(&c
->status
);
897 /* Update status line prompt with a new prompt string. */
899 status_prompt_update(struct client
*c
, const char *msg
, const char *input
)
901 free(c
->prompt_string
);
902 c
->prompt_string
= status_replace(c
, NULL
, NULL
, NULL
, msg
,
905 free(c
->prompt_buffer
);
906 c
->prompt_buffer
= status_replace(c
, NULL
, NULL
, NULL
, input
,
908 c
->prompt_index
= strlen(c
->prompt_buffer
);
910 c
->prompt_hindex
= 0;
912 c
->flags
|= CLIENT_STATUS
;
915 /* Draw client prompt on status line of present else on last line. */
917 status_prompt_redraw(struct client
*c
)
919 struct screen_write_ctx ctx
;
920 struct session
*s
= c
->session
;
921 struct screen old_status
;
922 size_t i
, size
, left
, len
, off
;
923 struct grid_cell gc
, *gcp
;
926 if (c
->tty
.sx
== 0 || c
->tty
.sy
== 0)
928 memcpy(&old_status
, &c
->status
, sizeof old_status
);
929 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
931 utf8flag
= options_get_number(&s
->options
, "status-utf8");
933 len
= screen_write_strlen(utf8flag
, "%s", c
->prompt_string
);
938 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
940 /* Change colours for command mode. */
941 if (c
->prompt_mdata
.mode
== 1) {
942 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-command-fg"));
943 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-command-bg"));
944 gc
.attr
|= options_get_number(&s
->options
, "message-command-attr");
946 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-fg"));
947 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-bg"));
948 gc
.attr
|= options_get_number(&s
->options
, "message-attr");
951 screen_write_start(&ctx
, NULL
, &c
->status
);
953 screen_write_cursormove(&ctx
, 0, 0);
954 screen_write_nputs(&ctx
, len
, &gc
, utf8flag
, "%s", c
->prompt_string
);
956 left
= c
->tty
.sx
- len
;
958 size
= screen_write_strlen(utf8flag
, "%s", c
->prompt_buffer
);
959 if (c
->prompt_index
>= left
) {
960 off
= c
->prompt_index
- left
+ 1;
961 if (c
->prompt_index
== size
)
966 &ctx
, left
, &gc
, utf8flag
, "%s", c
->prompt_buffer
+ off
);
968 for (i
= len
+ size
; i
< c
->tty
.sx
; i
++)
969 screen_write_putc(&ctx
, &gc
, ' ');
972 screen_write_stop(&ctx
);
974 /* Apply fake cursor. */
975 off
= len
+ c
->prompt_index
- off
;
976 gcp
= grid_view_get_cell(c
->status
.grid
, off
, 0);
977 gcp
->attr
^= GRID_ATTR_REVERSE
;
979 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
980 screen_free(&old_status
);
983 screen_free(&old_status
);
987 /* Handle keys in prompt. */
989 status_prompt_key(struct client
*c
, int key
)
991 struct session
*sess
= c
->session
;
992 struct options
*oo
= &sess
->options
;
993 struct paste_buffer
*pb
;
994 char *s
, *first
, *last
, word
[64], swapc
;
996 const char *wsep
= NULL
;
998 size_t size
, n
, off
, idx
;
1000 size
= strlen(c
->prompt_buffer
);
1001 switch (mode_key_lookup(&c
->prompt_mdata
, key
, NULL
)) {
1002 case MODEKEYEDIT_CURSORLEFT
:
1003 if (c
->prompt_index
> 0) {
1005 c
->flags
|= CLIENT_STATUS
;
1008 case MODEKEYEDIT_SWITCHMODE
:
1009 c
->flags
|= CLIENT_STATUS
;
1011 case MODEKEYEDIT_SWITCHMODEAPPEND
:
1012 c
->flags
|= CLIENT_STATUS
;
1014 case MODEKEYEDIT_CURSORRIGHT
:
1015 if (c
->prompt_index
< size
) {
1017 c
->flags
|= CLIENT_STATUS
;
1020 case MODEKEYEDIT_SWITCHMODEBEGINLINE
:
1021 c
->flags
|= CLIENT_STATUS
;
1023 case MODEKEYEDIT_STARTOFLINE
:
1024 if (c
->prompt_index
!= 0) {
1025 c
->prompt_index
= 0;
1026 c
->flags
|= CLIENT_STATUS
;
1029 case MODEKEYEDIT_SWITCHMODEAPPENDLINE
:
1030 c
->flags
|= CLIENT_STATUS
;
1032 case MODEKEYEDIT_ENDOFLINE
:
1033 if (c
->prompt_index
!= size
) {
1034 c
->prompt_index
= size
;
1035 c
->flags
|= CLIENT_STATUS
;
1038 case MODEKEYEDIT_COMPLETE
:
1039 if (*c
->prompt_buffer
== '\0')
1042 idx
= c
->prompt_index
;
1046 /* Find the word we are in. */
1047 first
= c
->prompt_buffer
+ idx
;
1048 while (first
> c
->prompt_buffer
&& *first
!= ' ')
1050 while (*first
== ' ')
1052 last
= c
->prompt_buffer
+ idx
;
1053 while (*last
!= '\0' && *last
!= ' ')
1055 while (*last
== ' ')
1059 if (last
<= first
||
1060 ((size_t) (last
- first
)) > (sizeof word
) - 1)
1062 memcpy(word
, first
, last
- first
);
1063 word
[last
- first
] = '\0';
1065 /* And try to complete it. */
1066 if ((s
= status_prompt_complete(word
)) == NULL
)
1069 /* Trim out word. */
1070 n
= size
- (last
- c
->prompt_buffer
) + 1; /* with \0 */
1071 memmove(first
, last
, n
);
1072 size
-= last
- first
;
1074 /* Insert the new word. */
1076 off
= first
- c
->prompt_buffer
;
1077 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ 1);
1078 first
= c
->prompt_buffer
+ off
;
1079 memmove(first
+ strlen(s
), first
, n
);
1080 memcpy(first
, s
, strlen(s
));
1082 c
->prompt_index
= (first
- c
->prompt_buffer
) + strlen(s
);
1085 c
->flags
|= CLIENT_STATUS
;
1087 case MODEKEYEDIT_BACKSPACE
:
1088 if (c
->prompt_index
!= 0) {
1089 if (c
->prompt_index
== size
)
1090 c
->prompt_buffer
[--c
->prompt_index
] = '\0';
1092 memmove(c
->prompt_buffer
+ c
->prompt_index
- 1,
1093 c
->prompt_buffer
+ c
->prompt_index
,
1094 size
+ 1 - c
->prompt_index
);
1097 c
->flags
|= CLIENT_STATUS
;
1100 case MODEKEYEDIT_DELETE
:
1101 case MODEKEYEDIT_SWITCHMODESUBSTITUTE
:
1102 if (c
->prompt_index
!= size
) {
1103 memmove(c
->prompt_buffer
+ c
->prompt_index
,
1104 c
->prompt_buffer
+ c
->prompt_index
+ 1,
1105 size
+ 1 - c
->prompt_index
);
1106 c
->flags
|= CLIENT_STATUS
;
1109 case MODEKEYEDIT_DELETELINE
:
1110 case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE
:
1111 *c
->prompt_buffer
= '\0';
1112 c
->prompt_index
= 0;
1113 c
->flags
|= CLIENT_STATUS
;
1115 case MODEKEYEDIT_DELETETOENDOFLINE
:
1116 case MODEKEYEDIT_SWITCHMODECHANGELINE
:
1117 if (c
->prompt_index
< size
) {
1118 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1119 c
->flags
|= CLIENT_STATUS
;
1122 case MODEKEYEDIT_DELETEWORD
:
1123 wsep
= options_get_string(oo
, "word-separators");
1124 idx
= c
->prompt_index
;
1126 /* Find a non-separator. */
1129 if (!strchr(wsep
, c
->prompt_buffer
[idx
]))
1133 /* Find the separator at the beginning of the word. */
1136 if (strchr(wsep
, c
->prompt_buffer
[idx
])) {
1137 /* Go back to the word. */
1143 memmove(c
->prompt_buffer
+ idx
,
1144 c
->prompt_buffer
+ c
->prompt_index
,
1145 size
+ 1 - c
->prompt_index
);
1146 memset(c
->prompt_buffer
+ size
- (c
->prompt_index
- idx
),
1147 '\0', c
->prompt_index
- idx
);
1148 c
->prompt_index
= idx
;
1149 c
->flags
|= CLIENT_STATUS
;
1151 case MODEKEYEDIT_NEXTSPACE
:
1154 case MODEKEYEDIT_NEXTWORD
:
1156 wsep
= options_get_string(oo
, "word-separators");
1158 /* Find a separator. */
1159 while (c
->prompt_index
!= size
) {
1161 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1165 /* Find the word right after the separation. */
1166 while (c
->prompt_index
!= size
) {
1168 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1172 c
->flags
|= CLIENT_STATUS
;
1174 case MODEKEYEDIT_NEXTSPACEEND
:
1177 case MODEKEYEDIT_NEXTWORDEND
:
1179 wsep
= options_get_string(oo
, "word-separators");
1182 while (c
->prompt_index
!= size
) {
1184 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1188 /* Find the separator at the end of the word. */
1189 while (c
->prompt_index
!= size
) {
1191 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1195 /* Back up to the end-of-word like vi. */
1196 if (options_get_number(oo
, "status-keys") == MODEKEY_VI
&&
1197 c
->prompt_index
!= 0)
1200 c
->flags
|= CLIENT_STATUS
;
1202 case MODEKEYEDIT_PREVIOUSSPACE
:
1205 case MODEKEYEDIT_PREVIOUSWORD
:
1207 wsep
= options_get_string(oo
, "word-separators");
1209 /* Find a non-separator. */
1210 while (c
->prompt_index
!= 0) {
1212 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1216 /* Find the separator at the beginning of the word. */
1217 while (c
->prompt_index
!= 0) {
1219 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
])) {
1220 /* Go back to the word. */
1226 c
->flags
|= CLIENT_STATUS
;
1228 case MODEKEYEDIT_HISTORYUP
:
1229 histstr
= status_prompt_up_history(&c
->prompt_hindex
);
1230 if (histstr
== NULL
)
1232 free(c
->prompt_buffer
);
1233 c
->prompt_buffer
= xstrdup(histstr
);
1234 c
->prompt_index
= strlen(c
->prompt_buffer
);
1235 c
->flags
|= CLIENT_STATUS
;
1237 case MODEKEYEDIT_HISTORYDOWN
:
1238 histstr
= status_prompt_down_history(&c
->prompt_hindex
);
1239 if (histstr
== NULL
)
1241 free(c
->prompt_buffer
);
1242 c
->prompt_buffer
= xstrdup(histstr
);
1243 c
->prompt_index
= strlen(c
->prompt_buffer
);
1244 c
->flags
|= CLIENT_STATUS
;
1246 case MODEKEYEDIT_PASTE
:
1247 if ((pb
= paste_get_top(&global_buffers
)) == NULL
)
1249 for (n
= 0; n
< pb
->size
; n
++) {
1250 ch
= (u_char
) pb
->data
[n
];
1251 if (ch
< 32 || ch
== 127)
1255 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ n
+ 1);
1256 if (c
->prompt_index
== size
) {
1257 memcpy(c
->prompt_buffer
+ c
->prompt_index
, pb
->data
, n
);
1258 c
->prompt_index
+= n
;
1259 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1261 memmove(c
->prompt_buffer
+ c
->prompt_index
+ n
,
1262 c
->prompt_buffer
+ c
->prompt_index
,
1263 size
+ 1 - c
->prompt_index
);
1264 memcpy(c
->prompt_buffer
+ c
->prompt_index
, pb
->data
, n
);
1265 c
->prompt_index
+= n
;
1268 c
->flags
|= CLIENT_STATUS
;
1270 case MODEKEYEDIT_TRANSPOSECHARS
:
1271 idx
= c
->prompt_index
;
1275 swapc
= c
->prompt_buffer
[idx
- 2];
1276 c
->prompt_buffer
[idx
- 2] = c
->prompt_buffer
[idx
- 1];
1277 c
->prompt_buffer
[idx
- 1] = swapc
;
1278 c
->prompt_index
= idx
;
1279 c
->flags
|= CLIENT_STATUS
;
1282 case MODEKEYEDIT_ENTER
:
1283 if (*c
->prompt_buffer
!= '\0')
1284 status_prompt_add_history(c
->prompt_buffer
);
1285 if (c
->prompt_callbackfn(c
->prompt_data
, c
->prompt_buffer
) == 0)
1286 status_prompt_clear(c
);
1288 case MODEKEYEDIT_CANCEL
:
1289 if (c
->prompt_callbackfn(c
->prompt_data
, NULL
) == 0)
1290 status_prompt_clear(c
);
1293 if ((key
& 0xff00) != 0 || key
< 32 || key
== 127)
1295 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ 2);
1297 if (c
->prompt_index
== size
) {
1298 c
->prompt_buffer
[c
->prompt_index
++] = key
;
1299 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1301 memmove(c
->prompt_buffer
+ c
->prompt_index
+ 1,
1302 c
->prompt_buffer
+ c
->prompt_index
,
1303 size
+ 1 - c
->prompt_index
);
1304 c
->prompt_buffer
[c
->prompt_index
++] = key
;
1307 if (c
->prompt_flags
& PROMPT_SINGLE
) {
1308 if (c
->prompt_callbackfn(
1309 c
->prompt_data
, c
->prompt_buffer
) == 0)
1310 status_prompt_clear(c
);
1313 c
->flags
|= CLIENT_STATUS
;
1320 /* Get previous line from the history. */
1322 status_prompt_up_history(u_int
*idx
)
1327 * History runs from 0 to size - 1.
1329 * Index is from 0 to size. Zero is empty.
1332 size
= ARRAY_LENGTH(&status_prompt_history
);
1333 if (size
== 0 || *idx
== size
)
1336 return (ARRAY_ITEM(&status_prompt_history
, size
- *idx
));
1339 /* Get next line from the history. */
1341 status_prompt_down_history(u_int
*idx
)
1345 size
= ARRAY_LENGTH(&status_prompt_history
);
1346 if (size
== 0 || *idx
== 0)
1351 return (ARRAY_ITEM(&status_prompt_history
, size
- *idx
));
1354 /* Add line to the history. */
1356 status_prompt_add_history(const char *line
)
1360 size
= ARRAY_LENGTH(&status_prompt_history
);
1361 if (size
> 0 && strcmp(ARRAY_LAST(&status_prompt_history
), line
) == 0)
1364 if (size
== PROMPT_HISTORY
) {
1365 free(ARRAY_FIRST(&status_prompt_history
));
1366 ARRAY_REMOVE(&status_prompt_history
, 0);
1369 ARRAY_ADD(&status_prompt_history
, xstrdup(line
));
1372 /* Complete word. */
1374 status_prompt_complete(const char *s
)
1376 const struct cmd_entry
**cmdent
;
1377 const struct options_table_entry
*oe
;
1378 ARRAY_DECL(, const char *) list
;
1386 /* First, build a list of all the possible matches. */
1388 for (cmdent
= cmd_table
; *cmdent
!= NULL
; cmdent
++) {
1389 if (strncmp((*cmdent
)->name
, s
, strlen(s
)) == 0)
1390 ARRAY_ADD(&list
, (*cmdent
)->name
);
1392 for (oe
= server_options_table
; oe
->name
!= NULL
; oe
++) {
1393 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1394 ARRAY_ADD(&list
, oe
->name
);
1396 for (oe
= session_options_table
; oe
->name
!= NULL
; oe
++) {
1397 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1398 ARRAY_ADD(&list
, oe
->name
);
1400 for (oe
= window_options_table
; oe
->name
!= NULL
; oe
++) {
1401 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1402 ARRAY_ADD(&list
, oe
->name
);
1405 /* If none, bail now. */
1406 if (ARRAY_LENGTH(&list
) == 0) {
1411 /* If an exact match, return it, with a trailing space. */
1412 if (ARRAY_LENGTH(&list
) == 1) {
1413 xasprintf(&s2
, "%s ", ARRAY_FIRST(&list
));
1418 /* Now loop through the list and find the longest common prefix. */
1419 prefix
= xstrdup(ARRAY_FIRST(&list
));
1420 for (i
= 1; i
< ARRAY_LENGTH(&list
); i
++) {
1421 s
= ARRAY_ITEM(&list
, i
);
1424 if (j
> strlen(prefix
))
1426 for (; j
> 0; j
--) {
1427 if (prefix
[j
- 1] != s
[j
- 1])
1428 prefix
[j
- 1] = '\0';