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