Merge from emacs-23
[bpt/emacs.git] / lisp / frame.el
1 ;;; frame.el --- multi-frame management independent of window systems
2
3 ;; Copyright (C) 1993, 1994, 1996, 1997, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;;; Code:
28 (eval-when-compile (require 'cl))
29
30 (defvar frame-creation-function-alist
31 (list (cons nil
32 (if (fboundp 'tty-create-frame-with-faces)
33 'tty-create-frame-with-faces
34 (lambda (parameters)
35 (error "Can't create multiple frames without a window system")))))
36 "Alist of window-system dependent functions to call to create a new frame.
37 The window system startup file should add its frame creation
38 function to this list, which should take an alist of parameters
39 as its argument.")
40
41 (defvar window-system-default-frame-alist nil
42 "Alist of window-system dependent default frame parameters.
43 Parameters specified here supersede the values given in
44 `default-frame-alist'.")
45
46 ;; The initial value given here used to ask for a minibuffer.
47 ;; But that's not necessary, because the default is to have one.
48 ;; By not specifying it here, we let an X resource specify it.
49 (defcustom initial-frame-alist nil
50 "Alist of parameters for the initial X window frame.
51 You can set this in your init file; for example,
52
53 (setq initial-frame-alist
54 '((top . 1) (left . 1) (width . 80) (height . 55)))
55
56 Parameters specified here supersede the values given in
57 `default-frame-alist'.
58
59 If the value calls for a frame without a minibuffer, and you have
60 not created a minibuffer frame on your own, a minibuffer frame is
61 created according to `minibuffer-frame-alist'.
62
63 You can specify geometry-related options for just the initial
64 frame by setting this variable in your init file; however, they
65 won't take effect until Emacs reads your init file, which happens
66 after creating the initial frame. If you want the initial frame
67 to have the proper geometry as soon as it appears, you need to
68 use this three-step process:
69 * Specify X resources to give the geometry you want.
70 * Set `default-frame-alist' to override these options so that they
71 don't affect subsequent frames.
72 * Set `initial-frame-alist' in a way that matches the X resources,
73 to override what you put in `default-frame-alist'."
74 :type '(repeat (cons :format "%v"
75 (symbol :tag "Parameter")
76 (sexp :tag "Value")))
77 :group 'frames)
78
79 (defcustom minibuffer-frame-alist '((width . 80) (height . 2))
80 "Alist of parameters for the initial minibuffer frame.
81 This is the minibuffer frame created if `initial-frame-alist'
82 calls for a frame without a minibuffer. The parameters specified
83 here supersede those given in `default-frame-alist', for the
84 initial minibuffer frame.
85
86 You can set this in your init file; for example,
87
88 (setq minibuffer-frame-alist
89 '((top . 1) (left . 1) (width . 80) (height . 2)))
90
91 It is not necessary to include (minibuffer . only); that is
92 appended when the minibuffer frame is created."
93 :type '(repeat (cons :format "%v"
94 (symbol :tag "Parameter")
95 (sexp :tag "Value")))
96 :group 'frames)
97
98 (defcustom pop-up-frame-alist nil
99 "Alist of parameters for automatically generated new frames.
100 You can set this in your init file; for example,
101
102 (setq pop-up-frame-alist '((width . 80) (height . 20)))
103
104 If non-nil, the value you specify here is used by the default
105 `pop-up-frame-function' for the creation of new frames.
106
107 Since `pop-up-frame-function' is used by `display-buffer' for
108 making new frames, any value specified here by default affects
109 the automatic generation of new frames via `display-buffer' and
110 all functions based on it. The behavior of `make-frame' is not
111 affected by this variable."
112 :type '(repeat (cons :format "%v"
113 (symbol :tag "Parameter")
114 (sexp :tag "Value")))
115 :group 'frames)
116
117 (defcustom pop-up-frame-function
118 (lambda () (make-frame pop-up-frame-alist))
119 "Function used by `display-buffer' for creating a new frame.
120 This function is called with no arguments and should return a new
121 frame. The default value calls `make-frame' with the argument
122 `pop-up-frame-alist'."
123 :type 'function
124 :group 'frames)
125
126 (defcustom special-display-frame-alist
127 '((height . 14) (width . 80) (unsplittable . t))
128 "Alist of parameters for special frames.
129 Special frames are used for buffers whose names are listed in
130 `special-display-buffer-names' and for buffers whose names match
131 one of the regular expressions in `special-display-regexps'.
132
133 This variable can be set in your init file, like this:
134
135 (setq special-display-frame-alist '((width . 80) (height . 20)))
136
137 These supersede the values given in `default-frame-alist'."
138 :type '(repeat (cons :format "%v"
139 (symbol :tag "Parameter")
140 (sexp :tag "Value")))
141 :group 'frames)
142
143 (defun special-display-popup-frame (buffer &optional args)
144 "Display BUFFER and return the window chosen.
145 If BUFFER is already displayed in a visible or iconified frame,
146 raise that frame. Otherwise, display BUFFER in a new frame.
147
148 Optional argument ARGS is a list specifying additional
149 information.
150
151 If ARGS is an alist, use it as a list of frame parameters. If
152 these parameters contain \(same-window . t), display BUFFER in
153 the selected window. If they contain \(same-frame . t), display
154 BUFFER in a window of the selected frame.
155
156 If ARGS is a list whose car is a symbol, use (car ARGS) as a
157 function to do the work. Pass it BUFFER as first argument,
158 and (cdr ARGS) as second."
159 (if (and args (symbolp (car args)))
160 (apply (car args) buffer (cdr args))
161 (let ((window (get-buffer-window buffer 0)))
162 (or
163 ;; If we have a window already, make it visible.
164 (when window
165 (let ((frame (window-frame window)))
166 (make-frame-visible frame)
167 (raise-frame frame)
168 window))
169 ;; Reuse the current window if the user requested it.
170 (when (cdr (assq 'same-window args))
171 (condition-case nil
172 (progn (switch-to-buffer buffer) (selected-window))
173 (error nil)))
174 ;; Stay on the same frame if requested.
175 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
176 (let* ((pop-up-windows t)
177 pop-up-frames
178 special-display-buffer-names special-display-regexps)
179 (display-buffer buffer)))
180 ;; If no window yet, make one in a new frame.
181 (let ((frame
182 (with-current-buffer buffer
183 (make-frame (append args special-display-frame-alist)))))
184 (set-window-buffer (frame-selected-window frame) buffer)
185 (set-window-dedicated-p (frame-selected-window frame) t)
186 (frame-selected-window frame))))))
187
188 (defun handle-delete-frame (event)
189 "Handle delete-frame events from the X server."
190 (interactive "e")
191 (let ((frame (posn-window (event-start event)))
192 (i 0)
193 (tail (frame-list)))
194 (while tail
195 (and (frame-visible-p (car tail))
196 (not (eq (car tail) frame))
197 (setq i (1+ i)))
198 (setq tail (cdr tail)))
199 (if (> i 0)
200 (delete-frame frame t)
201 ;; Gildea@x.org says it is ok to ask questions before terminating.
202 (save-buffers-kill-emacs))))
203 \f
204 ;;;; Arrangement of frames at startup
205
206 ;; 1) Load the window system startup file from the lisp library and read the
207 ;; high-priority arguments (-q and the like). The window system startup
208 ;; file should create any frames specified in the window system defaults.
209 ;;
210 ;; 2) If no frames have been opened, we open an initial text frame.
211 ;;
212 ;; 3) Once the init file is done, we apply any newly set parameters
213 ;; in initial-frame-alist to the frame.
214
215 ;; These are now called explicitly at the proper times,
216 ;; since that is easier to understand.
217 ;; Actually using hooks within Emacs is bad for future maintenance. --rms.
218 ;; (add-hook 'before-init-hook 'frame-initialize)
219 ;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
220
221 ;; If we create the initial frame, this is it.
222 (defvar frame-initial-frame nil)
223
224 ;; Record the parameters used in frame-initialize to make the initial frame.
225 (defvar frame-initial-frame-alist)
226
227 (defvar frame-initial-geometry-arguments nil)
228
229 ;; startup.el calls this function before loading the user's init
230 ;; file - if there is no frame with a minibuffer open now, create
231 ;; one to display messages while loading the init file.
232 (defun frame-initialize ()
233 "Create an initial frame if necessary."
234 ;; Are we actually running under a window system at all?
235 (if (and initial-window-system
236 (not noninteractive)
237 (not (eq initial-window-system 'pc)))
238 (progn
239 ;; If there is no frame with a minibuffer besides the terminal
240 ;; frame, then we need to create the opening frame. Make sure
241 ;; it has a minibuffer, but let initial-frame-alist omit the
242 ;; minibuffer spec.
243 (or (delq terminal-frame (minibuffer-frame-list))
244 (progn
245 (setq frame-initial-frame-alist
246 (append initial-frame-alist default-frame-alist nil))
247 (or (assq 'horizontal-scroll-bars frame-initial-frame-alist)
248 (setq frame-initial-frame-alist
249 (cons '(horizontal-scroll-bars . t)
250 frame-initial-frame-alist)))
251 (setq frame-initial-frame-alist
252 (cons (cons 'window-system initial-window-system)
253 frame-initial-frame-alist))
254 (setq default-minibuffer-frame
255 (setq frame-initial-frame
256 (make-frame frame-initial-frame-alist)))
257 ;; Delete any specifications for window geometry parameters
258 ;; so that we won't reapply them in frame-notice-user-settings.
259 ;; It would be wrong to reapply them then,
260 ;; because that would override explicit user resizing.
261 (setq initial-frame-alist
262 (frame-remove-geometry-params initial-frame-alist))))
263 ;; Copy the environment of the Emacs process into the new frame.
264 (set-frame-parameter frame-initial-frame 'environment
265 (frame-parameter terminal-frame 'environment))
266 ;; At this point, we know that we have a frame open, so we
267 ;; can delete the terminal frame.
268 (delete-frame terminal-frame)
269 (setq terminal-frame nil))))
270
271 (defvar frame-notice-user-settings t
272 "Non-nil means function `frame-notice-user-settings' wasn't run yet.")
273
274 (declare-function tool-bar-mode "tool-bar" (&optional arg))
275
276 ;; startup.el calls this function after loading the user's init
277 ;; file. Now default-frame-alist and initial-frame-alist contain
278 ;; information to which we must react; do what needs to be done.
279 (defun frame-notice-user-settings ()
280 "Act on user's init file settings of frame parameters.
281 React to settings of `initial-frame-alist',
282 `window-system-default-frame-alist' and `default-frame-alist'
283 there (in decreasing order of priority)."
284 ;; Creating and deleting frames may shift the selected frame around,
285 ;; and thus the current buffer. Protect against that. We don't
286 ;; want to use save-excursion here, because that may also try to set
287 ;; the buffer of the selected window, which fails when the selected
288 ;; window is the minibuffer.
289 (let ((old-buffer (current-buffer))
290 (window-system-frame-alist
291 (cdr (assq initial-window-system
292 window-system-default-frame-alist))))
293
294 (when (and frame-notice-user-settings
295 (null frame-initial-frame))
296 ;; This case happens when we don't have a window system, and
297 ;; also for MS-DOS frames.
298 (let ((parms (frame-parameters)))
299 ;; Don't change the frame names.
300 (setq parms (delq (assq 'name parms) parms))
301 ;; Can't modify the minibuffer parameter, so don't try.
302 (setq parms (delq (assq 'minibuffer parms) parms))
303 (modify-frame-parameters
304 nil
305 (if initial-window-system
306 parms
307 ;; initial-frame-alist and default-frame-alist were already
308 ;; applied in pc-win.el.
309 (append initial-frame-alist window-system-frame-alist
310 default-frame-alist parms nil)))
311 (if (null initial-window-system) ;; MS-DOS does this differently in pc-win.el
312 (let ((newparms (frame-parameters))
313 (frame (selected-frame)))
314 (tty-handle-reverse-video frame newparms)
315 ;; If we changed the background color, we need to update
316 ;; the background-mode parameter, and maybe some faces,
317 ;; too.
318 (when (assq 'background-color newparms)
319 (unless (or (assq 'background-mode initial-frame-alist)
320 (assq 'background-mode default-frame-alist))
321 (frame-set-background-mode frame))
322 (face-set-after-frame-default frame))))))
323
324 ;; If the initial frame is still around, apply initial-frame-alist
325 ;; and default-frame-alist to it.
326 (when (frame-live-p frame-initial-frame)
327
328 ;; When tool-bar has been switched off, correct the frame size
329 ;; by the lines added in x-create-frame for the tool-bar and
330 ;; switch `tool-bar-mode' off.
331 (when (display-graphic-p)
332 (let ((tool-bar-lines (or (assq 'tool-bar-lines initial-frame-alist)
333 (assq 'tool-bar-lines window-system-frame-alist)
334 (assq 'tool-bar-lines default-frame-alist))))
335 (when (and tool-bar-originally-present
336 (or (null tool-bar-lines)
337 (null (cdr tool-bar-lines))
338 (eq 0 (cdr tool-bar-lines))))
339 (let* ((char-height (frame-char-height frame-initial-frame))
340 (image-height tool-bar-images-pixel-height)
341 (margin (cond ((and (consp tool-bar-button-margin)
342 (integerp (cdr tool-bar-button-margin))
343 (> tool-bar-button-margin 0))
344 (cdr tool-bar-button-margin))
345 ((and (integerp tool-bar-button-margin)
346 (> tool-bar-button-margin 0))
347 tool-bar-button-margin)
348 (t 0)))
349 (relief (if (and (integerp tool-bar-button-relief)
350 (> tool-bar-button-relief 0))
351 tool-bar-button-relief 3))
352 (lines (/ (+ image-height
353 (* 2 margin)
354 (* 2 relief)
355 (1- char-height))
356 char-height))
357 (height (frame-parameter frame-initial-frame 'height))
358 (newparms (list (cons 'height (- height lines))))
359 (initial-top (cdr (assq 'top
360 frame-initial-geometry-arguments)))
361 (top (frame-parameter frame-initial-frame 'top)))
362 (when (and (consp initial-top) (eq '- (car initial-top)))
363 (let ((adjusted-top
364 (cond ((and (consp top)
365 (eq '+ (car top)))
366 (list '+
367 (+ (cadr top)
368 (* lines char-height))))
369 ((and (consp top)
370 (eq '- (car top)))
371 (list '-
372 (- (cadr top)
373 (* lines char-height))))
374 (t (+ top (* lines char-height))))))
375 (setq newparms
376 (append newparms
377 `((top . ,adjusted-top))
378 nil))))
379 (modify-frame-parameters frame-initial-frame newparms)
380 (tool-bar-mode -1)))))
381
382 ;; The initial frame we create above always has a minibuffer.
383 ;; If the user wants to remove it, or make it a minibuffer-only
384 ;; frame, then we'll have to delete the current frame and make a
385 ;; new one; you can't remove or add a root window to/from an
386 ;; existing frame.
387 ;;
388 ;; NOTE: default-frame-alist was nil when we created the
389 ;; existing frame. We need to explicitly include
390 ;; default-frame-alist in the parameters of the screen we
391 ;; create here, so that its new value, gleaned from the user's
392 ;; .emacs file, will be applied to the existing screen.
393 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
394 (assq 'minibuffer window-system-frame-alist)
395 (assq 'minibuffer default-frame-alist)
396 '(minibuffer . t)))
397 t))
398 ;; Create the new frame.
399 (let (parms new)
400 ;; If the frame isn't visible yet, wait till it is.
401 ;; If the user has to position the window,
402 ;; Emacs doesn't know its real position until
403 ;; the frame is seen to be visible.
404 (while (not (cdr (assq 'visibility
405 (frame-parameters frame-initial-frame))))
406 (sleep-for 1))
407 (setq parms (frame-parameters frame-initial-frame))
408
409 ;; Get rid of `name' unless it was specified explicitly before.
410 (or (assq 'name frame-initial-frame-alist)
411 (setq parms (delq (assq 'name parms) parms)))
412 ;; An explicit parent-id is a request to XEmbed the frame.
413 (or (assq 'parent-id frame-initial-frame-alist)
414 (setq parms (delq (assq 'parent-id parms) parms)))
415
416 (setq parms (append initial-frame-alist
417 window-system-frame-alist
418 default-frame-alist
419 parms
420 nil))
421
422 ;; Get rid of `reverse', because that was handled
423 ;; when we first made the frame.
424 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
425
426 (if (assq 'height frame-initial-geometry-arguments)
427 (setq parms (assq-delete-all 'height parms)))
428 (if (assq 'width frame-initial-geometry-arguments)
429 (setq parms (assq-delete-all 'width parms)))
430 (if (assq 'left frame-initial-geometry-arguments)
431 (setq parms (assq-delete-all 'left parms)))
432 (if (assq 'top frame-initial-geometry-arguments)
433 (setq parms (assq-delete-all 'top parms)))
434 (setq new
435 (make-frame
436 ;; Use the geometry args that created the existing
437 ;; frame, rather than the parms we get for it.
438 (append frame-initial-geometry-arguments
439 '((user-size . t) (user-position . t))
440 parms)))
441 ;; The initial frame, which we are about to delete, may be
442 ;; the only frame with a minibuffer. If it is, create a
443 ;; new one.
444 (or (delq frame-initial-frame (minibuffer-frame-list))
445 (make-initial-minibuffer-frame nil))
446
447 ;; If the initial frame is serving as a surrogate
448 ;; minibuffer frame for any frames, we need to wean them
449 ;; onto a new frame. The default-minibuffer-frame
450 ;; variable must be handled similarly.
451 (let ((users-of-initial
452 (filtered-frame-list
453 (lambda (frame)
454 (and (not (eq frame frame-initial-frame))
455 (eq (window-frame
456 (minibuffer-window frame))
457 frame-initial-frame))))))
458 (if (or users-of-initial
459 (eq default-minibuffer-frame frame-initial-frame))
460
461 ;; Choose an appropriate frame. Prefer frames which
462 ;; are only minibuffers.
463 (let* ((new-surrogate
464 (car
465 (or (filtered-frame-list
466 (lambda (frame)
467 (eq (cdr (assq 'minibuffer
468 (frame-parameters frame)))
469 'only)))
470 (minibuffer-frame-list))))
471 (new-minibuffer (minibuffer-window new-surrogate)))
472
473 (if (eq default-minibuffer-frame frame-initial-frame)
474 (setq default-minibuffer-frame new-surrogate))
475
476 ;; Wean the frames using frame-initial-frame as
477 ;; their minibuffer frame.
478 (dolist (frame users-of-initial)
479 (modify-frame-parameters
480 frame (list (cons 'minibuffer new-minibuffer)))))))
481
482 ;; Redirect events enqueued at this frame to the new frame.
483 ;; Is this a good idea?
484 (redirect-frame-focus frame-initial-frame new)
485
486 ;; Finally, get rid of the old frame.
487 (delete-frame frame-initial-frame t))
488
489 ;; Otherwise, we don't need all that rigamarole; just apply
490 ;; the new parameters.
491 (let (newparms allparms tail)
492 (setq allparms (append initial-frame-alist
493 window-system-frame-alist
494 default-frame-alist nil))
495 (if (assq 'height frame-initial-geometry-arguments)
496 (setq allparms (assq-delete-all 'height allparms)))
497 (if (assq 'width frame-initial-geometry-arguments)
498 (setq allparms (assq-delete-all 'width allparms)))
499 (if (assq 'left frame-initial-geometry-arguments)
500 (setq allparms (assq-delete-all 'left allparms)))
501 (if (assq 'top frame-initial-geometry-arguments)
502 (setq allparms (assq-delete-all 'top allparms)))
503 (setq tail allparms)
504 ;; Find just the parms that have changed since we first
505 ;; made this frame. Those are the ones actually set by
506 ;; the init file. For those parms whose values we already knew
507 ;; (such as those spec'd by command line options)
508 ;; it is undesirable to specify the parm again
509 ;; once the user has seen the frame and been able to alter it
510 ;; manually.
511 (let (newval oldval)
512 (dolist (entry tail)
513 (setq oldval (assq (car entry) frame-initial-frame-alist))
514 (setq newval (cdr (assq (car entry) allparms)))
515 (or (and oldval (eq (cdr oldval) newval))
516 (setq newparms
517 (cons (cons (car entry) newval) newparms)))))
518 (setq newparms (nreverse newparms))
519
520 (let ((new-bg (assq 'background-color newparms)))
521 ;; If the `background-color' parameter is changed, apply
522 ;; it first, then make sure that the `background-mode'
523 ;; parameter and other faces are updated, before applying
524 ;; the other parameters.
525 (when new-bg
526 (modify-frame-parameters frame-initial-frame
527 (list new-bg))
528 (unless (assq 'background-mode newparms)
529 (frame-set-background-mode frame-initial-frame))
530 (face-set-after-frame-default frame-initial-frame)
531 (setq newparms (delq new-bg newparms)))
532 (modify-frame-parameters frame-initial-frame newparms)))))
533
534 ;; Restore the original buffer.
535 (set-buffer old-buffer)
536
537 ;; Make sure the initial frame can be GC'd if it is ever deleted.
538 ;; Make sure frame-notice-user-settings does nothing if called twice.
539 (setq frame-notice-user-settings nil)
540 (setq frame-initial-frame nil)))
541
542 (defun make-initial-minibuffer-frame (display)
543 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
544 (if display
545 (make-frame-on-display display parms)
546 (make-frame parms))))
547
548 ;;;; Creation of additional frames, and other frame miscellanea
549
550 (defun modify-all-frames-parameters (alist)
551 "Modify all current and future frames' parameters according to ALIST.
552 This changes `default-frame-alist' and possibly `initial-frame-alist'.
553 Furthermore, this function removes all parameters in ALIST from
554 `window-system-default-frame-alist'.
555 See help of `modify-frame-parameters' for more information."
556 (dolist (frame (frame-list))
557 (modify-frame-parameters frame alist))
558
559 (dolist (pair alist) ;; conses to add/replace
560 ;; initial-frame-alist needs setting only when
561 ;; frame-notice-user-settings is true.
562 (and frame-notice-user-settings
563 (setq initial-frame-alist
564 (assq-delete-all (car pair) initial-frame-alist)))
565 (setq default-frame-alist
566 (assq-delete-all (car pair) default-frame-alist))
567 ;; Remove any similar settings from the window-system specific
568 ;; parameters---they would override default-frame-alist.
569 (dolist (w window-system-default-frame-alist)
570 (setcdr w (assq-delete-all (car pair) (cdr w)))))
571
572 (and frame-notice-user-settings
573 (setq initial-frame-alist (append initial-frame-alist alist)))
574 (setq default-frame-alist (append default-frame-alist alist)))
575
576 (defun get-other-frame ()
577 "Return some frame other than the current frame.
578 Create one if necessary. Note that the minibuffer frame, if separate,
579 is not considered (see `next-frame')."
580 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
581 (make-frame)
582 (next-frame (selected-frame)))))
583 s))
584
585 (defun next-multiframe-window ()
586 "Select the next window, regardless of which frame it is on."
587 (interactive)
588 (select-window (next-window (selected-window)
589 (> (minibuffer-depth) 0)
590 0))
591 (select-frame-set-input-focus (selected-frame)))
592
593 (defun previous-multiframe-window ()
594 "Select the previous window, regardless of which frame it is on."
595 (interactive)
596 (select-window (previous-window (selected-window)
597 (> (minibuffer-depth) 0)
598 0))
599 (select-frame-set-input-focus (selected-frame)))
600
601 (declare-function x-initialize-window-system "term/x-win" ())
602 (declare-function ns-initialize-window-system "term/ns-win" ())
603 (defvar x-display-name) ; term/x-win
604
605 (defun make-frame-on-display (display &optional parameters)
606 "Make a frame on display DISPLAY.
607 The optional argument PARAMETERS specifies additional frame parameters."
608 (interactive "sMake frame on display: ")
609 (cond ((featurep 'ns)
610 (when (and (boundp 'ns-initialized) (not ns-initialized))
611 (setq x-display-name display)
612 (ns-initialize-window-system))
613 (make-frame `((window-system . ns)
614 (display . ,display) . ,parameters)))
615 ((eq system-type 'windows-nt)
616 ;; On Windows, ignore DISPLAY.
617 (make-frame parameters))
618 (t
619 (unless (string-match-p "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'" display)
620 (error "Invalid display, not HOST:SERVER or HOST:SERVER.SCREEN"))
621 (when (and (boundp 'x-initialized) (not x-initialized))
622 (setq x-display-name display)
623 (x-initialize-window-system))
624 (make-frame `((window-system . x)
625 (display . ,display) . ,parameters)))))
626
627 (declare-function x-close-connection "xfns.c" (terminal))
628
629 (defun close-display-connection (display)
630 "Close the connection to a display, deleting all its associated frames.
631 For DISPLAY, specify either a frame or a display name (a string).
632 If DISPLAY is nil, that stands for the selected frame's display."
633 (interactive
634 (list
635 (let* ((default (frame-parameter nil 'display))
636 (display (completing-read
637 (format "Close display (default %s): " default)
638 (delete-dups
639 (mapcar (lambda (frame)
640 (frame-parameter frame 'display))
641 (frame-list)))
642 nil t nil nil
643 default)))
644 (if (zerop (length display)) default display))))
645 (let ((frames (delq nil
646 (mapcar (lambda (frame)
647 (if (equal display
648 (frame-parameter frame 'display))
649 frame))
650 (frame-list)))))
651 (if (and (consp frames)
652 (not (y-or-n-p (if (cdr frames)
653 (format "Delete %s frames? " (length frames))
654 (format "Delete %s ? " (car frames))))))
655 (error "Abort!")
656 (mapc 'delete-frame frames)
657 (x-close-connection display))))
658
659 (defun make-frame-command ()
660 "Make a new frame, on the same terminal as the selected frame.
661 If the terminal is a text-only terminal, this also selects the
662 new frame."
663 (interactive)
664 (if (display-graphic-p)
665 (make-frame)
666 (select-frame (make-frame))))
667
668 (defvar before-make-frame-hook nil
669 "Functions to run before a frame is created.")
670
671 (defvar after-make-frame-functions nil
672 "Functions to run after a frame is created.
673 The functions are run with one arg, the newly created frame.")
674
675 (defvar after-setting-font-hook nil
676 "Functions to run after a frame's font has been changed.")
677
678 ;; Alias, kept temporarily.
679 (define-obsolete-function-alias 'new-frame 'make-frame "22.1")
680
681 (defvar frame-inherited-parameters '()
682 ;; FIXME: Shouldn't we add `font' here as well?
683 "Parameters `make-frame' copies from the `selected-frame' to the new frame.")
684
685 (defun make-frame (&optional parameters)
686 "Return a newly created frame displaying the current buffer.
687 Optional argument PARAMETERS is an alist of frame parameters for
688 the new frame. Each element of PARAMETERS should have the
689 form (NAME . VALUE), for example:
690
691 (name . STRING) The frame should be named STRING.
692
693 (width . NUMBER) The frame should be NUMBER characters in width.
694 (height . NUMBER) The frame should be NUMBER text lines high.
695
696 You cannot specify either `width' or `height', you must specify
697 neither or both.
698
699 (minibuffer . t) The frame should have a minibuffer.
700 (minibuffer . nil) The frame should have no minibuffer.
701 (minibuffer . only) The frame should contain only a minibuffer.
702 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
703
704 (window-system . nil) The frame should be displayed on a terminal device.
705 (window-system . x) The frame should be displayed in an X window.
706
707 (terminal . TERMINAL) The frame should use the terminal object TERMINAL.
708
709 In addition, any parameter specified in `default-frame-alist',
710 but not present in PARAMETERS, is applied.
711
712 Before creating the frame (via `frame-creation-function-alist'),
713 this function runs the hook `before-make-frame-hook'. After
714 creating the frame, it runs the hook `after-make-frame-functions'
715 with one arg, the newly created frame.
716
717 On graphical displays, this function does not itself make the new
718 frame the selected frame. However, the window system may select
719 the new frame according to its own rules."
720 (interactive)
721 (let* ((w (cond
722 ((assq 'terminal parameters)
723 (let ((type (terminal-live-p (cdr (assq 'terminal parameters)))))
724 (cond
725 ((eq type t) nil)
726 ((eq type nil) (error "Terminal %s does not exist"
727 (cdr (assq 'terminal parameters))))
728 (t type))))
729 ((assq 'window-system parameters)
730 (cdr (assq 'window-system parameters)))
731 (t window-system)))
732 (frame-creation-function (cdr (assq w frame-creation-function-alist)))
733 (oldframe (selected-frame))
734 (params parameters)
735 frame)
736 (unless frame-creation-function
737 (error "Don't know how to create a frame on window system %s" w))
738 ;; Add parameters from `window-system-default-frame-alist'.
739 (dolist (p (cdr (assq w window-system-default-frame-alist)))
740 (unless (assq (car p) params)
741 (push p params)))
742 ;; Add parameters from `default-frame-alist'.
743 (dolist (p default-frame-alist)
744 (unless (assq (car p) params)
745 (push p params)))
746 ;; Now make the frame.
747 (run-hooks 'before-make-frame-hook)
748 (setq frame (funcall frame-creation-function params))
749 (normal-erase-is-backspace-setup-frame frame)
750 ;; Inherit the original frame's parameters.
751 (dolist (param frame-inherited-parameters)
752 (unless (assq param parameters) ;Overridden by explicit parameters.
753 (let ((val (frame-parameter oldframe param)))
754 (when val (set-frame-parameter frame param val)))))
755 (run-hook-with-args 'after-make-frame-functions frame)
756 frame))
757
758 (defun filtered-frame-list (predicate)
759 "Return a list of all live frames which satisfy PREDICATE."
760 (let* ((frames (frame-list))
761 (list frames))
762 (while (consp frames)
763 (unless (funcall predicate (car frames))
764 (setcar frames nil))
765 (setq frames (cdr frames)))
766 (delq nil list)))
767
768 (defun minibuffer-frame-list ()
769 "Return a list of all frames with their own minibuffers."
770 (filtered-frame-list
771 (lambda (frame)
772 (eq frame (window-frame (minibuffer-window frame))))))
773
774 ;; Used to be called `terminal-id' in termdev.el.
775 (defun get-device-terminal (device)
776 "Return the terminal corresponding to DEVICE.
777 DEVICE can be a terminal, a frame, nil (meaning the selected frame's terminal),
778 the name of an X display device (HOST.SERVER.SCREEN) or a tty device file."
779 (cond
780 ((or (null device) (framep device))
781 (frame-terminal device))
782 ((stringp device)
783 (let ((f (car (filtered-frame-list
784 (lambda (frame)
785 (or (equal (frame-parameter frame 'display) device)
786 (equal (frame-parameter frame 'tty) device)))))))
787 (or f (error "Display %s does not exist" device))
788 (frame-terminal f)))
789 ((terminal-live-p device) device)
790 (t
791 (error "Invalid argument %s in `get-device-terminal'" device))))
792
793 (defun frames-on-display-list (&optional device)
794 "Return a list of all frames on DEVICE.
795
796 DEVICE should be a terminal, a frame,
797 or a name of an X display or tty (a string of the form
798 HOST:SERVER.SCREEN).
799
800 If DEVICE is omitted or nil, it defaults to the selected
801 frame's terminal device."
802 (let* ((terminal (get-device-terminal device))
803 (func #'(lambda (frame)
804 (eq (frame-terminal frame) terminal))))
805 (filtered-frame-list func)))
806
807 (defun framep-on-display (&optional terminal)
808 "Return the type of frames on TERMINAL.
809 TERMINAL may be a terminal id, a display name or a frame. If it
810 is a frame, its type is returned. If TERMINAL is omitted or nil,
811 it defaults to the selected frame's terminal device. All frames
812 on a given display are of the same type."
813 (or (terminal-live-p terminal)
814 (framep terminal)
815 (framep (car (frames-on-display-list terminal)))))
816
817 (defun frame-remove-geometry-params (param-list)
818 "Return the parameter list PARAM-LIST, but with geometry specs removed.
819 This deletes all bindings in PARAM-LIST for `top', `left', `width',
820 `height', `user-size' and `user-position' parameters.
821 Emacs uses this to avoid overriding explicit moves and resizings from
822 the user during startup."
823 (setq param-list (cons nil param-list))
824 (let ((tail param-list))
825 (while (consp (cdr tail))
826 (if (and (consp (car (cdr tail)))
827 (memq (car (car (cdr tail)))
828 '(height width top left user-position user-size)))
829 (progn
830 (setq frame-initial-geometry-arguments
831 (cons (car (cdr tail)) frame-initial-geometry-arguments))
832 (setcdr tail (cdr (cdr tail))))
833 (setq tail (cdr tail)))))
834 (setq frame-initial-geometry-arguments
835 (nreverse frame-initial-geometry-arguments))
836 (cdr param-list))
837
838 (declare-function x-focus-frame "xfns.c" (frame))
839
840 (defun select-frame-set-input-focus (frame)
841 "Select FRAME, raise it, and set input focus, if possible.
842 If `mouse-autoselect-window' is non-nil, also move mouse pointer
843 to FRAME's selected window. Otherwise, if `focus-follows-mouse'
844 is non-nil, move mouse cursor to FRAME."
845 (select-frame frame)
846 (raise-frame frame)
847 ;; Ensure, if possible, that FRAME gets input focus.
848 (when (memq (window-system frame) '(x w32 ns))
849 (x-focus-frame frame))
850 ;; Move mouse cursor if necessary.
851 (cond
852 (mouse-autoselect-window
853 (let ((edges (window-inside-edges (frame-selected-window frame))))
854 ;; Move mouse cursor into FRAME's selected window to avoid that
855 ;; Emacs mouse-autoselects another window.
856 (set-mouse-position frame (nth 2 edges) (nth 1 edges))))
857 (focus-follows-mouse
858 ;; Move mouse cursor into FRAME to avoid that another frame gets
859 ;; selected by the window manager.
860 (set-mouse-position frame (1- (frame-width frame)) 0))))
861
862 (defun other-frame (arg)
863 "Select the ARGth different visible frame on current display, and raise it.
864 All frames are arranged in a cyclic order.
865 This command selects the frame ARG steps away in that order.
866 A negative ARG moves in the opposite order.
867
868 To make this command work properly, you must tell Emacs
869 how the system (or the window manager) generally handles
870 focus-switching between windows. If moving the mouse onto a window
871 selects it (gives it focus), set `focus-follows-mouse' to t.
872 Otherwise, that variable should be nil."
873 (interactive "p")
874 (let ((frame (selected-frame)))
875 (while (> arg 0)
876 (setq frame (next-frame frame))
877 (while (not (eq (frame-visible-p frame) t))
878 (setq frame (next-frame frame)))
879 (setq arg (1- arg)))
880 (while (< arg 0)
881 (setq frame (previous-frame frame))
882 (while (not (eq (frame-visible-p frame) t))
883 (setq frame (previous-frame frame)))
884 (setq arg (1+ arg)))
885 (select-frame-set-input-focus frame)))
886
887 (defun iconify-or-deiconify-frame ()
888 "Iconify the selected frame, or deiconify if it's currently an icon."
889 (interactive)
890 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
891 (iconify-frame)
892 (make-frame-visible)))
893
894 (defun suspend-frame ()
895 "Do whatever is right to suspend the current frame.
896 Calls `suspend-emacs' if invoked from the controlling tty device,
897 `suspend-tty' from a secondary tty device, and
898 `iconify-or-deiconify-frame' from an X frame."
899 (interactive)
900 (let ((type (framep (selected-frame))))
901 (cond
902 ((memq type '(x ns w32)) (iconify-or-deiconify-frame))
903 ((eq type t)
904 (if (controlling-tty-p)
905 (suspend-emacs)
906 (suspend-tty)))
907 (t (suspend-emacs)))))
908
909 (defun make-frame-names-alist ()
910 ;; Only consider the frames on the same display.
911 (let* ((current-frame (selected-frame))
912 (falist
913 (cons
914 (cons (frame-parameter current-frame 'name) current-frame) nil))
915 (frame (next-frame nil 0)))
916 (while (not (eq frame current-frame))
917 (progn
918 (push (cons (frame-parameter frame 'name) frame) falist)
919 (setq frame (next-frame frame 0))))
920 falist))
921
922 (defvar frame-name-history nil)
923 (defun select-frame-by-name (name)
924 "Select the frame on the current terminal whose name is NAME and raise it.
925 If there is no frame by that name, signal an error."
926 (interactive
927 (let* ((frame-names-alist (make-frame-names-alist))
928 (default (car (car frame-names-alist)))
929 (input (completing-read
930 (format "Select Frame (default %s): " default)
931 frame-names-alist nil t nil 'frame-name-history)))
932 (if (= (length input) 0)
933 (list default)
934 (list input))))
935 (let* ((frame-names-alist (make-frame-names-alist))
936 (frame (cdr (assoc name frame-names-alist))))
937 (if frame
938 (select-frame-set-input-focus frame)
939 (error "There is no frame named `%s'" name))))
940 \f
941 ;;;; Frame configurations
942
943 (defun current-frame-configuration ()
944 "Return a list describing the positions and states of all frames.
945 Its car is `frame-configuration'.
946 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
947 where
948 FRAME is a frame object,
949 ALIST is an association list specifying some of FRAME's parameters, and
950 WINDOW-CONFIG is a window configuration object for FRAME."
951 (cons 'frame-configuration
952 (mapcar (lambda (frame)
953 (list frame
954 (frame-parameters frame)
955 (current-window-configuration frame)))
956 (frame-list))))
957
958 (defun set-frame-configuration (configuration &optional nodelete)
959 "Restore the frames to the state described by CONFIGURATION.
960 Each frame listed in CONFIGURATION has its position, size, window
961 configuration, and other parameters set as specified in CONFIGURATION.
962 However, this function does not restore deleted frames.
963
964 Ordinarily, this function deletes all existing frames not
965 listed in CONFIGURATION. But if optional second argument NODELETE
966 is given and non-nil, the unwanted frames are iconified instead."
967 (or (frame-configuration-p configuration)
968 (signal 'wrong-type-argument
969 (list 'frame-configuration-p configuration)))
970 (let ((config-alist (cdr configuration))
971 frames-to-delete)
972 (dolist (frame (frame-list))
973 (let ((parameters (assq frame config-alist)))
974 (if parameters
975 (progn
976 (modify-frame-parameters
977 frame
978 ;; Since we can't set a frame's minibuffer status,
979 ;; we might as well omit the parameter altogether.
980 (let* ((parms (nth 1 parameters))
981 (mini (assq 'minibuffer parms))
982 (name (assq 'name parms))
983 (explicit-name (cdr (assq 'explicit-name parms))))
984 (when mini (setq parms (delq mini parms)))
985 ;; Leave name in iff it was set explicitly.
986 ;; This should fix the behavior reported in
987 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg01632.html
988 (when (and name (not explicit-name))
989 (setq parms (delq name parms)))
990 parms))
991 (set-window-configuration (nth 2 parameters)))
992 (setq frames-to-delete (cons frame frames-to-delete)))))
993 (mapc (if nodelete
994 ;; Note: making frames invisible here was tried
995 ;; but led to some strange behavior--each time the frame
996 ;; was made visible again, the window manager asked afresh
997 ;; for where to put it.
998 'iconify-frame
999 'delete-frame)
1000 frames-to-delete)))
1001 \f
1002 ;;;; Convenience functions for accessing and interactively changing
1003 ;;;; frame parameters.
1004
1005 (defun frame-height (&optional frame)
1006 "Return number of lines available for display on FRAME.
1007 If FRAME is omitted, describe the currently selected frame.
1008 Exactly what is included in the return value depends on the
1009 window-system and toolkit in use - see `frame-pixel-height' for
1010 more details. The lines are in units of the default font height.
1011
1012 The result is roughly related to the frame pixel height via
1013 height in pixels = height in lines * `frame-char-height'.
1014 However, this is only approximate, and is complicated e.g. by the
1015 fact that individual window lines and menu bar lines can have
1016 differing font heights."
1017 (cdr (assq 'height (frame-parameters frame))))
1018
1019 (defun frame-width (&optional frame)
1020 "Return number of columns available for display on FRAME.
1021 If FRAME is omitted, describe the currently selected frame."
1022 (cdr (assq 'width (frame-parameters frame))))
1023
1024 (declare-function x-list-fonts "xfaces.c"
1025 (pattern &optional face frame maximum width))
1026
1027 (define-obsolete-function-alias 'set-default-font 'set-frame-font "23.1")
1028 (defun set-frame-font (font-name &optional keep-size)
1029 "Set the font of the selected frame to FONT-NAME.
1030 When called interactively, prompt for the name of the font to use.
1031 To get the frame's current default font, use `frame-parameters'.
1032
1033 The default behavior is to keep the numbers of lines and columns in
1034 the frame, thus may change its pixel size. If optional KEEP-SIZE is
1035 non-nil (interactively, prefix argument) the current frame size (in
1036 pixels) is kept by adjusting the numbers of the lines and columns."
1037 (interactive
1038 (let* ((completion-ignore-case t)
1039 (font (completing-read "Font name: "
1040 ;; x-list-fonts will fail with an error
1041 ;; if this frame doesn't support fonts.
1042 (x-list-fonts "*" nil (selected-frame))
1043 nil nil nil nil
1044 (frame-parameter nil 'font))))
1045 (list font current-prefix-arg)))
1046 (let (fht fwd)
1047 (if keep-size
1048 (setq fht (* (frame-parameter nil 'height) (frame-char-height))
1049 fwd (* (frame-parameter nil 'width) (frame-char-width))))
1050 (modify-frame-parameters (selected-frame)
1051 (list (cons 'font font-name)))
1052 (if keep-size
1053 (modify-frame-parameters
1054 (selected-frame)
1055 (list (cons 'height (round fht (frame-char-height)))
1056 (cons 'width (round fwd (frame-char-width)))))))
1057 (run-hooks 'after-setting-font-hook 'after-setting-font-hooks))
1058
1059 (defun set-frame-parameter (frame parameter value)
1060 "Set frame parameter PARAMETER to VALUE on FRAME.
1061 If FRAME is nil, it defaults to the selected frame.
1062 See `modify-frame-parameters'."
1063 (modify-frame-parameters frame (list (cons parameter value))))
1064
1065 (defun set-background-color (color-name)
1066 "Set the background color of the selected frame to COLOR-NAME.
1067 When called interactively, prompt for the name of the color to use.
1068 To get the frame's current background color, use `frame-parameters'."
1069 (interactive (list (read-color "Background color: ")))
1070 (modify-frame-parameters (selected-frame)
1071 (list (cons 'background-color color-name)))
1072 (or window-system
1073 (face-set-after-frame-default (selected-frame))))
1074
1075 (defun set-foreground-color (color-name)
1076 "Set the foreground color of the selected frame to COLOR-NAME.
1077 When called interactively, prompt for the name of the color to use.
1078 To get the frame's current foreground color, use `frame-parameters'."
1079 (interactive (list (read-color "Foreground color: ")))
1080 (modify-frame-parameters (selected-frame)
1081 (list (cons 'foreground-color color-name)))
1082 (or window-system
1083 (face-set-after-frame-default (selected-frame))))
1084
1085 (defun set-cursor-color (color-name)
1086 "Set the text cursor color of the selected frame to COLOR-NAME.
1087 When called interactively, prompt for the name of the color to use.
1088 To get the frame's current cursor color, use `frame-parameters'."
1089 (interactive (list (read-color "Cursor color: ")))
1090 (modify-frame-parameters (selected-frame)
1091 (list (cons 'cursor-color color-name))))
1092
1093 (defun set-mouse-color (color-name)
1094 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
1095 When called interactively, prompt for the name of the color to use.
1096 To get the frame's current mouse color, use `frame-parameters'."
1097 (interactive (list (read-color "Mouse color: ")))
1098 (modify-frame-parameters (selected-frame)
1099 (list (cons 'mouse-color
1100 (or color-name
1101 (cdr (assq 'mouse-color
1102 (frame-parameters))))))))
1103
1104 (defun set-border-color (color-name)
1105 "Set the color of the border of the selected frame to COLOR-NAME.
1106 When called interactively, prompt for the name of the color to use.
1107 To get the frame's current border color, use `frame-parameters'."
1108 (interactive (list (read-color "Border color: ")))
1109 (modify-frame-parameters (selected-frame)
1110 (list (cons 'border-color color-name))))
1111
1112 (define-minor-mode auto-raise-mode
1113 "Toggle whether or not the selected frame should auto-raise.
1114 With ARG, turn auto-raise mode on if and only if ARG is positive.
1115 Note that this controls Emacs's own auto-raise feature.
1116 Some window managers allow you to enable auto-raise for certain windows.
1117 You can use that for Emacs windows if you wish, but if you do,
1118 that is beyond the control of Emacs and this command has no effect on it."
1119 :variable (frame-parameter nil 'auto-raise)
1120 (if (frame-parameter nil 'auto-raise)
1121 (raise-frame)))
1122
1123 (define-minor-mode auto-lower-mode
1124 "Toggle whether or not the selected frame should auto-lower.
1125 With ARG, turn auto-lower mode on if and only if ARG is positive.
1126 Note that this controls Emacs's own auto-lower feature.
1127 Some window managers allow you to enable auto-lower for certain windows.
1128 You can use that for Emacs windows if you wish, but if you do,
1129 that is beyond the control of Emacs and this command has no effect on it."
1130 :variable (frame-parameter nil 'auto-lower))
1131
1132 (defun set-frame-name (name)
1133 "Set the name of the selected frame to NAME.
1134 When called interactively, prompt for the name of the frame.
1135 The frame name is displayed on the modeline if the terminal displays only
1136 one frame, otherwise the name is displayed on the frame's caption bar."
1137 (interactive "sFrame name: ")
1138 (modify-frame-parameters (selected-frame)
1139 (list (cons 'name name))))
1140
1141 (defun frame-current-scroll-bars (&optional frame)
1142 "Return the current scroll-bar settings in frame FRAME.
1143 Value is a cons (VERTICAL . HORIZ0NTAL) where VERTICAL specifies the
1144 current location of the vertical scroll-bars (left, right, or nil),
1145 and HORIZONTAL specifies the current location of the horizontal scroll
1146 bars (top, bottom, or nil)."
1147 (let ((vert (frame-parameter frame 'vertical-scroll-bars))
1148 (hor nil))
1149 (unless (memq vert '(left right nil))
1150 (setq vert default-frame-scroll-bars))
1151 (cons vert hor)))
1152 \f
1153 ;;;; Frame/display capabilities.
1154 (defun selected-terminal ()
1155 "Return the terminal that is now selected."
1156 (frame-terminal (selected-frame)))
1157
1158 (declare-function msdos-mouse-p "dosfns.c")
1159
1160 (defun display-mouse-p (&optional display)
1161 "Return non-nil if DISPLAY has a mouse available.
1162 DISPLAY can be a display name, a frame, or nil (meaning the selected
1163 frame's display)."
1164 (let ((frame-type (framep-on-display display)))
1165 (cond
1166 ((eq frame-type 'pc)
1167 (msdos-mouse-p))
1168 ((eq system-type 'windows-nt)
1169 (with-no-warnings
1170 (> w32-num-mouse-buttons 0)))
1171 ((memq frame-type '(x ns))
1172 t) ;; We assume X and NeXTstep *always* have a pointing device
1173 (t
1174 (or (and (featurep 'xt-mouse)
1175 xterm-mouse-mode)
1176 ;; t-mouse is distributed with the GPM package. It doesn't have
1177 ;; a toggle.
1178 (featurep 't-mouse))))))
1179
1180 (defun display-popup-menus-p (&optional display)
1181 "Return non-nil if popup menus are supported on DISPLAY.
1182 DISPLAY can be a display name, a frame, or nil (meaning the selected
1183 frame's display).
1184 Support for popup menus requires that the mouse be available."
1185 (and
1186 (let ((frame-type (framep-on-display display)))
1187 (memq frame-type '(x w32 pc ns)))
1188 (display-mouse-p display)))
1189
1190 (defun display-graphic-p (&optional display)
1191 "Return non-nil if DISPLAY is a graphic display.
1192 Graphical displays are those which are capable of displaying several
1193 frames and several different fonts at once. This is true for displays
1194 that use a window system such as X, and false for text-only terminals.
1195 DISPLAY can be a display name, a frame, or nil (meaning the selected
1196 frame's display)."
1197 (not (null (memq (framep-on-display display) '(x w32 ns)))))
1198
1199 (defun display-images-p (&optional display)
1200 "Return non-nil if DISPLAY can display images.
1201
1202 DISPLAY can be a display name, a frame, or nil (meaning the selected
1203 frame's display)."
1204 (and (display-graphic-p display)
1205 (fboundp 'image-mask-p)
1206 (fboundp 'image-size)))
1207
1208 (defalias 'display-multi-frame-p 'display-graphic-p)
1209 (defalias 'display-multi-font-p 'display-graphic-p)
1210
1211 (defun display-selections-p (&optional display)
1212 "Return non-nil if DISPLAY supports selections.
1213 A selection is a way to transfer text or other data between programs
1214 via special system buffers called `selection' or `clipboard'.
1215 DISPLAY can be a display name, a frame, or nil (meaning the selected
1216 frame's display)."
1217 (let ((frame-type (framep-on-display display)))
1218 (cond
1219 ((eq frame-type 'pc)
1220 ;; MS-DOG frames support selections when Emacs runs inside
1221 ;; the Windows' DOS Box.
1222 (with-no-warnings
1223 (not (null dos-windows-version))))
1224 ((memq frame-type '(x w32 ns))
1225 t) ;; FIXME?
1226 (t
1227 nil))))
1228
1229 (declare-function x-display-screens "xfns.c" (&optional terminal))
1230
1231 (defun display-screens (&optional display)
1232 "Return the number of screens associated with DISPLAY."
1233 (let ((frame-type (framep-on-display display)))
1234 (cond
1235 ((memq frame-type '(x w32 ns))
1236 (x-display-screens display))
1237 (t
1238 1))))
1239
1240 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
1241
1242 (defun display-pixel-height (&optional display)
1243 "Return the height of DISPLAY's screen in pixels.
1244 For character terminals, each character counts as a single pixel."
1245 (let ((frame-type (framep-on-display display)))
1246 (cond
1247 ((memq frame-type '(x w32 ns))
1248 (x-display-pixel-height display))
1249 (t
1250 (frame-height (if (framep display) display (selected-frame)))))))
1251
1252 (declare-function x-display-pixel-width "xfns.c" (&optional terminal))
1253
1254 (defun display-pixel-width (&optional display)
1255 "Return the width of DISPLAY's screen in pixels.
1256 For character terminals, each character counts as a single pixel."
1257 (let ((frame-type (framep-on-display display)))
1258 (cond
1259 ((memq frame-type '(x w32 ns))
1260 (x-display-pixel-width display))
1261 (t
1262 (frame-width (if (framep display) display (selected-frame)))))))
1263
1264 (defcustom display-mm-dimensions-alist nil
1265 "Alist for specifying screen dimensions in millimeters.
1266 The dimensions will be used for `display-mm-height' and
1267 `display-mm-width' if defined for the respective display.
1268
1269 Each element of the alist has the form (display . (width . height)),
1270 e.g. (\":0.0\" . (287 . 215)).
1271
1272 If `display' equals t, it specifies dimensions for all graphical
1273 displays not explicitely specified."
1274 :version "22.1"
1275 :type '(alist :key-type (choice (string :tag "Display name")
1276 (const :tag "Default" t))
1277 :value-type (cons :tag "Dimensions"
1278 (integer :tag "Width")
1279 (integer :tag "Height")))
1280 :group 'frames)
1281
1282 (declare-function x-display-mm-height "xfns.c" (&optional terminal))
1283
1284 (defun display-mm-height (&optional display)
1285 "Return the height of DISPLAY's screen in millimeters.
1286 System values can be overridden by `display-mm-dimensions-alist'.
1287 If the information is unavailable, value is nil."
1288 (and (memq (framep-on-display display) '(x w32 ns))
1289 (or (cddr (assoc (or display (frame-parameter nil 'display))
1290 display-mm-dimensions-alist))
1291 (cddr (assoc t display-mm-dimensions-alist))
1292 (x-display-mm-height display))))
1293
1294 (declare-function x-display-mm-width "xfns.c" (&optional terminal))
1295
1296 (defun display-mm-width (&optional display)
1297 "Return the width of DISPLAY's screen in millimeters.
1298 System values can be overridden by `display-mm-dimensions-alist'.
1299 If the information is unavailable, value is nil."
1300 (and (memq (framep-on-display display) '(x w32 ns))
1301 (or (cadr (assoc (or display (frame-parameter nil 'display))
1302 display-mm-dimensions-alist))
1303 (cadr (assoc t display-mm-dimensions-alist))
1304 (x-display-mm-width display))))
1305
1306 (declare-function x-display-backing-store "xfns.c" (&optional terminal))
1307
1308 (defun display-backing-store (&optional display)
1309 "Return the backing store capability of DISPLAY's screen.
1310 The value may be `always', `when-mapped', `not-useful', or nil if
1311 the question is inapplicable to a certain kind of display."
1312 (let ((frame-type (framep-on-display display)))
1313 (cond
1314 ((memq frame-type '(x w32 ns))
1315 (x-display-backing-store display))
1316 (t
1317 'not-useful))))
1318
1319 (declare-function x-display-save-under "xfns.c" (&optional terminal))
1320
1321 (defun display-save-under (&optional display)
1322 "Return non-nil if DISPLAY's screen supports the SaveUnder feature."
1323 (let ((frame-type (framep-on-display display)))
1324 (cond
1325 ((memq frame-type '(x w32 ns))
1326 (x-display-save-under display))
1327 (t
1328 'not-useful))))
1329
1330 (declare-function x-display-planes "xfns.c" (&optional terminal))
1331
1332 (defun display-planes (&optional display)
1333 "Return the number of planes supported by DISPLAY."
1334 (let ((frame-type (framep-on-display display)))
1335 (cond
1336 ((memq frame-type '(x w32 ns))
1337 (x-display-planes display))
1338 ((eq frame-type 'pc)
1339 4)
1340 (t
1341 (truncate (log (length (tty-color-alist)) 2))))))
1342
1343 (declare-function x-display-color-cells "xfns.c" (&optional terminal))
1344
1345 (defun display-color-cells (&optional display)
1346 "Return the number of color cells supported by DISPLAY."
1347 (let ((frame-type (framep-on-display display)))
1348 (cond
1349 ((memq frame-type '(x w32 ns))
1350 (x-display-color-cells display))
1351 ((eq frame-type 'pc)
1352 16)
1353 (t
1354 (tty-display-color-cells display)))))
1355
1356 (declare-function x-display-visual-class "xfns.c" (&optional terminal))
1357
1358 (defun display-visual-class (&optional display)
1359 "Return the visual class of DISPLAY.
1360 The value is one of the symbols `static-gray', `gray-scale',
1361 `static-color', `pseudo-color', `true-color', or `direct-color'."
1362 (let ((frame-type (framep-on-display display)))
1363 (cond
1364 ((memq frame-type '(x w32 ns))
1365 (x-display-visual-class display))
1366 ((and (memq frame-type '(pc t))
1367 (tty-display-color-p display))
1368 'static-color)
1369 (t
1370 'static-gray))))
1371
1372 \f
1373 ;;;; Frame geometry values
1374
1375 (defun frame-geom-value-cons (type value &optional frame)
1376 "Return equivalent geometry value for FRAME as a cons with car `+'.
1377 A geometry value equivalent to VALUE for FRAME is returned,
1378 where the value is a cons with car `+', not numeric.
1379 TYPE is the car of the original geometry spec (TYPE . VALUE).
1380 It is `top' or `left', depending on which edge VALUE is related to.
1381 VALUE is the cdr of a frame geometry spec: (left/top . VALUE).
1382 If VALUE is a number, then it is converted to a cons value, perhaps
1383 relative to the opposite frame edge from that in the original spec.
1384 FRAME defaults to the selected frame.
1385
1386 Examples (measures in pixels) -
1387 Assuming display height/width=1024, frame height/width=600:
1388 300 inside display edge: 300 => (+ 300)
1389 (+ 300) => (+ 300)
1390 300 inside opposite display edge: (- 300) => (+ 124)
1391 -300 => (+ 124)
1392 300 beyond display edge
1393 (= 724 inside opposite display edge): (+ -300) => (+ -300)
1394 300 beyond display edge
1395 (= 724 inside opposite display edge): (- -300) => (+ 724)
1396
1397 In the 3rd, 4th, and 6th examples, the returned value is relative to
1398 the opposite frame edge from the edge indicated in the input spec."
1399 (cond ((and (consp value) (eq '+ (car value))) ; e.g. (+ 300), (+ -300)
1400 value)
1401 ((natnump value) (list '+ value)) ; e.g. 300 => (+ 300)
1402 (t ; e.g. -300, (- 300), (- -300)
1403 (list '+ (- (if (eq 'left type) ; => (+ 124), (+ 124), (+ 724)
1404 (x-display-pixel-width)
1405 (x-display-pixel-height))
1406 (if (integerp value) (- value) (cadr value))
1407 (if (eq 'left type)
1408 (frame-pixel-width frame)
1409 (frame-pixel-height frame)))))))
1410
1411 (defun frame-geom-spec-cons (spec &optional frame)
1412 "Return equivalent geometry spec for FRAME as a cons with car `+'.
1413 A geometry specification equivalent to SPEC for FRAME is returned,
1414 where the value is a cons with car `+', not numeric.
1415 SPEC is a frame geometry spec: (left . VALUE) or (top . VALUE).
1416 If VALUE is a number, then it is converted to a cons value, perhaps
1417 relative to the opposite frame edge from that in the original spec.
1418 FRAME defaults to the selected frame.
1419
1420 Examples (measures in pixels) -
1421 Assuming display height=1024, frame height=600:
1422 top 300 below display top: (top . 300) => (top + 300)
1423 (top + 300) => (top + 300)
1424 bottom 300 above display bottom: (top - 300) => (top + 124)
1425 (top . -300) => (top + 124)
1426 top 300 above display top
1427 (= bottom 724 above display bottom): (top + -300) => (top + -300)
1428 bottom 300 below display bottom
1429 (= top 724 below display top): (top - -300) => (top + 724)
1430
1431 In the 3rd, 4th, and 6th examples, the returned value is relative to
1432 the opposite frame edge from the edge indicated in the input spec."
1433 (cons (car spec) (frame-geom-value-cons (car spec) (cdr spec))))
1434 \f
1435
1436 (defun delete-other-frames (&optional frame)
1437 "Delete all frames except FRAME.
1438 If FRAME uses another frame's minibuffer, the minibuffer frame is
1439 left untouched. FRAME nil or omitted means use the selected frame."
1440 (interactive)
1441 (unless frame
1442 (setq frame (selected-frame)))
1443 (let* ((mini-frame (window-frame (minibuffer-window frame)))
1444 (frames (delq mini-frame (delq frame (frame-list)))))
1445 ;; Only consider frames on the same terminal.
1446 (dolist (frame (prog1 frames (setq frames nil)))
1447 (if (eq (frame-terminal) (frame-terminal frame))
1448 (push frame frames)))
1449 ;; Delete mon-minibuffer-only frames first, because `delete-frame'
1450 ;; signals an error when trying to delete a mini-frame that's
1451 ;; still in use by another frame.
1452 (dolist (frame frames)
1453 (unless (eq (frame-parameter frame 'minibuffer) 'only)
1454 (delete-frame frame)))
1455 ;; Delete minibuffer-only frames.
1456 (dolist (frame frames)
1457 (when (eq (frame-parameter frame 'minibuffer) 'only)
1458 (delete-frame frame)))))
1459
1460 ;; miscellaneous obsolescence declarations
1461 (define-obsolete-variable-alias 'delete-frame-hook
1462 'delete-frame-functions "22.1")
1463
1464 \f
1465 ;; Highlighting trailing whitespace.
1466
1467 (make-variable-buffer-local 'show-trailing-whitespace)
1468
1469 \f
1470 ;; Scrolling
1471
1472 (defgroup scrolling nil
1473 "Scrolling windows."
1474 :version "21.1"
1475 :group 'frames)
1476
1477 (defvaralias 'automatic-hscrolling 'auto-hscroll-mode)
1478
1479 \f
1480 ;; Blinking cursor
1481
1482 (defgroup cursor nil
1483 "Displaying text cursors."
1484 :version "21.1"
1485 :group 'frames)
1486
1487 (defcustom blink-cursor-delay 0.5
1488 "Seconds of idle time after which cursor starts to blink."
1489 :type 'number
1490 :group 'cursor)
1491
1492 (defcustom blink-cursor-interval 0.5
1493 "Length of cursor blink interval in seconds."
1494 :type 'number
1495 :group 'cursor)
1496
1497 (defvar blink-cursor-idle-timer nil
1498 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
1499 The function `blink-cursor-start' is called when the timer fires.")
1500
1501 (defvar blink-cursor-timer nil
1502 "Timer started from `blink-cursor-start'.
1503 This timer calls `blink-cursor-timer-function' every
1504 `blink-cursor-interval' seconds.")
1505
1506 (defun blink-cursor-start ()
1507 "Timer function called from the timer `blink-cursor-idle-timer'.
1508 This starts the timer `blink-cursor-timer', which makes the cursor blink
1509 if appropriate. It also arranges to cancel that timer when the next
1510 command starts, by installing a pre-command hook."
1511 (when (null blink-cursor-timer)
1512 ;; Set up the timer first, so that if this signals an error,
1513 ;; blink-cursor-end is not added to pre-command-hook.
1514 (setq blink-cursor-timer
1515 (run-with-timer blink-cursor-interval blink-cursor-interval
1516 'blink-cursor-timer-function))
1517 (add-hook 'pre-command-hook 'blink-cursor-end)
1518 (internal-show-cursor nil nil)))
1519
1520 (defun blink-cursor-timer-function ()
1521 "Timer function of timer `blink-cursor-timer'."
1522 (internal-show-cursor nil (not (internal-show-cursor-p))))
1523
1524 (defun blink-cursor-end ()
1525 "Stop cursor blinking.
1526 This is installed as a pre-command hook by `blink-cursor-start'.
1527 When run, it cancels the timer `blink-cursor-timer' and removes
1528 itself as a pre-command hook."
1529 (remove-hook 'pre-command-hook 'blink-cursor-end)
1530 (internal-show-cursor nil t)
1531 (when blink-cursor-timer
1532 (cancel-timer blink-cursor-timer)
1533 (setq blink-cursor-timer nil)))
1534
1535 (define-minor-mode blink-cursor-mode
1536 "Toggle blinking cursor mode.
1537 With a numeric argument, turn blinking cursor mode on if ARG is positive,
1538 otherwise turn it off. When blinking cursor mode is enabled, the
1539 cursor of the selected window blinks.
1540
1541 Note that this command is effective only when Emacs
1542 displays through a window system, because then Emacs does its own
1543 cursor display. On a text-only terminal, this is not implemented."
1544 :init-value (not (or noninteractive
1545 no-blinking-cursor
1546 (eq system-type 'ms-dos)
1547 (not (memq window-system '(x w32 ns)))))
1548 :initialize 'custom-initialize-delay
1549 :group 'cursor
1550 :global t
1551 (if blink-cursor-idle-timer (cancel-timer blink-cursor-idle-timer))
1552 (setq blink-cursor-idle-timer nil)
1553 (blink-cursor-end)
1554 (when blink-cursor-mode
1555 ;; Hide the cursor.
1556 ;;(internal-show-cursor nil nil)
1557 (setq blink-cursor-idle-timer
1558 (run-with-idle-timer blink-cursor-delay
1559 blink-cursor-delay
1560 'blink-cursor-start))))
1561
1562 (define-obsolete-variable-alias 'blink-cursor 'blink-cursor-mode "22.1")
1563
1564 \f
1565 ;;;; Key bindings
1566
1567 (define-key ctl-x-5-map "2" 'make-frame-command)
1568 (define-key ctl-x-5-map "1" 'delete-other-frames)
1569 (define-key ctl-x-5-map "0" 'delete-frame)
1570 (define-key ctl-x-5-map "o" 'other-frame)
1571
1572 (provide 'frame)
1573
1574 ;;; frame.el ends here