Function: python-indent-context
python-indent-context is a byte-compiled function defined in
python.el.gz.
Signature
(python-indent-context)
Documentation
Get information about the current indentation context.
Context is returned in a cons with the form (STATUS . START).
STATUS can be one of the following:
keyword
-------
:after-comment
- Point is after a comment line.
- START is the position of the "#" character.
:inside-string
- Point is inside string.
- START is the position of the first quote that starts it.
:no-indent
- No possible indentation case matches.
- START is always zero.
:inside-paren
- Fallback case when point is inside paren.
- START is the first non space char position *after* the open paren.
:inside-paren-at-closing-nested-paren
- Point is on a line that contains a nested paren closer.
- START is the position of the open paren it closes.
:inside-paren-at-closing-paren
- Point is on a line that contains a paren closer.
- START is the position of the open paren.
:inside-paren-newline-start
- Point is inside a paren with items starting in their own line.
- START is the position of the open paren.
:inside-paren-newline-start-from-block
- Point is inside a paren with items starting in their own line
from a block start.
- START is the position of the open paren.
:inside-paren-from-block
- Point is inside a paren from a block start followed by some
items on the same line.
- START is the first non space char position *after* the open paren.
:inside-paren-continuation-line
- Point is on a continuation line inside a paren.
- START is the position where the previous line (excluding lines
for inner parens) starts.
:after-backslash
- Fallback case when point is after backslash.
- START is the char after the position of the backslash.
:after-backslash-assignment-continuation
- Point is after a backslashed assignment.
- START is the char after the position of the backslash.
:after-backslash-block-continuation
- Point is after a backslashed block continuation.
- START is the char after the position of the backslash.
:after-backslash-dotted-continuation
- Point is after a backslashed dotted continuation. Previous
line must contain a dot to align with.
- START is the char after the position of the backslash.
:after-backslash-first-line
- First line following a backslashed continuation.
- START is the char after the position of the backslash.
:after-block-end
- Point is after a line containing a block ender.
- START is the position where the ender starts.
:after-block-start
- Point is after a line starting a block.
- START is the position where the block starts.
:after-line
- Point is after a simple line.
- START is the position where the previous line starts.
:at-dedenter-block-start
- Point is on a line starting a dedenter block.
- START is the position where the dedenter block starts.
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
(defun python-indent-context ()
"Get information about the current indentation context.
Context is returned in a cons with the form (STATUS . START).
STATUS can be one of the following:
keyword
-------
:after-comment
- Point is after a comment line.
- START is the position of the \"#\" character.
:inside-string
- Point is inside string.
- START is the position of the first quote that starts it.
:no-indent
- No possible indentation case matches.
- START is always zero.
:inside-paren
- Fallback case when point is inside paren.
- START is the first non space char position *after* the open paren.
:inside-paren-at-closing-nested-paren
- Point is on a line that contains a nested paren closer.
- START is the position of the open paren it closes.
:inside-paren-at-closing-paren
- Point is on a line that contains a paren closer.
- START is the position of the open paren.
:inside-paren-newline-start
- Point is inside a paren with items starting in their own line.
- START is the position of the open paren.
:inside-paren-newline-start-from-block
- Point is inside a paren with items starting in their own line
from a block start.
- START is the position of the open paren.
:inside-paren-from-block
- Point is inside a paren from a block start followed by some
items on the same line.
- START is the first non space char position *after* the open paren.
:inside-paren-continuation-line
- Point is on a continuation line inside a paren.
- START is the position where the previous line (excluding lines
for inner parens) starts.
:after-backslash
- Fallback case when point is after backslash.
- START is the char after the position of the backslash.
:after-backslash-assignment-continuation
- Point is after a backslashed assignment.
- START is the char after the position of the backslash.
:after-backslash-block-continuation
- Point is after a backslashed block continuation.
- START is the char after the position of the backslash.
:after-backslash-dotted-continuation
- Point is after a backslashed dotted continuation. Previous
line must contain a dot to align with.
- START is the char after the position of the backslash.
:after-backslash-first-line
- First line following a backslashed continuation.
- START is the char after the position of the backslash.
:after-block-end
- Point is after a line containing a block ender.
- START is the position where the ender starts.
:after-block-start
- Point is after a line starting a block.
- START is the position where the block starts.
:after-line
- Point is after a simple line.
- START is the position where the previous line starts.
:at-dedenter-block-start
- Point is on a line starting a dedenter block.
- START is the position where the dedenter block starts."
(let ((ppss (save-excursion
(beginning-of-line)
(syntax-ppss))))
(cond
;; Beginning of buffer.
((= (line-number-at-pos) 1)
(cons :no-indent 0))
;; Inside a string.
((let ((start (python-syntax-context 'string ppss)))
(when start
(cons (if (python-info-docstring-p)
:inside-docstring
:inside-string) start))))
;; Inside a paren.
((let* ((start (python-syntax-context 'paren ppss))
(starts-in-newline
(when start
(save-excursion
(goto-char start)
(forward-char)
(not
(= (line-number-at-pos)
(progn
(python-util-forward-comment)
(line-number-at-pos)))))))
(continuation-start
(when start
(save-excursion
(forward-line -1)
(back-to-indentation)
;; Skip inner parens.
(cl-loop with prev-start = (python-syntax-context 'paren)
while (and prev-start (>= prev-start start))
if (= prev-start start)
return (point)
else do (goto-char prev-start)
(back-to-indentation)
(setq prev-start
(python-syntax-context 'paren)))))))
(when start
(cond
;; Current line only holds the closing paren.
((save-excursion
(skip-syntax-forward " ")
(when (and (python-syntax-closing-paren-p)
(progn
(forward-char 1)
(not (python-syntax-context 'paren))))
(cons :inside-paren-at-closing-paren start))))
;; Current line only holds a closing paren for nested.
((save-excursion
(back-to-indentation)
(python-syntax-closing-paren-p))
(cons :inside-paren-at-closing-nested-paren start))
;; This line is a continuation of the previous line.
(continuation-start
(cons :inside-paren-continuation-line continuation-start))
;; This line starts from an opening block in its own line.
((save-excursion
(goto-char start)
(when (and
starts-in-newline
(save-excursion
(back-to-indentation)
(looking-at (python-rx block-start))))
(cons
:inside-paren-newline-start-from-block start))))
(starts-in-newline
(cons :inside-paren-newline-start start))
;; General case.
(t (let ((after-start (save-excursion
(goto-char (1+ start))
(skip-syntax-forward "(" 1)
(skip-syntax-forward " ")
(point))))
(if (save-excursion
(python-nav-beginning-of-statement)
(python-info-looking-at-beginning-of-block))
(cons :inside-paren-from-block after-start)
(cons :inside-paren after-start))))))))
;; After backslash.
((let ((start (when (not (python-syntax-comment-or-string-p ppss))
(python-info-line-ends-backslash-p
(1- (line-number-at-pos))))))
(when start
(cond
;; Continuation of dotted expression.
((save-excursion
(back-to-indentation)
(when (eq (char-after) ?\.)
;; Move point back until it's not inside a paren.
(while (prog2
(forward-line -1)
(and (not (bobp))
(python-syntax-context 'paren))))
(goto-char (line-end-position))
(while (and (search-backward
"." (line-beginning-position) t)
(python-syntax-context-type)))
;; Ensure previous statement has dot to align with.
(when (and (eq (char-after) ?\.)
(not (python-syntax-context-type)))
(cons :after-backslash-dotted-continuation (point))))))
;; Continuation of block definition.
((let ((block-continuation-start
(python-info-block-continuation-line-p)))
(when block-continuation-start
(save-excursion
(goto-char block-continuation-start)
(re-search-forward
(python-rx block-start (* space))
(line-end-position) t)
(cons :after-backslash-block-continuation (point))))))
;; Continuation of assignment.
((let ((assignment-continuation-start
(python-info-assignment-continuation-line-p)))
(when assignment-continuation-start
(save-excursion
(goto-char assignment-continuation-start)
(cons :after-backslash-assignment-continuation (point))))))
;; First line after backslash continuation start.
((save-excursion
(goto-char start)
(when (or (= (line-number-at-pos) 1)
(not (python-info-beginning-of-backslash
(1- (line-number-at-pos)))))
(cons :after-backslash-first-line start))))
;; General case.
(t (cons :after-backslash start))))))
;; After beginning of block.
((let ((start (save-excursion
(back-to-indentation)
(python-util-forward-comment -1)
(when (equal (char-before) ?:)
(python-nav-beginning-of-block)))))
(when start
(cons :after-block-start start))))
;; At dedenter statement.
((let ((start (python-info-dedenter-statement-p)))
(when start
(cons :at-dedenter-block-start start))))
;; After normal line, comment or ender (default case).
((save-excursion
(back-to-indentation)
(skip-chars-backward " \t\n")
(if (bobp)
(cons :no-indent 0)
(python-nav-beginning-of-statement)
(cons
(cond ((python-info-current-line-comment-p)
:after-comment)
((save-excursion
(goto-char (line-end-position))
(python-util-forward-comment -1)
(python-nav-beginning-of-statement)
(looking-at (python-rx block-ender)))
:after-block-end)
(t :after-line))
(point))))))))