From f737bcd671457c3967907692da494f2e23b3a029 Mon Sep 17 00:00:00 2001 From: TatriX Date: Tue, 26 Jan 2021 09:31:52 +0100 Subject: [PATCH] Add `sonic-pi-send-dwim` 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. --- sonic-pi-mode.el | 4 ++-- sonic-pi-osc.el | 35 ++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/sonic-pi-mode.el b/sonic-pi-mode.el index ab4a68e..725f234 100644 --- a/sonic-pi-mode.el +++ b/sonic-pi-mode.el @@ -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 diff --git a/sonic-pi-osc.el b/sonic-pi-osc.el index a54a770..260422f 100644 --- a/sonic-pi-osc.el +++ b/sonic-pi-osc.el @@ -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