I use a monitor and a laptop screen as my multimonitor setup.

Unfortunately the monitor and the laptop differs in resolution. So I use 2 frames, one frame on the monitor and one frame on the laptop screen.

Both frames are connected to one Emacs instance.

MONITOR: I will call the frame that's opened on monitor as frame 1. In the frame 1 on the monitor, I have a buffer opened with the default SQL-mode that's shipped with Emacs.

LAPTOP: On the laptop screen, there is a SQL-process being displayed in the Emacs frame 2.

SITUATION: In the frame 1, in the buffer with SQL-mode, I call M-x sql-send-region to send a SQL-statement to the SQL-process.

I see the result on the laptop screen in frame 2. But in the meanwhile, the SQL-mode propably have detected there is no window in the Emacs frame 1, so Emacs opens a new window with the same buffer of the SQL-process.

As the result, there are two windows on two screens, viewing the same buffer (the SQL-process).

I would prevent that the SQL-mode opens a new window when sending a SQL-statement to the SQL-process. How could I 'tell' Emacs to prevent that?

EDIT as answered below, this question is solved by now. See the second option from the user bmag.

  • Please consider mentioning sql-send-region or sql-mode in your question title. I'd edit it myself, but ambiguous titles are a common feature of your questions. If you name them better, we can find them easier. :)
    – PythonNut
    Commented Feb 6, 2016 at 4:15
  • Thanks for the advice. I didn't have the idea opening new windows is specific for sql-mode.
    – ReneFroger
    Commented Feb 6, 2016 at 11:09

2 Answers 2


IIUC, the problem is that Emacs doesn't reuse the window from frame 2 when frame 1 is the selected frame. As per Window-Choice page in the manual, you can set pop-up-frames to t:

(setq pop-up-frames t)

Setting pop-up-frames to t will make Emacs consider windows on other frames whenever it needs to display a buffer. If you want Emacs to reuse windows from another frame only for SQL's process buffer, you need to add an entry to display-buffer-alist:

(push '("<buffer-name>" nil . ((reusable-frames . t))) display-buffer-alist)

Where <buffer-name> should be replaced with the name of SQL's process buffer. The documentation of display-buffer-alist and display-buffer (via C-h v and C-h f) explains them in great detail. You can read related pages from the Elisp manual to learn more.

Note that I am assuming sql-mode uses pop-to-buffer or similar to switch to SQL's process buffer. If it uses switch-to-buffer or split-window (it shouldn't), then my answer isn't likely to work.

  • The second option with pushing the buffer-name (which is *SQL*) to reusable frames, solved my question. I really appreciate you for that answer. Now I have a good base to figure out further how I could switch to the original frame afterwards. Thanks for the link about display-action. Emacs is so awesome, but it's sometimes too large to handle and so difficult to find your way around in all the documentation.
    – ReneFroger
    Commented Feb 6, 2016 at 11:52

Untested, as I don't use sql-mode:

(defun nadvice/sql-send-string (old-fun &rest args)
  (cl-letf* (((old-display-buffer (symbol-function #'display-buffer))
              ((symbol-function #'display-buffer)
               (lambda (buffer-or-name &rest iargs)
                 (unless (get-buffer-window-list buffer-or-name)
                   (apply old-display-buffer buffer-or-name iargs))))))
    (apply old-fun args)))

(advice-add 'sql-send-string :around #'nadvice/sql-send-string)
  • As you already said, it's untested. I ran into an error after applying your function: let*: Invalid function: (symbol-function (function display-buffer)).
    – ReneFroger
    Commented Feb 6, 2016 at 11:42
  • Hm... that is strange. I've corrected one typo in the code, but I doubt that's what was causing your issue.
    – PythonNut
    Commented Feb 6, 2016 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.