What is the standard method to debug emacs-lisp orgmode source code blocks with input variables as header arguments?

I found the hacky solution to insert the following snipped in front of a source code block.

(when t ;; t: do debug, nil: do not debug
  (let ((parse-sexp-ignore-comments t))
    (goto-char (point-min))
    (forward-sexp 2)

Debugging of the source code block can be turned on or off by the boolean on the first line. The org-edit-src-code makes sure that point is in the special source edit buffer when one executes the source code block. The snippet puts point in front of the actual code and enters edebug-defun.

There follows a full working example:

* edebug emacs-lisp source code block with input variables

  Example input data:
  #+TBLNAME: input-table
  | This   | table       | contains |
  | some   | strings     | for      |
  | trying | source code | blocks   |

  Further example input data (strings to be highlighted):
  #+TBLNAME: emph
  | table | strings | blocks |

  The code-block to be debugged:
  #+NAME: emphDebug
  #+BEGIN_SRC emacs-lisp :var input=input-table emph=emph
(when t ;; t: do debug, nil: do not debug
  (let ((parse-sexp-ignore-comments t))
    (goto-char (point-min))
    (forward-sexp 2)
;; The actual program starts here.
(let ((re (regexp-opt (car emph))))
  (mapcar (lambda (row)
        (mapcar (lambda (col)
              (if (string-match re col)
              (concat "*" (match-string 0 col) "*")

  #+RESULTS: emphDebug
  | This   | *table*     | contains |
  | some   | *strings*   | for      |
  | trying | source code | *blocks* |

Place point on the #+BEGIN_SRC line and press C-c C-c to start debugging.

If one often needs to debug source code blocks one can define the following function somewhere in his configuration file:

(defun org-src-debug ()
  "Put a call to this function at the beginning of the org source block to debug it."
    (let ((pt (let ((case-fold-search t)) (org-babel-where-is-src-block-head))))
      (unless pt (error "Not at source block"))
      (goto-char pt)
      (let ((parse-sexp-ignore-comments t))
        (goto-char (point-min))
        (forward-sexp 2)

This function can then be used at the beginning of source code blocks. Example:

#+BEGIN_SRC emacs-lisp :var test='(1 2 3)
   (message "arg: %s" test)

But, I guess there is some org-standard way for debugging which I am not yet aware of.

  • No standard way I know of: I usually write and debug the code outside of org and then create the source block. – NickD Jun 22 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.