mirror of
https://github.com/rfivet/uemacs.git
synced 2024-12-18 23:36:23 -05:00
bind: code review and minor refactoring.
basic: minor reformatting.
This commit is contained in:
parent
eaf516110f
commit
720603ac8e
1
basic.c
1
basic.c
@ -320,6 +320,7 @@ boolean setmark( int f, int n) {
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/* Swap the values of "." and "mark" in the current window. If no mark as
|
||||
been previously set, set it. Bound to C-X C-X exchange-point-and-mark.
|
||||
*/
|
||||
|
199
bind.c
199
bind.c
@ -9,24 +9,22 @@
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "estruct.h"
|
||||
#include "bindable.h"
|
||||
#include "buffer.h"
|
||||
#include "display.h"
|
||||
#include "display.h" /* upmode(), ostring() */
|
||||
#include "exec.h"
|
||||
#include "file.h"
|
||||
#include "flook.h"
|
||||
#include "input.h"
|
||||
#include "line.h"
|
||||
#include "mlout.h"
|
||||
#include "names.h"
|
||||
#include "util.h"
|
||||
#include "window.h"
|
||||
|
||||
|
||||
static int buildlist( char *mstring) ;
|
||||
static char *cmdstr( unsigned c, char *seq) ;
|
||||
static unsigned int getckey( int mflag) ;
|
||||
@ -34,14 +32,10 @@ static unsigned int stock( char *keyname) ;
|
||||
static const char *getfname( unsigned keycode, const char *failmsg) ;
|
||||
|
||||
|
||||
static boolean cmdfail( const char *msg) {
|
||||
mlwrite( "%s", msg) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
/* give me some help!!!! bring up a fake buffer and read the help file into
|
||||
it with view mode
|
||||
*/
|
||||
BINDABLE( help) {
|
||||
/* give me some help!!!!
|
||||
bring up a fake buffer and read the help file into it with view mode */
|
||||
char *fname = NULL; /* ptr to file returned by flook() */
|
||||
|
||||
/* first check if we are already here */
|
||||
@ -52,7 +46,7 @@ BINDABLE( help) {
|
||||
if( bp == NULL) {
|
||||
fname = flook( hlpfname, FALSE) ;
|
||||
if( fname == NULL)
|
||||
return cmdfail( "(Help file is not online)") ;
|
||||
return mloutfail( "(Help file is not online)") ;
|
||||
}
|
||||
|
||||
/* split the current window to make room for the help stuff */
|
||||
@ -60,10 +54,10 @@ BINDABLE( help) {
|
||||
&& splitwind( FALSE, 1) == FALSE) /* Split it */
|
||||
return FALSE ;
|
||||
|
||||
if (bp == NULL) {
|
||||
if( bp == NULL) {
|
||||
/* and read the stuff in */
|
||||
if (getfile(fname, FALSE) == FALSE)
|
||||
return FALSE;
|
||||
if( getfile( fname, FALSE) == FALSE)
|
||||
return FALSE ;
|
||||
} else
|
||||
swbuffer( bp) ;
|
||||
|
||||
@ -71,20 +65,22 @@ BINDABLE( help) {
|
||||
curwp->w_bufp->b_mode |= MDVIEW;
|
||||
curwp->w_bufp->b_flag |= BFINVS;
|
||||
upmode() ;
|
||||
return TRUE;
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
static boolean invalidkey( void) {
|
||||
return cmdfail( "(Invalid key sequence)") ;
|
||||
return mloutfail( "(Invalid key sequence)") ;
|
||||
}
|
||||
|
||||
|
||||
/* describe the command for a certain key */
|
||||
BINDABLE( deskey) {
|
||||
const char cmdname[] = "describe-key" ;
|
||||
char outseq[ NSTRING] ; /* output buffer for command sequence */
|
||||
char outseq[ 8] ; /* output buffer for keystroke sequence */
|
||||
|
||||
/* prompt the user to type a key to describe */
|
||||
mlwrite( "%s: ", cmdname) ;
|
||||
mloutfmt( "%s: ", cmdname) ;
|
||||
|
||||
/* get the command sequence to describe
|
||||
* change it to something we can print as well */
|
||||
@ -93,23 +89,23 @@ BINDABLE( deskey) {
|
||||
return invalidkey() ;
|
||||
|
||||
/* output the command sequence */
|
||||
mlwrite( "%s %s: 0x%x, %s", cmdname, cmdstr( keycode, outseq), keycode,
|
||||
mloutfmt( "%s %s: 0x%x, %s", cmdname, cmdstr( keycode, outseq), keycode,
|
||||
getfname( keycode, "Not Bound")) ;
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
/*
|
||||
* bindtokey:
|
||||
|
||||
/* bindtokey:
|
||||
* add a new key to the key binding table
|
||||
*
|
||||
* int f, n; command arguments [IGNORED]
|
||||
*/
|
||||
BINDABLE( bindtokey) {
|
||||
kbind_p ktp ; /* pointer into the command table */
|
||||
char outseq[ 80] ; /* output buffer for keystroke sequence */
|
||||
char outseq[ 8] ; /* output buffer for keystroke sequence */
|
||||
|
||||
/* prompt the user to type in a key to bind */
|
||||
mlwrite("bind-to-key: ");
|
||||
mloutstr( "bind-to-key: ") ;
|
||||
|
||||
/* get the function name to bind it to */
|
||||
nbind_p nbp = getname() ;
|
||||
@ -118,9 +114,9 @@ BINDABLE( bindtokey) {
|
||||
|
||||
fnp_t kfunc = nbp->n_func ;
|
||||
if( kfunc == NULL)
|
||||
return cmdfail( "(No such function)") ;
|
||||
return mloutfail( "(No such function)") ;
|
||||
|
||||
mlwrite( "bind-to-key %s: ", bind_name( nbp)) ;
|
||||
mloutfmt( "bind-to-key %s: ", bind_name( nbp)) ;
|
||||
|
||||
/* get the command sequence to bind */
|
||||
boolean prefix_f = (kfunc == metafn) || (kfunc == cex) ||
|
||||
@ -139,9 +135,9 @@ BINDABLE( bindtokey) {
|
||||
|| (c == ctlxc && kfunc == cex)
|
||||
|| (c == reptc && kfunc == unarg)
|
||||
|| (c == abortc && kfunc == ctrlg))
|
||||
return TRUE ;
|
||||
return TRUE ; /* be silent if keep current */
|
||||
|
||||
return cmdfail( "(Can't bind to active prefix)") ;
|
||||
return mloutfail( "(Can't bind to active prefix)") ;
|
||||
}
|
||||
|
||||
/* if the function is a prefix key */
|
||||
@ -166,22 +162,22 @@ BINDABLE( bindtokey) {
|
||||
|
||||
ktp = setkeybinding( c, nbp) ;
|
||||
if( ktp->k_code == 0)
|
||||
return cmdfail( "Binding table FULL!") ;
|
||||
return mloutfail( "Binding table FULL!") ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
/*
|
||||
* unbindkey:
|
||||
|
||||
/* unbindkey:
|
||||
* delete a key from the key binding table
|
||||
*
|
||||
* int f, n; command arguments [IGNORED]
|
||||
*/
|
||||
BINDABLE( unbindkey) {
|
||||
char outseq[ 80] ; /* output buffer for keystroke sequence */
|
||||
char outseq[ 8] ; /* output buffer for keystroke sequence */
|
||||
|
||||
/* prompt the user to type in a key to unbind */
|
||||
mlwrite( "unbind-key: ") ;
|
||||
mloutstr( "unbind-key: ") ;
|
||||
|
||||
/* get the command sequence to unbind */
|
||||
int c = getckey( FALSE) ; /* get a command sequence */
|
||||
@ -194,19 +190,17 @@ BINDABLE( unbindkey) {
|
||||
|
||||
/* prefix key sequence can't be undound, just redefined */
|
||||
if( c == reptc || c == abortc)
|
||||
return cmdfail( "(Can't unbind prefix)") ;
|
||||
return mloutfail( "(Can't unbind prefix)") ;
|
||||
|
||||
/* if it isn't bound, bitch */
|
||||
if( delkeybinding( c) == FALSE) {
|
||||
mlwrite( "(Key not bound)") ;
|
||||
return FALSE ;
|
||||
}
|
||||
if( delkeybinding( c) == FALSE)
|
||||
return mloutfail( "(Key not bound)") ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
/*
|
||||
* does source include sub?
|
||||
|
||||
/* does source include sub?
|
||||
*
|
||||
* char *source; string to search in
|
||||
* char *sub; substring to look for
|
||||
@ -232,6 +226,7 @@ static boolean strinc( const char *source, const char *sub) {
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
|
||||
/* describe bindings
|
||||
* bring up a fake buffer and list the key bindings
|
||||
* into it with view mode
|
||||
@ -240,6 +235,7 @@ BINDABLE( desbind) {
|
||||
return buildlist( "") ;
|
||||
}
|
||||
|
||||
|
||||
/* Apropos (List functions that match a substring) */
|
||||
BINDABLE( apro) {
|
||||
char *mstring ; /* string to match cmd names to */
|
||||
@ -254,54 +250,51 @@ BINDABLE( apro) {
|
||||
return status ;
|
||||
}
|
||||
|
||||
/*
|
||||
* build a binding list (limited or full)
|
||||
|
||||
/* build a binding list (limited or full)
|
||||
*
|
||||
* char *mstring; match string if a partial list, "" matches all
|
||||
*/
|
||||
static int buildlist( char *mstring) {
|
||||
struct window *wp; /* scanning pointer to windows */
|
||||
kbind_p ktp; /* pointer into the command table */
|
||||
nbind_p nptr;/* pointer into the name binding table */
|
||||
struct buffer *bp; /* buffer to put binding list into */
|
||||
char outseq[80]; /* output buffer for keystroke sequence */
|
||||
#define PADDING 28
|
||||
char outseq[ PADDING + 8] ; /* output buffer for command + keystroke */
|
||||
|
||||
/* split the current window to make room for the binding list */
|
||||
/* split the current window to make room for the binding list */
|
||||
if( wheadp->w_wndp == NULL /* One window */
|
||||
&& splitwind( FALSE, 1) == FALSE) /* Split it */
|
||||
return FALSE;
|
||||
return FALSE ;
|
||||
|
||||
/* and get a buffer for it */
|
||||
bp = bfind("*Binding list*", TRUE, 0);
|
||||
/* and get a buffer for it */
|
||||
buffer_p bp = bfind( "*Binding list*", TRUE, 0) ;
|
||||
if( bp == NULL || bclear( bp) == FALSE)
|
||||
return cmdfail( "Can't display binding list") ;
|
||||
return mloutfail( "Can't display binding list") ;
|
||||
|
||||
/* let us know this is in progress */
|
||||
mlwrite("(Building binding list)");
|
||||
/* let us know this is in progress */
|
||||
mloutstr( "(Building binding list)") ;
|
||||
|
||||
/* disconnect the current buffer */
|
||||
if (--curbp->b_nwnd == 0) { /* Last use. */
|
||||
curbp->b_dotp = curwp->w_dotp;
|
||||
curbp->b_doto = curwp->w_doto;
|
||||
curbp->b_markp = curwp->w_markp;
|
||||
curbp->b_marko = curwp->w_marko;
|
||||
/* disconnect the current buffer */
|
||||
if( --curbp->b_nwnd == 0) { /* Last use. */
|
||||
curbp->b_dotp = curwp->w_dotp ;
|
||||
curbp->b_doto = curwp->w_doto ;
|
||||
curbp->b_markp = curwp->w_markp ;
|
||||
curbp->b_marko = curwp->w_marko ;
|
||||
}
|
||||
|
||||
/* connect the current window to this buffer */
|
||||
curbp = bp; /* make this buffer current in current window */
|
||||
bp->b_mode = 0; /* no modes active in binding list */
|
||||
bp->b_nwnd++; /* mark us as more in use */
|
||||
wp = curwp;
|
||||
wp->w_bufp = bp;
|
||||
wp->w_linep = bp->b_linep;
|
||||
wp->w_flag = WFHARD | WFFORCE;
|
||||
wp->w_dotp = bp->b_dotp;
|
||||
wp->w_doto = bp->b_doto;
|
||||
wp->w_markp = NULL;
|
||||
wp->w_marko = 0;
|
||||
/* connect the current window to this buffer */
|
||||
curbp = bp ; /* make this buffer current in current window */
|
||||
bp->b_mode = 0 ; /* no modes active in binding list */
|
||||
bp->b_nwnd++ ; /* mark us as more in use */
|
||||
window_p wp = curwp ;
|
||||
wp->w_bufp = bp ;
|
||||
wp->w_linep = bp->b_linep ;
|
||||
wp->w_flag = WFHARD | WFFORCE ;
|
||||
wp->w_dotp = bp->b_dotp ;
|
||||
wp->w_doto = bp->b_doto ;
|
||||
wp->w_markp = NULL ;
|
||||
wp->w_marko = 0 ;
|
||||
|
||||
/* build the contents of this window, inserting it line by line */
|
||||
for( nptr = names ; nptr->n_func != NULL ; nptr++) {
|
||||
/* build the contents of this window, inserting it line by line */
|
||||
for( nbind_p nptr = names ; nptr->n_func != NULL ; nptr++) {
|
||||
int cpos ; /* current position to use in outseq */
|
||||
|
||||
/* if we are executing an apropos command..... */
|
||||
@ -311,47 +304,47 @@ static int buildlist( char *mstring) {
|
||||
|
||||
/* add in the command name */
|
||||
mystrscpy( outseq, bind_name( nptr), sizeof outseq) ;
|
||||
cpos = strlen(outseq);
|
||||
cpos = strlen( outseq) ;
|
||||
|
||||
/* search down any keys bound to this */
|
||||
for( ktp = keytab ; ktp->k_code != 0 ; ktp++) {
|
||||
for( kbind_p ktp = keytab ; ktp->k_code != 0 ; ktp++) {
|
||||
if( ktp->k_nbp == nptr) {
|
||||
/* padd out some spaces */
|
||||
while (cpos < 28)
|
||||
outseq[cpos++] = ' ';
|
||||
while( cpos < PADDING)
|
||||
outseq[ cpos++] = ' ' ;
|
||||
|
||||
/* add in the command sequence */
|
||||
cmdstr(ktp->k_code, &outseq[cpos]);
|
||||
strcat(outseq, "\n");
|
||||
cmdstr( ktp->k_code, &outseq[ cpos]) ;
|
||||
strcat( outseq, "\n") ;
|
||||
|
||||
/* and add it as a line into the buffer */
|
||||
if (linstr(outseq) != TRUE)
|
||||
return FALSE;
|
||||
if( linstr( outseq) != TRUE)
|
||||
return FALSE ;
|
||||
|
||||
cpos = 0; /* and clear the line */
|
||||
cpos = 0 ; /* and clear the line */
|
||||
}
|
||||
}
|
||||
|
||||
/* if no key was bound, we need to dump it anyway */
|
||||
if (cpos > 0) {
|
||||
outseq[cpos++] = '\n';
|
||||
outseq[cpos] = 0;
|
||||
if (linstr(outseq) != TRUE)
|
||||
return FALSE;
|
||||
if( cpos > 0) {
|
||||
outseq[ cpos++] = '\n';
|
||||
outseq[ cpos] = 0;
|
||||
if( linstr( outseq) != TRUE)
|
||||
return FALSE ;
|
||||
}
|
||||
}
|
||||
|
||||
bp->b_mode |= MDVIEW; /* put this buffer view mode */
|
||||
bp->b_flag &= ~BFCHG; /* don't flag this as a change */
|
||||
wp->w_dotp = lforw(bp->b_linep); /* back to the beginning */
|
||||
wp->w_doto = 0;
|
||||
bp->b_mode |= MDVIEW ; /* put this buffer view mode */
|
||||
bp->b_flag &= ~BFCHG ; /* don't flag this as a change */
|
||||
wp->w_dotp = lforw( bp->b_linep) ; /* back to the beginning */
|
||||
wp->w_doto = 0 ;
|
||||
upmode() ; /* and update ALL mode lines */
|
||||
mlwrite(""); /* clear the mode line */
|
||||
return TRUE;
|
||||
mloutstr( "") ; /* clear the mode line */
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
/*
|
||||
* get a command key sequence from the keyboard
|
||||
|
||||
/* get a command key sequence from the keyboard
|
||||
*
|
||||
* int mflag; going for a meta sequence?
|
||||
* returns ~0 on failure
|
||||
@ -378,8 +371,8 @@ static unsigned int getckey( int mflag) {
|
||||
return c ;
|
||||
}
|
||||
|
||||
/*
|
||||
* execute the startup file
|
||||
|
||||
/* execute the startup file
|
||||
*
|
||||
* char *fname; name of startup file (null if default)
|
||||
*/
|
||||
@ -394,8 +387,8 @@ int startup( const char *fname) {
|
||||
return dofile( fname) ; /* otherwise, execute the sucker */
|
||||
}
|
||||
|
||||
/*
|
||||
* change a key command to a string we can print out
|
||||
|
||||
/* change a key command to a string we can print out
|
||||
*
|
||||
* int c; sequence to translate
|
||||
* char *seq; destination string for sequence
|
||||
@ -433,6 +426,7 @@ static char *cmdstr( unsigned c, char *seq) {
|
||||
return seq ;
|
||||
}
|
||||
|
||||
|
||||
static const char *getfname( unsigned keycode, const char *failmsg) {
|
||||
/* takes a key code and gets the name of the function bound to it */
|
||||
kbind_p kbp = getkeybinding( keycode) ;
|
||||
@ -444,6 +438,7 @@ static const char *getfname( unsigned keycode, const char *failmsg) {
|
||||
return found ;
|
||||
}
|
||||
|
||||
|
||||
/* stock:
|
||||
* String key name TO Command Key
|
||||
*
|
||||
@ -494,8 +489,8 @@ static unsigned int stock( char *keyname) {
|
||||
return c ;
|
||||
}
|
||||
|
||||
/*
|
||||
* string key name to binding name....
|
||||
|
||||
/* string key name to binding name....
|
||||
*
|
||||
* char *skey; name of key to get binding for
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user