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