auctex-devel
[Top][All Lists]
Advanced

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

Re: [AUCTeX-devel] Adding key/vals to predefined ones


From: Arash Esbati
Subject: Re: [AUCTeX-devel] Adding key/vals to predefined ones
Date: Sat, 21 Feb 2015 15:35:12 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Tassilo Horn <tsdh <at> gnu.org> writes:

Hi Tassilo,

> [...]

> So your completion functions would use as completion table
> 
>   (append (LaTeX-enumitem-SetEnumitemKey-list)
>           (LaTeX-enumitem-SetEnumitemValue-list)
>           LaTeX-enumitem-key-val-options)

Thanks for your comments and sorry for my late response.  I played with this
idea but I ended up defining a `key-val-local' and a function
`LaTeX-enumitem-update-key-val-options' hooked in where required.  Having
that at hand, I also wrote `caption.el' as proof of concept.  Both final
versions are attached below (sorry for the long message).  Give them a roll,
I will make a proper submission upon your ok.

> So it seems you can drop the \SetEnumitem* handling
> from your cleanup function altogether.

You're right, done, Thnx.

> Another unrelated note: please name your auto-variables consistently,
> e.g. `LaTeX-auto-enumitem-WHATEVER' or `LaTeX-enumitem-auto-WHATEVER'.
> I prefer the latter style,

I know your preference here, I borrowed some ideas from `minted.el' and now
you caught me ;-)  Done.

Again, many thanks for your time and comments.

Best, Arash


--8<---------------cut here---------------start------------->8---
(defvar LaTeX-enumitem-key-val-options
  '(;; Vertical Spacing
    ("topsep")
    ("partopsep")
    ("parsep")
    ("itemsep")
    ;; Horizontal Spacing
    ("leftmargin"  ("*" "!"))
    ("itemindent"  ("*" "!"))
    ("labelsep"    ("*" "!"))
    ("labelwidth"  ("*" "!"))
    ("labelindent" ("*" "!"))
    ("labelsep*")
    ("labelindent*")
    ("widest")
    ("widest*")
    ("rightmargin")
    ;; Labels and cross reference format
    ("label")
    ("label*")
    ("ref")
    ("font")
    ("format")
    ("align" ("left" "right" "parleft"))
    ;; Numbering, stopping, resuming
    ("start")
    ("resume")
    ("resume*")
    ;; Series
    ("series")
    ;; Penalties
    ("beginpenalty")
    ("midpenalty")
    ("endpenalty")
    ("before")
    ("before*")
    ("after")
    ("after*")
    ;; Description styles
    ("style" ("standard" "multiline" "nextline" "sameline" "unboxed"))
    ;; Compact lists
    ("noitemsep")
    ("nosep")
    ;; Wide lists
    ("wide")
    ;; Inline lists
    ("itemjoin")
    ("itemjoin*")
    ("afterlabel")
    ("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)

;; Needed for auto-parsing.
(require 'tex)

;; Variables needed for \newlist: This command is not hooked into the
;; parser via `TeX-auto-add-type', we mimic that behaviour.

