cmd-split-window.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include "tmux.h"
  24. /*
  25. * Split a window (add a new pane).
  26. */
  27. #define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
  28. enum cmd_retval cmd_split_window_exec(struct cmd *, struct cmd_q *);
  29. const struct cmd_entry cmd_split_window_entry = {
  30. .name = "split-window",
  31. .alias = "splitw",
  32. .args = { "bc:dF:l:hp:Pt:v", 0, -1 },
  33. .usage = "[-bdhvP] [-c start-directory] [-F format] "
  34. "[-p percentage|-l size] " CMD_TARGET_PANE_USAGE " [command]",
  35. .tflag = CMD_PANE,
  36. .flags = 0,
  37. .exec = cmd_split_window_exec
  38. };
  39. enum cmd_retval
  40. cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq)
  41. {
  42. struct args *args = self->args;
  43. struct session *s = cmdq->state.tflag.s;
  44. struct winlink *wl = cmdq->state.tflag.wl;
  45. struct window *w = wl->window;
  46. struct window_pane *wp = cmdq->state.tflag.wp, *new_wp = NULL;
  47. struct environ *env;
  48. const char *cmd, *path, *shell, *template, *cwd, *to_free;
  49. char **argv, *cause, *new_cause, *cp;
  50. u_int hlimit;
  51. int argc, size, percentage;
  52. enum layout_type type;
  53. struct layout_cell *lc;
  54. struct format_tree *ft;
  55. struct environ_entry *envent;
  56. server_unzoom_window(w);
  57. env = environ_create();
  58. environ_copy(global_environ, env);
  59. environ_copy(s->environ, env);
  60. server_fill_environ(s, env);
  61. if (args->argc == 0) {
  62. cmd = options_get_string(s->options, "default-command");
  63. if (cmd != NULL && *cmd != '\0') {
  64. argc = 1;
  65. argv = (char **)&cmd;
  66. } else {
  67. argc = 0;
  68. argv = NULL;
  69. }
  70. } else {
  71. argc = args->argc;
  72. argv = args->argv;
  73. }
  74. to_free = NULL;
  75. if (args_has(args, 'c')) {
  76. ft = format_create(cmdq, 0);
  77. format_defaults(ft, cmdq->state.c, s, NULL, NULL);
  78. to_free = cwd = format_expand(ft, args_get(args, 'c'));
  79. format_free(ft);
  80. } else if (cmdq->client != NULL && cmdq->client->session == NULL)
  81. cwd = cmdq->client->cwd;
  82. else
  83. cwd = s->cwd;
  84. type = LAYOUT_TOPBOTTOM;
  85. if (args_has(args, 'h'))
  86. type = LAYOUT_LEFTRIGHT;
  87. size = -1;
  88. if (args_has(args, 'l')) {
  89. size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
  90. if (cause != NULL) {
  91. xasprintf(&new_cause, "size %s", cause);
  92. free(cause);
  93. cause = new_cause;
  94. goto error;
  95. }
  96. } else if (args_has(args, 'p')) {
  97. percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
  98. if (cause != NULL) {
  99. xasprintf(&new_cause, "percentage %s", cause);
  100. free(cause);
  101. cause = new_cause;
  102. goto error;
  103. }
  104. if (type == LAYOUT_TOPBOTTOM)
  105. size = (wp->sy * percentage) / 100;
  106. else
  107. size = (wp->sx * percentage) / 100;
  108. }
  109. hlimit = options_get_number(s->options, "history-limit");
  110. shell = options_get_string(s->options, "default-shell");
  111. if (*shell == '\0' || areshell(shell))
  112. shell = _PATH_BSHELL;
  113. lc = layout_split_pane(wp, type, size, args_has(args, 'b'));
  114. if (lc == NULL) {
  115. cause = xstrdup("pane too small");
  116. goto error;
  117. }
  118. new_wp = window_add_pane(w, hlimit);
  119. layout_assign_pane(lc, new_wp);
  120. path = NULL;
  121. if (cmdq->client != NULL && cmdq->client->session == NULL)
  122. envent = environ_find(cmdq->client->environ, "PATH");
  123. else
  124. envent = environ_find(s->environ, "PATH");
  125. if (envent != NULL)
  126. path = envent->value;
  127. if (window_pane_spawn(new_wp, argc, argv, path, shell, cwd, env,
  128. s->tio, &cause) != 0)
  129. goto error;
  130. server_redraw_window(w);
  131. if (!args_has(args, 'd')) {
  132. window_set_active_pane(w, new_wp);
  133. session_select(s, wl->idx);
  134. server_redraw_session(s);
  135. } else
  136. server_status_session(s);
  137. environ_free(env);
  138. if (args_has(args, 'P')) {
  139. if ((template = args_get(args, 'F')) == NULL)
  140. template = SPLIT_WINDOW_TEMPLATE;
  141. ft = format_create(cmdq, 0);
  142. format_defaults(ft, cmdq->state.c, s, wl, new_wp);
  143. cp = format_expand(ft, template);
  144. cmdq_print(cmdq, "%s", cp);
  145. free(cp);
  146. format_free(ft);
  147. }
  148. notify_window_layout_changed(w);
  149. if (to_free != NULL)
  150. free((void *)to_free);
  151. return (CMD_RETURN_NORMAL);
  152. error:
  153. environ_free(env);
  154. if (new_wp != NULL) {
  155. layout_close_pane(new_wp);
  156. window_remove_pane(w, new_wp);
  157. }
  158. cmdq_error(cmdq, "create pane failed: %s", cause);
  159. free(cause);
  160. if (to_free != NULL)
  161. free((void *)to_free);
  162. return (CMD_RETURN_ERROR);
  163. }