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: Sat, 07 Oct 2017 16:40:11 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.60 (gnu/linux)

Eli Zaretskii <address@hidden> writes:

>> From: Stefan Monnier <address@hidden>
>> Date: Sat, 07 Oct 2017 09:28:04 -0400
>> 
>> > Yes, that is the natural way. But should it go into the emacs-26 branch?
>> 
>> I'll let Eli&John decide.
>
> If it's just a Flymake back-end, and doesn't affect anything else
> unless activated, I see no reason not to have it on the release
> branch, assuming that its support in the version of Flymake on the
> release branch is reasonably good.

Great, I'll do my best to get there: current version of python-flymake,
ameliorated with Noam's suggestions, works pretty well, and I'm right now
trying it with customized settings so that it uses an alternative checker
(flake8).

However, I have a problem when using the new implementation in my real
development context, that wasn't present before (before João work I was using
python-flymake-pyflakes from ELPA).

I use the `desktop' functionality, to keep a persistent state of open files,
and in one project the `emacs.desktop' file has 367 entries, of which 284 are
python-mode buffers: now when I open that project the python-flymake backend
gets disabled with the following errors, for each python-mode buffer:

  Warning [flymake xxx.py]: Disabling backend python-flymake because 
(file-error Creating pipe Troppi file aperti)
  Warning [flymake xxx.py]: Disabling backend flymake-proc-legacy-flymake 
because (error Can find a suitable init function)

where "Troppi file aperti" means "Too many open files". The mode-line of all
those buffers tells that Flymake is in "Wait" status.

I wonder if the problem is in the backend or rather in the new Flymake
mechanism. Also, I will try to understand whether there could be a way to tell
Flymake to run the checker only after the buffer has been modified, in other
words to *not* run it on newly open files.

Any hint on how to investigate the issue?

I'm attaching below the current backend I'm trying out.

Thanks&bye, lele.

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 9aa5134ca0..7328170a9b 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -5137,6 +5137,106 @@ 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
+  "^\\(?:<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-msg-alist
+  '(("\\(^redefinition\\|.*unused.*\\|used$\\)" . :warning))
+  "Alist used to associate messages to their types.
+Each element should be a cons-cell (REGEXP . TYPE), where TYPE must be
+one defined in the variable `flymake-diagnostic-types-alist'."
+  :group 'python-flymake
+  :type `(alist :key-type (regexp)
+                :value-type (symbol)))
+                ;; :value-type (choice
+                ;;              (mapcar (lambda (type)
+                ;;                        (list 'const
+                ;;                              ':tag
+                ;;                              (capitalize
+                ;;                               (substring
+                ;;                                (symbol-name (car type))
+                ;;                                1 nil))
+                ;;                              (car type)))
+                ;;                      flymake-diagnostic-types-alist))))
+                ;; :value-type (choice (const :tag "Error" :error)
+                ;;                     (const :tag "Warning" :warning)
+                ;;                     (const :tag "Note" :note))))
+
+(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 = (or (assoc-default msg
+                                                      python-flymake-msg-alist
+                                                      #'string-match)
+                                       :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]