ob-lilypond.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. ;;; ob-lilypond.el --- Babel Functions for Lilypond -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2018 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 <https://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.4"
  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 (org-babel--get-vars params)))
  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* ((out-file (cdr (assq :file params)))
  136. (cmdline (or (cdr (assq :cmdline params))
  137. ""))
  138. (in-file (org-babel-temp-file "lilypond-")))
  139. (with-temp-file in-file
  140. (insert (org-babel-expand-body:generic body params)))
  141. (org-babel-eval
  142. (concat
  143. org-babel-lilypond-ly-command
  144. " -dbackend=eps "
  145. "-dno-gs-load-fonts "
  146. "-dinclude-eps-fonts "
  147. (or (cdr (assoc (file-name-extension out-file)
  148. '(("pdf" . "--pdf ")
  149. ("ps" . "--ps ")
  150. ("png" . "--png "))))
  151. "--png ")
  152. "--output="
  153. (file-name-sans-extension out-file)
  154. " "
  155. cmdline
  156. in-file) "")) nil)
  157. (defun org-babel-prep-session:lilypond (_session _params)
  158. "Return an error because LilyPond exporter does not support sessions."
  159. (error "Sorry, LilyPond does not currently support sessions!"))
  160. (defun org-babel-lilypond-execute-tangled-ly ()
  161. "Compile result of block tangle with lilypond.
  162. If error in compilation, attempt to mark the error in lilypond org file"
  163. (when org-babel-lilypond-compile-post-tangle
  164. (let ((org-babel-lilypond-tangled-file (org-babel-lilypond-switch-extension
  165. (buffer-file-name) ".lilypond"))
  166. (org-babel-lilypond-temp-file (org-babel-lilypond-switch-extension
  167. (buffer-file-name) ".ly")))
  168. (if (not (file-exists-p org-babel-lilypond-tangled-file))
  169. (error "Error: Tangle Failed!")
  170. (when (file-exists-p org-babel-lilypond-temp-file)
  171. (delete-file org-babel-lilypond-temp-file))
  172. (rename-file org-babel-lilypond-tangled-file
  173. org-babel-lilypond-temp-file))
  174. (switch-to-buffer-other-window "*lilypond*")
  175. (erase-buffer)
  176. (org-babel-lilypond-compile-lilyfile org-babel-lilypond-temp-file)
  177. (goto-char (point-min))
  178. (if (org-babel-lilypond-check-for-compile-error org-babel-lilypond-temp-file)
  179. (error "Error in Compilation!")
  180. (other-window -1)
  181. (org-babel-lilypond-attempt-to-open-pdf org-babel-lilypond-temp-file)
  182. (org-babel-lilypond-attempt-to-play-midi org-babel-lilypond-temp-file)))))
  183. (defun org-babel-lilypond-compile-lilyfile (file-name &optional test)
  184. "Compile lilypond file and check for compile errors
  185. FILE-NAME is full path to lilypond (.ly) file"
  186. (message "Compiling LilyPond...")
  187. (let ((arg-1 org-babel-lilypond-ly-command) ;program
  188. (arg-2 nil) ;infile
  189. (arg-3 "*lilypond*") ;buffer
  190. (arg-4 t) ;display
  191. (arg-5 (if org-babel-lilypond-gen-png "--png" "")) ;&rest...
  192. (arg-6 (if org-babel-lilypond-gen-html "--html" ""))
  193. (arg-7 (if org-babel-lilypond-gen-pdf "--pdf" ""))
  194. (arg-8 (if org-babel-lilypond-use-eps "-dbackend=eps" ""))
  195. (arg-9 (if org-babel-lilypond-gen-svg "-dbackend=svg" ""))
  196. (arg-10 (concat "--output=" (file-name-sans-extension file-name)))
  197. (arg-11 file-name))
  198. (if test
  199. `(,arg-1 ,arg-2 ,arg-3 ,arg-4 ,arg-5 ,arg-6
  200. ,arg-7 ,arg-8 ,arg-9 ,arg-10 ,arg-11)
  201. (call-process
  202. arg-1 arg-2 arg-3 arg-4 arg-5 arg-6
  203. arg-7 arg-8 arg-9 arg-10 arg-11))))
  204. (defun org-babel-lilypond-check-for-compile-error (file-name &optional test)
  205. "Check for compile error.
  206. This is performed by parsing the *lilypond* buffer
  207. containing the output message from the compilation.
  208. FILE-NAME is full path to lilypond file.
  209. If TEST is t just return nil if no error found, and pass
  210. nil as file-name since it is unused in this context"
  211. (let ((is-error (search-forward "error:" nil t)))
  212. (if test
  213. is-error
  214. (when is-error
  215. (org-babel-lilypond-process-compile-error file-name)))))
  216. (defun org-babel-lilypond-process-compile-error (file-name)
  217. "Process the compilation error that has occurred.
  218. FILE-NAME is full path to lilypond file"
  219. (let ((line-num (org-babel-lilypond-parse-line-num)))
  220. (let ((error-lines (org-babel-lilypond-parse-error-line file-name line-num)))
  221. (org-babel-lilypond-mark-error-line file-name error-lines)
  222. (error "Error: Compilation Failed!"))))
  223. (defun org-babel-lilypond-mark-error-line (file-name line)
  224. "Mark the erroneous lines in the lilypond org buffer.
  225. FILE-NAME is full path to lilypond file.
  226. LINE is the erroneous line"
  227. (switch-to-buffer-other-window
  228. (concat (file-name-nondirectory
  229. (org-babel-lilypond-switch-extension file-name ".org"))))
  230. (let ((temp (point)))
  231. (goto-char (point-min))
  232. (setq case-fold-search nil)
  233. (if (search-forward line nil t)
  234. (progn
  235. (outline-show-all)
  236. (set-mark (point))
  237. (goto-char (- (point) (length line))))
  238. (goto-char temp))))
  239. (defun org-babel-lilypond-parse-line-num (&optional buffer)
  240. "Extract error line number."
  241. (when buffer (set-buffer buffer))
  242. (let ((start
  243. (and (search-backward ":" nil t)
  244. (search-backward ":" nil t)
  245. (search-backward ":" nil t)
  246. (search-backward ":" nil t))))
  247. (when start
  248. (forward-char)
  249. (let ((num (string-to-number
  250. (buffer-substring
  251. (+ 1 start)
  252. (- (search-forward ":" nil t) 1)))))
  253. (and (numberp num) num)))))
  254. (defun org-babel-lilypond-parse-error-line (file-name lineNo)
  255. "Extract the erroneous line from the tangled .ly file
  256. FILE-NAME is full path to lilypond file.
  257. LINENO is the number of the erroneous line"
  258. (with-temp-buffer
  259. (insert-file-contents (org-babel-lilypond-switch-extension file-name ".ly")
  260. nil nil nil t)
  261. (if (> lineNo 0)
  262. (progn
  263. (goto-char (point-min))
  264. (forward-line (- lineNo 1))
  265. (buffer-substring (point) (point-at-eol)))
  266. nil)))
  267. (defun org-babel-lilypond-attempt-to-open-pdf (file-name &optional test)
  268. "Attempt to display the generated pdf file
  269. FILE-NAME is full path to lilypond file
  270. If TEST is non-nil, the shell command is returned and is not run"
  271. (when org-babel-lilypond-display-pdf-post-tangle
  272. (let ((pdf-file (org-babel-lilypond-switch-extension file-name ".pdf")))
  273. (if (file-exists-p pdf-file)
  274. (let ((cmd-string
  275. (concat org-babel-lilypond-pdf-command " " pdf-file)))
  276. (if test
  277. cmd-string
  278. (start-process
  279. "\"Audition pdf\""
  280. "*lilypond*"
  281. org-babel-lilypond-pdf-command
  282. pdf-file)))
  283. (message "No pdf file generated so can't display!")))))
  284. (defun org-babel-lilypond-attempt-to-play-midi (file-name &optional test)
  285. "Attempt to play the generated MIDI file
  286. FILE-NAME is full path to lilypond file
  287. If TEST is non-nil, the shell command is returned and is not run"
  288. (when org-babel-lilypond-play-midi-post-tangle
  289. (let ((midi-file (org-babel-lilypond-switch-extension file-name ".midi")))
  290. (if (file-exists-p midi-file)
  291. (let ((cmd-string
  292. (concat org-babel-lilypond-midi-command " " midi-file)))
  293. (if test
  294. cmd-string
  295. (start-process
  296. "\"Audition midi\""
  297. "*lilypond*"
  298. org-babel-lilypond-midi-command
  299. midi-file)))
  300. (message "No midi file generated so can't play!")))))
  301. (defun org-babel-lilypond-toggle-midi-play ()
  302. "Toggle whether midi will be played following a successful compilation."
  303. (interactive)
  304. (setq org-babel-lilypond-play-midi-post-tangle
  305. (not org-babel-lilypond-play-midi-post-tangle))
  306. (message (concat "Post-Tangle MIDI play has been "
  307. (if org-babel-lilypond-play-midi-post-tangle
  308. "ENABLED." "DISABLED."))))
  309. (defun org-babel-lilypond-toggle-pdf-display ()
  310. "Toggle whether pdf will be displayed following a successful compilation."
  311. (interactive)
  312. (setq org-babel-lilypond-display-pdf-post-tangle
  313. (not org-babel-lilypond-display-pdf-post-tangle))
  314. (message (concat "Post-Tangle PDF display has been "
  315. (if org-babel-lilypond-display-pdf-post-tangle
  316. "ENABLED." "DISABLED."))))
  317. (defun org-babel-lilypond-toggle-png-generation ()
  318. "Toggle whether png image will be generated by compilation."
  319. (interactive)
  320. (setq org-babel-lilypond-gen-png (not org-babel-lilypond-gen-png))
  321. (message (concat "PNG image generation has been "
  322. (if org-babel-lilypond-gen-png "ENABLED." "DISABLED."))))
  323. (defun org-babel-lilypond-toggle-html-generation ()
  324. "Toggle whether html will be generated by compilation."
  325. (interactive)
  326. (setq org-babel-lilypond-gen-html (not org-babel-lilypond-gen-html))
  327. (message (concat "HTML generation has been "
  328. (if org-babel-lilypond-gen-html "ENABLED." "DISABLED."))))
  329. (defun org-babel-lilypond-toggle-pdf-generation ()
  330. "Toggle whether pdf will be generated by compilation."
  331. (interactive)
  332. (setq org-babel-lilypond-gen-pdf (not org-babel-lilypond-gen-pdf))
  333. (message (concat "PDF generation has been "
  334. (if org-babel-lilypond-gen-pdf "ENABLED." "DISABLED."))))
  335. (defun org-babel-lilypond-toggle-arrange-mode ()
  336. "Toggle whether in Arrange mode or Basic mode."
  337. (interactive)
  338. (setq org-babel-lilypond-arrange-mode
  339. (not org-babel-lilypond-arrange-mode))
  340. (message (concat "Arrange mode has been "
  341. (if org-babel-lilypond-arrange-mode "ENABLED." "DISABLED."))))
  342. (defun org-babel-lilypond-switch-extension (file-name ext)
  343. "Utility command to swap current FILE-NAME extension with EXT"
  344. (concat (file-name-sans-extension
  345. file-name) ext))
  346. (defun org-babel-lilypond-get-header-args (mode)
  347. "Default arguments to use when evaluating a lilypond
  348. source block. These depend upon whether we are in arrange
  349. mode i.e. ARRANGE-MODE is t"
  350. (cond (mode
  351. '((:tangle . "yes")
  352. (:noweb . "yes")
  353. (:results . "silent")
  354. (:cache . "yes")
  355. (:comments . "yes")))
  356. (t
  357. '((:results . "file")
  358. (:exports . "results")))))
  359. (defun org-babel-lilypond-set-header-args (mode)
  360. "Set org-babel-default-header-args:lilypond
  361. dependent on ORG-BABEL-LILYPOND-ARRANGE-MODE"
  362. (setq org-babel-default-header-args:lilypond
  363. (org-babel-lilypond-get-header-args mode)))
  364. (provide 'ob-lilypond)
  365. ;;; ob-lilypond.el ends here