map.c 10.9 KB
Newer Older
1 2 3 4 5 6
/* map.c	8/19/1995
 * Map some key-sequences to some key-sequences, version 0.3
 * Requieres: tio.o
 */

/* Copyright (c) 1995,1996 Sascha Demetrio
7
 * Copyright (c) 2009, 2015 Peter Pentchev
8 9 10 11 12 13 14
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
15
 *    If you modify any part of HEXER and redistribute it, you must add
16 17 18 19 20 21
 *    a notice to the `README' file and the modified source files containing
 *    information about the  changes you made.  I do not want to take
 *    credit or be blamed for your modifications.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
22
 *    If you modify any part of HEXER and redistribute it in binary form,
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *    you must supply a `README' file containing information about the
 *    changes you made.
 * 3. The name of the developer may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * HEXER WAS DEVELOPED BY SASCHA DEMETRIO.
 * THIS SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
 * NOT MAKE USE OF THIS WORK.
 *
 * DISCLAIMER:
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "config.h"

50
#include <alloca.h>
51 52 53 54
#include <stdlib.h>
#include <string.h>
#include <assert.h>

55 56
#include "buffer.h"
#include "hexer.h"
57
#include "util.h"
58 59 60 61 62 63 64 65

#undef TIO_MAP
#define TIO_MAP 1
#include "tio.h"

#define MAP_MAXLEN 256
#define MAP_MAX 16

66
#define MAP_ESC ('\033')
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

int tio_m_remap = 0;
  /* if `tio_m_remap == 1', the sequence of keys another sequence is mapped
   * to may be mapped again, i.e. if `ab' maps to `bcd' and `bc' is mapped
   * to `cd', then `ab' maps to `cdd'.  the default is *not* to allow
   * remapping.
   */

struct map_s {
  int from[MAP_MAXLEN];
  int to[MAP_MAXLEN];
  struct map_s *next;
};

static struct map_s *map_first[MAP_MAX] = { 0 };
static int map_max = 0;
static int keys[MAP_MAXLEN];
static int keys_n = 0;

static int munget[MAP_MAXLEN];
static int munget_n = 0;


/* operations on strings of keys
 */

  static int
94
key_strcmp(const int *ks1, const int *ks2)
95 96 97 98 99 100 101
{
  while (*ks1 && *ks2 && *ks1 == *ks2) ++ks1, ++ks2;
  return *ks1 != *ks2;
}
/* key_strcmp */

  static int
102
key_strncmp(const int *ks1, const int *ks2, int n)
103 104 105 106 107 108 109
{
  while (*ks1 && *ks2 && *ks1 == *ks2 && n--) ++ks1, ++ks2;
  return n && *ks1 != *ks2;
}
/* key_strncmp */

  static char *
110
key_strrep(const int *ks)
111 112 113 114 115 116 117 118
{
  int n;
  const int *i;
  char *s, *t;
  int key;

  for (n = 0, i = ks; *i; ++i) {
    if (*i == MAP_ESC)
119
      key = *++i + HXKEY_BIAS;
120 121 122 123
    else
      key = *i;
    n += strlen(tio_keyrep(key));
  }
124
  s = t = (char *)malloc_fatal(n + 1);
125 126
  for (i = ks; *i; ++i) {
    if (*i == MAP_ESC)
127
      key = *++i + HXKEY_BIAS;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    else
      key = *i;
    strcpy(t, tio_keyrep(key)); t += strlen(t);
  }
  return s;
}
/* key_strrep */

#if 0
  static char *
key_strrep_simple(ks)
  const int *ks;
  /* like `key_strrep()', but special keys are translated to 0xff.
   */
{
  char *s, *t;

145
  s = t = (char *)malloc_fatal(key_strlen(ks) + 1);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  for (; *ks; ++ks) if (*ks < 0x100) *t++ = *ks; else *t++ = 0xff;
  *t = 0;
  return s;
}
/* key_strrep_simple */
#endif

#if 0
  static int
key_strncpy(ks1, ks2, n)
  int *ks1;
  const int *ks2;
  int n;
{
  while (*ks2 && n--) *ks1++ = *ks2++;
  return *ks1 = 0;
}
/* key_strncpy */
#endif

  static int
167
key_strcpy(int *ks1, const int *ks2)
168 169 170 171 172 173 174
{
  while (*ks2) *ks1++ = *ks2++;
  return *ks1 = 0;
}
/* key_strcpy */

  static int
