diff options
Diffstat (limited to 'lisp/progmodes/python.el')
| -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: | |||
| 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 |