[CM] Sending code to the REPL from Emacs

Tito Latini tito.01beta at gmail.com
Sun Aug 1 01:47:37 PDT 2021


On Sat, Jul 31, 2021 at 10:52:19PM +0000, Brad Christensen wrote:
> Any Emacs users out there with working s7 REPL configs?
> 
> I quite like the idea of keeping certain code in an org file and sending code to the REPL.
> [...]

I can share my Babel functions for Snd-Scheme Evaluation.
They are based on ob-lisp.el (GPL).

This message includes two attached files:

  - ob-snd-scheme.el
  - snd-scheme-example.org

Copy ob-snd-scheme.el to a directory in your load-path and update
the alist

  (org-babel-do-load-languages 'org-babel-load-languages
    ((snd-scheme . t) [...]))

in .emacs file (or require ob-snd-scheme) to enable the language.
See also [[info:org#Languages]]

Note: if the output is a sequence, it is corrupted if the length
is less than print-length.
-------------- next part --------------
;;; Babel Functions for Snd-Scheme Evaluation based on ob-lisp.el
;;; (GNU General Public License <https://www.gnu.org/licenses/>).
;;; Tito Latini 2018

;;; Note: if the output is a sequence, it is corrupted if the length
;;; is less than print-length.

(defun org-babel-expand-body:snd-scheme (body params)
  "Expand BODY according to PARAMS, return the expanded body."
  (let* ((vars (org-babel--get-vars params))
	 (body (format "(let (%s)\n%s)"
		       (mapconcat
			 (lambda (var)
			   (format "(%S (quote %S))" (car var) (cdr var)))
			 vars "\n      ")
                       body)))
    (if (intersection '("code" "pp") (cdr (assq :result-params params))
                      :test #'string=)
	(format "(snd-print %s)" body)
        body)))

(defun org-babel-execute:snd-scheme (body params)
  "Execute a block of Snd-Scheme code with Babel.
BODY is the contents of the block, as a string.  PARAMS is
a property list containing the parameters of the block."
  (org-babel-reassemble-table
    (let ((result (with-temp-buffer
                    (insert (org-babel-expand-body:snd-scheme body params))
                    (let* ((str "")
                           (comint-output-filter-functions
                             (list (lambda (text) (setq str (concat str text)))))
                           (proc (get-buffer-process (inf-snd-proc-buffer))))
                      (comint-send-region proc (point-min) (point-max))
                      (comint-send-string proc "\n")
                      (accept-process-output proc)
                      (subseq str 0 (position ?\n str :from-end t))))))
      (org-babel-result-cond (cdr (assq :result-params params))
        result
        (condition-case nil
            (read (org-babel-snd-scheme-vector-to-list result))
          (error result))))
    (org-babel-pick-name (cdr (assq :colname-names params))
			 (cdr (assq :colnames params)))
    (org-babel-pick-name (cdr (assq :rowname-names params))
			 (cdr (assq :rownames params)))))

(defun org-babel-snd-scheme-vector-to-list (results)
  (replace-regexp-in-string "#\\(i\\|r\\)?(" "(" results))

(provide 'ob-snd-scheme)
-------------- next part --------------
#+name: tab
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |

Start the inferior Snd-Scheme process if necessary:

[[elisp:run-snd-scheme]]

#+name: zoo
#+begin_src snd-scheme :var input=tab n=7
  (map (lambda (x) (list (* n (car x)))) input)
#+end_src

#+RESULTS: zoo
|  70 |
| 140 |
| 210 |
| 280 |
| 350 |

#+name: seq-1
#+begin_src shell :var n=10
  seq $n
#+end_src

#+call: zoo(seq-1,3)

#+RESULTS:
|  3 |
|  6 |
|  9 |
| 12 |
| 15 |
| 18 |
| 21 |
| 24 |
| 27 |
| 30 |

The output is not correct if list length is 40 but ~print-length~ is 32:

#+call: zoo(seq-1(40))

#+RESULTS:
| (7) | (14) | (21) | (28) | (35) | (42) | (49) | (56) | (63) | (70) | (77) | (84) | (91) | (98) | (105) | (112) | (119) | (126) | (133) | (140) | (147) | (154) | (161) | (168) | (175) | (182) | (189) | (196) | (203) | (210) | (217) | (224) | ... |


A possible fix is ~(set! (print-length) 100)~ in Snd.

#+begin_src snd-scheme
  (set! (print-length) 100)
  (new-sound "new.snd" :size 96 :srate 48000)
  (clm-channel (make-oscil 880))
  (map list (channel->float-vector))
#+end_src

#+RESULTS:
|                   0.0 |
|    0.1149371504928666 |
|   0.22835087011065572 |
|   0.33873792024529137 |
|   0.44463517918492745 |
|     0.544639035015027 |
|    0.6374239897486896 |
|     0.721760228098362 |
|    0.7965299180241961 |
|    0.8607420270039434 |
|    0.9135454576426008 |
|    0.9542403285162767 |
|    0.9822872507286886 |
|    0.9973144772244581 |
|    0.9991228300988584 |
|    0.9876883405951379 |
|    0.9631625667976583 |
|    0.9258705848099951 |
|    0.8763066800438639 |
|    0.8151277957285546 |
|    0.7431448254773948 |
|    0.6613118653236525 |
|    0.5707135676844325 |
|    0.4725507648690549 |
|     0.368124552684679 |
|   0.25881904510252185 |
|   0.14608302856241281 |
|   0.03141075907812957 |
|  -0.08367784333231415 |
|  -0.19765734037912477 |
|    -0.309016994374946 |
|  -0.41628079226039977 |
|   -0.5180270093731288 |
|   -0.6129070536529752 |
|   -0.6996633405133642 |
|     -0.77714596145697 |
|   -0.8443279255020143 |
|   -0.9003187714021929 |
|   -0.9443763702374806 |
|   -0.9759167619387471 |
|   -0.9945218953682732 |
|   -0.9999451693655121 |
|   -0.9921147013144781 |
|   -0.9711342799096365 |
|   -0.9372819894918921 |
|   -0.8910065241883688 |
|   -0.8329212407101007 |
|   -0.7637960286346436 |
|   -0.6845471059286903 |
|   -0.5962248749656176 |
|    -0.500000000000002 |
|   -0.3971478906347828 |
|   -0.2890317969444739 |
|  -0.17708474031958574 |
|  -0.06279051952931593 |
|   0.05233595624294122 |
|   0.16676874671609962 |
|    0.2789911060392266 |
|    0.3875155864521004 |
|    0.4909037536151383 |
|    0.5877852522924708 |
|    0.6768759696826585 |
|    0.7569950556517544 |
|      0.82708057427456 |
|    0.8862035792312132 |
|    0.9335804264972006 |
|    0.9685831611286303 |
|    0.9907478404714432 |
|    0.9997806834748454 |
|    0.9955619646030803 |
|    0.9781476007338062 |
|    0.9477684100095866 |
|    0.9048270524660207 |
|    0.8498926929868654 |
|    0.7836934573258416 |
|    0.7071067811865497 |
|    0.6211477802783127 |
|    0.5269557954966803 |
|   0.42577929156507555 |
|   0.31895930929807303 |
|    0.2079116908177626 |
|   0.09410831331851768 |
| -0.020942419883353512 |
|   -0.1357155724343009 |
|  -0.24868988716485133 |
|   -0.3583679495452969 |
|  -0.46329603511985845 |
|   -0.5620833778521275 |
|   -0.6534206039901025 |
|   -0.7360970871197317 |
|   -0.8090169943749451 |
|   -0.8712138111201875 |
|    -0.921863151588499 |
|    -0.960293685676942 |
|   -0.9859960370705042 |
|   -0.9986295347545736 |


More information about the Cmdist mailing list