Skip to content

Commit

Permalink
Add sonic-pi-send-dwim
Browse files Browse the repository at this point in the history
Function `sonic-pi-send-dwim` sends in a do wait I mean style.

If region is active, send it.
Then if there is an enclosing `live_loop' or `with_fx' sent it.
Otherwise send current line.
  • Loading branch information
TatriX committed Jan 26, 2021
1 parent 3cf101b commit f737bcd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions sonic-pi-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
(define-key map (kbd "C-c C-k") 'sonic-pi-send-buffer)
(define-key map (kbd "C-c C-r") 'sonic-pi-send-region)
(define-key map (kbd "C-c C-q") 'sonic-pi-quit)
(define-key map (kbd "C-c C-b") 'sonic-pi-stop-all)
(define-key map (kbd "C-c C-c") 'sonic-pi-send-live-loop)
(define-key map (kbd "C-c C-s") 'sonic-pi-stop-all)
(define-key map (kbd "C-c C-c") 'sonic-pi-send-dwim)
map))

;;;###autoload
Expand Down
35 changes: 28 additions & 7 deletions sonic-pi-osc.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,37 @@
(hlt-highlight-regexp-region nil nil ".+" 'eval-sonic-pi-flash nil)
(run-at-time flash-time nil 'hlt-unhighlight-region))

(defun sonic-pi--send (region)
"Helper function to send and highlighting region."
(cl-destructuring-bind (start end) region
(sonic-pi-osc-send-text start end)
(hlt-highlight-regexp-region start end ".+" 'eval-sonic-pi-flash nil))
(run-at-time flash-time nil 'hlt-unhighlight-region nil nil nil))

(defun sonic-pi-send-live-loop ()
"send a live-loop to sonic via osc"
(interactive)
(save-excursion
(let ((s (re-search-backward "^\\(live_loop\\|with_fx\\)")))
(ruby-end-of-block)
(end-of-line)
(sonic-pi-osc-send-text s (point))
(hlt-highlight-regexp-region s (point) ".+" 'eval-sonic-pi-flash nil))
(run-at-time flash-time nil 'hlt-unhighlight-region nil nil nil)))
(sonic-pi--send (sonic-pi--live-loop-region)))

(defun sonic-pi-send-dwim ()
"Send in a do wait I mean style.
If region is active, send it.
Then if there is an enclosing `live_loop' or `with_fx' sent it.
Otherwise send current line."
(interactive)
(sonic-pi--send (sonic-pi--dwim-region)))

(defun sonic-pi--dwim-region ()
"Find region for dwim command."
(if (region-active-p)
(list (region-beginning) (region-end))
(or (sonic-pi--live-loop-region)
(list (line-beginning-position) (line-end-position)))))

(defun sonic-pi--live-loop-region ()
"Find region with `live_loop' or `with_fx'."
(when-let (start (save-excursion (re-search-backward "^\\(live_loop\\|with_fx\\)" nil t)))
(list start (save-excursion (ruby-end-of-block) (line-end-position)))))

(defun sonic-pi-osc-make-client (host port)
(make-network-process
Expand Down

0 comments on commit f737bcd

Please sign in to comment.