ob-abc.el 3.5 KB

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