Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sonic-pi-send-dwim #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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