(frame-notice-user-settings): Don't add a
[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 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: internal
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; 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.
24
25 ;;; Code:
26
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.")
31
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'.
40
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'.
44
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)
59
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)
71
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)
83
84 (setq pop-up-frame-function
85 (function (lambda ()
86 (make-frame pop-up-frame-alist))))
87
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)
101
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))))))
124
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))))
140 \f
141 ;;;; Arrangement of frames at startup
142
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.
146 ;;;
147 ;;; 2) If no frames have been opened, we open an initial text frame.
148 ;;;
149 ;;; 3) Once the init file is done, we apply any newly set parameters
150 ;;; in initial-frame-alist to the frame.
151
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)
157
158 ;;; If we create the initial frame, this is it.
159 (defvar frame-initial-frame nil)
160
161 ;; Record the parameters used in frame-initialize to make the initial frame.
162 (defvar frame-initial-frame-alist)
163
164 (defvar frame-initial-geometry-arguments nil)
165
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)
176
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))
202
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"))))))))
213
214 (defvar frame-notice-user-settings t
215 "Non-nil means function `frame-notice-user-settings' wasn't run yet.")
216
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)))))
231
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)))))
245
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)))
252
253 (when (and frame-notice-user-settings
254 (null frame-initial-frame))
255 ;; This case happens when we don't have a window system.
256 (let ((parms (frame-parameters frame-initial-frame)))
257 ;; Don't change the frame names.
258 (setq parms (delq (assq 'name parms) parms))
259 ;; Can't modify the minibuffer parameter, so don't try.
260 (setq parms (delq (assq 'minibuffer parms) parms))
261 (modify-frame-parameters nil
262 (append initial-frame-alist
263 default-frame-alist
264 parms
265 nil))))
266
267 ;; If the initial frame is still around, apply initial-frame-alist
268 ;; and default-frame-alist to it.
269 (if (frame-live-p frame-initial-frame)
270
271 ;; The initial frame we create above always has a minibuffer.
272 ;; If the user wants to remove it, or make it a minibuffer-only
273 ;; frame, then we'll have to delete the current frame and make a
274 ;; new one; you can't remove or add a root window to/from an
275 ;; existing frame.
276 ;;
277 ;; NOTE: default-frame-alist was nil when we created the
278 ;; existing frame. We need to explicitly include
279 ;; default-frame-alist in the parameters of the screen we
280 ;; create here, so that its new value, gleaned from the user's
281 ;; .emacs file, will be applied to the existing screen.
282 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
283 (assq 'minibuffer default-frame-alist)
284 '(minibuffer . t)))
285 t))
286 ;; Create the new frame.
287 (let (parms new)
288 ;; If the frame isn't visible yet, wait till it is.
289 ;; If the user has to position the window,
290 ;; Emacs doesn't know its real position until
291 ;; the frame is seen to be visible.
292 (while (not (cdr (assq 'visibility
293 (frame-parameters frame-initial-frame))))
294 (sleep-for 1))
295 (setq parms (frame-parameters frame-initial-frame))
296 ;; Get rid of `name' unless it was specified explicitly before.
297 (or (assq 'name frame-initial-frame-alist)
298 (setq parms (delq (assq 'name parms) parms)))
299 (setq parms (append initial-frame-alist
300 default-frame-alist
301 parms
302 nil))
303 ;; Get rid of `reverse', because that was handled
304 ;; when we first made the frame.
305 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
306 (if (assq 'height frame-initial-geometry-arguments)
307 (setq parms (assq-delete-all 'height parms)))
308 (if (assq 'width frame-initial-geometry-arguments)
309 (setq parms (assq-delete-all 'width parms)))
310 (if (assq 'left frame-initial-geometry-arguments)
311 (setq parms (assq-delete-all 'left parms)))
312 (if (assq 'top frame-initial-geometry-arguments)
313 (setq parms (assq-delete-all 'top parms)))
314 (setq new
315 (make-frame
316 ;; Use the geometry args that created the existing
317 ;; frame, rather than the parms we get for it.
318 (append frame-initial-geometry-arguments
319 '((user-size . t) (user-position . t))
320 parms)))
321 ;; The initial frame, which we are about to delete, may be
322 ;; the only frame with a minibuffer. If it is, create a
323 ;; new one.
324 (or (delq frame-initial-frame (minibuffer-frame-list))
325 (make-initial-minibuffer-frame nil))
326
327 ;; If the initial frame is serving as a surrogate
328 ;; minibuffer frame for any frames, we need to wean them
329 ;; onto a new frame. The default-minibuffer-frame
330 ;; variable must be handled similarly.
331 (let ((users-of-initial
332 (filtered-frame-list
333 (function (lambda (frame)
334 (and (not (eq frame frame-initial-frame))
335 (eq (window-frame
336 (minibuffer-window frame))
337 frame-initial-frame)))))))
338 (if (or users-of-initial
339 (eq default-minibuffer-frame frame-initial-frame))
340
341 ;; Choose an appropriate frame. Prefer frames which
342 ;; are only minibuffers.
343 (let* ((new-surrogate
344 (car
345 (or (filtered-frame-list
346 (function
347 (lambda (frame)
348 (eq (cdr (assq 'minibuffer
349 (frame-parameters frame)))
350 'only))))
351 (minibuffer-frame-list))))
352 (new-minibuffer (minibuffer-window new-surrogate)))
353
354 (if (eq default-minibuffer-frame frame-initial-frame)
355 (setq default-minibuffer-frame new-surrogate))
356
357 ;; Wean the frames using frame-initial-frame as
358 ;; their minibuffer frame.
359 (mapcar
360 (function
361 (lambda (frame)
362 (modify-frame-parameters
363 frame (list (cons 'minibuffer new-minibuffer)))))
364 users-of-initial))))
365
366 ;; Redirect events enqueued at this frame to the new frame.
367 ;; Is this a good idea?
368 (redirect-frame-focus frame-initial-frame new)
369
370 ;; Finally, get rid of the old frame.
371 (delete-frame frame-initial-frame t))
372
373 ;; Otherwise, we don't need all that rigamarole; just apply
374 ;; the new parameters.
375 (let (newparms allparms tail)
376 (setq allparms (append initial-frame-alist
377 default-frame-alist nil))
378 (if (assq 'height frame-initial-geometry-arguments)
379 (setq allparms (assq-delete-all 'height allparms)))
380 (if (assq 'width frame-initial-geometry-arguments)
381 (setq allparms (assq-delete-all 'width allparms)))
382 (if (assq 'left frame-initial-geometry-arguments)
383 (setq allparms (assq-delete-all 'left allparms)))
384 (if (assq 'top frame-initial-geometry-arguments)
385 (setq allparms (assq-delete-all 'top allparms)))
386 (setq tail allparms)
387 ;; Find just the parms that have changed since we first
388 ;; made this frame. Those are the ones actually set by
389 ;; the init file. For those parms whose values we already knew
390 ;; (such as those spec'd by command line options)
391 ;; it is undesirable to specify the parm again
392 ;; once the user has seen the frame and been able to alter it
393 ;; manually.
394 (while tail
395 (let (newval oldval)
396 (setq oldval (assq (car (car tail))
397 frame-initial-frame-alist))
398 (setq newval (cdr (assq (car (car tail)) allparms)))
399 (or (and oldval (eq (cdr oldval) newval))
400 (setq newparms
401 (cons (cons (car (car tail)) newval) newparms))))
402 (setq tail (cdr tail)))
403 (setq newparms (nreverse newparms))
404 (modify-frame-parameters frame-initial-frame
405 newparms)
406 ;; If we changed the background color,
407 ;; we need to update the background-mode parameter
408 ;; and maybe some faces too.
409 (when (assq 'background-color newparms)
410 (unless (assq 'background-mode newparms)
411 (frame-set-background-mode frame-initial-frame))
412 (face-set-after-frame-default frame-initial-frame))
413 (if (assq 'font newparms)
414 (frame-update-faces frame-initial-frame)))))
415
416 ;; Restore the original buffer.
417 (set-buffer old-buffer)
418
419 ;; Make sure the initial frame can be GC'd if it is ever deleted.
420 ;; Make sure frame-notice-user-settings does nothing if called twice.
421 (setq frame-notice-user-settings nil)
422 (setq frame-initial-frame nil)))
423
424 (defun make-initial-minibuffer-frame (display)
425 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
426 (if display
427 (make-frame-on-display display parms)
428 (make-frame parms))))
429
430 ;;;; Creation of additional frames, and other frame miscellanea
431
432 (defun get-other-frame ()
433 "Return some frame other than the current frame.
434 Create one if necessary. Note that the minibuffer frame, if separate,
435 is not considered (see `next-frame')."
436 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
437 (make-frame)
438 (next-frame (selected-frame)))))
439 s))
440
441 (defun next-multiframe-window ()
442 "Select the next window, regardless of which frame it is on."
443 (interactive)
444 (select-window (next-window (selected-window)
445 (> (minibuffer-depth) 0)
446 t)))
447
448 (defun previous-multiframe-window ()
449 "Select the previous window, regardless of which frame it is on."
450 (interactive)
451 (select-window (previous-window (selected-window)
452 (> (minibuffer-depth) 0)
453 t)))
454
455 (defun make-frame-on-display (display &optional parameters)
456 "Make a frame on display DISPLAY.
457 The optional second argument PARAMETERS specifies additional frame parameters."
458 (interactive "sMake frame on display: ")
459 (or (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'" display)
460 (error "Invalid display, not HOST:SERVER or HOST:SERVER.SCREEN"))
461 (make-frame (cons (cons 'display display) parameters)))
462
463 (defun make-frame-command ()
464 "Make a new frame, and select it if the terminal displays only one frame."
465 (interactive)
466 (if (and window-system (not (eq window-system 'pc)))
467 (make-frame)
468 (select-frame (make-frame))))
469
470 (defvar before-make-frame-hook nil
471 "Functions to run before a frame is created.")
472
473 (defvar after-make-frame-functions nil
474 "Functions to run after a frame is created.
475 The functions are run with one arg, the newly created frame.")
476
477 (defvar after-setting-font-hooks nil
478 "Functions to run after a frame's font has been changed.")
479
480 ;; Alias, kept temporarily.
481 (defalias 'new-frame 'make-frame)
482
483 (defun make-frame (&optional parameters)
484 "Return a newly created frame displaying the current buffer.
485 Optional argument PARAMETERS is an alist of parameters for the new frame.
486 Each element of PARAMETERS should have the form (NAME . VALUE), for example:
487
488 (name . STRING) The frame should be named STRING.
489
490 (width . NUMBER) The frame should be NUMBER characters in width.
491 (height . NUMBER) The frame should be NUMBER text lines high.
492
493 You cannot specify either `width' or `height', you must use neither or both.
494
495 (minibuffer . t) The frame should have a minibuffer.
496 (minibuffer . nil) The frame should have no minibuffer.
497 (minibuffer . only) The frame should contain only a minibuffer.
498 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
499
500 Before the frame is created (via `frame-creation-function'), functions on the
501 hook `before-make-frame-hook' are run. After the frame is created, functions
502 on `after-make-frame-functions' are run with one arg, the newly created frame."
503 (interactive)
504 (run-hooks 'before-make-frame-hook)
505 (let ((frame (funcall frame-creation-function parameters)))
506 (run-hook-with-args 'after-make-frame-functions frame)
507 frame))
508
509 (defun filtered-frame-list (predicate)
510 "Return a list of all live frames which satisfy PREDICATE."
511 (let* ((frames (frame-list))
512 (list frames))
513 (while (consp frames)
514 (unless (funcall predicate (car frames))
515 (setcar frames nil))
516 (setq frames (cdr frames)))
517 (delq nil list)))
518
519 (defun minibuffer-frame-list ()
520 "Return a list of all frames with their own minibuffers."
521 (filtered-frame-list
522 (function (lambda (frame)
523 (eq frame (window-frame (minibuffer-window frame)))))))
524
525 (defun frames-on-display-list (&optional display)
526 "Return a list of all frames on DISPLAY.
527 DISPLAY is a name of a display, a string of the form HOST:SERVER.SCREEN.
528 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
529 (let* ((display (or display (frame-parameter nil 'display)))
530 (func #'(lambda (frame)
531 (eq (frame-parameter frame 'display) display))))
532 (filtered-frame-list func)))
533
534 (defun framep-on-display (&optional display)
535 "Return the type of frames on DISPLAY.
536 DISPLAY may be a display name or a frame. If it is a frame, its type is
537 returned.
538 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
539 All frames on a given display are of the same type."
540 (or (framep display)
541 (framep (car (frames-on-display-list display)))))
542
543 (defun frame-remove-geometry-params (param-list)
544 "Return the parameter list PARAM-LIST, but with geometry specs removed.
545 This deletes all bindings in PARAM-LIST for `top', `left', `width',
546 `height', `user-size' and `user-position' parameters.
547 Emacs uses this to avoid overriding explicit moves and resizings from
548 the user during startup."
549 (setq param-list (cons nil param-list))
550 (let ((tail param-list))
551 (while (consp (cdr tail))
552 (if (and (consp (car (cdr tail)))
553 (memq (car (car (cdr tail)))
554 '(height width top left user-position user-size)))
555 (progn
556 (setq frame-initial-geometry-arguments
557 (cons (car (cdr tail)) frame-initial-geometry-arguments))
558 (setcdr tail (cdr (cdr tail))))
559 (setq tail (cdr tail)))))
560 (setq frame-initial-geometry-arguments
561 (nreverse frame-initial-geometry-arguments))
562 (cdr param-list))
563
564
565 (defcustom focus-follows-mouse t
566 "*Non-nil if window system changes focus when you move the mouse."
567 :type 'boolean
568 :group 'frames
569 :version "20.3")
570
571 (defun other-frame (arg)
572 "Select the ARG'th different visible frame, and raise it.
573 All frames are arranged in a cyclic order.
574 This command selects the frame ARG steps away in that order.
575 A negative ARG moves in the opposite order."
576 (interactive "p")
577 (let ((frame (selected-frame)))
578 (while (> arg 0)
579 (setq frame (next-frame frame))
580 (while (not (eq (frame-visible-p frame) t))
581 (setq frame (next-frame frame)))
582 (setq arg (1- arg)))
583 (while (< arg 0)
584 (setq frame (previous-frame frame))
585 (while (not (eq (frame-visible-p frame) t))
586 (setq frame (previous-frame frame)))
587 (setq arg (1+ arg)))
588 (select-frame frame)
589 (raise-frame frame)
590 ;; Ensure, if possible, that frame gets input focus.
591 (when (eq window-system 'w32)
592 (w32-focus-frame frame))
593 (cond (focus-follows-mouse
594 (unless (eq window-system 'w32)
595 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))
596 (t
597 (when (eq window-system 'x)
598 (x-focus-frame frame))))))
599
600 (defun make-frame-names-alist ()
601 (let* ((current-frame (selected-frame))
602 (falist
603 (cons
604 (cons (frame-parameter current-frame 'name) current-frame) nil))
605 (frame (next-frame nil t)))
606 (while (not (eq frame current-frame))
607 (progn
608 (setq falist (cons (cons (frame-parameter frame 'name) frame) falist))
609 (setq frame (next-frame frame t))))
610 falist))
611
612 (defvar frame-name-history nil)
613 (defun select-frame-by-name (name)
614 "Select the frame whose name is NAME and raise it.
615 If there is no frame by that name, signal an error."
616 (interactive
617 (let* ((frame-names-alist (make-frame-names-alist))
618 (default (car (car frame-names-alist)))
619 (input (completing-read
620 (format "Select Frame (default %s): " default)
621 frame-names-alist nil t nil 'frame-name-history)))
622 (if (= (length input) 0)
623 (list default)
624 (list input))))
625 (let* ((frame-names-alist (make-frame-names-alist))
626 (frame (cdr (assoc name frame-names-alist))))
627 (or frame
628 (error "There is no frame named `%s'" name))
629 (make-frame-visible frame)
630 (raise-frame frame)
631 (select-frame frame)
632 ;; Ensure, if possible, that frame gets input focus.
633 (if (eq window-system 'w32)
634 (w32-focus-frame frame)
635 (when focus-follows-mouse
636 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))))
637 \f
638 ;;;; Frame configurations
639
640 (defun current-frame-configuration ()
641 "Return a list describing the positions and states of all frames.
642 Its car is `frame-configuration'.
643 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
644 where
645 FRAME is a frame object,
646 ALIST is an association list specifying some of FRAME's parameters, and
647 WINDOW-CONFIG is a window configuration object for FRAME."
648 (cons 'frame-configuration
649 (mapcar (function
650 (lambda (frame)
651 (list frame
652 (frame-parameters frame)
653 (current-window-configuration frame))))
654 (frame-list))))
655
656 (defun set-frame-configuration (configuration &optional nodelete)
657 "Restore the frames to the state described by CONFIGURATION.
658 Each frame listed in CONFIGURATION has its position, size, window
659 configuration, and other parameters set as specified in CONFIGURATION.
660 Ordinarily, this function deletes all existing frames not
661 listed in CONFIGURATION. But if optional second argument NODELETE
662 is given and non-nil, the unwanted frames are iconified instead."
663 (or (frame-configuration-p configuration)
664 (signal 'wrong-type-argument
665 (list 'frame-configuration-p configuration)))
666 (let ((config-alist (cdr configuration))
667 frames-to-delete)
668 (mapcar (function
669 (lambda (frame)
670 (let ((parameters (assq frame config-alist)))
671 (if parameters
672 (progn
673 (modify-frame-parameters
674 frame
675 ;; Since we can't set a frame's minibuffer status,
676 ;; we might as well omit the parameter altogether.
677 (let* ((parms (nth 1 parameters))
678 (mini (assq 'minibuffer parms)))
679 (if mini (setq parms (delq mini parms)))
680 parms))
681 (set-window-configuration (nth 2 parameters)))
682 (setq frames-to-delete (cons frame frames-to-delete))))))
683 (frame-list))
684 (if nodelete
685 ;; Note: making frames invisible here was tried
686 ;; but led to some strange behavior--each time the frame
687 ;; was made visible again, the window manager asked afresh
688 ;; for where to put it.
689 (mapcar 'iconify-frame frames-to-delete)
690 (mapcar 'delete-frame frames-to-delete))))
691 \f
692 ;;;; Convenience functions for accessing and interactively changing
693 ;;;; frame parameters.
694
695 (defun frame-height (&optional frame)
696 "Return number of lines available for display on FRAME.
697 If FRAME is omitted, describe the currently selected frame."
698 (cdr (assq 'height (frame-parameters frame))))
699
700 (defun frame-width (&optional frame)
701 "Return number of columns available for display on FRAME.
702 If FRAME is omitted, describe the currently selected frame."
703 (cdr (assq 'width (frame-parameters frame))))
704
705 (defalias 'set-default-font 'set-frame-font)
706 (defun set-frame-font (font-name)
707 "Set the font of the selected frame to FONT-NAME.
708 When called interactively, prompt for the name of the font to use.
709 To get the frame's current default font, use `frame-parameters'."
710 (interactive
711 (list
712 (let ((completion-ignore-case t))
713 (completing-read "Font name: "
714 (mapcar #'list
715 ;; x-list-fonts will fail with an error
716 ;; if this frame doesn't support fonts.
717 (x-list-fonts "*" nil (selected-frame)))))))
718 (modify-frame-parameters (selected-frame)
719 (list (cons 'font font-name)))
720 ;; Update faces that want a bold or italic version of the default font.
721 (frame-update-faces (selected-frame))
722 (run-hooks 'after-setting-font-hooks))
723
724 (defun set-background-color (color-name)
725 "Set the background color of the selected frame to COLOR-NAME.
726 When called interactively, prompt for the name of the color to use.
727 To get the frame's current background color, use `frame-parameters'."
728 (interactive (list (facemenu-read-color)))
729 (modify-frame-parameters (selected-frame)
730 (list (cons 'background-color color-name)))
731 (frame-update-face-colors (selected-frame)))
732
733 (defun set-foreground-color (color-name)
734 "Set the foreground color of the selected frame to COLOR-NAME.
735 When called interactively, prompt for the name of the color to use.
736 To get the frame's current foreground color, use `frame-parameters'."
737 (interactive (list (facemenu-read-color)))
738 (modify-frame-parameters (selected-frame)
739 (list (cons 'foreground-color color-name)))
740 (frame-update-face-colors (selected-frame)))
741
742 (defun set-cursor-color (color-name)
743 "Set the text cursor color of the selected frame to COLOR-NAME.
744 When called interactively, prompt for the name of the color to use.
745 To get the frame's current cursor color, use `frame-parameters'."
746 (interactive (list (facemenu-read-color)))
747 (modify-frame-parameters (selected-frame)
748 (list (cons 'cursor-color color-name))))
749
750 (defun set-mouse-color (color-name)
751 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
752 When called interactively, prompt for the name of the color to use.
753 To get the frame's current mouse color, use `frame-parameters'."
754 (interactive (list (facemenu-read-color)))
755 (modify-frame-parameters (selected-frame)
756 (list (cons 'mouse-color
757 (or color-name
758 (cdr (assq 'mouse-color
759 (frame-parameters))))))))
760
761 (defun set-border-color (color-name)
762 "Set the color of the border of the selected frame to COLOR-NAME.
763 When called interactively, prompt for the name of the color to use.
764 To get the frame's current border color, use `frame-parameters'."
765 (interactive (list (facemenu-read-color)))
766 (modify-frame-parameters (selected-frame)
767 (list (cons 'border-color color-name))))
768
769 (defun auto-raise-mode (arg)
770 "Toggle whether or not the selected frame should auto-raise.
771 With arg, turn auto-raise mode on if and only if arg is positive.
772 Note that this controls Emacs's own auto-raise feature.
773 Some window managers allow you to enable auto-raise for certain windows.
774 You can use that for Emacs windows if you wish, but if you do,
775 that is beyond the control of Emacs and this command has no effect on it."
776 (interactive "P")
777 (if (null arg)
778 (setq arg
779 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
780 -1 1)))
781 (if (> arg 0)
782 (raise-frame (selected-frame)))
783 (modify-frame-parameters (selected-frame)
784 (list (cons 'auto-raise (> arg 0)))))
785
786 (defun auto-lower-mode (arg)
787 "Toggle whether or not the selected frame should auto-lower.
788 With arg, turn auto-lower mode on if and only if arg is positive.
789 Note that this controls Emacs's own auto-lower feature.
790 Some window managers allow you to enable auto-lower for certain windows.
791 You can use that for Emacs windows if you wish, but if you do,
792 that is beyond the control of Emacs and this command has no effect on it."
793 (interactive "P")
794 (if (null arg)
795 (setq arg
796 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
797 -1 1)))
798 (modify-frame-parameters (selected-frame)
799 (list (cons 'auto-lower (> arg 0)))))
800 (defun set-frame-name (name)
801 "Set the name of the selected frame to NAME.
802 When called interactively, prompt for the name of the frame.
803 The frame name is displayed on the modeline if the terminal displays only
804 one frame, otherwise the name is displayed on the frame's caption bar."
805 (interactive "sFrame name: ")
806 (modify-frame-parameters (selected-frame)
807 (list (cons 'name name))))
808 \f
809 ;;;; Frame/display capabilities.
810 (defun display-mouse-p (&optional display)
811 "Return non-nil if DISPLAY has a mouse available.
812 DISPLAY can be a display name, a frame, or nil (meaning the selected
813 frame's display)."
814 (let ((frame-type (framep-on-display display)))
815 (cond
816 ((eq frame-type 'pc)
817 (msdos-mouse-p))
818 ((eq system-type 'windows-nt)
819 (> w32-num-mouse-buttons 0))
820 ((memq frame-type '(x mac))
821 t) ;; We assume X and Mac *always* have a pointing device
822 (t
823 (or (and (featurep 'xt-mouse)
824 xterm-mouse-mode)
825 ;; t-mouse is distributed with the GPM package. It doesn't have
826 ;; a toggle.
827 (featurep 't-mouse))))))
828
829 (defun display-popup-menus-p (&optional display)
830 "Return non-nil if popup menus are supported on DISPLAY.
831 DISPLAY can be a display name, a frame, or nil (meaning the selected
832 frame's display).
833 Support for popup menus requires that the mouse be available."
834 (and
835 (let ((frame-type (framep-on-display display)))
836 (memq frame-type '(x w32 pc mac)))
837 (display-mouse-p display)))
838
839 (defun display-graphic-p (&optional display)
840 "Return non-nil if DISPLAY is a graphic display.
841 Graphical displays are those which are capable of displaying several
842 frames and several different fonts at once. This is true for displays
843 that use a window system such as X, and false for text-only terminals.
844 DISPLAY can be a display name, a frame, or nil (meaning the selected
845 frame's display)."
846 (not (null (memq (framep-on-display display) '(x w32 mac)))))
847
848 (defalias 'display-multi-frame-p 'display-graphic-p)
849 (defalias 'display-multi-font-p 'display-graphic-p)
850
851 (defun display-selections-p (&optional display)
852 "Return non-nil if DISPLAY supports selections.
853 A selection is a way to transfer text or other data between programs
854 via special system buffers called `selection' or `cut buffer' or
855 `clipboard'.
856 DISPLAY can be a display name, a frame, or nil (meaning the selected
857 frame's display)."
858 (let ((frame-type (framep-on-display display)))
859 (cond
860 ((eq frame-type 'pc)
861 ;; MS-DOG frames support selections when Emacs runs inside
862 ;; the Windows' DOS Box.
863 (not (null dos-windows-version)))
864 ((memq frame-type '(x w32 mac))
865 t) ;; FIXME?
866 (t
867 nil))))
868
869 (defun display-screens (&optional display)
870 "Return the number of screens associated with DISPLAY."
871 (let ((frame-type (framep-on-display display)))
872 (cond
873 ((memq frame-type '(x w32))
874 (x-display-screens display))
875 (t ;; FIXME: is this correct for the Mac?
876 1))))
877
878 (defun display-pixel-height (&optional display)
879 "Return the height of DISPLAY's screen in pixels.
880 For character terminals, each character counts as a single pixel."
881 (let ((frame-type (framep-on-display display)))
882 (cond
883 ((memq frame-type '(x w32 mac))
884 (x-display-pixel-height display))
885 (t
886 (frame-height (if (framep display) display (selected-frame)))))))
887
888 (defun display-pixel-width (&optional display)
889 "Return the width of DISPLAY's screen in pixels.
890 For character terminals, each character counts as a single pixel."
891 (let ((frame-type (framep-on-display display)))
892 (cond
893 ((memq frame-type '(x w32 mac))
894 (x-display-pixel-width display))
895 (t
896 (frame-width (if (framep display) display (selected-frame)))))))
897
898 (defun display-mm-height (&optional display)
899 "Return the height of DISPLAY's screen in millimeters.
900 If the information is unavailable, value is nil."
901 (and (memq (framep-on-display display) '(x w32 mac))
902 (x-display-mm-height display)))
903
904 (defun display-mm-width (&optional display)
905 "Return the width of DISPLAY's screen in millimeters.
906 If the information is unavailable, value is nil."
907 (and (memq (framep-on-display display) '(x w32 mac))
908 (x-display-mm-width display)))
909
910 (defun display-backing-store (&optional display)
911 "Return the backing store capability of DISPLAY's screen.
912 The value may be `always', `when-mapped', `not-useful', or nil if
913 the question is inapplicable to a certain kind of display."
914 (let ((frame-type (framep-on-display display)))
915 (cond
916 ((memq frame-type '(x w32 mac))
917 (x-display-backing-store display))
918 (t
919 'not-useful))))
920
921 (defun display-save-under (&optional display)
922 "Return non-nil if DISPLAY's screen supports the SaveUnder feature."
923 (let ((frame-type (framep-on-display display)))
924 (cond
925 ((memq frame-type '(x w32 mac))
926 (x-display-save-under display))
927 (t
928 'not-useful))))
929
930 (defun display-planes (&optional display)
931 "Return the number of planes supported by DISPLAY."
932 (let ((frame-type (framep-on-display display)))
933 (cond
934 ((memq frame-type '(x w32 mac))
935 (x-display-planes display))
936 ((eq frame-type 'pc)
937 4)
938 (t
939 (truncate (log (length (tty-color-alist)) 2))))))
940
941 (defun display-color-cells (&optional display)
942 "Return the number of color cells supported by DISPLAY."
943 (let ((frame-type (framep-on-display display)))
944 (cond
945 ((memq frame-type '(x w32 mac))
946 (x-display-color-cells display))
947 ((eq frame-type 'pc)
948 16)
949 (t
950 (length (tty-color-alist))))))
951
952 (defun display-visual-class (&optional display)
953 "Returns the visual class of DISPLAY.
954 The value is one of the symbols `static-gray', `gray-scale',
955 `static-color', `pseudo-color', `true-color', or `direct-color'."
956 (let ((frame-type (framep-on-display display)))
957 (cond
958 ((memq frame-type '(x w32 mac))
959 (x-display-visual-class display))
960 ((and (memq frame-type '(pc t))
961 (tty-display-color-p display))
962 'static-color)
963 (t
964 'static-gray))))
965
966 \f
967 ;;;; Aliases for backward compatibility with Emacs 18.
968 (defalias 'screen-height 'frame-height)
969 (defalias 'screen-width 'frame-width)
970
971 (defun set-screen-width (cols &optional pretend)
972 "Obsolete function to change the size of the screen to COLS columns.
973 Optional second arg non-nil means that redisplay should use COLS columns
974 but that the idea of the actual width of the frame should not be changed.
975 This function is provided only for compatibility with Emacs 18; new code
976 should use `set-frame-width instead'."
977 (set-frame-width (selected-frame) cols pretend))
978
979 (defun set-screen-height (lines &optional pretend)
980 "Obsolete function to change the height of the screen to LINES lines.
981 Optional second arg non-nil means that redisplay should use LINES lines
982 but that the idea of the actual height of the screen should not be changed.
983 This function is provided only for compatibility with Emacs 18; new code
984 should use `set-frame-height' instead."
985 (set-frame-height (selected-frame) lines pretend))
986
987 (defun delete-other-frames (&optional frame)
988 "Delete all frames except FRAME.
989 FRAME nil or omitted means delete all frames except the selected frame."
990 (interactive)
991 (unless frame
992 (setq frame (selected-frame)))
993 (mapcar 'delete-frame (delq frame (frame-list))))
994
995
996 (make-obsolete 'screen-height 'frame-height) ;before 19.15
997 (make-obsolete 'screen-width 'frame-width) ;before 19.15
998 (make-obsolete 'set-screen-width 'set-frame-width) ;before 19.15
999 (make-obsolete 'set-screen-height 'set-frame-height) ;before 19.15
1000
1001 \f
1002 ;;; Highlighting trailing whitespace.
1003
1004 (make-variable-buffer-local 'show-trailing-whitespace)
1005
1006 (defcustom show-trailing-whitespace nil
1007 "*Non-nil means highlight trailing whitespace in face `trailing-whitespace'."
1008 :tag "Highlight trailing whitespace."
1009 :set #'(lambda (symbol value) (set-default symbol value))
1010 :type 'boolean
1011 :group 'font-lock)
1012
1013
1014 \f
1015 ;;; Scrolling
1016
1017 (defgroup scrolling nil
1018 "Scrolling windows."
1019 :version "21.1"
1020 :group 'frames)
1021
1022 (defcustom automatic-hscrolling t
1023 "*Allow or disallow autmatic scrolling windows horizontally.
1024 If non-nil, windows are automatically scrolled horizontally to make
1025 point visible."
1026 :version "21.1"
1027 :type 'boolean
1028 :group 'scrolling)
1029
1030 \f
1031 ;;; Blinking cursor
1032
1033 (defgroup cursor nil
1034 "Displaying text cursors."
1035 :version "21.1"
1036 :group 'frames)
1037
1038 (defcustom blink-cursor-delay 0.5
1039 "*Seconds of idle time after which cursor starts to blink."
1040 :tag "Delay in seconds."
1041 :type 'number
1042 :group 'cursor)
1043
1044 (defcustom blink-cursor-interval 0.5
1045 "*Length of cursor blink interval in seconds."
1046 :tag "Blink interval in seconds."
1047 :type 'number
1048 :group 'cursor)
1049
1050 (defvar blink-cursor-idle-timer nil
1051 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
1052 The function `blink-cursor-start' is called when the timer fires.")
1053
1054 (defvar blink-cursor-timer nil
1055 "Timer started from `blink-cursor-start'.
1056 This timer calls `blink-cursor' every `blink-cursor-interval' seconds.")
1057
1058 (defvar blink-cursor-mode nil
1059 "Non-nil means blinking cursor is active.")
1060
1061 (defun blink-cursor-mode (arg)
1062 "Toggle blinking cursor mode.
1063 With arg, turn blinking cursor mode on iff arg is positive.
1064 When blinking cursor mode is enabled, the cursor of the selected
1065 window blinks."
1066 (interactive "P")
1067 (let ((on-p (if (null arg)
1068 (not blink-cursor-mode)
1069 (> (prefix-numeric-value arg) 0))))
1070 (if blink-cursor-idle-timer
1071 (cancel-timer blink-cursor-idle-timer))
1072 (if blink-cursor-timer
1073 (cancel-timer blink-cursor-timer))
1074 (setq blink-cursor-idle-timer nil
1075 blink-cursor-timer nil
1076 blink-cursor-mode nil)
1077 (if on-p
1078 (progn
1079 ;; Hide the cursor.
1080 ;(internal-show-cursor nil nil)
1081 (setq blink-cursor-idle-timer
1082 (run-with-idle-timer blink-cursor-delay
1083 blink-cursor-delay
1084 'blink-cursor-start))
1085 (setq blink-cursor-mode t))
1086 (internal-show-cursor nil t))))
1087
1088 ;; Note that this is really initialized from startup.el before
1089 ;; the init-file is read.
1090
1091 (defcustom blink-cursor nil
1092 "*Non-nil means blinking cursor mode is active."
1093 :group 'cursor
1094 :tag "Blinking cursor"
1095 :type 'boolean
1096 :set #'(lambda (symbol value)
1097 (set-default symbol value)
1098 (blink-cursor-mode (or value 0))))
1099
1100 (defun blink-cursor-start ()
1101 "Timer function called from the timer `blink-cursor-idle-timer'.
1102 This starts the timer `blink-cursor-timer', which makes the cursor blink
1103 if appropriate. It also arranges to cancel that timer when the next
1104 command starts, by installing a pre-command hook."
1105 (when (null blink-cursor-timer)
1106 (add-hook 'pre-command-hook 'blink-cursor-end)
1107 (setq blink-cursor-timer
1108 (run-with-timer blink-cursor-interval blink-cursor-interval
1109 'blink-cursor-timer-function))))
1110
1111 (defun blink-cursor-timer-function ()
1112 "Timer function of timer `blink-cursor-timer'."
1113 (internal-show-cursor nil (not (internal-show-cursor-p))))
1114
1115 (defun blink-cursor-end ()
1116 "Stop cursor blinking.
1117 This is installed as a pre-command hook by `blink-cursor-start'.
1118 When run, it cancels the timer `blink-cursor-timer' and removes
1119 itself as a pre-command hook."
1120 (remove-hook 'pre-command-hook 'blink-cursor-end)
1121 (internal-show-cursor nil t)
1122 (cancel-timer blink-cursor-timer)
1123 (setq blink-cursor-timer nil))
1124
1125
1126 \f
1127 ;;; Busy-cursor.
1128
1129 (defcustom busy-cursor t
1130 "*Non-nil means show a busy-cursor when running under a window system."
1131 :tag "Busy-cursor"
1132 :type 'boolean
1133 :group 'cursor
1134 :get #'(lambda (symbol) display-busy-cursor)
1135 :set #'(lambda (symbol value)
1136 (set-default symbol value)
1137 (setq display-busy-cursor value)))
1138
1139 (defcustom busy-cursor-delay-seconds 1
1140 "*Seconds to wait before displaying a busy-cursor."
1141 :tag "Busy-cursor delay"
1142 :type 'number
1143 :group 'cursor
1144 :get #'(lambda (symbol) busy-cursor-delay)
1145 :set #'(lambda (symbol value)
1146 (set-default symbol value)
1147 (setq busy-cursor-delay value)))
1148
1149 \f
1150 (defcustom show-cursor-in-non-selected-windows t
1151 "*Non-nil means show a hollow box cursor in non-selected-windows.
1152 If nil, don't show a cursor except in the selected window."
1153 :tag "Cursor in non-selected windows"
1154 :type 'boolean
1155 :group 'cursor
1156 :get #'(lambda (symbol) cursor-in-non-selected-windows)
1157 :set #'(lambda (symbol value)
1158 (set-default symbol value)
1159 (setq cursor-in-non-selected-windows value)
1160 (force-mode-line-update t)))
1161
1162 \f
1163 ;;;; Key bindings
1164
1165 (define-key ctl-x-5-map "2" 'make-frame-command)
1166 (define-key ctl-x-5-map "1" 'delete-other-frames)
1167 (define-key ctl-x-5-map "0" 'delete-frame)
1168 (define-key ctl-x-5-map "o" 'other-frame)
1169
1170 (provide 'frame)
1171
1172 ;;; frame.el ends here
1173 (frame-notice-user-settings):