1 ;;; ob-lilypond.el --- org-babel functions for lilypond evaluation
3 ;; Copyright (C) 2010-2011 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
34 (defalias 'lilypond-mode
'LilyPond-mode
)
36 (declare-function show-all
"outline" ())
38 (add-to-list 'org-babel-tangle-lang-exts
'("LilyPond" .
"ly"))
40 (defvar org-babel-default-header-args
:lilypond
'()
41 "Default header arguments for js code blocks.")
43 (defconst ly-version
"0.3"
44 "The version number of the file ob-lilypond.el.")
46 (defvar ly-compile-post-tangle t
47 "Following the org-babel-tangle (C-c C-v t) command,
48 ly-compile-post-tangle determines whether ob-lilypond should
49 automatically attempt to compile the resultant tangled file.
50 If the value is nil, no automated compilation takes place.
53 (defvar ly-display-pdf-post-tangle t
54 "Following a successful LilyPond compilation
55 ly-display-pdf-post-tangle determines whether to automate the
56 drawing / redrawing of the resultant pdf. If the value is nil,
57 the pdf is not automatically redrawn. Default value is t")
59 (defvar ly-play-midi-post-tangle t
60 "Following a successful LilyPond compilation
61 ly-play-midi-post-tangle determines whether to automate the
62 playing of the resultant midi file. If the value is nil,
63 the midi file is not automatically played. Default value is t")
65 (defvar ly-OSX-ly-path
66 "/Applications/lilypond.app/Contents/Resources/bin/lilypond")
67 (defvar ly-OSX-pdf-path
"open")
68 (defvar ly-OSX-midi-path
"open")
70 (defvar ly-nix-ly-path
"/usr/bin/lilypond")
71 (defvar ly-nix-pdf-path
"evince")
72 (defvar ly-nix-midi-path
"timidity")
74 (defvar ly-win32-ly-path
"lilypond")
75 (defvar ly-win32-pdf-path
"")
76 (defvar ly-win32-midi-path
"")
78 (defvar ly-gen-png nil
79 "Image generation (png) can be turned on by default by setting
82 (defvar ly-gen-svg nil
83 "Image generation (SVG) can be turned on by default by setting
86 (defvar ly-gen-html nil
87 "HTML generation can be turned on by default by setting
90 (defvar ly-use-eps nil
91 "You can force the compiler to use the EPS backend by setting
94 (defvar ly-arrange-mode nil
95 "Arrange mode is turned on by setting LY-ARRANGE-MODE
96 to t. In Arrange mode the following settings are altered
98 :tangle yes, :noweb yes
99 :results silent :comments yes.
100 In addition lilypond block execution causes tangling of all lilypond
103 (defun org-babel-expand-body:lilypond
(body params
)
104 "Expand BODY according to PARAMS, return the expanded body."
106 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
))))
109 (let ((name (symbol-name (car pair
)))
112 (replace-regexp-in-string
113 (concat "\$" (regexp-quote name
))
114 (if (stringp value
) value
(format "%S" value
))
119 (defun org-babel-execute:lilypond
(body params
)
120 "This function is called by `org-babel-execute-src-block'.
121 Depending on whether we are in arrange mode either:
122 1. Attempt to execute lilypond block according to header settings
123 (This is the default basic mode)
124 2. Tangle all lilypond blocks and process the result (arrange mode)"
126 (ly-set-header-args ly-arrange-mode
)
129 (ly-process-basic body params
)))
132 "ob-lilypond specific tangle, attempts to invoke
133 =ly-execute-tangled-ly= if tangle is successful. Also passes
134 specific arguments to =org-babel-tangle="
137 (if (org-babel-tangle nil
"yes" "lilypond")
138 (ly-execute-tangled-ly) nil
))
140 (defun ly-process-basic (body params
)
141 "Execute a lilypond block in basic mode"
143 (let* ((result-params (cdr (assoc :result-params params
)))
144 (out-file (cdr (assoc :file params
)))
145 (cmdline (or (cdr (assoc :cmdline params
))
147 (in-file (org-babel-temp-file "lilypond-")))
149 (with-temp-file in-file
150 (insert (org-babel-expand-body:generic body params
)))
154 (ly-determine-ly-path)
156 "-dno-gs-load-fonts "
157 "-dinclude-eps-fonts "
160 (file-name-sans-extension out-file
)
166 (defun org-babel-prep-session:lilypond
(session params
)
167 "Return an error because LilyPond exporter does not support sessions."
169 (error "Sorry, LilyPond does not currently support sessions!"))
171 (defun ly-execute-tangled-ly ()
172 "Compile result of block tangle with lilypond.
173 If error in compilation, attempt to mark the error in lilypond org file"
175 (when ly-compile-post-tangle
176 (let ((ly-tangled-file (ly-switch-extension
177 (buffer-file-name) ".lilypond"))
178 (ly-temp-file (ly-switch-extension
179 (buffer-file-name) ".ly")))
180 (if (file-exists-p ly-tangled-file
)
182 (when (file-exists-p ly-temp-file
)
183 (delete-file ly-temp-file
))
184 (rename-file ly-tangled-file
186 (error "Error: Tangle Failed!") t
)
187 (switch-to-buffer-other-window "*lilypond*")
189 (ly-compile-lilyfile ly-temp-file
)
190 (goto-char (point-min))
191 (if (not (ly-check-for-compile-error ly-temp-file
))
194 (ly-attempt-to-open-pdf ly-temp-file
)
195 (ly-attempt-to-play-midi ly-temp-file
))
196 (error "Error in Compilation!")))) nil
)
198 (defun ly-compile-lilyfile (file-name &optional test
)
199 "Compile lilypond file and check for compile errors
200 FILE-NAME is full path to lilypond (.ly) file"
202 (message "Compiling LilyPond...")
203 (let ((arg-1 (ly-determine-ly-path)) ;program
205 (arg-3 "*lilypond*") ;buffer
207 (arg-5 (if ly-gen-png
"--png" "")) ;&rest...
208 (arg-6 (if ly-gen-html
"--html" ""))
209 (arg-7 (if ly-use-eps
"-dbackend=eps" ""))
210 (arg-8 (if ly-gen-svg
"-dbackend=svg" ""))
211 (arg-9 (concat "--output=" (file-name-sans-extension file-name
)))
214 `(,arg-1
,arg-2
,arg-3
,arg-4
,arg-5
215 ,arg-6
,arg-7
,arg-8
,arg-9
,arg-10
)
217 arg-1 arg-2 arg-3 arg-4 arg-5
218 arg-6 arg-7 arg-8 arg-9 arg-10
))))
220 (defun ly-check-for-compile-error (file-name &optional test
)
221 "Check for compile error.
222 This is performed by parsing the *lilypond* buffer
223 containing the output message from the compilation.
224 FILE-NAME is full path to lilypond file.
225 If TEST is t just return nil if no error found, and pass
226 nil as file-name since it is unused in this context"
227 (let ((is-error (search-forward "error:" nil t
)))
231 (ly-process-compile-error file-name
))
234 (defun ly-process-compile-error (file-name)
235 "Process the compilation error that has occurred.
236 FILE-NAME is full path to lilypond file"
238 (let ((line-num (ly-parse-line-num)))
239 (let ((error-lines (ly-parse-error-line file-name line-num
)))
240 (ly-mark-error-line file-name error-lines
)
241 (error "Error: Compilation Failed!"))))
243 (defun ly-mark-error-line (file-name line
)
244 "Mark the erroneous lines in the lilypond org buffer.
245 FILE-NAME is full path to lilypond file.
246 LINE is the erroneous line"
248 (switch-to-buffer-other-window
249 (concat (file-name-nondirectory
250 (ly-switch-extension file-name
".org"))))
251 (let ((temp (point)))
252 (goto-char (point-min))
253 (setq case-fold-search nil
)
254 (if (search-forward line nil t
)
258 (goto-char (- (point) (length line
))))
261 (defun ly-parse-line-num (&optional buffer
)
262 "Extract error line number."
267 (and (search-backward ":" nil t
)
268 (search-backward ":" nil t
)
269 (search-backward ":" nil t
)
270 (search-backward ":" nil t
)))
275 (let ((num (buffer-substring
277 (- (search-forward ":" nil t
) 1))))
278 (setq num
(string-to-number num
))
284 (defun ly-parse-error-line (file-name lineNo
)
285 "Extract the erroneous line from the tangled .ly file
286 FILE-NAME is full path to lilypond file.
287 LINENO is the number of the erroneous line"
290 (insert-file-contents (ly-switch-extension file-name
".ly")
294 (goto-char (point-min))
295 (forward-line (- lineNo
1))
296 (buffer-substring (point) (point-at-eol)))
299 (defun ly-attempt-to-open-pdf (file-name &optional test
)
300 "Attempt to display the generated pdf file
301 FILE-NAME is full path to lilypond file
302 If TEST is non-nil, the shell command is returned and is not run"
304 (when ly-display-pdf-post-tangle
305 (let ((pdf-file (ly-switch-extension file-name
".pdf")))
306 (if (file-exists-p pdf-file
)
308 (concat (ly-determine-pdf-path) " " pdf-file
)))
311 (shell-command cmd-string
)))
312 (message "No pdf file generated so can't display!")))))
314 (defun ly-attempt-to-play-midi (file-name &optional test
)
315 "Attempt to play the generated MIDI file
316 FILE-NAME is full path to lilypond file
317 If TEST is non-nil, the shell command is returned and is not run"
319 (when ly-play-midi-post-tangle
320 (let ((midi-file (ly-switch-extension file-name
".midi")))
321 (if (file-exists-p midi-file
)
323 (concat (ly-determine-midi-path) " " midi-file
)))
326 (shell-command cmd-string
)))
327 (message "No midi file generated so can't play!")))))
329 (defun ly-determine-ly-path (&optional test
)
330 "Return correct path to ly binary depending on OS
331 If TEST is non-nil, it contains a simulation of the OS for test purposes"
334 (or test system-type
)))
335 (cond ((string= sys-type
"darwin")
337 ((string= sys-type
"win32")
339 (t ly-nix-ly-path
))))
341 (defun ly-determine-pdf-path (&optional test
)
342 "Return correct path to pdf viewer depending on OS
343 If TEST is non-nil, it contains a simulation of the OS for test purposes"
346 (or test system-type
)))
347 (cond ((string= sys-type
"darwin")
349 ((string= sys-type
"win32")
351 (t ly-nix-pdf-path
))))
353 (defun ly-determine-midi-path (&optional test
)
354 "Return correct path to midi player depending on OS
355 If TEST is non-nil, it contains a simulation of the OS for test purposes"
358 (or test test system-type
)))
359 (cond ((string= sys-type
"darwin")
361 ((string= sys-type
"win32")
363 (t ly-nix-midi-path
))))
365 (defun ly-toggle-midi-play ()
366 "Toggle whether midi will be played following a successful compilation"
369 (setq ly-play-midi-post-tangle
370 (not ly-play-midi-post-tangle
))
371 (message (concat "Post-Tangle MIDI play has been "
372 (if ly-play-midi-post-tangle
373 "ENABLED." "DISABLED."))))
375 (defun ly-toggle-pdf-display ()
376 "Toggle whether pdf will be displayed following a successful compilation"
379 (setq ly-display-pdf-post-tangle
380 (not ly-display-pdf-post-tangle
))
381 (message (concat "Post-Tangle PDF display has been "
382 (if ly-display-pdf-post-tangle
383 "ENABLED." "DISABLED."))))
385 (defun ly-toggle-png-generation ()
386 "Toggle whether png image will be generated by compilation"
391 (message (concat "PNG image generation has been "
392 (if ly-gen-png
"ENABLED." "DISABLED."))))
394 (defun ly-toggle-html-generation ()
395 "Toggle whether html will be generated by compilation"
400 (message (concat "HTML generation has been "
401 (if ly-gen-html
"ENABLED." "DISABLED."))))
403 (defun ly-toggle-arrange-mode ()
404 "Toggle whether in Arrange mode or Basic mode"
407 (setq ly-arrange-mode
408 (not ly-arrange-mode
))
409 (message (concat "Arrange mode has been "
410 (if ly-arrange-mode
"ENABLED." "DISABLED."))))
412 (defun ly-version (&optional insert-at-point
)
414 (let ((version (format "ob-lilypond version %s" ly-version
)))
415 (when insert-at-point
(insert version
))
418 (defun ly-switch-extension (file-name ext
)
419 "Utility command to swap current FILE-NAME extension with EXT"
421 (concat (file-name-sans-extension
424 (defun ly-get-header-args (mode)
425 "Default arguments to use when evaluating a lilypond
426 source block. These depend upon whether we are in arrange
427 mode i.e. ARRANGE-MODE is t"
431 (:results .
"silent")
432 (:comments .
"yes")))
434 '((:results .
"file")
435 (:exports .
"results")))))
437 (defun ly-set-header-args (mode)
438 "Set org-babel-default-header-args:lilypond
439 dependent on LY-ARRANGE-MODE"
440 (setq org-babel-default-header-args
:lilypond
441 (ly-get-header-args mode
)))
443 (provide 'ob-lilypond
)
447 ;;; ob-lilypond.el ends here