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-use-overlay #79

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
66 changes: 49 additions & 17 deletions bqn-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

;; Author: Marshall Lochbaum <mwlochbaum@gmail.com>
;; Version: 0.1.0
;; Package-Requires: ((emacs "26.1") (compat "30.0.0.0"))
;; Package-Requires: ((emacs "26.1") (compat "30.0.0.0") (eros "0.1.0"))
;; URL: https://github.com/museoa/bqn-mode
;; SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -305,6 +305,15 @@ BQN buffers (or recreate them)."
:type 'boolean
:group 'bqn)

(defcustom bqn-comint-use-overlay nil
"Should BQN use overlays for all `bqn-comint-eval-*' style functions?
If this is nil, print the result in the minibuffer instead."
:type 'boolean
:group 'bqn
:set (lambda (sym val)
(when val (require 'eros))
(set-default-toplevel-value sym val)))

(defun bqn--comint-prefix ()
"The prefix for BQNs comint buffers."
(concat "*" bqn-comint--process-name "-"))
Expand Down Expand Up @@ -423,32 +432,55 @@ one if one doesn't already exist."
(let ((proc (get-buffer-process (bqn-comint-buffer))))
(bqn--comint-call-process-silently proc command)))

(defun bqn-comint-eval-region (start end)
(defun bqn-comint-eval-region (start end &optional arg)
"Evaluate the region bounded by START and END with the
bqn-comint-process-session and echoes the result."
(interactive "r")
bqn-comint-process-session and echoes the result. If ARG is non-nil or
when called with a prefix \\[universal-argument], insert the result in
the current buffer instead."
(interactive "rP")
(when (= start end)
(error "Attempt to evaluate empty region to %s" bqn-comint--process-name))
(when (and bqn-comint-flash-on-send (pulse-available-p))
(pulse-momentary-highlight-region start end))
(let ((region (buffer-substring-no-properties start end))
(process (get-buffer-process (bqn-comint-buffer))))
(message "%s" (bqn--comint-call-process-silently process region))))

(defun bqn-comint-eval-dwim ()
"Evaluate the active region or the current line, displaying the result."
(interactive)
(let* ((region (buffer-substring-no-properties start end))
(process (get-buffer-process (bqn-comint-buffer)))
(response (bqn--comint-call-process-silently process region))
(r-lines (string-lines response))
(single-line? (= 1 (length r-lines))))
(cond
(arg ; Insert in buffer
(save-excursion
(goto-char end)
(if single-line?
(insert " # ⇒ " response)
(dolist (l r-lines)
(insert "\n# " l)))))
(bqn-comint-use-overlay ; Use overlay
(eros--make-result-overlay response
:where end
:duration eros-eval-result-duration
:format (if single-line? " ⇒ %s" "%s")))
(t ; Insert in minibuffer
(message "%s" response)))))

(defun bqn-comint-eval-dwim (&optional arg)
"Evaluate the active region or the current line, displaying the result.
If ARG is non-nil or when called with a prefix \\[universal-argument],
insert the result in the current buffer instead."
(interactive "P")
(cond
((use-region-p)
(bqn-comint-eval-region (region-beginning) (region-end))
(bqn-comint-eval-region (region-beginning) (region-end) arg)
(deactivate-mark))
(t
(bqn-comint-eval-region (line-beginning-position) (line-end-position)))))
(bqn-comint-eval-region (line-beginning-position) (line-end-position) arg))))

(defun bqn-comint-eval-buffer ()
"Evaluate the current buffer contents, displaying the result."
(interactive)
(bqn-comint-eval-region (point-min) (point-max)))
(defun bqn-comint-eval-buffer (&optional arg)
"Evaluate the current buffer contents, displaying the result.
If ARG is non-nil or when called with a prefix \\[universal-argument],
insert the result in the current buffer instead."
(interactive "P")
(bqn-comint-eval-region (point-min) (point-max) arg))

(defun bqn-comint-bring ()
"Toggle between the comint buffer and its associated buffer."
Expand Down
Loading