(mml2015-epg-sign): Save the signing keys in
[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 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 2, or (at your option)
14 ;; 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; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-util)
34
35 (defgroup gnus-windows nil
36   "Window configuration."
37   :group 'gnus)
38
39 (defcustom gnus-use-full-window t
40   "*If non-nil, use the entire Emacs screen."
41   :group 'gnus-windows
42   :type 'boolean)
43
44 (defvar gnus-window-configuration nil
45   "Obsolete variable.  See `gnus-buffer-configuration'.")
46
47 (defcustom gnus-window-min-width 2
48   "*Minimum width of Gnus buffers."
49   :group 'gnus-windows
50   :type 'integer)
51
52 (defcustom gnus-window-min-height 1
53   "*Minimum height of Gnus buffers."
54   :group 'gnus-windows
55   :type 'integer)
56
57 (defcustom gnus-always-force-window-configuration nil
58   "*If non-nil, always force the Gnus window configurations."
59   :group 'gnus-windows
60   :type 'boolean)
61
62 (defcustom gnus-use-frames-on-any-display nil
63   "*If non-nil, frames on all displays will be considered useable by Gnus.
64 When nil, only frames on the same display as the selected frame will be
65 used to display Gnus windows."
66   :version "22.1"
67   :group 'gnus-windows
68   :type 'boolean)
69
70 (defvar gnus-buffer-configuration
71   '((group
72      (vertical 1.0
73                (group 1.0 point)
74                (if gnus-carpal '(group-carpal 4))))
75     (summary
76      (vertical 1.0
77                (summary 1.0 point)
78                (if gnus-carpal '(summary-carpal 4))))
79     (article
80      (cond
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 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                (if gnus-bug-create-help-buffer '("*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     (split-trace
158      (vertical 1.0
159                (summary 0.5 point)
160                ("*Split Trace*" 1.0)))
161     (category
162      (vertical 1.0
163                (category 1.0)))
164     (compose-bounce
165      (vertical 1.0
166                (article 0.5)
167                (message 1.0 point)))
168     (display-term
169      (vertical 1.0
170                ("*display*" 1.0)))
171     (mml-preview
172      (vertical 1.0
173                (message 0.5)
174                (mml-preview 1.0 point))))
175   "Window configuration for all possible Gnus buffers.
176 See the Gnus manual for an explanation of the syntax used.")
177
178 (defvar gnus-window-to-buffer
179   '((group . gnus-group-buffer)
180     (summary . gnus-summary-buffer)
181     (article . gnus-article-buffer)
182     (server . gnus-server-buffer)
183     (browse . "*Gnus Browse Server*")
184     (edit-group . gnus-group-edit-buffer)
185     (edit-form . gnus-edit-form-buffer)
186     (edit-server . gnus-server-edit-buffer)
187     (group-carpal . gnus-carpal-group-buffer)
188     (summary-carpal . gnus-carpal-summary-buffer)
189     (server-carpal . gnus-carpal-server-buffer)
190     (browse-carpal . gnus-carpal-browse-buffer)
191     (edit-score . gnus-score-edit-buffer)
192     (message . gnus-message-buffer)
193     (mail . gnus-message-buffer)
194     (post-news . gnus-message-buffer)
195     (faq . gnus-faq-buffer)
196     (tree . gnus-tree-buffer)
197     (score-trace . "*Score Trace*")
198     (split-trace . "*Split Trace*")
199     (info . gnus-info-buffer)
200     (category . gnus-category-buffer)
201     (article-copy . gnus-article-copy)
202     (draft . gnus-draft-buffer)
203     (mml-preview . mml-preview-buffer))
204   "Mapping from short symbols to buffer names or buffer variables.")
205
206 (defcustom gnus-configure-windows-hook nil
207   "*A hook called when configuring windows."
208   :version "22.1"
209   :group 'gnus-windows
210   :type 'hook)
211
212 ;;; Internal variables.
213
214 (defvar gnus-current-window-configuration nil
215   "The most recently set window configuration.")
216
217 (defvar gnus-created-frames nil)
218 (defvar gnus-window-frame-focus nil)
219
220 (defun gnus-kill-gnus-frames ()
221   "Kill all frames Gnus has created."
222   (while gnus-created-frames
223     (when (frame-live-p (car gnus-created-frames))
224       ;; We slap a condition-case around this `delete-frame' to ensure
225       ;; against errors if we try do delete the single frame that's left.
226       (ignore-errors
227         (delete-frame (car gnus-created-frames))))
228     (pop gnus-created-frames)))
229
230 (defun gnus-window-configuration-element (list)
231   (while (and list
232               (not (assq (car list) gnus-window-configuration)))
233     (pop list))
234   (cadr (assq (car list) gnus-window-configuration)))
235
236 (defun gnus-windows-old-to-new (setting)
237   ;; First we take care of the really, really old Gnus 3 actions.
238   (when (symbolp setting)
239     (setq setting
240           ;; Take care of ooold GNUS 3.x values.
241           (cond ((eq setting 'SelectArticle) 'article)
242                 ((memq setting '(SelectNewsgroup SelectSubject ExpandSubject))
243                  'summary)
244                 ((memq setting '(ExitNewsgroup)) 'group)
245                 (t setting))))
246   (if (or (listp setting)
247           (not (and gnus-window-configuration
248                     (memq setting '(group summary article)))))
249       setting
250     (let* ((elem
251             (cond
252              ((eq setting 'group)
253               (gnus-window-configuration-element
254                '(group newsgroups ExitNewsgroup)))
255              ((eq setting 'summary)
256               (gnus-window-configuration-element
257                '(summary SelectNewsgroup SelectSubject ExpandSubject)))
258              ((eq setting 'article)
259               (gnus-window-configuration-element
260                '(article SelectArticle)))))
261            (total (apply '+ elem))
262            (types '(group summary article))
263            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
264            (i 0)
265            perc out)
266       (while (< i 3)
267         (or (not (numberp (nth i elem)))
268             (zerop (nth i elem))
269             (progn
270               (setq perc (if (= i 2)
271                              1.0
272                            (/ (float (nth i elem)) total)))
273               (push (if (eq pbuf (nth i types))
274                         (list (nth i types) perc 'point)
275                       (list (nth i types) perc))
276                     out)))
277         (incf i))
278       `(vertical 1.0 ,@(nreverse out)))))
279
280 ;;;###autoload
281 (defun gnus-add-configuration (conf)
282   "Add the window configuration CONF to `gnus-buffer-configuration'."
283   (setq gnus-buffer-configuration
284         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
285                          gnus-buffer-configuration))))
286
287 (defvar gnus-frame-list nil)
288
289 (defun gnus-window-to-buffer-helper (obj)
290   (cond ((not (symbolp obj))
291          obj)
292         ((boundp obj)
293          (symbol-value obj))
294         ((fboundp obj)
295          (funcall obj))
296         (t
297          nil)))
298
299 (defun gnus-configure-frame (split &optional window)
300   "Split WINDOW according to SPLIT."
301   (let ((current-window
302          (or (get-buffer-window (current-buffer)) (selected-window))))
303     (unless window
304       (setq window current-window))
305     (select-window window)
306     ;; This might be an old-style buffer config.
307     (when (vectorp split)
308       (setq split (append split nil)))
309     (when (or (consp (car split))
310               (vectorp (car split)))
311       (push 1.0 split)
312       (push 'vertical split))
313     ;; The SPLIT might be something that is to be evaled to
314     ;; return a new SPLIT.
315     (while (and (not (assq (car split) gnus-window-to-buffer))
316                 (functionp (car split)))
317       (setq split (eval split)))
318     (let* ((type (car split))
319            (subs (cddr split))
320            (len (if (eq type 'horizontal) (window-width) (window-height)))
321            (total 0)
322            (window-min-width (or gnus-window-min-width window-min-width))
323            (window-min-height (or gnus-window-min-height window-min-height))
324            s result new-win rest comp-subs size sub)
325       (cond
326        ;; Nothing to do here.
327        ((null split))
328        ;; Don't switch buffers.
329        ((null type)
330         (and (memq 'point split) window))
331        ;; This is a buffer to be selected.
332        ((not (memq type '(frame horizontal vertical)))
333         (let ((buffer (cond ((stringp type) type)
334                             (t (cdr (assq type gnus-window-to-buffer))))))
335           (unless buffer
336             (error "Invalid buffer type: %s" type))
337           (let ((buf (gnus-get-buffer-create
338                       (gnus-window-to-buffer-helper buffer))))
339             (if (eq buf (window-buffer (selected-window))) (set-buffer buf)
340               (switch-to-buffer buf)))
341           (when (memq 'frame-focus split)
342             (setq gnus-window-frame-focus window))
343           ;; We return the window if it has the `point' spec.
344           (and (memq 'point split) window)))
345        ;; This is a frame split.
346        ((eq type 'frame)
347         (unless gnus-frame-list
348           (setq gnus-frame-list (list (window-frame current-window))))
349         (let ((i 0)
350               params frame fresult)
351           (while (< i (length subs))
352             ;; Frame parameter is gotten from the sub-split.
353             (setq params (cadr (elt subs i)))
354             ;; It should be a list.
355             (unless (listp params)
356               (setq params nil))
357             ;; Create a new frame?
358             (unless (setq frame (elt gnus-frame-list i))
359               (nconc gnus-frame-list (list (setq frame (make-frame params))))
360               (push frame gnus-created-frames))
361             ;; Is the old frame still alive?
362             (unless (frame-live-p frame)
363               (setcar (nthcdr i gnus-frame-list)
364                       (setq frame (make-frame params))))
365             ;; Select the frame in question and do more splits there.
366             (select-frame frame)
367             (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
368             (incf i))
369           ;; Select the frame that has the selected buffer.
370           (when fresult
371             (select-frame (window-frame fresult)))))
372        ;; This is a normal split.
373        (t
374         (when (> (length subs) 0)
375           ;; First we have to compute the sizes of all new windows.
376           (while subs
377             (setq sub (append (pop subs) nil))
378             (while (and (not (assq (car sub) gnus-window-to-buffer))
379                         (functionp (car sub)))
380               (setq sub (eval sub)))
381             (when sub
382               (push sub comp-subs)
383               (setq size (cadar comp-subs))
384               (cond ((equal size 1.0)
385                      (setq rest (car comp-subs))
386                      (setq s 0))
387                     ((floatp size)
388                      (setq s (floor (* size len))))
389                     ((integerp size)
390                      (setq s size))
391                     (t
392                      (error "Invalid size: %s" size)))
393               ;; Try to make sure that we are inside the safe limits.
394               (cond ((zerop s))
395                     ((eq type 'horizontal)
396                      (setq s (max s window-min-width)))
397                     ((eq type 'vertical)
398                      (setq s (max s window-min-height))))
399               (setcar (cdar comp-subs) s)
400               (incf total s)))
401           ;; Take care of the "1.0" spec.
402           (if rest
403               (setcar (cdr rest) (- len total))
404             (error "No 1.0 specs in %s" split))
405           ;; The we do the actual splitting in a nice recursive
406           ;; fashion.
407           (setq comp-subs (nreverse comp-subs))
408           (while comp-subs
409             (if (null (cdr comp-subs))
410                 (setq new-win window)
411               (setq new-win
412                     (split-window window (cadar comp-subs)
413                                   (eq type 'horizontal))))
414             (setq result (or (gnus-configure-frame
415                               (car comp-subs) window)
416                              result))
417             (select-window new-win)
418             (setq window new-win)
419             (setq comp-subs (cdr comp-subs))))
420         ;; Return the proper window, if any.
421         (when result
422           (select-window result)))))))
423
424 (defvar gnus-frame-split-p nil)
425
426 (defun gnus-configure-windows (setting &optional force)
427   (if (window-configuration-p setting)
428       (set-window-configuration setting)
429     (setq gnus-current-window-configuration setting)
430     (setq force (or force gnus-always-force-window-configuration))
431     (setq setting (gnus-windows-old-to-new setting))
432     (let ((split (if (symbolp setting)
433                      (cadr (assq setting gnus-buffer-configuration))
434                    setting))
435           all-visible)
436
437       (setq gnus-frame-split-p nil)
438
439       (unless split
440         (error "No such setting in `gnus-buffer-configuration': %s" setting))
441
442       (if (and (setq all-visible (gnus-all-windows-visible-p split))
443                (not force))
444           ;; All the windows mentioned are already visible, so we just
445           ;; put point in the assigned buffer, and do not touch the
446           ;; winconf.
447           (select-window all-visible)
448
449         ;; Make sure "the other" buffer, nntp-server-buffer, is live.
450         (unless (gnus-buffer-live-p nntp-server-buffer)
451           (nnheader-init-server-buffer))
452
453         ;; Either remove all windows or just remove all Gnus windows.
454         (let ((frame (selected-frame)))
455           (unwind-protect
456               (if gnus-use-full-window
457                   ;; We want to remove all other windows.
458                   (if (not gnus-frame-split-p)
459                       ;; This is not a `frame' split, so we ignore the
460                       ;; other frames.
461                       (delete-other-windows)
462                   ;; This is a `frame' split, so we delete all windows
463                     ;; on all frames.
464                     (gnus-delete-windows-in-gnusey-frames))
465                 ;; Just remove some windows.
466                 (gnus-remove-some-windows)
467                 (if (featurep 'xemacs)
468                     (switch-to-buffer nntp-server-buffer)
469                   (set-buffer nntp-server-buffer)))
470             (select-frame frame)))
471
472         (let (gnus-window-frame-focus)
473           (if (featurep 'xemacs)
474               (switch-to-buffer nntp-server-buffer)
475             (set-buffer nntp-server-buffer))
476           (gnus-configure-frame split)
477           (run-hooks 'gnus-configure-windows-hook)
478           (when gnus-window-frame-focus
479             (select-frame (window-frame gnus-window-frame-focus))))))))
480
481 (defun gnus-delete-windows-in-gnusey-frames ()
482   "Do a `delete-other-windows' in all frames that have Gnus windows."
483   (let ((buffers (gnus-buffers)))
484     (mapcar
485      (lambda (frame)
486        (unless (eq (cdr (assq 'minibuffer
487                               (frame-parameters frame)))
488                    'only)
489          (select-frame frame)
490          (let (do-delete)
491            (walk-windows
492             (lambda (window)
493               (when (memq (window-buffer window) buffers)
494                 (setq do-delete t))))
495            (when do-delete
496              (delete-other-windows)))))
497      (frame-list))))
498
499 (defun gnus-all-windows-visible-p (split)
500   "Say whether all buffers in SPLIT are currently visible.
501 In particular, the value returned will be the window that
502 should have point."
503   (let ((stack (list split))
504         (all-visible t)
505         type buffer win buf)
506     (while (and (setq split (pop stack))
507                 all-visible)
508       ;; Be backwards compatible.
509       (when (vectorp split)
510         (setq split (append split nil)))
511       (when (or (consp (car split))
512                 (vectorp (car split)))
513         (push 1.0 split)
514         (push 'vertical split))
515       ;; The SPLIT might be something that is to be evaled to
516       ;; return a new SPLIT.
517       (while (and (not (assq (car split) gnus-window-to-buffer))
518                   (functionp (car split)))
519         (setq split (eval split)))
520
521       (setq type (elt split 0))
522       (cond
523        ;; Nothing here.
524        ((null split) t)
525        ;; A buffer.
526        ((not (memq type '(horizontal vertical frame)))
527         (setq buffer (cond ((stringp type) type)
528                            (t (cdr (assq type gnus-window-to-buffer)))))
529         (unless buffer
530           (error "Invalid buffer type: %s" type))
531         (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
532                  (setq win (gnus-get-buffer-window buf t)))
533             (if (memq 'point split)
534                 (setq all-visible win))
535           (setq all-visible nil)))
536        (t
537         (when (eq type 'frame)
538           (setq gnus-frame-split-p t))
539         (setq stack (append (cddr split) stack)))))
540     (unless (eq all-visible t)
541       all-visible)))
542
543 (defun gnus-window-top-edge (&optional window)
544   (nth 1 (window-edges window)))
545
546 (defun gnus-remove-some-windows ()
547   (let ((buffers (gnus-buffers))
548         buf bufs lowest-buf lowest)
549     (save-excursion
550       ;; Remove windows on all known Gnus buffers.
551       (while (setq buf (pop buffers))
552         (when (get-buffer-window buf)
553           (push buf bufs)
554           (pop-to-buffer buf)
555           (when (or (not lowest)
556                     (< (gnus-window-top-edge) lowest))
557             (setq lowest (gnus-window-top-edge)
558                   lowest-buf buf))))
559       (when lowest-buf
560         (pop-to-buffer lowest-buf)
561         (if (featurep 'xemacs)
562             (switch-to-buffer nntp-server-buffer)
563           (set-buffer nntp-server-buffer)))
564       (mapcar (lambda (b) (delete-windows-on b t))
565               (delq lowest-buf bufs)))))
566
567 (eval-and-compile
568   (cond
569    ((fboundp 'frames-on-display-list)
570     (defalias 'gnus-frames-on-display-list 'frames-on-display-list))
571    ((and (featurep 'xemacs) (fboundp 'frame-device))
572     (defun gnus-frames-on-display-list ()
573       (apply 'filtered-frame-list 'identity (list (frame-device nil)))))
574    (t
575     (defalias 'gnus-frames-on-display-list 'frame-list))))
576
577 (defun gnus-get-buffer-window (buffer &optional frame)
578   (cond ((and (null gnus-use-frames-on-any-display)
579               (memq frame '(t 0 visible)))
580          (car
581           (let ((frames (gnus-frames-on-display-list)))
582             (gnus-remove-if (lambda (win) (not (memq (window-frame win)
583                                                      frames)))
584                             (get-buffer-window-list buffer nil frame)))))
585         (t
586          (get-buffer-window buffer frame))))
587
588 (provide 'gnus-win)
589
590 ;;; arch-tag: ccd5a394-2ddf-4397-b8f8-6d80d3e46e2b
591 ;;; gnus-win.el ends here