Wednesday, May 5, 2010

Re: Support for quickly classifying or annotating with labels from a hierarchy

" AnnotatePlugin : A completion plugin for Annotating a hierarchy
" Author: C.Brabandt <cb@256bit.org>
" Copyright: (c) 2010 by Christian Brabandt
" The VIM LICENSE applies to unicode.vim, and unicode.txt
" (see |copyright|) except use "unicode" instead of "Vim".
" No warranty, express or implied.
" *** *** Use At-Your-Own-Risk! *** ***
"
" ---------------------------------------------------------------------
" Load Once: {{{1
if &cp || exists("g:loaded_annotatePlugin")
finish
endif
let g:loaded_annotatePlugin = 1
let s:keepcpo = &cpo
set cpo&vim

let s:enableAnnotateCompletion = 0
" ------------------------------------------------------------------------------
" Public Interface: {{{1
com! EnableAnnotateCompletion call annotate#Init(1)
com! DisableAnnotateCompletion call annotate#Init(0)

if s:enableAnnotateCompletion
call annotate#Init(s:enableAnnotateCompletion)
endif

let s:class_hier = (exists("g:annotate_class_hier") ? g:annotate_class_hier : 'class_hier.txt')


fu! annotate#AnnotateDict()
let dict={}
let list=readfile(s:class_hier)
for glyph in list
let subclass = matchstr(glyph, '\(\d*\.\)*\d*$')
let subclass = (empty(subclass) ? '0' : subclass)

let dict[subclass] = matchstr(glyph, '\S\+$')
endfor
return dict
endfu

fu! annotate#CompleteAnnotate(findstart,base)
if a:findstart
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~ '\w\|\.'
let start -= 1
endwhile
return start
else
if a:base =~? '^top$'
let base = '0'
else
let base = matchstr(a:base, '\(\d*\.\)*\d*$')
let base = (empty(base) ? '' : base)
endif
let s:strip_decimal = ( exists("g:annotate_strip_decimal") ? g:annotate_strip_decimal : 0 )
let complete_list = filter(copy(s:Anno_dict), 'v:key =~? "^".base')
for [key, value] in sort(items(complete_list), "annotate#CompareList")
if s:strip_decimal
let value=split(value, '\.')[0]
endif
let abbr = printf("%-*s : %s", max(map(keys(complete_list), 'strlen(v:val)')), key, value)
call complete_add({'word':value, 'abbr':abbr})
if complete_check()
break
endif
endfor

return {}
endif
endfu

fu! annotate#CompareList(l1, l2)
return a:l1[0] == a:l2[0] ? 0 : a:l1[0] > a:l2[0] ? 1 : -1
endfu


fu! annotate#Init(enable)
if a:enable
let b:oldfunc=&l:cfu
let s:Anno_dict = annotate#AnnotateDict()
setl completefunc=annotate#CompleteAnnotate
set completeopt+=menuone
else
if !empty(b:oldfunc)
let &l:cfu=b:oldfunc
endif
unlet s:Anno_dict
endif
echo "Annotation Completion " . (a:enable?'ON':'OFF')
endfu

" =====================================================================
" Restoration And Modelines: {{{1
" vim: fdm=marker
let &cpo= s:keepcpo
unlet s:keepcpo
Hi Sven!

On Mi, 05 Mai 2010, Sven wrote:

> An all new plugin, unbelievable.
> Thanks Christian!
>
> I think it could be helpful for others
> (maybe on http://www.vim.org/scripts/ ?).

I am not sure, since I have a feeling that this is a very special use
case and might not be useful for many people. The plugin, is not very
complicated and a provides a very basic completion function, as you can
find in the help.

In fact, I created it by ripping of the essential part from another
plugin of mine unicode.vim
(http://www.vim.org/scripts/script.php?script_id=2822)

Suggestions?

> I am using it heavily, already; so here is one small tweak:
> '\(\d*\.\)*\d*$'
> in order to allow for class position numbers that consist
> of more than one digit.

Right.

>
> Another tweak would be to drop the numerical part from the class name
> (as an option) for the 'word' part in complete_add({'word':value,
> 'abbr':abbr})
> because the numerical part is intended to define the class hierarchy,
> only.
> For example, 4 of my classes are:
> island.1.14
> island_p.1.14.1
> ...
> lake.1.15
> mountain.1.16
> and the completion to island, island_p, lake, mountain would be
> sufficient.

I'll attach an updated version.

regards,
Christian

--
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: