1 ;;; gnus-win.el --- window configuration functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl
))
35 (defgroup gnus-windows nil
36 "Window configuration."
39 (defcustom gnus-use-full-window t
40 "*If non-nil, use the entire Emacs screen."
44 (defvar gnus-window-configuration nil
45 "Obsolete variable. See `gnus-buffer-configuration'.")
47 (defcustom gnus-window-min-width
2
48 "*Minimum width of Gnus buffers."
52 (defcustom gnus-window-min-height
1
53 "*Minimum height of Gnus buffers."
57 (defcustom gnus-always-force-window-configuration nil
58 "*If non-nil, always force the Gnus window configurations."
62 (defcustom gnus-use-frames-on-any-display nil
63 "*If non-nil, frames on all displays will be considered useable by Gnus.
64 When nil, only frames on the same display as the selected frame will be
65 used to display Gnus windows."
70 (defvar gnus-buffer-configuration
74 (if gnus-carpal
'(group-carpal 4))))
78 (if gnus-carpal
'(summary-carpal 4))))
89 (if gnus-carpal
'(summary-carpal 4))
94 (if gnus-carpal
'(server-carpal 2))))
98 (if gnus-carpal
'(browse-carpal 2))))
101 (message 1.0 point
)))
104 (article 1.0 point
)))
114 (article 1.0 point
)))
118 (edit-form 1.0 point
)))
122 (edit-score 1.0 point
)))
126 (edit-form 1.0 point
)))
133 (message 1.0 point
)))
136 (message 1.0 point
)))
139 (message 1.0 point
)))
143 (message 1.0 point
)))
147 (if gnus-carpal
'(summary-carpal 4))
148 ("*Shell Command Output*" 1.0)))
151 (if gnus-bug-create-help-buffer
'("*Gnus Help Bug*" 0.5))
152 ("*Gnus Bug*" 1.0 point
)))
156 ("*Score Trace*" 1.0)))
160 ("*Score Words*" 1.0)))
164 ("*Split Trace*" 1.0)))
171 (message 1.0 point
)))
178 (mml-preview 1.0 point
))))
179 "Window configuration for all possible Gnus buffers.
180 See the Gnus manual for an explanation of the syntax used.")
182 (defvar gnus-window-to-buffer
183 '((group . gnus-group-buffer
)
184 (summary . gnus-summary-buffer
)
185 (article . gnus-article-buffer
)
186 (server . gnus-server-buffer
)
187 (browse .
"*Gnus Browse Server*")
188 (edit-group . gnus-group-edit-buffer
)
189 (edit-form . gnus-edit-form-buffer
)
190 (edit-server . gnus-server-edit-buffer
)
191 (group-carpal . gnus-carpal-group-buffer
)
192 (summary-carpal . gnus-carpal-summary-buffer
)
193 (server-carpal . gnus-carpal-server-buffer
)
194 (browse-carpal . gnus-carpal-browse-buffer
)
195 (edit-score . gnus-score-edit-buffer
)
196 (message . gnus-message-buffer
)
197 (mail . gnus-message-buffer
)
198 (post-news . gnus-message-buffer
)
199 (faq . gnus-faq-buffer
)
200 (tree . gnus-tree-buffer
)
201 (score-trace .
"*Score Trace*")
202 (split-trace .
"*Split Trace*")
203 (info . gnus-info-buffer
)
204 (category . gnus-category-buffer
)
205 (article-copy . gnus-article-copy
)
206 (draft . gnus-draft-buffer
)
207 (mml-preview . mml-preview-buffer
))
208 "Mapping from short symbols to buffer names or buffer variables.")
210 (defcustom gnus-configure-windows-hook nil
211 "*A hook called when configuring windows."
216 ;;; Internal variables.
218 (defvar gnus-current-window-configuration nil
219 "The most recently set window configuration.")
221 (defvar gnus-created-frames nil
)
222 (defvar gnus-window-frame-focus nil
)
224 (defun gnus-kill-gnus-frames ()
225 "Kill all frames Gnus has created."
226 (while gnus-created-frames
227 (when (frame-live-p (car gnus-created-frames
))
228 ;; We slap a condition-case around this `delete-frame' to ensure
229 ;; against errors if we try do delete the single frame that's left.
231 (delete-frame (car gnus-created-frames
))))
232 (pop gnus-created-frames
)))
234 (defun gnus-window-configuration-element (list)
236 (not (assq (car list
) gnus-window-configuration
)))
238 (cadr (assq (car list
) gnus-window-configuration
)))
240 (defun gnus-windows-old-to-new (setting)
241 ;; First we take care of the really, really old Gnus 3 actions.
242 (when (symbolp setting
)
244 ;; Take care of ooold GNUS 3.x values.
245 (cond ((eq setting
'SelectArticle
) 'article
)
246 ((memq setting
'(SelectNewsgroup SelectSubject ExpandSubject
))
248 ((memq setting
'(ExitNewsgroup)) 'group
)
250 (if (or (listp setting
)
251 (not (and gnus-window-configuration
252 (memq setting
'(group summary article
)))))
257 (gnus-window-configuration-element
258 '(group newsgroups ExitNewsgroup
)))
259 ((eq setting
'summary
)
260 (gnus-window-configuration-element
261 '(summary SelectNewsgroup SelectSubject ExpandSubject
)))
262 ((eq setting
'article
)
263 (gnus-window-configuration-element
264 '(article SelectArticle
)))))
265 (total (apply '+ elem
))
266 (types '(group summary article
))
267 (pbuf (if (eq setting
'newsgroups
) 'group
'summary
))
271 (or (not (numberp (nth i elem
)))
274 (setq perc
(if (= i
2)
276 (/ (float (nth i elem
)) total
)))
277 (push (if (eq pbuf
(nth i types
))
278 (list (nth i types
) perc
'point
)
279 (list (nth i types
) perc
))
282 `(vertical 1.0 ,@(nreverse out
)))))
285 (defun gnus-add-configuration (conf)
286 "Add the window configuration CONF to `gnus-buffer-configuration'."
287 (setq gnus-buffer-configuration
288 (cons conf
(delq (assq (car conf
) gnus-buffer-configuration
)
289 gnus-buffer-configuration
))))
291 (defvar gnus-frame-list nil
)
293 (defun gnus-window-to-buffer-helper (obj)
294 (cond ((not (symbolp obj
))
303 (defun gnus-configure-frame (split &optional window
)
304 "Split WINDOW according to SPLIT."
305 (let ((current-window
306 (or (get-buffer-window (current-buffer)) (selected-window))))
308 (setq window current-window
))
309 (select-window window
)
310 ;; This might be an old-style buffer config.
311 (when (vectorp split
)
312 (setq split
(append split nil
)))
313 (when (or (consp (car split
))
314 (vectorp (car split
)))
316 (push 'vertical split
))
317 ;; The SPLIT might be something that is to be evaled to
318 ;; return a new SPLIT.
319 (while (and (not (assq (car split
) gnus-window-to-buffer
))
320 (symbolp (car split
)) (fboundp (car split
)))
321 (setq split
(eval split
)))
322 (let* ((type (car split
))
324 (len (if (eq type
'horizontal
) (window-width) (window-height)))
326 (window-min-width (or gnus-window-min-width window-min-width
))
327 (window-min-height (or gnus-window-min-height window-min-height
))
328 s result new-win rest comp-subs size sub
)
330 ;; Nothing to do here.
332 ;; Don't switch buffers.
334 (and (memq 'point split
) window
))
335 ;; This is a buffer to be selected.
336 ((not (memq type
'(frame horizontal vertical
)))
337 (let ((buffer (cond ((stringp type
) type
)
338 (t (cdr (assq type gnus-window-to-buffer
))))))
340 (error "Invalid buffer type: %s" type
))
341 (let ((buf (gnus-get-buffer-create
342 (gnus-window-to-buffer-helper buffer
))))
343 (if (eq buf
(window-buffer (selected-window))) (set-buffer buf
)
344 (switch-to-buffer buf
)))
345 (when (memq 'frame-focus split
)
346 (setq gnus-window-frame-focus window
))
347 ;; We return the window if it has the `point' spec.
348 (and (memq 'point split
) window
)))
349 ;; This is a frame split.
351 (unless gnus-frame-list
352 (setq gnus-frame-list
(list (window-frame current-window
))))
354 params frame fresult
)
355 (while (< i
(length subs
))
356 ;; Frame parameter is gotten from the sub-split.
357 (setq params
(cadr (elt subs i
)))
358 ;; It should be a list.
359 (unless (listp params
)
361 ;; Create a new frame?
362 (unless (setq frame
(elt gnus-frame-list i
))
363 (nconc gnus-frame-list
(list (setq frame
(make-frame params
))))
364 (push frame gnus-created-frames
))
365 ;; Is the old frame still alive?
366 (unless (frame-live-p frame
)
367 (setcar (nthcdr i gnus-frame-list
)
368 (setq frame
(make-frame params
))))
369 ;; Select the frame in question and do more splits there.
371 (setq fresult
(or (gnus-configure-frame (elt subs i
)) fresult
))
373 ;; Select the frame that has the selected buffer.
375 (select-frame (window-frame fresult
)))))
376 ;; This is a normal split.
378 (when (> (length subs
) 0)
379 ;; First we have to compute the sizes of all new windows.
381 (setq sub
(append (pop subs
) nil
))
382 (while (and (not (assq (car sub
) gnus-window-to-buffer
))
383 (symbolp (car sub
)) (fboundp (car sub
)))
384 (setq sub
(eval sub
)))
387 (setq size
(cadar comp-subs
))
388 (cond ((equal size
1.0)
389 (setq rest
(car comp-subs
))
392 (setq s
(floor (* size len
))))
396 (error "Invalid size: %s" size
)))
397 ;; Try to make sure that we are inside the safe limits.
399 ((eq type
'horizontal
)
400 (setq s
(max s window-min-width
)))
402 (setq s
(max s window-min-height
))))
403 (setcar (cdar comp-subs
) s
)
405 ;; Take care of the "1.0" spec.
407 (setcar (cdr rest
) (- len total
))
408 (error "No 1.0 specs in %s" split
))
409 ;; The we do the actual splitting in a nice recursive
411 (setq comp-subs
(nreverse comp-subs
))
413 (if (null (cdr comp-subs
))
414 (setq new-win window
)
416 (split-window window
(cadar comp-subs
)
417 (eq type
'horizontal
))))
418 (setq result
(or (gnus-configure-frame
419 (car comp-subs
) window
)
421 (select-window new-win
)
422 (setq window new-win
)
423 (setq comp-subs
(cdr comp-subs
))))
424 ;; Return the proper window, if any.
426 (select-window result
)))))))
428 (defvar gnus-frame-split-p nil
)
430 (defun gnus-configure-windows (setting &optional force
)
431 (if (window-configuration-p setting
)
432 (set-window-configuration setting
)
433 (setq gnus-current-window-configuration setting
)
434 (setq force
(or force gnus-always-force-window-configuration
))
435 (setq setting
(gnus-windows-old-to-new setting
))
436 (let ((split (if (symbolp setting
)
437 (cadr (assq setting gnus-buffer-configuration
))
441 (setq gnus-frame-split-p nil
)
444 (error "No such setting in `gnus-buffer-configuration': %s" setting
))
446 (if (and (setq all-visible
(gnus-all-windows-visible-p split
))
448 ;; All the windows mentioned are already visible, so we just
449 ;; put point in the assigned buffer, and do not touch the
451 (select-window all-visible
)
453 ;; Make sure "the other" buffer, nntp-server-buffer, is live.
454 (unless (gnus-buffer-live-p nntp-server-buffer
)
455 (nnheader-init-server-buffer))
457 ;; Either remove all windows or just remove all Gnus windows.
458 (let ((frame (selected-frame)))
460 (if gnus-use-full-window
461 ;; We want to remove all other windows.
462 (if (not gnus-frame-split-p
)
463 ;; This is not a `frame' split, so we ignore the
465 (delete-other-windows)
466 ;; This is a `frame' split, so we delete all windows
468 (gnus-delete-windows-in-gnusey-frames))
469 ;; Just remove some windows.
470 (gnus-remove-some-windows)
471 (if (featurep 'xemacs
)
472 (switch-to-buffer nntp-server-buffer
)
473 (set-buffer nntp-server-buffer
)))
474 (select-frame frame
)))
476 (let (gnus-window-frame-focus)
477 (if (featurep 'xemacs
)
478 (switch-to-buffer nntp-server-buffer
)
479 (set-buffer nntp-server-buffer
))
480 (gnus-configure-frame split
)
481 (run-hooks 'gnus-configure-windows-hook
)
482 (when gnus-window-frame-focus
483 (gnus-select-frame-set-input-focus
484 (window-frame gnus-window-frame-focus
))))))))
486 (defun gnus-delete-windows-in-gnusey-frames ()
487 "Do a `delete-other-windows' in all frames that have Gnus windows."
488 (let ((buffers (gnus-buffers)))
491 (unless (eq (cdr (assq 'minibuffer
492 (frame-parameters frame
)))
498 (when (memq (window-buffer window
) buffers
)
499 (setq do-delete t
))))
501 (delete-other-windows)))))
504 (defun gnus-all-windows-visible-p (split)
505 "Say whether all buffers in SPLIT are currently visible.
506 In particular, the value returned will be the window that
508 (let ((stack (list split
))
511 (while (and (setq split
(pop stack
))
513 ;; Be backwards compatible.
514 (when (vectorp split
)
515 (setq split
(append split nil
)))
516 (when (or (consp (car split
))
517 (vectorp (car split
)))
519 (push 'vertical split
))
520 ;; The SPLIT might be something that is to be evaled to
521 ;; return a new SPLIT.
522 (while (and (not (assq (car split
) gnus-window-to-buffer
))
523 (symbolp (car split
)) (fboundp (car split
)))
524 (setq split
(eval split
)))
526 (setq type
(elt split
0))
531 ((not (memq type
'(horizontal vertical frame
)))
532 (setq buffer
(cond ((stringp type
) type
)
533 (t (cdr (assq type gnus-window-to-buffer
)))))
535 (error "Invalid buffer type: %s" type
))
536 (if (and (setq buf
(get-buffer (gnus-window-to-buffer-helper buffer
)))
537 (setq win
(gnus-get-buffer-window buf t
)))
538 (if (memq 'point split
)
539 (setq all-visible win
))
540 (setq all-visible nil
)))
542 (when (eq type
'frame
)
543 (setq gnus-frame-split-p t
))
544 (setq stack
(append (cddr split
) stack
)))))
545 (unless (eq all-visible t
)
548 (defun gnus-window-top-edge (&optional window
)
549 (nth 1 (window-edges window
)))
551 (defun gnus-remove-some-windows ()
552 (let ((buffers (gnus-buffers))
553 buf bufs lowest-buf lowest
)
555 ;; Remove windows on all known Gnus buffers.
556 (while (setq buf
(pop buffers
))
557 (when (get-buffer-window buf
)
560 (when (or (not lowest
)
561 (< (gnus-window-top-edge) lowest
))
562 (setq lowest
(gnus-window-top-edge)
565 (pop-to-buffer lowest-buf
)
566 (if (featurep 'xemacs
)
567 (switch-to-buffer nntp-server-buffer
)
568 (set-buffer nntp-server-buffer
)))
569 (mapcar (lambda (b) (delete-windows-on b t
))
570 (delq lowest-buf bufs
)))))
574 ((fboundp 'frames-on-display-list
)
575 (defalias 'gnus-frames-on-display-list
'frames-on-display-list
))
576 ((and (featurep 'xemacs
) (fboundp 'frame-device
))
577 (defun gnus-frames-on-display-list ()
578 (apply 'filtered-frame-list
'identity
(list (frame-device nil
)))))
580 (defalias 'gnus-frames-on-display-list
'frame-list
))))
582 (defun gnus-get-buffer-window (buffer &optional frame
)
583 (cond ((and (null gnus-use-frames-on-any-display
)
584 (memq frame
'(t 0 visible
)))
586 (let ((frames (gnus-frames-on-display-list)))
587 (gnus-remove-if (lambda (win) (not (memq (window-frame win
)
589 (get-buffer-window-list buffer nil frame
)))))
591 (get-buffer-window buffer frame
))))
595 ;; arch-tag: ccd5a394-2ddf-4397-b8f8-6d80d3e46e2b
596 ;;; gnus-win.el ends here