diff --git a/bqn-mode.el b/bqn-mode.el index 0ab702f..a2df96b 100644 --- a/bqn-mode.el +++ b/bqn-mode.el @@ -266,6 +266,7 @@ BQN buffers (or recreate them)." (when bqn-glyph-map-modifier (set-keymap-parent bqn-mode-map (make-composed-keymap prog-mode-map bqn--glyph-map))) + (keymap-set bqn-mode-map "C-c C-z" #'bqn-comint-bring) (when bqn-use-input-method (activate-input-method "BQN-Z")) (setq-local syntax-propertize-function bqn--syntax-propertize) @@ -304,11 +305,28 @@ BQN buffers (or recreate them)." :type 'boolean :group 'bqn) +(defun bqn--comint-prefix () + "The prefix for BQNs comint buffers." + (concat "*" bqn-comint--process-name "-")) + +(defconst bqn--comint-suffix "*" + "The suffix for BQNs comint buffers.") + +(defun bqn--comint-buffer-name () + "Return the name of the comint buffer associated to the current buffer. +Note that the comint buffer may not exist yet, use `bqn-comint-buffer' +to create it." + (let* ((pref (bqn--comint-prefix)) + (buf (or (buffer-file-name) (buffer-name)))) + (if (string-prefix-p pref buf) + buf + (concat pref buf bqn--comint-suffix)))) + ;;;###autoload (defun bqn-comint-buffer () "Run an inferior BQN process inside Emacs and return its buffer." (interactive) - (let ((buf-name (concat "*" bqn-comint--process-name "*"))) + (let ((buf-name (bqn--comint-buffer-name))) ;; same buffer name as auto-created when passing nil below (if-let ((buf (get-buffer buf-name))) (if (comint-check-proc buf) @@ -432,6 +450,17 @@ bqn-comint-process-session and echoes the result." (interactive) (bqn-comint-eval-region (point-min) (point-max))) +(defun bqn-comint-bring () + "Toggle between the comint buffer and its associated buffer." + (interactive) + (let* ((comint (bqn-comint-buffer)) + (buf (thread-last + (buffer-name comint) + (string-remove-prefix (bqn--comint-prefix)) + (string-remove-suffix bqn--comint-suffix) + get-file-buffer))) + (pop-to-buffer (if (equal (current-buffer) comint) buf comint)))) + (define-derived-mode bqn-comint-mode comint-mode "BQN interactive" "Major mode for inferior BQN processes." :syntax-table bqn--syntax-table @@ -439,6 +468,7 @@ bqn-comint-process-session and echoes the result." (when bqn-glyph-map-modifier (set-keymap-parent bqn-comint-mode-map (make-composed-keymap comint-mode-map bqn--glyph-map))) + (keymap-set bqn-comint-mode-map "C-c C-z" #'bqn-comint-bring) (when bqn-use-input-method (activate-input-method "BQN-Z")) (setq-local syntax-propertize-function bqn--syntax-propertize)