a033d5ecd109bea8ca44a7720674f2fb35514841
[platform/upstream/bash.git] / lib / readline / keymaps.c
1 /* keymaps.c -- Functions and keymaps for the GNU Readline library. */
2
3 /* Copyright (C) 1988,1989-2009 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library (Readline), a library
6    for reading lines of text with interactive input and history editing.      
7
8    Readline is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12
13    Readline is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Readline.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #if defined (HAVE_STDLIB_H)
29 #  include <stdlib.h>
30 #else
31 #  include "ansi_stdlib.h"
32 #endif /* HAVE_STDLIB_H */
33
34 #include <stdio.h>      /* for FILE * definition for readline.h */
35
36 #include "readline.h"
37 #include "rlconf.h"
38
39 #include "emacs_keymap.c"
40
41 #if defined (VI_MODE)
42 #include "vi_keymap.c"
43 #endif
44
45 #include "xmalloc.h"
46
47 /* **************************************************************** */
48 /*                                                                  */
49 /*                    Functions for manipulating Keymaps.           */
50 /*                                                                  */
51 /* **************************************************************** */
52
53
54 /* Return a new, empty keymap.
55    Free it with free() when you are done. */
56 Keymap
57 rl_make_bare_keymap ()
58 {
59   register int i;
60   Keymap keymap = (Keymap)xmalloc (KEYMAP_SIZE * sizeof (KEYMAP_ENTRY));
61
62   for (i = 0; i < KEYMAP_SIZE; i++)
63     {
64       keymap[i].type = ISFUNC;
65       keymap[i].function = (rl_command_func_t *)NULL;
66     }
67
68 #if 0
69   for (i = 'A'; i < ('Z' + 1); i++)
70     {
71       keymap[i].type = ISFUNC;
72       keymap[i].function = rl_do_lowercase_version;
73     }
74 #endif
75
76   return (keymap);
77 }
78
79 /* Return a new keymap which is a copy of MAP. */
80 Keymap
81 rl_copy_keymap (map)
82      Keymap map;
83 {
84   register int i;
85   Keymap temp;
86
87   temp = rl_make_bare_keymap ();
88   for (i = 0; i < KEYMAP_SIZE; i++)
89     {
90       temp[i].type = map[i].type;
91       temp[i].function = map[i].function;
92     }
93   return (temp);
94 }
95
96 /* Return a new keymap with the printing characters bound to rl_insert,
97    the uppercase Meta characters bound to run their lowercase equivalents,
98    and the Meta digits bound to produce numeric arguments. */
99 Keymap
100 rl_make_keymap ()
101 {
102   register int i;
103   Keymap newmap;
104
105   newmap = rl_make_bare_keymap ();
106
107   /* All ASCII printing characters are self-inserting. */
108   for (i = ' '; i < 127; i++)
109     newmap[i].function = rl_insert;
110
111   newmap[TAB].function = rl_insert;
112   newmap[RUBOUT].function = rl_rubout;  /* RUBOUT == 127 */
113   newmap[CTRL('H')].function = rl_rubout;
114
115 #if KEYMAP_SIZE > 128
116   /* Printing characters in ISO Latin-1 and some 8-bit character sets. */
117   for (i = 128; i < 256; i++)
118     newmap[i].function = rl_insert;
119 #endif /* KEYMAP_SIZE > 128 */
120
121   return (newmap);
122 }
123
124 /* Free the storage associated with MAP. */
125 void
126 rl_discard_keymap (map)
127      Keymap map;
128 {
129   int i;
130
131   if (!map)
132     return;
133
134   for (i = 0; i < KEYMAP_SIZE; i++)
135     {
136       switch (map[i].type)
137         {
138         case ISFUNC:
139           break;
140
141         case ISKMAP:
142           rl_discard_keymap ((Keymap)map[i].function);
143           break;
144
145         case ISMACR:
146           free ((char *)map[i].function);
147           break;
148         }
149     }
150 }