175
string_to_keys(int *kkeys, char *string)
176 177 178 179 180
{
  while (*string) {
    if (*string == MAP_ESC) {
      ++string;
      if (*string == 1)
181
        *kkeys++ = (int)HXKEY_ESCAPE;
182
      else
183
        *kkeys++ = *string + (int)HXKEY_BIAS;
184
    } else
185
      *kkeys++ = *string;
186 187
    ++string;
  }
188
  *kkeys = 0;
189 190 191 192 193
  return 0;
}
/* string_to_keys */

  static int
194
scan_keys(int *kkeys, char *string, int mode)
195 196 197 198 199
{
  do {
    if (*string == MAP_ESC) {
      ++string;
      if (*string == 1)
200
        *kkeys++ = HXKEY_ESCAPE;
201
      else
202
        *kkeys++ = *string + HXKEY_BIAS;
203 204
      ++string;
    } else
205
      string = tio_keyscan(kkeys++, string, mode);
206
  } while (*string);
207
  *kkeys = 0;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  return 0;
}
/* scan_keys */

#if 0
  static void
mungetch(key)
  int key;
{
  if (tio_m_remap) tio_ungetch(key); else munget[munget_n++] = key;
}
/* mungetch */
#endif

  static void
223
mungets(int *kkeys)
224 225 226
{
  int i;

227
  for (i = 0; kkeys[i]; ++i);
228
  if (tio_m_remap)
229
    while (i) tio_ungetch(kkeys[--i]);
230
  else
231
    while (i) munget[munget_n++] = kkeys[--i];
232 233 234 235
}
/* mungets */

  int
236
tio_mgetch(int map, char *map_string)
237 238 239 240 241 242 243 244 245 246
{
  int key;
  struct map_s *i;
  char *s = map_string;
  int ungets[MAP_MAXLEN], ungets_n = 0;
  int k;
  int prefix;

  if (*tio_interrupt) {
    *tio_interrupt = 0;
247
    return (int)HXKEY_BREAK;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  }
  while (map_max < map) map_first[++map_max] = 0;
  if (map_string) {
    /* create the `map_string' (a string representation of the unmapped keys
     * typed so far)  */
    if (keys_n)
      for (k = 0; k < keys_n; ++k) {
        strcpy(s, tio_keyrep(keys[k]));
        s += strlen(s);
      }
    else
      *map_string = 0;
  }
  if (tio_getmore()) return tio_getch();
  if (munget_n) return munget[--munget_n];
  key = tio_getch();
264 265 266
  if (key == (int)HXKEY_ERROR) return (int)HXKEY_ERROR;
  if (key == (int)HXKEY_ESCAPE && !keys_n)
    if (!tio_readmore()) return (int)HXKEY_ESCAPE;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  if (key <= 0) return key;
  keys[keys_n++] = key;
  keys[keys_n] = 0;
  for (;;) {
    /* check if `keys' matches */
    for (i = map_first[map]; i; i = i->next)
      if (!key_strcmp(keys, i->from)) { /* match */
        if (ungets_n) {
          ungets[ungets_n] = 0;
          mungets(ungets);
          ungets[ungets_n = 0] = 0;
        }
        keys[keys_n = 0] = 0;
        mungets(i->to);
        if (map_string) *map_string = 0;
        return tio_mgetch(map, map_string);
      }
    /* check if `keys' is a prefix */
    for (i = map_first[map], prefix = 0; i; i = i->next)
      if (!key_strncmp(keys, i->from, keys_n)) { prefix = 1; break; }
    if (!prefix) {
      ungets[ungets_n++] = keys[0];
      if (!--keys_n) break;
      for (k = 0; k < keys_n; ++k) keys[k] = keys[k + 1];
      continue;
    } else
      break;
  }
  if (ungets_n) {
    ungets[ungets_n] = 0;
    mungets(ungets);
    ungets[ungets_n = 0] = 0;
  }
  if (s) {
    strcpy(s, tio_keyrep(key));
    s += strlen(s);
  }
  if (munget_n) return munget[--munget_n];
305
  return (int)HXKEY_NONE;
306 307 308 309
}
/* tio_mgetch */

  int
