-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql-indent.el
240 lines (193 loc) · 7.81 KB
/
sql-indent.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
;;; sql-indent.el --- indentation of SQL statements
;; Copyright (C) 2000 Alex Schroeder
;; Authors: Alex Schroeder <[email protected]>
;; Matt Henry <[email protected]>
;; Maintainer: Matt Henry <[email protected]>
;; Version: $Id: sql-indent.el,v 1.10 2009/03/25 22:52:25 mhenry Exp $
;; Keywords: languages
;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlIndent
;; This file is not part of GNU Emacs.
;; This is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Indent SQL statements.
;; As the indentation of SQL statements depends not only on the previous
;; line but also on the current line, empty lines cannot always be
;; indented correctly.
;; Usage note: Loading this file will make all SQL mode buffers created
;; from then on use `sql-indent-line' for indentation. A possible way
;; to install sql-indent.el would be to add the following to your
;; .emacs:
;; (eval-after-load "sql"
;; '(load-library "sql-indent"))
;; Thanks:
;; Arcady Genkin <[email protected]>
;;; History:
;; 2009-03-22*
;; * mhenry
;; Added `sql-indent-buffer' for efficient full buffer processing.
;; Modified `sql-indent' to be savvy to comments and strings.
;; Removed "and", "or" and "exists" from `sql-indent-first-column-regexp'
;; Added "create", "drop" and "truncate" to `sql-indent-first-column-regexp'
;;; Code:
(require 'sql)
;; Need the following to allow GNU Emacs 19 to compile the file.
(require 'regexp-opt)
(defcustom sql-indent-first-column-regexp
(concat "^\\s-*" (regexp-opt '(
"select" "update" "insert" "delete"
"union" "intersect"
"from" "where" "into" "group" "having" "order"
"set"
"create" "drop" "truncate"
"--") t) "\\(\\b\\|\\s-\\)")
"Regexp matching keywords relevant for indentation.
The regexp matches lines which start SQL statements and it matches lines
that should be indented at the same column as the start of the SQL
statement. The regexp is created at compile-time. Take a look at the
source before changing it. All lines not matching this regexp will be
indented by `sql-indent-offset'."
:type 'regexp
:group 'SQL)
(defcustom sql-indent-offset 4
"*Offset for SQL indentation."
:type 'number
:group 'SQL)
(defcustom sql-indent-maybe-tab nil
"If non-nil, call `insert-tab' if `current-column' did not change."
:type 'boolean
:group 'SQL)
(defvar sql-indent-debug nil
"If non-nil, `sql-indent-line' will output debugging messages.")
(defun sql-indent-is-string-or-comment ()
"Return nil if point is not in a comment or string; non-nil otherwise."
(let ((parse-state (syntax-ppss)))
(or (nth 3 parse-state) ; String
(nth 4 parse-state))) ; Comment
)
(defun sql-indent-get-last-line-start ()
"Find the last non-blank line. Return the beginning position of that line and its indentation."
(save-excursion
(forward-line -1)
(while (and (not (bobp))
(or
(looking-at "^\\s-*$")
(sql-indent-is-string-or-comment)) ; Skip comments or strings
)
(forward-line -1))
(list (point) (current-indentation))
)
)
(defun sql-indent-level-delta (&optional prev-start prev-indent)
"Calculate the change in level from the previous non-blank line.
Given the optional parameter `PREV-START' and `PREV-INDENT', assume that to be
the previous non-blank line.
Return a list containing the level change and the previous indentation."
(save-excursion
;; Go back to the previous non-blank line
(let* ((p-line (cond ((and prev-start prev-indent)
(list prev-start prev-indent))
((sql-indent-get-last-line-start))))
(curr-start (progn (beginning-of-line)
(point)))
(paren (nth 0 (parse-partial-sexp (nth 0 p-line) curr-start))))
;; Add opening or closing parens.
;; If the current line starts with a keyword statement (e.g. SELECT, FROM, ...) back up one level
;; If the previous line starts with a keyword statement then add one level
(list
(+ paren
(if (progn (goto-char (nth 0 p-line))
(looking-at sql-indent-first-column-regexp))
1
0)
(if (progn (goto-char curr-start)
(looking-at sql-indent-first-column-regexp))
-1
0)
)
(nth 1 p-line))
)
)
)
(defun sql-indent-buffer ()
"Indent the buffer's SQL statements."
(interactive)
(save-excursion
(beginning-of-buffer)
(let*
((line 0)
(level 0)
(start (point))
(indent (if (looking-at "^\\s-*$")
0
(current-indentation)))
(this-indent 0)
(vals '()))
(while (/= (point) (point-max))
(forward-line)
(setq vals
(sql-indent-level-delta start indent)
)
(setq level (nth 0 vals)
indent (nth 1 vals))
(setq this-indent
(max 0 ; Make sure the indentation is at least to column 0
(* sql-indent-offset
(if (< level 0)
0
level))))
(if sql-indent-debug
(progn
(setq line (1+ line))
(message "Line %3d; level %3d; indent was %3d; at %d" line level indent (point))))
(beginning-of-line)
(if (and (not (looking-at "^\\s-*$")) ; Leave blank lines alone
(not (sql-indent-is-string-or-comment)) ; Don't mess with comments or strings
(/= this-indent (current-indentation))) ; Don't change the line if already ok.
(indent-line-to this-indent)
)
(end-of-line)
)
)
)
)
(defun sql-indent-line ()
"Indent current line in an SQL statement."
(interactive)
(let* ((pos (point))
(indent-info (sql-indent-level-delta))
(level-delta (nth 0 indent-info))
(prev-indent (nth 1 indent-info))
(this-indent (max 0 ; Make sure the indentation is at least 0
(+ prev-indent
(* sql-indent-offset
(nth 0 indent-info)))))
)
(if sql-indent-debug
(message "SQL Indent: level delta: %3d; prev: %3d; this: %3d"
level-delta prev-indent this-indent))
(save-excursion
(beginning-of-line)
(if (and (not (looking-at "^\\s-*$")) ; Leave blank lines alone
(not (sql-indent-is-string-or-comment)) ; Don't mess with comments or strings
(/= this-indent (current-indentation))) ; Don't change the line if already ok.
(indent-line-to this-indent))
)
)
)
(add-hook 'sql-mode-hook
(function (lambda ()
(make-local-variable 'indent-line-function)
(setq indent-line-function 'sql-indent-line))))
(provide 'sql-indent)
;;; sql-indent.el ends here