cmd-list-keys.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <string.h>
  20. #include "tmux.h"
  21. /*
  22. * List key bindings.
  23. */
  24. enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmd_q *);
  25. enum cmd_retval cmd_list_keys_table(struct cmd *, struct cmd_q *);
  26. enum cmd_retval cmd_list_keys_commands(struct cmd_q *);
  27. const struct cmd_entry cmd_list_keys_entry = {
  28. .name = "list-keys",
  29. .alias = "lsk",
  30. .args = { "t:T:", 0, 0 },
  31. .usage = "[-t mode-table] [-T key-table]",
  32. .flags = CMD_STARTSERVER,
  33. .exec = cmd_list_keys_exec
  34. };
  35. const struct cmd_entry cmd_list_commands_entry = {
  36. .name = "list-commands",
  37. .alias = "lscm",
  38. .args = { "", 0, 0 },
  39. .usage = "",
  40. .flags = CMD_STARTSERVER,
  41. .exec = cmd_list_keys_exec
  42. };
  43. enum cmd_retval
  44. cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq)
  45. {
  46. struct args *args = self->args;
  47. struct key_table *table;
  48. struct key_binding *bd;
  49. const char *key, *tablename, *r;
  50. char *cp, tmp[BUFSIZ];
  51. int repeat, width, tablewidth, keywidth;
  52. if (self->entry == &cmd_list_commands_entry)
  53. return (cmd_list_keys_commands(cmdq));
  54. #ifdef TMATE
  55. /* XXX TODO Really nasty hack, we really need our own client instance... */
  56. struct client fake_client;
  57. if (!cmdq->client) {
  58. cmdq->client = &fake_client;
  59. cmdq->client->flags = 0;
  60. cmdq->client->session = RB_MIN(sessions, &sessions);
  61. }
  62. #endif
  63. if (args_has(args, 't'))
  64. return (cmd_list_keys_table(self, cmdq));
  65. tablename = args_get(args, 'T');
  66. if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
  67. cmdq_error(cmdq, "table %s doesn't exist", tablename);
  68. return (CMD_RETURN_ERROR);
  69. }
  70. repeat = 0;
  71. tablewidth = keywidth = 0;
  72. RB_FOREACH(table, key_tables, &key_tables) {
  73. if (tablename != NULL && strcmp(table->name, tablename) != 0)
  74. continue;
  75. RB_FOREACH(bd, key_bindings, &table->key_bindings) {
  76. key = key_string_lookup_key(bd->key);
  77. if (bd->can_repeat)
  78. repeat = 1;
  79. width = utf8_cstrwidth(table->name);
  80. if (width > tablewidth)
  81. tablewidth = width;
  82. width = utf8_cstrwidth(key);
  83. if (width > keywidth)
  84. keywidth = width;
  85. }
  86. }
  87. RB_FOREACH(table, key_tables, &key_tables) {
  88. if (tablename != NULL && strcmp(table->name, tablename) != 0)
  89. continue;
  90. RB_FOREACH(bd, key_bindings, &table->key_bindings) {
  91. key = key_string_lookup_key(bd->key);
  92. if (!repeat)
  93. r = "";
  94. else if (bd->can_repeat)
  95. r = "-r ";
  96. else
  97. r = " ";
  98. xsnprintf(tmp, sizeof tmp, "%s-T ", r);
  99. cp = utf8_padcstr(table->name, tablewidth);
  100. strlcat(tmp, cp, sizeof tmp);
  101. strlcat(tmp, " ", sizeof tmp);
  102. free(cp);
  103. cp = utf8_padcstr(key, keywidth);
  104. strlcat(tmp, cp, sizeof tmp);
  105. strlcat(tmp, " ", sizeof tmp);
  106. free(cp);
  107. cp = cmd_list_print(bd->cmdlist);
  108. strlcat(tmp, cp, sizeof tmp);
  109. free(cp);
  110. cmdq_print(cmdq, "bind-key %s", tmp);
  111. }
  112. }
  113. return (CMD_RETURN_NORMAL);
  114. }
  115. enum cmd_retval
  116. cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq)
  117. {
  118. struct args *args = self->args;
  119. const char *tablename;
  120. const struct mode_key_table *mtab;
  121. struct mode_key_binding *mbind;
  122. const char *key, *cmdstr, *mode;
  123. int width, keywidth, any_mode;
  124. tablename = args_get(args, 't');
  125. if ((mtab = mode_key_findtable(tablename)) == NULL) {
  126. cmdq_error(cmdq, "unknown key table: %s", tablename);
  127. return (CMD_RETURN_ERROR);
  128. }
  129. width = 0;
  130. any_mode = 0;
  131. RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
  132. key = key_string_lookup_key(mbind->key);
  133. if (mbind->mode != 0)
  134. any_mode = 1;
  135. keywidth = strlen(key);
  136. if (keywidth > width)
  137. width = keywidth;
  138. }
  139. RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
  140. key = key_string_lookup_key(mbind->key);
  141. mode = "";
  142. if (mbind->mode != 0)
  143. mode = "c";
  144. cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
  145. if (cmdstr != NULL) {
  146. cmdq_print(cmdq, "bind-key -%st %s%s %*s %s%s%s%s",
  147. mode, any_mode && *mode == '\0' ? " " : "",
  148. mtab->name, (int) width, key, cmdstr,
  149. mbind->arg != NULL ? " \"" : "",
  150. mbind->arg != NULL ? mbind->arg : "",
  151. mbind->arg != NULL ? "\"": "");
  152. }
  153. }
  154. return (CMD_RETURN_NORMAL);
  155. }
  156. enum cmd_retval
  157. cmd_list_keys_commands(struct cmd_q *cmdq)
  158. {
  159. const struct cmd_entry **entryp;
  160. const struct cmd_entry *entry;
  161. for (entryp = cmd_table; *entryp != NULL; entryp++) {
  162. entry = *entryp;
  163. if (entry->alias == NULL) {
  164. cmdq_print(cmdq, "%s %s", entry->name, entry->usage);
  165. continue;
  166. }
  167. cmdq_print(cmdq, "%s (%s) %s", entry->name, entry->alias,
  168. entry->usage);
  169. }
  170. return (CMD_RETURN_NORMAL);
  171. }