ob-exp: Preserve affiliated keywords when replacing a code block

* lisp/ob-exp.el (org-export-blocks-preprocess): Preserve affiliated
  keywords when replacing a code block.
This commit is contained in:
Nicolas Goaziou 2012-09-13 20:04:39 +02:00
parent 7efd61249b
commit 4d06f3349d
1 changed files with 24 additions and 28 deletions

View File

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