VimL functions to determine spelling categories?

32 views
Skip to first unread message

Benjamin R. Haskell

unread,
Jun 28, 2012, 5:44:19 AM6/28/12
to Vim Users
Are there VimL functions for determining that Vim has marked a word as
misspelled? E.g. so the highlighting could be exported in something
similar to TOhtml.

--
Best,
Ben

Marc Weber

unread,
Jun 28, 2012, 6:02:17 AM6/28/12
to vim_use
Excerpts from Benjamin R. Haskell's message of Thu Jun 28 11:44:19 +0200 2012:
> Are there VimL functions for determining that Vim has marked a word as
> misspelled? E.g. so the highlighting could be exported in something
> similar to TOhtml.

I don't know about it. Talk about what you really want to do and people
will provide alternative solutions.

There are mappings which "go to the next misspelled word" which you can
could use to add special markers easily .. However it'll be hard to also
show the "alternative spellings" vim can provide etc - but I don't know
the vim API about spelling well - so I may be wrong.

Marc Weber

Ben Fritz

unread,
Jun 28, 2012, 11:14:51 AM6/28/12
to vim...@googlegroups.com, Vim Users
I recently installed this, which populates the quickfix list with spelling errors, but I haven't looked at how it works yet:

http://www.vim.org/scripts/script.php?script_id=3844

I have in the TOhtml TODO list, adding guisp highlighting. I'm not exactly sure how I want to accomplish it though. It might be converted to a dotted underline or something of the appropriate color.

Benjamin R. Haskell

unread,
Jun 28, 2012, 1:35:19 PM6/28/12
to vim...@googlegroups.com, Vim Users
On Thu, 28 Jun 2012, Ben Fritz wrote:

> On Thursday, June 28, 2012 4:44:19 AM UTC-5, Benjamin R. Haskell wrote:
>> Are there VimL functions for determining that Vim has marked a word
>> as misspelled? E.g. so the highlighting could be exported in
>> something similar to TOhtml.
>
> I recently installed this, which populates the quickfix list with
> spelling errors, but I haven't looked at how it works yet:
>
> http://www.vim.org/scripts/script.php?script_id=3844

Thanks. I didn't find the spellbadword() function before, because it's
the 53rd item when tab-completing :help spell<Tab>.

Unfortunately, spellbadword() can change the cursor position. But,
c'est la vie. I like Ingo's approach:

" pseudocode

" {{{ prevent looping, save the setting and view
let saved_wrapscan = &wrapscan
set nowrapscan

let saved_view = winsaveview()
" }}}

sil! norm! gg0]s " go to first error

while 1
let start_pos = getpos('.')

sil! norm! ]s " go to next error

let error_pos = getpos('.')
let [word, type] = spellbadword()

if error_pos == start_pos " then, there was no error
break
end
endwhile

" {{{ restore setting and view
call winrestview(saved_view)
let &wrapscan = saved_wrapscan
" }}}

In a few places he uses ]s[s (instead of just ]s), but I don't quite
understand what it's working around (code comment mentions something
about multiple errors on one line).



> I have in the TOhtml TODO list, adding guisp highlighting. I'm not
> exactly sure how I want to accomplish it though. It might be converted
> to a dotted underline or something of the appropriate color.

CSS3 is adding properties that will make it properly-possible¹. This
is one way it will eventually work:

.undercurl {
text-decoration-line: underline;
text-decoration-style: wavy;
text-decoration-color: red;
/* possibly also: */ text-underline-position: alphabetic;
}

Shorter:

.undercurl {
text-decoration: red wavy underline;
}

Firefox has vendor-prefixed CSS properties² for some of this already.
The following worked for me (in FF) to simulate this Vim:

.SpellBad {
/* hi SpellBad gui=undercurl guisp=#ff0000 */
-moz-text-decoration-line: underline;
-moz-text-decoration-style: wavy;
-moz-text-decoration-color: #ff0000;
/* text-decoration: #ff0000 wavy underline; // not yet working, even with -moz- */
}

--
Best,
Ben H

¹: text-decoration-style - http://www.w3.org/TR/css3-text/#text-decoration-style

²: http://peter.sh/experiments/vendor-prefixed-css-property-overview/

Ben Fritz

unread,
Jun 28, 2012, 1:42:45 PM6/28/12
to vim...@googlegroups.com, Vim Users
On Thursday, June 28, 2012 12:35:19 PM UTC-5, Benjamin R. Haskell wrote:
> > I have in the TOhtml TODO list, adding guisp highlighting. I'm not
> > exactly sure how I want to accomplish it though. It might be converted
> > to a dotted underline or something of the appropriate color.
>
> CSS3 is adding properties that will make it properly-possible¹. This
> is one way it will eventually work:
>
> .undercurl {
> text-decoration-line: underline;
> text-decoration-style: wavy;
> text-decoration-color: red;
> /* possibly also: */ text-underline-position: alphabetic;
> }
>
> Shorter:
>
> .undercurl {
> text-decoration: red wavy underline;
> }
>
> Firefox has vendor-prefixed CSS properties² for some of this already.
> The following worked for me (in FF) to simulate this Vim:
>
> .SpellBad {
> /* hi SpellBad gui=undercurl guisp=#ff0000 */
> -moz-text-decoration-line: underline;
> -moz-text-decoration-style: wavy;
> -moz-text-decoration-color: #ff0000;
> /* text-decoration: #ff0000 wavy underline; // not yet working, even with -moz- */
> }
>

Thanks for that, I was not aware of this CSS3 development. I'll probably use that, with a fallback to a dotted underline or bottom border for browsers which don't support it.

Marc Weber

unread,
Jun 28, 2012, 7:49:16 PM6/28/12
to vim_use
Excerpts from Benjamin R. Haskell's message of Thu Jun 28 19:35:19 +0200 2012:
> Unfortunately, spellbadword() can change the cursor position.
So? use
let save_cursor = getpos(".")
MoveTheCursorAround
call setpos('.', save_cursor)

to set cursor to old position. Not perfect - but fine.
A second way is use sp to create a new window, move cursor there, then
:q the split window again.

Marc Weber

Benjamin R. Haskell

unread,
Jun 28, 2012, 8:01:51 PM6/28/12
to vim_use
On Fri, 29 Jun 2012, Marc Weber wrote:

> Excerpts from Benjamin R. Haskell's message of Thu Jun 28 19:35:19 +0200 2012:
>> Unfortunately, spellbadword() can change the cursor position.
> So?

So, it's a side-effect. It'd be better if there were a side-effect-free
version of the function. Plus the function combines two separate, but
related, tasks pertaining to the misspelled word under the cursor (if it
exists):

1. Move the cursor to the start of the word
2. Return information about the misspelling (the word and the type)

It'd be preferable if the function returned information about the
misspelled word, but that the information would also include its
location. Then setpos() could be used for functionality #1.


> use
> let save_cursor = getpos(".")
> MoveTheCursorAround
> call setpos('.', save_cursor)

The version I posted (based on Ingo Karkat's SpellCheck plugin) saved
the view, not just the cursor position. By only saving the cursor
position, the function that moves the cursor around might also alter
folds or the viewport (for example).


> to set cursor to old position. Not perfect - but fine.
> A second way is use sp to create a new window, move cursor there, then
> :q the split window again.

My point in calling it unfortunate wasn't to say that it's impossible
(or even that hard) to workaround. It just would have been nicer to
find an API similar to :synID (which takes a (line,col)-position as
input).

--
Best,
Ben H
Reply all
Reply to author
Forward
0 new messages