Size on split-window is -l not -s. Doh.
[tmux-openbsd.git] / xterm-keys.c
blob1e5583f7fc7bd644eaf3177cecd2bcd93753778e
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <string.h>
23 #include "tmux.h"
26 * xterm-style function keys append one of the following values before the last
27 * character:
29 * 2 Shift
30 * 3 Alt
31 * 4 Shift + Alt
32 * 5 Ctrl
33 * 6 Shift + Ctrl
34 * 7 Alt + Ctrl
35 * 8 Shift + Alt + Ctrl
37 * Rather than parsing them, just match against a table.
39 * There are two forms for F1-F4 (\\033O_P or \\033[1;_P). We accept either but
40 * always output the latter (it comes first in the table).
43 int xterm_keys_match(const char *, const char *, size_t);
44 int xterm_keys_modifiers(const char *, const char *, size_t);
46 struct xterm_keys_entry {
47 int key;
48 const char *template;
51 const struct xterm_keys_entry xterm_keys_table[] = {
52 { KEYC_F1, "\033[1;_P" },
53 { KEYC_F1, "\033O_P" },
54 { KEYC_F2, "\033[1;_Q" },
55 { KEYC_F2, "\033O_Q" },
56 { KEYC_F3, "\033[1;_R" },
57 { KEYC_F3, "\033O_R" },
58 { KEYC_F4, "\033[1;_S" },
59 { KEYC_F4, "\033O_S" },
60 { KEYC_F5, "\033[15;_~" },
61 { KEYC_F6, "\033[17;_~" },
62 { KEYC_F7, "\033[18;_~" },
63 { KEYC_F8, "\033[19;_~" },
64 { KEYC_F9, "\033[20;_~" },
65 { KEYC_F10, "\033[21;_~" },
66 { KEYC_F11, "\033[23;_~" },
67 { KEYC_F12, "\033[24;_~" },
68 { KEYC_F13, "\033[25;_~" },
69 { KEYC_F14, "\033[26;_~" },
70 { KEYC_F15, "\033[28;_~" },
71 { KEYC_F16, "\033[29;_~" },
72 { KEYC_F17, "\033[31;_~" },
73 { KEYC_F18, "\033[32;_~" },
74 { KEYC_F19, "\033[33;_~" },
75 { KEYC_F20, "\033[34;_~" },
76 { KEYC_UP, "\033[1;_A" },
77 { KEYC_DOWN, "\033[1;_B" },
78 { KEYC_RIGHT, "\033[1;_C" },
79 { KEYC_LEFT, "\033[1;_D" },
80 { KEYC_HOME, "\033[1;_H" },
81 { KEYC_END, "\033[1;_F" },
82 { KEYC_PPAGE, "\033[5;_~" },
83 { KEYC_NPAGE, "\033[6;_~" },
84 { KEYC_IC, "\033[2;_~" },
85 { KEYC_DC, "\033[3;_~" },
89 * Match key against buffer, treating _ as a wildcard. Return -1 for no match,
90 * 0 for match, 1 if the end of the buffer is reached (need more data).
92 int
93 xterm_keys_match(const char *template, const char *buf, size_t len)
95 size_t pos;
97 if (len == 0)
98 return (0);
100 pos = 0;
101 do {
102 if (*template != '_' && buf[pos] != *template)
103 return (-1);
104 } while (pos++ != len && *++template != '\0');
106 if (*template != '\0') /* partial */
107 return (1);
109 return (0);
112 /* Find modifiers based on template. */
114 xterm_keys_modifiers(const char *template, const char *buf, size_t len)
116 size_t idx;
117 int param, modifiers;
119 idx = strcspn(template, "_");
120 if (idx >= len)
121 return (0);
122 param = buf[idx] - '1';
124 modifiers = 0;
125 if (param & 1)
126 modifiers |= KEYC_SHIFT;
127 if (param & 2)
128 modifiers |= KEYC_ESCAPE;
129 if (param & 4)
130 modifiers |= KEYC_CTRL;
131 if (param & 8)
132 modifiers |= KEYC_ESCAPE;
133 return (modifiers);
137 * Lookup key from a buffer against the table. Returns 0 for found (and the
138 * key), -1 for not found, 1 for partial match.
141 xterm_keys_find(const char *buf, size_t len, size_t *size, int *key)
143 const struct xterm_keys_entry *entry;
144 u_int i;
146 for (i = 0; i < nitems(xterm_keys_table); i++) {
147 entry = &xterm_keys_table[i];
148 switch (xterm_keys_match(entry->template, buf, len)) {
149 case 0:
150 *size = strlen(entry->template);
151 *key = entry->key;
152 *key |= xterm_keys_modifiers(entry->template, buf, len);
153 return (0);
154 case 1:
155 return (1);
158 return (-1);
161 /* Lookup a key number from the table. */
162 char *
163 xterm_keys_lookup(int key)
165 const struct xterm_keys_entry *entry;
166 u_int i;
167 int modifiers;
168 char *out;
170 modifiers = 1;
171 if (key & KEYC_SHIFT)
172 modifiers += 1;
173 if (key & KEYC_ESCAPE)
174 modifiers += 2;
175 if (key & KEYC_CTRL)
176 modifiers += 4;
177 if (key & KEYC_ESCAPE)
178 modifiers += 8;
181 * If the key has no modifiers, return NULL and let it fall through to
182 * the normal lookup.
184 if (modifiers == 1)
185 return (NULL);
187 /* Otherwise, find the key in the table. */
188 key &= ~(KEYC_SHIFT|KEYC_ESCAPE|KEYC_CTRL);
189 for (i = 0; i < nitems(xterm_keys_table); i++) {
190 entry = &xterm_keys_table[i];
191 if (key == entry->key)
192 break;
194 if (i == nitems(xterm_keys_table))
195 return (NULL);
197 /* Copy the template and replace the modifier. */
198 out = xstrdup(entry->template);
199 out[strcspn(out, "_")] = '0' + modifiers;
200 return (out);