Skip to content

Commit

Permalink
feat: add g:wiki_ui_method for choosing ui backends
Browse files Browse the repository at this point in the history
refer: #317
  • Loading branch information
lervag committed Jul 19, 2023
1 parent b9cafb7 commit 176bb2a
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 95 deletions.
12 changes: 3 additions & 9 deletions autoload/wiki/ui.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ endfunction
" }}}1

function! wiki#ui#confirm(prompt) abort " {{{1
return has('nvim')
\ ? wiki#ui#nvim#confirm(a:prompt)
\ : wiki#ui#vim#confirm(a:prompt)
return wiki#ui#{g:wiki_ui_method.confirm}#confirm(a:prompt)
endfunction

" }}}1
Expand All @@ -35,9 +33,7 @@ function! wiki#ui#input(options) abort " {{{1
\ info: '',
\}, a:options)

return has('nvim')
\ ? wiki#ui#nvim#input(l:options)
\ : wiki#ui#vim#input(l:options)
return wiki#ui#{g:wiki_ui_method.input}#input(l:options)
endfunction

" }}}1
Expand All @@ -57,9 +53,7 @@ function! wiki#ui#select(container, ...) abort " {{{1
\ ? [-1, '']
\ : (len(l:list) == 1
\ ? [0, l:list[0]]
\ : (has('nvim')
\ ? wiki#ui#nvim#select(l:options, l:list)
\ : wiki#ui#vim#select(l:options, l:list)))
\ : wiki#ui#{g:wiki_ui_method.select}#select(l:options, l:list))

if l:options.return ==# 'value'
return l:value
Expand Down
104 changes: 104 additions & 0 deletions autoload/wiki/ui/legacy.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
" A wiki plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: [email protected]
"

function! wiki#ui#vim#confirm(prompt) abort " {{{1
let l:prompt = type(a:prompt) == v:t_list ? a:prompt : [a:prompt]
let l:prompt[-1] .= ' [y]es/[n]o: '

while v:true
redraw!
call wiki#ui#echo(l:prompt)

let l:input = nr2char(getchar())
if index(["\<c-c>", "\<esc>"], l:input) >= 0
break
endif

if index(['y', 'Y', 'n', 'N'], l:input) >= 0
echon l:input
sleep 75m
redraw!
break
endif
endwhile

return l:input ==? 'y'
endfunction

" }}}1
function! wiki#ui#vim#input(options) abort " {{{1
if !empty(a:options.info)
redraw!
call wiki#ui#echo(a:options.info)
endif

let l:input = has_key(a:options, 'completion')
\ ? input(a:options.prompt, a:options.text, a:options.completion)
\ : input(a:options.prompt, a:options.text)

return l:input
endfunction

" }}}1
function! wiki#ui#vim#select(options, list) abort " {{{1
let l:length = len(a:list)
let l:digits = len(l:length)

" Use simple menu when in operator mode
if !empty(&operatorfunc)
let l:choices = map(deepcopy(a:list), { i, x -> (i+1) . ': ' . x })
let l:choice = inputlist(l:choices) - 1
return l:choice >= 0 && l:choice < l:length
\ ? [l:choice, a:list[l:choice]]
\ : [-1, '']
endif

" Create the menu
let l:menu = [a:options.prompt]
let l:format = printf('%%%dd: ', l:digits)
let l:i = 0
for l:x in a:list
let l:i += 1
call add(l:menu, [
\ ['ModeMsg', printf(l:format, l:i)],
\ type(l:x) == v:t_dict ? l:x.name : l:x
\])
endfor
if !a:options.force_choice
call add(l:menu, [
\ ['ModeMsg', repeat(' ', l:digits - 1) . 'x: '],
\ 'Abort'
\])
endif

" Loop to get a valid choice
let l:value = ''
while v:true
redraw!

for l:line in l:menu
call wiki#ui#echo(l:line)
endfor

let l:choice = wiki#ui#get_number(
\ l:length, l:digits, a:options.force_choice, v:true)

if !a:options.force_choice && l:choice == -2
break
endif

if l:choice >= 0 && l:choice < l:length
let l:value = a:list[l:choice]
break
endif
endwhile

sleep 75m
redraw!
return [l:choice, l:value]
endfunction

" }}}1
89 changes: 3 additions & 86 deletions autoload/wiki/ui/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,17 @@
"

function! wiki#ui#vim#confirm(prompt) abort " {{{1
let l:prompt = type(a:prompt) == v:t_list ? a:prompt : [a:prompt]
let l:prompt[-1] .= ' [y]es/[n]o: '

while v:true
redraw!
call wiki#ui#echo(l:prompt)

let l:input = nr2char(getchar())
if index(["\<c-c>", "\<esc>"], l:input) >= 0
break
endif

if index(['y', 'Y', 'n', 'N'], l:input) >= 0
echon l:input
sleep 75m
redraw!
break
endif
endwhile

return l:input ==? 'y'
return wiki#ui#legacy#confirm(a:prompt)
endfunction

" }}}1
function! wiki#ui#vim#input(options) abort " {{{1
if !empty(a:options.info)
redraw!
call wiki#ui#echo(a:options.info)
endif

let l:input = has_key(a:options, 'completion')
\ ? input(a:options.prompt, a:options.text, a:options.completion)
\ : input(a:options.prompt, a:options.text)

return l:input
return wiki#ui#legacy#input(a:options)
endfunction

" }}}1
function! wiki#ui#vim#select(options, list) abort " {{{1
let l:length = len(a:list)
let l:digits = len(l:length)

" Use simple menu when in operator mode
if !empty(&operatorfunc)
let l:choices = map(deepcopy(a:list), { i, x -> (i+1) . ': ' . x })
let l:choice = inputlist(l:choices) - 1
return l:choice >= 0 && l:choice < l:length
\ ? [l:choice, a:list[l:choice]]
\ : [-1, '']
endif

" Create the menu
let l:menu = [a:options.prompt]
let l:format = printf('%%%dd: ', l:digits)
let l:i = 0
for l:x in a:list
let l:i += 1
call add(l:menu, [
\ ['ModeMsg', printf(l:format, l:i)],
\ type(l:x) == v:t_dict ? l:x.name : l:x
\])
endfor
if !a:options.force_choice
call add(l:menu, [
\ ['ModeMsg', repeat(' ', l:digits - 1) . 'x: '],
\ 'Abort'
\])
endif

" Loop to get a valid choice
let l:value = ''
while v:true
redraw!

for l:line in l:menu
call wiki#ui#echo(l:line)
endfor

let l:choice = wiki#ui#get_number(
\ l:length, l:digits, a:options.force_choice, v:true)

