" Vimball Archiver by Charles E. Campbell, Jr., Ph.D. UseVimball finish autoload/mklib/string.vim [[[1 11 " mklib.vim - another VimL non-standard library " Maintainer: Martin Kopischke " License: same as Vim (:h license) " Version: 0.1.0 " Trim leading and trailing white space function! mklib#string#trim(string) return matchstr(a:string, '\S.\+\S') endfunction " vim:set sw=2 sts=2 ts=8 et fdm=marker fdo+=jump fdl=1: autoload/mklib/text.vim [[[1 21 " mklib.vim - another VimL non-standard library " Maintainer: Martin Kopischke " License: same as Vim (:h license) " Version: 0.1.0 " Get character under cursor function! mklib#text#curchar() return matchstr(getline('.'), '\%'.col('.').'c.') endfunction " Get character before the cursor function! mklib#text#nextchar() return matchstr(getline('.'), '\%>'.col('.').'c.') endfunction " Get character after the cursor function! mklib#text#prevchar() return matchstr(getline('.'), '.*\zs\%<'.col('.').'c.') endfunction " vim:set sw=2 sts=2 ts=8 et fdm=marker fdo+=jump fdl=1: autoload/unite/kinds/substitution.vim [[[1 78 " unite-spell-suggest.vim - a spelling suggestion source for Unite " Maintainer: Martin Kopischke " based on work by MURAOKA Yusuke " License: MIT (see LICENSE.md) " Version: 1.0.0 if &compatible || v:version < 700 finish endif " 'substitution' Unite kind: allows replacing of the word under the cursor. " Can be constrained to a defined target by giving the candidate " a 'source__target_word' key. function! unite#kinds#substitution#define() return get(s:, 'unite_kind', []) endfunction let s:unite_kind = {'name': 'substitution'} let s:unite_kind.default_action = 'replace' let s:unite_kind.action_table = { \ 'replace': \ {'description': "replace the current target word with the candidate"}, \ 'replace_all': \ {'description': "replace all occurrences of the target word with the candidate"} \ } " * 'replace' [occurrence of target under cursor] action function! s:unite_kind.action_table.replace.func(candidate) abort let l:target = s:get_target(a:candidate) if !empty(l:target) && s:get_cword() ==# l:target " extract leading and trailing line parts using regexes only, as string " indexes are byte-based and thus not multi-byte safe to iterate let l:line = getline('.') let l:col = col('.') if match(l:target, '\M'.mklib#text#curchar().'$') != -1 && match(l:target, '\M'.mklib#text#nextchar()) == -1 " we are on the last character, but not on the end of the line: " using matchend() to the end of a word would get us the next word " instead of the current one let l:including = matchstr(l:line, '^.*\%'.l:col.'c.') else " we are somewhere inside, or before (as '' skips non-word " characters to get the next word), the word: use matchend() to locate the " end of the word (note: multi-byte alphabetic characters do not match " any word regex class, so we can't test for '\w') let l:including = l:line[: matchend(l:line[l:col :], '^.\{-}\(\>\|$\)') + l:col] " we get a trailing character everywhere but on line end: strip that if match(l:line, '\M'.l:target.'$') == -1 let l:including = substitute(l:including, '.$', '', '') endif endif let l:before = substitute(l:including, '\M'.l:target.'$', '', '') let l:after = substitute(l:line, '^\M'.l:including, '', '') call setline('.', l:before . a:candidate.word . l:after) endif endfunction " * 'replace all' [occurrences of target] action function! s:unite_kind.action_table.replace_all.func(candidate) abort let l:target = s:get_target(a:candidate) if &gdefault == 0 let l:gflag = 'g' else let l:gflag = '' endif if !empty(l:target) execute '% substitute/\<'.l:target.'\>/'.a:candidate.word.'/I'.l:gflag endif endfunction " Helper functions {{{1 function! s:get_target(candidate) return get(a:candidate, 'source__target_word', s:get_cword()) endfunction function! s:get_cword() return mklib#string#trim(expand('')) endfunction " }}} " vim:set sw=2 sts=2 ts=8 et fdm=marker fdo+=jump fdl=1: autoload/unite/sources/spell_suggest.vim [[[1 111 " unite-spell-suggest.vim - a spelling suggestion source for Unite " Maintainer: Martin Kopischke " based on work by MURAOKA Yusuke " License: MIT (see LICENSE.md) " Version: 1.0.0 if !has('spell') || &compatible || v:version < 700 finish endif let s:old_cpo = &cpo set cpo&vim " ensure we can use line continuation " 'spell_suggest' source: spelling suggestions for Unite function! unite#sources#spell_suggest#define() return get(s:, 'unite_source', []) endfunction let s:unite_source = { \ 'name' : 'spell_suggest', \ 'description': 'candidates from spellsuggest()', \ 'hooks' : {}, \ } " * candidate listing function! s:unite_source.gather_candidates(args, context) abort " get word to base suggestions on if len(a:args) == 0 let s:cword = s:do_outside_unite(a:context, function('s:cword_info')) let l:word = s:cword.word let l:kind = s:cword.modifiable ? 'substitution' : 'word' else let s:cword = {} let l:word = mklib#string#trim(a:args[0] == '?' ? \ input('Suggest spelling for: ', '', 'history') : \ a:args[0]) let l:kind = 'word' endif " get suggestions let l:suggestions = s:do_outside_unite(a:context, function('spellsuggest'), l:word) return map(l:suggestions, \'{"word" : v:val, \ "abbr" : printf("%2d: %s", v:key+1, v:val), \ "kind" : l:kind, \ "source__target_word": l:word}') endfunction " * syntax highlighting function! s:unite_source.hooks.on_syntax(args, context) syntax match uniteSource_spell_suggest_LineNr /^\s\+\d\+:/ highlight default link uniteSource_spell_suggest_LineNr LineNr endfunction " * set up live sync autocmd group function! s:unite_source.hooks.on_init(args, context) if !empty(a:context) && !a:context.no_buffer && !a:context.no_split && empty(a:args) let s:context = a:context augroup unite_spell_suggest autocmd! autocmd BufEnter,CursorMoved,CursorMovedI * call s:unite_source.source__update() augroup END endif endfunction " * remove live sync autocmd group function! s:unite_source.hooks.on_close(args, context) call s:unite_source.source__cleanup() endfunction " * trigger suggestion update if cword changes function! s:unite_source.source__update() dict try if &spell && empty(&buftype) && s:cword != s:cword_info() call unite#force_redraw(unite#helper#get_unite_winnr(s:context.buffer_name)) endif catch call s:unite_source.source__cleanup() endtry endfunction " * clean up after source function! s:unite_source.source__cleanup() dict silent! autocmd! unite_spell_suggest silent! augroup! unite_spell_suggest endfunction " Helper functions: {{{1 " * get info about word under cursor function! s:cword_info() return {'word': mklib#string#trim(expand('')), 'modifiable': &modifiable} endfunction " * execute function out of Unite context function! s:do_outside_unite(unite_context, funcref, ...) abort let l:unite_winnr = !empty(a:unite_context) ? \ unite#helper#get_unite_winnr(a:unite_context.buffer_name) : -1 if l:unite_winnr > -1 && winnr() == l:unite_winnr wincmd p try return call(a:funcref, a:000) finally execute l:unite_winnr.'wincmd w' endtry else return call(a:funcref, a:000) endif endfunction " }}} let &cpo = s:old_cpo " vim:set sw=2 sts=2 ts=8 et fdm=marker fdo+=jump fdl=1: doc/unite-spell-suggest.txt [[[1 119 *unite-spell-suggest.txt* For Vim version 7.0 or better version 1.0.0 VIM REFERENCE for the Unite plug-in Unite spelling suggestion source *unite-spell-suggest-source* For help on Unite and its usage, see |unite.txt|. 1. Introduction |unite-spell-suggest-introduction| 2. Usage |unite-spell-suggest-usage| 3. Troubleshooting |unite-spell-suggest-troubleshooting| 4. Credits and license |unite-spell-suggest-credits-license| {not available without the |+spell| feature or when |'compatible'| is set} ============================================================================== 1. Introduction *unite-spell-suggest-intro* *unite-spell-suggest* is a |unite| source of spelling suggestions. Suggestions based either on the argument passed to the source, or on the current word under the cursor, are gathered from Vim's spell checker and shown using Unite's interface. When based on the current word, the suggestions are updated live when the cursor moves to another word in any open buffer. If that buffer is 'modifiable', the selected candidate can be substituted for the current word (or even all of its occurrences in the current buffer), which makes for a convenient alternative to |z=|. ============================================================================== 2. Usage *unite-spell-suggest-usage* SHOWING SUGGESTIONS FOR THE WORD UNDER THE CURSOR: To show spelling suggestions for the word under the cursor, execute the |:Unite| command with "spell_suggest" as the source parameter. > :Unite spell_suggest < unite-spell-suggest will retrieve the word under the cursor (||) and display a Unite window with spelling suggestions. That list will be updated whenever the word under the cursor changes. REPLACING THE WORD UNDER THE CURSOR: If the buffer the cursor is placed in is 'modifiable', the default action in the suggestion list is "replace", which replaces the word under the cursor with the selected candidate. There is also a "replace_all" action that replaces all occurrences of the word under the cursor in the current buffer. SHOWING SUGGESTIONS FOR ARBITRARY WORDS: Spelling suggestions for arbitrary terms can be displayed by passing the term as the source parameter: > :Unite spell_suggest:arbitrary_term < Passing "?" as the parameter will prompt for a word to look up: > :Unite spell_suggest:? < Note these do not update with changes of the word under the cursor, and cannot be used as substitutions for that word; the normal Unite word actions (see |unite-action-common|) are available, however. ============================================================================== 3. Troubleshooting *unite-spell-suggest-troubleshooting* THE SOURCE IS NOT AVAILABLE IN UNITE: unite-spell-suggest needs Vim version 7 or better with |+spell|. The source is not available when 'compatible' is set. NO SPELLING SUGGESTIONS ARE SHOWN: unite-spell-suggest relies on Vim's |spellsuggest()| function and will only show suggestions when |z=| would. In particular, this means 'spell' must be on in the active buffer. SPELLING SUGGESTIONS ARE IN THE WRONG LANGUAGE: The spelling suggestion language is set by the 'spelllang' setting. REPLACE AND REPLACE_ALL ACTIONS ARE MISSING: The active buffer is not 'modifiable', or unite-spell-suggest has been called with an arbitrary lookup term. OTHER ISSUES: You might have found a bug. Please refer to https://github.com/kopischke/unite-spell-suggest/issues and see if it is listed already; if not, report it by all means. Please include a scenario that reproducibly leads to the issue using a minimal vimrc (50 lines or less). ============================================================================== 4. Credits and License *unite-spell-suggest-credits-license* unite-spell-suggest is maintained by Martin Kopischke http://martin.kopischke.net and licensed under the terms of the MIT license according to the accompanying license file (LICENSE.md). It is based on original work by MURAOKA Yusuke, . vim:tw=78:ts=8:ft=help:norl:noet:fen:fdl=0:fdm=marker: