Browse Source

Export: Allow example and src blocks to be indented

Carsten Dominik 16 years ago
parent
commit
aea59ca7a6
3 changed files with 32 additions and 2 deletions
  1. 6 0
      lisp/ChangeLog
  2. 2 2
      lisp/org-exp.el
  3. 24 0
      lisp/org.el

+ 6 - 0
lisp/ChangeLog

@@ -1,5 +1,11 @@
 2009-05-24  Carsten Dominik  <carsten.dominik@gmail.com>
 
+	* org-exp.el (org-export-replace-src-segments-and-examples): FInd
+	indented blocks.
+	(org-export-format-source-code-or-example): Fix indentation of
+	blocks.
+	(org-export-remove-indentation): New function.
+
 	* org.el (org-fontify-meta-lines): New function.
 	(org-set-font-lock-defaults): Call the new fontification
 	function.

+ 2 - 2
lisp/org-exp.el

@@ -2123,7 +2123,7 @@ in the list) and remove property and value from the list in LISTVAR."
 	lang code trans opts)
     (goto-char (point-min))
     (while (re-search-forward
-	    "\\(^#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)\\(.*\\)\n\\([^\000]+?\n\\)#\\+END_SRC.*\\)\\|\\(^#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)#\\+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)
       (if (match-end 1)
 	  ;; src segments
@@ -2169,7 +2169,7 @@ Numbering lines works for all three major backends (html, latex, and ascii)."
 	;; we cannot use numbering or highlighting.
 	(setq num nil cont nil lang nil))
       (if keepp (setq rpllbl 'keep))
-      (setq rtn code)
+      (setq rtn (org-remove-indentation code))
       (when (equal lang "org")
 	(setq rtn (with-temp-buffer
 		    (insert rtn)

+ 24 - 0
lisp/org.el

@@ -15457,6 +15457,30 @@ leave it alone.  If it is larger than ind, set it to the target."
 	(concat (make-string i1 ?\ ) l)
       l)))
 
+(defun org-remove-indentation (code &optional n)
+  "Remove the maximum common indentation from the lines in CODE.
+N may optionally be the number of spaces to remove."
+  (with-temp-buffer
+    (insert code)
+    (org-do-remove-indentation n)
+    (buffer-string)))
+
+(defun org-do-remove-indentation (&optional n)
+  "Remove the maximum common indentation from the buffer."
+  (let ((min 10000) re)
+    (if n
+	(setq min n)
+      (goto-char (point-min))
+      (while (re-search-forward "^ +[^ \n]" nil t)
+	(setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
+    (unless (= min 0)
+      (setq re (format "^ \\{%d\\}" min))
+      (goto-char (point-min))
+      (while (re-search-forward re nil t)
+	(replace-match "")
+	(end-of-line 1))
+      n)))
+
 (defun org-base-buffer (buffer)
   "Return the base buffer of BUFFER, if it has one.  Else return the buffer."
   (if (not buffer)