Function: sqlind-maybe-defun-statement

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

Signature

(sqlind-maybe-defun-statement)

Documentation

If (point) is on a procedure definition statement, report its syntax.

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-defun-statement ()
  "If (point) is on a procedure definition statement, report its syntax.
See also `sqlind-beginning-of-block'"
  (catch 'exit
    (when (looking-at "\\(procedure\\|function\\)\\(?:[ \t\n\r\f]+\\)\\(?:[a-z0-9_]+\\.\\)?\\([a-z0-9_]+\\)")
      (prog1 t                          ; make sure we return t
	(let ((proc-name (sqlind-match-string 2)))
	  ;; need to find out if this is a procedure/function
	  ;; declaration or a definition
	  (save-excursion
	    (goto-char (match-end 0))
            (sqlind-forward-syntactic-ws)
	    ;; skip param list, if any.
	    (when (looking-at "(")
	      (ignore-errors (forward-sexp 1))
	      (sqlind-forward-syntactic-ws))
	    (when (looking-at "return\\(?:[ \t\n\r\f]+\\)\\([a-z0-9_.]+\\(?:%\\(?:row\\)?type\\)?\\)")
	      (goto-char (match-end 0))
	      (sqlind-forward-syntactic-ws))
	    (when (looking-at ";")
	      ;; not a procedure after all.
	      (throw 'exit nil)))

          (save-excursion
            (sqlind-backward-syntactic-ws)
            ;; Find out if it is a drop procedure or function statement
            (forward-word -1)
            (when (looking-at "drop")
              ;; not a procedure after all
              (throw 'exit nil))
            ;; Find out if it is a "comment on" statement (postgres only)
            (when (and (eq sql-product 'postgres)
                       (looking-at "on"))
              (sqlind-backward-syntactic-ws)
              (forward-word -1)
              (when (looking-at "comment")
                ;; not a procedure after all.
                (throw 'exit nil))))

	  ;; so it is a definition

	  ;; if the procedure starts with "create or replace", move
	  ;; point to the real start
	  (let ((real-start (point)))
	    (save-excursion
	      (forward-word -1)
	      (when (looking-at "replace")
		(forward-word -2))
	      (when (and (or (looking-at "create\\([ \t\r\n\f]+or[ \t\r\n\f]+replace\\)?")
                             (looking-at "alter"))
			 (not (sqlind-in-comment-or-string (point))))
		(setq real-start (point))))
	    (goto-char real-start))

	  (when (null sqlind-end-stmt-stack)
	    (throw 'finished (list 'defun-start proc-name)))

	  (cl-destructuring-bind (pos kind label) (pop sqlind-end-stmt-stack)
	    (unless (and (eq kind nil)
			 (sqlind-labels-match label proc-name))
	      (throw 'finished
		(list 'syntax-error "bad end label for defun" (point) pos)))))))))