Skip to content

Instantly share code, notes, and snippets.

@davazp
Created December 11, 2015 14:31
Show Gist options
  • Save davazp/6f054a8f2e4469c4d92a to your computer and use it in GitHub Desktop.
Save davazp/6f054a8f2e4469c4d92a to your computer and use it in GitHub Desktop.
Upcase SQL keywords
;;; sql-autoupcase.el --- Minor mode for upcasing SQL keywords -*- lexical-binding: t; -*-
;; Copyright (C) 2015 David Vazquez
;; Author: David Vazquez <davazp@gmail.com>
;; Keywords: abbrev, convenience, languages
;; 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/>.
;;; Commentary:
;;
;;; Code:
(defvar sql-autoupcase-keywords
'("absolute" "action" "add" "admin" "after" "aggregate" "alias" "all"
"allocate" "alter" "and" "any" "are" "as" "asc" "assertion" "at"
"authorization" "before" "begin" "both" "breadth" "by" "call"
"cascade" "cascaded" "case" "catalog" "check" "class" "close"
"collate" "collation" "column" "commit" "completion" "connect"
"connection" "constraint" "constraints" "constructor" "continue"
"corresponding" "create" "cross" "cube" "current" "cursor" "cycle"
"data" "day" "deallocate" "declare" "default" "deferrable" "deferred"
"delete" "depth" "deref" "desc" "describe" "descriptor" "destroy"
"destructor" "deterministic" "diagnostics" "dictionary" "disconnect"
"distinct" "domain" "drop" "dynamic" "each" "else" "end" "equals"
"escape" "every" "except" "exception" "exec" "execute" "external"
"false" "fetch" "first" "for" "foreign" "found" "free" "from" "full"
"function" "general" "get" "global" "go" "goto" "grant" "group"
"grouping" "having" "host" "hour" "identity" "ignore" "immediate" "in"
"indicator" "initialize" "initially" "inner" "inout" "input" "insert"
"intersect" "into" "is" "isolation" "iterate" "join" "key" "language"
"last" "lateral" "leading" "left" "less" "level" "like" "limit"
"local" "locator" "map" "match" "minute" "modifies" "modify" "module"
"month" "names" "natural" "new" "next" "no" "none" "not" "null" "of"
"off" "old" "on" "only" "open" "operation" "option" "or" "order"
"ordinality" "out" "outer" "output" "pad" "parameter" "parameters"
"partial" "path" "postfix" "prefix" "preorder" "prepare" "preserve"
"primary" "prior" "privileges" "procedure" "public" "read" "reads"
"recursive" "references" "referencing" "relative" "restrict" "result"
"return" "returns" "revoke" "right" "role" "rollback" "rollup"
"routine" "rows" "savepoint" "schema" "scroll" "search" "second"
"section" "select" "sequence" "session" "set" "sets" "size" "some"
"space" "specific" "specifictype" "sql" "sqlexception" "sqlstate"
"sqlwarning" "start" "state" "statement" "static" "structure" "table"
"temporary" "terminate" "than" "then" "timezone_hour"
"timezone_minute" "to" "trailing" "transaction" "translation"
"trigger" "true" "under" "union" "unique" "unknown" "unnest" "update"
"usage" "using" "value" "values" "variable" "view" "when" "whenever"
"where" "with" "without" "work" "write" "year"))
(define-abbrev-table 'sql-mode-abbrev-table
(mapcar #'(lambda (v) (list v (upcase v) nil 1))
sql-autoupcase-keywords))
(defun sql-autoupcase-in-code-p ()
(let ((list (syntax-ppss)))
(and (null (nth 3 list)) ; inside string.
(null (nth 4 list))))) ; inside cocmment
(defun sql-autoupcase-pre-abbrev-hook ()
;; Allow our abbrevs only in a code context.
(setq local-abbrev-table
(and (sql-autoupcase-in-code-p)
sql-mode-abbrev-table)))
(define-minor-mode sql-autoupcase-mode
"Upcase SQL keywords automatically"
nil
" SQL-AU"
nil
(unless (eq major-mode 'sql-mode)
(error "sql-autoupcase-mode can only work with sql-mode"))
(make-local-variable 'pre-abbrev-expand-hook)
(cond
(sql-autoupcase-mode
(setq abbrev-mode t)
(add-hook 'pre-abbrev-expand-hook 'sql-autoupcase-pre-abbrev-hook))
(t
(remove-hook 'pre-abbrev-expand-hook 'sql-autoupcase-pre-abbrev-hook)
(setq local-abbrev-table nil))))
(provide 'sql-autoupcase)
;;; sql-autoupcase.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment