2017-12-08 22:59:42 -05:00
|
|
|
;;; lang/org/+babel.el -*- lexical-binding: t; -*-
|
2017-04-11 08:28:02 -04:00
|
|
|
|
2018-01-08 20:38:46 -05:00
|
|
|
(add-hook 'org-load-hook #'+org|init-babel)
|
|
|
|
|
2018-01-28 17:20:23 -05:00
|
|
|
(defvar +org-babel-mode-alist
|
2018-05-31 17:25:59 +02:00
|
|
|
'((cpp . C)
|
|
|
|
(C++ . C)
|
|
|
|
(D . C)
|
|
|
|
(sh . shell)
|
|
|
|
(bash . shell)
|
|
|
|
(matlab . octave))
|
2018-01-28 17:20:23 -05:00
|
|
|
"An alist that maps languages to babel libraries. This is necessary for babel
|
|
|
|
libraries (ob-*.el) that don't match the name of the language.")
|
|
|
|
|
2018-05-30 18:11:12 +02:00
|
|
|
(defvar +org-babel-load-functions ()
|
|
|
|
"A list of functions that are used to try to load the current executing src
|
|
|
|
block. They take one argument (the language specified in the src block, as
|
|
|
|
string). Stops at the first function to return non-nil.")
|
|
|
|
|
2018-01-08 20:38:46 -05:00
|
|
|
(defun +org|init-babel ()
|
2017-07-05 02:33:41 +02:00
|
|
|
(setq org-src-fontify-natively t ; make code pretty
|
|
|
|
org-src-preserve-indentation t ; use native major-mode indentation
|
2017-04-11 08:28:02 -04:00
|
|
|
org-src-tab-acts-natively t
|
|
|
|
org-src-window-setup 'current-window
|
2017-07-05 02:33:41 +02:00
|
|
|
org-confirm-babel-evaluate nil) ; you don't need my permission
|
2017-04-11 08:28:02 -04:00
|
|
|
|
2018-05-31 17:25:59 +02:00
|
|
|
(defun +org*babel-lazy-load-library (orig-fn info)
|
2018-01-28 15:30:02 -05:00
|
|
|
"Load babel libraries as needed when babel blocks are executed."
|
2018-05-31 17:25:59 +02:00
|
|
|
(when (funcall orig-fn info)
|
|
|
|
(let* ((lang (nth 0 info))
|
|
|
|
(lang (if (symbolp lang) lang (intern lang))))
|
|
|
|
(when (and (not (cdr (assq lang org-babel-load-languages)))
|
|
|
|
(or (run-hook-with-args-until-success '+org-babel-load-functions lang)
|
|
|
|
(require
|
|
|
|
(intern (format "ob-%s"
|
|
|
|
(or (cdr (assq lang +org-babel-mode-alist))
|
|
|
|
lang)))
|
|
|
|
nil t)))
|
|
|
|
(map-put org-babel-load-languages lang t))
|
|
|
|
t)))
|
|
|
|
(advice-add #'org-babel-confirm-evaluate :around #'+org*babel-lazy-load-library)
|
2017-11-06 00:51:37 +01:00
|
|
|
|
|
|
|
;; I prefer C-c C-c for confirming over the default C-c '
|
2018-06-03 15:46:00 +02:00
|
|
|
(define-key org-src-mode-map (kbd "C-c C-c") #'org-edit-src-exit)
|
2017-04-11 08:28:02 -04:00
|
|
|
|
2017-04-27 18:03:37 -04:00
|
|
|
;; In a recent update, `org-babel-get-header' was removed from org-mode, which
|
2017-05-19 03:29:00 +02:00
|
|
|
;; is something a fair number of babel plugins use. So until those plugins
|
2017-07-05 02:33:41 +02:00
|
|
|
;; update, this polyfill will do:
|
2017-04-27 18:03:37 -04:00
|
|
|
(defun org-babel-get-header (params key &optional others)
|
2017-07-05 02:33:41 +02:00
|
|
|
(cl-loop with fn = (if others #'not #'identity)
|
|
|
|
for p in params
|
|
|
|
if (funcall fn (eq (car p) key))
|
2017-12-08 22:59:42 -05:00
|
|
|
collect p)))
|
2018-05-30 18:13:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Plugins
|
|
|
|
;;
|
|
|
|
|
|
|
|
(def-package! ob-ipython
|
|
|
|
:when (featurep! +ipython)
|
|
|
|
:defer t
|
|
|
|
:init
|
|
|
|
(defvar +ob-ipython-local-runtime-dir nil
|
|
|
|
"TODO")
|
|
|
|
|
|
|
|
(setq ob-ipython-resources-dir ".ob-ipython-resrc")
|
|
|
|
|
2018-05-31 17:25:59 +02:00
|
|
|
(defun +org|babel-load-ipython (lang)
|
|
|
|
(and (string-match-p "^jupyter-" (symbol-name lang))
|
2018-05-30 18:13:22 +02:00
|
|
|
(require 'ob-ipython nil t)))
|
|
|
|
(add-hook '+org-babel-load-functions #'+org|babel-load-ipython)
|
|
|
|
:config
|
|
|
|
(set! :popups
|
|
|
|
'("^\\*Org Src"
|
|
|
|
((size . 100) (side . right) (slot . -1) (window-height . 0.6))
|
|
|
|
((quit) (select . t) (modeline)))
|
|
|
|
'("^\\*Python"
|
|
|
|
((slot . 0) (side . right) (size . 100))
|
|
|
|
((select) (quit) (transient)))
|
|
|
|
'("\\*ob-ipython.*"
|
|
|
|
((slot . 2) (side . right) (size . 100) (window-height . 0.2))
|
|
|
|
((select) (quit) (transient)))
|
|
|
|
'("\\*Python:.*"
|
|
|
|
((slot . 0) (side . right) (size . 100))
|
|
|
|
((select) (quit) (transient))))
|
|
|
|
;; TODO Add more popup styles
|
|
|
|
|
|
|
|
;; advices for remote kernel and org-src-edit
|
|
|
|
(advice-add 'org-babel-edit-prep:ipython :override #'+org*org-babel-edit-prep:ipython)
|
|
|
|
(advice-add 'org-babel-ipython-initiate-session :override #'+org*org-babel-ipython-initiate-session)
|
|
|
|
(advice-add 'ob-ipython--create-repl :override #'+org*ob-ipython--create-repl)
|
|
|
|
(advice-add 'org-babel-execute:ipython :override #'+org*org-babel-execute:ipython)
|
|
|
|
|
|
|
|
;; retina resolution image hack
|
|
|
|
(when (eq window-system 'ns)
|
|
|
|
(advice-add 'ob-ipython--write-base64-string :around #'+org*ob-ipython--write-base64-string)))
|