Browse Source

ob: simplified code block exportation -- same process for inline and regular blocks

* lisp/ob-exp.el (org-babel-exp-inline-src-blocks): Simplified
  exportation of inline code blocks using normal code block execution
  mechanism to insert results.
  (org-babel-exp-results): Results exportation mechanism is unified
  for both inline and regular code blocks.
* lisp/ob.el (org-babel-where-is-src-block-result): Returns the point
  after an inline code block for inline code blocks.
Eric Schulte 14 years ago
parent
commit
d0ca215105
2 changed files with 43 additions and 58 deletions
  1. 34 55
      lisp/ob-exp.el
  2. 9 3
      lisp/ob.el

+ 34 - 55
lisp/ob-exp.el

@@ -150,21 +150,20 @@ options and are taken from `org-babel-default-inline-header-args'."
     (while (and (< (point) end)
     (while (and (< (point) end)
                 (re-search-forward org-babel-inline-src-block-regexp end t))
                 (re-search-forward org-babel-inline-src-block-regexp end t))
       (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
       (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
-	     (params (nth 2 info))
-	     (replacement
-	      (save-match-data
-		(if (org-babel-in-example-or-verbatim)
-		    (buffer-substring (match-beginning 0) (match-end 0))
-		  ;; expand noweb references in the original file
-		  (setf (nth 1 info)
-			(if (and (cdr (assoc :noweb params))
-				 (string= "yes" (cdr (assoc :noweb params))))
-			    (org-babel-expand-noweb-references
-			     info (get-file-buffer org-current-export-file))
-			  (nth 1 info)))
-		  (org-babel-exp-do-export info 'inline)))))
-	(setq end (+ end (- (length replacement) (length (match-string 1)))))
-	(replace-match replacement t t nil 1)))))
+	     (params (nth 2 info)))
+	(save-match-data
+	  (goto-char (match-beginning 2))
+	  (if (org-babel-in-example-or-verbatim)
+	      (buffer-substring (match-beginning 0) (match-end 0))
+	    ;; expand noweb references in the original file
+	    (setf (nth 1 info)
+		  (if (and (cdr (assoc :noweb params))
+			   (string= "yes" (cdr (assoc :noweb params))))
+		      (org-babel-expand-noweb-references
+		       info (get-file-buffer org-current-export-file))
+		    (nth 1 info)))
+	    (org-babel-exp-do-export info 'inline)))
+	(delete-region (match-beginning 0) (match-end 0))))))
 
 
 (defun org-exp-res/src-name-cleanup ()
 (defun org-exp-res/src-name-cleanup ()
   "Clean up #+results and #+srcname lines for export.
   "Clean up #+results and #+srcname lines for export.
@@ -280,46 +279,26 @@ Results are prepared in a manner suitable for export by org-mode.
 This function is called by `org-babel-exp-do-export'.  The code
 This function is called by `org-babel-exp-do-export'.  The code
 block will be evaluated.  Optional argument SILENT can be used to
 block will be evaluated.  Optional argument SILENT can be used to
 inhibit insertion of results into the buffer."
 inhibit insertion of results into the buffer."
-  (or
-   (when org-export-babel-evaluate
-     (let ((lang (nth 0 info))
-	   (body (nth 1 info)))
-       (setf (nth 2 info) (org-babel-exp-in-export-file
-			   (org-babel-process-params (nth 2 info))))
-       ;; skip code blocks which we can't evaluate
-       (when (fboundp (intern (concat "org-babel-execute:" lang)))
-	 (org-babel-eval-wipe-error-buffer)
-	 (if (equal type 'inline)
-	     (let ((raw (org-babel-execute-src-block
-			 nil info '((:results . "silent"))))
-		   (result-params (split-string
-				   (cdr (assoc :results (nth 2 info))))))
-	       (unless silent
-		 (cond ;; respect the value of the :results header argument
-		  ((member "file" result-params)
-		   (org-babel-result-to-file raw))
-		  ((or (member "raw" result-params)
-		       (member "org" result-params))
-		   (format "%s" raw))
-		  ((member "code" result-params)
-		   (format "src_%s{%s}" lang raw))
-		  (t
-		   (if (stringp raw)
-		       (if (= 0 (length raw)) "=(no results)="
-			 (format "%s" raw))
-		     (format "%S" raw))))))
-	   (prog1 nil
-	     (setf (nth 2 info)
-		   (org-babel-merge-params
-		    (nth 2 info)
-		    `((:results . ,(if silent "silent" "replace")))))
-	     (cond
-	      ((equal type 'block) (org-babel-execute-src-block nil info))
-	      ((equal type 'lob)
-	       (save-excursion
-		 (re-search-backward org-babel-lob-one-liner-regexp nil t)
-		 (org-babel-execute-src-block nil info)))))))))
-   ""))
+  (when org-export-babel-evaluate
+    (let ((lang (nth 0 info))
+	  (body (nth 1 info)))
+      (setf (nth 2 info) (org-babel-exp-in-export-file
+			  (org-babel-process-params (nth 2 info))))
+      ;; skip code blocks which we can't evaluate
+      (when (fboundp (intern (concat "org-babel-execute:" lang)))
+	(org-babel-eval-wipe-error-buffer)
+	(prog1 nil
+	  (setf (nth 2 info)
+		(org-babel-merge-params
+		 (nth 2 info)
+		 `((:results . ,(if silent "silent" "replace")))))
+	  (cond
+	   ((or (equal type 'block) (equal type 'inline))
+	    (org-babel-execute-src-block nil info))
+	   ((equal type 'lob)
+	    (save-excursion
+	      (re-search-backward org-babel-lob-one-liner-regexp nil t)
+	      (org-babel-execute-src-block nil info)))))))))
 
 
 (provide 'ob-exp)
 (provide 'ob-exp)
 
 

+ 9 - 3
lisp/ob.el

@@ -1280,8 +1280,13 @@ source block.  Specifically at the beginning of the results line.
 If no result exists for this block then create a results line
 If no result exists for this block then create a results line
 following the source block."
 following the source block."
   (save-excursion
   (save-excursion
-    (let* ((on-lob-line (progn (beginning-of-line 1)
-			       (looking-at org-babel-lob-one-liner-regexp)))
+    (let* ((on-lob-line (save-excursion
+			  (beginning-of-line 1)
+			  (looking-at org-babel-lob-one-liner-regexp)))
+	   (inlinep (save-excursion
+		      (re-search-backward "[ \f\t\n\r\v]" nil t)
+		      (when (looking-at org-babel-inline-src-block-regexp)
+			(match-end 0))))
 	   (name (if on-lob-line
 	   (name (if on-lob-line
 		     (nth 0 (org-babel-lob-get-info))
 		     (nth 0 (org-babel-lob-get-info))
 		   (nth 4 (or info (org-babel-get-src-block-info)))))
 		   (nth 4 (or info (org-babel-get-src-block-info)))))
@@ -1291,6 +1296,7 @@ following the source block."
       (setq
       (setq
        found ;; was there a result (before we potentially insert one)
        found ;; was there a result (before we potentially insert one)
        (or
        (or
+	inlinep
 	(and
 	(and
 	 ;; named results:
 	 ;; named results:
 	 ;; - return t if it is found, else return nil
 	 ;; - return t if it is found, else return nil
@@ -1550,7 +1556,7 @@ code ---- the results are extracted in the syntax of the source
 	    (org-babel-examplize-region beg end results-switches)
 	    (org-babel-examplize-region beg end results-switches)
 	    (setq end (point)))))
 	    (setq end (point)))))
 	;; possibly indent the results to match the #+results line
 	;; possibly indent the results to match the #+results line
-	(when (and indent (> indent 0)
+	(when (and (not inlinep) indent (> indent 0)
 		   ;; in this case `table-align' does the work for us
 		   ;; in this case `table-align' does the work for us
 		   (not (and (listp result)
 		   (not (and (listp result)
 			     (member "append" result-params))))
 			     (member "append" result-params))))