Friday, September 17, 2010

Re: Supplying common word endings

On Tue, 14 Sep 2010, Andre Majorel wrote:

> Has anyone ever written a macro to enter common word endings ?
>
> For example, if "spe" is an abbreviation for "special" and "spec"
> for "specify",
> "spe<f2>" expands to specialising,
> "spe<f3>" expands to specialisation,
> "spec<f2>" expands to specifying", # Tricky
> "spec<f3>" expands to specification"...
>
>

Here's a very rough draft of a naïve implementation. It mostly
addresses the logistics of changing the word in front of the cursor.
(So, mostly solves your "thousands of abbreviations" problem from the
other thread. Note the 'g:disable_word_expansion' test below.) You
might be better off using the omnifunc mechanism.

:help complete-functions
(The a:findstart=1 call corresponds to GetBaseWord() below)

Instead of <F2> and <F3>, this uses .in (for the '-ing' form), and .at
(for the -ation' form), just for example's sake.

e.g. without g:disable_word_expansion set:
spe.at -> specialization
spe.in -> specialising
spec.at -> specification
spec.in -> specifying

Might respond sometime over the weekend with an update. In general, my
original thoughts on the idea still stand: I think it's going to be
really hard to come up with a set of base forms and expansions that
provide an easily-learned system that saves a significant number of
keystrokes.

Note the awkward second parameter to ExpandWordBeforeCursor() to handle
the case where it's disabled. Anyone know if there's a way to get the
currently running :imap {lhs}?

--
Best,
Ben

==> ~/.vim/plugin/word-endings-thing.vim <==

" find the last set of non-space chars
fun! GetBaseWord()
" :help strpart()
" :help getline()
let line = strpart(getline('.'), 0, col('.'))
" :help matchlist()
let m = matchlist(line, '\S*$')
return m[0]
endfun

fun! s:SetupExpansions()
if exists('s:setup')
return
endif
let s:setup = 1
let s:expand = {}
let s:expand.spe = { 'ing': 'specialising', 'ation': 'specialization' }
let s:expand.spec = { 'ing': 'specifying', 'ation': 'specification' }
" thousands more -- better to generate this structure from a file
endfun

fun! ExpandWordBeforeCursor(expansion, if_disabled)
call s:SetupExpansions()
let base = GetBaseWord()
if exists('g:disable_word_expansion')
let base = ''
let replacement = a:if_disabled
else
if !strlen(base)
return
endif
let replacement = get(get(s:expand, base, {}), a:expansion, '')
if !strlen(replacement)
return
endif
endif

let c = col('.')
let line = getline('.')
let pre = strpart(line, 0, c-strlen(base)-1)
let post = strlen(line) > c ? strpart(line, c) : ''
let newcol = c + strlen(replacement) - strlen(base)
" :help setline()
call setline('.', pre.replacement.post)
" :help setpos()
call setpos('.', [0, line('.'), newcol, 0])
endfun

" :help i_CTRL-\_CTRL-O
imap .in <C-\><C-O>:call ExpandWordBeforeCursor('ing','.in')<cr>
imap .at <C-\><C-O>:call ExpandWordBeforeCursor('ation','.at')<cr>

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: