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