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