Change in 'completefunc' option

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Change in 'completefunc' option

Bram Moolenaar

I am currently looking into the Insert mode completion code, in
preparation for intellisense-like completion.

I noticed a mistake in the 'completefunc' code.  Looking a bit closer I
think it's good to change the specification to have the function return
a List.  Separating matches with line breaks will be bad when there are
many matches.  The function will become simpler too.

Here is the new documentation.  If you used 'completefunc' you will have
to adjust your scripts when using the next snapshot.

                                                *'completefunc'* *'cfu'*
'completefunc' 'cfu' string (default: empty)
                        local to buffer
                        {not in Vi}
                        {not available when compiled without the +eval
                        or +insert_expand feature}
        This option specifies a function to be used for CTRL-X CTRL-U
        completion. |i_CTRL-X_CTRL-U|

        The function will be invoked with three arguments:
           a:findstart  either 1 or 0
           a:col        column in the cursor line where the completion ends,
                        first column is zero
           a:base the text with which matches should match

        When the a:findstart argument is 1, the function must return the
        column of where the completion starts.  It must be a number between
        zero and "a:col".  This involves looking at the characters in the
        cursor line before column a:col and include those characters that
        could be part of the completed item.  The text between this column and
        a:col will be replaced with the matches.  Return -1 if no completion
        can be done.

        When the a:findstart argument is 0 the function must return a List
        with the matching words.  These matches should include the "a:base"
        text.  When there are no matches return an empty List.

        The function must not move the cursor!

        An example that completes the names of the months: >
                fun! CompleteMonths(findstart, col, base)
                  if a:findstart
                    " locate the start of the word
                    let line = getline('.')
                    let start = a:col
                    while start > 0 && line[start - 1] =~ '\a'
                      let start -= 1
                    endwhile
                    return start
                  else
                    " find months matching with "a:base"
                    let res = []
                    for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
                      if m =~ '^' . a:base
                        call add(res, m)
                      endif
                    endfor
                    return res
                  endif
                endfun
                set completefunc=CompleteMonths
<

--
Westheimer's Discovery:
        A couple of months in the laboratory can
        frequently save a couple of hours in the library.

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: Change in 'completefunc' option

Aaron Griffin
On 7/30/05, Bram Moolenaar <[hidden email]> wrote:

>
> I am currently looking into the Insert mode completion code, in
> preparation for intellisense-like completion.
>
> I noticed a mistake in the 'completefunc' code.  Looking a bit closer I
> think it's good to change the specification to have the function return
> a List.  Separating matches with line breaks will be bad when there are
> many matches.  The function will become simpler too.
>
> Here is the new documentation.  If you used 'completefunc' you will have
> to adjust your scripts when using the next snapshot.
>
>                                                 *'completefunc'* *'cfu'*
> 'completefunc' 'cfu'    string  (default: empty)
>                         local to buffer
>                         {not in Vi}
>                         {not available when compiled without the +eval
>                         or +insert_expand feature}
>         This option specifies a function to be used for CTRL-X CTRL-U
>         completion. |i_CTRL-X_CTRL-U|
>
>         The function will be invoked with three arguments:
>            a:findstart  either 1 or 0
>            a:col        column in the cursor line where the completion ends,
>                         first column is zero
>            a:base       the text with which matches should match
>
>         When the a:findstart argument is 1, the function must return the
>         column of where the completion starts.  It must be a number between
>         zero and "a:col".  This involves looking at the characters in the
>         cursor line before column a:col and include those characters that
>         could be part of the completed item.  The text between this column and
>         a:col will be replaced with the matches.  Return -1 if no completion
>         can be done.
>
>         When the a:findstart argument is 0 the function must return a List
>         with the matching words.  These matches should include the "a:base"
>         text.  When there are no matches return an empty List.
>
>         The function must not move the cursor!
>
>         An example that completes the names of the months: >
>                 fun! CompleteMonths(findstart, col, base)
>                   if a:findstart
>                     " locate the start of the word
>                     let line = getline('.')
>                     let start = a:col
>                     while start > 0 && line[start - 1] =~ '\a'
>                       let start -= 1
>                     endwhile
>                     return start
>                   else
>                     " find months matching with "a:base"
>                     let res = []
>                     for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
>                       if m =~ '^' . a:base
>                         call add(res, m)
>                       endif
>                     endfor
>                     return res
>                   endif
>                 endfun
>                 set completefunc=CompleteMonths
> <
>
> --
> Westheimer's Discovery:
>         A couple of months in the laboratory can
>         frequently save a couple of hours in the library.
>
>  /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
> ///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
> \\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
>  \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
>

Hooray for completion!  This is something I've been looking for for a
long time... personally I love the way the IComplete plugin functions
(with a previewwindow)... just to let you know.