2

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 it 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

Setjmp
  • 27,279
  • 27
  • 74
  • 92

2 Answers2

4

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.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
2

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.

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • Is this code for some older version of Emacs? It fails with (void-function buffer-mode) on my Emacs 23.3.1 (OS X). I am guessing it means to extract the buffer's major mode. It is available in the variable `major-mode` which is buffer local. – vpit3833 Mar 25 '11 at 23:36
  • No, it's not old. Its just something I wrote, but I totally forgot that - (defun buffer-mode (buffer-or-name) (with-current-buffer buffer-or-name major-mode)) – Bozhidar Batsov Mar 27 '11 at 19:06