From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Madhu Newsgroups: gmane.emacs.help Subject: Re: Working with buffers in frames Date: Thu, 03 Oct 2024 12:30:07 +0530 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4033"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:qqTHARORqBlxID4uNA79KBOO13U= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Oct 03 09:01:12 2024 Return-path: Envelope-to: geh-help-gnu-emacs@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 1swFpo-0000qS-41 for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 03 Oct 2024 09:01:12 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1swFp5-0006u9-T7; Thu, 03 Oct 2024 03:00:27 -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 1swFp0-0006tp-K9 for help-gnu-emacs@gnu.org; Thu, 03 Oct 2024 03:00:23 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1swFoy-0003Tc-LS for help-gnu-emacs@gnu.org; Thu, 03 Oct 2024 03:00:22 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1swFou-000AL7-H4 for help-gnu-emacs@gnu.org; Thu, 03 Oct 2024 09:00:16 +0200 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:148115 Archived-At: * Heime : Wrote on Mon, 30 Sep 2024 12:32:44 +0000: > After inserting some text, text properties and possibly some buttons in a > named buffer, I want this to be contained in some dedicated frame that is > separate from the current working frame. > > For instance, consider the following function > > (defun quest-background-colour-chart-b (hex-colours &optional bfrn) > "Display background colors for a given list of HEX-COLOURS in a new buffer." > > (interactive) > > (let* ( (colours hex-colours) > (bfname (or bfrn "Quest Colours")) > (dbuffer (get-buffer-create (concat "𒆳 " bfname)))) > > (with-current-buffer dbuffer > > (erase-buffer) > > ;; Set background colour for buffer > (face-remap-add-relative 'default :background "blue") > > (insert "\n") > > (dolist (hex colours) > (let* ( (hex-text (format " %s " hex)) > (text (format "Colour: %s\n" hex)) > (hex-pt (point)) ) > > (insert hex-text) > (add-text-properties hex-pt (point) > `(face (:foreground "white"))) > > (let ( (text-pt (point)) ) > (insert text) > (add-text-properties text-pt (point) > `(face (:background ,hex :foreground "black")))))) > > (goto-char (point-min))) > > (switch-to-buffer dbuffer))) > > Instead of the common switch-to-buffer I have begun calling > the following, to handle the buffer in the frame quest-frame. > > (select-frame-set-input-focus quest-frame) > (set-window-buffer (frame-selected-window quest-frame) dbuffer))) > > Still, one do not necessarily need to use select-frame-set-input-focus or > set-window-buffer directly. Because Emacs provides built-in functions like > pop-to-buffer, display-buffer, and switch-to-buffer, which can handle displaying > buffers in different frames. > > display-buffer is the most flexible function for displaying a buffer. It can > be used to specify rules for how and where to display the buffer, including > creating new frames. > > The same can be said of pop-to-buffer. But for switch-to-buffer you'd have > to manually switch to the frame first, then use switch-to-buffer to show the > buffer in that frame. view-buffer is similar to switch-to-buffer, but intended > for read-only viewing of a buffer. > > I would value some practical advice, examples, and useful approaches and takeaways > if one wants to display buffers in dedicated frames. First write your own function to display buffer in a dedicated $heime frame ``` (defvar $heime-frame nil) (defun display-buffer-in-heime-frame (buffer alist) (let* ((frame-created-p nil) (window-created-p nil) (heime-frame (cond ((and (framep $heime-frame) (frame-live-p $heime-frame)) $heime-frame) (t (setq frame-created-p t) (setq $heime-frame (make-frame '((name . "Heime Frame") (unsplittable . t) )))))) (window (frame-selected-window heime-frame))) (window--display-buffer buffer window (cond (frame-created-p 'frame) (t 'reuse))))) ``` Then change the call to switch-to-buffer in quest-background-colour-chart-b to ``` (display-buffer dbuffer '(display-buffer-in-heime-frame) $heime-frame) ``` Now calling (quest-background-colour-chart-b '("009090" "008080" )) will use your $heime-frame to display the buffer. If you to change the behaviour of pop-to-buffer so they will go to the new frame if the buffer is a quest color buffer. This can be done by adding an entry to display-buffer-alist ``` (setq display-buffer-alist '((".*Quest Col" . ((display-buffer-in-heime-frame) . ((dedicated . t) (unsplittable . t)))))) ``` Assuming your buffer names have to match the regexp. now (pop-to-buffer "𒆳 Quest Colours") will go to the $heime-frame. To make switch-to-buffer also got to the $heime-frame, read the doc for switch-to-buffer-obey-display-actions (what it says about pop-to-buffer-new-window, and advice pop-to-buffer-new-window. ``` (setq switch-to-buffer-obey-display-actions t) ``` Then (switch-to-buffer "𒆳 Quest Colours" nil nil) will also go to $heime-frame. Only lightly tested.