diff options
author | Fabián Ezequiel Gallina <fgallina@gnu.org> | 2015-08-23 19:55:54 -0300 |
---|---|---|
committer | Fabián Ezequiel Gallina <fgallina@gnu.org> | 2015-08-23 19:56:47 -0300 |
commit | af013e0d4a76f0a2fd4a0e76912e8e49ae86ec2e (patch) | |
tree | f8da1577054fc099af4606ec0b38d2d72e0dcd29 /lisp | |
parent | 41cb0162c5bcf440dca36afcd493db585e8c4901 (diff) | |
download | emacs-af013e0d4a76f0a2fd4a0e76912e8e49ae86ec2e.tar.gz emacs-af013e0d4a76f0a2fd4a0e76912e8e49ae86ec2e.tar.bz2 emacs-af013e0d4a76f0a2fd4a0e76912e8e49ae86ec2e.zip |
python.el: Fix python-shell-buffer-substring on indented code
Fixes: debbugs:21086
* lisp/progmodes/python.el (python-shell-buffer-substring):
Respect current line indentation when calculating string.
* test/automated/python-tests.el
(python-shell-buffer-substring-10)
(python-shell-buffer-substring-11)
(python-shell-buffer-substring-12): New tests.
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/progmodes/python.el | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index fbe5b8b0743..abae8aff47b 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -2986,29 +2986,32 @@ the python shell: coding cookie is added. 4. Wraps indented regions under an \"if True:\" block so the interpreter evaluates them correctly." - (let* ((substring (buffer-substring-no-properties start end)) + (let* ((start (save-excursion + ;; Normalize start to the line beginning position. + (goto-char start) + (line-beginning-position))) + (substring (buffer-substring-no-properties start end)) (starts-at-point-min-p (save-restriction (widen) (= (point-min) start))) (encoding (python-info-encoding)) + (toplevel-p (zerop (save-excursion + (goto-char start) + (python-util-forward-comment 1) + (current-indentation)))) (fillstr (when (not starts-at-point-min-p) (concat (format "# -*- coding: %s -*-\n" encoding) (make-string ;; Subtract 2 because of the coding cookie. - (- (line-number-at-pos start) 2) ?\n)))) - (toplevel-block-p (save-excursion - (goto-char start) - (or (zerop (line-number-at-pos start)) - (progn - (python-util-forward-comment 1) - (zerop (current-indentation))))))) + (- (line-number-at-pos start) 2) ?\n))))) (with-temp-buffer (python-mode) - (if fillstr (insert fillstr)) + (when fillstr + (insert fillstr)) (insert substring) (goto-char (point-min)) - (when (not toplevel-block-p) + (when (not toplevel-p) (insert "if True:") (delete-region (point) (line-end-position))) (when nomain |