Skip to content

Commit 4e824fc

Browse files
Charles Campbellvim-scripts
authored andcommitted
Version 13
improved string length handling for multi-character characters various bug fixes
1 parent 22ed063 commit 4e824fc

File tree

4 files changed

+195
-143
lines changed

4 files changed

+195
-143
lines changed

autoload/DrawIt.vim

Lines changed: 107 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
" Maintainer: Charles E. Campbell
44
" Authors: Charles E. Campbell <[email protected]> - NOSPAM
55
" Sylvain Viart ([email protected])
6-
" Version: 12
7-
" Date: Mar 12, 2013
6+
" Version: 13
7+
" Date: Nov 25, 2013
88
"
99
" Quick Setup: {{{1
1010
" tar -oxvf DrawIt.tar
@@ -41,7 +41,7 @@
4141
if &cp || exists("g:loaded_DrawIt")
4242
finish
4343
endif
44-
let g:loaded_DrawIt= "v12"
44+
let g:loaded_DrawIt= "v13"
4545
if v:version < 700
4646
echohl WarningMsg
4747
echo "***warning*** this version of DrawIt needs vim 7.0"
@@ -66,7 +66,7 @@ if !exists("g:drawit_xstrlen")
6666
endif
6767
endif
6868
if !exists("g:drawit_mode")
69-
let g:drawit_mode= 'N' " other options: =S (single-line utf-8,cp437) or =D (double-line utf-8,cp437)
69+
let g:drawit_mode= 'N' " other options: =S (single-line utf-*,cp437) or =D (double-line utf-*,cp437)
7070
endif
7171
if !exists("s:saveposn_count")
7272
let s:saveposn_count= 0
@@ -84,9 +84,9 @@ fun! DrawIt#DrawItStart(...)
8484
" call Dfunc("DrawItStart()")
8585

8686
if a:0 > 0
87-
if exists("bz:di_gfxchr") && b:di_gfxchr && (a:1 == 'S' || a:1 == 's')
87+
if exists("b:di_gfxchr") && b:di_gfxchr && (a:1 == 'S' || a:1 == 's')
8888
DIsngl
89-
elseif exists("bz:di_gfxchr") && b:di_gfxchr && (a:1 == 'D' || a:1 == 'd')
89+
elseif exists("b:di_gfxchr") && b:di_gfxchr && (a:1 == 'D' || a:1 == 'd')
9090
DIdbl
9191
elseif !exists("g:drawit_mode")
9292
let g:drawit_mode= 'N'
@@ -109,7 +109,7 @@ fun! DrawIt#DrawItStart(...)
109109
if !exists("b:drawit_keep_mouse")
110110
let b:drawit_keep_mouse= &mouse
111111
endif
112-
setlocal mouse=a
112+
setl mouse=a
113113

114114
" DrawItStart: set up DrawIt commands {{{3
115115
com! -nargs=1 -range SetBrush <line1>,<line2>call DrawIt#SetBrush(<q-args>)
@@ -125,10 +125,11 @@ fun! DrawIt#DrawItStart(...)
125125
if !exists("b:di_ellipse")|let b:di_ellipse = '*' |endif
126126

127127
let b:di_gfxchr= 1
128-
if &enc == 'utf-8'
129-
" Box drawing characters using utf-8
128+
if &enc == 'utf-8' || &enc == 'utf-16' || &enc == "ucs-4"
129+
" Box drawing characters using unicode
130130
" │ ─ ┌ ┐ └ ┘ ┬ ┴ ├ ┤ ┼ ╱ ╲ ╳
131131
" ║ ═ ╔ ╗ ╚ ╝ ╦ ╩ ╠ ╣ ╬
132+
" call Decho("box drawing characters using unicode")
132133
if !exists("b:di_Svert") |let b:di_Svert = '' |endif
133134
if !exists("b:di_Dvert") |let b:di_Dvert = '' |endif
134135
if !exists("b:di_Shoriz") |let b:di_Shoriz = '' |endif
@@ -155,7 +156,7 @@ fun! DrawIt#DrawItStart(...)
155156
if !exists("b:di_Supleft") |let b:di_Supleft = "" |endif " same as Sdownright
156157
if !exists("b:di_Scross") |let b:di_Scross = "" |endif
157158

