'How can I execute an org-babel file from the command line?
This seems like it'd be straightforward, but I can't find instructions for this anywhere, so I thought I'd ask here. Is there a way to execute a whole buffer's worth of code blocks, in an org file, and save them all in that org file, headlessly, from the command line?
Here's what I've tried so far, but it's not working (just hangs):
emacsclient -a --eval '(find-file "index.org")(org-babel-execute-buffer)(save-buffer)'
Solution 1:[1]
This might be of help:
#!/usr/bin/env sh
":"; exec emacs --quick --script "$0" -- "$@" # -*- mode: emacs-lisp; lexical-binding: t; -*-
(pop argv)
(require 'org-element)
(defun require-lang (lang)
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(straight-use-package 'use-package)
(setq straight-use-package-by-default t)
(pcase (downcase lang)
((or "hy" "hylang") (use-package ob-hy :demand t :straight '(ob-hy :type git :host github :repo "allison-casey/ob-hy") :init (setq org-babel-hy-command "/usr/bin/env hy")))))
(defun message-advice (func &rest args) (interactive)
(let* ((*message (apply #'format args)))
(unless (or (string-prefix-p "executing" *message)
(string-prefix-p "Code block" *message))
(apply func args))))
(advice-add #'message :around #'message-advice)
(defun org-babel-eval-error-notify-advice (exit-code stderr)
"Open a buffer to display STDERR and a message with the value of EXIT-CODE."
(let ((buf (get-buffer-create org-babel-error-buffer-name)))
(with-current-buffer buf
(goto-char (point-max))
(save-excursion (insert stderr))
(message (buffer-string)))
(display-buffer buf))
(message "Babel evaluation exited with code %S" exit-code))
(advice-add #'org-babel-eval-error-notify :override #'org-babel-eval-error-notify-advice)
(let ((org-confirm-babel-evaluate)
(lang-list '()))
(with-temp-buffer
(while argv
(let ((arg (pop argv)))
(pcase arg
((or "-l" "--languages")
(while (and (> (length argv) 1) (not (string-prefix-p "-" (car argv))))
(add-to-list 'lang-list (pop argv) t)))
(_ (setq file arg)))))
(insert-file-contents file)
(mapc 'require-lang lang-list)
(org-babel-execute-buffer)))
More information here:
https://github.com/sylvorg/settings/blob/main/README.org#org-interpreter
There are also a bunch of resources if you search specifically execute org files
; I'd post the links, but I don't know if that would be welcome here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ShadowRylander |