Last active
April 2, 2021 22:28
-
-
Save mg979/d9c9e1f36d009b10f689553f1e02e755 to your computer and use it in GitHub Desktop.
Virtual text separators for neovim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" ========================================================================/// | |
" Description: separators using virtual text for nvim | |
" File: virtualSep.vim | |
" Author: Gianmaria Bajo <mg1979.git@gmail.com> | |
" License: MIT | |
" Created: ven 02 aprile 2021 09:58:06 | |
" ========================================================================/// | |
" GUARD {{{1 | |
if !has('nvim') || exists('g:loaded_virtualSep') | |
finish | |
endif | |
let g:loaded_virtualSep = 1 | |
let s:save_cpo = &cpo | |
set cpo&vim | |
"}}} | |
"" | |
" Invoke with pattern to draw separators in empty lines before that pattern. | |
" Invoke with no arguments to clear the pattern and disable the separators. | |
" If BANG is used, separator will not be drawn before folded regions. | |
" Note: separators only update on TextChanged/InsertLeave anyway. | |
"" | |
command! -nargs=? -bang VirtualSep call s:toggle(<q-args>, <bang>0) | |
fun! s:virtualSep(off) | |
"{{{ | |
if exists('b:ns_id') | |
call nvim_buf_clear_namespace(bufnr(), b:ns_id, 1, line('$')) | |
endif | |
if a:off | |
return | |
endif | |
let b:ns_id = nvim_create_namespace("virtualSep") | |
let s = [[repeat('─', &tw ? &tw - 1 : &columns), 'NonText']] | |
for l in range(1, line('$')) | |
if getline(l) == '' | |
if b:virtualSepPattern[1] && (foldclosed(l+1) > 0 || foldclosed(l+2) > 0) | |
continue | |
endif | |
if getline(l+1) =~ b:virtualSepPattern[0] | |
call nvim_buf_set_virtual_text(bufnr(), b:ns_id, l-1, s, {}) | |
endif | |
endif | |
endfor | |
endfun "}}} | |
fun! s:toggle(pattern, nofolded) | |
"{{{ | |
if a:pattern != '' | |
let b:virtualSepPattern = [a:pattern, a:nofolded] | |
exe 'augroup virtualSep_' . bufnr() | |
au! | |
au InsertEnter <buffer> call s:virtualSep(1) | |
au InsertLeave,TextChanged <buffer> call s:virtualSep(0) | |
exe 'augroup END' | |
else | |
exe 'sil! au! virtualSep_' . bufnr() | |
exe 'sil! aug! virtualSep_' . bufnr() | |
endif | |
call s:virtualSep(a:pattern == '') | |
endfun "}}} | |
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" Restore previous external compatibility options {{{1 | |
let &cpo = s:save_cpo | |
unlet s:save_cpo | |
" vim: et sw=4 ts=4 sts=4 fdm=marker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment