bindable: code review and minor refactoring.

This commit is contained in:
Renaud 2021-08-09 15:45:01 +08:00
parent 720603ac8e
commit 665d9ca1da
2 changed files with 95 additions and 105 deletions

View File

@ -3,146 +3,127 @@
#include <stdlib.h> #include <stdlib.h>
#include "defines.h" #include "defines.h"
#include "buffer.h" #include "buffer.h"
#include "display.h" #include "display.h" /* vttidy() */
#include "estruct.h"
#include "file.h" #include "file.h"
#include "input.h" #include "input.h"
#include "lock.h" #include "lock.h"
#include "mlout.h" #include "mlout.h"
#include "terminal.h" #include "terminal.h"
/*
* Fancy quit command, as implemented by Norm. If the any buffer has /* Fancy quit command, as implemented by Norm. If any buffer has changed
* changed do a write on that buffer and exit emacs, otherwise simply exit. do a write on that buffer and exit emacs, otherwise simply exit.
*/ */
int quickexit(int f, int n) BINDABLE( quickexit) {
{ buffer_p oldcb = curbp ; /* save in case we fail */
struct buffer *bp; /* scanning pointer to buffers */ for( buffer_p bp = bheadp ; bp != NULL ; bp = bp->b_bufp) {
struct buffer *oldcb; /* original current buffer */ if( (bp->b_flag & (BFCHG | BFTRUNC | BFINVS)) == BFCHG) {
int status; /* Changed, Not truncated and real buffer */
curbp = bp ; /* make that buffer cur */
mloutfmt( "(Saving %s)", bp->b_fname) ;
int status = filesave( f, n) ;
if( status != TRUE) {
curbp = oldcb ; /* restore curbp */
return status ;
}
}
}
oldcb = curbp; /* save in case we fail */ return quit( f, n) ; /* conditionally quit */
bp = bheadp;
while (bp != NULL) {
if ((bp->b_flag & BFCHG) != 0 /* Changed. */
&& (bp->b_flag & BFTRUNC) == 0 /* Not truncated P.K. */
&& (bp->b_flag & BFINVS) == 0) { /* Real. */
curbp = bp; /* make that buffer cur */
mloutfmt( "(Saving %s)", bp->b_fname) ;
#if PKCODE
#else
mloutstr( "\n") ;
#endif
if ((status = filesave(f, n)) != TRUE) {
curbp = oldcb; /* restore curbp */
return status;
}
}
bp = bp->b_bufp; /* on to the next buffer */
}
quit(f, n); /* conditionally quit */
return TRUE;
} }
/*
* Quit command. If an argument, always quit. Otherwise confirm if a buffer /* Quit command. If an argument, always quit. Otherwise confirm if a buffer
* has been changed and not written out. Normally bound to "C-X C-C". * has been changed and not written out. Normally bound to "C-X C-C".
*/ */
int quit(int f, int n) BINDABLE( quit) {
{ int s ; /* status of user query */
int s;
if (f != FALSE /* Argument forces it. */ if( f != FALSE /* Argument forces it. */
|| anycb() == FALSE /* All buffers clean. */ || anycb() == FALSE /* All buffers clean. */
/* User says it's OK. */ /* User says it's OK. */
|| (s = || (s = mlyesno( "Modified buffers exist. Leave anyway")) == TRUE) {
mlyesno("Modified buffers exist. Leave anyway")) == TRUE) { #if (FILOCK && BSD) || SVR4
#if (FILOCK && BSD) || SVR4 if( lockrel() != TRUE) {
if (lockrel() != TRUE) { TTputc('\n') ;
TTputc('\n'); TTputc('\r') ;
TTputc('\r'); TTclose() ;
TTclose(); TTkclose() ;
TTkclose(); exit( EXIT_FAILURE) ;
exit( EXIT_FAILURE) ; }
}
#endif #endif
vttidy(); vttidy() ;
if (f) if( f)
exit(n); exit( n) ;
else else
exit( EXIT_SUCCESS) ; exit( EXIT_SUCCESS) ;
} }
mloutstr( "") ;
return s; mloutstr( "") ;
return s ;
} }
/*
* Begin a keyboard macro. /* Begin a keyboard macro.
* Error if not at the top level in keyboard processing. Set up variables and * Error if not at the top level in keyboard processing. Set up variables and
* return. * return.
*/ */
int ctlxlp(int f, int n) BINDABLE( ctlxlp) {
{ if( kbdmode != STOP)
if (kbdmode != STOP) { return mloutfail( "%Macro already active") ;
mloutstr( "%Macro already active") ;
return FALSE; mloutstr( "(Start macro)") ;
} kbdptr = kbdm ;
mloutstr( "(Start macro)") ; kbdend = kbdptr ;
kbdptr = &kbdm[0]; kbdmode = RECORD ;
kbdend = kbdptr; return TRUE ;
kbdmode = RECORD;
return TRUE;
} }
/*
* End keyboard macro. Check for the same limit conditions as the above /* End keyboard macro. Check for the same limit conditions as the above
* routine. Set up the variables and return to the caller. * routine. Set up the variables and return to the caller.
*/ */
int ctlxrp(int f, int n) BINDABLE( ctlxrp) {
{ if( kbdmode == STOP)
if (kbdmode == STOP) { return mloutfail( "%Macro not active") ;
mloutstr( "%Macro not active") ;
return FALSE; if (kbdmode == RECORD) {
} mloutstr( "(End macro)") ;
if (kbdmode == RECORD) { kbdmode = STOP;
mloutstr( "(End macro)") ; }
kbdmode = STOP;
} return TRUE ;
return TRUE;
} }
/*
* Execute a macro. /* Execute a macro.
* The command argument is the number of times to loop. Quit as soon as a * The command argument is the number of times to loop. Quit as soon as a
* command gets an error. Return TRUE if all ok, else FALSE. * command gets an error. Return TRUE if all ok, else FALSE.
*/ */
int ctlxe(int f, int n) BINDABLE( ctlxe) {
{ if( kbdmode != STOP)
if (kbdmode != STOP) { return mloutfail( "%Macro already active") ;
mloutstr( "%Macro already active") ;
return FALSE; if( n <= 0)
} return TRUE ;
if (n <= 0)
return TRUE; kbdrep = n ; /* remember how many times to execute */
kbdrep = n; /* remember how many times to execute */ kbdmode = PLAY ; /* start us in play mode */
kbdmode = PLAY; /* start us in play mode */ kbdptr = kbdm ; /* at the beginning */
kbdptr = &kbdm[0]; /* at the beginning */ return TRUE ;
return TRUE;
} }
/*
* abort: /* abort:
* Beep the beeper. Kill off any keyboard macro, etc., that is in progress. * Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
* Sometimes called as a routine, to do general aborting of stuff. * Sometimes called as a routine, to do general aborting of stuff.
*/ */
int ctrlg( int f, int n) { BINDABLE( ctrlg) {
kbdmode = STOP ; kbdmode = STOP ;
mloutfmt( "%B(Aborted)") ; mloutfmt( "%B(Aborted)") ;
return ABORT ; return ABORT ;
} }
/* end of bindable.c */ /* end of bindable.c */

View File

@ -1,3 +1,8 @@
/* bindable.h -- misc bindable functions */
#ifndef _BINDABLE_H_
#define _BINDABLE_H_
#include "names.h" #include "names.h"
/* functions that can be bound to keys or procedure names */ /* functions that can be bound to keys or procedure names */
@ -7,3 +12,7 @@ BINDABLE( ctlxlp) ;
BINDABLE( ctlxrp) ; BINDABLE( ctlxrp) ;
BINDABLE( ctlxe) ; BINDABLE( ctlxe) ;
BINDABLE( ctrlg) ; BINDABLE( ctrlg) ;
#endif
/* end of bindable.h */