(Fmove_to_column): Extend end of line only if FORCE is t.
[bpt/emacs.git] / lisp / frame.el
CommitLineData
dc6d9681 1;;; frame.el --- multi-frame management independent of window systems.
c88ab9ce 2
30e19aee 3;; Copyright (C) 1993, 1994, 1996, 1997 Free Software Foundation, Inc.
3a801d0c 4
fc68affa 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: internal
fc68affa 7
b578f267
EN
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.
64c669bc 24
fc68affa
ER
25;;; Code:
26
dc6d9681
JB
27(defvar frame-creation-function nil
28 "Window-system dependent function to call to create a new frame.
29The window system startup file should set this to its frame creation
64c669bc
JB
30function, which should take an alist of parameters as its argument.")
31
216f1169
RS
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.
0c966f88 35(defcustom initial-frame-alist nil
0b9d32af 36 "*Alist of frame parameters for creating the initial X window frame.
eaa974e1 37You can set this in your `.emacs' file; for example,
dc6d9681 38 (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
de17da15
RS
39Parameters specified here supersede the values given in `default-frame-alist'.
40
41If the value calls for a frame without a minibuffer, and you have not created
42a minibuffer frame on your own, one is created according to
7eadab74 43`minibuffer-frame-alist'.
de17da15
RS
44
45You can specify geometry-related options for just the initial frame
46by setting this variable in your `.emacs' file; however, they won't
85557d7e 47take effect until Emacs reads `.emacs', which happens after first creating
de17da15
RS
48the frame. If you want the frame to have the proper geometry as soon
49as 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,
0c966f88
RS
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)
64c669bc 59
0b9d32af
RS
60(defcustom minibuffer-frame-alist '((width . 80) (height . 2))
61 "*Alist of frame parameters for initially creating a minibuffer frame.
eaa974e1 62You can set this in your `.emacs' file; for example,
dc6d9681 63 (setq minibuffer-frame-alist
7eadab74 64 '((top . 1) (left . 1) (width . 80) (height . 2)))
eaa974e1 65Parameters specified here supersede the values given in
0b9d32af
RS
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)
64c669bc 71
0b9d32af
RS
72(defcustom pop-up-frame-alist nil
73 "*Alist of frame parameters used when creating pop-up frames.
dc6d9681 74Pop-up frames are used for completions, help, and the like.
64c669bc 75This variable can be set in your init file, like this:
dc6d9681 76 (setq pop-up-frame-alist '((width . 80) (height . 20)))
0b9d32af
RS
77These supersede the values given in `default-frame-alist',
78for pop-up frames."
79 :type '(repeat (cons :format "%v"
80 (symbol :tag "Parameter")
81 (sexp :tag "Value")))
82 :group 'frames)
64c669bc 83
dc6d9681 84(setq pop-up-frame-function
64c669bc 85 (function (lambda ()
6eb018ba 86 (make-frame pop-up-frame-alist))))
64c669bc 87
30e19aee 88(defcustom special-display-frame-alist
8a9e86e6
RS
89 '((height . 14) (width . 80) (unsplittable . t))
90 "*Alist of frame parameters used when creating special frames.
91Special frames are used for buffers whose names are in
92`special-display-buffer-names' and for buffers whose names match
93one of the regular expressions in `special-display-regexps'.
94This variable can be set in your init file, like this:
95 (setq special-display-frame-alist '((width . 80) (height . 20)))
30e19aee
RS
96These 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)
8a9e86e6
RS
101
102;; Display BUFFER in its own frame, reusing an existing window if any.
103;; Return the window chosen.
104;; Currently we do not insist on selecting the window within its frame.
95e6cf39 105;; If ARGS is an alist, use it as a list of frame parameter specs.
85557d7e 106;; If ARGS is a list whose car is a symbol,
95e6cf39
RS
107;; use (car ARGS) as a function to do the work.
108;; Pass it BUFFER as first arg, and (cdr ARGS) gives the rest of the args.
109(defun special-display-popup-frame (buffer &optional 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))))))
8a9e86e6 124
924be53a
RS
125;; Handle delete-frame events from the X server.
126(defun handle-delete-frame (event)
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)
a9562d19
RS
138 ;; Gildea@x.org says it is ok to ask questions before terminating.
139 (save-buffers-kill-emacs))))
64c669bc 140\f
dc6d9681 141;;;; Arrangement of frames at startup
64c669bc
JB
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
dc6d9681 145;;; file should create any frames specified in the window system defaults.
85557d7e 146;;;
dc6d9681 147;;; 2) If no frames have been opened, we open an initial text frame.
64c669bc
JB
148;;;
149;;; 3) Once the init file is done, we apply any newly set parameters
dc6d9681 150;;; in initial-frame-alist to the frame.
64c669bc 151
85557d7e 152;; These are now called explicitly at the proper times,
fc4d4afb
RS
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)
64c669bc 157
dc6d9681
JB
158;;; If we create the initial frame, this is it.
159(defvar frame-initial-frame nil)
64c669bc 160
3f2f8c83
RS
161;; Record the parameters used in frame-initialize to make the initial frame.
162(defvar frame-initial-frame-alist)
163
791e09d8
RS
164(defvar frame-initial-geometry-arguments nil)
165
64c669bc 166;;; startup.el calls this function before loading the user's init
dc6d9681 167;;; file - if there is no frame with a minibuffer open now, create
64c669bc 168;;; one to display messages while loading the init file.
dc6d9681 169(defun frame-initialize ()
85557d7e 170
64c669bc 171 ;; Are we actually running under a window system at all?
004f057a 172 (if (and window-system (not noninteractive) (not (eq window-system 'pc)))
7eadab74 173 (progn
62642af9
RS
174 ;; Turn on special-display processing only if there's a window system.
175 (setq special-display-function 'special-display-popup-frame)
176
7eadab74
JB
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))
36fc9c9f 182 (progn
3f2f8c83
RS
183 (setq frame-initial-frame-alist
184 (append initial-frame-alist default-frame-alist))
a8bb5882
RS
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)))
36fc9c9f
RS
189 (setq default-minibuffer-frame
190 (setq frame-initial-frame
96a84970 191 (make-frame frame-initial-frame-alist)))
11281034
RS
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.
58bf6042 196 (setq initial-frame-alist
c2079f0a 197 (frame-remove-geometry-params initial-frame-alist))))
85557d7e 198 ;; At this point, we know that we have a frame open, so we
dc6d9681
JB
199 ;; can delete the terminal frame.
200 (delete-frame terminal-frame)
201 (setq terminal-frame nil))
85557d7e 202
0759c4d0
KH
203 ;; No, we're not running a window system. Use make-terminal-frame if
204 ;; we support that feature, otherwise arrange to cause errors.
004f057a
RS
205 (or (eq window-system 'pc)
206 (setq frame-creation-function
81b99826
GM
207 (if (fboundp 'tty-create-frame-with-faces)
208 'tty-create-frame-with-faces
004f057a
RS
209 (function
210 (lambda (parameters)
211 (error
212 "Can't create multiple frames without a window system"))))))))
85557d7e 213
7eadab74
JB
214;;; startup.el calls this function after loading the user's init
215;;; file. Now default-frame-alist and initial-frame-alist contain
216;;; information to which we must react; do what needs to be done.
dc6d9681 217(defun frame-notice-user-settings ()
7eadab74 218
2ffa6186 219 ;; Make menu-bar-mode and default-frame-alist consistent.
b65be6a8
KH
220 (if (boundp 'menu-bar-mode)
221 (let ((default (assq 'menu-bar-lines default-frame-alist)))
222 (if default
223 (setq menu-bar-mode (not (eq (cdr default) 0)))
224 (setq default-frame-alist
225 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
226 default-frame-alist)))))
2ffa6186 227
7eadab74
JB
228 ;; Creating and deleting frames may shift the selected frame around,
229 ;; and thus the current buffer. Protect against that. We don't
230 ;; want to use save-excursion here, because that may also try to set
231 ;; the buffer of the selected window, which fails when the selected
232 ;; window is the minibuffer.
233 (let ((old-buffer (current-buffer)))
234
235 ;; If the initial frame is still around, apply initial-frame-alist
236 ;; and default-frame-alist to it.
237 (if (frame-live-p frame-initial-frame)
238
239 ;; The initial frame we create above always has a minibuffer.
240 ;; If the user wants to remove it, or make it a minibuffer-only
241 ;; frame, then we'll have to delete the current frame and make a
242 ;; new one; you can't remove or add a root window to/from an
243 ;; existing frame.
244 ;;
ec558adc
JB
245 ;; NOTE: default-frame-alist was nil when we created the
246 ;; existing frame. We need to explicitly include
247 ;; default-frame-alist in the parameters of the screen we
248 ;; create here, so that its new value, gleaned from the user's
249 ;; .emacs file, will be applied to the existing screen.
7eadab74
JB
250 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
251 (assq 'minibuffer default-frame-alist)
252 '(minibuffer . t)))
253 t))
254 ;; Create the new frame.
b65e2f74
RS
255 (let (parms new)
256 ;; If the frame isn't visible yet, wait till it is.
257 ;; If the user has to position the window,
258 ;; Emacs doesn't know its real position until
259 ;; the frame is seen to be visible.
260 (while (not (cdr (assq 'visibility
261 (frame-parameters frame-initial-frame))))
262 (sleep-for 1))
06d7dff2
RS
263 (setq parms (frame-parameters frame-initial-frame))
264 ;; Get rid of `name' unless it was specified explicitly before.
265 (or (assq 'name frame-initial-frame-alist)
266 (setq parms (delq (assq 'name parms) parms)))
b65e2f74 267 (setq parms (append initial-frame-alist
6eb018ba 268 default-frame-alist
06d7dff2 269 parms
6eb018ba 270 nil))
c1e67401
RS
271 ;; Get rid of `reverse', because that was handled
272 ;; when we first made the frame.
273 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
274 (if (assq 'height frame-initial-geometry-arguments)
5b66a32f 275 (setq parms (frame-delete-all 'height parms)))
c1e67401 276 (if (assq 'width frame-initial-geometry-arguments)
5b66a32f 277 (setq parms (frame-delete-all 'width parms)))
c1e67401 278 (if (assq 'left frame-initial-geometry-arguments)
5b66a32f 279 (setq parms (frame-delete-all 'left parms)))
c1e67401 280 (if (assq 'top frame-initial-geometry-arguments)
5b66a32f 281 (setq parms (frame-delete-all 'top parms)))
c1e67401
RS
282 (setq new
283 (make-frame
284 ;; Use the geometry args that created the existing
285 ;; frame, rather than the parms we get for it.
3ce3ff11
KH
286 (append frame-initial-geometry-arguments
287 '((user-size . t) (user-position . t))
288 parms)))
7eadab74
JB
289 ;; The initial frame, which we are about to delete, may be
290 ;; the only frame with a minibuffer. If it is, create a
291 ;; new one.
292 (or (delq frame-initial-frame (minibuffer-frame-list))
746bd265 293 (make-initial-minibuffer-frame nil))
7eadab74
JB
294
295 ;; If the initial frame is serving as a surrogate
296 ;; minibuffer frame for any frames, we need to wean them
297 ;; onto a new frame. The default-minibuffer-frame
298 ;; variable must be handled similarly.
299 (let ((users-of-initial
300 (filtered-frame-list
301 (function (lambda (frame)
302 (and (not (eq frame frame-initial-frame))
303 (eq (window-frame
304 (minibuffer-window frame))
305 frame-initial-frame)))))))
306 (if (or users-of-initial
307 (eq default-minibuffer-frame frame-initial-frame))
308
309 ;; Choose an appropriate frame. Prefer frames which
310 ;; are only minibuffers.
311 (let* ((new-surrogate
312 (car
313 (or (filtered-frame-list
314 (function
315 (lambda (frame)
316 (eq (cdr (assq 'minibuffer
317 (frame-parameters frame)))
318 'only))))
319 (minibuffer-frame-list))))
320 (new-minibuffer (minibuffer-window new-surrogate)))
321
322 (if (eq default-minibuffer-frame frame-initial-frame)
323 (setq default-minibuffer-frame new-surrogate))
324
325 ;; Wean the frames using frame-initial-frame as
326 ;; their minibuffer frame.
327 (mapcar
328 (function
329 (lambda (frame)
330 (modify-frame-parameters
331 frame (list (cons 'minibuffer new-minibuffer)))))
332 users-of-initial))))
ec558adc
JB
333
334 ;; Redirect events enqueued at this frame to the new frame.
335 ;; Is this a good idea?
7eadab74 336 (redirect-frame-focus frame-initial-frame new)
ec558adc 337
7eadab74 338 ;; Finally, get rid of the old frame.
0cffbbb9 339 (delete-frame frame-initial-frame t))
7eadab74
JB
340
341 ;; Otherwise, we don't need all that rigamarole; just apply
342 ;; the new parameters.
3f2f8c83
RS
343 (let (newparms allparms tail)
344 (setq allparms (append initial-frame-alist
345 default-frame-alist))
5b66a32f
RS
346 (if (assq 'height frame-initial-geometry-arguments)
347 (setq allparms (frame-delete-all 'height allparms)))
348 (if (assq 'width frame-initial-geometry-arguments)
349 (setq allparms (frame-delete-all 'width allparms)))
350 (if (assq 'left frame-initial-geometry-arguments)
351 (setq allparms (frame-delete-all 'left allparms)))
352 (if (assq 'top frame-initial-geometry-arguments)
353 (setq allparms (frame-delete-all 'top allparms)))
3f2f8c83
RS
354 (setq tail allparms)
355 ;; Find just the parms that have changed since we first
356 ;; made this frame. Those are the ones actually set by
357 ;; the init file. For those parms whose values we already knew
358 ;; (such as those spec'd by command line options)
359 ;; it is undesirable to specify the parm again
360 ;; once the user has seen the frame and been able to alter it
361 ;; manually.
362 (while tail
363 (let (newval oldval)
66de157d
RS
364 (setq oldval (assq (car (car tail))
365 frame-initial-frame-alist))
3f2f8c83 366 (setq newval (cdr (assq (car (car tail)) allparms)))
66de157d 367 (or (and oldval (eq (cdr oldval) newval))
3f2f8c83
RS
368 (setq newparms
369 (cons (cons (car (car tail)) newval) newparms))))
370 (setq tail (cdr tail)))
86e58378 371 (setq newparms (nreverse newparms))
3f2f8c83 372 (modify-frame-parameters frame-initial-frame
86e58378
RS
373 newparms)
374 (if (assq 'font newparms)
375 (frame-update-faces frame-initial-frame)))))
64c669bc 376
7eadab74
JB
377 ;; Restore the original buffer.
378 (set-buffer old-buffer)
379
380 ;; Make sure the initial frame can be GC'd if it is ever deleted.
d202f1f2
JB
381 ;; Make sure frame-notice-user-settings does nothing if called twice.
382 (setq frame-initial-frame nil)))
64c669bc 383
746bd265
KH
384(defun make-initial-minibuffer-frame (display)
385 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
386 (if display
387 (make-frame-on-display display parms)
388 (make-frame parms))))
389
5b66a32f
RS
390;; Delete from ALIST all elements whose car is KEY.
391;; Return the modified alist.
392(defun frame-delete-all (key alist)
9759578f 393 (setq alist (copy-sequence alist))
5b66a32f
RS
394 (let ((tail alist))
395 (while tail
396 (if (eq (car (car tail)) key)
397 (setq alist (delq (car tail) alist)))
398 (setq tail (cdr tail)))
399 alist))
64c669bc 400\f
7eadab74 401;;;; Creation of additional frames, and other frame miscellanea
dc6d9681 402
7eadab74 403;;; Return some frame other than the current frame, creating one if
eb8c3be9 404;;; necessary. Note that the minibuffer frame, if separate, is not
7eadab74 405;;; considered (see next-frame).
7253d8e0 406(defun get-other-frame ()
dc6d9681 407 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
6eb018ba 408 (make-frame)
dc6d9681 409 (next-frame (selected-frame)))))
64c669bc
JB
410 s))
411
dc6d9681
JB
412(defun next-multiframe-window ()
413 "Select the next window, regardless of which frame it is on."
64c669bc
JB
414 (interactive)
415 (select-window (next-window (selected-window)
416 (> (minibuffer-depth) 0)
417 t)))
418
dc6d9681
JB
419(defun previous-multiframe-window ()
420 "Select the previous window, regardless of which frame it is on."
64c669bc
JB
421 (interactive)
422 (select-window (previous-window (selected-window)
423 (> (minibuffer-depth) 0)
424 t)))
425
6d73e337
RS
426(defun make-frame-on-display (display &optional parameters)
427 "Make a frame on display DISPLAY.
428The optional second argument PARAMETERS specifies additional frame parameters."
429 (interactive "sMake frame on display: ")
a9ca74cd
RS
430 (or (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'" display)
431 (error "Invalid display, not HOST:SERVER or HOST:SERVER.SCREEN"))
6d73e337
RS
432 (make-frame (cons (cons 'display display) parameters)))
433
6238bfaf
RS
434(defun make-frame-command ()
435 "Make a new frame, and select it if the terminal displays only one frame."
436 (interactive)
c1b1350b 437 (if (and window-system (not (eq window-system 'pc)))
6238bfaf
RS
438 (make-frame)
439 (select-frame (make-frame))))
440
45c4fdeb
SM
441(defvar before-make-frame-hook nil
442 "Functions to run before a frame is created.")
443
444(defvar after-make-frame-functions nil
445 "Functions to run after a frame is created.
446The functions are run with one arg, the newly created frame.")
447
81b99826
GM
448(defvar after-setting-font-hooks nil
449 "Functions to run after a frame's font has been changed.")
450
92e443b1 451;; Alias, kept temporarily.
31e1d920 452(defalias 'new-frame 'make-frame)
bc93c097 453
45c4fdeb
SM
454(defun make-frame (&optional parameters)
455 "Return a newly created frame displaying the current buffer.
456Optional argument PARAMETERS is an alist of parameters for the new frame.
457Each element of PARAMETERS should have the form (NAME . VALUE), for example:
a105105a 458
45c4fdeb 459 (name . STRING) The frame should be named STRING.
43a2e52c 460
45c4fdeb
SM
461 (width . NUMBER) The frame should be NUMBER characters in width.
462 (height . NUMBER) The frame should be NUMBER text lines high.
43a2e52c 463
45c4fdeb 464You cannot specify either `width' or `height', you must use neither or both.
43a2e52c 465
45c4fdeb
SM
466 (minibuffer . t) The frame should have a minibuffer.
467 (minibuffer . nil) The frame should have no minibuffer.
468 (minibuffer . only) The frame should contain only a minibuffer.
469 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
43a2e52c 470
45c4fdeb
SM
471Before the frame is created (via `frame-creation-function'), functions on the
472hook `before-make-frame-hook' are run. After the frame is created, functions
473on `after-make-frame-functions' are run with one arg, the newly created frame."
64c669bc 474 (interactive)
45c4fdeb
SM
475 (run-hooks 'before-make-frame-hook)
476 (let ((frame (funcall frame-creation-function parameters)))
477 (run-hook-with-args 'after-make-frame-functions frame)
478 frame))
64c669bc 479
7eadab74
JB
480(defun filtered-frame-list (predicate)
481 "Return a list of all live frames which satisfy PREDICATE."
482 (let ((frames (frame-list))
483 good-frames)
484 (while (consp frames)
485 (if (funcall predicate (car frames))
486 (setq good-frames (cons (car frames) good-frames)))
487 (setq frames (cdr frames)))
488 good-frames))
489
490(defun minibuffer-frame-list ()
491 "Return a list of all frames with their own minibuffers."
492 (filtered-frame-list
493 (function (lambda (frame)
494 (eq frame (window-frame (minibuffer-window frame)))))))
495
58bf6042
JB
496(defun frame-remove-geometry-params (param-list)
497 "Return the parameter list PARAM-LIST, but with geometry specs removed.
498This deletes all bindings in PARAM-LIST for `top', `left', `width',
791e09d8 499`height', `user-size' and `user-position' parameters.
58bf6042
JB
500Emacs uses this to avoid overriding explicit moves and resizings from
501the user during startup."
502 (setq param-list (cons nil param-list))
503 (let ((tail param-list))
504 (while (consp (cdr tail))
505 (if (and (consp (car (cdr tail)))
791e09d8
RS
506 (memq (car (car (cdr tail)))
507 '(height width top left user-position user-size)))
508 (progn
509 (setq frame-initial-geometry-arguments
510 (cons (car (cdr tail)) frame-initial-geometry-arguments))
511 (setcdr tail (cdr (cdr tail))))
58bf6042 512 (setq tail (cdr tail)))))
e11c3dc2
KH
513 (setq frame-initial-geometry-arguments
514 (nreverse frame-initial-geometry-arguments))
58bf6042
JB
515 (cdr param-list))
516
517
0ac18fb0
RS
518(defcustom focus-follows-mouse t
519 "*Non-nil if window system changes focus when you move the mouse."
520 :type 'boolean
cd32a7ba
DN
521 :group 'frames
522 :version "20.3")
26ef026d 523
ceab6935 524(defun other-frame (arg)
a569dbc3 525 "Select the ARG'th different visible frame, and raise it.
ceab6935
RM
526All frames are arranged in a cyclic order.
527This command selects the frame ARG steps away in that order.
528A negative ARG moves in the opposite order."
529 (interactive "p")
530 (let ((frame (selected-frame)))
531 (while (> arg 0)
a569dbc3
RM
532 (setq frame (next-frame frame))
533 (while (not (eq (frame-visible-p frame) t))
534 (setq frame (next-frame frame)))
535 (setq arg (1- arg)))
ceab6935 536 (while (< arg 0)
a569dbc3
RM
537 (setq frame (previous-frame frame))
538 (while (not (eq (frame-visible-p frame) t))
539 (setq frame (previous-frame frame)))
915cfd1f 540 (setq arg (1+ arg)))
ceab6935 541 (raise-frame frame)
699213bc 542 (select-frame frame)
9d702ce6
GV
543 ;; Ensure, if possible, that frame gets input focus.
544 (if (eq window-system 'w32)
545 (w32-focus-frame frame)
5265a842 546 (when focus-follows-mouse
26ef026d 547 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))))
845cde06
EZ
548
549(defun make-frame-names-alist ()
550 (let* ((current-frame (selected-frame))
551 (falist
552 (cons
553 (cons (frame-parameter current-frame 'name) current-frame) nil))
554 (frame (next-frame nil t)))
555 (while (not (eq frame current-frame))
556 (progn
557 (setq falist (cons (cons (frame-parameter frame 'name) frame) falist))
558 (setq frame (next-frame frame t))))
559 falist))
560
561(defvar frame-name-history nil)
845cde06
EZ
562(defun select-frame-by-name (name)
563 "Select the frame whose name is NAME and raise it.
564If there is no frame by that name, signal an error."
565 (interactive
716ff1c9
RS
566 (let* ((frame-names-alist (make-frame-names-alist))
567 (default (car (car frame-names-alist)))
568 (input (completing-read
569 (format "Select Frame (default %s): " default)
570 frame-names-alist nil t nil 'frame-name-history)))
845cde06
EZ
571 (if (= (length input) 0)
572 (list default)
573 (list input))))
716ff1c9
RS
574 (let* ((frame-names-alist (make-frame-names-alist))
575 (frame (cdr (assoc name frame-names-alist))))
845cde06
EZ
576 (or frame
577 (error "There is no frame named `%s'" name))
578 (make-frame-visible frame)
579 (raise-frame frame)
880a9ae1
AI
580 (select-frame frame)
581 ;; Ensure, if possible, that frame gets input focus.
582 (if (eq window-system 'w32)
583 (w32-focus-frame frame)
584 (when focus-follows-mouse
585 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))))
64c669bc 586\f
dc6d9681
JB
587;;;; Frame configurations
588
589(defun current-frame-configuration ()
590 "Return a list describing the positions and states of all frames.
376a7584
JB
591Its car is `frame-configuration'.
592Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
593where
594 FRAME is a frame object,
595 ALIST is an association list specifying some of FRAME's parameters, and
596 WINDOW-CONFIG is a window configuration object for FRAME."
597 (cons 'frame-configuration
598 (mapcar (function
599 (lambda (frame)
600 (list frame
601 (frame-parameters frame)
602 (current-window-configuration frame))))
603 (frame-list))))
dc6d9681 604
68cd265f 605(defun set-frame-configuration (configuration &optional nodelete)
dc6d9681
JB
606 "Restore the frames to the state described by CONFIGURATION.
607Each frame listed in CONFIGURATION has its position, size, window
68cd265f 608configuration, and other parameters set as specified in CONFIGURATION.
a78db71c
RS
609Ordinarily, this function deletes all existing frames not
610listed in CONFIGURATION. But if optional second argument NODELETE
5da841d2 611is given and non-nil, the unwanted frames are iconified instead."
376a7584
JB
612 (or (frame-configuration-p configuration)
613 (signal 'wrong-type-argument
614 (list 'frame-configuration-p configuration)))
615 (let ((config-alist (cdr configuration))
616 frames-to-delete)
06b1a5ef 617 (mapcar (function
dc6d9681 618 (lambda (frame)
376a7584 619 (let ((parameters (assq frame config-alist)))
06b1a5ef
JB
620 (if parameters
621 (progn
98e9d14b
JB
622 (modify-frame-parameters
623 frame
85557d7e 624 ;; Since we can't set a frame's minibuffer status,
98e9d14b
JB
625 ;; we might as well omit the parameter altogether.
626 (let* ((parms (nth 1 parameters))
627 (mini (assq 'minibuffer parms)))
628 (if mini (setq parms (delq mini parms)))
629 parms))
06b1a5ef 630 (set-window-configuration (nth 2 parameters)))
dc6d9681
JB
631 (setq frames-to-delete (cons frame frames-to-delete))))))
632 (frame-list))
a78db71c 633 (if nodelete
5da841d2
RS
634 ;; Note: making frames invisible here was tried
635 ;; but led to some strange behavior--each time the frame
636 ;; was made visible again, the window manager asked afresh
637 ;; for where to put it.
638 (mapcar 'iconify-frame frames-to-delete)
a78db71c 639 (mapcar 'delete-frame frames-to-delete))))
64c669bc 640\f
dc6d9681
JB
641;;;; Convenience functions for accessing and interactively changing
642;;;; frame parameters.
64c669bc 643
b5d48854 644(defun frame-parameter (frame parameter)
3196c5a7 645 "Return FRAME's value for parameter PARAMETER.
ed956b81 646If FRAME is nil, describe the currently selected frame."
3196c5a7 647 (cdr (assq parameter (frame-parameters frame))))
b5d48854 648
151bdc83 649(defun frame-height (&optional frame)
dc6d9681
JB
650 "Return number of lines available for display on FRAME.
651If FRAME is omitted, describe the currently selected frame."
151bdc83 652 (cdr (assq 'height (frame-parameters frame))))
dc6d9681
JB
653
654(defun frame-width (&optional frame)
655 "Return number of columns available for display on FRAME.
656If FRAME is omitted, describe the currently selected frame."
151bdc83 657 (cdr (assq 'width (frame-parameters frame))))
dc6d9681 658
889611f0
RS
659(defalias 'set-default-font 'set-frame-font)
660(defun set-frame-font (font-name)
7eadab74 661 "Set the font of the selected frame to FONT.
61298010
RS
662When called interactively, prompt for the name of the font to use.
663To get the frame's current default font, use `frame-parameters'."
64c669bc 664 (interactive "sFont name: ")
dc6d9681 665 (modify-frame-parameters (selected-frame)
4033f46b
RS
666 (list (cons 'font font-name)))
667 ;; Update faces that want a bold or italic version of the default font.
81b99826
GM
668 (frame-update-faces (selected-frame))
669 (run-hooks 'after-setting-font-hooks))
64c669bc 670
8290babd 671(defun set-background-color (color-name)
7eadab74 672 "Set the background color of the selected frame to COLOR.
61298010
RS
673When called interactively, prompt for the name of the color to use.
674To get the frame's current background color, use `frame-parameters'."
64c669bc 675 (interactive "sColor: ")
dc6d9681 676 (modify-frame-parameters (selected-frame)
1787c22a
RS
677 (list (cons 'background-color color-name)))
678 (frame-update-face-colors (selected-frame)))
64c669bc 679
8290babd 680(defun set-foreground-color (color-name)
7eadab74 681 "Set the foreground color of the selected frame to COLOR.
61298010
RS
682When called interactively, prompt for the name of the color to use.
683To get the frame's current foreground color, use `frame-parameters'."
64c669bc 684 (interactive "sColor: ")
dc6d9681 685 (modify-frame-parameters (selected-frame)
1787c22a
RS
686 (list (cons 'foreground-color color-name)))
687 (frame-update-face-colors (selected-frame)))
64c669bc
JB
688
689(defun set-cursor-color (color-name)
7eadab74 690 "Set the text cursor color of the selected frame to COLOR.
61298010
RS
691When called interactively, prompt for the name of the color to use.
692To get the frame's current cursor color, use `frame-parameters'."
64c669bc 693 (interactive "sColor: ")
dc6d9681 694 (modify-frame-parameters (selected-frame)
7eadab74 695 (list (cons 'cursor-color color-name))))
64c669bc 696
eaa974e1 697(defun set-mouse-color (color-name)
7eadab74 698 "Set the color of the mouse pointer of the selected frame to COLOR.
61298010
RS
699When called interactively, prompt for the name of the color to use.
700To get the frame's current mouse color, use `frame-parameters'."
64c669bc 701 (interactive "sColor: ")
dc6d9681 702 (modify-frame-parameters (selected-frame)
7eadab74
JB
703 (list (cons 'mouse-color color-name))))
704
eaa974e1
JB
705(defun set-border-color (color-name)
706 "Set the color of the border of the selected frame to COLOR.
61298010
RS
707When called interactively, prompt for the name of the color to use.
708To get the frame's current border color, use `frame-parameters'."
eaa974e1
JB
709 (interactive "sColor: ")
710 (modify-frame-parameters (selected-frame)
711 (list (cons 'border-color color-name))))
712
713(defun auto-raise-mode (arg)
7eadab74 714 "Toggle whether or not the selected frame should auto-raise.
79e6ae33
RS
715With arg, turn auto-raise mode on if and only if arg is positive.
716Note that this controls Emacs's own auto-raise feature.
717Some window managers allow you to enable auto-raise for certain windows.
718You can use that for Emacs windows if you wish, but if you do,
719that is beyond the control of Emacs and this command has no effect on it."
7eadab74
JB
720 (interactive "P")
721 (if (null arg)
722 (setq arg
723 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
724 -1 1)))
dc6d9681 725 (modify-frame-parameters (selected-frame)
7eadab74
JB
726 (list (cons 'auto-raise (> arg 0)))))
727
eaa974e1 728(defun auto-lower-mode (arg)
7eadab74 729 "Toggle whether or not the selected frame should auto-lower.
79e6ae33
RS
730With arg, turn auto-lower mode on if and only if arg is positive.
731Note that this controls Emacs's own auto-lower feature.
732Some window managers allow you to enable auto-lower for certain windows.
733You can use that for Emacs windows if you wish, but if you do,
734that is beyond the control of Emacs and this command has no effect on it."
7eadab74
JB
735 (interactive "P")
736 (if (null arg)
737 (setq arg
738 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
739 -1 1)))
dc6d9681 740 (modify-frame-parameters (selected-frame)
7eadab74 741 (list (cons 'auto-lower (> arg 0)))))
7777d03b
EZ
742(defun set-frame-name (name)
743 "Set the name of the selected frame to NAME.
744When called interactively, prompt for the name of the frame.
745The frame name is displayed on the modeline if the terminal displays only
746one frame, otherwise the name is displayed on the frame's caption bar."
747 (interactive "sFrame name: ")
748 (modify-frame-parameters (selected-frame)
749 (list (cons 'name name))))
64c669bc 750\f
dc6d9681 751;;;; Aliases for backward compatibility with Emacs 18.
31e1d920
ER
752(defalias 'screen-height 'frame-height)
753(defalias 'screen-width 'frame-width)
9e2b097b
JB
754
755(defun set-screen-width (cols &optional pretend)
756 "Obsolete function to change the size of the screen to COLS columns.\n\
757Optional second arg non-nil means that redisplay should use COLS columns\n\
758but that the idea of the actual width of the frame should not be changed.\n\
759This function is provided only for compatibility with Emacs 18; new code\n\
fe668515 760should use `set-frame-width instead'."
9e2b097b
JB
761 (set-frame-width (selected-frame) cols pretend))
762
763(defun set-screen-height (lines &optional pretend)
764 "Obsolete function to change the height of the screen to LINES lines.\n\
765Optional second arg non-nil means that redisplay should use LINES lines\n\
766but that the idea of the actual height of the screen should not be changed.\n\
767This function is provided only for compatibility with Emacs 18; new code\n\
ef5a6345 768should use `set-frame-height' instead."
9e2b097b
JB
769 (set-frame-height (selected-frame) lines pretend))
770
771(make-obsolete 'screen-height 'frame-height)
772(make-obsolete 'screen-width 'frame-width)
773(make-obsolete 'set-screen-width 'set-frame-width)
774(make-obsolete 'set-screen-height 'set-frame-height)
dc6d9681
JB
775
776\f
81b99826
GM
777;;; Highlighting trailing whitespace.
778
779(make-variable-buffer-local 'show-trailing-whitespace)
780
781(defcustom show-trailing-whitespace nil
782 "*Non-nil means highlight trailing whitespace in face `trailing-whitespace'."
783 :tag "Highlight trailing whitespace."
784 :set #'(lambda (symbol value) (set-default symbol value))
785 :type 'boolean
786 :group 'font-lock)
787
788
789\f
790;;; Blinking cursor
791
792(defgroup cursor nil
793 "Cursor on frames."
794 :group 'frames)
795
796(defcustom blink-cursor-delay 0.5
797 "*Seconds of Emacs idle time after which cursor starts to blink."
798 :tag "Delay in seconds."
799 :type 'number
800 :group 'cursor)
801
802(defcustom blink-cursor-interval 0.5
803 "*Length of cursor blink interval in seconds."
804 :tag "Blink interval in seconds."
805 :type 'number
806 :group 'cursor)
807
808(defvar blink-cursor-idle-timer nil
809 "Timer started after blink-cursor-delay seconds of Emacs idle time.
810The function blink-cursor-start is called when the timer fires.")
811
812(defvar blink-cursor-timer nil
813 "Time started from blink-cursor-start. This timer calls blink-cursor
814every blink-cursor-interval seconds.")
815
816(defvar blink-cursor-mode nil
817 "Non-nil means blinking cursor is active.")
818
819(defun blink-cursor-mode (arg)
820 "Toggle blinking cursor mode.
821With arg, turn blinking cursor mode on iff arg is positive.
822When blinking cursor mode is enabled, the cursor of the selected
823window blinks."
824 (interactive "P")
825 (let ((on-p (if (null arg)
826 (not blink-cursor-mode)
827 (> (prefix-numeric-value arg) 0))))
828 (if blink-cursor-idle-timer
829 (cancel-timer blink-cursor-idle-timer))
830 (if blink-cursor-timer
831 (cancel-timer blink-cursor-timer))
832 (setq blink-cursor-idle-timer nil
833 blink-cursor-timer nil
834 blink-cursor-mode nil)
835 (if on-p
836 (progn
837 ;; Hide the cursor.
838 (show-cursor 0)
839 (setq blink-cursor-idle-timer
840 (run-with-idle-timer blink-cursor-delay
841 blink-cursor-delay
842 'blink-cursor-start))
843 (setq blink-cursor-mode t)))))
844
845(defcustom blink-cursor t
846 "*Non-nil means blink-cursor-mode is active."
847 :tag "Blinking cursor"
848 :type 'boolean
849 :group 'cursor
850 :set #'(lambda (symbol value)
851 (set-default symbol value)
852 (blink-cursor-mode (or value 0))))
853
854(defun blink-cursor-start ()
855 "Timer function called from timer blink-cursor-idle-timer.
856Starts the timer blink-cursor-timer which lets the cursor blink
857if the selected frame has a non-nil `cursor-blinking' frame parameter.
858Arranges to cancel that timer by installing a pre command hook."
859 (when (null blink-cursor-timer)
860 (add-hook 'pre-command-hook 'blink-cursor-end)
861 (setq blink-cursor-timer
862 (run-with-timer blink-cursor-interval blink-cursor-interval
863 'show-cursor))))
864
865(defun blink-cursor-end ()
866 "Stop cursor blinking.
867Installed as a pre-command hook by blink-cursor-start. Cancels
868the timer blink-cursor-timer and removes itself from the hook."
869 (remove-hook 'pre-command-hook 'blink-cursor-end)
870 (show-cursor 0)
871 (cancel-timer blink-cursor-timer)
872 (setq blink-cursor-timer nil))
873
874
875\f
876;;; Busy-cursor.
877
878(defcustom busy-cursor t
879 "*Non-nil means show a busy-cursor when running under a window-system."
880 :tag "Busy-cursor"
881 :type 'boolean
882 :group 'cursor
883 :get #'(lambda (symbol) display-busy-cursor)
884 :set #'(lambda (symbol value)
885 (set-default symbol value)
886 (setq display-busy-cursor value)))
887
888
889\f
64c669bc 890;;;; Key bindings
64c669bc 891
6238bfaf 892(define-key ctl-x-5-map "2" 'make-frame-command)
dc6d9681 893(define-key ctl-x-5-map "0" 'delete-frame)
ceab6935 894(define-key ctl-x-5-map "o" 'other-frame)
49116ac0 895
dc6d9681 896(provide 'frame)
c88ab9ce 897
dc6d9681 898;;; frame.el ends here