summaryrefslogtreecommitdiff
path: root/lisp/progmodes
diff options
context:
space:
mode:
authorkobarity <kobarity@gmail.com>2022-12-22 23:08:40 +0900
committerEli Zaretskii <eliz@gnu.org>2022-12-31 10:14:28 +0200
commiteee2aeca251a6fb3db09cfeeb3ae3aaf48db02fc (patch)
tree3643b7b2de10f7ce10e298c65cdc90dbe6bc7e6d /lisp/progmodes
parentbfdad6c4e5ce95025f1761a404b762589d126505 (diff)
downloademacs-eee2aeca251a6fb3db09cfeeb3ae3aaf48db02fc.tar.gz
emacs-eee2aeca251a6fb3db09cfeeb3ae3aaf48db02fc.tar.bz2
emacs-eee2aeca251a6fb3db09cfeeb3ae3aaf48db02fc.zip
Fix python-shell-buffer-substring when retrieving a single statement
* lisp/progmodes/python.el (python-shell-buffer-substring): Do not add "if True:" line when retrieving a single statement. (python-shell-send-region): Add a reference to `python-shell-buffer-substring' in docstring. * test/lisp/progmodes/python-tests.el (python-shell-buffer-substring-13) (python-shell-buffer-substring-14, python-shell-buffer-substring-15) (python-shell-buffer-substring-16, python-shell-buffer-substring-17): New tests. (Bug#60142)
Diffstat (limited to 'lisp/progmodes')
-rw-r--r--lisp/progmodes/python.el45
1 files changed, 31 insertions, 14 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 07f86d31551..641b4ccff23 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3736,19 +3736,35 @@ the python shell:
appending extra empty lines so tracebacks are correct.
3. When the region sent is a substring of the current buffer, a
coding cookie is added.
- 4. Wraps indented regions under an \"if True:\" block so the
- interpreter evaluates them correctly."
- (let* ((start (save-excursion
- ;; If we're at the start of the expression, and
- ;; there's just blank space ahead of it, then expand
- ;; the region to include the start of the line.
- ;; This makes things work better with the rest of
- ;; the data we're sending over.
+ 4. When the region consists of a single statement, leading
+ whitespaces will be removed. Otherwise, wraps indented
+ regions under an \"if True:\" block so the interpreter
+ evaluates them correctly."
+ (let* ((single-p (save-restriction
+ (narrow-to-region start end)
+ (= (progn
+ (goto-char start)
+ (python-nav-beginning-of-statement))
+ (progn
+ (goto-char end)
+ (python-nav-beginning-of-statement)))))
+ (start (save-excursion
+ ;; If we're at the start of the expression, and if
+ ;; the region consists of a single statement, then
+ ;; remove leading whitespaces, else if there's just
+ ;; blank space ahead of it, then expand the region
+ ;; to include the start of the line. This makes
+ ;; things work better with the rest of the data
+ ;; we're sending over.
(goto-char start)
- (if (string-blank-p
- (buffer-substring (line-beginning-position) start))
- (line-beginning-position)
- start)))
+ (if single-p
+ (progn
+ (skip-chars-forward "[:space:]" end)
+ (point))
+ (if (string-blank-p
+ (buffer-substring (line-beginning-position) start))
+ (line-beginning-position)
+ start))))
(substring (buffer-substring-no-properties start end))
(starts-at-point-min-p (save-restriction
(widen)
@@ -3772,7 +3788,7 @@ the python shell:
(python-mode)
(when fillstr
(insert fillstr))
- (when (not toplevel-p)
+ (when (and (not single-p) (not toplevel-p))
(forward-line -1)
(insert "if True:\n")
(delete-region (point) (line-end-position)))
@@ -3816,7 +3832,8 @@ code inside blocks delimited by \"if __name__== \\='__main__\\=':\".
When called interactively SEND-MAIN defaults to nil, unless it's
called with prefix argument. When optional argument MSG is
non-nil, forces display of a user-friendly message if there's no
-process running; defaults to t when called interactively."
+process running; defaults to t when called interactively. The
+substring to be sent is retrieved using `python-shell-buffer-substring'."
(interactive
(list (region-beginning) (region-end) current-prefix-arg t))
(let* ((string (python-shell-buffer-substring start end (not send-main)