(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / gnus / gnus-win.el
blob43fe77267c2b8f4524b884db7b62a1d874543903
1 ;;; gnus-win.el --- window configuration functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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 2, or (at your option)
13 ;; 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; 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.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
31 (require 'gnus)
32 (require 'gnus-util)
34 (defgroup gnus-windows nil
35 "Window configuration."
36 :group 'gnus)
38 (defcustom gnus-use-full-window t
39 "*If non-nil, use the entire Emacs screen."
40 :group 'gnus-windows
41 :type 'boolean)
43 (defvar gnus-window-configuration nil
44 "Obsolete variable. See `gnus-buffer-configuration'.")
46 (defcustom gnus-window-min-width 2
47 "*Minimum width of Gnus buffers."
48 :group 'gnus-windows
49 :type 'integer)
51 (defcustom gnus-window-min-height 1
52 "*Minimum height of Gnus buffers."
53 :group 'gnus-windows
54 :type 'integer)
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)
61 (defcustom gnus-use-frames-on-any-display nil
62 "*If non-nil, frames on all displays will be considered useable by Gnus.
63 When nil, only frames on the same display as the selected frame will be
64 used to display Gnus windows."
65 :version "22.1"
66 :group 'gnus-windows
67 :type 'boolean)
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
80 (gnus-use-trees
81 '(vertical 1.0
82 (summary 0.25 point)
83 (tree 0.25)
84 (article 1.0)))
86 '(vertical 1.0
87 (summary 0.25 point)
88 (if gnus-carpal '(summary-carpal 4))
89 (article 1.0)))))
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
127 (article 0.5)
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)))
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
146 (if gnus-bug-create-help-buffer '("*Gnus Help Bug*" 0.5))
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)))
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)))
163 (compose-bounce
164 (vertical 1.0
165 (article 0.5)
166 (message 1.0 point)))
167 (display-term
168 (vertical 1.0
169 ("*display*" 1.0))))
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 (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)
191 (tree . gnus-tree-buffer)
192 (score-trace . "*Score Trace*")
193 (split-trace . "*Split Trace*")
194 (info . gnus-info-buffer)
195 (category . gnus-category-buffer)
196 (article-copy . gnus-article-copy)
197 (draft . gnus-draft-buffer))
198 "Mapping from short symbols to buffer names or buffer variables.")
200 (defcustom gnus-configure-windows-hook nil
201 "*A hook called when configuring windows."
202 :version "22.1"
203 :group 'gnus-windows
204 :type 'hook)
206 ;;; Internal variables.
208 (defvar gnus-current-window-configuration nil
209 "The most recently set window configuration.")
211 (defvar gnus-created-frames nil)
212 (defvar gnus-window-frame-focus nil)
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)))
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)))
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)
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)))))
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))))
281 (defvar gnus-frame-list nil)
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))
291 nil)))
293 (defun gnus-configure-frame (split &optional window)
294 "Split WINDOW according to SPLIT."
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)
300 ;; This might be an old-style buffer config.
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))
310 (functionp (car split)))
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.
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))
373 (functionp (car sub)))
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))
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)))))))
418 (defvar gnus-frame-split-p nil)
420 (defun gnus-configure-windows (setting &optional force)
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)
431 (setq gnus-frame-split-p nil)
433 (unless split
434 (error "No such setting in `gnus-buffer-configuration': %s" setting))
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)
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))
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)
456 ;; This is a `frame' split, so we delete all windows
457 ;; on all frames.
458 (gnus-delete-windows-in-gnusey-frames))
459 ;; Just remove some windows.
460 (gnus-remove-some-windows)
461 (if (featurep 'xemacs)
462 (switch-to-buffer nntp-server-buffer)
463 (set-buffer nntp-server-buffer)))
464 (select-frame frame)))
466 (let (gnus-window-frame-focus)
467 (if (featurep 'xemacs)
468 (switch-to-buffer nntp-server-buffer)
469 (set-buffer nntp-server-buffer))
470 (gnus-configure-frame split)
471 (run-hooks 'gnus-configure-windows-hook)
472 (when gnus-window-frame-focus
473 (select-frame (window-frame gnus-window-frame-focus))))))))
475 (defun gnus-delete-windows-in-gnusey-frames ()
476 "Do a `delete-other-windows' in all frames that have Gnus windows."
477 (let ((buffers (gnus-buffers)))
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))))
493 (defun gnus-all-windows-visible-p (split)
494 "Say whether all buffers in SPLIT are currently visible.
495 In particular, the value returned will be the window that
496 should 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))
512 (functionp (car split)))
513 (setq split (eval split)))
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
524 (error "Invalid buffer type: %s" type))
525 (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
526 (setq win (gnus-get-buffer-window buf t)))
527 (if (memq 'point split)
528 (setq all-visible win))
529 (setq all-visible nil)))
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)))
537 (defun gnus-window-top-edge (&optional window)
538 (nth 1 (window-edges window)))
540 (defun gnus-remove-some-windows ()
541 (let ((buffers (gnus-buffers))
542 buf bufs lowest-buf lowest)
543 (save-excursion
544 ;; Remove windows on all known Gnus buffers.
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))))
553 (when lowest-buf
554 (pop-to-buffer lowest-buf)
555 (if (featurep 'xemacs)
556 (switch-to-buffer nntp-server-buffer)
557 (set-buffer nntp-server-buffer)))
558 (mapcar (lambda (b) (delete-windows-on b t))
559 (delq lowest-buf bufs)))))
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)))))
569 (defalias 'gnus-frames-on-display-list 'frame-list))))
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)))))
580 (get-buffer-window buffer frame))))
582 (provide 'gnus-win)
584 ;;; arch-tag: ccd5a394-2ddf-4397-b8f8-6d80d3e46e2b
585 ;;; gnus-win.el ends here