ob-abc.el (3538B)
1 ;;; ob-abc.el --- Org Babel Functions for ABC -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2013-2021 Free Software Foundation, Inc. 4 5 ;; Author: William Waites 6 ;; Maintainer: William Waites 7 ;; Keywords: literate programming, music 8 ;; Homepage: https://www.tardis.ed.ac.uk/~wwaites 9 10 ;; This file is not part of GNU Emacs. 11 12 ;; GNU Emacs is free software: you can redistribute it and/or modify 13 ;; it under the terms of the GNU General Public License as published by 14 ;; the Free Software Foundation, either version 3 of the License, or 15 ;; (at your option) any later version. 16 17 ;; GNU Emacs is distributed in the hope that it will be useful, 18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 ;; GNU General Public License for more details. 21 22 ;; You should have received a copy of the GNU General Public License 23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 24 25 ;;; Commentary: 26 27 ;;; This file adds support to Org Babel for music in ABC notation. 28 ;;; It requires that the abcm2ps program is installed. 29 ;;; See http://moinejf.free.fr/ 30 31 ;;; Code: 32 33 (require 'ob) 34 35 ;; optionally define a file extension for this language 36 37 (add-to-list 'org-babel-tangle-lang-exts '("abc" . "abc")) 38 39 ;; optionally declare default header arguments for this language 40 (defvar org-babel-default-header-args:abc 41 '((:results . "file") (:exports . "results")) 42 "Default arguments to use when evaluating an ABC source block.") 43 44 (defun org-babel-expand-body:abc (body params) 45 "Expand BODY according to PARAMS, return the expanded body." 46 (let ((vars (org-babel--get-vars params))) 47 (mapc 48 (lambda (pair) 49 (let ((name (symbol-name (car pair))) 50 (value (cdr pair))) 51 (setq body 52 (replace-regexp-in-string 53 (concat "\\$" (regexp-quote name)) 54 (if (stringp value) value (format "%S" value)) 55 body)))) 56 vars) 57 body)) 58 59 (defun org-babel-execute:abc (body params) 60 "Execute a block of ABC code with org-babel. 61 This function is called by `org-babel-execute-src-block'." 62 (message "executing Abc source code block") 63 (let* ((cmdline (cdr (assq :cmdline params))) 64 (out-file (let ((file (cdr (assq :file params)))) 65 (if file (replace-regexp-in-string "\\.pdf$" ".ps" file) 66 (error "abc code block requires :file header argument")))) 67 (in-file (org-babel-temp-file "abc-")) 68 (render (concat "abcm2ps" " " cmdline 69 " -O " (org-babel-process-file-name out-file) 70 " " (org-babel-process-file-name in-file)))) 71 (with-temp-file in-file (insert (org-babel-expand-body:abc body params))) 72 (org-babel-eval render "") 73 ;;; handle where abcm2ps changes the file name (to support multiple files 74 (when (or (string= (file-name-extension out-file) "eps") 75 (string= (file-name-extension out-file) "svg")) 76 (rename-file (concat 77 (file-name-sans-extension out-file) "001." 78 (file-name-extension out-file)) 79 out-file t)) 80 ;;; if we were asked for a pdf... 81 (when (string= (file-name-extension (cdr (assq :file params))) "pdf") 82 (org-babel-eval (concat "ps2pdf" " " out-file " " (cdr (assq :file params))) "")) 83 ;;; indicate that the file has been written 84 nil)) 85 86 ;; This function should be used to assign any variables in params in 87 ;; the context of the session environment. 88 (defun org-babel-prep-session:abc (_session _params) 89 "Return an error because abc does not support sessions." 90 (error "ABC does not support sessions")) 91 92 (provide 'ob-abc) 93 94 ;;; ob-abc.el ends here