0
0
Fork 1
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-08-26 15:02:52 +00:00

Remember the original indentation of source code snippets

As the export preprocessor removes indentation from indented blocks,
this causes conflicts about interpreting indentation as list
termination.  Now the original indentation is stored in a text
property, so hopefully  the exporters can make use of this information
in due time.
This commit is contained in:
Carsten Dominik 2009-06-05 08:55:40 +02:00
parent b6152ed5b0
commit 4cc6309f6f
2 changed files with 90 additions and 84 deletions

View file

@ -1,5 +1,8 @@
2009-06-04 Carsten Dominik <carsten.dominik@gmail.com> 2009-06-04 Carsten Dominik <carsten.dominik@gmail.com>
* org-exp.el (org-export-format-source-code-or-example): Remember
the original indentation of source code snippets and examples.
* org-latex.el (org-export-as-latex): Relocate the table of * org-latex.el (org-export-as-latex): Relocate the table of
contents. contents.

View file

@ -2124,32 +2124,33 @@ in the list) and remove property and value from the list in LISTVAR."
lang code trans opts) lang code trans opts)
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward (while (re-search-forward
"\\(^[ \t]*#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\\)\\|\\(^[ \t]*#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\\)" "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\\)\\|\\(^[ \t]*#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\\)"
nil t) nil t)
(if (match-end 1) (if (match-end 1)
;; src segments ;; src segments
(setq lang (match-string 2) (setq lang (match-string 3)
opts (match-string 3) opts (match-string 4)
code (match-string 4)) code (match-string 5))
(setq lang nil (setq lang nil
opts (match-string 6) opts (match-string 7)
code (match-string 7))) code (match-string 8)))
(setq trans (org-export-format-source-code-or-example (setq trans (org-export-format-source-code-or-example
backend lang code opts)) backend lang code opts (length (match-string 2))))
(replace-match trans t t)))) (replace-match trans t t))))
(defvar htmlp) ;; dynamically scoped (defvar htmlp) ;; dynamically scoped
(defvar latexp) ;; dynamically scoped (defvar latexp) ;; dynamically scoped
(defun org-export-format-source-code-or-example (backend (defun org-export-format-source-code-or-example
lang code &optional opts) (backend lang code &optional opts indent)
"Format CODE from language LANG and return it formatted for export. "Format CODE from language LANG and return it formatted for export.
If LANG is nil, do not add any fontification. If LANG is nil, do not add any fontification.
OPTS contains formatting optons, like `-n' for triggering numbering lines, OPTS contains formatting optons, like `-n' for triggering numbering lines,
and `+n' for continuing previous numering. and `+n' for continuing previous numering.
Code formatting according to language currently only works for HTML. Code formatting according to language currently only works for HTML.
Numbering lines works for all three major backends (html, latex, and ascii)." Numbering lines works for all three major backends (html, latex, and ascii).
INDENT was the original indentation of the block."
(save-match-data (save-match-data
(let (num cont rtn rpllbl keepp textareap cols rows fmt) (let (num cont rtn rpllbl keepp textareap cols rows fmt)
(setq opts (or opts "") (setq opts (or opts "")
@ -2184,80 +2185,82 @@ Numbering lines works for all three major backends (html, latex, and ascii)."
(end-of-line 1)) (end-of-line 1))
(buffer-string)))) (buffer-string))))
;; Now backend-specific coding ;; Now backend-specific coding
(cond (setq rtn
((eq backend 'docbook) (cond
(setq rtn (org-export-number-lines rtn 'docbook 0 0 num cont rpllbl fmt)) ((eq backend 'docbook)
(concat "\n#+BEGIN_DOCBOOK\n" (setq rtn (org-export-number-lines rtn 'docbook 0 0 num cont rpllbl fmt))
(org-add-props (concat "<programlisting><![CDATA[" (concat "\n#+BEGIN_DOCBOOK\n"
rtn (org-add-props (concat "<programlisting><![CDATA["
"]]>\n</programlisting>\n") rtn
'(org-protected t)) "]]>\n</programlisting>\n")
"#+END_DOCBOOK\n")) '(org-protected t))
((eq backend 'html) "#+END_DOCBOOK\n"))
;; We are exporting to HTML ((eq backend 'html)
(when lang ;; We are exporting to HTML
(require 'htmlize nil t) (when lang
(when (not (fboundp 'htmlize-region-for-paste)) (require 'htmlize nil t)
;; we do not have htmlize.el, or an old version of it (when (not (fboundp 'htmlize-region-for-paste))
(setq lang nil) ;; we do not have htmlize.el, or an old version of it
(message (setq lang nil)
"htmlize.el 1.34 or later is needed for source code formatting"))) (message
"htmlize.el 1.34 or later is needed for source code formatting")))
(if lang
(let* ((mode (and lang (intern (concat lang "-mode")))) (if lang
(org-inhibit-startup t) (let* ((mode (and lang (intern (concat lang "-mode"))))
(org-startup-folded nil)) (org-inhibit-startup t)
(setq rtn (org-startup-folded nil))
(with-temp-buffer (setq rtn
(insert rtn) (with-temp-buffer
(if (functionp mode) (insert rtn)
(funcall mode) (if (functionp mode)
(fundamental-mode)) (funcall mode)
(font-lock-fontify-buffer) (fundamental-mode))
(set-buffer-modified-p nil) (font-lock-fontify-buffer)
(org-export-htmlize-region-for-paste (set-buffer-modified-p nil)
(point-min) (point-max)))) (org-export-htmlize-region-for-paste
(if (string-match "<pre\\([^>]*\\)>\n?" rtn) (point-min) (point-max))))
(setq rtn (replace-match (if (string-match "<pre\\([^>]*\\)>\n?" rtn)
(format "<pre class=\"src src-%s\">\n" lang) (setq rtn (replace-match
t t rtn)))) (format "<pre class=\"src src-%s\">\n" lang)
(if textareap t t rtn))))
(setq rtn (concat (if textareap
(format "<p>\n<textarea cols=\"%d\" rows=\"%d\" overflow-x:scroll >\n" (setq rtn (concat
cols rows) (format "<p>\n<textarea cols=\"%d\" rows=\"%d\" overflow-x:scroll >\n"
rtn "</textarea>\n</p>\n")) cols rows)
(with-temp-buffer rtn "</textarea>\n</p>\n"))
(insert rtn) (with-temp-buffer
(goto-char (point-min)) (insert rtn)
(while (re-search-forward "[<>&]" nil t) (goto-char (point-min))
(replace-match (cdr (assq (char-before) (while (re-search-forward "[<>&]" nil t)
'((?&."&amp;")(?<."&lt;")(?>."&gt;")))) (replace-match (cdr (assq (char-before)
t t)) '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
(setq rtn (buffer-string))) t t))
(setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n")))) (setq rtn (buffer-string)))
(unless textareap (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
(setq rtn (org-export-number-lines rtn 'html 1 1 num (unless textareap
cont rpllbl fmt))) (setq rtn (org-export-number-lines rtn 'html 1 1 num
(concat "\n#+BEGIN_HTML\n" (org-add-props rtn '(org-protected t)) "\n#+END_HTML\n\n")) cont rpllbl fmt)))
((eq backend 'latex) (concat "\n#+BEGIN_HTML\n" (org-add-props rtn '(org-protected t)) "\n#+END_HTML\n\n"))
(setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt)) ((eq backend 'latex)
(concat "\n#+BEGIN_LaTeX\n" (setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt))
(org-add-props (concat "\\begin{verbatim}\n" rtn "\n\\end{verbatim}\n") (concat "\n#+BEGIN_LaTeX\n"
'(org-protected t)) (org-add-props (concat "\\begin{verbatim}\n" rtn "\n\\end{verbatim}\n")
"#+END_LaTeX\n\n")) '(org-protected t))
((eq backend 'ascii) "#+END_LaTeX\n\n"))
;; This is not HTML or LaTeX, so just make it an example. ((eq backend 'ascii)
(setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt)) ;; This is not HTML or LaTeX, so just make it an example.
(concat "#+BEGIN_ASCII\n" (setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt))
(org-add-props (concat "#+BEGIN_ASCII\n"
(concat (org-add-props
(mapconcat (concat
(lambda (l) (concat " " l)) (mapconcat
(org-split-string rtn "\n") (lambda (l) (concat " " l))
"\n") (org-split-string rtn "\n")
"\n") "\n")
'(org-protected t)) "\n")
"#+END_ASCII\n")))))) '(org-protected t))
"#+END_ASCII\n"))))
(org-add-props rtn nil 'original-indentation indent))))
(defun org-export-number-lines (text backend (defun org-export-number-lines (text backend
&optional skip1 skip2 number cont &optional skip1 skip2 number cont