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,65 +158,82 @@ 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)
(let* ((element (save-match-data (org-element-context))) (save-excursion
(type (org-element-type element))) (let* ((element (save-match-data (org-element-context)))
(cond (type (org-element-type element)))
((not (memq type '(babel-call inline-babel-call inline-src-block)))) (when (memq type '(babel-call inline-babel-call inline-src-block))
((eq type 'inline-src-block) (let ((beg-el (org-element-property :begin element))
(let* ((beg (org-element-property :begin element)) (end-el (org-element-property :end element)))
(end (save-excursion (case type
(goto-char (org-element-property :end element)) (inline-src-block
(skip-chars-forward " \t") (let* ((info (org-babel-parse-inline-src-block-match))
(point))) (params (nth 2 info)))
(info (org-babel-parse-inline-src-block-match)) (setf (nth 1 info)
(params (nth 2 info))) (if (and (cdr (assoc :noweb params))
;; Expand noweb references in the original file. (string= "yes" (cdr (assoc :noweb params))))
(setf (nth 1 info) (org-babel-expand-noweb-references
(if (and (cdr (assoc :noweb params)) info (org-babel-exp-get-export-buffer))
(string= "yes" (cdr (assoc :noweb params)))) (nth 1 info)))
(org-babel-expand-noweb-references (goto-char beg-el)
info (org-babel-exp-get-export-buffer)) (let ((replacement (org-babel-exp-do-export info 'inline)))
(nth 1 info))) (if (equal replacement "")
(let ((code-replacement ;; Replacement code is empty: completely
(save-match-data (org-babel-exp-do-export info 'inline)))) ;; remove inline src block, including extra
(if code-replacement ;; white space that might have been created
(progn ;; when inserting results.
(delete-region (delete-region beg-el
(progn (goto-char beg) (progn (goto-char end-el)
(skip-chars-backward " \t") (skip-chars-forward " \t")
(point)) (point)))
end) ;; Otherwise: remove inline src block but
(insert code-replacement)) ;; preserve following white spaces. Then
(org-babel-examplize-region beg end) ;; insert value.
(forward-char 2))))) (delete-region beg-el
(t (let* ((lob-info (org-babel-lob-get-info)) (progn (goto-char end-el)
(inlinep (match-string 11)) (skip-chars-backward " \t")
(inline-start (match-end 11)) (point)))
(inline-end (match-end 0)) (insert replacement)))))
(results (save-match-data ((babel-call inline-babel-call)
(org-babel-exp-do-export (let* ((lob-info (org-babel-lob-get-info))
(list "emacs-lisp" "results" (results
(org-babel-merge-params (org-babel-exp-do-export
org-babel-default-header-args (list "emacs-lisp" "results"
org-babel-default-lob-header-args (org-babel-merge-params
(org-babel-params-from-properties) org-babel-default-header-args
(org-babel-parse-header-arguments org-babel-default-lob-header-args
(org-no-properties (org-babel-params-from-properties)
(concat ":var results=" (org-babel-parse-header-arguments
(mapconcat #'identity (org-no-properties
(butlast lob-info) (concat ":var results="
" "))))) (mapconcat 'identity
"" nil (car (last lob-info))) (butlast lob-info)
'lob))) " ")))))
(rep (org-fill-template "" nil (car (last lob-info)))
org-babel-exp-call-line-template 'lob))
`(("line" . ,(nth 0 lob-info)))))) (rep (org-fill-template
(if inlinep org-babel-exp-call-line-template
(save-excursion `(("line" . ,(nth 0 lob-info))))))
(goto-char inline-start) ;; If replacement is empty, completely remove the
(delete-region inline-start inline-end) ;; object/element, including any extra white space
(insert rep)) ;; that might have been created when including
(replace-match rep t t)))))))))) ;; results.
(if (equal rep "")
(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 ()