"Fossies" - the Fresh Open Source Software Archive

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

    1 ;;; listings.el --- AUCTeX style for `listings.sty'
    2 
    3 ;; Copyright (C) 2004, 2005, 2009, 2013-2020 Free Software Foundation, Inc.
    4 
    5 ;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
    6 ;; Maintainer: auctex-devel@gnu.org
    7 ;; Created: 2004-10-17
    8 ;; Keywords: tex
    9 
   10 ;; This file is part of AUCTeX.
   11 
   12 ;; AUCTeX is free software; you can redistribute it and/or modify it
   13 ;; under the terms of the GNU General Public License as published by
   14 ;; the Free Software Foundation; either version 3, or (at your option)
   15 ;; any later version.
   16 
   17 ;; AUCTeX is distributed in the hope that it will be useful, but
   18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
   19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   20 ;; General Public License for more details.
   21 
   22 ;; You should have received a copy of the GNU General Public License
   23 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
   24 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   25 ;; 02110-1301, USA.
   26 
   27 ;;; Commentary:
   28 
   29 ;; This file adds support for `listings.sty'.
   30 ;;
   31 ;; May 2015: The style detects new environments defined with
   32 ;; `\lstnewenvironment'.  Users need to invoke `C-c C-n' for this.
   33 ;;
   34 ;; October 2015: The style detects new "styles" defined with
   35 ;; `\lstdefinestyle' and offers them during key-value query.
   36 ;;
   37 ;; January 2017: Put label in opt. argument of environment.
   38 ;;
   39 ;; October 2018: Extract label context for RefTeX.
   40 ;;
   41 ;; FIXME: Please make me more sophisticated!
   42 
   43 ;;; Code:
   44 
   45 ;; Needed for compiling `cl-pushnew':
   46 (eval-when-compile
   47   (require 'cl-lib))
   48 
   49 ;; Needed for auto-parsing:
   50 (require 'tex)
   51 (require 'latex)
   52 
   53 ;; Silence the compiler:
   54 (declare-function font-latex-add-keywords
   55           "font-latex"
   56           (keywords class))
   57 
   58 (declare-function font-latex-set-syntactic-keywords
   59           "font-latex")
   60 
   61 ;; The following are options taken from chapter 4 of the listings
   62 ;; manual (2007/02/22 Version 1.4).
   63 (defvar LaTeX-listings-key-val-options
   64   '(;; Space and placement
   65     ("float" ("t" "b" "p" "h")) ; Support [*] as an optional prefix and that
   66                 ; tbph are not exclusive.
   67     ("floatplacement" ("t" "b" "p" "h"))
   68     ("aboveskip")
   69     ("belowskip")
   70     ("lineskip")
   71     ("boxpos" ("b" "c" "t"))
   72     ;; The printed range
   73     ("print" ("true" "false"))
   74     ("firstline")
   75     ("lastline")
   76     ("linerange")
   77     ("consecutivenumbers" ("true" "false"))
   78     ("showlines" ("true" "false"))
   79     ("emptylines")
   80     ("gobble")
   81     ;; Languages and styles
   82     ("style")
   83     ("language")
   84     ("alsolanguage")
   85     ("defaultdialect")
   86     ("printpod" ("true" "false"))
   87     ("usekeywordsintag" ("true" "false"))
   88     ("tagstyle")
   89     ("markfirstintag")
   90     ("makemacrouse" ("true" "false"))
   91     ;; Figure out the appearance
   92     ("basicstyle")
   93     ("identifierstyle")
   94     ("commentstyle")
   95     ("stringstyle")
   96     ("keywordstyle")
   97     ("classoffset")
   98     ("texcsstyle")
   99     ("directivestyle")
  100     ("emph")
  101     ("moreemph")
  102     ("deleteemph")
  103     ("emphstyle")
  104     ("delim")
  105     ("moredelim")
  106     ("deletedelim")
  107     ;; Getting all characters right
  108     ("extendedchars" ("true" "false"))
  109     ("inputencoding") ; Could make use of `latex-inputenc-coding-alist'.
  110     ("upquote" ("true" "false"))
  111     ("tabsize")
  112     ("showtabs" ("true" "false"))
  113     ("tab")
  114     ("showspaces" ("true" "false"))
  115     ("showstringspaces" ("true" "false"))
  116     ("formfeed")
  117     ;; Line numbers
  118     ("numbers" ("none" "left" "right"))
  119     ("stepnumber")
  120     ("numberfirstline" ("true" "false"))
  121     ("numberstyle")
  122     ("numbersep")
  123     ("numberblanklines" ("true" "false"))
  124     ("firstnumber" ("auto" "last")) ; Can also take a number.
  125     ("name")
  126     ;; Captions
  127     ("title")
  128     ("caption") ; Insert braces?
  129     ;; Label is inserted as part of environment insertion; see below
  130     ;; for "lstlisting" in style hook
  131     ;; ("label")
  132     ("nolol" ("true" "false"))
  133     ("numberbychapter" ("true" "false"))
  134     ("captionpos" ("t" "b")) ; Can be a subset of tb.
  135     ("abovecaptionskip")
  136     ("belowcaptionskip")
  137     ;; Margins and line shape
  138     ("linewidth")
  139     ("xleftmargin")
  140     ("xrightmargin")
  141     ("resetmargins" ("true" "false"))
  142     ("breaklines" ("true" "false"))
  143     ("breakatwhitespace" ("true" "false"))
  144     ("prebreak")
  145     ("postbreak")
  146     ("breakindent")
  147     ("breakautoindent" ("true" "false"))
  148     ;; Frames
  149     ("frame" ("none" "leftline" "topline" "bottomline" "lines" "single"
  150           "shadowbox"
  151           ;; Alternative to the above values.  A subset of trblTRBL can be
  152           ;; given.
  153           "t" "r" "b" "l" "T" "R" "B" "L"))
  154     ("frameround" ("t" "f")) ; The input actually has to be four times {t,f}.
  155     ("framesep")
  156     ("rulesep")
  157     ("framerule")
  158     ("framexleftmargin")
  159     ("framexrightmargin")
  160     ("framextopmargin")
  161     ("framebottommargin")
  162     ("backgroundcolor")
  163     ("rulecolor")
  164     ("fillcolor")
  165     ("fulesepcolor")
  166     ("frameshape")
  167     ;; Indexing
  168     ("index")
  169     ("moreindex")
  170     ("deleteindex")
  171     ("indexstyle")
  172     ;; Column alignment
  173     ("columns" ("fixed" "flexible" "fullflexible" "spaceflexible")) ;
  174                     ; Also supports an optional
  175                     ; argument with {c,l,r}.
  176     ("flexiblecolumns" ("true" "false"))
  177     ("keepspaces" ("true" "false"))
  178     ("basewidth")
  179     ("fontadjust" ("true" "false"))
  180     ;; Escaping to LaTeX
  181     ("texcl" ("true" "false"))
  182     ("mathescape" ("true" "false"))
  183     ("escapechar")
  184     ("escapeinside")
  185     ("escapebegin")
  186     ("escapeend")
  187     ;; Interface to fancyvrb
  188     ("fancyvrb" ("true" "false"))
  189     ("fvcmdparams")
  190     ("morefvcmdparams")
  191     ;; Language definitions
  192     ("keywordsprefix")
  193     ("keywords")
  194     ("morekeywords")
  195     ("deletekeywords")
  196     ("texcs")
  197     ("moretexcs")
  198     ("deletetexcs")
  199     ("directives")
  200     ("moredirectives")
  201     ("deletedirectives")
  202     ("sensitive" ("true" "false"))
  203     ("alsoletter")
  204     ("alsodigit")
  205     ("alsoother")
  206     ("otherkeywords")
  207     ("tag")
  208     ("string")
  209     ("morestring")
  210     ("deletestring")
  211     ("comment")
  212     ("morecomment")
  213     ("deletecomment")
  214     ("keywordcomment")
  215     ("morekeywordcomment")
  216     ("deletekeywordcomment")
  217     ("keywordcommentsemicolon")
  218     ("podcomment" ("true" "false"))
  219     ;; The following are all options from chapter 5, which are
  220     ;; experimental
  221     ;; Export of identifiers
  222     ("procnamekeys")
  223     ("moreprocnamekeys")
  224     ("deleteprocnamekeys")
  225     ("procnamestyle")
  226     ("indexprocnames" ("true" "false"))
  227     ;; Hyperlink references
  228     ("hyperref")
  229     ("morehyperref")
  230     ("deletehyperref")
  231     ("hyperanchor")
  232     ("hyperlink")
  233     ;; Literate programming
  234     ("literate") ;; three arguments: replace,replacement text,length
  235     ;; LGrind definitions
  236     ("lgrindef")
  237     ;; Arbitrary linerange markers
  238     ("rangebeginprefix")
  239     ("rangebeginsuffix")
  240     ("rangeendprefix")
  241     ("rangeendsuffix")
  242     ("rangeprefix")
  243     ("rangesuffix")
  244     ("includerangemarker" ("true" "false"))
  245     ;; Multicolumn Listing
  246     ("multicolumn"))
  247   "Key=value options for listings macros and environments.")
  248 
  249 (defvar LaTeX-listings-key-val-options-local nil
  250   "Buffer-local Key=value options for listings macros and environments.")
  251 (make-variable-buffer-local 'LaTeX-listings-key-val-options-local)
  252 
  253 ;; Setup for \lstnewenvironment:
  254 (defvar LaTeX-auto-listings-lstnewenvironment nil
  255   "Temporary for parsing the arguments of `\\lstnewenvironment'
  256 from `listings' package.")
  257 
  258 (defvar LaTeX-listings-lstnewenvironment-regexp
  259   `(,(concat "\\\\lstnewenvironment"
  260          "[ \t\n\r]*{\\([A-Za-z0-9]+\\)}%?"
  261          "[ \t\n\r]*\\[?\\([0-9]?\\)\\]?%?"
  262          "[ \t\n\r]*\\(\\[\\)?")
  263     (1 2 3) LaTeX-auto-listings-lstnewenvironment)
  264   "Matches the argument of `\\lstnewenvironment' from `listings.sty'.")
  265 
  266 ;; Setup for \lstdefinestyle:
  267 (TeX-auto-add-type "listings-lstdefinestyle" "LaTeX")
  268 
  269 (defvar LaTeX-listings-lstdefinestyle-regexp
  270   '("\\\\lstdefinestyle{\\([^}]+\\)}"
  271     1 LaTeX-auto-listings-lstdefinestyle)
  272   "Matches the argument of \"\\lstdefinestyle\" from
  273 \"listings\" package.")
  274 
  275 ;; Setup for parsing the labels inside optional arguments:
  276 
  277 (defvar LaTeX-listings-key-val-label-regexp
  278   `(,(concat
  279       "\\\\begin{lstlisting}" (LaTeX-extract-key-value-label))
  280     1 LaTeX-auto-label)
  281   "Matches the label inside an optional argument after \\begin{lstlisting}.")
  282 
  283 (defun LaTeX-listings-update-style-key ()
  284   "Update the \"style\" key from `LaTeX-listings-key-val-options-local'
  285 with user-defined values via the \"lstdefinestyle\" macro."
  286   (let* ((elt (assoc "style" LaTeX-listings-key-val-options-local))
  287      (key (car elt))
  288      (temp (copy-alist LaTeX-listings-key-val-options-local))
  289      (opts (assq-delete-all (car (assoc key temp)) temp)))
  290     (cl-pushnew (list key (TeX-delete-duplicate-strings
  291                (mapcar #'car (LaTeX-listings-lstdefinestyle-list))))
  292         opts :test #'equal)
  293     (setq LaTeX-listings-key-val-options-local
  294       (copy-alist opts))))
  295 
  296 (defun LaTeX-listings-auto-prepare ()
  297   "Clear temporary variable from `listings.sty' before parsing."
  298   (setq LaTeX-auto-listings-lstnewenvironment nil)
  299   (setq LaTeX-auto-listings-lstdefinestyle    nil))
  300 
  301 (defun LaTeX-listings-auto-cleanup ()
  302   "Process the parsed results of \"\\lstnewenvironment\" and
  303 \"\\lstdefinestyle\"."
  304   (dolist (env-args LaTeX-auto-listings-lstnewenvironment)
  305     (let ((env  (car   env-args))
  306       (args (cadr  env-args))
  307       (opt  (nth 2 env-args)))
  308       (cond (;; opt. 1st argument and mandatory argument(s)
  309          (and args (not (string-equal args ""))
  310           opt  (not (string-equal opt  "")))
  311          (LaTeX-add-environments
  312           `(,env
  313         LaTeX-env-args
  314         [TeX-arg-key-val LaTeX-listings-key-val-options-local]
  315         (LaTeX-env-label-as-keyval "caption")
  316         ,(1- (string-to-number args)))))
  317         (;; mandatory argument(s) only
  318          (and args (not (string-equal args ""))
  319           (string-equal opt ""))
  320          (LaTeX-add-environments
  321           (list env (string-to-number args))))
  322         (t ; No args
  323          (LaTeX-add-environments (list env))))
  324       (add-to-list 'LaTeX-indent-environment-list `(,env current-indentation) t)
  325       (add-to-list 'LaTeX-verbatim-environments-local env t)
  326       (add-to-list 'LaTeX-label-alist `(,env . LaTeX-listing-label) t)
  327       ;; Add new env to parser for labels in opt. argument:
  328       (TeX-auto-add-regexp `(,(concat "\\\\begin{" env "}"
  329                       (LaTeX-extract-key-value-label))
  330                  1 LaTeX-auto-label))
  331       ;; Tell RefTeX
  332       (when (fboundp 'reftex-add-label-environments)
  333     (reftex-add-label-environments
  334      `((,env ?l "lst:" "~\\ref{%s}"
  335          LaTeX-listings-reftex-label-context-function
  336          (regexp "[Ll]isting")))))
  337       ;; Fontification
  338       (when (and (fboundp 'font-latex-add-keywords)
  339          (eq TeX-install-font-lock 'font-latex-setup))
  340     ;; Tell font-lock about the update.
  341     (font-latex-set-syntactic-keywords))
  342       ;; Add new env's to `ispell-tex-skip-alist': skip the entire env
  343       (TeX-ispell-skip-setcdr `(,(cons env (concat "\\\\end{" env "}"))))))
  344   (when (LaTeX-listings-lstdefinestyle-list)
  345     (LaTeX-listings-update-style-key)))
  346 
  347 (add-hook 'TeX-auto-prepare-hook #'LaTeX-listings-auto-prepare t)
  348 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-listings-auto-cleanup t)
  349 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
  350 
  351 (defun LaTeX-listings-reftex-label-context-function (env)
  352   "Extract and return a context string for RefTeX.
  353 The context string is the value given to the caption key.  If no
  354 caption key is found, an error is issued."
  355   (let* ((envstart (save-excursion
  356              (re-search-backward (concat "\\\\begin{" env "}")
  357                      nil t)))
  358      (capt-key (save-excursion
  359              (re-search-backward "caption[ \t\n\r%]*=[ \t\n\r%]*"
  360                      envstart t)))
  361      capt-start capt-end)
  362     (if capt-key
  363     (save-excursion
  364       (goto-char capt-key)
  365       (re-search-forward
  366        "caption[ \t\n\r%]*=[ \t\n\r%]*" nil t)
  367       (cond (;; Short caption inside [] is available, extract it only
  368          (looking-at-p (regexp-quote (concat TeX-grop LaTeX-optop)))
  369          (forward-char)
  370          (setq capt-start (1+ (point)))
  371          (setq capt-end (1- (progn (forward-sexp) (point)))))
  372         ;; Extract the entire caption which is enclosed in braces
  373         ((looking-at-p TeX-grop)
  374          (setq capt-start (1+ (point)))
  375          (setq capt-end (1- (progn (forward-sexp) (point)))))
  376         ;; Extract everything to next comma ,
  377         (t
  378          (setq capt-start (point))
  379          (setq capt-end (progn (skip-chars-forward "^,") (point)))))
  380       ;; Return the extracted string
  381       (buffer-substring-no-properties capt-start capt-end))
  382       (error "No caption found"))))
  383 
  384 (TeX-add-style-hook
  385  "listings"
  386  (lambda ()
  387 
  388    ;; Add to parser:
  389    (TeX-auto-add-regexp LaTeX-listings-lstnewenvironment-regexp)
  390    (TeX-auto-add-regexp LaTeX-listings-lstdefinestyle-regexp)
  391    (TeX-auto-add-regexp LaTeX-listings-key-val-label-regexp)
  392 
  393    ;; Local version of key-val options:
  394    (setq LaTeX-listings-key-val-options-local
  395      (copy-alist LaTeX-listings-key-val-options))
  396 
  397    ;; New symbols
  398    (TeX-add-symbols
  399     '("lstalias" ["Alias dialect"] "Alias" ["Dialect"] "Language")
  400     '("lstdefinestyle"
  401       (TeX-arg-eval
  402        (lambda ()
  403      (let ((name (TeX-read-string "Style name: ")))
  404        (LaTeX-add-listings-lstdefinestyles name)
  405        (LaTeX-listings-update-style-key)
  406        (format "%s" name))))
  407       (TeX-arg-key-val LaTeX-listings-key-val-options-local))
  408     '("lstinline" [TeX-arg-key-val LaTeX-listings-key-val-options-local]
  409       TeX-arg-verb-delim-or-brace)
  410     '("lstinputlisting" [TeX-arg-key-val LaTeX-listings-key-val-options-local]
  411       TeX-arg-file)
  412     "lstlistoflistings"
  413     '("lstnewenvironment" "Name" ["Number or arguments"] ["Default argument"]
  414       "Starting code" "Ending code")
  415     '("lstset" (TeX-arg-key-val LaTeX-listings-key-val-options-local))
  416     '("lstloadlanguages" t)
  417     ;; 4.17 Short Inline Listing Commands
  418     '("lstMakeShortInline" [ "Options" ] "Character")
  419     '("lstDeleteShortInline" "Character")
  420 
  421     "lstgrinddeffile" "lstaspectfiles" "lstlanguagefiles"
  422     "lstlistingname" "lstlistingnamestyle" "thelstlisting"
  423     "lstlistlistingname")
  424 
  425    ;; New environments
  426    (LaTeX-add-environments
  427     '("lstlisting" LaTeX-env-args
  428       [TeX-arg-key-val LaTeX-listings-key-val-options-local]
  429       (LaTeX-env-label-as-keyval "caption")))
  430 
  431    ;; Append "lstlisting" to `LaTeX-label-alist':
  432    (add-to-list 'LaTeX-label-alist '("lstlisting" . LaTeX-listing-label) t)
  433 
  434    ;; Filling
  435    (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
  436         '("lstlisting" current-indentation) t)
  437    (add-to-list 'LaTeX-verbatim-environments-local "lstlisting")
  438    (add-to-list 'LaTeX-verbatim-macros-with-delims-local "lstinline")
  439    (add-to-list 'LaTeX-verbatim-macros-with-braces-local "lstinline")
  440 
  441    ;; RefTeX support lstlistings environment via
  442    ;; `reftex-label-alist-builtin'.  We add the same thing here only
  443    ;; with our function as 5th element:
  444    (when (fboundp 'reftex-add-label-environments)
  445      (reftex-add-label-environments
  446       '(("lstlisting" ?l "lst:" "~\\ref{%s}"
  447      LaTeX-listings-reftex-label-context-function
  448      (regexp "[Ll]isting")))))
  449 
  450    ;; Fontification
  451    (when (and (fboundp 'font-latex-add-keywords)
  452           (eq TeX-install-font-lock 'font-latex-setup))
  453      (font-latex-add-keywords '(("lstnewenvironment" "{[[{{")) 'function)
  454      (font-latex-add-keywords '(("lstinputlisting" "[{")) 'reference)
  455      (font-latex-add-keywords '(("lstinline" "[")
  456                 ("lstlistoflistings" ""))
  457                   'textual)
  458      (font-latex-add-keywords '(("lstalias" "{{")
  459                 ("lstdefinestyle" "{{")
  460                 ("lstset" "{"))
  461                   'variable)))
  462  LaTeX-dialect)
  463 
  464 (defvar LaTeX-listings-package-options '("draft" "final" "savemem"
  465                      "noaspects"
  466                      ;; procnames is mentioned in
  467                      ;; Section 5.2
  468                      "procnames")
  469   "Package options for the listings package.")
  470 
  471 ;;; listings.el ends here