7cdd165a68b0056f02bb9b88a6de5c841295c104
[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       ((and gnus-use-picons (not (eq gnus-picons-display-where 'article)))
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 ;;; Internal variables.
187
188 (defvar gnus-current-window-configuration nil
189   "The most recently set window configuration.")
190
191 (defvar gnus-created-frames nil)
192
193 (defun gnus-kill-gnus-frames ()
194   "Kill all frames Gnus has created."
195   (while gnus-created-frames
196     (when (frame-live-p (car gnus-created-frames))
197       ;; We slap a condition-case around this `delete-frame' to ensure 
198       ;; against errors if we try do delete the single frame that's left.
199       (condition-case ()
200           (delete-frame (car gnus-created-frames))
201         (error nil)))
202     (pop gnus-created-frames)))
203
204 (defun gnus-window-configuration-element (list)
205   (while (and list
206               (not (assq (car list) gnus-window-configuration)))
207     (pop list))
208   (cadr (assq (car list) gnus-window-configuration)))
209
210 (defun gnus-windows-old-to-new (setting)
211   ;; First we take care of the really, really old Gnus 3 actions.
212   (when (symbolp setting)
213     (setq setting
214           ;; Take care of ooold GNUS 3.x values.
215           (cond ((eq setting 'SelectArticle) 'article)
216                 ((memq setting '(SelectNewsgroup SelectSubject ExpandSubject))
217                  'summary)
218                 ((memq setting '(ExitNewsgroup)) 'group)
219                 (t setting))))
220   (if (or (listp setting)
221           (not (and gnus-window-configuration
222                     (memq setting '(group summary article)))))
223       setting
224     (let* ((elem
225             (cond 
226              ((eq setting 'group)
227               (gnus-window-configuration-element
228                '(group newsgroups ExitNewsgroup)))
229              ((eq setting 'summary)
230               (gnus-window-configuration-element
231                '(summary SelectNewsgroup SelectSubject ExpandSubject)))
232              ((eq setting 'article)
233               (gnus-window-configuration-element
234                '(article SelectArticle)))))
235            (total (apply '+ elem))
236            (types '(group summary article))
237            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
238            (i 0)
239            perc out)
240       (while (< i 3)
241         (or (not (numberp (nth i elem)))
242             (zerop (nth i elem))
243             (progn
244               (setq perc (if (= i 2)
245                              1.0
246                            (/ (float (nth i elem)) total)))
247               (push (if (eq pbuf (nth i types))
248                         (list (nth i types) perc 'point)
249                       (list (nth i types) perc))
250                     out)))
251         (incf i))
252       `(vertical 1.0 ,@(nreverse out)))))
253
254 ;;;###autoload
255 (defun gnus-add-configuration (conf)
256   "Add the window configuration CONF to `gnus-buffer-configuration'."
257   (setq gnus-buffer-configuration
258         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
259                          gnus-buffer-configuration))))
260
261 (defvar gnus-frame-list nil)
262
263 (defun gnus-configure-frame (split &optional window)
264   "Split WINDOW according to SPLIT."
265   (unless window
266     (setq window (get-buffer-window (current-buffer))))
267   (select-window window)
268   ;; This might be an old-stylee buffer config.
269   (when (vectorp split)
270     (setq split (append split nil)))
271   (when (or (consp (car split))
272             (vectorp (car split)))
273     (push 1.0 split)
274     (push 'vertical split))
275   ;; The SPLIT might be something that is to be evaled to
276   ;; return a new SPLIT.
277   (while (and (not (assq (car split) gnus-window-to-buffer))
278               (gnus-functionp (car split)))
279     (setq split (eval split)))
280   (let* ((type (car split))
281          (subs (cddr split))
282          (len (if (eq type 'horizontal) (window-width) (window-height)))
283          (total 0)
284          (window-min-width (or gnus-window-min-width window-min-width))
285          (window-min-height (or gnus-window-min-height window-min-height))
286          s result new-win rest comp-subs size sub)
287     (cond
288      ;; Nothing to do here.
289      ((null split))
290      ;; Don't switch buffers.
291      ((null type)
292       (and (memq 'point split) window))
293      ;; This is a buffer to be selected.
294      ((not (memq type '(frame horizontal vertical)))
295       (let ((buffer (cond ((stringp type) type)
296                           (t (cdr (assq type gnus-window-to-buffer)))))
297             buf)
298         (unless buffer
299           (error "Illegal buffer type: %s" type))
300         (unless (setq buf (get-buffer (if (symbolp buffer)
301                                           (symbol-value buffer) buffer)))
302           (setq buf (get-buffer-create (if (symbolp buffer)
303                                            (symbol-value buffer) buffer))))
304         (switch-to-buffer buf)
305         ;; We return the window if it has the `point' spec.
306         (and (memq 'point split) window)))
307      ;; This is a frame split.
308      ((eq type 'frame)
309       (unless gnus-frame-list
310         (setq gnus-frame-list (list (window-frame
311                                      (get-buffer-window (current-buffer))))))
312       (let ((i 0)
313             params frame fresult)
314         (while (< i (length subs))
315           ;; Frame parameter is gotten from the sub-split.
316           (setq params (cadr (elt subs i)))
317           ;; It should be a list.
318           (unless (listp params)
319             (setq params nil))
320           ;; Create a new frame?
321           (unless (setq frame (elt gnus-frame-list i))
322             (nconc gnus-frame-list (list (setq frame (make-frame params))))
323             (push frame gnus-created-frames))
324           ;; Is the old frame still alive?
325           (unless (frame-live-p frame)
326             (setcar (nthcdr i gnus-frame-list)
327                     (setq frame (make-frame params))))
328           ;; Select the frame in question and do more splits there.
329           (select-frame frame)
330           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
331           (incf i))
332         ;; Select the frame that has the selected buffer.
333         (when fresult
334           (select-frame (window-frame fresult)))))
335      ;; This is a normal split.
336      (t
337       (when (> (length subs) 0)
338         ;; First we have to compute the sizes of all new windows.
339         (while subs
340           (setq sub (append (pop subs) nil))
341           (while (and (not (assq (car sub) gnus-window-to-buffer))
342                       (gnus-functionp (car sub)))
343             (setq sub (eval sub)))
344           (when sub
345             (push sub comp-subs)
346             (setq size (cadar comp-subs))
347             (cond ((equal size 1.0)
348                    (setq rest (car comp-subs))
349                    (setq s 0))
350                   ((floatp size)
351                    (setq s (floor (* size len))))
352                   ((integerp size)
353                    (setq s size))
354                   (t
355                    (error "Illegal size: %s" size)))
356             ;; Try to make sure that we are inside the safe limits.
357             (cond ((zerop s))
358                   ((eq type 'horizontal)
359                    (setq s (max s window-min-width)))
360                   ((eq type 'vertical)
361                    (setq s (max s window-min-height))))
362             (setcar (cdar comp-subs) s)
363             (incf total s)))
364         ;; Take care of the "1.0" spec.
365         (if rest
366             (setcar (cdr rest) (- len total))
367           (error "No 1.0 specs in %s" split))
368         ;; The we do the actual splitting in a nice recursive
369         ;; fashion.
370         (setq comp-subs (nreverse comp-subs))
371         (while comp-subs
372           (if (null (cdr comp-subs))
373               (setq new-win window)
374             (setq new-win
375                   (split-window window (cadar comp-subs)
376                                 (eq type 'horizontal))))
377           (setq result (or (gnus-configure-frame
378                             (car comp-subs) window) result))
379           (select-window new-win)
380           (setq window new-win)
381           (setq comp-subs (cdr comp-subs))))
382       ;; Return the proper window, if any.
383       (when result
384         (select-window result))))))
385
386 (defvar gnus-frame-split-p nil)
387
388 (defun gnus-configure-windows (setting &optional force)
389   (setq gnus-current-window-configuration setting)
390   (setq setting (gnus-windows-old-to-new setting))
391   (let ((split (if (symbolp setting)
392                    (cadr (assq setting gnus-buffer-configuration))
393                  setting))
394         all-visible)
395
396     (setq gnus-frame-split-p nil)
397
398     (unless split
399       (error "No such setting: %s" setting))
400
401     (if (and (setq all-visible (gnus-all-windows-visible-p split))
402              (not force))
403         ;; All the windows mentioned are already visible, so we just
404         ;; put point in the assigned buffer, and do not touch the
405         ;; winconf.
406         (select-window all-visible)
407
408       ;; Either remove all windows or just remove all Gnus windows.
409       (let ((frame (selected-frame)))
410         (unwind-protect
411             (if gnus-use-full-window
412                 ;; We want to remove all other windows.
413                 (if (not gnus-frame-split-p)
414                     ;; This is not a `frame' split, so we ignore the
415                     ;; other frames.  
416                     (delete-other-windows)
417                   ;; This is a `frame' split, so we delete all windows
418                   ;; on all frames.
419                   (mapcar 
420                    (lambda (frame)
421                      (unless (eq (cdr (assq 'minibuffer
422                                             (frame-parameters frame)))
423                                  'only)
424                        (select-frame frame)
425                        (delete-other-windows)))
426                    (frame-list)))
427               ;; Just remove some windows.
428               (gnus-remove-some-windows)
429               (switch-to-buffer nntp-server-buffer))
430           (select-frame frame)))
431
432       (switch-to-buffer nntp-server-buffer)
433       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
434
435 (defun gnus-all-windows-visible-p (split)
436   "Say whether all buffers in SPLIT are currently visible.
437 In particular, the value returned will be the window that
438 should have point."
439   (let ((stack (list split))
440         (all-visible t)
441         type buffer win buf)
442     (while (and (setq split (pop stack))
443                 all-visible)
444       ;; Be backwards compatible.
445       (when (vectorp split)
446         (setq split (append split nil)))
447       (when (or (consp (car split))
448                 (vectorp (car split)))
449         (push 1.0 split)
450         (push 'vertical split))
451       ;; The SPLIT might be something that is to be evaled to
452       ;; return a new SPLIT.
453       (while (and (not (assq (car split) gnus-window-to-buffer))
454                   (gnus-functionp (car split)))
455         (setq split (eval split)))
456
457       (setq type (elt split 0))
458       (cond
459        ;; Nothing here.
460        ((null split) t)
461        ;; A buffer.
462        ((not (memq type '(horizontal vertical frame)))
463         (setq buffer (cond ((stringp type) type)
464                            (t (cdr (assq type gnus-window-to-buffer)))))
465         (unless buffer
466           (error "Illegal buffer type: %s" type))
467         (when (setq buf (get-buffer (if (symbolp buffer)
468                                         (symbol-value buffer)
469                                       buffer)))
470           (setq win (get-buffer-window buf t)))
471         (if win
472             (when (memq 'point split)
473                 (setq all-visible win))
474           (setq all-visible nil)))
475        (t
476         (when (eq type 'frame)
477           (setq gnus-frame-split-p t))
478         (setq stack (append (cddr split) stack)))))
479     (unless (eq all-visible t)
480       all-visible)))
481
482 (defun gnus-window-top-edge (&optional window)
483   (nth 1 (window-edges window)))
484
485 (defun gnus-remove-some-windows ()
486   (let ((buffers gnus-window-to-buffer)
487         buf bufs lowest-buf lowest)
488     (save-excursion
489       ;; Remove windows on all known Gnus buffers.
490       (while buffers
491         (setq buf (cdar buffers))
492         (if (symbolp buf)
493             (setq buf (and (boundp buf) (symbol-value buf))))
494         (and buf
495              (get-buffer-window buf)
496              (progn
497                (setq bufs (cons buf bufs))
498                (pop-to-buffer buf)
499                (if (or (not lowest)
500                        (< (gnus-window-top-edge) lowest))
501                    (progn
502                      (setq lowest (gnus-window-top-edge))
503                      (setq lowest-buf buf)))))
504         (setq buffers (cdr buffers)))
505       ;; Remove windows on *all* summary buffers.
506       (walk-windows
507        (lambda (win)
508          (let ((buf (window-buffer win)))
509            (if (string-match    "^\\*Summary" (buffer-name buf))
510                (progn
511                  (setq bufs (cons buf bufs))
512                  (pop-to-buffer buf)
513                  (if (or (not lowest)
514                          (< (gnus-window-top-edge) lowest))
515                      (progn
516                        (setq lowest-buf buf)
517                        (setq lowest (gnus-window-top-edge)))))))))
518       (and lowest-buf
519            (progn
520              (pop-to-buffer lowest-buf)
521              (switch-to-buffer nntp-server-buffer)))
522       (while bufs
523         (and (not (eq (car bufs) lowest-buf))
524              (delete-windows-on (car bufs)))
525         (setq bufs (cdr bufs))))))
526
527 (provide 'gnus-win)
528
529 ;;; gnus-win.el ends here