ob-lilypond.el 15 KB

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