Setting $curcol works on line containing UTF-8 encoded characters.

This commit is contained in:
Renaud 2017-05-18 12:16:07 +08:00
parent df0d663b35
commit 674a3baba4
3 changed files with 100 additions and 12 deletions

89
maze.cmd Normal file
View File

@ -0,0 +1,89 @@
# 5 set $seed
# setup direction offsets
set %D1 0
set %D2 1
set %D3 0
set %D4 -1
set %D5 0
# draw the maze layout
$curwidth insert-string " "
newline
set %w &sub $curwidth 2
!if &equ 0 &mod %w 2
set %w &sub %w 1
!endif
insert-string " "
%w insert-string ""
insert-string " "
set %buf $line
set %l &sub $pagelen 4
!if &equ 1 &mod %l 2
set %l &sub %l 1
!endif
set %cnt %l
!while &less 0 %cnt
set %cnt &sub %cnt 1
newline
insert-string %buf
!endwhile
newline
set %w &add %w 1
%w insert-string " "
set %l &add %l 3
# draw the exit
set $curline &sub %l 2
set $curcol &sub %w 1
set $curchar 32
# draw the maze
set %x 2
set %y 3
set $curline %y
set $curcol %x
set $curchar 32
set %flags 0
set %cnt &tim &sub &div %w 2 1 &sub &div %l 2 1
!while &les 1 %cnt
!if &or &equ %flags 15 &not &equ $curchar 32
set %flags 0
set %y &add %y 2
!if &equ %y %l
set %y 3
set %x &add %x 2
!if &equ %x %w
set %x 2
!endif
!endif
!else
set %D &rnd 4
set %OX &ind &cat "%D" %D
set %OY &ind &cat "%D" &add %D 1
set $curline &add %y &tim 2 %OY
set %i &add %x &tim 2 %OX
set $curcol %i
!if &equ $curchar 32
!if &equ %D 3 # turn direction into bitmask {1,2,4,8}
set %D 8
!endif
set %flags &bor %flags %D # mark direction as checked
!else
set $curchar 32
set %y $curline # update current position
set %x %i
set $curline &sub %y %OY # erase path between old and cur pos
set $curcol &sub %x %OX
set $curchar 32
set %flags 0
set %cnt &sub %cnt 1
!endif
!endif
set $curline %y
set $curcol %x
!endwhile
# draw the entrance
set $curline 3
set $curcol 1
set $curchar 32

View File

@ -181,29 +181,28 @@ int getccol(int bflg)
*
* int pos; position to set cursor
*/
int setccol(int pos)
{
boolean setccol( int pos) {
int i; /* index into current line */
int col; /* current cursor column */
int llen; /* length of line in bytes */
char *text ;
col = 0;
llen = llength(curwp->w_dotp);
text = curwp->w_dotp->l_text ;
/* scan the line until we are at or past the target column */
for (i = 0; i < llen; ++i) {
int c; /* character being scanned */
for( i = 0 ; i < llen && col < pos ; ) {
unicode_t c ; /* character being scanned */
/* upon reaching the target, drop out */
if (col >= pos)
break;
/* advance one character */
c = lgetc(curwp->w_dotp, i);
/* advance one character */
i += utf8_to_unicode( text, i, llen, &c) ;
if (c == '\t')
col += tabwidth - col % tabwidth ;
else if (c < 0x20 || c == 0x7F)
else if (c < 0x20 || c == 0x7F) /* displayed as ^C */
col += 2 ;
else if (c >= 0x80 && c <= 0xa0) /* displayed as \xx */
col += 3 ;
else
col += 1 ;
}

View File

@ -23,7 +23,7 @@ int setfillcol( int f, int n) ;
int showcpos( int f, int n) ;
int getcline( void) ;
int getccol( int bflg) ;
int setccol( int pos) ;
boolean setccol( int pos) ;
boolean twiddle( int f, int n) ;
int quote( int f, int n) ;
int insert_tab( int f, int n) ;