* El 15/07/06 a las 21:20, Yakov Lerner chamullaba:

> On 7/15/06, Luis A. Florit <[EMAIL PROTECTED]> wrote:
> > * El 13/07/06 a las 20:54, Luis A. Florit chamullaba:
> > 
> > > * El 12/07/06 a las 23:34, Stefan Karlsson chamullaba:
> > > 
> > > > >     Is there an easy way to autoselect language for the builtin
> > > > > spell checker in vim 7.0? Vimspell plugin has this functionality:
> > > > > the plugin looks for a specified number of lines and try to guess
> > > > > which language should it use.
> > > > >
> > > >
> > > > Set spelllang -- yes there is three "l" in that option :-) -- to a
> > > > comma-separated list of the languages you're interested in. Vim will
> > figure
> > > > out which of them to use.
> > > >
> > > > For example, this is how I do it in my .vimrc (I want US English and
> > Swedish):
> > > >
> > > >   set spelllang=en_us,sv
> > > 
> > > Oh... perfect!!!!
> > > How I didn't find this in the manual?
> > > I (think I) searched thoroughly...
> > > Anyway, thanks a lot!!!!
> > 
> > No, wait, there is a problem. If I put
> > 
> >    set spelllang=en,pt,es
> > 
> > then vim ADDS this dictionaries!! So, something like
> > 
> >    "You want to go yo quiero ir eu quero ir"
> > 
> > will be accepted without any error!!
> > So, vim just add all the dictionaries
> > and then this is not a good solution.
> 
> Indeed, it is documented so in :help spelllang:
> all listed dictionaries are used, not one of them.

You're right, it's in the help, I didn't find it. Sorry.

> It is possible to write a plugin on top of existing spell functionality,
> a plugin that would automaticlly recognize a lanugage
> of the document out of the list of languages. The algorithm is
> below.
>
> Here is the idea, the algorithm, based on the spell-counting
> script that I posted to the list some time ago. That script counted
> number of spell-bad and spell-good words in the buffer.
>
> Now the algorithm: for every language in the list, spell the buffer
> with this lanugage. Count spell-bad and spell-good for this language.
> Out of all language in the list, select language with
> most spell-good and least spell-bad numbers. [If there is no
> clear winner, present a dialog so that user can choose manually.]
>
> I'd  much rather leave the plugin writing to someone who
> himself uses  multilanguage spelling often. To save his time,
> the CountMisspell() function is below.

Works perfect! Quite nice.
I've already implemented a rough plugin by slightly changing your
function, but inside my .vimrc with 2 functions called sequentially.
Works well for me.

Just in case someone needs it, it is included below.
The language is selected taking into account the ratio bad/total.
Invoke it with:

:call MySpellChoose()

    HTH, and thanks Yakov!!!!

        L.

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

function CountMisspell(mylang)
   "Provided by Yakov Lerner
   let save_cursor = getpos('.')

   norm gg

   "set spell
   let &spelllang=a:mylang

   let total = 0
   let bad = 0
   let good = 0
   "let warn = 0

   let flags='Wc'
   while search('\i\+', flags)
      let flags = 'W'
      let total  += 1

      let word = expand('<cword>')
      let list =  spellbadword(word)

      if list[1] == ''
         let good += 1
      elseif list[1] == 'bad'
         let bad += 1
   "   else " rare, local, caps
   "      let warn += 1
      endif
   endwh
   "echo "Total words:     " . good
   "echo "Bad words:       " . bad
   "echo "Semi-bad words:  " . warn
   if bad != 0
       let ratio = (10000 * bad) / total
       let ratio = printf("%03d", ratio)
       let ratio = substitute(ratio, '\d\d$', '.&', '')
   else
       let ratio='0.00'
   endif
   "echo "Bad words ratio: " . ratio . '%'
   call setpos('.', save_cursor )
   return ratio
endfu

function MySpellChoose()
    " mylangs: list of your languages
    let mylangs = ['en','pt','es']
    set spell
    let best = 101
    for i in mylangs
        if CountMisspell(i) <= best
            let best = CountMisspell(i)
            let bestlang = i
        endif
    endfor
    let &spelllang=bestlang
endfu

Reply via email to