Function: vera-guess-basic-syntax
vera-guess-basic-syntax is a byte-compiled function defined in
vera-mode.el.gz.
Signature
(vera-guess-basic-syntax)
Documentation
Determine syntactic context of current line of code.
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/vera-mode.el.gz
(defun vera-guess-basic-syntax ()
"Determine syntactic context of current line of code."
(save-excursion
(beginning-of-line)
(let ((indent-point (point))
syntax state placeholder)
;; determine syntax state
(setq state (parse-partial-sexp (point-min) (point)))
(cond
;; CASE 1: in a comment?
((nth 4 state)
;; skip empty lines
(while (and (zerop (forward-line -1))
(looking-at "^\\s-*$")))
(vera-add-syntax 'comment (vera-point 'boi)))
;; CASE 2: in a string?
((nth 3 state)
(vera-add-syntax 'string))
;; CASE 3: at a directive?
((save-excursion (back-to-indentation) (= (following-char) ?\#))
(vera-add-syntax 'directive (point)))
;; CASE 4: after an opening parenthesis (argument list continuation)?
((and (nth 1 state)
(or (= (char-after (nth 1 state)) ?\()
;; also for concatenation (opening '{' and ',' on eol/eopl)
(and (= (char-after (nth 1 state)) ?\{)
(or (save-excursion
(vera-backward-syntactic-ws) (= (char-before) ?,))
(save-excursion
(end-of-line) (= (char-before) ?,))))))
(goto-char (1+ (nth 1 state)))
;; is there code after the opening parenthesis on the same line?
(if (looking-at "\\s-*$")
(vera-add-syntax 'arglist-cont (vera-point 'boi))
(vera-add-syntax 'arglist-cont-nonempty (point))))
;; CASE 5: at a block closing?
((save-excursion (back-to-indentation) (looking-at vera-end-block-re))
;; look for the corresponding begin
(vera-corresponding-begin)
(vera-add-syntax 'block-close (vera-point 'boi)))
;; CASE 6: at a block intro (the first line after a block opening)?
((and (save-excursion
(vera-backward-syntactic-ws nil t)
;; previous line ends with a block opening?
(or (/= (skip-chars-backward "{") 0) (backward-word-strictly 1))
(when (looking-at vera-beg-block-re)
;; go to beginning of substatement
(vera-beginning-of-substatement)
(setq placeholder (point))))
;; not if "fork" is followed by "{"
(save-excursion
(not (and (progn (back-to-indentation) (looking-at "{"))
(progn (goto-char placeholder)
(looking-at "\\<fork\\>"))))))
(goto-char placeholder)
(vera-add-syntax 'block-intro (vera-point 'boi)))
;; CASE 7: at the beginning of an else clause?
((save-excursion (back-to-indentation) (looking-at "\\<else\\>"))
;; find corresponding if
(vera-corresponding-if)
(vera-add-syntax 'else-clause (vera-point 'boi)))
;; CASE 8: at the beginning of a statement?
;; is the previous command completed?
((or (save-excursion
(vera-backward-syntactic-ws nil t)
(setq placeholder (point))
;; at the beginning of the buffer?
(or (bobp)
;; previous line ends with a semicolon or
;; is a block opening or closing?
(when (or (/= (skip-chars-backward "{};") 0)
(progn (back-to-indentation)
(looking-at (concat vera-beg-block-re "\\|"
vera-end-block-re))))
;; if at a block closing, go to beginning
(when (looking-at vera-end-block-re)
(vera-corresponding-begin))
;; go to beginning of the statement
(vera-beginning-of-statement)
(setq placeholder (point)))
;; at a directive?
(when (progn (back-to-indentation) (looking-at "#"))
;; go to previous statement
(vera-beginning-of-statement)
(setq placeholder (point)))))
;; at a block opening?
(when (save-excursion (back-to-indentation)
(looking-at vera-beg-block-re))
;; go to beginning of the substatement
(vera-beginning-of-substatement)
(setq placeholder (point))))
(goto-char placeholder)
(vera-add-syntax 'statement (vera-point 'boi)))
;; CASE 9: at the beginning of a substatement?
;; is this line preceded by a substatement opening statement?
((save-excursion (vera-backward-syntactic-ws nil t)
(when (= (preceding-char) ?\)) (backward-sexp))
(backward-word-strictly 1)
(setq placeholder (point))
(looking-at vera-beg-substatement-re))
(goto-char placeholder)
(vera-add-syntax 'substatement (vera-point 'boi)))
;; CASE 10: it must be a statement continuation!
(t
;; go to beginning of statement
(vera-beginning-of-substatement)
(vera-add-syntax 'statement-cont (vera-point 'boi))))
;; special case: look for a comment start
(goto-char indent-point)
(skip-chars-forward " \t")
(when (looking-at comment-start)
(vera-add-syntax 'comment-intro))
;; return syntax
syntax)))