0010-sql-indent-function.el 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. (defvar *sql-indent-width* 2)
  2. (defun sql-indent-line ()
  3. (interactive)
  4. (save-excursion
  5. (indent-line-to
  6. (cond
  7. ((or (sql-in-column-list?)
  8. (sql-in-join-clause?))
  9. (sql-indent-under-clause))
  10. (t 0)))))
  11. (defun sql-indent-under-clause ()
  12. (let ((current-line (line-number-at-pos)))
  13. (save-excursion
  14. (sql-last-clause)
  15. (if (= current-line (line-number-at-pos))
  16. 0
  17. (+ *sql-indent-width* (current-column))))))
  18. (defun sql-last-clause ()
  19. (catch 'clause
  20. (while t
  21. (cond
  22. ((looking-at "\\bselect\\b")
  23. (throw 'clause 'select))
  24. ((looking-at "\\bfrom\\b")
  25. (throw 'clause 'from))
  26. ((looking-at "\\bjoin\\b")
  27. (throw 'clause 'join))
  28. ((looking-at "\\bwhere\\b")
  29. (throw 'clause 'where))
  30. ((bobp)
  31. (throw 'clause nil))
  32. (t (backward-word))))))
  33. (defun sql-in-column-list? ()
  34. (save-excursion
  35. (eql 'select (sql-last-clause))))
  36. (defun sql-in-join-clause? ()
  37. (save-excursion
  38. (eql 'join (sql-last-clause))))
  39. (add-hook 'sql-mode-hook
  40. (lambda ()
  41. (setq-local indent-line-function
  42. 'sql-indent-line)))