2001-12-19 01:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[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 ;;; Internal variables.
212
213 (defvar gnus-current-window-configuration nil
214   "The most recently set window configuration.")
215
216 (defvar gnus-created-frames nil)
217 (defvar gnus-window-frame-focus nil)
218
219 (defun gnus-kill-gnus-frames ()
220   "Kill all frames Gnus has created."
221   (while gnus-created-frames
222     (when (frame-live-p (car gnus-created-frames))
223       ;; We slap a condition-case around this `delete-frame' to ensure
224       ;; against errors if we try do delete the single frame that's left.
225       (ignore-errors
226         (delete-frame (car gnus-created-frames))))
227     (pop gnus-created-frames)))
228
229 (defun gnus-window-configuration-element (list)
230   (while (and list
231               (not (assq (car list) gnus-window-configuration)))
232     (pop list))
233   (cadr (assq (car list) gnus-window-configuration)))
234
235 (defun gnus-windows-old-to-new (setting)
236   ;; First we take care of the really, really old Gnus 3 actions.
237   (when (symbolp setting)
238     (setq setting
239           ;; Take care of ooold GNUS 3.x values.
240           (cond ((eq setting 'SelectArticle) 'article)
241                 ((memq setting '(SelectNewsgroup SelectSubject ExpandSubject))
242                  'summary)
243                 ((memq setting '(ExitNewsgroup)) 'group)
244                 (t setting))))
245   (if (or (listp setting)
246           (not (and gnus-window-configuration
247                     (memq setting '(group summary article)))))
248       setting
249     (let* ((elem
250             (cond
251              ((eq setting 'group)
252               (gnus-window-configuration-element
253                '(group newsgroups ExitNewsgroup)))
254              ((eq setting 'summary)
255               (gnus-window-configuration-element
256                '(summary SelectNewsgroup SelectSubject ExpandSubject)))
257              ((eq setting 'article)
258               (gnus-window-configuration-element
259                '(article SelectArticle)))))
260            (total (apply '+ elem))
261            (types '(group summary article))
262            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
263            (i 0)
264            perc out)
265       (while (< i 3)
266         (or (not (numberp (nth i elem)))
267             (zerop (nth i elem))
268             (progn
269               (setq perc (if (= i 2)
270                              1.0
271                            (/ (float (nth i elem)) total)))
272               (push (if (eq pbuf (nth i types))
273                         (list (nth i types) perc 'point)
274                       (list (nth i types) perc))
275                     out)))
276         (incf i))
277       `(vertical 1.0 ,@(nreverse out)))))
278
279 ;;;###autoload
280 (defun gnus-add-configuration (conf)
281   "Add the window configuration CONF to `gnus-buffer-configuration'."
282   (setq gnus-buffer-configuration
283         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
284                          gnus-buffer-configuration))))
285
286 (defvar gnus-frame-list nil)
287
288 (defun gnus-window-to-buffer-helper (obj)
289   (cond ((not (symbolp obj))
290          obj)
291         ((boundp obj)
292          (symbol-value obj))
293         ((fboundp obj)
294          (funcall obj))
295         (t
296          nil)))
297
298 (defun gnus-configure-frame (split &optional window)
299   "Split WINDOW according to SPLIT."
300   (let ((current-window
301          (or (get-buffer-window (current-buffer)) (selected-window))))
302     (unless window
303       (setq window current-window))
304     (select-window window)
305     ;; This might be an old-stylee buffer config.
306     (when (vectorp split)
307       (setq split (append split nil)))
308     (when (or (consp (car split))
309               (vectorp (car split)))
310       (push 1.0 split)
311       (push 'vertical split))
312     ;; The SPLIT might be something that is to be evaled to
313     ;; return a new SPLIT.
314     (while (and (not (assq (car split) gnus-window-to-buffer))
315                 (gnus-functionp (car split)))
316       (setq split (eval split)))
317     (let* ((type (car split))
318            (subs (cddr split))
319            (len (if (eq type 'horizontal) (window-width) (window-height)))
320            (total 0)
321            (window-min-width (or gnus-window-min-width window-min-width))
322            (window-min-height (or gnus-window-min-height window-min-height))
323            s result new-win rest comp-subs size sub)
324       (cond
325        ;; Nothing to do here.
326        ((null split))
327        ;; Don't switch buffers.
328        ((null type)
329         (and (memq 'point split) window))
330        ;; This is a buffer to be selected.
331        ((not (memq type '(frame horizontal vertical)))
332         (let ((buffer (cond ((stringp type) type)
333                             (t (cdr (assq type gnus-window-to-buffer))))))
334           (unless buffer
335             (error "Invalid buffer type: %s" type))
336           (let ((buf (gnus-get-buffer-create
337                       (gnus-window-to-buffer-helper buffer))))
338             (if (eq buf (window-buffer (selected-window))) (set-buffer buf)
339               (switch-to-buffer buf)))
340           (when (memq 'frame-focus split)
341             (setq gnus-window-frame-focus window))
342           ;; We return the window if it has the `point' spec.
343           (and (memq 'point split) window)))
344        ;; This is a frame split.
345        ((eq type 'frame)
346         (unless gnus-frame-list
347           (setq gnus-frame-list (list (window-frame current-window))))
348         (let ((i 0)
349               params frame fresult)
350           (while (< i (length subs))
351             ;; Frame parameter is gotten from the sub-split.
352             (setq params (cadr (elt subs i)))
353             ;; It should be a list.
354             (unless (listp params)
355               (setq params nil))
356             ;; Create a new frame?
357             (unless (setq frame (elt gnus-frame-list i))
358               (nconc gnus-frame-list (list (setq frame (make-frame params))))
359               (push frame gnus-created-frames))
360             ;; Is the old frame still alive?
361             (unless (frame-live-p frame)
362               (setcar (nthcdr i gnus-frame-list)
363                       (setq frame (make-frame params))))
364             ;; Select the frame in question and do more splits there.
365             (select-frame frame)
366             (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
367             (incf i))
368           ;; Select the frame that has the selected buffer.
369           (when fresult
370             (select-frame (window-frame fresult)))))
371        ;; This is a normal split.
372        (t
373         (when (> (length subs) 0)
374           ;; First we have to compute the sizes of all new windows.
375           (while subs
376             (setq sub (append (pop subs) nil))
377             (while (and (not (assq (car sub) gnus-window-to-buffer))
378                         (gnus-functionp (car sub)))
379               (setq sub (eval sub)))
380             (when sub
381               (push sub comp-subs)
382               (setq size (cadar comp-subs))
383               (cond ((equal size 1.0)
384                      (setq rest (car comp-subs))
385                      (setq s 0))
386                     ((floatp size)
387                      (setq s (floor (* size len))))
388                     ((integerp size)
389                      (setq s size))
390                     (t
391                      (error "Invalid size: %s" size)))
392               ;; Try to make sure that we are inside the safe limits.
393               (cond ((zerop s))
394                     ((eq type 'horizontal)
395                      (setq s (max s window-min-width)))
396                     ((eq type 'vertical)
397                      (setq s (max s window-min-height))))
398               (setcar (cdar comp-subs) s)
399               (incf total s)))
400           ;; Take care of the "1.0" spec.
401           (if rest
402               (setcar (cdr rest) (- len total))
403             (error "No 1.0 specs in %s" split))
404           ;; The we do the actual splitting in a nice recursive
405           ;; fashion.
406           (setq comp-subs (nreverse comp-subs))
407           (while comp-subs
408             (if (null (cdr comp-subs))
409                 (setq new-win window)
410               (setq new-win
411                     (split-window window (cadar comp-subs)
412                                   (eq type 'horizontal))))
413             (setq result (or (gnus-configure-frame
414                               (car comp-subs) window)
415                              result))
416             (select-window new-win)
417             (setq window new-win)
418             (setq comp-subs (cdr comp-subs))))
419         ;; Return the proper window, if any.
420         (when result
421           (select-window result)))))))
422
423 (defvar gnus-frame-split-p nil)
424
425 (defun gnus-configure-windows (setting &optional force)
426   (if (window-configuration-p setting)
427       (set-window-configuration setting)
428     (setq gnus-current-window-configuration setting)
429     (setq force (or force gnus-always-force-window-configuration))
430     (setq setting (gnus-windows-old-to-new setting))
431     (let ((split (if (symbolp setting)
432                      (cadr (assq setting gnus-buffer-configuration))
433                    setting))
434           all-visible)
435
436       (setq gnus-frame-split-p nil)
437
438       (unless split
439         (error "No such setting in `gnus-buffer-configuration': %s" setting))
440
441       (if (and (setq all-visible (gnus-all-windows-visible-p split))
442                (not force))
443           ;; All the windows mentioned are already visible, so we just
444           ;; put point in the assigned buffer, and do not touch the
445           ;; winconf.
446           (select-window all-visible)
447
448         ;; Make sure "the other" buffer, nntp-server-buffer, is live.
449         (unless (gnus-buffer-live-p nntp-server-buffer)
450           (nnheader-init-server-buffer))
451
452         ;; Either remove all windows or just remove all Gnus windows.
453         (let ((frame (selected-frame)))
454           (unwind-protect
455               (if gnus-use-full-window
456                   ;; We want to remove all other windows.
457                   (if (not gnus-frame-split-p)
458                       ;; This is not a `frame' split, so we ignore the
459                       ;; other frames.
460                       (delete-other-windows)
461                     ;; This is a `frame' split, so we delete all windows
462                     ;; on all frames.
463                     (gnus-delete-windows-in-gnusey-frames))
464                 ;; Just remove some windows.
465                 (gnus-remove-some-windows)
466                 (if (featurep 'xemacs)
467                     (switch-to-buffer nntp-server-buffer)
468                   (set-buffer nntp-server-buffer)))
469             (select-frame frame)))
470
471         (let (gnus-window-frame-focus)
472           (if (featurep 'xemacs)
473               (switch-to-buffer nntp-server-buffer)
474             (set-buffer nntp-server-buffer))
475           (gnus-configure-frame split)
476           (when gnus-window-frame-focus
477             (select-frame (window-frame gnus-window-frame-focus))))))))
478
479 (defun gnus-delete-windows-in-gnusey-frames ()
480   "Do a `delete-other-windows' in all frames that have Gnus windows."
481   (let ((buffers (gnus-buffers)))
482     (mapcar
483      (lambda (frame)
484        (unless (eq (cdr (assq 'minibuffer
485                               (frame-parameters frame)))
486                    'only)
487          (select-frame frame)
488          (let (do-delete)
489            (walk-windows
490             (lambda (window)
491               (when (memq (window-buffer window) buffers)
492                 (setq do-delete t))))
493            (when do-delete
494              (delete-other-windows)))))
495      (frame-list))))
496
497 (defun gnus-all-windows-visible-p (split)
498   "Say whether all buffers in SPLIT are currently visible.
499 In particular, the value returned will be the window that
500 should have point."
501   (let ((stack (list split))
502         (all-visible t)
503         type buffer win buf)
504     (while (and (setq split (pop stack))
505                 all-visible)
506       ;; Be backwards compatible.
507       (when (vectorp split)
508         (setq split (append split nil)))
509       (when (or (consp (car split))
510                 (vectorp (car split)))
511         (push 1.0 split)
512         (push 'vertical split))
513       ;; The SPLIT might be something that is to be evaled to
514       ;; return a new SPLIT.
515       (while (and (not (assq (car split) gnus-window-to-buffer))
516                   (gnus-functionp (car split)))
517         (setq split (eval split)))
518
519       (setq type (elt split 0))
520       (cond
521        ;; Nothing here.
522        ((null split) t)
523        ;; A buffer.
524        ((not (memq type '(horizontal vertical frame)))
525         (setq buffer (cond ((stringp type) type)
526                            (t (cdr (assq type gnus-window-to-buffer)))))
527         (unless buffer
528           (error "Invalid buffer type: %s" type))
529         (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
530                  (setq win (gnus-get-buffer-window buf t)))
531             (if (memq 'point split)
532                 (setq all-visible win))
533           (setq all-visible nil)))
534        (t
535         (when (eq type 'frame)
536           (setq gnus-frame-split-p t))
537         (setq stack (append (cddr split) stack)))))
538     (unless (eq all-visible t)
539       all-visible)))
540
541 (defun gnus-window-top-edge (&optional window)
542   (nth 1 (window-edges window)))
543
544 (defun gnus-remove-some-windows ()
545   (let ((buffers (gnus-buffers))
546         buf bufs lowest-buf lowest)
547     (save-excursion
548       ;; Remove windows on all known Gnus buffers.
549       (while (setq buf (pop buffers))
550         (when (get-buffer-window buf)
551           (push buf bufs)
552           (pop-to-buffer buf)
553           (when (or (not lowest)
554                     (< (gnus-window-top-edge) lowest))
555             (setq lowest (gnus-window-top-edge)
556                   lowest-buf buf))))
557       (when lowest-buf
558         (pop-to-buffer lowest-buf)
559         (if (featurep 'xemacs)
560             (switch-to-buffer nntp-server-buffer)
561           (set-buffer nntp-server-buffer)))
562       (mapcar (lambda (b) (delete-windows-on b t))
563               (delq lowest-buf bufs)))))
564
565 (eval-and-compile
566   (cond 
567    ((fboundp 'frames-on-display-list)
568     (defalias 'gnus-frames-on-display-list 'frames-on-display-list))
569    ((and (featurep 'xemacs) (fboundp 'frame-device))
570     (defun gnus-frames-on-display-list ()
571       (apply 'filtered-frame-list 'identity (list (frame-device nil)))))
572    (t
573     (defalias 'gnus-frames-on-display-list 'frame-list))))
574
575 (defun gnus-get-buffer-window (buffer &optional frame)
576   (cond ((and (null gnus-use-frames-on-any-display)
577               (memq frame '(t 0 visible)))
578          (car 
579           (let ((frames (gnus-frames-on-display-list)))
580             (gnus-delete-if (lambda (win) (not (memq (window-frame win) 
581                                                      frames)))
582                             (get-buffer-window-list buffer nil frame)))))
583         (t
584          (get-buffer-window buffer frame))))
585
586 (provide 'gnus-win)
587
588 ;;; gnus-win.el ends here