* gnus-win.el (gnus-configure-windows-hook): New hook.
[gnus] / lisp / gnus-win.el
1 ;;; gnus-win.el --- window configuration functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-util)
33
34 (defgroup gnus-windows nil
35   "Window configuration."
36   :group 'gnus)
37
38 (defcustom gnus-use-full-window t
39   "*If non-nil, use the entire Emacs screen."
40   :group 'gnus-windows
41   :type 'boolean)
42
43 (defvar gnus-window-configuration nil
44   "Obsolete variable.  See `gnus-buffer-configuration'.")
45
46 (defcustom gnus-window-min-width 2
47   "*Minimum width of Gnus buffers."
48   :group 'gnus-windows
49   :type 'integer)
50
51 (defcustom gnus-window-min-height 1
52   "*Minimum height of Gnus buffers."
53   :group 'gnus-windows
54   :type 'integer)
55
56 (defcustom gnus-always-force-window-configuration nil
57   "*If non-nil, always force the Gnus window configurations."
58   :group 'gnus-windows
59   :type 'boolean)
60
61 (defcustom gnus-use-frames-on-any-display nil
62   "*If non-nil, frames on all displays will be considered useable by Gnus.
63 When nil, only frames on the same display as the selected frame will be
64 used to display Gnus windows."
65   :group 'gnus-windows
66   :type 'boolean)
67
68 (defvar gnus-buffer-configuration
69   '((group
70      (vertical 1.0
71                (group 1.0 point)
72                (if gnus-carpal '(group-carpal 4))))
73     (summary
74      (vertical 1.0
75                (summary 1.0 point)
76                (if gnus-carpal '(summary-carpal 4))))
77     (article
78      (cond
79       ((and gnus-use-picons
80             (eq gnus-picons-display-where 'picons))
81        '(frame 1.0
82                (vertical 1.0
83                          (summary 0.25 point)
84                          (if gnus-carpal '(summary-carpal 4))
85                          (article 1.0))
86                (vertical ((height . 5) (width . 15)
87                           (user-position . t)
88                           (left . -1) (top . 1))
89                          (picons 1.0))))
90       (gnus-use-trees
91        '(vertical 1.0
92                   (summary 0.25 point)
93                   (tree 0.25)
94                   (article 1.0)))
95       (t
96        '(vertical 1.0
97                   (summary 0.25 point)
98                   (if gnus-carpal '(summary-carpal 4))
99                   (article 1.0)))))
100     (server
101      (vertical 1.0
102                (server 1.0 point)
103                (if gnus-carpal '(server-carpal 2))))
104     (browse
105      (vertical 1.0
106                (browse 1.0 point)
107                (if gnus-carpal '(browse-carpal 2))))
108     (message
109      (vertical 1.0
110                (message 1.0 point)))
111     (pick
112      (vertical 1.0
113                (article 1.0 point)))
114     (info
115      (vertical 1.0
116                (info 1.0 point)))
117     (summary-faq
118      (vertical 1.0
119                (summary 0.25)
120                (faq 1.0 point)))
121     (edit-article
122      (vertical 1.0
123                (article 1.0 point)))
124     (edit-form
125      (vertical 1.0
126                (group 0.5)
127                (edit-form 1.0 point)))
128     (edit-score
129      (vertical 1.0
130                (summary 0.25)
131                (edit-score 1.0 point)))
132     (post
133      (vertical 1.0
134                (post 1.0 point)))
135     (reply
136      (vertical 1.0
137                (article-copy 0.5)
138                (message 1.0 point)))
139     (forward
140      (vertical 1.0
141                (message 1.0 point)))
142     (reply-yank
143      (vertical 1.0
144                (message 1.0 point)))
145     (mail-bounce
146      (vertical 1.0
147                (article 0.5)
148                (message 1.0 point)))
149     (pipe
150      (vertical 1.0
151                (summary 0.25 point)
152                (if gnus-carpal '(summary-carpal 4))
153                ("*Shell Command Output*" 1.0)))
154     (bug
155      (vertical 1.0
156                (if gnus-bug-create-help-buffer '("*Gnus Help Bug*" 0.5))
157                ("*Gnus Bug*" 1.0 point)))
158     (score-trace
159      (vertical 1.0
160                (summary 0.5 point)
161                ("*Score Trace*" 1.0)))
162     (score-words
163      (vertical 1.0
164                (summary 0.5 point)
165                ("*Score Words*" 1.0)))
166     (split-trace
167      (vertical 1.0
168                (summary 0.5 point)
169                ("*Split Trace*" 1.0)))
170     (category
171      (vertical 1.0
172                (category 1.0)))
173     (compose-bounce
174      (vertical 1.0
175                (article 0.5)
176                (message 1.0 point)))
177     (display-term
178       (vertical 1.0
179                 ("*display*" 1.0))))
180   "Window configuration for all possible Gnus buffers.
181 See the Gnus manual for an explanation of the syntax used.")
182
183 (defvar gnus-window-to-buffer
184   '((group . gnus-group-buffer)
185     (summary . gnus-summary-buffer)
186     (article . gnus-article-buffer)
187     (server . gnus-server-buffer)
188     (browse . "*Gnus Browse Server*")
189     (edit-group . gnus-group-edit-buffer)
190     (edit-form . gnus-edit-form-buffer)
191     (edit-server . gnus-server-edit-buffer)
192     (group-carpal . gnus-carpal-group-buffer)
193     (summary-carpal . gnus-carpal-summary-buffer)
194     (server-carpal . gnus-carpal-server-buffer)
195     (browse-carpal . gnus-carpal-browse-buffer)
196     (edit-score . gnus-score-edit-buffer)
197     (message . gnus-message-buffer)
198     (mail . gnus-message-buffer)
199     (post-news . gnus-message-buffer)
200     (faq . gnus-faq-buffer)
201     (picons . gnus-picons-buffer-name)
202     (tree . gnus-tree-buffer)
203     (score-trace . "*Score Trace*")
204     (split-trace . "*Split Trace*")
205     (info . gnus-info-buffer)
206     (category . gnus-category-buffer)
207     (article-copy . gnus-article-copy)
208     (draft . gnus-draft-buffer))
209   "Mapping from short symbols to buffer names or buffer variables.")
210
211 (defcustom gnus-configure-windows-hook nil
212   "*A hook called when configuring windows."
213   :group 'gnus-windowns
214   :type 'hook)
215
216 ;;; Internal variables.
217
218 (defvar gnus-current-window-configuration nil
219   "The most recently set window configuration.")
220
221 (defvar gnus-created-frames nil)
222 (defvar gnus-window-frame-focus nil)
223
224 (defun gnus-kill-gnus-frames ()
225   "Kill all frames Gnus has created."
226   (while gnus-created-frames
227     (when (frame-live-p (car gnus-created-frames))
228       ;; We slap a condition-case around this `delete-frame' to ensure
229       ;; against errors if we try do delete the single frame that's left.
230       (ignore-errors
231         (delete-frame (car gnus-created-frames))))
232     (pop gnus-created-frames)))
233
234 (defun gnus-window-configuration-element (list)
235   (while (and list
236               (not (assq (car list) gnus-window-configuration)))
237     (pop list))
238   (cadr (assq (car list) gnus-window-configuration)))
239
240 (defun gnus-windows-old-to-new (setting)
241   ;; First we take care of the really, really old Gnus 3 actions.
242   (when (symbolp setting)
243     (setq setting
244           ;; Take care of ooold GNUS 3.x values.
245           (cond ((eq setting 'SelectArticle) 'article)
246                 ((memq setting '(SelectNewsgroup SelectSubject ExpandSubject))
247                  'summary)
248                 ((memq setting '(ExitNewsgroup)) 'group)
249                 (t setting))))
250   (if (or (listp setting)
251           (not (and gnus-window-configuration
252                     (memq setting '(group summary article)))))
253       setting
254     (let* ((elem
255             (cond
256              ((eq setting 'group)
257               (gnus-window-configuration-element
258                '(group newsgroups ExitNewsgroup)))
259              ((eq setting 'summary)
260               (gnus-window-configuration-element
261                '(summary SelectNewsgroup SelectSubject ExpandSubject)))
262              ((eq setting 'article)
263               (gnus-window-configuration-element
264                '(article SelectArticle)))))
265            (total (apply '+ elem))
266            (types '(group summary article))
267            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
268            (i 0)
269            perc out)
270       (while (< i 3)
271         (or (not (numberp (nth i elem)))
272             (zerop (nth i elem))
273             (progn
274               (setq perc (if (= i 2)
275                              1.0
276                            (/ (float (nth i elem)) total)))
277               (push (if (eq pbuf (nth i types))
278                         (list (nth i types) perc 'point)
279                       (list (nth i types) perc))
280                     out)))
281         (incf i))
282       `(vertical 1.0 ,@(nreverse out)))))
283
284 ;;;###autoload
285 (defun gnus-add-configuration (conf)
286   "Add the window configuration CONF to `gnus-buffer-configuration'."
287   (setq gnus-buffer-configuration
288         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
289                          gnus-buffer-configuration))))
290
291 (defvar gnus-frame-list nil)
292
293 (defun gnus-window-to-buffer-helper (obj)
294   (cond ((not (symbolp obj))
295          obj)
296         ((boundp obj)
297          (symbol-value obj))
298         ((fboundp obj)
299          (funcall obj))
300         (t
301          nil)))
302
303 (defun gnus-configure-frame (split &optional window)
304   "Split WINDOW according to SPLIT."
305   (let ((current-window
306          (or (get-buffer-window (current-buffer)) (selected-window))))
307     (unless window
308       (setq window current-window))
309     (select-window window)
310     ;; This might be an old-stylee buffer config.
311     (when (vectorp split)
312       (setq split (append split nil)))
313     (when (or (consp (car split))
314               (vectorp (car split)))
315       (push 1.0 split)
316       (push 'vertical split))
317     ;; The SPLIT might be something that is to be evaled to
318     ;; return a new SPLIT.
319     (while (and (not (assq (car split) gnus-window-to-buffer))
320                 (gnus-functionp (car split)))
321       (setq split (eval split)))
322     (let* ((type (car split))
323            (subs (cddr split))
324            (len (if (eq type 'horizontal) (window-width) (window-height)))
325            (total 0)
326            (window-min-width (or gnus-window-min-width window-min-width))
327            (window-min-height (or gnus-window-min-height window-min-height))
328            s result new-win rest comp-subs size sub)
329       (cond
330        ;; Nothing to do here.
331        ((null split))
332        ;; Don't switch buffers.
333        ((null type)
334         (and (memq 'point split) window))
335        ;; This is a buffer to be selected.
336        ((not (memq type '(frame horizontal vertical)))
337         (let ((buffer (cond ((stringp type) type)
338                             (t (cdr (assq type gnus-window-to-buffer))))))
339           (unless buffer
340             (error "Invalid buffer type: %s" type))
341           (let ((buf (gnus-get-buffer-create
342                       (gnus-window-to-buffer-helper buffer))))
343             (if (eq buf (window-buffer (selected-window))) (set-buffer buf)
344               (switch-to-buffer buf)))
345           (when (memq 'frame-focus split)
346             (setq gnus-window-frame-focus window))
347           ;; We return the window if it has the `point' spec.
348           (and (memq 'point split) window)))
349        ;; This is a frame split.
350        ((eq type 'frame)
351         (unless gnus-frame-list
352           (setq gnus-frame-list (list (window-frame current-window))))
353         (let ((i 0)
354               params frame fresult)
355           (while (< i (length subs))
356             ;; Frame parameter is gotten from the sub-split.
357             (setq params (cadr (elt subs i)))
358             ;; It should be a list.
359             (unless (listp params)
360               (setq params nil))
361             ;; Create a new frame?
362             (unless (setq frame (elt gnus-frame-list i))
363               (nconc gnus-frame-list (list (setq frame (make-frame params))))
364               (push frame gnus-created-frames))
365             ;; Is the old frame still alive?
366             (unless (frame-live-p frame)
367               (setcar (nthcdr i gnus-frame-list)
368                       (setq frame (make-frame params))))
369             ;; Select the frame in question and do more splits there.
370             (select-frame frame)
371             (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
372             (incf i))
373           ;; Select the frame that has the selected buffer.
374           (when fresult
375             (select-frame (window-frame fresult)))))
376        ;; This is a normal split.
377        (t
378         (when (> (length subs) 0)
379           ;; First we have to compute the sizes of all new windows.
380           (while subs
381             (setq sub (append (pop subs) nil))
382             (while (and (not (assq (car sub) gnus-window-to-buffer))
383                         (gnus-functionp (car sub)))
384               (setq sub (eval sub)))
385             (when sub
386               (push sub comp-subs)
387               (setq size (cadar comp-subs))
388               (cond ((equal size 1.0)
389                      (setq rest (car comp-subs))
390                      (setq s 0))
391                     ((floatp size)
392                      (setq s (floor (* size len))))
393                     ((integerp size)
394                      (setq s size))
395                     (t
396                      (error "Invalid size: %s" size)))
397               ;; Try to make sure that we are inside the safe limits.
398               (cond ((zerop s))
399                     ((eq type 'horizontal)
400                      (setq s (max s window-min-width)))
401                     ((eq type 'vertical)
402                      (setq s (max s window-min-height))))
403               (setcar (cdar comp-subs) s)
404               (incf total s)))
405           ;; Take care of the "1.0" spec.
406           (if rest
407               (setcar (cdr rest) (- len total))
408             (error "No 1.0 specs in %s" split))
409           ;; The we do the actual splitting in a nice recursive
410           ;; fashion.
411           (setq comp-subs (nreverse comp-subs))
412           (while comp-subs
413             (if (null (cdr comp-subs))
414                 (setq new-win window)
415               (setq new-win
416                     (split-window window (cadar comp-subs)
417                                   (eq type 'horizontal))))
418             (setq result (or (gnus-configure-frame
419                               (car comp-subs) window)
420                              result))
421             (select-window new-win)
422             (setq window new-win)
423             (setq comp-subs (cdr comp-subs))))
424         ;; Return the proper window, if any.
425         (when result
426           (select-window result)))))))
427
428 (defvar gnus-frame-split-p nil)
429
430 (defun gnus-configure-windows (setting &optional force)
431   (if (window-configuration-p setting)
432       (set-window-configuration setting)
433     (setq gnus-current-window-configuration setting)
434     (setq force (or force gnus-always-force-window-configuration))
435     (setq setting (gnus-windows-old-to-new setting))
436     (let ((split (if (symbolp setting)
437                      (cadr (assq setting gnus-buffer-configuration))
438                    setting))
439           all-visible)
440
441       (setq gnus-frame-split-p nil)
442
443       (unless split
444         (error "No such setting in `gnus-buffer-configuration': %s" setting))
445
446       (if (and (setq all-visible (gnus-all-windows-visible-p split))
447                (not force))
448           ;; All the windows mentioned are already visible, so we just
449           ;; put point in the assigned buffer, and do not touch the
450           ;; winconf.
451           (select-window all-visible)
452
453         ;; Make sure "the other" buffer, nntp-server-buffer, is live.
454         (unless (gnus-buffer-live-p nntp-server-buffer)
455           (nnheader-init-server-buffer))
456
457         ;; Either remove all windows or just remove all Gnus windows.
458         (let ((frame (selected-frame)))
459           (unwind-protect
460               (if gnus-use-full-window
461                   ;; We want to remove all other windows.
462                   (if (not gnus-frame-split-p)
463                       ;; This is not a `frame' split, so we ignore the
464                       ;; other frames.
465                       (delete-other-windows)
466                   ;; This is a `frame' split, so we delete all windows
467                     ;; on all frames.
468                     (gnus-delete-windows-in-gnusey-frames))
469                 ;; Just remove some windows.
470                 (gnus-remove-some-windows)
471                 (if (featurep 'xemacs)
472                     (switch-to-buffer nntp-server-buffer)
473                   (set-buffer nntp-server-buffer)))
474             (select-frame frame)))
475
476         (let (gnus-window-frame-focus)
477           (if (featurep 'xemacs)
478               (switch-to-buffer nntp-server-buffer)
479             (set-buffer nntp-server-buffer))
480           (gnus-configure-frame split)
481           (run-hooks 'gnus-configure-windows-hook)
482           (when gnus-window-frame-focus
483             (select-frame (window-frame gnus-window-frame-focus))))))))
484
485 (defun gnus-delete-windows-in-gnusey-frames ()
486   "Do a `delete-other-windows' in all frames that have Gnus windows."
487   (let ((buffers (gnus-buffers)))
488     (mapcar
489      (lambda (frame)
490        (unless (eq (cdr (assq 'minibuffer
491                               (frame-parameters frame)))
492                    'only)
493          (select-frame frame)
494          (let (do-delete)
495            (walk-windows
496             (lambda (window)
497               (when (memq (window-buffer window) buffers)
498                 (setq do-delete t))))
499            (when do-delete
500              (delete-other-windows)))))
501      (frame-list))))
502
503 (defun gnus-all-windows-visible-p (split)
504   "Say whether all buffers in SPLIT are currently visible.
505 In particular, the value returned will be the window that
506 should have point."
507   (let ((stack (list split))
508         (all-visible t)
509         type buffer win buf)
510     (while (and (setq split (pop stack))
511                 all-visible)
512       ;; Be backwards compatible.
513       (when (vectorp split)
514         (setq split (append split nil)))
515       (when (or (consp (car split))
516                 (vectorp (car split)))
517         (push 1.0 split)
518         (push 'vertical split))
519       ;; The SPLIT might be something that is to be evaled to
520       ;; return a new SPLIT.
521       (while (and (not (assq (car split) gnus-window-to-buffer))
522                   (gnus-functionp (car split)))
523         (setq split (eval split)))
524
525       (setq type (elt split 0))
526       (cond
527        ;; Nothing here.
528        ((null split) t)
529        ;; A buffer.
530        ((not (memq type '(horizontal vertical frame)))
531         (setq buffer (cond ((stringp type) type)
532                            (t (cdr (assq type gnus-window-to-buffer)))))
533         (unless buffer
534           (error "Invalid buffer type: %s" type))
535         (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
536                  (setq win (gnus-get-buffer-window buf t)))
537             (if (memq 'point split)
538                 (setq all-visible win))
539           (setq all-visible nil)))
540        (t
541         (when (eq type 'frame)
542           (setq gnus-frame-split-p t))
543         (setq stack (append (cddr split) stack)))))
544     (unless (eq all-visible t)
545       all-visible)))
546
547 (defun gnus-window-top-edge (&optional window)
548   (nth 1 (window-edges window)))
549
550 (defun gnus-remove-some-windows ()
551   (let ((buffers (gnus-buffers))
552         buf bufs lowest-buf lowest)
553     (save-excursion
554       ;; Remove windows on all known Gnus buffers.
555       (while (setq buf (pop buffers))
556         (when (get-buffer-window buf)
557           (push buf bufs)
558           (pop-to-buffer buf)
559           (when (or (not lowest)
560                     (< (gnus-window-top-edge) lowest))
561             (setq lowest (gnus-window-top-edge)
562                   lowest-buf buf))))
563       (when lowest-buf
564         (pop-to-buffer lowest-buf)
565         (if (featurep 'xemacs)
566             (switch-to-buffer nntp-server-buffer)
567           (set-buffer nntp-server-buffer)))
568       (mapcar (lambda (b) (delete-windows-on b t))
569               (delq lowest-buf bufs)))))
570
571 (eval-and-compile
572   (cond 
573    ((fboundp 'frames-on-display-list)
574     (defalias 'gnus-frames-on-display-list 'frames-on-display-list))
575    ((and (featurep 'xemacs) (fboundp 'frame-device))
576     (defun gnus-frames-on-display-list ()
577       (apply 'filtered-frame-list 'identity (list (frame-device nil)))))
578    (t
579     (defalias 'gnus-frames-on-display-list 'frame-list))))
580
581 (defun gnus-get-buffer-window (buffer &optional frame)
582   (cond ((and (null gnus-use-frames-on-any-display)
583               (memq frame '(t 0 visible)))
584          (car 
585           (let ((frames (gnus-frames-on-display-list)))
586             (gnus-delete-if (lambda (win) (not (memq (window-frame win) 
587                                                      frames)))
588                             (get-buffer-window-list buffer nil frame)))))
589         (t
590          (get-buffer-window buffer frame))))
591
592 (provide 'gnus-win)
593
594 ;;; gnus-win.el ends here