implement contact completion with completefunc (#250)

This allows users to define a command for contact completion with
`g:himalaya_complete_contact_cmd` and trigger it with `<C-x><C-u>` when
writing an email.
This commit is contained in:
Jason Cox 2021-11-01 02:38:15 -06:00 committed by GitHub
parent 538a0ac301
commit 52b55ac394
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 0 deletions

View file

@ -49,6 +49,24 @@ let g:himalaya_telescope_preview_enabled = 0
Should enable telescope preview when picking a mailbox with the telescope
provider.
### Contact completion
```vim
let g:himalaya_complete_contact_cmd = '<your completion command>'
```
Define the command to use for contact completion. When this is set,
`completefunc` will be set when composing messages so that contacts can be
completed with `<C-x><C-u>`.
The command must print each possible result on its own line. Each line must
contain tab-separated fields; the first must be the email address, and the
second, if present, must be the name. `%s` in the command will be replaced
with the search query.
For example, to complete contacts with khard, you could use
`khard email --remove-first-line --parsable '%s'` as the completion command.
## Usage
### List messages view

View file

@ -305,6 +305,37 @@ function! himalaya#msg#attachments()
endtry
endfunction
function! himalaya#msg#complete_contact(findstart, base)
try
if a:findstart
if !exists("g:himalaya_complete_contact_cmd")
echoerr "You must set 'g:himalaya_complete_contact_cmd' to complete contacts"
return -3
endif
" search for everything up to the last colon or comma
let line_to_cursor = getline(".")[:col(".") - 1]
let start = match(line_to_cursor, '[^:,]*$')
" don't include leading spaces
while start <= len(line_to_cursor) && line_to_cursor[start] == " "
let start += 1
endwhile
return start
else
let output = system(substitute(g:himalaya_complete_contact_cmd, "%s", a:base, ""))
let lines = split(output, "\n")
return map(lines, "s:line_to_complete_item(v:val)")
endif
catch
if !empty(v:exception)
redraw | call himalaya#shared#log#err(v:exception)
endif
endtry
endfunction
" Utils
" https://newbedev.com/get-usable-window-width-in-vim-script
@ -353,3 +384,14 @@ function! s:close_open_buffers(name)
execute ":bwipeout " . buffer_to_close
endfor
endfunction
function! s:line_to_complete_item(line)
let fields = split(a:line, "\t")
let email = fields[0]
let name = ""
if len(fields) > 1
let name = '"' . fields[1] . '" '
endif
return name . "<" . email . ">"
endfunction

View file

@ -46,6 +46,23 @@ TELESCOPE PREVIEW
Should enable telescope preview when picking a mailbox with the telescope
provider.
------------------------------------------------------------------------------
CONTACT COMPLETION
>
let g:himalaya_complete_contact_cmd = '<your completion command>'
<
Define the command to use for contact completion. When this is set,
'completefunc' will be set when composing messages so that contacts can be
completed with |i_CTRL-X_CTRL-U|.
The command must print each possible result on its own line. Each line must
contain tab-separated fields; the first must be the email address, and the
second, if present, must be the name. `%s` in the command will be replaced
with the search query.
For example, to complete contacts with khard, you could use
`khard email --remove-first-line --parsable '%s'` as the completion command.
==============================================================================
USAGE *himalaya-usage*

View file

@ -3,6 +3,10 @@ setlocal foldexpr=himalaya#shared#thread#fold(v:lnum)
setlocal foldmethod=expr
setlocal startofline
if exists("g:himalaya_complete_contact_cmd")
setlocal completefunc=himalaya#msg#complete_contact
endif
augroup himalaya_write
autocmd! * <buffer>
autocmd BufWriteCmd <buffer> call himalaya#msg#draft_save()