Function: pascal-calculate-indent
pascal-calculate-indent is a byte-compiled function defined in
pascal.el.gz.
Signature
(pascal-calculate-indent)
Documentation
Calculate the indent of the current Pascal line.
Return a list of two elements: (INDENT-TYPE INDENT-LEVEL).
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/pascal.el.gz
(defun pascal-calculate-indent ()
"Calculate the indent of the current Pascal line.
Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
(save-excursion
(let* ((parse-sexp-ignore-comments t)
(oldpos (point))
(state (save-excursion (parse-partial-sexp (point-min) (point))))
(nest 0) (par 0) (complete (looking-at "[ \t]*end\\>"))
(elsed (looking-at "[ \t]*else\\>")) (funccnt 0)
(did-func (looking-at "[ \t]*\\(procedure\\|function\\)\\>"))
(type (catch 'nesting
;; Check if inside a string, comment or parenthesis
(cond ((nth 3 state) (throw 'nesting 'string))
((nth 4 state) (throw 'nesting 'comment))
((> (car state) 0)
(goto-char (scan-lists (point) -1 (car state)))
(setq par (1+ (current-column))))
((save-excursion (beginning-of-line)
(eq (following-char) ?#))
(throw 'nesting 'cpp)))
;; Loop until correct indent is found
(while t
(backward-sexp 1)
(cond (;--Escape from case statements
(and (looking-at "[A-Za-z0-9]+[ \t]*:[^=]")
(not complete)
(save-excursion (skip-chars-backward " \t")
(bolp))
(= (save-excursion
(end-of-line) (backward-sexp) (point))
(point))
(> (save-excursion (goto-char oldpos)
(beginning-of-line)
(point))
(point)))
(throw 'nesting 'caseblock))
(;--Beginning of program
(looking-at pascal-progbeg-re)
(throw 'nesting 'progbeg))
(;--No known statements
(bobp)
(throw 'nesting 'progbeg))
(;--Nest block outwards
(looking-at pascal-beg-block-re)
(if (= nest 0)
(cond ((looking-at "case\\>")
(throw 'nesting 'case))
((looking-at "record\\>")
(throw 'nesting 'declaration))
(t (throw 'nesting 'block)))
(if (and (looking-at "record\\>") (= nest 1))
(setq funccnt (1- funccnt)))
(setq nest (1- nest))))
(;--Nest block inwards
(looking-at pascal-end-block-re)
(if (and (looking-at "end\\s ")
elsed (not complete))
(throw 'nesting 'block))
(if (= nest 0)
(setq funccnt (1+ funccnt)))
(setq complete t
nest (1+ nest)))
(;--Defun (or parameter list)
(and (looking-at pascal-defun-re)
(progn (setq funccnt (1- funccnt)
did-func t)
(or (bolp) (< funccnt 0))))
;; Prevent searching whole buffer
(if (and (bolp) (>= funccnt 0))
(throw 'nesting 'progbeg))
(if (= 0 par)
(throw 'nesting 'defun)
(setq par 0)
(let ((n 0))
(while (re-search-forward
"\\(\\<record\\>\\)\\|\\<end\\>"
oldpos t)
(if (match-end 1)
(setq n (1+ n)) (setq n (1- n))))
(if (> n 0)
(throw 'nesting 'declaration)
(throw 'nesting 'paramlist)))))
(;--Declaration part
(and (looking-at pascal-declaration-re)
(not did-func)
(= funccnt 0))
(if (save-excursion
(goto-char oldpos)
(forward-line -1)
(looking-at "^[ \t]*$"))
(throw 'nesting 'unknown)
(throw 'nesting 'declaration)))
(;--If, else or while statement
(and (not complete)
(looking-at pascal-sub-block-re))
(throw 'nesting 'block))
(;--Found complete statement
(save-excursion (forward-sexp 1)
(= (following-char) ?\;))
(setq complete t))
)))))
;; Return type of block and indent level.
(if (> par 0) ; Unclosed Parenthesis
(list 'contexp par)
(list type (pascal-indent-level))))))