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