Subject: bug#30423: 2018-01-25; [patch] flymake support

From: Alex Branham <[email-masked]>
Date: Sun, 11 Feb 2018 12:06:56 -0600

Flymake got a major rewrite in Emacs 26, the attached patch adds support for this in latex.el. Users can enable it by calling adding flymake-mode to LaTeX-mode-hook.

>From 0b8eeebd01d0ed25823d4f40a7ab3cff0d150b8f Mon Sep 17 00:00:00 2001
From: Alex Branham <[email-masked]>
Date: Sun, 11 Feb 2018 11:54:09 -0600
Subject: [PATCH] Support flymake in Emacs 26+ using chktex

---
 latex.el | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 72 insertions(+), 1 deletion(-)

diff --git a/latex.el b/latex.el
index c2d8a4ca..e3792faf 100644
--- a/latex.el
+++ b/latex.el
@@ -1,4 +1,4 @@
-;;; latex.el --- Support for LaTeX documents.
+;;; latex.el --- Support for LaTeX documents.  -*- lexical-binding: t; -*-
 
 ;; Copyright (C) 1991, 1993-2017 Free Software Foundation, Inc.
 
@@ -6568,6 +6568,77 @@ function would return non-nil and `(match-string 1)' would return
 		     (1+ any-col)
 		   beg-col))))))))
 
+;; Flymake
+
+(defvar-local LaTeX--flymake-proc nil)
+
+(defun LaTeX-flymake (report-fn &rest _args)
+  "Setup flymake integration.
+
+REPORT-FN is flymake's callback function."
+  (unless (executable-find "chktex")
+    (error "Cannot find chktex"))
+  (when (process-live-p LaTeX--flymake-proc)
+    (kill-process LaTeX--flymake-proc))
+  (let ((source (current-buffer)))
+    (save-restriction
+      (widen)
+      (setq
+       LaTeX--flymake-proc
+       (make-process
+        :name "LaTeX-flymake" :noquery t :connection-type 'pipe
+        :buffer (generate-new-buffer " *LaTeX-flymake*")
+        :command '("chktex" "--verbosity=0" "--quiet" "--inputfiles")
+        :sentinel
+        (lambda (proc _event)
+          (when (eq 'exit (process-status proc))
+            (unwind-protect
+                (if (with-current-buffer source (eq proc LaTeX--flymake-proc))
+                    (with-current-buffer (process-buffer proc)
+                      (goto-char (point-min))
+                      (cl-loop
+                       while (search-forward-regexp
+                              (rx line-start "stdin:"
+                                  ;; line
+                                  (group-n 1 (one-or-more num))
+                                  ":"
+                                  ;; column
+                                  (group-n 2 (one-or-more num))
+                                  ":"
+                                  ;; This is information about the
+                                  ;; number of the warning, which we
+                                  ;; probably don't care about:
+                                  (one-or-more num)
+                                  ":"
+                                  ;; Warning message:
+                                  (group-n 3 (one-or-more not-newline)) line-end)
+                              nil t)
+                       for msg = (match-string 3)
+                       for (beg . end) = (flymake-diag-region
+                                          source
+                                          (string-to-number (match-string 1))
+                                          (string-to-number (match-string 2)))
+                       for type = :warning
+                       collect (flymake-make-diagnostic source
+                                                        beg
+                                                        end
+                                                        type
+                                                        msg)
+                       into diags
+                       finally (funcall report-fn diags)))
+                  (flymake-log :warning "Canceling obsolete check %s"
+                               proc))
+              (kill-buffer (process-buffer proc)))))))
+      (process-send-region LaTeX--flymake-proc (point-min) (point-max))
+      (process-send-eof LaTeX--flymake-proc))))
+
+(defun LaTeX-setup-flymake-backend ()
+  "Setup flymake backend for LaTeX."
+  (add-hook 'flymake-diagnostic-functions 'LaTeX-flymake nil t))
+
+(when (< 25 emacs-major-version)
+  (add-hook 'LaTeX-mode-hook 'LaTeX-setup-flymake-backend))
+
 (provide 'latex)
 
 ;;; latex.el ends here
-- 
2.16.1

>From 0b8eeebd01d0ed25823d4f40a7ab3cff0d150b8f Mon Sep 17 00:00:00 2001
From: Alex Branham <[email-masked]>
Date: Sun, 11 Feb 2018 11:54:09 -0600
Subject: [PATCH] Support flymake in Emacs 26+ using chktex

---
 latex.el | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 72 insertions(+), 1 deletion(-)

diff --git a/latex.el b/latex.el
index c2d8a4ca..e3792faf 100644
--- a/latex.el
+++ b/latex.el
@@ -1,4 +1,4 @@
-;;; latex.el --- Support for LaTeX documents.
+;;; latex.el --- Support for LaTeX documents.  -*- lexical-binding: t; -*-
 
 ;; Copyright (C) 1991, 1993-2017 Free Software Foundation, Inc.
 
@@ -6568,6 +6568,77 @@ function would return non-nil and `(match-string 1)' would return
 		     (1+ any-col)
 		   beg-col))))))))
 
