Function: f90-indent-region

f90-indent-region is an interactive and byte-compiled function defined in f90.el.gz.

Signature

(f90-indent-region BEG-REGION END-REGION)

Documentation

Indent every line in region by forward parsing.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/f90.el.gz
  (f90-indent-line 'no-update))         ; nothing to update


;; TODO not add spaces to empty lines at the start.
;; Why is second line getting extra indent over first?
(defun f90-indent-region (beg-region end-region)
  "Indent every line in region by forward parsing."
  (interactive "*r")
  (let ((end-region-mark (copy-marker end-region))
        (save-point (point-marker))
        (case-fold-search t)
        block-list ind-lev ind-curr ind-b cont struct beg-struct end-struct)
    (goto-char beg-region)
    ;; First find a line which is not a continuation line or comment.
    (beginning-of-line)
    (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
                (progn (f90-indent-line 'no-update)
                       (zerop (forward-line 1)))
                (< (point) end-region-mark)))
    (setq cont (f90-present-statement-cont))
    (while (and (memq cont '(middle end))
                (f90-previous-statement))
      (setq cont (f90-present-statement-cont)))
    ;; Process present line for beginning of block.
    (setq f90-cache-position (point))
    (f90-indent-line 'no-update)
    (setq ind-lev (f90-current-indentation)
          ind-curr ind-lev)
    (beginning-of-line)
    (skip-chars-forward " \t0-9")
    (setq struct nil
          ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
                      ((or (setq struct (f90-looking-at-if-then))
                           (setq struct (f90-looking-at-select-case))
                           (setq struct (f90-looking-at-where-or-forall))
                           (looking-at f90-else-like-re))
                       f90-if-indent)
                      ((setq struct (f90-looking-at-type-like))
                       f90-type-indent)
                      ((setq struct (f90-looking-at-associate))
                       f90-associate-indent)
                      ((setq struct (f90-looking-at-critical))
                       f90-critical-indent)
                      ((or (setq struct (f90-looking-at-program-block-start))
                           (looking-at "contains[ \t]*\\($\\|!\\)"))
                       f90-program-indent)))
    (if ind-b (setq ind-lev (+ ind-lev ind-b)))
    (if struct (setq block-list (cons struct block-list)))
    (while (and (f90-line-continued) (zerop (forward-line 1))
                (< (point) end-region-mark))
      (if (looking-at "[ \t]*!")
          (f90-indent-to (f90-comment-indent))
        (or (= (current-indentation)
               (+ ind-curr f90-continuation-indent))
            (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
    ;; Process all following lines.
    (while (and (zerop (forward-line 1)) (< (point) end-region-mark))
      (beginning-of-line)
      (f90-indent-line-no)
      (setq f90-cache-position (point))
      (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
            ((looking-at "[ \t]*#") (setq ind-curr 0))
            ((looking-at "!") (setq ind-curr (f90-comment-indent)))
            ((f90-no-block-limit) (setq ind-curr ind-lev))
            ((looking-at f90-else-like-re) (setq ind-curr
                                                 (- ind-lev f90-if-indent)))
            ((looking-at "contains[ \t]*\\($\\|!\\)")
             (setq ind-curr (- ind-lev f90-program-indent)))
            ((setq ind-b
                   (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
                         ((or (setq struct (f90-looking-at-if-then))
                              (setq struct (f90-looking-at-select-case))
                              (setq struct (f90-looking-at-where-or-forall)))
                          f90-if-indent)
                         ((setq struct (f90-looking-at-type-like))
                          f90-type-indent)
                         ((setq struct (f90-looking-at-associate))
                          f90-associate-indent)
                         ((setq struct (f90-looking-at-critical))
                          f90-critical-indent)
                         ((setq struct (f90-looking-at-program-block-start))
                          f90-program-indent)))
             (setq ind-curr ind-lev)
             (if ind-b (setq ind-lev (+ ind-lev ind-b)))
             (setq block-list (cons struct block-list)))
            ((setq end-struct (f90-looking-at-program-block-end))
             (setq beg-struct (car block-list)
                   block-list (cdr block-list))
             (if f90-smart-end
                 (save-excursion
                   (f90-block-match (car beg-struct) (cadr beg-struct)
                                    (car end-struct) (cadr end-struct))))
             (setq ind-b
                   (cond ((looking-at f90-end-if-re) f90-if-indent)
                         ((looking-at "end[ \t]*do\\_>")  f90-do-indent)
                         ((looking-at f90-end-type-re) f90-type-indent)
                         ((looking-at f90-end-associate-re)
                          f90-associate-indent)
                         ((f90-looking-at-end-critical) f90-critical-indent)
                         ((f90-looking-at-program-block-end)
                          f90-program-indent)))
             (if ind-b (setq ind-lev (- ind-lev ind-b)))
             (setq ind-curr ind-lev))
            (t (setq ind-curr ind-lev)))
      ;; Do the indentation if necessary.
      (or (= ind-curr (current-column))
          (f90-indent-to ind-curr))
      (while (and (f90-line-continued) (zerop (forward-line 1))
                  (< (point) end-region-mark))
        (cond ((looking-at "[ \t]*#") (f90-indent-to 0))
              ((looking-at "[ \t]*!") (f90-indent-to (f90-comment-indent)))
              (t
               (or (= (current-indentation)
                      (+ ind-curr f90-continuation-indent))
                   (f90-indent-to
                    (+ ind-curr f90-continuation-indent) 'no-line-no))))))
    ;; Restore point, etc.
    (setq f90-cache-position nil)
    (goto-char save-point)
    (set-marker end-region-mark nil)
    (set-marker save-point nil)
    (deactivate-mark)))