From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Pranshu Sharma Newsgroups: gmane.emacs.devel Subject: Re: Add function to rotate/transpose all windows Date: Sun, 17 Nov 2024 12:45:35 +1000 Message-ID: <87cyiuefxs.fsf@gmail.com> References: <87setpdv21.fsf@gmail.com> <87zfms6z1a.fsf@gmail.com> <71571413-02ba-4e3c-ad43-35c110811fab@gmx.at> <875xpfns32.fsf@gmail.com> <0a2f09a0-4115-4421-a391-30d27e7d0821@gmx.at> <87ses9k9wo.fsf@gmail.com> <877c9ersei.fsf@gmail.com> <51068b75-e12d-4161-9a63-8c280e8b2668@gmx.at> <8734k1gnt9.fsf@gmail.com> <8a2007d9-d501-404b-966d-57a7a51310ef@gmx.at> <87ses0r81d.fsf@gmail.com> <69658762-5fc7-4a9d-9262-528dfd9e93cd@gmx.at> <87wmhb2yew.fsf@gmail.com> <801bbd24-8f79-48a4-9615-f5ef21b2341e@gmx.at> <87ikstsu7f.fsf@gmail.com> <68dc98d6-42c0-431a-aefe-eecb02991764@gmx.at> <875xosy316.fsf@gmail.com> <87bjyfcncu.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40543"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Juri Linkov , Eli Zaretskii , emacs-devel@gnu.org To: martin rudalics Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Nov 17 07:01:22 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tCYLZ-000ANM-B0 for ged-emacs-devel@m.gmane-mx.org; Sun, 17 Nov 2024 07:01:21 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tCYKl-0000B3-GT; Sun, 17 Nov 2024 01:00:31 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tCVHy-00065a-0L for emacs-devel@gnu.org; Sat, 16 Nov 2024 21:45:26 -0500 Original-Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tCVHw-00075e-Av; Sat, 16 Nov 2024 21:45:25 -0500 Original-Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-72410cc7be9so2459012b3a.0; Sat, 16 Nov 2024 18:45:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731811522; x=1732416322; darn=gnu.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=FkAN+Z1oMthHeOFRT4txh/iTBejWMmQQGegqXyzYU5o=; b=O6bGHbzGruWZeSQZksRIXWp/w4AGtSN05cQ+SvtR6NP42CO1zRnJf+k4jYXQ/da0Xg csyd/0ahCj85WHsptlWwScUlCrDf+G7/T9ANuEhrZmnlh2ybNYBJMwQKLW+tENktC9ur hKK7rGQQaCaMmpFNzuDUI4JmKkMbHS22tZgSzmD6ngTK6GdKqrSeXhMw88KXuKfTr0wF +0FI3tUyLyaYiXbVI4wVkQH7/sHJXuih9yEGnU4aiPiRfgG+hBpiNrpHPZH3zYbUN0g6 iotWWioWE0aeCfzx+kIK4QPVSIIM+7pMSzOKyxq6AmBEZGIInVAdAPiwAUAgrfXWEpU5 rPGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731811522; x=1732416322; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=FkAN+Z1oMthHeOFRT4txh/iTBejWMmQQGegqXyzYU5o=; b=UG50AQ3geWeId5xoGXWg8OZONVtiprA0LECtENAcpeqi/WAKwg99U5L1CxH78c60yK AGzhjfLoBk6c/43DpT7YVC4evEeo7Q6v0bCxKzQN2es1w+9nlh8A/P7fQR5lWCecPhm2 aNubQVwXCvub6SUxAHNpZObrGl8zvRAjha1p0oDJqkMygZX4tltztBe2nN2QVa4mdK5J WJlpmEZh8xCl2pRZ9VNIX8gBmfyQWOJqx16blz3tNKKQNvqEVfltMIe5LGM6wBgIinDK 9YdGtjg2GsoEPl3AZlGDSQf0eGriadMcXOm/R9vM6xkMw6K1vGQxkAX7/oCCo2O8hQlE 4rVw== X-Forwarded-Encrypted: i=1; AJvYcCUzuUHPdfu3egIbzTTqOqoy0aAIlMFoU4B81MPSShW1OktwuEBsz7/YmCcpE6CCh5YD8Mli@gnu.org, AJvYcCXonpCpYewDUPUcjOz1PWHsOAeJXUDEJwQvKGlpA0iDgRSNrIrZdf8QEeczNDvimP5iQnD/YZr/Bnat0eE=@gnu.org X-Gm-Message-State: AOJu0Yy2mOCEiyavqHsm+XEJYzUDVBIt4JbYFBSBF9MEyJmTCCRwuQuR rsXivYNub0+lf9XZXlxl3i3ER8u5Er4NKCiUkec+Kvs7l8Dp/Lna8zi1Cw== X-Google-Smtp-Source: AGHT+IGXjd/g21rhR27wxC37uXeqLlLFWjQOjt7gyH6q+w6rGd/CVNCJnS5kZFYG1kwD9PurxSp8qg== X-Received: by 2002:a05:6a00:998:b0:724:58dd:43aa with SMTP id d2e1a72fcca58-72476bbaa26mr10760196b3a.14.1731811521941; Sat, 16 Nov 2024 18:45:21 -0800 (PST) Original-Received: from pranshu-ThinkPad-E560 ([2001:8003:7816:8300:38e5:d3bf:3d39:d377]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-724771c0ab1sm3680588b3a.97.2024.11.16.18.45.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Nov 2024 18:45:21 -0800 (PST) In-Reply-To: (martin rudalics's message of "Sat, 16 Nov 2024 17:54:40 +0100") Received-SPF: pass client-ip=2607:f8b0:4864:20::42f; envelope-from=pranshusharma366@gmail.com; helo=mail-pf1-x42f.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sun, 17 Nov 2024 01:00:25 -0500 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:325486 Archived-At: --=-=-= Content-Type: text/plain martin rudalics writes: > When I set up my frame thusly > > (defun window-transpose-setup () > (interactive) > (let ((foo (get-buffer-create "*foo*")) > window) > (delete-other-windows) > (setq window (split-window nil nil t)) > (set-window-buffer window foo) > (split-window window) > (setq window (split-window window nil t)) > (split-window window) > (select-window window))) > > and then run 'rotate-windows-clockwise' I'm told > > which is one of the problems I tried to make you aware of. IIUC window > 44 is a deleted parent window and you try to get its pixel height. But > Emacs doesn't allow to retrieve the width or height of a deleted window. > In this case, cwin was the undead window that exists. I just had a bug in delete-other-windows-internal where the WINDOW argument was nil. I just changed that, and didn't touch the window--transpose-1 code and it works now. Look in the attached file. --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=window-transpose.el Content-Transfer-Encoding: quoted-printable ;;; -*- lexical-binding:t -*- (defun window-tree-pixel-sizes (window &optional next) "Return pixel sizes of all windows rooted at WINDOW. The return value is a list where each window is represented either by a triple whose first element is either t for an internal window that is a horizontal combination, nil for an internal window that is a vertical combination, or the window itself for a valid window. The second element is a cons of the pixel height and pixel width of the window. The third element is specified for internal windows only and recursively lists that window's child windows using the same triple structure." (let (list) (while window (setq list (cons (cond ((window-top-child window) (cons (cons t window) (cons (cons (window-pixel-height window) (window-pixel-width window)) (window-tree-pixel-sizes (window-top-child window) t)))) ((window-left-child window) (cons (cons nil window) (cons (cons (window-pixel-height window) (window-pixel-width window)) (window-tree-pixel-sizes (window-left-child window) t)))) (t (list window (cons (window-pixel-height window) (window-pixel-width window))))) list)) (setq window (when next (window-next-sibling window)))) (nreverse list))) (defun rotate-windows-anticlockwise (&optional subtree frame-or-window) "Rotate windows of FRAME-OR-WINDOW anticlockwise by 90 degrees. FRAME-OR-WINDOW must be a valid frame or window and defaults to the selected frame. If FRAME-OR-WINDOW is a frame, rotate the main window of the frame, otherwise rotate FRAME-OR-WINDOW. See `rotate-windows-clockwise' for how to rotate windows in the opposite direction. If SUBTREE is non-nil, the function will act as if FRAME-OR-WINDOW is the parent window of the selected window." (interactive "P") (let ((window (cond (subtree (window-parent)) ((windowp frame-or-window) frame-or-window) (t (window-main-window frame-or-window))))) (window--transpose window '(right . above) nil))) (defun rotate-windows-clockwise (&optional subtree frame-or-window) "Rotate windows of FRAME-OR-WINDOW clockwise by 90 degrees. FRAME-OR-WINDOW must be a valid frame or window and defaults to the selected frame. If FRAME-OR-WINDOW is a frame, rotate the main window of the frame, otherwise rotate FRAME-OR-WINDOW. See `rotate-windows-anticlockwise' for how to rotate windows in the opposite direction. If SUBTREE is non-nil, the function will act as if FRAME-OR-WINDOW is the parent window of the selected window." (interactive "P") (let ((window (cond (subtree (window-parent)) ((windowp frame-or-window) frame-or-window) (t (window-main-window frame-or-window))))) (window--transpose window '(left . below) nil))) (defun flip-windows-horizontally (&optional subtree frame-or-window) "Horizontally flip windows of FRAME-OR-WINDOW. When the windows are flipped horzontally, the window layout is made to it's reflection from the side edge. FRAME-OR-WINDOW must be a valid frame or window and defaults to the selected frame. If FRAME-OR-WINDOW is a frame, flip from the main window of the frame, otherwise flip from FRAME-OR-WINDOW. See `flip-windows-vertically' for how to flip windows vertically. If SUBTREE is non-nil, the function will act as if FRAME-OR-WINDOW is the parent window of the selected window." (interactive "P") (let ((window (cond (subtree (window-parent)) ((windowp frame-or-window) frame-or-window) (t (window-main-window frame-or-window))))) (window--transpose window '(below . left) t))) (defun flip-windows-vertically (&optional subtree frame-or-window) "Horizontally flip windows of FRAME-OR-WINDOW. When the windows are flipped vertically, the window layout is made to it's reflection from the top edge. FRAME-OR-WINDOW must be a valid frame or window and defaults to the selected frame. If FRAME-OR-WINDOW is a frame, flip from the main window of the frame, otherwise flip from FRAME-OR-WINDOW. See `flip-windows-horizontally' for how to flip windows horizontally." (interactive "P") (let ((window (cond (subtree (window-parent)) ((windowp frame-or-window) frame-or-window) (t (window-main-window frame-or-window))))) (window--transpose window '(above . right) t))) (defun transpose-windows (&optional subtree frame-or-window) "Transpose windows of FRAME-OR-WINDOW. Rearrange windows such that where a horizontal split was used a vertical one is used instead, and vice versa. FRAME-OR-WINDOW must be a valid frame or window and defaults to the selected frame. If FRAME-OR-WINDOW is a frame, transpose the main window of the frame, otherwise transpose FRAME-OR-WINDOW. If SUBTREE is non-nil, the function will act as if FRAME-OR-WINDOW is the parent window of the selected window." (interactive "P") (let ((window (cond (subtree (window-parent)) ((windowp frame-or-window) frame-or-window) (t (window-main-window frame-or-window))))) (window--transpose window '(right . below) nil))) (defun window--transpose (window conf do-not-convert-size) "Rearrange windows of WINDOW recursively. CONF should be a cons cell: (HORIZONTAL-SPLIT . VERTICAL-SPLIT) where HORIZONTAL-SPLIT will be used as the third argument of `split-window' when splitting a window that was previously horizontally split, and VERTICAL-SPLIT as third argument of `split-window' for a window that was previously vertically split. If DO-NOT-CONVERT-SIZE non-nil, the size argument of the window-split is converted from vertical to horizontal or vice versa, with the same proportion of the total split." (if (or (not window) (window-live-p window)) (message "No windows to transpose") (let* ((frame (window-frame window)) (fwin window) (selwin (frame-selected-window window)) (win-tree (car (window-tree-pixel-sizes window)))) (while (not (window-live-p fwin)) (setq fwin (window-child fwin))) ;; All child windows need to be recursively deleted. (delete-other-windows-internal fwin window) (window--transpose-1 win-tree fwin conf do-not-convert-size) ;; Go back to previously selected window. (set-frame-selected-window frame selwin)))) (defun window--transpose-1 (subtree cwin conf do-not-convert-size) "Subroutine of `window--transpose'. SUBTREE must be in the format of the result of `window-tree-pixel-sizes'. CWIN is the current window through which the window splits are made. The CONF and DO-NOT-CONVERT-SIZE arguments are the same as the ones in `window--transpose'." ;; `ilen' is the max size a window could be of given the split type. ;; `flen' is max size the window could be converted to the opposite ;; of the given split type. (pcase-let ((`(,ilen . ,flen) (if (caar subtree) (cons (float (car (cadr subtree))) (float (window-pixel-width cwin))) (cons (float (cdr (cadr subtree))) (float (window-pixel-height cwin)))))) (mapc (pcase-lambda (`(,win . ,size)) (let ((split-size (- (if do-not-convert-size size (round (* flen (/ size ilen)))))) (split-type (funcall (if (caar subtree) 'car 'cdr) conf))) (if (listp win) ;; `win' is a window subtree. (window--transpose-1 win (split-window cwin split-size split-type t (progn (while (listp (caaddr win)) (setq win (caddr win))) (cons (caaddr win) (cdar win)))) conf do-not-convert-size) ;; `win' is a window. (split-window cwin split-size split-type t ;; THIS IS WHERE IS GOES OF THE MAP spew ;; (cons win (cdar subtree)) ;; To get a basic working thing, comment above line and uncoment below = line win )))) (mapcar (lambda (e) (let ((window? (if (windowp (car e)) (car e) e))) (cons window? ;; The respective size of the window. (if (caar subtree) (car (cadr e)) (cdr (cadr e)))))) ;; By using cdddr, we ignore window split type, sizes and the ;; first window (it's implicitly created). (nreverse (cdddr subtree)))) ;; (caaddr subtree) is the first window. (unless (windowp (caaddr subtree)) (window--transpose-1 (caddr subtree) cwin conf do-not-convert-size)))) (define-key global-map (kbd "C-x w t") 'transpose-windows) --=-=-=--