branch: externals/auctex commit 1b3f6e813326b65eb753c4be00bd74f7ae15963b Author: Arash Esbati <ar...@gnu.org> Commit: Arash Esbati <ar...@gnu.org>
Update style/listings.el to use `LaTeX-env-label-as-keyval' * style/listings.el (LaTeX-listings-key-val-options): Comment the "label" key. (LaTeX-listings-key-val-label-extract): (LaTeX-listings-key-val-label-regexp): New variable. (LaTeX-listings-auto-cleanup): Use `LaTeX-add-environments' instead of adding new enviroments to `LaTeX-auto-environment'. Use the function `LaTeX-env-label-as-keyval' to insert labels. Add parsed enviroments to AUCTeX parser with `TeX-auto-add-regexp'. ("listings"): Add `LaTeX-listings-key-val-label-regexp' to the parser. Use `LaTeX-env-label-as-keyval' for "lstlisting". --- style/listings.el | 66 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/style/listings.el b/style/listings.el index b2b8f90..e36af7a 100644 --- a/style/listings.el +++ b/style/listings.el @@ -1,6 +1,6 @@ ;;; listings.el --- AUCTeX style for `listings.sty' -;; Copyright (C) 2004, 2005, 2009, 2013-2016 Free Software Foundation, Inc. +;; Copyright (C) 2004, 2005, 2009, 2013-2017 Free Software Foundation, Inc. ;; Author: Ralf Angeli <ang...@iwi.uni-sb.de> ;; Maintainer: auctex-de...@gnu.org @@ -34,6 +34,8 @@ ;; October 2015: The style detects new "styles" defined with ;; `\lstdefinestyle' and offers them during key-value query. ;; +;; January 2017: Put label in opt. argument of environment. +;; ;; FIXME: Please make me more sophisticated! ;;; Code: @@ -108,7 +110,9 @@ ;; Captions ("title") ("caption") ; Insert braces? - ("label") + ;; Label is inserted as part of environment insertion; see below + ;; for "lstlisting" in style hook + ;; ("label") ("nolol" ("true" "false")) ("numberbychapter" ("true" "false")) ("captionpos" ("t" "b")) ; Can be a subset of tb. @@ -252,6 +256,27 @@ from `listings' package.") "Matches the argument of \"\\lstdefinestyle\" from \"listings\" package.") +;; Setup for parsing the labels inside optional arguments: + +(defvar LaTeX-listings-key-val-label-extract + (concat + "\\(?:\\[[^][]*" + "\\(?:{[^}{]*" + "\\(?:{[^}{]*" + "\\(?:{[^}{]*}[^}{]*\\)*" + "}[^}{]*\\)*" + "}[^][]*\\)*" + "label[ \t]*=[ \t]*{\\([^}]+\\)}" + "\\(?:[^]]*\\)*" + "\\]\\)") + "Helper regexp to extract the label out of optional argument.") + +(defvar LaTeX-listings-key-val-label-regexp + `(,(concat + "\\\\begin{lstlisting}" LaTeX-listings-key-val-label-extract) + 1 LaTeX-auto-label) + "Matches the label inside an optional argument after \\begin{lstlisting}.") + (defun LaTeX-listings-update-style-key () "Update the \"style\" key from `LaTeX-listings-key-val-options-local' with user-defined values via the \"lstdefinestyle\" macro." @@ -280,18 +305,36 @@ with user-defined values via the \"lstdefinestyle\" macro." (cond (;; opt. 1st argument and mandatory argument(s) (and args (not (string-equal args "")) opt (not (string-equal opt ""))) - (add-to-list 'LaTeX-auto-environment - (list env 'LaTeX-env-args (vector "argument") - (1- (string-to-number args))))) + (LaTeX-add-environments + `(,env + LaTeX-env-args + [TeX-arg-key-val LaTeX-listings-key-val-options-local] + (LaTeX-env-label-as-keyval "caption") + ,(1- (string-to-number args))))) (;; mandatory argument(s) only (and args (not (string-equal args "")) (string-equal opt "")) - (add-to-list 'LaTeX-auto-environment - (list env (string-to-number args)))) + (LaTeX-add-environments + (list env (string-to-number args)))) (t ; No args - (add-to-list 'LaTeX-auto-environment (list env)))) + (LaTeX-add-environments (list env)))) (add-to-list 'LaTeX-indent-environment-list `(,env current-indentation) t) - (add-to-list 'LaTeX-verbatim-environments-local env) + (add-to-list 'LaTeX-verbatim-environments-local env t) + (add-to-list 'LaTeX-label-alist (cons env LaTeX-listing-label) t) + ;; Add new env to parser for labels in opt. argument: + (TeX-auto-add-regexp `(,(concat "\\\\begin{" env "}" + LaTeX-listings-key-val-label-extract) + 1 LaTeX-auto-label)) + ;; Tell RefTeX + (when (fboundp 'reftex-add-label-environments) + (reftex-add-label-environments + `((,env ?l "lst:" "~\\ref{%s}" nil (regexp "[Ll]isting"))))) + ;; Fontification + (when (and (fboundp 'font-latex-add-keywords) + (fboundp 'font-latex-update-font-lock) + (eq TeX-install-font-lock 'font-latex-setup)) + ;; Tell font-lock about the update. + (font-latex-update-font-lock t)) ;; Add new env's to `ispell-tex-skip-alist': skip the entire env (TeX-ispell-skip-setcdr `(,(cons env (concat "\\\\end{" env "}")))))) (when (LaTeX-listings-lstdefinestyle-list) @@ -308,6 +351,7 @@ with user-defined values via the \"lstdefinestyle\" macro." ;; Add to parser: (TeX-auto-add-regexp LaTeX-listings-lstnewenvironment-regexp) (TeX-auto-add-regexp LaTeX-listings-lstdefinestyle-regexp) + (TeX-auto-add-regexp LaTeX-listings-key-val-label-regexp) ;; Local version of key-val options: (setq LaTeX-listings-key-val-options-local @@ -342,7 +386,9 @@ with user-defined values via the \"lstdefinestyle\" macro." ;; New environments (LaTeX-add-environments '("lstlisting" LaTeX-env-args - [TeX-arg-key-val LaTeX-listings-key-val-options-local])) + [TeX-arg-key-val LaTeX-listings-key-val-options-local] + (LaTeX-env-label-as-keyval "caption"))) + ;; Filling (add-to-list (make-local-variable 'LaTeX-indent-environment-list) '("lstlisting" current-indentation) t)