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 winlink
*cmd_lookup_winlink_windowid(struct session
*, const char *);
124 struct window
*cmd_lookup_windowid(const char *);
125 struct session
*cmd_window_session(struct cmd_ctx
*,
126 struct window
*, struct winlink
**);
127 struct winlink
*cmd_find_window_offset(const char *, struct session
*, int *);
128 int cmd_find_index_offset(const char *, struct session
*, int *);
129 struct window_pane
*cmd_find_pane_offset(const char *, struct winlink
*);
132 cmd_pack_argv(int argc
, char **argv
, char *buf
, size_t len
)
138 for (i
= 0; i
< argc
; i
++) {
139 if (strlcpy(buf
, argv
[i
], len
) >= len
)
141 arglen
= strlen(argv
[i
]) + 1;
150 cmd_unpack_argv(char *buf
, size_t len
, int argc
, char ***argv
)
157 *argv
= xcalloc(argc
, sizeof **argv
);
160 for (i
= 0; i
< argc
; i
++) {
162 cmd_free_argv(argc
, *argv
);
166 arglen
= strlen(buf
) + 1;
167 (*argv
)[i
] = xstrdup(buf
);
176 cmd_copy_argv(int argc
, char *const *argv
)
183 new_argv
= xcalloc(argc
, sizeof *new_argv
);
184 for (i
= 0; i
< argc
; i
++) {
186 new_argv
[i
] = xstrdup(argv
[i
]);
192 cmd_free_argv(int argc
, char **argv
)
198 for (i
= 0; i
< argc
; i
++) {
206 cmd_parse(int argc
, char **argv
, char **cause
)
208 const struct cmd_entry
**entryp
, *entry
;
216 xasprintf(cause
, "no command");
221 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
222 if ((*entryp
)->alias
!= NULL
&&
223 strcmp((*entryp
)->alias
, argv
[0]) == 0) {
229 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
235 /* Bail now if an exact match. */
236 if (strcmp(entry
->name
, argv
[0]) == 0)
242 xasprintf(cause
, "unknown command: %s", argv
[0]);
246 args
= args_parse(entry
->args_template
, argc
, argv
);
249 if (entry
->args_lower
!= -1 && args
->argc
< entry
->args_lower
)
251 if (entry
->args_upper
!= -1 && args
->argc
> entry
->args_upper
)
253 if (entry
->check
!= NULL
&& entry
->check(args
) != 0)
256 cmd
= xmalloc(sizeof *cmd
);
263 for (entryp
= cmd_table
; *entryp
!= NULL
; entryp
++) {
264 if (strncmp((*entryp
)->name
, argv
[0], strlen(argv
[0])) != 0)
266 if (strlcat(s
, (*entryp
)->name
, sizeof s
) >= sizeof s
)
268 if (strlcat(s
, ", ", sizeof s
) >= sizeof s
)
271 s
[strlen(s
) - 2] = '\0';
272 xasprintf(cause
, "ambiguous command: %s, could be: %s", argv
[0], s
);
278 xasprintf(cause
, "usage: %s %s", entry
->name
, entry
->usage
);
283 cmd_exec(struct cmd
*cmd
, struct cmd_ctx
*ctx
)
285 return (cmd
->entry
->exec(cmd
, ctx
));
289 cmd_free(struct cmd
*cmd
)
291 if (cmd
->args
!= NULL
)
292 args_free(cmd
->args
);
297 cmd_print(struct cmd
*cmd
, char *buf
, size_t len
)
301 off
= xsnprintf(buf
, len
, "%s ", cmd
->entry
->name
);
303 used
= args_print(cmd
->args
, buf
+ off
, len
- off
);
315 * Figure out the current session. Use: 1) the current session, if the command
316 * context has one; 2) the most recently used session containing the pty of the
317 * calling client, if any; 3) the session specified in the TMUX variable from
318 * the environment (as passed from the client); 4) the most recently used
319 * session from all sessions.
322 cmd_current_session(struct cmd_ctx
*ctx
, int prefer_unattached
)
324 struct msg_command_data
*data
= ctx
->msgdata
;
325 struct client
*c
= ctx
->cmdclient
;
327 struct sessionslist ss
;
329 struct window_pane
*wp
;
332 if (ctx
->curclient
!= NULL
&& ctx
->curclient
->session
!= NULL
)
333 return (ctx
->curclient
->session
);
336 * If the name of the calling client's pty is know, build a list of the
337 * sessions that contain it and if any choose either the first or the
340 if (c
!= NULL
&& c
->tty
.path
!= NULL
) {
342 RB_FOREACH(s
, sessions
, &sessions
) {
344 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
345 TAILQ_FOREACH(wp
, &wl
->window
->panes
, entry
) {
346 if (strcmp(wp
->tty
, c
->tty
.path
) == 0) {
358 s
= cmd_choose_session_list(&ss
);
364 /* Use the session from the TMUX environment variable. */
365 if (data
!= NULL
&& data
->pid
== getpid() && data
->idx
!= -1) {
366 s
= session_find_by_index(data
->idx
);
371 return (cmd_choose_session(prefer_unattached
));
375 * Find the most recently used session, preferring unattached if the flag is
379 cmd_choose_session(int prefer_unattached
)
381 struct session
*s
, *sbest
;
382 struct timeval
*tv
= NULL
;
385 RB_FOREACH(s
, sessions
, &sessions
) {
386 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >) ||
387 (prefer_unattached
&&
388 !(sbest
->flags
& SESSION_UNATTACHED
) &&
389 (s
->flags
& SESSION_UNATTACHED
))) {
391 tv
= &s
->activity_time
;
398 /* Find the most recently used session from a list. */
400 cmd_choose_session_list(struct sessionslist
*ss
)
402 struct session
*s
, *sbest
;
403 struct timeval
*tv
= NULL
;
407 for (i
= 0; i
< ARRAY_LENGTH(ss
); i
++) {
408 if ((s
= ARRAY_ITEM(ss
, i
)) == NULL
)
411 if (tv
== NULL
|| timercmp(&s
->activity_time
, tv
, >)) {
413 tv
= &s
->activity_time
;
421 * Find the current client. First try the current client if set, then pick the
422 * most recently used of the clients attached to the current session if any,
423 * then of all clients.
426 cmd_current_client(struct cmd_ctx
*ctx
)
433 if (ctx
->curclient
!= NULL
)
434 return (ctx
->curclient
);
437 * No current client set. Find the current session and return the
438 * newest of its clients.
440 s
= cmd_current_session(ctx
, 0);
441 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
443 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
444 if ((c
= ARRAY_ITEM(&clients
, i
)) == NULL
)
450 c
= cmd_choose_client(&cc
);
456 return (cmd_choose_client(&clients
));
459 /* Choose the most recently used client from a list. */
461 cmd_choose_client(struct clients
*cc
)
463 struct client
*c
, *cbest
;
464 struct timeval
*tv
= NULL
;
468 for (i
= 0; i
< ARRAY_LENGTH(cc
); i
++) {
469 if ((c
= ARRAY_ITEM(cc
, i
)) == NULL
)
471 if (c
->session
== NULL
)
474 if (tv
== NULL
|| timercmp(&c
->activity_time
, tv
, >)) {
476 tv
= &c
->activity_time
;
483 /* Find the target client or report an error and return NULL. */
485 cmd_find_client(struct cmd_ctx
*ctx
, const char *arg
)
491 /* A NULL argument means the current client. */
493 return (cmd_current_client(ctx
));
494 tmparg
= xstrdup(arg
);
496 /* Trim a single trailing colon if any. */
497 arglen
= strlen(tmparg
);
498 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
499 tmparg
[arglen
- 1] = '\0';
501 /* Find the client, if any. */
502 c
= cmd_lookup_client(tmparg
);
504 /* If no client found, report an error. */
506 ctx
->error(ctx
, "client not found: %s", tmparg
);
513 * Lookup a client by device path. Either of a full match and a match without a
514 * leading _PATH_DEV ("/dev/") is accepted.
517 cmd_lookup_client(const char *name
)
523 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
524 c
= ARRAY_ITEM(&clients
, i
);
525 if (c
== NULL
|| c
->session
== NULL
)
529 /* Check for exact matches. */
530 if (strcmp(name
, path
) == 0)
533 /* Check without leading /dev if present. */
534 if (strncmp(path
, _PATH_DEV
, (sizeof _PATH_DEV
) - 1) != 0)
536 if (strcmp(name
, path
+ (sizeof _PATH_DEV
) - 1) == 0)
543 /* Lookup a session by name. If no session is found, NULL is returned. */
545 cmd_lookup_session(const char *name
, int *ambiguous
)
547 struct session
*s
, *sfound
;
552 * Look for matches. First look for exact matches - session names must
553 * be unique so an exact match can't be ambigious and can just be
556 if ((s
= session_find(name
)) != NULL
)
560 * Otherwise look for partial matches, returning early if it is found to
564 RB_FOREACH(s
, sessions
, &sessions
) {
565 if (strncmp(name
, s
->name
, strlen(name
)) == 0 ||
566 fnmatch(name
, s
->name
, 0) == 0) {
567 if (sfound
!= NULL
) {
578 * Lookup a window or return -1 if not found or ambigious. First try as an
579 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
580 * idx if the window index is a valid number but there is no window with that
584 cmd_lookup_window(struct session
*s
, const char *name
, int *ambiguous
)
586 struct winlink
*wl
, *wlfound
;
592 /* Try as a window id. */
593 if ((wl
= cmd_lookup_winlink_windowid(s
, name
)) != NULL
)
596 /* First see if this is a valid window index in this session. */
597 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
598 if (errstr
== NULL
) {
599 if ((wl
= winlink_find_by_index(&s
->windows
, idx
)) != NULL
)
603 /* Look for exact matches, error if more than one. */
605 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
606 if (strcmp(name
, wl
->window
->name
) == 0) {
607 if (wlfound
!= NULL
) {
617 /* Now look for pattern matches, again error if multiple. */
619 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
620 if (strncmp(name
, wl
->window
->name
, strlen(name
)) == 0 ||
621 fnmatch(name
, wl
->window
->name
, 0) == 0) {
622 if (wlfound
!= NULL
) {
636 * Find a window index - if the window doesn't exist, check if it is a
637 * potential index and return it anyway.
640 cmd_lookup_index(struct session
*s
, const char *name
, int *ambiguous
)
646 if ((wl
= cmd_lookup_window(s
, name
, ambiguous
)) != NULL
)
651 idx
= strtonum(name
, 0, INT_MAX
, &errstr
);
658 /* Lookup pane id. An initial % means a pane id. */
660 cmd_lookup_paneid(const char *arg
)
668 paneid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
671 return (window_pane_find_by_id(paneid
));
674 /* Lookup window id in a session. An initial @ means a window id. */
676 cmd_lookup_winlink_windowid(struct session
*s
, const char *arg
)
684 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
687 return (winlink_find_by_window_id(&s
->windows
, windowid
));
690 /* Lookup window id. An initial @ means a window id. */
692 cmd_lookup_windowid(const char *arg
)
700 windowid
= strtonum(arg
+ 1, 0, UINT_MAX
, &errstr
);
703 return (window_find_by_id(windowid
));
706 /* Find session and winlink for window. */
708 cmd_window_session(struct cmd_ctx
*ctx
, struct window
*w
, struct winlink
**wlp
)
711 struct sessionslist ss
;
714 /* If this window is in the current session, return that winlink. */
715 s
= cmd_current_session(ctx
, 0);
717 wl
= winlink_find_by_window(&s
->windows
, w
);
725 /* Otherwise choose from all sessions with this window. */
727 RB_FOREACH(s
, sessions
, &sessions
) {
728 if (winlink_find_by_window(&s
->windows
, w
) != NULL
)
731 s
= cmd_choose_session_list(&ss
);
734 *wlp
= winlink_find_by_window(&s
->windows
, w
);
738 /* Find the target session or report an error and return NULL. */
740 cmd_find_session(struct cmd_ctx
*ctx
, const char *arg
, int prefer_unattached
)
743 struct window_pane
*wp
;
750 /* A NULL argument means the current session. */
752 return (cmd_current_session(ctx
, prefer_unattached
));
754 /* Lookup as pane id or window id. */
755 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
)
756 return (cmd_window_session(ctx
, wp
->window
, NULL
));
757 if ((w
= cmd_lookup_windowid(arg
)) != NULL
)
758 return (cmd_window_session(ctx
, w
, NULL
));
760 /* Trim a single trailing colon if any. */
761 tmparg
= xstrdup(arg
);
762 arglen
= strlen(tmparg
);
763 if (arglen
!= 0 && tmparg
[arglen
- 1] == ':')
764 tmparg
[arglen
- 1] = '\0';
766 /* An empty session name is the current session. */
767 if (*tmparg
== '\0') {
769 return (cmd_current_session(ctx
, prefer_unattached
));
772 /* Find the session, if any. */
773 s
= cmd_lookup_session(tmparg
, &ambiguous
);
775 /* If it doesn't, try to match it as a client. */
776 if (s
== NULL
&& (c
= cmd_lookup_client(tmparg
)) != NULL
)
779 /* If no session found, report an error. */
782 ctx
->error(ctx
, "more than one session: %s", tmparg
);
784 ctx
->error(ctx
, "session not found: %s", tmparg
);
791 /* Find the target session and window or report an error and return NULL. */
793 cmd_find_window(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
797 struct window_pane
*wp
;
799 char *sessptr
= NULL
;
803 * Find the current session. There must always be a current session, if
804 * it can't be found, report an error.
806 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
807 ctx
->error(ctx
, "can't establish current session");
811 /* A NULL argument means the current session and window. */
818 /* Lookup as pane id. */
819 if ((wp
= cmd_lookup_paneid(arg
)) != NULL
) {
820 s
= cmd_window_session(ctx
, wp
->window
, &wl
);
826 /* Time to look at the argument. If it is empty, that is an error. */
830 /* Find the separating colon and split into window and session. */
831 winptr
= strchr(arg
, ':');
834 winptr
++; /* skip : */
835 sessptr
= xstrdup(arg
);
836 *strchr(sessptr
, ':') = '\0';
838 /* Try to lookup the session if present. */
839 if (*sessptr
!= '\0') {
840 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
847 * Then work out the window. An empty string is the current window,
848 * otherwise try special cases then to look it up in the session.
852 else if (winptr
[0] == '!' && winptr
[1] == '\0')
853 wl
= TAILQ_FIRST(&s
->lastw
);
854 else if (winptr
[0] == '+' || winptr
[0] == '-')
855 wl
= cmd_find_window_offset(winptr
, s
, &ambiguous
);
857 wl
= cmd_lookup_window(s
, winptr
, &ambiguous
);
867 * No colon in the string, first try special cases, then as a window
868 * and lastly as a session.
870 if (arg
[0] == '!' && arg
[1] == '\0') {
871 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
873 } else if (arg
[0] == '+' || arg
[0] == '-') {
874 if ((wl
= cmd_find_window_offset(arg
, s
, &ambiguous
)) == NULL
)
876 } else if ((wl
= cmd_lookup_window(s
, arg
, &ambiguous
)) == NULL
)
887 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
897 ctx
->error(ctx
, "multiple sessions: %s", arg
);
899 ctx
->error(ctx
, "session not found: %s", arg
);
906 ctx
->error(ctx
, "multiple windows: %s", arg
);
908 ctx
->error(ctx
, "window not found: %s", arg
);
915 cmd_find_window_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
920 if (winptr
[1] != '\0')
921 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
923 wl
= cmd_lookup_window(s
, winptr
, ambiguous
);
925 if (winptr
[0] == '+')
926 wl
= winlink_next_by_number(s
->curw
, s
, offset
);
928 wl
= winlink_previous_by_number(s
->curw
, s
, offset
);
935 * Find the target session and window index, whether or not it exists in the
936 * session. Return -2 on error or -1 if no window index is specified. This is
937 * used when parsing an argument for a window target that may not exist (for
938 * example if it is going to be created).
941 cmd_find_index(struct cmd_ctx
*ctx
, const char *arg
, struct session
**sp
)
946 char *sessptr
= NULL
;
947 int idx
, ambiguous
= 0;
950 * Find the current session. There must always be a current session, if
951 * it can't be found, report an error.
953 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
954 ctx
->error(ctx
, "can't establish current session");
958 /* A NULL argument means the current session and "no window" (-1). */
965 /* Time to look at the argument. If it is empty, that is an error. */
969 /* Find the separating colon. If none, assume the current session. */
970 winptr
= strchr(arg
, ':');
973 winptr
++; /* skip : */
974 sessptr
= xstrdup(arg
);
975 *strchr(sessptr
, ':') = '\0';
977 /* Try to lookup the session if present. */
978 if (sessptr
!= NULL
&& *sessptr
!= '\0') {
979 if ((s
= cmd_lookup_session(sessptr
, &ambiguous
)) == NULL
)
986 * Then work out the window. An empty string is a new window otherwise
987 * try to look it up in the session.
991 else if (winptr
[0] == '!' && winptr
[1] == '\0') {
992 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
995 } else if (winptr
[0] == '+' || winptr
[0] == '-') {
996 if ((idx
= cmd_find_index_offset(winptr
, s
, &ambiguous
)) < 0)
998 } else if ((idx
= cmd_lookup_index(s
, winptr
, &ambiguous
)) == -1)
1001 if (sessptr
!= NULL
)
1007 * No colon in the string, first try special cases, then as a window
1008 * and lastly as a session.
1010 if (arg
[0] == '!' && arg
[1] == '\0') {
1011 if ((wl
= TAILQ_FIRST(&s
->lastw
)) == NULL
)
1014 } else if (arg
[0] == '+' || arg
[0] == '-') {
1015 if ((idx
= cmd_find_index_offset(arg
, s
, &ambiguous
)) < 0)
1016 goto lookup_session
;
1017 } else if ((idx
= cmd_lookup_index(s
, arg
, &ambiguous
)) == -1)
1018 goto lookup_session
;
1028 if (*arg
!= '\0' && (s
= cmd_lookup_session(arg
, &ambiguous
)) == NULL
)
1038 ctx
->error(ctx
, "multiple sessions: %s", arg
);
1040 ctx
->error(ctx
, "session not found: %s", arg
);
1041 if (sessptr
!= NULL
)
1048 ctx
->error(ctx
, "invalid index: %s", arg
);
1050 if (sessptr
!= NULL
)
1056 ctx
->error(ctx
, "multiple windows: %s", arg
);
1058 ctx
->error(ctx
, "window not found: %s", arg
);
1059 if (sessptr
!= NULL
)
1065 cmd_find_index_offset(const char *winptr
, struct session
*s
, int *ambiguous
)
1067 int idx
, offset
= 1;
1069 if (winptr
[1] != '\0')
1070 offset
= strtonum(winptr
+ 1, 1, INT_MAX
, NULL
);
1072 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1074 if (winptr
[0] == '+') {
1075 if (s
->curw
->idx
== INT_MAX
)
1076 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1078 idx
= s
->curw
->idx
+ offset
;
1080 if (s
->curw
->idx
== 0)
1081 idx
= cmd_lookup_index(s
, winptr
, ambiguous
);
1083 idx
= s
->curw
->idx
- offset
;
1091 * Find the target session, window and pane number or report an error and
1092 * return NULL. The pane number is separated from the session:window by a .,
1093 * such as mysession:mywindow.0.
1096 cmd_find_pane(struct cmd_ctx
*ctx
,
1097 const char *arg
, struct session
**sp
, struct window_pane
**wpp
)
1101 const char *period
, *errstr
;
1102 char *winptr
, *paneptr
;
1105 /* Get the current session. */
1106 if ((s
= cmd_current_session(ctx
, 0)) == NULL
) {
1107 ctx
->error(ctx
, "can't establish current session");
1113 /* A NULL argument means the current session, window and pane. */
1115 *wpp
= s
->curw
->window
->active
;
1119 /* Lookup as pane id. */
1120 if ((*wpp
= cmd_lookup_paneid(arg
)) != NULL
) {
1121 s
= cmd_window_session(ctx
, (*wpp
)->window
, &wl
);
1127 /* Look for a separating period. */
1128 if ((period
= strrchr(arg
, '.')) == NULL
)
1131 /* Pull out the window part and parse it. */
1132 winptr
= xstrdup(arg
);
1133 winptr
[period
- arg
] = '\0';
1134 if (*winptr
== '\0')
1136 else if ((wl
= cmd_find_window(ctx
, winptr
, sp
)) == NULL
)
1139 /* Find the pane section and look it up. */
1140 paneptr
= winptr
+ (period
- arg
) + 1;
1141 if (*paneptr
== '\0')
1142 *wpp
= wl
->window
->active
;
1143 else if (paneptr
[0] == '+' || paneptr
[0] == '-')
1144 *wpp
= cmd_find_pane_offset(paneptr
, wl
);
1146 idx
= strtonum(paneptr
, 0, INT_MAX
, &errstr
);
1149 *wpp
= window_pane_at_index(wl
->window
, idx
);
1158 /* Try pane string description. */
1159 if ((*wpp
= window_find_string(wl
->window
, paneptr
)) == NULL
) {
1160 ctx
->error(ctx
, "can't find pane: %s", paneptr
);
1168 /* Try as a pane number alone. */
1169 idx
= strtonum(arg
, 0, INT_MAX
, &errstr
);
1173 /* Try index in the current session and window. */
1174 if ((*wpp
= window_pane_at_index(s
->curw
->window
, idx
)) == NULL
)
1180 /* Try pane string description. */
1181 if ((*wpp
= window_find_string(s
->curw
->window
, arg
)) != NULL
)
1184 /* Try as a window and use the active pane. */
1185 if ((wl
= cmd_find_window(ctx
, arg
, sp
)) != NULL
)
1186 *wpp
= wl
->window
->active
;
1194 struct window_pane
*
1195 cmd_find_pane_offset(const char *paneptr
, struct winlink
*wl
)
1197 struct window
*w
= wl
->window
;
1198 struct window_pane
*wp
= w
->active
;
1201 if (paneptr
[1] != '\0')
1202 offset
= strtonum(paneptr
+ 1, 1, INT_MAX
, NULL
);
1204 if (paneptr
[0] == '+')
1205 wp
= window_pane_next_by_number(w
, wp
, offset
);
1207 wp
= window_pane_previous_by_number(w
, wp
, offset
);
1213 /* Replace the first %% or %idx in template by s. */
1215 cmd_template_replace(char *template, const char *s
, int idx
)
1222 if (strstr(template, "%") == NULL
)
1223 return (xstrdup(template));
1231 while (*ptr
!= '\0') {
1232 switch (ch
= *ptr
++) {
1234 if (*ptr
< '1' || *ptr
> '9' || *ptr
- '0' != idx
) {
1235 if (*ptr
!= '%' || replaced
)
1242 buf
= xrealloc(buf
, 1, len
+ 1);
1243 strlcat(buf
, s
, len
+ 1);
1246 buf
= xrealloc(buf
, 1, len
+ 2);
1254 /* Return the default path for a new pane. */
1256 cmd_get_default_path(struct cmd_ctx
*ctx
)
1260 struct window_pane
*wp
;
1261 struct environ_entry
*envent
;
1263 if ((s
= cmd_current_session(ctx
, 0)) == NULL
)
1266 cwd
= options_get_string(&s
->options
, "default-path");
1267 if ((cwd
[0] == '~' && cwd
[1] == '\0') || !strcmp(cwd
, "$HOME")) {
1268 envent
= environ_find(&global_environ
, "HOME");
1269 if (envent
!= NULL
&& *envent
->value
!= '\0')
1270 return envent
->value
;
1274 if (ctx
->cmdclient
!= NULL
&& ctx
->cmdclient
->cwd
!= NULL
)
1275 return (ctx
->cmdclient
->cwd
);
1276 if (ctx
->curclient
!= NULL
) {
1277 wp
= s
->curw
->window
->active
;
1278 if ((cwd
= get_proc_cwd(wp
->pid
)) != NULL
)