1 /* $OpenBSD: cmd-select-layout.c,v 1.17 2013/01/17 00:11:22 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 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. 9 * 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. 17 */ 18 19 #include <sys/types.h> 20 21 #include "tmux.h" 22 23 /* 24 * Switch window to selected layout. 25 */ 26 27 void cmd_select_layout_key_binding(struct cmd *, int); 28 enum cmd_retval cmd_select_layout_exec(struct cmd *, struct cmd_ctx *); 29 30 const struct cmd_entry cmd_select_layout_entry = { 31 "select-layout", "selectl", 32 "npt:", 0, 1, 33 "[-np] " CMD_TARGET_WINDOW_USAGE " [layout-name]", 34 0, 35 cmd_select_layout_key_binding, 36 NULL, 37 cmd_select_layout_exec 38 }; 39 40 const struct cmd_entry cmd_next_layout_entry = { 41 "next-layout", "nextl", 42 "t:", 0, 0, 43 CMD_TARGET_WINDOW_USAGE, 44 0, 45 NULL, 46 NULL, 47 cmd_select_layout_exec 48 }; 49 50 const struct cmd_entry cmd_previous_layout_entry = { 51 "previous-layout", "prevl", 52 "t:", 0, 0, 53 CMD_TARGET_WINDOW_USAGE, 54 0, 55 NULL, 56 NULL, 57 cmd_select_layout_exec 58 }; 59 60 void 61 cmd_select_layout_key_binding(struct cmd *self, int key) 62 { 63 switch (key) { 64 case '1' | KEYC_ESCAPE: 65 self->args = args_create(1, "even-horizontal"); 66 break; 67 case '2' | KEYC_ESCAPE: 68 self->args = args_create(1, "even-vertical"); 69 break; 70 case '3' | KEYC_ESCAPE: 71 self->args = args_create(1, "main-horizontal"); 72 break; 73 case '4' | KEYC_ESCAPE: 74 self->args = args_create(1, "main-vertical"); 75 break; 76 case '5' | KEYC_ESCAPE: 77 self->args = args_create(1, "tiled"); 78 break; 79 default: 80 self->args = args_create(0); 81 break; 82 } 83 } 84 85 enum cmd_retval 86 cmd_select_layout_exec(struct cmd *self, struct cmd_ctx *ctx) 87 { 88 struct args *args = self->args; 89 struct winlink *wl; 90 struct window *w; 91 const char *layoutname; 92 int next, previous, layout; 93 94 if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL) 95 return (CMD_RETURN_ERROR); 96 w = wl->window; 97 98 next = self->entry == &cmd_next_layout_entry; 99 if (args_has(self->args, 'n')) 100 next = 1; 101 previous = self->entry == &cmd_previous_layout_entry; 102 if (args_has(self->args, 'p')) 103 previous = 1; 104 105 if (next || previous) { 106 if (next) 107 layout = layout_set_next(wl->window); 108 else 109 layout = layout_set_previous(wl->window); 110 server_redraw_window(wl->window); 111 ctx->info(ctx, "arranging in: %s", layout_set_name(layout)); 112 return (CMD_RETURN_NORMAL); 113 } 114 115 if (args->argc == 0) 116 layout = wl->window->lastlayout; 117 else 118 layout = layout_set_lookup(args->argv[0]); 119 if (layout != -1) { 120 layout = layout_set_select(wl->window, layout); 121 server_redraw_window(wl->window); 122 ctx->info(ctx, "arranging in: %s", layout_set_name(layout)); 123 return (CMD_RETURN_NORMAL); 124 } 125 126 if (args->argc != 0) { 127 layoutname = args->argv[0]; 128 if (layout_parse(wl->window, layoutname) == -1) { 129 ctx->error(ctx, "can't set layout: %s", layoutname); 130 return (CMD_RETURN_ERROR); 131 } 132 server_redraw_window(wl->window); 133 ctx->info(ctx, "arranging in: %s", layoutname); 134 } 135 return (CMD_RETURN_NORMAL); 136 } 137