[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Working with buffers in frames
From: |
Madhu |
Subject: |
Re: Working with buffers in frames |
Date: |
Thu, 03 Oct 2024 12:30:07 +0530 |
* 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.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: Working with buffers in frames,
Madhu <=