Function: LaTeX-indent-calculate-last

LaTeX-indent-calculate-last is a byte-compiled function defined in latex.el.

Signature

(LaTeX-indent-calculate-last &optional FORCE-TYPE)

Documentation

Return the correct indentation of a normal line of text.

The point is supposed to be at the beginning of the current line. FORCE-TYPE can be used to force the calculation of an inner or outer indentation in case of a commented line. The symbols inner and outer are recognized.

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/latex.el
(defun LaTeX-indent-calculate-last (&optional force-type)
  "Return the correct indentation of a normal line of text.
The point is supposed to be at the beginning of the current line.
FORCE-TYPE can be used to force the calculation of an inner or
outer indentation in case of a commented line.  The symbols
`inner' and `outer' are recognized."
  (let (line-comment-current-flag
        line-comment-last-flag
        comment-current-flag
        comment-last-flag
        (indent-across-comments (or docTeX-indent-across-comments
                                    (not (eq major-mode 'docTeX-mode)))))
    (beginning-of-line)
    (setq line-comment-current-flag (TeX-in-line-comment)
          comment-current-flag (TeX-in-commented-line))
    (if comment-current-flag
        (skip-chars-backward "%\n\t ")
      (skip-chars-backward "\n\t "))
    (beginning-of-line)
    ;; If we are called in a non-comment line, skip over comment
    ;; lines.  The computation of indentation should in this case
    ;; rather take the last non-comment line into account.
    ;; Otherwise there might arise problems with e.g. multi-line
    ;; code comments.  This behavior can be disabled in docTeX mode
    ;; where large amounts of line comments may have to be skipped
    ;; and indentation should not be influenced by unrelated code in
    ;; other macrocode environments.
    (while (and indent-across-comments
                (not comment-current-flag)
                (TeX-in-commented-line)
                (not (bobp)))
      (skip-chars-backward "\n\t ")
      (beginning-of-line))
    (setq line-comment-last-flag (TeX-in-line-comment)
          comment-last-flag (TeX-in-commented-line))
    (LaTeX-back-to-indentation force-type)
    ;; Separate line comments and other stuff (normal text/code and
    ;; code comments).  Additionally we don't want to compute inner
    ;; indentation when a commented and a non-commented line are
    ;; compared.
    (cond ((or (and (eq major-mode 'docTeX-mode)
                    (or (and line-comment-current-flag
                             (not line-comment-last-flag))
                        (and (not line-comment-current-flag)
                             line-comment-last-flag)))
               (and force-type
                    (eq force-type 'inner)
                    (or (and comment-current-flag
                             (not comment-last-flag))
                        (and (not comment-current-flag)
                             comment-last-flag))))
           0)
          ((looking-at (concat (regexp-quote TeX-esc)
                               "begin *{\\("
                               LaTeX-document-regexp
                               "\\)}"))
           ;; I dislike having all of the document indented...
           (+ (LaTeX-current-indentation force-type)
              ;; Some people have opening braces at the end of the
              ;; line, e.g. in case of `\begin{letter}{%'.
              (TeX-brace-count-line)))
          ((and (eq major-mode 'docTeX-mode)
                (looking-at (concat (regexp-quote TeX-esc)
                                    "end[ \t]*{macrocode\\*?}"))
                fill-prefix
                (TeX-in-line-comment))
           ;; Reset indentation to zero after a macrocode environment
           ;; only when we're not still inside a describing
           ;; environment like "macro" or "environment" etc.  Text
           ;; inside these environments after '\end{macrocode}' is
           ;; indented with `LaTeX-indent-level':
           (let ((outer-env (LaTeX-current-environment 2)))
             (cond ((member outer-env '("macro" "environment"))
                    LaTeX-indent-level)
                   ((and (fboundp 'LaTeX-doc-NewDocElement-list)
                         (LaTeX-doc-NewDocElement-list)
                         (member outer-env
                                 (mapcar #'cadr (LaTeX-doc-NewDocElement-list))))
                    LaTeX-indent-level)
                   (t 0))))
          ((looking-at (concat (regexp-quote TeX-esc)
                               "begin *{"
                               ;; Don't give optional argument here
                               ;; because indent would be disabled
                               ;; inside comment env otherwise.
                               (LaTeX-verbatim-regexp)
                               "}"))
           0)
          ((looking-at (concat (regexp-quote TeX-esc)
                               "end *{"
                               (LaTeX-verbatim-regexp t)
                               "}"))
           ;; If I see an \end{verbatim} in the previous line I skip
           ;; back to the preceding \begin{verbatim}.
           (save-excursion
             (if (re-search-backward (concat (regexp-quote TeX-esc)
                                             "begin *{"
                                             (LaTeX-verbatim-regexp t)
                                             "}") 0 t)
                 (LaTeX-indent-calculate-last force-type)
               0)))
          (t (+ (LaTeX-current-indentation force-type)
                (if (not (and force-type
                              (eq force-type 'outer)
                              (TeX-in-commented-line)))
                    (+ (LaTeX-indent-level-count)
                       (TeX-brace-count-line))
                  0)
                (cond ((looking-at (concat (regexp-quote TeX-esc)
                                           "\\("
                                           LaTeX-end-regexp
                                           "\\)"))
                       LaTeX-indent-level)
                      ((looking-at
                        (concat (regexp-quote TeX-esc) "right\\b"))
                       LaTeX-left-right-indent-level)
                      ((looking-at (concat (regexp-quote TeX-esc)
                                           "\\("
                                           LaTeX-item-regexp
                                           "\\)"))
                       (- LaTeX-item-indent))
                      ((looking-at (concat (regexp-quote TeX-esc)
                                           "\\("
                                           LaTeX-indent-end-regexp-local
                                           "\\)"))
                       LaTeX-indent-level)
                      ((looking-at (concat (regexp-quote TeX-esc)
                                           "\\("
                                           LaTeX-indent-mid-regexp-local
                                           "\\)"))
                       LaTeX-indent-level)
                      ((memq (char-after) (append
                                           TeX-indent-close-delimiters
                                           '(?\})))
                       TeX-brace-indent-level)
                      (t 0)))))))