Skip to content

Commit 5e93094

Browse files
committed
Inspect code for error and update docstrings
1 parent ac1fc1e commit 5e93094

File tree

6 files changed

+116
-126
lines changed

6 files changed

+116
-126
lines changed

languagetool-console.el

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Keywords: grammar text docs tools convenience checker
77
;; URL: https://github.com/PillFall/Emacs-LanguageTool.el
88
;; Version: 1.2.0
9-
;; Package-Requires: ((emacs "27.0"))
9+
;; Package-Requires: ((emacs "27.1"))
1010

1111
;; This program is free software; you can redistribute it and/or modify
1212
;; it under the terms of the GNU General Public License as published by
@@ -32,11 +32,13 @@
3232
(require 'languagetool-core)
3333
(require 'languagetool-issue)
3434
(require 'languagetool-java)
35+
(eval-when-compile
36+
(require 'subr-x))
3537

3638
;; Group definition:
3739

3840
(defgroup languagetool-console nil
39-
"LanguageTool command line parser and checking"
41+
"LanguageTool command line parser and checking."
4042
:tag "Console"
4143
:prefix "languagetool-console-"
4244
:group 'languagetool)
@@ -74,7 +76,7 @@ Command Line.")
7476
;;; Function definitions:
7577

7678
(defun languagetool-console-class-p ()
77-
"Return nil if `languagetool-console-command' is not a Java class."
79+
"Return non-nil if `languagetool-console-command' is a Java class."
7880
(let ((regex (rx
7981
line-start
8082
(zero-or-more
@@ -90,7 +92,7 @@ Command Line.")
9092
(string-match-p regex languagetool-console-command)))
9193

9294
(defun languagetool-console-command-exists-p ()
93-
"Return t is `languagetool-console-command' can be used or exists.
95+
"Return non-nil if `languagetool-console-command' can be used or exists.
9496
9597
Also sets `languagetool-console-command' to a full path if needed
9698
for this package to work."
@@ -104,63 +106,55 @@ for this package to work."
104106
(unless (listp languagetool-console-arguments)
105107
(error "LanguageTool Console Arguments must be a list of strings"))
106108

107-
(let ((arguments nil))
109+
(let (arguments)
108110

109111
;; Appends LanguageTool Console Command
110112
(unless (languagetool-console-class-p)
111-
(setq arguments (append arguments (list "-jar"))))
112-
(setq arguments (append arguments (list languagetool-console-command)))
113+
(push "-jar" arguments))
114+
(push languagetool-console-command arguments)
113115

114116
;; Appends the LanguageTool arguments
115-
(setq arguments (append arguments languagetool-console-arguments))
117+
(push languagetool-console-arguments arguments)
116118

117119
;; Appends the common arguments
118-
(setq arguments (append arguments
119-
(list "--encoding" "utf8")
120-
(list "--json")))
120+
(push (list "--encoding" "utf8" "--json") arguments)
121121

122122
;; Appends the correction language information
123123
(if (string= languagetool-correction-language "auto")
124-
(setq arguments (append arguments (list "--autoDetect")))
125-
(setq arguments (append arguments (list "--language" languagetool-correction-language))))
124+
(push "--autoDetect" arguments)
125+
(push (list "--language" languagetool-correction-language) arguments))
126126

127127
;; Appends the mother tongue information
128128
(when (stringp languagetool-mother-tongue)
129-
(setq arguments (append arguments (list "--mothertongue" languagetool-mother-tongue))))
129+
(push (list "--mothertongue" languagetool-mother-tongue) arguments))
130130

131131
;; Appends the disabled rules
132-
(let ((rules ""))
133-
;; Global disabled rules
134-
(dolist (rule languagetool-disabled-rules)
135-
(if (string= rules "")
136-
(setq rules (concat rules rule))
137-
(setq rules (concat rules "," rule))))
138-
;; Local disabled rules
139-
(dolist (rule languagetool-local-disabled-rules)
140-
(if (string= rules "")
141-
(setq rules (concat rules rule))
142-
(setq rules (concat rules "," rule))))
132+
(let ((rules (string-join (append languagetool-disabled-rules languagetool-local-disabled-rules) ",")))
143133
(unless (string= rules "")
144-
(setq arguments (append arguments (list "--disable" rules)))))
145-
arguments))
134+
(push (list "--disable" rules) arguments )))
135+
(flatten-tree (reverse arguments))))
146136

147137
(defun languagetool-console-write-debug-info (text)
148138
"Write debug info in `languagetool-console-output-buffer-name'.
149139
150140
The argument TEXT is the region passed to LanguageTool for
151141
checking."
152-
(insert (propertize " ----- LanguageTool Command:" 'face 'font-lock-warning-face)
153-
"\n\n")
154-
(insert languagetool-java-bin " "
155-
(mapconcat (lambda (x) (format "%s" x)) (append
156-
(languagetool-console-parse-arguments)
157-
(languagetool-java-parse-arguments)) " ")
158-
"\n\n\n\n")
159-
(insert (propertize " ----- LanguageTool Text:" 'face 'font-lock-warning-face)
160-
"\n\n")
161-
(insert text "\n\n\n\n")
162-
(insert (propertize " ----- LanguageTool Output:" 'face 'font-lock-warning-face)
163-
"\n\n"))
142+
(insert
143+
(propertize " ----- LanguageTool Command:" 'face 'font-lock-warning-face)
144+
"\n\n"
145+
(string-join
146+
(append
147+
(list languagetool-java-bin)
148+
(languagetool-java-parse-arguments)
149+
(languagetool-console-parse-arguments))
150+
" ")
151+
"\n\n\n\n"
152+
(propertize " ----- LanguageTool Text:" 'face 'font-lock-warning-face)
153+
"\n\n"
154+
text
155+
"\n\n\n\n"
156+
(propertize " ----- LanguageTool Output:" 'face 'font-lock-warning-face)
157+
"\n\n"))
164158

165159
(defun languagetool-console-invoke-command-region (begin end)
166160
"Invoke LanguageTool passing the current region to STDIN.
@@ -219,17 +213,17 @@ Found no errors.")
219213

220214
(defun languagetool-console-matches-exists-p ()
221215
"Return t if issues where found by LanguageTool or nil otherwise."
222-
(/= 0 (length (cdr (assoc 'matches languagetool-console-output-parsed)))))
216+
(/= 0 (length (alist-get 'matches languagetool-console-output-parsed))))
223217

224218
(defun languagetool-console-highlight-matches (begin)
225219
"Highlight issues in the buffer.
226220
227221
BEGIN defines the start of the current region."
228-
(let ((corrections (cdr (assoc 'matches languagetool-console-output-parsed))))
222+
(let ((corrections (alist-get 'matches languagetool-console-output-parsed)))
229223
(dotimes (index (length corrections))
230224
(let* ((correction (aref corrections index))
231-
(offset (cdr (assoc 'offset correction)))
232-
(size (cdr (assoc 'length correction)))
225+
(offset (alist-get 'offset correction))
226+
(size (alist-get 'length correction))
233227
(start (+ begin offset))
234228
(end (+ begin offset size))
235229
(word (buffer-substring-no-properties start end)))

languagetool-core.el

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Keywords: grammar text docs tools convenience checker
77
;; URL: https://github.com/PillFall/Emacs-LanguageTool.el
88
;; Version: 1.2.0
9-
;; Package-Requires: ((emacs "27.0"))
9+
;; Package-Requires: ((emacs "27.1"))
1010

1111
;; This program is free software; you can redistribute it and/or modify
1212
;; it under the terms of the GNU General Public License as published by
@@ -30,6 +30,8 @@
3030
;; Variable definitions:
3131

3232
(require 'ispell)
33+
(eval-when-compile
34+
(require 'subr-x))
3335

3436
(defcustom languagetool-api-key nil
3537
"LanguageTool API Key for Premium features."
@@ -165,13 +167,12 @@ A example hint function:
165167
(when (overlay-get ov 'languagetool-message)
166168
(unless (current-message)
167169
(message
168-
\"%s%s\" (overlay-get ov 'languagetool-short-message)
169-
(if (/= 0
170-
(length (overlay-get ov 'languagetool-replacements)))
170+
\"%s%s\"
171+
(overlay-get ov 'languagetool-short-message)
172+
(if (/= 0 (length (overlay-get ov 'languagetool-replacements)))
171173
(concat
172174
\" -> (\"
173-
(mapconcat
174-
#'identity (languagetool-core-get-replacements ov) \", \")
175+
(string-join (languagetool-core-get-replacements ov) \", \")
175176
\")\")
176177
\"\"))))))"
177178
:group 'languagetool
@@ -208,30 +209,28 @@ A example hint function:
208209
(when (overlay-get ov 'languagetool-message)
209210
(unless (current-message)
210211
(message
211-
"%s%s" (overlay-get ov 'languagetool-short-message)
212-
(if (/= 0
213-
(length (overlay-get ov 'languagetool-replacements)))
212+
"%s%s"
213+
(overlay-get ov 'languagetool-short-message)
214+
(if (/= 0 (length (overlay-get ov 'languagetool-replacements)))
214215
(concat
215216
" -> ("
216-
(mapconcat
217-
#'identity (languagetool-core-get-replacements ov) ", ")
217+
(string-join (languagetool-core-get-replacements ov) ", ")
218218
")")
219219
""))))))
220220

221221
(defun languagetool-core-get-replacements (overlay)
222222
"Return the replacements of OVERLAY in a list."
223223
(let ((replacements (overlay-get overlay 'languagetool-replacements))
224-
(replace nil))
224+
replace)
225225
(dotimes (index (length replacements))
226-
(setq replace (append replace
227-
(list (cdr (assoc 'value (aref replacements index)))))))
228-
replace))
226+
(push (alist-get 'value (aref replacements index)) replace))
227+
(reverse replace)))
229228

230229
(defun languagetool-core-correct-p (word)
231-
"Check if WORD is assumed correct in the current buffer."
230+
"Return non-nil if WORD is on the LocalWords comment in the current buffer."
232231
(save-excursion
233232
(goto-char (point-min))
234-
(let ((found nil))
233+
(let (found)
235234
(while (and (search-forward ispell-words-keyword nil t)
236235
(not found))
237236
(when (re-search-forward (rx

languagetool-correction.el

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Keywords: grammar text docs tools convenience checker
77
;; URL: https://github.com/PillFall/Emacs-LanguageTool.el
88
;; Version: 1.2.0
9-
;; Package-Requires: ((emacs "27.0"))
9+
;; Package-Requires: ((emacs "27.1"))
1010

1111
;; This program is free software; you can redistribute it and/or modify
1212
;; it under the terms of the GNU General Public License as published by
@@ -48,52 +48,54 @@
4848
"Parse and style minibuffer correction.
4949
5050
Get the information about corrections from OVERLAY."
51-
(let ((msg nil)
52-
(rule (cdr (assoc 'id (overlay-get overlay 'languagetool-rule))))
53-
(message (overlay-get overlay 'languagetool-message)))
51+
(let* ((msg nil)
52+
(rule (alist-get 'id (overlay-get overlay 'languagetool-rule)))
53+
(message (overlay-get overlay 'languagetool-message))
54+
(replacements (languagetool-core-get-replacements overlay))
55+
(num-choices (length replacements)))
5456
;; Add LanguageTool rule to the message
5557
(setq msg (concat msg "[" rule "] "))
5658

5759
;; Add LanguageTool correction suggestion
5860
(setq msg (concat msg (propertize (format "%s" message) 'face 'font-lock-warning-face) "\n"))
5961

6062
;; Format all the possible replacements for the correction suggestion
61-
(let ((replacements (languagetool-core-get-replacements overlay)))
62-
(when (< 0 (length replacements))
63-
(let ((num-choices (length replacements)))
64-
;; If can't assoc each replacement with each hotkey
65-
(when (> (length replacements) (length languagetool-correction-keys))
66-
(setq num-choices (length languagetool-correction-keys))
67-
(setq msg (concat msg "Not all choices shown.\n")))
68-
(setq msg (concat msg "\n"))
69-
;; Format all choices
70-
(dotimes (index num-choices)
71-
(setq msg (concat msg
72-
"["
73-
(propertize
74-
(format "%c" (aref languagetool-correction-keys index))
75-
'face 'font-lock-keyword-face)
76-
"]: "))
77-
(setq msg (concat msg (nth index replacements) " "))))))
63+
;; If can't assoc each replacement with each hotkey truncate the replacements
64+
(when (> (length replacements) (length languagetool-correction-keys))
65+
(setq num-choices (length languagetool-correction-keys))
66+
(setq msg (concat msg "Not all choices shown.\n")))
67+
(setq msg (concat msg "\n"))
68+
;; Format all choices
69+
(dotimes (index num-choices)
70+
(setq msg (concat msg
71+
"["
72+
(propertize
73+
(format "%c" (aref languagetool-correction-keys index))
74+
'face 'font-lock-keyword-face)
75+
"]: "))
76+
(setq msg (concat msg (nth index replacements) " ")))
7877
;; Add default Ignore and Skip options
7978
(setq msg (concat msg "\n["
8079
(propertize "C-i" 'face 'font-lock-keyword-face)
8180
"]: Ignore "))
8281
(setq msg (concat msg "["
8382
(propertize "C-s" 'face 'font-lock-keyword-face)
84-
"]: Skip\n"))
85-
msg))
83+
"]: Skip "))
84+
;; Some people do not know C-g is the global exit key
85+
(setq msg (concat msg "["
86+
(propertize "C-g" 'face 'font-lock-keyword-face)
87+
"]: Quit\n"))))
8688

8789
(defun languagetool-correction-apply (pressed-key overlay)
88-
"Correct text marked by LanguageTool with user choice.
90+
"Apply LanguageTool replacement suggestion in OVERLAY.
8991
9092
PRESSED-KEY is the index of the suggestion in the array contained
9193
on OVERLAY."
9294
(cond
9395
((char-equal ?\C-i pressed-key)
9496
(progn
9597
(goto-char (overlay-end overlay))
96-
(ispell-add-per-file-word-list (buffer-substring-no-properties (overlay-end overlay) (overlay-start overlay)))
98+
(ispell-add-per-file-word-list (buffer-substring-no-properties (overlay-start overlay) (overlay-end overlay)))
9799
(delete-overlay overlay)))
98100
((char-equal ?\C-s pressed-key)
99101
(goto-char (overlay-end overlay)))
@@ -110,13 +112,13 @@ on OVERLAY."
110112

111113
(defun languagetool-correction-at-point ()
112114
"Show issue at point and try to apply suggestion."
113-
(let (pressed-key)
114-
(dolist (ov (overlays-at (point)))
115-
(when (overlay-get ov 'languagetool-message)
116-
(message nil)
117-
(setq pressed-key
118-
(read-char (languagetool-correction-parse-message ov)))
119-
(languagetool-correction-apply pressed-key ov)))))
115+
(dolist (ov (overlays-at (point)))
116+
(when (overlay-get ov 'languagetool-message)
117+
;; Cancel any previous message
118+
(message nil)
119+
(languagetool-correction-apply
120+
(read-char (languagetool-correction-parse-message ov))
121+
ov))))
120122

121123
(provide 'languagetool-correction)
122124

languagetool-issue.el

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Keywords: grammar text docs tools convenience checker
77
;; URL: https://github.com/PillFall/Emacs-LanguageTool.el
88
;; Version: 1.2.0
9-
;; Package-Requires: ((emacs "27.0"))
9+
;; Package-Requires: ((emacs "27.1"))
1010

1111
;; This program is free software; you can redistribute it and/or modify
1212
;; it under the terms of the GNU General Public License as published by
@@ -37,7 +37,7 @@
3737
;; Group definition:
3838

3939
(defgroup languagetool-issue nil
40-
"LanguageTool faces for marking issues"
40+
"LanguageTool faces for marking issues."
4141
:tag "Issue Faces"
4242
:prefix "languagetool-issue-"
4343
:group 'languagetool)
@@ -92,7 +92,7 @@ Each element is a cons cell with the form (ISSUE_TYPE . FACE_NAME)."
9292

9393
(defun languagetool-issue-get-face (issue-type)
9494
"Return the face for ISSUE-TYPE."
95-
(or (cdr (assoc issue-type languagetool-issue-face-alist))
95+
(or (alist-get issue-type languagetool-issue-face-alist)
9696
'languagetool-issue-default))
9797

9898
(defun languagetool-issue-create-overlay (begin end correction)
@@ -102,11 +102,11 @@ Create an overlay for correction in the region delimited by BEGIN
102102
and END, parsing CORRECTION as overlay properties."
103103
(save-excursion
104104
(let* ((ov (make-overlay begin end))
105-
(short-message (cdr (assoc 'shortMessage correction)))
106-
(message (cdr (assoc 'message correction)))
107-
(replacements (cdr (assoc 'replacements correction)))
108-
(rule (cdr (assoc 'rule correction)))
109-
(issue-type (cdr (assoc 'issueType rule))))
105+
(short-message (alist-get 'shortMessage correction))
106+
(message (alist-get 'message correction))
107+
(replacements (alist-get 'replacements correction))
108+
(rule (alist-get 'rule correction))
109+
(issue-type (alist-get 'issueType rule)))
110110
(when (string= short-message "")
111111
(setq short-message message))
112112
(overlay-put ov 'languagetool-short-message short-message)

0 commit comments

Comments
 (0)