ob-lilypond: test suite, all tests passing
[org-mode.git] / lisp / ob-lilypond.el
blob39aa78ccf8a792dd8c0ec659c7a1668f455b4091
1 ;;; ob-lilypond.el --- org-babel functions for lilypond evaluation
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Martyn Jago
6 ;; Keywords: babel language, literate programming
7 ;; Homepage: https://github.com/mjago/ob-lilypond
8 ;; Version: 0.2
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;; Installation / usage info, and examples are available at
28 ;; https://github.com/mjago/ob-lilypond
30 ;;; Code:
31 (require 'ob)
32 (require 'ob-eval)
33 (defalias 'lilypond-mode 'LilyPond-mode)
34 (add-to-list 'org-babel-tangle-lang-exts '("LilyPond" . "ly"))
36 (defconst ly-version "0.2"
37 "The version number of the file ob-lilypond.el.")
39 (defvar ly-compile-post-tangle t
40 "Following the org-babel-tangle (C-c C-v t) command,
41 ly-compile-post-tangle determines whether ob-lilypond should
42 automatically attempt to compile the resultant tangled file.
43 If the value is nil, no automated compilation takes place.
44 Default value is t")
46 (defvar ly-display-pdf-post-tangle t
47 "Following a successful LilyPond compilation
48 ly-display-pdf-post-tangle determines whether to automate the
49 drawing / redrawing of the resultant pdf. If the value is nil,
50 the pdf is not automatically redrawn. Default value is t")
52 (defvar ly-play-midi-post-tangle t
53 "Following a successful LilyPond compilation
54 ly-play-midi-post-tangle determines whether to automate the
55 playing of the resultant midi file. If the value is nil,
56 the midi file is not automatically played. Default value is t")
58 (defvar ly-OSX-ly-path
59 "/Applications/lilypond.app/Contents/Resources/bin/lilypond")
60 (defvar ly-OSX-pdf-path "open")
61 (defvar ly-OSX-midi-path "open")
63 (defvar ly-nix-ly-path "/usr/bin/lilypond")
64 (defvar ly-nix-pdf-path "evince")
65 (defvar ly-nix-midi-path "timidity")
67 (defvar ly-win32-ly-path "lilypond")
68 (defvar ly-win32-pdf-path "")
69 (defvar ly-win32-midi-path "")
71 (defvar ly-gen-png nil
72 "Image generation (png) can be turned on by default by setting
73 LY-GEN-PNG to t")
75 (defvar ly-gen-svg nil
76 "Image generation (SVG) can be turned on by default by setting
77 LY-GEN-SVG to t")
79 (defvar ly-gen-html nil
80 "HTML generation can be turned on by default by setting
81 LY-GEN-HTML to t")
83 (defvar ly-use-eps nil
84 "You can force the compiler to use the EPS backend by setting
85 LY-USE-EPS to t")
87 (defvar org-babel-default-header-args:lilypond
88 '((:tangle . "yes") (:noweb . "yes") (:results . "silent") (:comments . "yes"))
89 "Default arguments to use when evaluating a lilypond source block.")
91 (defun org-babel-expand-body:lilypond (body params)
92 "Expand BODY according to PARAMS, return the expanded body."
94 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
95 (mapc
96 (lambda (pair)
97 (let ((name (symbol-name (car pair)))
98 (value (cdr pair)))
99 (setq body
100 (replace-regexp-in-string
101 (concat "\$" (regexp-quote name))
102 (if (stringp value) value (format "%S" value))
103 body))))
104 vars)
105 body))
107 (defun org-babel-execute:lilypond (body params)
108 "This function is called by `org-babel-execute-src-block'.
109 tTangle all lilypond blocks and process the result"
111 (ly-tangle))
113 (defun ly-tangle ()
114 "ob-lilypond specific tangle, attempts to invoke
115 =ly-execute-tangled-ly= if tangle is successful. Also passes
116 specific arguments to =org-babel-tangle="
118 (interactive)
119 (if (org-babel-tangle nil "yes" "lilypond")
120 (ly-execute-tangled-ly) nil))
122 (defun org-babel-prep-session:lilypond (session params)
123 "Return an error because LilyPond exporter does not support sessions."
125 (error "Sorry, LilyPond does not currently support sessions!"))
127 (defun ly-execute-tangled-ly ()
128 "Compile result of block tangle with lilypond.
129 If error in compilation, attempt to mark the error in lilypond org file"
131 (when ly-compile-post-tangle
132 (let ((ly-tangled-file (ly-switch-extension
133 (buffer-file-name) ".lilypond"))
134 (ly-temp-file (ly-switch-extension
135 (buffer-file-name) ".ly")))
136 (if (file-exists-p ly-tangled-file)
137 (progn
138 (when (file-exists-p ly-temp-file)
139 (delete-file ly-temp-file))
140 (rename-file ly-tangled-file
141 ly-temp-file))
142 (error "Error: Tangle Failed!") t)
143 (switch-to-buffer-other-window "*lilypond*")
144 (erase-buffer)
145 (ly-compile-lilyfile ly-temp-file)
146 (goto-char (point-min))
147 (if (not (ly-check-for-compile-error ly-temp-file))
148 (progn
149 (other-window -1)
150 (ly-attempt-to-open-pdf ly-temp-file)
151 (ly-attempt-to-play-midi ly-temp-file))
152 (error "Error in Compilation!")))) nil)
154 (defun ly-compile-lilyfile (file-name &optional test)
155 "Compile lilypond file and check for compile errors
156 FILE-NAME is full path to lilypond (.ly) file"
158 (message "Compiling LilyPond...")
159 (let ((arg-1 (ly-determine-ly-path)) ;program
160 (arg-2 nil) ;infile
161 (arg-3 "*lilypond*") ;buffer
162 (arg-4 t) ;display
163 (arg-5 (if ly-gen-png "--png" "")) ;&rest...
164 (arg-6 (if ly-gen-html "--html" ""))
165 (arg-7 (if ly-use-eps "-dbackend=eps" ""))
166 (arg-8 (if ly-gen-svg "-dbackend=svg" ""))
167 (arg-9 (concat "--output=" (file-name-sans-extension file-name)))
168 (arg-10 file-name))
169 (if test
170 `(,arg-1 ,arg-2 ,arg-3 ,arg-4 ,arg-5
171 ,arg-6 ,arg-7 ,arg-8 ,arg-9 ,arg-10)
172 (call-process
173 arg-1 arg-2 arg-3 arg-4 arg-5
174 arg-6 arg-7 arg-8 arg-9 arg-10))))
176 (defun ly-check-for-compile-error (file-name &optional test)
177 "Check for compile error.
178 This is performed by parsing the *lilypond* buffer
179 containing the output message from the compilation.
180 FILE-NAME is full path to lilypond file.
181 If TEST is t just return nil if no error found, and pass
182 nil as file-name since it is unused in this context"
183 (let ((is-error (search-forward "error:" nil t)))
184 (if (not test)
185 (if (not is-error)
187 (ly-process-compile-error file-name))
188 is-error)))
190 (defun ly-process-compile-error (file-name)
191 "Process the compilation error that has occurred.
192 FILE-NAME is full path to lilypond file"
194 (let ((line-num (ly-parse-line-num)))
195 (let ((error-lines (ly-parse-error-line file-name line-num)))
196 (ly-mark-error-line file-name error-lines)
197 (error "Error: Compilation Failed!"))))
199 (defun ly-mark-error-line (file-name line)
200 "Mark the erroneous lines in the lilypond org buffer.
201 FILE-NAME is full path to lilypond file.
202 LINE is the erroneous line"
204 (switch-to-buffer-other-window
205 (concat (file-name-nondirectory
206 (ly-switch-extension file-name ".org"))))
207 (let ((temp (point)))
208 (goto-char (point-min))
209 (setq case-fold-search nil)
210 (if (search-forward line nil t)
211 (progn
212 (show-all)
213 (set-mark (point))
214 (goto-char (- (point) (length line))))
215 (goto-char temp))))
217 (defun ly-parse-line-num (&optional buffer)
218 "Extract error line number."
220 (when buffer
221 (set-buffer buffer))
222 (let ((start
223 (and (search-backward ":" nil t)
224 (search-backward ":" nil t)
225 (search-backward ":" nil t)
226 (search-backward ":" nil t)))
227 (num nil))
228 (if start
229 (progn
230 (forward-char)
231 (let ((num (buffer-substring
232 (+ 1 start)
233 (- (search-forward ":" nil t) 1))))
234 (setq num (string-to-number num))
235 (if (numberp num)
237 nil)))
238 nil)))
240 (defun ly-parse-error-line (file-name lineNo)
241 "Extract the erroneous line from the tangled .ly file
242 FILE-NAME is full path to lilypond file.
243 LINENO is the number of the erroneous line"
245 (with-temp-buffer
246 (insert-file-contents (ly-switch-extension file-name ".ly")
247 nil nil nil t)
248 (if (> lineNo 0)
249 (progn
250 (goto-char (point-min))
251 (forward-line (- lineNo 1))
252 (buffer-substring (point) (point-at-eol)))
253 nil)))
255 (defun ly-attempt-to-open-pdf (file-name &optional test)
256 "Attempt to display the generated pdf file
257 FILE-NAME is full path to lilypond file
258 If TEST is non-nil, the shell command is returned and is not run"
260 (when ly-display-pdf-post-tangle
261 (let ((pdf-file (ly-switch-extension file-name ".pdf")))
262 (if (file-exists-p pdf-file)
263 (let ((cmd-string
264 (concat (ly-determine-pdf-path) " " pdf-file)))
265 (if test
266 cmd-string
267 (shell-command cmd-string)))
268 (message "No pdf file generated so can't display!")))))
270 (defun ly-attempt-to-play-midi (file-name &optional test)
271 "Attempt to play the generated MIDI file
272 FILE-NAME is full path to lilypond file
273 If TEST is non-nil, the shell command is returned and is not run"
275 (when ly-play-midi-post-tangle
276 (let ((midi-file (ly-switch-extension file-name ".midi")))
277 (if (file-exists-p midi-file)
278 (let ((cmd-string
279 (concat (ly-determine-midi-path) " " midi-file)))
280 (if test
281 cmd-string
282 (shell-command cmd-string)))
283 (message "No midi file generated so can't play!")))))
285 (defun ly-determine-ly-path (&optional test)
286 "Return correct path to ly binary depending on OS
287 If TEST is non-nil, it contains a simulation of the OS for test purposes"
289 (let ((sys-type
290 (or test system-type)))
291 (cond ((string= sys-type "darwin")
292 ly-OSX-ly-path)
293 ((string= sys-type "win32")
294 ly-win32-ly-path)
295 (t ly-nix-ly-path))))
297 (defun ly-determine-pdf-path (&optional test)
298 "Return correct path to pdf viewer depending on OS
299 If TEST is non-nil, it contains a simulation of the OS for test purposes"
301 (let ((sys-type
302 (or test system-type)))
303 (cond ((string= sys-type "darwin")
304 ly-OSX-pdf-path)
305 ((string= sys-type "win32")
306 ly-win32-pdf-path)
307 (t ly-nix-pdf-path))))
309 (defun ly-determine-midi-path (&optional test)
310 "Return correct path to midi player depending on OS
311 If TEST is non-nil, it contains a simulation of the OS for test purposes"
313 (let ((sys-type
314 (or test test system-type)))
315 (cond ((string= sys-type "darwin")
316 ly-OSX-midi-path)
317 ((string= sys-type "win32")
318 ly-win32-midi-path)
319 (t ly-nix-midi-path))))
321 (defun ly-toggle-midi-play ()
322 "Toggle whether midi will be played following a successful compilation"
324 (interactive)
325 (setq ly-play-midi-post-tangle
326 (not ly-play-midi-post-tangle))
327 (message (concat "Post-Tangle MIDI play has been "
328 (if ly-play-midi-post-tangle
329 "ENABLED." "DISABLED."))))
331 (defun ly-toggle-pdf-display ()
332 "Toggle whether pdf will be displayed following a successful compilation"
334 (interactive)
335 (setq ly-display-pdf-post-tangle
336 (not ly-display-pdf-post-tangle))
337 (message (concat "Post-Tangle PDF display has been "
338 (if ly-display-pdf-post-tangle
339 "ENABLED." "DISABLED."))))
341 (defun ly-toggle-png-generation ()
342 "Toggle whether png image will be generated by compilation"
344 (interactive)
345 (setq ly-gen-png
346 (not ly-gen-png))
347 (message (concat "PNG image generation has been "
348 (if ly-gen-png "ENABLED." "DISABLED."))))
350 (defun ly-toggle-html-generation ()
351 "Toggle whether html will be generated by compilation"
353 (interactive)
354 (setq ly-gen-html
355 (not ly-gen-html))
356 (message (concat "HTML generation has been "
357 (if ly-gen-html "ENABLED." "DISABLED."))))
359 (defun ly-version (&optional insert-at-point)
360 (interactive)
361 (setq version (format "ob-lilypond version %s" ly-version))
362 (when insert-at-point (insert version))
363 (message version))
365 (defun ly-switch-extension (file-name ext)
366 "Utility command to swap current FILE-NAME extension with EXT"
368 (concat (file-name-sans-extension
369 file-name) ext))
371 (provide 'ob-lilypond)
373 ;; arch-tag: ac449eea-2cf2-4dc5-ae33-426f57ba4894
375 ;;; ob-lilypond.el ends here