diff options
| -rw-r--r-- | modules/org-babel-config.el | 23 | ||||
| -rw-r--r-- | tests/test-org-babel-config.el | 35 |
2 files changed, 47 insertions, 11 deletions
diff --git a/modules/org-babel-config.el b/modules/org-babel-config.el index 2c52ae46..b3a3036a 100644 --- a/modules/org-babel-config.el +++ b/modules/org-babel-config.el @@ -19,7 +19,7 @@ (setq org-src-fontify-natively t) ;; fontify the code in blocks (setq org-src-tab-acts-natively t) ;; tabs act like in language major mode buffer (setq org-src-window-setup 'current-window) ;; don't split window when source editing wih C-c ' - (setq org-confirm-babel-evaluate nil) ;; just evaluate the source code + (setq org-confirm-babel-evaluate t) ;; confirm before running babel; toggle with C-; k (setq org-babel-default-header-args (cons '(:tangle . "yes") (assq-delete-all :tangle org-babel-default-header-args)))) ;; default header args for babel @@ -28,16 +28,17 @@ ;; ------------------- Babel Execution Confirmation Toggle ------------------- ;; org-babel verifies before each execution -(defun babel-confirm (flag) - "Report the setting of `org-confirm-babel-evaluate'. - -If invoked with \[universal-argument], toggle the setting based on FLAG. -FLAG is the raw prefix argument passed interactively." - (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"))) +(defun cj/org-babel-toggle-confirm () + "Toggle whether Org babel blocks are confirmed before evaluation. +`org-confirm-babel-evaluate' defaults to t (confirm), which is the safe default +for files from cloned repos, web clips, or downloads. Flip it off for the +session when working in trusted files, and back on when done." + (interactive) + (setq org-confirm-babel-evaluate (not org-confirm-babel-evaluate)) + (message "Babel evaluation confirmation %s" + (if org-confirm-babel-evaluate "ON" "OFF"))) + +(keymap-global-set "C-; k" #'cj/org-babel-toggle-confirm) ;; ---------------------------- Org Babel Languages ---------------------------- diff --git a/tests/test-org-babel-config.el b/tests/test-org-babel-config.el new file mode 100644 index 00000000..b62a9422 --- /dev/null +++ b/tests/test-org-babel-config.el @@ -0,0 +1,35 @@ +;;; test-org-babel-config.el --- Tests for babel confirmation toggle -*- lexical-binding: t; -*- + +;;; Commentary: +;; Covers cj/org-babel-toggle-confirm, which flips `org-confirm-babel-evaluate' +;; between t (the safe default — confirm before running a block) and nil, and +;; the C-; k binding that invokes it. + +;;; Code: + +(require 'ert) +(require 'org-babel-config) + +;; org defines this as a defcustom, but org is not loaded in batch; declare it +;; special here so the let-bindings below are dynamic. +(defvar org-confirm-babel-evaluate t) + +(ert-deftest test-org-babel-toggle-confirm-flips-from-t-to-nil () + "Normal: toggling when confirmation is on turns it off." + (let ((org-confirm-babel-evaluate t)) + (cj/org-babel-toggle-confirm) + (should-not org-confirm-babel-evaluate))) + +(ert-deftest test-org-babel-toggle-confirm-flips-from-nil-to-t () + "Normal: toggling when confirmation is off turns it on." + (let ((org-confirm-babel-evaluate nil)) + (cj/org-babel-toggle-confirm) + (should (eq t org-confirm-babel-evaluate)))) + +(ert-deftest test-org-babel-toggle-confirm-bound-to-key () + "Smoke: C-; k invokes the toggle command." + (should (eq (keymap-lookup (current-global-map) "C-; k") + #'cj/org-babel-toggle-confirm))) + +(provide 'test-org-babel-config) +;;; test-org-babel-config.el ends here |
