Browse Source

ob-exp: Improve `org-export-blocks-preprocess'

* lisp/ob-exp.el (org-export-blocks-preprocess): Improve blank lines
  handling in function. Add comments. Remove
  `org-export-blocks-postblock-hook' since it's defined nowhere
  now (and doesn't need to, there's `org-export-before-parsing-hook'
  already).
Nicolas Goaziou 7 years ago
parent
commit
3c6a715bce
1 changed files with 29 additions and 24 deletions
  1. 29 24
      lisp/ob-exp.el

+ 29 - 24
lisp/ob-exp.el

@@ -224,14 +224,14 @@ this template."
   (interactive)
   (save-window-excursion
     (let ((case-fold-search t)
-          (start (point-min)))
+	  (start (point-min)))
       (goto-char start)
       (while (re-search-forward "^[ \t]*#\\+BEGIN_SRC" nil t)
         (let ((element (save-match-data (org-element-at-point))))
           (when (eq (org-element-type element) 'src-block)
             (let* ((block-start (copy-marker (match-beginning 0)))
                    (match-start (copy-marker
-                                 (org-element-property :begin element)))
+				 (org-element-property :begin element)))
                    ;; Make sure we don't remove any blank lines after
                    ;; the block when replacing it.
                    (match-end (save-excursion
@@ -250,31 +250,36 @@ this template."
               ;; Execute all non-block elements between START and
               ;; MATCH-START.
               (org-babel-exp-non-block-elements start match-start)
-              (let ((replacement
-                     (progn (goto-char block-start)
-                            (org-babel-exp-src-block headers))))
-                (when replacement
-                  (goto-char match-start)
-                  (delete-region (point) match-end)
-                  (insert replacement)
-                  (if preserve-indent
-                      ;; Indent only the code block markers.
-                      (save-excursion
-                        (skip-chars-backward " \r\t\n")
-                        (indent-line-to indentation)
-                        (goto-char match-start)
-                        (indent-line-to indentation))
-                    ;; Indent everything.
-                    (indent-code-rigidly match-start (point) indentation))))
+	      ;; Take care of matched block: compute replacement
+	      ;; string. In particular, a nil REPLACEMENT means the
+	      ;; block should be left as-is while an empty string
+	      ;; should remove the block.
+              (let ((replacement (progn (goto-char block-start)
+					(org-babel-exp-src-block headers))))
+                (cond ((not replacement) (goto-char match-end))
+		      ((equal replacement "")
+		       (delete-region (org-element-property :begin element)
+				      (org-element-property :end element)))
+		      (t
+		       (goto-char match-start)
+		       (delete-region (point) match-end)
+		       (insert replacement)
+		       (if preserve-indent
+			   ;; Indent only the code block markers.
+			   (save-excursion (skip-chars-backward " \r\t\n")
+					   (indent-line-to indentation)
+					   (goto-char match-start)
+					   (indent-line-to indentation))
+			 ;; Indent everything.
+			 (indent-code-rigidly match-start (point) indentation)))))
+	      (setq start (point))
               ;; Cleanup markers.
 	      (set-marker block-start nil)
               (set-marker match-start nil)
-              (set-marker match-end nil))))
-        (setq start (point)))
-      ;; Execute all non-block Babel elements between last src-block
-      ;; and end of buffer.
-      (org-babel-exp-non-block-elements start (point-max))
-      (run-hooks 'org-export-blocks-postblock-hook))))
+              (set-marker match-end nil)))))
+      ;; Eventually execute all non-block Babel elements between last
+      ;; src-block and end of buffer.
+      (org-babel-exp-non-block-elements start (point-max)))))
 
 (defun org-babel-in-example-or-verbatim ()
   "Return true if point is in example or verbatim code.