"Fossies" - the Fresh Open Source Software Archive

Member "auctex-12.3/style/empheq.el" (18 Oct 2020, 17536 Bytes) of package /linux/misc/auctex-12.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lisp source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "empheq.el": 12.2_vs_12.3.

    1 ;;; empheq.el --- AUCTeX style for `empheq.sty' (v2.14)
    2 
    3 ;; Copyright (C) 2016-2020 Free Software Foundation, Inc.
    4 
    5 ;; Author: Arash Esbati <arash@gnu.org>
    6 ;; Maintainer: auctex-devel@gnu.org
    7 ;; Created: 2016-08-07
    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 `empheq.sty' (v2.14) from 2014/08/04.
   30 ;; `empheq.sty' is part of TeXLive.
   31 
   32 ;;; Code:
   33 
   34 (eval-when-compile
   35   (require 'cl-lib))
   36 
   37 ;; Needed for auto-parsing:
   38 (require 'tex)
   39 
   40 ;; Silence the compiler:
   41 (declare-function font-latex-add-keywords
   42           "font-latex"
   43           (keywords class))
   44 
   45 (declare-function LaTeX-item-equation-alignat
   46           "amsmath" (&optional suppress))
   47 
   48 (defvar LaTeX-mathtools-package-options)
   49 
   50 (defvar LaTeX-empheq-key-val-options
   51   `(("box")
   52     ("innerbox")
   53     ("left" ,(mapcar
   54           (lambda (x)
   55         (concat TeX-esc x))
   56           '("empheqlbrace"
   57         "empheqlbrack"
   58         "empheqlangle"
   59         "empheqlparen"
   60         "empheqlvert"
   61         "empheqlVert"
   62         "empheqlfloor"
   63         "empheqlceil"
   64         "empheqbiglbrace"
   65         "empheqbiglbrack"
   66         "empheqbiglangle"
   67         "empheqbiglparen"
   68         "empheqbiglvert"
   69         "empheqbiglVert"
   70         "empheqbiglfloor"
   71         "empheqbiglceil")))
   72     ("right" ,(mapcar
   73            (lambda (x)
   74          (concat TeX-esc x))
   75            '("empheqrbrace"
   76          "empheqrbrack"
   77          "empheqrangle"
   78          "empheqrparen"
   79          "empheqrvert"
   80          "empheqrVert"
   81          "empheqrfloor"
   82          "empheqrceil"
   83          "empheqbigrbrace"
   84          "empheqbigrbrack"
   85          "empheqbigrangle"
   86          "empheqbigrparen"
   87          "empheqbigrvert"
   88          "empheqbigrVert"
   89          "empheqbigrfloor"
   90          "empheqbigrceil")))
   91     ("outerbox")
   92     ("marginbox"))
   93   "Key=value options for environments from empheq.sty.")
   94 
   95 (defvar LaTeX-empheq-key-val-options-local nil
   96   "Buffer-local key=value options for environments from empheq.sty.")
   97 (make-local-variable 'LaTeX-empheq-key-val-options-local)
   98 
   99 (defvar LaTeX-empheq-supported-amsmath-envs
  100   '("equation"  "equation*"
  101     "align"     "align*"
  102     "gather"    "gather*"
  103     "flalign"   "flalign*"
  104     "alignat"   "alignat*"
  105     "multline"  "multline*")
  106   "List of amsmath environments supported by empheq package.")
  107 
  108 (defvar LaTeX-empheq-package-options
  109   '("overload" "overload2" "ntheorem" "newmultline" "oldmultline")
  110   "Package options for the empheq package.")
  111 (TeX-load-style "mathtools")
  112 ;; Add elements from `LaTeX-mathtools-package-options' only once
  113 ;; and not every time the style hook runs
  114 (dolist (elt LaTeX-mathtools-package-options)
  115   (add-to-list 'LaTeX-empheq-package-options elt))
  116 
  117 ;; Setup for \Declare(Left|Right)Delimiter:
  118 
  119 (TeX-auto-add-type "empheq-declaredelimiter" "LaTeX")
  120 
  121 (defvar LaTeX-empheq-declaredelimiter-regexp
  122   `(,(concat "\\\\Declare\\(Left\\|Right\\)Delimiter"
  123          "[ \t\n\r%]*"
  124          "\\(?:\\[[^]]*\\]\\)?"
  125          "[ \t\n\r%]*"
  126          "{"
  127          (regexp-quote TeX-esc)
  128          "\\([^}]+\\)}")
  129     (2 1) LaTeX-auto-empheq-declaredelimiter)
  130   "Matches the argument of \\Declare(Left|Right)Delimiter from empheq package.")
  131 
  132 (defun LaTeX-empheq-auto-prepare ()
  133   "Clear `LaTeX-auto-empheq-declaredelimiter' before parsing."
  134   (setq LaTeX-auto-empheq-declaredelimiter nil))
  135 
  136 (defun LaTeX-empheq-auto-cleanup ()
  137   "Process parsed delimiters."
  138   (dolist (delim (mapcar #'car (LaTeX-empheq-declaredelimiter-list)))
  139     (TeX-add-symbols (concat "empheq" delim)
  140              (concat "empheqbig" delim)))
  141   (LaTeX-empheq-update-key-val-options))
  142 
  143 (add-hook 'TeX-auto-prepare-hook #'LaTeX-empheq-auto-prepare t)
  144 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-empheq-auto-cleanup t)
  145 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
  146 
  147 (defun LaTeX-empheq-update-key-val-options ()
  148   "Update `LaTeX-empheq-key-val-options-local' if the function
  149 `LaTeX-empheq-declaredelimiter-list' returns non-nil."
  150   (when (LaTeX-empheq-declaredelimiter-list)
  151     (let ((lvals (cadr (assoc "left" LaTeX-empheq-key-val-options)))
  152       (rvals (cadr (assoc "right" LaTeX-empheq-key-val-options)))
  153       (tmp (copy-alist LaTeX-empheq-key-val-options))
  154       lval rval)
  155       (dolist (delims (LaTeX-empheq-declaredelimiter-list))
  156     (let ((delim (car delims))
  157           (where (cadr delims)))
  158       (if (string= where "Left")
  159           (progn
  160         (cl-pushnew (concat TeX-esc "empheq" delim) lval :test #'equal)
  161         (cl-pushnew (concat TeX-esc "empheqbig" delim) lval :test #'equal))
  162         (progn
  163           (cl-pushnew (concat TeX-esc "empheq" delim) rval :test #'equal)
  164           (cl-pushnew (concat TeX-esc "empheqbig" delim) rval :test #'equal)))))
  165       (when lval
  166     (setq tmp (assq-delete-all (car (assoc "left" tmp)) tmp))
  167     (setq lvals (append lval lvals))
  168     (push (list "left" lvals) tmp))
  169       (when rval
  170     (setq tmp (assq-delete-all (car (assoc "right" tmp)) tmp))
  171     (setq rvals (append rval rvals))
  172     (push (list "right" rvals) tmp))
  173       (setq LaTeX-empheq-key-val-options-local
  174         (copy-alist tmp)))))
  175 
  176 (defun LaTeX-empheq-env (env)
  177   "Query for a supported amsmath environment and insert it accordingly."
  178   (let* ((keyvals (TeX-read-key-val t LaTeX-empheq-key-val-options-local))
  179      (amsenv (completing-read
  180           (TeX-argument-prompt nil nil "amsmath environment")
  181           LaTeX-empheq-supported-amsmath-envs))
  182      (ncols (when (or (string= amsenv "alignat")
  183               (string= amsenv "alignat*"))
  184           (TeX-read-string
  185            (TeX-argument-prompt nil nil "Number of columns"))))
  186      num)
  187     (LaTeX-insert-environment
  188      env
  189      (concat
  190       (when (and keyvals (not (string= keyvals "")))
  191     (concat LaTeX-optop keyvals LaTeX-optcl))
  192       TeX-grop
  193       (if (and ncols (not (string= ncols "")))
  194       (concat amsenv "=" ncols)
  195     (symbol-value 'amsenv))
  196       TeX-grcl))
  197     (when (and (assoc amsenv LaTeX-label-alist)
  198            (LaTeX-label amsenv 'environment))
  199       (LaTeX-newline)
  200       (indent-according-to-mode))
  201     (when (and ncols (not (string= ncols "")))
  202       (setq num (string-to-number ncols))
  203       (save-excursion
  204     (insert (make-string (+ num num -1) ?&))))))
  205 
  206 (defun LaTeX-empheq-env-overload (env &optional _ignore)
  207   "Insert amsmath ENV's when option overload is given to empheq package.
  208 This function combines the capabilities of `LaTeX-env-label' and
  209 `LaTeX-amsmath-env-alignat'.  It overwrites the definitions of
  210 `amsmath.el'."
  211   (if (or (string= env "alignat")
  212       (string= env "alignat*"))
  213       (let ((ncols (TeX-read-string
  214             (TeX-argument-prompt nil nil "Number of columns")))
  215         (keyvals (TeX-read-key-val t
  216                        LaTeX-empheq-key-val-options-local
  217                        "empheq options (k=v)")))
  218     (LaTeX-insert-environment env (concat TeX-grop ncols TeX-grcl
  219                           (when (and keyvals (not (string= keyvals "")))
  220                         (concat LaTeX-optop keyvals LaTeX-optcl))))
  221     (LaTeX-item-equation-alignat t))
  222     (let ((keyvals
  223        (TeX-read-key-val t LaTeX-empheq-key-val-options-local "empheq options (k=v)")))
  224       (LaTeX-insert-environment env (when (and keyvals (not (string= keyvals "")))
  225                       (concat LaTeX-optop keyvals LaTeX-optcl)))
  226       (when (and (assoc env LaTeX-label-alist)
  227          (LaTeX-label env 'environment))
  228     (LaTeX-newline)
  229     (indent-according-to-mode)))))
  230 
  231 (defun LaTeX-empheq-item-equation ()
  232   "Insert contents to terminate a line in multi-line equations environment.
  233 Put line break macro on the last line.  Next, if the current
  234 environment wants \\label, insert it also.  And insert suitable
  235 number of ampersands if possible."
  236   (let ((env (LaTeX-current-environment))
  237     amsenv ncols match)
  238     (save-excursion
  239       (LaTeX-find-matching-begin)
  240       (re-search-forward (concat (regexp-quote TeX-esc)
  241                  "begin" TeX-grop env TeX-grcl))
  242       (when (looking-at "[ \t\n\r%]*\\[")
  243     (forward-sexp))
  244       (re-search-forward "[ \t\n\r%]*{\\([^}]+\\)}")
  245       (setq match (replace-regexp-in-string "[ \t\n\r%]" ""
  246                         (match-string-no-properties 1)))
  247       (if (string-match "=" match)
  248       (progn
  249         (setq amsenv (car (split-string match "=")))
  250         (setq ncols (string-to-number (cadr (split-string match "=")))))
  251     (setq amsenv match)))
  252     ;; Do not ask for "\\" if in "equation" or "equation*" since these
  253     ;; are single line equations only
  254     (if (or (string= amsenv "equation")
  255         (string= amsenv "equation*"))
  256     ;; Nullify the effect of `M-RET'
  257     (progn
  258       (message "This environment does not support multi-line equations")
  259       (end-of-line 0)
  260       (kill-line 1))
  261       (progn
  262     (end-of-line 0)
  263     (just-one-space)
  264     (TeX-insert-macro "\\")
  265     (forward-line 1)
  266     (indent-according-to-mode)))
  267     ;; Add a new label only if not in "equation"
  268     (when (and (not (string= amsenv "equation"))
  269            (assoc amsenv LaTeX-label-alist)
  270            (LaTeX-label amsenv 'environment))
  271       (LaTeX-newline)
  272       (indent-according-to-mode))
  273     (when ncols
  274       (save-excursion
  275     (insert (make-string (+ ncols ncols -1) ?&))))))
  276 
  277 (TeX-add-style-hook
  278  "empheq"
  279  (lambda ()
  280 
  281    ;; Add empheq to parser
  282    (TeX-auto-add-regexp LaTeX-empheq-declaredelimiter-regexp)
  283 
  284    ;; Load amsmath.el and mathtools.el
  285    (TeX-run-style-hooks "amsmath" "mathtools")
  286 
  287    ;; Local version of key-val options
  288    (setq LaTeX-empheq-key-val-options-local
  289      (copy-alist LaTeX-empheq-key-val-options))
  290 
  291    ;; Initial update of key-vals
  292    (LaTeX-empheq-update-key-val-options)
  293 
  294    (LaTeX-add-environments
  295     '("empheq" LaTeX-empheq-env))
  296 
  297    ;; Add "empheq" to `LaTeX-item-list' and run
  298    ;; `LaTeX-empheq-item-equation' when `M-RET' is invoked
  299    (add-to-list 'LaTeX-item-list '("empheq" . LaTeX-empheq-item-equation) t)
  300 
  301    ;; Reftex support: Use `reftex-add-label-environments'
  302    (when (fboundp 'reftex-add-label-environments)
  303      (reftex-add-label-environments '(("empheq" ?e nil nil t))))
  304 
  305    (TeX-add-symbols
  306     '("empheqset" (TeX-arg-key-val LaTeX-empheq-key-val-options-local))
  307 
  308     ;; 1.4 Special delimiters
  309     ;; Normal
  310     '("empheqlbrace" TeX-arg-insert-right-brace-maybe)
  311     '("empheqrbrace")
  312     '("empheqlbrack" TeX-arg-insert-right-brace-maybe)
  313     '("empheqrbrack")
  314     '("empheqlangle" TeX-arg-insert-right-brace-maybe)
  315     '("empheqrangle")
  316     '("empheqlparen" TeX-arg-insert-right-brace-maybe)
  317     '("empheqrparen")
  318     '("empheqlvert" TeX-arg-insert-right-brace-maybe)
  319     '("empheqrvert")
  320     '("empheqlVert" TeX-arg-insert-right-brace-maybe)
  321     '("empheqrVert")
  322     '("empheqlfloor" TeX-arg-insert-right-brace-maybe)
  323     '("empheqrfloor")
  324     '("empheqlceil" TeX-arg-insert-right-brace-maybe)
  325     '("empheqrceil")
  326     ;; Bigger
  327     '("empheqbiglbrace" TeX-arg-insert-right-brace-maybe)
  328     '("empheqbigrbrace")
  329     '("empheqbiglbrack" TeX-arg-insert-right-brace-maybe)
  330     '("empheqbigrbrack")
  331     '("empheqbiglangle" TeX-arg-insert-right-brace-maybe)
  332     '("empheqbigrangle")
  333     '("empheqbiglparen" TeX-arg-insert-right-brace-maybe)
  334     '("empheqbigrparen")
  335     '("empheqbiglvert" TeX-arg-insert-right-brace-maybe)
  336     '("empheqbigrvert")
  337     '("empheqbiglVert" TeX-arg-insert-right-brace-maybe)
  338     '("empheqbigrVert")
  339     '("empheqbiglfloor" TeX-arg-insert-right-brace-maybe)
  340     '("empheqbigrfloor")
  341     '("empheqbiglceil" TeX-arg-insert-right-brace-maybe)
  342     '("empheqbigrceil"))
  343 
  344    ;; Append delimiters to `TeX-braces-association'
  345    (make-local-variable 'TeX-braces-association)
  346    (let ((delimiters '(("\\empheqlbrace" . "\\empheqrbrace")
  347                ("\\empheqlbrack" . "\\empheqrbrack")
  348                ("\\empheqlangle" . "\\empheqrangle")
  349                ("\\empheqlparen" . "\\empheqrparen")
  350                ("\\empheqlvert"  . "\\empheqrvert")
  351                ("\\empheqlVert"  . "\\empheqrVert")
  352                ("\\empheqlfloor" . "\\empheqrfloor")
  353                ("\\empheqlceil"  . "\\empheqrceil")
  354                ("\\empheqbiglbrace" . "\\empheqbigrbrace")
  355                ("\\empheqbiglbrack" . "\\empheqbigrbrack")
  356                ("\\empheqbiglangle" . "\\empheqbigrangle")
  357                ("\\empheqbiglparen" . "\\empheqbigrparen")
  358                ("\\empheqbiglvert"  . "\\empheqbigrvert")
  359                ("\\empheqbiglVert"  . "\\empheqbigrVert")
  360                ("\\empheqbiglfloor" . "\\empheqbigrfloor")
  361                ("\\empheqbiglceil"  . "\\empheqbigrceil"))))
  362      (dolist (elt delimiters)
  363        (add-to-list 'TeX-braces-association elt t)))
  364 
  365    ;; 2.2.1 Using multline
  366    (when (LaTeX-provided-package-options-member "empheq" "oldmultline")
  367      (LaTeX-add-environments
  368       '("MTmultlined" LaTeX-mathtools-env-multlined)))
  369 
  370    ;; 2.2.2 The overload option
  371    ;; I simplify it and ignore the additional feature overload2:
  372    (when (or (LaTeX-provided-package-options-member "empheq" "overload")
  373          (LaTeX-provided-package-options-member "empheq" "overload2"))
  374      (LaTeX-add-environments
  375       '("align"      LaTeX-empheq-env-overload)
  376       '("alignat"    LaTeX-empheq-env-overload)
  377       '("equation"   LaTeX-empheq-env-overload)
  378       '("flalign"    LaTeX-empheq-env-overload)
  379       '("gather"     LaTeX-empheq-env-overload)
  380       '("multline"   LaTeX-empheq-env-overload)
  381       '("align*"     LaTeX-env-args [TeX-arg-key-val LaTeX-empheq-key-val-options-local])
  382       '("alignat*"   LaTeX-empheq-env-overload)
  383       '("equation*"  LaTeX-env-args [TeX-arg-key-val LaTeX-empheq-key-val-options-local])
  384       '("flalign*"   LaTeX-env-args [TeX-arg-key-val LaTeX-empheq-key-val-options-local])
  385       '("gather*"    LaTeX-env-args [TeX-arg-key-val LaTeX-empheq-key-val-options-local])
  386       '("multline*"  LaTeX-env-args [TeX-arg-key-val LaTeX-empheq-key-val-options-local])
  387 
  388       ;; Original definitions are stored prefixed with "AmS"
  389       '("AmSalign"      LaTeX-env-label)
  390       '("AmSalignat"    LaTeX-amsmath-env-alignat)
  391       '("AmSequation"   LaTeX-env-label)
  392       '("AmSflalign"    LaTeX-env-label)
  393       '("AmSgather"     LaTeX-env-label)
  394       '("AmSmultline"   LaTeX-env-label)
  395       '("AmSalign*")
  396       '("AmSalignat*"   LaTeX-amsmath-env-alignat)
  397       '("AmSequation*")
  398       '("AmSflalign*")
  399       '("AmSgather*")
  400       '("AmSmultline*"))
  401 
  402      ;; Append original definitions to `LaTeX-label-alist'
  403      (let ((envs '("AmSalign"
  404            "AmSalignat"
  405            "AmSequation"
  406            "AmSflalign"
  407            "AmSgather"
  408            "AmSmultline")))
  409        (dolist (env envs)
  410      (add-to-list 'LaTeX-label-alist `(,env . LaTeX-amsmath-label) t)))
  411 
  412      ;; RefTeX support: Add original definitions with `reftex-add-label-environments'
  413      (when (fboundp 'reftex-add-label-environments)
  414        (let ((envs '(("AmSalign"     ?e nil nil eqnarray-like)
  415              ("AmSequation"  ?e nil nil t)
  416              ("AmSgather"    ?e nil nil eqnarray-like)
  417              ("AmSmultline"  ?e nil nil t)
  418              ("AmSflalign"   ?e nil nil eqnarray-like)
  419              ("AmSalignat"   ?e nil nil alignat-like))))
  420      (dolist (env envs)
  421        (reftex-add-label-environments `(,env)))))
  422 
  423      ;; Append original definitions to `LaTeX-item-list'; functions
  424      ;; are provided by amsmath.el
  425      (let ((envs '(("AmSalign" . LaTeX-item-equation)
  426            ("AmSalign*" . LaTeX-item-equation)
  427            ("AmSflalign" . LaTeX-item-equation)
  428            ("AmSalignat" . LaTeX-item-equation-alignat)
  429            ("AmSalignat*" . LaTeX-item-equation-alignat)
  430            ("AmSflalign*" . LaTeX-item-equation)
  431            ("AmSgather" . LaTeX-item-equation)
  432            ("AmSgather*" . LaTeX-item-equation)
  433            ("AmSmultline" . LaTeX-item-equation)
  434            ("AmSmultline*" . LaTeX-item-equation))))
  435        (dolist (env envs)
  436      (add-to-list 'LaTeX-item-list env t)))
  437 
  438      ;; Ispell skip lists:
  439      (TeX-ispell-skip-setcdr
  440       `(,(cons (concat "\\(AmS\\(?:align\\(?:\\*\\|at\\*?\\)?\\|"
  441                "equation\\*?\\|flalign\\*?\\|gather\\*?\\|multline\\*?\\)\\)")
  442            (concat "\\\\end{"
  443                "\\(AmS\\(?:align\\(?:\\*\\|at\\*?\\)?\\|"
  444                "equation\\*?\\|flalign\\*?\\|gather\\*?\\|multline\\*?\\)\\)}")))))
  445 
  446    ;; 3.2 Support for ntheorem
  447    (LaTeX-add-lengths "mintagvsep")
  448 
  449    ;; 4.1 Creating your own delimiters
  450    (TeX-add-symbols
  451     '("DeclareLeftDelimiter"
  452       [ "Space adjustment" ]
  453       (TeX-arg-eval
  454        (lambda ()
  455      (let ((delim (TeX-read-string (concat "Delimiter: " TeX-esc))))
  456        (TeX-add-symbols (concat "empheq" delim)
  457                 (concat "empheqbig" delim))
  458        (LaTeX-add-empheq-declaredelimiters `(,delim "Left"))
  459        (LaTeX-empheq-update-key-val-options)
  460        (concat TeX-esc delim)))))
  461 
  462     '("DeclareRightDelimiter"
  463       [ "Space adjustment" ]
  464       (TeX-arg-eval
  465        (lambda ()
  466      (let ((delim (TeX-read-string (concat "Delimiter: " TeX-esc))))
  467        (TeX-add-symbols (concat "empheq" delim)
  468                 (concat "empheqbig" delim))
  469        (LaTeX-add-empheq-declaredelimiters `(,delim "Right"))
  470        (LaTeX-empheq-update-key-val-options)
  471        (concat TeX-esc delim))))))
  472 
  473    ;; 4.2 Fine-tuning of delimiters
  474    (LaTeX-add-lengths "EmphEqdelimitershortfall")
  475    (LaTeX-add-counters "EmphEqdelimiterfactor")
  476 
  477    (TeX-add-symbols
  478     ;; 4.3 Scaling material yourself
  479     '("EmphEqdisplayheight" 0)
  480     '("EmphEqdisplaydepth"  0)
  481     ;; 6.1 New empheq-like environments
  482     '("EmphEqMainEnv" 0)
  483     '("endEmphEqMainEnv" 0))
  484 
  485    ;; Fontification
  486    (when (and (featurep 'font-latex)
  487           (eq TeX-install-font-lock 'font-latex-setup))
  488      (font-latex-add-keywords '(("empheqset"             "{")
  489                 ("DeclareLeftDelimiter"  "[{")
  490                 ("DeclareRightDelimiter" "[{"))
  491                   'function)))
  492  LaTeX-dialect)
  493 
  494 ;;; empheq.el ends here