ob-lilypond.el 14 KB

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