Function: opascal-previous-indent-of

opascal-previous-indent-of is a byte-compiled function defined in opascal.el.gz.

Signature

(opascal-previous-indent-of FROM-TOKEN)

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/opascal.el.gz
(defun opascal-previous-indent-of (from-token)
  ;; Returns the indentation of the previous statement of the token.
  (let ((token from-token)
        (from-kind (opascal-token-kind from-token))
        (last-colon nil)
        (last-of nil)
        (last-token nil))
    (catch 'done
      (opascal--scan-non-whitespace-backward token last-token
        ;; An open ( or [ always is an indent point.
        ('open-group
         (throw 'done (opascal-open-group-indent token last-token)))

        ;; Skip over any ()/[] groups.
        ('close-group (setq token (opascal-group-start token)))

        ((opascal--in opascal-end-block-statements)
         (if (eq 'newline (opascal-token-kind (opascal-previous-token token)))
             ;; We can stop at an end token that is right up against the
             ;; margin.
             (throw 'done 0)
           ;; Otherwise, skip over any nested blocks.
           (setq token (opascal-block-start token))))

        ;; Special case: if we encounter a ", word;" then we assume that we
        ;; are in some kind of uses clause, and thus indent to column 0. This
        ;; works because no other constructs are known to have that form.
        ;; This fixes the irritating case of having indents after a uses
        ;; clause look like:
        ;;   uses
        ;;      someUnit,
        ;;      someOtherUnit;
        ;;      // this should be at column 0!
        ((guard
          (opascal-is-use-clause-end token last-token last-colon from-kind))
         (throw 'done 0))

        ;; A previous terminator means we can stop. If we are on a directive,
        ;; however, then we are not actually encountering a new statement.
        ((and (guard last-token)
              (opascal--in opascal-previous-terminators)
              (guard (not (memq (opascal-token-kind last-token)
                                opascal-directives))))
         (throw 'done (opascal-stmt-line-indent-of last-token 0)))

        ;; Remember any "of" we encounter, since that affects how we
        ;; indent to a case statement within a record declaration
        ;; (i.e. a variant part).
        ('of
         (setq last-of token))

        ;; Remember any ':' we encounter (until we reach an "of"),
        ;; since that affects how we indent to case statements in
        ;; general.
        ('colon
         (unless last-of (setq last-colon token)))

        ;; A case statement delimits a previous statement. We indent labels
        ;; specially.
        ('case
         (throw 'done
                (if last-colon (opascal-line-indent-of last-colon)
                  (opascal-line-indent-of token opascal-case-label-indent))))

        ;; If we are in a use clause then commas mark an enclosing rather than
        ;; a previous statement.
        ((opascal--in opascal-use-clauses)
         (throw 'done
                (if (eq 'comma from-kind)
                    (if last-token
                        ;; Indent to first unit in use clause.
                        (opascal-indent-of last-token)
                      ;; Indent from use clause keyword.
                      (opascal-line-indent-of token opascal-indent-level))
                  ;; Indent to use clause keyword.
                  (opascal-line-indent-of token))))

        ;; Assembly sections always indent in from the asm keyword.
        ('asm
         (throw 'done (opascal-stmt-line-indent-of token opascal-indent-level)))

        ;; An enclosing statement delimits a previous statement.
        ;; We try to use the existing indent of the previous statement,
        ;; otherwise we calculate from the enclosing statement.
        ((opascal--in opascal-previous-enclosing-statements)
         (throw 'done (if last-token
                          ;; Otherwise indent to the last token
                          (opascal-line-indent-of last-token)
                        ;; Just indent from the enclosing keyword
                        (opascal-line-indent-of token opascal-indent-level))))

        ;; A class or record declaration also delimits a previous statement.
        ((guard (opascal-composite-type-start token last-token))
         (throw
          'done
          (if (opascal-is-simple-class-type last-token from-token)
              ;; c = class; or c = class of T; are previous statements.
              (opascal-line-indent-of token)
            ;; Otherwise c = class ... or r = record ... are enclosing
            ;; statements.
            (opascal-line-indent-of last-token opascal-indent-level))))

        ;; We have a definite previous statement delimiter.
        ((opascal--in opascal-previous-statements)
         (throw 'done (opascal-stmt-line-indent-of token 0)))
        )
      ;; We ran out of tokens. Indent to column 0.
      0)))