From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#57884: [PATCH] Flymake backend using the shellcheck program Date: Sun, 18 Sep 2022 11:55:42 +0000 Message-ID: <87zgewdhhd.fsf@posteo.net> References: <87a66yaqwc.fsf@gmail.com> <83bkre0w4m.fsf@gnu.org> <871qs9c3er.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11427"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , 57884@debbugs.gnu.org To: Augusto Stoffel Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Sep 18 13:56:27 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oZsuQ-0002nR-Q5 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 18 Sep 2022 13:56:26 +0200 Original-Received: from localhost ([::1]:39752 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oZsuP-0004yA-JQ for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 18 Sep 2022 07:56:25 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55994) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oZsu3-0004jR-0c for bug-gnu-emacs@gnu.org; Sun, 18 Sep 2022 07:56:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:49081) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oZsu2-00026g-PJ for bug-gnu-emacs@gnu.org; Sun, 18 Sep 2022 07:56:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oZsu2-0007BH-6A for bug-gnu-emacs@gnu.org; Sun, 18 Sep 2022 07:56:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 18 Sep 2022 11:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 57884 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 57884-submit@debbugs.gnu.org id=B57884.166350215627588 (code B ref 57884); Sun, 18 Sep 2022 11:56:02 +0000 Original-Received: (at 57884) by debbugs.gnu.org; 18 Sep 2022 11:55:56 +0000 Original-Received: from localhost ([127.0.0.1]:48159 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oZstw-0007Au-4e for submit@debbugs.gnu.org; Sun, 18 Sep 2022 07:55:56 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:53003) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oZsts-0007Ab-Ov for 57884@debbugs.gnu.org; Sun, 18 Sep 2022 07:55:54 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 06F79240105 for <57884@debbugs.gnu.org>; Sun, 18 Sep 2022 13:55:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1663502147; bh=lGqlbeSxVZPZ0k5cCOLCbBiPEoeLUnFQ8WdGwgZfr5U=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=jbttdKUPmYyF6tEcFOAROOxFkzOF1EnRoXEZTaCWCTyV3R8HbGW8peU3wIPwjxK20 DQIMOMZ/KMWJz0Qw4mufVhsbkEQb0uksmOdOWNuxBDJIdlFD8EY9+kuGzTHjBz+aTu T1aheb6D86ixbdA4knOLLWiVC7j7dpzz4Nwezmy9UCywFxIXCfN6o/icyQo4qB1d/D VhiucZb62lEZ+GkDDgm0qhYEtHGFIGYgLvzY/tviQ4kinj+XWBeWnugxpiXiG4T58Z uImuFrigSELX34kv6Q21Jfe5I9cxHfwhQ22vGVjtKVXja+Ctkqfb6ikfuld1wr7XBD XW1z8gdpdv1fA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4MVmTq53fNz9rxF; Sun, 18 Sep 2022 13:55:42 +0200 (CEST) In-Reply-To: <871qs9c3er.fsf@gmail.com> (Augusto Stoffel's message of "Sat, 17 Sep 2022 19:32:44 +0200") Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:242970 Archived-At: Augusto Stoffel writes: > > I could split this into two defcustoms if you feel strongly about it, > but it seems a bit of overengineering to me. Not many people will want >to customize the program name, and the switches would have to be > provided as a list anyway, since we're not going to call this through a > shell. I think it would be good, because then you could make the flags file local in case you need something special, without having to worry about some evil file that sets the command to '("rm" "-rf" "--no-preserve-root" "/") > Let me know what you think. > >>> +(defun sh-shellcheck-fmake (report-fn &rest _args) >>> + "Flymake backend using the shellchecprogram. >> >> The first line of a function's doc string should mtion its >> arguments. > > It didn't fit the first line, so as per standd proceu it's in the > body of the dctring. > >> And I think this warrants a NEWS entry. > > Dne. > >>>From 7598653c31bc72a30bc7ed7ba28cc9b6c6b6c843Mo Sep 17 00:00:00 2001 > From: Augusto Stoffel > Date: St17 Sep 2022 18:30:04 +0200 > Subject: [PATCH] New Flymake backend usinghe shellcheck program >>See bug#57884. > > * lisp/progmodes/sh-scrpel (shsellcheck-program, > sh--shellcheck-process, sh-shellcheck-flymake): Variles and function > defining a Flymake backen > (sh-mode): Add it to 'flymake-diagntic-functions'. > --- > etc/NEWS | 5 +++ > lsprogmodes/sh-sci.el | 71 +++++++++++++++++++++++++++++++++++++ > 2 fis changed, 76 insertions(+) > > diff --git a/etc/NEWS b/etc/NEWS > indea6a8883593..60e171ad13 10064> --- a/etc/NEWS > +++ b/etc/NEWS > @@ -1320,6 +1320, @@ is controls how statements like the foowing are indented: > foo && > bar > > ++++ > +*** New Fmake backend using the ShellCheck poam > +It is enable by default, on requiring that ShellCheck is instald. > +See https/www.shellchecket/ for details. > + > ** Cperl Mode > > --- > diff --git a/lisp/progmodes/sh-scrt.el b/lisp/ogmodes/sh-scptl > iex 517fbbd8e7..9c0f6dabd5 100644 > --- a/lisp/progmodesh-script.el > +++ b/lisp/progmodes/sh-script.el > @@ -31,6 +31,9 @@ > ;; available for filenames, variables known fromhe script, the shell and > ;; the environment as well as commands. > > +;; A Flymake backend using the ShellCheck program is provided. See > +;; https://www.shellecnet/ for instalti instctions. > + > ;;; Known Bugs: > > ;; - In Bourne the keyword `in' inot anchored to case, for, select ..> @@ -1580,6 +1583,7 @@ sh-mode > ((equal (file-name-nondirectory bfer-file-name) ".pfile") "sh") > (t sh-shell-file)) > nil nil) > + (add-hook 'fmake-diagnostic-functions #'sh-shellchecklyke nil t) > (add-hook 'hack-local-variables-hook > #'sh-afterack-local-variables nil t)) > > @@ -3103,6 +3107,73 @@ sh-dele-ckslash > ele-region (1+ (point)) > (progn (skip-chars-backward " \t") (point))))) > > +;;; Flymake backend > + > +(defcustom sh-shellcheck-command '("shellcheck") > + "The shellcheck program followed by extra arguments." > + :type '(repeat string) > + :version "29.1") > + > +(defvar-local sh--shellcheck-process nil) > + > +(defun sh-shellcheck-flymake (report-fn &rest _args) > + "Flymake backend using the shellcheck program. > +Takes a Flymake callback REPORT-FN as argument, as expected of a > +member of `flymake-diagnostic-functions'." > + (when (process-live-p sh--shellcheck-process) > + (kill-process sh--shellcheck-process)) > + (let* ((source (current-buffer)) > + (dialect (named-let dialect ((s sh-shell)) > + (pcase s > + ((or 'bash 'dash 'sh) (symbol-name s)) > + ('ksh88 "ksh") > + ((guard s) > + (dialect (alist-get s sh-ancestor-alist)))))) I don't use `named-let' that much, but calling both the result and the recursive function `dialect' seems confusing to me. > + (pattern "^-:\\([0-9]+\\):\\([0-9]+\\): \\([^:]+\\): \\(.*\\)$") Do you think that that using `rx' would make this pattern more maintainable? > + (sentinel > + (lambda (proc _event) Wouldn't it be cleaner to pull this lambda out into a separate named function? > + (when (memq (process-status proc) '(exit signal)) > + (unwind-protect > + (if (with-current-buffer source > + (not (eq proc sh--shellcheck-process))) > + (flymake-log :warning "Canceling obsolete check %s" proc) > + (with-current-buffer (process-buffer proc) > + (goto-char (point-min)) > + (cl-loop > + while (search-forward-regexp pattern nil t) > + 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 = (pcase (match-string 3) > + ("error" :error) > + ("warning" :warning) > + (_ :note)) > + when (and beg end) > + collect (flymake-make-diagnostic source beg end type msg) > + into diags > + finally (funcall report-fn diags)))) > + (kill-buffer (process-buffer proc))))))) > + (unless dialect > + (error "`sh-shellcheck-flymake' is not suitable for shell type `%s'" > + sh-shell)) > + (setq sh--shellcheck-process > + (make-process > + :name "luacheck" :noquery t :connection-type 'pipe ^ Typo? > + :buffer (generate-new-buffer " *flymake-luacheck*") ^ same here > + :command `(,(car sh-shellcheck-command) > + "--format=gcc" > + "--color=never" > + "-s" ,dialect > + ,@(cdr sh-shellcheck-command) > + "-") > + :sentinel sentinel)) > + (save-restriction > + (widen) > + (process-send-region sh--shellcheck-process (point-min) (point-max)) > + (process-send-eof sh--shellcheck-process)))) > + > (provide 'sh-script) > > ;;; sh-script.el ends here