0

I use isend-mode (see here) to send (bash) code to an ansi-term as follows:

1) `M-x ansi-term` # open ansi-term
2) Open buffer with code I want to send to *ansi-term*
3) `M-x isend RET *ansi-term* RET` # => associates code buffer with `*ansi-term*`

Then you can step through code with C-RET and see the output in ansi-term. There are two things that are tedious:

  1. On M-x isend RET, the current buffer name is suggested as output where to send the code to, but I never do that. I would like to have *ansi-term* as default there. This should be rather easy given the definition of isend-associate (see the above link, line 262), but I'm not an Elisp programmer (is it simply (setq isend--command-buffer *ansi-term*)?)

  2. Ideally, M-x ansi-term in Step 1) would not be necessary. It would be great if calling M-x isend RET *ansi-term* would automatically open a new ansi-term (unless there's already one open in which case it should go to that) and associate it with the code buffer.

  3. Out of interest, is there any other method that can send code from a buffer (.sh file) to an ansi-term (or even 'real' terminal provided by the OS) so that you can step through the code one line at a time or send regions? I didn't find any other ones but isend-mode.

  • Step 3 should be M-x isend-associate RET *ansi-term* RET if I understand correctly? – npostavs May 15 at 23:24
  • M-x isend is an alias for that, so also works – Marius Hofert May 15 at 23:25
1

This command should do steps 1 and 3, run it in the buffer you open in step 2 (bind to a key, or rename to something shorter, as you like).

(defun isend-with-ansi-term ()
  (interactive)
  (let ((term-buf (get-buffer "*ansi-term*")))
    (unless term-buf
      (display-buffer
       (save-window-excursion ; Work around term.el's use of `switch-to-buffer'.
         (save-current-buffer
           (setq term-buf (ansi-term (or explicit-shell-file-name
                                         (getenv "ESHELL")
                                         shell-file-name)))))))
    (isend-associate (buffer-name term-buf))))

(defun isend-dissociate-on-kill (buffer-name)
  (let ((buf (get-buffer buffer-name))
        (orig-buffer (current-buffer)))
    (with-current-buffer buf
      ;; Disable `isend-mode' once the associated buffer is killed.
      (add-hook 'kill-buffer-hook
                ;; Backquote and comma could be dropped if using
                ;; lexical-binding.
                `(lambda ()
                   (with-current-buffer ,orig-buffer
                     (isend-mode -1)))
                nil t))))
(advice-add 'isend-associate :after #'isend-dissociate-on-kill)

Do you know how to bind M-x isend-with-ansi-term to C-RET so that on the first C-RET it executes the function but subsequent C-RET work in isend-mode and send the code to ansi-term?

Just (define-key global-map [C-return] 'isend-with-ansi-term). When isend-mode is turned on it will override that binding because minor mode bindings take precedence over global bindings. (PS RET is C-m so C-RET doesn't work, you need [C-return] or (kbd "<C-return>") in this case).


is it simply (setq isend--command-buffer *ansi-term*)?

Generally, the -- in the name is a signal from the author that this variable might be renamed or somehow change its meaning in the future, so you shouldn't touch it. See (elisp) Coding Conventions.

  • Thanks, npostavs. On M-x isend-with-ansi-term, the current buffer containing the code disappears and you just see the ansi-term, so you can't execute the code without changing the buffer again. Is there a way to open the ansi-term in a buffer you'd get when doing C-x 4 c? – Marius Hofert May 16 at 0:07
  • @MariusHofert I think display-buffer+save-window-excursion should do the trick, see edit. – npostavs May 16 at 1:39
  • Thanks a lot, works great! If you don't mind, I'd suggest to the author of isend-mode to incorporate this function. – Marius Hofert May 16 at 9:45
  • Do you know how to bind M-x isend-with-ansi-term to C-RET so that on the first C-RET it executes the function but subsequent C-RET work in isend-mode and send the code to ansi-term? That would be great (I can also ask a new question here). – Marius Hofert May 16 at 9:53
  • @MariusHofert Sure, see edit. – npostavs May 16 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.