ob-lilypond.el 16 KB

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