Add a Flymake backend for Perl

Define a simple backend in perl-mode.el, which cperl-mode.el also
uses.

* lisp/progmodes/cperl-mode.el (cperl-mode): Add to
flymake-diagnostic-functions.

* lisp/progmodes/flymake-proc.el
(flymake-proc-allowed-file-name-masks): Disable legacy backend
for perl files.

* lisp/progmodes/perl-mode.el (perl-flymake-command): New
defcustom.
(perl--flymake-proc): New buffer-local variable.
(perl-flymake): New function.
(perl-mode): Add to flymake-diagnostic-functions.
This commit is contained in:
João Távora 2017-11-03 16:05:39 +00:00
parent 9dee764165
commit 58e742b21d
3 changed files with 74 additions and 3 deletions

View file

@ -1896,7 +1896,9 @@ or as help on variables `cperl-tips', `cperl-problems',
(if cperl-pod-here-scan
(or cperl-syntaxify-by-font-lock
(progn (or cperl-faces-init (cperl-init-faces-weak))
(cperl-find-pods-heres)))))
(cperl-find-pods-heres))))
;; Setup Flymake
(add-hook 'flymake-diagnostic-functions 'perl-flymake nil t))
;; Fix for perldb - make default reasonable
(defun cperl-db ()

View file

@ -73,7 +73,7 @@
("\\.xml\\'" flymake-proc-xml-init)
("\\.html?\\'" flymake-proc-xml-init)
("\\.cs\\'" flymake-proc-simple-make-init)
("\\.p[ml]\\'" flymake-proc-perl-init)
;; ("\\.p[ml]\\'" flymake-proc-perl-init)
("\\.php[345]?\\'" flymake-proc-php-init)
("\\.h\\'" flymake-proc-master-make-header-init flymake-proc-master-cleanup)
("\\.java\\'" flymake-proc-simple-make-java-init flymake-proc-simple-java-cleanup)

View file

@ -580,6 +580,73 @@ create a new comment."
(if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
(match-string-no-properties 1))))
;;; Flymake support
(defcustom perl-flymake-command '("perl" "-w" "-c")
"External tool used to check Perl source code.
This is a non empty list of strings, the checker tool possibly
followed by required arguments. Once launched it will receive
the Perl source to be checked as its standard input."
:group 'perl
:type '(repeat string))
(defvar-local perl--flymake-proc nil)
;;;###autoload
(defun perl-flymake (report-fn &rest _args)
"Perl backend for Flymake. Launches
`perl-flymake-command' (which see) and passes to its standard
input the contents of the current buffer. The output of this
command is analysed for error and warning messages."
(unless (executable-find (car perl-flymake-command))
(error "Cannot find a suitable checker"))
(when (process-live-p perl--flymake-proc)
(kill-process perl--flymake-proc))
(let ((source (current-buffer)))
(save-restriction
(widen)
(setq
perl--flymake-proc
(make-process
:name "perl-flymake" :noquery t :connection-type 'pipe
:buffer (generate-new-buffer " *perl-flymake*")
:command perl-flymake-command
:sentinel
(lambda (proc _event)
(when (eq 'exit (process-status proc))
(unwind-protect
(if (with-current-buffer source (eq proc perl--flymake-proc))
(with-current-buffer (process-buffer proc)
(goto-char (point-min))
(cl-loop
while (search-forward-regexp
"^\\(.+\\) at - line \\([0-9]+\\)"
nil t)
for msg = (match-string 1)
for (beg . end) = (flymake-diag-region
source
(string-to-number (match-string 2)))
for type =
(if (string-match
"\\(Scalar value\\|Useless use\\|Unquoted string\\)"
msg)
:warning
:error)
collect (flymake-make-diagnostic source
beg
end
type
msg)
into diags
finally (funcall report-fn diags)))
(flymake-log :debug "Canceling obsolete check %s"
proc))
(kill-buffer (process-buffer proc)))))))
(process-send-region perl--flymake-proc (point-min) (point-max))
(process-send-eof perl--flymake-proc))))
(defvar perl-mode-hook nil
"Normal hook to run when entering Perl mode.")
@ -665,7 +732,9 @@ Turning on Perl mode runs the normal hook `perl-mode-hook'."
;; Setup outline-minor-mode.
(setq-local outline-regexp perl-outline-regexp)
(setq-local outline-level 'perl-outline-level)
(setq-local add-log-current-defun-function #'perl-current-defun-name))
(setq-local add-log-current-defun-function #'perl-current-defun-name)
;; Setup Flymake
(add-hook 'flymake-diagnostic-functions #'perl-flymake nil t))
;; This is used by indent-for-comment
;; to decide how much to indent a comment in Perl code