From mboxrd@z Thu Jan 1 00:00:00 1970 From: Aaron Ecay Subject: [PATCH 1/4] Clean up various org-babel-*-maybe commands Date: Thu, 18 Apr 2013 04:06:55 -0400 Message-ID: <1366272415-16823-1-git-send-email-aaronecay@gmail.com> References: <87li8gs4kn.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([208.118.235.92]:42383) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USjs5-0007xN-4e for emacs-orgmode@gnu.org; Thu, 18 Apr 2013 04:07:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USjs2-0005YZ-ME for emacs-orgmode@gnu.org; Thu, 18 Apr 2013 04:07:08 -0400 Received: from mail-ve0-x22c.google.com ([2607:f8b0:400c:c01::22c]:41740) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USjs2-0005YR-FX for emacs-orgmode@gnu.org; Thu, 18 Apr 2013 04:07:06 -0400 Received: by mail-ve0-f172.google.com with SMTP id db10so6238veb.3 for ; Thu, 18 Apr 2013 01:07:06 -0700 (PDT) In-Reply-To: <87li8gs4kn.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org * lisp/ob-core.el (org-babel-when-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 | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index b8d93f2..e44fc02 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -351,15 +351,25 @@ 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) + "Execute BODY if point is in a source block and return t. + +Otherwise do nothing and return nil." + `(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 () @@ -395,10 +405,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 () @@ -406,10 +414,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) @@ -419,8 +425,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-switch-to-session current-prefix-arg))) (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe) -- 1.8.2.1