EasyPG 1.07 Released
[packages] / xemacs-packages / auctex / style / amsthm.el
1 ;;; amsthm.el --- Style hook for the AMS-LaTeX amsthm package.
2
3 ;; Copyright (C) 1997, 2013--2015 Free Software Foundation, Inc.
4
5 ;; Author: Carsten Dominik <dominik@strw.leidenuniv.nl>
6 ;; Maintainer: auctex-devel@gnu.org
7
8 ;; This file is part of AUCTeX.
9
10 ;; AUCTeX is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; AUCTeX is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
22 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 ;; 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; The style provides the function `LaTeX-amsthm-env-label' which
28 ;; enables new defined environments with "\newtheoreom" to interact
29 ;; with AUCTeX and RefTeX mechanisms for inserting labels.  Check
30 ;; docstring of `LaTeX-amsthm-env-label' for instructions.
31
32 ;;; Code:
33
34 (defvar LaTeX-amsthm-package-options nil
35   "Package options for the amsthm package.")
36
37 (defvar LaTeX-amsthm-theoremstyle-list
38   '(("plain") ("definition") ("remark"))
39   "List of theorem styles provided by `amsthm.el' and new ones
40 defined with \"\\newtheoremstyle\".")
41
42 (defvar LaTeX-amsthm-fontdecl
43   '(;; family
44     "rmfamily" "sffamily" "ttfamily"
45     ;; series
46     "mdseries" "bfseries"
47     ;; shape
48     "upshape" "itshape" "slshape" "scshape"
49     ;; size
50     "tiny"  "scriptsize" "footnotesize"
51     "small" "normalsize" "large"
52     "Large" "LARGE" "huge" "Huge"
53     ;; reset macro
54     "normalfont")
55   "List of font declaration commands for \"\\newtheoremstyle\".")
56
57 (defun LaTeX-arg-amsthm-fontdecl (optional &optional prompt)
58   "Prompt for font declaration commands in \"\\newtheoremstyle\".
59 If OPTIONAL is non-nil, insert the resulting value as an optional
60 argument.  Use PROMPT as the prompt string."
61   ;; `INITIAL-INPUT' (5th argument to `TeX-completing-read-multiple')
62   ;; is hard-coded to `TeX-esc'.
63   (let* ((crm-separator (regexp-quote TeX-esc))
64          (fontdecl (mapconcat 'identity
65                               (TeX-completing-read-multiple
66                                (TeX-argument-prompt optional prompt "Font")
67                                LaTeX-amsthm-fontdecl nil nil TeX-esc)
68                               TeX-esc)))
69     (TeX-argument-insert fontdecl optional)))
70
71 (defun LaTeX-amsthm-env-label (environment)
72   "Insert ENVIRONMENT, query for an optional argument and prompt
73 for label.  AUCTeX users should add ENVIRONMENT to
74 `LaTeX-label-alist' via customize or in init-file with:
75
76   (add-to-list \\='LaTeX-label-alist \\='(\"lemma\" . \"lem:\"))
77
78 RefTeX users should customize or add ENVIRONMENT to
79 `LaTeX-label-alist' and `reftex-label-alist', e.g.
80
81   (add-to-list \\='LaTeX-label-alist \\='(\"lemma\" . \"lem:\"))
82   (add-to-list \\='reftex-label-alist
83                \\='(\"lemma\" ?m \"lem:\" \"~\\ref{%s}\"
84                  nil (\"Lemma\" \"lemma\") nil))"
85   (let ((opthead (TeX-read-string
86                   (TeX-argument-prompt t nil "Heading"))))
87     (LaTeX-insert-environment environment
88                               (when (and opthead
89                                          (not (string= opthead "")))
90                                 (format "[%s]" opthead))))
91   (when (LaTeX-label environment 'environment)
92     (LaTeX-newline)
93     (indent-according-to-mode)))
94
95 ;; Needed for auto-parsing
96 (require 'tex)
97
98 ;; Setup parsing for \newtheorem
99 (TeX-auto-add-type "amsthm-newtheorem" "LaTeX")
100
101 ;; Setup parsing for \newtheoremstyle
102 (TeX-auto-add-type "amsthm-newtheoremstyle" "LaTeX")
103
104 (defun LaTeX-amsthm-auto-prepare ()
105   "Clear `LaTeX-auto-amsthm-newtheorem' and
106 `LaTeX-auto-amsthm-newtheoremstyle' before parsing."
107   (setq LaTeX-auto-amsthm-newtheorem nil)
108   (setq LaTeX-auto-amsthm-newtheoremstyle nil))
109
110 (defun LaTeX-amsthm-auto-cleanup ()
111   "Move parsed results from `LaTeX-auto-amsthm-newtheorem' and
112 make them available as new environments.  Update
113 `LaTeX-amsthm-theoremstyle-list' with styles defined with
114 \"\\newtheoremstyle\"."
115   (dolist (newthm (mapcar 'car (LaTeX-amsthm-newtheorem-list)))
116     (LaTeX-add-environments (list newthm 'LaTeX-amsthm-env-label)))
117   (dolist (newthmstyle (LaTeX-amsthm-newtheoremstyle-list))
118     (add-to-list (make-local-variable 'LaTeX-amsthm-theoremstyle-list)
119                  newthmstyle)))
120
121 (add-hook 'TeX-auto-prepare-hook #'LaTeX-amsthm-auto-prepare t)
122 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-amsthm-auto-cleanup t)
123 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
124
125 (TeX-add-style-hook
126  "amsthm"
127  (lambda ()
128    (LaTeX-add-environments
129     '("proof" LaTeX-amsthm-env-label))
130    (TeX-add-symbols
131     ;; Overrule the defintion in `latex.el':
132     '("newtheorem"
133       (TeX-arg-eval
134        (lambda ()
135          (let ((nthm (TeX-read-string
136                       (TeX-argument-prompt nil nil "Environment"))))
137            (LaTeX-add-amsthm-newtheorems nthm)
138            (LaTeX-add-environments (list nthm 'LaTeX-amsthm-env-label))
139            (format "%s" nthm))))
140       [ TeX-arg-environment "Numbered like" ]
141       t [ (TeX-arg-eval progn (if (eq (save-excursion
142                                         (backward-char 2)
143                                         (preceding-char)) ?\])
144                                   ()
145                                 (TeX-arg-counter t "Within counter"))
146                         "") ])
147
148     '("newtheorem*"
149       (TeX-arg-eval
150        (lambda ()
151          (let ((nthm (TeX-read-string
152                       (TeX-argument-prompt nil nil "Environment")))
153                (heading (TeX-read-string
154                          (TeX-argument-prompt nil nil "Heading"))))
155            (LaTeX-add-amsthm-newtheorems nthm)
156            (LaTeX-add-environments (list nthm 'LaTeX-amsthm-env-label))
157            (insert (concat TeX-grop nthm TeX-grcl))
158            (format "%s" heading)))))
159
160     '("theoremstyle"
161       (TeX-arg-eval completing-read "Style: "
162                     LaTeX-amsthm-theoremstyle-list))
163     "qedhere"
164     "swapnumbers"
165
166     '("newtheoremstyle"
167       (TeX-arg-eval
168        (lambda ()
169          (let ((nthmstyle (TeX-read-string
170                            (TeX-argument-prompt nil nil "Style name"))))
171            (LaTeX-add-amsthm-newtheoremstyles nthmstyle)
172            (add-to-list (make-local-variable 'LaTeX-amsthm-theoremstyle-list)
173                         (list nthmstyle))
174            (format "%s" nthmstyle))))
175       (TeX-arg-length "Space above")
176       (TeX-arg-length "Space below")
177       (LaTeX-arg-amsthm-fontdecl "Body font")
178       "Indent amount"
179       (LaTeX-arg-amsthm-fontdecl "Theorem head font")
180       "Punctuation after head"
181       (TeX-arg-length "Space after head")
182       "Theorem head spec"))
183
184    (TeX-auto-add-regexp
185     `(,(concat "\\\\newtheorem\\*?{\\(" TeX-token-char "+\\)}")
186       1 LaTeX-auto-amsthm-newtheorem))
187    (TeX-auto-add-regexp
188     `(,(concat "\\\\newtheoremstyle{\\(" TeX-token-char "+\\)}")
189       1 LaTeX-auto-amsthm-newtheoremstyle))
190
191    ;; Fontification
192    (when (and (featurep 'font-latex)
193               (eq TeX-install-font-lock 'font-latex-setup))
194      (font-latex-add-keywords '(("newtheorem"      "*{[{[")
195                                 ("theoremstyle"    "{")
196                                 ("newtheoremstyle" "{{{{{{{{{"))
197                               'function)))
198  LaTeX-dialect)
199
200 ;;; amsthm.el ends here