1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-18 23:36:23 -05:00

Rewrite cinsert to avoid buffer size constraint when doing indentation in C mode.

This commit is contained in:
Renaud 2016-02-25 21:50:04 +08:00
parent a42c60045d
commit 775b16c7b1

View File

@ -540,8 +540,7 @@ static int cinsert(void)
char *cptr; /* string pointer into text to copy */ char *cptr; /* string pointer into text to copy */
int tptr; /* index to scan into line */ int tptr; /* index to scan into line */
int bracef; /* was there a brace at the end of line? */ int bracef; /* was there a brace at the end of line? */
int i; int i, nicol ;
char ichar[NSTRING]; /* buffer to hold indent of last line */
/* grab a pointer to text to copy indentation from */ /* grab a pointer to text to copy indentation from */
cptr = &curwp->w_dotp->l_text[0]; cptr = &curwp->w_dotp->l_text[0];
@ -551,25 +550,43 @@ static int cinsert(void)
bracef = (tptr > 0) && (cptr[ tptr - 1] == '{') ; bracef = (tptr > 0) && (cptr[ tptr - 1] == '{') ;
/* save the indent of the previous line */ /* save the indent of the previous line */
i = 0; nicol = 0 ;
while ((i < tptr) && (cptr[i] == ' ' || cptr[i] == '\t') for( i = 0 ; i < tptr ; i += 1) {
&& (i < NSTRING - 1)) { char ch ;
ichar[i] = cptr[i];
++i;
}
ichar[i] = 0; /* terminate it */
ch = cptr[ i] ;
if( ch == ' ')
nicol += 1 ;
else if( ch == '\t')
nicol += tabwidth - nicol % tabwidth ;
else
break ;
}
if( i == tptr) { /* all line is blank */
curwp->w_doto = 0 ; /* gotobol */
lnewline() ;
curwp->w_doto = tptr ; /* gotoeol */
} else {
/* put in the newline */ /* put in the newline */
if (lnewline() == FALSE) if (lnewline() == FALSE)
return FALSE; return FALSE;
/* and the saved indentation */ /* and the saved indentation */
linstr(ichar); i = nicol % tabwidth ; /* spaces */
nicol /= tabwidth ; /* tabs */
if( bracef) {
/* and one more tab for a brace */
nicol += 1 ;
i = 0 ;
}
/* and one more tab for a brace */ if( nicol > 0)
if (bracef) insert_tab( FALSE, nicol) ;
insert_tab(FALSE, 1);
if( i > 0)
linsert( i, ' ') ;
}
#if SCROLLCODE #if SCROLLCODE
curwp->w_flag |= WFINS; curwp->w_flag |= WFINS;
#endif #endif