5

*Help* windows appear to have a special behavior. For example, if I open a help window using C-h f butterfly, the window will open in a separate half-page buffer. If I use the help feature again C-h f tetris, the same help-mode buffer will be reused to display the documentation for tetris. Is there a way to replicate this behavior for other windows? For example, I would like EWW windows to have a behavior similar to *Help*.

So far, I have this:

;; Reuse old *eww* windows.
;; Otherwise, pop up a new window.
(setq-default display-buffer-alist
              (cons '("^\\*\\eww\\*$"
                      (display-buffer-reuse-window
                       display-buffer-pop-up-window))
                     display-buffer-alist))

Is this method correct? I feel uneasy about the use of the use of regex to drive the behavior. Perhaps there is some other way of achieving the same result (e.g. using advice-add)?

Flux
  • 583
  • 2
  • 16
  • I'm not sure exactly what behavior you're looking for. But maybe start by looking at `C-h f with-help-window`. That's what's generally used to create and show a help window. – Drew Dec 31 '20 at 23:40
  • @Drew I've actually looked at `with-help-window`, but my Emacs Lisp isn't great. – Flux Jan 01 '21 at 00:04

2 Answers2

3

As @Drew says, it's not clear what you're looking for. However, since I found your question while creating my own function to reuse an eww window, I thought you might find it helpful.

It looks for a buffer called "*eww*" (the default name for an eww buffer). If there's a live window, then use that. Otherwise, call eww in the current window.

I've tried to document it fully. You can use C-h f, C-h v and C-h i d m elisp to learn more.

(defun my-lookup ()
  "Lookup symbol at point in PySide online documentation."
  (interactive)
  ;; define function variables
  ;; let form defines variables, the * means you can use one definition within another
  (let* ((sym (thing-at-point 'symbol)) ; get symbol at point
         (url (concat
                      "https://doc-snapshots.qt.io/qtforpython-5.15/PySide2/QtWidgets/"
                      sym
                      ".html"
                      ))  ; define a web address using that symbol
         (buff (get-buffer-window "*eww*")))  ; check if there exists an eww buffer, get its window
    ;; begin function behavior
    (if buff
        (with-selected-window buff
          (eww url))  ; if eww buffer exists, call eww command in that window
      (eww url))))  ; otherwise call it in the current window
NickD
  • 27,023
  • 3
  • 23
  • 42
Lorem Ipsum
  • 4,327
  • 2
  • 14
  • 35
0

Is this method correct? I feel uneasy about the use of the use of regex to drive the behavior.

You can use any logic you want to recognise the buffer. Here's an example which tests the major mode of the buffer, instead of its name:

(add-to-list 'display-buffer-alist
             (cons (lambda (buffer _alist)
                     (with-current-buffer buffer
                       (derived-mode-p 'eww-mode)))
                   (cons '(display-buffer-reuse-window
                           display-buffer-pop-up-window)
                         '((inhibit-same-window . nil)
                           (reusable-frames . visible)
                           (inhibit-switch-frame . nil)))))

You might also consider display-buffer-reuse-mode-window as one of your fallback display actions.

Perhaps there is some other way of achieving the same result (e.g. using advice-add)?

Advice is powerful and useful, but also not something to use unnecessarily. When there's already a mechanism provided, you definitely want to prefer that mechanism.

phils
  • 48,657
  • 3
  • 76
  • 115