Last active
February 22, 2019 05:55
-
-
Save noprompt/ab272a68b2e8e758f8150c02916d8c31 to your computer and use it in GitHub Desktop.
Automatically shout SQL keywords and builtins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun ~/end-of-previous-face-change (face-name) | |
(save-excursion | |
(let ((continue t) | |
(answer nil)) | |
(while continue | |
(let ((p (or (previous-single-property-change (point) 'face) | |
1))) | |
(if (= p 1) | |
(progn | |
(setq continue nil) | |
(setq answer p)) | |
(if (equalp (get-text-property p 'face) | |
face-name) | |
(progn | |
(setq continue nil) | |
(setq answer (next-single-property-change p 'face))) | |
(goto-char p))))) | |
answer))) | |
(defun ~/inside-string-p () | |
(save-excursion | |
(let ((p (point))) | |
;; There is a " to the left. | |
(when (re-search-backward "\"" (point-min) t) | |
;; It's not in a comment. | |
(when (not (equalp (get-text-property (point) 'face) | |
'font-lock-comment-face)) | |
;; Goto to the end of the previous comment or beginning of | |
;; buffer. It'd be nice to use syntax tables here but that's | |
;; not always possible. | |
(goto-char (~/end-of-previous-face-change 'font-lock-comment-face)) | |
(let ((continue t) | |
(answer nil)) | |
(while continue | |
(if (re-search-forward "\"[^\"]*\\(\"\\|\\'\\)" (point-max) t) | |
(progn | |
(goto-char (match-end 0)) | |
(when (< (match-beginning 0) p (match-end 0)) | |
(setq continue nil) | |
(setq answer t))) | |
(setq continue nil))) | |
answer)))))) | |
(defun ~/sql/upcase-keywords-after-change (beg end length) | |
(interactive) | |
(if (string-match-p (regexp-opt (list " " "\n" "\r")) | |
(buffer-substring-no-properties beg end)) | |
(save-excursion | |
(backward-word) | |
(let ((next-change (or (next-property-change (point) (current-buffer)) | |
(point-max)))) | |
(if next-change | |
(pcase (get-text-property (point) 'face) | |
((or 'font-lock-keyword-face | |
'font-lock-builtin-face) | |
(if (not (~/inside-string-p)) | |
(upcase-region (point) next-change))) | |
(_))))))) | |
(defun ~/sql-mode () | |
(add-hook 'after-change-functions | |
'~/sql/upcase-keywords-after-change | |
nil | |
t)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment