-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflymake-markdownlint-cli2.el
More file actions
154 lines (125 loc) · 6.17 KB
/
flymake-markdownlint-cli2.el
File metadata and controls
154 lines (125 loc) · 6.17 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
;;; flymake-markdownlint-cli2.el --- A markdownlint-cli-2 Flymake backend -*- lexical-binding: t; -*-
;; Copyright (c) 2024 Micah Elliott
;; Copyright (c) 2025 Edd Wilder-James
;; Author: Edd Wilder-James @ewilderj
;; URL: https://github.com/ewilderj/flymake-markdownlint-cli2
;; Package-Version: 0
;; Original author
;; Author: Micah Elliott <mde@micahelliott.com>
;; URL: https://github.com/micahelliott/flymake-mdl
;; 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:
;; Usage:
;; (require 'flymake-markdownlint-cli2)
;; (add-hook 'markdown-mode-hook 'flymake-markdownlint-cli2-setup)
;;
;; Derived largely from ruby example:
;; https://www.gnu.org/software/emacs/manual/html_node/flymake/An-annotated-example-backend.html
;;; Code:
(require 'cl-lib)
(defvar-local mdl--flymake-proc nil)
(defvar-local mdl--config-file nil)
(message "loading flymake-markdownlint-cli2 package")
(defgroup flymake-markdownlint-cli2 nil
"A markdownlint-cli2 backend for Flymake."
:prefix "flymake-markdownlint-cli2-"
:group 'tools)
(defcustom flymake-markdownlint-cli2-program
"markdownlint-cli2"
"Name of the `markdownlint-cli2' executable."
:type 'string)
(defcustom flymake-markdownlint-cli2-config-filename
".markdownlint-cli2.mjs"
"File name of the linter config file."
:type 'string)
(defcustom flymake-markdownlint-cli2-config
nil
"Full path of linter config file. Overrides search."
:type 'string)
(defun find-mdl-config (dir)
(let ((config-file (expand-file-name flymake-markdownlint-cli2-config-filename dir)))
(if (file-exists-p config-file)
config-file
(let ((parent-dir (file-name-directory (directory-file-name dir))))
(if (equal dir parent-dir)
nil
(find-mdl-config parent-dir))))))
(defun flymake-markdownlint-cli2 (report-fn &rest _args)
;; Not having the linter is a serious problem which should cause
;; the backend to disable itself, so an error is signaled.
(unless (executable-find flymake-markdownlint-cli2-program)
(error "Could not find '%s' executable" flymake-markdownlint-cli2-program))
;; If a live process launched in an earlier check was found, that
;; process is killed. When that process's sentinel eventually runs,
;; it will notice its obsoletion, since it have since reset
;; `flymake-mdl-proc' to a different value
(when (process-live-p mdl--flymake-proc) (kill-process mdl--flymake-proc))
;; Save the current buffer, the narrowing restriction, remove any narrowing restriction.
(setq mdl--config-file
(or flymake-markdownlint-cli2-config
(find-mdl-config default-directory)))
(let ((source (current-buffer))
(mdl-args
(if mdl--config-file
(list "--config" mdl--config-file "-")
(list "-")))
(default-directory (if mdl--config-file
(file-name-directory mdl--config-file)
default-directory)))
(save-restriction
(widen)
;; Reset the `mdl--flymake-proc' process to a new process calling the linter
(setq
mdl--flymake-proc
(make-process
:name "flymake-markdownlint-cli2" :noquery t :connection-type 'pipe
:buffer (generate-new-buffer " *flymake-markdownlint-cli2*") ; Make output go to a temporary buffer.
:command (append (list flymake-markdownlint-cli2-program) mdl-args)
:sentinel
(lambda (proc _event)
;; Check that the process has indeed exited, as it might be simply suspended.
(when (memq (process-status proc) '(exit signal))
(unwind-protect
;; Only proceed if `proc' is the same as `mdl--flymake-proc',
;; which indicates that `proc' is not an obsolete process.
(if (with-current-buffer source (eq proc mdl--flymake-proc))
(with-current-buffer (process-buffer proc)
;; echo buffer working directory
;; (message (expand-file-name default-directory))
;; (message (buffer-string))
(goto-char (point-min))
;; Parse the output buffer for diagnostic's messages and
;; locations, collect them in a list of objects, and call `report-fn'.
(cl-loop
while (search-forward-regexp
"^\\(stdin\\):\\([0-9]+\\):?[0-9]* \\([A-Z]+[0-9]+/.*\\)$"
nil t)
for msg = (match-string 3)
for (beg . end) = (flymake-diag-region source (string-to-number (match-string 2)))
;; for type = (if (string-match "^warning" msg) :warning :error)
for type = :warning
when (and beg end)
collect (flymake-make-diagnostic source beg end type msg)
into diags
finally (funcall report-fn diags)))
(flymake-log :warning "Canceling obsolete check %s" proc))
;; Cleanup the temporary buffer used to hold the check's output.
(kill-buffer (process-buffer proc)))))))
;; Send the buffer contents to the process's stdin, followed by an EOF.
(process-send-region mdl--flymake-proc (point-min) (point-max))
(process-send-eof mdl--flymake-proc))))
;;;###autoload
(defun flymake-markdownlint-cli2-setup ()
"Enable markdownlint-cli2 markdown flymake backend."
(add-hook 'flymake-diagnostic-functions #'flymake-markdownlint-cli2 nil t))
(provide 'flymake-markdownlint-cli2)
;;; flymake-markdownlint-cli2.el ends here