Make `gnus-msg-mail' warn about Gnus not running
[gnus] / lisp / gnus-win.el
1 ;;; gnus-win.el --- window configuration functions for Gnus
2
3 ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
29 (require 'gnus)
30 (require 'gnus-util)
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 (defcustom gnus-window-min-width 2
42   "*Minimum width of Gnus buffers."
43   :group 'gnus-windows
44   :type 'integer)
45
46 (defcustom gnus-window-min-height 1
47   "*Minimum height of Gnus buffers."
48   :group 'gnus-windows
49   :type 'integer)
50
51 (defcustom gnus-always-force-window-configuration nil
52   "*If non-nil, always force the Gnus window configurations."
53   :group 'gnus-windows
54   :type 'boolean)
55
56 (defcustom gnus-use-frames-on-any-display nil
57   "*If non-nil, frames on all displays will be considered usable by Gnus.
58 When nil, only frames on the same display as the selected frame will be
59 used to display Gnus windows."
60   :version "22.1"
61   :group 'gnus-windows
62   :type 'boolean)
63
64 (defvar gnus-buffer-configuration
65   '((group
66      (vertical 1.0
67                (group 1.0 point)))
68     (summary
69      (vertical 1.0
70                (summary 1.0 point)))
71     (article
72      (cond
73       (gnus-use-trees
74        '(vertical 1.0
75                   (summary 0.25 point)
76                   (tree 0.25)
77                   (article 1.0)))
78       (t
79        '(vertical 1.0
80                   (summary 0.25 point)
81                   (article 1.0)))))
82     (server
83      (vertical 1.0
84                (server 1.0 point)))
85     (browse
86      (vertical 1.0
87                (browse 1.0 point)))
88     (message
89      (vertical 1.0
90                (message 1.0 point)))
91     (pick
92      (vertical 1.0
93                (article 1.0 point)))
94     (info
95      (vertical 1.0
96                (info 1.0 point)))
97     (summary-faq
98      (vertical 1.0
99                (summary 0.25)
100                (faq 1.0 point)))
101     (only-article
102      (vertical 1.0
103                (article 1.0 point)))
104     (edit-article
105      (vertical 1.0
106                (article 1.0 point)))
107     (edit-form
108      (vertical 1.0
109                (group 0.5)
110                (edit-form 1.0 point)))
111     (edit-score
112      (vertical 1.0
113                (summary 0.25)
114                (edit-score 1.0 point)))
115     (edit-server
116      (vertical 1.0
117                (server 0.5)
118                (edit-form 1.0 point)))
119     (post
120      (vertical 1.0
121                (post 1.0 point)))
122     (reply
123      (vertical 1.0
124                (article 0.5)
125                (message 1.0 point)))
126     (forward
127      (vertical 1.0
128                (message 1.0 point)))
129     (reply-yank
130      (vertical 1.0
131                (message 1.0 point)))
132     (mail-bounce
133      (vertical 1.0
134                (article 0.5)
135                (message 1.0 point)))
136     (pipe
137      (vertical 1.0
138                (summary 0.25 point)
139                ("*Shell Command Output*" 1.0)))
140     (bug
141      (vertical 1.0
142                (if gnus-bug-create-help-buffer '("*Gnus Help Bug*" 0.5))
143                ("*Gnus Bug*" 1.0 point)))
144     (score-trace
145      (vertical 1.0
146                (summary 0.5 point)
147                ("*Score Trace*" 1.0)))
148     (score-words
149      (vertical 1.0
150                (summary 0.5 point)
151                ("*Score Words*" 1.0)))
152     (split-trace
153      (vertical 1.0
154                (summary 0.5 point)
155                ("*Split Trace*" 1.0)))
156     (category
157      (vertical 1.0
158                (category 1.0)))
159     (compose-bounce
160      (vertical 1.0
161                (article 0.5)
162                (message 1.0 point)))
163     (display-term
164      (vertical 1.0
165                ("*display*" 1.0)))
166     (mml-preview
167      (vertical 1.0
168                (message 0.5)
169                (mml-preview 1.0 point))))
170   "Window configuration for all possible Gnus buffers.
171 See the Gnus manual for an explanation of the syntax used.")
172
173 (defvar gnus-window-to-buffer
174   '((group . gnus-group-buffer)
175     (summary . gnus-summary-buffer)
176     (article . gnus-article-buffer)
177     (server . gnus-server-buffer)
178     (browse . "*Gnus Browse Server*")
179     (edit-group . gnus-group-edit-buffer)
180     (edit-form . gnus-edit-form-buffer)
181     (edit-server . gnus-server-edit-buffer)
182     (edit-score . gnus-score-edit-buffer)
183     (message . gnus-message-buffer)
184     (mail . gnus-message-buffer)
185     (post-news . gnus-message-buffer)
186     (faq . gnus-faq-buffer)
187     (tree . gnus-tree-buffer)
188     (score-trace . "*Score Trace*")
189     (split-trace . "*Split Trace*")
190     (info . gnus-info-buffer)
191     (category . gnus-category-buffer)
192     (article-copy . gnus-article-copy)
193     (draft . gnus-draft-buffer)
194     (mml-preview . mml-preview-buffer))
195   "Mapping from short symbols to buffer names or buffer variables.")
196
197 (defcustom gnus-configure-windows-hook nil
198   "*A hook called when configuring windows."
199   :version "22.1"
200   :group 'gnus-windows
201   :type 'hook)
202
203 ;;; Internal variables.
204
205 (defvar gnus-current-window-configuration nil
206   "The most recently set window configuration.")
207
208 (defvar gnus-created-frames nil)
209 (defvar gnus-window-frame-focus nil)
210
211 (defun gnus-kill-gnus-frames ()
212   "Kill all frames Gnus has created."
213   (while gnus-created-frames
214     (when (frame-live-p (car gnus-created-frames))
215       ;; We slap a condition-case around this `delete-frame' to ensure
216       ;; against errors if we try do delete the single frame that's left.
217       (ignore-errors
218         (delete-frame (car gnus-created-frames))))
219     (pop gnus-created-frames)))
220
221 ;;;###autoload
222 (defun gnus-add-configuration (conf)
223   "Add the window configuration CONF to `gnus-buffer-configuration'."
224   (setq gnus-buffer-configuration
225         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
226                          gnus-buffer-configuration))))
227
228 (defvar gnus-frame-list nil)
229
230 (defun gnus-window-to-buffer-helper (obj)
231   (cond ((not (symbolp obj))
232          obj)
233         ((boundp obj)
234          (symbol-value obj))
235         ((fboundp obj)
236          (funcall obj))
237         (t
238          nil)))
239
240 (defun gnus-configure-frame (split &optional window)
241   "Split WINDOW according to SPLIT."
242   (let* ((current-window (or (get-buffer-window (current-buffer))
243                              (selected-window)))
244          (window (or window current-window)))
245     (select-window window)
246     ;; The SPLIT might be something that is to be evalled to
247     ;; return a new SPLIT.
248     (while (and (not (assq (car split) gnus-window-to-buffer))
249                 (symbolp (car split)) (fboundp (car split)))
250       (setq split (eval split)))
251     (let* ((type (car split))
252            (subs (cddr split))
253            (len (if (eq type 'horizontal) (window-width) (window-height)))
254            (total 0)
255            (window-min-width (or gnus-window-min-width window-min-width))
256            (window-min-height (or gnus-window-min-height window-min-height))
257            s result new-win rest comp-subs size sub)
258       (cond
259        ;; Nothing to do here.
260        ((null split))
261        ;; Don't switch buffers.
262        ((null type)
263         (and (memq 'point split) window))
264        ;; This is a buffer to be selected.
265        ((not (memq type '(frame horizontal vertical)))
266         (let ((buffer (cond ((stringp type) type)
267                             (t (cdr (assq type gnus-window-to-buffer))))))
268           (unless buffer
269             (error "Invalid buffer type: %s" type))
270           (let ((buf (gnus-get-buffer-create
271                       (gnus-window-to-buffer-helper buffer))))
272             (when (buffer-name buf)
273               (cond
274                ((eq buf (window-buffer (selected-window)))
275                 (set-buffer buf))
276                ((eq t (window-dedicated-p))
277                 ;; If the window is hard-dedicated, we have a problem because
278                 ;; we just can't do what we're asked.  But signaling an error,
279                 ;; like `switch-to-buffer' would do, is not an option because
280                 ;; it would prevent things like "^" (to jump to the *Servers*)
281                 ;; in a dedicated *Group*.
282                 ;; FIXME: Maybe a better/additional fix would be to change
283                 ;; gnus-configure-windows so that when called
284                 ;; from a hard-dedicated frame, it creates (and
285                 ;; configures) a new frame, leaving the dedicated frame alone.
286                 (pop-to-buffer buf))
287                (t (switch-to-buffer buf)))))
288           (when (memq 'frame-focus split)
289             (setq gnus-window-frame-focus window))
290           ;; We return the window if it has the `point' spec.
291           (and (memq 'point split) window)))
292        ;; This is a frame split.
293        ((eq type 'frame)
294         (unless gnus-frame-list
295           (setq gnus-frame-list (list (window-frame current-window))))
296         (let ((i 0)
297               params frame fresult)
298           (while (< i (length subs))
299             ;; Frame parameter is gotten from the sub-split.
300             (setq params (cadr (elt subs i)))
301             ;; It should be a list.
302             (unless (listp params)
303               (setq params nil))
304             ;; Create a new frame?
305             (unless (setq frame (elt gnus-frame-list i))
306               (nconc gnus-frame-list (list (setq frame (make-frame params))))
307               (push frame gnus-created-frames))
308             ;; Is the old frame still alive?
309             (unless (frame-live-p frame)
310               (setcar (nthcdr i gnus-frame-list)
311                       (setq frame (make-frame params))))
312             ;; Select the frame in question and do more splits there.
313             (select-frame frame)
314             (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
315             (incf i))
316           ;; Select the frame that has the selected buffer.
317           (when fresult
318             (select-frame (window-frame fresult)))))
319        ;; This is a normal split.
320        (t
321         (when (> (length subs) 0)
322           ;; First we have to compute the sizes of all new windows.
323           (while subs
324             (setq sub (append (pop subs) nil))
325             (while (and (not (assq (car sub) gnus-window-to-buffer))
326                         (symbolp (car sub)) (fboundp (car sub)))
327               (setq sub (eval sub)))
328             (when sub
329               (push sub comp-subs)
330               (setq size (cadar comp-subs))
331               (cond ((equal size 1.0)
332                      (setq rest (car comp-subs))
333                      (setq s 0))
334                     ((floatp size)
335                      (setq s (floor (* size len))))
336                     ((integerp size)
337                      (setq s size))
338                     (t
339                      (error "Invalid size: %s" size)))
340               ;; Try to make sure that we are inside the safe limits.
341               (cond ((zerop s))
342                     ((eq type 'horizontal)
343                      (setq s (max s window-min-width)))
344                     ((eq type 'vertical)
345                      (setq s (max s window-min-height))))
346               (setcar (cdar comp-subs) s)
347               (incf total s)))
348           ;; Take care of the "1.0" spec.
349           (if rest
350               (setcar (cdr rest) (- len total))
351             (error "No 1.0 specs in %s" split))
352           ;; The we do the actual splitting in a nice recursive
353           ;; fashion.
354           (setq comp-subs (nreverse comp-subs))
355           (while comp-subs
356             (setq new-win
357                   (if (null (cdr comp-subs))
358                       window
359                     (split-window window (cadar comp-subs)
360                                   (eq type 'horizontal))))
361             (setq result (or (gnus-configure-frame
362                               (car comp-subs) window)
363                              result))
364             (select-window new-win)
365             (setq window new-win)
366             (setq comp-subs (cdr comp-subs))))
367         ;; Return the proper window, if any.
368         (when result
369           (select-window result)))))))
370
371 (defvar gnus-frame-split-p nil)
372
373 (defun gnus-configure-windows (setting &optional force)
374   (cond
375    ((null setting)
376     ;; Do nothing.
377     )
378    ((window-configuration-p setting)
379     (set-window-configuration setting))
380    (t
381     (setq gnus-current-window-configuration setting)
382     (setq force (or force gnus-always-force-window-configuration))
383     (let ((split (if (symbolp setting)
384                      (cadr (assq setting gnus-buffer-configuration))
385                    setting))
386           all-visible)
387
388       (setq gnus-frame-split-p nil)
389
390       (unless split
391         (error "No such setting in `gnus-buffer-configuration': %s" setting))
392
393       (if (and (setq all-visible (gnus-all-windows-visible-p split))
394                (not force))
395           ;; All the windows mentioned are already visible, so we just
396           ;; put point in the assigned buffer, and do not touch the
397           ;; winconf.
398           (select-window all-visible)
399
400         ;; Make sure "the other" buffer, nntp-server-buffer, is live.
401         (unless (gnus-buffer-live-p nntp-server-buffer)
402           (nnheader-init-server-buffer))
403
404         ;; Either remove all windows or just remove all Gnus windows.
405         (let ((frame (selected-frame)))
406           (unwind-protect
407               (if gnus-use-full-window
408                   ;; We want to remove all other windows.
409                   (if (not gnus-frame-split-p)
410                       ;; This is not a `frame' split, so we ignore the
411                       ;; other frames.
412                       (delete-other-windows)
413                     ;; This is a `frame' split, so we delete all windows
414                     ;; on all frames.
415                     (gnus-delete-windows-in-gnusey-frames))
416                 ;; Just remove some windows.
417                 (gnus-remove-some-windows)
418                 (if (featurep 'xemacs)
419                     (switch-to-buffer nntp-server-buffer)
420                   (set-buffer nntp-server-buffer)))
421             (select-frame frame)))
422
423         (let (gnus-window-frame-focus)
424           (if (featurep 'xemacs)
425               (switch-to-buffer nntp-server-buffer)
426             (set-buffer nntp-server-buffer))
427           (gnus-configure-frame split)
428           (run-hooks 'gnus-configure-windows-hook)
429           (when gnus-window-frame-focus
430             (gnus-select-frame-set-input-focus
431              (window-frame gnus-window-frame-focus)))))))))
432
433 (defun gnus-delete-windows-in-gnusey-frames ()
434   "Do a `delete-other-windows' in all frames that have Gnus windows."
435   (let ((buffers (gnus-buffers)))
436     (mapcar
437      (lambda (frame)
438        (unless (eq (cdr (assq 'minibuffer
439                               (frame-parameters frame)))
440                    'only)
441          (select-frame frame)
442          (let (do-delete)
443            (walk-windows
444             (lambda (window)
445               (when (memq (window-buffer window) buffers)
446                 (setq do-delete t))))
447            (when do-delete
448              (delete-other-windows)))))
449      (frame-list))))
450
451 (defun gnus-all-windows-visible-p (split)
452   "Say whether all buffers in SPLIT are currently visible.
453 In particular, the value returned will be the window that
454 should have point."
455   (let ((stack (list split))
456         (all-visible t)
457         type buffer win buf)
458     (while (and (setq split (pop stack))
459                 all-visible)
460       (when (consp (car split))
461         (push 1.0 split)
462         (push 'vertical split))
463       ;; The SPLIT might be something that is to be evalled to
464       ;; return a new SPLIT.
465       (while (and (not (assq (car split) gnus-window-to-buffer))
466                   (symbolp (car split)) (fboundp (car split)))
467         (setq split (eval split)))
468
469       (setq type (elt split 0))
470       (cond
471        ;; Nothing here.
472        ((null split) t)
473        ;; A buffer.
474        ((not (memq type '(horizontal vertical frame)))
475         (setq buffer (cond ((stringp type) type)
476                            (t (cdr (assq type gnus-window-to-buffer)))))
477         (unless buffer
478           (error "Invalid buffer type: %s" type))
479         (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
480                  (buffer-live-p buf)
481                  (setq win (gnus-get-buffer-window buf t)))
482             (if (memq 'point split)
483                 (setq all-visible win))
484           (setq all-visible nil)))
485        (t
486         (when (eq type 'frame)
487           (setq gnus-frame-split-p t))
488         (setq stack (append (cddr split) stack)))))
489     (unless (eq all-visible t)
490       all-visible)))
491
492 (defun gnus-window-top-edge (&optional window)
493   "Return the top coordinate of WINDOW."
494   (nth 1 (window-edges window)))
495
496 (defun gnus-remove-some-windows ()
497   (let ((buffers (gnus-buffers))
498         buf bufs lowest-buf lowest)
499     (save-excursion
500       ;; Remove windows on all known Gnus buffers.
501       (while (setq buf (pop buffers))
502         (when (get-buffer-window buf)
503           (push buf bufs)
504           (pop-to-buffer buf)
505           (when (or (not lowest)
506                     (< (gnus-window-top-edge) lowest))
507             (setq lowest (gnus-window-top-edge)
508                   lowest-buf buf))))
509       (when lowest-buf
510         (pop-to-buffer lowest-buf)
511         (if (featurep 'xemacs)
512             (switch-to-buffer nntp-server-buffer)
513           (set-buffer nntp-server-buffer)))
514       (mapcar (lambda (b) (delete-windows-on b t))
515               (delq lowest-buf bufs)))))
516
517 (eval-and-compile
518   (cond
519    ((fboundp 'frames-on-display-list)
520     (defalias 'gnus-frames-on-display-list 'frames-on-display-list))
521    ((and (featurep 'xemacs) (fboundp 'frame-device))
522     (defun gnus-frames-on-display-list ()
523       (apply 'filtered-frame-list 'identity (list (frame-device nil)))))
524    (t
525     (defalias 'gnus-frames-on-display-list 'frame-list))))
526
527 (defun gnus-get-buffer-window (buffer &optional frame)
528   (cond ((and (null gnus-use-frames-on-any-display)
529               (memq frame '(t 0 visible)))
530          (car
531           (let ((frames (gnus-frames-on-display-list)))
532             (gnus-remove-if (lambda (win) (not (memq (window-frame win)
533                                                      frames)))
534                             (get-buffer-window-list buffer nil frame)))))
535         (t
536          (get-buffer-window buffer frame))))
537
538 (provide 'gnus-win)
539
540 ;;; gnus-win.el ends here