1 ;;; sql-indent.el --- indentation of SQL statements
3 ;; Copyright (C) 2000 Alex Schroeder
5 ;; Authors: Alex Schroeder <alex@gnu.org>
6 ;; Matt Henry <mcthenry+gnu@gmail.com>
7 ;; Maintainer: Matt Henry <mcthenry+gnu@gmail.com>
8 ;; Version: $Id: sql-indent.el,v 1.10 2009/03/25 22:52:25 mhenry Exp $
10 ;; Keywords: languages
11 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlIndent
13 ;; This file is not part of GNU Emacs.
15 ;; This is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; This is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 ;; Indent SQL statements.
34 ;; As the indentation of SQL statements depends not only on the previous
35 ;; line but also on the current line, empty lines cannot always be
36 ;; indented correctly.
38 ;; Usage note: Loading this file will make all SQL mode buffers created
39 ;; from then on use `sql-indent-line' for indentation. A possible way
40 ;; to install sql-indent.el would be to add the following to your
43 ;; (eval-after-load "sql"
44 ;; '(load-library "sql-indent"))
47 ;; Arcady Genkin <antipode@thpoon.com>
53 ;; Added `sql-indent-buffer' for efficient full buffer processing.
54 ;; Modified `sql-indent' to be savvy to comments and strings.
55 ;; Removed "and", "or" and "exists" from `sql-indent-first-column-regexp'
56 ;; Added "create", "drop" and "truncate" to `sql-indent-first-column-regexp'
62 ;; Need the following to allow GNU Emacs 19 to compile the file.
65 (defcustom sql-indent-first-column-regexp
66 (concat "^\\s-*" (regexp-opt '(
67 "select" "update" "insert" "delete"
69 "from" "where" "into" "group" "having" "order"
71 "create" "drop" "truncate"
72 "--") t) "\\(\\b\\|\\s-\\)")
73 "Regexp matching keywords relevant for indentation.
74 The regexp matches lines which start SQL statements and it matches lines
75 that should be indented at the same column as the start of the SQL
76 statement. The regexp is created at compile-time. Take a look at the
77 source before changing it. All lines not matching this regexp will be
78 indented by `sql-indent-offset'."
82 (defcustom sql-indent-offset 4
83 "*Offset for SQL indentation."
87 (defcustom sql-indent-maybe-tab nil
88 "If non-nil, call `insert-tab' if `current-column' did not change."
92 (defvar sql-indent-debug nil
93 "If non-nil, `sql-indent-line' will output debugging messages.")
95 (defun sql-indent-is-string-or-comment ()
96 "Return nil if point is not in a comment or string; non-nil otherwise."
97 (let ((parse-state (syntax-ppss)))
98 (or (nth 3 parse-state) ; String
99 (nth 4 parse-state))) ; Comment
102 (defun sql-indent-get-last-line-start ()
103 "Find the last non-blank line. Return the beginning position of that line and its indentation."
108 (while (and (not (bobp))
110 (looking-at "^\\s-*$")
111 (sql-indent-is-string-or-comment)) ; Skip comments or strings
115 (list (point) (current-indentation))
119 (defun sql-indent-level-delta (&optional prev-start prev-indent)
120 "Calculate the change in level from the previous non-blank line.
121 Given the optional parameter `PREV-START' and `PREV-INDENT', assume that to be
122 the previous non-blank line.
123 Return a list containing the level change and the previous indentation."
126 ;; Go back to the previous non-blank line
127 (let* ((p-line (cond ((and prev-start prev-indent)
128 (list prev-start prev-indent))
129 ((sql-indent-get-last-line-start))))
130 (curr-start (progn (beginning-of-line)
132 (paren (nth 0 (parse-partial-sexp (nth 0 p-line) curr-start))))
134 ;; Add opening or closing parens.
135 ;; If the current line starts with a keyword statement (e.g. SELECT, FROM, ...) back up one level
136 ;; If the previous line starts with a keyword statement then add one level
140 (if (progn (goto-char (nth 0 p-line))
141 (looking-at sql-indent-first-column-regexp))
144 (if (progn (goto-char curr-start)
145 (looking-at sql-indent-first-column-regexp))
154 (defun sql-indent-buffer ()
155 "Indent the buffer's SQL statements."
158 (beginning-of-buffer)
163 (indent (if (looking-at "^\\s-*$")
165 (current-indentation)))
169 (while (/= (point) (point-max))
173 (sql-indent-level-delta start indent)
175 (setq level (nth 0 vals)
179 (max 0 ; Make sure the indentation is at least to column 0
187 (setq line (1+ line))
188 (message "Line %3d; level %3d; indent was %3d; at %d" line level indent (point))))
191 (if (and (not (looking-at "^\\s-*$")) ; Leave blank lines alone
192 (not (sql-indent-is-string-or-comment)) ; Don't mess with comments or strings
193 (/= this-indent (current-indentation))) ; Don't change the line if already ok.
195 (indent-line-to this-indent)
204 (defun sql-indent-line ()
205 "Indent current line in an SQL statement."
208 (indent-info (sql-indent-level-delta))
209 (level-delta (nth 0 indent-info))
210 (prev-indent (nth 1 indent-info))
211 (this-indent (max 0 ; Make sure the indentation is at least 0
214 (nth 0 indent-info)))))
218 (message "SQL Indent: level delta: %3d; prev: %3d; this: %3d"
219 level-delta prev-indent this-indent))
225 (if (and (not (looking-at "^\\s-*$")) ; Leave blank lines alone
226 (not (sql-indent-is-string-or-comment)) ; Don't mess with comments or strings
227 (/= this-indent (current-indentation))) ; Don't change the line if already ok.
228 (indent-line-to this-indent))
233 (add-hook 'sql-mode-hook
235 (make-local-variable 'indent-line-function)
236 (setq indent-line-function 'sql-indent-line))))
238 (provide 'sql-indent)
240 ;;; sql-indent.el ends here