Bladeren bron

ob-lilypond: more consistent behavior with other code block languages

>    Hi
>
>    I've added functionality to make ob-lilypond act in a consistent
>    org-babel way by default (think ob-dot).
>
>    The previous modus operandi is now known as arrange-mode and is
>    selected by setting ly-arrange-mode to t
>
>    More details including examples are at
>    http://github.com/mjago/ob-lilypond
Martyn Jago 13 jaren geleden
bovenliggende
commit
514ed6b79c
2 gewijzigde bestanden met toevoegingen van 126 en 23 verwijderingen
  1. 77 12
      lisp/ob-lilypond.el
  2. 49 11
      testing/lisp/test-ob-lilypond.el

+ 77 - 12
lisp/ob-lilypond.el

@@ -5,7 +5,7 @@
 ;; Author: Martyn Jago
 ;; Keywords: babel language, literate programming
 ;; Homepage: https://github.com/mjago/ob-lilypond
-;; Version: 0.2
+;; Version: 0.3
 
 ;; This file is part of GNU Emacs.
 
@@ -33,7 +33,7 @@
 (defalias 'lilypond-mode 'LilyPond-mode)
 (add-to-list 'org-babel-tangle-lang-exts '("LilyPond" . "ly"))
 
-(defconst ly-version "0.2"
+(defconst ly-version "0.3"
   "The version number of the file ob-lilypond.el.")
 
 (defvar ly-compile-post-tangle t
@@ -84,9 +84,14 @@ LY-GEN-HTML to t")
 "You can force the compiler to use the EPS backend by setting
 LY-USE-EPS to t")
 
-(defvar org-babel-default-header-args:lilypond
-  '((:tangle . "yes") (:noweb . "yes") (:results . "silent") (:comments . "yes"))
-  "Default arguments to use when evaluating a lilypond source block.")
+(defvar ly-arrange-mode nil
+  "Arrange mode is turned on by setting LY-ARRANGE-MODE
+to t. In Arrange mode the following settings are altered
+from default...
+:tangle yes,    :noweb yes
+:results silent :comments yes.
+In addition lilypond block execution causes tangling of all lilypond
+blocks")
 
 (defun org-babel-expand-body:lilypond (body params)
   "Expand BODY according to PARAMS, return the expanded body."
@@ -106,9 +111,15 @@ LY-USE-EPS to t")
  
 (defun org-babel-execute:lilypond (body params)
   "This function is called by `org-babel-execute-src-block'.
-tTangle all lilypond blocks and process the result"
+Depending on whether we are in arrange mode either:
+1. Attempt to execute lilypond block according to header settings
+  (This is the default basic mode)
+2. Tangle all lilypond blocks and process the result (arrange mode)"
 
-  (ly-tangle))
+  (ly-set-header-args ly-arrange-mode)
+  (if ly-arrange-mode
+      (ly-tangle)
+    (ly-process-basic body params)))
 
 (defun ly-tangle ()
   "ob-lilypond specific tangle, attempts to invoke
@@ -119,6 +130,32 @@ specific arguments to =org-babel-tangle="
   (if (org-babel-tangle nil "yes" "lilypond")
       (ly-execute-tangled-ly) nil))
 
+(defun ly-process-basic (body params)
+  "Execute a lilypond block in basic mode"
+  
+  (let* ((result-params (cdr (assoc :result-params params)))
+	 (out-file (cdr (assoc :file params)))
+	 (cmdline (or (cdr (assoc :cmdline params))
+		      ""))
+	 (in-file (org-babel-temp-file "dot-")))
+
+    (with-temp-file in-file
+      (insert (org-babel-expand-body:dot body params)))
+    
+    (org-babel-eval
+     (concat
+      (ly-determine-ly-path)
+      " -dbackend=eps "
+      "-dno-gs-load-fonts "
+      "-dinclude-eps-fonts "
+      "--png "
+      "--output="
+      (file-name-sans-extension out-file)
+      " "
+      cmdline
+      in-file) "")
+    ) nil)
+
 (defun org-babel-prep-session:lilypond (session params)
   "Return an error because LilyPond exporter does not support sessions."
 
@@ -161,7 +198,7 @@ FILE-NAME is full path to lilypond (.ly) file"
         (arg-3 "*lilypond*")           ;buffer
         (arg-4 t)                      ;display
         (arg-5 (if ly-gen-png  "--png"  "")) ;&rest...
-        (arg-6 (if ly-gen-html "--html" ""))
+  (arg-6 (if ly-gen-html "--html" ""))
         (arg-7 (if ly-use-eps  "-dbackend=eps" ""))
         (arg-8 (if ly-gen-svg  "-dbackend=svg" ""))
         (arg-9 (concat "--output=" (file-name-sans-extension file-name)))
@@ -340,7 +377,7 @@ If TEST is non-nil, it contains a simulation of the OS for test purposes"
 
 (defun ly-toggle-png-generation ()
   "Toggle whether png image will be generated by compilation"
-  
+
   (interactive)
   (setq ly-gen-png
         (not ly-gen-png))
@@ -349,13 +386,22 @@ If TEST is non-nil, it contains a simulation of the OS for test purposes"
 
 (defun ly-toggle-html-generation ()
   "Toggle whether html will be generated by compilation"
-  
+
   (interactive)
   (setq ly-gen-html
         (not ly-gen-html))
   (message (concat "HTML generation has been "
                    (if ly-gen-html "ENABLED." "DISABLED."))))
 
+(defun ly-toggle-arrange-mode ()
+  "Toggle whether in Arrange mode or Basic mode"
+
+  (interactive)
+  (setq ly-arrange-mode
+        (not ly-arrange-mode))
+  (message (concat "Arrange mode has been "
+                   (if ly-arrange-mode "ENABLED." "DISABLED."))))
+
 (defun ly-version (&optional insert-at-point)
   (interactive)
     (setq version (format "ob-lilypond version %s" ly-version))
@@ -364,12 +410,31 @@ If TEST is non-nil, it contains a simulation of the OS for test purposes"
 
   (defun ly-switch-extension (file-name ext)
   "Utility command to swap current FILE-NAME extension with EXT"
-  
+
   (concat (file-name-sans-extension
            file-name) ext))
 
+(defun ly-get-header-args (mode)
+  "Default arguments to use when evaluating a lilypond
+source block. These depend upon whether we are in arrange
+mode i.e. ARRANGE-MODE is t"
+  (cond (mode
+         '((:tangle . "yes")
+           (:noweb . "yes")
+           (:results . "silent")
+           (:comments . "yes")))
+        (t
+         '((:results . "file")
+           (:exports . "results")))))
+
+(defun ly-set-header-args (mode)
+  "Set org-babel-default-header-args:lilypond
+dependent on LY-ARRANGE-MODE"
+  (setq org-babel-default-header-args:lilypond
+        (ly-get-header-args mode)))
+
 (provide 'ob-lilypond)
 
 ;; arch-tag: ac449eea-2cf2-4dc5-ae33-426f57ba4894
- 
+
 ;;; ob-lilypond.el ends here

+ 49 - 11
testing/lisp/test-ob-lilypond.el

@@ -32,10 +32,10 @@
   (should (boundp 'ly-version)))
 
 (ert-deftest ob-lilypond/ly-version-command ()
-  (should (equal "ob-lilypond version 0.2" (ly-version)))
+  (should (equal "ob-lilypond version 0.3" (ly-version)))
   (with-temp-buffer
     (ly-version t)
-    (should (equal "ob-lilypond version 0.2"
+    (should (equal "ob-lilypond version 0.3"
                    (buffer-substring (point-min) (point-max))))))
 
 (ert-deftest ob-lilypond/ly-compile-lilyfile ()
@@ -109,12 +109,15 @@
 (ert-deftest ob-lilypond/use-eps ()
   (should (boundp 'ly-use-eps)))
 
-(ert-deftest ob-lilypond/org-babel-default-header-args:lilypond ()
-  (should (equal  '((:tangle . "yes")
-                    (:noweb . "yes")
-                    (:results . "silent")
-                    (:comments . "yes"))
-                  org-babel-default-header-args:lilypond)))
+(ert-deftest ob-lilypond/ly-arrange-mode ()
+  (should (boundp 'ly-arrange-mode)))
+
+;; (ert-deftest ob-lilypond/org-babel-default-header-args:lilypond ()
+;;   (should (equal  '((:tangle . "yes")
+;;                     (:noweb . "yes")
+;;                     (:results . "silent")
+;;                     (:comments . "yes"))
+;;                   org-babel-default-header-args:lilypond)))
 
 ;;TODO finish...
 (ert-deftest ob-lilypond/org-babel-expand-body:lilypond ()
@@ -197,7 +200,7 @@
         (pdf-file (concat
                    ly-here
                    "../examples/ob-lilypond-test.pdf")))
-    (setq ly-open-pdf-post-tangle t)
+    (setq ly-display-pdf-post-tangle t)
     (when (not (file-exists-p pdf-file))
       (set-buffer (get-buffer-create (file-name-nondirectory pdf-file)))
       (write-file pdf-file))
@@ -258,7 +261,7 @@
                  (ly-determine-midi-path "win32")))
   (should (equal ly-nix-midi-path
                  (ly-determine-midi-path "nix"))))
- 
+
 (ert-deftest ob-lilypond/ly-toggle-midi-play-toggles-flag ()
   (if ly-play-midi-post-tangle
       (progn
@@ -283,6 +286,18 @@
     (ly-toggle-pdf-display)
     (should (not ly-display-pdf-post-tangle))))
 
+(ert-deftest ob-lilypond/ly-toggle-arrange-mode ()
+  (if ly-arrange-mode
+      (progn
+        (ly-toggle-arrange-mode)
+        (should (not ly-arrange-mode))
+        (ly-toggle-arrange-mode)
+        (should ly-arrange-mode))
+    (ly-toggle-arrange-mode)
+    (should ly-arrange-mode)
+    (ly-toggle-arrange-mode)
+    (should (not ly-arrange-mode))))
+
 (ert-deftest ob-lilypond/ly-toggle-png-generation-toggles-flag ()
   (if ly-gen-png
       (progn
@@ -294,7 +309,7 @@
     (should ly-gen-png)
     (ly-toggle-png-generation)
     (should (not ly-gen-png))))
-
+ 
 (ert-deftest ob-lilypond/ly-toggle-html-generation-toggles-flag ()
   (if ly-gen-html
       (progn
@@ -319,6 +334,29 @@
   (should (equal "/some/path/to/test-name.xyz"
                   (ly-switch-extension "/some/path/to/test-name" ".xyz"))))
 
+(ert-deftest ob-lilypond/ly-get-header-args ()
+  (should (equal '((:tangle . "yes")
+                   (:noweb . "yes")
+                   (:results . "silent")
+                   (:comments . "yes"))
+                 (ly-set-header-args t)))
+  (should (equal '((:results . "file")
+                   (:exports . "results"))
+                 (ly-set-header-args nil))))
+
+(ert-deftest ob-lilypond/ly-set-header-args ()
+  (ly-set-header-args t)
+  (should (equal '((:tangle . "yes")
+                   (:noweb . "yes")
+                   (:results . "silent")
+                   (:comments . "yes"))
+                 org-babel-default-header-args:lilypond))
+  (ly-set-header-args nil)
+  (should (equal '((:results . "file")
+                   (:exports . "results"))
+                 org-babel-default-header-args:lilypond)))
+                 
 (provide 'test-ob-lilypond)
 
 ;;; test-ob-lilypond.el ends here
+