key-bindings.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <ctype.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "tmux.h"
  22. RB_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp);
  23. RB_GENERATE(key_tables, key_table, entry, key_table_cmp);
  24. struct key_tables key_tables = RB_INITIALIZER(&key_tables);
  25. int
  26. key_table_cmp(struct key_table *e1, struct key_table *e2)
  27. {
  28. return (strcmp(e1->name, e2->name));
  29. }
  30. int
  31. key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
  32. {
  33. if (bd1->key < bd2->key)
  34. return (-1);
  35. if (bd1->key > bd2->key)
  36. return (1);
  37. return (0);
  38. }
  39. struct key_table *
  40. key_bindings_get_table(const char *name, int create)
  41. {
  42. struct key_table table_find, *table;
  43. table_find.name = name;
  44. table = RB_FIND(key_tables, &key_tables, &table_find);
  45. if (table != NULL || !create)
  46. return (table);
  47. table = xmalloc(sizeof *table);
  48. table->name = xstrdup(name);
  49. RB_INIT(&table->key_bindings);
  50. table->references = 1; /* one reference in key_tables */
  51. RB_INSERT(key_tables, &key_tables, table);
  52. return (table);
  53. }
  54. void
  55. key_bindings_unref_table(struct key_table *table)
  56. {
  57. struct key_binding *bd;
  58. struct key_binding *bd1;
  59. if (--table->references != 0)
  60. return;
  61. RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1) {
  62. RB_REMOVE(key_bindings, &table->key_bindings, bd);
  63. cmd_list_free(bd->cmdlist);
  64. free(bd);
  65. }
  66. free((void *)table->name);
  67. free(table);
  68. }
  69. void
  70. key_bindings_add(const char *name, key_code key, int can_repeat,
  71. struct cmd_list *cmdlist)
  72. {
  73. struct key_table *table;
  74. struct key_binding bd_find, *bd;
  75. table = key_bindings_get_table(name, 1);
  76. bd_find.key = key;
  77. bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
  78. if (bd != NULL) {
  79. RB_REMOVE(key_bindings, &table->key_bindings, bd);
  80. cmd_list_free(bd->cmdlist);
  81. free(bd);
  82. }
  83. bd = xmalloc(sizeof *bd);
  84. bd->key = key;
  85. RB_INSERT(key_bindings, &table->key_bindings, bd);
  86. bd->can_repeat = can_repeat;
  87. bd->cmdlist = cmdlist;
  88. }
  89. void
  90. key_bindings_remove(const char *name, key_code key)
  91. {
  92. struct key_table *table;
  93. struct key_binding bd_find, *bd;
  94. table = key_bindings_get_table(name, 0);
  95. if (table == NULL)
  96. return;
  97. bd_find.key = key;
  98. bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
  99. if (bd == NULL)
  100. return;
  101. RB_REMOVE(key_bindings, &table->key_bindings, bd);
  102. cmd_list_free(bd->cmdlist);
  103. free(bd);
  104. if (RB_EMPTY(&table->key_bindings)) {
  105. RB_REMOVE(key_tables, &key_tables, table);
  106. key_bindings_unref_table(table);
  107. }
  108. }
  109. void
  110. key_bindings_remove_table(const char *name)
  111. {
  112. struct key_table *table;
  113. table = key_bindings_get_table(name, 0);
  114. if (table != NULL) {
  115. RB_REMOVE(key_tables, &key_tables, table);
  116. key_bindings_unref_table(table);
  117. }
  118. }
  119. void
  120. key_bindings_init(void)
  121. {
  122. static const char *defaults[] = {
  123. "bind C-b send-prefix",
  124. "bind C-o rotate-window",
  125. "bind C-z suspend-client",
  126. "bind Space next-layout",
  127. "bind ! break-pane",
  128. "bind '\"' split-window",
  129. "bind '#' list-buffers",
  130. "bind '$' command-prompt -I'#S' \"rename-session '%%'\"",
  131. "bind % split-window -h",
  132. "bind & confirm-before -p\"kill-window #W? (y/n)\" kill-window",
  133. "bind \"'\" command-prompt -pindex \"select-window -t ':%%'\"",
  134. "bind ( switch-client -p",
  135. "bind ) switch-client -n",
  136. "bind , command-prompt -I'#W' \"rename-window '%%'\"",
  137. "bind - delete-buffer",
  138. "bind . command-prompt \"move-window -t '%%'\"",
  139. "bind 0 select-window -t:=0",
  140. "bind 1 select-window -t:=1",
  141. "bind 2 select-window -t:=2",
  142. "bind 3 select-window -t:=3",
  143. "bind 4 select-window -t:=4",
  144. "bind 5 select-window -t:=5",
  145. "bind 6 select-window -t:=6",
  146. "bind 7 select-window -t:=7",
  147. "bind 8 select-window -t:=8",
  148. "bind 9 select-window -t:=9",
  149. "bind : command-prompt",
  150. "bind \\; last-pane",
  151. "bind = choose-buffer",
  152. "bind ? list-keys",
  153. "bind D choose-client",
  154. "bind L switch-client -l",
  155. "bind M select-pane -M",
  156. "bind [ copy-mode",
  157. "bind ] paste-buffer",
  158. "bind c new-window",
  159. "bind d detach-client",
  160. "bind f command-prompt \"find-window '%%'\"",
  161. "bind i display-message",
  162. "bind l last-window",
  163. "bind m select-pane -m",
  164. "bind n next-window",
  165. "bind o select-pane -t:.+",
  166. "bind p previous-window",
  167. "bind q display-panes",
  168. "bind r refresh-client",
  169. "bind s choose-tree",
  170. "bind t clock-mode",
  171. "bind w choose-window",
  172. "bind x confirm-before -p\"kill-pane #P? (y/n)\" kill-pane",
  173. "bind z resize-pane -Z",
  174. "bind { swap-pane -U",
  175. "bind } swap-pane -D",
  176. "bind '~' show-messages",
  177. "bind PPage copy-mode -u",
  178. "bind -r Up select-pane -U",
  179. "bind -r Down select-pane -D",
  180. "bind -r Left select-pane -L",
  181. "bind -r Right select-pane -R",
  182. "bind M-1 select-layout even-horizontal",
  183. "bind M-2 select-layout even-vertical",
  184. "bind M-3 select-layout main-horizontal",
  185. "bind M-4 select-layout main-vertical",
  186. "bind M-5 select-layout tiled",
  187. "bind M-n next-window -a",
  188. "bind M-o rotate-window -D",
  189. "bind M-p previous-window -a",
  190. "bind -r M-Up resize-pane -U 5",
  191. "bind -r M-Down resize-pane -D 5",
  192. "bind -r M-Left resize-pane -L 5",
  193. "bind -r M-Right resize-pane -R 5",
  194. "bind -r C-Up resize-pane -U",
  195. "bind -r C-Down resize-pane -D",
  196. "bind -r C-Left resize-pane -L",
  197. "bind -r C-Right resize-pane -R",
  198. "bind -n MouseDown1Pane select-pane -t=\\; send-keys -M",
  199. "bind -n MouseDrag1Border resize-pane -M",
  200. "bind -n MouseDown1Status select-window -t=",
  201. "bind -n WheelDownStatus next-window",
  202. "bind -n WheelUpStatus previous-window",
  203. "bind -n MouseDrag1Pane if -Ft= '#{mouse_any_flag}' 'if -Ft= \"#{pane_in_mode}\" \"copy-mode -M\" \"send-keys -M\"' 'copy-mode -M'",
  204. "bind -n MouseDown3Pane if-shell -Ft= '#{mouse_any_flag}' 'select-pane -t=; send-keys -M' 'select-pane -mt='",
  205. "bind -n WheelUpPane if-shell -Ft= '#{mouse_any_flag}' 'send-keys -M' 'if -Ft= \"#{pane_in_mode}\" \"send-keys -M\" \"copy-mode -et=\"'",
  206. };
  207. u_int i;
  208. struct cmd_list *cmdlist;
  209. char *cause;
  210. int error;
  211. struct cmd_q *cmdq;
  212. cmdq = cmdq_new(NULL);
  213. for (i = 0; i < nitems(defaults); i++) {
  214. error = cmd_string_parse(defaults[i], &cmdlist,
  215. "<default-keys>", i, &cause);
  216. if (error != 0)
  217. fatalx("bad default key");
  218. cmdq_run(cmdq, cmdlist, NULL);
  219. cmd_list_free(cmdlist);
  220. }
  221. cmdq_free(cmdq);
  222. }
  223. void
  224. key_bindings_dispatch(struct key_binding *bd, struct client *c,
  225. struct mouse_event *m)
  226. {
  227. struct cmd *cmd;
  228. int readonly;
  229. readonly = 1;
  230. TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) {
  231. if (!(cmd->entry->flags & CMD_READONLY))
  232. readonly = 0;
  233. }
  234. if (!readonly && (c->flags & CLIENT_READONLY)) {
  235. cmdq_error(c->cmdq, "client is read-only");
  236. return;
  237. }
  238. cmdq_run(c->cmdq, bd->cmdlist, m);
  239. }