ob-lilypond.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. ;;; ob-lilypond.el --- Babel Functions for Lilypond -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. ;; Author: Martyn Jago
  4. ;; Keywords: babel language, literate programming
  5. ;; Homepage: https://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. ;; https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html
  20. ;;
  21. ;; Lilypond documentation can be found at
  22. ;; https://lilypond.org/manuals.html
  23. ;;
  24. ;; This depends on epstopdf --- See https://www.ctan.org/pkg/epstopdf.
  25. ;;; Code:
  26. (require 'ob)
  27. (declare-function org-show-all "org" (&optional types))
  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. (set symbol value)
  82. (setq
  83. org-babel-lilypond-ly-command (nth 0 value)
  84. org-babel-lilypond-pdf-command (nth 1 value)
  85. org-babel-lilypond-midi-command (nth 2 value))))
  86. (defvar org-babel-lilypond-gen-png nil
  87. "Non-nil means image generation (PNG) is turned on by default.")
  88. (defvar org-babel-lilypond-gen-svg nil
  89. "Non-nil means image generation (SVG) is be turned on by default.")
  90. (defvar org-babel-lilypond-gen-html nil
  91. "Non-nil means HTML generation is turned on by default.")
  92. (defvar org-babel-lilypond-gen-pdf nil
  93. "Non-nil means PDF generation is be turned on by default.")
  94. (defvar org-babel-lilypond-use-eps nil
  95. "Non-nil forces the compiler to use the EPS backend.")
  96. (defvar org-babel-lilypond-arrange-mode nil
  97. "Non-nil turns Arrange mode on.
  98. In Arrange mode the following settings are altered from default:
  99. :tangle yes, :noweb yes
  100. :results silent :comments yes.
  101. In addition lilypond block execution causes tangling of all lilypond
  102. blocks.")
  103. (defun org-babel-expand-body:lilypond (body params)
  104. "Expand BODY according to PARAMS, return the expanded body."
  105. (let ((vars (org-babel--get-vars params)))
  106. (mapc
  107. (lambda (pair)
  108. (let ((name (symbol-name (car pair)))
  109. (value (cdr pair)))
  110. (setq body
  111. (replace-regexp-in-string
  112. (concat "$" (regexp-quote name))
  113. (if (stringp value) value (format "%S" value))
  114. body))))
  115. vars)
  116. body))
  117. (defun org-babel-execute:lilypond (body params)
  118. "This function is called by `org-babel-execute-src-block'.
  119. Depending on whether we are in arrange mode either:
  120. 1. Attempt to execute lilypond block according to header settings
  121. (This is the default basic mode)
  122. 2. Tangle all lilypond blocks and process the result (arrange mode)"
  123. (org-babel-lilypond-set-header-args org-babel-lilypond-arrange-mode)
  124. (if org-babel-lilypond-arrange-mode
  125. (org-babel-lilypond-tangle)
  126. (org-babel-lilypond-process-basic body params)))
  127. (defun org-babel-lilypond-tangle ()
  128. "ob-lilypond specific tangle, attempts to invoke
  129. =ly-execute-tangled-ly= if tangle is successful. Also passes
  130. specific arguments to =org-babel-tangle=."
  131. (interactive)
  132. (if (org-babel-tangle nil "yes" "lilypond")
  133. (org-babel-lilypond-execute-tangled-ly) nil))
  134. (defun org-babel-lilypond-process-basic (body params)
  135. "Execute a lilypond block in basic mode."
  136. (let* ((out-file (cdr (assq :file params)))
  137. (cmdline (or (cdr (assq :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. (org-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 ,nil ,arg-3 ,arg-4 ,arg-5 ,arg-6 ;; arg-2
  201. ,arg-7 ,arg-8 ,arg-9 ,arg-10 ,arg-11)
  202. (call-process
  203. arg-1 nil arg-3 arg-4 arg-5 arg-6 ;; arg-2
  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. (org-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. (org-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 (set-buffer buffer))
  243. (let ((start
  244. (and (search-backward ":" nil t)
  245. (search-backward ":" nil t)
  246. (search-backward ":" nil t)
  247. (search-backward ":" nil t))))
  248. (when start
  249. (forward-char)
  250. (let ((num (string-to-number
  251. (buffer-substring
  252. (+ 1 start)
  253. (- (search-forward ":" nil t) 1)))))
  254. (and (numberp num) num)))))
  255. (defun org-babel-lilypond-parse-error-line (file-name lineNo)
  256. "Extract the erroneous line from the tangled .ly file.
  257. FILE-NAME is full path to lilypond file.
  258. LINENO is the number of the erroneous line."
  259. (with-temp-buffer
  260. (insert-file-contents (org-babel-lilypond-switch-extension file-name ".ly")
  261. nil nil nil t)
  262. (if (> lineNo 0)
  263. (progn
  264. (goto-char (point-min))
  265. (forward-line (- lineNo 1))
  266. (buffer-substring (point) (point-at-eol)))
  267. nil)))
  268. (defun org-babel-lilypond-attempt-to-open-pdf (file-name &optional test)
  269. "Attempt to display the generated pdf file.
  270. FILE-NAME is full path to lilypond file.
  271. If TEST is non-nil, the shell command is returned and is not run."
  272. (when org-babel-lilypond-display-pdf-post-tangle
  273. (let ((pdf-file (org-babel-lilypond-switch-extension file-name ".pdf")))
  274. (if (file-exists-p pdf-file)
  275. (let ((cmd-string
  276. (concat org-babel-lilypond-pdf-command " " pdf-file)))
  277. (if test
  278. cmd-string
  279. (start-process
  280. "\"Audition pdf\""
  281. "*lilypond*"
  282. org-babel-lilypond-pdf-command
  283. pdf-file)))
  284. (message "No pdf file generated so can't display!")))))
  285. (defun org-babel-lilypond-attempt-to-play-midi (file-name &optional test)
  286. "Attempt to play the generated MIDI file.
  287. FILE-NAME is full path to lilypond file.
  288. If TEST is non-nil, the shell command is returned and is not run."
  289. (when org-babel-lilypond-play-midi-post-tangle
  290. (let ((midi-file (org-babel-lilypond-switch-extension file-name ".midi")))
  291. (if (file-exists-p midi-file)
  292. (let ((cmd-string
  293. (concat org-babel-lilypond-midi-command " " midi-file)))
  294. (if test
  295. cmd-string
  296. (start-process
  297. "\"Audition midi\""
  298. "*lilypond*"
  299. org-babel-lilypond-midi-command
  300. midi-file)))
  301. (message "No midi file generated so can't play!")))))
  302. (defun org-babel-lilypond-toggle-midi-play ()
  303. "Toggle whether midi will be played following a successful compilation."
  304. (interactive)
  305. (setq org-babel-lilypond-play-midi-post-tangle
  306. (not org-babel-lilypond-play-midi-post-tangle))
  307. (message (concat "Post-Tangle MIDI play has been "
  308. (if org-babel-lilypond-play-midi-post-tangle
  309. "ENABLED." "DISABLED."))))
  310. (defun org-babel-lilypond-toggle-pdf-display ()
  311. "Toggle whether pdf will be displayed following a successful compilation."
  312. (interactive)
  313. (setq org-babel-lilypond-display-pdf-post-tangle
  314. (not org-babel-lilypond-display-pdf-post-tangle))
  315. (message (concat "Post-Tangle PDF display has been "
  316. (if org-babel-lilypond-display-pdf-post-tangle
  317. "ENABLED." "DISABLED."))))
  318. (defun org-babel-lilypond-toggle-png-generation ()
  319. "Toggle whether png image will be generated by compilation."
  320. (interactive)
  321. (setq org-babel-lilypond-gen-png (not org-babel-lilypond-gen-png))
  322. (message (concat "PNG image generation has been "
  323. (if org-babel-lilypond-gen-png "ENABLED." "DISABLED."))))
  324. (defun org-babel-lilypond-toggle-html-generation ()
  325. "Toggle whether html will be generated by compilation."
  326. (interactive)
  327. (setq org-babel-lilypond-gen-html (not org-babel-lilypond-gen-html))
  328. (message (concat "HTML generation has been "
  329. (if org-babel-lilypond-gen-html "ENABLED." "DISABLED."))))
  330. (defun org-babel-lilypond-toggle-pdf-generation ()
  331. "Toggle whether pdf will be generated by compilation."
  332. (interactive)
  333. (setq org-babel-lilypond-gen-pdf (not org-babel-lilypond-gen-pdf))
  334. (message (concat "PDF generation has been "
  335. (if org-babel-lilypond-gen-pdf "ENABLED." "DISABLED."))))
  336. (defun org-babel-lilypond-toggle-arrange-mode ()
  337. "Toggle whether in Arrange mode or Basic mode."
  338. (interactive)
  339. (setq org-babel-lilypond-arrange-mode
  340. (not org-babel-lilypond-arrange-mode))
  341. (message (concat "Arrange mode has been "
  342. (if org-babel-lilypond-arrange-mode "ENABLED." "DISABLED."))))
  343. (defun org-babel-lilypond-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)
  347. ext))
  348. (defun org-babel-lilypond-get-header-args (mode)
  349. "Default arguments to use when evaluating a lilypond source block.
  350. These depend upon whether we are in Arrange mode i.e. MODE is t."
  351. (cond (mode
  352. '((:tangle . "yes")
  353. (:noweb . "yes")
  354. (:results . "silent")
  355. (:cache . "yes")
  356. (:comments . "yes")))
  357. (t
  358. '((:results . "file")
  359. (:exports . "results")))))
  360. (defun org-babel-lilypond-set-header-args (mode)
  361. "Set org-babel-default-header-args:lilypond
  362. dependent on ORG-BABEL-LILYPOND-ARRANGE-MODE."
  363. (setq org-babel-default-header-args:lilypond
  364. (org-babel-lilypond-get-header-args mode)))
  365. (provide 'ob-lilypond)
  366. ;;; ob-lilypond.el ends here