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_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_list(struct sessionslist
*);
115 struct session
*cmd_choose_session(int);
116 struct client
*cmd_choose_client(struct clients
*);
117 struct client
*cmd_lookup_client(const char *);
118 struct session
*cmd_lookup_session(const char *, int *);
119 struct winlink
*cmd_lookup_window(struct session
*, const char *, int *);
120 int cmd_lookup_index(struct session
*, const char *, int *);
121 struct window_pane
*cmd_lookup_paneid(const char *);
122 struct session
*cmd_pane_session(struct cmd_ctx
*,
123 struct window_pane
*, struct winlink
**);
124 struct winlink
*cmd_find_window_offset(const char *, struct session
*, int *);
125 int cmd_find_index_offset(const char *, struct session
*, int *);
126 struct window_pane
*cmd_find_pane_offset(const char *, struct winlink
*);
129 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
135 for (i
= 0; i
< argc
; i
++) {
136 if (strlcpy(buf
, argv
[i
], len
) >= len
)
138 arglen
= strlen(argv
[i
]) + 1;
147 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
154 *argv
= xcalloc(argc
, sizeof **argv
);
157 for (i
= 0; i
< argc
; i
++) {
159 cmd_free_argv(argc
, *argv
);
163 arglen
= strlen(buf
) + 1;
164 (*argv
)[i
] = xstrdup(buf
);
173 cmd_copy_argv(int argc
, char *const *argv
)
180 new_argv
= xcalloc(argc
, sizeof *new_argv
);
181 for (i
= 0; i
< argc
; i
++) {
183 new_argv
[i
] = xstrdup(argv
[i
]);
189 cmd_free_argv(int argc
, char **argv
)
195 for (i
= 0; i
< argc
; i
++) {
203 cmd_parse(int argc
, char **argv
, char **cause
)
205 const struct cmd_entry
**entryp
, *entry
;
213 xasprintf(cause
, "no command");
218 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
219 if ((*entryp
)->alias
!= NULL
&&
220 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
226 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
232 /* Bail now if an exact match. */
233 if (strcmp(entry
->name
, argv
[0]) == 0)
239 xasprintf(cause
, "unknown command: %s", argv
[0]);
243 args
= args_parse(entry
->args_template
, argc
, argv
);
246 if (entry
->args_lower
!= -1 && args
->argc
< entry
->args_lower
)
248 if (entry
->args_upper
!= -1 && args
->argc
> entry
->args_upper
)
250 if (entry
->check
!= NULL
&& entry
->check(args
) != 0)
253 cmd
= xmalloc(sizeof *cmd
);
260 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
261 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
263 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
265 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
268 s
[strlen(s
) - 2] = '\0';
269 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
275 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
280 cmd_exec(struct cmd
*cmd
, struct cmd_ctx
*ctx
)
282 return (cmd
->entry
->exec(cmd
, ctx
));
286 cmd_free(struct cmd
*cmd
)
288 if (cmd
->args
!= NULL
)
289 args_free(cmd
->args
);
294 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
298 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
300 used
= args_print(cmd
->args
, buf
+ off
, len
- off
);
312 * Figure out the current session. Use: 1) the current session, if the command
313 * context has one; 2) the most recently used session containing the pty of the
314 * calling client, if any; 3) the session specified in the TMUX variable from
315 * the environment (as passed from the client); 4) the most recently used
316 * session from all sessions.
319 cmd_current_session(struct cmd_ctx
*ctx
, int prefer_unattached
)
321 struct msg_command_data
*data
= ctx
->msgdata
;
322 struct client
*c
= ctx
->cmdclient
;
324 struct sessionslist ss
;
326 struct window_pane
*wp
;
329 if (ctx
->curclient
!= NULL
&& ctx
->curclient
->session
!= NULL
)
330 return (ctx
->curclient
->session
);
333 * If the name of the calling client's pty is know, build a list of the
334 * sessions that contain it and if any choose either the first or the
337 if (c
!= NULL
&& c
->tty
.path
!= NULL
) {
339 RB_FOREACH(s
, sessions
, &sessions
) {
341 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
342 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
343 if (strcmp(wp
->tty
, c
->tty
.path
) == 0) {
355 s
= cmd_choose_session_list(&ss
);
361 /* Use the session from the TMUX environment variable. */
362 if (data
!= NULL
&& data
->pid
== getpid() && data
->idx
!= -1) {
363 s
= session_find_by_index(data
->idx
);
368 return (cmd_choose_session(prefer_unattached
));
372 * Find the most recently used session, preferring unattached if the flag is
376 cmd_choose_session(int prefer_unattached
)
378 struct session
*s
, *sbest
;
379 struct timeval
*tv
= NULL
;
382 RB_FOREACH(s
, sessions
, &sessions
) {
383 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >) ||
384 (prefer_unattached
&&
385 !(sbest
->flags
& SESSION_UNATTACHED
) &&
386 (s
->flags
& SESSION_UNATTACHED
))) {
388 tv
= &s
->activity_time
;
395 /* Find the most recently used session from a list. */
397 cmd_choose_session_list(struct sessionslist
*ss
)
399 struct session
*s
, *sbest
;
400 struct timeval
*tv
= NULL
;
404 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
405 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
408 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
410 tv
= &s
->activity_time
;
418 * Find the current client. First try the current client if set, then pick the
419 * most recently used of the clients attached to the current session if any,
420 * then of all clients.
423 cmd_current_client(struct cmd_ctx
*ctx
)
430 if (ctx
->curclient
!= NULL
)
431 return (ctx
->curclient
);
434 * No current client set. Find the current session and return the
435 * newest of its clients.
437 s
= cmd_current_session(ctx
, 0);
438 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
440 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
441 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
447 c
= cmd_choose_client(&cc
);
453 return (cmd_choose_client(&clients
));
456 /* Choose the most recently used client from a list. */
458 cmd_choose_client(struct clients
*cc
)
460 struct client
*c
, *cbest
;
461 struct timeval
*tv
= NULL
;
465 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
466 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
468 if (c
->session
== NULL
)
471 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
473 tv
= &c
->activity_time
;
480 /* Find the target client or report an error and return NULL. */
482 cmd_find_client(struct cmd_ctx
*ctx
, const char *arg
)
488 /* A NULL argument means the current client. */
490 return (cmd_current_client(ctx
));
491 tmparg
= xstrdup(arg
);
493 /* Trim a single trailing colon if any. */
494 arglen
= strlen(tmparg
);
495 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
496 tmparg
[arglen
- 1] = '\0';
498 /* Find the client, if any. */
499 c
= cmd_lookup_client(tmparg
);
501 /* If no client found, report an error. */
503 ctx
->error(ctx
, "client not found: %s", tmparg
);
510 * Lookup a client by device path. Either of a full match and a match without a
511 * leading _PATH_DEV ("/dev/") is accepted.
514 cmd_lookup_client(const char *name
)
520 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
521 c
= ARRAY_ITEM(&clients
, i
);
522 if (c
== NULL
|| c
->session
== NULL
)
526 /* Check for exact matches. */
527 if (strcmp(name
, path
) == 0)
530 /* Check without leading /dev if present. */
531 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
533 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
540 /* Lookup a session by name. If no session is found, NULL is returned. */
542 cmd_lookup_session(const char *name
, int *ambiguous
)
544 struct session
*s
, *sfound
;
549 * Look for matches. First look for exact matches - session names must
550 * be unique so an exact match can't be ambigious and can just be
553 if ((s
= session_find(name
)) != NULL
)
557 * Otherwise look for partial matches, returning early if it is found to
561 RB_FOREACH(s
, sessions
, &sessions
) {
562 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
563 fnmatch(name
, s
->name
, 0) == 0) {
564 if (sfound
!= NULL
) {
575 * Lookup a window or return -1 if not found or ambigious. First try as an
576 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
577 * idx if the window index is a valid number but there is no window with that
581 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
583 struct winlink
*wl
, *wlfound
;
589 /* First see if this is a valid window index in this session. */
590 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
591 if (errstr
== NULL
) {
592 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
596 /* Look for exact matches, error if more than one. */
598 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
599 if (strcmp(name
, wl
->window
->name
) == 0) {
600 if (wlfound
!= NULL
) {
610 /* Now look for pattern matches, again error if multiple. */
612 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
613 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
614 fnmatch(name
, wl
->window
->name
, 0) == 0) {
615 if (wlfound
!= NULL
) {
629 * Find a window index - if the window doesn't exist, check if it is a
630 * potential index and return it anyway.
633 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
639 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
644 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
652 * Lookup pane id. An initial % means a pane id. sp must already point to the
656 cmd_lookup_paneid(const char *arg
)
664 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
667 return (window_pane_find_by_id(paneid
));
670 /* Find session and winlink for pane. */
672 cmd_pane_session(struct cmd_ctx
*ctx
, struct window_pane
*wp
,
673 struct winlink
**wlp
)
676 struct sessionslist ss
;
679 /* If this pane is in the current session, return that winlink. */
680 s
= cmd_current_session(ctx
, 0);
682 wl
= winlink_find_by_window(&s
->windows
, wp
->window
);
690 /* Otherwise choose from all sessions with this pane. */
692 RB_FOREACH(s
, sessions
, &sessions
) {
693 if (winlink_find_by_window(&s
->windows
, wp
->window
) != NULL
)
696 s
= cmd_choose_session_list(&ss
);
699 *wlp
= winlink_find_by_window(&s
->windows
, wp
->window
);
703 /* Find the target session or report an error and return NULL. */
705 cmd_find_session(struct cmd_ctx
*ctx
, const char *arg
, int prefer_unattached
)
708 struct window_pane
*wp
;
714 /* A NULL argument means the current session. */
716 return (cmd_current_session(ctx
, prefer_unattached
));
717 tmparg
= xstrdup(arg
);
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 arglen
= strlen(tmparg
);
725 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
726 tmparg
[arglen
- 1] = '\0';
728 /* An empty session name is the current session. */
729 if (*tmparg
== '\0') {
731 return (cmd_current_session(ctx
, prefer_unattached
));
734 /* Find the session, if any. */
735 s
= cmd_lookup_session(tmparg
, &ambiguous
);
737 /* If it doesn't, try to match it as a client. */
738 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
741 /* If no session found, report an error. */
744 ctx
->error(ctx
, "more than one session: %s", tmparg
);
746 ctx
->error(ctx
, "session not found: %s", tmparg
);
753 /* Find the target session and window or report an error and return NULL. */
755 cmd_find_window(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
759 struct window_pane
*wp
;
761 char *sessptr
= NULL
;
765 * Find the current session. There must always be a current session, if
766 * it can't be found, report an error.
768 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
769 ctx
->error(ctx
, "can't establish current session");
773 /* A NULL argument means the current session and window. */
780 /* Lookup as pane id. */
781 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
782 s
= cmd_pane_session(ctx
, wp
, &wl
);
788 /* Time to look at the argument. If it is empty, that is an error. */
792 /* Find the separating colon and split into window and session. */
793 winptr
= strchr(arg
, ':');
796 winptr
++; /* skip : */
797 sessptr
= xstrdup(arg
);
798 *strchr(sessptr
, ':') = '\0';
800 /* Try to lookup the session if present. */
801 if (*sessptr
!= '\0') {
802 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
809 * Then work out the window. An empty string is the current window,
810 * otherwise try special cases then to look it up in the session.
814 else if (winptr
[0] == '!' && winptr
[1] == '\0')
815 wl
= TAILQ_FIRST(&s
->lastw
);
816 else if (winptr
[0] == '+' || winptr
[0] == '-')
817 wl
= cmd_find_window_offset(winptr
, s
, &ambiguous
);
819 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
829 * No colon in the string, first try special cases, then as a window
830 * and lastly as a session.
832 if (arg
[0] == '!' && arg
[1] == '\0') {
833 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
835 } else if (arg
[0] == '+' || arg
[0] == '-') {
836 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
838 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
849 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
859 ctx
->error(ctx
, "multiple sessions: %s", arg
);
861 ctx
->error(ctx
, "session not found: %s", arg
);
868 ctx
->error(ctx
, "multiple windows: %s", arg
);
870 ctx
->error(ctx
, "window not found: %s", arg
);
877 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
882 if (winptr
[1] != '\0')
883 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
885 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
887 if (winptr
[0] == '+')
888 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
890 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
897 * Find the target session and window index, whether or not it exists in the
898 * session. Return -2 on error or -1 if no window index is specified. This is
899 * used when parsing an argument for a window target that may not exist (for
900 * example if it is going to be created).
903 cmd_find_index(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
908 char *sessptr
= NULL
;
909 int idx
, ambiguous
= 0;
912 * Find the current session. There must always be a current session, if
913 * it can't be found, report an error.
915 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
916 ctx
->error(ctx
, "can't establish current session");
920 /* A NULL argument means the current session and "no window" (-1). */
927 /* Time to look at the argument. If it is empty, that is an error. */
931 /* Find the separating colon. If none, assume the current session. */
932 winptr
= strchr(arg
, ':');
935 winptr
++; /* skip : */
936 sessptr
= xstrdup(arg
);
937 *strchr(sessptr
, ':') = '\0';
939 /* Try to lookup the session if present. */
940 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
941 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
948 * Then work out the window. An empty string is a new window otherwise
949 * try to look it up in the session.
953 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
954 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
957 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
958 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
960 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
969 * No colon in the string, first try special cases, then as a window
970 * and lastly as a session.
972 if (arg
[0] == '!' && arg
[1] == '\0') {
973 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
976 } else if (arg
[0] == '+' || arg
[0] == '-') {
977 if ((idx
= cmd_find_index_offset(arg
, s
, &ambiguous
)) < 0)
979 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1)
990 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1000 ctx
->error(ctx
, "multiple sessions: %s", arg
);
1002 ctx
->error(ctx
, "session not found: %s", arg
);
1003 if (sessptr
!= NULL
)
1010 ctx
->error(ctx
, "invalid index: %s", arg
);
1012 if (sessptr
!= NULL
)
1018 ctx
->error(ctx
, "multiple windows: %s", arg
);
1020 ctx
->error(ctx
, "window not found: %s", arg
);
1021 if (sessptr
!= NULL
)
1027 cmd_find_index_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
1029 int idx
, offset
= 1;
1031 if (winptr
[1] != '\0')
1032 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
1034 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1036 if (winptr
[0] == '+') {
1037 if (s
->curw
->idx
== INT_MAX
)
1038 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1040 idx
= s
->curw
->idx
+ offset
;
1042 if (s
->curw
->idx
== 0)
1043 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1045 idx
= s
->curw
->idx
- offset
;
1053 * Find the target session, window and pane number or report an error and
1054 * return NULL. The pane number is separated from the session:window by a .,
1055 * such as mysession:mywindow.0.
1058 cmd_find_pane(struct cmd_ctx
*ctx
,
1059 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1063 struct layout_cell
*lc
;
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 ((lc
= layout_find_string(wl
->window
, paneptr
)) == NULL
) {
1123 ctx
->error(ctx
, "can't find pane: %s", paneptr
);
1132 /* Try as a pane number alone. */
1133 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1137 /* Try index in the current session and window. */
1138 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1144 /* Try pane string description. */
1145 if ((lc
= layout_find_string(s
->curw
->window
, arg
)) != NULL
) {
1150 /* Try as a window and use the active pane. */
1151 if ((wl
= cmd_find_window(ctx
, arg
, sp
)) != NULL
)
1152 *wpp
= wl
->window
->active
;
1160 struct window_pane
*
1161 cmd_find_pane_offset(const char *paneptr
, struct winlink
*wl
)
1163 struct window
*w
= wl
->window
;
1164 struct window_pane
*wp
= w
->active
;
1167 if (paneptr
[1] != '\0')
1168 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1170 if (paneptr
[0] == '+')
1171 wp
= window_pane_next_by_number(w
, wp
, offset
);
1173 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1179 /* Replace the first %% or %idx in template by s. */
1181 cmd_template_replace(char *template, const char *s
, int idx
)
1188 if (strstr(template, "%") == NULL
)
1189 return (xstrdup(template));
1197 while (*ptr
!= '\0') {
1198 switch (ch
= *ptr
++) {
1200 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1201 if (*ptr
!= '%' || replaced
)
1208 buf
= xrealloc(buf
, 1, len
+ 1);
1209 strlcat(buf
, s
, len
+ 1);
1212 buf
= xrealloc(buf
, 1, len
+ 2);