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