ob-abc.el 3.4 KB

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