Setting the Babel Evaluate Confirm Status

Because Babel provides a facility to execute arbitrary code, it presents a security risk. Code blocks are evaluated when a document is exported as well as when a user explicitly asks for evaluation by typing C-c C-c in the block. To prevent an unwary user from unintentionally executing malicious code, Babel asks for confirmation before executing any code block. That’s a sensible precaution and one that I leave on even though it can be disabled.

There are times, however, when it’s really inconvenient. For example, in the Calling Babel From A Table post, Babel asks for confirmation for each table cell. In that case, I wanted to turn off the confirmation request. You do that by setting org-confirm-babel-evaluate to nil. That’s a long name and if a week goes by without my using it, I won’t remember the name and will have to look it up. It’s also a pain to type. Also, there’s no way to check whether you forgot to turn it back on other than by looking at org-confirm-babel-evaluate.

Here’s a little function that does all that in a convenient way and that has a name short enough to remember. Just add it to your .emacs or init.el file.

(defun babel-confirm (flag)
  "Report the setting of org-confirm-babel-evaluate.
If invoked with C-u, toggle the setting"
  (interactive "P")
  (if (equal flag '(4))
      (setq org-confirm-babel-evaluate (not org-confirm-babel-evaluate)))
  (message "Babel evaluation confirmation is %s"
           (if org-confirm-babel-evaluate "on" "off")))

Calling babel-confirm will tell you if confirmation is on or off. If you give it a prefix argument (call it as C-u M-x babel-confirm) it will toggle the setting and tell you what the new setting is.

If this isn’t fine enough control, you can also set org-confirm-babel-evaluate to a function. The function is passed the language of the code block and the block itself. The function can decide if the block is safe or not and return t to have Babel ask for confirmation or nil to just execute the block. All this is explained in the Org manual’s Miscellaneous section.

This entry was posted in Programming and tagged . Bookmark the permalink.