diff options
| author | Fabián Ezequiel Gallina | 2015-08-23 19:55:54 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2015-08-23 19:56:47 -0300 |
| commit | af013e0d4a76f0a2fd4a0e76912e8e49ae86ec2e (patch) | |
| tree | f8da1577054fc099af4606ec0b38d2d72e0dcd29 | |
| parent | 41cb0162c5bcf440dca36afcd493db585e8c4901 (diff) | |
| download | emacs-af013e0d4a76f0a2fd4a0e76912e8e49ae86ec2e.tar.gz 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.
| -rw-r--r-- | lisp/progmodes/python.el | 23 | ||||
| -rw-r--r-- | test/automated/python-tests.el | 55 |
2 files changed, 68 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: | |||
| 2986 | coding cookie is added. | 2986 | coding cookie is added. |
| 2987 | 4. Wraps indented regions under an \"if True:\" block so the | 2987 | 4. Wraps indented regions under an \"if True:\" block so the |
| 2988 | interpreter evaluates them correctly." | 2988 | interpreter evaluates them correctly." |
| 2989 | (let* ((substring (buffer-substring-no-properties start end)) | 2989 | (let* ((start (save-excursion |
| 2990 | ;; Normalize start to the line beginning position. | ||
| 2991 | (goto-char start) | ||
| 2992 | (line-beginning-position))) | ||
| 2993 | (substring (buffer-substring-no-properties start end)) | ||
| 2990 | (starts-at-point-min-p (save-restriction | 2994 | (starts-at-point-min-p (save-restriction |
| 2991 | (widen) | 2995 | (widen) |
| 2992 | (= (point-min) start))) | 2996 | (= (point-min) start))) |
| 2993 | (encoding (python-info-encoding)) | 2997 | (encoding (python-info-encoding)) |
| 2998 | (toplevel-p (zerop (save-excursion | ||
| 2999 | (goto-char start) | ||
| 3000 | (python-util-forward-comment 1) | ||
| 3001 | (current-indentation)))) | ||
| 2994 | (fillstr (when (not starts-at-point-min-p) | 3002 | (fillstr (when (not starts-at-point-min-p) |
| 2995 | (concat | 3003 | (concat |
| 2996 | (format "# -*- coding: %s -*-\n" encoding) | 3004 | (format "# -*- coding: %s -*-\n" encoding) |
| 2997 | (make-string | 3005 | (make-string |
| 2998 | ;; Subtract 2 because of the coding cookie. | 3006 | ;; Subtract 2 because of the coding cookie. |
| 2999 | (- (line-number-at-pos start) 2) ?\n)))) | 3007 | (- (line-number-at-pos start) 2) ?\n))))) |
| 3000 | (toplevel-block-p (save-excursion | ||
| 3001 | (goto-char start) | ||
| 3002 | (or (zerop (line-number-at-pos start)) | ||
| 3003 | (progn | ||
| 3004 | (python-util-forward-comment 1) | ||
| 3005 | (zerop (current-indentation))))))) | ||
| 3006 | (with-temp-buffer | 3008 | (with-temp-buffer |
| 3007 | (python-mode) | 3009 | (python-mode) |
| 3008 | (if fillstr (insert fillstr)) | 3010 | (when fillstr |
| 3011 | (insert fillstr)) | ||
| 3009 | (insert substring) | 3012 | (insert substring) |
| 3010 | (goto-char (point-min)) | 3013 | (goto-char (point-min)) |
| 3011 | (when (not toplevel-block-p) | 3014 | (when (not toplevel-p) |
| 3012 | (insert "if True:") | 3015 | (insert "if True:") |
| 3013 | (delete-region (point) (line-end-position))) | 3016 | (delete-region (point) (line-end-position))) |
| 3014 | (when nomain | 3017 | (when nomain |
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el index e792b0f1a1a..30b1b480a25 100644 --- a/test/automated/python-tests.el +++ b/test/automated/python-tests.el | |||
| @@ -3276,6 +3276,61 @@ class Foo(models.Model): | |||
| 3276 | 3276 | ||
| 3277 | ")))) | 3277 | ")))) |
| 3278 | 3278 | ||
| 3279 | (ert-deftest python-shell-buffer-substring-10 () | ||
| 3280 | "Check substring from partial block." | ||
| 3281 | (python-tests-with-temp-buffer | ||
| 3282 | " | ||
| 3283 | def foo(): | ||
| 3284 | print ('a') | ||
| 3285 | " | ||
| 3286 | (should (string= (python-shell-buffer-substring | ||
| 3287 | (python-tests-look-at "print ('a')") | ||
| 3288 | (point-max)) | ||
| 3289 | "if True: | ||
| 3290 | |||
| 3291 | print ('a') | ||
| 3292 | ")))) | ||
| 3293 | |||
| 3294 | (ert-deftest python-shell-buffer-substring-11 () | ||
| 3295 | "Check substring from partial block and point within indentation." | ||
| 3296 | (python-tests-with-temp-buffer | ||
| 3297 | " | ||
| 3298 | def foo(): | ||
| 3299 | print ('a') | ||
| 3300 | " | ||
| 3301 | (should (string= (python-shell-buffer-substring | ||
| 3302 | (progn | ||
| 3303 | (python-tests-look-at "print ('a')") | ||
| 3304 | (backward-char 1) | ||
| 3305 | (point)) | ||
| 3306 | (point-max)) | ||
| 3307 | "if True: | ||
| 3308 | |||
| 3309 | print ('a') | ||
| 3310 | ")))) | ||
| 3311 | |||
| 3312 | (ert-deftest python-shell-buffer-substring-12 () | ||
| 3313 | "Check substring from partial block and point in whitespace." | ||
| 3314 | (python-tests-with-temp-buffer | ||
| 3315 | " | ||
| 3316 | def foo(): | ||
| 3317 | |||
| 3318 | # Whitespace | ||
| 3319 | |||
| 3320 | print ('a') | ||
| 3321 | " | ||
| 3322 | (should (string= (python-shell-buffer-substring | ||
| 3323 | (python-tests-look-at "# Whitespace") | ||
| 3324 | (point-max)) | ||
| 3325 | "if True: | ||
| 3326 | |||
| 3327 | |||
| 3328 | # Whitespace | ||
| 3329 | |||
| 3330 | print ('a') | ||
| 3331 | ")))) | ||
| 3332 | |||
| 3333 | |||
| 3279 | 3334 | ||
| 3280 | ;;; Shell completion | 3335 | ;;; Shell completion |
| 3281 | 3336 | ||