*** 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     (draft
141      (vertical 1.0
142                (draft 1.0 point)))
143     (pipe
144      (vertical 1.0
145                (summary 0.25 point)
146                (if gnus-carpal '(summary-carpal 4))
147                ("*Shell Command Output*" 1.0)))
148     (bug
149      (vertical 1.0
150                ("*Gnus Help Bug*" 0.5)
151                ("*Gnus Bug*" 1.0 point)))
152     (score-trace
153      (vertical 1.0
154                (summary 0.5 point)
155                ("*Score Trace*" 1.0)))
156     (score-words
157      (vertical 1.0
158                (summary 0.5 point)
159                ("*Score Words*" 1.0)))
160     (category
161      (vertical 1.0
162                (category 1.0)))
163     (compose-bounce
164      (vertical 1.0
165                (article 0.5)
166                (message 1.0 point))))
167   "Window configuration for all possible Gnus buffers.
168 See the Gnus manual for an explanation of the syntax used.")
169
170 (defvar gnus-window-to-buffer
171   '((group . gnus-group-buffer)
172     (summary . gnus-summary-buffer)
173     (article . gnus-article-buffer)
174     (server . gnus-server-buffer)
175     (browse . "*Gnus Browse Server*")
176     (edit-group . gnus-group-edit-buffer)
177     (edit-form . gnus-edit-form-buffer)
178     (edit-server . gnus-server-edit-buffer)
179     (group-carpal . gnus-carpal-group-buffer)
180     (summary-carpal . gnus-carpal-summary-buffer)
181     (server-carpal . gnus-carpal-server-buffer)
182     (browse-carpal . gnus-carpal-browse-buffer)
183     (edit-score . gnus-score-edit-buffer)
184     (message . gnus-message-buffer)
185     (mail . gnus-message-buffer)
186     (post-news . gnus-message-buffer)
187     (faq . gnus-faq-buffer)
188     (picons . "*Picons*")
189     (tree . gnus-tree-buffer)
190     (score-trace . "*Score Trace*")
191     (info . gnus-info-buffer)
192     (category . gnus-category-buffer)
193     (article-copy . gnus-article-copy)
194     (draft . gnus-draft-buffer))
195   "Mapping from short symbols to buffer names or buffer variables.")
196
197 ;;; Internal variables.
198
199 (defvar gnus-current-window-configuration nil
200   "The most recently set window configuration.")
201
202 (defvar gnus-created-frames nil)
203
204 (defun gnus-kill-gnus-frames ()
205   "Kill all frames Gnus has created."
206   (while gnus-created-frames
207     (when (frame-live-p (car gnus-created-frames))
208       ;; We slap a condition-case around this `delete-frame' to ensure
209       ;; against errors if we try do delete the single frame that's left.
210       (ignore-errors
211         (delete-frame (car gnus-created-frames))))
212     (pop gnus-created-frames)))
213
214 (defun gnus-window-configuration-element (list)
215   (while (and list
216               (not (assq (car list) gnus-window-configuration)))
217     (pop list))
218   (cadr (assq (car list) gnus-window-configuration)))
219
220 (defun gnus-windows-old-to-new (setting)
221   ;; First we take care of the really, really old Gnus 3 actions.
222   (when (symbolp setting)
223     (setq setting
224           ;; Take care of ooold GNUS 3.x values.
225           (cond ((eq setting 'SelectArticle) 'article)
226                 ((memq setting '(SelectNewsgroup SelectSubject ExpandSubject))
227                  'summary)
228                 ((memq setting '(ExitNewsgroup)) 'group)
229                 (t setting))))
230   (if (or (listp setting)
231           (not (and gnus-window-configuration
232                     (memq setting '(group summary article)))))
233       setting
234     (let* ((elem
235             (cond
236              ((eq setting 'group)
237               (gnus-window-configuration-element
238                '(group newsgroups ExitNewsgroup)))
239              ((eq setting 'summary)
240               (gnus-window-configuration-element
241                '(summary SelectNewsgroup SelectSubject ExpandSubject)))
242              ((eq setting 'article)
243               (gnus-window-configuration-element
244                '(article SelectArticle)))))
245            (total (apply '+ elem))
246            (types '(group summary article))
247            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
248            (i 0)
249            perc out)
250       (while (< i 3)
251         (or (not (numberp (nth i elem)))
252             (zerop (nth i elem))
253             (progn
254               (setq perc (if (= i 2)
255                              1.0
256                            (/ (float (nth i elem)) total)))
257               (push (if (eq pbuf (nth i types))
258                         (list (nth i types) perc 'point)
259                       (list (nth i types) perc))
260                     out)))
261         (incf i))
262       `(vertical 1.0 ,@(nreverse out)))))
263
264 ;;;###autoload
265 (defun gnus-add-configuration (conf)
266   "Add the window configuration CONF to `gnus-buffer-configuration'."
267   (setq gnus-buffer-configuration
268         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
269                          gnus-buffer-configuration))))
270
271 (defvar gnus-frame-list nil)
272
273 (defun gnus-configure-frame (split &optional window)
274   "Split WINDOW according to SPLIT."
275   (unless window
276     (setq window (get-buffer-window (current-buffer))))
277   (select-window window)
278   ;; This might be an old-stylee buffer config.
279   (when (vectorp split)
280     (setq split (append split nil)))
281   (when (or (consp (car split))
282             (vectorp (car split)))
283     (push 1.0 split)
284     (push 'vertical split))
285   ;; The SPLIT might be something that is to be evaled to
286   ;; return a new SPLIT.
287   (while (and (not (assq (car split) gnus-window-to-buffer))
288               (gnus-functionp (car split)))
289     (setq split (eval split)))
290   (let* ((type (car split))
291          (subs (cddr split))
292          (len (if (eq type 'horizontal) (window-width) (window-height)))
293          (total 0)
294          (window-min-width (or gnus-window-min-width window-min-width))
295          (window-min-height (or gnus-window-min-height window-min-height))
296          s result new-win rest comp-subs size sub)
297     (cond
298      ;; Nothing to do here.
299      ((null split))
300      ;; Don't switch buffers.
301      ((null type)
302       (and (memq 'point split) window))
303      ;; This is a buffer to be selected.
304      ((not (memq type '(frame horizontal vertical)))
305       (let ((buffer (cond ((stringp type) type)
306                           (t (cdr (assq type gnus-window-to-buffer)))))
307             buf)
308         (unless buffer
309           (error "Illegal buffer type: %s" type))
310         (unless (setq buf (get-buffer (if (symbolp buffer)
311                                           (symbol-value buffer) buffer)))
312           (setq buf (get-buffer-create (if (symbolp buffer)
313                                            (symbol-value buffer) buffer))))
314         (switch-to-buffer buf)
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             (if (symbolp (cdr elem))
446                 (when (and (boundp (cdr elem))
447                            (symbol-value (cdr elem)))
448                   (get-buffer (symbol-value (cdr elem))))
449               (when (cdr elem)
450                 (get-buffer (cdr elem)))))
451           gnus-window-to-buffer)))
452     (mapcar
453      (lambda (frame)
454        (unless (eq (cdr (assq 'minibuffer
455                               (frame-parameters frame)))
456                    'only)
457          (select-frame frame)
458          (let (do-delete)
459            (walk-windows
460             (lambda (window)
461               (when (memq (window-buffer window) buffers)
462                 (setq do-delete t))))
463            (when do-delete
464              (delete-other-windows)))))
465      (frame-list))))
466
467 (defun gnus-all-windows-visible-p (split)
468   "Say whether all buffers in SPLIT are currently visible.
469 In particular, the value returned will be the window that
470 should have point."
471   (let ((stack (list split))
472         (all-visible t)
473         type buffer win buf)
474     (while (and (setq split (pop stack))
475                 all-visible)
476       ;; Be backwards compatible.
477       (when (vectorp split)
478         (setq split (append split nil)))
479       (when (or (consp (car split))
480                 (vectorp (car split)))
481         (push 1.0 split)
482         (push 'vertical split))
483       ;; The SPLIT might be something that is to be evaled to
484       ;; return a new SPLIT.
485       (while (and (not (assq (car split) gnus-window-to-buffer))
486                   (gnus-functionp (car split)))
487         (setq split (eval split)))
488
489       (setq type (elt split 0))
490       (cond
491        ;; Nothing here.
492        ((null split) t)
493        ;; A buffer.
494        ((not (memq type '(horizontal vertical frame)))
495         (setq buffer (cond ((stringp type) type)
496                            (t (cdr (assq type gnus-window-to-buffer)))))
497         (unless buffer
498           (error "Illegal buffer type: %s" type))
499         (when (setq buf (get-buffer (if (symbolp buffer)
500                                         (symbol-value buffer)
501                                       buffer)))
502           (setq win (get-buffer-window buf t)))
503         (if win
504             (when (memq 'point split)
505               (setq all-visible win))
506           (setq all-visible nil)))
507        (t
508         (when (eq type 'frame)
509           (setq gnus-frame-split-p t))
510         (setq stack (append (cddr split) stack)))))
511     (unless (eq all-visible t)
512       all-visible)))
513
514 (defun gnus-window-top-edge (&optional window)
515   (nth 1 (window-edges window)))
516
517 (defun gnus-remove-some-windows ()
518   (let ((buffers gnus-window-to-buffer)
519         buf bufs lowest-buf lowest)
520     (save-excursion
521       ;; Remove windows on all known Gnus buffers.
522       (while buffers
523         (setq buf (cdar buffers))
524         (when (symbolp buf)
525           (setq buf (and (boundp buf) (symbol-value buf))))
526         (and buf
527              (get-buffer-window buf)
528              (progn
529                (push buf bufs)
530                (pop-to-buffer buf)
531                (when (or (not lowest)
532                          (< (gnus-window-top-edge) lowest))
533                  (setq lowest (gnus-window-top-edge))
534                  (setq lowest-buf buf))))
535         (setq buffers (cdr buffers)))
536       ;; Remove windows on *all* summary buffers.
537       (walk-windows
538        (lambda (win)
539          (let ((buf (window-buffer win)))
540            (when (string-match  "^\\*Summary" (buffer-name buf))
541              (push buf bufs)
542              (pop-to-buffer buf)
543              (when (or (not lowest)
544                        (< (gnus-window-top-edge) lowest))
545                (setq lowest-buf buf)
546                (setq lowest (gnus-window-top-edge)))))))
547       (when lowest-buf
548         (pop-to-buffer lowest-buf)
549         (switch-to-buffer nntp-server-buffer))
550       (while bufs
551         (when (not (eq (car bufs) lowest-buf))
552           (delete-windows-on (car bufs)))
553         (setq bufs (cdr bufs))))))
554
555 (provide 'gnus-win)
556
557 ;;; gnus-win.el ends here