Org Babel

about | blog | config | notes | github

1. Babel: Multi-Language Computing Environment for Literate-Programming/Reproducible-Research

tower_of_babel.png

The Tower of Babel by Pieter Brueghel the Elder

Babel is a powerful extension to Org-mode which adds multi-language Literate Programming to Org files within emacs. My whole configuration basically exists because of the power of this package…

2. Useful Resources

3. Configuration

3.1. Setup Dependencies

Make sure that we load in Org-mode first just to be safe.

(require 'init-org)

3.2. Customize Parameters

Now we confiugre some of the parameters for babel.

(setq org-confirm-babel-evaluate nil)
(setq org-src-fontify-natively t)
(setq org-src-strip-leading-and-trailing-blank-lines t)
(setq org-src-preserve-indentation nil)
(setq org-src-tab-acts-natively t)
(setq org-edit-src-content-indentation 0)

3.3. Languages

(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (python     . t)
   (gnuplot    . t)
   (latex      . t)
   (plantuml   . t)
   (shell      . t)
   (org        . t)
   ))

3.4. Structure Tempalates

I use this only for org-babel at the moment, but maybe there is more to this package.

Structure Template Template Expansion
cf src conf
py src python
el src emacs-lisp
sh src shell
a export ascii
c center
C comment
e example
E export
h export html
l export latex
q quote
s src
v verse
uml plantuml
;; Setup structure templates for org-babel
(require 'org-tempo)
(add-to-list `org-structure-template-alist '("sh" . "src shell"))
(add-to-list `org-structure-template-alist '("el" . "src emacs-lisp"))
(add-to-list `org-structure-template-alist '("py" . "src python"))
(add-to-list `org-structure-template-alist '("cf" . "src conf"))
(add-to-list `org-structure-template-alist '("uml" . "src plantuml"))

3.5. Auto-Tangle Configuration Files

Hook to configure auto-tangling of config files. This needs to be updated to look for my roam tag CONFIG instead.

(defun zamlz/org-babel-tangle-config ()
  (when (or (member "CONFIG" (org-get-tags)) (member "config" (org-get-tags)))
    ;; Dynamic scoping to the rescue
    (let ((org-confirm-babel-evaluate nil))
      (org-babel-tangle))))

(add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook #'zamlz/org-babel-tangle-config)))

3.6. Package Provider

(provide 'init-org-babel)

Created: 2021-11-13

Emacs 26.1 (Org mode 9.5)