17c478bd9Sstevel@tonic-gate #ifndef keytab_h
27c478bd9Sstevel@tonic-gate #define keytab_h
37c478bd9Sstevel@tonic-gate 
47c478bd9Sstevel@tonic-gate /*
57c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
6*1da57d55SToomas Soome  *
77c478bd9Sstevel@tonic-gate  * All rights reserved.
8*1da57d55SToomas Soome  *
97c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
107c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
117c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
127c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
137c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
147c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
157c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
167c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
177c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
18*1da57d55SToomas Soome  *
197c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
207c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
217c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
227c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
237c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
247c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
257c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
267c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
277c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28*1da57d55SToomas Soome  *
297c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
307c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
317c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
327c478bd9Sstevel@tonic-gate  * of the copyright holder.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "libtecla.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*-----------------------------------------------------------------------*
387c478bd9Sstevel@tonic-gate  * This module defines a binary-search symbol table of key-bindings.     *
397c478bd9Sstevel@tonic-gate  *-----------------------------------------------------------------------*/
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * All key-binding functions are defined as follows.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * Input:
457c478bd9Sstevel@tonic-gate  *  gl    GetLine *  The resource object of this library.
467c478bd9Sstevel@tonic-gate  *  count     int    A positive repeat count specified by the user,
477c478bd9Sstevel@tonic-gate  *                   or 1. Action functions should ignore this if
487c478bd9Sstevel@tonic-gate  *                   repeating the action multiple times isn't
497c478bd9Sstevel@tonic-gate  *                   appropriate.
507c478bd9Sstevel@tonic-gate  *  data     void *  A pointer to action-specific data,
517c478bd9Sstevel@tonic-gate  *                   cast to (void *).
527c478bd9Sstevel@tonic-gate  * Output:
537c478bd9Sstevel@tonic-gate  *  return    int    0 - OK.
547c478bd9Sstevel@tonic-gate  *                   1 - Error.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate #define KT_KEY_FN(fn) int (fn)(GetLine *gl, int count, void *data)
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate typedef KT_KEY_FN(KtKeyFn);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Allow the association of arbitrary callback data with each action
627c478bd9Sstevel@tonic-gate  * function.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate typedef struct {
657c478bd9Sstevel@tonic-gate   KtKeyFn *fn;          /* The acion function */
667c478bd9Sstevel@tonic-gate   void *data;           /* A pointer to arbitrary data to be passed to */
677c478bd9Sstevel@tonic-gate                         /*  fn() whenever it is called. */
687c478bd9Sstevel@tonic-gate } KtAction;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Enumerate the possible sources of key-bindings in order of decreasing
727c478bd9Sstevel@tonic-gate  * priority.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate typedef enum {
757c478bd9Sstevel@tonic-gate   KTB_USER,         /* This is a binding being set by the user */
767c478bd9Sstevel@tonic-gate   KTB_NORM,         /* This is the default binding set by the library */
777c478bd9Sstevel@tonic-gate   KTB_TERM,         /* This is a binding taken from the terminal settings */
787c478bd9Sstevel@tonic-gate /* The following entry must always be last */
797c478bd9Sstevel@tonic-gate   KTB_NBIND         /* The number of binding sources listed above */
807c478bd9Sstevel@tonic-gate } KtBinder;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Define an entry of a key-binding binary symbol table.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate typedef struct {
867c478bd9Sstevel@tonic-gate   char *keyseq;                /* The key sequence that triggers the macro */
877c478bd9Sstevel@tonic-gate   int nc;                      /* The number of characters in keyseq[] */
887c478bd9Sstevel@tonic-gate   KtAction actions[KTB_NBIND]; /* Bindings from different sources */
897c478bd9Sstevel@tonic-gate   int binder;                  /* The index of the highest priority element */
907c478bd9Sstevel@tonic-gate                                /*  of actions[] that has been assigned an */
917c478bd9Sstevel@tonic-gate                                /*  action function, or -1 if none have. */
927c478bd9Sstevel@tonic-gate } KeySym;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Provide an opaque type alias to the symbol table container.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate typedef struct KeyTab KeyTab;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Create a new symbol table.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate KeyTab *_new_KeyTab(void);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Delete the symbol table.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate KeyTab *_del_KeyTab(KeyTab *kt);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate int _kt_set_keybinding(KeyTab *kt, KtBinder binder,
1107c478bd9Sstevel@tonic-gate 		       const char *keyseq, const char *action);
1117c478bd9Sstevel@tonic-gate int _kt_set_keyfn(KeyTab *kt, KtBinder binder, const char *keyseq,
1127c478bd9Sstevel@tonic-gate 		  KtKeyFn *fn, void *data);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate int _kt_set_action(KeyTab *kt, const char *action, KtKeyFn *fn, void *data);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Lookup the function that implements a given action.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate int _kt_lookup_action(KeyTab *kt, const char *action,
1207c478bd9Sstevel@tonic-gate 		      KtKeyFn **fn, void **data);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate typedef enum {
1237c478bd9Sstevel@tonic-gate   KT_EXACT_MATCH,   /* An exact match was found */
1247c478bd9Sstevel@tonic-gate   KT_AMBIG_MATCH,   /* An ambiguous match was found */
1257c478bd9Sstevel@tonic-gate   KT_NO_MATCH,      /* No match was found */
1267c478bd9Sstevel@tonic-gate   KT_BAD_MATCH      /* An error occurred while searching */
1277c478bd9Sstevel@tonic-gate } KtKeyMatch;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate KtKeyMatch _kt_lookup_keybinding(KeyTab *kt, const char *binary_keyseq,
1307c478bd9Sstevel@tonic-gate 				 int nc, KeySym **matches, int *nmatch);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Remove all key bindings that came from a specified source.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate void _kt_clear_bindings(KeyTab *kt, KtBinder binder);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * When installing an array of keybings each binding is defined by
1397c478bd9Sstevel@tonic-gate  * an element of the following type:
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate typedef struct {
1427c478bd9Sstevel@tonic-gate   const char *keyseq;   /* The sequence of keys that trigger this binding */
1437c478bd9Sstevel@tonic-gate   const char *action;   /* The name of the action function that is triggered */
1447c478bd9Sstevel@tonic-gate } KtKeyBinding;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Merge an array of bindings with existing bindings.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate int _kt_add_bindings(KeyTab *kt, KtBinder binder, const KtKeyBinding *bindings,
1507c478bd9Sstevel@tonic-gate 		     unsigned n);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Get information about the last error in this module.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate const char *_kt_last_error(KeyTab *kt);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #endif
158