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