Using python.el
and ipython.exe
(from Miniconda3), I'd like to highlight a portion of code and execute it in the associated inferior python buffer. Example:
#!/usr/bin/python3
myvar = 100+200
^^^^^^^ (this is my selected region)
If I use C-c C-c
, it correctly sources the file. That's not what I want.
If I highlight the region as depicted above and use C-c C-r
(python-shell-send-region
), the entire line is executed, not just the region. That is, myvar
should not be defined by this step. I'd like to send just the highlight region.
So I created an edited version of python-shell-buffer-substring
, omitting the part that redefines start
:
(let* ((start (save-excursion
;; Normalize start to the line beginning position.
(goto-char start)
(line-beginning-position)))
I also create my-python-shell-send-region
, where the only difference is it calls my-python-shell-buffer-substring
. This properly creates the substring
# -*- coding: utf-8 -*-
100+200
and sends it correctly to the inferior process. Except ... the output is inhibited (generally a good thing), and I'd like to see the output 300
. I see the function python-shell-send-string-no-output
, but I don't see a way to allow output when using *-send-string
.
Q: how to allow the output, and/or is there a better mechanism for doing this?
(In the unrelated package ess
, I use C-u C-c C-r
for show-no-output, and C-c C-r
when I want the output shown. I can deal with a reversal of this logic, that's not the issue for this question.)
(emacs-25.2.1 (x86_64-w64-mingw32)
and ein-20171128.516
)
EDIT
After some more digging (thanks wvxvw), I found that removing the inserted # -*- coding: utf-8 -*-
line from the pasted string allowed the inferior process to print the output, but only on single-line regions. For instance:
#!/usr/bin/python3
myvar = 100+200
200+300
Highlighting just 100+200
produced the output 300
, good. Highlighting from 100
through 300
on the next line, however, produced no output. If the highlighted string is on a single line and contains semicolon-delimited commands (e.g., 100+200;200+300
), then the output from the last command is shown, nothing else (which is, I believe, correct behavior for python.) So the problem remains with multi-line regions.