*** empty log message ***
[gnus] / lisp / gnus-win.el
1 ;;; gnus-win.el --- window configuration functions for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus-load)
29
30 (defvar gnus-use-full-window t
31   "*If non-nil, use the entire Emacs screen.")
32
33 (defvar gnus-window-configuration nil
34   "Obsolete variable.  See `gnus-buffer-configuration'.")
35
36 (defvar gnus-window-min-width 2
37   "*Minimum width of Gnus buffers.")
38
39 (defvar gnus-window-min-height 1
40   "*Minimum height of Gnus buffers.")
41
42 (defvar gnus-buffer-configuration
43   '((group
44      (vertical 1.0
45                (group 1.0 point)
46                (if gnus-carpal '(group-carpal 4))))
47     (summary
48      (vertical 1.0
49                (summary 1.0 point)
50                (if gnus-carpal '(summary-carpal 4))))
51     (article
52      (cond 
53       (gnus-use-picons
54        '(frame 1.0
55                (vertical 1.0
56                          (summary 0.25 point)
57                          (if gnus-carpal '(summary-carpal 4))
58                          (article 1.0))
59                (vertical ((height . 5) (width . 15)
60                           (user-position . t)
61                           (left . -1) (top . 1))
62                          (picons 1.0))))
63       (gnus-use-trees
64        '(vertical 1.0
65                   (summary 0.25 point)
66                   (tree 0.25)
67                   (article 1.0)))
68       (t
69        '(vertical 1.0
70                  (summary 0.25 point)
71                  (if gnus-carpal '(summary-carpal 4))
72                  (article 1.0)))))
73     (server
74      (vertical 1.0
75                (server 1.0 point)
76                (if gnus-carpal '(server-carpal 2))))
77     (browse
78      (vertical 1.0
79                (browse 1.0 point)
80                (if gnus-carpal '(browse-carpal 2))))
81     (message
82      (vertical 1.0
83                (message 1.0 point)))
84     (pick
85      (vertical 1.0
86                (article 1.0 point)))
87     (info
88      (vertical 1.0
89                (info 1.0 point)))
90     (summary-faq
91      (vertical 1.0
92                (summary 0.25)
93                (faq 1.0 point)))
94     (edit-form
95      (vertical 1.0
96                (group 0.5)
97                (edit-form 1.0 point)))
98     (edit-score
99      (vertical 1.0
100                (summary 0.25)
101                (edit-score 1.0 point)))
102     (post
103      (vertical 1.0
104                (post 1.0 point)))
105     (reply
106      (vertical 1.0
107                (article-copy 0.5)
108                (message 1.0 point)))
109     (forward
110      (vertical 1.0
111                (message 1.0 point)))
112     (reply-yank
113      (vertical 1.0
114                (message 1.0 point)))
115     (mail-bounce
116      (vertical 1.0
117                (article 0.5)
118                (message 1.0 point)))
119     (draft
120      (vertical 1.0
121                (draft 1.0 point)))
122     (pipe
123      (vertical 1.0
124                (summary 0.25 point)
125                (if gnus-carpal '(summary-carpal 4))
126                ("*Shell Command Output*" 1.0)))
127     (bug
128      (vertical 1.0
129                ("*Gnus Help Bug*" 0.5)
130                ("*Gnus Bug*" 1.0 point)))
131     (compose-bounce
132      (vertical 1.0
133                (article 0.5)
134                (message 1.0 point))))
135   "Window configuration for all possible Gnus buffers.
136 This variable is a list of lists.  Each of these lists has a NAME and
137 a RULE.  The NAMEs are commonsense names like `group', which names a
138 rule used when displaying the group buffer; `summary', which names a
139 rule for what happens when you enter a group and do not display an
140 article buffer; and so on.  See the value of this variable for a
141 complete list of NAMEs.
142
143 Each RULE is a list of vectors.  The first element in this vector is
144 the name of the buffer to be displayed; the second element is the
145 percentage of the screen this buffer is to occupy (a number in the
146 0.0-0.99 range); the optional third element is `point', which should
147 be present to denote which buffer point is to go to after making this
148 buffer configuration.")
149
150 (defvar gnus-window-to-buffer
151   '((group . gnus-group-buffer)
152     (summary . gnus-summary-buffer)
153     (article . gnus-article-buffer)
154     (server . gnus-server-buffer)
155     (browse . "*Gnus Browse Server*")
156     (edit-group . gnus-group-edit-buffer)
157     (edit-group . gnus-edit-form-buffer)
158     (edit-server . gnus-server-edit-buffer)
159     (group-carpal . gnus-carpal-group-buffer)
160     (summary-carpal . gnus-carpal-summary-buffer)
161     (server-carpal . gnus-carpal-server-buffer)
162     (browse-carpal . gnus-carpal-browse-buffer)
163     (edit-score . gnus-score-edit-buffer)
164     (message . gnus-message-buffer)
165     (mail . gnus-message-buffer)
166     (post-news . gnus-message-buffer)
167     (faq . gnus-faq-buffer)
168     (picons . "*Picons*")
169     (tree . gnus-tree-buffer)
170     (info . gnus-info-buffer)
171     (article-copy . gnus-article-copy)
172     (draft . gnus-draft-buffer))
173   "Mapping from short symbols to buffer names or buffer variables.")
174
175 (defvar gnus-created-frames nil)
176
177 (defun gnus-kill-gnus-frames ()
178   "Kill all frames Gnus has created."
179   (while gnus-created-frames
180     (when (frame-live-p (car gnus-created-frames))
181       ;; We slap a condition-case around this `delete-frame' to ensure 
182       ;; against errors if we try do delete the single frame that's left.
183       (condition-case ()
184           (delete-frame (car gnus-created-frames))
185         (error nil)))
186     (pop gnus-created-frames)))
187
188 (defun gnus-windows-old-to-new (setting)
189   ;; First we take care of the really, really old Gnus 3 actions.
190   (when (symbolp setting)
191     (setq setting
192           ;; Take care of ooold GNUS 3.x values.
193           (cond ((eq setting 'SelectArticle) 'article)
194                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
195                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
196                 (t setting))))
197   (if (or (listp setting)
198           (not (and gnus-window-configuration
199                     (memq setting '(group summary article)))))
200       setting
201     (let* ((setting (if (eq setting 'group)
202                         (if (assq 'newsgroup gnus-window-configuration)
203                             'newsgroup
204                           'newsgroups) setting))
205            (elem (cadr (assq setting gnus-window-configuration)))
206            (total (apply '+ elem))
207            (types '(group summary article))
208            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
209            (i 0)
210            perc
211            out)
212       (while (< i 3)
213         (or (not (numberp (nth i elem)))
214             (zerop (nth i elem))
215             (progn
216               (setq perc (if (= i 2)
217                              1.0
218                            (/ (float (nth 0 elem)) total)))
219               (setq out (cons (if (eq pbuf (nth i types))
220                                   (list (nth i types) perc 'point)
221                                 (list (nth i types) perc))
222                               out))))
223         (setq i (1+ i)))
224       `(vertical 1.0 ,@(nreverse out)))))
225
226 ;;;###autoload
227 (defun gnus-add-configuration (conf)
228   "Add the window configuration CONF to `gnus-buffer-configuration'."
229   (setq gnus-buffer-configuration
230         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
231                          gnus-buffer-configuration))))
232
233 (defvar gnus-frame-list nil)
234
235 (defun gnus-configure-frame (split &optional window)
236   "Split WINDOW according to SPLIT."
237   (unless window
238     (setq window (get-buffer-window (current-buffer))))
239   (select-window window)
240   ;; This might be an old-stylee buffer config.
241   (when (vectorp split)
242     (setq split (append split nil)))
243   (when (or (consp (car split))
244             (vectorp (car split)))
245     (push 1.0 split)
246     (push 'vertical split))
247   ;; The SPLIT might be something that is to be evaled to
248   ;; return a new SPLIT.
249   (while (and (not (assq (car split) gnus-window-to-buffer))
250               (gnus-functionp (car split)))
251     (setq split (eval split)))
252   (let* ((type (car split))
253          (subs (cddr split))
254          (len (if (eq type 'horizontal) (window-width) (window-height)))
255          (total 0)
256          (window-min-width (or gnus-window-min-width window-min-width))
257          (window-min-height (or gnus-window-min-height window-min-height))
258          s result new-win rest comp-subs size sub)
259     (cond
260      ;; Nothing to do here.
261      ((null split))
262      ;; Don't switch buffers.
263      ((null type)
264       (and (memq 'point split) window))
265      ;; This is a buffer to be selected.
266      ((not (memq type '(frame horizontal vertical)))
267       (let ((buffer (cond ((stringp type) type)
268                           (t (cdr (assq type gnus-window-to-buffer)))))
269             buf)
270         (unless buffer
271           (error "Illegal buffer type: %s" type))
272         (unless (setq buf (get-buffer (if (symbolp buffer)
273                                           (symbol-value buffer) buffer)))
274           (setq buf (get-buffer-create (if (symbolp buffer)
275                                            (symbol-value buffer) buffer))))
276         (switch-to-buffer buf)
277         ;; We return the window if it has the `point' spec.
278         (and (memq 'point split) window)))
279      ;; This is a frame split.
280      ((eq type 'frame)
281       (unless gnus-frame-list
282         (setq gnus-frame-list (list (window-frame
283                                      (get-buffer-window (current-buffer))))))
284       (let ((i 0)
285             params frame fresult)
286         (while (< i (length subs))
287           ;; Frame parameter is gotten from the sub-split.
288           (setq params (cadr (elt subs i)))
289           ;; It should be a list.
290           (unless (listp params)
291             (setq params nil))
292           ;; Create a new frame?
293           (unless (setq frame (elt gnus-frame-list i))
294             (nconc gnus-frame-list (list (setq frame (make-frame params))))
295             (push frame gnus-created-frames))
296           ;; Is the old frame still alive?
297           (unless (frame-live-p frame)
298             (setcar (nthcdr i gnus-frame-list)
299                     (setq frame (make-frame params))))
300           ;; Select the frame in question and do more splits there.
301           (select-frame frame)
302           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
303           (incf i))
304         ;; Select the frame that has the selected buffer.
305         (when fresult
306           (select-frame (window-frame fresult)))))
307      ;; This is a normal split.
308      (t
309       (when (> (length subs) 0)
310         ;; First we have to compute the sizes of all new windows.
311         (while subs
312           (setq sub (append (pop subs) nil))
313           (while (and (not (assq (car sub) gnus-window-to-buffer))
314                       (gnus-functionp (car sub)))
315             (setq sub (eval sub)))
316           (when sub
317             (push sub comp-subs)
318             (setq size (cadar comp-subs))
319             (cond ((equal size 1.0)
320                    (setq rest (car comp-subs))
321                    (setq s 0))
322                   ((floatp size)
323                    (setq s (floor (* size len))))
324                   ((integerp size)
325                    (setq s size))
326                   (t
327                    (error "Illegal size: %s" size)))
328             ;; Try to make sure that we are inside the safe limits.
329             (cond ((zerop s))
330                   ((eq type 'horizontal)
331                    (setq s (max s window-min-width)))
332                   ((eq type 'vertical)
333                    (setq s (max s window-min-height))))
334             (setcar (cdar comp-subs) s)
335             (incf total s)))
336         ;; Take care of the "1.0" spec.
337         (if rest
338             (setcar (cdr rest) (- len total))
339           (error "No 1.0 specs in %s" split))
340         ;; The we do the actual splitting in a nice recursive
341         ;; fashion.
342         (setq comp-subs (nreverse comp-subs))
343         (while comp-subs
344           (if (null (cdr comp-subs))
345               (setq new-win window)
346             (setq new-win
347                   (split-window window (cadar comp-subs)
348                                 (eq type 'horizontal))))
349           (setq result (or (gnus-configure-frame
350                             (car comp-subs) window) result))
351           (select-window new-win)
352           (setq window new-win)
353           (setq comp-subs (cdr comp-subs))))
354       ;; Return the proper window, if any.
355       (when result
356         (select-window result))))))
357
358 (defvar gnus-frame-split-p nil)
359
360 (defun gnus-configure-windows (setting &optional force)
361   (setq setting (gnus-windows-old-to-new setting))
362   (let ((split (if (symbolp setting)
363                    (cadr (assq setting gnus-buffer-configuration))
364                  setting))
365         all-visible)
366
367     (setq gnus-frame-split-p nil)
368
369     (unless split
370       (error "No such setting: %s" setting))
371
372     (if (and (setq all-visible (gnus-all-windows-visible-p split))
373              (not force))
374         ;; All the windows mentioned are already visible, so we just
375         ;; put point in the assigned buffer, and do not touch the
376         ;; winconf.
377         (select-window all-visible)
378
379       ;; Either remove all windows or just remove all Gnus windows.
380       (let ((frame (selected-frame)))
381         (unwind-protect
382             (if gnus-use-full-window
383                 ;; We want to remove all other windows.
384                 (if (not gnus-frame-split-p)
385                     ;; This is not a `frame' split, so we ignore the
386                     ;; other frames.  
387                     (delete-other-windows)
388                   ;; This is a `frame' split, so we delete all windows
389                   ;; on all frames.
390                   (mapcar 
391                    (lambda (frame)
392                      (unless (eq (cdr (assq 'minibuffer
393                                             (frame-parameters frame)))
394                                  'only)
395                        (select-frame frame)
396                        (delete-other-windows)))
397                    (frame-list)))
398               ;; Just remove some windows.
399               (gnus-remove-some-windows)
400               (switch-to-buffer nntp-server-buffer))
401           (select-frame frame)))
402
403       (switch-to-buffer nntp-server-buffer)
404       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
405
406 (defun gnus-all-windows-visible-p (split)
407   "Say whether all buffers in SPLIT are currently visible.
408 In particular, the value returned will be the window that
409 should have point."
410   (let ((stack (list split))
411         (all-visible t)
412         type buffer win buf)
413     (while (and (setq split (pop stack))
414                 all-visible)
415       ;; Be backwards compatible.
416       (when (vectorp split)
417         (setq split (append split nil)))
418       (when (or (consp (car split))
419                 (vectorp (car split)))
420         (push 1.0 split)
421         (push 'vertical split))
422       ;; The SPLIT might be something that is to be evaled to
423       ;; return a new SPLIT.
424       (while (and (not (assq (car split) gnus-window-to-buffer))
425                   (gnus-functionp (car split)))
426         (setq split (eval split)))
427
428       (setq type (elt split 0))
429       (cond
430        ;; Nothing here.
431        ((null split) t)
432        ;; A buffer.
433        ((not (memq type '(horizontal vertical frame)))
434         (setq buffer (cond ((stringp type) type)
435                            (t (cdr (assq type gnus-window-to-buffer)))))
436         (unless buffer
437           (error "Illegal buffer type: %s" type))
438         (when (setq buf (get-buffer (if (symbolp buffer)
439                                         (symbol-value buffer)
440                                       buffer)))
441           (setq win (get-buffer-window buf t)))
442         (if win
443             (when (memq 'point split)
444                 (setq all-visible win))
445           (setq all-visible nil)))
446        (t
447         (when (eq type 'frame)
448           (setq gnus-frame-split-p t))
449         (setq stack (append (cddr split) stack)))))
450     (unless (eq all-visible t)
451       all-visible)))
452
453 (defun gnus-window-top-edge (&optional window)
454   (nth 1 (window-edges window)))
455
456 (defun gnus-remove-some-windows ()
457   (let ((buffers gnus-window-to-buffer)
458         buf bufs lowest-buf lowest)
459     (save-excursion
460       ;; Remove windows on all known Gnus buffers.
461       (while buffers
462         (setq buf (cdar buffers))
463         (if (symbolp buf)
464             (setq buf (and (boundp buf) (symbol-value buf))))
465         (and buf
466              (get-buffer-window buf)
467              (progn
468                (setq bufs (cons buf bufs))
469                (pop-to-buffer buf)
470                (if (or (not lowest)
471                        (< (gnus-window-top-edge) lowest))
472                    (progn
473                      (setq lowest (gnus-window-top-edge))
474                      (setq lowest-buf buf)))))
475         (setq buffers (cdr buffers)))
476       ;; Remove windows on *all* summary buffers.
477       (walk-windows
478        (lambda (win)
479          (let ((buf (window-buffer win)))
480            (if (string-match    "^\\*Summary" (buffer-name buf))
481                (progn
482                  (setq bufs (cons buf bufs))
483                  (pop-to-buffer buf)
484                  (if (or (not lowest)
485                          (< (gnus-window-top-edge) lowest))
486                      (progn
487                        (setq lowest-buf buf)
488                        (setq lowest (gnus-window-top-edge)))))))))
489       (and lowest-buf
490            (progn
491              (pop-to-buffer lowest-buf)
492              (switch-to-buffer nntp-server-buffer)))
493       (while bufs
494         (and (not (eq (car bufs) lowest-buf))
495              (delete-windows-on (car bufs)))
496         (setq bufs (cdr bufs))))))
497
498 (provide 'gnus-win)
499
500 ;;; gnus-win.el ends here