AUCTeX Sync -- New Style Files
[packages] / xemacs-packages / auctex / style / environ.el
1 ;;; environ.el --- AUCTeX style for `environ.sty' version v0.3
2
3 ;; Copyright (C) 2015, 2016 Free Software Foundation, Inc.
4
5 ;; Author: Arash Esbati <arash@gnu.org>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Created: 2015-07-04
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 `environ.sty' version v0.3 from
30 ;; 2014/05/04.  `environ.sty' is part of TeXLive.
31
32 ;; Name of new env's defined with `\NewEnviron' are automatically
33 ;; added to list of known env's, e.g.:
34 ;;
35 ;;     \NewEnviron{test}{<macro code>}
36 ;;
37 ;; `test' will be in completion list upon `C-c C-e'.
38
39 ;; More sophisticated definions must go through AUCTeX's parser, e.g.:
40 ;;
41 ;;     \NewEnviron{test}[2][]{<macro code>}
42 ;;
43 ;; After a definition like this, you have to invoke `C-c C-n' to get
44 ;; the correct completion.
45
46 ;;; Code:
47
48 (defvar LaTeX-auto-environ-NewEnviron nil
49   "Temporary for parsing the arguments of `\\NewEnviron'
50 from `environ' package.")
51
52 (defvar LaTeX-environ-NewEnviron-regexp
53   `(,(concat "\\\\\\(?:Ren\\|N\\)ewEnviron"
54              "[ \t\n\r]*{\\([A-Za-z0-9]+\\)}%?"
55              "[ \t\n\r]*\\[?\\([0-9]?\\)\\]?%?"
56              "[ \t\n\r]*\\(\\[\\)?")
57     (1 2 3) LaTeX-auto-environ-NewEnviron)
58   "Matches the argument of `\\NewEnviron' and `\\RenewEnviron'
59 from `environ.sty'.")
60
61 (defun LaTeX-environ-auto-prepare ()
62   "Clear temporary variable from `environ.sty' before parsing."
63   (setq LaTeX-auto-environ-NewEnviron nil))
64
65 (defun LaTeX-environ-auto-cleanup ()
66   "Process the parsed results of `\\NewEnviron'."
67   (dolist (env-args LaTeX-auto-environ-NewEnviron)
68     (let ((env  (car   env-args))
69           (args (cadr  env-args))
70           (opt  (nth 2 env-args)))
71       (cond (;; opt. 1st argument and mandatory argument(s)
72              (and args (not (string-equal args ""))
73                   opt  (not (string-equal opt  "")))
74              (add-to-list 'LaTeX-auto-environment
75                           (list env 'LaTeX-env-args (vector "argument")
76                                 (1- (string-to-number args)))))
77             (;; mandatory argument(s) only
78              (and args (not (string-equal args ""))
79                   (string-equal opt ""))
80              (add-to-list 'LaTeX-auto-environment
81                           (list env (string-to-number args))))
82             (t ; No args
83              (add-to-list 'LaTeX-auto-environment (list env)))))))
84
85 (add-hook 'TeX-auto-prepare-hook #'LaTeX-environ-auto-prepare t)
86 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-environ-auto-cleanup t)
87 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
88
89 (defun TeX-arg-environ-final-code (_optional)
90   "Query for the presence of optional `final code' as argument to
91 `\\NewEnviron' and insert the appropriate brackets."
92   (let ((fincode (y-or-n-p "With optional final code? ")))
93     (when fincode
94         (insert "[]"))))
95
96 (TeX-add-style-hook
97  "environ"
98  (lambda ()
99
100    ;; Add it to the parser
101    (TeX-auto-add-regexp LaTeX-environ-NewEnviron-regexp)
102
103    (TeX-add-symbols
104
105     ;; \NewEnviron{<name>}[<No.args>][<Opt.arg.>]{<Macro code>}[<Final code>]
106     '("NewEnviron"
107       (TeX-arg-define-environment "Environment")
108       [ "Number of arguments" ] [ "argument" ] t TeX-arg-environ-final-code)
109
110     '("RenewEnviron"
111       (TeX-arg-environment "Environment")
112       [ "Number of arguments" ] [ "argument" ] t TeX-arg-environ-final-code)
113
114     ;; Insert a pair of braces and we're done
115     '("environfinalcode" t)
116
117     ;; Pre-defined
118     '("BODY")
119
120     ;; Define another macro instead of \BODY
121     '("environbodyname" TeX-arg-define-macro))
122
123    ;; Fontification
124    (when (and (featurep 'font-latex)
125               (eq TeX-install-font-lock 'font-latex-setup))
126      (font-latex-add-keywords '(("NewEnviron"      "{[[{[")
127                                 ("RenewEnviron"    "{[[{[")
128                                 ("environbodyname" "|{\\"))
129                               'function)))
130   LaTeX-dialect)
131
132 (defvar LaTeX-environ-package-options nil
133   "Package options for the environ package.")
134
135 ;;; environ.el ends here