Fix: avoid extra NL when newline/yank/insert-file at end of last line of non empty buffer.

Refactor yank.
Detab before commit.
This commit is contained in:
Renaud 2024-04-04 12:03:24 +08:00
parent 12d307b5b4
commit 1ee0ebf4b9
1 changed files with 106 additions and 108 deletions

40
line.c
View File

@ -42,7 +42,7 @@ static int ldelnewline( void) ;
typedef struct kill {
struct kill *d_next ; /* Link to next chunk, NULL if last. */
char d_chunk[ KBLOCK] ; /* Deleted text. */
unsigned char d_chunk[ KBLOCK] ; /* Deleted text. */
} *kill_p ;
static kill_p kbufp = NULL ; /* current kill buffer chunk pointer */
@ -467,6 +467,16 @@ boolean lnewline( void) {
#endif
line_p lp1 = curwp->w_dotp ; /* Get the address and */
int doto = curwp->w_doto ; /* offset of "." */
/* at end of last line of non empty buffer */
if( lp1->l_fp == curbp->b_linep && lp1 != curbp->b_linep &&
doto == lp1->l_used) {
/* move dot at end of buffer, no nl inserted */
curwp->w_dotp = curbp->b_linep ;
curwp->w_doto = 0 ;
return TRUE ;
}
line_p lp2 = lalloc( doto) ; /* New first half line */
if( lp2 == NULL)
return FALSE ;
@ -480,7 +490,7 @@ boolean lnewline( void) {
lp2->l_bp->l_fp = lp2 ;
for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp)
if( wp->w_bufp == curbp) {
if( wp->w_linep == lp1)
if( wp->w_linep == lp1) /* adjust top line of window */
wp->w_linep = lp2 ;
if( wp->w_dotp == lp1) {
@ -789,40 +799,28 @@ int kinsert( int c) {
* check for errors. Bound to "C-Y".
*/
BINDABLE( yank) {
int c;
int i;
char *sp; /* pointer into string to insert */
kill_p kp; /* pointer into kill buffer */
assert( !(curbp->b_mode & MDVIEW)) ;
if (n < 0)
return FALSE;
/* make sure there is something to yank */
if (kbufh == NULL)
return TRUE; /* not an error, just nothing */
/* for each time.... */
while (n--) {
kp = kbufh;
while (kp != NULL) {
if (kp->d_next == NULL)
i = kused;
else
i = KBLOCK;
sp = kp->d_chunk;
for( kill_p kp = kbufh ; kp != NULL ; kp = kp->d_next) {
unsigned char *sp = kp->d_chunk ;
int i = (kp->d_next == NULL) ? kused : KBLOCK ;
while (i--) {
if ((c = *sp++) == '\n') {
if (lnewline() == FALSE)
return FALSE;
} else {
if (linsert_byte(1, c) == FALSE)
int c = *sp++ ;
if( FALSE == (( c == '\n') ? lnewline() : linsert_byte( 1, c)))
return FALSE ;
}
}
kp = kp->d_next;
}
}
return TRUE;
}