Function: sqlind-maybe-then-statement
sqlind-maybe-then-statement is a natively compiled function defined in
sql-indent.el.
Signature
(sqlind-maybe-then-statement)
Documentation
Find the corresponding start block for a "then" statement.
When (point) is on a "then" statement, find the corresponding start of the block and report its syntax. This code will skip over nested statements to determine the actual start.
Only keywords in program code are matched, not the ones inside expressions.
See also sqlind-beginning-of-block
Source Code
;; Defined in /nix/store/jxjf2h9nnkdqnn1w1mr0ag8bqng2rlrs-emacs-packages-deps/share/emacs/site-lisp/elpa/sql-indent-1.7/sql-indent.el
(defun sqlind-maybe-then-statement ()
"Find the corresponding start block for a \"then\" statement.
When (point) is on a \"then\" statement, find the corresponding
start of the block and report its syntax. This code will skip
over nested statements to determine the actual start.
Only keywords in program code are matched, not the ones inside
expressions.
See also `sqlind-beginning-of-block'"
(when (looking-at "then")
(prog1 t ; make sure we return t
(let ((start-pos (point)))
;; a then keyword at the begining of a line is a block start
(when (null sqlind-end-stmt-stack)
(save-excursion
(back-to-indentation)
(when (eq (point) start-pos)
(throw 'finished (list 'in-block 'then "")))))
;; if it is not at the beginning of a line, the beginning of
;; the statement is the block start
(ignore-errors (forward-char -1))
(sqlind-beginning-of-statement)
;; a then keyword only starts a block when it is part of an
;; if, case/when or exception statement
(cond
((looking-at "\\(<<[a-z0-9_]+>>\\)?\\(?:[ \t\n\r\f]*\\)\\(\\(?:els\\)?if\\)\\_>")
(let ((if-label (sqlind-match-string 1))
(if-kind (intern (sqlind-match-string 2)))) ; can be if or elsif
(setq if-label (if if-label (substring if-label 2 -2) ""))
(if (null sqlind-end-stmt-stack)
(throw 'finished (list 'in-block if-kind if-label))
;; "if" blocks (but not "elsif" blocks) need to be
;; ended with "end if"
(when (eq if-kind 'if)
(cl-destructuring-bind (pos kind label)
(pop sqlind-end-stmt-stack)
(unless (and (eq kind 'if)
(sqlind-labels-match label if-label))
(throw 'finished
(list 'syntax-error
"bad closing for if block" (point) pos))))))))
((looking-at "\\(<<[a-z0-9_]+>>\\)?\\(?:[ \t\n\r\f]*\\)case\\_>")
;; find the nearest when block, but only if there are no
;; end statements in the stack
(let ((case-label (sqlind-match-string 1)))
(setq case-label
(if case-label (substring case-label 2 -2) ""))
(if (null sqlind-end-stmt-stack)
(save-excursion
(when (sqlind-search-backward start-pos "\\_<when\\_>" (point))
(throw 'finished (list 'in-block 'case case-label))))
;; else
(cl-destructuring-bind (pos kind label)
(pop sqlind-end-stmt-stack)
(unless (and (eq kind 'case)
(sqlind-labels-match label case-label))
(throw 'finished
(list 'syntax-error
"bad closing for case block" (point) pos)))))))
((looking-at "exception\\_>")
;; an exception statement is a block start only if we have
;; no end statements in the stack
(when (null sqlind-end-stmt-stack)
(throw 'finished (list 'in-block 'exception ""))))
(t ; restore position if we didn't do anything
(goto-char start-pos)
nil))))))