Changeset e1b136bf for keybinding.c


Ignore:
Timestamp:
May 11, 2009, 9:50:58 PM (12 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
70110286, dfaa47d
Parents:
fa8f439
Message:
Fix handling of C-SPACE, etc.

Instead of storing key sequences NULL-terminated, store an explicit
length everywhere, so that we can handle C-SPACE (which is a 0).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • keybinding.c

    r1716fed re1b136bf  
    3434    return(-1);
    3535  }
    36   kb->j = owl_malloc((nktokens+1)*sizeof(int));
     36  kb->keys = owl_malloc(nktokens*sizeof(int));
    3737  for (i=0; i<nktokens; i++) {
    38     kb->j[i] = owl_keypress_fromstring(ktokens[i]);
    39     if (kb->j[i] == ERR) {
     38    kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
     39    if (kb->keys[i] == ERR) {
    4040      atokenize_free(ktokens, nktokens);
    41       owl_free(kb->j);
     41      owl_free(kb->keys);
    4242      return(-1);
    4343    }
    4444  }
    45   kb->j[i] = 0;
     45  kb->len = nktokens;
    4646
    4747  atokenize_free(ktokens, nktokens);
     
    5757void owl_keybinding_free(owl_keybinding *kb)
    5858{
    59   if (kb->j) owl_free(kb->j);
     59  if (kb->keys) owl_free(kb->keys);
    6060  if (kb->desc) owl_free(kb->desc);
    6161  if (kb->command) owl_free(kb->command);
     
    8080
    8181/* returns 0 on success */
    82 int owl_keybinding_stack_tostring(int *j, char *buff, int bufflen)
     82int owl_keybinding_stack_tostring(int *j, int len, char *buff, int bufflen)
    8383{
    8484  char *pos = buff;
     
    8686  int   i, n;
    8787
    88   for (i=0; j[i]; i++) {
     88  for (i=0; i < len; i++) {
    8989    owl_keypress_tostring(j[i], 0, pos, rem-1);
    90     if (j[i+1]) strcat(pos, " ");
     90    if (i < len - 1) strcat(pos, " ");
    9191    n = strlen(pos);
    9292    pos += n;
     
    9999int owl_keybinding_tostring(owl_keybinding *kb, char *buff, int bufflen)
    100100{
    101   return owl_keybinding_stack_tostring(kb->j, buff, bufflen);
     101  return owl_keybinding_stack_tostring(kb->keys, kb->len, buff, bufflen);
    102102}
    103103
     
    108108
    109109/* returns 0 on no match, 1 on subset match, and 2 on complete match */
    110 int owl_keybinding_match(owl_keybinding *kb, int *kpstack)
     110int owl_keybinding_match(owl_keybinding *kb, owl_keyhandler *kh)
    111111{
    112   int *kbstack = kb->j;
    113  
    114   while (*kbstack && *kpstack) {
    115     if (*kbstack != *kpstack) {
     112  int i;
     113  for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) {
     114    if(kb->keys[i] != kh->kpstack[i])
    116115      return 0;
    117     }
    118     kbstack++; kpstack++;
    119116  }
    120   if (!*kpstack && !*kbstack) {
     117
     118  /* If we've made it to this point, then they match as far as they are. */
     119  if(kb->len == kh->kpstackpos + 1) {
     120    /* Equal length */
    121121    return 2;
    122   } else if (!*kpstack) {
     122  } else if(kb->len > kh->kpstackpos + 1) {
    123123    return 1;
    124   } else {
    125     return 0;
    126124  }
     125
     126  return 0;
    127127}
    128128
     
    130130int owl_keybinding_equal(owl_keybinding *kb1, owl_keybinding *kb2)
    131131{
    132   int *j1 = kb1->j;
    133   int *j2 = kb2->j;
    134   while (*j1 && *j2) {
    135     if (*(j1++) != *(j2++))  return(0);
     132  int i;
     133
     134  if(kb1->len != kb2->len) return 0;
     135
     136  for(i = 0; i < kb1->len; i++) {
     137    if(kb1->keys[i] != kb2->keys[i])
     138      return 0;
    136139  }
    137   if (*j1 != *j2) return(0);
    138   return(1);
     140
     141  return 1;
    139142}
Note: See TracChangeset for help on using the changeset viewer.