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