Subject: bug#34516: Multi-monitor frame sets

From: Juri Linkov <[email-masked]>
Date: Wed, 20 Feb 2019 23:20:39 +0200

>   (display-monitor-attributes-list)
>   ;; ==>
>   '(((geometry 0 0 1920 1080)
>      (workarea 0 0 1920 1050)
>      (mm-size 677 381)
>      (name . "\\\\.\\DISPLAY1")
>      (frames ...))
>     ((geometry -1680 -1050 1680 1050)
>      (workarea -1680 -1050 1680 1050)
>      (mm-size 593 370)
>      (name . "\\\\.\\DISPLAY2")
>      (frames ...)))

I see that ‘geometry’ is the same as ‘workarea’ on the second monitor,
but slightly different on the first one.  Mine are all the same,
so I don't know whether to use ‘geometry’ or ‘workarea’.

Meanwhile I found a mistake in my previous patch.  I assumed that
both pairs of attributes top/left and height/width for ‘make-frame’
are measured in pixels.  In fact, only top/left are in pixels,
whereas height/width are in text units.  So needed to wrap them
in ‘text-pixels’.  Now with this patch everything is fine:

diff --git a/lisp/frame.el b/lisp/frame.el
index dc81302939..208748ef1d 100644
--- a/lisp/frame.el
+++ b/lisp/frame.el
@@ -644,9 +644,37 @@ window-system-for-display
 (defun make-frame-on-display (display &optional parameters)
   "Make a frame on display DISPLAY.
 The optional argument PARAMETERS specifies additional frame parameters."
-  (interactive "sMake frame on display: ")
+  (interactive (list (completing-read
+                      (format "Make frame on display: ")
+                      (delete-dups
+                       (mapcar (lambda (frame)
+                                 (frame-parameter frame 'display))
+                               (frame-list))))))
   (make-frame (cons (cons 'display display) parameters)))
 
+(defun make-frame-on-monitor (monitor &optional display parameters)
+  "Make a frame on monitor MONITOR.
+The optional argument PARAMETERS specifies additional frame parameters."
+  (interactive (list (completing-read
+                      (format "Make frame on monitor: ")
+                      (mapcar (lambda (a)
+                                (cdr (assq 'name a)))
+                              (display-monitor-attributes-list)))))
+  (let* ((monitor-geometry (car (delq nil (mapcar (lambda (a)
+                                                    (when (equal (cdr (assq 'name a)) monitor)
+                                                      (cdr (assq 'geometry a))))
+                                                  (display-monitor-attributes-list display)))))
+         (frame-geometry (x-parse-geometry (format "%dx%d+%d+%d"
+                                                   (nth 2 monitor-geometry)
+                                                   (nth 3 monitor-geometry)
+                                                   (nth 0 monitor-geometry)
+                                                   (nth 1 monitor-geometry))))
+         (frame-geometry-in-pixels `((top . ,(cdr (assq 'top frame-geometry)))
+                                     (left . ,(cdr (assq 'left frame-geometry)))
+                                     (height . (text-pixels . ,(cdr (assq 'height frame-geometry))))
+                                     (width . (text-pixels . ,(cdr (assq 'width frame-geometry)))))))
+    (make-frame (append frame-geometry-in-pixels parameters))))
+
 (declare-function x-close-connection "xfns.c" (terminal))
 
 (defun close-display-connection (display)