all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Working with buffers in frames
@ 2024-09-30 12:32 Heime
  2024-10-03  7:00 ` Madhu
  0 siblings, 1 reply; 2+ messages in thread
From: Heime @ 2024-09-30 12:32 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor


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. 




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Working with buffers in frames
  2024-09-30 12:32 Working with buffers in frames Heime
@ 2024-10-03  7:00 ` Madhu
  0 siblings, 0 replies; 2+ messages in thread
From: Madhu @ 2024-10-03  7:00 UTC (permalink / raw)
  To: help-gnu-emacs


* Heime <ZkvtfOj7K5MQr1sVd0Oy_wFQak3U2_yIkiWyAFiQmHQkUiICrSIL9jqKtmGi_tu7TP0OHRJHYnxLFB_NQ8CP96zCFLGEj75XZTZdj0da8lI=@protonmail.com> :
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.





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-10-03  7:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-30 12:32 Working with buffers in frames Heime
2024-10-03  7:00 ` Madhu

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.