Emacs: How to capitalize all keywords (example in SQL)

Here is my attempt at solution (assuming you want to upcase 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 do M-xmy-capitalize-all-mysql-keywordsRET. The advantage of this solution is that it picks up the keywords from Emacs sql-mode, you do not need to specify them.

Also I assumed you meant you wanted to upcase the words


I've written sql-upcase.el for upper-casing keywords and function names in sql-mode and/or sql-interative-mode.

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

This will hopefully Just Work for all the SQL products supported by Emacs, as it uses the keyword regexps defined for the buffer's sql-product. This was initially inspired by the use of font-lock keywords in Douglas La Rocca's function for upper-casing all PostgreSQL keywords in a buffer, which can be found on the EmacsWiki (which also bears similarities to the method used in user2053036's accepted answer here).

Follow the link for more details.

Tags:

Sql

Emacs