(shr-tag-li): Get <li> indentation right.
[gnus] / lisp / gnus-win.el
1 ;;; gnus-win.el --- window configuration functions for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-util)
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 (defcustom gnus-use-frames-on-any-display nil
61   "*If non-nil, frames on all displays will be considered useable by Gnus.
62 When nil, only frames on the same display as the selected frame will be
63 used to display Gnus windows."
64   :version "22.1"
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       (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     (only-article
111      (vertical 1.0
112                (article 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     (edit-server
125      (vertical 1.0
126                (server 0.5)
127                (edit-form 1.0 point)))
128     (post
129      (vertical 1.0
130                (post 1.0 point)))
131     (reply
132      (vertical 1.0
133                (article 0.5)
134                (message 1.0 point)))
135     (forward
136      (vertical 1.0
137                (message 1.0 point)))
138     (reply-yank
139      (vertical 1.0
140                (message 1.0 point)))
141     (mail-bounce
142      (vertical 1.0
143                (article 0.5)
144                (message 1.0 point)))
145     (pipe
146      (vertical 1.0
147                (summary 0.25 point)
148                (if gnus-carpal '(summary-carpal 4))
149                ("*Shell Command Output*" 1.0)))
150     (bug
151      (vertical 1.0
152                (if gnus-bug-create-help-buffer '("*Gnus Help Bug*" 0.5))
153                ("*Gnus Bug*" 1.0 point)))
154     (score-trace
155      (vertical 1.0
156                (summary 0.5 point)
157                ("*Score Trace*" 1.0)))
158     (score-words
159      (vertical 1.0
160                (summary 0.5 point)
161                ("*Score Words*" 1.0)))
162     (split-trace
163      (vertical 1.0
164                (summary 0.5 point)
165                ("*Split Trace*" 1.0)))
166     (category
167      (vertical 1.0
168                (category 1.0)))
169     (compose-bounce
170      (vertical 1.0
171                (article 0.5)
172                (message 1.0 point)))
173     (display-term
174      (vertical 1.0
175                ("*display*" 1.0)))
176     (mml-preview
177      (vertical 1.0
178                (message 0.5)
179                (mml-preview 1.0 point))))
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     (tree . gnus-tree-buffer)
202     (score-trace . "*Score Trace*")
203     (split-trace . "*Split Trace*")
204     (info . gnus-info-buffer)
205     (category . gnus-category-buffer)
206     (article-copy . gnus-article-copy)
207     (draft . gnus-draft-buffer)
208     (mml-preview . mml-preview-buffer))
209   "Mapping from short symbols to buffer names or buffer variables.")
210
211 (defcustom gnus-configure-windows-hook nil
212   "*A hook called when configuring windows."
213   :version "22.1"
214   :group 'gnus-windows
215   :type 'hook)
216
217 ;;; Internal variables.
218
219 (defvar gnus-current-window-configuration nil
220   "The most recently set window configuration.")
221
222 (defvar gnus-created-frames nil)
223 (defvar gnus-window-frame-focus nil)
224
225 (defun gnus-kill-gnus-frames ()
226   "Kill all frames Gnus has created."
227   (while gnus-created-frames
228     (when (frame-live-p (car gnus-created-frames))
229       ;; We slap a condition-case around this `delete-frame' to ensure
230       ;; against errors if we try do delete the single frame that's left.
231       (ignore-errors
232         (delete-frame (car gnus-created-frames))))
233     (pop gnus-created-frames)))
234
235 (defun gnus-window-configuration-element (list)
236   (while (and list
237               (not (assq (car list) gnus-window-configuration)))
238     (pop list))
239   (cadr (assq (car list) gnus-window-configuration)))
240
241 (defun gnus-windows-old-to-new (setting)
242   ;; First we take care of the really, really old Gnus 3 actions.
243   (when (symbolp setting)
244     (setq setting
245           ;; Take care of ooold GNUS 3.x values.
246           (cond ((eq setting 'SelectArticle) 'article)
247                 ((memq setting '(SelectNewsgroup SelectSubject ExpandSubject))
248                  'summary)
249                 ((memq setting '(ExitNewsgroup)) 'group)
250                 (t setting))))
251   (if (or (listp setting)
252           (not (and gnus-window-configuration
253                     (memq setting '(group summary article)))))
254       setting
255     (let* ((elem
256             (cond
257              ((eq setting 'group)
258               (gnus-window-configuration-element
259                '(group newsgroups ExitNewsgroup)))
260              ((eq setting 'summary)
261               (gnus-window-configuration-element
262                '(summary SelectNewsgroup SelectSubject ExpandSubject)))
263              ((eq setting 'article)
264               (gnus-window-configuration-element
265                '(article SelectArticle)))))
266            (total (apply '+ elem))
267            (types '(group summary article))
268            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
269            (i 0)
270            perc out)
271       (while (< i 3)
272         (or (not (numberp (nth i elem)))
273             (zerop (nth i elem))
274             (progn
275               (setq perc (if (= i 2)
276                              1.0
277                            (/ (float (nth i elem)) total)))
278               (push (if (eq pbuf (nth i types))
279                         (list (nth i types) perc 'point)
280                       (list (nth i types) perc))
281                     out)))
282         (incf i))
283       `(vertical 1.0 ,@(nreverse out)))))
284
285 ;;;###autoload
286 (defun gnus-add-configuration (conf)
287   "Add the window configuration CONF to `gnus-buffer-configuration'."
288   (setq gnus-buffer-configuration
289         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
290                          gnus-buffer-configuration))))
291
292 (defvar gnus-frame-list nil)
293
294 (defun gnus-window-to-buffer-helper (obj)
295   (cond ((not (symbolp obj))
296          obj)
297         ((boundp obj)
298          (symbol-value obj))
299         ((fboundp obj)
300          (funcall obj))
301         (t
302          nil)))
303
304 (defun gnus-configure-frame (split &optional window)
305   "Split WINDOW according to SPLIT."
306   (let ((current-window
307          (or (get-buffer-window (current-buffer)) (selected-window))))
308     (unless window
309       (setq window current-window))
310     (select-window window)
311     ;; This might be an old-style buffer config.
312     (when (vectorp split)
313       (setq split (append split nil)))
314     (when (or (consp (car split))
315               (vectorp (car split)))
316       (push 1.0 split)
317       (push 'vertical split))
318     ;; The SPLIT might be something that is to be evaled to
319     ;; return a new SPLIT.
320     (while (and (not (assq (car split) gnus-window-to-buffer))
321                 (symbolp (car split)) (fboundp (car split)))
322       (setq split (eval split)))
323     (let* ((type (car split))
324            (subs (cddr split))
325            (len (if (eq type 'horizontal) (window-width) (window-height)))
326            (total 0)
327            (window-min-width (or gnus-window-min-width window-min-width))
328            (window-min-height (or gnus-window-min-height window-min-height))
329            s result new-win rest comp-subs size sub)
330       (cond
331        ;; Nothing to do here.
332        ((null split))
333        ;; Don't switch buffers.
334        ((null type)
335         (and (memq 'point split) window))
336        ;; This is a buffer to be selected.
337        ((not (memq type '(frame horizontal vertical)))
338         (let ((buffer (cond ((stringp type) type)
339                             (t (cdr (assq type gnus-window-to-buffer))))))
340           (unless buffer
341             (error "Invalid buffer type: %s" type))
342           (let ((buf (gnus-get-buffer-create
343                       (gnus-window-to-buffer-helper buffer))))
344             (if (eq buf (window-buffer (selected-window))) (set-buffer buf)
345               (switch-to-buffer buf)))
346           (when (memq 'frame-focus split)
347             (setq gnus-window-frame-focus window))
348           ;; We return the window if it has the `point' spec.
349           (and (memq 'point split) window)))
350        ;; This is a frame split.
351        ((eq type 'frame)
352         (unless gnus-frame-list
353           (setq gnus-frame-list (list (window-frame current-window))))
354         (let ((i 0)
355               params frame fresult)
356           (while (< i (length subs))
357             ;; Frame parameter is gotten from the sub-split.
358             (setq params (cadr (elt subs i)))
359             ;; It should be a list.
360             (unless (listp params)
361               (setq params nil))
362             ;; Create a new frame?
363             (unless (setq frame (elt gnus-frame-list i))
364               (nconc gnus-frame-list (list (setq frame (make-frame params))))
365               (push frame gnus-created-frames))
366             ;; Is the old frame still alive?
367             (unless (frame-live-p frame)
368               (setcar (nthcdr i gnus-frame-list)
369                       (setq frame (make-frame params))))
370             ;; Select the frame in question and do more splits there.
371             (select-frame frame)
372             (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
373             (incf i))
374           ;; Select the frame that has the selected buffer.
375           (when fresult
376             (select-frame (window-frame fresult)))))
377        ;; This is a normal split.
378        (t
379         (when (> (length subs) 0)
380           ;; First we have to compute the sizes of all new windows.
381           (while subs
382             (setq sub (append (pop subs) nil))
383             (while (and (not (assq (car sub) gnus-window-to-buffer))
384                         (symbolp (car sub)) (fboundp (car sub)))
385               (setq sub (eval sub)))
386             (when sub
387               (push sub comp-subs)
388               (setq size (cadar comp-subs))
389               (cond ((equal size 1.0)
390                      (setq rest (car comp-subs))
391                      (setq s 0))
392                     ((floatp size)
393                      (setq s (floor (* size len))))
394                     ((integerp size)
395                      (setq s size))
396                     (t
397                      (error "Invalid size: %s" size)))
398               ;; Try to make sure that we are inside the safe limits.
399               (cond ((zerop s))
400                     ((eq type 'horizontal)
401                      (setq s (max s window-min-width)))
402                     ((eq type 'vertical)
403                      (setq s (max s window-min-height))))
404               (setcar (cdar comp-subs) s)
405               (incf total s)))
406           ;; Take care of the "1.0" spec.
407           (if rest
408               (setcar (cdr rest) (- len total))
409             (error "No 1.0 specs in %s" split))
410           ;; The we do the actual splitting in a nice recursive
411           ;; fashion.
412           (setq comp-subs (nreverse comp-subs))
413           (while comp-subs
414             (if (null (cdr comp-subs))
415                 (setq new-win window)
416               (setq new-win
417                     (split-window window (cadar comp-subs)
418                                   (eq type 'horizontal))))
419             (setq result (or (gnus-configure-frame
420                               (car comp-subs) window)
421                              result))
422             (select-window new-win)
423             (setq window new-win)
424             (setq comp-subs (cdr comp-subs))))
425         ;; Return the proper window, if any.
426         (when result
427           (select-window result)))))))
428
429 (defvar gnus-frame-split-p nil)
430
431 (defun gnus-configure-windows (setting &optional force)
432   (if (window-configuration-p setting)
433       (set-window-configuration setting)
434     (setq gnus-current-window-configuration setting)
435     (setq force (or force gnus-always-force-window-configuration))
436     (setq setting (gnus-windows-old-to-new setting))
437     (let ((split (if (symbolp setting)
438                      (cadr (assq setting gnus-buffer-configuration))
439                    setting))
440           all-visible)
441
442       (setq gnus-frame-split-p nil)
443
444       (unless split
445         (error "No such setting in `gnus-buffer-configuration': %s" setting))
446
447       (if (and (setq all-visible (gnus-all-windows-visible-p split))
448                (not force))
449           ;; All the windows mentioned are already visible, so we just
450           ;; put point in the assigned buffer, and do not touch the
451           ;; winconf.
452           (select-window all-visible)
453
454         ;; Make sure "the other" buffer, nntp-server-buffer, is live.
455         (unless (gnus-buffer-live-p nntp-server-buffer)
456           (nnheader-init-server-buffer))
457
458         ;; Either remove all windows or just remove all Gnus windows.
459         (let ((frame (selected-frame)))
460           (unwind-protect
461               (if gnus-use-full-window
462                   ;; We want to remove all other windows.
463                   (if (not gnus-frame-split-p)
464                       ;; This is not a `frame' split, so we ignore the
465                       ;; other frames.
466                       (delete-other-windows)
467                   ;; This is a `frame' split, so we delete all windows
468                     ;; on all frames.
469                     (gnus-delete-windows-in-gnusey-frames))
470                 ;; Just remove some windows.
471                 (gnus-remove-some-windows)
472                 (if (featurep 'xemacs)
473                     (switch-to-buffer nntp-server-buffer)
474                   (set-buffer nntp-server-buffer)))
475             (select-frame frame)))
476
477         (let (gnus-window-frame-focus)
478           (if (featurep 'xemacs)
479               (switch-to-buffer nntp-server-buffer)
480             (set-buffer nntp-server-buffer))
481           (gnus-configure-frame split)
482           (run-hooks 'gnus-configure-windows-hook)
483           (when gnus-window-frame-focus
484             (gnus-select-frame-set-input-focus
485              (window-frame gnus-window-frame-focus))))))))
486
487 (defun gnus-delete-windows-in-gnusey-frames ()
488   "Do a `delete-other-windows' in all frames that have Gnus windows."
489   (let ((buffers (gnus-buffers)))
490     (mapcar
491      (lambda (frame)
492        (unless (eq (cdr (assq 'minibuffer
493                               (frame-parameters frame)))
494                    'only)
495          (select-frame frame)
496          (let (do-delete)
497            (walk-windows
498             (lambda (window)
499               (when (memq (window-buffer window) buffers)
500                 (setq do-delete t))))
501            (when do-delete
502              (delete-other-windows)))))
503      (frame-list))))
504
505 (defun gnus-all-windows-visible-p (split)
506   "Say whether all buffers in SPLIT are currently visible.
507 In particular, the value returned will be the window that
508 should have point."
509   (let ((stack (list split))
510         (all-visible t)
511         type buffer win buf)
512     (while (and (setq split (pop stack))
513                 all-visible)
514       ;; Be backwards compatible.
515       (when (vectorp split)
516         (setq split (append split nil)))
517       (when (or (consp (car split))
518                 (vectorp (car split)))
519         (push 1.0 split)
520         (push 'vertical split))
521       ;; The SPLIT might be something that is to be evaled to
522       ;; return a new SPLIT.
523       (while (and (not (assq (car split) gnus-window-to-buffer))
524                   (symbolp (car split)) (fboundp (car split)))
525         (setq split (eval split)))
526
527       (setq type (elt split 0))
528       (cond
529        ;; Nothing here.
530        ((null split) t)
531        ;; A buffer.
532        ((not (memq type '(horizontal vertical frame)))
533         (setq buffer (cond ((stringp type) type)
534                            (t (cdr (assq type gnus-window-to-buffer)))))
535         (unless buffer
536           (error "Invalid buffer type: %s" type))
537         (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
538                  (setq win (gnus-get-buffer-window buf t)))
539             (if (memq 'point split)
540                 (setq all-visible win))
541           (setq all-visible nil)))
542        (t
543         (when (eq type 'frame)
544           (setq gnus-frame-split-p t))
545         (setq stack (append (cddr split) stack)))))
546     (unless (eq all-visible t)
547       all-visible)))
548
549 (defun gnus-window-top-edge (&optional window)
550   (nth 1 (window-edges window)))
551
552 (defun gnus-remove-some-windows ()
553   (let ((buffers (gnus-buffers))
554         buf bufs lowest-buf lowest)
555     (save-excursion
556       ;; Remove windows on all known Gnus buffers.
557       (while (setq buf (pop buffers))
558         (when (get-buffer-window buf)
559           (push buf bufs)
560           (pop-to-buffer buf)
561           (when (or (not lowest)
562                     (< (gnus-window-top-edge) lowest))
563             (setq lowest (gnus-window-top-edge)
564                   lowest-buf buf))))
565       (when lowest-buf
566         (pop-to-buffer lowest-buf)
567         (if (featurep 'xemacs)
568             (switch-to-buffer nntp-server-buffer)
569           (set-buffer nntp-server-buffer)))
570       (mapcar (lambda (b) (delete-windows-on b t))
571               (delq lowest-buf bufs)))))
572
573 (eval-and-compile
574   (cond
575    ((fboundp 'frames-on-display-list)
576     (defalias 'gnus-frames-on-display-list 'frames-on-display-list))
577    ((and (featurep 'xemacs) (fboundp 'frame-device))
578     (defun gnus-frames-on-display-list ()
579       (apply 'filtered-frame-list 'identity (list (frame-device nil)))))
580    (t
581     (defalias 'gnus-frames-on-display-list 'frame-list))))
582
583 (defun gnus-get-buffer-window (buffer &optional frame)
584   (cond ((and (null gnus-use-frames-on-any-display)
585               (memq frame '(t 0 visible)))
586          (car
587           (let ((frames (gnus-frames-on-display-list)))
588             (gnus-remove-if (lambda (win) (not (memq (window-frame win)
589                                                      frames)))
590                             (get-buffer-window-list buffer nil frame)))))
591         (t
592          (get-buffer-window buffer frame))))
593
594 (provide 'gnus-win)
595
596 ;;; gnus-win.el ends here