home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / readline / keymaps.c < prev    next >
C/C++ Source or Header  |  1994-01-26  |  4KB  |  195 lines

  1. /* keymaps.c -- Functions and keymaps for the GNU Readline library. */
  2.  
  3. /* Copyright (C) 1988,1989 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Readline, a library for reading lines
  6.    of text with interactive input and history editing.
  7.  
  8.    Readline is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by the
  10.    Free Software Foundation; either version 1, or (at your option) any
  11.    later version.
  12.  
  13.    Readline is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with Readline; see the file COPYING.  If not, write to the Free
  20.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22. #if defined (HAVE_STDLIB_H)
  23. #  include <stdlib.h>
  24. #else
  25. #  include "ansi_stdlib.h"
  26. #endif /* HAVE_STDLIB_H */
  27.  
  28. #include "keymaps.h"
  29. #include "emacs_keymap.c"
  30.  
  31. #ifdef VI_MODE
  32. #include "vi_keymap.c"
  33. #endif
  34.  
  35. #if defined (STATIC_MALLOC)
  36. static char *xmalloc (), *xrealloc ();
  37. #else
  38. extern char *xmalloc (), *xrealloc ();
  39. #endif /* STATIC_MALLOC */
  40.  
  41. /* **************************************************************** */
  42. /*                                    */
  43. /*              Functions for manipulating Keymaps.        */
  44. /*                                    */
  45. /* **************************************************************** */
  46.  
  47.  
  48. /* Return a new, empty keymap.
  49.    Free it with free() when you are done. */
  50. Keymap
  51. rl_make_bare_keymap ()
  52. {
  53.   register int i;
  54.   Keymap keymap = (Keymap)xmalloc (KEYMAP_SIZE * sizeof (KEYMAP_ENTRY));
  55.  
  56.   for (i = 0; i < KEYMAP_SIZE; i++)
  57.     {
  58.       keymap[i].type = ISFUNC;
  59.       keymap[i].function = (Function *)NULL;
  60.     }
  61.  
  62.   for (i = 'A'; i < ('Z' + 1); i++)
  63.     {
  64.       keymap[i].type = ISFUNC;
  65.       keymap[i].function = rl_do_lowercase_version;
  66.     }
  67.  
  68.   return (keymap);
  69. }
  70.  
  71. /* Return a new keymap which is a copy of MAP. */
  72. Keymap
  73. rl_copy_keymap (map)
  74.      Keymap map;
  75. {
  76.   register int i;
  77.   Keymap temp = rl_make_bare_keymap ();
  78.  
  79.   for (i = 0; i < KEYMAP_SIZE; i++)
  80.     {
  81.       temp[i].type = map[i].type;
  82.       temp[i].function = map[i].function;
  83.     }
  84.   return (temp);
  85. }
  86.  
  87. /* Return a new keymap with the printing characters bound to rl_insert,
  88.    the uppercase Meta characters bound to run their lowercase equivalents,
  89.    and the Meta digits bound to produce numeric arguments. */
  90. Keymap
  91. rl_make_keymap ()
  92. {
  93.   extern int rl_insert (), rl_rubout ();
  94.   register int i;
  95.   Keymap newmap;
  96.  
  97.   newmap = rl_make_bare_keymap ();
  98.  
  99.   /* All ASCII printing characters are self-inserting. */
  100.   for (i = ' '; i < 126; i++)
  101.     newmap[i].function = rl_insert;
  102.  
  103.   newmap[TAB].function = rl_insert;
  104.   newmap[RUBOUT].function = rl_rubout;
  105.   newmap[CTRL('H')].function = rl_rubout;
  106.  
  107. #if KEYMAP_SIZE > 128
  108.   /* Printing characters in some 8-bit character sets. */
  109.   for (i = 128; i < 160; i++)
  110.     newmap[i].function = rl_insert;
  111.  
  112.   /* ISO Latin-1 printing characters should self-insert. */
  113.   for (i = 160; i < 256; i++)
  114.     newmap[i].function = rl_insert;
  115. #endif /* KEYMAP_SIZE > 128 */
  116.  
  117.   return (newmap);
  118. }
  119.  
  120. /* Free the storage associated with MAP. */
  121. int
  122. rl_discard_keymap (map)
  123.      Keymap (map);
  124. {
  125.   int i;
  126.  
  127.   if (!map)
  128.     return 0;
  129.  
  130.   for (i = 0; i < KEYMAP_SIZE; i++)
  131.     {
  132.       switch (map[i].type)
  133.     {
  134.     case ISFUNC:
  135.       break;
  136.  
  137.     case ISKMAP:
  138.       rl_discard_keymap ((Keymap)map[i].function);
  139.       break;
  140.  
  141.     case ISMACR:
  142.       free ((char *)map[i].function);
  143.       break;
  144.     }
  145.     }
  146.  
  147.   return 0;
  148. }
  149.  
  150. #if defined (STATIC_MALLOC)
  151.  
  152. /* **************************************************************** */
  153. /*                                    */
  154. /*            xmalloc and xrealloc ()                     */
  155. /*                                    */
  156. /* **************************************************************** */
  157.  
  158. static void memory_error_and_abort ();
  159.  
  160. static char *
  161. xmalloc (bytes)
  162.      int bytes;
  163. {
  164.   char *temp = (char *)malloc (bytes);
  165.  
  166.   if (!temp)
  167.     memory_error_and_abort ();
  168.   return (temp);
  169. }
  170.  
  171. static char *
  172. xrealloc (pointer, bytes)
  173.      char *pointer;
  174.      int bytes;
  175. {
  176.   char *temp;
  177.  
  178.   if (!pointer)
  179.     temp = (char *)malloc (bytes);
  180.   else
  181.     temp = (char *)realloc (pointer, bytes);
  182.  
  183.   if (!temp)
  184.     memory_error_and_abort ();
  185.   return (temp);
  186. }
  187.  
  188. static void
  189. memory_error_and_abort ()
  190. {
  191.   fprintf (stderr, "readline: Out of virtual memory!\n");
  192.   abort ();
  193. }
  194. #endif /* STATIC_MALLOC */
  195.