ob-sclang/ob-sclang.el

91 lines
3.3 KiB
EmacsLisp
Raw Normal View History

2022-07-30 10:18:39 +00:00
;;; ob-sclang.el --- Org-babel interface for SuperCollider -*- coding: utf-8;
2018-07-25 19:53:33 +00:00
;; Copyright (C) 2018 Michal Seta
;; Author: Michal Seta
2022-07-30 10:18:39 +00:00
;; 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
2018-07-25 19:53:33 +00:00
;;; 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.
2022-07-30 10:18:39 +00:00
;;; Commentary:
;; ob-sclang enables SuperCollider source blocks to be used in org files
2018-07-25 19:53:33 +00:00
;;; Code:
(require 'ob)
(require 'sclang)
2022-07-30 10:18:39 +00:00
;; add file extensions for sclang
2018-07-25 19:53:33 +00:00
(add-to-list 'org-babel-tangle-lang-exts '("sclang" . "scd"))
2022-07-30 10:18:39 +00:00
;; declare default header arguments for sclang code blocks
2018-09-22 21:58:57 +00:00
(defvar org-babel-default-header-args:sclang
2022-07-30 10:18:39 +00:00
'((:session . "*SCLang:Workspace*")
(:result . "none")))
2018-07-25 19:53:33 +00:00
(defun org-babel-expand-body:sclang (body params)
2019-12-12 04:53:28 +00:00
"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)
2019-12-12 04:53:28 +00:00
(org-babel-sclang-var-to-sclang value)
body))))
vars)
body))
2019-12-12 04:53:28 +00:00
(defun org-babel-sclang-var-to-sclang (var)
2022-07-30 10:18:39 +00:00
"Convert an elisp value VAR to a string of sclang code.
The value of the variable should be represented with correct type."
2019-12-12 04:53:28 +00:00
(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))
2022-07-30 10:18:39 +00:00
((symbolp var) (concat (format "%S" (symbol-name var)) ".asSymbol")))))
2019-12-12 04:53:28 +00:00
2018-07-25 19:53:33 +00:00
(defun org-babel-execute:sclang (body params)
2022-07-30 10:18:39 +00:00
"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)))
2018-07-25 19:53:33 +00:00
(defun org-babel-prep-session:sclang (session params)
"Prepare SESSION according to the header arguments specified in PARAMS."
2022-07-30 10:18:39 +00:00
(message "No need for session preparation"))
2018-07-25 19:53:33 +00:00
(defun org-babel-sclang-initiate-session (&optional session)
2022-07-30 10:18:39 +00:00
"If there is not a current inferior-process-buffer in SESSION then create one.
2018-07-25 19:53:33 +00:00
Return the initialized session."
(unless (string= session "none")
2022-07-30 10:18:39 +00:00
(let ((session (if sclang-library-initialized-p
2018-07-25 19:53:33 +00:00
sclang-post-buffer
(save-window-excursion
(sclang-start)
2022-07-30 10:18:39 +00:00
(current-buffer)))))
session)))
2018-07-25 19:53:33 +00:00
(provide 'ob-sclang)
;;; ob-sclang.el ends here