jorgenschaefer / elpy

Emacs Python Development Environment
GNU General Public License v3.0
1.9k stars 261 forks source link

elpy-shell-send-statement-and-step changes focus to the python buffer #1687

Open steve-the-bayesian opened 5 years ago

steve-the-bayesian commented 5 years ago

Summary

When I try to step through a python script, I often enter a state where C-Return switches focus to the python buffer. This obviously makes it hard to do the stepping, because I need to switch back to the script after every statement.

Steps to reproduce

1) Open a python script (.py file). 2) C-c C-z to start a python process. 3) Repeatedly press C-Return to step through the python script. 4) Kill the python process in the python buffer, but leave the buffer open 5) From the python script buffer, press C-c C-z again to relaunch python. 6) Press C-Return (or directly enter the command elpy-shell-send-statement-and-step)

Focus changes to the python buffer, making it hard to step through the program. I only see this behavior when restarting python in a previous python buffer. I have gotten into the habit of killing the buffer and starting over from scratch, but that obliterates the command history, which would be useful to keep around.

My configuration

OS

MacOS Mojave 10.14.6

Result of (elpy-config)

Elpy Configuration

Virtualenv........: None
RPC Python........: 3.7.4 (/usr/local/bin/python3)
Interactive Python: python3 (/usr/local/bin/python3)
Emacs.............: 26.2
Elpy..............: 1.29.1
Jedi..............: 0.15.1
Rope..............: 0.14.0
Autopep8..........: 1.4.4
Yapf..............: 0.28.0
Black.............: 19.3b0
Syntax checker....: flake8 (/usr/local/bin/flake8)

You have not activated a virtual env. While Elpy supports this, it is often a
good idea to work inside a virtual env. You can use M-x pyvenv-activate or M-x
pyvenv-workon to activate a virtual env.

The directory ~/.local/bin/ is not in your PATH. As there is no active
virtualenv, installing Python packages locally will place executables in that
directory, so Emacs won't find them. If you are missing some commands, do add
this directory to your PATH -- and then do `elpy-rpc-restart'.

Options

`Raised' text indicates buttons; type RET or click mouse-1 on a button to invoke
its action.  Invoke [+] to expand a group, and [-] to collapse an expanded
group.  Invoke the [Group], [Face], and [Option] buttons below to edit that item
in another window.

[+]-- Group Elpy
[+]-- Group Python
[+]-- Group Virtual Environments (Pyvenv)
[+]-- Group Completion (Company)
[+]-- Group Call Signatures (ElDoc)
[+]-- Group Inline Errors (Flymake)
[+]-- Group Snippets (YASnippet)
[+]-- Group Directory Grep (rgrep)
[+]-- Group Search as You Type (ido)
[+]-- Group Django extension
[+]-- Group Autodoc extension

Elpy configuration in my init.el

;;=-=-=-=-=-=-=-=-=-= Python =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(setq python-shell-interpreter "python3")

(require 'package)
(setq package-archives
      '(("GNU ELPA"     . "https://elpa.gnu.org/packages/")
        ("MELPA Stable" . "https://stable.melpa.org/packages/")
        ("MELPA"        . "https://melpa.org/packages/"))
      package-archive-priorities
      '(("MELPA Stable" . 3)
        ("GNU ELPA"     . 2)
        ("MELPA"        . 1)))