if !a:options.force_choice && l:choice == -2
break
endif

if l:choice >= 0 && l:choice < l:length
let l:value = a:list[l:choice]
break
endif
endwhile

sleep 75m
redraw!
return [l:choice, l:value]
return wiki#ui#legacy#select(a:options, a:list)
endfunction

" }}}1
36 changes: 36 additions & 0 deletions doc/wiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,42 @@ a reference of all available options.

Default: `'# Summary, %(year) week %(week)'`

*g:wiki_ui_method*
A dictionary that specifies the backend various input methods. The method
names are the keys of the dictionary and the backend choices are the values.

The available methods:

confirm: Confirm dialogues, e.g. before deleting a pages.
input: Input dialogues, e.g. when renaming a page.
select: Selection dialogues, e.g. when creating a link from text with
|<plug>(wiki-link-follow)|, where the text can refer to multiple
target pages.

The available backends:

nvim: Popup menu created with neovim APIs.
vim: Currently there is no Vim-specific implementation. Setting the
backend to "vim" will currently fallback to "legacy".
legacy: Legacy backends that are created by |:echo|ing the menus and
using |input()| and similar for getting input.

Default: >vim

" On neovim
let g:wiki_ui_method = {
\ 'confirm': 'nvim',
\ 'input': 'nvim',
\ 'select': 'nvim',
\}

" Otherwise
let g:wiki_ui_method = {
\ 'confirm': 'legacy',
\ 'input': 'legacy',
\ 'select': 'legacy',
\}

*g:wiki_viewer*
A dictionary that specifies which viewer to use for a given filetype. The
entry `_` specifies the fallback or generic viewer. This option thus allows
Expand Down
5 changes: 5 additions & 0 deletions plugin/wiki.vim
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ call wiki#init#option('wiki_template_title_week',
\ '# Summary, %(year) week %(week)')
call wiki#init#option('wiki_toc_title', 'Contents')
call wiki#init#option('wiki_toc_depth', 6)
call wiki#init#option('wiki_ui_method', {
\ 'confirm': has('nvim') ? 'nvim' : 'legacy',
\ 'input': has('nvim') ? 'nvim' : 'legacy',
\ 'select': has('nvim') ? 'nvim' : 'legacy',
\})
call wiki#init#option('wiki_viewer', {
\ '_' : get({
\ 'linux' : 'xdg-open',
Expand Down

0 comments on commit 176bb2a

Please sign in to comment.