From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#57884: [PATCH] Flymake backend using the shellcheck program Date: Sat, 17 Sep 2022 18:48:19 +0200 Message-ID: <87a66yaqwc.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25279"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) To: 57884@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 17 18:50:53 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 1oZb1o-0006OK-N8 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Sep 2022 18:50:53 +0200 Original-Received: from localhost ([::1]:49346 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oZb1n-000151-MP for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Sep 2022 12:50:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37900) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oZb04-0008Mh-6r for bug-gnu-emacs@gnu.org; Sat, 17 Sep 2022 12:49:12 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:47968) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oZb02-0004wL-Nz for bug-gnu-emacs@gnu.org; Sat, 17 Sep 2022 12:49:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oZb02-0006Sk-D7 for bug-gnu-emacs@gnu.org; Sat, 17 Sep 2022 12:49:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 17 Sep 2022 16:49:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 57884 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.166343330924789 (code B ref -1); Sat, 17 Sep 2022 16:49:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 17 Sep 2022 16:48:29 +0000 Original-Received: from localhost ([127.0.0.1]:47043 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oZazV-0006Rl-Dx for submit@debbugs.gnu.org; Sat, 17 Sep 2022 12:48:29 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:43968) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oZazS-0006Rb-Ei for submit@debbugs.gnu.org; Sat, 17 Sep 2022 12:48:27 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41670) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oZazS-00085h-6W for bug-gnu-emacs@gnu.org; Sat, 17 Sep 2022 12:48:26 -0400 Original-Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]:44028) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oZazQ-0004uO-3N for bug-gnu-emacs@gnu.org; Sat, 17 Sep 2022 12:48:25 -0400 Original-Received: by mail-ed1-x534.google.com with SMTP id y8so28720334edc.10 for ; Sat, 17 Sep 2022 09:48:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:user-agent:message-id:date:subject:to:from:from:to:cc :subject:date; bh=tmY+NbvoQaNIaeQPrSakXyG22aG6fKCSvqc4ScGePps=; b=oaJvP3lWME04+1FCS91siZWns354PM3IR4BVOiOFO/V70DobspdHSs3npMtN8yJsvQ /D3l69lRT9sNYvIngG6L3d8lJnLfw2iZGjNAj2dTmmvtVZBqzA2CjzMMTDTJqiHgD3PB XnBHF79aZndT4saHh8Q8nle10WksVe50zruTiIEpPBCkEyk6bdDCA0DbcoC6kqKXOhaA ZO/J8TwSI94A/LhcTo6O1fEW+ptkuSaAoHsqkIHqB1jaR1hx4KLggpME1NBrY6H4Is4x +XKEyWhCNQY9bOl9j409qkVQCPAEV/mjoAaGfXzjC5CFhcgIt4+T4SAkU3eNF5wIx5ou N6Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:message-id:date:subject:to:from :x-gm-message-state:from:to:cc:subject:date; bh=tmY+NbvoQaNIaeQPrSakXyG22aG6fKCSvqc4ScGePps=; b=OFpPJzrxWvi+waRKNDicSEKljCVnsbiK/UMblNGppqmcGpH32SMN5xPe+gC1Ny1WqB ULjRaby03GyzHa77jSzhPgxqxlc9YYK2+wLjTHDWTI/JvW27tJ57XAvqZYYhYyle+GPM +6wi57m8wj747bIXdQSaWjsPCrVWQcG5Hf2XMOM073SdyNYU/HsnVcpBAg91Gd731uJX sn4NhiB7VHgVIlmAzpxKVDylp/h2dZN7LatFhf7+e3DlfmSmpssXYAloElysJqCGmVUv vQHJTzh29tjTYgpi5fDU67cTuANcSL7lf1Ccfm1sDHtkVzNuAa5HzeOBytOzTxrbLGH3 hgdw== X-Gm-Message-State: ACrzQf1NCwqZ/GFfFCM1RPpT2JgkczpNb6cI1+4IY56iSxJPClbT8/Nr u1Wml2QToe7C/89pJyWnBScmWT00lfRPdw== X-Google-Smtp-Source: AMsMyM7h/Ql5mKf1fyzYvr0e+vpNPgRe7K9Jjf8TqYRGdO406fsNYaFN2dSPAZzs3JB5TgrCL+FxjQ== X-Received: by 2002:a05:6402:e86:b0:440:d1be:20c7 with SMTP id h6-20020a0564020e8600b00440d1be20c7mr8393813eda.349.1663433301658; Sat, 17 Sep 2022 09:48:21 -0700 (PDT) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::8510]) by smtp.gmail.com with ESMTPSA id s1-20020a05640217c100b0044ec76521a1sm16114084edy.55.2022.09.17.09.48.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Sep 2022 09:48:20 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::534; envelope-from=arstoffel@gmail.com; helo=mail-ed1-x534.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:242885 Archived-At: --=-=-= Content-Type: text/plain Tags: patch See https://www.shellcheck.net/ for details on the ShellCheck program. It seems to be the most usual linter for shell scripts. --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-New-Flymake-backend-using-the-shellcheck-program.patch >From 7571d833179fa49871a23607dc34f17d380eb147 Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Sat, 17 Sep 2022 18:30:04 +0200 Subject: [PATCH] New Flymake backend using the shellcheck program * lisp/progmodes/sh-script.el (sh-shellcheck-program, sh--shellcheck-process, sh-shellcheck-flymake): Variables and function defining a Flymake backend. (sh-mode): Add it to 'flymake-diagnostic-functions'. --- lisp/progmodes/sh-script.el | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 517fbbd8e7..242afccb3f 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -31,6 +31,9 @@ ;; available for filenames, variables known from the script, the shell and ;; the environment as well as commands. +;; A Flymake backend using the ShellCheck program is provided. See +;; https://www.shellcheck.net/ for installation instructions. + ;;; Known Bugs: ;; - In Bourne the keyword `in' is not anchored to case, for, select ... @@ -1580,6 +1583,7 @@ sh-mode ((equal (file-name-nondirectory buffer-file-name) ".profile") "sh") (t sh-shell-file)) nil nil) + (add-hook 'flymake-diagnostic-functions #'sh-shellcheck-flymake nil t) (add-hook 'hack-local-variables-hook #'sh-after-hack-local-variables nil t)) @@ -3103,6 +3107,72 @@ sh-delete-backslash (delete-region (1+ (point)) (progn (skip-chars-backward " \t") (point))))))) +;;; Flymake backend + +(defcustom sh-shellcheck-command '("shellcheck") + "The shellcheck program followed by extra arguments." + :type 'string) + +(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)))))) + (pattern "^-:\\([0-9]+\\):\\([0-9]+\\): \\([^:]+\\): \\(.*\\)$") + (sentinel + (lambda (proc _event) + (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 + :buffer (generate-new-buffer " *flymake-luacheck*") + :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 -- 2.37.3 --=-=-=--