158-
" Mixed Single-Double utf-8 box drawing characters
159+
" Mixed Single-Double unicode box drawing characters
159160
" ╞ ╟ ╡ ╢ ╤ ╥ ╧ ╪ ╫
160161
if !exists("b:di_DhSd") |let b:di_DhSd = '' |endif
161162
if !exists("b:di_DhSu") |let b:di_DhSu = '' |endif
@@ -170,7 +171,7 @@ fun! DrawIt#DrawItStart(...)
170171
if !exists("b:di_SuDh") |let b:di_SuDh = '' |endif
171172
if !exists("b:di_SuDr") |let b:di_SuDr = '' |endif
172173

173-
" Mixed Single-Double utf-8 box drawing corner characters
174+
" Mixed Single-Double unicode box drawing corner characters
174175
" ╒ ╓ ╕ ╖ ╘ ╙ ╛ ╜
175176
if !exists("b:di_cSdDr")| let b:di_cSdDr= ''| endif
176177
if !exists("b:di_cDdSr")| let b:di_cDdSr= ''| endif
@@ -180,8 +181,10 @@ fun! DrawIt#DrawItStart(...)
180181
if !exists("b:di_cSrDu")| let b:di_cSrDu= ''| endif
181182
if !exists("b:di_cDlSu")| let b:di_cDlSu= ''| endif
182183
if !exists("b:di_cSlDu")| let b:di_cSlDu= ''| endif
184+
183185
elseif &enc == 'cp437'
184186
" Box drawing characters using cp437 (dos)
187+
" call Decho("box drawing characters using cp437")
185188
if !exists("b:di_Svert") |let b:di_Svert = nr2char(179) |endif "
186189
if !exists("b:di_Dvert") |let b:di_Dvert = nr2char(186) |endif "
187190
if !exists("b:di_Shoriz") |let b:di_Shoriz = nr2char(196) |endif "
@@ -231,32 +234,16 @@ fun! DrawIt#DrawItStart(...)
231234
if !exists("b:di_cSrDu")| let b:di_cSrDu= nr2char(211)| endif "
232235
if !exists("b:di_cDlSu")| let b:di_cDlSu= nr2char(190)| endif "
233236
if !exists("b:di_cSlDu")| let b:di_cSlDu= nr2char(189)| endif "
237+
234238
else
235-
let b:di_gfxchr= 0
239+
" call Decho("regular box drawing characters only")
240+
let b:di_gfxchr = 0
236241
endif
237242

238243
" set up initial DrawIt behavior (as opposed to erase behavior)
239244
let b:di_erase = 0
240245

241-
" DrawItStart: option recording {{{3
242-
let b:di_aikeep = &l:ai
243-
let b:di_cinkeep = &l:cin
244-
let b:di_cpokeep = &l:cpo
245-
let b:di_etkeep = &l:et
246-
let b:di_fokeep = &l:fo
247-
let b:di_gdkeep = &l:gd
248-
let b:di_gokeep = &l:go
249-
let b:di_magickeep = &l:magic
250-
let b:di_remapkeep = &l:remap
251-
let b:di_repkeep = &l:report
252-
let b:di_sikeep = &l:si
253-
let b:di_stakeep = &l:sta
254-
let b:di_vekeep = &l:ve
255-
setlocal cpo&vim
256-
setlocal nocin noai nosi nogd sta et ve=all report=10000
257-
setlocal go-=aA
258-
setlocal fo-=a
259-
setlocal remap magic
246+
call s:DrawItSaveUserSettings()
260247

261248
" DrawItStart: save and unmap user maps {{{3
262249
let b:lastdir = 1
@@ -495,7 +482,61 @@ fun! DrawIt#DrawItStop()
495482
" DrawItStop: restore user map(s), if any {{{3
496483
call RestoreUserMaps("DrawIt")
497484

