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, 2009, 2010 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 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl
))
33 (defgroup gnus-windows nil
34 "Window configuration."
37 (defcustom gnus-use-full-window t
38 "*If non-nil, use the entire Emacs screen."
42 (defvar gnus-window-configuration nil
43 "Obsolete variable. See `gnus-buffer-configuration'.")
45 (defcustom gnus-window-min-width
2
46 "*Minimum width of Gnus buffers."
50 (defcustom gnus-window-min-height
1
51 "*Minimum height of Gnus buffers."
55 (defcustom gnus-always-force-window-configuration nil
56 "*If non-nil, always force the Gnus window configurations."
60 (defcustom gnus-use-frames-on-any-display nil
61 "*If non-nil, frames on all displays will be considered useable by Gnus.
62 When nil, only frames on the same display as the selected frame will be
63 used to display Gnus windows."
68 (defvar gnus-buffer-configuration
72 (if gnus-carpal
'(group-carpal 4))))
76 (if gnus-carpal
'(summary-carpal 4))))
87 (if gnus-carpal
'(summary-carpal 4))
92 (if gnus-carpal
'(server-carpal 2))))
96 (if gnus-carpal
'(browse-carpal 2))))
102 (article 1.0 point
)))
112 (article 1.0 point
)))
116 (edit-form 1.0 point
)))
120 (edit-score 1.0 point
)))
124 (edit-form 1.0 point
)))
131 (message 1.0 point
)))
134 (message 1.0 point
)))
137 (message 1.0 point
)))
141 (message 1.0 point
)))
145 (if gnus-carpal
'(summary-carpal 4))
146 ("*Shell Command Output*" 1.0)))
149 (if gnus-bug-create-help-buffer
'("*Gnus Help Bug*" 0.5))
150 ("*Gnus Bug*" 1.0 point
)))
154 ("*Score Trace*" 1.0)))
158 ("*Score Words*" 1.0)))
162 ("*Split Trace*" 1.0)))
169 (message 1.0 point
)))
176 (mml-preview 1.0 point
))))
177 "Window configuration for all possible Gnus buffers.
178 See the Gnus manual for an explanation of the syntax used.")
180 (defvar gnus-window-to-buffer
181 '((group . gnus-group-buffer
)
182 (summary . gnus-summary-buffer
)
183 (article . gnus-article-buffer
)
184 (server . gnus-server-buffer
)
185 (browse .
"*Gnus Browse Server*")
186 (edit-group . gnus-group-edit-buffer
)
187 (edit-form . gnus-edit-form-buffer
)
188 (edit-server . gnus-server-edit-buffer
)
189 (group-carpal . gnus-carpal-group-buffer
)
190 (summary-carpal . gnus-carpal-summary-buffer
)
191 (server-carpal . gnus-carpal-server-buffer
)
192 (browse-carpal . gnus-carpal-browse-buffer
)
193 (edit-score . gnus-score-edit-buffer
)
194 (message . gnus-message-buffer
)
195 (mail . gnus-message-buffer
)
196 (post-news . gnus-message-buffer
)
197 (faq . gnus-faq-buffer
)
198 (tree . gnus-tree-buffer
)
199 (score-trace .
"*Score Trace*")
200 (split-trace .
"*Split Trace*")
201 (info . gnus-info-buffer
)
202 (category . gnus-category-buffer
)
203 (article-copy . gnus-article-copy
)
204 (draft . gnus-draft-buffer
)
205 (mml-preview . mml-preview-buffer
))
206 "Mapping from short symbols to buffer names or buffer variables.")
208 (defcustom gnus-configure-windows-hook nil
209 "*A hook called when configuring windows."
214 ;;; Internal variables.
216 (defvar gnus-current-window-configuration nil
217 "The most recently set window configuration.")
219 (defvar gnus-created-frames nil
)
220 (defvar gnus-window-frame-focus nil
)
222 (defun gnus-kill-gnus-frames ()
223 "Kill all frames Gnus has created."
224 (while gnus-created-frames
225 (when (frame-live-p (car gnus-created-frames
))
226 ;; We slap a condition-case around this `delete-frame' to ensure
227 ;; against errors if we try do delete the single frame that's left.
229 (delete-frame (car gnus-created-frames
))))
230 (pop gnus-created-frames
)))
232 (defun gnus-window-configuration-element (list)
234 (not (assq (car list
) gnus-window-configuration
)))
236 (cadr (assq (car list
) gnus-window-configuration
)))
238 (defun gnus-windows-old-to-new (setting)
239 ;; First we take care of the really, really old Gnus 3 actions.
240 (when (symbolp setting
)
242 ;; Take care of ooold GNUS 3.x values.
243 (cond ((eq setting
'SelectArticle
) 'article
)
244 ((memq setting
'(SelectNewsgroup SelectSubject ExpandSubject
))
246 ((memq setting
'(ExitNewsgroup)) 'group
)
248 (if (or (listp setting
)
249 (not (and gnus-window-configuration
250 (memq setting
'(group summary article
)))))
255 (gnus-window-configuration-element
256 '(group newsgroups ExitNewsgroup
)))
257 ((eq setting
'summary
)
258 (gnus-window-configuration-element
259 '(summary SelectNewsgroup SelectSubject ExpandSubject
)))
260 ((eq setting
'article
)
261 (gnus-window-configuration-element
262 '(article SelectArticle
)))))
263 (total (apply '+ elem
))
264 (types '(group summary article
))
265 (pbuf (if (eq setting
'newsgroups
) 'group
'summary
))
269 (or (not (numberp (nth i elem
)))
272 (setq perc
(if (= i
2)
274 (/ (float (nth i elem
)) total
)))
275 (push (if (eq pbuf
(nth i types
))
276 (list (nth i types
) perc
'point
)
277 (list (nth i types
) perc
))
280 `(vertical 1.0 ,@(nreverse out
)))))
283 (defun gnus-add-configuration (conf)
284 "Add the window configuration CONF to `gnus-buffer-configuration'."
285 (setq gnus-buffer-configuration
286 (cons conf
(delq (assq (car conf
) gnus-buffer-configuration
)
287 gnus-buffer-configuration
))))
289 (defvar gnus-frame-list nil
)
291 (defun gnus-window-to-buffer-helper (obj)
292 (cond ((not (symbolp obj
))
301 (defun gnus-configure-frame (split &optional window
)
302 "Split WINDOW according to SPLIT."
303 (let ((current-window
304 (or (get-buffer-window (current-buffer)) (selected-window))))
306 (setq window current-window
))
307 (select-window window
)
308 ;; This might be an old-style buffer config.
309 (when (vectorp split
)
310 (setq split
(append split nil
)))
311 (when (or (consp (car split
))
312 (vectorp (car split
)))
314 (push 'vertical split
))
315 ;; The SPLIT might be something that is to be evaled to
316 ;; return a new SPLIT.
317 (while (and (not (assq (car split
) gnus-window-to-buffer
))
318 (symbolp (car split
)) (fboundp (car split
)))
319 (setq split
(eval split
)))
320 (let* ((type (car split
))
322 (len (if (eq type
'horizontal
) (window-width) (window-height)))
324 (window-min-width (or gnus-window-min-width window-min-width
))
325 (window-min-height (or gnus-window-min-height window-min-height
))
326 s result new-win rest comp-subs size sub
)
328 ;; Nothing to do here.
330 ;; Don't switch buffers.
332 (and (memq 'point split
) window
))
333 ;; This is a buffer to be selected.
334 ((not (memq type
'(frame horizontal vertical
)))
335 (let ((buffer (cond ((stringp type
) type
)
336 (t (cdr (assq type gnus-window-to-buffer
))))))
338 (error "Invalid buffer type: %s" type
))
339 (let ((buf (gnus-get-buffer-create
340 (gnus-window-to-buffer-helper buffer
))))
341 (if (eq buf
(window-buffer (selected-window))) (set-buffer buf
)
342 (switch-to-buffer buf
)))
343 (when (memq 'frame-focus split
)
344 (setq gnus-window-frame-focus window
))
345 ;; We return the window if it has the `point' spec.
346 (and (memq 'point split
) window
)))
347 ;; This is a frame split.
349 (unless gnus-frame-list
350 (setq gnus-frame-list
(list (window-frame current-window
))))
352 params frame fresult
)
353 (while (< i
(length subs
))
354 ;; Frame parameter is gotten from the sub-split.
355 (setq params
(cadr (elt subs i
)))
356 ;; It should be a list.
357 (unless (listp params
)
359 ;; Create a new frame?
360 (unless (setq frame
(elt gnus-frame-list i
))
361 (nconc gnus-frame-list
(list (setq frame
(make-frame params
))))
362 (push frame gnus-created-frames
))
363 ;; Is the old frame still alive?
364 (unless (frame-live-p frame
)
365 (setcar (nthcdr i gnus-frame-list
)
366 (setq frame
(make-frame params
))))
367 ;; Select the frame in question and do more splits there.
369 (setq fresult
(or (gnus-configure-frame (elt subs i
)) fresult
))
371 ;; Select the frame that has the selected buffer.
373 (select-frame (window-frame fresult
)))))
374 ;; This is a normal split.
376 (when (> (length subs
) 0)
377 ;; First we have to compute the sizes of all new windows.
379 (setq sub
(append (pop subs
) nil
))
380 (while (and (not (assq (car sub
) gnus-window-to-buffer
))
381 (symbolp (car sub
)) (fboundp (car sub
)))
382 (setq sub
(eval sub
)))
385 (setq size
(cadar comp-subs
))
386 (cond ((equal size
1.0)
387 (setq rest
(car comp-subs
))
390 (setq s
(floor (* size len
))))
394 (error "Invalid size: %s" size
)))
395 ;; Try to make sure that we are inside the safe limits.
397 ((eq type
'horizontal
)
398 (setq s
(max s window-min-width
)))
400 (setq s
(max s window-min-height
))))
401 (setcar (cdar comp-subs
) s
)
403 ;; Take care of the "1.0" spec.
405 (setcar (cdr rest
) (- len total
))
406 (error "No 1.0 specs in %s" split
))
407 ;; The we do the actual splitting in a nice recursive
409 (setq comp-subs
(nreverse comp-subs
))
411 (if (null (cdr comp-subs
))
412 (setq new-win window
)
414 (split-window window
(cadar comp-subs
)
415 (eq type
'horizontal
))))
416 (setq result
(or (gnus-configure-frame
417 (car comp-subs
) window
)
419 (select-window new-win
)
420 (setq window new-win
)
421 (setq comp-subs
(cdr comp-subs
))))
422 ;; Return the proper window, if any.
424 (select-window result
)))))))
426 (defvar gnus-frame-split-p nil
)
428 (defun gnus-configure-windows (setting &optional force
)
429 (if (window-configuration-p setting
)
430 (set-window-configuration setting
)
431 (setq gnus-current-window-configuration setting
)
432 (setq force
(or force gnus-always-force-window-configuration
))
433 (setq setting
(gnus-windows-old-to-new setting
))
434 (let ((split (if (symbolp setting
)
435 (cadr (assq setting gnus-buffer-configuration
))
439 (setq gnus-frame-split-p nil
)
442 (error "No such setting in `gnus-buffer-configuration': %s" setting
))
444 (if (and (setq all-visible
(gnus-all-windows-visible-p split
))
446 ;; All the windows mentioned are already visible, so we just
447 ;; put point in the assigned buffer, and do not touch the
449 (select-window all-visible
)
451 ;; Make sure "the other" buffer, nntp-server-buffer, is live.
452 (unless (gnus-buffer-live-p nntp-server-buffer
)
453 (nnheader-init-server-buffer))
455 ;; Either remove all windows or just remove all Gnus windows.
456 (let ((frame (selected-frame)))
458 (if gnus-use-full-window
459 ;; We want to remove all other windows.
460 (if (not gnus-frame-split-p
)
461 ;; This is not a `frame' split, so we ignore the
463 (delete-other-windows)
464 ;; This is a `frame' split, so we delete all windows
466 (gnus-delete-windows-in-gnusey-frames))
467 ;; Just remove some windows.
468 (gnus-remove-some-windows)
469 (if (featurep 'xemacs
)
470 (switch-to-buffer nntp-server-buffer
)
471 (set-buffer nntp-server-buffer
)))
472 (select-frame frame
)))
474 (let (gnus-window-frame-focus)
475 (if (featurep 'xemacs
)
476 (switch-to-buffer nntp-server-buffer
)
477 (set-buffer nntp-server-buffer
))
478 (gnus-configure-frame split
)
479 (run-hooks 'gnus-configure-windows-hook
)
480 (when gnus-window-frame-focus
481 (gnus-select-frame-set-input-focus
482 (window-frame gnus-window-frame-focus
))))))))
484 (defun gnus-delete-windows-in-gnusey-frames ()
485 "Do a `delete-other-windows' in all frames that have Gnus windows."
486 (let ((buffers (gnus-buffers)))
489 (unless (eq (cdr (assq 'minibuffer
490 (frame-parameters frame
)))
496 (when (memq (window-buffer window
) buffers
)
497 (setq do-delete t
))))
499 (delete-other-windows)))))
502 (defun gnus-all-windows-visible-p (split)
503 "Say whether all buffers in SPLIT are currently visible.
504 In particular, the value returned will be the window that
506 (let ((stack (list split
))
509 (while (and (setq split
(pop stack
))
511 ;; Be backwards compatible.
512 (when (vectorp split
)
513 (setq split
(append split nil
)))
514 (when (or (consp (car split
))
515 (vectorp (car split
)))
517 (push 'vertical split
))
518 ;; The SPLIT might be something that is to be evaled to
519 ;; return a new SPLIT.
520 (while (and (not (assq (car split
) gnus-window-to-buffer
))
521 (symbolp (car split
)) (fboundp (car split
)))
522 (setq split
(eval split
)))
524 (setq type
(elt split
0))
529 ((not (memq type
'(horizontal vertical frame
)))
530 (setq buffer
(cond ((stringp type
) type
)
531 (t (cdr (assq type gnus-window-to-buffer
)))))
533 (error "Invalid buffer type: %s" type
))
534 (if (and (setq buf
(get-buffer (gnus-window-to-buffer-helper buffer
)))
535 (setq win
(gnus-get-buffer-window buf t
)))
536 (if (memq 'point split
)
537 (setq all-visible win
))
538 (setq all-visible nil
)))
540 (when (eq type
'frame
)
541 (setq gnus-frame-split-p t
))
542 (setq stack
(append (cddr split
) stack
)))))
543 (unless (eq all-visible t
)
546 (defun gnus-window-top-edge (&optional window
)
547 (nth 1 (window-edges window
)))
549 (defun gnus-remove-some-windows ()
550 (let ((buffers (gnus-buffers))
551 buf bufs lowest-buf lowest
)
553 ;; Remove windows on all known Gnus buffers.
554 (while (setq buf
(pop buffers
))
555 (when (get-buffer-window buf
)
558 (when (or (not lowest
)
559 (< (gnus-window-top-edge) lowest
))
560 (setq lowest
(gnus-window-top-edge)
563 (pop-to-buffer lowest-buf
)
564 (if (featurep 'xemacs
)
565 (switch-to-buffer nntp-server-buffer
)
566 (set-buffer nntp-server-buffer
)))
567 (mapcar (lambda (b) (delete-windows-on b t
))
568 (delq lowest-buf bufs
)))))
572 ((fboundp 'frames-on-display-list
)
573 (defalias 'gnus-frames-on-display-list
'frames-on-display-list
))
574 ((and (featurep 'xemacs
) (fboundp 'frame-device
))
575 (defun gnus-frames-on-display-list ()
576 (apply 'filtered-frame-list
'identity
(list (frame-device nil
)))))
578 (defalias 'gnus-frames-on-display-list
'frame-list
))))
580 (defun gnus-get-buffer-window (buffer &optional frame
)
581 (cond ((and (null gnus-use-frames-on-any-display
)
582 (memq frame
'(t 0 visible
)))
584 (let ((frames (gnus-frames-on-display-list)))
585 (gnus-remove-if (lambda (win) (not (memq (window-frame win
)
587 (get-buffer-window-list buffer nil frame
)))))
589 (get-buffer-window buffer frame
))))
593 ;; arch-tag: ccd5a394-2ddf-4397-b8f8-6d80d3e46e2b
594 ;;; gnus-win.el ends here