1 ;;; ob-lilypond.el --- org-babel functions for lilypond evaluation
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
6 ;; Keywords: babel language, literate programming
7 ;; Homepage: https://github.com/mjago/ob-lilypond
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/>.
27 ;; Installation / usage info, and examples are available at
28 ;; https://github.com/mjago/ob-lilypond
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.
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
75 (defvar ly-gen-svg nil
76 "Image generation (SVG) can be turned on by default by setting
79 (defvar ly-gen-html nil
80 "HTML generation can be turned on by default by setting
83 (defvar ly-use-eps nil
84 "You can force the compiler to use the EPS backend by setting
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
))))
97 (let ((name (symbol-name (car pair
)))
100 (replace-regexp-in-string
101 (concat "\$" (regexp-quote name
))
102 (if (stringp value
) value
(format "%S" value
))
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"
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="
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
)
138 (when (file-exists-p ly-temp-file
)
139 (delete-file ly-temp-file
))
140 (rename-file ly-tangled-file
142 (error "Error: Tangle Failed!") t
)
143 (switch-to-buffer-other-window "*lilypond*")
145 (ly-compile-lilyfile ly-temp-file
)
146 (goto-char (point-min))
147 (if (not (ly-check-for-compile-error ly-temp-file
))
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
161 (arg-3 "*lilypond*") ;buffer
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
)))
170 `(,arg-1
,arg-2
,arg-3
,arg-4
,arg-5
171 ,arg-6
,arg-7
,arg-8
,arg-9
,arg-10
)
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
)))
187 (ly-process-compile-error file-name
))
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
)
214 (goto-char (- (point) (length line
))))
217 (defun ly-parse-line-num (&optional buffer
)
218 "Extract error line number."
223 (and (search-backward ":" nil t
)
224 (search-backward ":" nil t
)
225 (search-backward ":" nil t
)
226 (search-backward ":" nil t
)))
231 (let ((num (buffer-substring
233 (- (search-forward ":" nil t
) 1))))
234 (setq num
(string-to-number num
))
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"
246 (insert-file-contents (ly-switch-extension file-name
".ly")
250 (goto-char (point-min))
251 (forward-line (- lineNo
1))
252 (buffer-substring (point) (point-at-eol)))
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
)
264 (concat (ly-determine-pdf-path) " " pdf-file
)))
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
)
279 (concat (ly-determine-midi-path) " " midi-file
)))
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"
290 (or test system-type
)))
291 (cond ((string= sys-type
"darwin")
293 ((string= sys-type
"win32")
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"
302 (or test system-type
)))
303 (cond ((string= sys-type
"darwin")
305 ((string= sys-type
"win32")
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"
314 (or test test system-type
)))
315 (cond ((string= sys-type
"darwin")
317 ((string= sys-type
"win32")
319 (t ly-nix-midi-path
))))
321 (defun ly-toggle-midi-play ()
322 "Toggle whether midi will be played following a successful compilation"
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"
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"
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"
356 (message (concat "HTML generation has been "
357 (if ly-gen-html
"ENABLED." "DISABLED."))))
359 (defun ly-version (&optional insert-at-point
)
361 (setq version
(format "ob-lilypond version %s" ly-version
))
362 (when insert-at-point
(insert 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
371 (provide 'ob-lilypond
)
373 ;; arch-tag: ac449eea-2cf2-4dc5-ae33-426f57ba4894
375 ;;; ob-lilypond.el ends here