emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

per-file (or, really, per buffer) allowing/disallowing code block execut


From: Fedja Beader
Subject: per-file (or, really, per buffer) allowing/disallowing code block execution
Date: Mon, 05 Sep 2022 23:50:49 +0000

Hello,

Pressing C-c C-c in a code block asks the user whether to
execute that code block or not. This soon becomes annoying.
To remedy this, org-mode provides us the variable
org-confirm-babel-evaluate. But this is not very user friendly.

Additionally, as per documentation, this variable only controls
whether org-mode (babel? Forgive me, I am sort of a new user of
Emacs) will execute the code block without asking, or ask.

What I would like to have, to safely and easily use org-mode
as an interactive notebook, is to not have to overload this
function and to be asked only once per buffer/file whether to:
1) Unconditionally allow executing all code blocks
2) Unconditionally disallow executing all code blocks
3) Ask for every block.

Particularly the second case is the one that cannot be
supported by simply defining org-confirm-babel-evaluate.

This is what I cooked up as a solution, with help and advice
from Ian Eure:


#+BEGIN_SRC elisp
(defvar-local org-babel-code-execute-mode nil)

(defun org-babel-check-confirm-evaluate-wrapper (orig-fun &rest args)
  (pcase org-babel-code-execute-mode
    ("always" t)
    ("never" nil)
    ("query" (apply orig-fun args))
    (- (progn
         (setq
           org-babel-code-execute-mode
           (completing-read ; can also return ""
             "Allow execution of code blocks in this buffer (always, never, 
query)?"
             '(always never query)
             nil ; predicate
             t ; require match to one of the three options
           )
         )
         (apply orig-fun args)
       )
    )
  )
)

(advice-add 'org-babel-check-confirm-evaluate :around 
#'org-babel-check-confirm-evaluate-wrapper)
#+END_SRC

I would like this functionality to become part of org-mode
in the future.

Sincerely,
Fedja



reply via email to

[Prev in Thread] Current Thread [Next in Thread]