Small optimization
[org-mode.git] / lisp / ob-abc.el
bloba980b0225d43b73d2c7258db381c1d8d8c4ce0c4
1 ;;; ob-abc.el --- org-babel functions for template evaluation
3 ;; Copyright (C) Free Software Foundation
5 ;; Author: William Waites
6 ;; Keywords: literate programming, music
7 ;; Homepage: http://www.tardis.ed.ac.uk/wwaites
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;;; This file adds support to Org Babel for music in ABC notation.
30 ;;; It requires that the abcm2ps program is installed.
31 ;;; See http://moinejf.free.fr/
33 (require 'ob)
35 ;; optionally define a file extension for this language
36 (add-to-list 'org-babel-tangle-lang-exts '("abc" . "abc"))
38 ;; optionally declare default header arguments for this language
39 (defvar org-babel-default-header-args:abc
40 '((:results . "file") (:exports . "results"))
41 "Default arguments to use when evaluating an ABC source block.")
43 (defun org-babel-expand-body:abc (body params)
44 "Expand BODY according to PARAMS, return the expanded body."
45 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
46 (mapc
47 (lambda (pair)
48 (let ((name (symbol-name (car pair)))
49 (value (cdr pair)))
50 (setq body
51 (replace-regexp-in-string
52 (concat "\$" (regexp-quote name))
53 (if (stringp value) value (format "%S" value))
54 body))))
55 vars)
56 body))
58 (defun org-babel-execute:abc (body params)
59 "Execute a block of ABC code with org-babel. This function is
60 called by `org-babel-execute-src-block'"
61 (message "executing Abc source code block")
62 (let* ((result-params (split-string (or (cdr (assoc :results params)))))
63 (cmdline (cdr (assoc :cmdline params)))
64 (out-file ((lambda (el)
65 (or el
66 (error "abc code block requires :file header argument")))
67 (replace-regexp-in-string "\.pdf$" ".ps" (cdr (assoc :file params)))))
68 (in-file (org-babel-temp-file "abc-"))
69 (render (concat "abcm2ps" " " cmdline
70 " -O " (org-babel-process-file-name out-file)
71 " " (org-babel-process-file-name in-file))))
72 (with-temp-file in-file (insert (org-babel-expand-body:abc body params)))
73 (org-babel-eval render "")
74 ;;; handle where abcm2ps changes the file name (to support multiple files
75 (when (or (string= (file-name-extension out-file) "eps")
76 (string= (file-name-extension out-file) "svg"))
77 (rename-file (concat
78 (file-name-sans-extension out-file) "001."
79 (file-name-extension out-file))
80 out-file t))
81 ;;; if we were asked for a pdf...
82 (when (string= (file-name-extension (cdr (assoc :file params))) "pdf")
83 (org-babel-eval (concat "ps2pdf" " " out-file " " (cdr (assoc :file params))) ""))
84 ;;; indicate that the file has been written
85 nil))
87 ;; This function should be used to assign any variables in params in
88 ;; the context of the session environment.
89 (defun org-babel-prep-session:abc (session params)
90 "Return an error because abc does not support sessions."
91 (error "ABC does not support sessions"))
93 (provide 'ob-abc)
94 ;;; ob-abc.el ends here