ob-lilypond.el 16 KB

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