Function: antlr-indent-line
antlr-indent-line is a byte-compiled function defined in
antlr-mode.el.gz.
Signature
(antlr-indent-line)
Documentation
Indent the current line as ANTLR grammar code.
The indentation of grammar lines are calculated by c-basic-offset,
multiplied by:
- the level of the paren/brace/bracket depth,
- plus 0/2/1, depending on the position inside the rule: header, body,
exception part,
- minus 1 if antlr-indent-item-regexp matches the beginning of the
line starting from the first non-whitespace.
Lines inside block comments are indented by c-indent-line according to
antlr-indent-comment.
Lines in actions except top-level actions in a header part or an option
area are indented by c-indent-line.
Lines in header actions are indented at column 0 if antlr-language
equals to a key in antlr-indent-at-bol-alist and the line starting at
the first non-whitespace is matched by the corresponding value.
For the initialization of c-basic-offset, see antlr-indent-style and,
to a lesser extent, antlr-tab-offset-alist.
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/antlr-mode.el.gz
;;;===========================================================================
;;; Indentation
;;;===========================================================================
(defun antlr-indent-line ()
"Indent the current line as ANTLR grammar code.
The indentation of grammar lines are calculated by `c-basic-offset',
multiplied by:
- the level of the paren/brace/bracket depth,
- plus 0/2/1, depending on the position inside the rule: header, body,
exception part,
- minus 1 if `antlr-indent-item-regexp' matches the beginning of the
line starting from the first non-whitespace.
Lines inside block comments are indented by `c-indent-line' according to
`antlr-indent-comment'.
Lines in actions except top-level actions in a header part or an option
area are indented by `c-indent-line'.
Lines in header actions are indented at column 0 if `antlr-language'
equals to a key in `antlr-indent-at-bol-alist' and the line starting at
the first non-whitespace is matched by the corresponding value.
For the initialization of `c-basic-offset', see `antlr-indent-style' and,
to a lesser extent, `antlr-tab-offset-alist'."
(save-restriction
(let ((orig (point))
(min0 (point-min))
bol boi indent syntax cc-syntax)
(widen)
(beginning-of-line)
(setq bol (point))
(if (< bol min0)
(error "Beginning of current line not visible"))
(skip-chars-forward " \t")
(setq boi (point))
;; check syntax at beginning of indentation ----------------------------
(with-syntax-table antlr-action-syntax-table
(antlr-invalidate-context-cache)
(setq syntax (antlr-syntactic-context))
(cond ((symbolp syntax)
(setq indent nil)) ; block-comments, strings, (comments)
((progn
(antlr-next-rule -1 t)
(if (antlr-search-forward ":") (< boi (1- (point))) t))
(setq indent 0)) ; in rule header
((if (antlr-search-forward ";") (< boi (point)) t)
(setq indent 2)) ; in rule body
(t
(forward-char)
(antlr-skip-exception-part nil)
(setq indent (if (> (point) boi) 1 0))))) ; in exception part?
;; check whether to use indentation engine of cc-mode ------------------
(antlr-invalidate-context-cache)
(goto-char boi)
(when (and indent (> syntax 0))
(cond ((> syntax 1) ; block in action => use cc-mode
(setq indent nil))
((and (= indent 0)
(assq antlr-language antlr-indent-at-bol-alist)
(looking-at (cdr (assq antlr-language
antlr-indent-at-bol-alist))))
(setq syntax 'bol))
((setq cc-syntax (c-guess-basic-syntax))
(let ((cc cc-syntax) symbol)
(while (setq symbol (pop cc))
(when (cdr symbol)
(or (memq (car symbol)
antlr-disabling-cc-syntactic-symbols)
(setq indent nil))
(setq cc nil)))))))
;;; ((= indent 1) ; exception part => use cc-mode
;;; (setq indent nil))
;;; ((save-restriction ; not in option part => cc-mode
;;; (goto-char (scan-lists (point) -1 1))
;;; (skip-chars-backward " \t\n")
;;; (narrow-to-region (point-min) (point))
;;; (not (re-search-backward "\\<options\\'" nil t)))
;;; (setq indent nil)))))
;; compute the corresponding indentation and indent --------------------
(if (null indent)
;; Use the indentation engine of cc-mode
(progn
(goto-char orig)
(if (or (numberp syntax)
(if (eq syntax 'string) nil (eq antlr-indent-comment t)))
(c-indent-line cc-syntax)))
;; do it ourselves
(goto-char boi)
(unless (symbolp syntax) ; direct indentation
;;(antlr-invalidate-context-cache)
(cl-incf indent (antlr-syntactic-context))
(and (> indent 0) (looking-at antlr-indent-item-regexp) (cl-decf indent))
(setq indent (* indent c-basic-offset)))
;; the usual major-mode indent stuff ---------------------------------
(setq orig (- (point-max) orig))
(unless (= (current-column) indent)
(delete-region bol boi)
(beginning-of-line)
(indent-to indent))
;; If initial point was within line's indentation,
;; position after the indentation. Else stay at same point in text.
(if (> (- (point-max) orig) (point))
(goto-char (- (point-max) orig)))))))