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