;; (add-to-list 'package-archives
;;              '("melpa-stable" . "https://stable.melpa.org/packages/"))
;; (add-to-list 'package-pinned-packages '(ess . "melpa-stable") t)
(package-initialize)
(elpy-enable)
(setq elpy-rpc-backend "rope")

;; Set the fill column to 79 rather than 80 when editing python files to comply
;; with pep8.
(add-hook 'python-mode-hook
          (lambda()
            (set-fill-column 79)))

(require 'ein)
galaunay commented 5 years ago

That is indeed an unexpected behavior.

I didn't manage to reproduce the issue using exit() to kill the python process. Is it the way you are killing it ?

Could you also try without shell echoing ((setq elpy-shell-echo-input nil elpy-shell-echo-output nil)), just to check if it could be coming from there ?

steve-the-bayesian commented 5 years ago

I kill the process by pressing Ctrl-D.

If I follow the procedure listed in the bug report, with shell echoing turned off, then focus stays in the script. Removing shell echoing seems to have solved the problem. Thanks!

On Wed, Oct 9, 2019 at 2:40 AM galaunay notifications@github.com wrote:

That is indeed an unexpected behavior.

I didn't manage to reproduce the issue using exit() to kill the python process. Is it the way you are killing it ?

Could you also try without shell echoing ((setq elpy-shell-echo-input nil elpy-shell-echo-output nil)), just to check if it could be coming from there ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jorgenschaefer/elpy/issues/1687?email_source=notifications&email_token=ABMVDVKFWKQ4S2YLHGQ4F5TQNWRHXA5CNFSM4I6VQXG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAXJLSA#issuecomment-539923912, or mute the thread https://github.com/notifications/unsubscribe-auth/ABMVDVP243WHHQ6OFOBFRUDQNWRHXANCNFSM4I6VQXGQ .

galaunay commented 5 years ago

Glad to know it works.

It is still an unexpected behavior, would you accept to give me some more information, so I can try to fix the problem for good ?

Notably, It would be nice to know which of the two options (elpy-shell-echo-input or elpy-shell-echo-output) is creating the problem.

Could you also try the same procedure you listed earlier, but running (setq elpy-shell--captured-output nil) just after killing the shell with Ctrl-D ?

steve-the-bayesian commented 5 years ago

Interestingly, I'm having trouble getting BACK to a state where the problem surfaces. I have lived with this behavior for over a year and am glad to be rid of it. If I'm able to recreate the setting where I experience this issue I will run the requested experiment. Whether or not that actually happens thanks for your help.

On Thu, Oct 10, 2019 at 5:14 AM galaunay notifications@github.com wrote:

Glad to know it works.

It is still an unexpected behavior, would you accept to give me some more information, so I can try to fix the problem for good ?

Notably, It would be nice to know which of the two options ( elpy-shell-echo-input or elpy-shell-echo-output) is creating the problem.

Could you also try the same procedure you listed earlier, but running (setq elpy-shell--captured-output nil) just after killing the shell with Ctrl-D ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jorgenschaefer/elpy/issues/1687?email_source=notifications&email_token=ABMVDVOWIGIG3I4ESIMVUWLQN4MEHA5CNFSM4I6VQXG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEA4AWZY#issuecomment-540543847, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMVDVJE32GCXSRWD72PZX3QN4MEHANCNFSM4I6VQXGQ .

steve-the-bayesian commented 5 years ago

It happened again.

While experiencing the undesired switching behavior, I executed the command (setq elpy-shell--captured-output nil)

I'm still getting sent to the python buffer each time I press C-Return to execute a python command. Executing (setq elpy-shell-echo-input nil elpy-shell-echo-output nil) does not get me out of it, so maybe my previous message was a false diagnosis.

I tried running several versions of the first command, in case your message had a typo: (setq elpy-shell--captured-output nil) (setq elpy-shell-captured-output nil) ;; No double -- (setq elpy-shell-capture-output nil)

On Thu, Oct 10, 2019 at 8:20 AM Steven Scott steve.the.bayesian@gmail.com wrote:

Interestingly, I'm having trouble getting BACK to a state where the problem surfaces. I have lived with this behavior for over a year and am glad to be rid of it. If I'm able to recreate the setting where I experience this issue I will run the requested experiment. Whether or not that actually happens thanks for your help.

On Thu, Oct 10, 2019 at 5:14 AM galaunay notifications@github.com wrote:

Glad to know it works.

It is still an unexpected behavior, would you accept to give me some more information, so I can try to fix the problem for good ?

Notably, It would be nice to know which of the two options ( elpy-shell-echo-input or elpy-shell-echo-output) is creating the problem.

Could you also try the same procedure you listed earlier, but running (setq elpy-shell--captured-output nil) just after killing the shell with Ctrl-D ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jorgenschaefer/elpy/issues/1687?email_source=notifications&email_token=ABMVDVOWIGIG3I4ESIMVUWLQN4MEHA5CNFSM4I6VQXG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEA4AWZY#issuecomment-540543847, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMVDVJE32GCXSRWD72PZX3QN4MEHANCNFSM4I6VQXGQ .

galaunay commented 4 years ago

I reviewed the function bound to C-RET but I fail to understand what happens.

Could you try running those statements in place of hitting C-RET:

ps: as a temporary workaround, this may work:

(define-key elpy-mode-map (kbd "<C-return>")
  (lambda ()
    (interactive)
    (save-excursion (elpy-shell-send-statement-and-step))))