Emacs: how to use all keywords (example in SQL)

For example, I have the following SQL document in SQL[ANSI] mode SQL[ANSI] :

 create table title_price as select title, price from frumble group by title, price order by title; select *, count(*) from frumble group by title, price order by title; 

Any ideas on how to use keywords like select , from , group , by , table , as , etc.? They are already shown in blue in my editor.

+8
source share
6 answers

Here is my attempt at a solution (assuming you want to expose MySQL keywords)

 (defun point-in-comment () (let ((syn (syntax-ppss))) (and (nth 8 syn) (not (nth 3 syn))))) (defun my-capitalize-all-mysql-keywords () (interactive) (require 'sql) (save-excursion (dolist (keywords sql-mode-mysql-font-lock-keywords) (goto-char (point-min)) (while (re-search-forward (car keywords) nil t) (unless (point-in-comment) (goto-char (match-beginning 0)) (upcase-word 1)))))) 

After evaluating this function, just Mx my-capitalize-all-mysql-keywords RET . The advantage of this solution is that it selects keywords from Emacs sql-mode , you do not need to specify them.

I also suggested that you mean that you want upcase words

+8
source

I wrote sql-upcase.el for keywords and top-level function names in sql-mode and / or sql-interative-mode .

It provides the sql-upcase-region and sql-upcase-buffer for processing already existing SQL, but differs significantly from other solutions in that it also provides the sql-upcase-mode , which processes the text automatically as it is inserted. This means that (a) SQL keywords will be added as you type them, and (b) you can paste SQL into the sql-mode buffer and all keywords will be automatically updated.

This, hopefully, will just work for all SQL products supported by Emacs, as it uses the regexps keyword defined for the sql-product buffer. This was originally based on the use of keywords to block fonts in the Douglas La Rocca function for the top of all the PostgreSQL keywords in the buffer, which can be found on EmacsWiki (which also resembles the method used in user2053036 adopted by the answer here.)

Follow the link for more details.

+5
source

Try this feature:

 (defun capitalize-sql-in-buffer () (interactive) (let ((regex (format "\\_<%s\\_>" (regexp-opt '("create" "table" "select" "from" "group by" "order by" "as" "count"))))) (goto-char (point-min)) (while (re-search-forward regex nil t) (let ((beg (match-beginning 0)) (end (match-end 0)) (str (match-string 0))) (delete-region beg end) (insert (upcase str)))))) 

You can add more keywords as needed, I do not own SQL.

+2
source

You can use Mx replace-regexp . Just try these commands.

 ESC M-< Mx replace-regexp RET \(select\|from\|group\|by\|table\|as\) RET \,(capitalize \1) RET 

Input:

 create table title_price as select title, price from frumble group by title, price order by title; select *, count(*) from frumble group by title, price order by title; 

output:

 create table title_price as select title, price from frumble group by title, price order by title; select *, count(*) from frumble group by title, price order by title; 
+2
source

sqlup-mode smooths keywords as you type them and provides a function for using keywords in the selected region.

+2
source

Here's the hacky way that I use for capitalization in sql-interactive-mode buffers:

 (defun upcase-last-keyword () (when (and (= last-input-event ? ) (save-excursion (backward-char 2) (eq (face-at-point) 'font-lock-keyword-face))) (upcase-word -1))) (add-hook 'sql-interactive-mode-hook (lambda () (make-local-variable 'post-self-insert-hook) (add-hook 'post-self-insert-hook #'upcase-last-keyword)))) 
0
source

All Articles