开发者

Emacs: How to visit all sql-mode buffers and set the appropriate sql-buffer

My typical usage of sql-mode in emacs is to:

a. open a foo.sql file and begin editing

b. decide I want to run i开发者_开发知识库t using the key bindings for sql-send-region

c. fire up my custom (db-connect) function to connect to an appropriate db and create a *SQL* buffer.

However the foo.sql doesn't know about the existence of the *SQL* buffer unless I perform a "m-x sql-mode" in the buffer in order to refresh its environment and detect that such a buffer exists at this point. I would like to embed some code in my custom db-connect function to visit all buffers using sql-mode and update the sql-buffer variable. I am sure several stack overflow members must have done this or something similar before.

Thanks,

SetJmp


A quick look in the sql.el file revealed the command sql-set-sqli-buffer-generally, maybe this is something for you?

Another way you could hand this is to kill the buffer-local variant of sql-buffer by calling kill-local-variable in your major-mode hook. (That way, the effect would be that all SQL buffers would talk to the latest SQL buffer.)

Disclaimer: I don't know anything about SQL or SQL mode, only Emacs in general.


I've implemented this small helper function to filter buffers by their major-mode

(defun buffer-mode (buffer-or-name)
  (with-current-buffer buffer-or-name major-mode))


(defun filter-buffers-by-mode (mode)
  (delq nil
        (mapcar
         (lambda (x) (and (eq (buffer-mode x) mode) x))
         (buffer-list))))

You can pass 'sql-mode as the argument and you'll get a list of all open sql buffers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