(file-remote-p): Docstring fix.
[bpt/emacs.git] / lisp / gnus / gnus-win.el
CommitLineData
eec82323 1;;; gnus-win.el --- window configuration functions for Gnus
e84b4b86
TTN
2
3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
e3fe4da0 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
7;; Keywords: news
8
9;; This file is part of GNU Emacs.
10
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
5a9dffec 13;; the Free Software Foundation; either version 3, or (at your option)
eec82323
LMI
14;; any later version.
15
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.
20
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
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
eec82323
LMI
25
26;;; Commentary:
27
28;;; Code:
29
5ab7173c
RS
30(eval-when-compile (require 'cl))
31
eec82323 32(require 'gnus)
23f87bed 33(require 'gnus-util)
eec82323
LMI
34
35(defgroup gnus-windows nil
36 "Window configuration."
37 :group 'gnus)
38
39(defcustom gnus-use-full-window t
40 "*If non-nil, use the entire Emacs screen."
41 :group 'gnus-windows
42 :type 'boolean)
43
44(defvar gnus-window-configuration nil
45 "Obsolete variable. See `gnus-buffer-configuration'.")
46
47(defcustom gnus-window-min-width 2
48 "*Minimum width of Gnus buffers."
49 :group 'gnus-windows
50 :type 'integer)
51
52(defcustom gnus-window-min-height 1
53 "*Minimum height of Gnus buffers."
54 :group 'gnus-windows
55 :type 'integer)
56
57(defcustom gnus-always-force-window-configuration nil
58 "*If non-nil, always force the Gnus window configurations."
59 :group 'gnus-windows
60 :type 'boolean)
61
23f87bed
MB
62(defcustom gnus-use-frames-on-any-display nil
63 "*If non-nil, frames on all displays will be considered useable by Gnus.
64When nil, only frames on the same display as the selected frame will be
65used to display Gnus windows."
bf247b6e 66 :version "22.1"
23f87bed
MB
67 :group 'gnus-windows
68 :type 'boolean)
69
eec82323
LMI
70(defvar gnus-buffer-configuration
71 '((group
72 (vertical 1.0
73 (group 1.0 point)
74 (if gnus-carpal '(group-carpal 4))))
75 (summary
76 (vertical 1.0
77 (summary 1.0 point)
78 (if gnus-carpal '(summary-carpal 4))))
79 (article
80 (cond
eec82323
LMI
81 (gnus-use-trees
82 '(vertical 1.0
83 (summary 0.25 point)
84 (tree 0.25)
85 (article 1.0)))
86 (t
87 '(vertical 1.0
16409b0b
GM
88 (summary 0.25 point)
89 (if gnus-carpal '(summary-carpal 4))
90 (article 1.0)))))
eec82323
LMI
91 (server
92 (vertical 1.0
93 (server 1.0 point)
94 (if gnus-carpal '(server-carpal 2))))
95 (browse
96 (vertical 1.0
97 (browse 1.0 point)
98 (if gnus-carpal '(browse-carpal 2))))
99 (message
100 (vertical 1.0
101 (message 1.0 point)))
102 (pick
103 (vertical 1.0
104 (article 1.0 point)))
105 (info
106 (vertical 1.0
107 (info 1.0 point)))
108 (summary-faq
109 (vertical 1.0
110 (summary 0.25)
111 (faq 1.0 point)))
112 (edit-article
113 (vertical 1.0
114 (article 1.0 point)))
115 (edit-form
116 (vertical 1.0
117 (group 0.5)
118 (edit-form 1.0 point)))
119 (edit-score
120 (vertical 1.0
121 (summary 0.25)
122 (edit-score 1.0 point)))
01c52d31
MB
123 (edit-server
124 (vertical 1.0
125 (server 0.5)
126 (edit-form 1.0 point)))
eec82323
LMI
127 (post
128 (vertical 1.0
129 (post 1.0 point)))
130 (reply
131 (vertical 1.0
23f87bed 132 (article 0.5)
eec82323
LMI
133 (message 1.0 point)))
134 (forward
135 (vertical 1.0
136 (message 1.0 point)))
137 (reply-yank
138 (vertical 1.0
139 (message 1.0 point)))
140 (mail-bounce
141 (vertical 1.0
142 (article 0.5)
143 (message 1.0 point)))
eec82323
LMI
144 (pipe
145 (vertical 1.0
146 (summary 0.25 point)
147 (if gnus-carpal '(summary-carpal 4))
148 ("*Shell Command Output*" 1.0)))
149 (bug
150 (vertical 1.0
55388e55 151 (if gnus-bug-create-help-buffer '("*Gnus Help Bug*" 0.5))
eec82323
LMI
152 ("*Gnus Bug*" 1.0 point)))
153 (score-trace
154 (vertical 1.0
155 (summary 0.5 point)
156 ("*Score Trace*" 1.0)))
157 (score-words
158 (vertical 1.0
159 (summary 0.5 point)
160 ("*Score Words*" 1.0)))
6748645f
LMI
161 (split-trace
162 (vertical 1.0
163 (summary 0.5 point)
164 ("*Split Trace*" 1.0)))
165 (category
166 (vertical 1.0
167 (category 1.0)))
eec82323
LMI
168 (compose-bounce
169 (vertical 1.0
170 (article 0.5)
23f87bed
MB
171 (message 1.0 point)))
172 (display-term
01c52d31
MB
173 (vertical 1.0
174 ("*display*" 1.0)))
175 (mml-preview
176 (vertical 1.0
177 (message 0.5)
178 (mml-preview 1.0 point))))
eec82323
LMI
179 "Window configuration for all possible Gnus buffers.
180See the Gnus manual for an explanation of the syntax used.")
181
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)
eec82323 200 (tree . gnus-tree-buffer)
a8151ef7 201 (score-trace . "*Score Trace*")
6748645f 202 (split-trace . "*Split Trace*")
eec82323 203 (info . gnus-info-buffer)
6748645f 204 (category . gnus-category-buffer)
eec82323 205 (article-copy . gnus-article-copy)
01c52d31
MB
206 (draft . gnus-draft-buffer)
207 (mml-preview . mml-preview-buffer))
eec82323
LMI
208 "Mapping from short symbols to buffer names or buffer variables.")
209
23f87bed
MB
210(defcustom gnus-configure-windows-hook nil
211 "*A hook called when configuring windows."
bf247b6e 212 :version "22.1"
23f87bed
MB
213 :group 'gnus-windows
214 :type 'hook)
215
eec82323
LMI
216;;; Internal variables.
217
218(defvar gnus-current-window-configuration nil
219 "The most recently set window configuration.")
220
221(defvar gnus-created-frames nil)
6748645f 222(defvar gnus-window-frame-focus nil)
eec82323
LMI
223
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.
230 (ignore-errors
231 (delete-frame (car gnus-created-frames))))
232 (pop gnus-created-frames)))
233
234(defun gnus-window-configuration-element (list)
235 (while (and list
236 (not (assq (car list) gnus-window-configuration)))
237 (pop list))
238 (cadr (assq (car list) gnus-window-configuration)))
239
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)
243 (setq setting
244 ;; Take care of ooold GNUS 3.x values.
245 (cond ((eq setting 'SelectArticle) 'article)
246 ((memq setting '(SelectNewsgroup SelectSubject ExpandSubject))
247 'summary)
248 ((memq setting '(ExitNewsgroup)) 'group)
249 (t setting))))
250 (if (or (listp setting)
251 (not (and gnus-window-configuration
252 (memq setting '(group summary article)))))
253 setting
254 (let* ((elem
255 (cond
256 ((eq setting 'group)
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))
268 (i 0)
269 perc out)
270 (while (< i 3)
271 (or (not (numberp (nth i elem)))
272 (zerop (nth i elem))
273 (progn
274 (setq perc (if (= i 2)
275 1.0
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))
280 out)))
281 (incf i))
282 `(vertical 1.0 ,@(nreverse out)))))
283
284;;;###autoload
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))))
290
291(defvar gnus-frame-list nil)
292
6748645f
LMI
293(defun gnus-window-to-buffer-helper (obj)
294 (cond ((not (symbolp obj))
295 obj)
296 ((boundp obj)
297 (symbol-value obj))
298 ((fboundp obj)
299 (funcall obj))
300 (t
301 nil)))
302
eec82323
LMI
303(defun gnus-configure-frame (split &optional window)
304 "Split WINDOW according to SPLIT."
ca3ab2d8
SZ
305 (let ((current-window
306 (or (get-buffer-window (current-buffer)) (selected-window))))
307 (unless window
308 (setq window current-window))
309 (select-window window)
8f688cb0 310 ;; This might be an old-style buffer config.
ca3ab2d8
SZ
311 (when (vectorp split)
312 (setq split (append split nil)))
313 (when (or (consp (car split))
314 (vectorp (car split)))
315 (push 1.0 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))
23f87bed 320 (functionp (car split)))
ca3ab2d8
SZ
321 (setq split (eval split)))
322 (let* ((type (car split))
323 (subs (cddr split))
324 (len (if (eq type 'horizontal) (window-width) (window-height)))
325 (total 0)
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)
329 (cond
330 ;; Nothing to do here.
331 ((null split))
332 ;; Don't switch buffers.
333 ((null type)
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))))))
339 (unless 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.
350 ((eq type 'frame)
351 (unless gnus-frame-list
352 (setq gnus-frame-list (list (window-frame current-window))))
353 (let ((i 0)
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)
360 (setq params nil))
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.
370 (select-frame frame)
371 (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
372 (incf i))
373 ;; Select the frame that has the selected buffer.
374 (when fresult
375 (select-frame (window-frame fresult)))))
376 ;; This is a normal split.
377 (t
378 (when (> (length subs) 0)
379 ;; First we have to compute the sizes of all new windows.
380 (while subs
381 (setq sub (append (pop subs) nil))
382 (while (and (not (assq (car sub) gnus-window-to-buffer))
23f87bed 383 (functionp (car sub)))
ca3ab2d8
SZ
384 (setq sub (eval sub)))
385 (when sub
386 (push sub comp-subs)
387 (setq size (cadar comp-subs))
388 (cond ((equal size 1.0)
389 (setq rest (car comp-subs))
390 (setq s 0))
391 ((floatp size)
392 (setq s (floor (* size len))))
393 ((integerp size)
394 (setq s size))
395 (t
396 (error "Invalid size: %s" size)))
397 ;; Try to make sure that we are inside the safe limits.
398 (cond ((zerop s))
399 ((eq type 'horizontal)
400 (setq s (max s window-min-width)))
401 ((eq type 'vertical)
402 (setq s (max s window-min-height))))
403 (setcar (cdar comp-subs) s)
404 (incf total s)))
405 ;; Take care of the "1.0" spec.
406 (if rest
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
410 ;; fashion.
411 (setq comp-subs (nreverse comp-subs))
412 (while comp-subs
413 (if (null (cdr comp-subs))
414 (setq new-win window)
415 (setq new-win
416 (split-window window (cadar comp-subs)
417 (eq type 'horizontal))))
418 (setq result (or (gnus-configure-frame
419 (car comp-subs) window)
420 result))
421 (select-window new-win)
422 (setq window new-win)
423 (setq comp-subs (cdr comp-subs))))
424 ;; Return the proper window, if any.
425 (when result
426 (select-window result)))))))
eec82323
LMI
427
428(defvar gnus-frame-split-p nil)
429
430(defun gnus-configure-windows (setting &optional force)
16409b0b
GM
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))
438 setting))
439 all-visible)
440
441 (setq gnus-frame-split-p nil)
442
443 (unless split
444 (error "No such setting in `gnus-buffer-configuration': %s" setting))
445
446 (if (and (setq all-visible (gnus-all-windows-visible-p split))
447 (not force))
448 ;; All the windows mentioned are already visible, so we just
449 ;; put point in the assigned buffer, and do not touch the
450 ;; winconf.
451 (select-window all-visible)
55388e55 452
25fc4fd5
SZ
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))
16409b0b
GM
456
457 ;; Either remove all windows or just remove all Gnus windows.
458 (let ((frame (selected-frame)))
459 (unwind-protect
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
464 ;; other frames.
465 (delete-other-windows)
23f87bed 466 ;; This is a `frame' split, so we delete all windows
16409b0b
GM
467 ;; on all frames.
468 (gnus-delete-windows-in-gnusey-frames))
469 ;; Just remove some windows.
470 (gnus-remove-some-windows)
619ac84f
SZ
471 (if (featurep 'xemacs)
472 (switch-to-buffer nntp-server-buffer)
473 (set-buffer nntp-server-buffer)))
16409b0b
GM
474 (select-frame frame)))
475
476 (let (gnus-window-frame-focus)
619ac84f
SZ
477 (if (featurep 'xemacs)
478 (switch-to-buffer nntp-server-buffer)
479 (set-buffer nntp-server-buffer))
16409b0b 480 (gnus-configure-frame split)
23f87bed 481 (run-hooks 'gnus-configure-windows-hook)
16409b0b 482 (when gnus-window-frame-focus
b4fde39f
MB
483 (gnus-select-frame-set-input-focus
484 (window-frame gnus-window-frame-focus))))))))
eec82323
LMI
485
486(defun gnus-delete-windows-in-gnusey-frames ()
487 "Do a `delete-other-windows' in all frames that have Gnus windows."
6748645f 488 (let ((buffers (gnus-buffers)))
eec82323
LMI
489 (mapcar
490 (lambda (frame)
491 (unless (eq (cdr (assq 'minibuffer
492 (frame-parameters frame)))
493 'only)
494 (select-frame frame)
495 (let (do-delete)
496 (walk-windows
497 (lambda (window)
498 (when (memq (window-buffer window) buffers)
499 (setq do-delete t))))
500 (when do-delete
501 (delete-other-windows)))))
502 (frame-list))))
503
504(defun gnus-all-windows-visible-p (split)
505 "Say whether all buffers in SPLIT are currently visible.
506In particular, the value returned will be the window that
507should have point."
508 (let ((stack (list split))
509 (all-visible t)
510 type buffer win buf)
511 (while (and (setq split (pop stack))
512 all-visible)
513 ;; Be backwards compatible.
514 (when (vectorp split)
515 (setq split (append split nil)))
516 (when (or (consp (car split))
517 (vectorp (car split)))
518 (push 1.0 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))
23f87bed 523 (functionp (car split)))
eec82323
LMI
524 (setq split (eval split)))
525
526 (setq type (elt split 0))
527 (cond
528 ;; Nothing here.
529 ((null split) t)
530 ;; A buffer.
531 ((not (memq type '(horizontal vertical frame)))
532 (setq buffer (cond ((stringp type) type)
533 (t (cdr (assq type gnus-window-to-buffer)))))
534 (unless buffer
16409b0b 535 (error "Invalid buffer type: %s" type))
6748645f 536 (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
23f87bed 537 (setq win (gnus-get-buffer-window buf t)))
6748645f 538 (if (memq 'point split)
16409b0b 539 (setq all-visible win))
eec82323
LMI
540 (setq all-visible nil)))
541 (t
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)
546 all-visible)))
547
548(defun gnus-window-top-edge (&optional window)
549 (nth 1 (window-edges window)))
550
551(defun gnus-remove-some-windows ()
6748645f 552 (let ((buffers (gnus-buffers))
eec82323
LMI
553 buf bufs lowest-buf lowest)
554 (save-excursion
555 ;; Remove windows on all known Gnus buffers.
6748645f
LMI
556 (while (setq buf (pop buffers))
557 (when (get-buffer-window buf)
558 (push buf bufs)
559 (pop-to-buffer buf)
560 (when (or (not lowest)
561 (< (gnus-window-top-edge) lowest))
562 (setq lowest (gnus-window-top-edge)
563 lowest-buf buf))))
eec82323
LMI
564 (when lowest-buf
565 (pop-to-buffer lowest-buf)
619ac84f
SZ
566 (if (featurep 'xemacs)
567 (switch-to-buffer nntp-server-buffer)
568 (set-buffer nntp-server-buffer)))
23f87bed
MB
569 (mapcar (lambda (b) (delete-windows-on b t))
570 (delq lowest-buf bufs)))))
571
572(eval-and-compile
573 (cond
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)))))
579 (t
580 (defalias 'gnus-frames-on-display-list 'frame-list))))
581
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)))
585 (car
586 (let ((frames (gnus-frames-on-display-list)))
587 (gnus-remove-if (lambda (win) (not (memq (window-frame win)
588 frames)))
589 (get-buffer-window-list buffer nil frame)))))
590 (t
591 (get-buffer-window buffer frame))))
eec82323
LMI
592
593(provide 'gnus-win)
594
ab5796a9 595;;; arch-tag: ccd5a394-2ddf-4397-b8f8-6d80d3e46e2b
eec82323 596;;; gnus-win.el ends here