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>
30 const struct cmd_entry
*cmd_table
[] = {
31 &cmd_attach_session_entry
,
33 &cmd_break_pane_entry
,
34 &cmd_capture_pane_entry
,
35 &cmd_choose_buffer_entry
,
36 &cmd_choose_client_entry
,
37 &cmd_choose_session_entry
,
38 &cmd_choose_window_entry
,
39 &cmd_clear_history_entry
,
40 &cmd_clock_mode_entry
,
41 &cmd_command_prompt_entry
,
42 &cmd_confirm_before_entry
,
43 &cmd_copy_buffer_entry
,
45 &cmd_delete_buffer_entry
,
46 &cmd_detach_client_entry
,
47 &cmd_display_message_entry
,
48 &cmd_display_panes_entry
,
49 &cmd_find_window_entry
,
50 &cmd_has_session_entry
,
54 &cmd_kill_server_entry
,
55 &cmd_kill_session_entry
,
56 &cmd_kill_window_entry
,
57 &cmd_last_window_entry
,
58 &cmd_link_window_entry
,
59 &cmd_list_buffers_entry
,
60 &cmd_list_clients_entry
,
61 &cmd_list_commands_entry
,
63 &cmd_list_panes_entry
,
64 &cmd_list_sessions_entry
,
65 &cmd_list_windows_entry
,
66 &cmd_load_buffer_entry
,
67 &cmd_lock_client_entry
,
68 &cmd_lock_server_entry
,
69 &cmd_lock_session_entry
,
70 &cmd_move_window_entry
,
71 &cmd_new_session_entry
,
72 &cmd_new_window_entry
,
73 &cmd_next_layout_entry
,
74 &cmd_next_window_entry
,
75 &cmd_paste_buffer_entry
,
77 &cmd_previous_layout_entry
,
78 &cmd_previous_window_entry
,
79 &cmd_refresh_client_entry
,
80 &cmd_rename_session_entry
,
81 &cmd_rename_window_entry
,
82 &cmd_resize_pane_entry
,
83 &cmd_respawn_window_entry
,
84 &cmd_rotate_window_entry
,
86 &cmd_save_buffer_entry
,
87 &cmd_select_layout_entry
,
88 &cmd_select_pane_entry
,
89 &cmd_select_window_entry
,
91 &cmd_send_prefix_entry
,
92 &cmd_server_info_entry
,
93 &cmd_set_buffer_entry
,
94 &cmd_set_environment_entry
,
95 &cmd_set_option_entry
,
96 &cmd_set_window_option_entry
,
97 &cmd_show_buffer_entry
,
98 &cmd_show_environment_entry
,
99 &cmd_show_messages_entry
,
100 &cmd_show_options_entry
,
101 &cmd_show_window_options_entry
,
102 &cmd_source_file_entry
,
103 &cmd_split_window_entry
,
104 &cmd_start_server_entry
,
105 &cmd_suspend_client_entry
,
106 &cmd_swap_pane_entry
,
107 &cmd_swap_window_entry
,
108 &cmd_switch_client_entry
,
109 &cmd_unbind_key_entry
,
110 &cmd_unlink_window_entry
,
114 struct session
*cmd_choose_session(struct sessions
*);
115 struct client
*cmd_choose_client(struct clients
*);
116 struct client
*cmd_lookup_client(const char *);
117 struct session
*cmd_lookup_session(const char *, int *);
118 struct winlink
*cmd_lookup_window(struct session
*, const char *, int *);
119 int cmd_lookup_index(struct session
*, const char *, int *);
122 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
128 for (i
= 0; i
< argc
; i
++) {
129 if (strlcpy(buf
, argv
[i
], len
) >= len
)
131 arglen
= strlen(argv
[i
]) + 1;
140 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
147 *argv
= xcalloc(argc
, sizeof **argv
);
150 for (i
= 0; i
< argc
; i
++) {
152 cmd_free_argv(argc
, *argv
);
156 arglen
= strlen(buf
) + 1;
157 (*argv
)[i
] = xstrdup(buf
);
166 cmd_free_argv(int argc
, char **argv
)
172 for (i
= 0; i
< argc
; i
++) {
180 cmd_parse(int argc
, char **argv
, char **cause
)
182 const struct cmd_entry
**entryp
, *entry
;
185 int opt
, ambiguous
= 0;
189 xasprintf(cause
, "no command");
194 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
195 if ((*entryp
)->alias
!= NULL
&&
196 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
202 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
208 /* Bail now if an exact match. */
209 if (strcmp(entry
->name
, argv
[0]) == 0)
215 xasprintf(cause
, "unknown command: %s", argv
[0]);
221 if (entry
->parse
== NULL
) {
222 while ((opt
= getopt(argc
, argv
, "")) != -1) {
234 cmd
= xmalloc(sizeof *cmd
);
237 if (entry
->parse
!= NULL
) {
238 if (entry
->parse(cmd
, argc
, argv
, cause
) != 0) {
247 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
248 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
250 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
252 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
255 s
[strlen(s
) - 2] = '\0';
256 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
260 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
265 cmd_exec(struct cmd
*cmd
, struct cmd_ctx
*ctx
)
267 return (cmd
->entry
->exec(cmd
, ctx
));
271 cmd_free(struct cmd
*cmd
)
273 if (cmd
->data
!= NULL
&& cmd
->entry
->free
!= NULL
)
274 cmd
->entry
->free(cmd
);
279 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
281 if (cmd
->entry
->print
== NULL
)
282 return (xsnprintf(buf
, len
, "%s", cmd
->entry
->name
));
283 return (cmd
->entry
->print(cmd
, buf
, len
));
287 * Figure out the current session. Use: 1) the current session, if the command
288 * context has one; 2) the most recently used session containing the pty of the
289 * calling client, if any; 3) the session specified in the TMUX variable from
290 * the environment (as passed from the client); 4) the most recently used
291 * session from all sessions.
294 cmd_current_session(struct cmd_ctx
*ctx
)
296 struct msg_command_data
*data
= ctx
->msgdata
;
297 struct client
*c
= ctx
->cmdclient
;
301 struct window_pane
*wp
;
305 if (ctx
->curclient
!= NULL
&& ctx
->curclient
->session
!= NULL
)
306 return (ctx
->curclient
->session
);
309 * If the name of the calling client's pty is know, build a list of the
310 * sessions that contain it and if any choose either the first or the
313 if (c
!= NULL
&& c
->tty
.path
!= NULL
) {
315 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
316 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
319 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
320 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
321 if (strcmp(wp
->tty
, c
->tty
.path
) == 0) {
333 s
= cmd_choose_session(&ss
);
339 /* Use the session from the TMUX environment variable. */
340 if (data
!= NULL
&& data
->pid
!= -1) {
341 if (data
->pid
!= getpid())
343 if (data
->idx
> ARRAY_LENGTH(&sessions
))
345 if ((s
= ARRAY_ITEM(&sessions
, data
->idx
)) == NULL
)
350 return (cmd_choose_session(&sessions
));
353 /* Find the most recently used session from a list. */
355 cmd_choose_session(struct sessions
*ss
)
357 struct session
*s
, *sbest
;
358 struct timeval
*tv
= NULL
;
362 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
363 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
366 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
368 tv
= &s
->activity_time
;
376 * Find the current client. First try the current client if set, then pick the
377 * most recently used of the clients attached to the current session if any,
378 * then of all clients.
381 cmd_current_client(struct cmd_ctx
*ctx
)
388 if (ctx
->curclient
!= NULL
)
389 return (ctx
->curclient
);
392 * No current client set. Find the current session and return the
393 * newest of its clients.
395 s
= cmd_current_session(ctx
);
396 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
398 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
399 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
405 c
= cmd_choose_client(&cc
);
411 return (cmd_choose_client(&clients
));
414 /* Choose the most recently used client from a list. */
416 cmd_choose_client(struct clients
*cc
)
418 struct client
*c
, *cbest
;
419 struct timeval
*tv
= NULL
;
423 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
424 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
426 if (c
->session
== NULL
)
429 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
431 tv
= &c
->activity_time
;
438 /* Find the target client or report an error and return NULL. */
440 cmd_find_client(struct cmd_ctx
*ctx
, const char *arg
)
446 /* A NULL argument means the current client. */
448 return (cmd_current_client(ctx
));
449 tmparg
= xstrdup(arg
);
451 /* Trim a single trailing colon if any. */
452 arglen
= strlen(tmparg
);
453 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
454 tmparg
[arglen
- 1] = '\0';
456 /* Find the client, if any. */
457 c
= cmd_lookup_client(tmparg
);
459 /* If no client found, report an error. */
461 ctx
->error(ctx
, "client not found: %s", tmparg
);
468 * Lookup a client by device path. Either of a full match and a match without a
469 * leading _PATH_DEV ("/dev/") is accepted.
472 cmd_lookup_client(const char *name
)
478 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
479 c
= ARRAY_ITEM(&clients
, i
);
480 if (c
== NULL
|| c
->session
== NULL
)
484 /* Check for exact matches. */
485 if (strcmp(name
, path
) == 0)
488 /* Check without leading /dev if present. */
489 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
491 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
498 /* Lookup a session by name. If no session is found, NULL is returned. */
500 cmd_lookup_session(const char *name
, int *ambiguous
)
502 struct session
*s
, *sfound
;
508 * Look for matches. First look for exact matches - session names must
509 * be unique so an exact match can't be ambigious and can just be
512 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
513 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
515 if (strcmp(name
, s
->name
) == 0)
520 * Otherwise look for partial matches, returning early if it is found to
524 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
525 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
527 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
528 fnmatch(name
, s
->name
, 0) == 0) {
529 if (sfound
!= NULL
) {
540 * Lookup a window or return -1 if not found or ambigious. First try as an
541 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
542 * idx if the window index is a valid number but there is no window with that
546 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
548 struct winlink
*wl
, *wlfound
;
554 /* First see if this is a valid window index in this session. */
555 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
556 if (errstr
== NULL
) {
557 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
561 /* Look for exact matches, error if more than one. */
563 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
564 if (strcmp(name
, wl
->window
->name
) == 0) {
565 if (wlfound
!= NULL
) {
575 /* Now look for pattern matches, again error if multiple. */
577 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
578 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
579 fnmatch(name
, wl
->window
->name
, 0) == 0) {
580 if (wlfound
!= NULL
) {
594 * Find a window index - if the window doesn't exist, check if it is a
595 * potential index and return it anyway.
598 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
604 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
609 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
616 /* Find the target session or report an error and return NULL. */
618 cmd_find_session(struct cmd_ctx
*ctx
, const char *arg
)
626 /* A NULL argument means the current session. */
628 return (cmd_current_session(ctx
));
629 tmparg
= xstrdup(arg
);
631 /* Trim a single trailing colon if any. */
632 arglen
= strlen(tmparg
);
633 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
634 tmparg
[arglen
- 1] = '\0';
636 /* Find the session, if any. */
637 s
= cmd_lookup_session(tmparg
, &ambiguous
);
639 /* If it doesn't, try to match it as a client. */
640 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
643 /* If no session found, report an error. */
646 ctx
->error(ctx
, "more than one session: %s", tmparg
);
648 ctx
->error(ctx
, "session not found: %s", tmparg
);
655 /* Find the target session and window or report an error and return NULL. */
657 cmd_find_window(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
662 char *sessptr
= NULL
;
667 * Find the current session. There must always be a current session, if
668 * it can't be found, report an error.
670 if ((s
= cmd_current_session(ctx
)) == NULL
) {
671 ctx
->error(ctx
, "can't establish current session");
675 /* A NULL argument means the current session and window. */
682 /* Time to look at the argument. If it is empty, that is an error. */
686 /* Find the separating colon and split into window and session. */
687 winptr
= strchr(arg
, ':');
690 winptr
++; /* skip : */
691 sessptr
= xstrdup(arg
);
692 *strchr(sessptr
, ':') = '\0';
694 /* Try to lookup the session if present. */
695 if (*sessptr
!= '\0') {
696 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
703 * Then work out the window. An empty string is the current window,
704 * otherwise try special cases then to look it up in the session.
708 else if (winptr
[0] == '!' && winptr
[1] == '\0')
709 wl
= TAILQ_FIRST(&s
->lastw
);
710 else if (winptr
[0] == '+' || winptr
[0] == '-') {
711 if (winptr
[1] != '\0')
712 n
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
714 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
716 if (winptr
[0] == '+')
717 wl
= winlink_next_by_number(s
->curw
, n
);
719 wl
= winlink_previous_by_number(s
->curw
, n
);
720 /* Search by name before giving up. */
722 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
725 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
735 * No colon in the string, first try special cases, then as a window
736 * and lastly as a session.
738 if (arg
[0] == '!' && arg
[1] == '\0') {
739 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
741 } else if (arg
[0] == '+' || arg
[0] == '-') {
743 n
= strtonum(arg
+ 1, 1, INT_MAX
, NULL
);
745 wl
= cmd_lookup_window(s
, arg
, &ambiguous
);
748 wl
= winlink_next_by_number(s
->curw
, n
);
750 wl
= winlink_previous_by_number(s
->curw
, n
);
751 /* Search by name before giving up. */
753 wl
= cmd_lookup_window(s
, arg
, &ambiguous
);
757 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
768 if ((s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
778 ctx
->error(ctx
, "multiple sessions: %s", arg
);
780 ctx
->error(ctx
, "session not found: %s", arg
);
787 ctx
->error(ctx
, "multiple windows: %s", arg
);
789 ctx
->error(ctx
, "window not found: %s", arg
);
796 * Find the target session and window index, whether or not it exists in the
797 * session. Return -2 on error or -1 if no window index is specified. This is
798 * used when parsing an argument for a window target that may not exist (for
799 * example if it is going to be created).
802 cmd_find_index(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
807 char *sessptr
= NULL
;
808 int idx
, ambiguous
= 0;
812 * Find the current session. There must always be a current session, if
813 * it can't be found, report an error.
815 if ((s
= cmd_current_session(ctx
)) == NULL
) {
816 ctx
->error(ctx
, "can't establish current session");
820 /* A NULL argument means the current session and "no window" (-1). */
827 /* Time to look at the argument. If it is empty, that is an error. */
831 /* Find the separating colon. If none, assume the current session. */
832 winptr
= strchr(arg
, ':');
835 winptr
++; /* skip : */
836 sessptr
= xstrdup(arg
);
837 *strchr(sessptr
, ':') = '\0';
839 /* Try to lookup the session if present. */
840 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
841 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
848 * Then work out the window. An empty string is a new window otherwise
849 * try to look it up in the session.
853 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
854 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
857 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
858 if (winptr
[1] != '\0')
859 n
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
860 if (winptr
[0] == '+' && s
->curw
->idx
== INT_MAX
)
861 idx
= cmd_lookup_index(s
, winptr
, &ambiguous
);
862 else if (winptr
[0] == '-' && s
->curw
->idx
== 0)
863 idx
= cmd_lookup_index(s
, winptr
, &ambiguous
);
865 idx
= cmd_lookup_index(s
, winptr
, &ambiguous
);
866 else if (winptr
[0] == '+')
867 idx
= s
->curw
->idx
+ n
;
869 idx
= s
->curw
->idx
- n
;
872 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
881 * No colon in the string, first try special cases, then as a window
882 * and lastly as a session.
884 if (arg
[0] == '!' && arg
[1] == '\0') {
885 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
888 } else if (arg
[0] == '+' || arg
[0] == '-') {
890 n
= strtonum(arg
+ 1, 1, INT_MAX
, NULL
);
891 if (arg
[0] == '+' && s
->curw
->idx
== INT_MAX
)
892 idx
= cmd_lookup_index(s
, arg
, &ambiguous
);
893 else if (arg
[0] == '-' && s
->curw
->idx
== 0)
894 idx
= cmd_lookup_index(s
, arg
, &ambiguous
);
896 idx
= cmd_lookup_index(s
, arg
, &ambiguous
);
897 else if (arg
[0] == '+')
898 idx
= s
->curw
->idx
+ n
;
900 idx
= s
->curw
->idx
- n
;
903 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1)
914 if ((s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
924 ctx
->error(ctx
, "multiple sessions: %s", arg
);
926 ctx
->error(ctx
, "session not found: %s", arg
);
934 ctx
->error(ctx
, "invalid index: %s", arg
);
942 ctx
->error(ctx
, "multiple windows: %s", arg
);
944 ctx
->error(ctx
, "window not found: %s", arg
);
951 * Find the target session, window and pane number or report an error and
952 * return NULL. The pane number is separated from the session:window by a .,
953 * such as mysession:mywindow.0.
956 cmd_find_pane(struct cmd_ctx
*ctx
,
957 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
961 struct layout_cell
*lc
;
962 const char *period
, *errstr
;
963 char *winptr
, *paneptr
;
966 /* Get the current session. */
967 if ((s
= cmd_current_session(ctx
)) == NULL
) {
968 ctx
->error(ctx
, "can't establish current session");
974 /* A NULL argument means the current session, window and pane. */
976 *wpp
= s
->curw
->window
->active
;
980 /* Look for a separating period. */
981 if ((period
= strrchr(arg
, '.')) == NULL
)
984 /* Pull out the window part and parse it. */
985 winptr
= xstrdup(arg
);
986 winptr
[period
- arg
] = '\0';
989 else if ((wl
= cmd_find_window(ctx
, winptr
, sp
)) == NULL
)
992 /* Find the pane section and look it up. */
993 paneptr
= winptr
+ (period
- arg
) + 1;
994 if (*paneptr
== '\0')
995 *wpp
= wl
->window
->active
;
996 else if (paneptr
[0] == '+' || paneptr
[0] == '-') {
997 if (paneptr
[1] != '\0')
998 n
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
999 idx
= window_pane_index(wl
->window
, wl
->window
->active
);
1000 if (paneptr
[0] == '+' && idx
== INT_MAX
)
1001 *wpp
= TAILQ_FIRST(&wl
->window
->panes
);
1002 else if (paneptr
[0] == '-' && idx
== 0)
1003 *wpp
= TAILQ_LAST(&wl
->window
->panes
, window_panes
);
1005 *wpp
= wl
->window
->active
;
1006 else if (paneptr
[0] == '+')
1007 *wpp
= window_pane_at_index(wl
->window
, idx
+ n
);
1009 *wpp
= window_pane_at_index(wl
->window
, idx
- n
);
1010 if (paneptr
[0] == '+' && *wpp
== NULL
)
1011 *wpp
= TAILQ_FIRST(&wl
->window
->panes
);
1012 else if (paneptr
[0] == '-' && *wpp
== NULL
)
1013 *wpp
= TAILQ_LAST(&wl
->window
->panes
, window_panes
);
1014 else if (*wpp
== NULL
)
1017 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1020 *wpp
= window_pane_at_index(wl
->window
, idx
);
1029 /* Try pane string description. */
1030 if ((lc
= layout_find_string(wl
->window
, paneptr
)) == NULL
) {
1031 ctx
->error(ctx
, "can't find pane: %s", paneptr
);
1040 /* Try as a pane number alone. */
1041 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1045 /* Try index in the current session and window. */
1046 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1052 /* Try pane string description. */
1053 if ((lc
= layout_find_string(s
->curw
->window
, arg
)) != NULL
) {
1058 /* Try as a window and use the active pane. */
1059 if ((wl
= cmd_find_window(ctx
, arg
, sp
)) != NULL
)
1060 *wpp
= wl
->window
->active
;
1068 /* Replace the first %% or %idx in template by s. */
1070 cmd_template_replace(char *template, const char *s
, int idx
)
1077 if (strstr(template, "%") == NULL
)
1078 return (xstrdup(template));
1086 while (*ptr
!= '\0') {
1087 switch (ch
= *ptr
++) {
1089 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1090 if (*ptr
!= '%' || replaced
)
1097 buf
= xrealloc(buf
, 1, len
+ 1);
1098 strlcat(buf
, s
, len
+ 1);
1101 buf
= xrealloc(buf
, 1, len
+ 2);