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