source: keymap.c @ 4787581

release-1.10release-1.8release-1.9
Last change on this file since 4787581 was 47e0a6a, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Punt a number of g_new NULL checks, various minor cleanups No real need to check g_new's return value as it always succeeds. (Or destroys the universe^Wprocess in embarrassment.) Also make owl_global manage the lifetime of the kill buffer since it's part of owl_global now. And replace a g_new/strncpy with a g_strndup. It's shorter.
  • Property mode set to 100644
File size: 9.2 KB
RevLine 
[7d4fbcd]1#include <string.h>
2#include "owl.h"
3
[13ebf92]4static void _owl_keymap_format_bindings(const owl_keymap *km, owl_fmtext *fm);
[44cc9ab]5static void _owl_keymap_format_with_parents(const owl_keymap *km, owl_fmtext *fm);
[13ebf92]6
[7d4fbcd]7/* returns 0 on success */
[e19eb97]8int owl_keymap_init(owl_keymap *km, const char *name, const char *desc, void (*default_fn)(owl_input), void (*prealways_fn)(owl_input), void (*postalways_fn)(owl_input))
[cf83b7a]9{
[7d4fbcd]10  if (!name || !desc) return(-1);
[d4927a7]11  if ((km->name = g_strdup(name)) == NULL) return(-1);
12  if ((km->desc = g_strdup(desc)) == NULL) return(-1);
[7d4fbcd]13  if (0 != owl_list_create(&km->bindings)) return(-1);
[44cc9ab]14  km->parent = NULL;
[7d4fbcd]15  km->default_fn = default_fn;
16  km->prealways_fn = prealways_fn;
17  km->postalways_fn = postalways_fn;
18  return(0);
19}
20
21/* note that this will free the memory for the bindings! */
[0bdb3a6]22void owl_keymap_cleanup(owl_keymap *km)
[cf83b7a]23{
[ddbbcffa]24  g_free(km->name);
25  g_free(km->desc);
[920201c]26  owl_list_cleanup(&km->bindings, (void (*)(void *))owl_keybinding_delete);
[7d4fbcd]27}
28
[44cc9ab]29void owl_keymap_set_parent(owl_keymap *km, const owl_keymap *parent)
[cf83b7a]30{
[44cc9ab]31  km->parent = parent;
[7d4fbcd]32}
33
34/* creates and adds a key binding */
[e19eb97]35int owl_keymap_create_binding(owl_keymap *km, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
[cf83b7a]36{
[7d4fbcd]37  owl_keybinding *kb, *curkb;
38  int i;
39
[b13daa0]40  kb = owl_keybinding_new(keyseq, command, function_fn, desc);
41  if (kb == NULL)
42    return -1;
[7d4fbcd]43  /* see if another matching binding, and if so remove it.
44   * otherwise just add this one.
45   */
46  for (i = owl_list_get_size(&km->bindings)-1; i>=0; i--) {
[4d86e06]47    curkb = owl_list_get_element(&km->bindings, i);
[7d4fbcd]48    if (owl_keybinding_equal(curkb, kb)) {
49      owl_list_remove_element(&km->bindings, i);
[920201c]50      owl_keybinding_delete(curkb);
[7d4fbcd]51    }
52  }
53  return owl_list_append_element(&km->bindings, kb); 
[8a921b5]54
[7d4fbcd]55}
56
[8a921b5]57/* removes the binding associated with the keymap */
58int owl_keymap_remove_binding(owl_keymap *km, const char *keyseq)
59{
60  owl_keybinding *kb, *curkb;
61  int i;
62
[b13daa0]63  kb = owl_keybinding_new(keyseq, NULL, NULL, NULL);
64  if (kb == NULL)
65    return -1;
[8a921b5]66
67  for (i = owl_list_get_size(&km->bindings)-1; i >= 0; i--) {
68    curkb = owl_list_get_element(&km->bindings, i);
69    if (owl_keybinding_equal(curkb, kb)) {
70      owl_list_remove_element(&km->bindings, i);
71      owl_keybinding_delete(curkb);
[b13daa0]72      owl_keybinding_delete(kb);
[8a921b5]73      return(0);
74    }
75  }
[b13daa0]76  owl_keybinding_delete(kb);
[8a921b5]77  return(-2);
78}
79
80
[7d4fbcd]81/* returns a summary line describing this keymap.  the caller must free. */
[afa200a]82char *owl_keymap_summary(const owl_keymap *km)
[cf83b7a]83{
[7d4fbcd]84  if (!km || !km->name || !km->desc) return NULL;
[3472845]85  return g_strdup_printf("%-15s - %s", km->name, km->desc);
[7d4fbcd]86}
87
88/* Appends details about the keymap to fm */
[13ebf92]89void owl_keymap_get_details(const owl_keymap *km, owl_fmtext *fm, int recurse)
[cf83b7a]90{
[7d4fbcd]91  owl_fmtext_append_bold(fm, "KEYMAP - ");
92  owl_fmtext_append_bold(fm, km->name);
93  owl_fmtext_append_normal(fm, "\n");
94  if (km->desc) {
95    owl_fmtext_append_normal(fm, OWL_TABSTR "Purpose:    ");
96    owl_fmtext_append_normal(fm, km->desc);
97    owl_fmtext_append_normal(fm, "\n");
98  }
[44cc9ab]99  if (km->parent) {
100    owl_fmtext_append_normal(fm, OWL_TABSTR "Has parent: ");
101    owl_fmtext_append_normal(fm, km->parent->name);
[7d4fbcd]102    owl_fmtext_append_normal(fm, "\n");
103  }
104    owl_fmtext_append_normal(fm, "\n");
105  if (km->default_fn) {
106    owl_fmtext_append_normal(fm, OWL_TABSTR
107     "Has a default keypress handler (default_fn).\n");
108  }
109  if (km->prealways_fn) {
110    owl_fmtext_append_normal(fm, OWL_TABSTR
111     "Executes a function (prealways_fn) on every keypress.\n");
112  }
113  if (km->postalways_fn) {
114    owl_fmtext_append_normal(fm, OWL_TABSTR
115     "Executes a function (postalways_fn) after handling every keypress.\n");
116  }
117
118  owl_fmtext_append_bold(fm, "\nKey bindings:\n\n"); 
[13ebf92]119  if (recurse) {
[44cc9ab]120    _owl_keymap_format_with_parents(km, fm);
[13ebf92]121  } else {
122    _owl_keymap_format_bindings(km, fm);
123  }
124}
125
[44cc9ab]126static void _owl_keymap_format_with_parents(const owl_keymap *km, owl_fmtext *fm)
[13ebf92]127{
128  while (km) {
129    _owl_keymap_format_bindings(km, fm);
[44cc9ab]130    km = km->parent;
[13ebf92]131    if (km) {
132      owl_fmtext_append_bold(fm, "\nInherited from ");
133      owl_fmtext_append_bold(fm, km->name);
134      owl_fmtext_append_bold(fm, ":\n\n");
135    }
136  }
137}
138
139static void _owl_keymap_format_bindings(const owl_keymap *km, owl_fmtext *fm)
140{
141  int i, nbindings;
142  const owl_keybinding *kb;
143 
[7d4fbcd]144  nbindings = owl_list_get_size(&km->bindings);
145  for (i=0; i<nbindings; i++) {
[45e2c95]146    char *kbstr;
[0a0fb74]147    const owl_cmd *cmd;
[e19eb97]148    const char *tmpdesc, *desc = "";
[7d4fbcd]149
150    kb = owl_list_get_element(&km->bindings, i);
[45e2c95]151    kbstr = owl_keybinding_tostring(kb);
[7d4fbcd]152    owl_fmtext_append_normal(fm, OWL_TABSTR);
[45e2c95]153    owl_fmtext_append_normal(fm, kbstr);
154    owl_fmtext_append_spaces(fm, 11-strlen(kbstr));
155    g_free(kbstr);
[7d4fbcd]156    owl_fmtext_append_normal(fm, " - ");
157    if (kb->desc && *kb->desc) {
158      desc = kb->desc;
159    } else if ((cmd=owl_function_get_cmd(kb->command))
160               && (tmpdesc = owl_cmd_get_summary(cmd))) {
161      desc = tmpdesc;
162    }
163    owl_fmtext_append_normal(fm, desc);
164    if (kb->command && *(kb->command)) {
165      owl_fmtext_append_normal(fm, "   [");
166      owl_fmtext_append_normal(fm, kb->command);
167      owl_fmtext_append_normal(fm, "]");
168    } 
169    owl_fmtext_append_normal(fm, "\n");
170  }
171}
172
173
174
175/***********************************************************************/
176/***************************** KEYHANDLER ******************************/
177/***********************************************************************/
178
179/* NOTE: keyhandler has private access to the internals of keymap */
180
[cf83b7a]181int owl_keyhandler_init(owl_keyhandler *kh)
182{
[7d4fbcd]183  if (0 != owl_dict_create(&kh->keymaps)) return(-1); 
184  kh->active = NULL;
185  owl_keyhandler_reset(kh);
186  return(0);
187}
188
189/* adds a new keymap */
[cf83b7a]190void owl_keyhandler_add_keymap(owl_keyhandler *kh, owl_keymap *km)
191{
[7d4fbcd]192  owl_dict_insert_element(&kh->keymaps, km->name, km, NULL);
193}
194
[e19eb97]195owl_keymap *owl_keyhandler_create_and_add_keymap(owl_keyhandler *kh, const char *name, const char *desc, void (*default_fn)(owl_input), void (*prealways_fn)(owl_input), void (*postalways_fn)(owl_input))
[cf83b7a]196{
[7d4fbcd]197  owl_keymap *km;
[96828e4]198  km = g_new(owl_keymap, 1);
[7d4fbcd]199  owl_keymap_init(km, name, desc, default_fn, prealways_fn, postalways_fn);
200  owl_keyhandler_add_keymap(kh, km);
201  return km;
202}
203
204/* resets state and clears out key stack */
[cf83b7a]205void owl_keyhandler_reset(owl_keyhandler *kh)
206{
[7d4fbcd]207  kh->in_esc = 0;
208  memset(kh->kpstack, 0, (OWL_KEYMAP_MAXSTACK+1)*sizeof(int));
209  kh->kpstackpos = -1;
210}
211
[12bc46a]212owl_keymap *owl_keyhandler_get_keymap(const owl_keyhandler *kh, const char *mapname)
[cf83b7a]213{
[4d86e06]214  return owl_dict_find_element(&kh->keymaps, mapname);
[7d4fbcd]215}
216
[12bc46a]217void owl_keyhandler_get_keymap_names(const owl_keyhandler *kh, owl_list *l)
[cf83b7a]218{
[7d4fbcd]219  owl_dict_get_keys(&kh->keymaps, l);
220}
221
222
223/* sets the active keymap, which will also reset any key state.
224 * returns the new keymap, or NULL on failure. */
[afa200a]225const owl_keymap *owl_keyhandler_activate(owl_keyhandler *kh, const char *mapname)
[cf83b7a]226{
[afa200a]227  const owl_keymap *km;
[7d4fbcd]228  if (kh->active && !strcmp(mapname, kh->active->name)) return(kh->active);
[4d86e06]229  km = owl_dict_find_element(&kh->keymaps, mapname);
[7d4fbcd]230  if (!km) return(NULL);
231  owl_keyhandler_reset(kh);
232  kh->active = km;
233  return(km);
234}
235
236/* processes a keypress.  returns 0 if the keypress was handled,
237 * 1 if not handled, -1 on error, and -2 if j==ERR. */
[428834d]238int owl_keyhandler_process(owl_keyhandler *kh, owl_input j)
[cf83b7a]239{
[afa200a]240  const owl_keymap     *km;
[f1d7d0f]241  const owl_keybinding *kb;
[7d4fbcd]242  int i, match;
243
244  if (!kh->active) {
245    owl_function_makemsg("No active keymap!!!");
246    return(-1);
247  }
[7131cf2]248
[7d4fbcd]249  /* deal with ESC prefixing */
[428834d]250  if (!kh->in_esc && j.ch == 27) {
[7d4fbcd]251    kh->in_esc = 1;
252    return(0);
253  }
254  if (kh->in_esc) {
[428834d]255    j.ch = OWL_META(j.ch);
[7d4fbcd]256    kh->in_esc = 0;
257  }
258 
[428834d]259  kh->kpstack[++(kh->kpstackpos)] = j.ch;
[7d4fbcd]260  if (kh->kpstackpos >= OWL_KEYMAP_MAXSTACK) {
261    owl_keyhandler_reset(kh);
262    owl_function_makemsg("Too many prefix keys pressed...");
263    return(-1);
264  }
265
[44cc9ab]266  /* deal with the always_fn for the map and parents */
267  for (km=kh->active; km; km=km->parent) {
[7d4fbcd]268    if (km->prealways_fn) {
269      km->prealways_fn(j);
270    }
271  }
272
273  /* search for a match.  goes through active keymap and then
[44cc9ab]274   * through parents... TODO:  clean this up so we can pull
[7d4fbcd]275   * keyhandler and keymap apart.  */
[44cc9ab]276  for (km=kh->active; km; km=km->parent) {
[7d4fbcd]277    for (i=owl_list_get_size(&km->bindings)-1; i>=0; i--) {
[4d86e06]278      kb = owl_list_get_element(&km->bindings, i);
[e1b136bf]279      match = owl_keybinding_match(kb, kh);
[7d4fbcd]280      if (match == 1) {         /* subset match */
[10b866d]281
282        /* owl_function_debugmsg("processkey: found subset match in %s", km->name); */
[7d4fbcd]283        return(0);
284      } else if (match == 2) {  /* exact match */
[10b866d]285        /* owl_function_debugmsg("processkey: found exact match in %s", km->name); */
[428834d]286        owl_keybinding_execute(kb, j.ch);
[7d4fbcd]287        owl_keyhandler_reset(kh);
288        if (km->postalways_fn) {
289          km->postalways_fn(j);
290        }
291        return(0);
292      }
293    }
294  }
295
296  /* see if a default action exists for the active keymap */
297  if (kh->active->default_fn && kh->kpstackpos<1) {
[10b866d]298    /*owl_function_debugmsg("processkey: executing default_fn");*/
299
[7d4fbcd]300    kh->active->default_fn(j);
301    owl_keyhandler_reset(kh);
302    if (kh->active->postalways_fn) {
303      kh->active->postalways_fn(j);
304    }
305    return(0);
306  }
307
308  owl_keyhandler_invalidkey(kh);
309  /* unable to handle */
310  return(1); 
311}
312
[cf83b7a]313void owl_keyhandler_invalidkey(owl_keyhandler *kh)
314{
[45e2c95]315    char *kbbuff = owl_keybinding_stack_tostring(kh->kpstack, kh->kpstackpos+1);
[7d4fbcd]316    owl_function_makemsg("'%s' is not a valid key in this context.", kbbuff);
[45e2c95]317    g_free(kbbuff);
[7d4fbcd]318    owl_keyhandler_reset(kh);
319}
Note: See TracBrowser for help on using the repository browser.