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