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