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