ob-abc.el 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. (require 'ob)
  23. ;; optionally define a file extension for this language
  24. (add-to-list 'org-babel-tangle-lang-exts '("abc" . "abc"))
  25. ;; optionally declare default header arguments for this language
  26. (defvar org-babel-default-header-args:abc
  27. '((:results . "file") (:exports . "results"))
  28. "Default arguments to use when evaluating an ABC source block.")
  29. (defun org-babel-expand-body:abc (body params)
  30. "Expand BODY according to PARAMS, return the expanded body."
  31. (let ((vars (org-babel--get-vars params)))
  32. (mapc
  33. (lambda (pair)
  34. (let ((name (symbol-name (car pair)))
  35. (value (cdr pair)))
  36. (setq body
  37. (replace-regexp-in-string
  38. (concat "\\$" (regexp-quote name))
  39. (if (stringp value) value (format "%S" value))
  40. body))))
  41. vars)
  42. body))
  43. (defun org-babel-execute:abc (body params)
  44. "Execute a block of ABC code with org-babel. This function is
  45. called by `org-babel-execute-src-block'"
  46. (message "executing Abc source code block")
  47. (let* ((cmdline (cdr (assq :cmdline params)))
  48. (out-file (let ((file (cdr (assq :file params))))
  49. (if file (replace-regexp-in-string "\\.pdf$" ".ps" file)
  50. (error "abc code block requires :file header argument"))))
  51. (in-file (org-babel-temp-file "abc-"))
  52. (render (concat "abcm2ps" " " cmdline
  53. " -O " (org-babel-process-file-name out-file)
  54. " " (org-babel-process-file-name in-file))))
  55. (with-temp-file in-file (insert (org-babel-expand-body:abc body params)))
  56. (org-babel-eval render "")
  57. ;;; handle where abcm2ps changes the file name (to support multiple files
  58. (when (or (string= (file-name-extension out-file) "eps")
  59. (string= (file-name-extension out-file) "svg"))
  60. (rename-file (concat
  61. (file-name-sans-extension out-file) "001."
  62. (file-name-extension out-file))
  63. out-file t))
  64. ;;; if we were asked for a pdf...
  65. (when (string= (file-name-extension (cdr (assq :file params))) "pdf")
  66. (org-babel-eval (concat "ps2pdf" " " out-file " " (cdr (assq :file params))) ""))
  67. ;;; indicate that the file has been written
  68. nil))
  69. ;; This function should be used to assign any variables in params in
  70. ;; the context of the session environment.
  71. (defun org-babel-prep-session:abc (_session _params)
  72. "Return an error because abc does not support sessions."
  73. (error "ABC does not support sessions"))
  74. (provide 'ob-abc)
  75. ;;; ob-abc.el ends here