]> git.donarmstrong.com Git - lib.git/blob - emacs_el/sql-indent.el
fix missing ) for org-mode
[lib.git] / emacs_el / sql-indent.el
1 ;;; sql-indent.el --- indentation of SQL statements
2
3 ;; Copyright (C) 2000  Alex Schroeder
4
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 $  
9
10 ;; Keywords: languages
11 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?SqlIndent
12
13 ;; This file is not part of GNU Emacs.
14
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)
18 ;; any later version.
19
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.
24
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.
29
30 ;;; Commentary:
31
32 ;; Indent SQL statements.
33
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.
37
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
41 ;; .emacs:
42
43 ;; (eval-after-load "sql"
44 ;;   '(load-library "sql-indent"))
45
46 ;; Thanks:
47 ;; Arcady Genkin <antipode@thpoon.com>
48
49
50 ;;; History:
51 ;; 2009-03-22*
52 ;;     * mhenry
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'
57
58 ;;; Code:
59
60 (require 'sql)
61
62 ;; Need the following to allow GNU Emacs 19 to compile the file.
63 (require 'regexp-opt)
64
65 (defcustom sql-indent-first-column-regexp
66   (concat "^\\s-*" (regexp-opt '(
67                                  "select" "update" "insert" "delete"
68                                  "union" "intersect"
69                                  "from" "where" "into" "group" "having" "order"
70                                  "set"
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'."
79   :type 'regexp
80   :group 'SQL)
81
82 (defcustom sql-indent-offset 4
83   "*Offset for SQL indentation."
84   :type 'number
85   :group 'SQL)
86
87 (defcustom sql-indent-maybe-tab nil
88   "If non-nil, call `insert-tab' if `current-column' did not change."
89   :type 'boolean
90   :group 'SQL)
91
92 (defvar sql-indent-debug nil
93   "If non-nil, `sql-indent-line' will output debugging messages.")
94
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
100   )
101
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."
104
105  (save-excursion
106    (forward-line -1)
107
108    (while (and (not (bobp))
109                (or
110                 (looking-at "^\\s-*$")
111                 (sql-indent-is-string-or-comment)) ; Skip comments or strings
112                )
113
114      (forward-line -1))
115    (list (point) (current-indentation))
116    )
117  )
118
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."
124
125   (save-excursion
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)
131                               (point)))
132            (paren (nth 0 (parse-partial-sexp (nth 0 p-line) curr-start))))
133
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
137
138       (list
139        (+ paren
140           (if (progn (goto-char (nth 0 p-line))
141                      (looking-at sql-indent-first-column-regexp))
142               1
143             0)
144           (if (progn (goto-char curr-start)
145                      (looking-at sql-indent-first-column-regexp))
146               -1
147             0)
148           )
149        (nth 1 p-line))
150       )
151     )
152   )
153
154 (defun sql-indent-buffer ()
155   "Indent the buffer's SQL statements."
156   (interactive)
157   (save-excursion
158     (beginning-of-buffer)
159     (let*
160         ((line 0)
161          (level 0)
162          (start (point))
163          (indent (if (looking-at "^\\s-*$")
164                      0
165                    (current-indentation)))
166          (this-indent 0)
167          (vals '()))
168
169       (while (/= (point) (point-max))
170         (forward-line)
171
172         (setq vals
173               (sql-indent-level-delta start indent)
174               )
175         (setq level  (nth 0 vals)
176               indent (nth 1 vals))
177
178         (setq this-indent
179               (max 0       ; Make sure the indentation is at least to column 0
180                    (* sql-indent-offset
181                       (if (< level 0)
182                           0
183                         level))))
184
185         (if sql-indent-debug
186             (progn
187               (setq line (1+ line))
188               (message "Line %3d; level %3d; indent was %3d; at %d" line level indent (point))))
189         
190         (beginning-of-line)
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.
194
195             (indent-line-to this-indent)
196           )
197
198         (end-of-line)
199         )
200       )
201     )
202   )
203
204 (defun sql-indent-line ()
205   "Indent current line in an SQL statement."
206   (interactive)
207   (let* ((pos (point))
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
212                            (+ prev-indent
213                               (* sql-indent-offset
214                                  (nth 0 indent-info)))))
215          )
216
217     (if sql-indent-debug
218         (message "SQL Indent: level delta: %3d; prev: %3d; this: %3d"
219                  level-delta prev-indent this-indent))
220
221     (save-excursion
222
223       (beginning-of-line)
224
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))
229       )
230     )
231   )
232
233 (add-hook 'sql-mode-hook
234           (function (lambda ()
235                       (make-local-variable 'indent-line-function)
236                       (setq indent-line-function 'sql-indent-line))))
237
238 (provide 'sql-indent)
239
240 ;;; sql-indent.el ends here