Browse Source

ob: Remove variables related to inline Babel code

* lisp/ob-lob.el (org-babel-block-lob-one-liner-regexp):
(org-babel-inline-lob-one-liner-regexp):
(org-babel-lob-one-line-regexp):
* lisp/ob-core.el (org-babel-inline-src-block-regexp): Remove variables.

(org-babel-map-inline-src-blocks):
(org-babel-map-call-lines):
(org-babel-map-executables):
(org-babel-execute-buffer):
* lisp/ob-exp.el (org-babel-exp-process-buffer):
(org-babel-exp-results): Do not use removed variables.

Babel now mostly uses the parser to handle Babel code.  Regexps are not
needed anymore.
Nicolas Goaziou 9 years ago
parent
commit
49bb05487c
4 changed files with 69 additions and 95 deletions
  1. 62 61
      lisp/ob-core.el
  2. 6 15
      lisp/ob-exp.el
  3. 1 18
      lisp/ob-lob.el
  4. 0 1
      lisp/ob-ref.el

+ 62 - 61
lisp/ob-core.el

@@ -204,16 +204,6 @@ This string must include a \"%s\" which will be replaced by the results."
    "\\([^\000]*?\n\\)??[ \t]*#\\+end_src")
    "\\([^\000]*?\n\\)??[ \t]*#\\+end_src")
   "Regexp used to identify code blocks.")
   "Regexp used to identify code blocks.")
 
 
