forked from mhayashi1120/Emacs-langtool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
langtool.el
1784 lines (1586 loc) · 60.5 KB
/
langtool.el
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; langtool.el --- Grammar check utility using LanguageTool
;; Copyright (C) 2011-2020 Masahiro Hayashi
;; Author: Masahiro Hayashi <[email protected]>
;; Keywords: docs
;; URL: https://github.com/mhayashi1120/Emacs-langtool
;; Emacs: GNU Emacs 24 or later
;; Version: 2.2.1
;; Package-Requires: ((cl-lib "0.3"))
;; 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 3, or (at
;; your option) any later version.
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; ## Install:
;; Install LanguageTool version 3.0 or later (and java)
;; https://languagetool.org/
;; Put this file into load-path'ed directory, and byte compile it if
;; desired. And put the following expression into your ~/.emacs.
;;
;; (require 'langtool)
;; ## Settings (required):
;;
;; langtool.el have 3 types of client.
;; 1. Command line
;;
;; This setting should be set, if you use rest of clients, to get full of
;; completion support. And you should be set the variables before load
;; this library.
;;
;; (setq langtool-language-tool-jar "/path/to/languagetool-commandline.jar")
;; (require 'langtool)
;;
;; Alternatively, you can set the classpath where LanguageTool's jars reside
;; (e.g. ArchLinux):
;;
;; (setq langtool-java-classpath
;; "/usr/share/languagetool:/usr/share/java/languagetool/*")
;; (require 'langtool)
;;
;;
;; You can set a script that hold java setting (e.g. Gentoo):
;;
;; (setq langtool-bin "/path/to/your/langtool")
;; (require 'langtool)
;; 2. HTTP server & client
;;
;; You can use HTTP server implementation. This is very fast after listen server,
;; but has security risk if there are multiple user on a same host.
;;
;; (setq langtool-language-tool-server-jar "/path/to/languagetool-server.jar")
;;
;; You can change HTTP server port number like following.
;;
;; (setq langtool-server-user-arguments '("-p" "8082"))
;; 3. HTTP client
;;
;; If you have running HTTP LanguageTool server instance on any machine:
;;
;; (setq langtool-http-server-host "localhost"
;; langtool-http-server-port 8082)
;;
;; Now testing although, that running instance is working under HTTPSServer or via
;; general ssl support (e.g. nginx) following may be working. Again, this is now
;; testing, so please open issue when the ssl/tls connection is not working.
;;
;; (setq langtool-http-server-stream-type 'tls)
;; ## Optional settings
;;
;; * Key binding if you desired.
;;
;; (global-set-key "\C-x4w" 'langtool-check)
;; (global-set-key "\C-x4W" 'langtool-check-done)
;; (global-set-key "\C-x4l" 'langtool-switch-default-language)
;; (global-set-key "\C-x44" 'langtool-show-message-at-point)
;; (global-set-key "\C-x4c" 'langtool-correct-buffer)
;; * Default language is detected by LanguageTool automatically.
;; Please set `langtool-default-language` if you need specific language.
;;
;; (setq langtool-default-language "en-US")
;;
;; Otherwise, invoke `M-x langtool-check` with `C-u` (universal-argument)
;; * Currently GNU java version is not working.
;; Please change the variable to your favorite java executable.
;;
;; (setq langtool-java-bin "/path/to/java")
;; * Maybe your LanguageTool have launcher. (e.g. Gentoo)
;; You need to set `langtool-bin'.
;; See https://github.com/mhayashi1120/Emacs-langtool/issues/24
;;
;; (setq langtool-bin "/usr/bin/languagetool")
;; * Maybe you want to specify your mother tongue.
;;
;; (setq langtool-mother-tongue "en")
;; * To customize LanguageTool commandline arguments.
;;
;; (setq langtool-java-user-arguments '("-Dfile.encoding=UTF-8"))
;;
;; You can also make the variable to buffer local like following:
;;
;; (add-hook '**SOME**-mode-hook
;; (lambda () (set (make-local-variable 'langtool-java-user-arguments)
;; '("-Dfile.encoding=UTF-8"))))
;;
;; NOTE: Although there is no good example, `langtool-user-arguments' is
;; a similar custom variable.
;; ## Usage:
;; * To check current buffer and show warnings.
;;
;; M-x langtool-check
;;
;; Check with different language. You can complete supported language
;; with C-i/TAB
;;
;; C-u M-x langtool-check
;; * To correct marker follow LanguageTool suggestions.
;;
;; M-x langtool-correct-buffer
;; * Go to warning point you can see a report from LanguageTool.
;; Otherwise:
;;
;; M-x langtool-show-message-at-point
;; * Show LanguageTool report automatically by `popup'
;; This idea come from:
;; https://laclefyoshi.hatenablog.com/entry/20150912/langtool_popup
;;
;; (defun langtool-autoshow-detail-popup (overlays)
;; (when (require 'popup nil t)
;; ;; Do not interrupt current popup
;; (unless (or popup-instances
;; ;; suppress popup after type `C-g' .
;; (memq last-command '(keyboard-quit)))
;; (let ((msg (langtool-details-error-message overlays)))
;; (popup-tip msg)))))
;;
;; (setq langtool-autoshow-message-function
;; 'langtool-autoshow-detail-popup)
;; * To finish checking. All langtool marker is removed.
;;
;; M-x langtool-check-done
;;; TODO:
;; * process coding system (test on Windows)
;; * check only docstring (emacs-lisp-mode)
;; or using (derived-mode-p 'prog-mode) and only string and comment
;; * java encoding <-> elisp encoding (No enough information..)
;; * change to --json argument to parse.
;;; Code:
(require 'cl-lib)
(require 'compile)
(require 'json)
(require 'pcase)
(defgroup langtool nil
"Customize langtool"
:prefix "langtool-"
:group 'applications)
;;;
;;; Variables / Faces
;;;
;;
;; constants
;;
(defconst langtool-output-regexp
(eval-when-compile
(concat
"^[0-9]+\\.) Line \\([0-9]+\\), column \\([0-9]+\\), Rule ID: \\(.*\\)\n"
"Message: \\(.*\\)\n"
"\\(?:Suggestion: \\(.*\\)\n\\)?"
;; As long as i can read
;; src/dev/de/danielnaber/languagetool/dev/wikipedia/OutputDumpHandler.java
"\\(\\(?:.*\\)\n\\(?:[ ^]+\\)\\)\n"
"\n?" ; last result have no new-line
)))
;;
;; externals
;;
(defvar current-prefix-arg)
(defvar unread-command-events)
(defvar locale-language-names)
;;
;; faces
;;
(defface langtool-errline
'((((class color) (background dark)) (:background "Firebrick4"))
(((class color) (background light)) (:background "LightPink"))
(t (:bold t)))
"Face used for marking error lines."
:group 'langtool)
(defface langtool-correction-face
'((((class mono)) (:inverse-video t :bold t :underline t))
(t (:background "red1" :foreground "yellow" :bold t)))
"Face used to visualize correction."
:group 'langtool)
;;
;; customize variables
;;
(defcustom langtool-java-bin "java"
"Executing java command."
:group 'langtool
:type 'file)
(defcustom langtool-bin nil
"Executing LanguageTool command."
:group 'langtool
:type 'file)
(defcustom langtool-java-user-arguments nil
"List of string which is passed to java command as arguments.
This java command holds LanguageTool process.
Otherwise, function which return above value.
e.g. ( Described at http://wiki.languagetool.org/command-line-options )
\(setq langtool-java-user-arguments '(\"-Dfile.encoding=UTF-8\"))
"
:group 'langtool
:type '(choice
(repeat string)
function))
(defcustom langtool-language-tool-jar nil
"LanguageTool jar file.
No need to set this variable when `langtool-java-classpath' is set."
:group 'langtool
:type 'file)
(defcustom langtool-language-tool-server-jar nil
"LanguageTool server jar file.
Very fast, but do not use it if there is unreliable user on a same host."
:group 'langtool
:type 'file)
(defcustom langtool-http-server-host nil
"Normally should be \"localhost\" . Do not set the untrusted host/network.
Your post may not be encrypted application layer, so your privacy may be leaked.
Please set `langtool-http-server-port' either.
"
:group 'langtool
:type 'string)
(defcustom langtool-http-server-port nil
"See `langtool-http-server-host' ."
:group 'langtool
:type 'number)
(defcustom langtool-http-server-stream-type nil
"This is now testing and not enough tested yet. This value is passed to
`open-network-stream' `:type' argument.
Valid arguments are same to above except `nil'. This means `plain'."
:group 'langtool
:type 'symbol)
(defcustom langtool-java-classpath nil
"Custom classpath to use on special environment. (e.g. Arch Linux)
Do not set both of this variable and `langtool-language-tool-jar'.
https://github.com/mhayashi1120/Emacs-langtool/pull/12
https://github.com/mhayashi1120/Emacs-langtool/issues/8"
:group 'langtool
:type 'string)
(defcustom langtool-default-language nil
"Language name pass to LanguageTool command.
This is string which indicate locale or `auto' or `nil'.
Currently `auto' and `nil' is a same meaning."
:group 'langtool
:type '(choice
string
(const auto)
(const nil)))
(defcustom langtool-mother-tongue nil
"Your mothertongue Language name pass to LanguageTool."
:group 'langtool
:type 'string)
(defcustom langtool-disabled-rules nil
"Disabled rules pass to LanguageTool.
String that separated by comma or list of string.
"
:group 'langtool
:type '(choice
(list string)
string))
(defcustom langtool-user-arguments nil
"Similar to `langtool-java-user-arguments' except this list is appended
after `-jar' argument.
Valid values are described below:
http://wiki.languagetool.org/command-line-options
Do not change this variable if you don't understand what you are doing.
"
:group 'langtool
:type '(choice
(repeat string)
function))
(defcustom langtool-server-user-arguments nil
"`langtool-language-tool-server-jar' customize arguments.
You can pass `--config' option to the server that indicate java property file.
You can see all valid arguments with following command (Replace path by yourself):
java -jar /path/to/languagetool-server.jar --help
"
:group 'langtool
:type '(choice
(repeat string)
function))
(defcustom langtool-client-filter-query-function nil
"Filter function that accept one query form argument.
This query form is an alist will be encoded by `url-build-query-string'.
Call just before POST with `application/x-www-form-urlencoded'."
:group 'langtool
:type 'function)
(defcustom langtool-error-exists-hook
'(langtool-autoshow-ensure-timer)
"Hook run after LanguageTool process found any error(s)."
:group 'langtool
:type 'hook)
(defcustom langtool-noerror-hook nil
"Hook run after LanguageTool report no error."
:group 'langtool
:type 'hook)
(defcustom langtool-finish-hook
'(langtool-autoshow-cleanup-timer-maybe)
"Hook run after cleanup buffer."
:group 'langtool
:type 'hook)
;;
;; local variables
;;
(defvar langtool-generic-check-predicate nil
"Function providing per-mode customization over which regions are checked.
The \"start\" and \"end\" is passed as parameters of predicate.
Returns t to continue checking, nil otherwise.
Sample setup for `org-mode',
(eval-after-load 'org-mode
'(progn
(setq langtool-generic-check-predicate
'(lambda (start end)
;; set up for `org-mode'
(let* ((begin-regexp \"^[ \t]*#\\+begin_\\(src\\|html\\|latex\\|example\\|quote\\)\")
(end-regexp \"^[ \t]*#\\+end_\\(src\\|html\\|latex\\|example\\|quote\\)\")
(case-fold-search t)
(ignored-font-faces '(org-verbatim
org-block-begin-line
org-meta-line
org-tag org-link
org-level-1
org-document-info))
(rlt t)
ff
th
b e)
(save-excursion
(goto-char start)
;; get current font face
(setq ff (get-text-property start 'face))
(if (listp ff) (setq ff (car ff)))
;; ignore certain errors by set rlt to nil
(cond
((memq ff ignored-font-faces)
;; check current font face
(setq rlt nil))
((string-match \"^ *- $\" (buffer-substring (line-beginning-position) (+ start 2)))
;; dash character of \" - list item 1\"
(setq rlt nil))
((and (setq th (thing-at-point 'evil-WORD))
(or (string-match \"^=[^=]*=[,.]?$\" th)
(string-match \"^\\[\\[\" th)))
;; embedded cde like =w3m= or org-link [[http://google.com][Google]] or [[google.com]]
;; langtool could finish checking before major mode prepare font face for all texts
(setq rlt nil))
(t
;; inside source block?
(setq b (re-search-backward begin-regexp nil t))
(if b (setq e (re-search-forward end-regexp nil t)))
(if (and b e (< start e)) (setq rlt nil)))))
;; (if rlt (message \"start=%s end=%s ff=%s\" start end ff))
rlt)))))
")
(make-variable-buffer-local 'langtool-generic-check-predicate)
(defvar langtool-local-disabled-rules nil)
(make-variable-buffer-local 'langtool-local-disabled-rules)
(defvar langtool-temp-file nil)
(make-variable-buffer-local 'langtool-temp-file)
(defvar langtool-buffer-process nil)
(make-variable-buffer-local 'langtool-buffer-process)
(defvar langtool-mode-line-message nil)
(make-variable-buffer-local 'langtool-mode-line-message)
(put 'langtool-mode-line-message 'risky-local-variable t)
(defvar langtool-mode-line-process nil)
(make-variable-buffer-local 'langtool-mode-line-process)
(put 'langtool-mode-line-process 'risky-local-variable t)
(defvar langtool-mode-line-server-process nil)
(put 'langtool-mode-line-server-process 'risky-local-variable t)
(defvar langtool-error-buffer-name " *LanguageTool Errors* ")
(defvar langtool--debug nil)
(defvar langtool--correction-keys
;; (q)uit, (c)lear, (e)dit, (i)gnore
[?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
;; suggestions may over 10.
;; define rest of alphabet just in case.
?a ?b ?d ?f ?g ?h ?j ?k ?l ?m ?n
?o ?p ?r ?s ?t ?u ?v ?w ?x ?y ?z])
;;;
;;; Internal functions
;;;
;;
;; basic functions
;;
(defun langtool-region-active-p ()
(cond
((fboundp 'region-active-p)
(funcall 'region-active-p))
(t
(and transient-mark-mode mark-active))))
(defun langtool--debug (key fmt &rest args)
(when langtool--debug
(let ((buf (get-buffer-create "*Langtool Debug*")))
(with-current-buffer buf
(goto-char (point-max))
(insert "---------- [" key "] ----------\n")
(insert (apply 'format fmt args) "\n")))))
(defun langtool--chomp (s)
(if (string-match "\\(?:\\(\r\n\\)+\\|\\(\n\\)+\\)\\'" s)
(substring s 0 (match-beginning 0))
s))
(defun langtool--make-temp-file ()
(make-temp-file "langtool-"))
;;
;; HTTP basic
;;
(defun langtool-http--parse-response-header ()
;; Not a exact parser. Just a necessary. ;-)
(save-excursion
(goto-char (point-min))
(unless (re-search-forward "^\r\n" nil t)
(error "Parse error. Not found http header separator."))
(let (status headers body-start)
(setq body-start (point))
(forward-line -1)
(save-restriction
(narrow-to-region (point-min) (point))
(goto-char (point-min))
(unless (looking-at "^HTTP/[0-9.]+[\s\t]+\\([0-9]+\\)")
(error "Parse error. Not found HTTP status code"))
(setq status (string-to-number (match-string-no-properties 1)))
(forward-line)
(while (not (eobp))
(let (key value)
(unless (looking-at "^\\([^:]+\\):")
(error "Invalid header of HTTP response"))
(setq key (match-string-no-properties 1))
(goto-char (match-end 0))
(while (looking-at "[\s\t]+\\(.*\\)\r")
(setq value (concat value (match-string-no-properties 1)))
(forward-line 1))
(setq headers (cons (cons key value) headers))))
(list status headers body-start)))))
;;
;; handle error overlay
;;
;;FIXME
;;http://sourceforge.net/tracker/?func=detail&aid=3054895&group_id=110216&atid=655717
(defun langtool--fuzzy-search (context-regexp length)
(let* ((regexp (concat ".*?" context-regexp))
(default (cons (point) (+ (point) length))))
(or (and (null regexp)
(cons (point) (+ (point) length)))
(and (looking-at regexp)
(cons (match-beginning 1) (match-end 1)))
(let ((beg (min (point-at-bol) (- (point) 20))))
(cl-loop while (and (not (bobp))
(<= beg (point)))
;; backward just sentence length to search sentence after point
do (condition-case nil
(backward-char length)
(beginning-of-buffer nil))
if (looking-at regexp)
return (cons (match-beginning 1) (match-end 1))))
default)))
(defun langtool--compute-start&end (version check)
(let ((line (nth 0 check))
(col (nth 1 check))
(len (nth 2 check))
(context (nth 7 check))
;; Only Server <-> Client have the data
(offset (nth 8 check)))
(cond
(offset
(let* ((start (+ (point-min) offset))
(end (+ start len)))
(cons start end)))
(context
;; Command-line client have a bug that point to wrong place.
(goto-char (point-min))
(forward-line (1- line))
;; 1. sketchy move to column that is indicated by LanguageTool.
;; 2. fuzzy match to reported sentence which indicated by ^^^ like string.
;; 3. restrict to the current line
(when (< 0 col)
(forward-char (1- col)))
(langtool--fuzzy-search context len))
(t
(goto-char (point-min))
(forward-line (1- line))
(forward-char col)
(cons (point) (+ (point) len))))))
(defun langtool--create-overlay (version check)
(cl-destructuring-bind (start . end)
(langtool--compute-start&end version check)
(unless (and langtool-generic-check-predicate
(not (funcall langtool-generic-check-predicate start end)))
(let ((ov (make-overlay start end)))
(overlay-put ov 'langtool-simple-message (nth 4 check))
(overlay-put ov 'langtool-message (nth 5 check))
(overlay-put ov 'langtool-suggestions (nth 3 check))
(overlay-put ov 'langtool-rule-id (nth 6 check))
(overlay-put ov 'priority 1)
(overlay-put ov 'face 'langtool-errline)))))
(defun langtool--clear-buffer-overlays ()
(mapc
(lambda (ov)
(delete-overlay ov))
(langtool--overlays-region (point-min) (point-max))))
(defun langtool--overlays-region (start end)
(sort
(remove
nil
(mapcar
(lambda (ov)
(when (overlay-get ov 'langtool-message)
ov))
(overlays-in start end)))
(lambda (ov1 ov2)
(< (overlay-start ov1) (overlay-start ov2)))))
(defun langtool--current-error-overlays ()
(remove nil
(mapcar
(lambda (ov)
(and (overlay-get ov 'langtool-message)
ov))
(overlays-at (point)))))
(defun langtool--expire-buffer-overlays ()
(mapc
(lambda (o)
(unless (overlay-get o 'face)
(delete-overlay o)))
(langtool--overlays-region (point-min) (point-max))))
(defun langtool--erase-overlay (ov)
(overlay-put ov 'face nil))
(defun langtool--next-overlay (current overlays)
(cl-loop for o in (cdr (memq current overlays))
if (overlay-get o 'face)
return o))
(defun langtool--prev-overlay (current overlays)
(cl-loop for o in (cdr (memq current (reverse overlays)))
if (overlay-get o 'face)
return o))
(defun langtool--goto-error (overlays predicate)
(catch 'done
(mapc
(lambda (ov)
(when (funcall predicate ov)
(goto-char (overlay-start ov))
(throw 'done t)))
overlays)
nil))
(defun langtool-working-p ()
(cl-loop with current = (current-buffer)
for buf in (buffer-list)
when (and (not (eq buf current))
(with-current-buffer buf
(langtool--overlays-region
(point-min) (point-max))))
return buf
finally return nil))
;;
;; utility
;;
(defun langtool-simple-error-message (overlays)
"Textify error messages as long as simple."
(mapconcat
(lambda (ov)
(format
"[%s] %s%s"
(overlay-get ov 'langtool-rule-id)
(overlay-get ov 'langtool-simple-message)
(if (overlay-get ov 'langtool-suggestions)
(concat
" -> ("
(mapconcat 'identity (overlay-get ov 'langtool-suggestions) ", ")
")")
"")))
overlays "\n"))
(defun langtool-details-error-message (overlays)
"Textify error messages."
(mapconcat
(lambda (ov)
(concat
(format "Rule ID: %s\n"
(overlay-get ov 'langtool-rule-id))
(format "Message: %s\n"
(overlay-get ov 'langtool-simple-message))
(if (overlay-get ov 'langtool-suggestions)
(concat
"Suggestions: "
(mapconcat
'identity
(overlay-get ov 'langtool-suggestions)
"; "))
"")))
overlays
"\n\n"))
(defun langtool--current-error-messages ()
(mapcar
(lambda (ov)
(overlay-get ov 'langtool-message))
(langtool--current-error-overlays)))
;;;
;;; LanguageTool Process
;;;
;;
;; Process basic
;;
(defmacro langtool--with-java-environ (&rest form)
`(let ((coding-system-for-read langtool-process-coding-system))
(progn ,@form)))
(defun langtool--process-file-name (path)
"Correct the file name depending on the underlying platform.
PATH: The file-name path to be corrected.
Currently corrects the file-name-path when running under Cygwin."
(setq path (expand-file-name path))
(cond
((eq system-type 'cygwin)
;; no need to catch error. (e.g. cygpath is not found)
;; this failure means LanguageTools is not working completely.
(with-temp-buffer
(call-process "cygpath" nil t nil "--windows" path)
(langtool--chomp (buffer-string))))
(t
path)))
(defcustom langtool-process-coding-system
(cond
((eq system-type 'cygwin)
'dos)
(t nil))
"LanguageTool process coding-system.
Ordinary no need to change this."
:group 'langtool
:type 'coding-system)
(defun langtool--custom-arguments (var)
(let ((value (symbol-value var))
args)
(cond
((functionp value)
(setq args (funcall value)))
((consp value)
(setq args value)))
(copy-sequence args)))
;;
;; Command interaction
;;
(defun langtool--disabled-rules ()
(let ((custom langtool-disabled-rules)
(locals langtool-local-disabled-rules))
(cond
((stringp custom)
(mapconcat 'identity
(cons custom locals)
","))
(t
(mapconcat 'identity
(append custom locals)
",")))))
(defun langtool--basic-command&args ()
(cond
(langtool-bin
(list langtool-bin nil))
(t
(let (command args)
(setq command langtool-java-bin)
;; Construct arguments pass to java command
(setq args (langtool--custom-arguments 'langtool-java-user-arguments))
(cond
(langtool-java-classpath
(setq args (append
args
(list "-cp" langtool-java-classpath
"org.languagetool.commandline.Main")))
(list command args))
(langtool-language-tool-jar
(setq args (append
args
(list "-jar" (langtool--process-file-name langtool-language-tool-jar))))
(list command args))
(t nil))))))
(defun langtool--process-create-client-buffer ()
(generate-new-buffer " *Langtool* "))
(defun langtool--sentence-to-fuzzy (sentence)
(mapconcat 'regexp-quote
;; this sentence is reported by LanguageTool
(split-string sentence " +")
;; LanguageTool interpreted newline as space.
"[[:space:]\n]+?"))
(defun langtool--pointed-length (message)
(or
(and (string-match "\n\\( *\\)\\(\\^+\\)" message)
(length (match-string 2 message)))
;; never through here, but if return nil from this function make stop everything.
1))
;;FIXME sometimes LanguageTool reports wrong column.
(defun langtool--pointed-context-regexp (message)
(when (string-match "\\(.*\\)\n\\( *\\)\\(\\^+\\)" message)
(let* ((msg1 (match-string 1 message))
;; calculate marker "^" start at column
(pre (length (match-string 2 message)))
;; "^" marker length
(len (length (match-string 3 message)))
(end (+ pre len))
(sentence (substring msg1 pre end))
(regexp (cond
((string-match "^[[:space:]]+$" sentence)
;; invalid sentence only have whitespace,
;; search with around sentence.
(concat
"\\("
(let* ((count (length sentence))
(spaces (format "[[:space:]\n]\\{%d\\}" count)))
spaces)
"\\)"
;; considered truncated spaces that is caused by
;; `langtool--sentence-to-fuzzy'
"[[:space:]]*?"
;; to match the correct block
;; suffix of invalid spaces.
(langtool--sentence-to-fuzzy
(let ((from (min end (length msg1))))
;;TODO magic number.
(substring msg1 from (min (length msg1) (+ from 20)))))))
(t
(concat "\\("
(langtool--sentence-to-fuzzy sentence)
"\\)")))))
regexp)))
;;
;; Commandline / HTTP integration
;;
(defun langtool--checker-mode ()
;; NOTE: This priority is order by light weight.
(cond
((and langtool-http-server-host
langtool-http-server-port)
'http-client)
(langtool-language-tool-server-jar
'client-server)
((or langtool-language-tool-jar
langtool-java-classpath
langtool-bin)
'commandline)
(t
(error "There is no valid setting."))))
(defun langtool--apply-checks (proc checks)
(let ((source (process-get proc 'langtool-source-buffer))
(version (process-get proc 'langtool-jar-version))
(begin (process-get proc 'langtool-region-begin))
(finish (process-get proc 'langtool-region-finish)))
(when (buffer-live-p source)
(with-current-buffer source
(save-excursion
(save-restriction
(when (and begin finish)
(narrow-to-region begin finish))
(mapc
(lambda (check)
(langtool--create-overlay version check))
(nreverse checks))))))))
(defun langtool--lazy-apply-checks (proc version checks)
(let ((source (process-get proc 'langtool-source-buffer))
(begin (process-get proc 'langtool-region-begin))
(finish (process-get proc 'langtool-region-finish)))
(when (buffer-live-p source)
(with-current-buffer source
(save-excursion
(save-restriction
(when (and begin finish)
(narrow-to-region begin finish))
(cond
((consp checks)
(langtool--create-overlay version (car checks))
(run-with-idle-timer
1 nil 'langtool--lazy-apply-checks
proc version (cdr checks)))
(t
(let ((source (process-get proc 'langtool-source-buffer)))
(langtool--check-finish source nil))))))))))
(defun langtool--check-finish (source errmsg)
(let (marks face)
(when (buffer-live-p source)
(with-current-buffer source
(setq marks (langtool--overlays-region (point-min) (point-max)))
(setq face (cond
(errmsg
compilation-error-face)
(marks
compilation-warning-face)
(t
compilation-info-face)))
(setq langtool-buffer-process nil)
(setq langtool-mode-line-process
(propertize ":exit" 'face face))
(cond
(errmsg
(message "%s" errmsg))
(marks
(run-hooks 'langtool-error-exists-hook)
(message "%s"
(substitute-command-keys
"Type \\[langtool-correct-buffer] to correct buffer.")))
(t
(run-hooks 'langtool-noerror-hook)
(message "LanguageTool successfully finished with no error.")))))))
;;
;; LanguageTool Commandline
;;
(defun langtool-command--check-command ()
(cond
(langtool-bin
(unless (executable-find langtool-bin)
(error "LanguageTool command not executable")))
((or (null langtool-java-bin)
(not (executable-find langtool-java-bin)))
(error "java command is not found")))
(cond
(langtool-java-classpath)
(langtool-language-tool-jar
(unless (file-readable-p langtool-language-tool-jar)
(error "langtool jar file is not readable"))))
(when langtool-buffer-process
(error "Another process is running")))
;; Create utf-8-unix temporary file if need. This coding-system is
;; troubleless, I think.
(defun langtool-command--maybe-create-temp-file (&optional begin finish)
(let* ((file (buffer-file-name))
(cs buffer-file-coding-system)
(cs-base (coding-system-base cs))
custom-cs)
(unless langtool-temp-file
(setq langtool-temp-file (langtool--make-temp-file)))
;; create temporary file to pass the text contents to LanguageTool
(when (or (null file)
(buffer-modified-p)
(and begin finish)
;; 1 is dos EOL style, this must convert to unix
;; dos (CR-LF) style EOL may destroy position of marker.
(eq (coding-system-eol-type cs) 1)
;; us-ascii is included in utf-8
(and (not (coding-system-equal cs-base 'us-ascii))
(not (coding-system-equal cs-base 'utf-8))))
(save-restriction
(widen)
(let ((coding-system-for-write 'utf-8-unix))
;; BEGIN nil means entire buffer
(write-region begin finish langtool-temp-file nil 'no-msg))
(setq file langtool-temp-file)))
file))
(defun langtool-command--invoke-process (file begin finish &optional lang)
(let ((version (langtool--jar-version)))
(cl-destructuring-bind (command args)
(langtool--basic-command&args)
;; Construct arguments pass to jar file.
;; http://wiki.languagetool.org/command-line-options
(setq args (append
args
(list
"-d" (langtool--disabled-rules))))
(cond
((stringp (or lang langtool-default-language))
(setq args (append args (list "-l" (or lang langtool-default-language)))))
(t
(setq args (append args (list "--autoDetect")))))
(when langtool-mother-tongue
(setq args (append args (list "-m" langtool-mother-tongue))))