ob-exp: More accurate white space handling when evaluating inline code

* lisp/ob-exp.el (org-babel-exp-non-block-elements): More accurate
white space handling when evaluating inline-src-block, babel-call and
inline-babel-call elements or objects.  Also removed use of
`org-babel-examplize-region' since it would never be called anyway
(return value from `org-babel-exp-do-export' is never nil).
This commit is contained in:
Nicolas Goaziou 2012-09-15 22:31:07 +02:00
parent ef125abe61
commit a5756f5e1d
1 changed files with 76 additions and 59 deletions

View File

@ -158,42 +158,44 @@ this template."
(let ((rx (concat "\\(?:" org-babel-inline-src-block-regexp (let ((rx (concat "\\(?:" org-babel-inline-src-block-regexp
"\\|" org-babel-lob-one-liner-regexp "\\)"))) "\\|" org-babel-lob-one-liner-regexp "\\)")))
(while (re-search-forward rx end t) (while (re-search-forward rx end t)
(save-excursion
(let* ((element (save-match-data (org-element-context))) (let* ((element (save-match-data (org-element-context)))
(type (org-element-type element))) (type (org-element-type element)))
(cond (when (memq type '(babel-call inline-babel-call inline-src-block))
((not (memq type '(babel-call inline-babel-call inline-src-block)))) (let ((beg-el (org-element-property :begin element))
((eq type 'inline-src-block) (end-el (org-element-property :end element)))
(let* ((beg (org-element-property :begin element)) (case type
(end (save-excursion (inline-src-block
(goto-char (org-element-property :end element)) (let* ((info (org-babel-parse-inline-src-block-match))
(skip-chars-forward " \t")
(point)))
(info (org-babel-parse-inline-src-block-match))
(params (nth 2 info))) (params (nth 2 info)))
;; Expand noweb references in the original file.
(setf (nth 1 info) (setf (nth 1 info)
(if (and (cdr (assoc :noweb params)) (if (and (cdr (assoc :noweb params))
(string= "yes" (cdr (assoc :noweb params)))) (string= "yes" (cdr (assoc :noweb params))))
(org-babel-expand-noweb-references (org-babel-expand-noweb-references
info (org-babel-exp-get-export-buffer)) info (org-babel-exp-get-export-buffer))
(nth 1 info))) (nth 1 info)))
(let ((code-replacement (goto-char beg-el)
(save-match-data (org-babel-exp-do-export info 'inline)))) (let ((replacement (org-babel-exp-do-export info 'inline)))
(if code-replacement (if (equal replacement "")
(progn ;; Replacement code is empty: completely
(delete-region ;; remove inline src block, including extra
(progn (goto-char beg) ;; white space that might have been created
;; when inserting results.
(delete-region beg-el
(progn (goto-char end-el)
(skip-chars-forward " \t")
(point)))
;; Otherwise: remove inline src block but
;; preserve following white spaces. Then
;; insert value.
(delete-region beg-el
(progn (goto-char end-el)
(skip-chars-backward " \t") (skip-chars-backward " \t")
(point)) (point)))
end) (insert replacement)))))
(insert code-replacement)) ((babel-call inline-babel-call)
(org-babel-examplize-region beg end) (let* ((lob-info (org-babel-lob-get-info))
(forward-char 2))))) (results
(t (let* ((lob-info (org-babel-lob-get-info))
(inlinep (match-string 11))
(inline-start (match-end 11))
(inline-end (match-end 0))
(results (save-match-data
(org-babel-exp-do-export (org-babel-exp-do-export
(list "emacs-lisp" "results" (list "emacs-lisp" "results"
(org-babel-merge-params (org-babel-merge-params
@ -203,20 +205,35 @@ this template."
(org-babel-parse-header-arguments (org-babel-parse-header-arguments
(org-no-properties (org-no-properties
(concat ":var results=" (concat ":var results="
(mapconcat #'identity (mapconcat 'identity
(butlast lob-info) (butlast lob-info)
" "))))) " ")))))
"" nil (car (last lob-info))) "" nil (car (last lob-info)))
'lob))) 'lob))
(rep (org-fill-template (rep (org-fill-template
org-babel-exp-call-line-template org-babel-exp-call-line-template
`(("line" . ,(nth 0 lob-info)))))) `(("line" . ,(nth 0 lob-info))))))
(if inlinep ;; If replacement is empty, completely remove the
(save-excursion ;; object/element, including any extra white space
(goto-char inline-start) ;; that might have been created when including
(delete-region inline-start inline-end) ;; results.
(insert rep)) (if (equal rep "")
(replace-match rep t t)))))))))) (delete-region
beg-el
(progn (goto-char end-el)
(if (not (eq type 'babel-call))
(progn (skip-chars-forward " \t") (point))
(skip-chars-forward " \r\t\n")
(line-beginning-position))))
;; Otherwise, preserve following white
;; spaces/newlines and then, insert replacement
;; string.
(goto-char beg-el)
(delete-region beg-el
(progn (goto-char end-el)
(skip-chars-backward " \r\t\n")
(point)))
(insert rep)))))))))))))
(defvar org-src-preserve-indentation) ; From org-src.el (defvar org-src-preserve-indentation) ; From org-src.el
(defun org-export-blocks-preprocess () (defun org-export-blocks-preprocess ()