(defvar LaTeX-enumitem-newlist-list nil
  "List of environments defined by command `\\newlist' from
`enumitem' package.")

(defvar LaTeX-enumitem-newlist-list-local nil
  "Local list of all environments definded with `\\newlist'
plus available through `enumitem' package.")
(make-variable-buffer-local 'LaTeX-enumitem-newlist-list-local)

(defvar LaTeX-enumitem-newlist-list-item-arg nil
  "List of description like environments defined by command
`\\newlist' from `enumitem' package.")

(defvar LaTeX-enumitem-newlist-list-item-arg-local nil
  "Local list of all description like environments defined by command
`\\newlist' plus available through `enumitem' package.")
(make-variable-buffer-local 'LaTeX-enumitem-newlist-list-item-arg-local)

(defvar LaTeX-auto-enumitem-newlist nil
  "Temporary for parsing the arguments of `\\newlist' from
`enumitem' package.")

(defvar LaTeX-enumitem-newlist-regexp
  '("\\\\newlist{\\([^}]+\\)}{\\([^}]+\\)}"
    (1 2) LaTeX-auto-enumitem-newlist)
  "Matches the arguments of `\\newlist' from `enumitem'
package.")


;; Setup for \SetEnumitemKey:

(TeX-auto-add-type "enumitem-SetEnumitemKey" "LaTeX")

(defvar LaTeX-enumitem-SetEnumitemKey-regexp
  '("\\\\SetEnumitemKey{\\([^}]+\\)}{\\([^}]+\\)}"
    1 LaTeX-auto-enumitem-SetEnumitemKey)
  "Matches the arguments of `\\SetEnumitemKey' from `enumitem'
package.")


;; Setup for \SetEnumitemValue:

(TeX-auto-add-type "enumitem-SetEnumitemValue" "LaTeX")

(defvar LaTeX-enumitem-SetEnumitemValue-regexp
  '("\\\\SetEnumitemValue{\\([^}]+\\)}{\\([^}]+\\)}{\\([^}]+\\)}"
    (1 2) LaTeX-auto-enumitem-SetEnumitemValue)
  "Matches the arguments of `\\SetEnumitemValue' from `enumitem'
package.")

;; Plug them into the machinery.
(defun LaTeX-enumitem-auto-prepare ()
  "Clear various `LaTeX-enumitem-*' before parsing."
  (setq LaTeX-auto-enumitem-newlist          nil
        LaTeX-enumitem-newlist-list          nil
        LaTeX-enumitem-newlist-list-item-arg nil
        LaTeX-auto-enumitem-SetEnumitemKey   nil
        LaTeX-auto-enumitem-SetEnumitemValue nil))

(defun LaTeX-enumitem-auto-cleanup ()
  "Move parsing results into right places for further usage."
  ;; \newlist{<name>}{<type>}{<max-depth>}
  ;; env=<name>, type=<type>, ignored=<max-depth>
  (dolist (env-type LaTeX-auto-enumitem-newlist)
    (let* ((env  (car env-type))
           (type (cadr env-type)))
      (add-to-list 'LaTeX-auto-environment
                   (list env 'LaTeX-enumitem-env-with-opts))
      (add-to-list 'LaTeX-enumitem-newlist-list
                   (list env))
      (when (or (string-equal type "description")
                (string-equal type "description*"))
        (add-to-list 'LaTeX-enumitem-newlist-list-item-arg
                     (list env))))))

(add-hook 'TeX-auto-prepare-hook #'LaTeX-enumitem-auto-prepare t)
(add-hook 'TeX-auto-cleanup-hook #'LaTeX-enumitem-auto-cleanup t)

;; Again, thanks to Tassilo Horn for his comments on this one.
(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)
  (LaTeX-insert-environment
   env
   (let ((opts (TeX-read-key-val t LaTeX-enumitem-key-val-options-local)))
     (when (and opts (not (string-equal opts "")))
       (format "[%s]" opts))))
  (if (TeX-active-mark)
      (progn
        (LaTeX-find-matching-begin)
        (end-of-line 1))
    (end-of-line 0))
  (delete-char 1)
  (when (looking-at (concat "^[ \t]+$\\|"
                            "^[ \t]*" TeX-comment-start-regexp "+[ \t]*$"))
    (delete-region (point) (line-end-position)))
  (delete-horizontal-space)
  ;; Deactivate the mark here in order to prevent `TeX-parse-macro'
  ;; from swapping point and mark and the \item ending up right after
  ;; \begin{...}.
  (TeX-deactivate-mark)
  (LaTeX-insert-item)
  ;; The inserted \item may have outdented the first line to the
  ;; right.  Fill it, if appropriate.
  (when (and (not (looking-at "$"))
             (not (assoc environment LaTeX-indent-environment-list))
             (> (- (line-end-position) (line-beginning-position))
                (current-fill-column)))
    (LaTeX-fill-paragraph nil)))

(defun LaTeX-arg-SetEnumitemKey (optional &optional prompt)
  "Ask for a new key to be defined and add it to
`LaTeX-enumitem-key-val-options-local'."
  (LaTeX-enumitem-update-key-val-options)
  (let ((key     (read-string "New Key: "))
        (replace (TeX-read-key-val optional
                                   LaTeX-enumitem-key-val-options-local 
"Replacement")))
    (TeX-argument-insert key     optional)
    (TeX-argument-insert replace optional)
    (add-to-list 'LaTeX-enumitem-key-val-options-local (list key))
    (LaTeX-add-enumitem-SetEnumitemKeys key)))

(defun LaTeX-arg-SetEnumitemValue (optional &optional prompt)
  "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 (TeX-read-key-val optional
LaTeX-enumitem-key-val-options-local "Key"))
         (val (read-string "String value: "))
         ;; (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 (null val-match)
        (add-to-list 'opts (list key (list val)))
      (add-to-list 'opts (list key
                               (delete-dups (apply 'append (list val) 
val-match)))))
    (setq LaTeX-enumitem-key-val-options-local (copy-alist opts))
    (TeX-argument-insert key optional)
    (TeX-argument-insert val optional)))

(defun LaTeX-enumitem-update-key-val-options ()
  "Update the buffer-local key-val options before offering them
in `enumitem'-completions."
  (unless (null LaTeX-enumitem-SetEnumitemKey-list)
    (dolist (key (apply 'append LaTeX-enumitem-SetEnumitemKey-list))
      (add-to-list 'LaTeX-enumitem-key-val-options-local (list key))))
  (unless (null LaTeX-enumitem-SetEnumitemValue-list)
    (dolist (keyvals (apply 'append LaTeX-enumitem-SetEnumitemValue-list))
      (let* ((key (car keyvals))
             (val (cadr 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 (null val-match)
            (add-to-list 'opts (list key (list val)))
          (add-to-list 'opts
                       (list key (delete-dups (apply 'append (list val) 
val-match)))))
        (setq LaTeX-enumitem-key-val-options-local (copy-alist opts))))))


(TeX-add-style-hook
 "enumitem"
 (lambda ()

   ;; Add enumitem to the parser.
   (TeX-auto-add-regexp LaTeX-enumitem-newlist-regexp)
   (TeX-auto-add-regexp LaTeX-enumitem-SetEnumitemKey-regexp)
   (TeX-auto-add-regexp LaTeX-enumitem-SetEnumitemValue-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 starred versions to the local list.
   (when (LaTeX-provided-package-options-member "enumitem" "inline")
     (setq LaTeX-enumitem-newlist-list-local
           (append '(("itemize*") ("enumerate*") ("description*"))
                   LaTeX-enumitem-newlist-list-local)))

   ;; Now add the parsed env's to the local list.
   (setq LaTeX-enumitem-newlist-list-local
         (append LaTeX-enumitem-newlist-list
                 LaTeX-enumitem-newlist-list-local))

   ;; Move parsed description like env's into a local variable.
   (setq LaTeX-enumitem-newlist-list-item-arg-local
         LaTeX-enumitem-newlist-list-item-arg)

   ;; Tell AUCTeX about special items parsed
   (dolist (env LaTeX-enumitem-newlist-list-item-arg-local)
    (add-to-list 'LaTeX-item-list `(,(car env) . LaTeX-item-argument)))

   ;; Standard env's take key-val as optional argument.
   (LaTeX-add-environments
    '("itemize"      LaTeX-enumitem-env-with-opts)
    '("enumerate"    LaTeX-enumitem-env-with-opts)
    '("description"  LaTeX-enumitem-env-with-opts))

   ;; Make inline env's available with package option "inline"
   (when (LaTeX-provided-package-options-member "enumitem" "inline")
     (LaTeX-add-environments
      '("itemize*"     LaTeX-enumitem-env-with-opts)
      '("enumerate*"   LaTeX-enumitem-env-with-opts)
      '("description*" LaTeX-enumitem-env-with-opts))
     (add-to-list 'LaTeX-item-list '("description*" . LaTeX-item-argument)))

   ;; Cloning lists
   (TeX-add-symbols
    ;; The easy way would be:
    ;; '("newlist"
    ;;   "Name" (TeX-arg-eval
    ;;           completing-read "Type: "
    ;;                 '(("itemize")  ("enumerate")  ("description")
    ;;                   ("itemize*") ("enumerate*") ("description*")))
    ;;  "Max-depth")
    ;; But we go the extra mile to improve the user experience and add
    ;; the arguments directly to appropriate lists.
    ;; \newlist{<name>}{<type>}{<max-depth>}
    '("newlist"
      (TeX-arg-eval
       (lambda ()
         (let ((name (read-string "Name: "))
               (type (completing-read
                      "Type: "
                      '(("itemize")  ("enumerate")  ("description")
                        ("itemize*") ("enumerate*") ("description*"))))
               (depth (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)))
           (LaTeX-add-environments `(,name LaTeX-enumitem-env-with-opts))
           (insert (format "{%s}" name)
                   (format "{%s}" type))
           (format "%s" depth)))))

    ;; \renewlist{<name>}{<type>}{<max-depth>}
    '("renewlist"
      (TeX-arg-eval completing-read "Name: "
                    LaTeX-enumitem-newlist-list-local)
      (TeX-arg-eval completing-read "Type: "
                    '(("itemize")  ("enumerate")  ("description")
                      ("itemize*") ("enumerate*") ("description*")))
      "Max-depth")

    ;; \setlist{<names,levels>}{<key-vals>}
    '("setlist"
      [TeX-arg-eval mapconcat 'identity
                    (TeX-completing-read-multiple
                     "Environment(s), level(s): "
                     `(,@LaTeX-enumitem-newlist-list-local
                       ("1") ("2") ("3") ("4"))) ","]
      ;; (TeX-arg-key-val LaTeX-enumitem-key-val-options)
      (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*{<names,levels>}{<key-vals>}
    '("setlist*"
      [TeX-arg-eval mapconcat 'identity
                    (TeX-completing-read-multiple
                     "Environment, level: "
                     `(,@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))))) )

   ;; General commands:
   (TeX-add-symbols

    ;; Ask for an Integer and insert it.
    '("setlistdepth" "Integer")

    ;; Just add the braces and let the user do the rest.
    '("AddEnumerateCounter" 3)

    ;; This command only makes sense for enumerate type environments.
    ;; Currently, we offer all defined env's -- to be improved
    ;; sometimes.
    '("restartlist"
      (TeX-arg-eval completing-read "List name: "
                    LaTeX-enumitem-newlist-list-local))

    ;; "Key" will be parsed and added to key-val list.
    '("SetEnumitemKey" LaTeX-arg-SetEnumitemKey)

    ;; "Key" and "Value" are added to our key-val list
    '("SetEnumitemValue" LaTeX-arg-SetEnumitemValue "Replacement"))

   ;; Setting enumerate short label
   (when (LaTeX-provided-package-options-member "enumitem" "shortlabels")
     (TeX-add-symbols
      '("SetEnumerateShortLabel"
        (TeX-arg-eval completing-read "Key: "
                      '(("A") ("a") ("I") ("i") ("1")))
        "Replacement")))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("newlist"             "{{{")
                                ("renewlist"           "{{{")
                                ("setlist"             "*[{")
                                ("AddEnumerateCounter" "{{{")
                                ("SetEnumitemKey"      "{{" )
                                ("SetEnumitemValue"    "{{{"))
                              'function)
     (font-latex-add-keywords '(("restartlist"            "{" )
                                ("setlistdepth"           "{" )
                                ("SetEnumerateShortLabel" "{{"))
                              'variable)))
 LaTeX-dialect)

(defvar LaTeX-enumitem-package-options
  '("inline" "ignoredisplayed" "shortlabels" "loadonly")
  "Package options for the enumitem package.")
--8<---------------cut here---------------end--------------->8---



--8<---------------cut here---------------start------------->8---
(defvar LaTeX-caption-key-val-options
  '(("aboveskip")
    ("belowskip")
    ("font"   ("scriptsize" "footnotesize" "small" "normalsize" "large"
               "Large" "normalfont" "up" "it" "sl" "sc" "md" "bf" "rm"
               "sf" "tt" "singlespacing" "onehalfspacing" "doublespacing"
               "stretch" "normalcolor" "color" "normal"))
    ("font+"  ("scriptsize" "footnotesize" "small" "normalsize" "large"
               "Large" "normalfont" "up" "it" "sl" "sc" "md" "bf" "rm"
               "sf" "tt" "singlespacing" "onehalfspacing" "doublespacing"
               "stretch" "normalcolor" "color" "normal"))
    ("format" ("plain" "hang"))
    ("hangindent")
    ("hypcap")
    ("hypcapspace")
    ("indention")
    ("justification" ("justified" "centering" "centerlast" "centerfirst"
                      "raggedright" "RaggedRight" "raggedleft"))
    ("labelfont"     ("scriptsize" "footnotesize" "small" "normalsize" "large"
                      "Large" "normalfont" "up" "it" "sl" "sc" "md" "bf" "rm"
                      "sf" "tt" "singlespacing" "onehalfspacing" "doublespacing"
                      "stretch" "normalcolor" "color" "normal"))
    ("labelfont+"    ("scriptsize" "footnotesize" "small" "normalsize" "large"
                      "Large" "normalfont" "up" "it" "sl" "sc" "md" "bf" "rm"
                      "sf" "tt" "singlespacing" "onehalfspacing" "doublespacing"
                      "stretch" "normalcolor" "color" "normal"))
    ("labelformat"   ("default" "empty" "simple" "brace" "parens"))
    ("labelsep"      ("none" "colon" "period" "space" "quad" "newline"
"endash"))
    ("list"          ("false" "no" "off" "0" "true" "yes" "on" "1"))
    ("listformat"    ("empty" "simple" "paren" "subsimple" "subparens"))
    ("margin"        ("type*" "width"))
    ("margin*")
    ("maxmargin")
    ("minmargin")
    ("name")
    ("oneside")
    ("parindent")
    ("parskip")
    ("position"        ("top" "above" "bottom" "below" "auto"))
    ("singlelinecheck" ("false" "no" "off" "0" "true" "yes" "on" "1"))
    ("skip")
    ("strut"      ("false" "no" "off" "0" "true" "yes" "on" "1"))
    ("style"      ("base" "default"))
    ("textfont"   ("scriptsize" "footnotesize" "small" "normalsize" "large"
                   "Large" "normalfont" "up" "it" "sl" "sc" "md" "bf" "rm"
                   "sf" "tt" "singlespacing" "onehalfspacing" "doublespacing"
                   "stretch" "normalcolor" "color" "normal"))
    ("textfont+"  ("scriptsize" "footnotesize" "small" "normalsize" "large"
                   "Large" "normalfont" "up" "it" "sl" "sc" "md" "bf" "rm"
                   "sf" "tt" "singlespacing" "onehalfspacing" "doublespacing"
                   "stretch" "normalcolor" "color" "normal"))
    ("textformat" ("empty" "simple" "period"))
    ("twoside")
    ("type"       ("figure" "table" "ContinuedFloat"))
    ("type*"      ("figure" "table" "ContinuedFloat"))
    ("width"))
  "Key=value options for caption macros.")

(defvar LaTeX-caption-key-val-options-local nil
  "Buffer-local key=value options for caption macros.")
(make-variable-buffer-local 'LaTeX-caption-key-val-options-local)

(defvar LaTeX-caption-supported-float-types
  '("figure" "table" "ContinuedFloat"   ; Standard caption.sty
    "ruled" "boxed"                     ; float.sty
    "floatingfigure" "floatingtable"    ; floatflt.sty
    "lstlisting"                        ; listings.sty
    "longtable"                         ; longtable.sty
    "figwindow" "tabwindow"             ; picinpar.sty
    "parpic"                            ; picins.sty
    "SCfigure" "SCtable"                ; sidecap.sty
    "supertabular" "xtabular"           ; supertabular.sty & xtab.sty
    "threeparttable" "measuredfigure"   ; threeparttable.sty
    "wrapfigure" "wraptable")           ; wrapfigure
  "List of float types provided by other LaTeX packages and
supported by `caption.sty'.")

;; Needed for auto-parsing.
(require 'tex)

;; Setup for \DeclareCaption*:
(TeX-auto-add-type "caption-DeclareCaption" "LaTeX")

(defvar LaTeX-caption-DeclareCaption-regexp
  `(,(concat "\\\\DeclareCaption\\(Font\\|Format\\|Justification"
             "\\|LabelFormat\\|LabelSeparator\\|ListFormat"
             "\\|Style\\|TextFormat\\|Option\\)"
             "\\*?"
             "{\\([^}]+\\)}")
    (1 2) LaTeX-auto-caption-DeclareCaption)
  "Matches the arguments of different `\\DeclareCaption*' from
`caption' package.")

;; Plug it into the machinery.
(defun LaTeX-caption-auto-prepare ()
  "Clear `LaTeX-auto-caption-DeclareCaption' before parsing."
  (setq LaTeX-auto-caption-DeclareCaption nil))

;; The following code is not necessary --
;; `LaTeX-add-caption-DeclareCaptions' does the right thing so this is
;; commented out.

;; (defun LaTeX-caption-auto-cleanup ()
;;   "Move parsing results into `LaTeX-caption-DeclareCaption-list'."
;;   ;; \DeclareCaption<format>{<name>}
;;   (dolist (format-name LaTeX-auto-caption-DeclareCaption)
;;     (let* ((format (car format-name))
;;         (name (cdr format-name)))
;;       (add-to-list 'LaTeX-caption-DeclareCaption-list (list format name)))))

(add-hook 'TeX-auto-prepare-hook #'LaTeX-caption-auto-prepare t)
;; (add-hook 'TeX-auto-cleanup-hook #'LaTeX-caption-auto-cleanup t)

(defun LaTeX-caption-update-key-val-options ()
  "Update the buffer-local key-val options before offering them
in `caption'-completions."
  (unless (null LaTeX-caption-DeclareCaption-list)
    (dolist (keyvals (apply 'append LaTeX-caption-DeclareCaption-list))
      (let* ((key (cond ((string-equal (car keyvals) "LabelSeparator")
                         (downcase (substring (car keyvals)  0 8)))
                        (t (downcase (car keyvals)))))
             (val (cadr keyvals))
             ;; (key-match (car (assoc key 
LaTeX-caption-key-val-options-local)))
             (val-match (cdr (assoc key LaTeX-caption-key-val-options-local)))
             (temp  (copy-alist LaTeX-caption-key-val-options-local))
             (opts (assq-delete-all (car (assoc key temp)) temp)))
        ;; For `\DeclareCaptionOption', only add the value to the list
        ;; (remember:      key=^^^^^^, val="defined key")
        (when (and (string-equal key "option") (null val-match))
          (add-to-list 'opts (list val)))
        ;; For anything but `\DeclareCaptionOption', do the standard procedure
        (unless (string-equal key "option")
          (if (null val-match)
              (add-to-list 'opts (list key (list val)))
            (add-to-list 'opts
                         (list key (delete-dups (apply 'append (list val) 
val-match))))))
        (setq LaTeX-caption-key-val-options-local (copy-alist opts))))))

(defun LaTeX-arg-caption-command (&optional optional prompt)
  "Insert caption-commands from `caption.sty'. If OPTIONAL is non-nil,
indicate `Optional' while reading key=val.  PROMPT replaces the
standard one."
  (LaTeX-caption-update-key-val-options)
  (let ((opts (TeX-read-key-val optional
                                LaTeX-caption-key-val-options-local
                                prompt)))
    (format "%s" opts)))

(defun LaTeX-arg-caption-DeclareCaption (format)
  "Insert various `\\DeclareCaptionFORMAT' commands.  FORMAT is
the suffix of the command."
  (let ((name  (read-string "Name: "))
        (form  (when (stringp format) format)))
    (LaTeX-add-caption-DeclareCaptions `(,format ,name))
    (format "%s" name)))

(TeX-add-style-hook
 "caption"
 (lambda ()

   ;; Add caption to the parser.
   (TeX-auto-add-regexp LaTeX-caption-DeclareCaption-regexp)

   ;; Activate the buffer-local version of key-vals.
   (setq LaTeX-caption-key-val-options-local
         (copy-alist LaTeX-caption-key-val-options))

   ;; Caption commands:
   (TeX-add-symbols
    '("caption*" t)

    '("captionlistentry"
      [ (TeX-arg-eval completing-read "Float type: "
                      LaTeX-caption-supported-float-types) ]
      t)

    '("captionof"
      (TeX-arg-eval completing-read "Float type: "
                    LaTeX-caption-supported-float-types) ["List entry"] t)

    '("captionof*"
      (TeX-arg-eval completing-read "Float type: "
                    LaTeX-caption-supported-float-types) ["List entry"] t)

    '("captionsetup"
      [ TeX-arg-eval completing-read "(Optional) Float type: "
                     LaTeX-caption-supported-float-types ]
      (TeX-arg-eval LaTeX-arg-caption-command))

    '("captionsetup*"
      [ TeX-arg-eval completing-read "(Optional) Float type: "
                     LaTeX-caption-supported-float-types ]
      (TeX-arg-eval LaTeX-arg-caption-command))

    '("clearcaptionsetup"
      [ (TeX-arg-eval LaTeX-arg-caption-command t "Key") ]
      (TeX-arg-eval completing-read "Float type: "
                    LaTeX-caption-supported-float-types))

    '("clearcaptionsetup*"
      [ (TeX-arg-eval LaTeX-arg-caption-command t "Key") ]
      (TeX-arg-eval completing-read "Float type: "
                    LaTeX-caption-supported-float-types))

    '("ContinuedFloat" 0)

    '("DeclareCaptionFont"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "Font") t)

    '("DeclareCaptionFormat"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "Format") t)

    '("DeclareCaptionFormat*"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "Format") t)

    '("DeclareCaptionJustification"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "Justification") t)

    '("DeclareCaptionLabelSeparator"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "Separator") t)

    '("DeclareCaptionLabelSeparator*"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "Separator") t)

    '("DeclareCaptionOption"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "Option") t)

    '("DeclareCaptionStyle"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "Style")
      [ TeX-arg-eval LaTeX-arg-caption-command t "Additional options" ]
      (TeX-arg-eval LaTeX-arg-caption-command nil "Options"))

    '("DeclareCaptionTextFormat"
      (TeX-arg-eval LaTeX-arg-caption-DeclareCaption "TextFormat") t)

    '("bothIfFirst" 2)

    '("bothIfSecond" 2))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("caption"           "*[{")
                                ("captionlistentry"  "[{")
                                ("captionof"         "*[{"))
                              'textual)
     (font-latex-add-keywords '(("captionsetup"                  "*[{")
                                ("clearcaptionsetup"             "*[{")
                                ("DeclareCaptionFont"            "{{")
                                ("DeclareCaptionFormat"          "*{{")
                                ("DeclareCaptionJustification"   "{{")
                                ("DeclareCaptionLabelSeparator"  "*{{")
                                ("DeclareCaptionOption"          "{{")
                                ("DeclareCaptionStyle"           "{[{")
                                ("DeclareCaptionTextFormat"      "{{"))
                              'function)) )
 LaTeX-dialect)

(defun LaTeX-caption-package-options ()
  "Prompt for package options for the caption package."
  (TeX-read-key-val
   t
   (append '(("compatibility"  ("true" "false")))
           '(("figureposition" ("top" "above" "bottom" "below")))
           '(("tableposition"  ("top" "above" "bottom" "below")))
           LaTeX-caption-key-val-options)))
--8<---------------cut here---------------end--------------->8---




reply via email to

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