diff options
| author | Christoph Wedler | 2015-06-19 13:38:24 +0000 |
|---|---|---|
| committer | Christoph Wedler | 2015-07-05 08:52:03 +0000 |
| commit | 044d4cc0c0debd4648ec637d63bcd72d4253b1cb (patch) | |
| tree | 5141ad9cfb3ab68795a80b4b87e0829d427226ed /lisp/progmodes/python.el | |
| parent | 4fff58dd1ec11543e2b0195f10cefc1746748499 (diff) | |
| download | emacs-044d4cc0c0debd4648ec637d63bcd72d4253b1cb.tar.gz emacs-044d4cc0c0debd4648ec637d63bcd72d4253b1cb.zip | |
Respect `prog-indentation-context' in python.el
* lisp/progmodes/python.el (python-indent-guess-indent-offset)
(python-indent-context, python-indent--calculate-indentation)
(python-info-current-defun)
(python-info-dedenter-opening-block-message)
(python-info-line-ends-backslash-p)
(python-info-beginning-of-backslash)
(python-info-continuation-line-p): Use `prog-widen'.
(python-indent--calculate-indentation)
(python-indent--calculate-levels)
(python-indent-calculate-indentation): Use `prog-first-column'.
(python-indent--calculate-levels): Simplify.
Ignore also initial empty lines for syntax calculation.
* lisp/progmodes/python.el (python-indent-context): Return
:no-indent for first non-empty line, not just in line 1.
* test/automated/python-tests.el (python-indent-base-case)
(python-indent-inside-paren-1, python-indent-inside-paren-2)
(python-indent-inside-paren-3, python-indent-inside-paren-4)
(python-indent-inside-paren-5, python-indent-inside-paren-6)
(python-indent-after-backslash-1)
(python-indent-after-backslash-2)
(python-indent-after-backslash-3)
(python-indent-after-backslash-4, python-indent-inside-string-1):
Expect :no-indent for first non-empty line.
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 60 |
1 files changed, 28 insertions, 32 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index d45d082c40a..f641880428c 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -734,7 +734,7 @@ work on `python-indent-calculate-indentation' instead." | |||
| 734 | (interactive) | 734 | (interactive) |
| 735 | (save-excursion | 735 | (save-excursion |
| 736 | (save-restriction | 736 | (save-restriction |
| 737 | (widen) | 737 | (prog-widen) |
| 738 | (goto-char (point-min)) | 738 | (goto-char (point-min)) |
| 739 | (let ((block-end)) | 739 | (let ((block-end)) |
| 740 | (while (and (not block-end) | 740 | (while (and (not block-end) |
| @@ -833,7 +833,7 @@ keyword | |||
| 833 | - Point is on a line starting a dedenter block. | 833 | - Point is on a line starting a dedenter block. |
| 834 | - START is the position where the dedenter block starts." | 834 | - START is the position where the dedenter block starts." |
| 835 | (save-restriction | 835 | (save-restriction |
| 836 | (widen) | 836 | (prog-widen) |
| 837 | (let ((ppss (save-excursion | 837 | (let ((ppss (save-excursion |
| 838 | (beginning-of-line) | 838 | (beginning-of-line) |
| 839 | (syntax-ppss)))) | 839 | (syntax-ppss)))) |
| @@ -958,18 +958,20 @@ keyword | |||
| 958 | ((save-excursion | 958 | ((save-excursion |
| 959 | (back-to-indentation) | 959 | (back-to-indentation) |
| 960 | (skip-chars-backward " \t\n") | 960 | (skip-chars-backward " \t\n") |
| 961 | (python-nav-beginning-of-statement) | 961 | (if (bobp) |
| 962 | (cons | 962 | (cons :no-indent 0) |
| 963 | (cond ((python-info-current-line-comment-p) | 963 | (python-nav-beginning-of-statement) |
| 964 | :after-comment) | 964 | (cons |
| 965 | ((save-excursion | 965 | (cond ((python-info-current-line-comment-p) |
| 966 | (goto-char (line-end-position)) | 966 | :after-comment) |
| 967 | (python-util-forward-comment -1) | 967 | ((save-excursion |
| 968 | (python-nav-beginning-of-statement) | 968 | (goto-char (line-end-position)) |
| 969 | (looking-at (python-rx block-ender))) | 969 | (python-util-forward-comment -1) |
| 970 | :after-block-end) | 970 | (python-nav-beginning-of-statement) |
| 971 | (t :after-line)) | 971 | (looking-at (python-rx block-ender))) |
| 972 | (point)))))))) | 972 | :after-block-end) |
| 973 | (t :after-line)) | ||
| 974 | (point))))))))) | ||
| 973 | 975 | ||
| 974 | (defun python-indent--calculate-indentation () | 976 | (defun python-indent--calculate-indentation () |
| 975 | "Internal implementation of `python-indent-calculate-indentation'. | 977 | "Internal implementation of `python-indent-calculate-indentation'. |
| @@ -978,10 +980,10 @@ current context or a list of integers. The latter case is only | |||
| 978 | happening for :at-dedenter-block-start context since the | 980 | happening for :at-dedenter-block-start context since the |
| 979 | possibilities can be narrowed to specific indentation points." | 981 | possibilities can be narrowed to specific indentation points." |
| 980 | (save-restriction | 982 | (save-restriction |
| 981 | (widen) | 983 | (prog-widen) |
| 982 | (save-excursion | 984 | (save-excursion |
| 983 | (pcase (python-indent-context) | 985 | (pcase (python-indent-context) |
| 984 | (`(:no-indent . ,_) 0) | 986 | (`(:no-indent . ,_) (prog-first-column)) ; usually 0 |
| 985 | (`(,(or :after-line | 987 | (`(,(or :after-line |
| 986 | :after-comment | 988 | :after-comment |
| 987 | :inside-string | 989 | :inside-string |
| @@ -1019,7 +1021,7 @@ possibilities can be narrowed to specific indentation points." | |||
| 1019 | (let ((opening-block-start-points | 1021 | (let ((opening-block-start-points |
| 1020 | (python-info-dedenter-opening-block-positions))) | 1022 | (python-info-dedenter-opening-block-positions))) |
| 1021 | (if (not opening-block-start-points) | 1023 | (if (not opening-block-start-points) |
| 1022 | 0 ; if not found default to first column | 1024 | (prog-first-column) ; if not found default to first column |
| 1023 | (mapcar (lambda (pos) | 1025 | (mapcar (lambda (pos) |
| 1024 | (save-excursion | 1026 | (save-excursion |
| 1025 | (goto-char pos) | 1027 | (goto-char pos) |
| @@ -1037,15 +1039,9 @@ integers. Levels are returned in ascending order, and in the | |||
| 1037 | case INDENTATION is a list, this order is enforced." | 1039 | case INDENTATION is a list, this order is enforced." |
| 1038 | (if (listp indentation) | 1040 | (if (listp indentation) |
| 1039 | (sort (copy-sequence indentation) #'<) | 1041 | (sort (copy-sequence indentation) #'<) |
| 1040 | (let* ((remainder (% indentation python-indent-offset)) | 1042 | (nconc (number-sequence (prog-first-column) (1- indentation) |
| 1041 | (steps (/ (- indentation remainder) python-indent-offset)) | 1043 | python-indent-offset) |
| 1042 | (levels (mapcar (lambda (step) | 1044 | (list indentation)))) |
| 1043 | (* python-indent-offset step)) | ||
| 1044 | (number-sequence steps 0 -1)))) | ||
| 1045 | (reverse | ||
| 1046 | (if (not (zerop remainder)) | ||
| 1047 | (cons indentation levels) | ||
| 1048 | levels))))) | ||
| 1049 | 1045 | ||
| 1050 | (defun python-indent--previous-level (levels indentation) | 1046 | (defun python-indent--previous-level (levels indentation) |
| 1051 | "Return previous level from LEVELS relative to INDENTATION." | 1047 | "Return previous level from LEVELS relative to INDENTATION." |
| @@ -1068,7 +1064,7 @@ minimum." | |||
| 1068 | (python-indent--previous-level levels (current-indentation)) | 1064 | (python-indent--previous-level levels (current-indentation)) |
| 1069 | (if levels | 1065 | (if levels |
| 1070 | (apply #'max levels) | 1066 | (apply #'max levels) |
| 1071 | 0)))) | 1067 | (prog-first-column))))) |
| 1072 | 1068 | ||
| 1073 | (defun python-indent-line (&optional previous) | 1069 | (defun python-indent-line (&optional previous) |
| 1074 | "Internal implementation of `python-indent-line-function'. | 1070 | "Internal implementation of `python-indent-line-function'. |
| @@ -4230,7 +4226,7 @@ Optional argument INCLUDE-TYPE indicates to include the type of the defun. | |||
| 4230 | This function can be used as the value of `add-log-current-defun-function' | 4226 | This function can be used as the value of `add-log-current-defun-function' |
| 4231 | since it returns nil if point is not inside a defun." | 4227 | since it returns nil if point is not inside a defun." |
| 4232 | (save-restriction | 4228 | (save-restriction |
| 4233 | (widen) | 4229 | (prog-widen) |
| 4234 | (save-excursion | 4230 | (save-excursion |
| 4235 | (end-of-line 1) | 4231 | (end-of-line 1) |
| 4236 | (let ((names) | 4232 | (let ((names) |
| @@ -4413,7 +4409,7 @@ likely an invalid python file." | |||
| 4413 | (let ((point (python-info-dedenter-opening-block-position))) | 4409 | (let ((point (python-info-dedenter-opening-block-position))) |
| 4414 | (when point | 4410 | (when point |
| 4415 | (save-restriction | 4411 | (save-restriction |
| 4416 | (widen) | 4412 | (prog-widen) |
| 4417 | (message "Closes %s" (save-excursion | 4413 | (message "Closes %s" (save-excursion |
| 4418 | (goto-char point) | 4414 | (goto-char point) |
| 4419 | (buffer-substring | 4415 | (buffer-substring |
| @@ -4434,7 +4430,7 @@ statement." | |||
| 4434 | With optional argument LINE-NUMBER, check that line instead." | 4430 | With optional argument LINE-NUMBER, check that line instead." |
| 4435 | (save-excursion | 4431 | (save-excursion |
| 4436 | (save-restriction | 4432 | (save-restriction |
| 4437 | (widen) | 4433 | (prog-widen) |
| 4438 | (when line-number | 4434 | (when line-number |
| 4439 | (python-util-goto-line line-number)) | 4435 | (python-util-goto-line line-number)) |
| 4440 | (while (and (not (eobp)) | 4436 | (while (and (not (eobp)) |
| @@ -4450,7 +4446,7 @@ With optional argument LINE-NUMBER, check that line instead." | |||
| 4450 | Optional argument LINE-NUMBER forces the line number to check against." | 4446 | Optional argument LINE-NUMBER forces the line number to check against." |
| 4451 | (save-excursion | 4447 | (save-excursion |
| 4452 | (save-restriction | 4448 | (save-restriction |
| 4453 | (widen) | 4449 | (prog-widen) |
| 4454 | (when line-number | 4450 | (when line-number |
| 4455 | (python-util-goto-line line-number)) | 4451 | (python-util-goto-line line-number)) |
| 4456 | (when (python-info-line-ends-backslash-p) | 4452 | (when (python-info-line-ends-backslash-p) |
| @@ -4467,7 +4463,7 @@ When current line is continuation of another return the point | |||
| 4467 | where the continued line ends." | 4463 | where the continued line ends." |
| 4468 | (save-excursion | 4464 | (save-excursion |
| 4469 | (save-restriction | 4465 | (save-restriction |
| 4470 | (widen) | 4466 | (prog-widen) |
| 4471 | (let* ((context-type (progn | 4467 | (let* ((context-type (progn |
| 4472 | (back-to-indentation) | 4468 | (back-to-indentation) |
| 4473 | (python-syntax-context-type))) | 4469 | (python-syntax-context-type))) |