498-
" DrawItStop: restore user's options {{{3
485+
call s:DrawItRestoreUserSettings()
486+
487+
" DrawItStop: DrChip menu support: {{{3
488+
if has("gui_running") && has("menu") && &go =~# 'm'
489+
exe 'menu '.g:DrChipTopLvlMenu.'DrawIt.Start\ DrawIt<tab>\\di <Leader>di'
490+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Stop\ \ DrawIt'
491+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Toggle\ Erase\ Mode'
492+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Arrow'
493+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Box'
494+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Ellipse'
495+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Flood'
496+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Line'
497+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Make\ Blank\ Zone'
498+
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Append\ Blanks'
499+
endif
500+
" call Dret("DrawItStop")
501+
endfun
502+
503+
" ---------------------------------------------------------------------
504+
" s:DrawItSaveUserSettings: saves user settings, changes them to be safe for DrawIt {{{2
505+
" Use s:DrawItRestoreUserSettings() to restore.
506+
fun! s:DrawItSaveUserSettings()
507+
" call Dfunc("s:DrawItSaveUserSettings()")
508+
" save user settings
509+
let b:di_aikeep = &l:ai
510+
let b:di_cedit = &cedit
511+
let b:di_cinkeep = &l:cin
512+
let b:di_cpokeep = &l:cpo
513+
let b:di_etkeep = &l:et
514+
let b:di_fokeep = &l:fo
515+
let b:di_gdkeep = &l:gd
516+
let b:di_gokeep = &l:go
517+
let b:di_magickeep = &l:magic
518+
let b:di_remapkeep = &l:remap
519+
let b:di_repkeep = &l:report
520+
let b:di_sikeep = &l:si
521+
let b:di_stakeep = &l:sta
522+
let b:di_vekeep = &l:ve
523+
524+
" change user settings to something safe for DrawIt
525+
setl cpo&vim
526+
setl nocin noai nosi nogd sta et ve=all report=10000
527+
setl go-=aA
528+
setl fo-=a
529+
setl remap magic
530+
set cedit&
531+
" call Dret("s:DrawItSaveUserSettings")
532+
endfun
533+
534+
" ---------------------------------------------------------------------
535+
" s:DrawItRestoreUserSettings: restore user settings {{{2
536+
fun! s:DrawItRestoreUserSettings()
537+
" call Dfunc("s:DrawItRestoreUserSettings()")
538+
539+
" restore user's settings
499540
let &l:ai = b:di_aikeep
500541
let &l:cin = b:di_cinkeep
501542
let &l:cpo = b:di_cpokeep
@@ -522,35 +563,21 @@ fun! DrawIt#DrawItStop()
522563
unlet b:di_sikeep
523564
unlet b:di_stakeep
524565
unlet b:di_vekeep
525-
526-
" DrawItStop: DrChip menu support: {{{3
527-
if has("gui_running") && has("menu") && &go =~# 'm'
528-
exe 'menu '.g:DrChipTopLvlMenu.'DrawIt.Start\ DrawIt<tab>\\di <Leader>di'
529-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Stop\ \ DrawIt'
530-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Toggle\ Erase\ Mode'
531-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Arrow'
532-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Box'
533-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Ellipse'
534-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Flood'
535-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Draw\ Line'
536-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Make\ Blank\ Zone'
537-
exe 'unmenu '.g:DrChipTopLvlMenu.'DrawIt.Append\ Blanks'
538-
endif
539-
" call Dret("DrawItStop")
566+
" call Dret("s:DrawItRestoreUserSettings")
540567
endfun
541568