-(defvar org-babel-inline-src-block-regexp
-  (concat
-   ;; (1) replacement target (2) lang
-   "\\(?:^\\|[^-[:alnum:]]?\\)\\(src_\\([^ \f\t\n\r\v[]+\\)"
-   ;; (3,4) (unused, headers)
-   "\\(\\|\\[[ \t]*\\(.*?\\)\\]\\)"
-   ;; (5) body
-   "{\\([^\f\n\r\v]+?\\)}\\)")
-  "Regexp used to identify inline src-blocks.")
-
 (defun org-babel--get-vars (params)
 (defun org-babel--get-vars (params)
   "Return the babel variable assignments in PARAMS.
   "Return the babel variable assignments in PARAMS.
 
 
@@ -1124,81 +1114,91 @@ end-body --------- point at the end of the body"
 
 
 ;;;###autoload
 ;;;###autoload
 (defmacro org-babel-map-inline-src-blocks (file &rest body)
 (defmacro org-babel-map-inline-src-blocks (file &rest body)
-  "Evaluate BODY forms on each inline source-block in FILE.
+  "Evaluate BODY forms on each inline source block in FILE.
 If FILE is nil evaluate BODY forms on source blocks in current
 If FILE is nil evaluate BODY forms on source blocks in current
 buffer."
 buffer."
-  (declare (indent 1))
-  (let ((tempvar (make-symbol "file")))
+  (declare (indent 1) (debug (form body)))
+  (org-with-gensyms (datum end point tempvar to-be-removed visitedp)
     `(let* ((case-fold-search t)
     `(let* ((case-fold-search t)
 	    (,tempvar ,file)
 	    (,tempvar ,file)
-	    (visited-p (or (null ,tempvar)
+	    (,visitedp (or (null ,tempvar)
 			   (get-file-buffer (expand-file-name ,tempvar))))
 			   (get-file-buffer (expand-file-name ,tempvar))))
-	    (point (point)) to-be-removed)
+	    (,point (point))
+	    ,to-be-removed)
        (save-window-excursion
        (save-window-excursion
 	 (when ,tempvar (find-file ,tempvar))
 	 (when ,tempvar (find-file ,tempvar))
-	 (setq to-be-removed (current-buffer))
+	 (setq ,to-be-removed (current-buffer))
 	 (goto-char (point-min))
 	 (goto-char (point-min))
-	 (while (re-search-forward org-babel-inline-src-block-regexp nil t)
-	   (when (org-babel-active-location-p)
-	     (goto-char (match-beginning 1))
-	     (save-match-data ,@body))
-	   (goto-char (match-end 0))))
-       (unless visited-p (kill-buffer to-be-removed))
-       (goto-char point))))
-(def-edebug-spec org-babel-map-inline-src-blocks (form body))
-
-(defvar org-babel-lob-one-liner-regexp)
+	 (while (re-search-forward "src_\\S-" nil t)
+	   (let ((,datum (save-match-data (org-element-context))))
+	     (when (eq (org-element-type ,datum) 'inline-src-block)
+	       (goto-char (match-beginning 0))
+	       (let ((,end (copy-marker (org-element-property :end ,datum))))
+		 ,@body
+		 (goto-char ,end)
+		 (set-marker ,end nil))))))
+       (unless ,visitedp (kill-buffer ,to-be-removed))
+       (goto-char ,point))))
 
 
 ;;;###autoload
 ;;;###autoload
 (defmacro org-babel-map-call-lines (file &rest body)
 (defmacro org-babel-map-call-lines (file &rest body)
   "Evaluate BODY forms on each call line in FILE.
   "Evaluate BODY forms on each call line in FILE.
 If FILE is nil evaluate BODY forms on source blocks in current
 If FILE is nil evaluate BODY forms on source blocks in current
 buffer."
 buffer."
-  (declare (indent 1))
-  (let ((tempvar (make-symbol "file")))
-    `(let* ((,tempvar ,file)
-	    (visited-p (or (null ,tempvar)
+  (declare (indent 1) (debug (form body)))
+  (org-with-gensyms (datum end point tempvar to-be-removed visitedp)
+    `(let* ((case-fold-search t)
+	    (,tempvar ,file)
+	    (,visitedp (or (null ,tempvar)
 			   (get-file-buffer (expand-file-name ,tempvar))))
 			   (get-file-buffer (expand-file-name ,tempvar))))
-	    (point (point)) to-be-removed)
+	    (,point (point))
+	    ,to-be-removed)
        (save-window-excursion
        (save-window-excursion
 	 (when ,tempvar (find-file ,tempvar))
 	 (when ,tempvar (find-file ,tempvar))
-	 (setq to-be-removed (current-buffer))
+	 (setq ,to-be-removed (current-buffer))
 	 (goto-char (point-min))
 	 (goto-char (point-min))
-	 (while (re-search-forward org-babel-lob-one-liner-regexp nil t)
-	   (when (org-babel-active-location-p)
-	     (goto-char (match-beginning 1))
-	     (save-match-data ,@body))
-	   (goto-char (match-end 0))))
-       (unless visited-p (kill-buffer to-be-removed))
-       (goto-char point))))
-(def-edebug-spec org-babel-map-call-lines (form body))
+	 (while (re-search-forward "call_\\S-\\|^[ \t]*#\\+CALL:" nil t)
+	   (let ((,datum (save-match-data (org-element-context))))
+	     (when (memq (org-element-type ,datum)
+			 '(babel-call inline-babel-call))
+	       (goto-char (match-beginning 0))
+	       (let ((,end (copy-marker (org-element-property :end ,datum))))
+		 ,@body
+		 (goto-char ,end)
+		 (set-marker ,end nil))))))
+       (unless ,visitedp (kill-buffer ,to-be-removed))
+       (goto-char ,point))))
 
 
 ;;;###autoload
 ;;;###autoload
 (defmacro org-babel-map-executables (file &rest body)
 (defmacro org-babel-map-executables (file &rest body)
-  (declare (indent 1))
-  (let ((tempvar (make-symbol "file"))
-	(rx (make-symbol "rx")))
-    `(let* ((,tempvar ,file)
-	    (,rx (concat "\\(" org-babel-src-block-regexp
-			 "\\|" org-babel-inline-src-block-regexp
-			 "\\|" org-babel-lob-one-liner-regexp "\\)"))
-	    (visited-p (or (null ,tempvar)
+  "Evaluate BODY forms on each active Babel code in FILE.
+If FILE is nil evaluate BODY forms on source blocks in current
+buffer."
+  (declare (indent 1) (debug (form body)))
+  (org-with-gensyms (datum end point tempvar to-be-removed visitedp)
+    `(let* ((case-fold-search t)
+	    (,tempvar ,file)
+	    (,visitedp (or (null ,tempvar)
 			   (get-file-buffer (expand-file-name ,tempvar))))
 			   (get-file-buffer (expand-file-name ,tempvar))))
-	    (point (point)) to-be-removed)
+	    (,point (point))
+	    ,to-be-removed)
        (save-window-excursion
        (save-window-excursion
 	 (when ,tempvar (find-file ,tempvar))
 	 (when ,tempvar (find-file ,tempvar))
-	 (setq to-be-removed (current-buffer))
+	 (setq ,to-be-removed (current-buffer))
 	 (goto-char (point-min))
 	 (goto-char (point-min))
-	 (while (re-search-forward ,rx nil t)
-	   (when (org-babel-active-location-p)
-	     (goto-char (match-beginning 1))
-	     (when (looking-at org-babel-inline-src-block-regexp)
-	       (forward-char 1))
-	     (save-match-data ,@body))
-	   (goto-char (match-end 0))))
-       (unless visited-p (kill-buffer to-be-removed))
-       (goto-char point))))
-(def-edebug-spec org-babel-map-executables (form body))
+	 (while (re-search-forward
+		 "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)" nil t)
+	   (let ((,datum (save-match-data (org-element-context))))
+	     (when (memq (org-element-type ,datum)
+			 '(babel-call inline-babel-call inline-src-block
+				      src-block))
+	       (goto-char (match-beginning 0))
+	       (let ((,end (copy-marker (org-element-property :end ,datum))))
+		 ,@body
+		 (goto-char ,end)
+		 (set-marker ,end nil))))))
+       (unless ,visitedp (kill-buffer ,to-be-removed))
+       (goto-char ,point))))
 
 
 ;;;###autoload
 ;;;###autoload
 (defun org-babel-execute-buffer (&optional arg)
 (defun org-babel-execute-buffer (&optional arg)
@@ -1209,7 +1209,8 @@ the current buffer."
   (org-babel-eval-wipe-error-buffer)
   (org-babel-eval-wipe-error-buffer)
   (org-save-outline-visibility t
   (org-save-outline-visibility t
     (org-babel-map-executables nil
     (org-babel-map-executables nil
-      (if (looking-at org-babel-lob-one-liner-regexp)
+      (if (memq (org-element-type (org-element-context))
+		'(babel-call inline-babel-call))
           (org-babel-lob-execute-maybe)
           (org-babel-lob-execute-maybe)
         (org-babel-execute-src-block arg)))))
         (org-babel-execute-src-block arg)))))
 
 

+ 6 - 15
lisp/ob-exp.el

@@ -28,7 +28,6 @@
 (eval-when-compile
 (eval-when-compile
   (require 'cl))
   (require 'cl))
 
 
-(defvar org-babel-lob-one-liner-regexp)
 (defvar org-babel-ref-split-regexp)
 (defvar org-babel-ref-split-regexp)
 
 
 (declare-function org-babel-lob-get-info "ob-lob" (&optional datum))
 (declare-function org-babel-lob-get-info "ob-lob" (&optional datum))
@@ -164,19 +163,11 @@ may make them unreachable."
     (save-excursion
     (save-excursion
       (let ((case-fold-search t)
       (let ((case-fold-search t)
 	    (org-babel-exp-reference-buffer reference-buffer)
 	    (org-babel-exp-reference-buffer reference-buffer)
-	    (regexp (concat org-babel-inline-src-block-regexp "\\|"
-			    org-babel-lob-one-liner-regexp "\\|"
-			    "^[ \t]*#\\+BEGIN_SRC")))
+	    (regexp "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)"))
 	(goto-char (point-min))
 	(goto-char (point-min))
 	(while (re-search-forward regexp nil t)
 	(while (re-search-forward regexp nil t)
 	  (unless (save-match-data (org-in-commented-heading-p))
 	  (unless (save-match-data (org-in-commented-heading-p))
-	    (let* ((element (save-excursion
-			      ;; If match is inline, point is at its
-			      ;; end.  Move backward so
-			      ;; `org-element-context' can get the
-			      ;; object, not the following one.
-			      (backward-char)
-			      (save-match-data (org-element-context))))
+	    (let* ((element (save-match-data (org-element-context)))
 		   (type (org-element-type element))
 		   (type (org-element-type element))
 		   (begin (copy-marker (org-element-property :begin element)))
 		   (begin (copy-marker (org-element-property :begin element)))
 		   (end (copy-marker
 		   (end (copy-marker
@@ -385,7 +376,7 @@ replaced with its value."
 
 
 (defun org-babel-exp-results (info type &optional silent hash)
 (defun org-babel-exp-results (info type &optional silent hash)
   "Evaluate and return the results of the current code block for export.
   "Evaluate and return the results of the current code block for export.
-Results are prepared in a manner suitable for export by org-mode.
+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."
@@ -421,9 +412,9 @@ inhibit insertion of results into the buffer."
 	      (org-babel-execute-src-block nil info))
 	      (org-babel-execute-src-block nil info))
 	    (`lob
 	    (`lob
 	     (save-excursion
 	     (save-excursion
-	       (re-search-backward org-babel-lob-one-liner-regexp nil t)
-	       (let (org-confirm-babel-evaluate)
-		 (org-babel-execute-src-block nil info))))))))))
+	      (goto-char (org-element-property :begin (org-element-context)))
+	      (let (org-confirm-babel-evaluate)
+		(org-babel-execute-src-block nil info))))))))))
 
 
 
 
 (provide 'ob-exp)
 (provide 'ob-exp)

+ 1 - 18
lisp/ob-lob.el

@@ -64,24 +64,7 @@ To add files to this list use the `org-babel-lob-ingest' command."
 	     lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
 	     lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
     lob-ingest-count))
     lob-ingest-count))
 
 
-(defconst org-babel-block-lob-one-liner-regexp
-  (concat
-   "^\\([ \t]*?\\)#\\+call:[ \t]+\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
-   "(\\([^\n]*?\\))\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
-  "Regexp to match non-inline calls to predefined source block functions.")
-
-(defconst org-babel-inline-lob-one-liner-regexp
-  (concat
-   "\\([^\n]*?\\)call_\\([^()[:space:]\n]+?\\)\\(\\[\\(.*?\\)\\]\\|\\(\\)\\)"
-   "(\\(.*?\\))\\(\\[\\(.*?\\)\\]\\)?")
-  "Regexp to match inline calls to predefined source block functions.")
-
-(defconst org-babel-lob-one-liner-regexp
-  (concat "\\(" org-babel-block-lob-one-liner-regexp
-	  "\\|" org-babel-inline-lob-one-liner-regexp "\\)")
-  "Regexp to match calls to predefined source block functions.")
-
-;; functions for executing lob one-liners
+;; Functions for executing lob one-liners.
 
 
 ;;;###autoload
 ;;;###autoload
 (defun org-babel-lob-execute-maybe ()
 (defun org-babel-lob-execute-maybe ()

+ 0 - 1
lisp/ob-ref.el

@@ -123,7 +123,6 @@ the variable."
 		     (point))
 		     (point))
      (point-max))))
      (point-max))))
 
 
-(defvar org-babel-lob-one-liner-regexp)
 (defvar org-babel-library-of-babel)
 (defvar org-babel-library-of-babel)
 (defun org-babel-ref-resolve (ref)
 (defun org-babel-ref-resolve (ref)
   "Resolve the reference REF and return its value."
   "Resolve the reference REF and return its value."