(auto-image-file-mode): Drop unneeded positional args.
[emacs.git] / lisp / frame.el
blob5a9c276a2fe72f7d5c67a1175b875df72dbbe641
1 ;;; frame.el --- multi-frame management independent of window systems.
3 ;; Copyright (C) 1993, 1994, 1996, 1997, 2000 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Code:
27 (defvar frame-creation-function nil
28 "Window-system dependent function to call to create a new frame.
29 The window system startup file should set this to its frame creation
30 function, which should take an alist of parameters as its argument.")
32 ;;; The initial value given here for used to ask for a minibuffer.
33 ;;; But that's not necessary, because the default is to have one.
34 ;;; By not specifying it here, we let an X resource specify it.
35 (defcustom initial-frame-alist nil
36 "*Alist of frame parameters for creating the initial X window frame.
37 You can set this in your `.emacs' file; for example,
38 (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
39 Parameters specified here supersede the values given in `default-frame-alist'.
41 If the value calls for a frame without a minibuffer, and you have not created
42 a minibuffer frame on your own, one is created according to
43 `minibuffer-frame-alist'.
45 You can specify geometry-related options for just the initial frame
46 by setting this variable in your `.emacs' file; however, they won't
47 take effect until Emacs reads `.emacs', which happens after first creating
48 the frame. If you want the frame to have the proper geometry as soon
49 as it appears, you need to use this three-step process:
50 * Specify X resources to give the geometry you want.
51 * Set `default-frame-alist' to override these options so that they
52 don't affect subsequent frames.
53 * Set `initial-frame-alist' in a way that matches the X resources,
54 to override what you put in `default-frame-alist'."
55 :type '(repeat (cons :format "%v"
56 (symbol :tag "Parameter")
57 (sexp :tag "Value")))
58 :group 'frames)
60 (defcustom minibuffer-frame-alist '((width . 80) (height . 2))
61 "*Alist of frame parameters for initially creating a minibuffer frame.
62 You can set this in your `.emacs' file; for example,
63 (setq minibuffer-frame-alist
64 '((top . 1) (left . 1) (width . 80) (height . 2)))
65 Parameters specified here supersede the values given in
66 `default-frame-alist', for a minibuffer frame."
67 :type '(repeat (cons :format "%v"
68 (symbol :tag "Parameter")
69 (sexp :tag "Value")))
70 :group 'frames)
72 (defcustom pop-up-frame-alist nil
73 "*Alist of frame parameters used when creating pop-up frames.
74 Pop-up frames are used for completions, help, and the like.
75 This variable can be set in your init file, like this:
76 (setq pop-up-frame-alist '((width . 80) (height . 20)))
77 These supersede the values given in `default-frame-alist',
78 for pop-up frames."
79 :type '(repeat (cons :format "%v"
80 (symbol :tag "Parameter")
81 (sexp :tag "Value")))
82 :group 'frames)
84 (setq pop-up-frame-function
85 (function (lambda ()
86 (make-frame pop-up-frame-alist))))
88 (defcustom special-display-frame-alist
89 '((height . 14) (width . 80) (unsplittable . t))
90 "*Alist of frame parameters used when creating special frames.
91 Special frames are used for buffers whose names are in
92 `special-display-buffer-names' and for buffers whose names match
93 one of the regular expressions in `special-display-regexps'.
94 This variable can be set in your init file, like this:
95 (setq special-display-frame-alist '((width . 80) (height . 20)))
96 These supersede the values given in `default-frame-alist'."
97 :type '(repeat (cons :format "%v"
98 (symbol :tag "Parameter")
99 (sexp :tag "Value")))
100 :group 'frames)
102 (defun special-display-popup-frame (buffer &optional args)
103 "Display BUFFER in its own frame, reusing an existing window if any.
104 Return the window chosen.
105 Currently we do not insist on selecting the window within its frame.
106 If ARGS is an alist, use it as a list of frame parameter specs.
107 If ARGS is a list whose car is a symbol,
108 use (car ARGS) as a function to do the work.
109 Pass it BUFFER as first arg, and (cdr ARGS) gives the rest of the args."
110 (if (and args (symbolp (car args)))
111 (apply (car args) buffer (cdr args))
112 (let ((window (get-buffer-window buffer t)))
113 (if window
114 ;; If we have a window already, make it visible.
115 (let ((frame (window-frame window)))
116 (make-frame-visible frame)
117 (raise-frame frame)
118 window)
119 ;; If no window yet, make one in a new frame.
120 (let ((frame (make-frame (append args special-display-frame-alist))))
121 (set-window-buffer (frame-selected-window frame) buffer)
122 (set-window-dedicated-p (frame-selected-window frame) t)
123 (frame-selected-window frame))))))
125 (defun handle-delete-frame (event)
126 "Handle delete-frame events from the X server."
127 (interactive "e")
128 (let ((frame (posn-window (event-start event)))
129 (i 0)
130 (tail (frame-list)))
131 (while tail
132 (and (frame-visible-p (car tail))
133 (not (eq (car tail) frame))
134 (setq i (1+ i)))
135 (setq tail (cdr tail)))
136 (if (> i 0)
137 (delete-frame frame t)
138 ;; Gildea@x.org says it is ok to ask questions before terminating.
139 (save-buffers-kill-emacs))))
141 ;;;; Arrangement of frames at startup
143 ;;; 1) Load the window system startup file from the lisp library and read the
144 ;;; high-priority arguments (-q and the like). The window system startup
145 ;;; file should create any frames specified in the window system defaults.
147 ;;; 2) If no frames have been opened, we open an initial text frame.
149 ;;; 3) Once the init file is done, we apply any newly set parameters
150 ;;; in initial-frame-alist to the frame.
152 ;; These are now called explicitly at the proper times,
153 ;; since that is easier to understand.
154 ;; Actually using hooks within Emacs is bad for future maintenance. --rms.
155 ;; (add-hook 'before-init-hook 'frame-initialize)
156 ;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
158 ;;; If we create the initial frame, this is it.
159 (defvar frame-initial-frame nil)
161 ;; Record the parameters used in frame-initialize to make the initial frame.
162 (defvar frame-initial-frame-alist)
164 (defvar frame-initial-geometry-arguments nil)
166 ;;; startup.el calls this function before loading the user's init
167 ;;; file - if there is no frame with a minibuffer open now, create
168 ;;; one to display messages while loading the init file.
169 (defun frame-initialize ()
170 "Create an in initial frame if necessary."
171 ;; Are we actually running under a window system at all?
172 (if (and window-system (not noninteractive) (not (eq window-system 'pc)))
173 (progn
174 ;; Turn on special-display processing only if there's a window system.
175 (setq special-display-function 'special-display-popup-frame)
177 ;; If there is no frame with a minibuffer besides the terminal
178 ;; frame, then we need to create the opening frame. Make sure
179 ;; it has a minibuffer, but let initial-frame-alist omit the
180 ;; minibuffer spec.
181 (or (delq terminal-frame (minibuffer-frame-list))
182 (progn
183 (setq frame-initial-frame-alist
184 (append initial-frame-alist default-frame-alist nil))
185 (or (assq 'horizontal-scroll-bars frame-initial-frame-alist)
186 (setq frame-initial-frame-alist
187 (cons '(horizontal-scroll-bars . t)
188 frame-initial-frame-alist)))
189 (setq default-minibuffer-frame
190 (setq frame-initial-frame
191 (make-frame frame-initial-frame-alist)))
192 ;; Delete any specifications for window geometry parameters
193 ;; so that we won't reapply them in frame-notice-user-settings.
194 ;; It would be wrong to reapply them then,
195 ;; because that would override explicit user resizing.
196 (setq initial-frame-alist
197 (frame-remove-geometry-params initial-frame-alist))))
198 ;; At this point, we know that we have a frame open, so we
199 ;; can delete the terminal frame.
200 (delete-frame terminal-frame)
201 (setq terminal-frame nil))
203 ;; No, we're not running a window system. Use make-terminal-frame if
204 ;; we support that feature, otherwise arrange to cause errors.
205 (or (eq window-system 'pc)
206 (setq frame-creation-function
207 (if (fboundp 'tty-create-frame-with-faces)
208 'tty-create-frame-with-faces
209 (function
210 (lambda (parameters)
211 (error
212 "Can't create multiple frames without a window system"))))))))
214 (defvar frame-notice-user-settings t
215 "Non-nil means function `frame-notice-user-settings' wasn't run yet.")
217 ;;; startup.el calls this function after loading the user's init
218 ;;; file. Now default-frame-alist and initial-frame-alist contain
219 ;;; information to which we must react; do what needs to be done.
220 (defun frame-notice-user-settings ()
221 "Act on user's init file settings of frame parameters.
222 React to settings of `default-frame-alist', `initial-frame-alist' there."
223 ;; Make menu-bar-mode and default-frame-alist consistent.
224 (when (boundp 'menu-bar-mode)
225 (let ((default (assq 'menu-bar-lines default-frame-alist)))
226 (if default
227 (setq menu-bar-mode (not (eq (cdr default) 0)))
228 (setq default-frame-alist
229 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
230 default-frame-alist)))))
232 ;; Make tool-bar-mode and default-frame-alist consistent. Don't do
233 ;; it in batch mode since that would leave a tool-bar-lines
234 ;; parameter in default-frame-alist in a dumped Emacs, which is not
235 ;; what we want. For some reason, menu-bar-mode is not bound
236 ;; in this case, but tool-bar-mode is.
237 (when (and (boundp 'tool-bar-mode)
238 (not noninteractive))
239 (let ((default (assq 'tool-bar-lines default-frame-alist)))
240 (if default
241 (setq tool-bar-mode (not (eq (cdr default) 0)))
242 (setq default-frame-alist
243 (cons (cons 'tool-bar-lines (if tool-bar-mode 1 0))
244 default-frame-alist)))))
246 ;; Creating and deleting frames may shift the selected frame around,
247 ;; and thus the current buffer. Protect against that. We don't
248 ;; want to use save-excursion here, because that may also try to set
249 ;; the buffer of the selected window, which fails when the selected
250 ;; window is the minibuffer.
251 (let ((old-buffer (current-buffer)))
253 (when (and frame-notice-user-settings
254 (null frame-initial-frame))
255 ;; This case happens when we don't have a window system, and
256 ;; also for MS-DOS frames.
257 (let ((parms (frame-parameters frame-initial-frame)))
258 ;; Don't change the frame names.
259 (setq parms (delq (assq 'name parms) parms))
260 ;; Can't modify the minibuffer parameter, so don't try.
261 (setq parms (delq (assq 'minibuffer parms) parms))
262 (modify-frame-parameters nil
263 (if (null window-system)
264 (append initial-frame-alist
265 default-frame-alist
266 parms
267 nil)
268 ;; initial-frame-alist and
269 ;; default-frame-alist were already
270 ;; applied in pc-win.el.
271 parms))
272 (if (null window-system) ;; MS-DOS does this differently in pc-win.el
273 (let ((newparms (frame-parameters))
274 (frame (selected-frame)))
275 (tty-handle-reverse-video frame newparms)
276 ;; If we changed the background color, we need to update
277 ;; the background-mode parameter, and maybe some faces,
278 ;; too.
279 (when (assq 'background-color newparms)
280 (unless (or (assq 'background-mode initial-frame-alist)
281 (assq 'background-mode default-frame-alist))
282 (frame-set-background-mode frame))
283 (face-set-after-frame-default frame))))))
285 ;; If the initial frame is still around, apply initial-frame-alist
286 ;; and default-frame-alist to it.
287 (if (frame-live-p frame-initial-frame)
289 ;; The initial frame we create above always has a minibuffer.
290 ;; If the user wants to remove it, or make it a minibuffer-only
291 ;; frame, then we'll have to delete the current frame and make a
292 ;; new one; you can't remove or add a root window to/from an
293 ;; existing frame.
295 ;; NOTE: default-frame-alist was nil when we created the
296 ;; existing frame. We need to explicitly include
297 ;; default-frame-alist in the parameters of the screen we
298 ;; create here, so that its new value, gleaned from the user's
299 ;; .emacs file, will be applied to the existing screen.
300 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
301 (assq 'minibuffer default-frame-alist)
302 '(minibuffer . t)))
304 ;; Create the new frame.
305 (let (parms new)
306 ;; If the frame isn't visible yet, wait till it is.
307 ;; If the user has to position the window,
308 ;; Emacs doesn't know its real position until
309 ;; the frame is seen to be visible.
310 (while (not (cdr (assq 'visibility
311 (frame-parameters frame-initial-frame))))
312 (sleep-for 1))
313 (setq parms (frame-parameters frame-initial-frame))
314 ;; Get rid of `name' unless it was specified explicitly before.
315 (or (assq 'name frame-initial-frame-alist)
316 (setq parms (delq (assq 'name parms) parms)))
317 (setq parms (append initial-frame-alist
318 default-frame-alist
319 parms
320 nil))
321 ;; Get rid of `reverse', because that was handled
322 ;; when we first made the frame.
323 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
324 (if (assq 'height frame-initial-geometry-arguments)
325 (setq parms (assq-delete-all 'height parms)))
326 (if (assq 'width frame-initial-geometry-arguments)
327 (setq parms (assq-delete-all 'width parms)))
328 (if (assq 'left frame-initial-geometry-arguments)
329 (setq parms (assq-delete-all 'left parms)))
330 (if (assq 'top frame-initial-geometry-arguments)
331 (setq parms (assq-delete-all 'top parms)))
332 (setq new
333 (make-frame
334 ;; Use the geometry args that created the existing
335 ;; frame, rather than the parms we get for it.
336 (append frame-initial-geometry-arguments
337 '((user-size . t) (user-position . t))
338 parms)))
339 ;; The initial frame, which we are about to delete, may be
340 ;; the only frame with a minibuffer. If it is, create a
341 ;; new one.
342 (or (delq frame-initial-frame (minibuffer-frame-list))
343 (make-initial-minibuffer-frame nil))
345 ;; If the initial frame is serving as a surrogate
346 ;; minibuffer frame for any frames, we need to wean them
347 ;; onto a new frame. The default-minibuffer-frame
348 ;; variable must be handled similarly.
349 (let ((users-of-initial
350 (filtered-frame-list
351 (function (lambda (frame)
352 (and (not (eq frame frame-initial-frame))
353 (eq (window-frame
354 (minibuffer-window frame))
355 frame-initial-frame)))))))
356 (if (or users-of-initial
357 (eq default-minibuffer-frame frame-initial-frame))
359 ;; Choose an appropriate frame. Prefer frames which
360 ;; are only minibuffers.
361 (let* ((new-surrogate
362 (car
363 (or (filtered-frame-list
364 (function
365 (lambda (frame)
366 (eq (cdr (assq 'minibuffer
367 (frame-parameters frame)))
368 'only))))
369 (minibuffer-frame-list))))
370 (new-minibuffer (minibuffer-window new-surrogate)))
372 (if (eq default-minibuffer-frame frame-initial-frame)
373 (setq default-minibuffer-frame new-surrogate))
375 ;; Wean the frames using frame-initial-frame as
376 ;; their minibuffer frame.
377 (mapcar
378 (function
379 (lambda (frame)
380 (modify-frame-parameters
381 frame (list (cons 'minibuffer new-minibuffer)))))
382 users-of-initial))))
384 ;; Redirect events enqueued at this frame to the new frame.
385 ;; Is this a good idea?
386 (redirect-frame-focus frame-initial-frame new)
388 ;; Finally, get rid of the old frame.
389 (delete-frame frame-initial-frame t))
391 ;; Otherwise, we don't need all that rigamarole; just apply
392 ;; the new parameters.
393 (let (newparms allparms tail)
394 (setq allparms (append initial-frame-alist
395 default-frame-alist nil))
396 (if (assq 'height frame-initial-geometry-arguments)
397 (setq allparms (assq-delete-all 'height allparms)))
398 (if (assq 'width frame-initial-geometry-arguments)
399 (setq allparms (assq-delete-all 'width allparms)))
400 (if (assq 'left frame-initial-geometry-arguments)
401 (setq allparms (assq-delete-all 'left allparms)))
402 (if (assq 'top frame-initial-geometry-arguments)
403 (setq allparms (assq-delete-all 'top allparms)))
404 (setq tail allparms)
405 ;; Find just the parms that have changed since we first
406 ;; made this frame. Those are the ones actually set by
407 ;; the init file. For those parms whose values we already knew
408 ;; (such as those spec'd by command line options)
409 ;; it is undesirable to specify the parm again
410 ;; once the user has seen the frame and been able to alter it
411 ;; manually.
412 (while tail
413 (let (newval oldval)
414 (setq oldval (assq (car (car tail))
415 frame-initial-frame-alist))
416 (setq newval (cdr (assq (car (car tail)) allparms)))
417 (or (and oldval (eq (cdr oldval) newval))
418 (setq newparms
419 (cons (cons (car (car tail)) newval) newparms))))
420 (setq tail (cdr tail)))
421 (setq newparms (nreverse newparms))
422 (modify-frame-parameters frame-initial-frame
423 newparms)
424 ;; If we changed the background color,
425 ;; we need to update the background-mode parameter
426 ;; and maybe some faces too.
427 (when (assq 'background-color newparms)
428 (unless (assq 'background-mode newparms)
429 (frame-set-background-mode frame-initial-frame))
430 (face-set-after-frame-default frame-initial-frame)))))
432 ;; Restore the original buffer.
433 (set-buffer old-buffer)
435 ;; Make sure the initial frame can be GC'd if it is ever deleted.
436 ;; Make sure frame-notice-user-settings does nothing if called twice.
437 (setq frame-notice-user-settings nil)
438 (setq frame-initial-frame nil)))
440 (defun make-initial-minibuffer-frame (display)
441 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
442 (if display
443 (make-frame-on-display display parms)
444 (make-frame parms))))
446 ;;;; Creation of additional frames, and other frame miscellanea
448 (defun get-other-frame ()
449 "Return some frame other than the current frame.
450 Create one if necessary. Note that the minibuffer frame, if separate,
451 is not considered (see `next-frame')."
452 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
453 (make-frame)
454 (next-frame (selected-frame)))))
457 (defun next-multiframe-window ()
458 "Select the next window, regardless of which frame it is on."
459 (interactive)
460 (select-window (next-window (selected-window)
461 (> (minibuffer-depth) 0)
462 t)))
464 (defun previous-multiframe-window ()
465 "Select the previous window, regardless of which frame it is on."
466 (interactive)
467 (select-window (previous-window (selected-window)
468 (> (minibuffer-depth) 0)
469 t)))
471 (defun make-frame-on-display (display &optional parameters)
472 "Make a frame on display DISPLAY.
473 The optional second argument PARAMETERS specifies additional frame parameters."
474 (interactive "sMake frame on display: ")
475 (or (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'" display)
476 (error "Invalid display, not HOST:SERVER or HOST:SERVER.SCREEN"))
477 (make-frame (cons (cons 'display display) parameters)))
479 (defun make-frame-command ()
480 "Make a new frame, and select it if the terminal displays only one frame."
481 (interactive)
482 (if (and window-system (not (eq window-system 'pc)))
483 (make-frame)
484 (select-frame (make-frame))))
486 (defvar before-make-frame-hook nil
487 "Functions to run before a frame is created.")
489 (defvar after-make-frame-functions nil
490 "Functions to run after a frame is created.
491 The functions are run with one arg, the newly created frame.")
493 (defvar after-setting-font-hook nil
494 "Functions to run after a frame's font has been changed.")
496 ;; Alias, kept temporarily.
497 (defalias 'new-frame 'make-frame)
499 (defun make-frame (&optional parameters)
500 "Return a newly created frame displaying the current buffer.
501 Optional argument PARAMETERS is an alist of parameters for the new frame.
502 Each element of PARAMETERS should have the form (NAME . VALUE), for example:
504 (name . STRING) The frame should be named STRING.
506 (width . NUMBER) The frame should be NUMBER characters in width.
507 (height . NUMBER) The frame should be NUMBER text lines high.
509 You cannot specify either `width' or `height', you must use neither or both.
511 (minibuffer . t) The frame should have a minibuffer.
512 (minibuffer . nil) The frame should have no minibuffer.
513 (minibuffer . only) The frame should contain only a minibuffer.
514 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
516 Before the frame is created (via `frame-creation-function'), functions on the
517 hook `before-make-frame-hook' are run. After the frame is created, functions
518 on `after-make-frame-functions' are run with one arg, the newly created frame."
519 (interactive)
520 (run-hooks 'before-make-frame-hook)
521 (let ((frame (funcall frame-creation-function parameters)))
522 (run-hook-with-args 'after-make-frame-functions frame)
523 frame))
525 (defun filtered-frame-list (predicate)
526 "Return a list of all live frames which satisfy PREDICATE."
527 (let* ((frames (frame-list))
528 (list frames))
529 (while (consp frames)
530 (unless (funcall predicate (car frames))
531 (setcar frames nil))
532 (setq frames (cdr frames)))
533 (delq nil list)))
535 (defun minibuffer-frame-list ()
536 "Return a list of all frames with their own minibuffers."
537 (filtered-frame-list
538 (function (lambda (frame)
539 (eq frame (window-frame (minibuffer-window frame)))))))
541 (defun frames-on-display-list (&optional display)
542 "Return a list of all frames on DISPLAY.
543 DISPLAY is a name of a display, a string of the form HOST:SERVER.SCREEN.
544 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
545 (let* ((display (or display (frame-parameter nil 'display)))
546 (func #'(lambda (frame)
547 (eq (frame-parameter frame 'display) display))))
548 (filtered-frame-list func)))
550 (defun framep-on-display (&optional display)
551 "Return the type of frames on DISPLAY.
552 DISPLAY may be a display name or a frame. If it is a frame, its type is
553 returned.
554 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
555 All frames on a given display are of the same type."
556 (or (framep display)
557 (framep (car (frames-on-display-list display)))))
559 (defun frame-remove-geometry-params (param-list)
560 "Return the parameter list PARAM-LIST, but with geometry specs removed.
561 This deletes all bindings in PARAM-LIST for `top', `left', `width',
562 `height', `user-size' and `user-position' parameters.
563 Emacs uses this to avoid overriding explicit moves and resizings from
564 the user during startup."
565 (setq param-list (cons nil param-list))
566 (let ((tail param-list))
567 (while (consp (cdr tail))
568 (if (and (consp (car (cdr tail)))
569 (memq (car (car (cdr tail)))
570 '(height width top left user-position user-size)))
571 (progn
572 (setq frame-initial-geometry-arguments
573 (cons (car (cdr tail)) frame-initial-geometry-arguments))
574 (setcdr tail (cdr (cdr tail))))
575 (setq tail (cdr tail)))))
576 (setq frame-initial-geometry-arguments
577 (nreverse frame-initial-geometry-arguments))
578 (cdr param-list))
581 (defcustom focus-follows-mouse t
582 "*Non-nil if window system changes focus when you move the mouse."
583 :type 'boolean
584 :group 'frames
585 :version "20.3")
587 (defun other-frame (arg)
588 "Select the ARG'th different visible frame, and raise it.
589 All frames are arranged in a cyclic order.
590 This command selects the frame ARG steps away in that order.
591 A negative ARG moves in the opposite order."
592 (interactive "p")
593 (let ((frame (selected-frame)))
594 (while (> arg 0)
595 (setq frame (next-frame frame))
596 (while (not (eq (frame-visible-p frame) t))
597 (setq frame (next-frame frame)))
598 (setq arg (1- arg)))
599 (while (< arg 0)
600 (setq frame (previous-frame frame))
601 (while (not (eq (frame-visible-p frame) t))
602 (setq frame (previous-frame frame)))
603 (setq arg (1+ arg)))
604 (select-frame frame)
605 (raise-frame frame)
606 ;; Ensure, if possible, that frame gets input focus.
607 (when (eq window-system 'w32)
608 (w32-focus-frame frame))
609 (cond (focus-follows-mouse
610 (unless (eq window-system 'w32)
611 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))
613 (when (eq window-system 'x)
614 (x-focus-frame frame))))))
616 (defun make-frame-names-alist ()
617 (let* ((current-frame (selected-frame))
618 (falist
619 (cons
620 (cons (frame-parameter current-frame 'name) current-frame) nil))
621 (frame (next-frame nil t)))
622 (while (not (eq frame current-frame))
623 (progn
624 (setq falist (cons (cons (frame-parameter frame 'name) frame) falist))
625 (setq frame (next-frame frame t))))
626 falist))
628 (defvar frame-name-history nil)
629 (defun select-frame-by-name (name)
630 "Select the frame whose name is NAME and raise it.
631 If there is no frame by that name, signal an error."
632 (interactive
633 (let* ((frame-names-alist (make-frame-names-alist))
634 (default (car (car frame-names-alist)))
635 (input (completing-read
636 (format "Select Frame (default %s): " default)
637 frame-names-alist nil t nil 'frame-name-history)))
638 (if (= (length input) 0)
639 (list default)
640 (list input))))
641 (let* ((frame-names-alist (make-frame-names-alist))
642 (frame (cdr (assoc name frame-names-alist))))
643 (or frame
644 (error "There is no frame named `%s'" name))
645 (make-frame-visible frame)
646 (raise-frame frame)
647 (select-frame frame)
648 ;; Ensure, if possible, that frame gets input focus.
649 (if (eq window-system 'w32)
650 (w32-focus-frame frame)
651 (when focus-follows-mouse
652 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))))
654 ;;;; Frame configurations
656 (defun current-frame-configuration ()
657 "Return a list describing the positions and states of all frames.
658 Its car is `frame-configuration'.
659 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
660 where
661 FRAME is a frame object,
662 ALIST is an association list specifying some of FRAME's parameters, and
663 WINDOW-CONFIG is a window configuration object for FRAME."
664 (cons 'frame-configuration
665 (mapcar (function
666 (lambda (frame)
667 (list frame
668 (frame-parameters frame)
669 (current-window-configuration frame))))
670 (frame-list))))
672 (defun set-frame-configuration (configuration &optional nodelete)
673 "Restore the frames to the state described by CONFIGURATION.
674 Each frame listed in CONFIGURATION has its position, size, window
675 configuration, and other parameters set as specified in CONFIGURATION.
676 Ordinarily, this function deletes all existing frames not
677 listed in CONFIGURATION. But if optional second argument NODELETE
678 is given and non-nil, the unwanted frames are iconified instead."
679 (or (frame-configuration-p configuration)
680 (signal 'wrong-type-argument
681 (list 'frame-configuration-p configuration)))
682 (let ((config-alist (cdr configuration))
683 frames-to-delete)
684 (mapcar (function
685 (lambda (frame)
686 (let ((parameters (assq frame config-alist)))
687 (if parameters
688 (progn
689 (modify-frame-parameters
690 frame
691 ;; Since we can't set a frame's minibuffer status,
692 ;; we might as well omit the parameter altogether.
693 (let* ((parms (nth 1 parameters))
694 (mini (assq 'minibuffer parms)))
695 (if mini (setq parms (delq mini parms)))
696 parms))
697 (set-window-configuration (nth 2 parameters)))
698 (setq frames-to-delete (cons frame frames-to-delete))))))
699 (frame-list))
700 (if nodelete
701 ;; Note: making frames invisible here was tried
702 ;; but led to some strange behavior--each time the frame
703 ;; was made visible again, the window manager asked afresh
704 ;; for where to put it.
705 (mapcar 'iconify-frame frames-to-delete)
706 (mapcar 'delete-frame frames-to-delete))))
708 ;;;; Convenience functions for accessing and interactively changing
709 ;;;; frame parameters.
711 (defun frame-height (&optional frame)
712 "Return number of lines available for display on FRAME.
713 If FRAME is omitted, describe the currently selected frame."
714 (cdr (assq 'height (frame-parameters frame))))
716 (defun frame-width (&optional frame)
717 "Return number of columns available for display on FRAME.
718 If FRAME is omitted, describe the currently selected frame."
719 (cdr (assq 'width (frame-parameters frame))))
721 (defalias 'set-default-font 'set-frame-font)
722 (defun set-frame-font (font-name)
723 "Set the font of the selected frame to FONT-NAME.
724 When called interactively, prompt for the name of the font to use.
725 To get the frame's current default font, use `frame-parameters'."
726 (interactive
727 (list
728 (let ((completion-ignore-case t))
729 (completing-read "Font name: "
730 (mapcar #'list
731 ;; x-list-fonts will fail with an error
732 ;; if this frame doesn't support fonts.
733 (x-list-fonts "*" nil (selected-frame)))))))
734 (modify-frame-parameters (selected-frame)
735 (list (cons 'font font-name)))
736 (run-hooks 'after-setting-font-hook 'after-setting-font-hooks))
738 (defun set-background-color (color-name)
739 "Set the background color of the selected frame to COLOR-NAME.
740 When called interactively, prompt for the name of the color to use.
741 To get the frame's current background color, use `frame-parameters'."
742 (interactive (list (facemenu-read-color)))
743 (modify-frame-parameters (selected-frame)
744 (list (cons 'background-color color-name))))
746 (defun set-foreground-color (color-name)
747 "Set the foreground color of the selected frame to COLOR-NAME.
748 When called interactively, prompt for the name of the color to use.
749 To get the frame's current foreground color, use `frame-parameters'."
750 (interactive (list (facemenu-read-color)))
751 (modify-frame-parameters (selected-frame)
752 (list (cons 'foreground-color color-name))))
754 (defun set-cursor-color (color-name)
755 "Set the text cursor color of the selected frame to COLOR-NAME.
756 When called interactively, prompt for the name of the color to use.
757 To get the frame's current cursor color, use `frame-parameters'."
758 (interactive (list (facemenu-read-color)))
759 (modify-frame-parameters (selected-frame)
760 (list (cons 'cursor-color color-name))))
762 (defun set-mouse-color (color-name)
763 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
764 When called interactively, prompt for the name of the color to use.
765 To get the frame's current mouse color, use `frame-parameters'."
766 (interactive (list (facemenu-read-color)))
767 (modify-frame-parameters (selected-frame)
768 (list (cons 'mouse-color
769 (or color-name
770 (cdr (assq 'mouse-color
771 (frame-parameters))))))))
773 (defun set-border-color (color-name)
774 "Set the color of the border of the selected frame to COLOR-NAME.
775 When called interactively, prompt for the name of the color to use.
776 To get the frame's current border color, use `frame-parameters'."
777 (interactive (list (facemenu-read-color)))
778 (modify-frame-parameters (selected-frame)
779 (list (cons 'border-color color-name))))
781 (defun auto-raise-mode (arg)
782 "Toggle whether or not the selected frame should auto-raise.
783 With arg, turn auto-raise mode on if and only if arg is positive.
784 Note that this controls Emacs's own auto-raise feature.
785 Some window managers allow you to enable auto-raise for certain windows.
786 You can use that for Emacs windows if you wish, but if you do,
787 that is beyond the control of Emacs and this command has no effect on it."
788 (interactive "P")
789 (if (null arg)
790 (setq arg
791 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
792 -1 1)))
793 (if (> arg 0)
794 (raise-frame (selected-frame)))
795 (modify-frame-parameters (selected-frame)
796 (list (cons 'auto-raise (> arg 0)))))
798 (defun auto-lower-mode (arg)
799 "Toggle whether or not the selected frame should auto-lower.
800 With arg, turn auto-lower mode on if and only if arg is positive.
801 Note that this controls Emacs's own auto-lower feature.
802 Some window managers allow you to enable auto-lower for certain windows.
803 You can use that for Emacs windows if you wish, but if you do,
804 that is beyond the control of Emacs and this command has no effect on it."
805 (interactive "P")
806 (if (null arg)
807 (setq arg
808 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
809 -1 1)))
810 (modify-frame-parameters (selected-frame)
811 (list (cons 'auto-lower (> arg 0)))))
812 (defun set-frame-name (name)
813 "Set the name of the selected frame to NAME.
814 When called interactively, prompt for the name of the frame.
815 The frame name is displayed on the modeline if the terminal displays only
816 one frame, otherwise the name is displayed on the frame's caption bar."
817 (interactive "sFrame name: ")
818 (modify-frame-parameters (selected-frame)
819 (list (cons 'name name))))
821 ;;;; Frame/display capabilities.
822 (defun display-mouse-p (&optional display)
823 "Return non-nil if DISPLAY has a mouse available.
824 DISPLAY can be a display name, a frame, or nil (meaning the selected
825 frame's display)."
826 (let ((frame-type (framep-on-display display)))
827 (cond
828 ((eq frame-type 'pc)
829 (msdos-mouse-p))
830 ((eq system-type 'windows-nt)
831 (> w32-num-mouse-buttons 0))
832 ((memq frame-type '(x mac))
833 t) ;; We assume X and Mac *always* have a pointing device
835 (or (and (featurep 'xt-mouse)
836 xterm-mouse-mode)
837 ;; t-mouse is distributed with the GPM package. It doesn't have
838 ;; a toggle.
839 (featurep 't-mouse))))))
841 (defun display-popup-menus-p (&optional display)
842 "Return non-nil if popup menus are supported on DISPLAY.
843 DISPLAY can be a display name, a frame, or nil (meaning the selected
844 frame's display).
845 Support for popup menus requires that the mouse be available."
846 (and
847 (let ((frame-type (framep-on-display display)))
848 (memq frame-type '(x w32 pc mac)))
849 (display-mouse-p display)))
851 (defun display-graphic-p (&optional display)
852 "Return non-nil if DISPLAY is a graphic display.
853 Graphical displays are those which are capable of displaying several
854 frames and several different fonts at once. This is true for displays
855 that use a window system such as X, and false for text-only terminals.
856 DISPLAY can be a display name, a frame, or nil (meaning the selected
857 frame's display)."
858 (not (null (memq (framep-on-display display) '(x w32 mac)))))
860 (defalias 'display-multi-frame-p 'display-graphic-p)
861 (defalias 'display-multi-font-p 'display-graphic-p)
863 (defun display-selections-p (&optional display)
864 "Return non-nil if DISPLAY supports selections.
865 A selection is a way to transfer text or other data between programs
866 via special system buffers called `selection' or `cut buffer' or
867 `clipboard'.
868 DISPLAY can be a display name, a frame, or nil (meaning the selected
869 frame's display)."
870 (let ((frame-type (framep-on-display display)))
871 (cond
872 ((eq frame-type 'pc)
873 ;; MS-DOG frames support selections when Emacs runs inside
874 ;; the Windows' DOS Box.
875 (not (null dos-windows-version)))
876 ((memq frame-type '(x w32 mac))
877 t) ;; FIXME?
879 nil))))
881 (defun display-screens (&optional display)
882 "Return the number of screens associated with DISPLAY."
883 (let ((frame-type (framep-on-display display)))
884 (cond
885 ((memq frame-type '(x w32))
886 (x-display-screens display))
887 (t ;; FIXME: is this correct for the Mac?
888 1))))
890 (defun display-pixel-height (&optional display)
891 "Return the height of DISPLAY's screen in pixels.
892 For character terminals, each character counts as a single pixel."
893 (let ((frame-type (framep-on-display display)))
894 (cond
895 ((memq frame-type '(x w32 mac))
896 (x-display-pixel-height display))
898 (frame-height (if (framep display) display (selected-frame)))))))
900 (defun display-pixel-width (&optional display)
901 "Return the width of DISPLAY's screen in pixels.
902 For character terminals, each character counts as a single pixel."
903 (let ((frame-type (framep-on-display display)))
904 (cond
905 ((memq frame-type '(x w32 mac))
906 (x-display-pixel-width display))
908 (frame-width (if (framep display) display (selected-frame)))))))
910 (defun display-mm-height (&optional display)
911 "Return the height of DISPLAY's screen in millimeters.
912 If the information is unavailable, value is nil."
913 (and (memq (framep-on-display display) '(x w32 mac))
914 (x-display-mm-height display)))
916 (defun display-mm-width (&optional display)
917 "Return the width of DISPLAY's screen in millimeters.
918 If the information is unavailable, value is nil."
919 (and (memq (framep-on-display display) '(x w32 mac))
920 (x-display-mm-width display)))
922 (defun display-backing-store (&optional display)
923 "Return the backing store capability of DISPLAY's screen.
924 The value may be `always', `when-mapped', `not-useful', or nil if
925 the question is inapplicable to a certain kind of display."
926 (let ((frame-type (framep-on-display display)))
927 (cond
928 ((memq frame-type '(x w32 mac))
929 (x-display-backing-store display))
931 'not-useful))))
933 (defun display-save-under (&optional display)
934 "Return non-nil if DISPLAY's screen supports the SaveUnder feature."
935 (let ((frame-type (framep-on-display display)))
936 (cond
937 ((memq frame-type '(x w32 mac))
938 (x-display-save-under display))
940 'not-useful))))
942 (defun display-planes (&optional display)
943 "Return the number of planes supported by DISPLAY."
944 (let ((frame-type (framep-on-display display)))
945 (cond
946 ((memq frame-type '(x w32 mac))
947 (x-display-planes display))
948 ((eq frame-type 'pc)
951 (truncate (log (length (tty-color-alist)) 2))))))
953 (defun display-color-cells (&optional display)
954 "Return the number of color cells supported by DISPLAY."
955 (let ((frame-type (framep-on-display display)))
956 (cond
957 ((memq frame-type '(x w32 mac))
958 (x-display-color-cells display))
959 ((eq frame-type 'pc)
962 (length (tty-color-alist))))))
964 (defun display-visual-class (&optional display)
965 "Returns the visual class of DISPLAY.
966 The value is one of the symbols `static-gray', `gray-scale',
967 `static-color', `pseudo-color', `true-color', or `direct-color'."
968 (let ((frame-type (framep-on-display display)))
969 (cond
970 ((memq frame-type '(x w32 mac))
971 (x-display-visual-class display))
972 ((and (memq frame-type '(pc t))
973 (tty-display-color-p display))
974 'static-color)
976 'static-gray))))
979 ;;;; Aliases for backward compatibility with Emacs 18.
980 (defalias 'screen-height 'frame-height)
981 (defalias 'screen-width 'frame-width)
983 (defun set-screen-width (cols &optional pretend)
984 "Obsolete function to change the size of the screen to COLS columns.
985 Optional second arg non-nil means that redisplay should use COLS columns
986 but that the idea of the actual width of the frame should not be changed.
987 This function is provided only for compatibility with Emacs 18; new code
988 should use `set-frame-width instead'."
989 (set-frame-width (selected-frame) cols pretend))
991 (defun set-screen-height (lines &optional pretend)
992 "Obsolete function to change the height of the screen to LINES lines.
993 Optional second arg non-nil means that redisplay should use LINES lines
994 but that the idea of the actual height of the screen should not be changed.
995 This function is provided only for compatibility with Emacs 18; new code
996 should use `set-frame-height' instead."
997 (set-frame-height (selected-frame) lines pretend))
999 (defun delete-other-frames (&optional frame)
1000 "Delete all frames except FRAME.
1001 FRAME nil or omitted means delete all frames except the selected frame."
1002 (interactive)
1003 (unless frame
1004 (setq frame (selected-frame)))
1005 (mapcar 'delete-frame (delq frame (frame-list))))
1008 (make-obsolete 'screen-height 'frame-height) ;before 19.15
1009 (make-obsolete 'screen-width 'frame-width) ;before 19.15
1010 (make-obsolete 'set-screen-width 'set-frame-width) ;before 19.15
1011 (make-obsolete 'set-screen-height 'set-frame-height) ;before 19.15
1014 ;;; Highlighting trailing whitespace.
1016 (make-variable-buffer-local 'show-trailing-whitespace)
1018 (defcustom show-trailing-whitespace nil
1019 "*Non-nil means highlight trailing whitespace in face `trailing-whitespace'."
1020 :tag "Highlight trailing whitespace."
1021 :set #'(lambda (symbol value) (set-default symbol value))
1022 :type 'boolean
1023 :group 'font-lock)
1027 ;;; Scrolling
1029 (defgroup scrolling nil
1030 "Scrolling windows."
1031 :version "21.1"
1032 :group 'frames)
1034 (defcustom automatic-hscrolling t
1035 "*Allow or disallow autmatic scrolling windows horizontally.
1036 If non-nil, windows are automatically scrolled horizontally to make
1037 point visible."
1038 :version "21.1"
1039 :type 'boolean
1040 :group 'scrolling)
1043 ;;; Blinking cursor
1045 (defgroup cursor nil
1046 "Displaying text cursors."
1047 :version "21.1"
1048 :group 'frames)
1050 (defcustom blink-cursor-delay 0.5
1051 "*Seconds of idle time after which cursor starts to blink."
1052 :tag "Delay in seconds."
1053 :type 'number
1054 :group 'cursor)
1056 (defcustom blink-cursor-interval 0.5
1057 "*Length of cursor blink interval in seconds."
1058 :tag "Blink interval in seconds."
1059 :type 'number
1060 :group 'cursor)
1062 (defvar blink-cursor-idle-timer nil
1063 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
1064 The function `blink-cursor-start' is called when the timer fires.")
1066 (defvar blink-cursor-timer nil
1067 "Timer started from `blink-cursor-start'.
1068 This timer calls `blink-cursor' every `blink-cursor-interval' seconds.")
1070 (defvar blink-cursor-mode nil
1071 "Non-nil means blinking cursor is active.")
1073 (defun blink-cursor-mode (arg)
1074 "Toggle blinking cursor mode.
1075 With arg, turn blinking cursor mode on iff arg is positive.
1076 When blinking cursor mode is enabled, the cursor of the selected
1077 window blinks."
1078 (interactive "P")
1079 (let ((on-p (if (null arg)
1080 (not blink-cursor-mode)
1081 (> (prefix-numeric-value arg) 0))))
1082 (if blink-cursor-idle-timer
1083 (cancel-timer blink-cursor-idle-timer))
1084 (if blink-cursor-timer
1085 (cancel-timer blink-cursor-timer))
1086 (setq blink-cursor-idle-timer nil
1087 blink-cursor-timer nil
1088 blink-cursor-mode nil)
1089 (if on-p
1090 (progn
1091 ;; Hide the cursor.
1092 ;(internal-show-cursor nil nil)
1093 (setq blink-cursor-idle-timer
1094 (run-with-idle-timer blink-cursor-delay
1095 blink-cursor-delay
1096 'blink-cursor-start))
1097 (setq blink-cursor-mode t))
1098 (internal-show-cursor nil t))))
1100 ;; Note that this is really initialized from startup.el before
1101 ;; the init-file is read.
1103 (defcustom blink-cursor nil
1104 "*Non-nil means blinking cursor mode is active."
1105 :group 'cursor
1106 :tag "Blinking cursor"
1107 :type 'boolean
1108 :set #'(lambda (symbol value)
1109 (set-default symbol value)
1110 (blink-cursor-mode (or value 0))))
1112 (defun blink-cursor-start ()
1113 "Timer function called from the timer `blink-cursor-idle-timer'.
1114 This starts the timer `blink-cursor-timer', which makes the cursor blink
1115 if appropriate. It also arranges to cancel that timer when the next
1116 command starts, by installing a pre-command hook."
1117 (when (null blink-cursor-timer)
1118 (add-hook 'pre-command-hook 'blink-cursor-end)
1119 (setq blink-cursor-timer
1120 (run-with-timer blink-cursor-interval blink-cursor-interval
1121 'blink-cursor-timer-function))))
1123 (defun blink-cursor-timer-function ()
1124 "Timer function of timer `blink-cursor-timer'."
1125 (internal-show-cursor nil (not (internal-show-cursor-p))))
1127 (defun blink-cursor-end ()
1128 "Stop cursor blinking.
1129 This is installed as a pre-command hook by `blink-cursor-start'.
1130 When run, it cancels the timer `blink-cursor-timer' and removes
1131 itself as a pre-command hook."
1132 (remove-hook 'pre-command-hook 'blink-cursor-end)
1133 (internal-show-cursor nil t)
1134 (cancel-timer blink-cursor-timer)
1135 (setq blink-cursor-timer nil))
1139 ;;; Busy-cursor.
1141 (defcustom busy-cursor t
1142 "*Non-nil means show a busy-cursor when running under a window system."
1143 :tag "Busy-cursor"
1144 :type 'boolean
1145 :group 'cursor
1146 :get #'(lambda (symbol) display-busy-cursor)
1147 :set #'(lambda (symbol value)
1148 (set-default symbol value)
1149 (setq display-busy-cursor value)))
1151 (defcustom busy-cursor-delay-seconds 1
1152 "*Seconds to wait before displaying a busy-cursor."
1153 :tag "Busy-cursor delay"
1154 :type 'number
1155 :group 'cursor
1156 :get #'(lambda (symbol) busy-cursor-delay)
1157 :set #'(lambda (symbol value)
1158 (set-default symbol value)
1159 (setq busy-cursor-delay value)))
1162 (defcustom show-cursor-in-non-selected-windows t
1163 "*Non-nil means show a hollow box cursor in non-selected-windows.
1164 If nil, don't show a cursor except in the selected window."
1165 :tag "Cursor in non-selected windows"
1166 :type 'boolean
1167 :group 'cursor
1168 :get #'(lambda (symbol) cursor-in-non-selected-windows)
1169 :set #'(lambda (symbol value)
1170 (set-default symbol value)
1171 (setq cursor-in-non-selected-windows value)
1172 (force-mode-line-update t)))
1175 ;;;; Key bindings
1177 (define-key ctl-x-5-map "2" 'make-frame-command)
1178 (define-key ctl-x-5-map "1" 'delete-other-frames)
1179 (define-key ctl-x-5-map "0" 'delete-frame)
1180 (define-key ctl-x-5-map "o" 'other-frame)
1182 (provide 'frame)
1184 ;;; frame.el ends here