Function: python-indent--calculate-indentation

python-indent--calculate-indentation is a byte-compiled function defined in python.el.gz.

Signature

(python-indent--calculate-indentation)

Documentation

Internal implementation of python-indent-calculate-indentation.

May return an integer for the maximum possible indentation at current context or a list of integers. The latter case is only happening for :at-dedenter-block-start context since the possibilities can be narrowed to specific indentation points.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
(defun python-indent--calculate-indentation ()
  "Internal implementation of `python-indent-calculate-indentation'.
May return an integer for the maximum possible indentation at
current context or a list of integers.  The latter case is only
happening for :at-dedenter-block-start context since the
possibilities can be narrowed to specific indentation points."
    (save-excursion
      (pcase (python-indent-context)
        (`(:no-indent . ,_) (prog-first-column)) ; usually 0
        (`(,(or :after-line
                :after-comment
                :inside-string
                :after-backslash
                :inside-paren-continuation-line) . ,start)
         ;; Copy previous indentation.
         (goto-char start)
         (current-indentation))
        (`(,(or :inside-paren-at-closing-paren
                :inside-paren-at-closing-nested-paren) . ,start)
         (goto-char (+ 1 start))
         (if (looking-at "[ \t]*\\(?:#\\|$\\)")
             ;; Copy previous indentation.
             (current-indentation)
           ;; Align with opening paren.
           (current-column)))
        (`(:inside-docstring . ,start)
         (let* ((line-indentation (current-indentation))
                (base-indent (progn
                               (goto-char start)
                               (current-indentation))))
           (max line-indentation base-indent)))
        (`(,(or :after-block-start
                :after-backslash-first-line
                :after-backslash-assignment-continuation
                :inside-paren-newline-start) . ,start)
         ;; Add one indentation level.
         (goto-char start)
         (+ (current-indentation) python-indent-offset))
        (`(:after-backslash-block-continuation . ,start)
         (goto-char start)
         (let ((column (current-column)))
           (if (= column (+ (current-indentation) python-indent-offset))
               ;; Add one level to avoid same indent as next logical line.
               (+ column python-indent-offset)
             column)))
        (`(,(or :inside-paren
                :after-backslash-dotted-continuation) . ,start)
         ;; Use the column given by the context.
         (goto-char start)
         (current-column))
        (`(:after-block-end . ,start)
         ;; Subtract one indentation level.
         (goto-char start)
         (max 0 (- (current-indentation) python-indent-offset)))
        (`(:at-dedenter-block-start . ,_)
         ;; List all possible indentation levels from opening blocks.
         (let ((opening-block-start-points
                (python-info-dedenter-opening-block-positions)))
           (if (not opening-block-start-points)
               (prog-first-column) ; if not found default to first column
             (mapcar (lambda (pos)
                       (save-excursion
                         (goto-char pos)
                         (current-indentation)))
                     opening-block-start-points))))
        (`(,(or :inside-paren-newline-start-from-block) . ,start)
         (goto-char start)
         (+ (current-indentation)
            (* python-indent-offset python-indent-def-block-scale)))
        (`(,:inside-paren-from-block . ,start)
         (goto-char start)
         (let ((column (current-column)))
           (if (and python-indent-block-paren-deeper
                    (= column (+ (save-excursion
                                   (python-nav-beginning-of-statement)
                                   (current-indentation))
                                 python-indent-offset)))
               (+ column python-indent-offset)
             column))))))