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
,
44 &cmd_delete_buffer_entry
,
45 &cmd_detach_client_entry
,
46 &cmd_display_message_entry
,
47 &cmd_display_panes_entry
,
48 &cmd_find_window_entry
,
49 &cmd_has_session_entry
,
53 &cmd_kill_server_entry
,
54 &cmd_kill_session_entry
,
55 &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_pane_entry
,
84 &cmd_respawn_window_entry
,
85 &cmd_rotate_window_entry
,
87 &cmd_save_buffer_entry
,
88 &cmd_select_layout_entry
,
89 &cmd_select_pane_entry
,
90 &cmd_select_window_entry
,
92 &cmd_send_prefix_entry
,
93 &cmd_server_info_entry
,
94 &cmd_set_buffer_entry
,
95 &cmd_set_environment_entry
,
96 &cmd_set_option_entry
,
97 &cmd_set_window_option_entry
,
98 &cmd_show_buffer_entry
,
99 &cmd_show_environment_entry
,
100 &cmd_show_messages_entry
,
101 &cmd_show_options_entry
,
102 &cmd_show_window_options_entry
,
103 &cmd_source_file_entry
,
104 &cmd_split_window_entry
,
105 &cmd_start_server_entry
,
106 &cmd_suspend_client_entry
,
107 &cmd_swap_pane_entry
,
108 &cmd_swap_window_entry
,
109 &cmd_switch_client_entry
,
110 &cmd_unbind_key_entry
,
111 &cmd_unlink_window_entry
,
115 struct session
*cmd_choose_session_list(struct sessionslist
*);
116 struct session
*cmd_choose_session(int);
117 struct client
*cmd_choose_client(struct clients
*);
118 struct client
*cmd_lookup_client(const char *);
119 struct session
*cmd_lookup_session(const char *, int *);
120 struct winlink
*cmd_lookup_window(struct session
*, const char *, int *);
121 int cmd_lookup_index(struct session
*, const char *, int *);
122 struct window_pane
*cmd_lookup_paneid(const char *);
123 struct session
*cmd_pane_session(struct cmd_ctx
*,
124 struct window_pane
*, struct winlink
**);
125 struct winlink
*cmd_find_window_offset(const char *, struct session
*, int *);
126 int cmd_find_index_offset(const char *, struct session
*, int *);
127 struct window_pane
*cmd_find_pane_offset(const char *, struct winlink
*);
130 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
136 for (i
= 0; i
< argc
; i
++) {
137 if (strlcpy(buf
, argv
[i
], len
) >= len
)
139 arglen
= strlen(argv
[i
]) + 1;
148 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
155 *argv
= xcalloc(argc
, sizeof **argv
);
158 for (i
= 0; i
< argc
; i
++) {
160 cmd_free_argv(argc
, *argv
);
164 arglen
= strlen(buf
) + 1;
165 (*argv
)[i
] = xstrdup(buf
);
174 cmd_copy_argv(int argc
, char *const *argv
)
181 new_argv
= xcalloc(argc
, sizeof *new_argv
);
182 for (i
= 0; i
< argc
; i
++) {
184 new_argv
[i
] = xstrdup(argv
[i
]);
190 cmd_free_argv(int argc
, char **argv
)
196 for (i
= 0; i
< argc
; i
++) {
204 cmd_parse(int argc
, char **argv
, char **cause
)
206 const struct cmd_entry
**entryp
, *entry
;
214 xasprintf(cause
, "no command");
219 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
220 if ((*entryp
)->alias
!= NULL
&&
221 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
227 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
233 /* Bail now if an exact match. */
234 if (strcmp(entry
->name
, argv
[0]) == 0)
240 xasprintf(cause
, "unknown command: %s", argv
[0]);
244 args
= args_parse(entry
->args_template
, argc
, argv
);
247 if (entry
->args_lower
!= -1 && args
->argc
< entry
->args_lower
)
249 if (entry
->args_upper
!= -1 && args
->argc
> entry
->args_upper
)
251 if (entry
->check
!= NULL
&& entry
->check(args
) != 0)
254 cmd
= xmalloc(sizeof *cmd
);
261 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
262 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
264 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
266 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
269 s
[strlen(s
) - 2] = '\0';
270 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
276 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
281 cmd_exec(struct cmd
*cmd
, struct cmd_ctx
*ctx
)
283 return (cmd
->entry
->exec(cmd
, ctx
));
287 cmd_free(struct cmd
*cmd
)
289 if (cmd
->args
!= NULL
)
290 args_free(cmd
->args
);
295 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
299 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
301 used
= args_print(cmd
->args
, buf
+ off
, len
- off
);
313 * Figure out the current session. Use: 1) the current session, if the command
314 * context has one; 2) the most recently used session containing the pty of the
315 * calling client, if any; 3) the session specified in the TMUX variable from
316 * the environment (as passed from the client); 4) the most recently used
317 * session from all sessions.
320 cmd_current_session(struct cmd_ctx
*ctx
, int prefer_unattached
)
322 struct msg_command_data
*data
= ctx
->msgdata
;
323 struct client
*c
= ctx
->cmdclient
;
325 struct sessionslist ss
;
327 struct window_pane
*wp
;
330 if (ctx
->curclient
!= NULL
&& ctx
->curclient
->session
!= NULL
)
331 return (ctx
->curclient
->session
);
334 * If the name of the calling client's pty is know, build a list of the
335 * sessions that contain it and if any choose either the first or the
338 if (c
!= NULL
&& c
->tty
.path
!= NULL
) {
340 RB_FOREACH(s
, sessions
, &sessions
) {
342 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
343 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
344 if (strcmp(wp
->tty
, c
->tty
.path
) == 0) {
356 s
= cmd_choose_session_list(&ss
);
362 /* Use the session from the TMUX environment variable. */
363 if (data
!= NULL
&& data
->pid
== getpid() && data
->idx
!= -1) {
364 s
= session_find_by_index(data
->idx
);
369 return (cmd_choose_session(prefer_unattached
));
373 * Find the most recently used session, preferring unattached if the flag is
377 cmd_choose_session(int prefer_unattached
)
379 struct session
*s
, *sbest
;
380 struct timeval
*tv
= NULL
;
383 RB_FOREACH(s
, sessions
, &sessions
) {
384 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >) ||
385 (prefer_unattached
&&
386 !(sbest
->flags
& SESSION_UNATTACHED
) &&
387 (s
->flags
& SESSION_UNATTACHED
))) {
389 tv
= &s
->activity_time
;
396 /* Find the most recently used session from a list. */
398 cmd_choose_session_list(struct sessionslist
*ss
)
400 struct session
*s
, *sbest
;
401 struct timeval
*tv
= NULL
;
405 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
406 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
409 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
411 tv
= &s
->activity_time
;
419 * Find the current client. First try the current client if set, then pick the
420 * most recently used of the clients attached to the current session if any,
421 * then of all clients.
424 cmd_current_client(struct cmd_ctx
*ctx
)
431 if (ctx
->curclient
!= NULL
)
432 return (ctx
->curclient
);
435 * No current client set. Find the current session and return the
436 * newest of its clients.
438 s
= cmd_current_session(ctx
, 0);
439 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
441 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
442 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
448 c
= cmd_choose_client(&cc
);
454 return (cmd_choose_client(&clients
));
457 /* Choose the most recently used client from a list. */
459 cmd_choose_client(struct clients
*cc
)
461 struct client
*c
, *cbest
;
462 struct timeval
*tv
= NULL
;
466 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
467 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
469 if (c
->session
== NULL
)
472 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
474 tv
= &c
->activity_time
;
481 /* Find the target client or report an error and return NULL. */
483 cmd_find_client(struct cmd_ctx
*ctx
, const char *arg
)
489 /* A NULL argument means the current client. */
491 return (cmd_current_client(ctx
));
492 tmparg
= xstrdup(arg
);
494 /* Trim a single trailing colon if any. */
495 arglen
= strlen(tmparg
);
496 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
497 tmparg
[arglen
- 1] = '\0';
499 /* Find the client, if any. */
500 c
= cmd_lookup_client(tmparg
);
502 /* If no client found, report an error. */
504 ctx
->error(ctx
, "client not found: %s", tmparg
);
511 * Lookup a client by device path. Either of a full match and a match without a
512 * leading _PATH_DEV ("/dev/") is accepted.
515 cmd_lookup_client(const char *name
)
521 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
522 c
= ARRAY_ITEM(&clients
, i
);
523 if (c
== NULL
|| c
->session
== NULL
)
527 /* Check for exact matches. */
528 if (strcmp(name
, path
) == 0)
531 /* Check without leading /dev if present. */
532 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
534 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
541 /* Lookup a session by name. If no session is found, NULL is returned. */
543 cmd_lookup_session(const char *name
, int *ambiguous
)
545 struct session
*s
, *sfound
;
550 * Look for matches. First look for exact matches - session names must
551 * be unique so an exact match can't be ambigious and can just be
554 if ((s
= session_find(name
)) != NULL
)
558 * Otherwise look for partial matches, returning early if it is found to
562 RB_FOREACH(s
, sessions
, &sessions
) {
563 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
564 fnmatch(name
, s
->name
, 0) == 0) {
565 if (sfound
!= NULL
) {
576 * Lookup a window or return -1 if not found or ambigious. First try as an
577 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
578 * idx if the window index is a valid number but there is no window with that
582 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
584 struct winlink
*wl
, *wlfound
;
590 /* First see if this is a valid window index in this session. */
591 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
592 if (errstr
== NULL
) {
593 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
597 /* Look for exact matches, error if more than one. */
599 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
600 if (strcmp(name
, wl
->window
->name
) == 0) {
601 if (wlfound
!= NULL
) {
611 /* Now look for pattern matches, again error if multiple. */
613 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
614 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
615 fnmatch(name
, wl
->window
->name
, 0) == 0) {
616 if (wlfound
!= NULL
) {
630 * Find a window index - if the window doesn't exist, check if it is a
631 * potential index and return it anyway.
634 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
640 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
645 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
653 * Lookup pane id. An initial % means a pane id. sp must already point to the
657 cmd_lookup_paneid(const char *arg
)
665 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
668 return (window_pane_find_by_id(paneid
));
671 /* Find session and winlink for pane. */
673 cmd_pane_session(struct cmd_ctx
*ctx
, struct window_pane
*wp
,
674 struct winlink
**wlp
)
677 struct sessionslist ss
;
680 /* If this pane is in the current session, return that winlink. */
681 s
= cmd_current_session(ctx
, 0);
683 wl
= winlink_find_by_window(&s
->windows
, wp
->window
);
691 /* Otherwise choose from all sessions with this pane. */
693 RB_FOREACH(s
, sessions
, &sessions
) {
694 if (winlink_find_by_window(&s
->windows
, wp
->window
) != NULL
)
697 s
= cmd_choose_session_list(&ss
);
700 *wlp
= winlink_find_by_window(&s
->windows
, wp
->window
);
704 /* Find the target session or report an error and return NULL. */
706 cmd_find_session(struct cmd_ctx
*ctx
, const char *arg
, int prefer_unattached
)
709 struct window_pane
*wp
;
715 /* A NULL argument means the current session. */
717 return (cmd_current_session(ctx
, prefer_unattached
));
719 /* Lookup as pane id. */
720 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
)
721 return (cmd_pane_session(ctx
, wp
, NULL
));
723 /* Trim a single trailing colon if any. */
724 tmparg
= xstrdup(arg
);
725 arglen
= strlen(tmparg
);
726 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
727 tmparg
[arglen
- 1] = '\0';
729 /* An empty session name is the current session. */
730 if (*tmparg
== '\0') {
732 return (cmd_current_session(ctx
, prefer_unattached
));
735 /* Find the session, if any. */
736 s
= cmd_lookup_session(tmparg
, &ambiguous
);
738 /* If it doesn't, try to match it as a client. */
739 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
742 /* If no session found, report an error. */
745 ctx
->error(ctx
, "more than one session: %s", tmparg
);
747 ctx
->error(ctx
, "session not found: %s", tmparg
);
754 /* Find the target session and window or report an error and return NULL. */
756 cmd_find_window(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
760 struct window_pane
*wp
;
762 char *sessptr
= NULL
;
766 * Find the current session. There must always be a current session, if
767 * it can't be found, report an error.
769 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
770 ctx
->error(ctx
, "can't establish current session");
774 /* A NULL argument means the current session and window. */
781 /* Lookup as pane id. */
782 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
783 s
= cmd_pane_session(ctx
, wp
, &wl
);
789 /* Time to look at the argument. If it is empty, that is an error. */
793 /* Find the separating colon and split into window and session. */
794 winptr
= strchr(arg
, ':');
797 winptr
++; /* skip : */
798 sessptr
= xstrdup(arg
);
799 *strchr(sessptr
, ':') = '\0';
801 /* Try to lookup the session if present. */
802 if (*sessptr
!= '\0') {
803 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
810 * Then work out the window. An empty string is the current window,
811 * otherwise try special cases then to look it up in the session.
815 else if (winptr
[0] == '!' && winptr
[1] == '\0')
816 wl
= TAILQ_FIRST(&s
->lastw
);
817 else if (winptr
[0] == '+' || winptr
[0] == '-')
818 wl
= cmd_find_window_offset(winptr
, s
, &ambiguous
);
820 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
830 * No colon in the string, first try special cases, then as a window
831 * and lastly as a session.
833 if (arg
[0] == '!' && arg
[1] == '\0') {
834 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
836 } else if (arg
[0] == '+' || arg
[0] == '-') {
837 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
839 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
850 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
860 ctx
->error(ctx
, "multiple sessions: %s", arg
);
862 ctx
->error(ctx
, "session not found: %s", arg
);
869 ctx
->error(ctx
, "multiple windows: %s", arg
);
871 ctx
->error(ctx
, "window not found: %s", arg
);
878 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
883 if (winptr
[1] != '\0')
884 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
886 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
888 if (winptr
[0] == '+')
889 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
891 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
898 * Find the target session and window index, whether or not it exists in the
899 * session. Return -2 on error or -1 if no window index is specified. This is
900 * used when parsing an argument for a window target that may not exist (for
901 * example if it is going to be created).
904 cmd_find_index(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
909 char *sessptr
= NULL
;
910 int idx
, ambiguous
= 0;
913 * Find the current session. There must always be a current session, if
914 * it can't be found, report an error.
916 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
917 ctx
->error(ctx
, "can't establish current session");
921 /* A NULL argument means the current session and "no window" (-1). */
928 /* Time to look at the argument. If it is empty, that is an error. */
932 /* Find the separating colon. If none, assume the current session. */
933 winptr
= strchr(arg
, ':');
936 winptr
++; /* skip : */
937 sessptr
= xstrdup(arg
);
938 *strchr(sessptr
, ':') = '\0';
940 /* Try to lookup the session if present. */
941 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
942 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
949 * Then work out the window. An empty string is a new window otherwise
950 * try to look it up in the session.
954 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
955 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
958 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
959 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
961 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
970 * No colon in the string, first try special cases, then as a window
971 * and lastly as a session.
973 if (arg
[0] == '!' && arg
[1] == '\0') {
974 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
977 } else if (arg
[0] == '+' || arg
[0] == '-') {
978 if ((idx
= cmd_find_index_offset(arg
, s
, &ambiguous
)) < 0)
980 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1)
991 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1001 ctx
->error(ctx
, "multiple sessions: %s", arg
);
1003 ctx
->error(ctx
, "session not found: %s", arg
);
1004 if (sessptr
!= NULL
)
1011 ctx
->error(ctx
, "invalid index: %s", arg
);
1013 if (sessptr
!= NULL
)
1019 ctx
->error(ctx
, "multiple windows: %s", arg
);
1021 ctx
->error(ctx
, "window not found: %s", arg
);
1022 if (sessptr
!= NULL
)
1028 cmd_find_index_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
1030 int idx
, offset
= 1;
1032 if (winptr
[1] != '\0')
1033 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
1035 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1037 if (winptr
[0] == '+') {
1038 if (s
->curw
->idx
== INT_MAX
)
1039 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1041 idx
= s
->curw
->idx
+ offset
;
1043 if (s
->curw
->idx
== 0)
1044 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1046 idx
= s
->curw
->idx
- offset
;
1054 * Find the target session, window and pane number or report an error and
1055 * return NULL. The pane number is separated from the session:window by a .,
1056 * such as mysession:mywindow.0.
1059 cmd_find_pane(struct cmd_ctx
*ctx
,
1060 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1064 const char *period
, *errstr
;
1065 char *winptr
, *paneptr
;
1068 /* Get the current session. */
1069 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
1070 ctx
->error(ctx
, "can't establish current session");
1076 /* A NULL argument means the current session, window and pane. */
1078 *wpp
= s
->curw
->window
->active
;
1082 /* Lookup as pane id. */
1083 if ((*wpp
= cmd_lookup_paneid(arg
)) != NULL
) {
1084 s
= cmd_pane_session(ctx
, *wpp
, &wl
);
1090 /* Look for a separating period. */
1091 if ((period
= strrchr(arg
, '.')) == NULL
)
1094 /* Pull out the window part and parse it. */
1095 winptr
= xstrdup(arg
);
1096 winptr
[period
- arg
] = '\0';
1097 if (*winptr
== '\0')
1099 else if ((wl
= cmd_find_window(ctx
, winptr
, sp
)) == NULL
)
1102 /* Find the pane section and look it up. */
1103 paneptr
= winptr
+ (period
- arg
) + 1;
1104 if (*paneptr
== '\0')
1105 *wpp
= wl
->window
->active
;
1106 else if (paneptr
[0] == '+' || paneptr
[0] == '-')
1107 *wpp
= cmd_find_pane_offset(paneptr
, wl
);
1109 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1112 *wpp
= window_pane_at_index(wl
->window
, idx
);
1121 /* Try pane string description. */
1122 if ((*wpp
= window_find_string(wl
->window
, paneptr
)) == NULL
) {
1123 ctx
->error(ctx
, "can't find pane: %s", paneptr
);
1131 /* Try as a pane number alone. */
1132 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1136 /* Try index in the current session and window. */
1137 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1143 /* Try pane string description. */
1144 if ((*wpp
= window_find_string(s
->curw
->window
, arg
)) != NULL
)
1147 /* Try as a window and use the active pane. */
1148 if ((wl
= cmd_find_window(ctx
, arg
, sp
)) != NULL
)
1149 *wpp
= wl
->window
->active
;
1157 struct window_pane
*
1158 cmd_find_pane_offset(const char *paneptr
, struct winlink
*wl
)
1160 struct window
*w
= wl
->window
;
1161 struct window_pane
*wp
= w
->active
;
1164 if (paneptr
[1] != '\0')
1165 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1167 if (paneptr
[0] == '+')
1168 wp
= window_pane_next_by_number(w
, wp
, offset
);
1170 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1176 /* Replace the first %% or %idx in template by s. */
1178 cmd_template_replace(char *template, const char *s
, int idx
)
1185 if (strstr(template, "%") == NULL
)
1186 return (xstrdup(template));
1194 while (*ptr
!= '\0') {
1195 switch (ch
= *ptr
++) {
1197 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1198 if (*ptr
!= '%' || replaced
)
1205 buf
= xrealloc(buf
, 1, len
+ 1);
1206 strlcat(buf
, s
, len
+ 1);
1209 buf
= xrealloc(buf
, 1, len
+ 2);