From 60fa8cf31a91836eccbc8b3ef6c2a9f533d2b57e Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Tue, 12 Oct 2010 23:20:28 +0100 Subject: [PATCH 1/3] LaTeX minted package for fontified source code export * org-exp.el (org-export-format-source-code-or-example): latex formatting of source code blocks using the minted package (org-export-plist-vars): Add :latex-minted property (org-export-latex-minted): Ensure variable is defined (org-export-latex-minted-langs): Ensure variable is defined * org-latex.el (org-export-latex-minted): new variable (org-export-latex-minted-langs): new variable (org-export-latex-minted-with-line-numbers): new variable (org-export-latex-minted): --- doc/org.texi | 25 +++++++++++---- lisp/org-exp.el | 81 ++++++++++++++++++++++++++++++----------------- lisp/org-latex.el | 46 +++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 36 deletions(-) diff --git a/doc/org.texi b/doc/org.texi index 801bf1210..088d43a35 100644 --- a/doc/org.texi +++ b/doc/org.texi @@ -8653,13 +8653,24 @@ Here is an example @cindex formatting source code, markup rules If the example is source code from a programming language, or any other text that can be marked up by font-lock in Emacs, you can ask for the example to -look like the fontified Emacs buffer@footnote{Currently this works for the -HTML backend, and requires the @file{htmlize.el} package version 1.34 or -later. It also works for LaTeX with the listings package, if you turn on the -option @code{org-export-latex-listings} and make sure that the listings -package is included by the LaTeX header.}. This is done with the @samp{src} -block, where you also need to specify the name of the major mode that should -be used to fontify the example: +look like the fontified Emacs buffer@footnote{This works automatically for +the HTML backend (it requires version 1.34 of the @file{htmlize.el} package, +which is distributed with Org.) Fontified code chunks in LaTeX can be +achieved using either the listings or the +@url{http://code.google.com/p/minted, minted,} package. To use listings, turn +on the variable @code{org-export-latex-listings} and ensure that the listings +package is included by the LaTeX header (e.g. by configuring +@code{org-export-latex-packages-alist}). See the listings documentation for +configuration options, including obtaining colored output. For minted it is +necessary to install the program @url{http://pygments.org, pygments}, in +addition to setting @code{org-export-latex-minted}, ensuring that the minted +package is included by the LaTeX header, and ensuring that the +@code{-shell-escape} option is passed to @file{pdflatex} (see +@code{org-latex-to-pdf-process}). See the documentation of the variables +@code{org-export-latex-listings} and @code{org-export-latex-minted} for +further details.}. This is done with the @samp{src} block, where you also +need to specify the name of the major mode that should be used to fontify the +example: @cindex #+BEGIN_SRC @example diff --git a/lisp/org-exp.el b/lisp/org-exp.el index f75cfb475..6486e46a6 100644 --- a/lisp/org-exp.el +++ b/lisp/org-exp.el @@ -607,6 +607,7 @@ table.el tables." (:TeX-macros "TeX" org-export-with-TeX-macros) (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments) (:latex-listings nil org-export-latex-listings) + (:latex-minted nil org-export-latex-minted) (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading) (:fixed-width ":" org-export-with-fixed-width) (:timestamps "<" org-export-with-timestamps) @@ -2208,6 +2209,8 @@ in the list) and remove property and value from the list in LISTVAR." (defvar org-export-latex-listings) ;; defined in org-latex.el (defvar org-export-latex-listings-langs) ;; defined in org-latex.el (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el +(defvar org-export-latex-minted) ;; defined in org-latex.el +(defvar org-export-latex-minted-langs) ;; defined in org-latex.el (defun org-export-format-source-code-or-example (backend lang code &optional opts indent caption) @@ -2337,35 +2340,55 @@ INDENT was the original indentation of the block." (setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt)) (concat "#+BEGIN_LaTeX\n" (org-add-props - (if org-export-latex-listings - (concat - (if lang - (let* - ((lang-sym (intern lang)) - (lstlang - (or (cadr - (assq - lang-sym - org-export-latex-listings-langs)) - lang))) - (format "\\lstset{language=%s}\n" lstlang)) - "\n") - (when (and caption - org-export-latex-listings-w-names) - (format "\n%s $\\equiv$ \n" - (replace-regexp-in-string - "_" "\\\\_" caption))) - "\\begin{lstlisting}\n" - rtn "\\end{lstlisting}\n") - (concat (car org-export-latex-verbatim-wrap) - rtn (cdr org-export-latex-verbatim-wrap))) - '(org-protected t org-example t)) - "#+END_LaTeX\n")) - ((eq backend 'ascii) - ;; This is not HTML or LaTeX, so just make it an example. - (setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt)) - (concat caption "\n" - "#+BEGIN_ASCII\n" + (cond + (org-export-latex-listings + (concat + (if lang + (let* + ((lang-sym (intern lang)) + (lstlang + (or (cadr + (assq + lang-sym + org-export-latex-listings-langs)) + lang))) + (format "\\lstset{language=%s}\n" lstlang)) + "\n") + (when (and caption + org-export-latex-listings-w-names) + (format "\n%s $\\equiv$ \n" + (replace-regexp-in-string + "_" "\\\\_" caption))) + "\\begin{lstlisting}\n" + rtn "\\end{lstlisting}\n")) + (org-export-latex-minted + (if lang + (let* + ((lang-sym (intern lang)) + (minted-lang + (or (cadr + (assq + lang-sym + org-export-latex-minted-langs)) + (downcase lang)))) + (concat + (when (and caption + org-export-latex-listings-w-names) + (format "\n%s $\\equiv$ \n" + (replace-regexp-in-string + "_" "\\\\_" caption))) + (format + "\\begin{minted}[mathescape,%s\nnumbersep=5pt,\nframe=lines,\nframesep=2mm]{%s}\n" (if org-export-latex-minted-with-line-numbers "\nlinenos," "") minted-lang) + rtn "\\end{minted}\n")))) + (t (concat (car org-export-latex-verbatim-wrap) + rtn (cdr org-export-latex-verbatim-wrap)))) + '(org-protected t org-example t)) + "#+END_LaTeX\n")) + ((eq backend 'ascii) + ;; This is not HTML or LaTeX, so just make it an example. + (setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt)) + (concat caption "\n" + "#+BEGIN_ASCII\n" (org-add-props (concat (mapconcat diff --git a/lisp/org-latex.el b/lisp/org-latex.el index bae4c929b..1abec609d 100644 --- a/lisp/org-latex.el +++ b/lisp/org-latex.el @@ -411,6 +411,52 @@ of noweb." :group 'org-export-latex :type 'boolean) +(defcustom org-export-latex-minted nil + "Non-nil means export source code using the minted package. +This package will fontify source code with color. +If you want to use this, you need to make LaTeX use the +minted package. Add this to `org-export-latex-packages-alist', +for example using customize, or with something like + + (require 'org-latex) + (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\")) + +In addition, it is neccessary to configure +`org-latex-to-pdf-process' so that the -shell-escape option is +passed to pdflatex." + :group 'org-export-latex + :type 'boolean) + +(defcustom org-export-latex-minted-langs + '((emacs-lisp "common-lisp") + (cc "c++") + (cperl "perl") + (shell-script "bash") + (caml "ocaml")) + "Alist mapping languages to their minted language counterpart. +The key is a symbol, the major mode symbol without the \"-mode\". +The value is the string that should be inserted as the language parameter +for the minted package. If the mode name and the listings name are +the same, the language does not need an entry in this list - but it does not +hurt if it is present. + +Note that minted uses all lower case for language identifiers, +and that the full list of language identifiers can be obtained +with: +pygmentize -L lexers +" + :group 'org-export-latex + :type '(repeat + (list + (symbol :tag "Major mode ") + (string :tag "Listings language")))) + +(defcustom org-export-latex-minted-with-line-numbers nil + "Should source code line numbers be included when exporting +with the latex minted package?" + :group 'org-export-latex + :type 'boolean) + (defcustom org-export-latex-remove-from-headlines '(:todo nil :priority nil :tags nil) "A plist of keywords to remove from headlines. OBSOLETE. From db3798fd2b1b60a4526bd14fb3619438bedd0892 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Tue, 12 Oct 2010 22:59:24 -0600 Subject: [PATCH 2/3] ob-python: fixed compiler warning * lisp/ob-python.el (org-src-preserve-indentation): fixed compiler warning --- lisp/ob-python.el | 1 + 1 file changed, 1 insertion(+) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index a80d6907b..c056cb633 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -47,6 +47,7 @@ (defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python) "Preferred python mode for use in running python interactively.") +(defvar org-src-preserve-indentation) (defun org-babel-expand-body:python (body params &optional processed-params) "Expand BODY according to PARAMS, return the expanded body." (concat From eb75d6de4035067120f34bf387e728e61b53fe57 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Wed, 13 Oct 2010 08:42:02 +0100 Subject: [PATCH 3/3] Eliminate a compiler warning caused by the latex minted patch * org-exp.el (org-export-latex-minted-with-line-numbers): Ensure that variable is declared --- lisp/org-exp.el | 1 + 1 file changed, 1 insertion(+) diff --git a/lisp/org-exp.el b/lisp/org-exp.el index 6486e46a6..9b455b1bc 100644 --- a/lisp/org-exp.el +++ b/lisp/org-exp.el @@ -2211,6 +2211,7 @@ in the list) and remove property and value from the list in LISTVAR." (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el (defvar org-export-latex-minted) ;; defined in org-latex.el (defvar org-export-latex-minted-langs) ;; defined in org-latex.el +(defvar org-export-latex-minted-with-line-numbers) ;; defined in org-latex.el (defun org-export-format-source-code-or-example (backend lang code &optional opts indent caption)