-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLanguageTool.vim
More file actions
409 lines (376 loc) · 14.2 KB
/
LanguageTool.vim
File metadata and controls
409 lines (376 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
" LanguageTool: Grammar checker in Vim for English, French, German, etc.
" Maintainer: Dominique Pellé <dominique.pelle@gmail.com>
" Screenshots: http://dominique.pelle.free.fr/pic/LanguageToolVimPlugin_en.png
" http://dominique.pelle.free.fr/pic/LanguageToolVimPlugin_fr.png
" Last Change: 2016/10/06
" Version: 1.30
"
" Long Description: {{{1
"
" This plugin integrates the LanguageTool grammar checker into Vim.
" Current version of LanguageTool can check grammar in many languages:
" ast, be, br, ca, da, de, el, en, eo, es, fa, fr, gl, is, it, ja, km, lt,
" ml, nl, pl, pt, ro, ru, sk, sl, sv, ta, tl, uk, zh.
"
" See doc/LanguageTool.txt for more details about how to use the
" LanguageTool plugin.
"
" See http://www.languagetool.org/ for more information about LanguageTool.
"
" License: {{{1
"
" The VIM LICENSE applies to LanguageTool.vim plugin
" (see ":help copyright" except use "LanguageTool.vim" instead of "Vim").
"
" Plugin set up {{{1
if &cp || exists("g:loaded_languagetool")
finish
endif
let g:loaded_languagetool = "1"
" Guess language from 'a:lang' (either 'spelllang' or 'v:lang')
function s:FindLanguage(lang) "{{{1
" This replaces things like en_gb en-GB as expected by LanguageTool,
" only for languages that support variants in LanguageTool.
let l:language = substitute(substitute(a:lang,
\ '\(\a\{2,3}\)\(_\a\a\)\?.*',
\ '\=tolower(submatch(1)) . toupper(submatch(2))', ''),
\ '_', '-', '')
" All supported languages (with variants) from version LanguageTool.
let l:supportedLanguages = {
\ 'ast' : 1,
\ 'be' : 1,
\ 'br' : 1,
\ 'ca' : 1,
\ 'cs' : 1,
\ 'da' : 1,
\ 'de' : 1,
\ 'de-AT' : 1,
\ 'de-CH' : 1,
\ 'de-DE' : 1,
\ 'el' : 1,
\ 'en' : 1,
\ 'en-AU' : 1,
\ 'en-CA' : 1,
\ 'en-GB' : 1,
\ 'en-NZ' : 1,
\ 'en-US' : 1,
\ 'en-ZA' : 1,
\ 'eo' : 1,
\ 'es' : 1,
\ 'fa' : 1,
\ 'fr' : 1,
\ 'gl' : 1,
\ 'is' : 1,
\ 'it' : 1,
\ 'ja' : 1,
\ 'km' : 1,
\ 'lt' : 1,
\ 'ml' : 1,
\ 'nl' : 1,
\ 'pl' : 1,
\ 'pt' : 1,
\ 'pt-BR' : 1,
\ 'pt-PT' : 1,
\ 'ro' : 1,
\ 'ru' : 1,
\ 'sk' : 1,
\ 'sl' : 1,
\ 'sv' : 1,
\ 'ta' : 1,
\ 'tl' : 1,
\ 'uk' : 1,
\ 'zh' : 1
\}
if has_key(l:supportedLanguages, l:language)
return l:language
endif
" Removing the region (if any) and trying again.
let l:language = substitute(l:language, '-.*', '', '')
return has_key(l:supportedLanguages, l:language) ? l:language : ''
endfunction
" Return a regular expression used to highlight a grammatical error
" at line a:line in text. The error starts at character a:start in
" context a:context and its length in context is a:len.
function s:LanguageToolHighlightRegex(line, context, start, len) "{{{1
let l:start_idx = byteidx(a:context, a:start)
let l:end_idx = byteidx(a:context, a:start + a:len) - 1
let l:start_ctx_idx = byteidx(a:context, a:start + a:len)
let l:end_ctx_idx = byteidx(a:context, a:start + a:len + 5) - 1
" The substitute allows matching errors which span multiple lines.
" The part after \ze gives a bit of context to avoid spurious
" highlighting when the text of the error is present multiple
" times in the line.
return '\V'
\ . '\%' . a:line . 'l'
\ . substitute(escape(a:context[l:start_idx : l:end_idx], "'\\"), ' ', '\\_\\s', 'g')
\ . '\ze'
\ . substitute(escape(a:context[l:start_ctx_idx : l:end_ctx_idx], "'\\"), ' ', '\\_\\s', 'g')
endfunction
" Unescape XML special characters in a:text.
function s:XmlUnescape(text) "{{{1
" Change XML escape char such as " into "
" Substitution of & must be done last or else something
" like &quot; would get first transformed into "
" and then wrongly transformed into " (correct is ")
let l:escaped = substitute(a:text, '"', '"', 'g')
let l:escaped = substitute(l:escaped, ''', "'", 'g')
let l:escaped = substitute(l:escaped, '>', '>', 'g')
let l:escaped = substitute(l:escaped, '<', '<', 'g')
let l:escaped = substitute(l:escaped, '	', ' ', 'g')
return substitute(l:escaped, '&', '\&', 'g')
endfunction
" Parse a xml attribute such as: ruleId="FOO" in line a:line.
" where ruleId is the key a:key, and FOO is the returned value corresponding
" to that key.
function s:ParseKeyValue(key, line) "{{{1
return s:XmlUnescape(matchstr(a:line, '\<' . a:key . '="\zs[^"]*\ze"'))
endfunction
" Set up configuration.
" Returns 0 if success, < 0 in case of error.
function s:LanguageToolSetUp() "{{{1
let s:languagetool_disable_rules = exists("g:languagetool_disable_rules")
\ ? g:languagetool_disable_rules
\ : 'WHITESPACE_RULE,EN_QUOTES'
let s:languagetool_win_height = exists("g:languagetool_win_height")
\ ? g:languagetool_win_height
\ : 14
let s:languagetool_encoding = &fenc ? &fenc : &enc
" Setting up language...
if exists("g:languagetool_lang")
let s:languagetool_lang = g:languagetool_lang
else
" Trying to guess language from 'spelllang' or 'v:lang'.
let s:languagetool_lang = s:FindLanguage(&spelllang)
if s:languagetool_lang == ''
let s:languagetool_lang = s:FindLanguage(v:lang)
if s:languagetool_lang == ''
echoerr 'Failed to guess language from spelllang=['
\ . &spelllang . '] or from v:lang=[' . v:lang . ']. '
\ . 'Defauling to English (en-US). '
\ . 'See ":help LanguageTool" regarding setting g:languagetool_lang.'
let s:languagetool_lang = 'en-US'
endif
endif
endif
let s:languagetool_jar = exists("g:languagetool_jar")
\ ? g:languagetool_jar
\ : $HOME . '/languagetool/languagetool-commandline.jar'
if !filereadable(s:languagetool_jar)
" Hmmm, can't find the jar file. Try again with expand() in case user
" set it up as: let g:languagetool_jar = '$HOME/languagetool-commandline.jar'
let l:languagetool_jar = expand(s:languagetool_jar)
if !filereadable(expand(l:languagetool_jar))
echomsg "LanguageTool cannot be found at: " . s:languagetool_jar
echomsg "You need to install LanguageTool and/or set up g:languagetool_jar"
echomsg "to indicate the location of the languagetool-commandline.jar file."
return -1
endif
let s:languagetool_jar = l:languagetool_jar
endif
return 0
endfunction
" Jump to a grammar mistake (called when pressing <Enter>
" on a particular error in scratch buffer).
function <sid>JumpToCurrentError() "{{{1
let l:save_cursor = getpos('.')
norm! $
if search('^Error:\s\+', 'beW') > 0
let l:error_idx = expand('<cword>')
let l:error = s:errors[l:error_idx - 1]
let l:line = l:error['fromy']
let l:col = l:error['fromx']
let l:rule = l:error['ruleId']
call setpos('.', l:save_cursor)
exe s:languagetool_text_win . ' wincmd w'
exe 'norm! ' . l:line . 'G0'
if l:col > 0
exe 'norm! ' . (l:col - 1) . 'l'
endif
echon 'Jump to error ' . l:error_idx . '/' . len(s:errors)
\ . ' (' . l:rule . ') ...@ ' . l:line . 'L ' . l:col . 'C'
norm! zz
else
call setpos('.', l:save_cursor)
endif
endfunction
" This function performs grammar checking of text in the current buffer.
" It highlights grammar mistakes in current buffer and opens a scratch
" window with all errors found. It also populates the location-list of
" the window with all errors.
" a:line1 and a:line2 parameters are the first and last line number of
" the range of line to check.
" Returns 0 if success, < 0 in case of error.
function s:LanguageToolCheck(line1, line2) "{{{1
if s:LanguageToolSetUp() < 0
return -1
endif
call s:LanguageToolClear()
let s:languagetool_text_win = winnr()
sil %y
botright new
let s:languagetool_error_buffer = bufnr('%')
let s:languagetool_error_win = winnr()
sil put!
" LanguageTool somehow gives incorrect line/column numbers when
" reading from stdin so we need to use a temporary file to get
" correct results.
let l:tmpfilename = tempname()
let l:tmperror = tempname()
let l:range = a:line1 . ',' . a:line2
silent exe l:range . 'w!' . l:tmpfilename
let l:fts = ['tex']
if (executable('detex') && index(l:fts, &filetype) == -1)
let l:tmpdetex = tempname()
if g:tex_flavor == 'latex'
let l:detex_cmd = 'detex -nlc ' . l:tmpfilename . ' > ' . l:tmpdetex
else
let l:detex_cmd = 'detex -nc ' . l:tmpfilename . ' > ' . l:tmpdetex
endif
sil exe '%!' . l:detex_cmd
call delete(l:tmpfilename)
let l:tmpfilename = l:tmpdetex
endif
let l:languagetool_cmd = 'java'
\ . ' -jar ' . s:languagetool_jar
\ . ' -c ' . s:languagetool_encoding
\ . (empty(s:languagetool_disable_rules) ? '' : ' -d '.s:languagetool_disable_rules)
\ . ' -l ' . s:languagetool_lang
\ . ' --api ' . l:tmpfilename
\ . ' 2> ' . l:tmperror
sil exe '%!' . l:languagetool_cmd
call delete(l:tmpfilename)
if v:shell_error
echoerr 'Command [' . l:languagetool_cmd . '] failed with error: '
\ . v:shell_error
if filereadable(l:tmperror)
echoerr string(readfile(l:tmperror))
endif
call delete(l:tmperror)
call s:LanguageToolClear()
return -1
endif
call delete(l:tmperror)
" Loop on all errors in XML output of LanguageTool and
" collect information about all errors in list s:errors
let s:errors = []
while search('^<error ', 'eW') > 0
let l:l = getline('.')
" The fromx and tox given by LanguageTool are not reliable.
" They are even sometimes negative!
let l:error= {}
for l:k in [ 'fromy', 'fromx', 'tox', 'toy',
\ 'ruleId', 'subId', 'msg', 'replacements',
\ 'context', 'contextoffset', 'errorlength', 'url' ]
let l:error[l:k] = s:ParseKeyValue(l:k, l:l)
endfor
" Make line/column number start at 1 rather than 0.
" Make also line number absolute as in buffer.
let l:error['fromy'] += a:line1
let l:error['fromx'] += 1
let l:error['toy'] += a:line1
let l:error['tox'] += 1
call add(s:errors, l:error)
endwhile
if s:languagetool_win_height >= 0
" Reformat the output of LanguageTool (XML is not human friendly) and
" set up syntax highlighting in the buffer which shows all errors.
sil %d
call append(0, '# ' . l:languagetool_cmd)
set bt=nofile
setlocal nospell
syn clear
call matchadd('LanguageToolCmd', '\%1l.*')
call matchadd('LanguageToolErrorCount', '^Error:\s\+\d\+.\d\+')
call matchadd('LanguageToolLabel', '^\(Context\|Message\|Correction\|URL\):')
call matchadd('LanguageToolUrl', '^URL:\s*\zs.*')
let l:i = 0
for l:error in s:errors
call append('$', 'Error: '
\ . (l:i + 1) . '/' . len(s:errors)
\ . ' ' . l:error['ruleId'] . ((len(l:error['subId']) == 0) ? '' : ':') . l:error['subId']
\ . ' @ ' . l:error['fromy'] . 'L ' . l:error['fromx'] . 'C')
call append('$', 'Message: ' . l:error['msg'])
call append('$', 'Context: ' . l:error['context'])
let l:re =
\ '\%' . line('$') . 'l\%9c'
\ . '.\{' . (4 + l:error['contextoffset']) . '}\zs'
\ . '.\{' . (l:error['errorlength']) . '}'
if l:error['ruleId'] =~# 'HUNSPELL_RULE\|HUNSPELL_NO_SUGGEST_RULE\|MORFOLOGIK_RULE_.*\|GERMAN_SPELLER_RULE'
call matchadd('LanguageToolSpellingError', l:re)
else
call matchadd('LanguageToolGrammarError', l:re)
endif
if !empty(l:error['replacements'])
call append('$', 'Correction: ' . l:error['replacements'])
endif
if !empty(l:error['url'])
call append('$', 'URL: ' . l:error['url'])
endif
call append('$', '')
let l:i += 1
endfor
exe "norm! z" . s:languagetool_win_height . "\<CR>"
0
map <silent> <buffer> <CR> :call <sid>JumpToCurrentError()<CR>
redraw
echon 'Press <Enter> on error in scratch buffer to jump its location'
exe "norm! \<C-W>\<C-P>"
else
" Negative s:languagetool_win_height -> no scratch window.
bd!
unlet! s:languagetool_error_buffer
endif
" Also highlight errors in original buffer and populate location list.
setlocal errorformat=%f:%l:%c:%m
for l:error in s:errors
let l:re = s:LanguageToolHighlightRegex(l:error['fromy'],
\ l:error['context'],
\ l:error['contextoffset'],
\ l:error['errorlength'])
if l:error['ruleId'] =~# 'HUNSPELL_RULE\|HUNSPELL_NO_SUGGEST_RULE\|MORFOLOGIK_RULE_.*\|GERMAN_SPELLER_RULE'
call matchadd('LanguageToolSpellingError', l:re)
else
call matchadd('LanguageToolGrammarError', l:re)
endif
laddexpr expand('%') . ':'
\ . l:error['fromy'] . ':' . l:error['fromx'] . ':'
\ . l:error['ruleId'] . ' ' . l:error['msg']
endfor
return 0
endfunction
" This function clears syntax highlighting created by LanguageTool plugin
" and removes the scratch window containing grammar errors.
function s:LanguageToolClear() "{{{1
if exists('s:languagetool_error_buffer')
if bufexists(s:languagetool_error_buffer)
sil! exe "bd! " . s:languagetool_error_buffer
endif
endif
if exists('s:languagetool_text_win')
let l:win = winnr()
exe s:languagetool_text_win . ' wincmd w'
call setmatches(filter(getmatches(), 'v:val["group"] !~# "LanguageTool.*Error"'))
lexpr ''
lclose
exe l:win . ' wincmd w'
endif
unlet! s:languagetool_error_buffer
unlet! s:languagetool_error_win
unlet! s:languagetool_text_win
endfunction
hi def link LanguageToolCmd Comment
hi def link LanguageToolErrorCount Title
hi def link LanguageToolLabel Label
hi def link LanguageToolUrl Underlined
hi def link LanguageToolGrammarError Error
hi def link LanguageToolSpellingError WarningMsg
" Menu items {{{1
if has("gui_running") && has("menu") && &go =~# 'm'
amenu <silent> &Plugin.LanguageTool.Chec&k :LanguageToolCheck<CR>
amenu <silent> &Plugin.LanguageTool.Clea&r :LanguageToolClear<CR>
endif
" Defines commands {{{1
com! -nargs=0 LanguageToolClear :call s:LanguageToolClear()
com! -nargs=0 -range=% LanguageToolCheck :call s:LanguageToolCheck(<line1>,
\ <line2>)
" vim: fdm=marker