[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [External] : Re: Make window-list return windows for all frames

From: Arthur Miller
Subject: Re: [External] : Re: Make window-list return windows for all frames
Date: Thu, 15 Jun 2023 19:18:39 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Drew Adams <drew.adams@oracle.com> writes:

>> (defun window-list-by-mode (mode &optional all-frames)
>>   (let ((window-list))
>>     (walk-windows
>>      (lambda (w)
>>        (with-current-buffer (window-buffer w)
>>          (when (eq major-mode mode)
>>            (push (cons (prin1-to-string w) w) window-list))))
>>      nil all-frames)
>>     window-list))
> It's also possible to filter the buffers first.
> (defun windows-with-mode (mode &optional minibuf all-frames)
>   (let ((wins  ()))
>     (dolist (buf  (seq-filter
>                    (lambda (buf)
>                      (with-current-buffer buf
>                        (eq major-mode mode)))
>                    (buffer-list)))
>       (setq wins  (nconc (get-buffer-window-list
>                           buf minibuf all-frames)
>                          wins)))
>     wins))
> (windows-with-mode 'Info-mode nil 'visible)
> But it's no doubt faster to filter the `window-list',
> just as you've done, because there are typically many
> buffers that are not displayed.

Indeed, but as you say probably slower and seems even less elegant than the
niether-so-elegant let-wrap over walk-windows.

Personally I would prefer more functional interface to it, something like map,
but that takes a predicate to filter stuff out; lighter then cl-remove-if and
cl-remove-if-not (no :key and that stuff, no need to pull in cl-seq) and
more elisp like-ish. Something like:

(cull predicate list)

For example (cull #'oddp '(1 2 3 4 5)) => (2 4)

We can do it in Lisp:

(defun cull (predicate list)
  "Remove elements from LIST that satisfy PREDICATE."
  (remq 'nil (mapcar (lambda (elt)
               (if (funcall predicate elt) nil elt)) list)))

(cull (lambda (w)
        (with-selected-window w
          (not (eq major-mode 'Info-mode))))
        (window-list-1 nil nil t))

=> (#<window 95 on *info*> #<window 102 on *info*<1>>)

I would prefer it though implemented in C so we don't have to do the iteration
through the list twice, but considering the last experience, I guess I should
not suggest a patch :).

reply via email to

[Prev in Thread] Current Thread [Next in Thread]