瀏覽代碼

DONE optionally evaluate header references when we switch to =*Org Edit Src*= buffer

  If `org-edit-special' is called with a prefix argument from inside a
  source-code block which has a session, then the session is prepared
  according to the header arguments of the source-code blocks.

  This is implemented at a `defadvice' because the org-edit-src-hook
  is called from inside of the edit buffer, rather than the org
  buffer, and it wasn't clear how to access a prefix arg from inside
  of the hook.
Eric Schulte 16 年之前
父節點
當前提交
aa8bcd9f38
共有 2 個文件被更改,包括 51 次插入24 次删除
  1. 35 21
      lisp/org-babel.el
  2. 16 3
      org-babel.org

+ 35 - 21
lisp/org-babel.el

@@ -40,6 +40,20 @@ then run `org-babel-execute-src-block'."
 
 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
 
+(defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
+  "Prepare the current source block's session according to it's
+header arguments before editing in an org-src buffer.  This
+function is called when `org-edit-special' is called with a
+prefix argument from inside of a source-code block."
+  (when current-prefix-arg
+    (let* ((info (org-babel-get-src-block-info))
+           (lang (first info))
+           (params (third info))
+           (session (cdr (assoc :session params))))
+      (when (and info session) ;; if we are in a source-code block which has a session
+        (funcall (intern (concat "org-babel-prep-session:" lang)) session params))))
+  ad-do-it)
+
 (defadvice org-open-at-point (around org-babel-open-at-point activate)
   "If `point' is on a source code block, then open that block's
 results with `org-babel-open-src-block-results', otherwise defer
@@ -131,27 +145,6 @@ lisp code use the `org-babel-add-interpreter' function."
 	      (const "ruby")))
 
 ;;; functions
-(defun org-babel-pop-to-session (&optional arg info)
-  "Pop to the session of the current source-code block.  If
-called with a prefix argument then evaluate the header arguments
-for the source block before entering the session.  Copy the body
-of the source block to the kill ring."
-  (interactive)
-  (let* ((info (or info (org-babel-get-src-block-info)))
-         (lang (first info))
-         (body (second info))
-         (params (third info))
-         (session (cdr (assoc :session params))))
-    (unless (member lang org-babel-interpreters)
-      (error "Language is not in `org-babel-interpreters': %s" lang))
-    ;; copy body to the kill ring
-    (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
-    ;; if called with a prefix argument, then process header arguments
-    (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
-    ;; just to the session using pop-to-buffer
-    (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
-    (move-end-of-line 1)))
-
 (defun org-babel-execute-src-block (&optional arg info params)
   "Execute the current source code block, and dump the results
 into the buffer immediately following the block.  Results are
@@ -188,6 +181,27 @@ the header arguments specified at the source code block."
     (org-babel-insert-result result result-params)
     (case result-type (output nil) (value result))))
 
+(defun org-babel-pop-to-session (&optional arg info)
+  "Pop to the session of the current source-code block.  If
+called with a prefix argument then evaluate the header arguments
+for the source block before entering the session.  Copy the body
+of the source block to the kill ring."
+  (interactive)
+  (let* ((info (or info (org-babel-get-src-block-info)))
+         (lang (first info))
+         (body (second info))
+         (params (third info))
+         (session (cdr (assoc :session params))))
+    (unless (member lang org-babel-interpreters)
+      (error "Language is not in `org-babel-interpreters': %s" lang))
+    ;; copy body to the kill ring
+    (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
+    ;; if called with a prefix argument, then process header arguments
+    (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
+    ;; just to the session using pop-to-buffer
+    (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
+    (move-end-of-line 1)))
+
 (defun org-babel-open-src-block-result (&optional re-run)
   "If `point' is on a src block then open the results of the
 source code block, otherwise return nil.  With optional prefix

+ 16 - 3
org-babel.org

@@ -269,8 +269,8 @@ but with preference given to
    mechanism similar to :export: and :noexport: to control evaluation
    on a per-subtree basis.
    
-** TODO support for working with =*Org Edit Src Example*= buffers [3/5]
-*** Patch against org source. 
+** TODO support for working with =*Org Edit Src Example*= buffers [4/6]
+*** STARTED Patch against org source. 
     I've worked on several related changes to source code edit buffer
     behaviour in the org core.  My current patch (below) does the
     following. Detailed explanation / working notes are below.
@@ -458,7 +458,7 @@ index 2083c77..2be21e6 100644
 
 *** DONE name edit buffer according to #+srcname (and language?)
     See above patch agains org.
-*** TODO optionally evaluate header references when we switch to =*Org Edit Src*= buffer
+*** DONE optionally evaluate header references when we switch to =*Org Edit Src*= buffer
 That seems to imply that the header references need to be evaluated
 and transformed into the target language object when we hit C-c ' to
 enter the *Org Edit Src* buffer [DED]
@@ -485,6 +485,19 @@ Yes [Dan]
 org-babel should do this by using the hook to make sure that, when C-c
 C-' is issued on a source block, any references are resolved and
 assignments are made in the appropriate session.
+
+#+tblname: my-little-table
+| 1 | 2 |
+| 3 | 4 |
+
+#+srcname: resolve-vars-on-edit
+#+begin_src ruby :var table=my-little-table :results silent :session test
+  table.size.times.do |n|
+    puts n
+  end
+#+end_src
+
+
 *** TODO set buffer-local-process variables appropriately [DED]
     I think something like this would be great. You've probably
 already thought of this, but just to note it down: it would be really