Definite's Extractor

My findings on Life, Linux, Open Source, and so on.

Monthly Archives: December 2006

Cream – Spelling Check

Cream is a macro that “soften” the Vim. I am very comfortable about its setting and key binding. However, so far I discover two bugs of it. One is spelling check, another is block comment. I will talk about the block comment problem in next blog.

In Cream v 0.36, the misspelled word is marked correctly, but you cannot select the correct word using the popup menu, nor the Tools -> Spell Check -> “Suggest Alternatives” works. I think its the reason why the author remove the popup correction in version 0.38.

However, even in version 0.38, there are no place to add word from menu. Therefore, I “recycle” the code of popup menu from vim in order to provide the functionality to select the near candidate words from popup menu, as well as the ability to add new word to dictionary.

Just replace content of $CREAM/cream-menu-popup.vim with the following code:

"=====================================================================
" cream-menu-popup.vim
"
" Cream -- An easy-to-use configuration of the famous Vim text editor
" [ http://cream.sourceforge.net ] Copyright (C) 2001-2006  Steve Hall
"
" License:
" This program is free software; you can redistribute it and/or modify
" it under the terms of the GNU General Public License as published by
" the Free Software Foundation; either version 2 of the License, or
" (at your option) any later version.
" [ http://www.gnu.org/licenses/gpl.html ]
"
" This program is distributed in the hope that it will be useful, but
" WITHOUT ANY WARRANTY; without even the implied warranty of
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
" General Public License for more details.
"
" You should have received a copy of the GNU General Public License
" along with this program; if not, write to the Free Software
" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
" 02111-1307, USA.
"

" Note: This is functionalized so we can re-set it with a call.

function! Cream_menu_popup()
" The popup menu
an 1.10 PopUp.&Undo			u
an 1.15 PopUp.-SEP1-			
vnoremenu 1.20 PopUp.Cu&t		"+x
vnoremenu 1.30 PopUp.&Copy		"+y
cnoremenu 1.30 PopUp.&Copy		
nnoremenu 1.40 PopUp.&Paste		"+gP
cnoremenu 1.40 PopUp.&Paste		+
vnoremenu  1.42 PopUp.&Paste	      :call Cream_paste("v")
inoremenu  1.45 PopUp.&Paste  :call Cream_paste("i")
vnoremenu 1.50 PopUp.&Delete		x
an 1.55 PopUp.-SEP2-			
vnoremenu 1.60 PopUp.Select\ Blockwise	

nnoremenu 1.70 PopUp.Select\ &Word	vaw
onoremenu 1.70 PopUp.Select\ &Word	aw
vnoremenu 1.70 PopUp.Select\ &Word	vaw
inoremenu 1.70 PopUp.Select\ &Word	vaw
cnoremenu 1.70 PopUp.Select\ &Word	vaw

nnoremenu 1.73 PopUp.Select\ &Sentence	vas
onoremenu 1.73 PopUp.Select\ &Sentence	as
vnoremenu 1.73 PopUp.Select\ &Sentence	vas
inoremenu 1.73 PopUp.Select\ &Sentence	vas
cnoremenu 1.73 PopUp.Select\ &Sentence	vas

nnoremenu 1.77 PopUp.Select\ Pa&ragraph	vap
onoremenu 1.77 PopUp.Select\ Pa&ragraph	ap
vnoremenu 1.77 PopUp.Select\ Pa&ragraph	vap
inoremenu 1.77 PopUp.Select\ Pa&ragraph	vap
cnoremenu 1.77 PopUp.Select\ Pa&ragraph	vap

nnoremenu 1.80 PopUp.Select\ &Line	V
onoremenu 1.80 PopUp.Select\ &Line	V
vnoremenu 1.80 PopUp.Select\ &Line	V
inoremenu 1.80 PopUp.Select\ &Line	V
cnoremenu 1.80 PopUp.Select\ &Line	V

nnoremenu 1.90 PopUp.Select\ &Block	
onoremenu 1.90 PopUp.Select\ &Block	
vnoremenu 1.90 PopUp.Select\ &Block	
inoremenu 1.90 PopUp.Select\ &Block	
cnoremenu 1.90 PopUp.Select\ &Block	

noremenu    1.100 PopUp.Select\ &All	:call SelectAll()
inoremenu   1.100 PopUp.Select\ &All	:call SelectAll()
cnoremenu   1.100 PopUp.Select\ &All	call SelectAll()

if has("spell")
  " Spell suggestions in the popup menu.  Note that this will slow down the
  " appearance of the menu!
  func! SpellPopup()
    if exists("s:changeitem") && s:changeitem != ''
      call SpellDel()
    endif
    if !&spell || &spelllang == ''
      return
    endif

    let curcol = col('.')
    let [w, a] = spellbadword()
    if col('.') > curcol		" don't use word after the cursor
      let w = ''
      call cursor(0, curcol)	" put the cursor back where it was
    endif
    if w != ''
      if a == 'caps'
	let s:suglist = [substitute(w, '.*', '\u&', '')]
      else
	let s:suglist = spellsuggest(w, 10)
      endif
      if len(s:suglist) <= 0
	call cursor(0, curcol)	" put the cursor back where it was
      else
	let s:changeitem = 'change\ "' . escape(w, ' .'). '"\ to'
	let s:fromword = w
	let pri = 1
	for sug in s:suglist
	  exe 'amenu 1.5.' . pri . ' PopUp.' . s:changeitem . '.' . escape(sug, ' .')
		\ . ' :call SpellReplace(' . pri . ')'
	  let pri += 1
	endfor

	let s:additem = 'add\ "' . escape(w, ' .') . '"\ to\ word\ list'
	exe 'amenu 1.6 PopUp.' . s:additem . ' :spellgood ' . w . ''

	let s:ignoreitem = 'ignore\ "' . escape(w, ' .') . '"'
	exe 'amenu 1.7 PopUp.' . s:ignoreitem . ' :spellgood! ' . w . ''

	amenu 1.8 PopUp.-SpellSep- :
      endif
    endif
  endfunc

  func! SpellReplace(n)
    let l = getline('.')
    call setline('.', strpart(l, 0, col('.') - 1) . s:suglist[a:n - 1]
	  \ . strpart(l, col('.') + len(s:fromword) - 1))
  endfunc

  func! SpellDel()
    exe "aunmenu PopUp." . s:changeitem
    exe "aunmenu PopUp." . s:additem
    exe "aunmenu PopUp." . s:ignoreitem
    aunmenu PopUp.-SpellSep-
    let s:changeitem = ''
  endfun

  au! MenuPopup * call SpellPopup()
endif
endfunction
call Cream_menu_popup()
Advertisements