*** 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   (cond ((not (symbolp obj))
272          obj)
273         ((boundp obj)
274          (symbol-value obj))
275         ((fboundp obj)
276          (funcall obj))
277         (t
278          nil)))
279
280 (defun gnus-configure-frame (split &optional window)
281   "Split WINDOW according to SPLIT."
282   (unless window
283     (setq window (get-buffer-window (current-buffer))))
284   (select-window window)
285   ;; This might be an old-stylee buffer config.
286   (when (vectorp split)
287     (setq split (append split nil)))
288   (when (or (consp (car split))
289             (vectorp (car split)))
290     (push 1.0 split)
291     (push 'vertical split))
292   ;; The SPLIT might be something that is to be evaled to
293   ;; return a new SPLIT.
294   (while (and (not (assq (car split) gnus-window-to-buffer))
295               (gnus-functionp (car split)))
296     (setq split (eval split)))
297   (let* ((type (car split))
298          (subs (cddr split))
299          (len (if (eq type 'horizontal) (window-width) (window-height)))
300          (total 0)
301          (window-min-width (or gnus-window-min-width window-min-width))
302          (window-min-height (or gnus-window-min-height window-min-height))
303          s result new-win rest comp-subs size sub)
304     (cond
305      ;; Nothing to do here.
306      ((null split))
307      ;; Don't switch buffers.
308      ((null type)
309       (and (memq 'point split) window))
310      ;; This is a buffer to be selected.
311      ((not (memq type '(frame horizontal vertical)))
312       (let ((buffer (cond ((stringp type) type)
313                           (t (cdr (assq type gnus-window-to-buffer))))))
314         (unless buffer
315           (error "Illegal buffer type: %s" type))
316         (switch-to-buffer (get-buffer-create
317                            (gnus-window-to-buffer-helper buffer)))
318         ;; We return the window if it has the `point' spec.
319         (and (memq 'point split) window)))
320      ;; This is a frame split.
321      ((eq type 'frame)
322       (unless gnus-frame-list
323         (setq gnus-frame-list (list (window-frame
324                                      (get-buffer-window (current-buffer))))))
325       (let ((i 0)
326             params frame fresult)
327         (while (< i (length subs))
328           ;; Frame parameter is gotten from the sub-split.
329           (setq params (cadr (elt subs i)))
330           ;; It should be a list.
331           (unless (listp params)
332             (setq params nil))
333           ;; Create a new frame?
334           (unless (setq frame (elt gnus-frame-list i))
335             (nconc gnus-frame-list (list (setq frame (make-frame params))))
336             (push frame gnus-created-frames))
337           ;; Is the old frame still alive?
338           (unless (frame-live-p frame)
339             (setcar (nthcdr i gnus-frame-list)
340                     (setq frame (make-frame params))))
341           ;; Select the frame in question and do more splits there.
342           (select-frame frame)
343           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
344           (incf i))
345         ;; Select the frame that has the selected buffer.
346         (when fresult
347           (select-frame (window-frame fresult)))))
348      ;; This is a normal split.
349      (t
350       (when (> (length subs) 0)
351         ;; First we have to compute the sizes of all new windows.
352         (while subs
353           (setq sub (append (pop subs) nil))
354           (while (and (not (assq (car sub) gnus-window-to-buffer))
355                       (gnus-functionp (car sub)))
356             (setq sub (eval sub)))
357           (when sub
358             (push sub comp-subs)
359             (setq size (cadar comp-subs))
360             (cond ((equal size 1.0)
361                    (setq rest (car comp-subs))
362                    (setq s 0))
363                   ((floatp size)
364                    (setq s (floor (* size len))))
365                   ((integerp size)
366                    (setq s size))
367                   (t
368                    (error "Illegal size: %s" size)))
369             ;; Try to make sure that we are inside the safe limits.
370             (cond ((zerop s))
371                   ((eq type 'horizontal)
372                    (setq s (max s window-min-width)))
373                   ((eq type 'vertical)
374                    (setq s (max s window-min-height))))
375             (setcar (cdar comp-subs) s)
376             (incf total s)))
377         ;; Take care of the "1.0" spec.
378         (if rest
379             (setcar (cdr rest) (- len total))
380           (error "No 1.0 specs in %s" split))
381         ;; The we do the actual splitting in a nice recursive
382         ;; fashion.
383         (setq comp-subs (nreverse comp-subs))
384         (while comp-subs
385           (if (null (cdr comp-subs))
386               (setq new-win window)
387             (setq new-win
388                   (split-window window (cadar comp-subs)
389                                 (eq type 'horizontal))))
390           (setq result (or (gnus-configure-frame
391                             (car comp-subs) window)
392                            result))
393           (select-window new-win)
394           (setq window new-win)
395           (setq comp-subs (cdr comp-subs))))
396       ;; Return the proper window, if any.
397       (when result
398         (select-window result))))))
399
400 (defvar gnus-frame-split-p nil)
401
402 (defun gnus-configure-windows (setting &optional force)
403   (setq gnus-current-window-configuration setting)
404   (setq force (or force gnus-always-force-window-configuration))
405   (setq setting (gnus-windows-old-to-new setting))
406   (let ((split (if (symbolp setting)
407                    (cadr (assq setting gnus-buffer-configuration))
408                  setting))
409         all-visible)
410
411     (setq gnus-frame-split-p nil)
412
413     (unless split
414       (error "No such setting: %s" setting))
415
416     (if (and (setq all-visible (gnus-all-windows-visible-p split))
417              (not force))
418         ;; All the windows mentioned are already visible, so we just
419         ;; put point in the assigned buffer, and do not touch the
420         ;; winconf.
421         (select-window all-visible)
422
423       ;; Either remove all windows or just remove all Gnus windows.
424       (let ((frame (selected-frame)))
425         (unwind-protect
426             (if gnus-use-full-window
427                 ;; We want to remove all other windows.
428                 (if (not gnus-frame-split-p)
429                     ;; This is not a `frame' split, so we ignore the
430                     ;; other frames.
431                     (delete-other-windows)
432                   ;; This is a `frame' split, so we delete all windows
433                   ;; on all frames.
434                   (gnus-delete-windows-in-gnusey-frames))
435               ;; Just remove some windows.
436               (gnus-remove-some-windows)
437               (switch-to-buffer nntp-server-buffer))
438           (select-frame frame)))
439
440       (switch-to-buffer nntp-server-buffer)
441       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
442
443 (defun gnus-delete-windows-in-gnusey-frames ()
444   "Do a `delete-other-windows' in all frames that have Gnus windows."
445   (let ((buffers
446          (mapcar
447           (lambda (elem)
448             (get-buffer (gnus-window-to-buffer-helper (cdr elem))))
449           gnus-window-to-buffer)))
450     (mapcar
451      (lambda (frame)
452        (unless (eq (cdr (assq 'minibuffer
453                               (frame-parameters frame)))
454                    'only)
455          (select-frame frame)
456          (let (do-delete)
457            (walk-windows
458             (lambda (window)
459               (when (memq (window-buffer window) buffers)
460                 (setq do-delete t))))
461            (when do-delete
462              (delete-other-windows)))))
463      (frame-list))))
464
465 (defun gnus-all-windows-visible-p (split)
466   "Say whether all buffers in SPLIT are currently visible.
467 In particular, the value returned will be the window that
468 should have point."
469   (let ((stack (list split))
470         (all-visible t)
471         type buffer win buf)
472     (while (and (setq split (pop stack))
473                 all-visible)
474       ;; Be backwards compatible.
475       (when (vectorp split)
476         (setq split (append split nil)))
477       (when (or (consp (car split))
478                 (vectorp (car split)))
479         (push 1.0 split)
480         (push 'vertical split))
481       ;; The SPLIT might be something that is to be evaled to
482       ;; return a new SPLIT.
483       (while (and (not (assq (car split) gnus-window-to-buffer))
484                   (gnus-functionp (car split)))
485         (setq split (eval split)))
486
487       (setq type (elt split 0))
488       (cond
489        ;; Nothing here.
490        ((null split) t)
491        ;; A buffer.
492        ((not (memq type '(horizontal vertical frame)))
493         (setq buffer (cond ((stringp type) type)
494                            (t (cdr (assq type gnus-window-to-buffer)))))
495         (unless buffer
496           (error "Illegal buffer type: %s" type))
497         (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
498                  (setq win (get-buffer-window buf t)))
499             (if (memq 'point split)
500               (setq all-visible win))
501           (setq all-visible nil)))
502        (t
503         (when (eq type 'frame)
504           (setq gnus-frame-split-p t))
505         (setq stack (append (cddr split) stack)))))
506     (unless (eq all-visible t)
507       all-visible)))
508
509 (defun gnus-window-top-edge (&optional window)
510   (nth 1 (window-edges window)))
511
512 (defun gnus-remove-some-windows ()
513   (let ((buffers gnus-window-to-buffer)
514         buf bufs lowest-buf lowest)
515     (save-excursion
516       ;; Remove windows on all known Gnus buffers.
517       (while buffers
518         (and (setq buf (gnus-window-to-buffer-helper (cdar buffers)))
519              (get-buffer-window buf)
520              (progn
521                (push buf bufs)
522                (pop-to-buffer buf)
523                (when (or (not lowest)
524                          (< (gnus-window-top-edge) lowest))
525                  (setq lowest (gnus-window-top-edge))
526                  (setq lowest-buf buf))))
527         (setq buffers (cdr buffers)))
528       ;; Remove windows on *all* summary buffers.
529       (walk-windows
530        (lambda (win)
531          (let ((buf (window-buffer win)))
532            (when (string-match  "^\\*\\(Dead \\)?Summary" (buffer-name buf))
533              (push buf bufs)
534              (pop-to-buffer buf)
535              (when (or (not lowest)
536                        (< (gnus-window-top-edge) lowest))
537                (setq lowest-buf buf)
538                (setq lowest (gnus-window-top-edge)))))))
539       (when lowest-buf
540         (pop-to-buffer lowest-buf)
541         (switch-to-buffer nntp-server-buffer))
542       (while bufs
543         (when (not (eq (car bufs) lowest-buf))
544           (delete-windows-on (car bufs)))
545         (setq bufs (cdr bufs))))))
546
547 (provide 'gnus-win)
548
549 ;;; gnus-win.el ends here