From a42c60045db69789ed921dc774c8ba6462fd9419 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Thu, 25 Feb 2016 17:08:27 +0800 Subject: [PATCH] Improve consistency of tab insertion behaviour while indenting and brace matching. --- line.c | 31 ++++++++++++++++++------------- random.c | 14 ++++++++------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/line.c b/line.c index 94b9af4..7e3e47c 100644 --- a/line.c +++ b/line.c @@ -392,24 +392,29 @@ static int linsert_byte(int n, int c) } int linsert( int n, unicode_t c) { - char utf8[6]; - int bytes, i ; - - assert( n > 0) ; + assert( n >= 0) ; if (curbp->b_mode & MDVIEW) /* don't allow this command if */ return rdonly(); /* we are in read only mode */ - bytes = unicode_to_utf8(c, utf8) ; - if (bytes == 1) - return linsert_byte(n, (unsigned char) utf8[0]); - for (i = 0; i < n; i++) { - int j; - for (j = 0; j < bytes; j++) { - unsigned char c = utf8[j]; - if (!linsert_byte(1, c)) - return FALSE; + if( n > 0) { + char utf8[ 6] ; + int bytes, i ; + + bytes = unicode_to_utf8(c, utf8) ; + if (bytes == 1) + return linsert_byte(n, (unsigned char) utf8[0]); + + for (i = 0; i < n; i++) { + int j; + + for (j = 0; j < bytes; j++) { + unsigned char c = utf8[j]; + if (!linsert_byte(1, c)) + return FALSE; + } } } + return TRUE; } diff --git a/random.c b/random.c index 8f7bb26..4a58703 100644 --- a/random.c +++ b/random.c @@ -661,8 +661,9 @@ int insbrace(int n, int c) while (getccol(FALSE) > target) backdel(FALSE, 1); else { /* on doit en inserer */ - while (target - getccol(FALSE) >= 8) - linsert(1, '\t'); + while (target - getccol(FALSE) >= tabwidth) + insert_tab( FALSE, 1) ; + linsert(target - getccol(FALSE), ' '); } } @@ -681,7 +682,6 @@ int insbrace(int n, int c) */ int insbrace(int n, int c) { - int ch; /* last character before input */ int i; int target; /* column brace should go after */ @@ -691,6 +691,8 @@ int insbrace(int n, int c) /* scan to see if all space before this is white space */ for (i = curwp->w_doto - 1; i >= 0; --i) { + int ch; /* last character before input */ + ch = lgetc(curwp->w_dotp, i); if (ch != ' ' && ch != '\t') return linsert(n, c); @@ -698,8 +700,8 @@ int insbrace(int n, int c) /* delete back first */ target = getccol(FALSE); /* calc where we will delete to */ - target -= 1; - target -= target % (tabsize == 0 ? 8 : tabsize); + i = target % tabwidth ; + target -= ( i != 0) ? i : tabwidth ; while (getccol(FALSE) > target) backdel(FALSE, 1); @@ -774,7 +776,7 @@ int indent( int f, int n) { nicol %= tabwidth ; /* # of space to insert */ while( n--) if( lnewline() == FALSE - || ( i != 0 && linsert( i, '\t') == FALSE) + || ( i != 0 && insert_tab( FALSE, i) == FALSE) || ( nicol != 0 && linsert( nicol, ' ') == FALSE)) return FALSE ;