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 bqn-comint-bring #80

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 31 additions & 1 deletion bqn-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split the keymap modifications because I think it is a little bit controversial to bind keys "by default".

https://github.com/positron-solutions/user-keys?tab=readme-ov-file#please-package-authors

Copy link
Contributor Author

@slotThe slotThe Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought a little bit about this, but since keybindings of the form C-c C-«letter» are reserved for major modes I figured it was fine. I think that it helps with discoverability, personally, and unbinding a keybinding from a keymap is not all that difficult. Though I don't feel too strongly either way and am fine with dropping this part if you'd prefer

EDIT: I now see that the link you posted talks about this in some length in another place. As I said, no strong opinions from me

(when bqn-use-input-method
(activate-input-method "BQN-Z"))
(setq-local syntax-propertize-function bqn--syntax-propertize)
Expand Down Expand Up @@ -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))))
Comment on lines +319 to +323
Copy link
Contributor Author

@slotThe slotThe Sep 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One could also use some internal state to keep track of comint buffer–buffer associations, but I figured since this is the only state that's currently needed this would be a bit overkill


;;;###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)
Expand Down Expand Up @@ -432,13 +450,25 @@ 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
:group 'bqn
(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)
Expand Down
Loading