ob-lilypond.el 16 KB

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