Skip to content

Commit e826a0c

Browse files
committed
Backslash proper handling
1 parent fd6cd65 commit e826a0c

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

autoload/drvo.vim

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim drvo plugin
22
" Maintainer: matveyt
3-
" Last Change: 2021 Aug 20
3+
" Last Change: 2021 Aug 22
44
" License: https://unlicense.org
55
" URL: https://github.com/matveyt/vim-drvo
66

@@ -9,7 +9,7 @@ set cpo&vim
99

1010
" Remove trailing slash
1111
function s:chomp(name) abort
12-
return a:name =~# '[\/]$' ? a:name[:-2] : a:name
12+
return a:name =~# printf('[%s]$', drvo#slash()) ? a:name[:-2] : a:name
1313
endfunction
1414

1515
" Convert file size to a printable string
@@ -180,9 +180,10 @@ function! drvo#mark() abort
180180
" make sure our head ends in a slash
181181
let l:head = fnamemodify(l:head, ':p')
182182
" match tail if preceded by head and followed by slash (for dirs)
183-
execute printf('syntax match drvoMark %s/\V%s\%%(\^%s\)\@%d<=%s%s\$/ contained',
184-
\ l:isdir ? 'nextgroup=drvoLastSlash ' : '', &fileignorecase ? '\c' : '\C',
185-
\ escape(l:head, '\/'), strlen(l:head), l:tail, l:isdir ? '\ze\[\/]' : '')
183+
execute printf('syntax match drvoMark %s/\V\%s\%%(\^%s\)\@%d<=%s%s\$/ contained',
184+
\ l:isdir ? 'nextgroup=drvoLastSlash ' : '', &fileignorecase ? 'c' : 'C',
185+
\ escape(l:head, '\/'), strlen(l:head), l:tail, l:isdir ? printf('\ze\[%s]',
186+
\ drvo#slash()) : '')
186187
endfor
187188
endfunction
188189

@@ -192,8 +193,8 @@ function! drvo#prettify() abort
192193
silent! noautocmd lcd .
193194

194195
" sort directories first; then sort files by extension
195-
execute printf('sort %s /^.*[\/]/', &fileignorecase ? 'i' : '')
196-
execute printf('sort %s /\.[^.\/]\+$/r', &fileignorecase ? 'i' : '')
196+
execute printf('sort %s /^.*[%s]/', &fileignorecase ? 'i' : '', drvo#slash())
197+
execute printf('sort %s /\.[^.%s]\+$/r', &fileignorecase ? 'i' : '', drvo#slash())
197198

198199
" remember altbuf
199200
let l:altbuf = bufnr(0)
@@ -258,5 +259,11 @@ function! drvo#shdo(fmt, dir, items) abort
258259
filetype detect
259260
endfunction
260261

262+
" drvo#slash()
263+
" Get path separator(s), i.e. '\/' or '/'
264+
function! drvo#slash() abort
265+
return exists('+shellslash') ? '\/' : '/'
266+
endfunction
267+
261268
let &cpo = s:save_cpo
262269
unlet s:save_cpo

doc/drvo.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,18 @@ drvo#readcmd({fmt}) *drvo#readcmd()*
140140

141141
drvo#sel_toggle({items}) *drvo#sel_toggle()*
142142

143-
"Xor" |arglist| with new {items} List. Note all names in {items} List must
144-
be fully expanded first.
143+
"Xor" |arglist| with new {items} List. All names in {items} List must be
144+
fully expanded first.
145145

146146
drvo#shdo({fmt}, {dir}, {items}) *drvo#shdo()*
147147

148148
Implements |:Shdo| command. {fmt} is the same as in |:Shdo|. {dir} is the
149149
new local directory |:lcd| to set. {items} is the file name List to dump
150150
(if it's |empty()| then |arglist| is used instead).
151151

152+
drvo#slash() *drvo#slash()*
153+
154+
Get path separator(s), i.e. '\/' or '/'.
155+
152156
==============================================================================
153157
vim:tw=78:ts=8:ft=help:norl:

syntax/drvo.vim

+39-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" Vim syntax file
22
" Language: vim-drvo plugin
33
" Maintainer: matveyt
4-
" Last Change: 2021 Aug 16
4+
" Last Change: 2021 Aug 22
55
" License: https://unlicense.org
66
" URL: https://github.com/matveyt/vim-drvo
77

@@ -13,34 +13,50 @@ let b:current_syntax = 'drvo'
1313
let s:save_cpo = &cpo
1414
set cpo&vim
1515

16-
function s:glyph(ix) abort
17-
return exists('g:drvo_glyph') ? 'cchar='..nr2char(g:drvo_glyph[a:ix]) : ''
18-
endfunction
16+
function s:synmatch(group, pat, opts, arg = v:null) abort
17+
let l:arg = (type(a:arg) == v:t_list) ? join(a:arg, '\|') : a:arg
18+
let l:case = &fileignorecase ? 'c' : 'C'
19+
let l:slash = drvo#slash()
20+
let l:pat = substitute(a:pat, '$\(\a\+\)', '\=eval(submatch(1))', 'g')
1921

20-
function s:syncontained(group, pat) abort
21-
if empty(a:pat)
22-
return
23-
endif
22+
let l:cmd = printf('syntax match %s /%s/', a:group, l:pat)
23+
for [l:key, l:value] in items(a:opts)
24+
if !empty(l:value)
25+
let l:cmd .= ' ' . l:key
26+
if type(l:value) == v:t_string
27+
let l:cmd .= '=' . l:value
28+
endif
29+
endif
30+
endfor
2431

25-
let l:pat = type(a:pat) == v:t_string ? a:pat :
26-
\ printf('\V\%%(%s\)\$', join(a:pat, '\|'))
27-
execute printf('syntax match %s /.\+%s%s/ contained', a:group,
28-
\ &fileignorecase ? '\c' : '\C', l:pat)
32+
return execute(l:cmd)
2933
endfunction
3034

3135
" drvoDir is {drvoDirRoot/}{drvoDirTrunk}{/}
32-
syntax match drvoDir /^.*[\/]$/ contains=drvoDirRoot
33-
execute 'syntax match drvoDirRoot nextgroup=drvoDirTrunk,drvoMark /^.*[\/]\ze./'
34-
\ 'contained conceal' s:glyph(0)
35-
syntax match drvoDirTrunk nextgroup=drvoLastSlash /[^\/]\+/ contained
36-
syntax match drvoLastSlash /[\/]/ contained conceal
36+
call s:synmatch('drvoDir', '^.*[$slash]$', #{contains: 'drvoDirRoot'})
37+
call s:synmatch('drvoDirRoot', '^.*[$slash]\ze.', #{nextgroup: 'drvoDirTrunk,drvoMark',
38+
\ contained: v:true, conceal: v:true, cchar: exists('g:drvo_glyph[0]') ?
39+
\ nr2char(g:drvo_glyph[0]) : 0})
40+
call s:synmatch('drvoDirTrunk', '[^$slash]\+', #{nextgroup: 'drvoLastSlash',
41+
\ contained: v:true})
42+
call s:synmatch('drvoLastSlash', '[$slash]', #{contained: v:true, conceal: v:true})
3743
" drvoFile is {drvoFileRoot/}{drvoFileXXX}
38-
syntax match drvoFile /^.*[^\/]$/ contains=drvoFileRoot
39-
execute 'syntax match drvoFileRoot nextgroup=drvoFileExecutable,drvoFileIgnore,'
40-
\ 'drvoFileSuffixes,drvoMark /^.*[\/]/ contained conceal' s:glyph(1)
41-
call s:syncontained('drvoFileExecutable', split($PATHEXT, ';'))
42-
call s:syncontained('drvoFileIgnore', g:ft_ignore_pat)
43-
call s:syncontained('drvoFileSuffixes', split(&suffixes, ','))
44+
call s:synmatch('drvoFile', '^.*[^$slash]$', #{contains: 'drvoFileRoot'})
45+
call s:synmatch('drvoFileRoot', '^.*[$slash]', #{nextgroup:
46+
\ 'drvoFileExecutable,drvoFileIgnore,drvoFileSuffixes,drvoMark', contained: v:true,
47+
\ conceal: v:true, cchar: exists('g:drvo_glyph[1]') ? nr2char(g:drvo_glyph[1]) : 0})
48+
if !empty(getenv('PATHEXT'))
49+
call s:synmatch('drvoFileExecutable', '.\+\$case\V\%($arg\)\$', #{contained: v:true},
50+
\ split($PATHEXT, ';'))
51+
endif
52+
if !empty(get(g:, 'ft_ignore_pat'))
53+
call s:synmatch('drvoFileIgnore', '.\+\$case$arg', #{contained: v:true},
54+
\ g:ft_ignore_pat)
55+
endif
56+
if !empty(&suffixes)
57+
call s:synmatch('drvoFileSuffixes', '.\+\$case\V\%($arg\)\$', #{contained: v:true},
58+
\ split(&suffixes, ','))
59+
endif
4460

4561
" default colors
4662
highlight default link drvoDirTrunk Directory

0 commit comments

Comments
 (0)