Function: org-latex-src-block--listings

org-latex-src-block--listings is a byte-compiled function defined in ox-latex.el.gz.

Signature

(org-latex-src-block--listings &key SRC-BLOCK INFO LANG CAPTION CAPTION-ABOVE-P LABEL NUM-START RETAIN-LABELS ATTRIBUTES FLOAT &allow-other-keys)

Documentation

Transcode a SRC-BLOCK element from Org to LaTeX, using listings.

LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES and FLOAT are extracted from SRC-BLOCK and INFO in org-latex-src-block.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ox-latex.el.gz
(cl-defun org-latex-src-block--listings
    (&key src-block info lang caption caption-above-p label num-start retain-labels attributes float &allow-other-keys)
  "Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
  (let ((lst-lang
         (or (cadr (assq (intern lang)
                         (plist-get info :latex-listings-langs)))
             lang))
        (caption-str
         (when caption
           (let ((main (org-export-get-caption src-block))
                 (secondary (org-export-get-caption src-block t)))
             (if (not secondary)
                 (format "{%s}" (org-export-data main info))
               (format "{[%s]%s}"
                       (org-export-data secondary info)
                       (org-export-data main info))))))
        (lst-opt (plist-get info :latex-listings-options)))
    (concat
     (format
      "\\begin{lstlisting}[%s]\n%s\\end{lstlisting}"
      ;; Options.
      (concat
       (org-latex--make-option-string
        (append
         lst-opt
         (cond
          ((and (not float) (plist-member attributes :float)) nil)
          ((string= "multicolumn" float) '(("float" "*")))
          ((and float (not (assoc "float" lst-opt)))
           `(("float" ,(plist-get info :latex-default-figure-position)))))
         (unless (plist-get info :latex-listings-src-omit-language)
           `(("language" ,lst-lang)))
         (when label
           `(("label" ,(org-latex--label src-block info))))
         (when caption-str
           `(("caption" ,caption-str)))
         (when caption-str
           ;; caption-above-p means captionpos is t(op)
           ;; else b(ottom)
           `(("captionpos" ,(if caption-above-p "t" "b"))))
         (cond ((assoc "numbers" lst-opt) nil)
               ((not num-start) '(("numbers" "none")))
               (t `(("firstnumber" ,(number-to-string (1+ num-start)))
                    ("numbers" "left"))))))
       (let ((local-options (plist-get attributes :options)))
         (and local-options (concat "," local-options))))
      ;; Source code.
      (let* ((code-info (org-export-unravel-code src-block))
             (max-width
              (apply 'max
                     (mapcar 'string-width
                             (org-split-string (car code-info) "\n")))))
        (org-export-format-code
         (car code-info)
         (lambda (loc _num ref)
           (concat
            loc
            (when ref
              ;; Ensure references are flushed to the right,
              ;; separated with 6 spaces from the widest line of
              ;; code
              (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
                      (format "(%s)" ref)))))
         nil (and retain-labels (cdr code-info))))))))