ob-lilypond.el 12 KB

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