你有一大堆错误。
- “在脚本上下文中使用 ”用于调用
s: 函数。您需要将s: 替换为<SID>,并确保在定义函数的同一文件中定义映射。 (或“手动”扩展<SID>,但这相当棘手。)
-
l:line=… 是错误的赋值表达式。使用:let 分配和删除l: 前缀(在函数上下文中它是默认前缀)。
-
&commentstring+'}}}' 可能会导致 0。 + 始终是数字加法,使用 . 连接字符串。
-
append() 函数没有缩进。因此,您需要将 matchstr(getline('.'), '^\s*') 添加到您的线路或做一些更聪明的事情。
-
&commentstring 看起来像 /*%s*/。您不需要连接,您需要使用printf(&cms, '}}}') 或substitute()。
- 标记是可配置的。使用
split(&foldmarker, ',')[0] 代替'{{{' 和split(&foldmarker, ',')[1] 代替'}}}'。
我个人使用这个(不是直接你需要的,至少没有一些编辑):
function InsertBlock(foldlevel, ...)
let line=getline('.')
if !empty(a:000)
let text=a:000[0]
else
let text=matchstr(line, '\S.*\S\@<=')
endif
if empty(line)
normal! "_cc_"
let indent=getline('.')[:-3]
else
let indent=matchstr(line, '^\s*')
endif
let cmsl=split(&commentstring, '%s', 1)
let [startmarker, endmarker]=split(&foldmarker, ',')
let left=(indent).get(cmsl, 0, '').startmarker
if a:foldlevel>0
let left.=a:foldlevel
elseif a:foldlevel==0
let left.='1'
endif
let left.=' '.text
let right=''
if !empty(get(cmsl, 1, ''))
let right=' '.cmsl[1]
endif
call setline('.', left)
normal! $
if !empty(right)
call setline('.', left.right)
normal! l
endif
if a:foldlevel==-1
call append('.', (indent).get(cmsl, 0, '').endmarker)
endif
endfunction
function CloseBlock(foldlevel)
if a:foldlevel==0
return
endif
let [startmarker, endmarker]=split(&foldmarker, ',')
let foldstart=search('\V'.escape(startmarker, '\').a:foldlevel, 'bnW')
let cmsl=split(&commentstring, '%s', 1)
if foldstart
let indent=matchstr(getline(foldstart), '^\s*')
else
normal! "_cc_"
let indent=getline('.')[:-3]
endif
call setline('.', (indent).get(cmsl, 0, '').endmarker.a:foldlevel.
\ get(cmsl, 1, ''))
endfunction
nnoremap ,{ o<C-o>:call InsertBlock(foldlevel('.'))<CR><Esc>
nnoremap ,} o<C-o>:call InsertBlock(foldlevel('.')+1)<CR><Esc>
nnoremap ,[ o<C-o>:call InsertBlock(foldlevel('.')-1)<CR><Esc>
nnoremap ,- o<C-o>:call CloseBlock(foldlevel('.'))<CR><Esc>
inoremap ,{ <C-o>:call InsertBlock(foldlevel('.'))<CR>
inoremap ,} <C-o>:call InsertBlock(foldlevel('.')+1)<CR>
inoremap ,[ <C-o>:call InsertBlock(foldlevel('.')-1)<CR>
inoremap ,- <C-o>:call CloseBlock(foldlevel('.'))<CR>
inoremap ,+ <C-o>:call InsertBlock(foldlevel('.')+1)<CR><CR><C-o>:call CloseBlock(foldlevel('.'))<CR>
。请注意,这 a) 将非空行转换为带有标题的标记,看起来像 "▶3 Caption({{{ 太胖而无法使用)。结束标记的使用比较少见,它们看起来像"▲3。具有级别的标记会自动以更大或相等的级别结束所有折叠,因此大部分时间不需要结束标记。