From 993dda2b88220a994422becdef11773986094ce6 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Wed, 30 Jun 2010 12:25:22 -0700 Subject: [PATCH 1/2] babel: evaluation of code blocks now requires confirmation * lisp/babel/ob.el (org-confirm-babel-evaluate): variable used to control evaluation of code blocks, default value it t, meaning all code block evaluation requires confirmation (org-babel-confirm-evaluate): function used to request confirmation of code block evaluation from the user (org-babel-execute-src-block-maybe): now requires explicit user confirmation before evaluating a code block (org-babel-open-src-block-result): now requires explicit user confirmation before evaluating a code block * lisp/org.el (org-ctrl-c-ctrl-c): added documentation of code block evaluation behavior --- lisp/babel/ob.el | 76 +++++++++++++++++++++++++++++++++++++---------------- lisp/org.el | 7 ++++- 2 files changed, 59 insertions(+), 24 deletions(-) diff --git a/lisp/babel/ob.el b/lisp/babel/ob.el index 6a42bcc..e2ffd67 100644 --- a/lisp/babel/ob.el +++ b/lisp/babel/ob.el @@ -61,6 +61,18 @@ (declare-function org-babel-ref-variables "ob-ref" (params)) (declare-function org-babel-ref-resolve-reference "ob-ref" (ref &optional params)) +(defcustom org-confirm-babel-evaluate t + "Require confirmation before interactively evaluating code +blocks in Org-mode buffers. The default value of this variable +is t, meaning confirmation is required for any code block +evaluation. This variable can be set to nil to inhibit any +future confirmation requests. + +Note disabling confirmation may result in accidental evaluation +of potentially harmful code." + :group 'org-babel + :type 'boolean) + (defvar org-babel-source-name-regexp "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*" "Regular expression used to match a source name line.") @@ -134,6 +146,19 @@ added to the header-arguments-alist." (org-babel-parse-inline-src-block-match) nil)))) +(defun org-babel-confirm-evaluate (&optional info) + "Confirm that the user wishes to evaluate the code block +defined by INFO. This behavior can be suppressed by setting the +value of `org-confirm-babel-evaluate' to nil, in which case all +future interactive code block evaluations will proceed without +any confirmation from the user. + +Note disabling confirmation may result in accidental evaluation +of potentially harmful code." + (or (not org-confirm-babel-evaluate) + (yes-or-no-p (format "Evaluate this%scode on your system?" + (if info (format " %s " (nth 0 info)) " "))))) + ;;;###autoload (defun org-babel-execute-src-block-maybe () "Detect if this is context for a org-babel src-block and if so @@ -141,7 +166,10 @@ then run `org-babel-execute-src-block'." (interactive) (let ((info (org-babel-get-src-block-info))) (if info - (progn (org-babel-execute-src-block current-prefix-arg info) t) nil))) + (if (org-babel-confirm-evaluate info) + (progn (org-babel-execute-src-block current-prefix-arg info) t) + (error "evaluation aborted")) + nil))) (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe) @@ -379,28 +407,30 @@ argument RE-RUN the source-code block is evaluated even if results already exist." (interactive "P") (when (org-babel-get-src-block-info) - (save-excursion - ;; go to the results, if there aren't any then run the block - (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result)) - (progn (org-babel-execute-src-block) - (org-babel-where-is-src-block-result)))) - (end-of-line 1) - (while (looking-at "[\n\r\t\f ]") (forward-char 1)) - ;; open the results - (if (looking-at org-bracket-link-regexp) - ;; file results - (org-open-at-point) - (let ((results (org-babel-read-result))) - (flet ((echo-res (result) - (if (stringp result) result (format "%S" result)))) - (pop-to-buffer (get-buffer-create "org-babel-results")) - (delete-region (point-min) (point-max)) - (if (listp results) - ;; table result - (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res))) - ;; scalar result - (insert (echo-res results)))))) - t))) + (if (org-babel-confirm-evaluate) + (save-excursion + ;; go to the results, if there aren't any then run the block + (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result)) + (progn (org-babel-execute-src-block) + (org-babel-where-is-src-block-result)))) + (end-of-line 1) + (while (looking-at "[\n\r\t\f ]") (forward-char 1)) + ;; open the results + (if (looking-at org-bracket-link-regexp) + ;; file results + (org-open-at-point) + (let ((results (org-babel-read-result))) + (flet ((echo-res (result) + (if (stringp result) result (format "%S" result)))) + (pop-to-buffer (get-buffer-create "org-babel-results")) + (delete-region (point-min) (point-max)) + (if (listp results) + ;; table result + (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res))) + ;; scalar result + (insert (echo-res results)))))) + t) + (error "evaluation aborted")))) ;;;###autoload (defun org-babel-execute-buffer (&optional arg) diff --git a/lisp/org.el b/lisp/org.el index f2d9ade..22aaef8 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -17011,7 +17011,12 @@ This command does many different things, depending on context: - If the cursor is on a numbered item in a plain list, renumber the ordered list. -- If the cursor is on a checkbox, toggle it." +- If the cursor is on a checkbox, toggle it. + +- If the cursor is on a code block, evaluate it. The variable + `org-confirm-babel-evaluate' can be used to control prompting + before code block evaluation, by default every code block + evaluation requires confirmation." (interactive "P") (let ((org-enable-table-editor t)) (cond -- 1.7.0.4