add lisp packages

This commit is contained in:
2020-12-05 21:29:49 +01:00
parent 85e20365ae
commit a6e2395755
7272 changed files with 1363243 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
name: CI
on:
pull_request:
push:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
emacs_version:
- 26.3
steps:
- uses: purcell/setup-emacs@master
with:
version: ${{ matrix.emacs_version }}
- uses: actions/checkout@v1
- name: Byte Compile Files
run: 'emacs -batch -Q --no-site-file -L . -f batch-byte-compile *.el'
- name: Run Tests
run: 'emacs -batch -Q --no-site-file -L . -l sql-indent-test.el -f ert-run-tests-batch-and-exit'

View File

@@ -0,0 +1,52 @@
# If you have a question or found a bug
To ask a question about the package, please please [create an
issue][gh-issue-link] containing a description of what you are trying to do
and consider providing sample SQL code if appropriate. There is also a
[manual](./sql-indent.org) which provides information about customizing the
indentation rules.
If you found a bug in the SQL indentation code, or don't know how to configure
the indentation rules to suit your preferences, please [create an
issue][gh-issue-link]. Please provide a sample SQL code snippet that
demonstrates the problem.
# Submitting code changes
The preferred way to accept contributions is to submit a pull request using
GitHub. Before doing so, you need to be aware of the copyright assignment
requirements and the automated test-suite. These are detailed in the sections
below.
## Copyright Assignment
This package is part of [GNU ELPA][elpa-link] and it is subject to the GNU
[Copyright Assignment][copy-papers-link] policy. Any [legally
significant][legally-link] contributions can only be accepted after the author
has completed their paperwork. Please see [the request form][request-link] if
you want to proceed with the assignment.
## Automated test suite
There's an automated test suite which is used to ensure we don't re-introduce
bugs that that were already fixed. If you fix the problem with the
indentation, please provide an automated test for your fixes and add it to the
test suite. The "Commentary" section in the
[sql-indent-test.el](./sql-indent-test.el) file contains a description on how
to add and run tests.
## Other considerations for the pull request
In your pull request, please provide a clear description of what the changes
do and add a sample SQL snippet that illustrates the problem being solved.
Once you submit the pull request, an automated build will start and will run
the unit tests, please verify that the build succeeds and fix any issues if
the build failed.
[elpa-link]: http://elpa.gnu.org/packages/
[copy-papers-link]: http://www.gnu.org/prep/maintain/html_node/Copyright-Papers.html
[legally-link]: http://www.gnu.org/prep/maintain/html_node/Legally-Significant.html#Legally-Significant
[request-link]: http://git.savannah.gnu.org/cgit/gnulib.git/tree/doc/Copyright/request-assign.future
[gh-issue-link]: https://github.com/alex-hhh/emacs-sql-indent/issues

998
lisp/sql-indent/ChangeLog Normal file
View File

