lifepillar / vim-mucomplete

Chained completion that works the way you want!
MIT License
912 stars 18 forks source link

Allow completing thesaurus words with spaces #77

Closed lifepillar closed 6 years ago

lifepillar commented 6 years ago

(Issue by @dbmrq, moved from https://github.com/lifepillar/vim-mucomplete/issues/76)

Vim's thesaurus doesn't allow spaces within its entries. One attempt to work around it is to set iskeyword+=32, but it has other side effects, so we have to do set iskeyword-=32 right after that.

People talk about it here, here and this is my own attempt.

But it would be easy to add setl isk+=32 to mucomplete#tab_complete(dir) or something like that, and then use an autocmd to do setl isk-=32 right after that.

If you don't want to do that, it would also be nice if you could add a custom event (something like doautocmd User MUBeforeTab) before tab completion so I could use it to set isk myself.

lifepillar commented 6 years ago

MUcomplete tries not to mess with any option, so I am not keen to let it update such a critical setting as iskeyword. But you may define a custom completion method invoking your function:

    function! MyThesaurus()
      let s:saved_ut = &ut
      if &ut > 200 | let &ut = 200 | endif
      augroup ThesaurusAuGroup
        autocmd CursorHold,CursorHoldI <buffer>
              \ let &ut = s:saved_ut |
              \ set iskeyword-=32 |
              \ autocmd! ThesaurusAuGroup
      augroup END
      set iskeyword+=32
      return "\<c-x>\<c-t>"
    endfunction

    let g:mucomplete#user_mappings = { 'mythes': "\<c-r>=MyThesaurus()\<cr>" }
    let g:mucomplete#chains = { 'default': ['path', 'omni', 'keyn', 'dict', 'mythes', 'uspl'] }

Anyway, the idea of adding hooks to custom events is worth considering. Do you know whether doautocmd has any performance impact?

Btw, I do not get the same suggestions as you (for a fortiori, you get fortiori, above all, all the more, chiefly, etc…, while I get fortiori, a priori, a fortiori, a posteriori, analytic, etc…): yours seem much better suggestions. I have tried with mthesaur.txt and mobythes.aur. Are you using a customised thesaurus?

lifepillar commented 6 years ago

it would also be nice if you could add a custom event (something like doautocmd User MUBeforeTab) before tab completion

That is not strictly needed. See :h mucomplete-tips, second tip.

dbmrq commented 6 years ago

Awesome, defining a custom method works perfectly. Thank you! 🎉

I agree its better not to change iskeyword from the plugin, that's why I suggested a custom event. I don't know if doautocmd has any significant overhead, but I've seen other plugins use it generously, so I would imagine that it doesn't. But anyway, as you mentioned, that's not necessary for the <tab> key, I missed that part in the docs. So I think it's fine to do without it unless another reason to use it shows up.

As for the Moby Thesaurus, I think for the screenshots I just went down the list far enough to have enough examples with spaces to illustrate the problem. I just tried it again and my examples are still there, just not near the top:

captura de tela 2017-12-09 as 00 24 05

Like I mentioned here, I still think Moby has way too many suggestions to be useful though. I've been happy with the approach I mentioned in that answer, I'll note the words I use the most, google them and add them to my own thesaurus files.

Thanks again for all your help, I've been through every possible completion plugin and mucomplete is by far the best. Do add that PayPal link, I'll buy you a beer. :)

lifepillar commented 6 years ago

Please note that the documentation was not up to date, and the example I have suggested you look at was not working. Now I have fixed the docs, so please make sure you have the current master.

Regarding auto-commands, for now I am not planning to introduce any, since your issue can be solved without auto-commands. Should the need arise again, I will reconsider the option. Thanks for pointing that out!

Moby has way too many suggestions to be useful though

Thesaurus would be much more useful and efficient, IMO, if Vim had an option to match only the first word in each thesaurus line instead of any word, and filled the menu with the remaining words in that line. That would make it almost trivial (after some massaging) to use {Open|Libre}Office thesauri, which are pretty good, but not symmetric (e.g., “algorithm” has “rule” as a synonym, but “rule” does not have “algorithm” as a synonym).

dbmrq commented 6 years ago

Yes, that would be great, and I think entries with spaces and hyphens should be supported out of the box too. C'est la vie. 🤷‍♂️