"Fossies" - the Fresh Open Source Software Archive

Member "auctex-12.3/style/xparse.el" (18 Oct 2020, 10058 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 "xparse.el": 12.2_vs_12.3.

    1 ;;; xparse.el --- AUCTeX style for `xparse.sty' version 2020-03-06
    2 
    3 ;; Copyright (C) 2013, 2020 Free Software Foundation, Inc.
    4 
    5 ;; Maintainer: auctex-devel@gnu.org
    6 ;; Author: Mosè Giordano <mose@gnu.org>
    7 ;; Keywords: tex
    8 
    9 ;; This file is part of AUCTeX.
   10 
   11 ;; AUCTeX is free software; you can redistribute it and/or modify it
   12 ;; under the terms of the GNU General Public License as published by
   13 ;; the Free Software Foundation; either version 3, or (at your option)
   14 ;; any later version.
   15 
   16 ;; AUCTeX is distributed in the hope that it will be useful, but
   17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
   18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   19 ;; General Public License for more details.
   20 
   21 ;; You should have received a copy of the GNU General Public License
   22 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
   23 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   24 ;; 02110-1301, USA.
   25 
   26 ;;; Commentary:
   27 
   28 ;; This file adds basic support for `xparse.sty' version 2020-03-06.
   29 ;; It parses argument specification of macros and environments.
   30 
   31 ;; Currently, this style doesn't parse the embellishments specifiers
   32 ;; `e' and `E'.  The "yet not more supported" specifiers `l', `u', `g'
   33 ;; and `G' are ignored completely and may lead to wrong parsing
   34 ;; results.
   35 
   36 ;;; Code:
   37 
   38 ;; Silence the compiler:
   39 (declare-function font-latex-add-keywords
   40           "font-latex"
   41           (keywords class))
   42 
   43 ;; Needed for auto-parsing.
   44 (require 'tex)
   45 
   46 (TeX-auto-add-type "xparse-macro" "LaTeX")
   47 
   48 (defvar LaTeX-xparse-macro-regexp
   49   `(,(concat
   50       (regexp-quote TeX-esc)
   51       "\\(?:New\\|Renew\\|Provide\\|Declare\\)"
   52       "\\(?:Expandable\\)?"
   53       "DocumentCommand"
   54       "[ \t\n\r]*"
   55       "{?"
   56       "[ \t\n\r]*"
   57       (regexp-quote TeX-esc)
   58       "\\([A-Za-z]+\\)"
   59       "[ \t\n\r]*"
   60       "}?"
   61       "[ \t\n\r]*"
   62       "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*}[^}{]*\\)*}[^}{]*\\)*\\)}")
   63     (1 2) LaTeX-auto-xparse-macro)
   64   "Matches macros by xparse package.")
   65 
   66 (TeX-auto-add-type "xparse-environment" "LaTeX")
   67 
   68 (defvar LaTeX-xparse-environment-regexp
   69   `(,(concat
   70       (regexp-quote TeX-esc)
   71       "\\(?:New\\|Renew\\|Provide\\|Declare\\)"
   72       "DocumentEnvironment"
   73       "[ \t\n\r]*"
   74       "{"
   75       "[ \t\n\r]*"
   76       "\\([A-Za-z]+\\)"
   77       "[ \t\n\r]*"
   78       "}"
   79       "[ \t\n\r]*"
   80       "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*}[^}{]*\\)*}[^}{]*\\)*\\)}")
   81     (1 2) LaTeX-auto-xparse-environment)
   82   "Matches environments by xparse package.")
   83 
   84 (defun LaTeX-arg-xparse-query (optional op-brace cl-brace &optional prompt)
   85   "Special query function for parsed elements from xparse package.
   86 If OPTIONAL is non-nil, indicate it in minibuffer.  OP-BRACE sets
   87 the opening brace, CL-BRACE sets the closing one.  PROMPT
   88 replaces the standard one."
   89   (let ((TeX-arg-opening-brace op-brace)
   90     (TeX-arg-closing-brace cl-brace))
   91     (TeX-argument-insert
   92      (TeX-read-string (TeX-argument-prompt optional prompt "Text"))
   93      optional)))
   94 
   95 (defun LaTeX-xparse-macro-parse (type)
   96   "Process parsed macro and environment definitions.
   97 TYPE is one of the symobols mac or env."
   98   (dolist (xcmd (if (eq type 'mac)
   99             (LaTeX-xparse-macro-list)
  100           (LaTeX-xparse-environment-list)))
  101     (let ((name (car xcmd))
  102       (spec (cadr xcmd))
  103       args opt-star opt-token)
  104       (with-temp-buffer
  105     (set-syntax-table LaTeX-mode-syntax-table)
  106     ;; This one is probably not really needed?
  107     (goto-char (point-min))
  108     (insert (replace-regexp-in-string "[ \t\r\n%]" "" spec))
  109     (goto-char (point-min))
  110     (while (looking-at-p "[+!>bmrRvodODsteE]")
  111       (cond (;; + or !: Long argument or space aware: Move over
  112          ;; them.  b is special; only available for
  113          ;; enviroments
  114          (looking-at-p "[+!b]")
  115          (forward-char 1))
  116         ((looking-at-p ">")
  117          ;; Argument processors: Move over > and a balanced
  118          ;; {}
  119          (forward-char 1)
  120          (forward-sexp))
  121         ;; Mandatory arguments:
  122         ;; m: Ask for input with "Text" as prompt
  123         ((looking-at-p "m")
  124          (forward-char 1)
  125          (push "Text" args))
  126         ;; r<token1><token2>
  127         ((looking-at-p "r")
  128          (re-search-forward "r\\(?:\\(.\\)\\(.\\)\\)" (+ (point) 3) t)
  129          (push `(LaTeX-arg-xparse-query
  130              ,(match-string-no-properties 1)
  131              ,(match-string-no-properties 2))
  132                args))
  133         ;; R<token1><token2>{default}
  134         ((looking-at-p "R")
  135          (re-search-forward "R\\(?:\\(.\\)\\(.\\)\\)" (+ (point) 3) t)
  136          (forward-sexp)
  137          (push `(LaTeX-arg-xparse-query
  138              ,(match-string-no-properties 1)
  139              ,(match-string-no-properties 2))
  140                args))
  141         ;; v: Use `TeX-arg-verb-delim-or-brace'
  142         ((looking-at-p "v")
  143          (forward-char 1)
  144          (push 'TeX-arg-verb-delim-or-brace args))
  145         ;; Optional arguments:
  146         ;; o standard LaTeX optional in square brackets
  147         ((looking-at-p "o")
  148          (forward-char 1)
  149          (push (vector "Text") args))
  150         ;; d<token1><token2>
  151         ((looking-at-p "d")
  152          (re-search-forward "d\\(?:\\(.\\)\\(.\\)\\)" (+ (point) 3) t)
  153          (push (vector 'LaTeX-arg-xparse-query
  154                    (match-string-no-properties 1)
  155                    (match-string-no-properties 2))
  156                args))
  157         ;; O{default}
  158         ((looking-at-p "O")
  159          (forward-char 1)
  160          (forward-sexp)
  161          (push (vector "Text") args))
  162         ;; D<token1><token2>{default}
  163         ((looking-at-p "D")
  164          (re-search-forward "D\\(?:\\(.\\)\\(.\\)\\)" (+ (point) 3) t)
  165          (forward-sexp)
  166          (push (vector 'LaTeX-arg-xparse-query
  167                    (match-string-no-properties 1)
  168                    (match-string-no-properties 2))
  169                args))
  170         ;; s: optional star
  171         ((looking-at-p "s")
  172          (forward-char 1)
  173          (setq opt-star t))
  174         ;; t: optional <token>
  175         ((looking-at-p "t")
  176          (re-search-forward "t\\(.\\)" (+ (point) 2) t)
  177          (setq opt-token (match-string-no-properties 1)))
  178         ;; e & E are currently ignored.  e: If looking at a
  179         ;; {, move one balanced expression, otherwise only
  180         ;; one character.
  181         ((looking-at-p "e")
  182          (forward-char)
  183          (if (looking-at-p TeX-grop)
  184              (forward-sexp)
  185            (forward-char)))
  186         ;; E
  187         ((looking-at-p "E")
  188          (forward-char)
  189          (if (looking-at-p TeX-grop)
  190              (forward-sexp)
  191            (forward-char))
  192          (if (looking-at-p TeX-grop)
  193              (forward-sexp)
  194            (forward-char)))
  195         ;; Finished:
  196         (t nil))))
  197       (if (eq type 'env)
  198       (LaTeX-add-environments `(,name
  199                     LaTeX-env-args
  200                     ,@(reverse (copy-sequence args))))
  201     (TeX-add-symbols (cons name
  202                    (reverse (copy-sequence args))))
  203     (when opt-star
  204       (TeX-add-symbols (cons (concat name "*")
  205                  (reverse (copy-sequence args)))))
  206     (when opt-token
  207       (TeX-add-symbols (cons (concat name opt-token)
  208                  (reverse (copy-sequence args)))))))))
  209 
  210 (defun LaTeX-xparse-auto-prepare ()
  211   "Clear various `LaTeX-auto-xparse-*' variables before parsing."
  212   (setq LaTeX-auto-xparse-macro nil
  213     LaTeX-auto-xparse-environment nil))
  214 
  215 (defun LaTeX-xparse-auto-cleanup ()
  216   "Process parsed elements for xparse package."
  217   (LaTeX-xparse-macro-parse 'mac)
  218   (LaTeX-xparse-macro-parse 'env))
  219 
  220 (add-hook 'TeX-auto-prepare-hook #'LaTeX-xparse-auto-prepare t)
  221 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-xparse-auto-cleanup t)
  222 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
  223 
  224 (TeX-add-style-hook
  225  "xparse"
  226  (lambda ()
  227    (TeX-auto-add-regexp LaTeX-xparse-macro-regexp)
  228    (TeX-auto-add-regexp LaTeX-xparse-environment-regexp)
  229    (TeX-run-style-hooks
  230     "expl3")
  231    (TeX-add-symbols
  232     ;; Declaring commands
  233     '("DeclareDocumentCommand"
  234       TeX-arg-define-macro "Argument specification" t)
  235     '("NewDocumentCommand"
  236       TeX-arg-define-macro "Argument specification" t)
  237     '("RenewDocumentCommand"
  238       TeX-arg-macro "Argument specification" t)
  239     '("ProvideDocumentCommand"
  240       TeX-arg-define-macro "Argument specification" t)
  241 
  242     ;; Declaring commands and environments
  243     '("DeclareDocumentEnvironment" TeX-arg-define-environment
  244       "Argument specification" t t)
  245     '("NewDocumentEnvironment" TeX-arg-define-environment
  246       "Argument specification" t t)
  247     '("RenewDocumentEnvironment" TeX-arg-environment
  248       "Argument specification" t t)
  249     '("ProvideDocumentEnvironment" TeX-arg-define-environment
  250       "Argument specification" t t)
  251 
  252     ;; Fully-expandable document commands
  253     '("DeclareExpandableDocumentCommand"
  254       TeX-arg-define-macro "Argument specification" t)
  255     '("NewExpandableDocumentCommand"
  256       TeX-arg-define-macro "Argument specification" t)
  257     '("RenewExpandableDocumentCommand"
  258       TeX-arg-macro "Argument specification" t)
  259     '("ProvideExpandableDocumentCommand"
  260       TeX-arg-define-macro "Argument specification" t)
  261 
  262     ;; Testing special values
  263     '("IfBooleanTF" 3)
  264     '("IfBooleanT" 3)
  265     '("IfBooleanF" 3)
  266     '("IfNoValueTF" 3)
  267     '("IfNoValueT" 3)
  268     '("IfNoValueF" 3)
  269     '("IfValueTF" 3)
  270     '("IfValueT" 3)
  271     '("IfValueF" 3)
  272     "BooleanTrue"
  273     "BooleanFalse"
  274     ;; Argument processors
  275     "ProcessedArgument"
  276     "ReverseBoolean"
  277     '("SplitArgument" "Number" "Token")
  278     "SplitList"
  279     "TrimSpaces"
  280     '("ProcessList" "List" "Functiom")
  281     ;; Access to the argument specification
  282     '("GetDocumentCommandArgSpec" TeX-arg-macro)
  283     '("GetDocumentEnvironmmentArgSpec" TeX-arg-environment)
  284     '("ShowDocumentCommandArgSpec" TeX-arg-macro)
  285     '("ShowDocumentEnvironmentArgSpec" TeX-arg-environment))
  286 
  287    ;; Fontification
  288    (when (and (featurep 'font-latex)
  289           (eq TeX-install-font-lock 'font-latex-setup))
  290      (font-latex-add-keywords '(("DeclareDocumentCommand" "|{\\{{")
  291                 ("NewDocumentCommand"     "|{\\{{")
  292                 ("ProvideDocumentCommand" "|{\\{{")
  293                 ("RenewDocumentCommand"   "|{\\{{")
  294                 ;;
  295                 ("DeclareExpandableDocumentCommand" "|{\\{{")
  296                 ("NewExpandableDocumentCommand"     "|{\\{{")
  297                 ("ProvideExpandableDocumentCommand" "|{\\{{")
  298                 ("RenewExpandableDocumentCommand"   "|{\\{{")
  299                 ;;
  300                 ("DeclareDocumentEnvironment" "{{{{")
  301                 ("NewDocumentEnvironment"     "{{{{")
  302                 ("ProvideDocumentEnvironment" "{{{{")
  303                 ("RenewDocumentEnvironment"   "{{{{"))
  304                   'function)))
  305  LaTeX-dialect)
  306 
  307 (defun LaTeX-xparse-package-options ()
  308   "Read the xparse package options from the user."
  309   (TeX-read-key-val t '(("log-declarations" ("true" "false")))))
  310 
  311 ;;; xparse.el ends here