diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 9aa5134ca0..d29139fbde 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -5137,6 +5137,89 @@ python-util-valid-regexp-p (ignore-errors (string-match regexp "") t)) +;;; Flymake integration + +(defgroup python-flymake nil + "Integration between Python and Flymake." + :group 'python + :link '(custom-group-link :tag "Flymake" flymake) + :version "26.1") + +(defcustom python-flymake-command '("pyflakes") + "The external tool that will be used to perform the syntax check." + :group 'python-flymake + :type '(repeat string)) + +;; The default regexp accomodates for older pyflakes, which did not +;; report the column number +(defcustom python-flymake-command-output-regexp + "^\\(?:.*\\.py\\|\\):\\(?1:[0-9]+\\):\\(?:\\(?2:[0-9]+\\):\\)? \\(?3:.*\\)$" + "The regexp used to parse the output of the specified tool. +It must contain two or three groups: group 1 is the line number, group 2 the +optional column number and the third is the actual message." + :group 'python-flymake + :type 'regexp) + +(defcustom python-flymake-warn-msg-regexp + "\\(^redefinition\\|.*unused.*\\|used$\\)" + "The regexp used to discriminate warnings from errors." + :group 'python-flymake + :type 'regexp) + +(defvar-local python--flymake-proc nil) + +(defun python-flymake (report-fn &rest _args) + (unless (executable-find (car python-flymake-command)) + (error "Cannot find a suitable checker")) + + (unless (derived-mode-p 'python-mode) + (error "Can only work on `python-mode' buffers")) + + (when (process-live-p python--flymake-proc) + (kill-process python--flymake-proc)) + + (let ((source (current-buffer))) + (save-restriction + (widen) + (setq python--flymake-proc + (make-process + :name "python-flymake" + :noquery t + :connection-type 'pipe + :buffer (generate-new-buffer " *python-flymake*") + :command python-flymake-command + :sentinel + (lambda (proc _event) + (unwind-protect + (when (eq proc python--flymake-proc) + (with-current-buffer (process-buffer proc) + (goto-char (point-min)) + (cl-loop + while (search-forward-regexp + python-flymake-command-output-regexp nil t) + for msg = (match-string 3) + for (beg . end) = (flymake-diag-region + source + (string-to-number (match-string 1)) + (and (match-string 2) + (string-to-number + (match-string 2)))) + for type = (if (string-match + python-flymake-warn-msg-regexp msg) + :warning :error) + collect (flymake-make-diagnostic + source beg end type msg) + into diags + finally (funcall report-fn diags)))) + (kill-buffer (process-buffer proc)))))) + (process-send-region python--flymake-proc (point-min) (point-max)) + (process-send-eof python--flymake-proc)))) + +(defun python-flymake-activate () + "Activate the Flymake syntax check on all python-mode buffers." + (add-hook 'flymake-diagnostic-functions #'python-flymake nil t)) + + (defun python-electric-pair-string-delimiter () (when (and electric-pair-mode (memq last-command-event '(?\" ?\'))