*** 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-article
95      (vertical 1.0
96                (article 1.0 point)))
97     (edit-form
98      (vertical 1.0
99                (group 0.5)
100                (edit-form 1.0 point)))
101     (edit-score
102      (vertical 1.0
103                (summary 0.25)
104                (edit-score 1.0 point)))
105     (post
106      (vertical 1.0
107                (post 1.0 point)))
108     (reply
109      (vertical 1.0
110                (article-copy 0.5)
111                (message 1.0 point)))
112     (forward
113      (vertical 1.0
114                (message 1.0 point)))
115     (reply-yank
116      (vertical 1.0
117                (message 1.0 point)))
118     (mail-bounce
119      (vertical 1.0
120                (article 0.5)
121                (message 1.0 point)))
122     (draft
123      (vertical 1.0
124                (draft 1.0 point)))
125     (pipe
126      (vertical 1.0
127                (summary 0.25 point)
128                (if gnus-carpal '(summary-carpal 4))
129                ("*Shell Command Output*" 1.0)))
130     (bug
131      (vertical 1.0
132                ("*Gnus Help Bug*" 0.5)
133                ("*Gnus Bug*" 1.0 point)))
134     (score-trace
135      (vertical 1.0
136                (summary 0.5 point)
137                ("*Score Trace*" 1.0)))
138     (score-trace
139      (vertical 1.0
140                (summary 0.5 point)
141                ("*Score Words*" 1.0)))
142     (compose-bounce
143      (vertical 1.0
144                (article 0.5)
145                (message 1.0 point))))
146   "Window configuration for all possible Gnus buffers.
147 This variable is a list of lists.  Each of these lists has a NAME and
148 a RULE.  The NAMEs are commonsense names like `group', which names a
149 rule used when displaying the group buffer; `summary', which names a
150 rule for what happens when you enter a group and do not display an
151 article buffer; and so on.  See the value of this variable for a
152 complete list of NAMEs.
153
154 Each RULE is a list of vectors.  The first element in this vector is
155 the name of the buffer to be displayed; the second element is the
156 percentage of the screen this buffer is to occupy (a number in the
157 0.0-0.99 range); the optional third element is `point', which should
158 be present to denote which buffer point is to go to after making this
159 buffer configuration.")
160
161 (defvar gnus-window-to-buffer
162   '((group . gnus-group-buffer)
163     (summary . gnus-summary-buffer)
164     (article . gnus-article-buffer)
165     (server . gnus-server-buffer)
166     (browse . "*Gnus Browse Server*")
167     (edit-group . gnus-group-edit-buffer)
168     (edit-form . gnus-edit-form-buffer)
169     (edit-server . gnus-server-edit-buffer)
170     (group-carpal . gnus-carpal-group-buffer)
171     (summary-carpal . gnus-carpal-summary-buffer)
172     (server-carpal . gnus-carpal-server-buffer)
173     (browse-carpal . gnus-carpal-browse-buffer)
174     (edit-score . gnus-score-edit-buffer)
175     (message . gnus-message-buffer)
176     (mail . gnus-message-buffer)
177     (post-news . gnus-message-buffer)
178     (faq . gnus-faq-buffer)
179     (picons . "*Picons*")
180     (tree . gnus-tree-buffer)
181     (info . gnus-info-buffer)
182     (article-copy . gnus-article-copy)
183     (draft . gnus-draft-buffer))
184   "Mapping from short symbols to buffer names or buffer variables.")
185
186 (defvar gnus-created-frames nil)
187
188 (defun gnus-kill-gnus-frames ()
189   "Kill all frames Gnus has created."
190   (while gnus-created-frames
191     (when (frame-live-p (car gnus-created-frames))
192       ;; We slap a condition-case around this `delete-frame' to ensure 
193       ;; against errors if we try do delete the single frame that's left.
194       (condition-case ()
195           (delete-frame (car gnus-created-frames))
196         (error nil)))
197     (pop gnus-created-frames)))
198
199 (defun gnus-windows-old-to-new (setting)
200   ;; First we take care of the really, really old Gnus 3 actions.
201   (when (symbolp setting)
202     (setq setting
203           ;; Take care of ooold GNUS 3.x values.
204           (cond ((eq setting 'SelectArticle) 'article)
205                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
206                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
207                 (t setting))))
208   (if (or (listp setting)
209           (not (and gnus-window-configuration
210                     (memq setting '(group summary article)))))
211       setting
212     (let* ((setting (if (eq setting 'group)
213                         (if (assq 'newsgroup gnus-window-configuration)
214                             'newsgroup
215                           'newsgroups) setting))
216            (elem (cadr (assq setting gnus-window-configuration)))
217            (total (apply '+ elem))
218            (types '(group summary article))
219            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
220            (i 0)
221            perc
222            out)
223       (while (< i 3)
224         (or (not (numberp (nth i elem)))
225             (zerop (nth i elem))
226             (progn
227               (setq perc (if (= i 2)
228                              1.0
229                            (/ (float (nth 0 elem)) total)))
230               (setq out (cons (if (eq pbuf (nth i types))
231                                   (list (nth i types) perc 'point)
232                                 (list (nth i types) perc))
233                               out))))
234         (setq i (1+ i)))
235       `(vertical 1.0 ,@(nreverse out)))))
236
237 ;;;###autoload
238 (defun gnus-add-configuration (conf)
239   "Add the window configuration CONF to `gnus-buffer-configuration'."
240   (setq gnus-buffer-configuration
241         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
242                          gnus-buffer-configuration))))
243
244 (defvar gnus-frame-list nil)
245
246 (defun gnus-configure-frame (split &optional window)
247   "Split WINDOW according to SPLIT."
248   (unless window
249     (setq window (get-buffer-window (current-buffer))))
250   (select-window window)
251   ;; This might be an old-stylee buffer config.
252   (when (vectorp split)
253     (setq split (append split nil)))
254   (when (or (consp (car split))
255             (vectorp (car split)))
256     (push 1.0 split)
257     (push 'vertical split))
258   ;; The SPLIT might be something that is to be evaled to
259   ;; return a new SPLIT.
260   (while (and (not (assq (car split) gnus-window-to-buffer))
261               (gnus-functionp (car split)))
262     (setq split (eval split)))
263   (let* ((type (car split))
264          (subs (cddr split))
265          (len (if (eq type 'horizontal) (window-width) (window-height)))
266          (total 0)
267          (window-min-width (or gnus-window-min-width window-min-width))
268          (window-min-height (or gnus-window-min-height window-min-height))
269          s result new-win rest comp-subs size sub)
270     (cond
271      ;; Nothing to do here.
272      ((null split))
273      ;; Don't switch buffers.
274      ((null type)
275       (and (memq 'point split) window))
276      ;; This is a buffer to be selected.
277      ((not (memq type '(frame horizontal vertical)))
278       (let ((buffer (cond ((stringp type) type)
279                           (t (cdr (assq type gnus-window-to-buffer)))))
280             buf)
281         (unless buffer
282           (error "Illegal buffer type: %s" type))
283         (unless (setq buf (get-buffer (if (symbolp buffer)
284                                           (symbol-value buffer) buffer)))
285           (setq buf (get-buffer-create (if (symbolp buffer)
286                                            (symbol-value buffer) buffer))))
287         (switch-to-buffer buf)
288         ;; We return the window if it has the `point' spec.
289         (and (memq 'point split) window)))
290      ;; This is a frame split.
291      ((eq type 'frame)
292       (unless gnus-frame-list
293         (setq gnus-frame-list (list (window-frame
294                                      (get-buffer-window (current-buffer))))))
295       (let ((i 0)
296             params frame fresult)
297         (while (< i (length subs))
298           ;; Frame parameter is gotten from the sub-split.
299           (setq params (cadr (elt subs i)))
300           ;; It should be a list.
301           (unless (listp params)
302             (setq params nil))
303           ;; Create a new frame?
304           (unless (setq frame (elt gnus-frame-list i))
305             (nconc gnus-frame-list (list (setq frame (make-frame params))))
306             (push frame gnus-created-frames))
307           ;; Is the old frame still alive?
308           (unless (frame-live-p frame)
309             (setcar (nthcdr i gnus-frame-list)
310                     (setq frame (make-frame params))))
311           ;; Select the frame in question and do more splits there.
312           (select-frame frame)
313           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
314           (incf i))
315         ;; Select the frame that has the selected buffer.
316         (when fresult
317           (select-frame (window-frame fresult)))))
318      ;; This is a normal split.
319      (t
320       (when (> (length subs) 0)
321         ;; First we have to compute the sizes of all new windows.
322         (while subs
323           (setq sub (append (pop subs) nil))
324           (while (and (not (assq (car sub) gnus-window-to-buffer))
325                       (gnus-functionp (car sub)))
326             (setq sub (eval sub)))
327           (when sub
328             (push sub comp-subs)
329             (setq size (cadar comp-subs))
330             (cond ((equal size 1.0)
331                    (setq rest (car comp-subs))
332                    (setq s 0))
333                   ((floatp size)
334                    (setq s (floor (* size len))))
335                   ((integerp size)
336                    (setq s size))
337                   (t
338                    (error "Illegal size: %s" size)))
339             ;; Try to make sure that we are inside the safe limits.
340             (cond ((zerop s))
341                   ((eq type 'horizontal)
342                    (setq s (max s window-min-width)))
343                   ((eq type 'vertical)
344                    (setq s (max s window-min-height))))
345             (setcar (cdar comp-subs) s)
346             (incf total s)))
347         ;; Take care of the "1.0" spec.
348         (if rest
349             (setcar (cdr rest) (- len total))
350           (error "No 1.0 specs in %s" split))
351         ;; The we do the actual splitting in a nice recursive
352         ;; fashion.
353         (setq comp-subs (nreverse comp-subs))
354         (while comp-subs
355           (if (null (cdr comp-subs))
356               (setq new-win window)
357             (setq new-win
358                   (split-window window (cadar comp-subs)
359                                 (eq type 'horizontal))))
360           (setq result (or (gnus-configure-frame
361                             (car comp-subs) window) result))
362           (select-window new-win)
363           (setq window new-win)
364           (setq comp-subs (cdr comp-subs))))
365       ;; Return the proper window, if any.
366       (when result
367         (select-window result))))))
368
369 (defvar gnus-frame-split-p nil)
370
371 (defun gnus-configure-windows (setting &optional force)
372   (setq setting (gnus-windows-old-to-new setting))
373   (let ((split (if (symbolp setting)
374                    (cadr (assq setting gnus-buffer-configuration))
375                  setting))
376         all-visible)
377
378     (setq gnus-frame-split-p nil)
379
380     (unless split
381       (error "No such setting: %s" setting))
382
383     (if (and (setq all-visible (gnus-all-windows-visible-p split))
384              (not force))
385         ;; All the windows mentioned are already visible, so we just
386         ;; put point in the assigned buffer, and do not touch the
387         ;; winconf.
388         (select-window all-visible)
389
390       ;; Either remove all windows or just remove all Gnus windows.
391       (let ((frame (selected-frame)))
392         (unwind-protect
393             (if gnus-use-full-window
394                 ;; We want to remove all other windows.
395                 (if (not gnus-frame-split-p)
396                     ;; This is not a `frame' split, so we ignore the
397                     ;; other frames.  
398                     (delete-other-windows)
399                   ;; This is a `frame' split, so we delete all windows
400                   ;; on all frames.
401                   (mapcar 
402                    (lambda (frame)
403                      (unless (eq (cdr (assq 'minibuffer
404                                             (frame-parameters frame)))
405                                  'only)
406                        (select-frame frame)
407                        (delete-other-windows)))
408                    (frame-list)))
409               ;; Just remove some windows.
410               (gnus-remove-some-windows)
411               (switch-to-buffer nntp-server-buffer))
412           (select-frame frame)))
413
414       (switch-to-buffer nntp-server-buffer)
415       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
416
417 (defun gnus-all-windows-visible-p (split)
418   "Say whether all buffers in SPLIT are currently visible.
419 In particular, the value returned will be the window that
420 should have point."
421   (let ((stack (list split))
422         (all-visible t)
423         type buffer win buf)
424     (while (and (setq split (pop stack))
425                 all-visible)
426       ;; Be backwards compatible.
427       (when (vectorp split)
428         (setq split (append split nil)))
429       (when (or (consp (car split))
430                 (vectorp (car split)))
431         (push 1.0 split)
432         (push 'vertical split))
433       ;; The SPLIT might be something that is to be evaled to
434       ;; return a new SPLIT.
435       (while (and (not (assq (car split) gnus-window-to-buffer))
436                   (gnus-functionp (car split)))
437         (setq split (eval split)))
438
439       (setq type (elt split 0))
440       (cond
441        ;; Nothing here.
442        ((null split) t)
443        ;; A buffer.
444        ((not (memq type '(horizontal vertical frame)))
445         (setq buffer (cond ((stringp type) type)
446                            (t (cdr (assq type gnus-window-to-buffer)))))
447         (unless buffer
448           (error "Illegal buffer type: %s" type))
449         (when (setq buf (get-buffer (if (symbolp buffer)
450                                         (symbol-value buffer)
451                                       buffer)))
452           (setq win (get-buffer-window buf t)))
453         (if win
454             (when (memq 'point split)
455                 (setq all-visible win))
456           (setq all-visible nil)))
457        (t
458         (when (eq type 'frame)
459           (setq gnus-frame-split-p t))
460         (setq stack (append (cddr split) stack)))))
461     (unless (eq all-visible t)
462       all-visible)))
463
464 (defun gnus-window-top-edge (&optional window)
465   (nth 1 (window-edges window)))
466
467 (defun gnus-remove-some-windows ()
468   (let ((buffers gnus-window-to-buffer)
469         buf bufs lowest-buf lowest)
470     (save-excursion
471       ;; Remove windows on all known Gnus buffers.
472       (while buffers
473         (setq buf (cdar buffers))
474         (if (symbolp buf)
475             (setq buf (and (boundp buf) (symbol-value buf))))
476         (and buf
477              (get-buffer-window buf)
478              (progn
479                (setq bufs (cons buf bufs))
480                (pop-to-buffer buf)
481                (if (or (not lowest)
482                        (< (gnus-window-top-edge) lowest))
483                    (progn
484                      (setq lowest (gnus-window-top-edge))
485                      (setq lowest-buf buf)))))
486         (setq buffers (cdr buffers)))
487       ;; Remove windows on *all* summary buffers.
488       (walk-windows
489        (lambda (win)
490          (let ((buf (window-buffer win)))
491            (if (string-match    "^\\*Summary" (buffer-name buf))
492                (progn
493                  (setq bufs (cons buf bufs))
494                  (pop-to-buffer buf)
495                  (if (or (not lowest)
496                          (< (gnus-window-top-edge) lowest))
497                      (progn
498                        (setq lowest-buf buf)
499                        (setq lowest (gnus-window-top-edge)))))))))
500       (and lowest-buf
501            (progn
502              (pop-to-buffer lowest-buf)
503              (switch-to-buffer nntp-server-buffer)))
504       (while bufs
505         (and (not (eq (car bufs) lowest-buf))
506              (delete-windows-on (car bufs)))
507         (setq bufs (cdr bufs))))))
508
509 (provide 'gnus-win)
510
511 ;;; gnus-win.el ends here