ob-lilypond.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. ;;; ob-lilypond.el --- org-babel functions for lilypond evaluation
  2. ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
  3. ;; Author: Martyn Jago
  4. ;; Keywords: babel language, literate programming
  5. ;; Homepage: http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Installation, ob-lilypond documentation, and examples are available at
  19. ;; http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html
  20. ;;
  21. ;; Lilypond documentation can be found at
  22. ;; http://lilypond.org/manuals.html
  23. ;;; Code:
  24. (require 'ob)
  25. (require 'ob-eval)
  26. (require 'ob-tangle)
  27. (require 'outline)
  28. (defalias 'lilypond-mode 'LilyPond-mode)
  29. (add-to-list 'org-babel-tangle-lang-exts '("LilyPond" . "ly"))
  30. (defvar org-babel-default-header-args:lilypond '()
  31. "Default header arguments for lilypond code blocks.
  32. NOTE: The arguments are determined at lilypond compile time.
  33. See (ly-set-header-args)")
  34. (defvar ly-compile-post-tangle t
  35. "Following the org-babel-tangle (C-c C-v t) command,
  36. ly-compile-post-tangle determines whether ob-lilypond should
  37. automatically attempt to compile the resultant tangled file.
  38. If the value is nil, no automated compilation takes place.
  39. Default value is t")
  40. (defvar ly-display-pdf-post-tangle t
  41. "Following a successful LilyPond compilation
  42. ly-display-pdf-post-tangle determines whether to automate the
  43. drawing / redrawing of the resultant pdf. If the value is nil,
  44. the pdf is not automatically redrawn. Default value is t")
  45. (defvar ly-play-midi-post-tangle t
  46. "Following a successful LilyPond compilation
  47. ly-play-midi-post-tangle determines whether to automate the
  48. playing of the resultant midi file. If the value is nil,
  49. the midi file is not automatically played. Default value is t")
  50. (defvar ly-OSX-ly-path
  51. "/Applications/lilypond.app/Contents/Resources/bin/lilypond")
  52. (defvar ly-OSX-pdf-path "open")
  53. (defvar ly-OSX-midi-path "open")
  54. (defvar ly-nix-ly-path "/usr/bin/lilypond")
  55. (defvar ly-nix-pdf-path "evince")
  56. (defvar ly-nix-midi-path "timidity")
  57. (defvar ly-w32-ly-path "lilypond")
  58. (defvar ly-w32-pdf-path "")
  59. (defvar ly-w32-midi-path "")
  60. (defvar ly-gen-png nil
  61. "Image generation (png) can be turned on by default by setting
  62. LY-GEN-PNG to t")
  63. (defvar ly-gen-svg nil
  64. "Image generation (SVG) can be turned on by default by setting
  65. LY-GEN-SVG to t")
  66. (defvar ly-gen-html nil
  67. "HTML generation can be turned on by default by setting
  68. LY-GEN-HTML to t")
  69. (defvar ly-gen-pdf nil
  70. "PDF generation can be turned on by default by setting
  71. LY-GEN-PDF to t")
  72. (defvar ly-use-eps nil
  73. "You can force the compiler to use the EPS backend by setting
  74. LY-USE-EPS to t")
  75. (defvar ly-arrange-mode nil
  76. "Arrange mode is turned on by setting LY-ARRANGE-MODE
  77. to t. In Arrange mode the following settings are altered
  78. from default...
  79. :tangle yes, :noweb yes
  80. :results silent :comments yes.
  81. In addition lilypond block execution causes tangling of all lilypond
  82. blocks")
  83. (defun org-babel-expand-body:lilypond (body params)
  84. "Expand BODY according to PARAMS, return the expanded body."
  85. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  86. (mapc
  87. (lambda (pair)
  88. (let ((name (symbol-name (car pair)))
  89. (value (cdr pair)))
  90. (setq body
  91. (replace-regexp-in-string
  92. (concat "\$" (regexp-quote name))
  93. (if (stringp value) value (format "%S" value))
  94. body))))
  95. vars)
  96. body))
  97. (defun org-babel-execute:lilypond (body params)
  98. "This function is called by `org-babel-execute-src-block'.
  99. Depending on whether we are in arrange mode either:
  100. 1. Attempt to execute lilypond block according to header settings
  101. (This is the default basic mode)
  102. 2. Tangle all lilypond blocks and process the result (arrange mode)"
  103. (ly-set-header-args ly-arrange-mode)
  104. (if ly-arrange-mode
  105. (ly-tangle)
  106. (ly-process-basic body params)))
  107. (defun ly-tangle ()
  108. "ob-lilypond specific tangle, attempts to invoke
  109. =ly-execute-tangled-ly= if tangle is successful. Also passes
  110. specific arguments to =org-babel-tangle="
  111. (interactive)
  112. (if (org-babel-tangle nil "yes" "lilypond")
  113. (ly-execute-tangled-ly) nil))
  114. (defun ly-process-basic (body params)
  115. "Execute a lilypond block in basic mode."
  116. (let* ((result-params (cdr (assoc :result-params params)))
  117. (out-file (cdr (assoc :file params)))
  118. (cmdline (or (cdr (assoc :cmdline params))
  119. ""))
  120. (in-file (org-babel-temp-file "lilypond-")))
  121. (with-temp-file in-file
  122. (insert (org-babel-expand-body:generic body params)))
  123. (org-babel-eval
  124. (concat
  125. (ly-determine-ly-path)
  126. " -dbackend=eps "
  127. "-dno-gs-load-fonts "
  128. "-dinclude-eps-fonts "
  129. "--png "
  130. "--output="
  131. (file-name-sans-extension out-file)
  132. " "
  133. cmdline
  134. in-file) "")) nil)
  135. (defun org-babel-prep-session:lilypond (session params)
  136. "Return an error because LilyPond exporter does not support sessions."
  137. (error "Sorry, LilyPond does not currently support sessions!"))
  138. (defun ly-execute-tangled-ly ()
  139. "Compile result of block tangle with lilypond.
  140. If error in compilation, attempt to mark the error in lilypond org file"
  141. (when ly-compile-post-tangle
  142. (let ((ly-tangled-file (ly-switch-extension
  143. (buffer-file-name) ".lilypond"))
  144. (ly-temp-file (ly-switch-extension
  145. (buffer-file-name) ".ly")))
  146. (if (file-exists-p ly-tangled-file)
  147. (progn
  148. (when (file-exists-p ly-temp-file)
  149. (delete-file ly-temp-file))
  150. (rename-file ly-tangled-file
  151. ly-temp-file))
  152. (error "Error: Tangle Failed!") t)
  153. (switch-to-buffer-other-window "*lilypond*")
  154. (erase-buffer)
  155. (ly-compile-lilyfile ly-temp-file)
  156. (goto-char (point-min))
  157. (if (not (ly-check-for-compile-error ly-temp-file))
  158. (progn
  159. (other-window -1)
  160. (ly-attempt-to-open-pdf ly-temp-file)
  161. (ly-attempt-to-play-midi ly-temp-file))
  162. (error "Error in Compilation!")))) nil)
  163. (defun ly-compile-lilyfile (file-name &optional test)
  164. "Compile lilypond file and check for compile errors
  165. FILE-NAME is full path to lilypond (.ly) file"
  166. (message "Compiling LilyPond...")
  167. (let ((arg-1 (ly-determine-ly-path)) ;program
  168. (arg-2 nil) ;infile
  169. (arg-3 "*lilypond*") ;buffer
  170. (arg-4 t) ;display
  171. (arg-4 t) ;display
  172. (arg-5 (if ly-gen-png "--png" "")) ;&rest...
  173. (arg-6 (if ly-gen-html "--html" ""))
  174. (arg-7 (if ly-gen-pdf "--pdf" ""))
  175. (arg-8 (if ly-use-eps "-dbackend=eps" ""))
  176. (arg-9 (if ly-gen-svg "-dbackend=svg" ""))
  177. (arg-10 (concat "--output=" (file-name-sans-extension file-name)))
  178. (arg-11 file-name))
  179. (if test
  180. `(,arg-1 ,arg-2 ,arg-3 ,arg-4 ,arg-5 ,arg-6
  181. ,arg-7 ,arg-8 ,arg-9 ,arg-10 ,arg-11)
  182. (call-process
  183. arg-1 arg-2 arg-3 arg-4 arg-5 arg-6
  184. arg-7 arg-8 arg-9 arg-10 arg-11))))
  185. (defun ly-check-for-compile-error (file-name &optional test)
  186. "Check for compile error.
  187. This is performed by parsing the *lilypond* buffer
  188. containing the output message from the compilation.
  189. FILE-NAME is full path to lilypond file.
  190. If TEST is t just return nil if no error found, and pass
  191. nil as file-name since it is unused in this context"
  192. (let ((is-error (search-forward "error:" nil t)))
  193. (if (not test)
  194. (if (not is-error)
  195. nil
  196. (ly-process-compile-error file-name))
  197. is-error)))
  198. (defun ly-process-compile-error (file-name)
  199. "Process the compilation error that has occurred.
  200. FILE-NAME is full path to lilypond file"
  201. (let ((line-num (ly-parse-line-num)))
  202. (let ((error-lines (ly-parse-error-line file-name line-num)))
  203. (ly-mark-error-line file-name error-lines)
  204. (error "Error: Compilation Failed!"))))
  205. (defun ly-mark-error-line (file-name line)
  206. "Mark the erroneous lines in the lilypond org buffer.
  207. FILE-NAME is full path to lilypond file.
  208. LINE is the erroneous line"
  209. (switch-to-buffer-other-window
  210. (concat (file-name-nondirectory
  211. (ly-switch-extension file-name ".org"))))
  212. (let ((temp (point)))
  213. (goto-char (point-min))
  214. (setq case-fold-search nil)
  215. (if (search-forward line nil t)
  216. (progn
  217. (show-all)
  218. (set-mark (point))
  219. (goto-char (- (point) (length line))))
  220. (goto-char temp))))
  221. (defun ly-parse-line-num (&optional buffer)
  222. "Extract error line number."
  223. (when buffer
  224. (set-buffer buffer))
  225. (let ((start
  226. (and (search-backward ":" nil t)
  227. (search-backward ":" nil t)
  228. (search-backward ":" nil t)
  229. (search-backward ":" nil t)))
  230. (num nil))
  231. (if start
  232. (progn
  233. (forward-char)
  234. (let ((num (buffer-substring
  235. (+ 1 start)
  236. (- (search-forward ":" nil t) 1))))
  237. (setq num (string-to-number num))
  238. (if (numberp num)
  239. num
  240. nil)))
  241. nil)))
  242. (defun ly-parse-error-line (file-name lineNo)
  243. "Extract the erroneous line from the tangled .ly file
  244. FILE-NAME is full path to lilypond file.
  245. LINENO is the number of the erroneous line"
  246. (with-temp-buffer
  247. (insert-file-contents (ly-switch-extension file-name ".ly")
  248. nil nil nil t)
  249. (if (> lineNo 0)
  250. (progn
  251. (goto-char (point-min))
  252. (forward-line (- lineNo 1))
  253. (buffer-substring (point) (point-at-eol)))
  254. 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"
  259. (when ly-display-pdf-post-tangle
  260. (let ((pdf-file (ly-switch-extension file-name ".pdf")))
  261. (if (file-exists-p pdf-file)
  262. (let ((cmd-string
  263. (concat (ly-determine-pdf-path) " " pdf-file)))
  264. (if test
  265. cmd-string
  266. (start-process
  267. "\"Audition pdf\""
  268. "*lilypond*"
  269. (ly-determine-pdf-path)
  270. pdf-file)))
  271. (message "No pdf file generated so can't display!")))))
  272. (defun ly-attempt-to-play-midi (file-name &optional test)
  273. "Attempt to play the generated MIDI file
  274. FILE-NAME is full path to lilypond file
  275. If TEST is non-nil, the shell command is returned and is not run"
  276. (when ly-play-midi-post-tangle
  277. (let ((midi-file (ly-switch-extension file-name ".midi")))
  278. (if (file-exists-p midi-file)
  279. (let ((cmd-string
  280. (concat (ly-determine-midi-path) " " midi-file)))
  281. (if test
  282. cmd-string
  283. (start-process
  284. "\"Audition midi\""
  285. "*lilypond*"
  286. (ly-determine-midi-path)
  287. midi-file)))
  288. (message "No midi file generated so can't play!")))))
  289. (defun ly-determine-ly-path (&optional test)
  290. "Return correct path to ly binary depending on OS
  291. If TEST is non-nil, it contains a simulation of the OS for test purposes"
  292. (let ((sys-type
  293. (or test system-type)))
  294. (cond ((string= sys-type "darwin")
  295. ly-OSX-ly-path)
  296. ((string= sys-type "windows-nt")
  297. ly-w32-ly-path)
  298. (t ly-nix-ly-path))))
  299. (defun ly-determine-pdf-path (&optional test)
  300. "Return correct path to pdf viewer depending on OS
  301. If TEST is non-nil, it contains a simulation of the OS for test purposes"
  302. (let ((sys-type
  303. (or test system-type)))
  304. (cond ((string= sys-type "darwin")
  305. ly-OSX-pdf-path)
  306. ((string= sys-type "windows-nt")
  307. ly-w32-pdf-path)
  308. (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"
  312. (let ((sys-type
  313. (or test test system-type)))
  314. (cond ((string= sys-type "darwin")
  315. ly-OSX-midi-path)
  316. ((string= sys-type "windows-nt")
  317. ly-w32-midi-path)
  318. (t ly-nix-midi-path))))
  319. (defun ly-toggle-midi-play ()
  320. "Toggle whether midi will be played following a successful compilation."
  321. (interactive)
  322. (setq ly-play-midi-post-tangle
  323. (not ly-play-midi-post-tangle))
  324. (message (concat "Post-Tangle MIDI play has been "
  325. (if ly-play-midi-post-tangle
  326. "ENABLED." "DISABLED."))))
  327. (defun ly-toggle-pdf-display ()
  328. "Toggle whether pdf will be displayed following a successful compilation."
  329. (interactive)
  330. (setq ly-display-pdf-post-tangle
  331. (not ly-display-pdf-post-tangle))
  332. (message (concat "Post-Tangle PDF display has been "
  333. (if ly-display-pdf-post-tangle
  334. "ENABLED." "DISABLED."))))
  335. (defun ly-toggle-png-generation ()
  336. "Toggle whether png image will be generated by compilation."
  337. (interactive)
  338. (setq ly-gen-png (not ly-gen-png))
  339. (message (concat "PNG image generation has been "
  340. (if ly-gen-png "ENABLED." "DISABLED."))))
  341. (defun ly-toggle-html-generation ()
  342. "Toggle whether html will be generated by compilation."
  343. (interactive)
  344. (setq ly-gen-html (not ly-gen-html))
  345. (message (concat "HTML generation has been "
  346. (if ly-gen-html "ENABLED." "DISABLED."))))
  347. (defun ly-toggle-pdf-generation ()
  348. "Toggle whether pdf will be generated by compilation."
  349. (interactive)
  350. (setq ly-gen-pdf (not ly-gen-pdf))
  351. (message (concat "PDF generation has been "
  352. (if ly-gen-pdf "ENABLED." "DISABLED."))))
  353. (defun ly-toggle-arrange-mode ()
  354. "Toggle whether in Arrange mode or Basic mode."
  355. (interactive)
  356. (setq ly-arrange-mode
  357. (not ly-arrange-mode))
  358. (message (concat "Arrange mode has been "
  359. (if ly-arrange-mode "ENABLED." "DISABLED."))))
  360. (defun ly-switch-extension (file-name ext)
  361. "Utility command to swap current FILE-NAME extension with EXT"
  362. (concat (file-name-sans-extension
  363. file-name) ext))
  364. (defun ly-get-header-args (mode)
  365. "Default arguments to use when evaluating a lilypond
  366. source block. These depend upon whether we are in arrange
  367. mode i.e. ARRANGE-MODE is t"
  368. (cond (mode
  369. '((:tangle . "yes")
  370. (:noweb . "yes")
  371. (:results . "silent")
  372. (:cache . "yes")
  373. (:comments . "yes")))
  374. (t
  375. '((:results . "file")
  376. (:exports . "results")))))
  377. (defun ly-set-header-args (mode)
  378. "Set org-babel-default-header-args:lilypond
  379. dependent on LY-ARRANGE-MODE"
  380. (setq org-babel-default-header-args:lilypond
  381. (ly-get-header-args mode)))
  382. (provide 'ob-lilypond)
  383. ;;; ob-lilypond.el ends here