Skip to main content
Module

x/denops_popup/popup.vim

https://deno.land/x/denops_popup
Go to Latest
File
if exists('g:loaded_denops_popup') finishendiflet g:loaded_denops_popup = v:true
function! Denops_popup_window_open(...) abort return call(function('s:_open'), a:000)endfunction
function! Denops_popup_window_move(...) abort return call(function('s:_move'), a:000)endfunction
function! Denops_popup_window_close(...) abort return call(function('s:_close'), a:000)endfunction
function! Denops_popup_window_info(...) abort return call(function('s:_info'), a:000)endfunction
function! Denops_popup_window_is_visible(...) abort return call(function('s:_is_visible'), a:000)endfunction
function! Denops_popup_window_is_popup_window(...) abort return call(function('s:_is_popup_window'), a:000)endfunction
"" _open"if has('nvim') function! s:_open(bufnr, style, event) abort let l:winid = nvim_open_win(a:bufnr, v:false, s:_style(a:style)) let s:win_close_listeners[l:winid] = { -> denops#notify(a:event.onClose[0], a:event.onClose[1], []) } return l:winid endfunctionelse function! s:_open(bufnr, style, event) abort let l:style = s:_style(a:style) let l:style.callback = { -> denops#notify(a:event.onClose[0], a:event.onClose[1], []) } return popup_create(a:bufnr, l:style) endfunctionendif
"" _move"if has('nvim') function! s:_move(winid, style) abort call nvim_win_set_config(a:winid, s:_style(a:style)) endfunctionelse function! s:_move(winid, style) abort let l:style = s:_style(a:style) call popup_setoptions(a:winid, l:style) call popup_move(a:winid, l:style) endfunctionendif
"" _close"if has('nvim') function! s:_close(winid) abort call nvim_win_close(a:winid, v:true) endfunctionelse function! s:_close(winid) abort call popup_close(a:winid) endfunctionendif
"" _info"if has('nvim') function! s:_info(winid) abort let l:info = getwininfo(a:winid)[0] return { \ 'row': l:info.winrow, \ 'col': l:info.wincol, \ 'width': l:info.width, \ 'height': l:info.height, \ 'topline': l:info.topline, \ } endfunctionelse function! s:_info(winid) abort let l:pos = popup_getpos(a:winid) return { \ 'row': l:pos.core_line, \ 'col': l:pos.core_col, \ 'width': l:pos.core_width, \ 'height': l:pos.core_height, \ 'topline': l:pos.firstline, \ } endfunctionendif
"" _is_visible"if has('nvim') function! s:_is_visible(winid) abort try return !!(type(a:winid) == type(0) && nvim_win_is_valid(a:winid) && nvim_win_get_number(a:winid) != -1) catch /.*/ endtry return 0 endfunctionelse function! s:_is_visible(winid) abort return !!(type(a:winid) == type(0) && winheight(a:winid) != -1) endfunctionendif
"" _is_popup_window"if has('nvim') function! s:_is_popup_window(winid) abort try let l:config = nvim_win_get_config(a:winid) return !!(empty(l:config) || !empty(get(l:config, 'relative', ''))) catch /.*/ endtry return 0 endfunctionelse function! s:_is_popup_window(winid) abort return !!(winheight(a:winid) != -1 && win_id2win(a:winid) == 0) endfunctionendif
"" _style"if has('nvim') function! s:_style(style) abort let l:style = s:_resolve_origin(a:style) let l:style = s:_resolve_border(l:style) let l:style = { \ 'relative': 'editor', \ 'row': l:style.row - 1, \ 'col': l:style.col - 1, \ 'width': l:style.width, \ 'height': l:style.height, \ 'focusable': v:true, \ 'style': 'minimal', \ 'border': has_key(l:style, 'border') ? l:style.border : 'none', \ } if !exists('*win_execute') " We can't detect neovim features via patch version so we try it by function existence. unlet l:style.border endif return l:style endfunctionelse function! s:_style(style) abort let l:style = s:_resolve_origin(a:style) let l:style = s:_resolve_border(l:style) return { \ 'line': l:style.row, \ 'col': l:style.col, \ 'pos': 'topleft', \ 'wrap': v:false, \ 'moved': [0, 0, 0], \ 'scrollbar': 0, \ 'maxwidth': l:style.width, \ 'maxheight': l:style.height, \ 'minwidth': l:style.width, \ 'minheight': l:style.height, \ 'tabpage': 0, \ 'firstline': get(l:style, 'topline', 1), \ 'padding': [0, 0, 0, 0], \ 'border': has_key(l:style, 'border') ? [1, 1, 1, 1] : [0, 0, 0, 0], \ 'borderchars': get(l:style, 'border', []), \ 'fixed': v:true, \ } endfunctionendif
"" _resolve_origin"function! s:_resolve_origin(style) abort if index(['topleft', 'topright', 'topcenter', 'bottomleft', 'bottomright', 'bottomcenter', 'centercenter'], get(a:style, 'origin', '')) == -1 let a:style.origin = 'topleft' endif if a:style.origin ==# 'topleft' let a:style.col = a:style.col let a:style.row = a:style.row elseif a:style.origin ==# 'topright' let a:style.col = a:style.col - (a:style.width - 1) let a:style.row = a:style.row elseif a:style.origin ==# 'topcenter' let a:style.col = a:style.col - float2nr(a:style.width / 2) let a:style.row = a:style.row elseif a:style.origin ==# 'bottomleft' let a:style.col = a:style.col let a:style.row = a:style.row - (a:style.height - 1) elseif a:style.origin ==# 'bottomright' let a:style.col = a:style.col - (a:style.width - 1) let a:style.row = a:style.row - (a:style.height - 1) elseif a:style.origin ==# 'bottomcenter' let a:style.col = a:style.col - float2nr(a:style.width / 2) let a:style.row = a:style.row - (a:style.height - 1) elseif a:style.origin ==# 'centercenter' let a:style.col = a:style.col - float2nr(a:style.width / 2) let a:style.row = a:style.row - float2nr(a:style.height / 2) endif return a:styleendfunction
"" _resolve_border"if has('nvim') function! s:_resolve_border(style) abort if !empty(get(a:style, 'border', v:null)) if &ambiwidth ==# 'single' let a:style.border = ['┌', '─', '┐', '│', '┘', '─', '└', '│'] else let a:style.border = ['+', '-', '+', '|', '+', '-', '+', '|'] endif elseif has_key(a:style, 'border') unlet a:style.border endif return a:style endfunctionelse function! s:_resolve_border(style) abort if !empty(get(a:style, 'border', v:null)) if &ambiwidth ==# 'single' let a:style.border = ['─', '│', '─', '│', '┌', '┐', '┘', '└'] else let a:style.border = ['-', '|', '-', '|', '+', '+', '+', '+'] endif elseif has_key(a:style, 'border') unlet a:style.border endif return a:style endfunctionendif

let s:win_close_listeners = {}if has('nvim') function! s:notify_closed() abort let l:winid = expand('<afile>') if has_key(s:win_close_listeners, l:winid) call remove(s:win_close_listeners, l:winid)() endif endfunction augroup denops_popup_win_close_listeners autocmd! autocmd WinClosed * call s:notify_closed() augroup ENDendif