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