forked from tidalcycles/Tidal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidal.el
471 lines (425 loc) · 14.7 KB
/
tidal.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
;; tidal.el - (c) [email protected], 20012, based heavily on...
;; hsc3.el - (c) rohan drape, 2006-2008
;; notes from hsc3:
;; This mode is implemented as a derivation of `haskell' mode,
;; indentation and font locking is courtesy that mode. The
;; inter-process communication is courtesy `comint'. The symbol at
;; point acquisition is courtesy `thingatpt'. The directory search
;; facilities are courtesy `find-lisp'.
(require 'scheme)
(require 'comint)
(require 'thingatpt)
(require 'find-lisp)
(require 'pulse)
(defvar tidal-buffer
"*tidal*"
"*The name of the tidal process buffer (default=*tidal*).")
(defvar tidal-interpreter
"ghci"
"*The haskell interpeter to use (default=ghci).")
(defvar tidal-interpreter-arguments
(list "-XOverloadedStrings"
)
"*Arguments to the haskell interpreter (default=none).")
(defvar tidal-literate-p
t
"*Flag to indicate if we are in literate mode (default=t).")
(make-variable-buffer-local 'tidal-literate-p)
(defun tidal-unlit (s)
"Remove bird literate marks"
(replace-regexp-in-string "^> " "" s))
(defun tidal-intersperse (e l)
(if (null l)
'()
(cons e (cons (car l) (tidal-intersperse e (cdr l))))))
(defun tidal-start-haskell ()
"Start haskell."
(interactive)
(if (comint-check-proc tidal-buffer)
(error "A tidal process is already running")
(apply
'make-comint
"tidal"
tidal-interpreter
nil
tidal-interpreter-arguments)
(tidal-see-output))
(tidal-send-string ":set prompt \"\"")
(tidal-send-string ":module Sound.Tidal.Context")
; (tidal-send-string "import Volca.Beat")
; (tidal-send-string "import Volca.Key")
; (tidal-send-string "(note, [portamento, expression, voice, octave, detune, vcoegint, kcutoff, vcfegint, lforate, lfopitchint, lfocutoffint, attack, decay, sustain, dtime , dfeedback], keystop) <- keystart")
; (tidal-send-string "(drum, [clapSpeed, clavesSpeed, agogoSpeed, crashSpeed, stutterTime, stutterDepth, tomDecay, clhatDecay, ophatDecay, hatGrain], beatstop) <- startbeat")
(tidal-send-string "d1 <- dirtStream")
(tidal-send-string "d2 <- dirtStream")
(tidal-send-string "d3 <- dirtStream")
(tidal-send-string "d4 <- dirtStream")
(tidal-send-string "d5 <- dirtStream")
(tidal-send-string "d6 <- dirtStream")
(tidal-send-string "d7 <- dirtStream")
(tidal-send-string "d8 <- dirtStream")
(tidal-send-string "d9 <- dirtStream")
(tidal-send-string "d10 <- dirtStream")
(tidal-send-string "(cps, getNow) <- bpsUtils")
(tidal-send-string "let bps x = cps (x/2)")
(tidal-send-string "let hush = mapM_ ($ silence) [d1,d2,d3,d4,d5,d6,d7,d8,d9,d10]")
(tidal-send-string "let solo = (>>) hush")
(tidal-send-string ":set prompt \"tidal> \"")
)
(defun tidal-see-output ()
"Show haskell output."
(interactive)
(when (comint-check-proc tidal-buffer)
(delete-other-windows)
(split-window-vertically)
(with-current-buffer tidal-buffer
(let ((window (display-buffer (current-buffer))))
(goto-char (point-max))
(save-selected-window
(set-window-point window (point-max)))))))
(defun tidal-quit-haskell ()
"Quit haskell."
(interactive)
(kill-buffer tidal-buffer)
(delete-other-windows))
(defun tidal-help ()
"Lookup up the name at point in the Help files."
(interactive)
(mapc (lambda (filename)
(find-file-other-window filename))
(find-lisp-find-files tidal-help-directory
(concat "^"
(thing-at-point 'symbol)
"\\.help\\.lhs"))))
(defun chunk-string (n s)
"Split a string into chunks of 'n' characters."
(let* ((l (length s))
(m (min l n))
(c (substring s 0 m)))
(if (<= l n)
(list c)
(cons c (chunk-string n (substring s n))))))
(defun tidal-send-string (s)
(if (comint-check-proc tidal-buffer)
(let ((cs (chunk-string 64 (concat s "\n"))))
(mapcar (lambda (c) (comint-send-string tidal-buffer c)) cs))
(error "no tidal process running?")))
(defun tidal-transform-and-store (f s)
"Transform example text into compilable form."
(with-temp-file f
(mapc (lambda (module)
(insert (concat module "\n")))
tidal-modules)
(insert "main = do\n")
(insert (if tidal-literate-p (tidal-unlit s) s))))
(defun tidal-get-now ()
"Store the current cycle position in a variable called 'now'."
(interactive)
(tidal-send-string "now' <- getNow")
(tidal-send-string "let now = nextSam now'")
(tidal-send-string "let retrig = (now ~>)")
(tidal-send-string "let fadeOut n = spread' (degradeBy) (retrig $ slow n $ envL)")
(tidal-send-string "let fadeIn n = spread' (degradeBy) (retrig $ slow n $ (1-) <$> envL)")
)
(defun tidal-run-line ()
"Send the current line to the interpreter."
(interactive)
(tidal-get-now)
(let* ((s (buffer-substring (line-beginning-position)
(line-end-position)))
(s* (if tidal-literate-p
(tidal-unlit s)
s)))
(tidal-send-string s*))
(pulse-momentary-highlight-one-line (point))
(next-line)
)
(defun tidal-run-multiple-lines ()
"Send the current region to the interpreter as a single line."
(interactive)
(tidal-get-now)
(save-excursion
(mark-paragraph)
(let* ((s (buffer-substring-no-properties (region-beginning)
(region-end)))
(s* (if tidal-literate-p
(tidal-unlit s)
s)))
(tidal-send-string ":{")
(tidal-send-string s*)
(tidal-send-string ":}")
(mark-paragraph)
(pulse-momentary-highlight-region (mark) (point))
)
;(tidal-send-string (replace-regexp-in-string "\n" " " s*))
)
)
(defun tidal-run-d1 ()
"Send the first instance of d1 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d1" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-run-d2 ()
"Send the d2 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d2" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-run-d3 ()
"Send the d3 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d3" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-run-d4 ()
"Send the d4 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d4" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-run-d5 ()
"Send the d5 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d5" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-run-d6 ()
"Send the d6 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d6" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-run-d7 ()
"Send the d7 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d7" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-run-d8 ()
"Send the d9 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d8" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-run-d9 ()
"Send the d9 to the interpreter as a single line."
(interactive)
(goto-char 0)
(search-forward "d9" nil nil 1)
(tidal-run-multiple-lines)
)
(defun tidal-stop-d1 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d1]")
(tidal-send-string ":}")
)
(defun tidal-stop-d2 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d2]")
(tidal-send-string ":}")
)
(defun tidal-stop-d3 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d3]")
(tidal-send-string ":}")
)
(defun tidal-stop-d4 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d4]")
(tidal-send-string ":}")
)
(defun tidal-stop-d5 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d5]")
(tidal-send-string ":}")
)
(defun tidal-stop-d6 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d6]")
(tidal-send-string ":}")
)
(defun tidal-stop-d7 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d7]")
(tidal-send-string ":}")
)
(defun tidal-stop-d8 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d8]")
(tidal-send-string ":}")
)
(defun tidal-stop-d9 ()
"send d1 $ silence as a single line"
(interactive)
(tidal-send-string ":{")
(tidal-send-string " mapM_ ($ silence) [d9]")
(tidal-send-string ":}")
)
(defun tidal-run-region ()
"Place the region in a do block and compile."
(interactive)
(tidal-transform-and-store
"/tmp/tidal.hs"
(buffer-substring-no-properties (region-beginning) (region-end)))
(tidal-send-string ":load \"/tmp/tidal.hs\"")
(tidal-send-string "main"))
(defun tidal-load-buffer ()
"Load the current buffer."
(interactive)
(save-buffer)
(tidal-send-string (format ":load \"%s\"" buffer-file-name)))
(defun tidal-run-main ()
"Run current main."
(interactive)
(tidal-send-string "main"))
(defun tidal-interrupt-haskell ()
(interactive)
(if (comint-check-proc tidal-buffer)
(with-current-buffer tidal-buffer
(interrupt-process (get-buffer-process (current-buffer))))
(error "no tidal process running?")))
(defvar tidal-mode-map nil
"Tidal keymap.")
(defun tidal-mode-keybindings (map)
"Haskell Tidal keybindings."
(define-key map [?\C-c ?\C-s] 'tidal-start-haskell)
(define-key map [?\C-c ?\C-v] 'tidal-see-output)
(define-key map [?\C-c ?\C-q] 'tidal-quit-haskell)
(define-key map [?\C-c ?\C-c] 'tidal-run-line)
(define-key map [?\C-c ?\C-e] 'tidal-run-multiple-lines)
(define-key map (kbd "<C-return>") 'tidal-run-multiple-lines)
(define-key map [?\C-c ?\C-r] 'tidal-run-region)
(define-key map [?\C-c ?\C-l] 'tidal-load-buffer)
(define-key map [?\C-c ?\C-i] 'tidal-interrupt-haskell)
(define-key map [?\C-c ?\C-m] 'tidal-run-main)
(define-key map [?\C-c ?\C-h] 'tidal-help)
(define-key map [?\C-c ?\C-1] 'tidal-run-d1)
(define-key map [?\C-c ?\C-2] 'tidal-run-d2)
(define-key map [?\C-c ?\C-3] 'tidal-run-d3)
(define-key map [?\C-c ?\C-4] 'tidal-run-d4)
(define-key map [?\C-c ?\C-5] 'tidal-run-d5)
(define-key map [?\C-c ?\C-6] 'tidal-run-d6)
(define-key map [?\C-c ?\C-7] 'tidal-run-d7)
(define-key map [?\C-c ?\C-8] 'tidal-run-d8)
(define-key map [?\C-c ?\C-9] 'tidal-run-d9)
(define-key map [?\C-v ?\C-1] 'tidal-stop-d1)
(define-key map [?\C-v ?\C-2] 'tidal-stop-d2)
(define-key map [?\C-v ?\C-3] 'tidal-stop-d3)
(define-key map [?\C-v ?\C-4] 'tidal-stop-d4)
(define-key map [?\C-v ?\C-5] 'tidal-stop-d5)
(define-key map [?\C-v ?\C-6] 'tidal-stop-d6)
(define-key map [?\C-v ?\C-7] 'tidal-stop-d7)
(define-key map [?\C-v ?\C-8] 'tidal-stop-d8)
(define-key map [?\C-v ?\C-9] 'tidal-stop-d9))
(defun turn-on-tidal-keybindings ()
"Haskell Tidal keybindings in the local map."
(local-set-key [?\C-c ?\C-s] 'tidal-start-haskell)
(local-set-key [?\C-c ?\C-v] 'tidal-see-output)
(local-set-key [?\C-c ?\C-q] 'tidal-quit-haskell)
(local-set-key [?\C-c ?\C-c] 'tidal-run-line)
(local-set-key [?\C-c ?\C-e] 'tidal-run-multiple-lines)
(local-set-key (kbd "<C-return>") 'tidal-run-multiple-lines)
(local-set-key [?\C-c ?\C-r] 'tidal-run-region)
(local-set-key [?\C-c ?\C-l] 'tidal-load-buffer)
(local-set-key [?\C-c ?\C-i] 'tidal-interrupt-haskell)
(local-set-key [?\C-c ?\C-m] 'tidal-run-main)
(local-set-key [?\C-c ?\C-h] 'tidal-help)
(local-set-key [?\C-c ?\C-1] 'tidal-run-d1)
(local-set-key [?\C-c ?\C-2] 'tidal-run-d2)
(local-set-key [?\C-c ?\C-3] 'tidal-run-d3)
(local-set-key [?\C-c ?\C-4] 'tidal-run-d4)
(local-set-key [?\C-c ?\C-5] 'tidal-run-d5)
(local-set-key [?\C-c ?\C-6] 'tidal-run-d6)
(local-set-key [?\C-c ?\C-7] 'tidal-run-d7)
(local-set-key [?\C-c ?\C-8] 'tidal-run-d8)
(local-set-key [?\C-c ?\C-9] 'tidal-run-d9)
(local-set-key [?\C-v ?\C-1] 'tidal-stop-d1)
(local-set-key [?\C-v ?\C-2] 'tidal-stop-d2)
(local-set-key [?\C-v ?\C-3] 'tidal-stop-d3)
(local-set-key [?\C-v ?\C-4] 'tidal-stop-d4)
(local-set-key [?\C-v ?\C-5] 'tidal-stop-d5)
(local-set-key [?\C-v ?\C-6] 'tidal-stop-d6)
(local-set-key [?\C-v ?\C-7] 'tidal-stop-d7)
(local-set-key [?\C-v ?\C-8] 'tidal-stop-d8)
(local-set-key [?\C-v ?\C-9] 'tidal-stop-d9))
(defun tidal-mode-menu (map)
"Haskell Tidal menu."
(define-key map [menu-bar tidal]
(cons "Haskell-Tidal" (make-sparse-keymap "Haskell-Tidal")))
(define-key map [menu-bar tidal help]
(cons "Help" (make-sparse-keymap "Help")))
(define-key map [menu-bar tidal expression]
(cons "Expression" (make-sparse-keymap "Expression")))
(define-key map [menu-bar tidal expression load-buffer]
'("Load buffer" . tidal-load-buffer))
(define-key map [menu-bar tidal expression run-main]
'("Run main" . tidal-run-main))
(define-key map [menu-bar tidal expression run-region]
'("Run region" . tidal-run-region))
(define-key map [menu-bar tidal expression run-multiple-lines]
'("Run multiple lines" . tidal-run-multiple-lines))
(define-key map [menu-bar tidal expression run-line]
'("Run line" . tidal-run-line))
(define-key map [menu-bar tidal haskell]
(cons "Haskell" (make-sparse-keymap "Haskell")))
(define-key map [menu-bar tidal haskell quit-haskell]
'("Quit haskell" . tidal-quit-haskell))
(define-key map [menu-bar tidal haskell see-output]
'("See output" . tidal-see-output))
(define-key map [menu-bar tidal haskell start-haskell]
'("Start haskell" . tidal-start-haskell)))
(if tidal-mode-map
()
(let ((map (make-sparse-keymap "Haskell-Tidal")))
(tidal-mode-keybindings map)
(tidal-mode-menu map)
(setq tidal-mode-map map)))
(define-derived-mode
literate-tidal-mode
tidal-mode
"Literate Haskell Tidal"
"Major mode for interacting with an inferior haskell process."
(set (make-local-variable 'paragraph-start) "\f\\|[ \t]*$")
(set (make-local-variable 'paragraph-separate) "[ \t\f]*$")
(setq tidal-literate-p t)
(setq haskell-literate 'bird)
(turn-on-font-lock))
(add-to-list 'auto-mode-alist '("\\.ltidal$" . literate-tidal-mode))
;(add-to-list 'load-path "/usr/share/emacs/site-lisp/haskell-mode/") ;required by olig1905 on linux
;(require 'haskell-mode) ;required by olig1905 on linux
(define-derived-mode
tidal-mode
haskell-mode
"Haskell Tidal"
"Major mode for interacting with an inferior haskell process."
(set (make-local-variable 'paragraph-start) "\f\\|[ \t]*$")
(set (make-local-variable 'paragraph-separate) "[ \t\f]*$")
(setq tidal-literate-p nil)
(turn-on-font-lock))
(add-to-list 'auto-mode-alist '("\\.tidal$" . tidal-mode))
(provide 'tidal)