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 /* 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(c
, NULL
,
84 NULL
, 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(c
, NULL
,
114 NULL
, 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 /* Set window at window list position. */
125 status_set_window_at(struct client
*c
, u_int x
)
127 struct session
*s
= c
->session
;
131 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
132 if (x
< wl
->status_width
&&
133 session_select(s
, wl
->idx
) == 0) {
134 server_redraw_session(s
);
136 x
-= wl
->status_width
+ 1;
140 /* Draw status for client on the last lines of given context. */
142 status_redraw(struct client
*c
)
144 struct screen_write_ctx ctx
;
145 struct session
*s
= c
->session
;
147 struct screen old_status
, window_list
;
148 struct grid_cell stdgc
, lgc
, rgc
, gc
;
151 u_int offset
, needed
;
152 u_int wlstart
, wlwidth
, wlavailable
, wloffset
, wlsize
;
154 int larrow
, rarrow
, utf8flag
;
156 /* No status line? */
157 if (c
->tty
.sy
== 0 || !options_get_number(&s
->options
, "status"))
162 /* Update status timer. */
163 if (gettimeofday(&c
->status_timer
, NULL
) != 0)
164 fatal("gettimeofday failed");
165 t
= c
->status_timer
.tv_sec
;
167 /* Set up default colour. */
168 memcpy(&stdgc
, &grid_default_cell
, sizeof gc
);
169 colour_set_fg(&stdgc
, options_get_number(&s
->options
, "status-fg"));
170 colour_set_bg(&stdgc
, options_get_number(&s
->options
, "status-bg"));
171 stdgc
.attr
|= options_get_number(&s
->options
, "status-attr");
173 /* Create the target screen. */
174 memcpy(&old_status
, &c
->status
, sizeof old_status
);
175 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
176 screen_write_start(&ctx
, NULL
, &c
->status
);
177 for (offset
= 0; offset
< c
->tty
.sx
; offset
++)
178 screen_write_putc(&ctx
, &stdgc
, ' ');
179 screen_write_stop(&ctx
);
181 /* If the height is one line, blank status line. */
185 /* Get UTF-8 flag. */
186 utf8flag
= options_get_number(&s
->options
, "status-utf8");
188 /* Work out left and right strings. */
189 memcpy(&lgc
, &stdgc
, sizeof lgc
);
190 left
= status_redraw_get_left(c
, t
, utf8flag
, &lgc
, &llen
);
191 memcpy(&rgc
, &stdgc
, sizeof rgc
);
192 right
= status_redraw_get_right(c
, t
, utf8flag
, &rgc
, &rlen
);
195 * Figure out how much space we have for the window list. If there
196 * isn't enough space, just show a blank status line.
203 if (c
->tty
.sx
== 0 || c
->tty
.sx
<= needed
)
205 wlavailable
= c
->tty
.sx
- needed
;
207 /* Calculate the total size needed for the window list. */
208 wlstart
= wloffset
= wlwidth
= 0;
209 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
210 if (wl
->status_text
!= NULL
)
211 xfree(wl
->status_text
);
212 memcpy(&wl
->status_cell
, &stdgc
, sizeof wl
->status_cell
);
213 wl
->status_text
= status_print(c
, wl
, t
, &wl
->status_cell
);
215 screen_write_cstrlen(utf8flag
, "%s", wl
->status_text
);
219 wlwidth
+= wl
->status_width
+ 1;
222 /* Create a new screen for the window list. */
223 screen_init(&window_list
, wlwidth
, 1, 0);
225 /* And draw the window list into it. */
226 screen_write_start(&ctx
, NULL
, &window_list
);
227 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
228 screen_write_cnputs(&ctx
,
229 -1, &wl
->status_cell
, utf8flag
, "%s", wl
->status_text
);
230 screen_write_putc(&ctx
, &stdgc
, ' ');
232 screen_write_stop(&ctx
);
234 /* If there is enough space for the total width, skip to draw now. */
235 if (wlwidth
<= wlavailable
)
238 /* Find size of current window text. */
239 wlsize
= s
->curw
->status_width
;
242 * If the current window is already on screen, good to draw from the
243 * start and just leave off the end.
245 if (wloffset
+ wlsize
< wlavailable
) {
246 if (wlavailable
> 0) {
250 wlwidth
= wlavailable
;
253 * Work out how many characters we need to omit from the
254 * start. There are wlavailable characters to fill, and
255 * wloffset + wlsize must be the last. So, the start character
256 * is wloffset + wlsize - wlavailable.
258 if (wlavailable
> 0) {
263 wlstart
= wloffset
+ wlsize
- wlavailable
;
264 if (wlavailable
> 0 && wlwidth
> wlstart
+ wlavailable
+ 1) {
269 wlwidth
= wlavailable
;
272 /* Bail if anything is now too small too. */
273 if (wlwidth
== 0 || wlavailable
== 0) {
274 screen_free(&window_list
);
279 * Now the start position is known, work out the state of the left and
283 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
284 if (wl
->flags
& WINLINK_ALERTFLAGS
&&
285 larrow
== 1 && offset
< wlstart
)
288 offset
+= wl
->status_width
;
290 if (wl
->flags
& WINLINK_ALERTFLAGS
&&
291 rarrow
== 1 && offset
> wlstart
+ wlwidth
)
297 screen_write_start(&ctx
, NULL
, &c
->status
);
299 /* Draw the left string and arrow. */
300 screen_write_cursormove(&ctx
, 0, 0);
302 screen_write_cnputs(&ctx
, llen
, &lgc
, utf8flag
, "%s", left
);
303 screen_write_putc(&ctx
, &stdgc
, ' ');
306 memcpy(&gc
, &stdgc
, sizeof gc
);
308 gc
.attr
^= GRID_ATTR_REVERSE
;
309 screen_write_putc(&ctx
, &gc
, '<');
312 /* Draw the right string and arrow. */
314 screen_write_cursormove(&ctx
, c
->tty
.sx
- rlen
- 2, 0);
315 memcpy(&gc
, &stdgc
, sizeof gc
);
317 gc
.attr
^= GRID_ATTR_REVERSE
;
318 screen_write_putc(&ctx
, &gc
, '>');
320 screen_write_cursormove(&ctx
, c
->tty
.sx
- rlen
- 1, 0);
322 screen_write_putc(&ctx
, &stdgc
, ' ');
323 screen_write_cnputs(&ctx
, rlen
, &rgc
, utf8flag
, "%s", right
);
326 /* Figure out the offset for the window list. */
331 if (wlwidth
< wlavailable
) {
332 switch (options_get_number(&s
->options
, "status-justify")) {
333 case 1: /* centered */
334 wloffset
+= (wlavailable
- wlwidth
) / 2;
337 wloffset
+= (wlavailable
- wlwidth
);
344 /* Copy the window list. */
345 s
->wlmouse
= -wloffset
+ wlstart
;
346 screen_write_cursormove(&ctx
, wloffset
, 0);
347 screen_write_copy(&ctx
, &window_list
, wlstart
, 0, wlwidth
, 1);
348 screen_free(&window_list
);
350 screen_write_stop(&ctx
);
358 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
359 screen_free(&old_status
);
362 screen_free(&old_status
);
366 /* Replace a single special sequence (prefixed by #). */
368 status_replace1(struct client
*c
, struct session
*s
, struct winlink
*wl
,
369 struct window_pane
*wp
, char **iptr
, char **optr
, char *out
,
370 size_t outsize
, int jobsflag
)
372 char ch
, tmp
[256], *ptr
, *endptr
, *freeptr
;
382 wp
= wl
->window
->active
;
385 limit
= strtol(*iptr
, &endptr
, 10);
386 if ((limit
== 0 && errno
!= EINVAL
) ||
387 (limit
== LONG_MIN
&& errno
!= ERANGE
) ||
388 (limit
== LONG_MAX
&& errno
!= ERANGE
) ||
396 switch (*(*iptr
)++) {
402 if ((ptr
= status_find_job(c
, iptr
)) == NULL
)
406 xsnprintf(tmp
, sizeof tmp
, "%%%u", wp
->id
);
410 if (gethostname(tmp
, sizeof tmp
) != 0)
411 fatal("gethostname failed");
415 if (gethostname(tmp
, sizeof tmp
) != 0)
416 fatal("gethostname failed");
417 if ((ptr
= strchr(tmp
, '.')) != NULL
)
422 xsnprintf(tmp
, sizeof tmp
, "%d", wl
->idx
);
426 if (window_pane_index(wp
, &idx
) != 0)
427 fatalx("index not found");
429 tmp
, sizeof tmp
, "%u", idx
);
436 ptr
= wp
->base
.title
;
439 ptr
= wl
->window
->name
;
442 ptr
= window_printable_flags(s
, wl
);
447 * Embedded style, handled at display time. Leave present and
448 * skip input until ].
460 ptrlen
= strlen(ptr
);
461 if ((size_t) limit
< ptrlen
)
464 if (*optr
+ ptrlen
>= out
+ outsize
- 1)
466 while (ptrlen
> 0 && *ptr
!= '\0') {
478 (*iptr
)--; /* include ch */
479 while (**iptr
!= ch
&& **iptr
!= '\0') {
480 if (*optr
>= out
+ outsize
- 1)
482 *(*optr
)++ = *(*iptr
)++;
486 /* Replace special sequences in fmt. */
488 status_replace(struct client
*c
, struct session
*s
, struct winlink
*wl
,
489 struct window_pane
*wp
, const char *fmt
, time_t t
, int jobsflag
)
491 static char out
[BUFSIZ
];
492 char in
[BUFSIZ
], ch
, *iptr
, *optr
;
494 strftime(in
, sizeof in
, fmt
, localtime(&t
));
495 in
[(sizeof in
) - 1] = '\0';
500 while (*iptr
!= '\0') {
501 if (optr
>= out
+ (sizeof out
) - 1)
505 if (ch
!= '#' || *iptr
== '\0') {
510 c
, s
, wl
, wp
, &iptr
, &optr
, out
, sizeof out
, jobsflag
);
514 return (xstrdup(out
));
517 /* Figure out job name and get its result, starting it off if necessary. */
519 status_find_job(struct client
*c
, char **iptr
)
521 struct status_out
*so
, so_find
;
528 if (**iptr
== ')') { /* no command given */
533 cmd
= xmalloc(strlen(*iptr
) + 1);
537 for (; **iptr
!= '\0'; (*iptr
)++) {
538 if (!lastesc
&& **iptr
== ')')
539 break; /* unescaped ) is the end */
540 if (!lastesc
&& **iptr
== '\\') {
542 continue; /* skip \ if not escaped */
547 if (**iptr
== '\0') /* no terminating ) */ {
551 (*iptr
)++; /* skip final ) */
554 /* First try in the new tree. */
556 so
= RB_FIND(status_out_tree
, &c
->status_new
, &so_find
);
557 if (so
!= NULL
&& so
->out
!= NULL
) {
562 /* If not found at all, start the job and add to the tree. */
564 job_run(cmd
, status_job_callback
, status_job_free
, c
);
567 so
= xmalloc(sizeof *so
);
568 so
->cmd
= xstrdup(cmd
);
570 RB_INSERT(status_out_tree
, &c
->status_new
, so
);
573 /* Lookup in the old tree. */
575 so
= RB_FIND(status_out_tree
, &c
->status_old
, &so_find
);
584 status_free_jobs(struct status_out_tree
*sotree
)
586 struct status_out
*so
, *so_next
;
588 so_next
= RB_MIN(status_out_tree
, sotree
);
589 while (so_next
!= NULL
) {
591 so_next
= RB_NEXT(status_out_tree
, sotree
, so
);
593 RB_REMOVE(status_out_tree
, sotree
, so
);
601 /* Update jobs on status interval. */
603 status_update_jobs(struct client
*c
)
605 /* Free the old tree. */
606 status_free_jobs(&c
->status_old
);
608 /* Move the new to old. */
609 memcpy(&c
->status_old
, &c
->status_new
, sizeof c
->status_old
);
610 RB_INIT(&c
->status_new
);
613 /* Free status job. */
615 status_job_free(void *data
)
617 struct client
*c
= data
;
622 /* Job has finished: save its result. */
624 status_job_callback(struct job
*job
)
626 struct client
*c
= job
->data
;
627 struct status_out
*so
, so_find
;
631 if (c
->flags
& CLIENT_DEAD
)
634 so_find
.cmd
= job
->cmd
;
635 so
= RB_FIND(status_out_tree
, &c
->status_new
, &so_find
);
636 if (so
== NULL
|| so
->out
!= NULL
)
640 if ((line
= evbuffer_readline(job
->event
->input
)) == NULL
) {
641 len
= EVBUFFER_LENGTH(job
->event
->input
);
642 buf
= xmalloc(len
+ 1);
644 memcpy(buf
, EVBUFFER_DATA(job
->event
->input
), len
);
650 server_status_client(c
);
653 /* Return winlink status line entry and adjust gc as necessary. */
656 struct client
*c
, struct winlink
*wl
, time_t t
, struct grid_cell
*gc
)
658 struct options
*oo
= &wl
->window
->options
;
659 struct session
*s
= c
->session
;
664 fg
= options_get_number(oo
, "window-status-fg");
666 colour_set_fg(gc
, fg
);
667 bg
= options_get_number(oo
, "window-status-bg");
669 colour_set_bg(gc
, bg
);
670 attr
= options_get_number(oo
, "window-status-attr");
673 fmt
= options_get_string(oo
, "window-status-format");
675 fg
= options_get_number(oo
, "window-status-current-fg");
677 colour_set_fg(gc
, fg
);
678 bg
= options_get_number(oo
, "window-status-current-bg");
680 colour_set_bg(gc
, bg
);
681 attr
= options_get_number(oo
, "window-status-current-attr");
684 fmt
= options_get_string(oo
, "window-status-current-format");
687 if (wl
->flags
& WINLINK_BELL
) {
688 fg
= options_get_number(oo
, "window-status-bell-fg");
690 colour_set_fg(gc
, fg
);
691 bg
= options_get_number(oo
, "window-status-bell-bg");
693 colour_set_bg(gc
, bg
);
694 attr
= options_get_number(oo
, "window-status-bell-attr");
697 } else if (wl
->flags
& WINLINK_CONTENT
) {
698 fg
= options_get_number(oo
, "window-status-content-fg");
700 colour_set_fg(gc
, fg
);
701 bg
= options_get_number(oo
, "window-status-content-bg");
703 colour_set_bg(gc
, bg
);
704 attr
= options_get_number(oo
, "window-status-content-attr");
707 } else if (wl
->flags
& (WINLINK_ACTIVITY
|WINLINK_SILENCE
)) {
708 fg
= options_get_number(oo
, "window-status-activity-fg");
710 colour_set_fg(gc
, fg
);
711 bg
= options_get_number(oo
, "window-status-activity-bg");
713 colour_set_bg(gc
, bg
);
714 attr
= options_get_number(oo
, "window-status-activity-attr");
719 text
= status_replace(c
, NULL
, wl
, NULL
, fmt
, t
, 1);
723 /* Set a status line message. */
725 status_message_set(struct client
*c
, const char *fmt
, ...)
728 struct session
*s
= c
->session
;
729 struct message_entry
*msg
;
734 status_prompt_clear(c
);
735 status_message_clear(c
);
738 xvasprintf(&c
->message_string
, fmt
, ap
);
741 ARRAY_EXPAND(&c
->message_log
, 1);
742 msg
= &ARRAY_LAST(&c
->message_log
);
743 msg
->msg_time
= time(NULL
);
744 msg
->msg
= xstrdup(c
->message_string
);
749 limit
= options_get_number(&s
->options
, "message-limit");
750 if (ARRAY_LENGTH(&c
->message_log
) > limit
) {
751 limit
= ARRAY_LENGTH(&c
->message_log
) - limit
;
752 for (i
= 0; i
< limit
; i
++) {
753 msg
= &ARRAY_FIRST(&c
->message_log
);
755 ARRAY_REMOVE(&c
->message_log
, 0);
759 delay
= options_get_number(&c
->session
->options
, "display-time");
760 tv
.tv_sec
= delay
/ 1000;
761 tv
.tv_usec
= (delay
% 1000) * 1000L;
763 evtimer_del(&c
->message_timer
);
764 evtimer_set(&c
->message_timer
, status_message_callback
, c
);
765 evtimer_add(&c
->message_timer
, &tv
);
767 c
->tty
.flags
|= (TTY_NOCURSOR
|TTY_FREEZE
);
768 c
->flags
|= CLIENT_STATUS
;
771 /* Clear status line message. */
773 status_message_clear(struct client
*c
)
775 if (c
->message_string
== NULL
)
778 xfree(c
->message_string
);
779 c
->message_string
= NULL
;
781 c
->tty
.flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
);
782 c
->flags
|= CLIENT_REDRAW
; /* screen was frozen and may have changed */
784 screen_reinit(&c
->status
);
787 /* Clear status line message after timer expires. */
790 status_message_callback(unused
int fd
, unused
short event
, void *data
)
792 struct client
*c
= data
;
794 status_message_clear(c
);
797 /* Draw client message on status line of present else on last line. */
799 status_message_redraw(struct client
*c
)
801 struct screen_write_ctx ctx
;
802 struct session
*s
= c
->session
;
803 struct screen old_status
;
808 if (c
->tty
.sx
== 0 || c
->tty
.sy
== 0)
810 memcpy(&old_status
, &c
->status
, sizeof old_status
);
811 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
813 utf8flag
= options_get_number(&s
->options
, "status-utf8");
815 len
= screen_write_strlen(utf8flag
, "%s", c
->message_string
);
819 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
820 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-fg"));
821 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-bg"));
822 gc
.attr
|= options_get_number(&s
->options
, "message-attr");
824 screen_write_start(&ctx
, NULL
, &c
->status
);
826 screen_write_cursormove(&ctx
, 0, 0);
827 screen_write_nputs(&ctx
, len
, &gc
, utf8flag
, "%s", c
->message_string
);
828 for (; len
< c
->tty
.sx
; len
++)
829 screen_write_putc(&ctx
, &gc
, ' ');
831 screen_write_stop(&ctx
);
833 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
834 screen_free(&old_status
);
837 screen_free(&old_status
);
841 /* Enable status line prompt. */
843 status_prompt_set(struct client
*c
, const char *msg
, const char *input
,
844 int (*callbackfn
)(void *, const char *), void (*freefn
)(void *),
845 void *data
, int flags
)
849 status_message_clear(c
);
850 status_prompt_clear(c
);
852 c
->prompt_string
= status_replace(c
, NULL
, NULL
, NULL
, msg
,
857 c
->prompt_buffer
= status_replace(c
, NULL
, NULL
, NULL
, input
,
859 c
->prompt_index
= strlen(c
->prompt_buffer
);
861 c
->prompt_callbackfn
= callbackfn
;
862 c
->prompt_freefn
= freefn
;
863 c
->prompt_data
= data
;
865 c
->prompt_hindex
= 0;
867 c
->prompt_flags
= flags
;
869 keys
= options_get_number(&c
->session
->options
, "status-keys");
870 if (keys
== MODEKEY_EMACS
)
871 mode_key_init(&c
->prompt_mdata
, &mode_key_tree_emacs_edit
);
873 mode_key_init(&c
->prompt_mdata
, &mode_key_tree_vi_edit
);
875 c
->tty
.flags
|= (TTY_NOCURSOR
|TTY_FREEZE
);
876 c
->flags
|= CLIENT_STATUS
;
879 /* Remove status line prompt. */
881 status_prompt_clear(struct client
*c
)
883 if (c
->prompt_string
== NULL
)
886 if (c
->prompt_freefn
!= NULL
&& c
->prompt_data
!= NULL
)
887 c
->prompt_freefn(c
->prompt_data
);
889 xfree(c
->prompt_string
);
890 c
->prompt_string
= NULL
;
892 xfree(c
->prompt_buffer
);
893 c
->prompt_buffer
= NULL
;
895 c
->tty
.flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
);
896 c
->flags
|= CLIENT_REDRAW
; /* screen was frozen and may have changed */
898 screen_reinit(&c
->status
);
901 /* Update status line prompt with a new prompt string. */
903 status_prompt_update(struct client
*c
, const char *msg
, const char *input
)
905 xfree(c
->prompt_string
);
906 c
->prompt_string
= status_replace(c
, NULL
, NULL
, NULL
, msg
,
909 xfree(c
->prompt_buffer
);
912 c
->prompt_buffer
= status_replace(c
, NULL
, NULL
, NULL
, input
,
914 c
->prompt_index
= strlen(c
->prompt_buffer
);
916 c
->prompt_hindex
= 0;
918 c
->flags
|= CLIENT_STATUS
;
921 /* Draw client prompt on status line of present else on last line. */
923 status_prompt_redraw(struct client
*c
)
925 struct screen_write_ctx ctx
;
926 struct session
*s
= c
->session
;
927 struct screen old_status
;
928 size_t i
, size
, left
, len
, off
;
929 struct grid_cell gc
, *gcp
;
932 if (c
->tty
.sx
== 0 || c
->tty
.sy
== 0)
934 memcpy(&old_status
, &c
->status
, sizeof old_status
);
935 screen_init(&c
->status
, c
->tty
.sx
, 1, 0);
937 utf8flag
= options_get_number(&s
->options
, "status-utf8");
939 len
= screen_write_strlen(utf8flag
, "%s", c
->prompt_string
);
944 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
945 /* Change colours for command mode. */
946 if (c
->prompt_mdata
.mode
== 1) {
947 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-command-fg"));
948 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-command-bg"));
949 gc
.attr
|= options_get_number(&s
->options
, "message-command-attr");
951 colour_set_fg(&gc
, options_get_number(&s
->options
, "message-fg"));
952 colour_set_bg(&gc
, options_get_number(&s
->options
, "message-bg"));
953 gc
.attr
|= options_get_number(&s
->options
, "message-attr");
956 screen_write_start(&ctx
, NULL
, &c
->status
);
958 screen_write_cursormove(&ctx
, 0, 0);
959 screen_write_nputs(&ctx
, len
, &gc
, utf8flag
, "%s", c
->prompt_string
);
961 left
= c
->tty
.sx
- len
;
963 size
= screen_write_strlen(utf8flag
, "%s", c
->prompt_buffer
);
964 if (c
->prompt_index
>= left
) {
965 off
= c
->prompt_index
- left
+ 1;
966 if (c
->prompt_index
== size
)
971 &ctx
, left
, &gc
, utf8flag
, "%s", c
->prompt_buffer
+ off
);
973 for (i
= len
+ size
; i
< c
->tty
.sx
; i
++)
974 screen_write_putc(&ctx
, &gc
, ' ');
977 screen_write_stop(&ctx
);
979 /* Apply fake cursor. */
980 off
= len
+ c
->prompt_index
- off
;
981 gcp
= grid_view_get_cell(c
->status
.grid
, off
, 0);
982 gcp
->attr
^= GRID_ATTR_REVERSE
;
984 if (grid_compare(c
->status
.grid
, old_status
.grid
) == 0) {
985 screen_free(&old_status
);
988 screen_free(&old_status
);
992 /* Handle keys in prompt. */
994 status_prompt_key(struct client
*c
, int key
)
996 struct session
*sess
= c
->session
;
997 struct options
*oo
= &sess
->options
;
998 struct paste_buffer
*pb
;
999 char *s
, *first
, *last
, word
[64], swapc
;
1000 const char *histstr
;
1001 const char *wsep
= NULL
;
1003 size_t size
, n
, off
, idx
;
1005 size
= strlen(c
->prompt_buffer
);
1006 switch (mode_key_lookup(&c
->prompt_mdata
, key
)) {
1007 case MODEKEYEDIT_CURSORLEFT
:
1008 if (c
->prompt_index
> 0) {
1010 c
->flags
|= CLIENT_STATUS
;
1013 case MODEKEYEDIT_SWITCHMODE
:
1014 c
->flags
|= CLIENT_STATUS
;
1016 case MODEKEYEDIT_SWITCHMODEAPPEND
:
1017 c
->flags
|= CLIENT_STATUS
;
1019 case MODEKEYEDIT_CURSORRIGHT
:
1020 if (c
->prompt_index
< size
) {
1022 c
->flags
|= CLIENT_STATUS
;
1025 case MODEKEYEDIT_STARTOFLINE
:
1026 if (c
->prompt_index
!= 0) {
1027 c
->prompt_index
= 0;
1028 c
->flags
|= CLIENT_STATUS
;
1031 case MODEKEYEDIT_ENDOFLINE
:
1032 if (c
->prompt_index
!= size
) {
1033 c
->prompt_index
= size
;
1034 c
->flags
|= CLIENT_STATUS
;
1037 case MODEKEYEDIT_COMPLETE
:
1038 if (*c
->prompt_buffer
== '\0')
1041 idx
= c
->prompt_index
;
1045 /* Find the word we are in. */
1046 first
= c
->prompt_buffer
+ idx
;
1047 while (first
> c
->prompt_buffer
&& *first
!= ' ')
1049 while (*first
== ' ')
1051 last
= c
->prompt_buffer
+ idx
;
1052 while (*last
!= '\0' && *last
!= ' ')
1054 while (*last
== ' ')
1058 if (last
<= first
||
1059 ((size_t) (last
- first
)) > (sizeof word
) - 1)
1061 memcpy(word
, first
, last
- first
);
1062 word
[last
- first
] = '\0';
1064 /* And try to complete it. */
1065 if ((s
= status_prompt_complete(word
)) == NULL
)
1068 /* Trim out word. */
1069 n
= size
- (last
- c
->prompt_buffer
) + 1; /* with \0 */
1070 memmove(first
, last
, n
);
1071 size
-= last
- first
;
1073 /* Insert the new word. */
1075 off
= first
- c
->prompt_buffer
;
1076 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ 1);
1077 first
= c
->prompt_buffer
+ off
;
1078 memmove(first
+ strlen(s
), first
, n
);
1079 memcpy(first
, s
, strlen(s
));
1081 c
->prompt_index
= (first
- c
->prompt_buffer
) + strlen(s
);
1084 c
->flags
|= CLIENT_STATUS
;
1086 case MODEKEYEDIT_BACKSPACE
:
1087 if (c
->prompt_index
!= 0) {
1088 if (c
->prompt_index
== size
)
1089 c
->prompt_buffer
[--c
->prompt_index
] = '\0';
1091 memmove(c
->prompt_buffer
+ c
->prompt_index
- 1,
1092 c
->prompt_buffer
+ c
->prompt_index
,
1093 size
+ 1 - c
->prompt_index
);
1096 c
->flags
|= CLIENT_STATUS
;
1099 case MODEKEYEDIT_DELETE
:
1100 if (c
->prompt_index
!= size
) {
1101 memmove(c
->prompt_buffer
+ c
->prompt_index
,
1102 c
->prompt_buffer
+ c
->prompt_index
+ 1,
1103 size
+ 1 - c
->prompt_index
);
1104 c
->flags
|= CLIENT_STATUS
;
1107 case MODEKEYEDIT_DELETELINE
:
1108 *c
->prompt_buffer
= '\0';
1109 c
->prompt_index
= 0;
1110 c
->flags
|= CLIENT_STATUS
;
1112 case MODEKEYEDIT_DELETETOENDOFLINE
:
1113 if (c
->prompt_index
< size
) {
1114 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1115 c
->flags
|= CLIENT_STATUS
;
1118 case MODEKEYEDIT_DELETEWORD
:
1119 wsep
= options_get_string(oo
, "word-separators");
1120 idx
= c
->prompt_index
;
1122 /* Find a non-separator. */
1125 if (!strchr(wsep
, c
->prompt_buffer
[idx
]))
1129 /* Find the separator at the beginning of the word. */
1132 if (strchr(wsep
, c
->prompt_buffer
[idx
])) {
1133 /* Go back to the word. */
1139 memmove(c
->prompt_buffer
+ idx
,
1140 c
->prompt_buffer
+ c
->prompt_index
,
1141 size
+ 1 - c
->prompt_index
);
1142 memset(c
->prompt_buffer
+ size
- (c
->prompt_index
- idx
),
1143 '\0', c
->prompt_index
- idx
);
1144 c
->prompt_index
= idx
;
1145 c
->flags
|= CLIENT_STATUS
;
1147 case MODEKEYEDIT_NEXTSPACE
:
1150 case MODEKEYEDIT_NEXTWORD
:
1152 wsep
= options_get_string(oo
, "word-separators");
1154 /* Find a separator. */
1155 while (c
->prompt_index
!= size
) {
1157 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1161 /* Find the word right after the separation. */
1162 while (c
->prompt_index
!= size
) {
1164 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1168 c
->flags
|= CLIENT_STATUS
;
1170 case MODEKEYEDIT_NEXTSPACEEND
:
1173 case MODEKEYEDIT_NEXTWORDEND
:
1175 wsep
= options_get_string(oo
, "word-separators");
1178 while (c
->prompt_index
!= size
) {
1180 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1184 /* Find the separator at the end of the word. */
1185 while (c
->prompt_index
!= size
) {
1187 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1191 c
->flags
|= CLIENT_STATUS
;
1193 case MODEKEYEDIT_PREVIOUSSPACE
:
1196 case MODEKEYEDIT_PREVIOUSWORD
:
1198 wsep
= options_get_string(oo
, "word-separators");
1200 /* Find a non-separator. */
1201 while (c
->prompt_index
!= 0) {
1203 if (!strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
]))
1207 /* Find the separator at the beginning of the word. */
1208 while (c
->prompt_index
!= 0) {
1210 if (strchr(wsep
, c
->prompt_buffer
[c
->prompt_index
])) {
1211 /* Go back to the word. */
1217 c
->flags
|= CLIENT_STATUS
;
1219 case MODEKEYEDIT_HISTORYUP
:
1220 histstr
= status_prompt_up_history(&c
->prompt_hindex
);
1221 if (histstr
== NULL
)
1223 xfree(c
->prompt_buffer
);
1224 c
->prompt_buffer
= xstrdup(histstr
);
1225 c
->prompt_index
= strlen(c
->prompt_buffer
);
1226 c
->flags
|= CLIENT_STATUS
;
1228 case MODEKEYEDIT_HISTORYDOWN
:
1229 histstr
= status_prompt_down_history(&c
->prompt_hindex
);
1230 if (histstr
== NULL
)
1232 xfree(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_PASTE
:
1238 if ((pb
= paste_get_top(&global_buffers
)) == NULL
)
1240 for (n
= 0; n
< pb
->size
; n
++) {
1241 ch
= (u_char
) pb
->data
[n
];
1242 if (ch
< 32 || ch
== 127)
1246 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ n
+ 1);
1247 if (c
->prompt_index
== size
) {
1248 memcpy(c
->prompt_buffer
+ c
->prompt_index
, pb
->data
, n
);
1249 c
->prompt_index
+= n
;
1250 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1252 memmove(c
->prompt_buffer
+ c
->prompt_index
+ n
,
1253 c
->prompt_buffer
+ c
->prompt_index
,
1254 size
+ 1 - c
->prompt_index
);
1255 memcpy(c
->prompt_buffer
+ c
->prompt_index
, pb
->data
, n
);
1256 c
->prompt_index
+= n
;
1259 c
->flags
|= CLIENT_STATUS
;
1261 case MODEKEYEDIT_TRANSPOSECHARS
:
1262 idx
= c
->prompt_index
;
1266 swapc
= c
->prompt_buffer
[idx
- 2];
1267 c
->prompt_buffer
[idx
- 2] = c
->prompt_buffer
[idx
- 1];
1268 c
->prompt_buffer
[idx
- 1] = swapc
;
1269 c
->prompt_index
= idx
;
1270 c
->flags
|= CLIENT_STATUS
;
1273 case MODEKEYEDIT_ENTER
:
1274 if (*c
->prompt_buffer
!= '\0')
1275 status_prompt_add_history(c
->prompt_buffer
);
1276 if (c
->prompt_callbackfn(c
->prompt_data
, c
->prompt_buffer
) == 0)
1277 status_prompt_clear(c
);
1279 case MODEKEYEDIT_CANCEL
:
1280 if (c
->prompt_callbackfn(c
->prompt_data
, NULL
) == 0)
1281 status_prompt_clear(c
);
1284 if ((key
& 0xff00) != 0 || key
< 32 || key
== 127)
1286 c
->prompt_buffer
= xrealloc(c
->prompt_buffer
, 1, size
+ 2);
1288 if (c
->prompt_index
== size
) {
1289 c
->prompt_buffer
[c
->prompt_index
++] = key
;
1290 c
->prompt_buffer
[c
->prompt_index
] = '\0';
1292 memmove(c
->prompt_buffer
+ c
->prompt_index
+ 1,
1293 c
->prompt_buffer
+ c
->prompt_index
,
1294 size
+ 1 - c
->prompt_index
);
1295 c
->prompt_buffer
[c
->prompt_index
++] = key
;
1298 if (c
->prompt_flags
& PROMPT_SINGLE
) {
1299 if (c
->prompt_callbackfn(
1300 c
->prompt_data
, c
->prompt_buffer
) == 0)
1301 status_prompt_clear(c
);
1304 c
->flags
|= CLIENT_STATUS
;
1311 /* Get previous line from the history. */
1313 status_prompt_up_history(u_int
*idx
)
1318 * History runs from 0 to size - 1.
1320 * Index is from 0 to size. Zero is empty.
1323 size
= ARRAY_LENGTH(&status_prompt_history
);
1324 if (size
== 0 || *idx
== size
)
1327 return (ARRAY_ITEM(&status_prompt_history
, size
- *idx
));
1330 /* Get next line from the history. */
1332 status_prompt_down_history(u_int
*idx
)
1336 size
= ARRAY_LENGTH(&status_prompt_history
);
1337 if (size
== 0 || *idx
== 0)
1342 return (ARRAY_ITEM(&status_prompt_history
, size
- *idx
));
1345 /* Add line to the history. */
1347 status_prompt_add_history(const char *line
)
1351 size
= ARRAY_LENGTH(&status_prompt_history
);
1352 if (size
> 0 && strcmp(ARRAY_LAST(&status_prompt_history
), line
) == 0)
1355 if (size
== PROMPT_HISTORY
) {
1356 xfree(ARRAY_FIRST(&status_prompt_history
));
1357 ARRAY_REMOVE(&status_prompt_history
, 0);
1360 ARRAY_ADD(&status_prompt_history
, xstrdup(line
));
1363 /* Complete word. */
1365 status_prompt_complete(const char *s
)
1367 const struct cmd_entry
**cmdent
;
1368 const struct options_table_entry
*oe
;
1369 ARRAY_DECL(, const char *) list
;
1377 /* First, build a list of all the possible matches. */
1379 for (cmdent
= cmd_table
; *cmdent
!= NULL
; cmdent
++) {
1380 if (strncmp((*cmdent
)->name
, s
, strlen(s
)) == 0)
1381 ARRAY_ADD(&list
, (*cmdent
)->name
);
1383 for (oe
= server_options_table
; oe
->name
!= NULL
; oe
++) {
1384 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1385 ARRAY_ADD(&list
, oe
->name
);
1387 for (oe
= session_options_table
; oe
->name
!= NULL
; oe
++) {
1388 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1389 ARRAY_ADD(&list
, oe
->name
);
1391 for (oe
= window_options_table
; oe
->name
!= NULL
; oe
++) {
1392 if (strncmp(oe
->name
, s
, strlen(s
)) == 0)
1393 ARRAY_ADD(&list
, oe
->name
);
1396 /* If none, bail now. */
1397 if (ARRAY_LENGTH(&list
) == 0) {
1402 /* If an exact match, return it, with a trailing space. */
1403 if (ARRAY_LENGTH(&list
) == 1) {
1404 xasprintf(&s2
, "%s ", ARRAY_FIRST(&list
));
1409 /* Now loop through the list and find the longest common prefix. */
1410 prefix
= xstrdup(ARRAY_FIRST(&list
));
1411 for (i
= 1; i
< ARRAY_LENGTH(&list
); i
++) {
1412 s
= ARRAY_ITEM(&list
, i
);
1415 if (j
> strlen(prefix
))
1417 for (; j
> 0; j
--) {
1418 if (prefix
[j
- 1] != s
[j
- 1])
1419 prefix
[j
- 1] = '\0';