-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
latex-flymake.el
119 lines (100 loc) · 4.68 KB
/
latex-flymake.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
;;; latex-flymake.el --- Flymake integration -*- lexical-binding: t; -*-
;; Copyright (C), 2018--2024 Free Software Foundation, Inc.
;; Author: Alex Branham <[email protected]>
;; Maintainer: [email protected]
;; Created: 2018-02-11
;; Keywords: tex
;; This file is part of AUCTeX.
;; AUCTeX is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the
;; Free Software Foundation; either version 3, or (at your option) any
;; later version.
;; AUCTeX is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
;; for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file provides flymake integration for latex documents using
;; "chktex" as a backend. You must be running Emacs 26 or newer.
;; Enable it by adding the following to your init file:
;; (add-hook 'LaTeX-mode-hook #'flymake-mode)
;;; Code:
(require 'cl-lib)
(require 'flymake)
(defvar-local LaTeX--flymake-proc nil)
(defcustom LaTeX-flymake-chktex-options nil
"A list of strings passed as options to the chktex backend.
You can use this to enable or disable specific warnings by setting it to
something like:
\\='(\"-n12\" \"-w41\")
Which would disable warning 12 (\"interword spacing should perhaps be
used\") and enable 41 (\"you ought not to use primitive TeX in LaTeX
code\").
See the chktex manual for warning numbers and details about how to use
flags."
:type '(choice (const :tag "Use chktex defaults" nil)
(repeat :tag "Custom chktex options" string))
:group 'LaTeX)
(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"
,@LaTeX-flymake-chktex-options)
: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))))
(provide 'latex-flymake)
;;; latex-flymake.el ends here