0

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

iLemming
  • 34,477
  • 60
  • 195
  • 309
  • 1
    How about `walk-windows` with the argument/function being something like `(walk-windows (lambda (window) (with-current-buffer (window-buffer window) (when (eq major-mode 'dired-mode) (message "YES!!! -- window: %s" window)))))` See the doc-string for `walk-windows` for additional arguments you may wish to include -- `M-x describe-function RET walk-windows RET` – lawlist Jun 05 '16 at 04:15
  • yeah, initially I thought about walk-windows. Looked like a slightly complicated overkill. I guess I'm gonna try that, thanks! – iLemming Jun 05 '16 at 04:17
  • Now that I think a little more about it, you may also wish to use `(with-selected-window window . . .)` instead of `with-current-buffer` so that you can operate directly on the target window if the criteria is met. – lawlist Jun 05 '16 at 04:21
  • yeah that one I have figured already. thanks again! – iLemming Jun 05 '16 at 05:01

1 Answers1

2

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))
Jürgen Hötzel
  • 18,997
  • 3
  • 42
  • 58