Merge common implementation of case-region-lower and case-region-upper.

This commit is contained in:
Renaud 2021-08-07 21:34:13 +08:00
parent 1cdc889f6f
commit 1fbb2fc565
1 changed files with 92 additions and 103 deletions

195
region.c
View File

@ -1,13 +1,11 @@
/* region.c -- implements region.h */ /* region.c -- implements region.h */
#include "region.h" #include "region.h"
/* region.c /* The routines in this file deal with the region, that magic space between
* "." and mark. Some functions are commands. Some functions are just for
* The routines in this file deal with the region, that magic space internal use.
* between "." and mark. Some functions are commands. Some functions are
* just for internal use. Modified by Petri Kutvonen
*
* Modified by Petri Kutvonen
*/ */
#include <assert.h> #include <assert.h>
@ -20,123 +18,114 @@
#include "random.h" #include "random.h"
#include "window.h" #include "window.h"
/* Kill the region. Ask "getregion" to figure out the bounds of the /* Kill the region. Ask getregion() to figure out the bounds of the
* region. Move "." to the start, and kill the characters. Bound to region. Move "." to the start, and kill the characters. Bound to C-W
* "C-W". kill-region.
*/ */
BINDABLE( killregion) { BINDABLE( killregion) {
int s; region_t region ;
region_t region;
assert( !(curbp->b_mode & MDVIEW)) ; assert( !(curbp->b_mode & MDVIEW)) ;
if ((s = getregion(&region)) != TRUE) int ret = getregion( &region) ;
return s; if( ret != TRUE)
if ((lastflag & CFKILL) == 0) /* This is a kill type */ return ret ;
kdelete(); /* command, so do magic */
thisflag |= CFKILL; /* kill buffer stuff. */ if( (lastflag & CFKILL) == 0) /* This is a kill type */
curwp->w_dotp = region.r_linep; kdelete() ; /* command, so do magic */
curwp->w_doto = region.r_offset;
return ldelete(region.r_size, TRUE); thisflag |= CFKILL ; /* kill buffer stuff. */
curwp->w_dotp = region.r_linep ;
curwp->w_doto = region.r_offset ;
return ldelete( region.r_size, TRUE) ;
} }
/* Copy all of the characters in the region to the kill buffer. Don't move /* Copy all of the characters in the region to the kill buffer. Don't move
* dot at all. This is a bit like a kill region followed by a yank. Bound dot at all. This is a bit like a kill region followed by a yank. Bound
* to "M-W". to M-W copy-region.
*/ */
BINDABLE( copyregion) { BINDABLE( copyregion) {
line_p linep; region_t region ;
int loffs;
int s;
region_t region;
if ((s = getregion(&region)) != TRUE) int ret = getregion( &region) ;
return s; if( ret != TRUE)
if ((lastflag & CFKILL) == 0) /* Kill type command. */ return ret ;
kdelete();
thisflag |= CFKILL; if( (lastflag & CFKILL) == 0) /* Kill type command. */
linep = region.r_linep; /* Current line. */ kdelete() ;
loffs = region.r_offset; /* Current offset. */
while (region.r_size--) { thisflag |= CFKILL ;
if (loffs == llength(linep)) { /* End of line. */ line_p linep = region.r_linep ; /* Current line. */
if ((s = kinsert('\n')) != TRUE) int loffs = region.r_offset ; /* Current offset. */
return s; while( region.r_size--) {
linep = lforw(linep); if( loffs == llength( linep)) { /* End of line. */
loffs = 0; ret = kinsert( '\n') ;
if( ret != TRUE)
return ret ;
linep = lforw( linep) ;
loffs = 0 ;
} else { /* Middle of line. */ } else { /* Middle of line. */
if ((s = kinsert(lgetc(linep, loffs))) != TRUE) ret = kinsert( lgetc( linep, loffs)) ;
return s; if( ret != TRUE)
++loffs; return ret ;
++loffs ;
} }
} }
mloutstr( "(region copied)") ; mloutstr( "(region copied)") ;
return TRUE; return TRUE ;
} }
/* Lower case region. Zap all of the upper case characters in the region /* Lower case region & Upper case region. Zap all of the upper/lower case
* to lower case. Use the region code to set the limits. Scan the buffer, characters in the region to lower/upper case. Use the region code to
* doing the changes. Call "lchange" to ensure that redisplay is done in set the limits. Scan the buffer, doing the changes. Call "lchange" to
* all buffers. Bound to "C-X C-L". ensure that redisplay is done in all buffers. Bound to C-X C-L
case-region-lower and C-X C-U case-region-upper.
*/ */
static int utol( int c) {
return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : -1 ;
}
static int ltou( int c) {
return (c >= 'a' && c <= 'z') ? c + 'A' - 'a' : -1 ;
}
static int caseregion( int (* cconv)( int)) {
region_t region ;
assert( !(curbp->b_mode & MDVIEW)) ;
int ret = getregion( &region) ;
if( ret != TRUE)
return ret ;
lchange( WFHARD) ;
line_p linep = region.r_linep ;
int loffs = region.r_offset ;
while( region.r_size--) {
if( loffs == llength( linep)) {
linep = lforw( linep) ;
loffs = 0 ;
} else {
int c = cconv( lgetc( linep, loffs)) ;
if( c != -1)
lputc( linep, loffs, c) ;
++loffs ;
}
}
return TRUE ;
}
BINDABLE( lowerregion) { BINDABLE( lowerregion) {
line_p linep; return caseregion( utol) ;
int loffs;
int c;
int s;
region_t region;
assert( !(curbp->b_mode & MDVIEW)) ;
if ((s = getregion(&region)) != TRUE)
return s;
lchange(WFHARD);
linep = region.r_linep;
loffs = region.r_offset;
while (region.r_size--) {
if (loffs == llength(linep)) {
linep = lforw(linep);
loffs = 0;
} else {
c = lgetc(linep, loffs);
if (c >= 'A' && c <= 'Z')
lputc(linep, loffs, c + 'a' - 'A');
++loffs;
}
}
return TRUE;
} }
/* Upper case region. Zap all of the lower case characters in the region
* to upper case. Use the region code to set the limits. Scan the buffer,
* doing the changes. Call "lchange" to ensure that redisplay is done in
* all buffers. Bound to "C-X C-L".
*/
BINDABLE( upperregion) { BINDABLE( upperregion) {
line_p linep; return caseregion( ltou) ;
int loffs;
int c;
int s;
region_t region;
assert( !(curbp->b_mode & MDVIEW)) ;
if ((s = getregion(&region)) != TRUE)
return s;
lchange(WFHARD);
linep = region.r_linep;
loffs = region.r_offset;
while (region.r_size--) {
if (loffs == llength(linep)) {
linep = lforw(linep);
loffs = 0;
} else {
c = lgetc(linep, loffs);
if (c >= 'a' && c <= 'z')
lputc(linep, loffs, c - 'a' + 'A');
++loffs;
}
}
return TRUE;
} }
/* This routine figures out the bounds of the region in the current window, /* This routine figures out the bounds of the region in the current window,