summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/thmtools.el
blob: d17b30eef21def9e710eeaf1b5e1569a4b6b3310 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
;;; thmtools.el --- AUCTeX style for `thmtools.sty' (v0.72)  -*- lexical-binding: t; -*-

;; Copyright (C) 2018--2021 Free Software Foundation, Inc.

;; Author: Arash Esbati <arash@gnu.org>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2018-07-07
;; 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 AUCTeX; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301, USA.

;;; Commentary:

;; This file adds support for `thmtools.sty' (v0.72) from 2020/08/01.
;; `thmtools.sty' is part of TeXLive.

;;; Code:

;; Silence the compiler:
(declare-function font-latex-add-keywords
                  "font-latex"
                  (keywords class))

;; Needed for auto-parsing:
(require 'tex)
(require 'latex)

;; Setup for \declaretheoremstyle:
(TeX-auto-add-type "thmtools-declaretheoremstyle" "LaTeX")

(defvar LaTeX-thmtools-declaretheoremstyle-regexp
  `(,(concat "\\\\declaretheoremstyle"
             "[ \t\n\r%]*"
             "\\(?:"
             (LaTeX-extract-key-value-label 'none)
             "\\)?"
             "[ \t\n\r%]*"
             "{\\([^}]+\\)}")
    1 LaTeX-auto-thmtools-declaretheoremstyle)
  "Matches the argument of \\declaretheoremstyle from thmtools package.")

;; Setup for \declaretheorem:
(TeX-auto-add-type "thmtools-declaretheorem" "LaTeX")

(defvar LaTeX-thmtools-declaretheorem-regexp
  `(,(concat "\\\\declaretheorem"
             "[ \t\n\r%]*"
             "\\(?:"
             (LaTeX-extract-key-value-label 'none)
             "\\)?"
             "[ \t\n\r%]*"
             "{\\([^}]+\\)}")
    1 LaTeX-auto-thmtools-declaretheorem)
  "Matches the argument of \\declaretheorem from thmtools package.")

(defun LaTeX-thmtools-auto-prepare ()
  "Clear `LaTeX-auto-thmtools-*' before parsing."
  (setq LaTeX-auto-thmtools-declaretheoremstyle nil
        LaTeX-auto-thmtools-declaretheorem      nil))

