]> git.eshelyaron.com Git - emacs.git/commitdiff
(flymake-go-to-diagnostic): Add 'export-function'
authorEshel Yaron <me@eshelyaron.com>
Sun, 14 Jul 2024 08:48:18 +0000 (10:48 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sun, 14 Jul 2024 08:48:18 +0000 (10:48 +0200)
lisp/progmodes/flymake.el

index 78c5571660962b574d3a120bbe4d85127aa3a60a..10572c89eba44fb25f5c552a41939df2b4c748ef 100644 (file)
@@ -1002,7 +1002,13 @@ that project instead."
                                 (eq (get (flymake--diag-type (get-text-property 0 'diagnostic c))
                                          'flymake-category)
                                     'flymake-error))
-                             . "type=error")))))))
+                             . "type=error")))))
+                (export-function
+                 . ,(lambda (cs &rest _)
+                      (flymake-diagnostics-buffer
+                       (or project buffer)
+                       (mapcar (lambda (c) (gethash (string-to-number c) tab))
+                               cs))))))
              nil t nil t))))
     (gethash (string-to-number text) tab)))
 
@@ -1519,7 +1525,7 @@ Interactively, with a prefix arg, FORCE is t."
 (defvar flymake-mode-map
   (let ((map (make-sparse-keymap)))
     (define-key map `[,flymake-fringe-indicator-position mouse-1]
-                #'flymake-show-buffer-diagnostics)
+                #'flymake-list-diagnostics)
     map)
   "Keymap for `flymake-mode'.")
 
@@ -1803,7 +1809,7 @@ default) no filter is applied."
     [ "Go to next problem"      flymake-goto-next-error t ]
     [ "Go to previous problem"  flymake-goto-prev-error t ]
     [ "Check now"               flymake-start t ]
-    [ "List all problems"       flymake-show-buffer-diagnostics t ]
+    [ "List all problems"       flymake-list-diagnostics t ]
     "--"
     [ "Go to log buffer"        flymake-switch-to-log-buffer t ]
     [ "Turn off Flymake"        flymake-mode t ]))
@@ -1994,6 +2000,7 @@ TYPE is usually keyword `:error', `:warning' or `:note'."
 ;;; Per-buffer diagnostic listing
 
 (defvar-local flymake--diagnostics-buffer-source nil)
+(defvar-local flymake--diagnostics-buffer-diags nil)
 
 (defvar-keymap flymake-diagnostics-buffer-mode-map
   :doc "Keymap for Flyamke diagnostics list buffers."
@@ -2045,7 +2052,7 @@ POS can be a buffer position or a button"
    (flymake-diagnostics-buffer-show-diagnostic
     (if (button-type pos) (button-start pos) pos))))
 
