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