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: Thu, 03 Oct 2024 20:09:43 +1000 Message-ID: <8734ldlcpk.fsf@gmail.com> References: <87setpdv21.fsf@gmail.com> <86zfnxcg57.fsf@gnu.org> <877cb09ln4.fsf@gmail.com> <9005cccc-7545-4257-b2c0-885a13d3bde2@gmx.at> <86o74aa41b.fsf@gnu.org> <3d4546ac-70d9-4865-b42d-0dc50cb0f3a7@gmx.at> <878qvcgqpu.fsf@gmail.com> <0138d382-8e63-4b53-8c2c-fece826a7d0a@gmx.at> <87plooaw1w.fsf@gmail.com> <87v7yeykr0.fsf@gmail.com> <19ca7821-e034-4ae5-9ff6-570243329d74@gmx.at> <87r09224pe.fsf@gmail.com> <87ikudk62k.fsf@gmail.com> <0d879e95-c37e-416d-b439-daa6384c4f30@gmx.at> <878qv8kws2.fsf@gmail.com> <87ed4xvf60.fsf@gmail.com> <32650808-7cf9-4288-91b5-c7d78663de8f@gmx.at> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26687"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Eli Zaretskii , emacs-devel@gnu.org To: martin rudalics Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Oct 03 12:35:24 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 1swJB6-0006mP-LL for ged-emacs-devel@m.gmane-mx.org; Thu, 03 Oct 2024 12:35:24 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1swJAf-000257-RY; Thu, 03 Oct 2024 06:34:57 -0400 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 1swIou-00073g-0w for emacs-devel@gnu.org; Thu, 03 Oct 2024 06:12:33 -0400 Original-Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1swIoG-0008Ve-8h for emacs-devel@gnu.org; Thu, 03 Oct 2024 06:12:15 -0400 Original-Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-20b58f2e1f4so4570015ad.2; Thu, 03 Oct 2024 03:10:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727950189; x=1728554989; 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=ka8OoMRx7t4kBc6r893dyJqylUhXmNoBG8lE5Vv+ppg=; b=SIYqtR2JITGVvZYERAxfwk2oOJtWtgx9gfgFk2zD7NP5szEfbzBABcuKuBS/AZH+Io NLKH0liUGganuYyTQcydSJZgMC/RDX+d0kprey6y2cIMS58ifD+OYRCdQuhrjRmTSoiB cvFCDYFwj4APz9lubHnnHv/vBS6dGUSOv5qWQ7RA0N9xLTyrTA5rjzmw3TKrBBtDPCNI CvDbHRjXtub6ockaz5anAjGuDqY+1jHb6sFNIWwzxdkn41NpwmMIlj4BDnycFE0/aV0d wqFlQXOIzy3wBdcsaaHH9wPoLylurv/kRL4RzxbxIk0G5qUTNLli0o6MQRTeMwaPwS2+ tLfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727950189; x=1728554989; 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=ka8OoMRx7t4kBc6r893dyJqylUhXmNoBG8lE5Vv+ppg=; b=SeJcRgqDn2QHGV/ml2kYrAyP5iglzZRNYWQwh4B0E94gNERP1H5jjqmEQPHDIINxBk P8rdpZbbFUv2CNP9mc3V0rnY7PbMyXq3Znzqj6n9dLqoJzMgtZ0E+fRvBIyO00tN7cxX 1Iuo/a48Ee0ypr5MsenoKbzy5pjx7DF9j2J+F7BPdwSlGIExWhtxo0h7qKeCLP7klxBW Ub5pLc7MfVOiQehvAI4XoSGpAqvDznlRDAoSyfkRjreX7lnFr8Bm1tB06iFE7OAWZ3yi mTlH+fkqEuya7M6L+CihSSkMkzryJ625bRJAJRw6h0ELkGKrsxxySvYiFQhwegDdvUkB 9zeA== X-Forwarded-Encrypted: i=1; AJvYcCWPrwYYQPSRaHpVPtUR+4BARa8UCMiLa1IYmv9gchaluyPZFE6P/XNzcHa0CW4+BRdZtgfM7Kjz6EUdIg==@gnu.org X-Gm-Message-State: AOJu0Yz84SUBJRQEEICH+sMfXQDULBIIZMcCFDwUxXDiHJypnRwJwfc5 DvR1L2A6pjVmEMSIxR1Ct/SUo/dq4QlkrwE7rudW4BLWOwqDTj3GlG5oHg== X-Google-Smtp-Source: AGHT+IGJblFclSoiIAAMHcjUDZa0UX2ZW9wtrDm/EEvvLA0411J0qosx8xFmwaFFSopt+mYmxuX5kg== X-Received: by 2002:a17:90b:3149:b0:2e0:8e36:132 with SMTP id 98e67ed59e1d1-2e1845323c3mr7272465a91.3.1727950188818; Thu, 03 Oct 2024 03:09:48 -0700 (PDT) Original-Received: from pebl ([2001:8003:7816:8300:3370:7ec8:c89b:24e5]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e18f79bb0fsm3296857a91.31.2024.10.03.03.09.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Oct 2024 03:09:48 -0700 (PDT) In-Reply-To: <32650808-7cf9-4288-91b5-c7d78663de8f@gmx.at> (martin rudalics's message of "Thu, 3 Oct 2024 10:17:31 +0200") Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=pranshusharma366@gmail.com; helo=mail-pl1-x62f.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, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 03 Oct 2024 06:34:56 -0400 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:324280 Archived-At: martin rudalics writes: >>> I'd call this just 'window--transpose'. >> >> done > > Not really IIUC. sorry, it is done now > Then please say "the arguments are the same as for > `window--transpose'". yes, done > Once we have installed it, we'll consult Juri Linkov. He knows more > about keybindings in this area. BTW, you could have a look at the > function 'window-swap-states'. IIUC it should then be rewritten in > terms of the 'flip-windows-...' functions to get rid of window states > and the overlay rigmarole. Hmm, the only way I can imagine using the flip thing is modifying window tree to swap 2 windows in the subtree, and then set conf to (below . left) and do-not-convert-size to nil. This will just rebuild the whole window tree. > > Please test what happens if you have a keybinding for this and you hit > that key in the minibuffer window, for example, after typing C-h f. > It just acts as if you are calling it without prefix arg, I think that is expected behaviour imo. It just goes back to orignal if read-minibuffer-restore-windows is non nil. > Still a space missing after the "." > >> (defun flip-windows-vertically (&optional frame-or-window) >> "Horizontally flip windows of FRAME-OR-WINDOW. When the windows are > > Newline missing after ".". > >> flipped vertically, the window layout is made to it's reflection from >> the top edge. FRAME-OR-WINDOW must be a live frame or window and >> defaults to the selected frame. If FRAME-OR-WINDOW is a frame, flip from > > Space missing after ".". >> (defun transpose-windows--rearrange (frame-or-window conf do-not-convert-size) > ... third argument _of_ ... > >> previously vertically split. When is DO-NOT-CONVERT-SIZE non-nil, the > ah puncutation comments, I just failed my english writing exam on wednesday :( > Actually > > (zerop (window-child-count rwin)) > > is equivalent to > > (window-live-p rwin) > > and the latter should be cheaper. Thanks, I applied that change > What I forgot to ask so far: Have you completed copyright assignment > for Emacs? I can't look into this myself. I sent the form to assign@gnu.org, 15 days ago, still waiting for reply. Does it normally take this long? Code: ---------------------------------------- (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 live 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 t (cons (cons (window-pixel-height window) (window-pixel-width window)) (window-tree-pixel-sizes (window-top-child window) t)))) ((window-left-child window) (cons nil (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 frame-or-window) "Rotate windows of FRAME-OR-WINDOW anticlockwise by 90 degrees. FRAME-OR-WINDOW must be a live 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" (interactive `(,(and current-prefix-arg (window-parent)))) (let ((window (if (windowp frame-or-window) frame-or-window (window-main-window frame-or-window)))) (window--transpose window '(right . above) nil))) (defun rotate-windows-clockwise (&optional frame-or-window) "Rotate windows of FRAME-OR-WINDOW clockwise by 90 degrees. FRAME-OR-WINDOW must be a live 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" (interactive `(,(and current-prefix-arg (window-parent)))) (let ((window (if (windowp frame-or-window) frame-or-window (window-main-window frame-or-window)))) (window--transpose window '(left . below) nil))) (defun flip-windows-horizontally (&optional 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 live 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." (interactive `(,(and current-prefix-arg (window-parent)))) (let ((window (if (windowp frame-or-window) frame-or-window (window-main-window frame-or-window)))) (window--transpose window '(below . left) t))) (defun flip-windows-vertically (&optional 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 live 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 `(,(and current-prefix-arg (window-parent)))) (let ((window (if (windowp frame-or-window) frame-or-window (window-main-window frame-or-window)))) (window--transpose window '(above . right) t))) (defun transpose-windows (&optional 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 live 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." (interactive `(,(and current-prefix-arg (window-parent)))) (let ((window (if (windowp frame-or-window) frame-or-window (window-main-window frame-or-window)))) (window--transpose window '(right . below) nil))) (defun window--transpose (frame-or-window conf do-not-convert-size) "Rearrange windows of FRAME-OR-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." (pcase-let ((`(,rwin . ,frame) (if (framep frame-or-window) (cons (window-main-window frame-or-window) frame-or-window) (cons frame-or-window (window-frame frame-or-window))))) (if (or (not rwin) (window-live-p rwin)) (message "No windows to transpose") (let* ((fwin rwin) (selwin (frame-selected-window frame-or-window)) (win-tree (car (window-tree-pixel-sizes rwin)))) (while (not (window-live-p fwin)) (setq fwin (window-child fwin))) ;; All child windows need to be recursively deleted. (mapc (lambda (win) (when (and (windowp win) (not (eq win fwin))) (delete-window win))) ;; We know for sure that first 2 in the list are not ;; windows. (cddr (flatten-list win-tree))) (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 (car 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 (car subtree) 'car 'cdr) conf))) (if (listp win) ;; `win' is a window subtree. (window--transpose-1 win (split-window cwin split-size split-type t (seq-some (lambda (x) (and (windowp x) x)) (flatten-list win))) conf do-not-convert-size) ;; `win' is a window. (split-window cwin split-size split-type t win)))) (mapcar (lambda (e) (let ((window? (if (windowp (car e)) (car e) e))) (cons window? ;; The respective size of the window. (if (car 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))))