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