cmd-unbind-key.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * Unbind key from command.
  22. */
  23. enum cmd_retval cmd_unbind_key_exec(struct cmd *, struct cmd_q *);
  24. enum cmd_retval cmd_unbind_key_mode_table(struct cmd *, struct cmd_q *,
  25. key_code);
  26. const struct cmd_entry cmd_unbind_key_entry = {
  27. .name = "unbind-key",
  28. .alias = "unbind",
  29. .args = { "acnt:T:", 0, 1 },
  30. .usage = "[-acn] [-t mode-table] [-T key-table] key",
  31. .flags = 0,
  32. .exec = cmd_unbind_key_exec
  33. };
  34. enum cmd_retval
  35. cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq)
  36. {
  37. struct args *args = self->args;
  38. key_code key;
  39. const char *tablename;
  40. if (!args_has(args, 'a')) {
  41. if (args->argc != 1) {
  42. cmdq_error(cmdq, "missing key");
  43. return (CMD_RETURN_ERROR);
  44. }
  45. key = key_string_lookup_string(args->argv[0]);
  46. if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
  47. cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
  48. return (CMD_RETURN_ERROR);
  49. }
  50. } else {
  51. if (args->argc != 0) {
  52. cmdq_error(cmdq, "key given with -a");
  53. return (CMD_RETURN_ERROR);
  54. }
  55. key = KEYC_UNKNOWN;
  56. }
  57. if (args_has(args, 't'))
  58. return (cmd_unbind_key_mode_table(self, cmdq, key));
  59. if (key == KEYC_UNKNOWN) {
  60. tablename = args_get(args, 'T');
  61. if (tablename == NULL) {
  62. key_bindings_remove_table("root");
  63. key_bindings_remove_table("prefix");
  64. return (CMD_RETURN_NORMAL);
  65. }
  66. if (key_bindings_get_table(tablename, 0) == NULL) {
  67. cmdq_error(cmdq, "table %s doesn't exist", tablename);
  68. return (CMD_RETURN_ERROR);
  69. }
  70. key_bindings_remove_table(tablename);
  71. return (CMD_RETURN_NORMAL);
  72. }
  73. if (args_has(args, 'T')) {
  74. tablename = args_get(args, 'T');
  75. if (key_bindings_get_table(tablename, 0) == NULL) {
  76. cmdq_error(cmdq, "table %s doesn't exist", tablename);
  77. return (CMD_RETURN_ERROR);
  78. }
  79. } else if (args_has(args, 'n'))
  80. tablename = "root";
  81. else
  82. tablename = "prefix";
  83. key_bindings_remove(tablename, key);
  84. return (CMD_RETURN_NORMAL);
  85. }
  86. enum cmd_retval
  87. cmd_unbind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, key_code key)
  88. {
  89. struct args *args = self->args;
  90. const char *tablename;
  91. const struct mode_key_table *mtab;
  92. struct mode_key_binding *mbind, mtmp;
  93. tablename = args_get(args, 't');
  94. if ((mtab = mode_key_findtable(tablename)) == NULL) {
  95. cmdq_error(cmdq, "unknown key table: %s", tablename);
  96. return (CMD_RETURN_ERROR);
  97. }
  98. if (key == KEYC_UNKNOWN) {
  99. while (!RB_EMPTY(mtab->tree)) {
  100. mbind = RB_ROOT(mtab->tree);
  101. RB_REMOVE(mode_key_tree, mtab->tree, mbind);
  102. free(mbind);
  103. }
  104. return (CMD_RETURN_NORMAL);
  105. }
  106. mtmp.key = key;
  107. mtmp.mode = !!args_has(args, 'c');
  108. if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
  109. RB_REMOVE(mode_key_tree, mtab->tree, mbind);
  110. free(mbind);
  111. }
  112. return (CMD_RETURN_NORMAL);
  113. }