ob-lilypond.el 14 KB

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