Re: [O] [PATCH 03/10] Clean up various org-babel-*-maybe commands
Hi Eric, 2013ko apirilak 20an, Eric Schulte-ek idatzi zuen: Could re-calculating the info cause referenced blocks to be executed more than once? If so then we should continue passing the info and *not* simply re-calculate it later on. This is a very good question. I will look into it and send an update when I have found out the answer. Thanks, -- Aaron Ecay
Re: [O] [PATCH 03/10] Clean up various org-babel-*-maybe commands
Aaron Ecay aarone...@gmail.com writes: Hi Bastien, Thanks for your comments. 2013ko apirilak 3an, Bastien-ek idatzi zuen: [...] org-babel-get-src-block-info is a potentially expensive operation, which is why its ‘light’ argument exists. But in any case, it is overkill to query the whole info, if all that is needed is whether point is in a block or not. Factor the simplified common code out into a macro. The let-bound info variable is not only used to check if we are within a src block, it is also passed as an argument to functions, see the ^^ marks below. All of these functions will re-calculate the info if it is not passed, using org-babel-get-src-block-info. So not passing it does no harm. Could re-calculating the info cause referenced blocks to be executed more than once? If so then we should continue passing the info and *not* simply re-calculate it later on. Cheers, --- lisp/ob-core.el | 31 +-- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 723aa9d..283628e 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -365,15 +365,22 @@ of potentially harmful code. (or (org-babel-execute-src-block-maybe) (org-babel-lob-execute-maybe))) +(defmacro org-babel-when-in-src-block (rest body) + `(if (or (org-babel-where-is-src-block-head) + (org-babel-get-inline-src-block-matches)) + (progn +,@body +t) + nil)) (Please always add a docstring of defuns and defmacros) I’ll resend the patch with a docstring and fixing the commit message problems you and Achim pointed out. [...] @@ -433,8 +436,8 @@ then run `org-babel-load-in-session'. Detect if this is context for a org-babel src-block and if so then run `org-babel-pop-to-session'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil))) + (org-babel-when-in-src-block + (org-babel-pop-to-session current-prefix-arg))) (Let's use the current name `org-babel-switch-to-session' instead of the obsolete alias.) OK. Maybe we don't always need to pass the info as an argument, but at least for this last example it is needed. o-b-switch-to-session does nothing with the info argument but pass it along to o-b-initiate-session, which will recalculate it if it is missing. So it takes 2 hops in contrast to the 1 in the other cases, but it still gets recalculated. -- Eric Schulte http://cs.unm.edu/~eschulte
Re: [O] [PATCH 03/10] Clean up various org-babel-*-maybe commands
Hi Bastien, Thanks for your comments. 2013ko apirilak 3an, Bastien-ek idatzi zuen: [...] org-babel-get-src-block-info is a potentially expensive operation, which is why its ‘light’ argument exists. But in any case, it is overkill to query the whole info, if all that is needed is whether point is in a block or not. Factor the simplified common code out into a macro. The let-bound info variable is not only used to check if we are within a src block, it is also passed as an argument to functions, see the ^^ marks below. All of these functions will re-calculate the info if it is not passed, using org-babel-get-src-block-info. So not passing it does no harm. --- lisp/ob-core.el | 31 +-- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 723aa9d..283628e 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -365,15 +365,22 @@ of potentially harmful code. (or (org-babel-execute-src-block-maybe) (org-babel-lob-execute-maybe))) +(defmacro org-babel-when-in-src-block (rest body) + `(if (or (org-babel-where-is-src-block-head) + (org-babel-get-inline-src-block-matches)) + (progn + ,@body + t) + nil)) (Please always add a docstring of defuns and defmacros) I’ll resend the patch with a docstring and fixing the commit message problems you and Achim pointed out. [...] @@ -433,8 +436,8 @@ then run `org-babel-load-in-session'. Detect if this is context for a org-babel src-block and if so then run `org-babel-pop-to-session'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil))) + (org-babel-when-in-src-block + (org-babel-pop-to-session current-prefix-arg))) (Let's use the current name `org-babel-switch-to-session' instead of the obsolete alias.) OK. Maybe we don't always need to pass the info as an argument, but at least for this last example it is needed. o-b-switch-to-session does nothing with the info argument but pass it along to o-b-initiate-session, which will recalculate it if it is missing. So it takes 2 hops in contrast to the 1 in the other cases, but it still gets recalculated. -- Aaron Ecay
Re: [O] [PATCH 03/10] Clean up various org-babel-*-maybe commands
Hi Aaron, Aaron Ecay aarone...@gmail.com writes: * lisp/ob-core.el (org-babel-if-in-src-block): New macro (org-babel-execute-src-block-maybe), (org-babel-expand-src-block-maybe), (org-babel-load-in-session-maybe), (org-babel-pop-to-session-maybe): Use it A slightly enhanced version: * lisp/ob-core.el (org-babel-if-in-src-block): New macro. (org-babel-execute-src-block-maybe) (org-babel-expand-src-block-maybe) (org-babel-load-in-session-maybe, org-babel-pop-to-session-maybe): Use it. In a nutshell: 1. No commas outside parentheses; 2. A full-stop at the end of sentences... C-x 4 a and M-q should be all what you need. org-babel-get-src-block-info is a potentially expensive operation, which is why its ‘light’ argument exists. But in any case, it is overkill to query the whole info, if all that is needed is whether point is in a block or not. Factor the simplified common code out into a macro. The let-bound info variable is not only used to check if we are within a src block, it is also passed as an argument to functions, see the ^^ marks below. --- lisp/ob-core.el | 31 +-- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 723aa9d..283628e 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -365,15 +365,22 @@ of potentially harmful code. (or (org-babel-execute-src-block-maybe) (org-babel-lob-execute-maybe))) +(defmacro org-babel-when-in-src-block (rest body) + `(if (or (org-babel-where-is-src-block-head) + (org-babel-get-inline-src-block-matches)) + (progn + ,@body + t) + nil)) (Please always add a docstring of defuns and defmacros) (defun org-babel-execute-src-block-maybe () Conditionally execute a source block. Detect if this is context for a Babel src-block and if so then run `org-babel-execute-src-block'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info - (progn (org-babel-eval-wipe-error-buffer) -(org-babel-execute-src-block current-prefix-arg info) t) nil))) + (org-babel-when-in-src-block + (org-babel-eval-wipe-error-buffer) + (org-babel-execute-src-block current-prefix-arg))) ;;;###autoload (defun org-babel-view-src-block-info () @@ -409,10 +416,8 @@ a window into the `org-babel-get-src-block-info' function. Detect if this is context for a org-babel src-block and if so then run `org-babel-expand-src-block'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info - (progn (org-babel-expand-src-block current-prefix-arg info) t) - nil))) + (org-babel-when-in-src-block + (org-babel-expand-src-block current-prefix-arg))) ;;;###autoload (defun org-babel-load-in-session-maybe () @@ -420,10 +425,8 @@ then run `org-babel-expand-src-block'. Detect if this is context for a org-babel src-block and if so then run `org-babel-load-in-session'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info - (progn (org-babel-load-in-session current-prefix-arg info) t) - nil))) + (org-babel-when-in-src-block + (org-babel-load-in-session current-prefix-arg))) (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe) @@ -433,8 +436,8 @@ then run `org-babel-load-in-session'. Detect if this is context for a org-babel src-block and if so then run `org-babel-pop-to-session'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil))) + (org-babel-when-in-src-block + (org-babel-pop-to-session current-prefix-arg))) (Let's use the current name `org-babel-switch-to-session' instead of the obsolete alias.) Maybe we don't always need to pass the info as an argument, but at least for this last example it is needed. Thanks, -- Bastien
Re: [O] [PATCH 03/10] Clean up various org-babel-*-maybe commands
Aaron Ecay writes: * lisp/ob-core.el (org-babel-if-in-src-block): New macro […] +(defmacro org-babel-when-in-src-block (rest body) + `(if (or (org-babel-where-is-src-block-head) + (org-babel-get-inline-src-block-matches)) + (progn + ,@body + t) + nil)) Commit message and patch disagree about the name. Regards, Achim. -- +[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]+ Factory and User Sound Singles for Waldorf rackAttack: http://Synth.Stromeko.net/Downloads.html#WaldorfSounds
[O] [PATCH 03/10] Clean up various org-babel-*-maybe commands
* lisp/ob-core.el (org-babel-if-in-src-block): New macro (org-babel-execute-src-block-maybe), (org-babel-expand-src-block-maybe), (org-babel-load-in-session-maybe), (org-babel-pop-to-session-maybe): Use it org-babel-get-src-block-info is a potentially expensive operation, which is why its ‘light’ argument exists. But in any case, it is overkill to query the whole info, if all that is needed is whether point is in a block or not. Factor the simplified common code out into a macro. --- lisp/ob-core.el | 31 +-- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 723aa9d..283628e 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -365,15 +365,22 @@ of potentially harmful code. (or (org-babel-execute-src-block-maybe) (org-babel-lob-execute-maybe))) +(defmacro org-babel-when-in-src-block (rest body) + `(if (or (org-babel-where-is-src-block-head) + (org-babel-get-inline-src-block-matches)) + (progn +,@body +t) + nil)) + (defun org-babel-execute-src-block-maybe () Conditionally execute a source block. Detect if this is context for a Babel src-block and if so then run `org-babel-execute-src-block'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info - (progn (org-babel-eval-wipe-error-buffer) - (org-babel-execute-src-block current-prefix-arg info) t) nil))) + (org-babel-when-in-src-block + (org-babel-eval-wipe-error-buffer) + (org-babel-execute-src-block current-prefix-arg))) ;;;###autoload (defun org-babel-view-src-block-info () @@ -409,10 +416,8 @@ a window into the `org-babel-get-src-block-info' function. Detect if this is context for a org-babel src-block and if so then run `org-babel-expand-src-block'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info - (progn (org-babel-expand-src-block current-prefix-arg info) t) - nil))) + (org-babel-when-in-src-block + (org-babel-expand-src-block current-prefix-arg))) ;;;###autoload (defun org-babel-load-in-session-maybe () @@ -420,10 +425,8 @@ then run `org-babel-expand-src-block'. Detect if this is context for a org-babel src-block and if so then run `org-babel-load-in-session'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info - (progn (org-babel-load-in-session current-prefix-arg info) t) - nil))) + (org-babel-when-in-src-block + (org-babel-load-in-session current-prefix-arg))) (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe) @@ -433,8 +436,8 @@ then run `org-babel-load-in-session'. Detect if this is context for a org-babel src-block and if so then run `org-babel-pop-to-session'. (interactive) - (let ((info (org-babel-get-src-block-info))) -(if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil))) + (org-babel-when-in-src-block + (org-babel-pop-to-session current-prefix-arg))) (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe) -- 1.8.2