ob-lilypond.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. ;;; ob-lilypond.el --- org-babel functions for lilypond evaluation
  2. ;; Copyright (C) Shelagh Manton, Martyn Jago
  3. ;; Authors: Shelagh Manton, Martyn Jago
  4. ;; Keywords: literate programming, weaving markup
  5. ;; Homepage: https://github.com/sshelagh/ob-lilypond
  6. ;; Version: 0.1
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; see http://github.com/mjago/ob-lilypond
  24. ;;; Requirements:
  25. ;; You need to have a copy of LilyPond
  26. (require 'ob)
  27. (require 'ob-eval)
  28. (defalias 'lilypond-mode 'LilyPond-mode)
  29. (add-to-list 'org-babel-tangle-lang-exts '("LilyPond" . "ly"))
  30. (defconst ly-version "0.1"
  31. "The version number of the file ob-lilypond.el.")
  32. (defvar ly-compile-post-tangle t
  33. "Following the org-babel-tangle (C-c C-v t) command,
  34. ly-compile-post-tangle determines whether ob-lilypond should
  35. automatically attempt to compile the resultant tangled file.
  36. If the value is nil, no automated compilation takes place.
  37. Default value is t")
  38. (defvar ly-display-pdf-post-tangle t
  39. "Following a successful LilyPond compilation
  40. ly-display-pdf-post-tangle determines whether to automate the
  41. drawing / redrawing of the resultant pdf. If the value is nil,
  42. the pdf is not automatically redrawn. Default value is t")
  43. (defvar ly-play-midi-post-tangle t
  44. "Following a successful LilyPond compilation
  45. ly-play-midi-post-tangle determines whether to automate the
  46. playing of the resultant midi file. If the value is nil,
  47. the midi file is not automatically played. Default value is t")
  48. (defvar ly-OSX-ly-path
  49. "/Applications/lilypond.app/Contents/Resources/bin/lilypond")
  50. (defvar ly-OSX-pdf-path "open")
  51. (defvar ly-OSX-midi-path "open")
  52. (defvar ly-nix-ly-path "/usr/bin/lilypond")
  53. (defvar ly-nix-pdf-path "evince")
  54. (defvar ly-nix-midi-path "timidity")
  55. (defvar ly-win32-ly-path "lilypond")
  56. (defvar ly-win32-pdf-path "")
  57. (defvar ly-win32-midi-path "")
  58. (defvar ly-gen-png nil
  59. "Image generation (png) can be turned on by default by setting
  60. LY-GEN-PNG to t")
  61. (defvar ly-gen-svg nil
  62. "Image generation (SVG) can be turned on by default by setting
  63. LY-GEN-SVG to t")
  64. (defvar ly-gen-html nil
  65. "HTML generation can be turned on by default by setting
  66. LY-GEN-HTML to t")
  67. (defvar ly-use-eps nil
  68. "You can force the compiler to use the EPS backend by setting
  69. LY-USE-EPS to t")
  70. (defvar org-babel-default-header-args:lilypond
  71. '((:tangle . "yes") (:noweb . "yes") (:results . "silent") (:comments . "yes"))
  72. "Default arguments to use when evaluating a lilypond source block.")
  73. (defun org-babel-expand-body:lilypond (body params)
  74. "Expand BODY according to PARAMS, return the expanded body."
  75. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  76. (mapc
  77. (lambda (pair)
  78. (let ((name (symbol-name (car pair)))
  79. (value (cdr pair)))
  80. (setq body
  81. (replace-regexp-in-string
  82. (concat "\$" (regexp-quote name))
  83. (if (stringp value) value (format "%S" value))
  84. body))))
  85. vars)
  86. body))
  87. (defun org-babel-execute:lilypond (body params)
  88. "This function is called by `org-babel-execute-src-block'.
  89. tTangle all lilypond blocks and process the result"
  90. (ly-tangle))
  91. (defun ly-tangle ()
  92. "ob-lilypond specific tangle, attempts to invoke
  93. =ly-execute-tangled-ly= if tangle is successful. Also passes
  94. specific arguments to =org-babel-tangle="
  95. (interactive)
  96. (if (org-babel-tangle nil "yes" "lilypond")
  97. (ly-execute-tangled-ly) nil))
  98. (defun org-babel-prep-session:lilypond (session params)
  99. "Return an error because LilyPond exporter does not support sessions."
  100. (error "Sorry, LilyPond does not currently support sessions!"))
  101. (defun ly-execute-tangled-ly ()
  102. "Compile result of block tangle with lilypond.
  103. If error in compilation, attempt to mark the error in lilypond org file"
  104. (when ly-compile-post-tangle
  105. (let ((ly-tangled-file (ly-switch-extension
  106. (buffer-file-name) ".lilypond"))
  107. (ly-temp-file (ly-switch-extension
  108. (buffer-file-name) ".ly")))
  109. (if (file-exists-p ly-tangled-file)
  110. (progn
  111. (when (file-exists-p ly-temp-file)
  112. (delete-file ly-temp-file))
  113. (rename-file ly-tangled-file
  114. ly-temp-file))
  115. (error "Error: Tangle Failed!") t)
  116. (switch-to-buffer-other-window "*lilypond*")
  117. (erase-buffer)
  118. (ly-compile-lilyfile ly-temp-file)
  119. (goto-char (point-min))
  120. (if (not (ly-check-for-compile-error ly-temp-file))
  121. (progn
  122. (other-window -1)
  123. (ly-attempt-to-open-pdf ly-temp-file)
  124. (ly-attempt-to-play-midi ly-temp-file))
  125. (error "Error in Compilation!")))) nil)
  126. (defun ly-compile-lilyfile (file-name &optional test)
  127. "Compile lilypond file and check for compile errors
  128. FILE-NAME is full path to lilypond (.ly) file"
  129. (message "Compiling LilyPond...")
  130. (let ((arg-1 (ly-determine-ly-path)) ;program
  131. (arg-2 nil) ;infile
  132. (arg-3 "*lilypond*") ;buffer
  133. (arg-4 t) ;display
  134. (arg-5 (if ly-gen-png "--png" "")) ;&rest...
  135. (arg-6 (if ly-gen-html "--html" ""))
  136. (arg-7 (if ly-use-eps "-dbackend=eps" ""))
  137. (arg-8 (if ly-gen-svg "-dbackend=svg" ""))
  138. (arg-9 (concat "--output=" (file-name-sans-extension file-name)))
  139. (arg-10 file-name))
  140. (if test
  141. `(,arg-1 ,arg-2 ,arg-3 ,arg-4 ,arg-5
  142. ,arg-6 ,arg-7 ,arg-8 ,arg-9 ,arg-10)
  143. (call-process
  144. arg-1 arg-2 arg-3 arg-4 arg-5
  145. arg-6 arg-7 arg-8 arg-9 arg-10))))
  146. (defun ly-check-for-compile-error (file-name &optional test)
  147. "Check for compile error.
  148. This is performed by parsing the *lilypond* buffer
  149. containing the output message from the compilation.
  150. FILE-NAME is full path to lilypond file.
  151. If TEST is t just return nil if no error found, and pass
  152. nil as file-name since it is unused in this context"
  153. (let ((is-error (search-forward "error:" nil t)))
  154. (if (not test)
  155. (if (not is-error)
  156. nil
  157. (ly-process-compile-error file-name))
  158. is-error)))
  159. (defun ly-process-compile-error (file-name)
  160. "Process the compilation error that has occurred.
  161. FILE-NAME is full path to lilypond file"
  162. (let ((line-num (ly-parse-line-num)))
  163. (let ((error-lines (ly-parse-error-line file-name line-num)))
  164. (ly-mark-error-line file-name error-lines)
  165. (error "Error: Compilation Failed!"))))
  166. (defun ly-mark-error-line (file-name line)
  167. "Mark the erroneous lines in the lilypond org buffer.
  168. FILE-NAME is full path to lilypond file.
  169. LINE is the erroneous line"
  170. (switch-to-buffer-other-window
  171. (concat (file-name-nondirectory
  172. (ly-switch-extension file-name ".org"))))
  173. (let ((temp (point)))
  174. (goto-char (point-min))
  175. (setq case-fold-search nil)
  176. (if (search-forward line nil t)
  177. (progn
  178. (show-all)
  179. (set-mark (point))
  180. (goto-char (- (point) (length line))))
  181. (goto-char temp))))
  182. (defun ly-parse-line-num (&optional buffer)
  183. "Extract error line number."
  184. (when buffer
  185. (set-buffer buffer))
  186. (let ((start
  187. (and (search-backward ":" nil t)
  188. (search-backward ":" nil t)
  189. (search-backward ":" nil t)
  190. (search-backward ":" nil t)))
  191. (num nil))
  192. (if start
  193. (progn
  194. (forward-char)
  195. (let ((num (buffer-substring
  196. (+ 1 start)
  197. (- (search-forward ":" nil t) 1))))
  198. (setq num (string-to-number num))
  199. (if (numberp num)
  200. num
  201. nil)))
  202. nil)))
  203. (defun ly-parse-error-line (file-name lineNo)
  204. "Extract the erroneous line from the tangled .ly file
  205. FILE-NAME is full path to lilypond file.
  206. LINENO is the number of the erroneous line"
  207. (set-buffer (get-buffer-create "temp-buf"))
  208. (insert-file-contents (ly-switch-extension file-name ".ly")
  209. nil nil nil t)
  210. (if (> lineNo 0)
  211. (progn
  212. (goto-line lineNo)
  213. (buffer-substring (point) (point-at-eol)))
  214. nil))
  215. (defun ly-attempt-to-open-pdf (file-name &optional test)
  216. "Attempt to display the generated pdf file
  217. FILE-NAME is full path to lilypond file
  218. If TEST is non-nil, the shell command is returned and is not run"
  219. (when ly-display-pdf-post-tangle
  220. (let ((pdf-file (ly-switch-extension file-name ".pdf")))
  221. (if (file-exists-p pdf-file)
  222. (let ((cmd-string
  223. (concat (ly-determine-pdf-path) " " pdf-file)))
  224. (if test
  225. cmd-string
  226. (shell-command cmd-string)))
  227. (message "No pdf file generated so can't display!")))))
  228. (defun ly-attempt-to-play-midi (file-name &optional test)
  229. "Attempt to play the generated MIDI file
  230. FILE-NAME is full path to lilypond file
  231. If TEST is non-nil, the shell command is returned and is not run"
  232. (when ly-play-midi-post-tangle
  233. (let ((midi-file (ly-switch-extension file-name ".midi")))
  234. (if (file-exists-p midi-file)
  235. (let ((cmd-string
  236. (concat (ly-determine-midi-path) " " midi-file)))
  237. (if test
  238. cmd-string
  239. (shell-command cmd-string)))
  240. (message "No midi file generated so can't play!")))))
  241. (defun ly-determine-ly-path (&optional test)
  242. "Return correct path to ly binary depending on OS
  243. If TEST is non-nil, it contains a simulation of the OS for test purposes"
  244. (let ((sys-type
  245. (or test system-type)))
  246. (cond ((string= sys-type "darwin")
  247. ly-OSX-ly-path)
  248. ((string= sys-type "win32")
  249. ly-win32-ly-path)
  250. (t ly-nix-ly-path))))
  251. (defun ly-determine-pdf-path (&optional test)
  252. "Return correct path to pdf viewer depending on OS
  253. If TEST is non-nil, it contains a simulation of the OS for test purposes"
  254. (let ((sys-type
  255. (or test system-type)))
  256. (cond ((string= sys-type "darwin")
  257. ly-OSX-pdf-path)
  258. ((string= sys-type "win32")
  259. ly-win32-pdf-path)
  260. (t ly-nix-pdf-path))))
  261. (defun ly-determine-midi-path (&optional test)
  262. "Return correct path to midi player depending on OS
  263. If TEST is non-nil, it contains a simulation of the OS for test purposes"
  264. (let ((sys-type
  265. (or test test system-type)))
  266. (cond ((string= sys-type "darwin")
  267. ly-OSX-midi-path)
  268. ((string= sys-type "win32")
  269. ly-win32-midi-path)
  270. (t ly-nix-midi-path))))
  271. (defun ly-toggle-midi-play ()
  272. "Toggle whether midi will be played following a successful compilation"
  273. (interactive)
  274. (setq ly-play-midi-post-tangle
  275. (not ly-play-midi-post-tangle))
  276. (message (concat "Post-Tangle MIDI play has been "
  277. (if ly-play-midi-post-tangle
  278. "ENABLED." "DISABLED."))))
  279. (defun ly-toggle-pdf-display ()
  280. "Toggle whether pdf will be displayed following a successful compilation"
  281. (interactive)
  282. (setq ly-display-pdf-post-tangle
  283. (not ly-display-pdf-post-tangle))
  284. (message (concat "Post-Tangle PDF display has been "
  285. (if ly-display-pdf-post-tangle
  286. "ENABLED." "DISABLED."))))
  287. (defun ly-toggle-png-generation ()
  288. "Toggle whether png image will be generated by compilation"
  289. (interactive)
  290. (setq ly-gen-png
  291. (not ly-gen-png))
  292. (message (concat "PNG image generation has been "
  293. (if ly-gen-png "ENABLED." "DISABLED."))))
  294. (defun ly-toggle-html-generation ()
  295. "Toggle whether html will be generated by compilation"
  296. (interactive)
  297. (setq ly-gen-html
  298. (not ly-gen-html))
  299. (message (concat "HTML generation has been "
  300. (if ly-gen-html "ENABLED." "DISABLED."))))
  301. (defun ly-version (&optional insert-at-point)
  302. (interactive)
  303. (setq version (format "ob-lilypond version %s" ly-version))
  304. (when insert-at-point (insert version))
  305. (message version))
  306. (defun ly-switch-extension (file-name ext)
  307. "Utility command to swap current FILE-NAME extension with EXT"
  308. (concat (file-name-sans-extension
  309. file-name) ext))
  310. (provide 'ob-lilypond)
  311. ;;; ob-lilypond.el ends here