Emacs:如何大写所有关键字(SQL中的示例)

mod*_*tos 9 sql emacs

例如,我在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;
Run Code Online (Sandbox Code Playgroud)

如何利用关键字任何想法,比如select,from,group,by,table,as,等?它们已在我的编辑器中以蓝色字体显示.

小智 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))))))
Run Code Online (Sandbox Code Playgroud)

评估此功能后,就这样做M-xmy-capitalize-all-mysql-keywordsRET.此解决方案的优点是它从Emacs中获取关键字sql-mode,您无需指定它们.

我也认为你的意思是你想要upcase的话


phi*_*ils 6

sql-upcase.elsql-mode和/或中写了大写的关键字和函数名sql-interative-mode

它提供了sql-upcase-regionsql-upcase-buffer用于处理预先存在的SQL命令,但显著之处在于,还提供了其他的解决方案不同sql-upcase-mode,因为它被插入该自动处理文本次要模式。这意味着(a)在键入SQL关键字时将它们大写,并且(b)您可以将SQL粘贴到sql-mode缓冲区中,并且所有关键字都将自动被大写。

希望这对Emacs支持的所有SQL产品都适用,因为它使用为缓冲区的定义的关键字regexps sql-product。最初的灵感来自道格拉斯·拉·罗卡(Douglas La Rocca)函数中使用字体锁定关键字,将所有PostgreSQL关键字大写在缓冲区中,可以在EmacsWiki上找到该缓冲区(也与user2053036 在此处接受的答案所使用的方法类似) 。

点击链接以获取更多详细信息。