310
tio_map(int map, char *from, char *to, int special_f)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  /* define a key mapping from `from' to `to' in keymap `map'.  if `special_f'
   * is set, special characters may also be written in ascii (i.e. "~UP" for
   * cursor up).  `special_f' should be set when reading commands from a file.
   */
{
  struct map_s *i, *j;
  int *kfrom, *kto;

  kfrom = (int *)alloca((strlen(from) + 1) * sizeof(int));
  if (special_f)
    scan_keys(kfrom, from, 0);
  else
    string_to_keys(kfrom, from);
  kto = (int *)alloca((strlen(to) + 1) * sizeof(int));
  if (special_f)
    scan_keys(kto, to, 0);
  else
    string_to_keys(kto, to);
  while (map_max < map) map_first[++map_max] = 0;
  for (i = map_first[map], j = 0; i; j = i, i = i->next)
    if (!key_strcmp(kfrom, i->from)) {
      key_strcpy(i->to, kto);
      return 0;
    }
335
  i = (struct map_s *)malloc_fatal(sizeof(struct map_s));
336 337 338 339 340 341 342 343 344
  i->next = 0;
  key_strcpy(i->from, kfrom);
  key_strcpy(i->to, kto);
  if (j) j->next = i; else map_first[map] = i;
  return 0;
}
/* tio_map */

  int
345
tio_unmap(int map, char *from, int special_f)
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  /* remove the mapping `from' from the keymap `map'.  the flag `special_f'
   * works the same as in `tio_map()'.
   */
{
  struct map_s *i, *j;
  int *kfrom;

  kfrom = (int *)alloca((strlen(from) + 1) * sizeof(int));
  if (special_f)
    scan_keys(kfrom, from, 0);
  else
    string_to_keys(kfrom, from);
  if (map > map_max) return -1;
  for (i = map_first[map], j = 0; i; j = i, i = i->next)
    if (!key_strcmp(i->from, kfrom)) {
      if (j) j->next = i->next; else map_first[map] = i->next;
      free((char *)i);
      return 0;
    }
  return -1;
}
/* tio_unmap */

  char **
370
tio_maplist(int map)
371
{
372
  unsigned n, k;
373 374
  struct map_s *i;
  char **from, **to, **list;
375
  size_t from_maxwidth;
376 377 378

  for (i = map_first[map], n = 0; i; i = i->next, ++n);
  if (!n) {
379
    *(list = (char **)malloc_fatal(sizeof(char *))) = 0;
380 381 382 383
    return list;
  }
  from = (char **)alloca(n * sizeof(char *));
  to = (char **)alloca(n * sizeof(char *));
384
  list = (char **)malloc_fatal((n + 1) * sizeof(char *));
385 386 387 388 389 390 391 392 393
  for (i = map_first[map], k = 0; i; i = i->next, ++k) {
    from[k] = key_strrep(i->from);
    to[k] = key_strrep(i->to);
  }
  from_maxwidth = strlen(from[0]);
  for (k = 1; k < n; ++k)
    if (strlen(from[k]) > from_maxwidth) from_maxwidth = strlen(from[k]);
  from_maxwidth += 2;
  for (k = 0; k < n; ++k) {
394
    list[k] = (char *)malloc_fatal(from_maxwidth + strlen(to[k]) + 2);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    strcpy(list[k], from[k]);
    memset(list[k] + strlen(list[k]), ' ', from_maxwidth - strlen(list[k]));
    strcpy(list[k] + from_maxwidth, to[k]);
    list[k][strlen(list[k]) + 1] = 0;
    list[k][strlen(list[k])] = '\n';
    free((char *)from[k]);
    free((char *)to[k]);
  }
  list[n] = 0;
  return list;
}
/* tio_maplist */

#if 0
  char *
tio_mapentry(map, from)
  int map;
  char *from;
{
  struct map_s *i, *j;
  int *kfrom;
  int length;
  char *rep, *sfrom, *sto;

  kfrom = (int *)alloca(strlen(from) * sizeof(int));
  string_to_keys(kfrom, from);
  if (map > map_max) return -1;
  for (i = map_first[map], j = 0; i; j = i, i = i->next)
    if (!key_strcmp(i->from, kfrom)) {
      length = strlen(sfrom = key_strrep(i->from))
               + strlen(sto = key_strrep(i->to)) + 3;
426
      rep = (char *)malloc_fatal(length);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
      strcpy(rep, sfrom);
      strcat(rep, "  ");
      strcat(rep, sto);
      free(sfrom);
      free(sto);
      return rep;
    }
  return 0;
}
/* tio_mapentry */
#endif

/* end of map.c */


/* VIM configuration: (do not delete this line)
 *
444 445
 * vim:bk:nodg:efm=%f\:%l\:%m:hid:icon:
 * vim:sw=2:sm:textwidth=79:ul=1024:wrap:
446
 */