From d4818329bf021e015ea92705b7bb417e78edbdef Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 9 May 2022 16:39:21 +1200 Subject: Commit first version --- README.md | 16 +++++++++++ VERSION | 1 + after/ftplugin/html/spelllang.vim | 33 ++++++++++++++++++++++ autoload/html/spelllang.vim | 59 +++++++++++++++++++++++++++++++++++++++ doc/html_spelllang.txt | 48 +++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 README.md create mode 100644 VERSION create mode 100644 after/ftplugin/html/spelllang.vim create mode 100644 autoload/html/spelllang.vim create mode 100644 doc/html_spelllang.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ce511d --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +html\_spelllang.vim +=================== + +This filetype plugin for HTML searches for ``, ``, and `
` +elements, and attempts to set the `'spelllang'` option appropriately if it +detects that spelling files exist corresponding to any `lang=` attribute +defined for the lattermost instances of these elements within the first 128 +lines. + +License +------- + +Copyright (c) [Tom Ryder][1]. Distributed under the same terms as Vim itself. +See `:help license`. + +[1]: https://sanctum.geek.nz/ diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..6e8bf73 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1.0 diff --git a/after/ftplugin/html/spelllang.vim b/after/ftplugin/html/spelllang.vim new file mode 100644 index 0000000..8f44384 --- /dev/null +++ b/after/ftplugin/html/spelllang.vim @@ -0,0 +1,33 @@ +" html/spelllang.vim: Attempt to detect 'spelllang' from HTML buffers + +" Don't load if running compatible or too old +if &compatible || v:version < 802 + finish +endif + +" Don't load if already loaded +if exists('b:did_ftplugin_html_spelllang') + finish +endif + +" Flag as loaded +let b:did_ftplugin_html_spelllang = 1 +let b:undo_ftplugin .= '|unlet b:did_ftplugin_html_spelllang' + +" Set up hooks for divining 'spelllang' from lang= attributes +augroup html_spelllang + if exists('##TextChanged') + autocmd TextChanged * + \ if &modified + \| call html#spelllang#Set() + \|endif + else + autocmd InsertLeave * + \ if &modified + \| call html#spelllang#Set() + \|endif + endif +augroup END +call html#spelllang#Set() +let b:undo_ftplugin .= '|execute ''autocmd! html_spelllang''' + \ . '|augroup! html_spelllang' diff --git a/autoload/html/spelllang.vim b/autoload/html/spelllang.vim new file mode 100644 index 0000000..dfabf40 --- /dev/null +++ b/autoload/html/spelllang.vim @@ -0,0 +1,59 @@ +" The line count limit for looking for the pattern; no sense churning through +" the whole document every time it's changed. +" +let s:lines = get(g:, 'html_spelllang_lines', 128) + +" Crude regular expression to match an , , or
tag on one +" line with a lang= attribute set. This isn't exact, by any means, but +" I don't want to write an actual HTML parser for a mere 'spelllang' hook +" nicety that fails silently anyway. +" +" The first submatch is the language code, e.g. "en", "es". The second +" submatch is optional and follows a hyphen and is for the regional code, +" e.g. "US", "GB", "AR". +" +let s:pattern = get(g:, 'html_spelllang_pattern', + \ '\m\c<\%(html\|body\|main\)\>[^>]*\ tag, or skip it + let matches = matchlist(line, s:pattern) + if empty(matches) + continue + endif + + " The line seems to match our pattern, and so we have a language code in + " matches[1], and possibly a region code in matches[2]. + " + " Next we need to check whether we'll attempt to use this to set + " a spelling language. Build the expected path for a spellfile; looks + " like `$VIMRUNTIME/spell/en.utf-8.spl`. Note that this path doesn't + " include any language region, per documentation. + " + let spellfile = 'spell/' . join([ + \ tolower(matches[1]), + \ &encoding, + \ 'spl', + \], '.') + + " If a spelling list file of the expected name exists in &runtimepath, try + " setting the local value of 'spelllang' to reflect what was in the lang= + " attribute; force it to lowercase and separate the region with an + " underscore rather than a hyphen (if there is a region). + " + if strlen(globpath(&runtimepath, spellfile)) + let &l:spelllang = tolower(join(matches[1:2], '_')) + endif + + endfor + +endfunction diff --git a/doc/html_spelllang.txt b/doc/html_spelllang.txt new file mode 100644 index 0000000..1ea93c0 --- /dev/null +++ b/doc/html_spelllang.txt @@ -0,0 +1,48 @@ +*html_spelllang.txt* For Vim version 8.2 Last change: 2022 May 9 + +DESCRIPTION *html_spelllang* + +This filetype plugin for HTML searches for ``, ``, and `
` +elements, and attempts to set the 'spelllang' option appropriately if it +detects that spelling files exist corresponding to any `lang=` attribute +defined for the lattermost instances of these elements within the first 128 +lines. + +This includes switching on common RFC 4646 language region codes; for example, +an `en-us` attribute may attempt to set 'spelllang' to `en_US`. Only simple +`LANGUAGE-REGION` codes are presently supported; there's quite a lot more to +the relevant RFC. + +It also includes a hook to re-check the same number of lines if the buffer is +modified, in case the language code has been updated. Where supported, this +is done with the |TextChanged| autocommand event. + +REQUIREMENTS *html_spelllang-requirements* + +This plugin is only available if 'compatible' is not set. It presently +requires Vim 8.2 or newer, but can almost certainly be back-ported. + +OPTIONS *html_spelllang-options* + +You probably don't actually want to change these, but just in case: + + *g:html_spelllang_lines* +Set `g:html_spelllang_lines` in your |vimrc| to a number besides 128 if you +want to change the number of lines to be checked for language codes in the +buffer, both on initial load and buffer modification. + + *g:html_spelllang_pattern* +Set `g:html_spelllang_pattern` in your |vimrc| if you really want to override +the pattern used to match the language code. You will want two matching +subgroups for |matchlist()|: one for the language, and a second for for the +optional region. + +AUTHOR *html_spelllang-author* + +Written and maintained by Tom Ryder . + +LICENSE *html_spelllang-license* + +Licensed for distribution under the same terms as Vim itself (see |license|). + + vim:tw=78:ts=8:ft=help:norl: -- cgit v1.2.3