NixOS / nix-mode

An Emacs major mode for editing Nix expressions.
GNU Lesser General Public License v2.1
293 stars 74 forks source link

Send input to nix repl? #185

Open alejandrogallo opened 1 year ago

alejandrogallo commented 1 year ago

Maybe I'm just missing something, but I could not find a functionality like this for instacne

(defun ale/nix-send-buffer-to-repl (&optional arg)
    (interactive "P")
    (let ((contents (buffer-string)))
      (save-excursion
        (nix-repl-show)
        (when arg
          (insert (format "%s = "
                          (read-string "Variable to store expression: "))))
        (insert contents)
        (comint-send-input))))

is there something planned to send input and so on to the repl?

idlip commented 1 year ago

Hey @alejandrogallo I was also thinking why wasn't there interactive interpreting feature for nix-mode. Just now, I went through python and ess way of evaluating lines.

Came up with an function:

(defun nix-eval-line ()
  "Evaluates the line at point in nix-repl."
  (interactive)
  (let* ((start (line-beginning-position))
         (end (line-end-position))
         (msg (format "%s" (buffer-substring start end))))
    (pop-to-buffer "*Nix-REPL*")
    (insert msg)
    (comint-send-input)
    (other-window 1)))

With little more work, we can bring similar function like, nix-eval-buffer, nix-eval-region (I thing, line and region should be made as dwin)

Note: It does not start nix-repl, instead uses existing one. Let's see if author wants something like this.

idlip commented 1 year ago

Made a working nix-eval-dwim variant:

(defun nix-eval-dwim ()
  (interactive)
  (let* ((start (line-beginning-position))
         (end (line-end-position))
     (region-string (buffer-substring (region-beginning) (region-end)))
         (msg (format "%s" (if (use-region-p) region-string (buffer-substring start end)))))
    (pop-to-buffer "*Nix-REPL*")
    (insert msg)
    (comint-send-input)
    (other-window 1)))