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