Function: sqlind-maybe-begin-statement

sqlind-maybe-begin-statement is a natively compiled function defined in sql-indent.el.

Signature

(sqlind-maybe-begin-statement)

Documentation

Return the syntax of a "begin" statement.

If (point) is on a "begin" statement, report its syntax. 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-begin-statement ()
  "Return the syntax of a \"begin\" statement.
If (point) is on a \"begin\" statement, report its syntax. Only
keywords in program code are matched, not the ones inside
expressions.

See also `sqlind-beginning-of-block'"
  (when (and (looking-at "begin") (not (sqlind-looking-at-begin-transaction)))
    ;; a begin statement starts a block unless it is the first begin in a
    ;; procedure over which we need to skip it.
    (prog1  t                           ; make sure we return t
      (let* ((saved-pos (point))
	     (begin-label (save-excursion
			    (sqlind-beginning-of-statement)
			    (if (looking-at "<<\\([a-z0-9_]+\\)>>\\_>")
				(sqlind-match-string 1) "")))
	     (previous-block (save-excursion
			       (ignore-errors (forward-char -1))
			       (cons (sqlind-beginning-of-block) (point))))
	     (previous-block-kind (nth 0 previous-block)))

	(goto-char saved-pos)
	(when (null sqlind-end-stmt-stack)
	  (throw 'finished
	    (cond ((memq previous-block-kind '(toplevel declare-statement))
		   (list 'in-begin-block 'toplevel-block begin-label))
		  ((and (listp previous-block-kind)
			(eq (nth 0 previous-block-kind) 'defun-start))
		   (list 'in-begin-block 'defun (nth 1 previous-block-kind)))
                  ((and (listp previous-block-kind)
                        (memq (nth 0 previous-block-kind) '(package package-body)))
                   (list 'in-begin-block 'package (nth 1 previous-block-kind)))
		  (t
		   (list 'in-begin-block nil begin-label)))))

	(cl-destructuring-bind (pos kind label) (pop sqlind-end-stmt-stack)
	  (cond
	    (kind
	     (throw 'finished
	       (list 'syntax-error "bad closing for begin block" (point) pos)))

	    ((not (equal begin-label ""))
	     ;; we have a begin label.  In this case it must match the end
	     ;; label
	     (unless (equal begin-label label)
	       (throw 'finished
		 (list 'syntax-error "mismatched block labels" (point) pos))))

	    (t
	     ;; we don't have a begin label.  In this case, the begin
	     ;; statement might not start a block if it is the begining of a
	     ;; procedure or declare block over which we need to skip.

	     (cond ((memq previous-block-kind '(toplevel declare-statement))
		    (goto-char (cdr previous-block)))
		   ((and (listp previous-block-kind)
			 (memq (nth 0 previous-block-kind)
                               '(defun-start package package-body)))
		    (unless (sqlind-labels-match
			     label (nth 1 previous-block-kind))
		      (throw 'finished
			(list 'syntax-error
			      "bad end label for defun" (point) pos)))
		    (goto-char (cdr previous-block)))))))))))