emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/auctex 43f6398 46/71: Improve handling of key=vals in s


From: Tassilo Horn
Subject: [elpa] externals/auctex 43f6398 46/71: Improve handling of key=vals in style/enumitem.el
Date: Fri, 17 Dec 2021 15:00:34 -0500 (EST)

branch: externals/auctex
commit 43f6398028d1d14e4ee5a9538e2dba4ef791ea53
Author: Arash Esbati <arash@gnu.org>
Commit: Arash Esbati <arash@gnu.org>

    Improve handling of key=vals in style/enumitem.el
    
    * style/enumitem.el (LaTeX-enumitem-key-val-options-local)
    (LaTeX-enumitem-newlist-list-local): Remove now unused variables.
    (LaTeX-enumitem-key-val-options): New function returning key=vals.
    Replace all occurrences of `LaTeX-enumitem-key-val-options-local'
    with a call to the new function.
    (LaTeX-arg-enumitem-SetLabelAlign)
    (LaTeX-arg-enumitem-SetEnumitemKey)
    (LaTeX-arg-enumitem-SetEnumitemValue): Add library prefix to the
    function names.
    (LaTeX-arg-enumitem-setlist): New function.
    (LaTeX-enumitem-update-key-val-options): Remove unused function.
    ("enumitem"): Update style to package version 3.9, add support for
    'size' feature in \setlist and adjust fontification.  Fix typo for
    'labelindent'.  Various docstring fixes.
---
 style/enumitem.el | 260 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 139 insertions(+), 121 deletions(-)

diff --git a/style/enumitem.el b/style/enumitem.el
index 1a858b1..d8ad056 100644
--- a/style/enumitem.el
+++ b/style/enumitem.el
@@ -1,6 +1,6 @@
-;;; enumitem.el --- AUCTeX style for `enumitem.sty' (v3.6)  -*- 
lexical-binding: t; -*-
+;;; enumitem.el --- AUCTeX style for `enumitem.sty' (v3.9)  -*- 
lexical-binding: t; -*-
 
-;; Copyright (C) 2015, 2016, 2018, 2020 Free Software Foundation, Inc.
+;; Copyright (C) 2015--2021 Free Software Foundation, Inc.
 
 ;; Author: Arash Esbati <arash@gnu.org>
 ;; Maintainer: auctex-devel@gnu.org
@@ -26,7 +26,7 @@
 
 ;;; Commentary:
 
-;; This file adds support for `enumitem.sty' (v3.6) from 2018/11/30.
+;; This file adds support for `enumitem.sty' (v3.9) from 2019/06/20.
 ;; `enumitem.sty' is part of TeXLive.
 
 ;; Tassilo Horn's `minted.el' was a major source of inspiration for
@@ -112,18 +112,19 @@
     ("beginpenalty")
     ("midpenalty")
     ("endpenalty")
+    ;; 3.6 Injecting code
     ("before")
     ("before*")
     ("after")
     ("after*")
     ("first")
     ("first*")
-    ;; 3.6 Description styles
+    ;; 3.7 Description styles
     ("style" ("standard" "unboxed" "nextline" "sameline" "multiline"))
-    ;; 3.7 Compact lists
+    ;; 3.8 Compact lists
     ("noitemsep")
     ("nosep")
-    ;; 3.8 Wide lists
+    ;; 3.9 Wide lists
     ("wide")
     ;; 4 Inline lists
     ("itemjoin")
@@ -132,14 +133,48 @@
     ("mode" ("boxed" "unboxed")))
   "Key=value options for enumitem macros and environments.")
 
