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
*,
42 struct winlink
*, 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 /* Retrieve options for left string. */
65 status_redraw_get_left(struct client
*c
,
66 time_t t
, int utf8flag
, struct grid_cell
*gc
, size_t *size
)
68 struct session
*s
= c
->session
;
73 fg
= options_get_number(&s
->options
, "status-left-fg");
75 colour_set_fg(gc
, fg
);
76 bg
= options_get_number(&s
->options
, "status-left-bg");
78 colour_set_bg(gc
, bg
);
79 attr
= options_get_number(&s
->options
, "status-left-attr");
83 left
= status_replace(
84 c
, NULL
, options_get_string(&s
->options
, "status-left"), t
, 1);
86 *size
= options_get_number(&s
->options
, "status-left-length");
87 leftlen
= screen_write_cstrlen(utf8flag
, "%s", left
);
93 /* Retrieve options for right string. */
95 status_redraw_get_right(struct client
*c
,
96 time_t t
, int utf8flag
, struct grid_cell
*gc
, size_t *size
)
98 struct session
*s
= c
->session
;
103 fg
= options_get_number(&s
->options
, "status-right-fg");
105 colour_set_fg(gc
, fg
);
106 bg
= options_get_number(&s
->options
, "status-right-bg");
108 colour_set_bg(gc
, bg
);
109 attr
= options_get_number(&s
->options
, "status-right-attr");
113 right
= status_replace(
114 c
, NULL
, options_get_string(&s
->options
, "status-right"), t
, 1);
116 *size
= options_get_number(&s
->options
, "status-right-length");
117 rightlen
= screen_write_cstrlen(utf8flag
, "%s", right
);
118 if (rightlen
< *size
)
123 /* Draw status for client on the last lines of given context. */
125 status_redraw(struct client
*c
)
127 struct screen_write_ctx ctx
;
128 struct session
*s
= c
->session
;
130 struct screen old_status
, window_list
;
131 struct grid_cell stdgc
, lgc
, rgc
, gc
;
134 u_int offset
, needed
;
135 u_int wlstart
, wlwidth
, wlavailable
, wloffset
, wlsize
;
137 int larrow
, rarrow
, utf8flag
;
139 /* No status line? */
140 if (c
->tty
.sy
== 0 || !options_get_number(&s
->options
, "status"))
145 /* Update status timer. */
146 if (gettimeofday(&c
->status_timer
, NULL
) != 0)
147 fatal("gettimeofday failed");
148 t
= c
->status_timer
.tv_sec
;
150 /* Set up default colour. */
151 memcpy(&stdgc
, &grid_default_cell
, sizeof gc
);
152 colour_set_fg(&stdgc
, options_get_number(&s
->options
, "status-fg"));
153 colour_set_bg(&stdgc
, options_get_number(&s
->options
, "status-bg"));
154 stdgc
.attr
|= options_get_number(&s
->options
, "status-attr");
156 /* Create the target screen. */
157 memcpy(&old_status
, &c
->status
, sizeof old_status
);
158 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
159 screen_write_start(&ctx
, NULL
, &c
->status
);
160 for (offset
= 0; offset
< c
->tty
.sx
; offset
++)
161 screen_write_putc(&ctx
, &stdgc
, ' ');
162 screen_write_stop(&ctx
);
164 /* If the height is one line, blank status line. */
168 /* Get UTF-8 flag. */
169 utf8flag
= options_get_number(&s
->options
, "status-utf8");
171 /* Work out left and right strings. */
172 memcpy(&lgc
, &stdgc
, sizeof lgc
);
173 left
= status_redraw_get_left(c
, t
, utf8flag
, &lgc
, &llen
);
174 memcpy(&rgc
, &stdgc
, sizeof rgc
);
175 right
= status_redraw_get_right(c
, t
, utf8flag
, &rgc
, &rlen
);
178 * Figure out how much space we have for the window list. If there
179 * isn't enough space, just show a blank status line.
186 if (c
->tty
.sx
== 0 || c
->tty
.sx
<= needed
)
188 wlavailable
= c
->tty
.sx
- needed
;
190 /* Calculate the total size needed for the window list. */
191 wlstart
= wloffset
= wlwidth
= 0;
192 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
193 if (wl
->status_text
!= NULL
)
194 xfree(wl
->status_text
);
195 memcpy(&wl
->status_cell
, &stdgc
, sizeof wl
->status_cell
);
196 wl
->status_text
= status_print(c
, wl
, t
, &wl
->status_cell
);
198 screen_write_cstrlen(utf8flag
, "%s", wl
->status_text
);
202 wlwidth
+= wl
->status_width
+ 1;
205 /* Create a new screen for the window list. */
206 screen_init(&window_list
, wlwidth
, 1, 0);
208 /* And draw the window list into it. */
209 screen_write_start(&ctx
, NULL
, &window_list
);
210 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
211 screen_write_cnputs(&ctx
,
212 -1, &wl
->status_cell
, utf8flag
, "%s", wl
->status_text
);
213 screen_write_putc(&ctx
, &stdgc
, ' ');
215 screen_write_stop(&ctx
);
217 /* If there is enough space for the total width, skip to draw now. */
218 if (wlwidth
<= wlavailable
)
221 /* Find size of current window text. */
222 wlsize
= s
->curw
->status_width
;
225 * If the current window is already on screen, good to draw from the
226 * start and just leave off the end.
228 if (wloffset
+ wlsize
< wlavailable
) {
229 if (wlavailable
> 0) {
233 wlwidth
= wlavailable
;
236 * Work out how many characters we need to omit from the
237 * start. There are wlavailable characters to fill, and
238 * wloffset + wlsize must be the last. So, the start character
239 * is wloffset + wlsize - wlavailable.
241 if (wlavailable
> 0) {
246 wlstart
= wloffset
+ wlsize
- wlavailable
;
247 if (wlavailable
> 0 && wlwidth
> wlstart
+ wlavailable
+ 1) {
252 wlwidth
= wlavailable
;
255 /* Bail if anything is now too small too. */
256 if (wlwidth
== 0 || wlavailable
== 0) {
257 screen_free(&window_list
);
262 * Now the start position is known, work out the state of the left and
266 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
267 if (wl
->flags
& WINLINK_ALERTFLAGS
&&
268 larrow
== 1 && offset
< wlstart
)
271 offset
+= wl
->status_width
;
273 if (wl
->flags
& WINLINK_ALERTFLAGS
&&
274 rarrow
== 1 && offset
> wlstart
+ wlwidth
)
280 screen_write_start(&ctx
, NULL
, &c
->status
);
282 /* Draw the left string and arrow. */
283 screen_write_cursormove(&ctx
, 0, 0);
285 screen_write_cnputs(&ctx
, llen
, &lgc
, utf8flag
, "%s", left
);
286 screen_write_putc(&ctx
, &stdgc
, ' ');
289 memcpy(&gc
, &stdgc
, sizeof gc
);
291 gc
.attr
^= GRID_ATTR_REVERSE
;
292 screen_write_putc(&ctx
, &gc
, '<');
295 /* Draw the right string and arrow. */
297 screen_write_cursormove(&ctx
, c
->tty
.sx
- rlen
- 2, 0);
298 memcpy(&gc
, &stdgc
, sizeof gc
);
300 gc
.attr
^= GRID_ATTR_REVERSE
;
301 screen_write_putc(&ctx
, &gc
, '>');
303 screen_write_cursormove(&ctx
, c
->tty
.sx
- rlen
- 1, 0);
305 screen_write_putc(&ctx
, &stdgc
, ' ');
306 screen_write_cnputs(&ctx
, rlen
, &rgc
, utf8flag
, "%s", right
);
309 /* Figure out the offset for the window list. */
314 if (wlwidth
< wlavailable
) {
315 switch (options_get_number(&s
->options
, "status-justify")) {
316 case 1: /* centered */
317 wloffset
+= (wlavailable
- wlwidth
) / 2;
320 wloffset
+= (wlavailable
- wlwidth
);
327 /* Copy the window list. */
328 screen_write_cursormove(&ctx
, wloffset
, 0);
329 screen_write_copy(&ctx
, &window_list
, wlstart
, 0, wlwidth
, 1);
330 screen_free(&window_list
);
332 screen_write_stop(&ctx
);
340 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
341 screen_free(&old_status
);
344 screen_free(&old_status
);
348 /* Replace a single special sequence (prefixed by #). */
350 status_replace1(struct client
*c
,struct winlink
*wl
,
351 char **iptr
, char **optr
, char *out
, size_t outsize
, int jobsflag
)
353 struct session
*s
= c
->session
;
354 char ch
, tmp
[256], *ptr
, *endptr
, *freeptr
;
362 limit
= strtol(*iptr
, &endptr
, 10);
363 if ((limit
== 0 && errno
!= EINVAL
) ||
364 (limit
== LONG_MIN
&& errno
!= ERANGE
) ||
365 (limit
== LONG_MAX
&& errno
!= ERANGE
) ||
373 switch (*(*iptr
)++) {
379 if ((ptr
= status_find_job(c
, iptr
)) == NULL
)
383 if (gethostname(tmp
, sizeof tmp
) != 0)
384 fatal("gethostname failed");
388 xsnprintf(tmp
, sizeof tmp
, "%d", wl
->idx
);
392 xsnprintf(tmp
, sizeof tmp
, "%u",
393 window_pane_index(wl
->window
, wl
->window
->active
));
400 ptr
= wl
->window
->active
->base
.title
;
403 ptr
= wl
->window
->name
;
406 ptr
= window_printable_flags(s
, wl
);
411 * Embedded style, handled at display time. Leave present and
412 * skip input until ].
424 ptrlen
= strlen(ptr
);
425 if ((size_t) limit
< ptrlen
)
428 if (*optr
+ ptrlen
>= out
+ outsize
- 1)
430 while (ptrlen
> 0 && *ptr
!= '\0') {
442 (*iptr
)--; /* include ch */
443 while (**iptr
!= ch
&& **iptr
!= '\0') {
444 if (*optr
>= out
+ outsize
- 1)
446 *(*optr
)++ = *(*iptr
)++;
450 /* Replace special sequences in fmt. */
452 status_replace(struct client
*c
,
453 struct winlink
*wl
, const char *fmt
, time_t t
, int jobsflag
)
455 static char out
[BUFSIZ
];
456 char in
[BUFSIZ
], ch
, *iptr
, *optr
;
458 strftime(in
, sizeof in
, fmt
, localtime(&t
));
459 in
[(sizeof in
) - 1] = '\0';
464 while (*iptr
!= '\0') {
465 if (optr
>= out
+ (sizeof out
) - 1)
469 if (ch
!= '#' || *iptr
== '\0') {
473 status_replace1(c
, wl
, &iptr
, &optr
, out
, sizeof out
, jobsflag
);
477 return (xstrdup(out
));
480 /* Figure out job name and get its result, starting it off if necessary. */
482 status_find_job(struct client
*c
, char **iptr
)
484 struct status_out
*so
, so_find
;
491 if (**iptr
== ')') { /* no command given */
496 cmd
= xmalloc(strlen(*iptr
) + 1);
500 for (; **iptr
!= '\0'; (*iptr
)++) {
501 if (!lastesc
&& **iptr
== ')')
502 break; /* unescaped ) is the end */
503 if (!lastesc
&& **iptr
== '\\') {
505 continue; /* skip \ if not escaped */
510 if (**iptr
== '\0') /* no terminating ) */ {
514 (*iptr
)++; /* skip final ) */
517 /* First try in the new tree. */
519 so
= RB_FIND(status_out_tree
, &c
->status_new
, &so_find
);
520 if (so
!= NULL
&& so
->out
!= NULL
)
523 /* If not found at all, start the job and add to the tree. */
525 job_run(cmd
, status_job_callback
, status_job_free
, c
);
528 so
= xmalloc(sizeof *so
);
529 so
->cmd
= xstrdup(cmd
);
531 RB_INSERT(status_out_tree
, &c
->status_new
, so
);
534 /* Lookup in the old tree. */
536 so
= RB_FIND(status_out_tree
, &c
->status_old
, &so_find
);
545 status_free_jobs(struct status_out_tree
*sotree
)
547 struct status_out
*so
, *so_next
;
549 so_next
= RB_MIN(status_out_tree
, sotree
);
550 while (so_next
!= NULL
) {
552 so_next
= RB_NEXT(status_out_tree
, sotree
, so
);
554 RB_REMOVE(status_out_tree
, sotree
, so
);
562 /* Update jobs on status interval. */
564 status_update_jobs(struct client
*c
)
566 /* Free the old tree. */
567 status_free_jobs(&c
->status_old
);
569 /* Move the new to old. */
570 memcpy(&c
->status_old
, &c
->status_new
, sizeof c
->status_old
);
571 RB_INIT(&c
->status_new
);
574 /* Free status job. */
576 status_job_free(void *data
)
578 struct client
*c
= data
;
583 /* Job has finished: save its result. */
585 status_job_callback(struct job
*job
)
587 struct client
*c
= job
->data
;
588 struct status_out
*so
, so_find
;
592 if (c
->flags
& CLIENT_DEAD
)
595 so_find
.cmd
= job
->cmd
;
596 so
= RB_FIND(status_out_tree
, &c
->status_new
, &so_find
);
597 if (so
== NULL
|| so
->out
!= NULL
)
601 if ((line
= evbuffer_readline(job
->event
->input
)) == NULL
) {
602 len
= EVBUFFER_LENGTH(job
->event
->input
);
603 buf
= xmalloc(len
+ 1);
605 memcpy(buf
, EVBUFFER_DATA(job
->event
->input
), len
);
611 server_redraw_client(c
);
614 /* Return winlink status line entry and adjust gc as necessary. */
617 struct client
*c
, struct winlink
*wl
, time_t t
, struct grid_cell
*gc
)
619 struct options
*oo
= &wl
->window
->options
;
620 struct session
*s
= c
->session
;
625 fg
= options_get_number(oo
, "window-status-fg");
627 colour_set_fg(gc
, fg
);
628 bg
= options_get_number(oo
, "window-status-bg");
630 colour_set_bg(gc
, bg
);
631 attr
= options_get_number(oo
, "window-status-attr");
634 fmt
= options_get_string(oo
, "window-status-format");
636 fg
= options_get_number(oo
, "window-status-current-fg");
638 colour_set_fg(gc
, fg
);
639 bg
= options_get_number(oo
, "window-status-current-bg");
641 colour_set_bg(gc
, bg
);
642 attr
= options_get_number(oo
, "window-status-current-attr");
645 fmt
= options_get_string(oo
, "window-status-current-format");
648 if (wl
->flags
& WINLINK_ALERTFLAGS
) {
649 fg
= options_get_number(oo
, "window-status-alert-fg");
651 colour_set_fg(gc
, fg
);
652 bg
= options_get_number(oo
, "window-status-alert-bg");
654 colour_set_bg(gc
, bg
);
655 attr
= options_get_number(oo
, "window-status-alert-attr");
660 text
= status_replace(c
, wl
, fmt
, t
, 1);
664 /* Set a status line message. */
666 status_message_set(struct client
*c
, const char *fmt
, ...)
669 struct session
*s
= c
->session
;
670 struct message_entry
*msg
;
675 status_prompt_clear(c
);
676 status_message_clear(c
);
679 xvasprintf(&c
->message_string
, fmt
, ap
);
682 ARRAY_EXPAND(&c
->message_log
, 1);
683 msg
= &ARRAY_LAST(&c
->message_log
);
684 msg
->msg_time
= time(NULL
);
685 msg
->msg
= xstrdup(c
->message_string
);
690 limit
= options_get_number(&s
->options
, "message-limit");
691 if (ARRAY_LENGTH(&c
->message_log
) > limit
) {
692 limit
= ARRAY_LENGTH(&c
->message_log
) - limit
;
693 for (i
= 0; i
< limit
; i
++) {
694 msg
= &ARRAY_FIRST(&c
->message_log
);
696 ARRAY_REMOVE(&c
->message_log
, 0);
700 delay
= options_get_number(&c
->session
->options
, "display-time");
701 tv
.tv_sec
= delay
/ 1000;
702 tv
.tv_usec
= (delay
% 1000) * 1000L;
704 evtimer_del(&c
->message_timer
);
705 evtimer_set(&c
->message_timer
, status_message_callback
, c
);
706 evtimer_add(&c
->message_timer
, &tv
);
708 c
->tty
.flags
|= (TTY_NOCURSOR
|TTY_FREEZE
);
709 c
->flags
|= CLIENT_STATUS
;
712 /* Clear status line message. */
714 status_message_clear(struct client
*c
)
716 if (c
->message_string
== NULL
)
719 xfree(c
->message_string
);
720 c
->message_string
= NULL
;
722 c
->tty
.flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
);
723 c
->flags
|= CLIENT_REDRAW
; /* screen was frozen and may have changed */
725 screen_reinit(&c
->status
);
728 /* Clear status line message after timer expires. */
731 status_message_callback(unused
int fd
, unused
short event
, void *data
)
733 struct client
*c
= data
;
735 status_message_clear(c
);
738 /* Draw client message on status line of present else on last line. */
740 status_message_redraw(struct client
*c
)
742 struct screen_write_ctx ctx
;
743 struct session
*s
= c
->session
;
744 struct screen old_status
;
749 if (c
->tty
.sx
== 0 || c
->tty
.sy
== 0)
751 memcpy(&old_status
, &c
->status
, sizeof old_status
);
752 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
754 utf8flag
= options_get_number(&s
->options
, "status-utf8");
756 len
= screen_write_strlen(utf8flag
, "%s", c
->message_string
);
760 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
761 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-fg"));
762 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-bg"));
763 gc
.attr
|= options_get_number(&s
->options
, "message-attr");
765 screen_write_start(&ctx
, NULL
, &c
->status
);
767 screen_write_cursormove(&ctx
, 0, 0);
768 screen_write_nputs(&ctx
, len
, &gc
, utf8flag
, "%s", c
->message_string
);
769 for (; len
< c
->tty
.sx
; len
++)
770 screen_write_putc(&ctx
, &gc
, ' ');
772 screen_write_stop(&ctx
);
774 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
775 screen_free(&old_status
);
778 screen_free(&old_status
);
782 /* Enable status line prompt. */
784 status_prompt_set(struct client
*c
, const char *msg
,
785 int (*callbackfn
)(void *, const char *), void (*freefn
)(void *),
786 void *data
, int flags
)
790 status_message_clear(c
);
791 status_prompt_clear(c
);
793 c
->prompt_string
= xstrdup(msg
);
795 c
->prompt_buffer
= xstrdup("");
798 c
->prompt_callbackfn
= callbackfn
;
799 c
->prompt_freefn
= freefn
;
800 c
->prompt_data
= data
;
802 c
->prompt_hindex
= 0;
804 c
->prompt_flags
= flags
;
806 keys
= options_get_number(&c
->session
->options
, "status-keys");
807 if (keys
== MODEKEY_EMACS
)
808 mode_key_init(&c
->prompt_mdata
, &mode_key_tree_emacs_edit
);
810 mode_key_init(&c
->prompt_mdata
, &mode_key_tree_vi_edit
);
812 c
->tty
.flags
|= (TTY_NOCURSOR
|TTY_FREEZE
);
813 c
->flags
|= CLIENT_STATUS
;
816 /* Remove status line prompt. */
818 status_prompt_clear(struct client
*c
)
820 if (c
->prompt_string
== NULL
)
823 if (c
->prompt_freefn
!= NULL
&& c
->prompt_data
!= NULL
)
824 c
->prompt_freefn(c
->prompt_data
);
826 xfree(c
->prompt_string
);
827 c
->prompt_string
= NULL
;
829 xfree(c
->prompt_buffer
);
830 c
->prompt_buffer
= NULL
;
832 c
->tty
.flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
);
833 c
->flags
|= CLIENT_REDRAW
; /* screen was frozen and may have changed */
835 screen_reinit(&c
->status
);
838 /* Update status line prompt with a new prompt string. */
840 status_prompt_update(struct client
*c
, const char *msg
)
842 xfree(c
->prompt_string
);
843 c
->prompt_string
= xstrdup(msg
);
845 *c
->prompt_buffer
= '\0';
848 c
->prompt_hindex
= 0;
850 c
->flags
|= CLIENT_STATUS
;
853 /* Draw client prompt on status line of present else on last line. */
855 status_prompt_redraw(struct client
*c
)
857 struct screen_write_ctx ctx
;
858 struct session
*s
= c
->session
;
859 struct screen old_status
;
860 size_t i
, size
, left
, len
, off
;
861 struct grid_cell gc
, *gcp
;
864 if (c
->tty
.sx
== 0 || c
->tty
.sy
== 0)
866 memcpy(&old_status
, &c
->status
, sizeof old_status
);
867 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
869 utf8flag
= options_get_number(&s
->options
, "status-utf8");
871 len
= screen_write_strlen(utf8flag
, "%s", c
->prompt_string
);
876 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
877 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-fg"));
878 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-bg"));
879 gc
.attr
|= options_get_number(&s
->options
, "message-attr");
881 screen_write_start(&ctx
, NULL
, &c
->status
);
883 screen_write_cursormove(&ctx
, 0, 0);
884 screen_write_nputs(&ctx
, len
, &gc
, utf8flag
, "%s", c
->prompt_string
);
886 left
= c
->tty
.sx
- len
;
888 size
= screen_write_strlen(utf8flag
, "%s", c
->prompt_buffer
);
889 if (c
->prompt_index
>= left
) {
890 off
= c
->prompt_index
- left
+ 1;
891 if (c
->prompt_index
== size
)
896 &ctx
, left
, &gc
, utf8flag
, "%s", c
->prompt_buffer
+ off
);
898 for (i
= len
+ size
; i
< c
->tty
.sx
; i
++)
899 screen_write_putc(&ctx
, &gc
, ' ');
902 screen_write_stop(&ctx
);
904 /* Apply fake cursor. */
905 off
= len
+ c
->prompt_index
- off
;
906 gcp
= grid_view_get_cell(c
->status
.grid
, off
, 0);
907 gcp
->attr
^= GRID_ATTR_REVERSE
;
909 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
910 screen_free(&old_status
);
913 screen_free(&old_status
);
917 /* Handle keys in prompt. */
919 status_prompt_key(struct client
*c
, int key
)
921 struct paste_buffer
*pb
;
922 char *s
, *first
, *last
, word
[64], swapc
;
925 size_t size
, n
, off
, idx
;
927 size
= strlen(c
->prompt_buffer
);
928 switch (mode_key_lookup(&c
->prompt_mdata
, key
)) {
929 case MODEKEYEDIT_CURSORLEFT
:
930 if (c
->prompt_index
> 0) {
932 c
->flags
|= CLIENT_STATUS
;
935 case MODEKEYEDIT_SWITCHMODEAPPEND
:
936 case MODEKEYEDIT_CURSORRIGHT
:
937 if (c
->prompt_index
< size
) {
939 c
->flags
|= CLIENT_STATUS
;
942 case MODEKEYEDIT_STARTOFLINE
:
943 if (c
->prompt_index
!= 0) {
945 c
->flags
|= CLIENT_STATUS
;
948 case MODEKEYEDIT_ENDOFLINE
:
949 if (c
->prompt_index
!= size
) {
950 c
->prompt_index
= size
;
951 c
->flags
|= CLIENT_STATUS
;
954 case MODEKEYEDIT_COMPLETE
:
955 if (*c
->prompt_buffer
== '\0')
958 idx
= c
->prompt_index
;
962 /* Find the word we are in. */
963 first
= c
->prompt_buffer
+ idx
;
964 while (first
> c
->prompt_buffer
&& *first
!= ' ')
966 while (*first
== ' ')
968 last
= c
->prompt_buffer
+ idx
;
969 while (*last
!= '\0' && *last
!= ' ')
976 ((size_t) (last
- first
)) > (sizeof word
) - 1)
978 memcpy(word
, first
, last
- first
);
979 word
[last
- first
] = '\0';
981 /* And try to complete it. */
982 if ((s
= status_prompt_complete(word
)) == NULL
)
986 n
= size
- (last
- c
->prompt_buffer
) + 1; /* with \0 */
987 memmove(first
, last
, n
);
988 size
-= last
- first
;
990 /* Insert the new word. */
992 off
= first
- c
->prompt_buffer
;
993 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ 1);
994 first
= c
->prompt_buffer
+ off
;
995 memmove(first
+ strlen(s
), first
, n
);
996 memcpy(first
, s
, strlen(s
));
998 c
->prompt_index
= (first
- c
->prompt_buffer
) + strlen(s
);
1001 c
->flags
|= CLIENT_STATUS
;
1003 case MODEKEYEDIT_BACKSPACE
:
1004 if (c
->prompt_index
!= 0) {
1005 if (c
->prompt_index
== size
)
1006 c
->prompt_buffer
[--c
->prompt_index
] = '\0';
1008 memmove(c
->prompt_buffer
+ c
->prompt_index
- 1,
1009 c
->prompt_buffer
+ c
->prompt_index
,
1010 size
+ 1 - c
->prompt_index
);
1013 c
->flags
|= CLIENT_STATUS
;
1016 case MODEKEYEDIT_DELETE
:
1017 if (c
->prompt_index
!= size
) {
1018 memmove(c
->prompt_buffer
+ c
->prompt_index
,
1019 c
->prompt_buffer
+ c
->prompt_index
+ 1,
1020 size
+ 1 - c
->prompt_index
);
1021 c
->flags
|= CLIENT_STATUS
;
1024 case MODEKEYEDIT_DELETELINE
:
1025 *c
->prompt_buffer
= '\0';
1026 c
->prompt_index
= 0;
1027 c
->flags
|= CLIENT_STATUS
;
1029 case MODEKEYEDIT_DELETETOENDOFLINE
:
1030 if (c
->prompt_index
< size
) {
1031 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1032 c
->flags
|= CLIENT_STATUS
;
1035 case MODEKEYEDIT_HISTORYUP
:
1036 histstr
= status_prompt_up_history(&c
->prompt_hindex
);
1037 if (histstr
== NULL
)
1039 xfree(c
->prompt_buffer
);
1040 c
->prompt_buffer
= xstrdup(histstr
);
1041 c
->prompt_index
= strlen(c
->prompt_buffer
);
1042 c
->flags
|= CLIENT_STATUS
;
1044 case MODEKEYEDIT_HISTORYDOWN
:
1045 histstr
= status_prompt_down_history(&c
->prompt_hindex
);
1046 if (histstr
== NULL
)
1048 xfree(c
->prompt_buffer
);
1049 c
->prompt_buffer
= xstrdup(histstr
);
1050 c
->prompt_index
= strlen(c
->prompt_buffer
);
1051 c
->flags
|= CLIENT_STATUS
;
1053 case MODEKEYEDIT_PASTE
:
1054 if ((pb
= paste_get_top(&global_buffers
)) == NULL
)
1056 for (n
= 0; n
< pb
->size
; n
++) {
1057 ch
= (u_char
) pb
->data
[n
];
1058 if (ch
< 32 || ch
== 127)
1062 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ n
+ 1);
1063 if (c
->prompt_index
== size
) {
1064 memcpy(c
->prompt_buffer
+ c
->prompt_index
, pb
->data
, n
);
1065 c
->prompt_index
+= n
;
1066 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1068 memmove(c
->prompt_buffer
+ c
->prompt_index
+ n
,
1069 c
->prompt_buffer
+ c
->prompt_index
,
1070 size
+ 1 - c
->prompt_index
);
1071 memcpy(c
->prompt_buffer
+ c
->prompt_index
, pb
->data
, n
);
1072 c
->prompt_index
+= n
;
1075 c
->flags
|= CLIENT_STATUS
;
1077 case MODEKEYEDIT_TRANSPOSECHARS
:
1078 idx
= c
->prompt_index
;
1082 swapc
= c
->prompt_buffer
[idx
- 2];
1083 c
->prompt_buffer
[idx
- 2] = c
->prompt_buffer
[idx
- 1];
1084 c
->prompt_buffer
[idx
- 1] = swapc
;
1085 c
->prompt_index
= idx
;
1086 c
->flags
|= CLIENT_STATUS
;
1089 case MODEKEYEDIT_ENTER
:
1090 if (*c
->prompt_buffer
!= '\0')
1091 status_prompt_add_history(c
->prompt_buffer
);
1092 if (c
->prompt_callbackfn(c
->prompt_data
, c
->prompt_buffer
) == 0)
1093 status_prompt_clear(c
);
1095 case MODEKEYEDIT_CANCEL
:
1096 if (c
->prompt_callbackfn(c
->prompt_data
, NULL
) == 0)
1097 status_prompt_clear(c
);
1100 if ((key
& 0xff00) != 0 || key
< 32 || key
== 127)
1102 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ 2);
1104 if (c
->prompt_index
== size
) {
1105 c
->prompt_buffer
[c
->prompt_index
++] = key
;
1106 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1108 memmove(c
->prompt_buffer
+ c
->prompt_index
+ 1,
1109 c
->prompt_buffer
+ c
->prompt_index
,
1110 size
+ 1 - c
->prompt_index
);
1111 c
->prompt_buffer
[c
->prompt_index
++] = key
;
1114 if (c
->prompt_flags
& PROMPT_SINGLE
) {
1115 if (c
->prompt_callbackfn(
1116 c
->prompt_data
, c
->prompt_buffer
) == 0)
1117 status_prompt_clear(c
);
1120 c
->flags
|= CLIENT_STATUS
;
1127 /* Get previous line from the history. */
1129 status_prompt_up_history(u_int
*idx
)
1134 * History runs from 0 to size - 1.
1136 * Index is from 0 to size. Zero is empty.
1139 size
= ARRAY_LENGTH(&status_prompt_history
);
1140 if (size
== 0 || *idx
== size
)
1143 return (ARRAY_ITEM(&status_prompt_history
, size
- *idx
));
1146 /* Get next line from the history. */
1148 status_prompt_down_history(u_int
*idx
)
1152 size
= ARRAY_LENGTH(&status_prompt_history
);
1153 if (size
== 0 || *idx
== 0)
1158 return (ARRAY_ITEM(&status_prompt_history
, size
- *idx
));
1161 /* Add line to the history. */
1163 status_prompt_add_history(const char *line
)
1167 size
= ARRAY_LENGTH(&status_prompt_history
);
1168 if (size
> 0 && strcmp(ARRAY_LAST(&status_prompt_history
), line
) == 0)
1171 if (size
== PROMPT_HISTORY
) {
1172 xfree(ARRAY_FIRST(&status_prompt_history
));
1173 ARRAY_REMOVE(&status_prompt_history
, 0);
1176 ARRAY_ADD(&status_prompt_history
, xstrdup(line
));
1179 /* Complete word. */
1181 status_prompt_complete(const char *s
)
1183 const struct cmd_entry
**cmdent
;
1184 const struct options_table_entry
*oe
;
1185 ARRAY_DECL(, const char *) list
;
1193 /* First, build a list of all the possible matches. */
1195 for (cmdent
= cmd_table
; *cmdent
!= NULL
; cmdent
++) {
1196 if (strncmp((*cmdent
)->name
, s
, strlen(s
)) == 0)
1197 ARRAY_ADD(&list
, (*cmdent
)->name
);
1199 for (oe
= server_options_table
; oe
->name
!= NULL
; oe
++) {
1200 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1201 ARRAY_ADD(&list
, oe
->name
);
1203 for (oe
= session_options_table
; oe
->name
!= NULL
; oe
++) {
1204 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1205 ARRAY_ADD(&list
, oe
->name
);
1207 for (oe
= window_options_table
; oe
->name
!= NULL
; oe
++) {
1208 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1209 ARRAY_ADD(&list
, oe
->name
);
1212 /* If none, bail now. */
1213 if (ARRAY_LENGTH(&list
) == 0) {
1218 /* If an exact match, return it, with a trailing space. */
1219 if (ARRAY_LENGTH(&list
) == 1) {
1220 xasprintf(&s2
, "%s ", ARRAY_FIRST(&list
));
1225 /* Now loop through the list and find the longest common prefix. */
1226 prefix
= xstrdup(ARRAY_FIRST(&list
));
1227 for (i
= 1; i
< ARRAY_LENGTH(&list
); i
++) {
1228 s
= ARRAY_ITEM(&list
, i
);
1231 if (j
> strlen(prefix
))
1233 for (; j
> 0; j
--) {
1234 if (prefix
[j
- 1] != s
[j
- 1])
1235 prefix
[j
- 1] = '\0';