Remove function pointer from key to bindable table.

Emphasize bindable functions in code.
Use function name based prompts.
This commit is contained in:
Renaud 2021-07-24 08:58:23 +08:00
parent f30ef38bc8
commit 1aadb53956
10 changed files with 109 additions and 138 deletions

101
bind.c
View File

@ -76,27 +76,22 @@ int help(int f, int n)
return TRUE;
}
int deskey( int f, int n) {
/* describe the command for a certain key */
int c; /* key to describe */
char outseq[NSTRING]; /* output buffer for command sequence */
BINDABLE( deskey) {
char outseq[ NSTRING] ; /* output buffer for command sequence */
/* prompt the user to type us a key to describe */
mlwrite(": describe-key ");
/* prompt the user to type a key to describe */
mlwrite( "describe-key: ");
/* get the command sequence to describe
change it to something we can print as well */
c = getckey( FALSE) ;
mlwrite( ": describe-key 0x%x, ", c) ;
cmdstr( c, &outseq[ 0]) ;
/* get the command sequence to describe
* change it to something we can print as well */
int c = getckey( FALSE) ;
cmdstr( c, outseq) ;
/* and dump it out */
ostring(outseq);
ostring(" ");
/* output the command sequence */
ostring( getfname( c, "Not Bound")) ;
return TRUE;
/* output the command sequence */
mlwrite( "describe-key %s: 0x%x, %s",
outseq, c, getfname( c, "Not Bound")) ;
return TRUE ;
}
/*
@ -105,12 +100,12 @@ int deskey( int f, int n) {
*
* int f, n; command arguments [IGNORED]
*/
int bindtokey( int f, int n) {
BINDABLE( bindtokey) {
key_tab *ktp ; /* pointer into the command table */
char outseq[ 80] ; /* output buffer for keystroke sequence */
/* prompt the user to type in a key to bind */
mlwrite(": bind-to-key ");
mlwrite("bind-to-key: ");
/* get the function name to bind it to */
const name_bind *nbp = getname() ;
@ -123,7 +118,7 @@ int bindtokey( int f, int n) {
return FALSE ;
}
ostring( " ") ;
mlwrite( "bind-to-key %s: ", bind_name( nbp)) ;
/* get the command sequence to bind */
boolean prefix_f = (kfunc == metafn) || (kfunc == cex) ||
@ -138,28 +133,27 @@ int bindtokey( int f, int n) {
/* if the function is a prefix key */
if( prefix_f) {
/* search for an existing binding for the prefix key */
for( ktp = keytab ; ktp->k_fp != NULL ; ktp++)
if( ktp->k_fp == kfunc)
/* search and remove all existing binding for the prefix */
for( ktp = keytab ; ktp->k_code != 0 ; ktp++)
if( ktp->k_nbp == nbp)
unbindchar( ktp->k_code) ;
/* reset the appropriate global prefix variable */
if (kfunc == metafn)
metac = c;
if (kfunc == cex)
ctlxc = c;
if (kfunc == unarg)
reptc = c;
if (kfunc == ctrlg)
abortc = c;
/* set the appropriate global prefix variable */
if( kfunc == metafn)
metac = c ;
else if( kfunc == cex)
ctlxc = c ;
if( kfunc == unarg)
reptc = c ;
if( kfunc == ctrlg)
abortc = c ;
}
/* search the table to see if it exists */
for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) {
for( ktp = keytab ; ktp->k_code != 0 ; ktp++) {
if( ktp->k_code == c) {
/* it exists, just change it then */
ktp->k_fp = kfunc ;
ktp->k_nbp = NULL ;
ktp->k_nbp = nbp ;
return TRUE ;
}
}
@ -172,11 +166,9 @@ int bindtokey( int f, int n) {
}
ktp->k_code = c; /* add keycode */
ktp->k_fp = kfunc; /* and the function pointer */
ktp->k_nbp = NULL ;
ktp->k_nbp = nbp ;
++ktp; /* and make sure the next is null */
ktp->k_code = 0;
ktp->k_fp = NULL;
ktp->k_nbp = NULL ;
return TRUE;
@ -194,7 +186,7 @@ int unbindkey(int f, int n)
char outseq[80]; /* output buffer for keystroke sequence */
/* prompt the user to type in a key to unbind */
mlwrite(": unbind-key ");
mlwrite("unbind-key: ");
/* get the command sequence to unbind */
c = getckey(FALSE); /* get a command sequence */
@ -223,21 +215,19 @@ static boolean unbindchar( unsigned c) {
key_tab *ktp ; /* pointer into the command table */
/* search the table to see if the key exists */
for( ktp = keytab ; ktp->k_fp != NULL ; ktp++) {
for( ktp = keytab ; ktp->k_code != 0 ; ktp++) {
if (ktp->k_code == c) {
/* save the pointer and scan to the end of the table */
key_tab *sktp = ktp ;
while( (++ktp)->k_fp != NULL) ;
key_tab *sav_ktp = ktp ;
while( (++ktp)->k_code != 0) ;
ktp -= 1 ; /* backup to the last legit entry */
/* copy the last entry to the current one */
sktp->k_code = ktp->k_code ;
sktp->k_fp = ktp->k_fp ;
sktp->k_nbp = ktp->k_nbp ;
sav_ktp->k_code = ktp->k_code ;
sav_ktp->k_nbp = ktp->k_nbp ;
/* null out the last one */
ktp->k_code = 0 ;
ktp->k_fp = NULL ;
ktp->k_nbp = NULL ;
return TRUE ;
}
@ -289,7 +279,7 @@ int apro( int f, int n) {
char *mstring ; /* string to match cmd names to */
int status ; /* status return */
status = newmlarg( &mstring, "Apropos string: ", 0) ;
status = newmlarg( &mstring, "apropos: ", 0) ;
if( status == TRUE) {
status = buildlist( mstring) ;
free( mstring) ;
@ -362,9 +352,8 @@ static int buildlist( char *mstring) {
cpos = strlen(outseq);
/* search down any keys bound to this */
ktp = &keytab[0];
while (ktp->k_fp != NULL) {
if (ktp->k_fp == nptr->n_func) {
for( ktp = keytab ; ktp->k_code != 0 ; ktp++) {
if( ktp->k_nbp == nptr) {
/* padd out some spaces */
while (cpos < 28)
outseq[cpos++] = ' ';
@ -379,7 +368,6 @@ static int buildlist( char *mstring) {
cpos = 0; /* and clear the line */
}
++ktp;
}
/* if no key was bound, we need to dump it anyway */
@ -494,7 +482,7 @@ static void cmdstr( int c, char *seq) {
key_tab *getkeybind( unsigned c) {
key_tab *ktp ;
for( ktp = keytab ; ktp->k_fp != NULL ; ktp++)
for( ktp = keytab ; ktp->k_code != 0 ; ktp++)
if (ktp->k_code == c)
break ;
@ -504,17 +492,12 @@ key_tab *getkeybind( unsigned c) {
static const char *getfname( unsigned keycode, char *failmsg) {
/* takes a key code and gets the name of the function bound to it */
key_tab *kbp = getkeybind( keycode) ;
fnp_t func = kbp->k_fp ;
if( func == NULL)
if( kbp->k_code == 0)
return failmsg ;
if( kbp->k_nbp == NULL)
kbp->k_nbp = getfncnb( func) ;
assert( func == kbp->k_nbp->n_func) ;
const char *found = bind_name( kbp->k_nbp) ;
assert( *found) ;
return *found ? found : failmsg ;
return found ;
}
/*

View File

@ -1,4 +1,4 @@
/* bindable.h -- implements bindable.c */
/* bindable.c -- implements bindable.h */
#include "bindable.h"
#include <stdlib.h>
@ -135,9 +135,9 @@ int ctlxe(int f, int n)
}
/*
* Abort.
* Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
* Sometimes called as a routine, to do general aborting of stuff.
* abort:
* Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
* Sometimes called as a routine, to do general aborting of stuff.
*/
int ctrlg( int f, int n) {
kbdmode = STOP ;
@ -145,26 +145,4 @@ int ctrlg( int f, int n) {
return ABORT ;
}
/* user function that does NOTHING */
int nullproc(int f, int n)
{
return TRUE;
}
/* dummy function for binding to meta prefix */
int metafn(int f, int n)
{
return TRUE;
}
/* dummy function for binding to control-x prefix */
int cex(int f, int n)
{
return TRUE;
}
/* dummy function for binding to universal-argument */
int unarg(int f, int n)
{
return TRUE;
}
/* end of bindable.c */

View File

@ -1,11 +1,9 @@
#include "names.h"
/* functions that can be bound to keys or procedure names */
int quickexit( int f, int n) ;
int quit( int f, int n) ;
int ctlxlp( int f, int n) ;
int ctlxrp( int f, int n) ;
int ctlxe( int f, int n) ;
int ctrlg( int f, int n) ;
int nullproc( int f, int n) ;
int metafn( int f, int n) ;
int cex( int f, int n) ;
int unarg( int f, int n) ;
BINDABLE( quickexit) ;
BINDABLE( quit) ;
BINDABLE( ctlxlp) ;
BINDABLE( ctlxrp) ;
BINDABLE( ctlxe) ;
BINDABLE( ctrlg) ;

2
exec.c
View File

@ -511,7 +511,7 @@ int execproc( int f, int n) {
char *name ;
/* find out what buffer the user wants to execute */
status = newmlarg( &name, "Execute procedure: ", sizeof bufn - 2) ;
status = newmlarg( &name, "execute-procedure: ", sizeof bufn - 2) ;
if( status != TRUE)
return status ;

View File

@ -214,18 +214,16 @@ int execute( int c, int f, int n) {
/* if the keystroke is a bound function...do it */
key_tab *ktp = getkeybind( c) ;
fnp_t execfunc = ktp->k_fp ;
if( execfunc != NULL) {
if( ktp->k_code != 0) {
thisflag = 0 ;
if( ktp->k_nbp == NULL)
ktp->k_nbp = getfncnb( execfunc) ;
assert( ktp->k_nbp->n_func == execfunc) ;
assert( ktp->k_nbp != NULL) ;
char tag = bind_tag( ktp->k_nbp) ;
if( (tag & 1) && (curbp->b_mode & MDVIEW))
status = rdonly() ;
else
else {
fnp_t execfunc = ktp->k_nbp->n_func ;
status = execfunc( f, n) ;
}
lastflag = thisflag ;
return status ;
@ -340,11 +338,12 @@ void kbd_loop( void) {
newc = getcmd() ;
update( FALSE) ;
do {
key_tab *ktp ;
fnp_t execfunc ;
if( c == newc
&& (execfunc = getkeybind( c)->k_fp) != NULL
&& execfunc != insert_newline
&& (ktp = getkeybind( c))->k_code == c
&& (execfunc = ktp->k_nbp->k_func) != insert_newline
&& execfunc != insert_tab)
newc = getcmd() ;
else

2
file.c
View File

@ -134,7 +134,7 @@ int filefind( int f, int n) {
if( restflag) /* don't allow this command if restricted */
return resterr() ;
status = newmlarg( &fname, "Find file: ", sizeof( fname_t)) ;
status = newmlarg( &fname, "find-file: ", sizeof( fname_t)) ;
if( status == TRUE) {
status = getfile( fname, TRUE) ;
free( fname) ;

11
input.c
View File

@ -157,17 +157,6 @@ int ectoc( int c) {
return c ;
}
const name_bind *getfncnb( fnp_t func) {
const name_bind *nptr ; /* pointer into the name binding table */
/* skim through the table, looking for a match */
for( nptr = names ; nptr->n_func != NULL ; nptr++)
if (nptr->n_func == func)
break ;
return nptr ;
}
/*
* get a command name from the command line. Command completion means
* that pressing a <SPACE> will attempt to complete an unfinished command

19
input.h
View File

@ -10,16 +10,16 @@ typedef enum {
extern kbdstate kbdmode ; /* current keyboard macro mode */
extern int lastkey ; /* last keystoke */
extern int kbdrep ; /* number of repetitions */
extern int kbdm[] ; /* Holds kayboard macro data */
extern int kbdrep ; /* number of repetitions */
extern int kbdm[] ; /* Holds kayboard macro data */
extern int *kbdptr ; /* current position in keyboard buf */
extern int *kbdend ; /* ptr to end of the keyboard */
extern int metac; /* current meta character */
extern int ctlxc; /* current control X prefix char */
extern int reptc; /* current universal repeat char */
extern int abortc; /* current abort command char */
extern const int nlc ; /* end of input char */
extern int metac ; /* current meta character */
extern int ctlxc ; /* current control X prefix char */
extern int reptc ; /* current universal repeat char */
extern int abortc ; /* current abort command char */
extern const int nlc ; /* end of input char */
void ue_system( const char *cmd) ;
@ -28,9 +28,8 @@ int newmlarg( char **outbufref, const char *prompt, int size) ;
int newmlargt( char **outbufref, const char *prompt, int size) ;
int ectoc( int c) ;
/* Look up in names to function association table */
const name_bind *getname( void) ; /* interactively */
const name_bind *getfncnb( fnp_t func) ; /* by function */
/* Get a command name from the command line or interactively */
const name_bind *getname( void) ;
int tgetc( void) ;
int get1key( void) ;

28
names.c
View File

@ -204,7 +204,7 @@ const name_bind names[] = {
{" reverse-incremental-search", risearch, CTLX | 'R'} ,
#endif
#if PROC
{" run", execproc, 0} ,
{" run", execproc, 0} , // alias of execute-procedure
#endif
{"!save-file", filesave, CTLX | CTL_ | 'S'} , /* also X^D */
{" save-window", savewnd, 0} ,
@ -278,7 +278,7 @@ const name_bind names[] = {
static int lastidx = 0 ;
key_tab keytab[ NBINDS] = {
{0, NULL, NULL}
{0, NULL}
} ;
@ -299,7 +299,6 @@ void init_bindings( void) {
/* Add key definition */
if( nbp->n_keycode) {
ktp->k_code = nbp->n_keycode ;
ktp->k_fp = nbp->n_func ;
ktp->k_nbp = nbp ;
ktp += 1 ;
}
@ -320,7 +319,6 @@ void init_bindings( void) {
for( fnbp = names ; fnbp->n_func != NULL ; fnbp++)
if( fnbp->n_func == nbp->n_func) {
ktp->k_code = nbp->n_keycode ;
ktp->k_fp = nbp->n_func ;
ktp->k_nbp = fnbp ;
ktp += 1 ;
break ;
@ -340,7 +338,6 @@ void init_bindings( void) {
/* Mark position after last valid key entry */
ktp->k_code = 0 ;
ktp->k_fp = NULL ;
ktp->k_nbp = NULL ;
}
@ -374,4 +371,25 @@ const name_bind *fncmatch( char *name) {
#endif
}
/* user function that does NOTHING */
BINDABLE( nullproc) {
return TRUE ;
}
/* dummy function for binding to meta prefix */
BINDABLE( metafn) {
return TRUE ;
}
/* dummy function for binding to control-x prefix */
BINDABLE( cex) {
return TRUE ;
}
/* dummy function for binding to universal-argument */
BINDABLE( unarg) {
return TRUE ;
}
/* end of names.c */

17
names.h
View File

@ -3,8 +3,10 @@
#ifndef _NAMES_H_
#define _NAMES_H_
/* Generic uEMACS function pointer type */
typedef int (*fnp_t)( int, int) ;
/* Bindable uEMACS function pointer type and definition template */
#define BINDABLE( fname) int fname( int f, int n)
typedef BINDABLE( (*fnp_t)) ;
/* Structure for the name binding table. */
@ -14,13 +16,12 @@ typedef struct {
unsigned n_keycode ; /* default key assignment, 0 when none */
} name_bind ;
#define bind_name( p) (&(p)->n_name[ 1])
#define bind_tag( p) (p)->n_name[ 0]
#define bind_name( p) (&( p)->n_name[ 1])
#define bind_tag( p) ( p)->n_name[ 0]
/* Structure for the key bindings table. */
typedef struct {
unsigned k_code ; /* Key code */
fnp_t k_fp ; /* Routine to handle it */
const name_bind *k_nbp ; /* entry in name to function map table */
} key_tab ;
@ -35,6 +36,12 @@ extern key_tab keytab[ NBINDS] ; /* key bind to functions table */
void init_bindings( void) ;
const name_bind *fncmatch( char *name) ; /* look up by name */
/* bindable functions mapped to prefix keys and hooks */
BINDABLE( nullproc) ;
BINDABLE( metafn) ;
BINDABLE( cex) ;
BINDABLE( unarg) ;
#endif
/* end of names.h */