ob-lilypond.el 14 KB

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