diff options
| author | Stefan Monnier | 2014-12-12 09:55:42 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2014-12-12 09:55:42 -0500 |
| commit | 074965f9355b7cc7f7a5f4385a19433e51c8afd2 (patch) | |
| tree | f8588795fae0a705242999476f276c305c010320 /lisp/progmodes/python.el | |
| parent | 88f3dbc46545d1f699dd3f2aba8156cf1cdaa500 (diff) | |
| download | emacs-074965f9355b7cc7f7a5f4385a19433e51c8afd2.tar.gz emacs-074965f9355b7cc7f7a5f4385a19433e51c8afd2.zip | |
* lisp/progmodes/python.el (python-indent-line): Use `noindent' in strings.
(python-indent-levels): Document extra value.
(python-indent-calculate-indentation): Return `noindent' in strings.
(python-indent-post-self-insert-function)
(python-indent-calculate-levels): Handle new value.
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 83 |
1 files changed, 45 insertions, 38 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 63597d5f75a..f1d8cff15ee 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -689,7 +689,8 @@ It makes underscores and dots word constituent chars.") | |||
| 689 | "Current indentation level `python-indent-line-function' is using.") | 689 | "Current indentation level `python-indent-line-function' is using.") |
| 690 | 690 | ||
| 691 | (defvar python-indent-levels '(0) | 691 | (defvar python-indent-levels '(0) |
| 692 | "Levels of indentation available for `python-indent-line-function'.") | 692 | "Levels of indentation available for `python-indent-line-function'. |
| 693 | Can also be `noindent' if automatic indentation can't be used.") | ||
| 693 | 694 | ||
| 694 | (defun python-indent-guess-indent-offset () | 695 | (defun python-indent-guess-indent-offset () |
| 695 | "Guess and set `python-indent-offset' for the current buffer." | 696 | "Guess and set `python-indent-offset' for the current buffer." |
| @@ -810,7 +811,9 @@ START is the buffer position where the sexp starts." | |||
| 810 | start)))) | 811 | start)))) |
| 811 | 812 | ||
| 812 | (defun python-indent-calculate-indentation () | 813 | (defun python-indent-calculate-indentation () |
| 813 | "Calculate correct indentation offset for the current line." | 814 | "Calculate correct indentation offset for the current line. |
| 815 | Returns `noindent' if the indentation does not depend on Python syntax, | ||
| 816 | such as in strings." | ||
| 814 | (let* ((indentation-context (python-indent-context)) | 817 | (let* ((indentation-context (python-indent-context)) |
| 815 | (context-status (car indentation-context)) | 818 | (context-status (car indentation-context)) |
| 816 | (context-start (cdr indentation-context))) | 819 | (context-start (cdr indentation-context))) |
| @@ -860,9 +863,7 @@ START is the buffer position where the sexp starts." | |||
| 860 | ;; When inside of a string, do nothing. just use the current | 863 | ;; When inside of a string, do nothing. just use the current |
| 861 | ;; indentation. XXX: perhaps it would be a good idea to | 864 | ;; indentation. XXX: perhaps it would be a good idea to |
| 862 | ;; invoke standard text indentation here | 865 | ;; invoke standard text indentation here |
| 863 | (`inside-string | 866 | (`inside-string 'noindent) |
| 864 | (goto-char context-start) | ||
| 865 | (current-indentation)) | ||
| 866 | ;; After backslash we have several possibilities. | 867 | ;; After backslash we have several possibilities. |
| 867 | (`after-backslash | 868 | (`after-backslash |
| 868 | (cond | 869 | (cond |
| @@ -988,14 +989,17 @@ START is the buffer position where the sexp starts." | |||
| 988 | ;; XXX: This asks for a refactor. Even if point is on a | 989 | ;; XXX: This asks for a refactor. Even if point is on a |
| 989 | ;; dedenter statement, it could be multiline and in that case | 990 | ;; dedenter statement, it could be multiline and in that case |
| 990 | ;; the continuation lines should be indented with normal rules. | 991 | ;; the continuation lines should be indented with normal rules. |
| 991 | (let* ((indentation (python-indent-calculate-indentation)) | 992 | (let* ((indentation (python-indent-calculate-indentation))) |
| 992 | (remainder (% indentation python-indent-offset)) | 993 | (if (not (numberp indentation)) |
| 993 | (steps (/ (- indentation remainder) python-indent-offset))) | 994 | (setq python-indent-levels indentation) |
| 994 | (setq python-indent-levels (list 0)) | 995 | (let* ((remainder (% indentation python-indent-offset)) |
| 995 | (dotimes (step steps) | 996 | (steps (/ (- indentation remainder) python-indent-offset))) |
| 996 | (push (* python-indent-offset (1+ step)) python-indent-levels)) | 997 | (setq python-indent-levels (list 0)) |
| 997 | (when (not (eq 0 remainder)) | 998 | (dotimes (step steps) |
| 998 | (push (+ (* python-indent-offset steps) remainder) python-indent-levels))) | 999 | (push (* python-indent-offset (1+ step)) python-indent-levels)) |
| 1000 | (when (not (eq 0 remainder)) | ||
| 1001 | (push (+ (* python-indent-offset steps) remainder) | ||
| 1002 | python-indent-levels))))) | ||
| 999 | (setq python-indent-levels | 1003 | (setq python-indent-levels |
| 1000 | (or | 1004 | (or |
| 1001 | (mapcar (lambda (pos) | 1005 | (mapcar (lambda (pos) |
| @@ -1004,8 +1008,9 @@ START is the buffer position where the sexp starts." | |||
| 1004 | (current-indentation))) | 1008 | (current-indentation))) |
| 1005 | (python-info-dedenter-opening-block-positions)) | 1009 | (python-info-dedenter-opening-block-positions)) |
| 1006 | (list 0)))) | 1010 | (list 0)))) |
| 1007 | (setq python-indent-current-level (1- (length python-indent-levels)) | 1011 | (when (listp python-indent-levels) |
| 1008 | python-indent-levels (nreverse python-indent-levels))) | 1012 | (setq python-indent-current-level (1- (length python-indent-levels)) |
| 1013 | python-indent-levels (nreverse python-indent-levels)))) | ||
| 1009 | 1014 | ||
| 1010 | (defun python-indent-toggle-levels () | 1015 | (defun python-indent-toggle-levels () |
| 1011 | "Toggle `python-indent-current-level' over `python-indent-levels'." | 1016 | "Toggle `python-indent-current-level' over `python-indent-levels'." |
| @@ -1033,28 +1038,30 @@ in the variable `python-indent-levels'. Afterwards it sets the | |||
| 1033 | variable `python-indent-current-level' correctly so offset is | 1038 | variable `python-indent-current-level' correctly so offset is |
| 1034 | equal to | 1039 | equal to |
| 1035 | (nth python-indent-current-level python-indent-levels)" | 1040 | (nth python-indent-current-level python-indent-levels)" |
| 1036 | (or | 1041 | (if (and (or (and (memq this-command python-indent-trigger-commands) |
| 1037 | (and (or (and (memq this-command python-indent-trigger-commands) | 1042 | (eq last-command this-command)) |
| 1038 | (eq last-command this-command)) | 1043 | force-toggle) |
| 1039 | force-toggle) | 1044 | (not (equal python-indent-levels '(0)))) |
| 1040 | (not (equal python-indent-levels '(0))) | 1045 | (if (listp python-indent-levels) |
| 1041 | (or (python-indent-toggle-levels) t)) | 1046 | (python-indent-toggle-levels)) |
| 1042 | (python-indent-calculate-levels)) | 1047 | (python-indent-calculate-levels)) |
| 1043 | (let* ((starting-pos (point-marker)) | 1048 | (if (eq python-indent-levels 'noindent) |
| 1044 | (indent-ending-position | 1049 | python-indent-levels |
| 1045 | (+ (line-beginning-position) (current-indentation))) | 1050 | (let* ((starting-pos (point-marker)) |
| 1046 | (follow-indentation-p | 1051 | (indent-ending-position |
| 1047 | (or (bolp) | 1052 | (+ (line-beginning-position) (current-indentation))) |
| 1048 | (and (<= (line-beginning-position) starting-pos) | 1053 | (follow-indentation-p |
| 1049 | (>= indent-ending-position starting-pos)))) | 1054 | (or (bolp) |
| 1050 | (next-indent (nth python-indent-current-level python-indent-levels))) | 1055 | (and (<= (line-beginning-position) starting-pos) |
| 1051 | (unless (= next-indent (current-indentation)) | 1056 | (>= indent-ending-position starting-pos)))) |
| 1052 | (beginning-of-line) | 1057 | (next-indent (nth python-indent-current-level python-indent-levels))) |
| 1053 | (delete-horizontal-space) | 1058 | (unless (= next-indent (current-indentation)) |
| 1054 | (indent-to next-indent) | 1059 | (beginning-of-line) |
| 1055 | (goto-char starting-pos)) | 1060 | (delete-horizontal-space) |
| 1056 | (and follow-indentation-p (back-to-indentation))) | 1061 | (indent-to next-indent) |
| 1057 | (python-info-dedenter-opening-block-message)) | 1062 | (goto-char starting-pos)) |
| 1063 | (and follow-indentation-p (back-to-indentation))) | ||
| 1064 | (python-info-dedenter-opening-block-message))) | ||
| 1058 | 1065 | ||
| 1059 | (defun python-indent-line-function () | 1066 | (defun python-indent-line-function () |
| 1060 | "`indent-line-function' for Python mode. | 1067 | "`indent-line-function' for Python mode. |
| @@ -1189,7 +1196,7 @@ the line will be re-indented automatically if needed." | |||
| 1189 | (save-excursion | 1196 | (save-excursion |
| 1190 | (goto-char (line-beginning-position)) | 1197 | (goto-char (line-beginning-position)) |
| 1191 | (let ((indentation (python-indent-calculate-indentation))) | 1198 | (let ((indentation (python-indent-calculate-indentation))) |
| 1192 | (when (< (current-indentation) indentation) | 1199 | (when (and (numberp indentation) (< (current-indentation) indentation)) |
| 1193 | (indent-line-to indentation))))) | 1200 | (indent-line-to indentation))))) |
| 1194 | ;; Electric colon | 1201 | ;; Electric colon |
| 1195 | ((and (eq ?: last-command-event) | 1202 | ((and (eq ?: last-command-event) |