*** empty log message ***
[gnus] / lisp / gnus-win.el
1 ;;; gnus-win.el --- window configuration functions for Gnus
2 ;; Copyright (C) 1996,97 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 (defgroup gnus-windows nil
31   "Window configuration."
32   :group 'gnus)
33
34 (defcustom gnus-use-full-window t
35   "*If non-nil, use the entire Emacs screen."
36   :group 'gnus-windows
37   :type 'boolean)
38
39 (defvar gnus-window-configuration nil
40   "Obsolete variable.  See `gnus-buffer-configuration'.")
41
42 (defcustom gnus-window-min-width 2
43   "*Minimum width of Gnus buffers."
44   :group 'gnus-windows
45   :type 'integer)
46
47 (defcustom gnus-window-min-height 1
48   "*Minimum height of Gnus buffers."
49   :group 'gnus-windows
50   :type 'integer)
51
52 (defcustom gnus-always-force-window-configuration nil
53   "*If non-nil, always force the Gnus window configurations."
54   :group 'gnus-windows
55   :type 'boolean)
56
57 (defvar gnus-buffer-configuration
58   '((group
59      (vertical 1.0
60                (group 1.0 point)
61                (if gnus-carpal '(group-carpal 4))))
62     (summary
63      (vertical 1.0
64                (summary 1.0 point)
65                (if gnus-carpal '(summary-carpal 4))))
66     (article
67      (cond 
68       ((and gnus-use-picons 
69             (eq gnus-picons-display-where 'picons))
70        '(frame 1.0
71                (vertical 1.0
72                          (summary 0.25 point)
73                          (if gnus-carpal '(summary-carpal 4))
74                          (article 1.0))
75                (vertical ((height . 5) (width . 15)
76                           (user-position . t)
77                           (left . -1) (top . 1))
78                          (picons 1.0))))
79       (gnus-use-trees
80        '(vertical 1.0
81                   (summary 0.25 point)
82                   (tree 0.25)
83                   (article 1.0)))
84       (t
85        '(vertical 1.0
86                  (summary 0.25 point)
87                  (if gnus-carpal '(summary-carpal 4))
88                  (article 1.0)))))
89     (server
90      (vertical 1.0
91                (server 1.0 point)
92                (if gnus-carpal '(server-carpal 2))))
93     (browse
94      (vertical 1.0
95                (browse 1.0 point)
96                (if gnus-carpal '(browse-carpal 2))))
97     (message
98      (vertical 1.0
99                (message 1.0 point)))
100     (pick
101      (vertical 1.0
102                (article 1.0 point)))
103     (info
104      (vertical 1.0
105                (info 1.0 point)))
106     (summary-faq
107      (vertical 1.0
108                (summary 0.25)
109                (faq 1.0 point)))
110     (edit-article
111      (vertical 1.0
112                (article 1.0 point)))
113     (edit-form
114      (vertical 1.0
115                (group 0.5)
116                (edit-form 1.0 point)))
117     (edit-score
118      (vertical 1.0
119                (summary 0.25)
120                (edit-score 1.0 point)))
121     (post
122      (vertical 1.0
123                (post 1.0 point)))
124     (reply
125      (vertical 1.0
126                (article-copy 0.5)
127                (message 1.0 point)))
128     (forward
129      (vertical 1.0
130                (message 1.0 point)))
131     (reply-yank
132      (vertical 1.0
133                (message 1.0 point)))
134     (mail-bounce
135      (vertical 1.0
136                (article 0.5)
137                (message 1.0 point)))
138     (draft
139      (vertical 1.0
140                (draft 1.0 point)))
141     (pipe
142      (vertical 1.0
143                (summary 0.25 point)
144                (if gnus-carpal '(summary-carpal 4))
145                ("*Shell Command Output*" 1.0)))
146     (bug
147      (vertical 1.0
148                ("*Gnus Help Bug*" 0.5)
149                ("*Gnus Bug*" 1.0 point)))
150     (score-trace
151      (vertical 1.0
152                (summary 0.5 point)
153                ("*Score Trace*" 1.0)))
154     (score-words
155      (vertical 1.0
156                (summary 0.5 point)
157                ("*Score Words*" 1.0)))
158     (compose-bounce
159      (vertical 1.0
160                (article 0.5)
161                (message 1.0 point))))
162   "Window configuration for all possible Gnus buffers.
163 See the Gnus manual for an explanation of the syntax used.")
164
165 (defvar gnus-window-to-buffer
166   '((group . gnus-group-buffer)
167     (summary . gnus-summary-buffer)
168     (article . gnus-article-buffer)
169     (server . gnus-server-buffer)
170     (browse . "*Gnus Browse Server*")
171     (edit-group . gnus-group-edit-buffer)
172     (edit-form . gnus-edit-form-buffer)
173     (edit-server . gnus-server-edit-buffer)
174     (group-carpal . gnus-carpal-group-buffer)
175     (summary-carpal . gnus-carpal-summary-buffer)
176     (server-carpal . gnus-carpal-server-buffer)
177     (browse-carpal . gnus-carpal-browse-buffer)
178     (edit-score . gnus-score-edit-buffer)
179     (message . gnus-message-buffer)
180     (mail . gnus-message-buffer)
181     (post-news . gnus-message-buffer)
182     (faq . gnus-faq-buffer)
183     (picons . "*Picons*")
184     (tree . gnus-tree-buffer)
185     (info . gnus-info-buffer)
186     (article-copy . gnus-article-copy)
187     (draft . gnus-draft-buffer))
188   "Mapping from short symbols to buffer names or buffer variables.")
189
190 ;;; Internal variables.
191
192 (defvar gnus-current-window-configuration nil
193   "The most recently set window configuration.")
194
195 (defvar gnus-created-frames nil)
196
197 (defun gnus-kill-gnus-frames ()
198   "Kill all frames Gnus has created."
199   (while gnus-created-frames
200     (when (frame-live-p (car gnus-created-frames))
201       ;; We slap a condition-case around this `delete-frame' to ensure 
202       ;; against errors if we try do delete the single frame that's left.
203       (ignore-errors
204         (delete-frame (car gnus-created-frames))))
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)
382                            result))
383           (select-window new-win)
384           (setq window new-win)
385           (setq comp-subs (cdr comp-subs))))
386       ;; Return the proper window, if any.
387       (when result
388         (select-window result))))))
389
390 (defvar gnus-frame-split-p nil)
391
392 (defun gnus-configure-windows (setting &optional force)
393   (setq gnus-current-window-configuration setting)
394   (setq force (or force gnus-always-force-window-configuration))
395   (setq setting (gnus-windows-old-to-new setting))
396   (let ((split (if (symbolp setting)
397                    (cadr (assq setting gnus-buffer-configuration))
398                  setting))
399         all-visible)
400
401     (setq gnus-frame-split-p nil)
402
403     (unless split
404       (error "No such setting: %s" setting))
405
406     (if (and (setq all-visible (gnus-all-windows-visible-p split))
407              (not force))
408         ;; All the windows mentioned are already visible, so we just
409         ;; put point in the assigned buffer, and do not touch the
410         ;; winconf.
411         (select-window all-visible)
412
413       ;; Either remove all windows or just remove all Gnus windows.
414       (let ((frame (selected-frame)))
415         (unwind-protect
416             (if gnus-use-full-window
417                 ;; We want to remove all other windows.
418                 (if (not gnus-frame-split-p)
419                     ;; This is not a `frame' split, so we ignore the
420                     ;; other frames.  
421                     (delete-other-windows)
422                   ;; This is a `frame' split, so we delete all windows
423                   ;; on all frames.
424                   (gnus-delete-windows-in-gnusey-frames))
425               ;; Just remove some windows.
426               (gnus-remove-some-windows)
427               (switch-to-buffer nntp-server-buffer))
428           (select-frame frame)))
429
430       (switch-to-buffer nntp-server-buffer)
431       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
432
433 (defun gnus-delete-windows-in-gnusey-frames ()
434   "Do a `delete-other-windows' in all frames that have Gnus windows."
435   (let ((buffers
436          (mapcar
437           (lambda (elem)
438             (if (symbolp (cdr elem))
439                 (when (and (boundp (cdr elem))
440                            (symbol-value (cdr elem)))
441                   (get-buffer (symbol-value (cdr elem))))
442               (when (cdr elem) 
443                 (get-buffer (cdr elem)))))
444           gnus-window-to-buffer)))
445     (mapcar 
446      (lambda (frame)
447        (unless (eq (cdr (assq 'minibuffer
448                               (frame-parameters frame)))
449                    'only)
450          (select-frame frame)
451          (let (do-delete)
452            (walk-windows
453             (lambda (window)
454               (when (memq (window-buffer window) buffers)
455                 (setq do-delete t))))
456            (when do-delete
457              (delete-other-windows)))))
458      (frame-list))))
459
460 (defun gnus-all-windows-visible-p (split)
461   "Say whether all buffers in SPLIT are currently visible.
462 In particular, the value returned will be the window that
463 should have point."
464   (let ((stack (list split))
465         (all-visible t)
466         type buffer win buf)
467     (while (and (setq split (pop stack))
468                 all-visible)
469       ;; Be backwards compatible.
470       (when (vectorp split)
471         (setq split (append split nil)))
472       (when (or (consp (car split))
473                 (vectorp (car split)))
474         (push 1.0 split)
475         (push 'vertical split))
476       ;; The SPLIT might be something that is to be evaled to
477       ;; return a new SPLIT.
478       (while (and (not (assq (car split) gnus-window-to-buffer))
479                   (gnus-functionp (car split)))
480         (setq split (eval split)))
481
482       (setq type (elt split 0))
483       (cond
484        ;; Nothing here.
485        ((null split) t)
486        ;; A buffer.
487        ((not (memq type '(horizontal vertical frame)))
488         (setq buffer (cond ((stringp type) type)
489                            (t (cdr (assq type gnus-window-to-buffer)))))
490         (unless buffer
491           (error "Illegal buffer type: %s" type))
492         (when (setq buf (get-buffer (if (symbolp buffer)
493                                         (symbol-value buffer)
494                                       buffer)))
495           (setq win (get-buffer-window buf t)))
496         (if win
497             (when (memq 'point split)
498               (setq all-visible win))
499           (setq all-visible nil)))
500        (t
501         (when (eq type 'frame)
502           (setq gnus-frame-split-p t))
503         (setq stack (append (cddr split) stack)))))
504     (unless (eq all-visible t)
505       all-visible)))
506
507 (defun gnus-window-top-edge (&optional window)
508   (nth 1 (window-edges window)))
509
510 (defun gnus-remove-some-windows ()
511   (let ((buffers gnus-window-to-buffer)
512         buf bufs lowest-buf lowest)
513     (save-excursion
514       ;; Remove windows on all known Gnus buffers.
515       (while buffers
516         (setq buf (cdar buffers))
517         (when (symbolp buf)
518           (setq buf (and (boundp buf) (symbol-value buf))))
519         (and buf
520              (get-buffer-window buf)
521              (progn
522                (push buf bufs)
523                (pop-to-buffer buf)
524                (when (or (not lowest)
525                          (< (gnus-window-top-edge) lowest))
526                  (setq lowest (gnus-window-top-edge))
527                  (setq lowest-buf buf))))
528         (setq buffers (cdr buffers)))
529       ;; Remove windows on *all* summary buffers.
530       (walk-windows
531        (lambda (win)
532          (let ((buf (window-buffer win)))
533            (when (string-match  "^\\*Summary" (buffer-name buf))
534              (push buf bufs)
535              (pop-to-buffer buf)
536              (when (or (not lowest)
537                        (< (gnus-window-top-edge) lowest))
538                (setq lowest-buf buf)
539                (setq lowest (gnus-window-top-edge)))))))
540       (when lowest-buf
541         (pop-to-buffer lowest-buf)
542         (switch-to-buffer nntp-server-buffer))
543       (while bufs
544         (when (not (eq (car bufs) lowest-buf))
545           (delete-windows-on (car bufs)))
546         (setq bufs (cdr bufs))))))
547
548 (provide 'gnus-win)
549
550 ;;; gnus-win.el ends here