@@ -0,0 +1,998 @@
2019-06-20 Alex Harsanyi <AlexHarsanyi@gmail.com>
Update version to 1.4
2019-06-08 Alex Harsanyi <AlexHarsanyi@gmail.com>
Recognize left and right outer join statements #85
* sql-indent.el (sqlind-find-join-start): new defun to look for join
statements, can find "left outer join" statements as well, see #85
(sqlind-syntax-in-select, sqlind-refine-syntax): use
`sqlind-find-join-start`
(sqlind-lineup-joins-to-anchor): only look for the first join keyword,
to detect the case where they un on multiple lines
2019-04-18 Alex Harsanyi <AlexHarsanyi@gmail.com>
Recognize more types of begin transaction statements (fixes #84)
"begin" follwed by a semicolon, "begin work" and "begin transaction" are
considered statements and not block start delimters.
2019-03-31 Alex Harsanyi <AlexHarsanyi@gmail.com>
Don't recognize select keywords in comments, fixes #83
when searching for select keywords for anchors, don't stop inside
comments -- this just uses `sqlind-search-backward`, which makes sure
that the position that is found is at the same level as the starting
position.
Also fixed a bug in `sqlind-same-level-statement` which missed that the
anchor was inside a comment.
2019-03-15 Alex Harsanyi <AlexHarsanyi@gmail.com>
recognize elseif in addition to elif #81
2019-03-15 Alex Harsanyi <AlexHarsanyi@gmail.com>
don't be confused by drop function or procedure statements #80
2019-03-10 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Apply advices of `checkdoc' (#77)
* sql-indent.el: spelling and other documentation fixes.
2019-03-10 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Fix `sqlind-comment-end' regexp. (#79)
* sql-indent.el: Fix the regexp detected by
trawl (https://github.com/mattiase/trawl).
2019-03-01 Alex Harsanyi <AlexHarsanyi@gmail.com>
also fix sqlind-end-statement-regexp
2019-03-01 Alex Harsanyi <AlexHarsanyi@gmail.com>
Fix regexp bug in `sqlind-good-if-candidate` (fixes #76)
2019-02-20 Alex Harsányi <alex-hhh@users.noreply.github.com>
Recognize more create keywords (#75)
2018-12-13 Alex Harsányi <alex-hhh@users.noreply.github.com>
Recognize FULL JOIN, UNION ALL and EXCEPT keywords #73 (#74)
* sql-indent.el (sqlind-select-clauses-regexp): match "UNION
ALL" and
"EXCEPT" as select clauses.
(sqlind-select-join-regexp): match FULL JOIN keywords
(sqlind-lineup-joins-to-anchor): use `sqlind-select-join-regexp`
to
match all join keywords.
2018-12-13 Alex Harsányi <alex-hhh@users.noreply.github.com>
Recognize `select-join-condition` syntax inside nested statements #70
(#71)
Updated syntax parser to recognize some JOIN condition keywords as
`select-join-condition` syntax, even inside nested statements.
Updated some non-related tests (m, pr17 and pr68), as this syntax was
not
recognized when these tests were written. While this change will
modify the
indentation in such cases, it allows more refined control
over the indention.
* sql-indent.el (sqlind-join-condition-regexp): keywords that
constitute JOIN conditions
(sqlind-syntax-in-select): match `sqlind-join-condition-regexp`
when
looking for join condition keywords
(sqlind-refine-syntax): try to refine a
`nested-statement-continuation into a `select-join-condition` if
there
is a JOIN in the nested statement.
2018-12-11 Alex Harsányi <alex-hhh@users.noreply.github.com>
`sqlind-search-backward` finds things at the same nesting level (#68)
(#69)
* sql-indent.el (sqlind-search-backward): use
`sqlind-same-level-statement` to check if the position is at the
same
nesting level as the starting point
* (sqlind-same-level-statement): fix to check that the point has the
same nesting, in-comment and in-string status as start.
2018-10-25 Alex Harsanyi <AlexHarsanyi@gmail.com>
update version to 1.3
2018-10-23 Alex Harsányi <alex-hhh@users.noreply.github.com>
Fix up bad detection of $$ delimiters in PostgreSQL dialect (#67)
this resulted in severe performance degradation for the indent function
and
bad anchor for the $$ blocks.
* sql-indent.el (sqlind-maybe-create-statement): strip off any
parens
off the function name.
(sqlind-maybe-$$-statement): re-wrote the function to avoid
calling
`sqlind-beginning-of-block` recursively.
(sqlind-start-block-regexp): fixup $$ matching
2018-10-09 Alex Harsanyi <AlexHarsanyi@gmail.com>
don't recognize "begin transaction" as a block
"begin transaction;" is a statement, and the code used to confuse the
"begin" with a block begin, breaking some indentation.
2018-07-12 Alex Harsányi <alex-hhh@users.noreply.github.com>
recognize implicit JOIN clauses (fixes #64) (#65)
... these are JOIN clauses without an explicit INNER, LEFT, RIGHT, etc.
2018-05-07 Stefan Monnier <monnier@iro.umontreal.ca>
* sql-indent.el: Add `package-requires` for cl-lib
2018-05-06 Alex Harsanyi <AlexHarsanyi@gmail.com>
update to version 1.2
2018-02-26 Alex Harsányi <alex-hhh@users.noreply.github.com>
Recognize MySQL optional clauses, #60 (#61)
(sqlind-maybe-skip-mysql-create-options): skip over optional clauses
between
CREATE and the thing being created -- this allows correct
identification of
the create statement
(sqlind-maybe-create-statement): skip over any "if not exist" keywords
between
the thing being created and its name -- this allows correct
identification of
the view or table name.
(sqlind-refine-syntax): refine SELECT statements inside table
declarations, as
in "CREATE TABLE AS SELECT..."
also recognize tabs as whitespace syntax in various regexps.
add tests, also update the `if-exists.sql` test as the view name is
correctly
determined now.
2018-02-13 Stefan Monnier <monnier@iro.umontreal.ca>
* sql-indent.el: Don't require cc-defs.
(sqlind-indent-comment-start): Use line-end-position.
(sqlind-indent-select-column, sqlind-indent-select-table)
(sqlind-lineup-to-clause-end): Use eolp.
2018-02-11 Stefan Monnier <monnier@iro.umontreal.ca>
sql-indent.el: Fix Author: email format
2018-01-30 Alex Harsanyi <AlexHarsanyi@gmail.com>
Add .elpaignore file
Files and patterns listed here will be ignored when the ELPA package is
assembled.
2018-01-28 Alex Harsányi <alex-hhh@users.noreply.github.com>
Fix travis build. (#58)
See also
* https://github.com/travis-ci/travis-ci/issues/9061
* https://github.com/flycheck/emacs-travis/issues/13
2018-01-28 Alex Harsanyi <AlexHarsanyi@gmail.com>
Update sql-indent.el to version 1.1
Updated / clarified some of the documentation as well
2018-01-06 Alex Harsanyi <AlexHarsanyi@gmail.com>
add explanation on how `sqlind-beginning-of-block` works
2018-01-06 Alex Harsányi <alex-hhh@users.noreply.github.com>
Recognize PostgreSQL $$ quote syntax, #54 (#55)
Function bodies in PostgreSQL are defined between '$$' markers. The
code now
recognizes these as begin/end statements. There's a test
file,
`test-data/pr54.sql`, specifying the cases that are correctly detected.
PostgreSQL support likely needs more work.
2018-01-04 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Fix various typos here and there. (#57)
2018-01-04 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Fix typo in sql-indent.org. (#56)
2018-01-03 Alex Harsanyi <AlexHarsanyi@gmail.com>
Updated documentation
* Simplified CONTRIBUTING.md -- removed information that is
available
elsewhere
* Fixed a type in the user manual, `sql-indent.org` specifying the
wrong hook
name for the customization
* Clarified test naming conventions in `sql-indent-test.el`
2018-01-03 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Correct indentation of column continuation with operators. (#53)
* Correct indentation of column continuation with operators.
Correct from
```sql
select my_col || chr(10)
|| '-*-' data_type,
x
from dual;
```
to
```sql
select my_col || chr(10)
|| '-*-' data_type,
x
from dual;
```
* sql-indent.el
(sqlind-column-definition-start): find the first column of the
statement.
(sqlind-use-anchor-indentation): use the indentation of the anchor
instead of the indentation of the line containing the anchor.
(sqlind-adjust-operator): search the column start in
select-column-continuation.
* Add test for PR#53.
* Simplify code.
(sqlind-column-definition-start): avoid unnecessary (unless (while
...)).
(sqlind-adjust-operator): avoid complex goto-char.
2017-12-16 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Correct the adjustment of indentation for operators in select (#52)
* Correct the adjustment of indentation for operators in select
With this fix the following statement is propely indented
``` sql
select mt.myVar
|| '-*-' myVarLabel,
myVar2
from myTable mt;
```
instead of
``` sql
select mt.myVar
|| '-*-' myVarLabel,
myVar2
from myTable mt;
```
* sql-indent.el: Use sqlind syntax table in adjustments functions.
(sqlind-adjust-operator): Use sqlind syntax table.
* Add test to pr52
2017-12-16 Alex Harsányi <alex-hhh@users.noreply.github.com>
Add CONTRIBUTING.md file (#51)
2017-12-12 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Add new indentation function for select columns. (#50)
* Add new indentation function for select columns.
* sql-indent-left.el: Improve indentation of select columns
(sqlind-indent-select-keywords): tweak indentation after DISTINCT,
UNIQUE or TOP keywords.
* Add tests for pr50, correct sqlind-indentation-left-offsets-alist.
* sql-indent-left.el: correct sqlind-indent-select-keywords
(sqlind-indent-select-keywords): avoid error when no keyword found.
* sql-indent-test.el: add test for pr50.
* Add `sqlind-indent-select-column-alt'.
Clone `sqlind-indent-select-column' into
`sqlind-indent-select-column-alt'
to allow alignment of select colums
with KEYWORDS.
* sql-indent-left.el: Change setup to use
`sqlind-indent-select-column-alt'
(sqlind-indent-select-column-alt): extends
`sqlind-indent-select-column'.
* Checkdoc style corrections.
sql-indent-left.el: apply chechdoc advices.
2017-12-02 Alex Harsanyi <AlexHarsanyi@gmail.com>
Update README with instructions on how to install from GNU ELPA
* README.md: add instructions for installing from GNU ELPA
* sql-indent.el: reference `sql-indent.org` in the commentary section
2017-11-30 Stefan Monnier <monnier@iro.umontreal.ca>
Fix up copyrights for GNU ELPA, plus minor tweaks
* .gitignore: Add -autoloads and -pkg files.
* sql-indent-test.el, sql-indent-left.el: Fix copyright header.
* sql-indent.el: Fix copyright header. Don't require `align`.
(sqlind-minor-mode-map): New keymap.
(sqlind-minor-mode): Don't modify sql-mode's keymap. Use
kill-local-variable.
2017-10-13 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Correct detection of labels in `sqlind-maybe-loop-statement'. (#49)
Correct detection of labels in `sqlind-maybe-loop-statement'
2017-10-09 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Correct detection of labels in `for` or `while` loops. (#48)
Correct detection of labels in `for` or `while` loops.
2017-09-21 Alex Harsanyi <AlexHarsanyi@gmail.com>
Introduce a nested-statement-close syntax
This is recognized inside a nested statement when the line to be
indented starts with a closing bracket. This is intended to replace
`sqlind-lineup-close-paren-to-open` and
`sqlind-lineup-close-paren-to-open-indentation` indentation helpers
which had to be added to too many syntactic symbols.
With the new syntax symbol, using `sqlind-use-anchor-indentation` will
lineup hanging close brackets with the start of the statement and
`sqlind-use-anchor-indentation` will line them up with the actual
opening bracket itself.
2017-09-19 Alex Harsanyi <AlexHarsanyi@gmail.com>
new alignment rule for issue #47
2017-09-19 Alex Harsanyi <AlexHarsanyi@gmail.com>
fixup refining a nested-statement-continuation (#46)
when the nested statement continuation had a comment at the start, any
select statements inside it were missed.
2017-09-18 Brendan Rocks <brendan-r@users.noreply.github.com>
Minor documentation tweaks (#44)
Fix installation command in documentation and other typos
`install-package-from-buffer` makes more linguistic sense, but
`package-install-from-buffer`, seems to be the appropriate
command
(Emacs 25.2)
2017-09-18 Alex Harsanyi <AlexHarsanyi@gmail.com>
new indentation helper function and clarified documentation
added `sqlind-lineup-close-paren-to-open-indentation` function to assist
with issue #43
2017-08-19 Alex Harsányi <alex-hhh@users.noreply.github.com>
Don't be confused by names that contain keywords (#42)
Don't be confused by names that contain keywords
for example, the "FOR" in "V_SPORT_ZONE_FOR_SESSION" is not a keyword.
* Add test cases for PR42
2017-08-19 Alex Harsanyi <AlexHarsanyi@gmail.com>
Fix example in documentation
2017-08-09 Alex Harsányi <alex-hhh@users.noreply.github.com>
Travis build and test support (#41)
Add Travis CI support to the repository
2017-08-09 Alex Harsanyi <AlexHarsanyi@gmail.com>
Recognize MsSQL directives and declarations (#40)
2017-08-02 Alex Harsanyi <AlexHarsanyi@gmail.com>
Fix detection of select column start and LIMIT keyword #39
* sql-indent.el (sqlind-column-definition-start): use LIMIT as the
bounds for `sqlind-same-level-statement`, so we search between the start
of the SELECT and the current point.
(sqlind-select-clauses-regexp): recognize LIMIT as a select clause
keyword.
2017-07-30 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Correct display of lighter when minor-mode is enabled. (#38)
Add an space before text to avoid it to stick on previous word in
modeline.
2017-07-24 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Allow detection of user name in package, procedure or function. (#37)
* Allow detection of user name in package, procedure or function.
Avoid `mismatched end label for package (1 104)` message whith and sql
of this form:
```sql
create or replace package my_user.my_wonderfull_package as
function my_amazing_func return number;
end my_wonderfull_package;
/
```
* Add tests to PR #37.
2017-07-21 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Try to correct 'bad closing for begin block' message. (#36)
Detection of for .. loop inside another statement
see "test-data/pr36.sql" for an example.
2017-07-15 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Add %type, %rowtype and table prefix in function's return type. (#33)
* Add %type, %rowtype and table prefix in function's return type.
Allow correct indenting after and function declaration like bellow:
declare
function dummy return my_user.my_table%rowtype;
function dummy2 return my_user.my_table.my_col%type;
var dummy3 number;
begin
null;
end;
* Add tests cases for PR #33.
2017-07-15 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Improve left indentation of case statement. (#34)
2017-06-29 Alex Harsanyi <AlexHarsanyi@gmail.com>
Don't be fooled by "if exists" statements
"IF" keywords in things like "drop index if exists" statements are no
longer considered statement starts.
2017-06-29 Alex Harsányi <alex-hhh@users.noreply.github.com>
Performance improvements (#31)
* Establish a limit for syntactic information search
See also issue #26
`sqlind-syntax-of-line' will use `sqlind-begining-of-directive' to
establish a
search limit for the current syntax. This avoids calling
`sqlind-begining-of-directive' repeatedly.
This changes the indentation anchor for toplevel statements when
directives
are present in the file. Unit test data updated
accordingly.
* sql-indent.el (sqlind-search-limit): new variable
(sqlind-beginning-of-statement-1): avoid searching before
`sqlind-search-limit'
(sqlind-beginning-of-statement): use `sqlind-search-limit'
(sqlind-maybe-loop-statement): fix mispelling of throw symbol
(sqlind-beginning-of-block): limit search to `sqlind-search-limit'
(sqlind-syntax-of-line): bind `sqlind-search-limit' to
`sqlind-begining-of-directive'
* Fix spelling of `sqlind-beginning-of-directive`
2017-06-20 Alex Harsanyi <AlexHarsanyi@gmail.com>
Performance improvements (#26)
* sql-indent.el (sqlind-backward-syntactic-ws)
(sqlind-forward-syntactic-ws, sqlind-search-backward): rewrite code to
avoid catch/throw forms
(sqlind-match-string): make it a `defsubst'
(sqlind-beginning-of-statement): avoid calling
`sqlind-begining-of-directive' when not needed
(sqlind-syntax-of-line): avoid unnecessary goto-char calls, and replace
`parse-partial-sexp' with `syntax-ppss'
2017-06-15 Alex Harsanyi <AlexHarsanyi@gmail.com>
Moved most of the README.md contents into sql-indent.org
... this is the beginning of a sql-ident manual. The ORG format should
make it easier to browse in Emacs, and Github also renders it quite
nicely.
2017-06-15 Alex Harsanyi <AlexHarsanyi@gmail.com>
Avoid infinite loop in `sqlind-syntax-in-case`
Updated the test case with code that triggered the bug
2017-06-15 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Detect end of exception block correctly (#30)
2017-06-13 Alex Harsanyi <AlexHarsanyi@gmail.com>
fix anchor point for case statement syntax (#29)
code that was looking for the "when" keyword was moving the point,
resulting in the incorrect anchor point being used for '(in-block case
...) syntax. Updated tests to reflect fixed code.
Also added `sqlind-syntax-keyword` helper function, to be used by
indentation functions.
2017-06-13 Alex Harsanyi <AlexHarsanyi@gmail.com>
Fix exception being a type as well as a keyword (#28)
"exception" is both a keyword and a datatype. Add code to differentiate
between the two. Only the keyword is a block start. Updated test files
to reflect that.
2017-06-12 Alex Harsanyi <AlexHarsanyi@gmail.com>
fix syntax detection in exception blocks and case statements (#28 and
#29)
Exception statements with multiple when clauses are correctly recognized
and classified. Case statements with multi-lone when clauses are also
correctly identified. Added unit tests for both cases.
Some refactoring was necessary to allow recursive syntax refinement
(`sqlind-refine-syntax`)
2017-06-12 Alex Harsanyi <AlexHarsanyi@gmail.com>
Fix incorrect match for end keyword in sqlind-end-statement-regexp
2017-06-08 Alex Harsanyi <AlexHarsanyi@gmail.com>
Fix bug with wrong regexp and throw clause
a wrong "end" keyword regexp would match non-keywords that ended with
the string "end", causing problems
also fixes a misspelling for a throw clause
2017-06-08 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Various minor improvements on left align SQL statments. (#22)
* Various minor improvements on left align SQL statments.
* Update pr17-io-left after updating sql-indent-left setup.
2017-06-03 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Remove execution bits on sql-indent-left.el (#25)
2017-06-03 Alex Harsanyi <AlexHarsanyi@gmail.com>
Recognize assignments as statement starts (#24)
2017-06-01 Alex Harsanyi <AlexHarsanyi@gmail.com>
Update test for issue #19 to be more comprehensive
2017-06-01 Alex Harsanyi <AlexHarsanyi@gmail.com>
Merge branch 'master' of github.com:alex-hhh/emacs-sql-indent
2017-06-01 Alex Harsanyi <AlexHarsanyi@gmail.com>
Update unit test suite to run independent of user settings
Previously, the test suite depended on whether the tabs are expanded
into spaces or not and other user settings.
2017-05-31 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Exclude equals inside strings from alignments. (#20)
Update align rules to ignore equal signs inside strings (#20)
2017-05-31 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Add a rule to align pipes (||). (#21)
Align rules for string concatenation operators (#21)
2017-05-30 Alex Harsanyi <AlexHarsanyi@gmail.com>
Correctly recognize the begin block of a package (#19)
2017-05-29 Alex Harsanyi <AlexHarsanyi@gmail.com>
Correctly detect syntax for cursor statements (fix #19)
Cursors are correctly detected as beginning of a statement. Also an
optional type for the cursor is recognized correctly.
Also fixed block navigation for package names (so package end statements
are correctly recognized)
Added tests for the above cases.
2017-05-26 Alex Harsanyi <AlexHarsanyi@gmail.com>
Identify syntax for CASE expressions in assignment (fixes #18)
Also added unit tests that shows/tests the types of CASE expressions
that can show up in PL/SQL.
2017-05-25 Alex Harsanyi <AlexHarsanyi@gmail.com>
use cl-lib instead of cl and provide some helpers
the cl library is deprecated in Emacs, to be replaced with cl-lib (which
has the same functions, but with the "cl-" prefix)
Also provided some helper functions to access syntactic context,
hopefully this will simplify the code somewhat.
2017-05-24 Alex Harsanyi <AlexHarsanyi@gmail.com>
Fix checkdoc errors in sql-indent.el
2017-05-24 Alex Harsanyi <AlexHarsanyi@gmail.com>
moved contents of customize-indentation.md into README.md
2017-05-24 Alex Harsanyi <AlexHarsanyi@gmail.com>
Recognize syntax of CASE statements which are not nested
Based on an example in https://github.com/bsvingen/sql-indent/issues/1
Also added test case for this syntax type.
2017-05-24 Alex Harsanyi <AlexHarsanyi@gmail.com>
Add test cases for sample indentation mode
Based on a sample SQL indentation example provided by the SQL mode
maintainer.
2017-05-24 Alex Harsanyi <AlexHarsanyi@gmail.com>
updated .gitignore
2017-05-23 Alex Harsanyi <AlexHarsanyi@gmail.com>
Add a test suite for sql-indent
2017-05-23 Alex Harsanyi <AlexHarsanyi@gmail.com>
Updated installation and customization documentation
2017-05-23 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Fix indentation.
Previous version didn't work.
2017-05-23 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Compilation error ...
2017-05-23 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Correct indentation of function's declaration in package specs.
Detect end of function declaration in package spec.
2017-05-22 Alex Harsanyi <AlexHarsanyi@gmail.com>
sqlind-begining-of-directive -- another approach to disambiguate SET
2017-05-22 Alex Harsanyi <AlexHarsanyi@gmail.com>
sqlind-begining-of-directive -- another approach to disambiguate SET
2017-05-22 Alex Harsanyi <AlexHarsanyi@gmail.com>
sqlind-adjust-operator -- fix up string concatenation cases
The operator correctly determines the position of the left hand operand
for the binary operator. For example the code below is indented
correctly now:
```sql select col1, 'abc def'--comment
|| 'abc'
|| 'def'
```
2017-05-22 Alex Harsanyi <AlexHarsanyi@gmail.com>
Renamed some indentation functions for consistency
* `sqlind-adjust-and-or-left` renamed to
`sqlind-left-justify-logical-operator`
* `sqlind-adjust-and-or-right` renamed to
`sqlind-right-justify-logical-operator`
2017-05-22 Alex Harsanyi <AlexHarsanyi@gmail.com>
silence byte compiler warnings
2017-05-22 Alex Harsanyi <AlexHarsanyi@gmail.com>
sqlind-begining-of-directive -- disambiguate Oracle SET keyword
The SET keyword in Oracle SQLPlus is a directive as well as part of the
SQL UPDATE statement. `sqlind-begining-of-directive' will correctly
identify uses of SET which are directives.
2017-05-20 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Start workgin on aligning keywords left or right. (#14)
Provide sample customization for the SQL indentation engine
Some extra indentation helper functions are also provided and
documented.
2017-05-14 Alex Harsanyi <AlexHarsanyi@gmail.com>
sqlind-beginning-of-statement-1 -- don't look inside comments, fixes #15
2017-05-14 Alex Harsanyi <AlexHarsanyi@gmail.com>
Separate indentation of AND, OR into a separate rule.
2017-05-11 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Use default value of indent-line-function when deactivating minor mode.
As the user could change the value of indent-line-function, we could not
reset it to 'indent-relative.
2017-05-11 Alex Harsanyi <AlexHarsanyi@gmail.com>
Create a `sqlind-minor-mode` to toggle syntactic indentation
Also updated commentary section with customization starting points.
2017-05-10 Alex Harsanyi <AlexHarsanyi@gmail.com>
update alignment rules to handle variable declarations with assignments
This is further work on pull request #13
2017-05-10 Alex Harsanyi <AlexHarsanyi@gmail.com>
Merge branch 'master' of github.com:alex-hhh/emacs-sql-indent
2017-05-10 Alex Harsányi <alex-hhh@users.noreply.github.com>
Merge pull request #13 from PierreTechoueyres/pte/sqlind-align-rules
Add support for assignment and arrow (=>) in align.
2017-05-10 Alex Harsanyi <AlexHarsanyi@gmail.com>
fixup sqlind-lineup-joins-to-anchor
2017-05-10 Alex Harsanyi <AlexHarsanyi@gmail.com>
Add a handful if indentation helpers
They are not used directly, but they can be used to construct custom
indentation tables.
2017-05-10 Alex Harsanyi <AlexHarsanyi@gmail.com>
classify select columns start start with a comma as select-column
This helps with indenting SQL statements that use a comma at the start
of the column, like this:
SELECT col1
,col2
FROM ...
This is also consistent with how we classify select-table syntaxes
2017-05-09 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Add support for assignment and arrow (=>) in align.
Allow to align some keywords in sql statements. Ex:
declare
function dummy(p_param_1 in varchar2,
p_param_2 in out varchar2,
p_param_2 out varchar2)
return boolean;
end dummy;
function dummy_2(p_param_1 out varchar2,
p_param_2 out varchar2,
p_param_2 out varchar2)
return boolean;
function dummy_3(p_param_1 varchar2,
p_param_2 in varchar2,
p_param_2 out varchar2)
return boolean;
var1 boolean := true;
var2 number := 1;
var42 varchar2(1) := 'Y'; begin
if dummy(p_param_1 => val1,
p_param_10 => val10) then
null;
end if; end;
/
2017-05-07 Alex Harsanyi <AlexHarsanyi@gmail.com>
install a separate syntax table for syntax parsing
keep the sql-mode syntax table unchanged, but use a special one during
syntactic parsing. This is done so that navigation over syntactic
elements is simpler for parsing, but stays reasonable while the user
edits the file.
For example, the string "table.column" will be considered one symbol for
indentation purposes, but two symbols for editing (so an M-b will move
to the dot)
2017-05-07 Alex Harsanyi <AlexHarsanyi@gmail.com>
put our own syntax table into sql-mode
2017-05-07 Alex Harsanyi <AlexHarsanyi@gmail.com>
move default indentation offsets in separate constant
`sqlind-indentation-offsets-alist' is now buffer local and it is simpler
to override
2017-05-06 Alex Harsanyi <AlexHarsanyi@gmail.com>
Merge branch 'master' of github.com:alex-hhh/emacs-sql-indent
2017-05-06 Alex Harsanyi <AlexHarsanyi@gmail.com>
Add initial align rules for SQL statements
2017-05-05 Alex Harsányi <alex-hhh@users.noreply.github.com>
Merge pull request #6 from PierreTechoueyres/pte/add-keywors
Add new keywords in `sqlind-sqlplus-directive'.
2017-05-05 Alex Harsanyi <AlexHarsanyi@gmail.com>
don't move cursor when indenting the line
previous simplified too much
2017-05-04 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Add new keywords in `sqlind-sqlplus-directive'.
Add Oracle's keywords to start scripts inside a script.
2017-05-04 Alex Harsanyi <AlexHarsanyi@gmail.com>
Updated README file
2017-05-04 Alex Harsanyi <AlexHarsanyi@gmail.com>
Use `indent-line-to` in `sqlind-indent-line`
2017-03-23 Alex Harsanyi <AlexHarsanyi@gmail.com>
Indent nested if statements correctly, fixes #4
`sqlind-begining-of-block` and `sqlind-begining-of-statement` recognize
IF statements and stop at the right point so that syntactic info is
created correctly.
2017-03-22 Alex Harsanyi <AlexHarsanyi@gmail.com>
fix parsing the "end if" block when an "elsif" is present
2017-01-02 Pierre Téchoueyres <pierre.techoueyres@free.fr>
Add new keywords in `sqlind-sqlplus-directive'.
2016-12-30 Alex Harsanyi <AlexHarsanyi@gmail.com>
update markdown formatting
2016-12-30 Alex Harsanyi <AlexHarsanyi@gmail.com>
Added documentation on how to customize indentation
2016-12-28 Pierre Téchoueyres <pierre.techoueyres@free.fr>

675
lisp/sql-indent/LICENSE Normal file
View File

@@ -0,0 +1,675 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
{one line to give the program's name and a brief idea of what it does.}
Copyright (C) {year} {name of author}
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
{project} Copyright (C) {year} {fullname}
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

47
lisp/sql-indent/README.md Normal file
View File

@@ -0,0 +1,47 @@
# Syntax based indentation for SQL files for GNU Emacs
![CI](https://github.com/alex-hhh/emacs-sql-indent/workflows/CI/badge.svg)
sql-indent.el is a GNU Emacs minor mode which adds support for syntax-based
indentation when editing SQL code: TAB indents the current line based on the
syntax of the SQL code on previous lines. This works like the indentation for
C and C++ code.
The package also defines align rules so that the `align` function works for
SQL statements, see `sqlind-align-rules` for which rules are defined. This
can be used to align multiple lines around equal signs or "as" statements,
like this.
`sqlind-minor-mode` together with the align rules can assist in writing tidy
SQL code or formatting existing SQL code. The indentation rules are
customizable and they can be adapted to match your coding style.
See the [manual](./sql-indent.org) for more details.
# Installation
You can install a released version of this package
from [GNU ELPA](http://elpa.gnu.org/packages/sql-indent.html), by running the
following commands in Emacs:
M-x package-install RET sql-indent RET
To install sql-indent from this repository, open the file `sql-indent.el` in
Emacs and type:
M-x package-install-from-buffer RET
The syntax-based indentation of SQL code can be turned ON/OFF at any time by
enabling or disabling `sqlind-minor-mode`:
M-x sqlind-minor-mode RET
To enable syntax-based indentation for every SQL buffer, you can add
`sqlind-minor-mode` to `sql-mode-hook`. First, bring up the customization
buffer using the command:
M-x customize-variable RET sql-mode-hook RET
Than, click on the "INS" button to add a new entry and put "sqlind-minor-mode"
in the text field.

View File

@@ -0,0 +1,351 @@
;;; sql-indent-left.el --- configuration options to indent sql -*- lexical-binding: t -*-
;; Copyright (C) 2017 Free Software Foundation, Inc
;; Filename: sql-indent-left.el
;; Description:
;; Author: pierre.techoueyres@free.fr
;; Maintainer: pierre.techoueyres@free.fr
;; Created:
;; Version: pierre.techoueyres@free.fr
;; Last-Updated:
;; By:
;; Update #: 0
;; URL:
;; Keywords: language sql indentation
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;; None
;; This program 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 3 of the License, or
;; (at your option) any later version.
;;
;; This program 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. If not, see <https://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Set configuration options to indent sql my way.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program 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 3, or
;; (at your option) any later version.
;;
;; This program 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 this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'sql-indent)
(defun indent-case-statement-items (syntax base-indentation)
"Look for a SYNTAX of ((block-start when) (in-block case \"\") ...)
or ((block-start else) (in-block case \"\") ...)."
(let ((outer (sqlind-outer-context syntax)))
(if (and (eq 'in-block (sqlind-syntax-symbol outer))
(eq 'case (sqlind-syntax-keyword outer))
(eq 'block-start (sqlind-syntax-symbol syntax))
(memq (sqlind-syntax-keyword syntax) '(when else)))
(+ base-indentation sqlind-basic-offset)
base-indentation)))
(defvar sqlind-indentation-right-offsets-alist
`((select-column-continuation sqlind-indent-select-column
sqlind-adjust-operator
sqlind-lone-semicolon)
(in-select-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-right-justify-logical-operator
sqlind-lone-semicolon)
(in-delete-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-right-justify-logical-operator
sqlind-lone-semicolon)
(in-insert-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-right-justify-logical-operator
sqlind-lone-semicolon)
(in-update-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-right-justify-logical-operator
sqlind-lone-semicolon)
;; mandatory
(select-table-continuation sqlind-indent-select-table +
sqlind-lone-semicolon)
;; rest picked up from the original indentation offsets
,@sqlind-default-indentation-offsets-alist)
"Align sql code like this :
clear columns
set linesize 2500
set trimout on trimspool on
select atc.column_name,
atc.data_type,
data_length,
data_precision,
nullable,
data_scale,
nvl(substr(comments, 1, 100), atc.column_name) comments
from all_tab_columns atc,
all_col_comments acc
where atc.owner = acc.owner
and atc.table_name = acc.table_name
and atc.column_name = acc.column_name
and atc.owner = user
and atc.table_name = 'MY_TABLE'
and atc.column_name = p_column_name
and not exists (select 1
from all_tab_columns atc1,
all_col_comments acc1
where atc1.owner = acc1.owner
and atc1.table_name = acc1.table_name
and atc1.column_name = acc1.column_name
and atc1.owner = atc.owner
and atc1.table_name = atc.table_name
and acc1.column_name = acc.column_name)
;
delete from my_table mt
where col_1 = v_col1
and ( col_2 = v_col2
or col_3 = v_col3)
and col_42 = '42'
;
update my_table
set col1_has_a_long_name = value1,
col2_is_short = value2
where cond1 is not null
and ( col_2 = v_col2
or col_3 = v_col3)
and col_42 = '42'
;
insert into xyzxx
( aaa, xxx, bbb, ccc,
ddd, eee, fff, ggg,
hhh )
select aaa,
xxx,
max (m.b1) as bbb,
min (m.b1) as ccc,
coalesce (max (n.c2), 0) as ddd,
coalesce (min (n.c2), 0) as eee,
max (m.b1) over ( partition by c2
order by aaa desc ) as fff,
min (m.b1) over ( partition by c2
order by aaa desc ) as ggg,
avg (n.c2)as hhh
from (select * from (select aaa,
jjj + kkk as b1,
row_number () over ( partition by qqq
order by rrr,
sss ) as rn
from mno)
where rn = 1) m
inner join (select aaa,
nnn + ooo as c2
from pqr) n
using (aaa),
group by aaa,
xxx
order by xxx desc,
aaa asc
;")
(defvar sqlind-indentation-left-offsets-alist
`((select-clause 0)
(insert-clause 0)
(delete-clause 0)
(update-clause 0)
(case-clause-item-cont 0)
(block-start indent-case-statement-items)
(begin-block 0)
(case-clause +)
(package +)
(package-body +)
(statement-continuation + sqlind-adjust-operator)
(nested-statement-open 1)
(nested-statement-continuation 1)
(nested-statement-close sqlind-use-anchor-indentation)
(string-continuation 0) ;; or should it be a beginning of line or aligned with the previous block ?
;; Anyway. It's really *BAD* to continue a string across lines.
(select-column sqlind-indent-select-column-alt
sqlind-adjust-operator
sqlind-lone-semicolon)
(select-column-continuation sqlind-indent-select-column-alt
sqlind-adjust-operator
sqlind-lone-semicolon)
(in-select-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-left-justify-logical-operator
sqlind-lone-semicolon)
(in-delete-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-left-justify-logical-operator
sqlind-lone-semicolon)
(in-insert-clause +
sqlind-adjust-operator
sqlind-left-justify-logical-operator
sqlind-lone-semicolon)
(in-update-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-left-justify-logical-operator
sqlind-lone-semicolon)
(select-table-continuation + sqlind-lone-semicolon)
;; rest picked up from the original indentation offsets
,@sqlind-default-indentation-offsets-alist)
"Align sql code like this :
clear columns
set linesize 2500
set trimout on trimspool on
select DISTINCT
atc.column_name,
atc.data_type,
data_length,
data_precision,
nullable,
data_scale,
nvl(substr(comments, 1, 100), atc.column_name) comments
from all_tab_columns atc,
all_col_comments acc
where atc.owner = acc.owner
and atc.table_name = acc.table_name
and atc.column_name = acc.column_name
and atc.owner = user
and atc.table_name = 'MY_TABLE'
and atc.column_name = p_column_name
and not exists (select 1
from all_tab_columns atc1,
all_col_comments acc1
where atc1.owner = acc1.owner
and atc1.table_name = acc1.table_name
and atc1.column_name = acc1.column_name
and atc1.owner = atc.owner
and atc1.table_name = atc.table_name
and acc1.column_name = acc.column_name)
;
delete from my_table mt
where col_1 = v_col1
and ( col_2 = v_col2
or col_3 = v_col3)
and col_42 = '42'
;
update my_table
set col1_has_a_long_name = value1,
col2_is_short = value2
where cond1 is not null
and ( col_2 = v_col2
or col_3 = v_col3)
and col_42 = '42'
;
insert into xyzxx
( aaa, xxx, bbb, ccc,
ddd, eee, fff, ggg,
hhh )
select aaa,
xxx,
max (m.b1) as bbb,
min (m.b1) as ccc,
coalesce (max (n.c2), 0) as ddd,
coalesce (min (n.c2), 0) as eee,
max (m.b1) over ( partition by c2
order by aaa desc ) as fff,
min (m.b1) over ( partition by c2
order by aaa desc ) as ggg,
avg (n.c2)as hhh
from (select * from (select aaa,
jjj + kkk as b1,
row_number () over ( partition by qqq
order by rrr,
sss ) as rn
from mno)
where rn = 1) m
inner join (select aaa,
nnn + ooo as c2
from pqr) n
using (aaa),
group by aaa,
xxx
order by xxx desc,
aaa asc
;")
(defun sqlind-indent-select-column-alt (syntax base-indentation)
"Return the indentation for a column after a SELECT DISTINCT clause.
SYNTAX is the syntax of the current line, BASE-INDENTATION is the
current indentation, which we need to update.
Like `sqlind-indent-select-column' but we try to align to the KEYWORD,
but if we are the first column after the SELECT clause we simply
add `sqlind-basic-offset'."
(save-excursion
(goto-char (sqlind-anchor-point syntax))
(when (looking-at "select\\s *\\(top\\s +[0-9]+\\|distinct\\|unique\\)?")
(if (match-beginning 1)
(goto-char (match-beginning 1))
(goto-char (match-end 0))))
(skip-syntax-forward " ")
(if (or (looking-at sqlind-comment-start-skip)
(looking-at "$"))
(+ base-indentation sqlind-basic-offset)
(current-column))))
;;;###autoload
(defun sqlind-setup-style-left ()
"Define an sql-indentation style where keywords are left aligned."
(interactive)
(setq sqlind-indentation-offsets-alist sqlind-indentation-left-offsets-alist))
;;;###autoload
(defun sqlind-setup-style-right ()
"Define an sql-indentation style where keywords are right aligned."
(interactive)
(setq sqlind-indentation-offsets-alist sqlind-indentation-right-offsets-alist))
;;;###autoload
(defun sqlind-setup-style-default ()
"Define an sql-indentation style where keywords are right aligned."
(interactive)
(setq sqlind-indentation-offsets-alist sqlind-default-indentation-offsets-alist))
(provide 'sql-indent-left)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; sql-indent-left.el ends here

View File

@@ -0,0 +1,2 @@
;; Generated package description from sql-indent.el -*- no-byte-compile: t -*-
(define-package "sql-indent" "1.5" "Support for indenting code in SQL files." '((cl-lib "0.5")) :keywords '("languages" "sql") :authors '(("Alex Harsanyi" . "AlexHarsanyi@gmail.com")) :maintainer '("Alex Harsanyi" . "AlexHarsanyi@gmail.com") :url "https://github.com/alex-hhh/emacs-sql-indent")

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,634 @@
sql-indent.el -- syntax based indentation for SQL files for GNU Emacs
*NOTE* This file is formatted as an Emacs Org file. If you open it in GNU
Emacs, you will be able to open-close sections of the file, which will make
navigation easier.
* Overview
sql-indent.el is a GNU Emacs minor mode which adds support for syntax-based
indentation when editing SQL code: TAB indents the current line based on the
syntax of the SQL code on previous lines. This works like the indentation for
C and C++ code.
The package also defines align rules so that the ~align~ function works for
SQL statements, see ~sqlind-align-rules~ for which rules are defined. This
can be used to align multiple lines around equal signs or "as" statements.
Here is an example of alignment rules:
#+BEGIN_SRC sql
update my_table
set col1_has_a_long_name = value1,
col2_is_short = value2,
col3_med = v2,
c4 = v5
where cond1 is not null;
select long_colum as lcol,
scol as short_column,
mcolu as mcol,
from my_table;
#+END_SRC
To use this feature, select the region you want to align and type:
#+BEGIN_SRC text
M-x align RET
#+END_SRC
~sqlind-minor-mode~ together with the align rules can assist in writing tidy
SQL code or formatting existing SQL code. The indentation rules are
customizable, so you can adapt it to match your own indentation preferences.
* Installation
To install this package, open the file ~sql-indent.el~ in Emacs and type
#+BEGIN_SRC text
M-x package-install-from-buffer RET
#+END_SRC
The syntax-based indentation of SQL code can be turned ON/OFF at any time by
enabling or disabling ~sqlind-minor-mode~:
#+BEGIN_SRC text
M-x sqlind-minor-mode RET
#+END_SRC
To enable syntax-based indentation for every SQL buffer, you can add
~sqlind-minor-mode~ to ~sql-mode-hook~. First, bring up the customization
buffer using the command:
#+BEGIN_SRC text
M-x customize-variable RET sql-mode-hook RET
#+END_SRC
Than, click on the "INS" button to add a new entry and put "sqlind-minor-mode"
in the text field.
* Customization
The sql-indent.el package allows customizing the indentation rules to suit
your personal preferences or site specific coding styles. To create a set of
customized rules, you will need to have basic familiarity with how indentation
works. See also the "A Simple Example" section below and the
~sql-indent-left.el~ file, which demonstrate how to set up custom indentation
rules. The sections below contain the rest of the details.
The indentation process happens in two separate phases: first syntactic
information is determined about the line, than the line is indented based on
that syntactic information. The syntactic parse is not expected to change
often, since it deals with the structure of the SQL code, however, indentation
is a personal preference, and can be easily customized.
Two variables control the way code is indented: ~sqlind-basic-offset~ and
~sqlind-indentation-offsets-alist~. To customize these variables, you need to
create a function that sets custom values and add it to ~sql-mode-hook~.
** A Simple Example
The default indentation rules will align to the right all the keywords in a
SELECT statement, like this:
#+BEGIN_SRC sql
select c1, c2
from t1
where c3 = 2
#+END_SRC
If you prefer to have them aligned to the left, like this:
#+BEGIN_SRC sql
select c1, c2
from t1
where c3 = 2
#+END_SRC
You can add the following code to your init file:
#+BEGIN_SRC emacs-lisp
(require 'sql-indent)
;; Update indentation rules, select, insert, delete and update keywords
;; are aligned with the clause start
(defvar my-sql-indentation-offsets-alist
`((select-clause 0)
(insert-clause 0)
(delete-clause 0)
(update-clause 0)
,@sqlind-default-indentation-offsets-alist))
(add-hook 'sqlind-minor-mode-hook
(lambda ()
(setq sqlind-indentation-offsets-alist
my-sql-indentation-offsets-alist)))
#+END_SRC
** Customization Basics
To customize indentation, you will need to provide a new value for the
~sqlind-indentation-offsets-alist~ variable. The variable is made buffer
local each time it is set, so you need to set it inside the ~sql-mode-hook~.
The variable specifies how each syntactic symbol should be indented. Since
only a few symbols need to be updated, the usual way to update it is to
"extend" the value of ~sqlind-default-indentation-offsets-alist~, like so:
#+BEGIN_SRC emacs-lisp
(defvar my-sql-indentation-offsets-alist
`( ;; put new syntactic symbols here, and add the default ones at the end.
;; If there is no value specified for a syntactic symbol, the default
;; will be picked up.
,@sqlind-default-indentation-offsets-alist))
;; Arrange for the new indentation offset to be set up for each SQL buffer.
(add-hook 'sqlind-minor-mode-hook
(lambda ()
(setq sqlind-indentation-offsets-alist
my-sql-indentation-offsets-alist)))
#+END_SRC
The simplest way to adjust the indentation is to explore the syntactic
information using ~sqlind-show-syntax-of-line~. To use it, move the cursor to
the line you would like to indent and type:
#+BEGIN_SRC text
M-x sqlind-show-syntax-of-line RET
#+END_SRC
A message like the one below will be shown in the messages buffer:
#+BEGIN_SRC text
((select-clause . 743) (statement-continuation . 743))
#+END_SRC
The first symbol displayed is the syntactic symbol used for indentation, in
this case ~select-clause~. The syntactic symbols are described in a section
below, however, for now, this is the symbol that will need to be updated in
~sqlind-indentation-offsets-alist~. The number next to it represents the
anchor, or reference position in the buffer where the current statement
starts. The anchor and is useful if you need to write your own indentation
functions.
To customize indentation for this type of statement, add an entry in the
~sqlind-indentation-offsets-alist~, for the syntactic symbol shown, with
information about how it should be indented. This information is a list
containing *indentation control items* (these are described below).
For example, to indent keyword in SELECT clauses at the same level as the
keyword itself, we use a number which is added to the indentation level of the
anchor, in this case, 0:
#+BEGIN_SRC text
(select-clause 0)
#+END_SRC
To indent it at ~sqlind-basic-offset~ plus one more space, use:
#+BEGIN_SRC text
(select-clause + 1)
#+END_SRC
To right-justify the keyword w.r.t the SELECT keyword, use:
#+BEGIN_SRC text
(select-clause sqlind-right-justify-clause)
#+END_SRC
The default value for ~sqlind-indentation-offsets-alist~ contains many
examples for indentation setup rules.
** Indentation control items
~sqlind-calculate-indentation~ is the function that calculates the indentation
offset to use, based on the contents of ~sqlind-indentation-offsets-alist~.
The indentation offset starts with the indentation column of the ANCHOR point
and it is adjusted based on the following items:
* a ~NUMBER~ -- the NUMBER will be added to the indentation offset.
* ~+~ -- the current indentation offset is incremented by
~sqlind-basic-offset~
* ~++~ -- the current indentation offset is indentation by ~2 *
sqlind-basic-offset~
* ~-~ -- the current indentation offset is decremented by
~sqlind-basic-offset~
* ~--~ -- the current indentation offset is decremented by 2 *
~sqlind-basic-offset~
* a ~FUNCTION~ -- the syntax and current indentation offset is passed to the
function and its result is used as the new indentation offset. This can be
used to further customize indentation.
*** Indentation Helper Functions
The following helper functions are available as part of the package and can be
used as the FUNCTION part in the ~sqlind-indentation-offsets-alist~
**** sqlind-use-anchor-indentation
discard the current offset and returns the indentation column of the ANCHOR
**** sqlind-lineup-to-anchor
discard the current offset and returns the column of the anchor point, which
may be different than the indentation column of the anchor point.
**** sqlind-use-previous-line-indentation
discard the current offset and returns the indentation column of the previous
line
**** sqlind-lineup-open-paren-to-anchor
if the line starts with an open parenthesis, discard the current
offset and return the column of the anchor point.
**** sqlind-lone-semicolon
if the line contains a single semicolon ';', use the value of
~sqlind-use-anchor-indentation~
**** sqlind-adjust-operator
if the line starts with an arithmetic operator (like ~+~ , ~-~, or ~||~), line
it up so that the right hand operand lines up with the left hand operand of
the previous line. For example, it will indent the ~||~ operator like this:
#+BEGIN_SRC sql
select col1, col2
|| col3 as composed_column, -- align col3 with col2
col4
|| col5 as composed_column2
from my_table
where cond1 = 1
and cond2 = 2;
#+END_SRC
**** sqlind-left-justify-logical-operator
If the line starts with a logic operator (AND, OR NOT), line the operator with
the start of the WHERE clause. This rule should be added to the
~in-select-clause~ syntax after the ~sqlind-lineup-to-clause-end~ rule.
**** sqlind-right-justify-logical-operator
If the line starts with a logic operator (AND, OR NOT), line the operator with
the end of the WHERE clause. This rule should be added to the
~in-select-clause~ syntax.
#+BEGIN_SRC sql
select *
from table
where a = b
and c = d; -- AND clause sits under the where clause
#+END_SRC
**** sqlind-adjust-comma
if the line starts with a comma, adjust the current offset so that the line is
indented to the first word character. For example, if added to a
~select-column~ syntax indentation rule, it will indent as follows:
#+BEGIN_SRC sql
select col1
, col2 -- align "col2" with "col1"
from my_table;
#+END_SRC
**** sqlind-lineup-into-nested-statement
discard the current offset and return the column of the first word inside a
nested statement. This rule should be added to
~nested-statement-continuation~ syntax indentation rule, and will indent as
follows:
#+BEGIN_SRC sql
( a,
b -- b is aligned with a
)
#+END_SRC
*** More Indentation Helper Functions
The following function contain indentation code specific to various SQL
statements. Have a look at their doc strings for what they do:
* ~sqlind-indent-comment-start~, ~sqlind-indent-comment-continuation~
* ~sqlind-indent-select-column~
* ~sqlind-indent-select-table~
* ~sqlind-lineup-to-clause-end~
* ~sqlind-right-justify-clause~
* ~sqlind-lineup-joins-to-anchor~
** Syntactic Symbols
The SQL parsing code returns a syntax definition (either a symbol or a
list) and an anchor point, which is a buffer position. The syntax symbols can
be used to define how to indent each line. The following syntax symbols are
defined for SQL code:
* ~(syntax-error MESSAGE START END)~ -- this is returned when the parse
failed. MESSAGE is an informative message, START and END are buffer
locations denoting the problematic region. ANCHOR is undefined for this
syntax info
* ~in-comment~ -- line is inside a multi line comment, ANCHOR is the start of
the comment.
* ~comment-start~ -- line starts with a comment. ANCHOR is the start of the
enclosing block.
* ~in-string~ -- line is inside a string, ANCHOR denotes the start of the
string.
* ~toplevel~ -- line is at toplevel (not inside any programming construct).
ANCHOR is usually (point-min).
* ~(in-block BLOCK-KIND LABEL)~ -- line is inside a block construct.
BLOCK-KIND (a symbol) is the actual block type and can be one of "if",
"case", "exception", "loop" etc. If the block is labeled, LABEL contains
the label. ANCHOR is the start of the block.
* ~(in-begin-block KIND LABEL)~ -- line is inside a block started by a begin
statement. KIND (a symbol) is "toplevel-block" for a begin at toplevel,
"defun" for a begin that starts the body of a procedure or function,
\"package\" for a begin that starts the body of a package, nil for a begin
that is none of the previous. For a "defun" or "package", LABEL is the
name of the procedure, function or package, for the other block types LABEL
contains the block label, or the empty string if the block has no label.
ANCHOR is the start of the block.
* ~(block-start KIND)~ -- line begins with a statement that starts a block.
KIND (a symbol) can be one of "then", "else" or "loop". ANCHOR is the
reference point for the block start (the corresponding if, case, etc).
* ~(block-end KIND LABEL)~ -- the line contains an end statement. KIND (a
symbol) is the type of block we are closing, LABEL (a string) is the block
label (or procedure name for an end defun).
* ~declare-statement~ -- line is after a declare keyword, but before the
begin. ANCHOR is the start of the declare statement.
* ~(package NAME)~ -- line is inside a package definition. NAME is the name
of the package, ANCHOR is the start of the package.
* ~(package-body NAME)~ -- line is inside a package body. NAME is the name
of the package, ANCHOR is the start of the package body.
* ~(create-statement WHAT NAME)~ -- line is inside a CREATE statement (other
than create procedure or function). WHAT is the thing being created, NAME
is its name. ANCHOR is the start of the create statement.
* ~(defun-start NAME)~ -- line is inside a procedure of function definition
but before the begin block that starts the body. NAME is the name of the
procedure/function, ANCHOR is the start of the procedure/function
definition.
The following SYNTAX-es are for SQL statements. For all of them ANCHOR points
to the start of a statement itself.
* ~labeled-statement-start~ -- line is just after a label.
* ~statement-continuation~ -- line is inside a statement which starts on a
previous line.
* ~nested-statement-open~ -- line is just inside an opening bracket, but the
actual bracket is on a previous line.
* ~nested-statement-continuation~ -- line is inside an opening bracket, but
not the first element after the bracket.
* ~nested-statement-close~ line is inside an opening bracket and the line
contains the closing bracket as the first character.
The following SYNTAX-es are for statements which are SQL code (DML
statements). They are specializations on the previous statement syntaxes and
for all of them a previous generic statement syntax is present earlier in the
SYNTAX list. Unless otherwise specified, ANCHOR points to the start of the
clause (select, from, where, etc) in which the current point is.
* ~with-clause~ -- line is inside a WITH clause, but before the main SELECT
clause.
* ~with-clause-cte~ -- line is inside a with clause before a CTE (common
table expression) declaration
* ~with-clause-cte-cont~ -- line is inside a with clause before a CTE
definition
* ~case-clause~ -- line is on a CASE expression (WHEN or END clauses).
ANCHOR is the start of the CASE expression.
* ~case-clause-item~ -- line is on a CASE expression (THEN and ELSE clauses).
ANCHOR is the position of the case clause.
* ~case-clause-item-cont~ -- line is on a CASE expression but not on one of
the CASE sub-keywords. ANCHOR points to the case keyword that this line is
a continuation of.
* ~select-clause~ -- line is inside a select statement, right before one of
its clauses (from, where, order by, etc).
* ~select-column~ -- line is inside the select column section, after a full
column was defined (and a new column definition is about to start).
* ~select-column-continuation~ -- line is inside the select column section,
but in the middle of a column definition. The defined column starts on a
previous like. Note that ANCHOR still points to the start of the select
statement itself.
* ~select-join-condition~ -- line is right before or just after the ON clause
for an INNER, LEFT or RIGHT join. ANCHOR points to the join statement for
which the ON is defined.
* ~select-table~ -- line is inside the from clause, just after a table was
defined and a new one is about to start.
* ~select-table-continuation~ -- line is inside the from clause, inside a
table definition which starts on a previous line. Note that ANCHOR still
points to the start of the select statement itself.
* ~(in-select-clause CLAUSE)~ -- line is inside the select CLAUSE, which can
be "where", "order by", "group by" or "having". Note that CLAUSE can never
be "select" and "from", because we have special syntaxes inside those
clauses.
* ~insert-clause~ -- line is inside an insert statement, right before one of
its clauses (values, select).
* ~(in-insert-clause CLAUSE)~ -- line is inside the insert CLAUSE, which can
be "insert into" or "values".
* ~delete-clause~ -- line is inside a delete statement right before one of
its clauses.
* ~(in-delete-clause CLAUSE)~ -- line is inside a delete CLAUSE, which can be
"delete from" or "where".
* ~update-clause~ -- line is inside an update statement right before one of
its clauses.
* ~(in-update-clause CLAUSE)~ -- line is inside an update CLAUSE, which can
be "update", "set" or "where"
* Limitations
The sql-indent package does not contain a full SQL parser, instead it relies
on various heuristics to determine the context of each line in a SQL program.
Relying on heuristics means that sometimes valid SQL code is not detected
correctly, and therefore the indentation will be wrong.
This section contains some of the known cases that are incorrectly detected,
and provides some workarounds for them.
** Parsing expressions
There is no support for parsing SQL expressions, so if an expression is broken
over several lines, sql-indent.el will consider all lines to be
~statement-continuation~ lines. The exception is that bracketed expressions
are identified correctly so they can be used for indentation.
The examples below summarize what is supported and what is not, as well as the
workarounds:
#+BEGIN_SRC sql
-- SUPPORTED: case expression immediately after assignment
var := case ind
when 1 then 'Guy'
when 2 then 'Abc'
when 3 then 'Def'
else 'World'
end case;
-- NOT SUPPORTED: any complex expression involving a case expression. entire
-- expression is a 'statement-continuation
var := 'abc'
|| case ind
when 1 then 'Guy'
when 2 then 'Abc'
when 3 then 'Def'
else 'World'
end case;
-- WORKAROUND: use brackets instead
var := 'abc'
|| (case ind
when 1 then 'Guy'
when 2 then 'Abc'
when 3 then 'Def'
else 'World'
end case);
-- SUPPORTED: case expression as select column
select col1,
case ind
when 1 then 'Guy'
when 2 then 'Abc'
when 3 then 'Def'
else 'World'
end case,
col2,
from some_table;
-- NOT SUPPORTED: any complex expression involving a case expression in a
-- select column. Entire column is a 'select-column-continuation
select col1,
'abc' || case ind
when 1 then 'Guy'
when 2 then 'Abc'
when 3 then 'Def'
else 'World'
end case,
col2,
from some_table;
-- WORKAROUND: use brackets instead
select col1,
'abc' || (case ind
when 1 then 'Guy'
when 2 then 'Abc'
when 3 then 'Def'
else 'World'
end case),
col2,
from some_table;
#+END_SRC
** DECLARE statements in Postgres
The DECLARE statement in Postgres can start a block of declarations or declare
a single item. Unfortunately, the sql-indent package is not always able to
differentiate between the two. Curently, DECLARE blocks are only recognized
at the start of another enclosing block, such as $$, BEGIN, THEN or ELSE, but
they can occur on other situations. The workaround is to enclose the DECLARE
block inside a BEGIN/END block or to use individual DECLARE statements.
For more info see https://github.com/alex-hhh/emacs-sql-indent/issues/92
DECLARE blocks are not recognized when the follow normal statements,
sql-indent considers them single statements instead. In the example below,
DECLARE is considered a statement, and the ~local_b~ declaration is anchored
off the previous BEGIN statement:
#+BEGIN_SRC sql
-- NOT SUPPORTED: `local_b` is not recognized as a declaration
create function less_than(a text, b text) returns boolean as $$
begin
raise debug 'less_than(%, %)', a, b;
declare
local_a text := a;
local_b text := b;
begin
return local_a < local_b;
end;
end;
end;
$$ language plpgsql;
#+END_SRC sql
The workaround is to either surround the DECLARE block with a BEGIN/END
statement, or to prefix each variable declaration with DECLARE, as in the two
examples below:
#+BEGIN_SRC sql
-- WORKAROUND 1: surround the DECLARE/BEGIN/END with another BEGIN/END block
create function less_than(a text, b text) returns boolean as $$
begin
raise debug 'less_than(%, %)', a, b;
begin
declare
local_a text := a;
local_b text := b;
begin
return local_a < local_b;
end;
end;
end;
end;
$$ language plpgsql;
-- WORKAROUND 2: declare each variable individually
create function less_than(a text, b text) returns boolean as $$
begin
raise debug 'less_than(%, %)', a, b;
declare
local_a text := a;
declare
local_b text := b;
begin
return local_a < local_b;
end;
end;
end;
$$ language plpgsql;
#+END_SRC sql
.