5

tl;dr I'm looking for way to detect when an Emacs frame has been moved from one monitor to another in a multi-monitor setup.

I have code in my ~/.emacs which sets the default font size for all displayed buffers in a frame based on the physical size and resolution of the monitor hosting the Emacs frame. In a multi-monitor setup, I would like to execute the font-sizing logic when a frame is moved from one monitor to another with possibly different rez + physical dimensions.

I've not found a way to do this in stock Emacs (have been looking mostly at available hooks) nor have I found any community code which might, say, add a hook for monitor change events.

1
  • Maybe you could have your OS call emacsclient and evaluate some elisp when it notices a monitor change?
    – npostavs
    May 25 '16 at 14:05
5

One way to do this is to save the original monitor for each frame (using frame-monitor-attributes) and then run a timer, where you check the current monitor.

If you run an idle timer and run it a few timed every second, I don't think it will slow down Emacs much at the same time it will be almost instant.

In your question you said that your code "sets the default font size for all displayed buffers in a frame". I don't think this is a good approach, as a buffer can be made visible in more than one frame. A better solution would be to change the default font of the frame itself using set-default-font.

(A different approach would be to define a function to move the frame for you, and at the same time change the font size etc.)

5
  • Lindydancer, thanks for the suggestions. I'll look into the timer-based approach (though now I'm curious if Emacs is multi-threaded and if there's code on the 'Net for cron-like functionality within Emacs). Looking back at my ~/.emacs, I am actually setting the font per-frame so I'm good there.
    – nrvale0
    May 25 '16 at 13:52
  • This looks promising re scheduling: gnu.org/software/emacs/manual/html_node/elisp/Timers.html
    – nrvale0
    May 25 '16 at 14:02
  • much thanks...I was able to implement the timer-based font twiddling via run-with-idle-timer. I'm going to do a blog post with all of the details. :D
    – nrvale0
    May 25 '16 at 14:14
  • 1
    I'm glad to hear that you managed to solve this and I'm looking forward to reading the blog post. To answer your question: No, Emacs is not multi-threaded, at least on on the lisp level. When you execute lisp code, you can be certain that no other lisp code do something behind your back. Having said that, there are a number of packages implementing pseudo-multi-threading including those that spawn new Emacs processes in the background. May 25 '16 at 14:43
  • 1
    @nrvale0 did you ever finish that post?
    – ctietze
    Oct 4 '18 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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