Skip to content

Instantly share code, notes, and snippets.

@bradhowes
Created August 1, 2025 20:30
Show Gist options
  • Save bradhowes/1a2a96cf5ef18756f692702e91264904 to your computer and use it in GitHub Desktop.
Save bradhowes/1a2a96cf5ef18756f692702e91264904 to your computer and use it in GitHub Desktop.
Bits of frame manipulation from my init.el to reproduce positioning issue
;;; init.el --- load the full configuration -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:
(defconst my/screen-laptop (intern "my/screen-laptop")
"Symbol to indicate display is MacBook Pro 16\" laptop screen.")
(defconst my/screen-4k (intern "my/screen-4k")
"Symbol to indicate display is 4K screen.")
(defconst my/screen-laptop-4k (intern "my/screen-laptop-4k")
"Symbol to indicate display width is laptop and 1 4K screen.")
(defconst my/screen-4k-4k (intern "my/screen-4k-4k")
"Symbol to indicate display width is 2 4K screens.")
(defconst my/screen-laptop-4k-4k (intern "my/screen-laptop-4k-4k")
"Symbol to indicate display width is laptop and 2 4K screens.")
(defconst my/screen-terminal (intern "my/screen-terminal")
"Symbol to indicate display is a terminal.")
(defconst my/laptop-screen-width 2056
"MacBook Pro 16\" M1 screen width in pixels.")
(defconst my/4k-screen-width 3840
"4K external display width in pixels.")
(defun my/screen-layout ()
"Identify current screen layout.
Uses result from `display-pixel-width' to determine what monitors
there are. Better would be to use `display-monitor-attributes-list'
like done in `my/frame-top'.
Returns one of the follow symbols based on width:
- `my/screen-laptop' -- only laptop screen
- `my/screen-4k' -- only 4K monitor.
- `my/screen-laptop-4k' -- laptop screen + 4K monitor.
- `my/screen-4k-4k' -- two 4K monitors.
- `my/screen-laptop-4k-4k' -- laptop screen + 2 4K monitors.
- `my/screen-terminal' -- unknown screen."
(declare (side-effect-free t))
(let* ((width (display-pixel-width nil))
(value (cond ((= width my/laptop-screen-width) my/screen-laptop)
((= width my/4k-screen-width) my/screen-4k)
((= width (+ my/laptop-screen-width my/4k-screen-width)) my/screen-laptop-4k)
((= width (* my/4k-screen-width 2)) my/screen-4k-4k)
((= width (+ my/laptop-screen-width (* 2 my/4k-screen-width))) my/screen-laptop-4k-4k)
(t my/screen-terminal))))
(message "my/screen-layout: %s" value)
value))
(defun my/is-laptop (layout)
"T if LAYOUT is laptop."
(declare (side-effect-free t))
(eq layout my/screen-laptop))
(defun my/is-4k (layout)
"T if LAYOUT is kind with at least 4K area."
(declare (side-effect-free t))
(memq layout '(my/screen-4k my/screen-laptop-4k my/screen-4k-4k my/screen-laptop-4k-4k)))
(defun my/rows (layout)
"The number of rows to show in a frame shown on LAYOUT."
(declare (side-effect-free t))
(if (my/is-4k layout) 103 (if (my/is-laptop layout) 88 40)))
(defun my/cols (layout)
"The number of columns to show in a frame shown on LAYOUT."
(declare (side-effect-free t))
(if (or (my/is-4k layout) (my/is-laptop layout)) 132 80))
(defun my/frame-pixel-width (layout)
"Width in pixels of a normal frame shown on LAYOUT.
These values are hard-coded based on current settings.
Probably a better way to figure this out."
(declare (side-effect-free t))
(if (my/is-4k layout) 1338 944))
(defun my/frame-top ()
"The top of the display area.
NOTE: this assumes that the laptop display, if present,
is on the left of any monitors."
(declare (side-effect-free t))
(let* ((settings (display-monitor-attributes-list))
(top (nth 2 (if (eq (length settings) 1)
(car (car settings))
(car (car (cdr settings)))))))
;; (setq top -1267)
;; (setq top 0)
(message "top: %d" top)
top))
(defun my/frame-initial-left (layout)
"Pixels to use for the `left' of a frame on LAYOUT.
This is to be used for the `initial-frame-alist' configuration."
(declare (side-effect-free t))
;; Use the first external monitor if there is one.
(if (memq layout '(my/screen-laptop-4k my/screen-laptop-4k-4k))
2056
0))
(defun my/initial-frame-alist (layout)
"Make alist to use for the initial frame on LAYOUT.
Frame's left side is flush with the left side of the main display."
(declare (side-effect-free t))
(list (cons 'width (my/cols layout))
(cons 'height (my/rows layout))
(cons 'top (my/frame-top))
(cons 'left (my/frame-initial-left layout))))
(defun my/frame-default-left (layout)
"Pixels to use for the `left' of a frame on LAYOUT.
This is to be use for the `default-frame-aliat' configuration."
(declare (side-effect-free t))
(+ (my/frame-initial-left layout) (my/frame-pixel-width layout)))
(defun my/default-frame-alist (layout)
"Make alist to use for the default frame on LAYOUT.
Frame's left side is next to the right side of the initial frame."
(declare (side-effect-free t))
(list (cons 'width (my/cols layout))
(cons 'height (my/rows layout))
(cons 'top (my/frame-top))
(cons 'left (my/frame-default-left layout))))
(defun my/update-screen-frame-alists (layout)
"Update frame alists for current LAYOUT."
(modify-all-frames-parameters (list (cons 'top (my/frame-top))))
(setq initial-frame-alist (my/initial-frame-alist layout))
(message "initial-frame: %s" initial-frame-alist)
(message "default-frame: %s" default-frame-alist))
(defun my/screen-layout-changed ()
"Recalculate values based on screen layout."
(interactive)
(let ((layout (my/screen-layout)))
(message "screen layout: %s" layout)
(my/update-screen-frame-alists layout)))
(my/screen-layout-changed)
(provide 'repro)
;;; repro.el ends here.
@bradhowes
Copy link
Author

Save above to ~/repro.el and then start Emacs from shell:

% /Applications/Emacs.app/Contents/MacOS/Emacs -Q -l ~/repro

Play with my/frame-top to adjust vertical positioning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment