ob-lilypond.el 14 KB

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