diff --git a/autoload/elin/compat/virtual_text.vim b/autoload/elin/compat/virtual_text.vim new file mode 100644 index 00000000..55463219 --- /dev/null +++ b/autoload/elin/compat/virtual_text.vim @@ -0,0 +1,70 @@ + +function! elin#compat#virtual_text#set(text, ...) abort + let opt = get(a:, 1, {}) + let bufnr = get(opt, 'bufnr', bufnr('%')) + let lnum = get(opt, 'lnum', line('.')) + let hl = get(opt, 'highlight', 'Normal') + let align = get(opt, 'align', 'after') + return s:set(bufnr, a:text, lnum, hl, align) +endfunction + +function! elin#compat#virtual_text#clear(...) abort + let opt = get(a:, 1, {}) + let bufnr = get(opt, 'bufnr', bufnr('%')) + let lnum = get(opt, 'lnum', v:null) + + if lnum is# v:null + return s:clear(bufnr, 1, line('$')) + else + return s:clear(bufnr, lnum, lnum + 1) + endif +endfunction + +if has('nvim') + let s:namespace = nvim_create_namespace('elin_virtual_text_namespace') + + function! s:text_align_to_virt_text_pos(align) abort + return get({'after': 'eol', 'right': 'right_align'}, a:align, 'eol') + endfunction + + function! s:set(bufnr, text, lnum, highlight, align) abort + call s:clear(a:bufnr, a:lnum - 1, a:lnum) + return nvim_buf_set_extmark(a:bufnr, s:namespace, a:lnum - 1, 0, { + \ 'virt_text': [[a:text, a:highlight]], + \ 'virt_text_pos': s:text_align_to_virt_text_pos(a:align) + \ }) + endfunction + + function! s:clear(bufnr, start_lnum, end_lnum) abort + return nvim_buf_clear_namespace(a:bufnr, s:namespace, a:start_lnum - 1, a:end_lnum - 1) + endfunction + + +else + + let s:textprop_type = 'elin_virtual_text_namespace' + call prop_type_delete(s:textprop_type, {}) + call prop_type_add(s:textprop_type, {}) + + function! s:set(bufnr, text, lnum, highlight, align) abort + echom printf('FIXME %s', a:lnum) + call prop_type_change(s:textprop_type, {'highlight': a:highlight}) + call prop_add(a:lnum, 0, { + \ 'type': s:textprop_type, + \ 'bufnr': a:bufnr, + \ 'text': a:text, + \ 'text_align': a:align, + \ 'text_padding_left': 1, + \ }) + redraw + endfunction + + function! s:clear(bufnr, start_lnum, end_lnum) abort + return prop_remove({ + \ 'type': s:textprop_type, + \ 'bufnr': a:bufnr, + \ 'all': v:true, + \ }, a:start_lnum, a:end_lnum) + endfunction + +endif diff --git a/src/elin/function/vim/virtual_text.clj b/src/elin/function/vim/virtual_text.clj new file mode 100644 index 00000000..c954f3a5 --- /dev/null +++ b/src/elin/function/vim/virtual_text.clj @@ -0,0 +1,13 @@ +(ns elin.function.vim.virtual-text + (:refer-clojure :exclude [set]) + (:require + [elin.function.vim :as e.f.vim])) + +(defn set + [writer text] + (let [text (str "=> " text)] + (e.f.vim/notify writer "elin#compat#virtual_text#set" [text]))) + +(defn clear-all + [writer] + (e.f.vim/notify writer "elin#compat#virtual_text#clear" []))