From 5190ea6259a5fd13ba5e87b92b20f450658cf532 Mon Sep 17 00:00:00 2001 From: kobarity Date: Tue, 14 Feb 2023 00:30:15 +0900 Subject: Fix point moving when calling python-shell-send-region * lisp/progmodes/python.el (python-shell-buffer-substring): Add `save-excursion' to prevent the point from moving. * test/lisp/progmodes/python-tests.el (python-tests-should-not-move): New helper function to assert that point does not move while calling a function. (python-shell-buffer-substring-*): Use `python-tests-should-not-move'. (Bug#61463) --- lisp/progmodes/python.el | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'lisp/progmodes/python.el') diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index df0d1c96965..0d714c31e9e 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -3759,14 +3759,15 @@ the python shell: 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))))) + (let* ((single-p (save-excursion + (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 @@ -3785,10 +3786,11 @@ the python shell: (line-beginning-position) start)))) (substring (buffer-substring-no-properties start end)) - (starts-at-first-line-p (save-restriction - (widen) - (goto-char start) - (= (line-number-at-pos) 1))) + (starts-at-first-line-p (save-excursion + (save-restriction + (widen) + (goto-char start) + (= (line-number-at-pos) 1)))) (encoding (python-info-encoding)) (toplevel-p (zerop (save-excursion (goto-char start) -- cgit v1.2.3