This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflymake-tldr-lint.el
357 lines (314 loc) · 13.8 KB
/
flymake-tldr-lint.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
;;; flymake-tldr-lint.el --- A TlDr Flymake backend powered by tldr-lint -*- lexical-binding: t; -*-
;; Copyright (c) 2022 Emily Grace Seville <[email protected]>
;; Version: 0.3
;; Package-Requires: ((emacs "27.1"))
;; 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Based in part on:
;; https://www.gnu.org/software/emacs/manual/html_node/flymake/An-annotated-example-backend.html
;;
;; Usage:
;; (add-hook 'markdown-mode-hook 'flymake-tldr-lint-load)
;; URL: https://github.com/tldr-pages/tldr-emacs-extension
;;; Code:
(require 'flymake)
(defgroup flymake-tldr-lint nil
"TlDr linter backend for Flymake."
:prefix "flymake-tldr-lint-"
:group 'tools)
(defcustom flymake-tldr-lint-program "tldr-lint"
"The name of the TlDr linter executable."
:type 'string)
(defcustom flymake-tldr-lint-ignored ""
"List of ignored errors."
:type 'string)
(defvar-local flymake-tldr-lint--proc nil)
(defun flymake-tldr-lint--replace-regexp-entire-buffer (pattern replacement)
"Perform regular-expression replacement throughout buffer.
PATTERN: pattern to search
REPLACEMENT: replacement for pattern"
(save-excursion
(goto-char (point-min))
(while (re-search-forward pattern nil t)
(replace-match replacement))))
(defun flymake-tldr-lint-remove-broken-ellipsis()
"Remove broken ellipsis placeholder.
Remove {{...}} placeholders in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"[ ]*{{\\.\\{3\\}}}[ ]*"
"")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-remove-broken-numbers()
"Remove broken numbers in placeholders.
Replace {{placeholder_number}}
placeholders with {{placeholder}} in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"{{\\([^{}_]+\\)_+\\(?:[0-9]+\\)}}"
"{{\\1}}")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-remove-broken-files()
"Remove broken file placeholders.
Remove {{file}}, {{filename}}, and {{file_name}}
placeholders in the current buffer.
Trailing numbers are respected too."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"[ ]*\\([\"']?\\){{file_?\\(?:name\\)?\\(?:[0-9]*\\)}}\\1[ ]*"
"")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-remove-broken-directories()
"Remove broken directory placeholders.
Remove {{dir}}, {{dirname}}, {{dir_name}},
{{directory}}, {{directoryname}}, and {{directory_name}}
placeholders in the current buffer.
Trailing numbers are respected too."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"[ ]*\\([\"']?\\){{dir\\(?:ectory\\)?_?\\(?:name\\)?\\(?:[0-9]*\\)}}\\1[ ]*"
"")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-remove-broken-all()
"Apply all `tldr-remove-*` actions."
(interactive)
(flymake-tldr-lint-remove-broken-ellipsis)
(flymake-tldr-lint-remove-broken-numbers)
(flymake-tldr-lint-remove-broken-files)
(flymake-tldr-lint-remove-broken-directories))
(defun flymake-tldr-lint-correct-broken-ellipsis()
"Correct broken ellipsis placeholder.
Replace {{placeholdernumber1}} {{placeholdernumber2}} ...
placeholders with {{placeholdernumber1 placeholdernumber2 ...}}
in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"{{\\([^{}]+\\)[0-9]+}}\\([ ]+\\(?:{{\\1[0-9]+}}\\)\\)+"
"{{\\11 \\12 ...}}")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-correct-broken-numbers()
"Correct broken numbers in placeholders.
Replace {{placeholder_number}}
placeholders with {{placeholdernumber}} in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"{{\\([^{}_]+\\)_+\\([0-9]+\\)}}" "{{\\1\\2}}")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-correct-broken-files()
"Remove broken file placeholders.
Replace {{file}}, {{filename}}, and {{file_name}}
placeholders with {{path/to/file}} in the current buffer.
Trailing numbers are respected too."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"\\([\"']?\\){{file_?\\(?:name\\)?\\([0-9]*\\)}}\\1"
"{{path/to/file\\2}}")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-correct-broken-directories()
"Correct broken directory placeholders.
Replace {{dir}}, {{dirname}}, {{dir_name}},
{{directory}}, {{directoryname}}, and {{directory_name}}
placeholders with {{path/to/directory}} in the current buffer.
Trailing numbers are respected too."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"\\([\"']?\\){{dir\\(?:ectory\\)?_?\\(?:name\\)?\\([0-9]*\\)}}\\1"
"{{path/to/directory\\2}}")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-correct-broken-ranges()
"Correct broken range placeholders.
Replace {{from-to}} placeholders with {{from..to}} in the current buffer.
If `from` or `to` is missing then
it's replaced with negative or positive infinity respectively."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"{{\\([0-9]+\\)-+\\([0-9]+\\)}}"
"{{\\1..\\2}}")
(flymake-tldr-lint--replace-regexp-entire-buffer
"{{-+\\([0-9]+\\)}}"
"{{-infinity..\\1}}")
(flymake-tldr-lint--replace-regexp-entire-buffer
"{{\\([0-9]+\\)-+}}"
"{{\\1..infinity}}")
(flymake-tldr-lint--replace-regexp-entire-buffer
"{{-+}}"
"{{any}}")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-correct-broken-long-option-argument()
"Correct term duplication for option-argument pair.
Replace --option option syntax with --option any in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"--\\([A-Za-z0-9]\\{2,\\}\\)[ ]+\\([\"']?\\)\\1\\2"
"--\\1 \\2any\\2")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-correct-broken-mnemonics()
"Correct broken mnemonics in description."
(interactive)
(let ((description-pattern "^- .*:$")
(command-pattern "^`[^`]+`$")
(descriptions '())
(new-descriptions '())
(commands '())
(option-lists '()))
(save-excursion
(goto-char (point-min))
(while (re-search-forward description-pattern nil t)
(push (match-string 0) descriptions)))
(save-excursion
(goto-char (point-min))
(while (re-search-forward command-pattern nil t)
(push (match-string 0) commands)))
(if (not (equal (length descriptions) (length commands)))
(error "Code example count (%d) is not equal to description count (%d)."
(length commands)
(length descriptions)))
(dolist (command commands) ;; extract short and long options
(with-temp-buffer
(let ((option-pattern "--?\\([[:alnum:]]+\\)")
(option-list '()))
(insert command)
(goto-char (point-min))
(while (re-search-forward option-pattern nil t)
(push (match-string 1) option-list))
(push option-list option-lists))))
(dolist (description descriptions) ;; remove all existing mnemonics
(push (replace-regexp-in-string "\\[\\([[:alpha:]]+\\)\\]"
"\\1"
description)
new-descriptions))
(defvar result nil)
(defvar old-result nil)
(dotimes (i (length new-descriptions)) ;; building descriptions with new mnemonics
(dolist (option (elt option-lists i))
(setq result (elt new-descriptions i))
(setq old-result result)
(setq result (replace-regexp-in-string (concat "\\("
option
"\\)[[:alpha:]]+.*\\'")
(concat "[" option "]") result nil nil 1))
(if (string= result old-result)
(setq result (replace-regexp-in-string (concat
"[[:alpha:]]+\\("
option
"\\).*\\'")
(concat "[" option "]") result nil nil 1)))
(setf (nth i new-descriptions) result)))
(dotimes (i (length new-descriptions))
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
(elt descriptions (- (length descriptions) i 1))
(elt new-descriptions i))))
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-correct-broken-all()
"Apply all `tldr-correct-*` actions."
(interactive)
(flymake-tldr-lint-correct-broken-numbers)
(flymake-tldr-lint-correct-broken-files)
(flymake-tldr-lint-correct-broken-directories)
(flymake-tldr-lint-correct-broken-ranges)
(flymake-tldr-lint-correct-broken-ellipsis)
(flymake-tldr-lint-correct-broken-long-option-argument)
(flymake-tldr-lint-correct-broken-mnemonics))
(defun flymake-tldr-lint-convert-long-option-space-separated()
"Convert long option-argument pair to space separated.
Replace --option=value syntax with --option value any in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"--\\([A-Za-z0-9]\\{2,\\}\\)=\\([^\"' ]+\\)"
"--\\1 \\2")
(flymake-tldr-lint--replace-regexp-entire-buffer
"--\\([A-Za-z0-9]\\{2,\\}\\)=\"\\([^\"]+\\)\""
"--\\1 \"\\2\"")
(flymake-tldr-lint--replace-regexp-entire-buffer
"--\\([A-Za-z0-9]\\{2,\\}\\)='\\([^\"]+\\)'"
"--\\1 '\\2'")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint-convert-long-option-equal-sign-separated()
"Convert long option-argument pair to sign separated.
Replace --option value syntax with --option=value any in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(flymake-tldr-lint--replace-regexp-entire-buffer
"--\\([A-Za-z0-9]\\{2,\\}\\)[ ]+\\([^\"' ]+\\)"
"--\\1=\\2")
(flymake-tldr-lint--replace-regexp-entire-buffer
"--\\([A-Za-z0-9]\\{2,\\}\\)[ ]+\"\\([^\"]+\\)\""
"--\\1=\"\\2\"")
(flymake-tldr-lint--replace-regexp-entire-buffer
"--\\([A-Za-z0-9]\\{2,\\}\\)[ ]+'\\([^\"]+\\)'"
"--\\1='\\2'")
(message "Save file to update list of TlDr errors")))
(defun flymake-tldr-lint--backend(report-fn &rest _args)
"TlDr lint backend for Flymake.
Check for problems, then call REPORT-FN with results."
(unless (executable-find flymake-tldr-lint-program)
(error "Could not find tldr-lint executable"))
(when (process-live-p flymake-tldr-lint--proc)
(kill-process flymake-tldr-lint--proc)
(setq flymake-tldr-lint--proc nil))
(let* ((source (current-buffer))
(filename (buffer-file-name source)))
(save-restriction
(widen)
(setq
flymake-tldr-lint--proc
(make-process
:name "tldr-lint-flymake" :noquery t :connection-type 'pipe
:buffer (generate-new-buffer " *tldr-lint-flymake*")
:command (remove nil (list flymake-tldr-lint-program
filename))
:sentinel
(lambda (proc _event)
(when (eq 'exit (process-status proc))
(unwind-protect
(if (with-current-buffer source (eq proc flymake-tldr-lint--proc))
(with-current-buffer (process-buffer proc)
(goto-char (point-min))
(cl-loop
while (search-forward-regexp
"^.+?:\\([0-9]+\\): \\(TLDR[0-9]+\\) \\(.*\\)$"
nil t)
for id = (match-string 2)
for msg = (match-string 3)
for (beg . end) = (flymake-diag-region
source
(string-to-number (match-string 1)) 1)
for type = :error
if (null (string-match (regexp-quote id) flymake-tldr-lint-ignored))
collect (flymake-make-diagnostic source
beg
end
type
(format "[%s] %s" id msg))
into diags
finally (funcall report-fn diags)))
(flymake-log :warning "Canceling obsolete check %s"
proc))
(kill-buffer (process-buffer proc))))))))))
;;;###autoload
(defun flymake-tldr-lint-load()
"Add the tldr-lint backend into Flymake's diagnostic functions list."
(add-hook 'flymake-diagnostic-functions #'flymake-tldr-lint--backend nil t))
(provide 'flymake-tldr-lint)
;;; flymake-tldr-lint.el ends here