>From 1620aa6ede738004244dc0dd9f8749459771ddea Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Mon, 3 May 2021 17:10:04 +0200 Subject: [PATCH] Add styles for listings from tcolorbox.sty In particular, support the listings, listingsutf8, and minted libraries. Supported environments: - tcblisting - tcboutputlisting - new environments defined by [re]newtcblisting Supported macros: - tcbinputlisting - new macros defined by [re]newtcbinputlisting - tcbuselistingtext - tcbuselistinglisting - tcbusetemplisting * style/tcolorbox-tcblisting.el: New file with common style shared between the listings, listingsutf8, and minted libraries. * style/tcolorboxlib-listings.el: New file with options provided by the listings library. * style/tcolorbox-listingsutf8.el: New file with options provided by the listingsutf8 library. * style/tcolorboxlib-minted.el: New file with options provided by the minted library. --- style/tcolorbox-tcblisting.el | 335 +++++++++++++++++++++++++++++ style/tcolorboxlib-listings.el | 66 ++++++ style/tcolorboxlib-listingsutf8.el | 67 ++++++ style/tcolorboxlib-minted.el | 62 ++++++ 4 files changed, 530 insertions(+) create mode 100644 style/tcolorbox-tcblisting.el create mode 100644 style/tcolorboxlib-listings.el create mode 100644 style/tcolorboxlib-listingsutf8.el create mode 100644 style/tcolorboxlib-minted.el diff --git a/style/tcolorbox-tcblisting.el b/style/tcolorbox-tcblisting.el new file mode 100644 index 00000000..e0cce69c --- /dev/null +++ b/style/tcolorbox-tcblisting.el @@ -0,0 +1,335 @@ +;;; tcolorbox-tcblisting.el --- AUCTeX style for listings from `tcolorbox.sty' (v4.00) -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 Free Software Foundation, Inc. + +;; Author: Dario Gjorgjevski +;; Maintainer: auctex-devel@gnu.org +;; Created: 2021-05-03 +;; Keywords: tex + +;; This file is part of AUCTeX. + +;; AUCTeX is free software; you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; AUCTeX is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with AUCTeX; see the file COPYING. If not, write to the Free +;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +;; 02110-1301, USA. + +;;; Commentary: + +;; This file adds support for listings from `tcolorbox.sty'. It +;; contains the common style shared among the `listings', +;; `listingsutf8', and `minted' libraries. + +;;; Code: + +;; Needed for compiling `cl-pushnew': +(eval-when-compile + (require 'cl-lib)) + +(require 'tex) +(require 'latex) + +(defvar LaTeX-tcolorbox-keyval-options-local) +(defvar LaTeX-tcolorbox-keyval-options-full) +(defvar LaTeX-tcolorbox-init-options) +(declare-function font-latex-add-keywords "font-latex" (keywords class)) + +;; 16.6 Common Option Keys of all Libraries +(defvar LaTeX-tcolorbox-tcblisting-keyval-options + (append + '(("listing engine" ("listings" "minted")) + ("listing file") + ("comment") + ("comment style") + ("image comment") + ("tcbimage comment") + ("pdf comment") + ("pdf extension")) + (mapcar (lambda (type) + `(,(format "%s only" type))) + '("listing" "text" "comment")) + (mapcan (lambda (type) + (mapcan (lambda (prep) + `((,(format "listing %s %s" prep type)) + (,(format "%s %s listing" type prep)))) + '("and" "side" "outside" "above" "above*"))) + '("text" "comment"))) + "Key-value options for listings from `tcolorbox.sty'.") + +(defvar-local LaTeX-tcolorbox-tcblisting-keyval-options-local nil + "Buffer-local version of `LaTeX-tcolorbox-tcblisting-keyval-options'.") + +;; Setup for \newtcblisting: +(TeX-auto-add-type "tcolorbox-newtcblisting" "LaTeX") + +(defvar LaTeX-tcolorbox-newtcblisting-regexp + `(,(concat (regexp-quote TeX-esc) + "\\(re\\)?newtcblisting" + "[ \t\n\r%]*" + "\\(?:" + (LaTeX-extract-key-value-label 'none) + "\\)?" + "[ \t\n\r%]*" + "{\\([a-zA-Z0-9]+\\)}" + "[ \t\n\r%]*" + "\\(?:\\[\\([0-9]*\\)\\]\\)?" + "[ \t\n\r%]*" + "\\(\\[\\)?") + (2 3 4 1) LaTeX-auto-tcolorbox-newtcblisting) + "Matches the arguments of \\newtcblisting from `tcolorbox.sty'.") + +(defun LaTeX-tcolorbox-tcblisting-process-newtcblisting () + "Process new environments from \\newtcblisting." + (let (to-add to-delete name arg opt renew nargs) + (mapc + (lambda (entry) + (setq name (nth 0 entry) + arg (nth 1 entry) + opt (nth 2 entry) + renew (string= (nth 3 entry) "re")) + ;; when renewing, delete any old entries first + (when renew + (push name to-delete)) + (cond + ;; optional 1st argument and mandatory argument(s) + ((and arg (not (string= arg "")) + opt (not (string= opt ""))) + (setq nargs (1- (string-to-number arg))) + (push + `(,name LaTeX-env-args + [ TeX-arg-eval + TeX-read-key-val t + (append + LaTeX-tcolorbox-tcblisting-keyval-options-local + LaTeX-tcolorbox-keyval-options-local) ] + ,@(mapcar (lambda (i) + (format "Listing box arg %d/%d" i nargs)) + (number-sequence 1 nargs))) + to-add)) + ;; mandatory argument(s) only + ((and arg (not (string= arg "")) + opt (string= opt "")) + (setq nargs (string-to-number arg)) + (push + `(,name LaTeX-env-args + ,@(mapcar (lambda (i) + (format "Listing box arg %d/%d" i nargs)) + (number-sequence 1 nargs))) + to-add)) + ;; no arguments + (t + (push `(,name) to-add)))) + (LaTeX-tcolorbox-newtcblisting-list)) + (setq to-add (nreverse to-add) + to-delete (nreverse to-delete)) + (make-local-variable 'LaTeX-environment-list) + (make-local-variable 'LaTeX-indent-environment-list) + ;; delete the environments that are being renewed + (mapc + (lambda (name) + (setq LaTeX-environment-list (assoc-delete-all + name LaTeX-environment-list) + LaTeX-indent-environment-list (assoc-delete-all + name LaTeX-indent-environment-list) + LaTeX-verbatim-environments-local (assoc-delete-all + name LaTeX-verbatim-environments-local))) + to-delete) + ;; add the environments that are being added + (apply #'LaTeX-add-environments to-add) + (mapc + (lambda (name) + (cl-pushnew `(,name current-indentation) LaTeX-indent-environment-list) + (cl-pushnew name LaTeX-verbatim-environments-local)) + (mapcar #'car to-add)))) + +;; Setup for \newtcbinputlisting: +(TeX-auto-add-type "tcolorbox-newtcbinputlisting" "LaTeX") +(defvar LaTeX-tcolorbox-newtcbinputlisting-regexp + `(,(concat (regexp-quote TeX-esc) + "\\(re\\)?newtcbinputlisting" + "[ \t\n\r%]*" + "\\(?:" + (LaTeX-extract-key-value-label 'none) + "\\)?" + "[ \t\n\r%]*" + "{\\([a-zA-Z0-9]+\\)}" + "[ \t\n\r%]*" + "\\(?:\\[\\([0-9]*\\)\\]\\)?" + "[ \t\n\r%]*" + "\\(\\[\\)?") + (2 3 4 1) LaTeX-auto-tcolorbox-newtcbinputlisting) + "Matches the arguments of \\newtcbinputlisting from `tcolorbox.sty'.") + +(defun LaTeX-tcolorbox-tcblisting-process-newtcbinputlisting () + "Process new macros from \\newtcbinputlisting." + (let (to-add to-delete name arg opt renew nargs) + (mapc + (lambda (entry) + (setq name (nth 0 entry) + arg (nth 1 entry) + opt (nth 2 entry) + renew (string= (nth 3 entry) "re")) + ;; when renewing, delete any old entries first + (when renew + (push name to-delete)) + (cond + ;; optional 1st argument and mandatory argument(s) + ((and arg (not (string= arg "")) + opt (not (string= opt ""))) + (setq nargs (1- (string-to-number arg))) + (push + (cons + `(,name [ TeX-arg-eval + TeX-read-key-val t + (append + LaTeX-tcolorbox-tcblisting-keyval-options-local + LaTeX-tcolorbox-keyval-options-local) ] + ,@(mapcar (lambda (i) + (format "Listing box arg %d/%d" i nargs)) + (number-sequence 1 nargs))) + `(,name ,(concat "[" (make-string nargs ?{)))) + to-add)) + ;; mandatory argument(s) only + ((and arg (not (string= arg "")) + opt (string= opt "")) + (setq nargs (string-to-number arg)) + (push + (cons + `(,name ,@(mapcar (lambda (i) + (format "Listing box arg %d/%d" i nargs)) + (number-sequence 1 nargs))) + `(,name ,(make-string nargs ?{))) + to-add)) + ;; no arguments + (t + (push + (cons `(,name) + `(,name "")) + to-add)))) + (LaTeX-tcolorbox-newtcbinputlisting-list)) + (setq to-add (nreverse to-add) + to-delete (nreverse to-delete)) + (make-local-variable 'TeX-symbol-list) + ;; delete the macros that are being renewed + (mapc + (lambda (name) + (setq TeX-symbol-list (assoc-delete-all + name TeX-symbol-list))) + to-delete) + ;; add the macros that are being added + (apply #'TeX-add-symbols (mapcar #'car to-add)) + (when (and (featurep 'font-latex) + (eq TeX-install-font-lock 'font-latex-setup)) + (font-latex-add-keywords (mapcar #'cdr to-add) + 'function)))) + +(defun LaTeX-tcolorbox-tcblisting-auto-prepare () + "Clear various auto variables before parsing." + (setq LaTeX-auto-tcolorbox-newtcblisting nil + LaTeX-auto-tcolorbox-newtcbinputlisting nil)) + +(defun LaTeX-tcolorbox-tcblisting-auto-cleanup () + "Process the parsed results." + (LaTeX-tcolorbox-tcblisting-process-newtcblisting) + (LaTeX-tcolorbox-tcblisting-process-newtcbinputlisting)) + +(add-hook 'TeX-auto-prepare-hook #'LaTeX-tcolorbox-tcblisting-auto-prepare t) +(add-hook 'TeX-auto-cleanup-hook #'LaTeX-tcolorbox-tcblisting-auto-cleanup t) + +(TeX-add-style-hook + "tcolorbox-tcblisting" + (lambda () + (setq + ;; activate the buffer-local key-value options + LaTeX-tcolorbox-tcblisting-keyval-options-local + (copy-alist LaTeX-tcolorbox-tcblisting-keyval-options) + ;; append to `LaTeX-tcolorbox-keyval-options-full' + LaTeX-tcolorbox-keyval-options-full + (append (copy-alist LaTeX-tcolorbox-tcblisting-keyval-options-local) + (copy-alist LaTeX-tcolorbox-keyval-options-full))) + ;; add environments + (LaTeX-add-environments + '("tcblisting" LaTeX-env-args + (TeX-arg-eval + TeX-read-key-val nil (append + LaTeX-tcolorbox-tcblisting-keyval-options-local + LaTeX-tcolorbox-keyval-options-local))) + '("tcboutputlisting")) + (make-local-variable 'LaTeX-indent-environment-list) + (mapc (lambda (name) + (cl-pushnew name LaTeX-verbatim-environments-local) + (cl-pushnew `(,name current-indentation) LaTeX-indent-environment-list)) + '("tcblisting" "tcboutputlisting")) + ;; add macros + (TeX-add-symbols + '("tcbinputlisting" + (TeX-arg-eval + TeX-read-key-val nil (append + LaTeX-tcolorbox-tcblisting-keyval-options-local + LaTeX-tcolorbox-keyval-options-local))) + '("tcbuselistingtext") + '("tcbuselistinglisting") + '("tcbusetemplisting") + '("newtcblisting" + [ TeX-arg-key-val LaTeX-tcolorbox-init-options ] + "Name" + [ TeX-arg-define-macro-arguments ] + (TeX-arg-eval + TeX-read-key-val nil (append + LaTeX-tcolorbox-tcblisting-keyval-options-local + LaTeX-tcolorbox-keyval-options-local))) + '("renewtcblisting" + [ TeX-arg-key-val LaTeX-tcolorbox-init-options ] + (TeX-arg-eval completing-read + (TeX-argument-prompt nil nil "Listing box") + (LaTeX-tcolorbox-newtcblisting-list)) + [ TeX-arg-define-macro-arguments ] + (TeX-arg-eval + TeX-read-key-val nil (append + LaTeX-tcolorbox-tcblisting-keyval-options-local + LaTeX-tcolorbox-keyval-options-local))) + '("newtcbinputlisting" + [ TeX-arg-key-val LaTeX-tcolorbox-init-options ] + "Name" + [ TeX-arg-define-macro-arguments ] + (TeX-arg-eval + TeX-read-key-val nil (append + LaTeX-tcolorbox-tcblisting-keyval-options-local + LaTeX-tcolorbox-keyval-options-local))) + '("renewtcbinputlisting" + [ TeX-arg-key-val LaTeX-tcolorbox-init-options ] + (TeX-arg-eval completing-read + (TeX-argument-prompt nil nil "Listing box") + (LaTeX-tcolorbox-newtcblisting-list)) + [ TeX-arg-define-macro-arguments ] + (TeX-arg-eval + TeX-read-key-val nil (append + LaTeX-tcolorbox-tcblisting-keyval-options-local + LaTeX-tcolorbox-keyval-options-local)))) + (when (and (featurep 'font-latex) + (eq TeX-install-font-lock 'font-latex-setup)) + (font-latex-add-keywords '(("tcbinputlisting" "{") + ("tcbuselistingtext" "") + ("tcbuselistinglisting" "") + ("tcbusetemplisting" "") + ("newtcblisting" "[{[[{") + ("renewtcblisting" "[{[[{") + ("newtcbinputlisting" "[{[[{") + ("renewtcbinputlisting" "[{[[{")) + 'function)) + ;; add to the parser + (TeX-auto-add-regexp LaTeX-tcolorbox-newtcblisting-regexp) + (TeX-auto-add-regexp LaTeX-tcolorbox-newtcbinputlisting-regexp)) + TeX-dialect) + +;;; tcolorbox-tcblisting.el ends here diff --git a/style/tcolorboxlib-listings.el b/style/tcolorboxlib-listings.el new file mode 100644 index 00000000..08588b00 --- /dev/null +++ b/style/tcolorboxlib-listings.el @@ -0,0 +1,66 @@ +;;; tcolorboxlib-listings.el --- AUCTeX style for the `listings' library from `tcolorbox.sty' -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 Free Software Foundation, Inc. + +;; Author: Dario Gjorgjevski +;; Maintainer: auctex-devel@gnu.org +;; Created: 2021-05-03 +;; Keywords: tex + +;; This file is part of AUCTeX. + +;; AUCTeX is free software; you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; AUCTeX is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with AUCTeX; see the file COPYING. If not, write to the Free +;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +;; 02110-1301, USA. + +;;; Commentary: + +;; This file adds support for the `listings' library from +;; `tcolorbox.sty'. + +;;; Code: + +(require 'tex) +(require 'latex) + +(defvar LaTeX-tcolorbox-keyval-options-full) +(defvar LaTeX-tcolorbox-tcblisting-keyval-options-local) + +(defvar LaTeX-tcolorboxlib-listings-keyval-options + '(;; 16.3 Option Keys of the `listings' Library + ("listing options") + ("no listing options") + ("listing style") + ("listing inputencoding") + ("listing remove caption") + ("every listing line") + ("every listing line*")) + "Key-value options for the `listings' library from `tcolorbox.sty'.") + +(TeX-add-style-hook + "tcolorboxlib-listings" + (lambda () + (TeX-run-style-hooks "tcolorbox-tcblisting") + (setq + ;; append to `LaTeX-tcolorbox-keyval-options-full' + LaTeX-tcolorbox-keyval-options-full + (append (copy-alist LaTeX-tcolorboxlib-listings-keyval-options) + (copy-alist LaTeX-tcolorbox-keyval-options-full)) + ;; append to `LaTeX-tcolorbox-tcblisting-keyval-options-local' + LaTeX-tcolorbox-tcblisting-keyval-options-local + (append (copy-alist LaTeX-tcolorboxlib-listings-keyval-options) + (copy-alist LaTeX-tcolorbox-tcblisting-keyval-options-local)))) + TeX-dialect) + +;;; tcolorboxlib-listings.el ends here diff --git a/style/tcolorboxlib-listingsutf8.el b/style/tcolorboxlib-listingsutf8.el new file mode 100644 index 00000000..f4cf0d3f --- /dev/null +++ b/style/tcolorboxlib-listingsutf8.el @@ -0,0 +1,67 @@ +;;; tcolorboxlib-listingsutf8.el --- AUCTeX style for the `listingsutf8' library from `tcolorbox.sty' -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 Free Software Foundation, Inc. + +;; Author: Dario Gjorgjevski +;; Maintainer: auctex-devel@gnu.org +;; Created: 2021-05-03 +;; Keywords: tex + +;; This file is part of AUCTeX. + +;; AUCTeX is free software; you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; AUCTeX is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with AUCTeX; see the file COPYING. If not, write to the Free +;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +;; 02110-1301, USA. + +;;; Commentary: + +;; This file adds support for the `listingsutf8' library from +;; `tcolorbox.sty'. + +;;; Code: + +(require 'tex) +(require 'latex) + +(defvar LaTeX-tcolorbox-keyval-options-full) +(defvar LaTeX-tcolorbox-tcblisting-keyval-options-local) + +(defvar LaTeX-tcolorboxlib-listingsutf8-keyval-options + '(;; 16.4 Option Keys of the `listingsutf8' Library + ("listing options") + ("no listing options") + ("listing style") + ("listing inputencoding") + ("listing remove caption") + ("every listing line") + ("every listing line*") + ("listing utf8")) + "Key-value options for the `listingsutf8' library from `tcolorbox.sty'.") + +(TeX-add-style-hook + "tcolorboxlib-listingsutf8" + (lambda () + (TeX-run-style-hooks "tcolorbox-tcblisting") + (setq + ;; append to `LaTeX-tcolorbox-keyval-options-full' + LaTeX-tcolorbox-keyval-options-full + (append (copy-alist LaTeX-tcolorboxlib-listingsutf8-keyval-options) + (copy-alist LaTeX-tcolorbox-keyval-options-full)) + ;; append to `LaTeX-tcolorbox-tcblisting-keyval-options-local' + LaTeX-tcolorbox-tcblisting-keyval-options-local + (append (copy-alist LaTeX-tcolorboxlib-listingsutf8-keyval-options) + (copy-alist LaTeX-tcolorbox-tcblisting-keyval-options-local)))) + TeX-dialect) + +;;; tcolorboxlib-listingsutf8.el ends here diff --git a/style/tcolorboxlib-minted.el b/style/tcolorboxlib-minted.el new file mode 100644 index 00000000..8aed40b3 --- /dev/null +++ b/style/tcolorboxlib-minted.el @@ -0,0 +1,62 @@ +;;; tcolorboxlib-minted.el --- AUCTeX style for the `minted' library from `tcolorbox.sty' -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 Free Software Foundation, Inc. + +;; Author: Dario Gjorgjevski +;; Maintainer: auctex-devel@gnu.org +;; Created: 2021-05-03 +;; Keywords: tex + +;; This file is part of AUCTeX. + +;; AUCTeX is free software; you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; AUCTeX is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with AUCTeX; see the file COPYING. If not, write to the Free +;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +;; 02110-1301, USA. + +;;; Commentary: + +;; This file adds support for the `minted' library from +;; `tcolorbox.sty'. + +;;; Code: + +(require 'tex) +(require 'latex) + +(defvar LaTeX-tcolorbox-keyval-options-full) +(defvar LaTeX-tcolorbox-tcblisting-keyval-options-local) + +(defvar LaTeX-tcolorboxlib-minted-keyval-options + '(;; 16.5 Option Keys of the `minted' Library + ("minted language") + ("minted options") + ("minted style")) + "Key-value options for the `minted' library from `tcolorbox.sty'.") + +(TeX-add-style-hook + "tcolorboxlib-minted" + (lambda () + (TeX-run-style-hooks "tcolorbox-tcblisting") + (setq + ;; append to `LaTeX-tcolorbox-keyval-options-full' + LaTeX-tcolorbox-keyval-options-full + (append (copy-alist LaTeX-tcolorboxlib-minted-keyval-options) + (copy-alist LaTeX-tcolorbox-keyval-options-full)) + ;; append to `LaTeX-tcolorbox-tcblisting-keyval-options-local' + LaTeX-tcolorbox-tcblisting-keyval-options-local + (append (copy-alist LaTeX-tcolorboxlib-minted-keyval-options) + (copy-alist LaTeX-tcolorbox-tcblisting-keyval-options-local)))) + TeX-dialect) + +;;; tcolorboxlib-minted.el ends here -- 2.31.GIT