cmd-new-window.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 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. * Create a new window.
  26. */
  27. #define NEW_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
  28. enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmd_q *);
  29. const struct cmd_entry cmd_new_window_entry = {
  30. .name = "new-window",
  31. .alias = "neww",
  32. .args = { "ac:dF:kn:Pt:", 0, -1 },
  33. .usage = "[-adkP] [-c start-directory] [-F format] [-n window-name] "
  34. CMD_TARGET_WINDOW_USAGE " [command]",
  35. .tflag = CMD_WINDOW_INDEX,
  36. .flags = 0,
  37. .exec = cmd_new_window_exec
  38. };
  39. enum cmd_retval
  40. cmd_new_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 client *c = cmdq->state.c;
  46. int idx = cmdq->state.tflag.idx;
  47. const char *cmd, *path, *template, *cwd, *to_free;
  48. char **argv, *cause, *cp;
  49. int argc, detached;
  50. struct format_tree *ft;
  51. struct environ_entry *envent;
  52. if (args_has(args, 'a')) {
  53. if ((idx = winlink_shuffle_up(s, wl)) == -1) {
  54. cmdq_error(cmdq, "no free window indexes");
  55. return (CMD_RETURN_ERROR);
  56. }
  57. }
  58. detached = args_has(args, 'd');
  59. if (args->argc == 0) {
  60. cmd = options_get_string(s->options, "default-command");
  61. if (cmd != NULL && *cmd != '\0') {
  62. argc = 1;
  63. argv = (char **)&cmd;
  64. } else {
  65. argc = 0;
  66. argv = NULL;
  67. }
  68. } else {
  69. argc = args->argc;
  70. argv = args->argv;
  71. }
  72. path = NULL;
  73. if (cmdq->client != NULL && cmdq->client->session == NULL)
  74. envent = environ_find(cmdq->client->environ, "PATH");
  75. else
  76. envent = environ_find(s->environ, "PATH");
  77. if (envent != NULL)
  78. path = envent->value;
  79. to_free = NULL;
  80. if (args_has(args, 'c')) {
  81. ft = format_create(cmdq, 0);
  82. format_defaults(ft, c, s, NULL, NULL);
  83. cwd = to_free = format_expand(ft, args_get(args, 'c'));
  84. format_free(ft);
  85. } else if (cmdq->client != NULL && cmdq->client->session == NULL)
  86. cwd = cmdq->client->cwd;
  87. else
  88. cwd = s->cwd;
  89. wl = NULL;
  90. if (idx != -1)
  91. wl = winlink_find_by_index(&s->windows, idx);
  92. if (wl != NULL && args_has(args, 'k')) {
  93. /*
  94. * Can't use session_detach as it will destroy session if this
  95. * makes it empty.
  96. */
  97. notify_window_unlinked(s, wl->window);
  98. wl->flags &= ~WINLINK_ALERTFLAGS;
  99. winlink_stack_remove(&s->lastw, wl);
  100. winlink_remove(&s->windows, wl);
  101. /* Force select/redraw if current. */
  102. if (wl == s->curw) {
  103. detached = 0;
  104. s->curw = NULL;
  105. }
  106. }
  107. if (idx == -1)
  108. idx = -1 - options_get_number(s->options, "base-index");
  109. wl = session_new(s, args_get(args, 'n'), argc, argv, path, cwd, idx,
  110. &cause);
  111. if (wl == NULL) {
  112. cmdq_error(cmdq, "create window failed: %s", cause);
  113. free(cause);
  114. goto error;
  115. }
  116. if (!detached) {
  117. session_select(s, wl->idx);
  118. server_redraw_session_group(s);
  119. } else
  120. server_status_session_group(s);
  121. if (args_has(args, 'P')) {
  122. if ((template = args_get(args, 'F')) == NULL)
  123. template = NEW_WINDOW_TEMPLATE;
  124. ft = format_create(cmdq, 0);
  125. format_defaults(ft, c, s, wl, NULL);
  126. cp = format_expand(ft, template);
  127. cmdq_print(cmdq, "%s", cp);
  128. free(cp);
  129. format_free(ft);
  130. }
  131. if (to_free != NULL)
  132. free((void *)to_free);
  133. return (CMD_RETURN_NORMAL);
  134. error:
  135. if (to_free != NULL)
  136. free((void *)to_free);
  137. return (CMD_RETURN_ERROR);
  138. }