home

Easy org-babel mode

Recently I thought that writing about Cuelang and thought “hey, wouldn’t it be nice if I could put some Cue code into Org and evaluate it so that I could showcase the result”?

Well, it’s a shame ob-cuelang doesn’t exist. However, thanks to the LLMs I could vibe code it, right? And so, I steeled my nerves, thought hours about the prompt and, after 30 minutes and 2 glasses I put nervously:

“Make me ob-cue implementation”

I’ve looked as Google Gemini’s circle spun and started spitting up long lines of comments and then… it presented exactly one function 1

(require 'ob-core)

(defun org-babel-execute:cue (body params)
  "Execute a block of CUE code with Org Babel.
This function is called by `org-babel-execute-src-block'.
The `body' argument is fully expanded with noweb references before
this function is called."
  (let* ((cmd (or (cdr (assoc :cmd params)) "eval"))
         (flags-raw (cdr (assoc :flags params)))
         (flags (if flags-raw (format " %s" flags-raw) ""))
         ;; The \"-\" tells the cue command to read from stdin.
         (shell-command (format "cue -E  %s %s - 2>&1" cmd flags)))
    (org-babel-eval shell-command body)))
(provide 'ob-cue)
;;; ob-cue.el ends here

Ummmm…. wait.. that’s it? This couldn’t be right, can it be? But.. it works. I always thought that there is some more magic required but what this code does is:

;; Parse "cmd" flag or use eval
(cmd (or (cdr (assoc :cmd params)) "eval"))
;; Take "flags" flag
(flags-raw (cdr (assoc :flags params)))
;; Combine them or set it to ""  if not exist
(shell-command (format "cue -E  %s %s - 2>&1" cmd flags))
;; Run body in the org-babel and shell-command context
(org-babel-eval shell-command body)

That’s it! I always thought it’s going to be much harded. It works with noweb context, and session probably could be skipped in favor of noweb.

Cue demo:

a: 2 // with name: base

and…

<<base>>
b: _ & a

Yields:

a: 2
b: 2

Hope that helps anyone, happy ob-ing! :)


  1. Added -E flag and renamed from cuelang to cue ↩︎

Przemysław Alexander Kamiński
vel xlii vel exlee

gh | li | rss

Powered by hugo and hugo-theme-nostyleplease.