2 * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, and/or sell copies of the Software, and to permit persons
11 * to whom the Software is furnished to do so, provided that the above
12 * copyright notice(s) and this permission notice appear in all copies of
13 * the Software and that both the above copyright notice(s) and this
14 * permission notice appear in supporting documentation.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
21 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
22 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
23 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Except as contained in this notice, the name of a copyright holder
27 * shall not be used in advertising or otherwise to promote the sale, use
28 * or other dealings in this Software without prior written authorization
29 * of the copyright holder.
45 * When allocating or reallocating the key-binding table, how
46 * many entries should be added?
48 #define KT_TABLE_INC 100
51 * Define the size of the hash table that is used to associate action
52 * names with action functions. This should be a prime number.
54 #define KT_HASH_SIZE 113
57 * Define a binary-symbol-table object.
60 ErrMsg
*err
; /* Information about the last error */
61 int size
; /* The allocated dimension of table[] */
62 int nkey
; /* The current number of members in the table */
63 KeySym
*table
; /* The table of lexically sorted key sequences */
64 HashTable
*actions
; /* The hash table of actions */
65 StringMem
*smem
; /* Memory for allocating strings */
68 static int _kt_extend_table(KeyTab
*kt
);
69 static int _kt_parse_keybinding_string(const char *keyseq
,
70 char *binary
, int *nc
);
71 static int _kt_compare_strings(const char *s1
, int n1
, const char *s2
, int n2
);
72 static void _kt_assign_action(KeySym
*sym
, KtBinder binder
, KtKeyFn
*keyfn
,
74 static char _kt_backslash_escape(const char *string
, const char **endp
);
75 static int _kt_is_emacs_meta(const char *string
);
76 static int _kt_is_emacs_ctrl(const char *string
);
77 static KtKeyMatch
_kt_locate_keybinding(KeyTab
*kt
, const char *binary_keyseq
,
78 int nc
, int *first
, int *last
);
80 /*.......................................................................
81 * Create a new key-binding symbol table.
84 * return KeyTab * The new object, or NULL on error.
86 KeyTab
*_new_KeyTab(void)
88 KeyTab
*kt
; /* The object to be returned */
90 * Allocate the container.
92 kt
= (KeyTab
*) malloc(sizeof(KeyTab
));
98 * Before attempting any operation that might fail, initialize the
99 * container at least up to the point at which it can safely be passed
103 kt
->size
= KT_TABLE_INC
;
109 * Allocate a place to record error messages.
111 kt
->err
= _new_ErrMsg();
113 return _del_KeyTab(kt
);
115 * Allocate the table.
117 kt
->table
= (KeySym
*) malloc(sizeof(kt
->table
[0]) * kt
->size
);
120 return _del_KeyTab(kt
);
123 * Allocate a hash table of actions.
125 kt
->actions
= _new_HashTable(NULL
, KT_HASH_SIZE
, IGNORE_CASE
, NULL
, 0);
127 return _del_KeyTab(kt
);
129 * Allocate a string allocation object. This allows allocation of
130 * small strings without fragmenting the heap.
132 kt
->smem
= _new_StringMem(KT_TABLE_INC
);
134 return _del_KeyTab(kt
);
138 /*.......................................................................
139 * Delete a KeyTab object.
142 * kt KeyTab * The object to be deleted.
144 * return KeyTab * The deleted object (always NULL).
146 KeyTab
*_del_KeyTab(KeyTab
*kt
)
150 kt
->actions
= _del_HashTable(kt
->actions
);
151 kt
->smem
= _del_StringMem(kt
->smem
, 1);
152 kt
->err
= _del_ErrMsg(kt
->err
);
158 /*.......................................................................
159 * Increase the size of the table to accomodate more keys.
162 * kt KeyTab * The table to be extended.
167 static int _kt_extend_table(KeyTab
*kt
)
170 * Attempt to increase the size of the table.
172 KeySym
*newtab
= (KeySym
*) reallocarray(kt
->table
,
173 (kt
->size
+ KT_TABLE_INC
),
174 sizeof(kt
->table
[0]));
179 _err_record_msg(kt
->err
, "Can't extend keybinding table", END_ERR_MSG
);
184 * Install the resized table.
187 kt
->size
+= KT_TABLE_INC
;
191 /*.......................................................................
192 * Add, update or remove a keybinding to the table.
195 * kt KeyTab * The table to add the binding to.
196 * binder KtBinder The source of the binding.
197 * keyseq const char * The key-sequence to bind.
198 * action char * The action to associate with the key sequence, or
199 * NULL to remove the action associated with the
205 int _kt_set_keybinding(KeyTab
*kt
, KtBinder binder
, const char *keyseq
,
208 KtKeyFn
*keyfn
; /* The action function */
209 void *data
; /* The callback data of the action function */
213 if(kt
==NULL
|| !keyseq
) {
216 _err_record_msg(kt
->err
, "NULL argument(s)", END_ERR_MSG
);
220 * Lookup the function that implements the specified action.
226 Symbol
*sym
= _find_HashSymbol(kt
->actions
, action
);
228 _err_record_msg(kt
->err
, "Unknown key-binding action: ", action
,
233 keyfn
= (KtKeyFn
*) sym
->fn
;
237 * Record the action in the table.
239 return _kt_set_keyfn(kt
, binder
, keyseq
, keyfn
, data
);
242 /*.......................................................................
243 * Add, update or remove a keybinding to the table, specifying an action
247 * kt KeyTab * The table to add the binding to.
248 * binder KtBinder The source of the binding.
249 * keyseq char * The key-sequence to bind.
250 * keyfn KtKeyFn * The action function, or NULL to remove any existing
252 * data void * A pointer to anonymous data to be passed to keyfn
253 * whenever it is called.
258 int _kt_set_keyfn(KeyTab
*kt
, KtBinder binder
, const char *keyseq
,
259 KtKeyFn
*keyfn
, void *data
)
261 const char *kptr
; /* A pointer into keyseq[] */
262 char *binary
; /* The binary version of keyseq[] */
263 int nc
; /* The number of characters in binary[] */
264 int first
,last
; /* The first and last entries in the table which */
265 /* minimally match. */
266 int size
; /* The size to allocate for the binary string */
271 if(kt
==NULL
|| !keyseq
) {
274 _err_record_msg(kt
->err
, "NULL argument(s)", END_ERR_MSG
);
278 * Work out a pessimistic estimate of how much space will be needed
279 * for the binary copy of the string, noting that binary meta characters
280 * embedded in the input string get split into two characters.
282 for(size
=0,kptr
= keyseq
; *kptr
; kptr
++)
283 size
+= IS_META_CHAR(*kptr
) ? 2 : 1;
285 * Allocate a string that has the length of keyseq[].
287 binary
= _new_StringMemString(kt
->smem
, size
+ 1);
290 _err_record_msg(kt
->err
, "Insufficient memory to record key sequence",
295 * Convert control and octal character specifications to binary characters.
297 if(_kt_parse_keybinding_string(keyseq
, binary
, &nc
)) {
298 binary
= _del_StringMemString(kt
->smem
, binary
);
302 * Lookup the position in the table at which to insert the binding.
304 switch(_kt_locate_keybinding(kt
, binary
, nc
, &first
, &last
)) {
306 * If an exact match for the key-sequence is already in the table,
307 * simply replace its binding function (or delete the entry if
308 * the new binding is 0).
312 _kt_assign_action(kt
->table
+ first
, binder
, keyfn
, data
);
314 _del_StringMemString(kt
->smem
, kt
->table
[first
].keyseq
);
315 memmove(kt
->table
+ first
, kt
->table
+ first
+ 1,
316 (kt
->nkey
- first
- 1) * sizeof(kt
->table
[0]));
319 binary
= _del_StringMemString(kt
->smem
, binary
);
322 * If an ambiguous match has been found and we are installing a
323 * callback, then our new key-sequence would hide all of the ambiguous
324 * matches, so we shouldn't allow it.
328 _err_record_msg(kt
->err
, "Can't bind \"", keyseq
,
329 "\", because it is a prefix of another binding",
331 binary
= _del_StringMemString(kt
->smem
, binary
);
337 * If the entry doesn't exist, create it.
346 * We will need a new entry, extend the table if needed.
348 if(kt
->nkey
+ 1 > kt
->size
) {
349 if(_kt_extend_table(kt
)) {
350 binary
= _del_StringMemString(kt
->smem
, binary
);
355 * Make space to insert the new key-sequence before 'last'.
357 if(last
< kt
->nkey
) {
358 memmove(kt
->table
+ last
+ 1, kt
->table
+ last
,
359 (kt
->nkey
- last
) * sizeof(kt
->table
[0]));
362 * Insert the new binding in the vacated position.
364 sym
= kt
->table
+ last
;
365 sym
->keyseq
= binary
;
367 for(i
=0; i
<KTB_NBIND
; i
++) {
368 KtAction
*action
= sym
->actions
+ i
;
373 _kt_assign_action(sym
, binder
, keyfn
, data
);
378 binary
= _del_StringMemString(kt
->smem
, binary
);
385 /*.......................................................................
386 * Perform a min-match lookup of a key-binding.
389 * kt KeyTab * The keybinding table to lookup in.
390 * binary_keyseq char * The binary key-sequence to lookup.
391 * nc int the number of characters in keyseq[].
393 * first,last int * If there is an ambiguous or exact match, the indexes
394 * of the first and last symbols that minimally match
395 * will be assigned to *first and *last respectively.
396 * If there is no match, then first and last will
397 * bracket the location where the symbol should be
400 * return KtKeyMatch One of the following enumerators:
401 * KT_EXACT_MATCH - An exact match was found.
402 * KT_AMBIG_MATCH - An ambiguous match was found.
403 * KT_NO_MATCH - No match was found.
404 * KT_BAD_MATCH - An error occurred while searching.
406 static KtKeyMatch
_kt_locate_keybinding(KeyTab
*kt
, const char *binary_keyseq
,
407 int nc
, int *first
, int *last
)
409 int mid
; /* The index at which to bisect the table */
410 int bot
; /* The lowest index of the table not searched yet */
411 int top
; /* The highest index of the table not searched yet */
412 int test
; /* The return value of strcmp() */
414 * Perform a binary search for the key-sequence.
420 test
= _kt_compare_strings(kt
->table
[mid
].keyseq
, kt
->table
[mid
].nc
,
427 *first
= *last
= mid
;
428 return KT_EXACT_MATCH
;
432 * An exact match wasn't found, but top is the index just below the
433 * index where a match would be found, and bot is the index just above
434 * where the match ought to be found.
439 * See if any ambiguous matches exist, and if so make *first and *last
440 * refer to the first and last matches.
442 if(*last
< kt
->nkey
&& kt
->table
[*last
].nc
> nc
&&
443 _kt_compare_strings(kt
->table
[*last
].keyseq
, nc
, binary_keyseq
, nc
)==0) {
445 while(*last
+1 < kt
->nkey
&& kt
->table
[*last
+1].nc
> nc
&&
446 _kt_compare_strings(kt
->table
[*last
+1].keyseq
, nc
, binary_keyseq
, nc
)==0)
448 return KT_AMBIG_MATCH
;
456 /*.......................................................................
457 * Lookup the sub-array of key-bindings who's key-sequences minimally
458 * match a given key-sequence.
461 * kt KeyTab * The keybinding table to lookup in.
462 * binary_keyseq char * The binary key-sequence to lookup.
463 * nc int the number of characters in keyseq[].
465 * matches KeySym ** The array of minimally matching symbols
466 * can be found in (*matches)[0..nmatch-1], unless
467 * no match was found, in which case *matches will
469 * nmatch int The number of ambiguously matching symbols. This
470 * will be 0 if there is no match, 1 for an exact
471 * match, and a number greater than 1 for an ambiguous
474 * return KtKeyMatch One of the following enumerators:
475 * KT_EXACT_MATCH - An exact match was found.
476 * KT_AMBIG_MATCH - An ambiguous match was found.
477 * KT_NO_MATCH - No match was found.
478 * KT_BAD_MATCH - An error occurred while searching.
480 KtKeyMatch
_kt_lookup_keybinding(KeyTab
*kt
, const char *binary_keyseq
,
481 int nc
, KeySym
**matches
, int *nmatch
)
483 KtKeyMatch status
; /* The return status */
484 int first
,last
; /* The indexes of the first and last matching entry */
485 /* in the symbol table. */
487 * Check the arguments.
489 if(!kt
|| !binary_keyseq
|| !matches
|| !nmatch
|| nc
< 0) {
492 _err_record_msg(kt
->err
, "NULL argument(s)", END_ERR_MSG
);
496 * Lookup the indexes of the binding-table entries that bracket the
497 * target key-sequence.
499 status
= _kt_locate_keybinding(kt
, binary_keyseq
, nc
, &first
, &last
);
501 * Translate the indexes into the corresponding subarray of matching
507 *matches
= kt
->table
+ first
;
508 *nmatch
= last
- first
+ 1;
518 /*.......................................................................
519 * Convert a keybinding string into a uniq binary representation.
521 * Control characters can be given directly in their binary form,
522 * expressed as either ^ or C-, followed by the character, expressed in
523 * octal, like \129 or via C-style backslash escapes, with the addition
524 * of '\E' to denote the escape key. Similarly, meta characters can be
525 * given directly in binary or expressed as M- followed by the character.
526 * Meta characters are recorded as two characters in the binary output
527 * string, the first being the escape key, and the second being the key
528 * that was modified by the meta key. This means that binding to
529 * \EA or ^[A or M-A are all equivalent.
532 * keyseq char * The key sequence being added.
534 * binary char * The binary version of the key sequence will be
535 * assigned to binary[], which must have at least
536 * as many characters as keyseq[] plus the number
537 * of embedded binary meta characters.
538 * nc int * The number of characters assigned to binary[]
539 * will be recorded in *nc.
544 static int _kt_parse_keybinding_string(const char *keyseq
, char *binary
,
547 const char *iptr
= keyseq
; /* Pointer into keyseq[] */
548 char *optr
= binary
; /* Pointer into binary[] */
549 char c
; /* An intermediate character */
551 * Parse the input characters until they are exhausted or the
552 * output string becomes full.
556 * Check for special characters.
559 case '^': /* A control character specification */
561 * Convert the caret expression into the corresponding control
562 * character unless no character follows the caret, in which case
563 * record a literal caret.
567 * Get the next, possibly escaped, character.
569 if(iptr
[1] == '\\') {
570 c
= _kt_backslash_escape(iptr
+2, &iptr
);
576 * Convert the character to a control character.
578 *optr
++ = MAKE_CTRL(c
);
584 * A backslash-escaped character?
588 * Convert the escape sequence to a binary character.
590 *optr
++ = _kt_backslash_escape(iptr
+1, &iptr
);
593 * Possibly an emacs-style meta character?
596 if(_kt_is_emacs_meta(iptr
)) {
597 *optr
++ = GL_ESC_CHAR
;
604 * Possibly an emacs-style control character specification?
607 if(_kt_is_emacs_ctrl(iptr
)) {
608 *optr
++ = MAKE_CTRL(iptr
[2]);
617 * Convert embedded meta characters into an escape character followed
618 * by the meta-unmodified character.
620 if(IS_META_CHAR(*iptr
)) {
621 *optr
++ = GL_ESC_CHAR
;
622 *optr
++ = META_TO_CHAR(*iptr
);
625 * To allow keysequences that start with printable characters to
626 * be distinguished from the cursor-key keywords, prepend a backslash
627 * to the former. This same operation is performed in gl_interpret_char()
628 * before looking up a keysequence that starts with a printable character.
630 } else if(iptr
==keyseq
&& !IS_CTRL_CHAR(*iptr
) &&
631 strcmp(keyseq
, "up") != 0 && strcmp(keyseq
, "down") != 0 &&
632 strcmp(keyseq
, "left") != 0 && strcmp(keyseq
, "right") != 0) {
641 * How many characters were placed in the output array?
647 /*.......................................................................
648 * Add, remove or modify an action.
651 * kt KeyTab * The key-binding table.
652 * action char * The name of the action.
653 * fn KtKeyFn * The function that implements the action, or NULL
654 * to remove an existing action.
655 * data void * A pointer to arbitrary callback data to pass to the
656 * action function whenever it is called.
661 int _kt_set_action(KeyTab
*kt
, const char *action
, KtKeyFn
*fn
, void *data
)
663 Symbol
*sym
; /* The symbol table entry of the action */
665 * Check the arguments.
670 _err_record_msg(kt
->err
, "NULL argument(s)", END_ERR_MSG
);
674 * If no function was provided, delete an existing action.
677 sym
= _del_HashSymbol(kt
->actions
, action
);
681 * If the action already exists, replace its action function.
683 sym
= _find_HashSymbol(kt
->actions
, action
);
685 sym
->fn
= (void (*)(void))fn
;
692 if(!_new_HashSymbol(kt
->actions
, action
, 0, (void (*)(void))fn
, data
, 0)) {
693 _err_record_msg(kt
->err
, "Insufficient memory to record key-binding action",
700 /*.......................................................................
701 * Compare two strings of specified length which may contain embedded
705 * s1 char * The first of the strings to be compared.
706 * n1 int The length of the string in s1.
707 * s2 char * The second of the strings to be compared.
708 * n2 int The length of the string in s2.
710 * return int < 0 if(s1 < s2)
714 static int _kt_compare_strings(const char *s1
, int n1
, const char *s2
, int n2
)
718 * Find the first character where the two strings differ.
720 for(i
=0; i
<n1
&& i
<n2
&& s1
[i
]==s2
[i
]; i
++)
723 * Did we hit the end of either string before finding a difference?
734 * Compare the two characters that differed to determine which
735 * string is greatest.
737 return s1
[i
] - s2
[i
];
740 /*.......................................................................
741 * Assign a given action function to a binding table entry.
744 * sym KeySym * The binding table entry to be modified.
745 * binder KtBinder The source of the binding.
746 * keyfn KtKeyFn * The action function.
747 * data void * A pointer to arbitrary callback data to pass to
748 * the action function whenever it is called.
750 static void _kt_assign_action(KeySym
*sym
, KtBinder binder
, KtKeyFn
*keyfn
,
753 KtAction
*action
; /* An action function/data pair */
756 * Unknown binding source?
758 if(binder
< 0 || binder
>= KTB_NBIND
)
761 * Record the action according to its source.
763 action
= sym
->actions
+ binder
;
767 * Find the highest priority binding source that has supplied an
768 * action. Note that the actions[] array is ordered in order of
769 * descreasing priority, so the first entry that contains a function
772 for(i
=0; i
<KTB_NBIND
&& !sym
->actions
[i
].fn
; i
++)
775 * Record the index of this action for use during lookups.
777 sym
->binder
= i
< KTB_NBIND
? i
: -1;
781 /*.......................................................................
782 * Remove all key bindings that came from a specified source.
785 * kt KeyTab * The table of key bindings.
786 * binder KtBinder The source of the bindings to be cleared.
788 void _kt_clear_bindings(KeyTab
*kt
, KtBinder binder
)
790 int oldkey
; /* The index of a key in the original binding table */
791 int newkey
; /* The index of a key in the updated binding table */
793 * If there is no table, then no bindings exist to be deleted.
798 * Clear bindings of the given source.
800 for(oldkey
=0; oldkey
<kt
->nkey
; oldkey
++)
801 _kt_assign_action(kt
->table
+ oldkey
, binder
, 0, NULL
);
803 * Delete entries that now don't have a binding from any source.
806 for(oldkey
=0; oldkey
<kt
->nkey
; oldkey
++) {
807 KeySym
*sym
= kt
->table
+ oldkey
;
808 if(sym
->binder
< 0) {
809 _del_StringMemString(kt
->smem
, sym
->keyseq
);
812 kt
->table
[newkey
] = *sym
;
817 * Record the number of keys that were kept.
823 /*.......................................................................
824 * Translate a backslash escape sequence to a binary character.
827 * string const char * The characters that follow the backslash.
829 * endp const char ** If endp!=NULL, on return *endp will be made to
830 * point to the character in string[] which follows
831 * the escape sequence.
833 * return char The binary character.
835 static char _kt_backslash_escape(const char *string
, const char **endp
)
837 char c
; /* The output character */
839 * Is the backslash followed by one or more octal digits?
842 case '0': case '1': case '2': case '3':
843 case '4': case '5': case '6': case '7':
844 c
= strtol(string
, (char **)&string
, 8);
854 case 'e': case 'E': /* Escape */
886 * Report the character which follows the escape sequence.
893 /*.......................................................................
894 * Return non-zero if the next two characters are M- and a third character
895 * follows. Otherwise return 0.
898 * string const char * The sub-string to scan.
900 * return int 1 - The next two characters are M- and these
901 * are followed by at least one character.
902 * 0 - The next two characters aren't M- or no
903 * character follows a M- pair.
905 static int _kt_is_emacs_meta(const char *string
)
907 return *string
++ == 'M' && *string
++ == '-' && *string
;
910 /*.......................................................................
911 * Return non-zero if the next two characters are C- and a third character
912 * follows. Otherwise return 0.
915 * string const char * The sub-string to scan.
917 * return int 1 - The next two characters are C- and these
918 * are followed by at least one character.
919 * 0 - The next two characters aren't C- or no
920 * character follows a C- pair.
922 static int _kt_is_emacs_ctrl(const char *string
)
924 return *string
++ == 'C' && *string
++ == '-' && *string
;
927 /*.......................................................................
928 * Merge an array of bindings with existing bindings.
931 * kt KeyTab * The table of key bindings.
932 * binder KtBinder The source of the bindings.
933 * bindings const KtKeyBinding * The array of bindings.
934 * n int The number of bindings in bindings[].
939 int _kt_add_bindings(KeyTab
*kt
, KtBinder binder
, const KtKeyBinding
*bindings
,
944 * Check the arguments.
946 if(!kt
|| !bindings
) {
949 _err_record_msg(kt
->err
, "NULL argument(s)", END_ERR_MSG
);
953 * Install the array of bindings.
956 if(_kt_set_keybinding(kt
, binder
, bindings
[i
].keyseq
, bindings
[i
].action
))
962 /*.......................................................................
963 * Lookup the function that implements a given action.
966 * kt KeyTab * The table of key bindings.
967 * action const char * The name of the action to look up.
969 * fn KtKeyFn ** If the action is found, the function that
970 * implements it will be assigned to *fn. Note
971 * that fn can be NULL.
972 * data void ** If the action is found, the callback data
973 * associated with the action function, will be
974 * assigned to *data. Note that data can be NULL.
977 * 1 - Action not found.
979 int _kt_lookup_action(KeyTab
*kt
, const char *action
,
980 KtKeyFn
**fn
, void **data
)
982 Symbol
*sym
; /* The symbol table entry of the action */
984 * Check the arguments.
989 _err_record_msg(kt
->err
, "NULL argument(s)", END_ERR_MSG
);
993 * Lookup the symbol table entry of the action.
995 sym
= _find_HashSymbol(kt
->actions
, action
);
999 * Return the function and ccallback data associated with the action.
1002 *fn
= (KtKeyFn
*) sym
->fn
;
1008 /*.......................................................................
1009 * Return extra information (ie. in addition to that provided by errno)
1010 * about the last error to occur in any of the public functions of this
1014 * kt KeyTab * The table of key bindings.
1016 * return const char * A pointer to the internal buffer in which
1017 * the error message is temporarily stored.
1019 const char *_kt_last_error(KeyTab
*kt
)
1021 return kt
? _err_get_msg(kt
->err
) : "NULL KeyTab argument";