Elpy: Help to get working: elpy-shell-send-word

Created on 6 Dec 2017  路  7Comments  路  Source: jorgenschaefer/elpy

I have spend a lot of time trying to modify the elpy-shell-send-statement-and-step function into a function that sends the word at cursor to the REPL.

I tried to add the above function as follows, but to no avail:

(defun elpy-shell-send-word-and-step ()
  "Send current word at cursor"
  (interactive)
  (elpy-shell--ensure-shell-running)
  (when (not elpy-shell-echo-input) (elpy-shell--append-to-shell-output "\n"))
  (let ((beg (progn (evil-backward-word-begin)
                    (save-excursion
                      (beginning-of-line)
                      (point))))
        (end (progn (evil-a-word) (point))))
    (unless (eq beg end)
      (elpy-shell--flash-and-message-region beg end)
        (elpy-shell--with-maybe-echo
         (python-shell-send-string (elpy-shell--region-without-indentation beg end)))))
  (python-nav-forward-statement))

Somehow the word isn't correctly picked up and I am clearly making a noob mistake. Can someone please tell how to easily send a word at the cursor to the REPL without first selecting it?

Question

All 7 comments

You probably want (beg (progn (backward-word) (point)))), i.e., don't go to the beginning of the line.

If you don't need the flashing, you could just use

(elpy-shell--with-maybe-echo
   (python-shell-send-string (word-at-point)))
(forward-word)

This is untested, though.

Be also aware that elpy-shell--flash-and-message-region displays a message with the first line of the region.
So even if beg and end delimit only a part of the line, the echo area will always say that it sent the whole current line.

@jobvisser03 Is this issue resolved?

@rgemulla For the bigger part it's solved. Using:

(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"))
  (let ((beg (progn (evil-backward-word-begin) (point)))
        (end (progn (evil-a-word) (point))))
    (elpy-shell--with-maybe-echo
     (python-shell-send-string (elpy-shell--region-without-indentation beg end))))
  )

I used evil-... statements to include variables with underscores. It works perfectly when my cursor is in the middle of the word. Now the problems that remain are:

  • When cursor is at the beginning of the word evil-backward-word-begin will jump to the previous word. (not intended)
  • When a selection is made, I want to send that region instead of the word.

Any idea on how to solve this?

Why not replace (elpy-shell--region-without-indentation beg end) with (thing-at-point 'symbol)? The beg and end variables are then not needed.

Works like a charm, thanks!

(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"))
  (python-shell-send-string (thing-at-point 'symbol))
  )

Any suggestion on: When a selection is made, I want to send that region instead of the word?

Weird. This works for me:

(elpy-shell--with-maybe-echo 
  (python-shell-send-string (thing-at-point 'symbol)))

I suggest to run M-x toggle-debug-on-error to see where this error actually occurs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yitang picture yitang  路  4Comments

kapilsh picture kapilsh  路  3Comments

chechoRP picture chechoRP  路  5Comments

mfcabrera picture mfcabrera  路  6Comments

SirSharpest picture SirSharpest  路  6Comments