(nnml-save-marks): Add missing format field in call to `error'.
[bpt/emacs.git] / lisp / gnus / gnus-win.el
CommitLineData
eec82323 1;;; gnus-win.el --- window configuration functions for Gnus
23f87bed 2;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
16409b0b 3;; Free Software Foundation, Inc.
eec82323 4
6748645f 5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
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 2, or (at your option)
13;; any later version.
14
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.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;;; Code:
28
5ab7173c
RS
29(eval-when-compile (require 'cl))
30
eec82323 31(require 'gnus)
23f87bed 32(require 'gnus-util)
eec82323
LMI
33
34(defgroup gnus-windows nil
35 "Window configuration."
36 :group 'gnus)
37
38(defcustom gnus-use-full-window t
39 "*If non-nil, use the entire Emacs screen."
40 :group 'gnus-windows
41 :type 'boolean)
42
43(defvar gnus-window-configuration nil
44 "Obsolete variable. See `gnus-buffer-configuration'.")
45
46(defcustom gnus-window-min-width 2
47 "*Minimum width of Gnus buffers."
48 :group 'gnus-windows
49 :type 'integer)
50
51(defcustom gnus-window-min-height 1
52 "*Minimum height of Gnus buffers."
53 :group 'gnus-windows
54 :type 'integer)
55
56(defcustom gnus-always-force-window-configuration nil
57 "*If non-nil, always force the Gnus window configurations."
58 :group 'gnus-windows
59 :type 'boolean)
60
23f87bed
MB
61(defcustom gnus-use-frames-on-any-display nil
62 "*If non-nil, frames on all displays will be considered useable by Gnus.
63When nil, only frames on the same display as the selected frame will be
64used to display Gnus windows."
e2642250 65 :version "21.4"
23f87bed
MB
66 :group 'gnus-windows
67 :type 'boolean)
68
eec82323
LMI
69(defvar gnus-buffer-configuration
70 '((group
71 (vertical 1.0
72 (group 1.0 point)
73 (if gnus-carpal '(group-carpal 4))))
74 (summary
75 (vertical 1.0
76 (summary 1.0 point)
77 (if gnus-carpal '(summary-carpal 4))))
78 (article
79 (cond
eec82323
LMI
80 (gnus-use-trees
81 '(vertical 1.0
82 (summary 0.25 point)
83 (tree 0.25)
84 (article 1.0)))
85 (t
86 '(vertical 1.0
16409b0b
GM
87 (summary 0.25 point)
88 (if gnus-carpal '(summary-carpal 4))
89 (article 1.0)))))
eec82323
LMI
90 (server
91 (vertical 1.0
92 (server 1.0 point)
93 (if gnus-carpal '(server-carpal 2))))
94 (browse
95 (vertical 1.0
96 (browse 1.0 point)
97 (if gnus-carpal '(browse-carpal 2))))
98 (message
99 (vertical 1.0
100 (message 1.0 point)))
101 (pick
102 (vertical 1.0
103 (article 1.0 point)))
104 (info
105 (vertical 1.0
106 (info 1.0 point)))
107 (summary-faq
108 (vertical 1.0
109 (summary 0.25)
110 (faq 1.0 point)))
111 (edit-article
112 (vertical 1.0
113 (article 1.0 point)))
114 (edit-form
115 (vertical 1.0
116 (group 0.5)
117 (edit-form 1.0 point)))
118 (edit-score
119 (vertical 1.0
120 (summary 0.25)
121 (edit-score 1.0 point)))
122 (post
123 (vertical 1.0
124 (post 1.0 point)))
125 (reply
126 (vertical 1.0
23f87bed 127 (article 0.5)
eec82323
LMI
128 (message 1.0 point)))
129 (forward
130 (vertical 1.0
131 (message 1.0 point)))
132 (reply-yank
133 (vertical 1.0
134 (message 1.0 point)))
135 (mail-bounce
136 (vertical 1.0
137 (article 0.5)
138 (message 1.0 point)))
eec82323
LMI
139 (pipe
140 (vertical 1.0
141 (summary 0.25 point)
142 (if gnus-carpal '(summary-carpal 4))
143 ("*Shell Command Output*" 1.0)))
144 (bug
145 (vertical 1.0
55388e55 146 (if gnus-bug-create-help-buffer '("*Gnus Help Bug*" 0.5))
eec82323
LMI
147 ("*Gnus Bug*" 1.0 point)))
148 (score-trace
149 (vertical 1.0
150 (summary 0.5 point)
151 ("*Score Trace*" 1.0)))
152 (score-words
153 (vertical 1.0
154 (summary 0.5 point)
155 ("*Score Words*" 1.0)))
6748645f
LMI
156 (split-trace
157 (vertical 1.0
158 (summary 0.5 point)
159 ("*Split Trace*" 1.0)))
160 (category
161 (vertical 1.0
162 (category 1.0)))
eec82323
LMI
163 (compose-bounce
164 (vertical 1.0
165 (article 0.5)
23f87bed
MB
166 (message 1.0 point)))
167 (display-term
168 (vertical 1.0
169 ("*display*" 1.0))))
eec82323
LMI
170 "Window configuration for all possible Gnus buffers.
171See the Gnus manual for an explanation of the syntax used.")
172
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 (group-carpal . gnus-carpal-group-buffer)
183 (summary-carpal . gnus-carpal-summary-buffer)
184 (server-carpal . gnus-carpal-server-buffer)
185 (browse-carpal . gnus-carpal-browse-buffer)
186 (edit-score . gnus-score-edit-buffer)
187 (message . gnus-message-buffer)
188 (mail . gnus-message-buffer)
189 (post-news . gnus-message-buffer)
190 (faq . gnus-faq-buffer)
eec82323 191 (tree . gnus-tree-buffer)
a8151ef7 192 (score-trace . "*Score Trace*")
6748645f 193 (split-trace . "*Split Trace*")
eec82323 194 (info . gnus-info-buffer)
6748645f 195 (category . gnus-category-buffer)
eec82323
LMI
196 (article-copy . gnus-article-copy)
197 (draft . gnus-draft-buffer))
198 "Mapping from short symbols to buffer names or buffer variables.")
199
23f87bed
MB
200(defcustom gnus-configure-windows-hook nil
201 "*A hook called when configuring windows."
e2642250 202 :version "21.4"
23f87bed
MB
203 :group 'gnus-windows
204 :type 'hook)
205
eec82323
LMI
206;;; Internal variables.
207
208(defvar gnus-current-window-configuration nil
209 "The most recently set window configuration.")
210
211(defvar gnus-created-frames nil)
6748645f 212(defvar gnus-window-frame-focus nil)
eec82323
LMI
213
214(defun gnus-kill-gnus-frames ()
215 "Kill all frames Gnus has created."
216 (while gnus-created-frames
217 (when (frame-live-p (car gnus-created-frames))
218 ;; We slap a condition-case around this `delete-frame' to ensure
219 ;; against errors if we try do delete the single frame that's left.
220 (ignore-errors
221 (delete-frame (car gnus-created-frames))))
222 (pop gnus-created-frames)))
223
224(defun gnus-window-configuration-element (list)
225 (while (and list
226 (not (assq (car list) gnus-window-configuration)))
227 (pop list))
228 (cadr (assq (car list) gnus-window-configuration)))
229
230(defun gnus-windows-old-to-new (setting)
231 ;; First we take care of the really, really old Gnus 3 actions.
232 (when (symbolp setting)
233 (setq setting
234 ;; Take care of ooold GNUS 3.x values.
235 (cond ((eq setting 'SelectArticle) 'article)
236 ((memq setting '(SelectNewsgroup SelectSubject ExpandSubject))
237 'summary)
238 ((memq setting '(ExitNewsgroup)) 'group)
239 (t setting))))
240 (if (or (listp setting)
241 (not (and gnus-window-configuration
242 (memq setting '(group summary article)))))
243 setting
244 (let* ((elem
245 (cond
246 ((eq setting 'group)
247 (gnus-window-configuration-element
248 '(group newsgroups ExitNewsgroup)))
249 ((eq setting 'summary)
250 (gnus-window-configuration-element
251 '(summary SelectNewsgroup SelectSubject ExpandSubject)))
252 ((eq setting 'article)
253 (gnus-window-configuration-element
254 '(article SelectArticle)))))
255 (total (apply '+ elem))
256 (types '(group summary article))
257 (pbuf (if (eq setting 'newsgroups) 'group 'summary))
258 (i 0)
259 perc out)
260 (while (< i 3)
261 (or (not (numberp (nth i elem)))
262 (zerop (nth i elem))
263 (progn
264 (setq perc (if (= i 2)
265 1.0
266 (/ (float (nth i elem)) total)))
267 (push (if (eq pbuf (nth i types))
268 (list (nth i types) perc 'point)
269 (list (nth i types) perc))
270 out)))
271 (incf i))
272 `(vertical 1.0 ,@(nreverse out)))))
273
274;;;###autoload
275(defun gnus-add-configuration (conf)
276 "Add the window configuration CONF to `gnus-buffer-configuration'."
277 (setq gnus-buffer-configuration
278 (cons conf (delq (assq (car conf) gnus-buffer-configuration)
279 gnus-buffer-configuration))))
280
281(defvar gnus-frame-list nil)
282
6748645f
LMI
283(defun gnus-window-to-buffer-helper (obj)
284 (cond ((not (symbolp obj))
285 obj)
286 ((boundp obj)
287 (symbol-value obj))
288 ((fboundp obj)
289 (funcall obj))
290 (t
291 nil)))
292
eec82323
LMI
293(defun gnus-configure-frame (split &optional window)
294 "Split WINDOW according to SPLIT."
ca3ab2d8
SZ
295 (let ((current-window
296 (or (get-buffer-window (current-buffer)) (selected-window))))
297 (unless window
298 (setq window current-window))
299 (select-window window)
8f688cb0 300 ;; This might be an old-style buffer config.
ca3ab2d8
SZ
301 (when (vectorp split)
302 (setq split (append split nil)))
303 (when (or (consp (car split))
304 (vectorp (car split)))
305 (push 1.0 split)
306 (push 'vertical split))
307 ;; The SPLIT might be something that is to be evaled to
308 ;; return a new SPLIT.
309 (while (and (not (assq (car split) gnus-window-to-buffer))
23f87bed 310 (functionp (car split)))
ca3ab2d8
SZ
311 (setq split (eval split)))
312 (let* ((type (car split))
313 (subs (cddr split))
314 (len (if (eq type 'horizontal) (window-width) (window-height)))
315 (total 0)
316 (window-min-width (or gnus-window-min-width window-min-width))
317 (window-min-height (or gnus-window-min-height window-min-height))
318 s result new-win rest comp-subs size sub)
319 (cond
320 ;; Nothing to do here.
321 ((null split))
322 ;; Don't switch buffers.
323 ((null type)
324 (and (memq 'point split) window))
325 ;; This is a buffer to be selected.
326 ((not (memq type '(frame horizontal vertical)))
327 (let ((buffer (cond ((stringp type) type)
328 (t (cdr (assq type gnus-window-to-buffer))))))
329 (unless buffer
330 (error "Invalid buffer type: %s" type))
331 (let ((buf (gnus-get-buffer-create
332 (gnus-window-to-buffer-helper buffer))))
333 (if (eq buf (window-buffer (selected-window))) (set-buffer buf)
334 (switch-to-buffer buf)))
335 (when (memq 'frame-focus split)
336 (setq gnus-window-frame-focus window))
337 ;; We return the window if it has the `point' spec.
338 (and (memq 'point split) window)))
339 ;; This is a frame split.
340 ((eq type 'frame)
341 (unless gnus-frame-list
342 (setq gnus-frame-list (list (window-frame current-window))))
343 (let ((i 0)
344 params frame fresult)
345 (while (< i (length subs))
346 ;; Frame parameter is gotten from the sub-split.
347 (setq params (cadr (elt subs i)))
348 ;; It should be a list.
349 (unless (listp params)
350 (setq params nil))
351 ;; Create a new frame?
352 (unless (setq frame (elt gnus-frame-list i))
353 (nconc gnus-frame-list (list (setq frame (make-frame params))))
354 (push frame gnus-created-frames))
355 ;; Is the old frame still alive?
356 (unless (frame-live-p frame)
357 (setcar (nthcdr i gnus-frame-list)
358 (setq frame (make-frame params))))
359 ;; Select the frame in question and do more splits there.
360 (select-frame frame)
361 (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
362 (incf i))
363 ;; Select the frame that has the selected buffer.
364 (when fresult
365 (select-frame (window-frame fresult)))))
366 ;; This is a normal split.
367 (t
368 (when (> (length subs) 0)
369 ;; First we have to compute the sizes of all new windows.
370 (while subs
371 (setq sub (append (pop subs) nil))
372 (while (and (not (assq (car sub) gnus-window-to-buffer))
23f87bed 373 (functionp (car sub)))
ca3ab2d8
SZ
374 (setq sub (eval sub)))
375 (when sub
376 (push sub comp-subs)
377 (setq size (cadar comp-subs))
378 (cond ((equal size 1.0)
379 (setq rest (car comp-subs))
380 (setq s 0))
381 ((floatp size)
382 (setq s (floor (* size len))))
383 ((integerp size)
384 (setq s size))
385 (t
386 (error "Invalid size: %s" size)))
387 ;; Try to make sure that we are inside the safe limits.
388 (cond ((zerop s))
389 ((eq type 'horizontal)
390 (setq s (max s window-min-width)))
391 ((eq type 'vertical)
392 (setq s (max s window-min-height))))
393 (setcar (cdar comp-subs) s)
394 (incf total s)))
395 ;; Take care of the "1.0" spec.
396 (if rest
397 (setcar (cdr rest) (- len total))
398 (error "No 1.0 specs in %s" split))
399 ;; The we do the actual splitting in a nice recursive
400 ;; fashion.
401 (setq comp-subs (nreverse comp-subs))
402 (while comp-subs
403 (if (null (cdr comp-subs))
404 (setq new-win window)
405 (setq new-win
406 (split-window window (cadar comp-subs)
407 (eq type 'horizontal))))
408 (setq result (or (gnus-configure-frame
409 (car comp-subs) window)
410 result))
411 (select-window new-win)
412 (setq window new-win)
413 (setq comp-subs (cdr comp-subs))))
414 ;; Return the proper window, if any.
415 (when result
416 (select-window result)))))))
eec82323
LMI
417
418(defvar gnus-frame-split-p nil)
419
420(defun gnus-configure-windows (setting &optional force)
16409b0b
GM
421 (if (window-configuration-p setting)
422 (set-window-configuration setting)
423 (setq gnus-current-window-configuration setting)
424 (setq force (or force gnus-always-force-window-configuration))
425 (setq setting (gnus-windows-old-to-new setting))
426 (let ((split (if (symbolp setting)
427 (cadr (assq setting gnus-buffer-configuration))
428 setting))
429 all-visible)
430
431 (setq gnus-frame-split-p nil)
432
433 (unless split
434 (error "No such setting in `gnus-buffer-configuration': %s" setting))
435
436 (if (and (setq all-visible (gnus-all-windows-visible-p split))
437 (not force))
438 ;; All the windows mentioned are already visible, so we just
439 ;; put point in the assigned buffer, and do not touch the
440 ;; winconf.
441 (select-window all-visible)
55388e55 442
25fc4fd5
SZ
443 ;; Make sure "the other" buffer, nntp-server-buffer, is live.
444 (unless (gnus-buffer-live-p nntp-server-buffer)
445 (nnheader-init-server-buffer))
16409b0b
GM
446
447 ;; Either remove all windows or just remove all Gnus windows.
448 (let ((frame (selected-frame)))
449 (unwind-protect
450 (if gnus-use-full-window
451 ;; We want to remove all other windows.
452 (if (not gnus-frame-split-p)
453 ;; This is not a `frame' split, so we ignore the
454 ;; other frames.
455 (delete-other-windows)
23f87bed 456 ;; This is a `frame' split, so we delete all windows
16409b0b
GM
457 ;; on all frames.
458 (gnus-delete-windows-in-gnusey-frames))
459 ;; Just remove some windows.
460 (gnus-remove-some-windows)
619ac84f
SZ
461 (if (featurep 'xemacs)
462 (switch-to-buffer nntp-server-buffer)
463 (set-buffer nntp-server-buffer)))
16409b0b
GM
464 (select-frame frame)))
465
466 (let (gnus-window-frame-focus)
619ac84f
SZ
467 (if (featurep 'xemacs)
468 (switch-to-buffer nntp-server-buffer)
469 (set-buffer nntp-server-buffer))
16409b0b 470 (gnus-configure-frame split)
23f87bed 471 (run-hooks 'gnus-configure-windows-hook)
16409b0b
GM
472 (when gnus-window-frame-focus
473 (select-frame (window-frame gnus-window-frame-focus))))))))
eec82323
LMI
474
475(defun gnus-delete-windows-in-gnusey-frames ()
476 "Do a `delete-other-windows' in all frames that have Gnus windows."
6748645f 477 (let ((buffers (gnus-buffers)))
eec82323
LMI
478 (mapcar
479 (lambda (frame)
480 (unless (eq (cdr (assq 'minibuffer
481 (frame-parameters frame)))
482 'only)
483 (select-frame frame)
484 (let (do-delete)
485 (walk-windows
486 (lambda (window)
487 (when (memq (window-buffer window) buffers)
488 (setq do-delete t))))
489 (when do-delete
490 (delete-other-windows)))))
491 (frame-list))))
492
493(defun gnus-all-windows-visible-p (split)
494 "Say whether all buffers in SPLIT are currently visible.
495In particular, the value returned will be the window that
496should have point."
497 (let ((stack (list split))
498 (all-visible t)
499 type buffer win buf)
500 (while (and (setq split (pop stack))
501 all-visible)
502 ;; Be backwards compatible.
503 (when (vectorp split)
504 (setq split (append split nil)))
505 (when (or (consp (car split))
506 (vectorp (car split)))
507 (push 1.0 split)
508 (push 'vertical split))
509 ;; The SPLIT might be something that is to be evaled to
510 ;; return a new SPLIT.
511 (while (and (not (assq (car split) gnus-window-to-buffer))
23f87bed 512 (functionp (car split)))
eec82323
LMI
513 (setq split (eval split)))
514
515 (setq type (elt split 0))
516 (cond
517 ;; Nothing here.
518 ((null split) t)
519 ;; A buffer.
520 ((not (memq type '(horizontal vertical frame)))
521 (setq buffer (cond ((stringp type) type)
522 (t (cdr (assq type gnus-window-to-buffer)))))
523 (unless buffer
16409b0b 524 (error "Invalid buffer type: %s" type))
6748645f 525 (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
23f87bed 526 (setq win (gnus-get-buffer-window buf t)))
6748645f 527 (if (memq 'point split)
16409b0b 528 (setq all-visible win))
eec82323
LMI
529 (setq all-visible nil)))
530 (t
531 (when (eq type 'frame)
532 (setq gnus-frame-split-p t))
533 (setq stack (append (cddr split) stack)))))
534 (unless (eq all-visible t)
535 all-visible)))
536
537(defun gnus-window-top-edge (&optional window)
538 (nth 1 (window-edges window)))
539
540(defun gnus-remove-some-windows ()
6748645f 541 (let ((buffers (gnus-buffers))
eec82323
LMI
542 buf bufs lowest-buf lowest)
543 (save-excursion
544 ;; Remove windows on all known Gnus buffers.
6748645f
LMI
545 (while (setq buf (pop buffers))
546 (when (get-buffer-window buf)
547 (push buf bufs)
548 (pop-to-buffer buf)
549 (when (or (not lowest)
550 (< (gnus-window-top-edge) lowest))
551 (setq lowest (gnus-window-top-edge)
552 lowest-buf buf))))
eec82323
LMI
553 (when lowest-buf
554 (pop-to-buffer lowest-buf)
619ac84f
SZ
555 (if (featurep 'xemacs)
556 (switch-to-buffer nntp-server-buffer)
557 (set-buffer nntp-server-buffer)))
23f87bed
MB
558 (mapcar (lambda (b) (delete-windows-on b t))
559 (delq lowest-buf bufs)))))
560
561(eval-and-compile
562 (cond
563 ((fboundp 'frames-on-display-list)
564 (defalias 'gnus-frames-on-display-list 'frames-on-display-list))
565 ((and (featurep 'xemacs) (fboundp 'frame-device))
566 (defun gnus-frames-on-display-list ()
567 (apply 'filtered-frame-list 'identity (list (frame-device nil)))))
568 (t
569 (defalias 'gnus-frames-on-display-list 'frame-list))))
570
571(defun gnus-get-buffer-window (buffer &optional frame)
572 (cond ((and (null gnus-use-frames-on-any-display)
573 (memq frame '(t 0 visible)))
574 (car
575 (let ((frames (gnus-frames-on-display-list)))
576 (gnus-remove-if (lambda (win) (not (memq (window-frame win)
577 frames)))
578 (get-buffer-window-list buffer nil frame)))))
579 (t
580 (get-buffer-window buffer frame))))
eec82323
LMI
581
582(provide 'gnus-win)
583
ab5796a9 584;;; arch-tag: ccd5a394-2ddf-4397-b8f8-6d80d3e46e2b
eec82323 585;;; gnus-win.el ends here