cmd-swap-window.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <stdlib.h>
  19. #include "tmux.h"
  20. /*
  21. * Swap one window with another.
  22. */
  23. enum cmd_retval cmd_swap_window_exec(struct cmd *, struct cmd_q *);
  24. const struct cmd_entry cmd_swap_window_entry = {
  25. .name = "swap-window",
  26. .alias = "swapw",
  27. .args = { "ds:t:", 0, 0 },
  28. .usage = "[-d] " CMD_SRCDST_WINDOW_USAGE,
  29. .sflag = CMD_WINDOW_MARKED,
  30. .tflag = CMD_WINDOW,
  31. .flags = 0,
  32. .exec = cmd_swap_window_exec
  33. };
  34. enum cmd_retval
  35. cmd_swap_window_exec(struct cmd *self, struct cmd_q *cmdq)
  36. {
  37. #ifdef TMATE
  38. cmdq_error(cmdq, "swap window is not supported with tmate");
  39. return (CMD_RETURN_ERROR);
  40. #else
  41. struct session *src, *dst;
  42. struct session_group *sg_src, *sg_dst;
  43. struct winlink *wl_src, *wl_dst;
  44. struct window *w;
  45. wl_src = cmdq->state.sflag.wl;
  46. src = cmdq->state.sflag.s;
  47. sg_src = session_group_find(src);
  48. wl_dst = cmdq->state.tflag.wl;
  49. dst = cmdq->state.tflag.s;
  50. sg_dst = session_group_find(dst);
  51. if (src != dst && sg_src != NULL && sg_dst != NULL &&
  52. sg_src == sg_dst) {
  53. cmdq_error(cmdq, "can't move window, sessions are grouped");
  54. return (CMD_RETURN_ERROR);
  55. }
  56. if (wl_dst->window == wl_src->window)
  57. return (CMD_RETURN_NORMAL);
  58. w = wl_dst->window;
  59. wl_dst->window = wl_src->window;
  60. wl_src->window = w;
  61. if (!args_has(self->args, 'd')) {
  62. session_select(dst, wl_dst->idx);
  63. if (src != dst)
  64. session_select(src, wl_src->idx);
  65. }
  66. session_group_synchronize_from(src);
  67. server_redraw_session_group(src);
  68. if (src != dst) {
  69. session_group_synchronize_from(dst);
  70. server_redraw_session_group(dst);
  71. }
  72. recalculate_sizes();
  73. return (CMD_RETURN_NORMAL);
  74. #endif
  75. }