ob-lilypond.el 17 KB

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