例如,我在SQL[ANSI]
模式下有以下示例SQL文档:
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;
有关如何将关键字大写的任何想法,例如select
,from
,group
,by
,table
,as
等?
它们已在我的编辑器中以蓝色字体显示。
答案 0 :(得分:8)
以下是我尝试解决方案(假设您想要提升MySQL
个关键字)
(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))))))
评估此函数后,只需执行 M-x my-capitalize-all-mysql-keywords
RET 。此解决方案的优势在于它从Emacs sql-mode
中获取关键字,您无需指定它们。
此外,我认为你的意思是想要upcase
字样
答案 1 :(得分:5)
我已在sql-mode
和/或sql-interative-mode
中为大写字母关键字和功能名称撰写了sql-upcase.el
。
它提供了sql-upcase-region
和sql-upcase-buffer
命令来处理预先存在的SQL,但与其他解决方案有很大不同之处在于它还提供了一个sql-upcase-mode
次要模式,可以自动处理文本。插入。这意味着(a)SQL关键字在您键入时会被提升,以及(b)您可以将SQL粘贴到sql-mode
缓冲区中,所有关键字将自动升级。
这有望为Emacs支持的所有SQL产品提供服务,因为它使用为缓冲区sql-product
定义的关键字regexps。这最初的灵感来自在Douglas La Rocca的函数中使用font-lock关键字来缓冲缓冲区中的所有PostgreSQL关键字,这可以在EmacsWiki上找到(它也有相似之处) user20530中使用的方法已在此处接受answer。
点击链接了解更多详情。
答案 2 :(得分:2)
试试这个功能:
(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))))))
您可以根据需要添加更多关键字,我并不十分精通SQL。
答案 3 :(得分:2)
您可以使用M-x replace-regexp
。
试试这些命令。
ESC M-<
M-x replace-regexp RET
\(select\|from\|group\|by\|table\|as\) RET
\,(capitalize \1) RET
输入:
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;
输出:
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;
答案 4 :(得分:2)
sqlup-mode会在您键入时对关键字进行大写,并提供一个功能来大写所选区域中的关键字。
答案 5 :(得分:0)
这是我用来在sql-interactive-mode
缓冲区中将关键字大写的一种hacky方式:
(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))))