emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Flymake refactored


From: Lele Gaifax
Subject: Re: Flymake refactored
Date: Fri, 06 Oct 2017 09:09:46 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.60 (gnu/linux)

Stefan Monnier <address@hidden> writes:

> I think the Python backend would naturally belong in python.el

I'm attaching what I'm currently testing out: how should I proceed to
contribute it?

Thanks a lot,
ciao, lele.

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\\|<stdin>\\):\\(?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 '(?\" ?\'))
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
address@hidden  |                 -- Fortunato Depero, 1929.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]