1 ;;; gnus-win.el --- window configuration functions for Gnus
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
32 (defgroup gnus-windows nil
33 "Window configuration."
36 (defcustom gnus-use-full-window t
37 "*If non-nil, use the entire Emacs screen."
41 (defcustom gnus-window-min-width
2
42 "*Minimum width of Gnus buffers."
46 (defcustom gnus-window-min-height
1
47 "*Minimum height of Gnus buffers."
51 (defcustom gnus-always-force-window-configuration nil
52 "*If non-nil, always force the Gnus window configurations."
56 (defcustom gnus-use-frames-on-any-display nil
57 "*If non-nil, frames on all displays will be considered usable by Gnus.
58 When nil, only frames on the same display as the selected frame will be
59 used to display Gnus windows."
64 (defvar gnus-buffer-configuration
103 (article 1.0 point
)))
106 (article 1.0 point
)))
110 (edit-form 1.0 point
)))
114 (edit-score 1.0 point
)))
118 (edit-form 1.0 point
)))
125 (message 1.0 point
)))
128 (message 1.0 point
)))
131 (message 1.0 point
)))
135 (message 1.0 point
)))
139 ("*Shell Command Output*" 1.0)))
142 (if gnus-bug-create-help-buffer
'("*Gnus Help Bug*" 0.5))
143 ("*Gnus Bug*" 1.0 point
)))
147 ("*Score Trace*" 1.0)))
151 ("*Score Words*" 1.0)))
155 ("*Split Trace*" 1.0)))
162 (message 1.0 point
)))
169 (mml-preview 1.0 point
))))
170 "Window configuration for all possible Gnus buffers.
171 See the Gnus manual for an explanation of the syntax used.")
173 (defvar gnus-window-to-buffer
174 '((group . gnus-group-buffer
)
175 (summary . gnus-summary-buffer
)
176 (article . gnus-article-buffer
)
177 (server . gnus-server-buffer
)
178 (browse .
"*Gnus Browse Server*")
179 (edit-group . gnus-group-edit-buffer
)
180 (edit-form . gnus-edit-form-buffer
)
181 (edit-server . gnus-server-edit-buffer
)
182 (edit-score . gnus-score-edit-buffer
)
183 (message . gnus-message-buffer
)
184 (mail . gnus-message-buffer
)
185 (post-news . gnus-message-buffer
)
186 (faq . gnus-faq-buffer
)
187 (tree . gnus-tree-buffer
)
188 (score-trace .
"*Score Trace*")
189 (split-trace .
"*Split Trace*")
190 (info . gnus-info-buffer
)
191 (category . gnus-category-buffer
)
192 (article-copy . gnus-article-copy
)
193 (draft . gnus-draft-buffer
)
194 (mml-preview . mml-preview-buffer
))
195 "Mapping from short symbols to buffer names or buffer variables.")
197 (defcustom gnus-configure-windows-hook nil
198 "*A hook called when configuring windows."
203 ;;; Internal variables.
205 (defvar gnus-current-window-configuration nil
206 "The most recently set window configuration.")
208 (defvar gnus-created-frames nil
)
209 (defvar gnus-window-frame-focus nil
)
211 (defun gnus-kill-gnus-frames ()
212 "Kill all frames Gnus has created."
213 (while gnus-created-frames
214 (when (frame-live-p (car gnus-created-frames
))
215 ;; We slap a condition-case around this `delete-frame' to ensure
216 ;; against errors if we try do delete the single frame that's left.
218 (delete-frame (car gnus-created-frames
))))
219 (pop gnus-created-frames
)))
222 (defun gnus-add-configuration (conf)
223 "Add the window configuration CONF to `gnus-buffer-configuration'."
224 (setq gnus-buffer-configuration
225 (cons conf
(delq (assq (car conf
) gnus-buffer-configuration
)
226 gnus-buffer-configuration
))))
228 (defvar gnus-frame-list nil
)
230 (defun gnus-window-to-buffer-helper (obj)
231 (cond ((not (symbolp obj
))
240 (defun gnus-configure-frame (split &optional window
)
241 "Split WINDOW according to SPLIT."
242 (let* ((current-window (or (get-buffer-window (current-buffer)) (selected-window)))
243 (window (or window current-window
)))
244 (select-window window
)
245 ;; The SPLIT might be something that is to be evalled to
246 ;; return a new SPLIT.
247 (while (and (not (assq (car split
) gnus-window-to-buffer
))
248 (symbolp (car split
)) (fboundp (car split
)))
249 (setq split
(eval split
)))
250 (let* ((type (car split
))
252 (len (if (eq type
'horizontal
) (window-width) (window-height)))
254 (window-min-width (or gnus-window-min-width window-min-width
))
255 (window-min-height (or gnus-window-min-height window-min-height
))
256 s result new-win rest comp-subs size sub
)
258 ;; Nothing to do here.
260 ;; Don't switch buffers.
262 (and (memq 'point split
) window
))
263 ;; This is a buffer to be selected.
264 ((not (memq type
'(frame horizontal vertical
)))
265 (let ((buffer (cond ((stringp type
) type
)
266 (t (cdr (assq type gnus-window-to-buffer
))))))
268 (error "Invalid buffer type: %s" type
))
269 (let ((buf (gnus-get-buffer-create
270 (gnus-window-to-buffer-helper buffer
))))
271 (when (buffer-name buf
)
272 (if (eq buf
(window-buffer (selected-window)))
274 (switch-to-buffer buf
))))
275 (when (memq 'frame-focus split
)
276 (setq gnus-window-frame-focus window
))
277 ;; We return the window if it has the `point' spec.
278 (and (memq 'point split
) window
)))
279 ;; This is a frame split.
281 (unless gnus-frame-list
282 (setq gnus-frame-list
(list (window-frame current-window
))))
284 params frame fresult
)
285 (while (< i
(length subs
))
286 ;; Frame parameter is gotten from the sub-split.
287 (setq params
(cadr (elt subs i
)))
288 ;; It should be a list.
289 (unless (listp params
)
291 ;; Create a new frame?
292 (unless (setq frame
(elt gnus-frame-list i
))
293 (nconc gnus-frame-list
(list (setq frame
(make-frame params
))))
294 (push frame gnus-created-frames
))
295 ;; Is the old frame still alive?
296 (unless (frame-live-p frame
)
297 (setcar (nthcdr i gnus-frame-list
)
298 (setq frame
(make-frame params
))))
299 ;; Select the frame in question and do more splits there.
301 (setq fresult
(or (gnus-configure-frame (elt subs i
)) fresult
))
303 ;; Select the frame that has the selected buffer.
305 (select-frame (window-frame fresult
)))))
306 ;; This is a normal split.
308 (when (> (length subs
) 0)
309 ;; First we have to compute the sizes of all new windows.
311 (setq sub
(append (pop subs
) nil
))
312 (while (and (not (assq (car sub
) gnus-window-to-buffer
))
313 (symbolp (car sub
)) (fboundp (car sub
)))
314 (setq sub
(eval sub
)))
317 (setq size
(cadar comp-subs
))
318 (cond ((equal size
1.0)
319 (setq rest
(car comp-subs
))
322 (setq s
(floor (* size len
))))
326 (error "Invalid size: %s" size
)))
327 ;; Try to make sure that we are inside the safe limits.
329 ((eq type
'horizontal
)
330 (setq s
(max s window-min-width
)))
332 (setq s
(max s window-min-height
))))
333 (setcar (cdar comp-subs
) s
)
335 ;; Take care of the "1.0" spec.
337 (setcar (cdr rest
) (- len total
))
338 (error "No 1.0 specs in %s" split
))
339 ;; The we do the actual splitting in a nice recursive
341 (setq comp-subs
(nreverse comp-subs
))
343 (if (null (cdr comp-subs
))
344 (setq new-win window
)
346 (split-window window
(cadar comp-subs
)
347 (eq type
'horizontal
))))
348 (setq result
(or (gnus-configure-frame
349 (car comp-subs
) window
)
351 (select-window new-win
)
352 (setq window new-win
)
353 (setq comp-subs
(cdr comp-subs
))))
354 ;; Return the proper window, if any.
356 (select-window result
)))))))
358 (defvar gnus-frame-split-p nil
)
360 (defun gnus-configure-windows (setting &optional force
)
365 ((window-configuration-p setting
)
366 (set-window-configuration setting
))
368 (setq gnus-current-window-configuration setting
)
369 (setq force
(or force gnus-always-force-window-configuration
))
370 (let ((split (if (symbolp setting
)
371 (cadr (assq setting gnus-buffer-configuration
))
375 (setq gnus-frame-split-p nil
)
378 (error "No such setting in `gnus-buffer-configuration': %s" setting
))
380 (if (and (setq all-visible
(gnus-all-windows-visible-p split
))
382 ;; All the windows mentioned are already visible, so we just
383 ;; put point in the assigned buffer, and do not touch the
385 (select-window all-visible
)
387 ;; Make sure "the other" buffer, nntp-server-buffer, is live.
388 (unless (gnus-buffer-live-p nntp-server-buffer
)
389 (nnheader-init-server-buffer))
391 ;; Either remove all windows or just remove all Gnus windows.
392 (let ((frame (selected-frame)))
394 (if gnus-use-full-window
395 ;; We want to remove all other windows.
396 (if (not gnus-frame-split-p
)
397 ;; This is not a `frame' split, so we ignore the
399 (delete-other-windows)
400 ;; This is a `frame' split, so we delete all windows
402 (gnus-delete-windows-in-gnusey-frames))
403 ;; Just remove some windows.
404 (gnus-remove-some-windows)
405 (if (featurep 'xemacs
)
406 (switch-to-buffer nntp-server-buffer
)
407 (set-buffer nntp-server-buffer
)))
408 (select-frame frame
)))
410 (let (gnus-window-frame-focus)
411 (if (featurep 'xemacs
)
412 (switch-to-buffer nntp-server-buffer
)
413 (set-buffer nntp-server-buffer
))
414 (gnus-configure-frame split
)
415 (run-hooks 'gnus-configure-windows-hook
)
416 (when gnus-window-frame-focus
417 (gnus-select-frame-set-input-focus
418 (window-frame gnus-window-frame-focus
)))))))))
420 (defun gnus-delete-windows-in-gnusey-frames ()
421 "Do a `delete-other-windows' in all frames that have Gnus windows."
422 (let ((buffers (gnus-buffers)))
425 (unless (eq (cdr (assq 'minibuffer
426 (frame-parameters frame
)))
432 (when (memq (window-buffer window
) buffers
)
433 (setq do-delete t
))))
435 (delete-other-windows)))))
438 (defun gnus-all-windows-visible-p (split)
439 "Say whether all buffers in SPLIT are currently visible.
440 In particular, the value returned will be the window that
442 (let ((stack (list split
))
445 (while (and (setq split
(pop stack
))
447 (when (consp (car split
))
449 (push 'vertical split
))
450 ;; The SPLIT might be something that is to be evalled to
451 ;; return a new SPLIT.
452 (while (and (not (assq (car split
) gnus-window-to-buffer
))
453 (symbolp (car split
)) (fboundp (car split
)))
454 (setq split
(eval split
)))
456 (setq type
(elt split
0))
461 ((not (memq type
'(horizontal vertical frame
)))
462 (setq buffer
(cond ((stringp type
) type
)
463 (t (cdr (assq type gnus-window-to-buffer
)))))
465 (error "Invalid buffer type: %s" type
))
466 (if (and (setq buf
(get-buffer (gnus-window-to-buffer-helper buffer
)))
467 (setq win
(gnus-get-buffer-window buf t
)))
468 (if (memq 'point split
)
469 (setq all-visible win
))
470 (setq all-visible nil
)))
472 (when (eq type
'frame
)
473 (setq gnus-frame-split-p t
))
474 (setq stack
(append (cddr split
) stack
)))))
475 (unless (eq all-visible t
)
478 (defun gnus-window-top-edge (&optional window
)
479 "Return the top coordinate of WINDOW."
480 (nth 1 (window-edges window
)))
482 (defun gnus-remove-some-windows ()
483 (let ((buffers (gnus-buffers))
484 buf bufs lowest-buf lowest
)
486 ;; Remove windows on all known Gnus buffers.
487 (while (setq buf
(pop buffers
))
488 (when (get-buffer-window buf
)
491 (when (or (not lowest
)
492 (< (gnus-window-top-edge) lowest
))
493 (setq lowest
(gnus-window-top-edge)
496 (pop-to-buffer lowest-buf
)
497 (if (featurep 'xemacs
)
498 (switch-to-buffer nntp-server-buffer
)
499 (set-buffer nntp-server-buffer
)))
500 (mapcar (lambda (b) (delete-windows-on b t
))
501 (delq lowest-buf bufs
)))))
505 ((fboundp 'frames-on-display-list
)
506 (defalias 'gnus-frames-on-display-list
'frames-on-display-list
))
507 ((and (featurep 'xemacs
) (fboundp 'frame-device
))
508 (defun gnus-frames-on-display-list ()
509 (apply 'filtered-frame-list
'identity
(list (frame-device nil
)))))
511 (defalias 'gnus-frames-on-display-list
'frame-list
))))
513 (defun gnus-get-buffer-window (buffer &optional frame
)
514 (cond ((and (null gnus-use-frames-on-any-display
)
515 (memq frame
'(t 0 visible
)))
517 (let ((frames (gnus-frames-on-display-list)))
518 (gnus-remove-if (lambda (win) (not (memq (window-frame win
)
520 (get-buffer-window-list buffer nil frame
)))))
522 (get-buffer-window buffer frame
))))
526 ;;; gnus-win.el ends here