eval-in-repl icon indicating copy to clipboard operation
eval-in-repl copied to clipboard

send word at cursor to REPL

Open jobvisser03 opened this issue 7 years ago • 3 comments

I want to improve my Python workflow in the following way:

  • Send word at cursor to REPL
  • Send word.info() from word (variable) at cursor to REPL
  • Send type(word) from word (variable) at cursor to REPL

I do the following with Elpy but since I use spacemacs I'd rather not use it since it is big.

(defun jv-python/elpy-shell-send-word ()
  "Send word at cursor."
  (interactive)
  (elpy-shell--ensure-shell-running)
  (when (not elpy-shell-echo-input) (elpy-shell--append-to-shell-output "\n"))
    (elpy-shell--with-maybe-echo
  (python-shell-send-string (thing-at-point 'symbol)))
  )

Can someone please help me with the points above using spacemacs + eir?

jobvisser03 avatar Jan 28 '18 19:01 jobvisser03

You could use (thing-at-point 'word) but I think symbol is better fit.

These are the three things you are looking for:

(require 'eval-in-repl-python)
(eir-python-shell-send-string (thing-at-point 'symbol))
(eir-python-shell-send-string (format "%s.info()" (thing-at-point 'symbol)))
(eir-python-shell-send-string (format "type(%s)" (thing-at-point 'symbol)))

terlar avatar Jan 28 '18 20:01 terlar

@terlar Thank you!!!

Last question: How can I replicate the functionality of elpy's send-current-line-or-statement? eval-in-repl can send a line, but when I call it from within a loop it gives and indent error. (the line is indented)

Is there a way to realise this behaviour?

jobvisser03 avatar Jan 29 '18 08:01 jobvisser03

You could do something like this, it will strip all leading space from the line if that is what you are looking for. The current line eval is made to be line by line, and therefore saves the indentation.

(eir-python-shell-send-string
  (replace-regexp-in-string
    "\\`[ \t\n]*"
    ""
    (buffer-substring-no-properties (line-beginning-position) (line-end-position))))

terlar avatar Jan 29 '18 21:01 terlar