Fix and standardize some copyright and license notices
[bpt/emacs.git] / lisp / org / ob-abc.el
1 ;;; ob-abc.el --- org-babel functions for template evaluation
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: William Waites
6 ;; Keywords: literate programming, music
7 ;; Homepage: http://www.tardis.ed.ac.uk/wwaites
8 ;; Version: 0.01
9
10 ;; This file is 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 <http://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 (require 'ob)
32
33 ;; optionally define a file extension for this language
34 (add-to-list 'org-babel-tangle-lang-exts '("abc" . "abc"))
35
36 ;; optionally declare default header arguments for this language
37 (defvar org-babel-default-header-args:abc
38 '((:results . "file") (:exports . "results"))
39 "Default arguments to use when evaluating an ABC source block.")
40
41 (defun org-babel-expand-body:abc (body params)
42 "Expand BODY according to PARAMS, return the expanded body."
43 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
44 (mapc
45 (lambda (pair)
46 (let ((name (symbol-name (car pair)))
47 (value (cdr pair)))
48 (setq body
49 (replace-regexp-in-string
50 (concat "\$" (regexp-quote name))
51 (if (stringp value) value (format "%S" value))
52 body))))
53 vars)
54 body))
55
56 (defun org-babel-execute:abc (body params)
57 "Execute a block of ABC code with org-babel. This function is
58 called by `org-babel-execute-src-block'"
59 (message "executing Abc source code block")
60 (let* ((result-params (split-string (or (cdr (assoc :results params)))))
61 (cmdline (cdr (assoc :cmdline params)))
62 (out-file ((lambda (el)
63 (or el
64 (error "abc code block requires :file header argument")))
65 (replace-regexp-in-string "\.pdf$" ".ps" (cdr (assoc :file params)))))
66 (in-file (org-babel-temp-file "abc-"))
67 (render (concat "abcm2ps" " " cmdline
68 " -O " (org-babel-process-file-name out-file)
69 " " (org-babel-process-file-name in-file))))
70 (with-temp-file in-file (insert (org-babel-expand-body:abc body params)))
71 (org-babel-eval render "")
72 ;;; handle where abcm2ps changes the file name (to support multiple files
73 (when (or (string= (file-name-extension out-file) "eps")
74 (string= (file-name-extension out-file) "svg"))
75 (rename-file (concat
76 (file-name-sans-extension out-file) "001."
77 (file-name-extension out-file))
78 out-file t))
79 ;;; if we were asked for a pdf...
80 (when (string= (file-name-extension (cdr (assoc :file params))) "pdf")
81 (org-babel-eval (concat "ps2pdf" " " out-file " " (cdr (assoc :file params))) ""))
82 ;;; indicate that the file has been written
83 nil))
84
85 ;; This function should be used to assign any variables in params in
86 ;; the context of the session environment.
87 (defun org-babel-prep-session:abc (session params)
88 "Return an error because abc does not support sessions."
89 (error "ABC does not support sessions"))
90
91 (provide 'ob-abc)
92 ;;; ob-abc.el ends here