-
Notifications
You must be signed in to change notification settings - Fork 1
/
ob-sclang.el
90 lines (75 loc) · 3.25 KB
/
ob-sclang.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
;;; ob-sclang.el --- Org-babel interface for SuperCollider -*- coding: utf-8;
;; Copyright (C) 2018 Michal Seta
;; Author: Michal Seta
;; Keywords: supercollider, literate programming, multimedia, languages, tools
;; URL: https://github.com/djiamnot/ob-sclang
;; Package-Requires: ((emacs "26.1") (sclang "1.0"))
;; Version: 0.3
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; ob-sclang enables SuperCollider source blocks to be used in org files
;;; Code:
(require 'ob)
(require 'sclang)
;; add file extensions for sclang
(add-to-list 'org-babel-tangle-lang-exts '("sclang" . "scd"))
;; declare default header arguments for sclang code blocks
(defvar org-babel-default-header-args:sclang
'((:session . "*SCLang:Workspace*")
(:result . "none")))
(defun org-babel-expand-body:sclang (body params)
"Expand BODY according to PARAMS, return the expanded body."
(let ((vars (org-babel--get-vars params)))
(mapc
(lambda (pair)
(let ((name (symbol-name (car pair)))
(value (cdr pair)))
(setq body
(replace-regexp-in-string
(regexp-quote name)
(org-babel-sclang-var-to-sclang value)
body))))
vars)
body))
(defun org-babel-sclang-var-to-sclang (var)
"Convert an elisp value VAR to a string of sclang code.
The value of the variable should be represented with correct type."
(if (listp var)
(concat "[" (mapconcat #'org-babel-sclang-var-to-sclang var ", ") "]")
(cond ((stringp var) (format "%S" var))
((floatp var) (format "%f" var))
((integerp var) (format "%d" var))
((symbolp var) (concat (format "%S" (symbol-name var)) ".asSymbol")))))
(defun org-babel-execute:sclang (body params)
"Execute a block of sclang code with org-babel.
This function is called by `org-babel-execute-src-block' with BODY and PARAMS"
(sclang-eval-string (org-babel-expand-body:sclang body params)))
(defun org-babel-prep-session:sclang (session params)
"Prepare SESSION according to the header arguments specified in PARAMS."
(message "No need for session preparation"))
(defun org-babel-sclang-initiate-session (&optional session)
"If there is not a current inferior-process-buffer in SESSION then create one.
Return the initialized session."
(unless (string= session "none")
(let ((session (if sclang-library-initialized-p
sclang-post-buffer
(save-window-excursion
(sclang-start)
(current-buffer)))))
session)))
(provide 'ob-sclang)
;;; ob-sclang.el ends here