-(defun flymake--tabulated-entries-1 (diags project-root)
+(defun flymake--tabulated-entries-1 (diags &optional project-root)
   "Helper for `flymake--diagnostics-buffer-entries'.
 PROJECT-ROOT indicates that each entry should be preceded by the
 filename of the diagnostic relative to that directory."
@@ -2124,10 +2131,13 @@ buffer."
   ;; calls us from its mode hook, when the diagnostic buffer has just
   ;; been created by 'flymake-show-buffer-diagnostics', but is not yet
   ;; set up properly (Bug#40529).
-  (when (bufferp flymake--diagnostics-buffer-source)
-    (with-current-buffer flymake--diagnostics-buffer-source
-      (when flymake-mode
-        (flymake--tabulated-entries-1 (flymake-diagnostics) nil)))))
+  (let ((diags (prog1 flymake--diagnostics-buffer-diags
+                 (setq flymake--diagnostics-buffer-diags nil))))
+    (when (bufferp flymake--diagnostics-buffer-source)
+      (with-current-buffer flymake--diagnostics-buffer-source
+        (when flymake-mode
+          (flymake--tabulated-entries-1
+           (or diags (flymake-diagnostics))))))))
 
 (defvar flymake--diagnostics-base-tabulated-list-format
   `[("Line" 5 ,(lambda (l1 l2)
@@ -2151,31 +2161,53 @@ buffer."
         'flymake--diagnostics-buffer-entries)
   (tabulated-list-init-header))
 
-(defun flymake--diagnostics-buffer-name ()
-  (format "*Flymake diagnostics for `%s'*" (current-buffer)))
+(defun flymake--diagnostics-buffer-name (&optional buffer)
+  (format "*Flymake diagnostics for %s*" (or buffer (current-buffer))))
 
-(define-obsolete-function-alias 'flymake-show-diagnostics-buffer
-  'flymake-show-buffer-diagnostics "1.2.1")
+(defun flymake-diagnostics-buffer (&optional scope diags)
+  "Return a buffer listing Flymake diagnostics DIAGS for SCOPE.
+
+SCOPE is either a buffer or a project.  If nil or omitted, it defaults
+to the current buffer.  If DIAGS is nil or omitted, it defaults to all
+diagnostics in SCOPE."
+  (if (or (bufferp scope) (null scope))
+      (flymake-buffer-diagnostics-buffer scope diags)
+    (flymake-project-diagnostics-buffer scope diags)))
+
+(defun flymake-list-diagnostics (&optional scope)
+  "Show a list of Flymake diagnostics.
+
+Optional argument SCOPE determines which diagnostics to list: if it is a
+buffer, list diagnostics for that buffer; if it a nil or omitted, list
+diagnostics for the current buffer; otherwise, SCOPE is a project, in
+this case list diagnostics across that project."
+  (interactive
+   (list (if current-prefix-arg (project-current t) (current-buffer))))
+  (display-buffer (flymake-diagnostics-buffer scope)))
 
 (defun flymake-show-buffer-diagnostics ()
-  "Show a list of Flymake diagnostics for current buffer."
+  "Show a list of Flymake diagnostics for the current buffer."
+  (interactive)
+  (flymake-list-diagnostics (current-buffer)))
+
+(defun flymake-show-project-diagnostics ()
+  "Show a list of Flymake diagnostics for the current project."
   (interactive)
-  (unless flymake-mode
-    (user-error "Flymake mode is not enabled in the current buffer"))
-  (let* ((name (flymake--diagnostics-buffer-name))
-         (source (current-buffer))
-         (target (or (get-buffer name)
-                     (with-current-buffer (get-buffer-create name)
-                       (flymake-diagnostics-buffer-mode)
-                       (current-buffer)))))
-    (with-current-buffer target
+  (flymake-list-diagnostics (project-current t)))
+
+(defun flymake-buffer-diagnostics-buffer (&optional buffer diags)
+  "Return a buffer listing Flymake diagnostics DIAGS for BUFFER.
+
+If nil or omitted, BUFFER defaults to the current buffer, and DIAGS
+defaults to all buffer diagnostics."
+  (let* ((name (flymake--diagnostics-buffer-name buffer))
+         (source (or buffer (current-buffer))))
+    (with-current-buffer (get-buffer-create name)
+      (flymake-diagnostics-buffer-mode)
       (setq flymake--diagnostics-buffer-source source)
+      (setq flymake--diagnostics-buffer-diags diags)
       (revert-buffer)
-      (display-buffer (current-buffer)
-                      `((display-buffer-reuse-window
-                         display-buffer-below-selected)
-                        (window-height . (lambda (window)
-                          (fit-window-to-buffer window 10))))))))
+      (current-buffer))))
 
 \f
 ;;; Per-project diagnostic listing
@@ -2199,7 +2231,7 @@ information about those neighboring files is acquired repeatedly
 and reliably, it may be more sensible to report them as
 \"foreign\" diagnostics instead.
 
-Commands such as `flymake-show-project-diagnostics' will include
+Commands such as `flymake-show-project-diagnostics' will include ;todo
 some of this variable's contents the diagnostic listings.")
 
 (defvar-local flymake--project-diagnostic-list-project nil)
@@ -2259,27 +2291,30 @@ some of this variable's contents the diagnostic listings.")
     (append buffer-annotated-diags relevant-foreign-diags list-only-diags)))
 
 (defun flymake--project-diagnostics-entries ()
-  (let ((p (project-current)))
-    (flymake--tabulated-entries-1 (flymake--project-diagnostics p)
-                                  (project-root p))))
+  (let ((p flymake--project-diagnostic-list-project))
+    (flymake--tabulated-entries-1
+     (or (prog1 flymake--diagnostics-buffer-diags
+           (setq flymake--diagnostics-buffer-diags nil))
+         (flymake--project-diagnostics p))
+     (project-root p))))
 
 (defun flymake--project-diagnostics-buffer (root)
   (get-buffer-create (format "*Flymake diagnostics for `%s'*" root)))
 
-(defun flymake-show-project-diagnostics ()
-  "Show a list of Flymake diagnostics for the current project."
-  (interactive)
-  (let* ((prj (project-current))
+(defun flymake-project-diagnostics-buffer (&optional project diags)
+  "Return a buffer listing Flymake diagnostics DIAGS for project PROJECT.
+
+If nil or omitted, PROJECT defaults to the current project, and DIAGS
+defaults to all project diagnostics."
+  (let* ((prj (or project (project-current)))
          (root (project-root prj))
          (buffer (flymake--project-diagnostics-buffer root)))
     (with-current-buffer buffer
       (flymake-project-diagnostics-mode)
-      (setq-local flymake--project-diagnostic-list-project prj)
+      (setq flymake--project-diagnostic-list-project prj)
+      (setq flymake--diagnostics-buffer-diags diags)
       (revert-buffer)
-      (display-buffer (current-buffer)
-                      `((display-buffer-reuse-window
-                         display-buffer-at-bottom)
-                        (window-height . fit-window-to-buffer))))))
+      (current-buffer))))
 
 (defun flymake--update-diagnostics-listings (buffer)
   "Update diagnostics listings somehow relevant to BUFFER."
@@ -2292,7 +2327,7 @@ some of this variable's contents the diagnostic listings.")
                       (project-buffers flymake--project-diagnostic-list-project)))
                 (and (eq major-mode 'flymake-diagnostics-buffer-mode)
                      (eq flymake--diagnostics-buffer-source buffer)))
-        (revert-buffer)))))
+        (unless flymake--diagnostics-buffer-diags (revert-buffer))))))
 
 (provide 'flymake)
 ;;; flymake.el ends here