ob-abc.el 3.5 KB

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