jupyter icon indicating copy to clipboard operation
jupyter copied to clipboard

How do I know when `org-babel-execute-src-block` fails due to an exception?

Open NightMachinery opened this issue 2 years ago • 0 comments

How do I know when org-babel-execute-src-block fails due to an exception?

Currently, it seems to always return nil.

My use case is that I want execution of subsequent cells to stop if there is an exception:

  (cl-defun night/h-org-babel-execute-with-move-fn
      (&key
       (move-fn (lambda () nil))
       (no-blocks-message "No source blocks found."))
    "Execute org-mode source blocks in a specified direction using MOVE-FN.
MOVE-FN should be a function that moves to the next or previous source block.
NO-BLOCKS-MESSAGE is a message string to display if no blocks are found in the specified direction."
    (cl-block 'blk
      (save-excursion
        ;; If not inside a source block, attempt to find the next or previous one.
        (when (not (org-in-src-block-p))
          (unless (funcall move-fn)
            (message no-blocks-message)
            (cl-return-from blk)))

        ;; Begin execution of source blocks.
        (while (org-in-src-block-p)
          (org-babel-execute-src-block)
          (unless (funcall move-fn)
            (cl-return-from blk)))
        (message "Execution of source blocks completed."))))

  (defun night/org-babel-execute-after ()
    "Execute the current org-mode source block and all following ones."
    (interactive)
    (night/h-org-babel-execute-with-move-fn
     :move-fn 'org-babel-next-src-block
     :no-blocks-message "No source blocks found ahead."))

  (defun night/org-babel-execute-before ()
    "Execute the current org-mode source block and all preceding ones."
    (interactive)
    (night/h-org-babel-execute-with-move-fn
     :move-fn 'org-babel-previous-src-block
     :no-blocks-message "No source blocks found before."))

NightMachinery avatar Oct 10 '23 19:10 NightMachinery