ob-lilypond.el 17 KB

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