+;; Flymake
+
+(defvar-local LaTeX--flymake-proc nil)
+
+(defun LaTeX-flymake (report-fn &rest _args)
+  "Setup flymake integration.
+
+REPORT-FN is flymake's callback function."
+  (unless (executable-find "chktex")
+    (error "Cannot find chktex"))
+  (when (process-live-p LaTeX--flymake-proc)
+    (kill-process LaTeX--flymake-proc))
+  (let ((source (current-buffer)))
+    (save-restriction
+      (widen)
+      (setq
+       LaTeX--flymake-proc
+       (make-process
+        :name "LaTeX-flymake" :noquery t :connection-type 'pipe
+        :buffer (generate-new-buffer " *LaTeX-flymake*")
+        :command '("chktex" "--verbosity=0" "--quiet" "--inputfiles")
+        :sentinel
+        (lambda (proc _event)
+          (when (eq 'exit (process-status proc))
+            (unwind-protect
+                (if (with-current-buffer source (eq proc LaTeX--flymake-proc))
+                    (with-current-buffer (process-buffer proc)
+                      (goto-char (point-min))
+                      (cl-loop
+                       while (search-forward-regexp
+                              (rx line-start "stdin:"
+                                  ;; line
+                                  (group-n 1 (one-or-more num))
+                                  ":"
+                                  ;; column
+                                  (group-n 2 (one-or-more num))
+                                  ":"
+                                  ;; This is information about the
+                                  ;; number of the warning, which we
+                                  ;; probably don't care about:
+                                  (one-or-more num)
+                                  ":"
+                                  ;; Warning message:
+                                  (group-n 3 (one-or-more not-newline)) line-end)
+                              nil t)
+                       for msg = (match-string 3)
+                       for (beg . end) = (flymake-diag-region
+                                          source
+                                          (string-to-number (match-string 1))
+                                          (string-to-number (match-string 2)))
+                       for type = :warning
+                       collect (flymake-make-diagnostic source
+                                                        beg
+                                                        end
+                                                        type
+                                                        msg)
+                       into diags
+                       finally (funcall report-fn diags)))
+                  (flymake-log :warning "Canceling obsolete check %s"
+                               proc))
+              (kill-buffer (process-buffer proc)))))))
+      (process-send-region LaTeX--flymake-proc (point-min) (point-max))
+      (process-send-eof LaTeX--flymake-proc))))
+
+(defun LaTeX-setup-flymake-backend ()
+  "Setup flymake backend for LaTeX."
+  (add-hook 'flymake-diagnostic-functions 'LaTeX-flymake nil t))
+
+(when (< 25 emacs-major-version)
+  (add-hook 'LaTeX-mode-hook 'LaTeX-setup-flymake-backend))
+
 (provide 'latex)
 
 ;;; latex.el ends here
-- 
2.16.1