-(defvar LaTeX-enumitem-key-val-options-local nil
-  "Buffer-local key=value options for enumitem macros and environments.")
-(make-variable-buffer-local 'LaTeX-enumitem-key-val-options-local)
-
-(defvar LaTeX-enumitem-newlist-list-local nil
-  "Local list of all environments definded with `\\newlist' plus
-the ones initially available through `enumitem' package.")
-(make-variable-buffer-local 'LaTeX-enumitem-newlist-list-local)
+(defun LaTeX-enumitem-key-val-options ()
+  "Return an updated list of key=vals from enumitem package."
+  (append
+   ;; New keys are valueless, so take them as is:
+   (when (LaTeX-enumitem-SetEnumitemKey-list)
+     (LaTeX-enumitem-SetEnumitemKey-list))
+   ;; New values defined available keys: We have to collect predefined
+   ;; values (if any) from `LaTeX-enumitem-key-val-options' (stored in
+   ;; `vals-predefined') and user-defined values (stored in
+   ;; `vals-parsed') which were parsed and added to `result' in the
+   ;; previous run of `dolist' and then combine them as value to a
+   ;; key.
+   (when (LaTeX-enumitem-SetEnumitemValue-list)
+     (let (result)
+       (dolist (keyvals (LaTeX-enumitem-SetEnumitemValue-list) result)
+         (let* ((key (nth 1 keyvals))
+                (val (nth 2 keyvals))
+                (vals-predefined
+                 (cadr (assoc key LaTeX-enumitem-key-val-options)))
+                (vals-parsed (cadr (assoc key result))))
+           ;; Remove entry in `result' if there is one for the `key':
+           (when (assoc key result)
+             (setq result (assq-delete-all (car (assoc key result))
+                                           result)))
+           ;; Add the entry to `result'; also remove any duplicates
+           (cl-pushnew (list key (TeX-delete-duplicate-strings
+                                  (append vals-parsed
+                                          vals-predefined
+                                          (list val))))
+                       result :test #'equal)))))
+   ;; New values to `align' key: We collect the predefined ones from
+   ;; `LaTeX-enumitem-key-val-options' in `vals-predefined' and
+   ;; prepend them to newly parsed ones:
+   (when (LaTeX-enumitem-SetLabelAlign-list)
+     (let* ((key "align")
+            (vals (mapcar #'car (LaTeX-enumitem-SetLabelAlign-list)))
+            (vals-predefined (cadr
+                              (assoc key LaTeX-enumitem-key-val-options))))
+       `(("align" ,(TeX-delete-duplicate-strings
+                    (append vals-predefined vals))))))
+   ;; Predefined key=vals:
+   LaTeX-enumitem-key-val-options))
 
 ;; Setup for \newlist:
 
@@ -148,8 +183,7 @@ the ones initially available through `enumitem' package.")
 (defvar LaTeX-enumitem-newlist-regexp
   '("\\\\newlist{\\([^}]+\\)}{\\([^}]+\\)}"
     (1 2) LaTeX-auto-enumitem-newlist)
-  "Matches the arguments of `\\newlist' from `enumitem'
-package.")
+  "Matches the arguments of `\\newlist' from `enumitem' package.")
 
 ;; Setup for \SetLabelAlign:
 
@@ -158,8 +192,7 @@ package.")
 (defvar LaTeX-enumitem-SetLabelAlign-regexp
   '("\\\\SetLabelAlign{\\([^}]+\\)}"
     1 LaTeX-auto-enumitem-SetLabelAlign)
-  "Matches the argument of `\\SetLabelAlign' from `enumitem'
-package.")
+  "Matches the argument of `\\SetLabelAlign' from `enumitem' package.")
 
 ;; Setup for \SetEnumitemKey:
 
@@ -168,8 +201,7 @@ package.")
 (defvar LaTeX-enumitem-SetEnumitemKey-regexp
   '("\\\\SetEnumitemKey{\\([^}]+\\)}"
     1 LaTeX-auto-enumitem-SetEnumitemKey)
-  "Matches the arguments of `\\SetEnumitemKey' from `enumitem'
-package.")
+  "Matches the arguments of `\\SetEnumitemKey' from `enumitem' package.")
 
 ;; Setup for \SetEnumitemValue:
 
@@ -185,8 +217,7 @@ package.")
 (defvar LaTeX-enumitem-SetEnumitemValue-regexp
   '("\\\\SetEnumitemValue{\\([^}]+\\)}{\\([^}]+\\)}"
     (0 1 2) LaTeX-auto-enumitem-SetEnumitemValue)
-  "Matches the arguments of `\\SetEnumitemValue' from `enumitem'
-package.")
+  "Matches the arguments of `\\SetEnumitemValue' from `enumitem' package.")
 
 ;; Plug them into the machinery.
 (defun LaTeX-enumitem-auto-prepare ()
@@ -208,25 +239,18 @@ package.")
       (when (or (string-equal type "description")
                 (string-equal type "description*"))
         (add-to-list 'LaTeX-item-list `(,env . LaTeX-item-argument)))
-      ;; Add new env's to `ispell-tex-skip-alist': skip the optional argument
-      (TeX-ispell-skip-setcdr `((,env ispell-tex-arg-end 0)))))
-  ;; Now add the parsed env's to the local list.
-  (when (LaTeX-enumitem-newlist-list)
-    (setq LaTeX-enumitem-newlist-list-local
-          (append (mapcar #'list (mapcar #'car (LaTeX-enumitem-newlist-list)))
-                  LaTeX-enumitem-newlist-list-local))))
+      ;; Add new env's to `ispell-tex-skip-alist': skip the opt. arg:
+      (TeX-ispell-skip-setcdr `((,env ispell-tex-arg-end 0))))))
 
 (add-hook 'TeX-auto-prepare-hook #'LaTeX-enumitem-auto-prepare t)
 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-enumitem-auto-cleanup t)
 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
 
 (defun LaTeX-enumitem-env-with-opts (env)
-  "Update available key-val options, then insert ENV and optional
-key-val and the first item."
-  (LaTeX-enumitem-update-key-val-options)
+  "Insert ENV provided by `enumitem' package."
   (LaTeX-insert-environment
    env
-   (let ((opts (TeX-read-key-val t LaTeX-enumitem-key-val-options-local)))
+   (let ((opts (TeX-read-key-val t (LaTeX-enumitem-key-val-options))))
      (when (and opts (not (string-equal opts "")))
        (format "[%s]" opts))))
   (if (TeX-active-mark)
@@ -253,21 +277,20 @@ key-val and the first item."
                 (current-fill-column)))
     (LaTeX-fill-paragraph nil)))
 
-(defun LaTeX-arg-SetLabelAlign (optional)
-  "Ask for new type (value) for the \"align\" key and add it to
-`LaTeX-enumitem-key-val-options-local'."
-  (LaTeX-enumitem-update-key-val-options)
+(defun LaTeX-arg-enumitem-SetLabelAlign (optional)
+  "Ask and insert a new type (value) for the \"align\" key.
+Insert value in brackets if OPTIONAL is non-nil."
   (let ((val (TeX-read-string "Alignment: ")))
     (TeX-argument-insert val optional)
     (LaTeX-add-enumitem-SetLabelAligns val)))
 
-(defun LaTeX-arg-SetEnumitemKey (optional)
-  "Ask for a new key to be defined and add it to
-`LaTeX-enumitem-key-val-options-local'."
-  (LaTeX-enumitem-update-key-val-options)
+(defun LaTeX-arg-enumitem-SetEnumitemKey (optional)
+  "Ask and insert a new key and its replacement.
+Insert key and value in brackets if OPTIONAL is non-nil."
   (let ((key     (TeX-read-string "New Key: "))
         (replace (TeX-read-key-val optional
-                                   LaTeX-enumitem-key-val-options-local 
"Replacement")))
+                                   (LaTeX-enumitem-key-val-options)
+                                   "Replacement")))
     (TeX-argument-insert key     optional)
     (TeX-argument-insert replace optional)
     (LaTeX-add-enumitem-SetEnumitemKeys key)))
@@ -278,11 +301,10 @@ key-val and the first item."
 ;; the elements and pass the result to
 ;; `LaTeX-add-enumitem-SetEnumitemValues'.  It will vanish upon next
 ;; invocation of `C-c C-n'.
-(defun LaTeX-arg-SetEnumitemValue (optional)
-  "Ask for a new value added to an existing key incl. the final
-replacement of the value."
-  (LaTeX-enumitem-update-key-val-options)
-  (let ((key (completing-read  "Key: " LaTeX-enumitem-key-val-options-local))
+(defun LaTeX-arg-enumitem-SetEnumitemValue (optional)
+  "Ask and insert for a new value added to an existing key.
+Insert key and value in brackets if OPTIONAL is non-nil."
+  (let ((key (completing-read  "Key: " LaTeX-enumitem-key-val-options))
         (val (TeX-read-string "String value: ")))
     (TeX-argument-insert key optional)
     (TeX-argument-insert val optional)
@@ -290,32 +312,41 @@ replacement of the value."
      (list (concat "\\SetEnumitemValue{" key "}{" val "}")
            key val))))
 
-(defun LaTeX-enumitem-update-key-val-options ()
-  "Update the buffer-local key-val options before offering them
-in `enumitem'-completions."
-  (dolist (key (LaTeX-enumitem-SetEnumitemKey-list))
-    (add-to-list 'LaTeX-enumitem-key-val-options-local key))
-  (dolist (keyvals (LaTeX-enumitem-SetEnumitemValue-list))
-    (let* ((key (nth 1 keyvals))
-           (val (nth 2 keyvals))
-           ;; (key-match (car (assoc key 
LaTeX-enumitem-key-val-options-local)))
-           (val-match (cdr (assoc key LaTeX-enumitem-key-val-options-local)))
-           (temp (copy-alist LaTeX-enumitem-key-val-options-local))
-           (opts (assq-delete-all (car (assoc key temp)) temp)))
-      (if val-match
-          (cl-pushnew (list key (TeX-delete-duplicate-strings (apply #'append 
(list val) val-match)))
-                      opts :test #'equal)
-        (cl-pushnew (list key (list val)) opts :test #'equal))
-      (setq LaTeX-enumitem-key-val-options-local (copy-alist opts))))
-  (dolist (newalign (LaTeX-enumitem-SetLabelAlign-list))
-    (let* ((key "align")
-           (val (car newalign))
-           (val-match (cdr (assoc key LaTeX-enumitem-key-val-options-local)))
-           (temp (copy-alist LaTeX-enumitem-key-val-options-local))
-           (opts (assq-delete-all (car (assoc key temp)) temp)))
-      (cl-pushnew (list key (TeX-delete-duplicate-strings (apply #'append 
(list val) val-match)))
-                  opts :test #'equal)
-      (setq LaTeX-enumitem-key-val-options-local (copy-alist opts)))))
+(defun LaTeX-arg-enumitem-setlist (_optional)
+  "Insert the arguments of `\\setlist' macro from `enumitem' package.
+This function inserts three arguments.  The first optional
+argument is only available when the package option `sizes' is
+provided.  OPTIONAL is ignored."
+  ;; First opt. argument:
+  (when (LaTeX-provided-package-options-member "enumitem" "sizes")
+    (let* ((TeX-arg-opening-brace "<")
+           (TeX-arg-closing-brace ">")
+           (sizes '("script" "tiny" "footnote" "small" "normal"
+                    "large" "Large" "LARGE" "huge" "Huge"))
+           (size (completing-read
+                  (TeX-argument-prompt t nil "Size")
+                  (append
+                   (mapcar (lambda (x) (concat "-" x)) sizes)
+                   (mapcar (lambda (x) (concat x "-")) sizes)
+                   sizes))))
+      (TeX-argument-insert size t)))
+  ;; Second opt. argument:
+  (TeX-argument-insert
+   (mapconcat #'identity
+              (TeX-completing-read-multiple
+               (TeX-argument-prompt t nil "Environment(s), level(s)")
+               (append
+                (when (LaTeX-provided-package-options-member
+                       "enumitem" "includedisplayed")
+                  '("trivlist"))
+                (mapcar #'car (LaTeX-enumitem-newlist-list))
+                '("1" "2" "3" "4")))
+              ",")
+   t)
+  ;; Mandatory argument:
+  (TeX-argument-insert
+   (TeX-read-key-val nil (LaTeX-enumitem-key-val-options))
+   nil))
 
 (TeX-add-style-hook
  "enumitem"
@@ -327,19 +358,16 @@ in `enumitem'-completions."
    (TeX-auto-add-regexp LaTeX-enumitem-SetEnumitemValue-regexp)
    (TeX-auto-add-regexp LaTeX-enumitem-SetLabelAlign-regexp)
 
-   ;; Activate the buffer-local version of key-vals.
-   (setq LaTeX-enumitem-key-val-options-local
-         (copy-alist LaTeX-enumitem-key-val-options))
-
-   ;; Set the standard env's to the local list.
-   (setq LaTeX-enumitem-newlist-list-local
-         '(("itemize") ("enumerate") ("description")))
+   ;; Add the standard environments:
+   (LaTeX-add-enumitem-newlists '("itemize"     "itemize")
+                                '("enumerate"   "enumerate")
+                                '("description" "description"))
 
-   ;; Add the starred versions to the local list.
+   ;; Add the starred versions with the 'inline' package option:
    (when (LaTeX-provided-package-options-member "enumitem" "inline")
-     (setq LaTeX-enumitem-newlist-list-local
-           (append '(("itemize*") ("enumerate*") ("description*"))
-                   LaTeX-enumitem-newlist-list-local)))
+     (LaTeX-add-enumitem-newlists '("itemize*"     "itemize*")
+                                  '("enumerate*"   "enumerate*")
+                                  '("description*" "description*")))
 
    ;; Standard env's take key-val as optional argument.
    (LaTeX-add-environments
@@ -355,8 +383,11 @@ in `enumitem'-completions."
       '("description*" LaTeX-enumitem-env-with-opts))
      (add-to-list 'LaTeX-item-list '("description*" . LaTeX-item-argument)))
 
-   ;; 7 Cloning the basic lists
    (TeX-add-symbols
+    ;; 6 Size dependent settings
+    '("SetEnumitemSize" 2)
+
+    ;; 7 Cloning the basic lists
     ;; The easy way would be:
     ;; '("newlist"
     ;;   "Name" (TeX-arg-eval
@@ -373,11 +404,8 @@ in `enumitem'-completions."
          (let ((name (TeX-read-string "Name: "))
                (type (completing-read
                       "Type: "
-                      '(("itemize")  ("enumerate")  ("description")
-                        ("itemize*") ("enumerate*") ("description*"))))
+                      (mapcar #'cadr (LaTeX-enumitem-newlist-list))))
                (depth (TeX-read-string "Max-depth: ")))
-           (setq LaTeX-enumitem-newlist-list-local
-                 (append `(,(list name)) LaTeX-enumitem-newlist-list-local))
            (when (or (string-equal type "description")
                      (string-equal type "description*"))
              (add-to-list 'LaTeX-item-list `(,name . LaTeX-item-argument)))
@@ -391,28 +419,13 @@ in `enumitem'-completions."
     ;; \renewlist{<name>}{<type>}{<max-depth>}
     '("renewlist"
       (TeX-arg-eval completing-read "Name: "
-                    LaTeX-enumitem-newlist-list-local)
+                    (mapcar #'car (LaTeX-enumitem-newlist-list)))
       (TeX-arg-eval completing-read "Type: "
-                    '(("itemize")  ("enumerate")  ("description")
-                      ("itemize*") ("enumerate*") ("description*")))
+                    (mapcar #'cadr (LaTeX-enumitem-newlist-list)))
       "Max-depth")
 
-    ;; \setlist[<names,levels>]{<key-vals>}
-    '("setlist"
-      [TeX-arg-eval mapconcat #'identity
-                    (TeX-completing-read-multiple
-                     (TeX-argument-prompt t nil "Environment(s), level(s)")
-                     (append
-                      (when (LaTeX-provided-package-options-member "enumitem"
-                                                                   
"includedisplayed")
-                        '("trivlist"))
-                      LaTeX-enumitem-newlist-list-local
-                      '(("1") ("2") ("3") ("4")))) ","]
-      (TeX-arg-eval
-       (lambda ()
-         (LaTeX-enumitem-update-key-val-options)
-         (let ((opts (TeX-read-key-val nil 
LaTeX-enumitem-key-val-options-local)))
-           (format "%s" opts)))))
+    ;; \setlist<size>[<names,levels>]{<key-vals>}
+    '("setlist" LaTeX-arg-enumitem-setlist)
 
     ;; \setlist*[<names,levels>]{<key-vals>}
     '("setlist*"
@@ -423,13 +436,10 @@ in `enumitem'-completions."
                       (when (LaTeX-provided-package-options-member "enumitem"
                                                                    
"includedisplayed")
                         '("trivlist"))
-                      LaTeX-enumitem-newlist-list-local
-                      '(("1") ("2") ("3") ("4")))) ","]
-      (TeX-arg-eval
-       (lambda ()
-         (LaTeX-enumitem-update-key-val-options)
-         (let ((opts (TeX-read-key-val nil 
LaTeX-enumitem-key-val-options-local)))
-           (format "%s" opts))))) )
+                      (mapcar #'car (LaTeX-enumitem-newlist-list))
+                      '("1" "2" "3" "4")))
+                    ","]
+      (TeX-arg-key-val (LaTeX-enumitem-key-val-options))) )
 
    ;; General commands:
    (TeX-add-symbols
@@ -460,13 +470,13 @@ in `enumitem'-completions."
            (completing-read "List name: " enums)))))
 
     ;; "Align" is added as new value to "align" key in key-val list.
-    '("SetLabelAlign" LaTeX-arg-SetLabelAlign t)
+    '("SetLabelAlign" LaTeX-arg-enumitem-SetLabelAlign t)
 
     ;; "Key" will be parsed and added to key-val list.
-    '("SetEnumitemKey" LaTeX-arg-SetEnumitemKey)
+    '("SetEnumitemKey" LaTeX-arg-enumitem-SetEnumitemKey)
 
     ;; "Key" and "Value" are added to our key-val list.
-    '("SetEnumitemValue" LaTeX-arg-SetEnumitemValue "Replacement")
+    '("SetEnumitemValue" LaTeX-arg-enumitem-SetEnumitemValue "Replacement")
 
     ;; v3.6 has a macro for visual debugging.
     '("DrawEnumitemLabel" 0))
@@ -476,23 +486,31 @@ in `enumitem'-completions."
      (TeX-add-symbols
       '("SetEnumerateShortLabel"
         (TeX-arg-eval completing-read "Key: "
-                      '(("A") ("a") ("I") ("i") ("1")))
+                      '("A" "a" "I" "i" "1"))
         "Replacement")))
 
    ;; Add \labelindent to list of known lengths:
-   (LaTeX-add-lengths "labelitem")
+   (LaTeX-add-lengths "labelindent")
 
    ;; Fontification
    (when (and (featurep 'font-latex)
               (eq TeX-install-font-lock 'font-latex-setup))
      (font-latex-add-keywords '(("newlist"             "{{{")
                                 ("renewlist"           "{{{")
-                                ("setlist"             "*[{")
+                                ("SetEnumitemSize"     "{{")
                                 ("AddEnumerateCounter" "*{{{")
                                 ("SetLabelAlign"       "{{")
                                 ("SetEnumitemKey"      "{{" )
                                 ("SetEnumitemValue"    "{{{"))
                               'function)
+     ;; Cater for additional optionals arg <size> based on package
+     ;; option 'sizes':
+     (font-latex-add-keywords
+      (if (LaTeX-provided-package-options-member "enumitem" "sizes")
+          '(("setlist" "*<[{"))
+        '(("setlist" "*[{")))
+      'function)
+
      (font-latex-add-keywords '(("restartlist"            "{" )
                                 ("setlistdepth"           "{" )
                                 ("SetEnumerateShortLabel" "{{"))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]