542569
" ---------------------------------------------------------------------
543570
" DrawIt#SetMode: sets normal, single, double drawing mode, and ensures that DrawIt mode is on {{{2
544571
fun! DrawIt#SetMode(mode)
545572
" call Dfunc("DrawIt#SetMode(mode=".a:mode.")")
546-
if &enc == 'utf-8' || &enc == 'cp437'
573+
if &enc == 'utf-8' || &enc == 'cp437' || &enc == 'utf-16' || &enc == 'ucs-4'
547574
let b:di_gfxchr= 1
548575
else
549576
let b:di_gfxchr= 0
550577
endif
551578
if b:di_gfxchr == 0
552579
let g:drawit_mode= 'N'
553-
elseif &enc != 'utf-8' && &enc != 'cp437'
580+
elseif &enc != 'utf-8' && &enc != 'cp437' && &enc != 'utf-16' && &enc != 'ucs-4'
554581
let g:drawit_mode = 'N'
555582
let b:di_gfxchr = 0
556583
elseif a:mode =~ '^[sS]$'
@@ -1851,7 +1878,7 @@ endfun
18511878
" s:DrawFatRArrow: ----|> {{{2
18521879
fun! s:DrawFatRArrow()
18531880
" call Dfunc("s:DrawFatRArrow()")
1854-
if g:drawit_mode == 'N' || &enc != 'utf-8'
1881+
if g:drawit_mode == 'N' || !b:di_gfxchr
18551882
call s:MoveRight()
18561883
norm! r|
18571884
call s:MoveRight()
@@ -1867,7 +1894,7 @@ endfun
18671894
" s:DrawFatLArrow: <|---- {{{2
18681895
fun! s:DrawFatLArrow()
18691896
" call Dfunc("s:DrawFatLArrow()")
1870-
if g:drawit_mode == 'N' || &enc != 'utf-8'
1897+
if g:drawit_mode == 'N' || !b:di_gfxchr
18711898
call s:MoveLeft()
18721899
norm! r|
18731900
call s:MoveLeft()
@@ -1885,7 +1912,7 @@ endfun
18851912
" |
18861913
fun! s:DrawFatUArrow()
18871914
" call Dfunc("s:DrawFatUArrow()")
1888-
if g:drawit_mode == 'N' || &enc != 'utf-8'
1915+
if g:drawit_mode == 'N' || !b:di_bfxchr
18891916
call s:MoveUp()
18901917
norm! r_
18911918
call s:MoveRight()
@@ -1909,7 +1936,7 @@ endfun
19091936
" '
19101937
fun! s:DrawFatDArrow()
19111938
" call Dfunc("s:DrawFatDArrow()")
1912-
if g:drawit_mode == 'N' || &enc != 'utf-8'
1939+
if g:drawit_mode == 'N' || !b:di_gfxchr
19131940
call s:MoveRight()
19141941
norm! r_
19151942
call s:MoveLeft()
@@ -2068,7 +2095,7 @@ fun! s:Flood()
20682095
" call Dfunc("s:Flood()")
20692096

