Finding window with specified mode

278 Views Asked by At

I need to programmatically resize a window, so I don't know how to:

among all windows in the current frame find one that's running specified mode, e.g. "dired".

let's say, I have:

|-------+------------+-----|
| Dired | Emacs-lisp | Org |
|       | -x-        |     |
|       |            |     |
|-------+------------+-----|

(with point being at 2nd window) now I need to programmatically find window with Dired mode (note that it can be at any position) and adjust its width.

To adjust the width, I know I can use something like:

(defun fit-w ()
  (let ((fit-window-to-buffer-horizontally t))
(fit-window-to-buffer)))

but first I need to detect the window

1

There are 1 best solutions below

0
On

This can also be done in a more declarative/functional way. Return first window of current windows buffers using dired-mode or nil, if not found:

(cl-find-if
 (lambda (window)
   (with-current-buffer (window-buffer window) (eq major-mode 'dired-mode)))
 (window-list))