(defun LaTeX-thmtools-auto-cleanup ()
  "Process parsed elements from thmtools package."
  (dolist (newthm (mapcar #'car (LaTeX-thmtools-declaretheorem-list)))
    (LaTeX-add-environments `(,newthm LaTeX-thmtools-env-label))))

(add-hook 'TeX-auto-prepare-hook #'LaTeX-thmtools-auto-prepare t)
(add-hook 'TeX-auto-cleanup-hook #'LaTeX-thmtools-auto-cleanup t)
(add-hook 'TeX-update-style-hook #'TeX-auto-parse t)

(defun LaTeX-thmtools-declaretheoremstyle-key-val (optional &optional prompt)
  "Query and return a key=val string for \\declaretheoremstyle macro.
If OPTIONAL is non-nil, indicate an optional argument in
minibuffer.  PROMPT replaces the standard one."
  (let ((lengths (mapcar (lambda (x)
                           (concat TeX-esc x))
                         (mapcar #'car (LaTeX-length-list))))
        (fonts (mapcar (lambda (x)
                         (concat TeX-esc x))
                       '("rmfamily" "sffamily" "ttfamily" "mdseries" "bfseries"
                         "upshape" "itshape" "slshape" "scshape"
                         "tiny"  "scriptsize" "footnotesize"
                         "small" "normalsize" "large"
                         "Large" "LARGE" "huge" "Huge" "normalfont"))))
    (TeX-read-key-val
     optional
     `(("spaceabove" ,lengths)
       ("spacebelow" ,lengths)
       ("headfont" ,fonts)
       ("notefont" ,fonts)
       ("bodyfont" ,fonts)
       ("headpunct")
       ("notebraces")
       ("postheadspace" ,lengths)
       ("headformat" ("margin" "swapnumber" "\\NUMBER" "\\NAME" "\\NOTE"))
       ("headindent" ,lengths))
     prompt)))

(defun LaTeX-arg-thmtools-declaretheoremstyle (optional &optional prompt)
  "Insert the key=val and style name defined by \\declaretheoremstyle.
If OPTIONAL is non-nil, also insert the second argument in square
brackets.  PROMPT replaces the standard one for the second
argument."
  (let ((TeX-arg-opening-brace "[")
        (TeX-arg-closing-brace "]"))
    (TeX-argument-insert
     (LaTeX-thmtools-declaretheoremstyle-key-val t)
     t))
  (let ((style (TeX-read-string
                (TeX-argument-prompt optional prompt "Style"))))
    (LaTeX-add-thmtools-declaretheoremstyles style)
    (TeX-argument-insert style optional)))

(defun LaTeX-thmtools-declaretheorem-key-val (optional &optional prompt)
  "Query and return a key=val string for \\declaretheorem macro.
If OPTIONAL is non-nil, indicate an optional argument in
minibuffer.  PROMPT replaces the standard one."
  (let ((counters (mapcar #'car (LaTeX-counter-list))))
    (TeX-read-key-val
     optional
     `(("parent" ,counters)
       ("numberwithin" ,counters)
       ("within" ,counters)
       ("sibling" ,counters)
       ("numberlike" ,counters)
       ("sharenumber" ,counters)
       ("title")
       ("name")
       ("heading")
       ("numbered" ("yes" "no" "unless unique"))
       ("style"
        ,(append
          ;; check for \newtheoremstyle from amsthm.sty:
          (when (and (fboundp 'LaTeX-amsthm-newtheoremstyle-list)
                     (LaTeX-amsthm-newtheoremstyle-list))
            (mapcar #'car (LaTeX-amsthm-newtheoremstyle-list)))
          ;; check for \newtheoremstyle from ntheorem.sty:
          (when (and (fboundp 'LaTeX-ntheorem-newtheoremstyle-list)
                     (LaTeX-ntheorem-newtheoremstyle-list))
            (mapcar #'car (LaTeX-ntheorem-newtheoremstyle-list)))
          ;; thmtools version is called \declaretheoremstyle:
          (mapcar #'car (LaTeX-thmtools-declaretheoremstyle-list))))
       ("preheadhook")
       ("postheadhook")
       ("prefoothook")
       ("postfoothook")
       ("refname")
       ("Refname")
       ("shaded" ("textwidth" "bgcolor" "rulecolor" "rulewidth" "margin"))
       ("thmbox" ("L" "M" "S")))
     prompt)))

(defun LaTeX-arg-thmtools-declaretheorem (optional &optional prompt)
  "Insert the key=val and environment name defined by \\declaretheorem.
If OPTIONAL is non-nil, also insert the second argument in square
brackets.  PROMPT replaces the standard one for the second
argument."
  (let ((env (TeX-read-string
              (TeX-argument-prompt optional prompt "Environment"))))
    (LaTeX-add-environments `(,env LaTeX-thmtools-env-label))
    (TeX-argument-insert env optional))
  (let ((TeX-arg-opening-brace "[")
        (TeX-arg-closing-brace "]"))
    (TeX-argument-insert
     (LaTeX-thmtools-declaretheorem-key-val t)
     t)))

(defun LaTeX-thmtools-listoftheorems-key-val (optional &optional prompt)
  "Query and return a key=val string for \\listoftheorems macro.
If OPTIONAL is non-nil, indicate an optional argument in
minibuffer.  PROMPT replaces the standard one."
  (let ((lengths (mapcar (lambda (x)
                           (concat TeX-esc x))
                         (mapcar #'car (LaTeX-length-list))))
        (thms (append
               ;; check for \newtheorem from amsthm.sty:
               (when (and (fboundp 'LaTeX-amsthm-newtheorem-list)
                          (LaTeX-amsthm-newtheorem-list))
                 (mapcar #'car (LaTeX-amsthm-newtheorem-list)))
               ;; check for \newtheorem from ntheorem.sty:
               (when (and (fboundp 'LaTeX-ntheorem-newtheorem-list)
                          (LaTeX-ntheorem-newtheorem-list))
                 (mapcar #'car (LaTeX-ntheorem-newtheorem-list)))
               ;; thmtools version is called \declaretheorem:
               (mapcar #'car (LaTeX-thmtools-declaretheorem-list)))))
    (TeX-read-key-val
     optional
     `(("title")
       ("ignore" ,thms)
       ("ignoreall" ("true" "false"))
       ("show" ,thms)
       ("showall" ("true" "false"))
       ("onlynamed" ,thms)
       ("swapnumber" ("true" "false"))
       ("numwidth" ,lengths))
     prompt)))

(defun LaTeX-arg-thmtools-listoftheorems (optional &optional prompt)
  "Insert the key=val to \\listoftheorems macro.
If OPTIONAL is non-nil, insert the result square brackets.
OPTIONAL and PROMPT are passed to `LaTeX-thmtools-listoftheorems-key-val'."
  (TeX-argument-insert
   (LaTeX-thmtools-listoftheorems-key-val optional prompt)
   optional))

(defun LaTeX-thmtools-env-label (environment)
  "Insert thmtools ENVIRONMENT, query for an optional argument and label.
AUCTeX users should add ENVIRONMENT to `LaTeX-label-alist' via
customize or in init-file with:

  (add-to-list \\='LaTeX-label-alist \\='(\"theorem\" . \"thm:\"))

RefTeX users should customize or add ENVIRONMENT to
`LaTeX-label-alist' and `reftex-label-alist', for example

  (add-to-list \\='LaTeX-label-alist \\='(\"theorem\" . \"thm:\"))
  (add-to-list \\='reftex-label-alist
               \\='(\"theorem\" ?m \"thm:\" \"~\\ref{%s}\"
                 nil (\"Theorem\" \"theorem\") nil))"
  (let* ((help-form "\
Select the content of the optional argument with a key:
'h' in order to insert a plain heading,
'k' in order to insert key=value pairs with completion,
RET in order to leave it empty.")
         (choice (read-char-choice
                  (TeX-argument-prompt
                   nil nil "Heading (h), Key=val (k), Empty (RET)")
                  '(?h ?k)))
         (opthead (cond ((= choice ?h)
                         (TeX-read-string
                          (TeX-argument-prompt t nil "Heading")))
                        ((= choice ?k)
                         (TeX-read-key-val
                          t
                          `(("name")
                            ("continues" ,(mapcar #'car (LaTeX-label-list)))
                            ("restate" ,(mapcar #'car (LaTeX-label-list)))
                            ;; We don't offer a label key here: It is
                            ;; marked "experimental" in the manual and
                            ;; inserting and parsing \label{foo} is
                            ;; much easier for AUCTeX and RefTeX
                            ;; ("label")
                            ("listhack" ("true" "false")))))
                        (t ""))))
    (LaTeX-insert-environment environment
                              (when (and opthead
                                         (not (string= opthead "")))
                                (format "[%s]" opthead))))
  (when (LaTeX-label environment 'environment)
    (LaTeX-newline)
    (indent-according-to-mode)))

(TeX-add-style-hook
 "thmtools"
 (lambda ()

   ;; Add thmtools to the parser.
   (TeX-auto-add-regexp LaTeX-thmtools-declaretheoremstyle-regexp)
   (TeX-auto-add-regexp LaTeX-thmtools-declaretheorem-regexp)

   (TeX-add-symbols
    '("declaretheoremstyle" LaTeX-arg-thmtools-declaretheoremstyle)
    '("declaretheorem" LaTeX-arg-thmtools-declaretheorem)

    '("listoftheorems"  [ LaTeX-arg-thmtools-listoftheorems ])
    '("ignoretheorems"
      (TeX-arg-eval mapconcat #'identity
                    (TeX-completing-read-multiple
                     (TeX-argument-prompt nil nil "Environment(s)")
                     (append
                      ;; check for \newtheorem from amsthm.sty:
                      (when (and (fboundp 'LaTeX-amsthm-newtheorem-list)
                                 (LaTeX-amsthm-newtheorem-list))
                        (mapcar #'car (LaTeX-amsthm-newtheorem-list)))
                      ;; check for \newtheorem from ntheorem.sty:
                      (when (and (fboundp 'LaTeX-ntheorem-newtheorem-list)
                                 (LaTeX-ntheorem-newtheorem-list))
                        (mapcar #'car (LaTeX-ntheorem-newtheorem-list)))
                      ;; thmtools version is called \declaretheorem:
                      (mapcar #'car (LaTeX-thmtools-declaretheorem-list))))
                    ","))
    '("listtheoremname" 0))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("declaretheoremstyle"  "[{")
                                ("declaretheorem"       "[{[")
                                ("listoftheorems"       "[")
                                ("ignoretheorems"       "{"))
                              'function)))
 TeX-dialect)

;; The package has only one option `debug'.  We ignore that in order
;; to make loading faster:
(defvar LaTeX-thmtools-package-options nil
  "Package options for the thmtools package.")

;;; thmtools.el ends here