20702097
let s:bndry = b:di_vert.b:di_horiz.b:di_plus.b:di_upright.b:di_upleft.b:di_cross.b:di_ellipse
2071-
if &enc == 'utf-8' || &enc == 'cp437'
2098+
if b:di_gfxchr
20722099
let s:bndry= s:bndry.b:di_Svert.b:di_Dvert.b:di_Shoriz.b:di_Dhoriz.b:di_Sulcorn.b:di_Dulcorn.b:di_Surcorn.b:di_Durcorn.b:di_Sllcorn.b:di_Dllcorn.b:di_Slrcorn.b:di_Dlrcorn.b:di_Splus.b:di_Dplus.b:di_Sdnplus.b:di_Ddnplus.b:di_Supplus.b:di_Dupplus.b:di_Slplus.b:di_Dlplus.b:di_Srplus.b:di_Drplus.b:di_Supright.b:di_Supleft.b:di_Scross
20732100
let s:bndry= s:bndry.b:di_DhSd.b:di_DhSu.b:di_DuSl.b:di_DuSlr.b:di_DuSr.b:di_ShDd.b:di_ShDu.b:di_SuDl.b:di_SuDlr.b:di_SdDh.b:di_SuDh.b:di_SuDr.b:di_cSdDr.b:di_cDdSr.b:di_cDlSd.b:di_cSlDd.b:di_cDrSu.b:di_cSrDu.b:di_cDlSu.b:di_cSlDu
20742101
endif
@@ -2364,7 +2391,10 @@ endfun
23642391
fun! s:Strlen(x)
23652392
" call Dfunc("s:Strlen(x<".a:x.">")
23662393

2367-
if g:drawit_xstrlen == 1
2394+
if v:version >= 703 && exists("*strdisplaywidth")
2395+
let ret= strdisplaywidth(a:x)
2396+
2397+
elseif g:drawit_xstrlen == 1
23682398
" number of codepoints (Latin a + combining circumflex is two codepoints)
23692399
" (comment from TM, solution from NW)
23702400
let ret= strlen(substitute(a:x,'.','c','g'))
@@ -2390,11 +2420,7 @@ fun! s:Strlen(x)
23902420

23912421
else
23922422
" at least give a decent default
2393-
if v:version >= 703
2394-
let ret= strdisplaywidth(a:x)
2395-
else
2396-
let ret= strlen(a:x)
2397-
endif
2423+
let ret= strlen(a:x)
23982424
endif
23992425

24002426
" call Dret("s:Strlen ".ret)
@@ -2588,14 +2614,17 @@ fun! s:DrawLine(x0, y0, x1, y1, horiz)
25882614
let y0 = a:y1
25892615
let x1 = a:x0
25902616
let y1 = a:y0
2617+
" call Decho("swap points: p0(".x0.",".y0.") p1(".x1.",".y1.")")
25912618
else
25922619
let x0 = a:x0
25932620
let y0 = a:y0
25942621
let x1 = a:x1
25952622
let y1 = a:y1
2623+
" call Decho("points: p0(".x0.",".y0.") p1(".x1.",".y1.")")
25962624
endif
25972625
let dy = y1 - y0
25982626
let dx = x1 - x0
2627+
" call Decho("[dx=x1-x0]=".dx." [dy=y1-y0]=".dy)
25992628

26002629
if dy < 0
26012630
let dy = -dy
@@ -2625,43 +2654,51 @@ fun! s:DrawLine(x0, y0, x1, y1, horiz)
26252654
endif
26262655

26272656
if dx > dy
2628-
" move under x
2657+
" call Decho("case dx>dy : Δ=".dx.",".dy." step=".stepx.",".stepy)
26292658
let char = horiz
26302659
call s:SetCharAt(char, x0, y0)
2631-
let fraction = dy - (dx / 2) " same as 2*dy - dx
2660+
let fraction = dy - (dx / 2) " same as 2*Δy - Δx
26322661
while x0 != x1
26332662
let char = horiz
26342663
if fraction >= 0
26352664
if stepx > 0
2665+
" call Decho("..case [fraction=".fraction."]≥0 and [stepx=".stepx."]>0: go upleft")
26362666
let char = (g:drawit_mode == 'N')? b:di_upleft : b:di_Supleft
26372667
else
2668+
" call Decho("..case [fraction=".fraction."]≥0 and [stepx=".stepx."]≤0: go upright")
26382669
let char = (g:drawit_mode == 'N')? b:di_upright : b:di_Supright
26392670
endif
2640-
let y0 = y0 + stepy
2641-
let fraction = fraction - dx " same as fraction -= 2*dx
2671+
let y0 = y0 + stepy
2672+
let fraction = fraction - dx " same as fraction -= 2*Δx
2673+
" call Decho("....[y0+=stepy]=".y0." [fraction-=dx]=".fraction)
26422674
endif
2643-
let x0 = x0 + stepx
2644-
let fraction = fraction + dy " same as fraction = fraction - 2*dy
2675+
let x0 = x0 + stepx
2676+
let fraction = fraction + dy " same as fraction = fraction - 2*Δy
2677+
" call Decho("..[x0+=stepx]=".x0." [fraction-=dy]=".fraction)
26452678
call s:SetCharAt(char, x0, y0)
26462679
endw
26472680
else
2648-
" move under y
2681+
" call Decho("case dx≤dy : Δ=".dx.",".dy." step=".stepx.",".stepy)
26492682
let char = vertline
26502683
call s:SetCharAt(char, x0, y0)
26512684
let fraction = dx - (dy / 2)
26522685
while y0 != y1
26532686
let char = (g:drawit_mode == 'N')? b:di_vert : ((g:drawit_mode == 'S')? b:di_Svert : b:di_Dvert)
26542687
if fraction >= 0
2655-
if stepy > 0 || stepx < 0
2688+
if stepx > 0
2689+
" call Decho("..case [fraction=".fraction."]≥0 and [stepx=".stepx."]>0: go upleft")
26562690
let char = (g:drawit_mode == 'N')? b:di_upleft : b:di_Supleft
26572691
else
2692+
" call Decho("..case [fraction=".fraction."]≥0 and [stepx=".stepy."]≤0: go upright")
26582693
let char = (g:drawit_mode == 'N')? b:di_upright : b:di_Supright
26592694
endif
2660-
let x0 = x0 + stepx
2695+
let x0 = x0 + stepx
26612696
let fraction = fraction - dy
2697+
" call Decho("....[x0+=stepx]=".x0." [fraction-=dy]=".fraction)
26622698
endif
2663-
let y0 = y0 + stepy
2699+
let y0 = y0 + stepy
26642700
let fraction = fraction + dx
2701+
" call Decho("..[y0+=stepy]=".y0." [fraction-=dy]=".fraction)
26652702
call s:SetCharAt(char, x0, y0)
26662703
endw
26672704
endif

0 commit comments

Comments
 (0)