-
Notifications
You must be signed in to change notification settings - Fork 7
/
flymake-shellcheck.el
126 lines (105 loc) · 5.05 KB
/
flymake-shellcheck.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
;;; flymake-shellcheck.el --- A bash/sh Flymake backend powered by ShellCheck -*- lexical-binding: t; -*-
;; Copyright (c) 2018 Federico Tedin
;; Copyright (c) 2020 Joseph LaFreniere (lafrenierejm) <joseph@lafreniere.xyz>
;; Author: Federico Tedin <federicotedin@gmail.com>
;; Homepage: https://github.com/federicotdn/flymake-shellcheck
;; Package-Version: 0.1
;; Package-Requires: ((emacs "26"))
;; 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 'sh-mode-hook 'flymake-shellcheck-load)
;;; Code:
(require 'flymake)
(defgroup flymake-shellcheck nil
"Shellcheck backend for Flymake."
:prefix "flymake-shellcheck-"
:group 'tools)
(define-obsolete-variable-alias 'flymake-shellcheck-path 'flymake-shellcheck-program "2022-03-08")
(defcustom flymake-shellcheck-program "shellcheck"
"The name of the `shellcheck' executable."
:type 'string)
(defcustom flymake-shellcheck-use-file nil
"When non-nil, send the contents of the file on disk to shellcheck.
Otherwise, send the contents of the buffer, whether they have been
saved or not.
Setting this variable to non-nil may yield slightly quicker syntax
checks on very large files."
:type 'boolean)
(defcustom flymake-shellcheck-allow-external-files nil
"When non-nil, allow shellcheck to source external files with the '-x' parameter.
Otherwise, external files won't be sourced."
:type 'boolean)
(defvar-local flymake-shellcheck--proc nil)
(defun flymake-shellcheck--backend (report-fn &rest _args)
"Shellcheck backend for Flymake.
Check for problems, then call REPORT-FN with results."
(unless (executable-find flymake-shellcheck-program)
(error "Could not find shellcheck executable"))
(when (process-live-p flymake-shellcheck--proc)
(kill-process flymake-shellcheck--proc)
(setq flymake-shellcheck--proc nil))
(let* ((source (current-buffer))
(filename (buffer-file-name source)))
(save-restriction
(widen)
(setq
flymake-shellcheck--proc
(make-process
:name "shellcheck-flymake" :noquery t :connection-type 'pipe
:buffer (generate-new-buffer " *shellcheck-flymake*")
:command (remove nil (list flymake-shellcheck-program
"-f" "gcc"
(if flymake-shellcheck-allow-external-files "-x")
(if flymake-shellcheck-use-file filename "-")))
:sentinel
(lambda (proc _event)
(when (eq 'exit (process-status proc))
(unwind-protect
(if (with-current-buffer source (eq proc flymake-shellcheck--proc))
(with-current-buffer (process-buffer proc)
(goto-char (point-min))
(cl-loop
while (search-forward-regexp
"^.+?:\\([0-9]+\\):\\([0-9]+\\): \\(.*\\): \\(.*\\)$"
nil t)
for severity = (match-string 3)
for msg = (match-string 4)
for (beg . end) = (flymake-diag-region
source
(string-to-number (match-string 1))
(string-to-number (match-string 2)))
for type = (cond ((string= severity "note") :note)
((string= severity "warning") :warning)
(t :error))
collect (flymake-make-diagnostic source
beg
end
type
msg)
into diags
finally (funcall report-fn diags)))
(flymake-log :warning "Canceling obsolete check %s"
proc))
(kill-buffer (process-buffer proc)))))))
(unless flymake-shellcheck-use-file
(process-send-region flymake-shellcheck--proc (point-min) (point-max))
(process-send-eof flymake-shellcheck--proc)))))
;;;###autoload
(defun flymake-shellcheck-load ()
"Add the Shellcheck backend into Flymake's diagnostic functions list."
(add-hook 'flymake-diagnostic-functions 'flymake-shellcheck--backend nil t))
(provide 'flymake-shellcheck)
;;; flymake-shellcheck.el ends here