* frame.c (delete_frame): New function derived from
[bpt/emacs.git] / src / terminal.c
CommitLineData
ed8dad6b 1/* Functions related to terminal devices.
dcb8ac09 2 Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
ed8dad6b
KL
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
ed8dad6b 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
ed8dad6b
KL
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
ed8dad6b
KL
18
19#include <config.h>
60f5c938 20#include <stdio.h>
ed8dad6b
KL
21
22#include "lisp.h"
23#include "frame.h"
24#include "termchar.h"
25#include "termhooks.h"
26#include "charset.h"
27#include "coding.h"
28#include "keyboard.h"
29
6ed8eeff
KL
30/* Chain of all terminals currently in use. */
31struct terminal *terminal_list;
ed8dad6b 32
6ed8eeff
KL
33/* The first unallocated terminal id. */
34static int next_terminal_id;
ed8dad6b 35
6ed8eeff
KL
36/* The initial terminal device, created by initial_term_init. */
37struct terminal *initial_terminal;
ed8dad6b
KL
38
39/* Function to use to ring the bell. */
40Lisp_Object Vring_bell_function;
41
6ed8eeff 42static void delete_initial_terminal P_ ((struct terminal *));
ed8dad6b
KL
43
44\f
45
46void
47ring_bell (struct frame *f)
48{
49 if (!NILP (Vring_bell_function))
50 {
51 Lisp_Object function;
52
53 /* Temporarily set the global variable to nil
54 so that if we get an error, it stays nil
55 and we don't call it over and over.
56
57 We don't specbind it, because that would carefully
58 restore the bad value if there's an error
59 and make the loop of errors happen anyway. */
60
61 function = Vring_bell_function;
62 Vring_bell_function = Qnil;
63
64 call0 (function);
65
66 Vring_bell_function = function;
67 }
6ed8eeff
KL
68 else if (FRAME_TERMINAL (f)->ring_bell_hook)
69 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
ed8dad6b
KL
70}
71
72void
73update_begin (struct frame *f)
74{
6ed8eeff
KL
75 if (FRAME_TERMINAL (f)->update_begin_hook)
76 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
ed8dad6b
KL
77}
78
79void
80update_end (struct frame *f)
81{
6ed8eeff
KL
82 if (FRAME_TERMINAL (f)->update_end_hook)
83 (*FRAME_TERMINAL (f)->update_end_hook) (f);
ed8dad6b
KL
84}
85
86/* Specify how many text lines, from the top of the window,
87 should be affected by insert-lines and delete-lines operations.
88 This, and those operations, are used only within an update
89 that is bounded by calls to update_begin and update_end. */
90
91void
92set_terminal_window (struct frame *f, int size)
93{
6ed8eeff
KL
94 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
95 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
ed8dad6b
KL
96}
97
98/* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
99 frame-relative coordinates. */
100
101void
102cursor_to (struct frame *f, int vpos, int hpos)
103{
6ed8eeff
KL
104 if (FRAME_TERMINAL (f)->cursor_to_hook)
105 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
ed8dad6b
KL
106}
107
108/* Similar but don't take any account of the wasted characters. */
109
110void
111raw_cursor_to (struct frame *f, int row, int col)
112{
6ed8eeff
KL
113 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
114 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
ed8dad6b
KL
115}
116
117/* Erase operations */
118
119/* Clear from cursor to end of frame. */
120void
121clear_to_end (struct frame *f)
122{
6ed8eeff
KL
123 if (FRAME_TERMINAL (f)->clear_to_end_hook)
124 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
ed8dad6b
KL
125}
126
127/* Clear entire frame */
128
129void
130clear_frame (struct frame *f)
131{
6ed8eeff
KL
132 if (FRAME_TERMINAL (f)->clear_frame_hook)
133 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
ed8dad6b
KL
134}
135
136/* Clear from cursor to end of line.
137 Assume that the line is already clear starting at column first_unused_hpos.
138
139 Note that the cursor may be moved, on terminals lacking a `ce' string. */
140
141void
142clear_end_of_line (struct frame *f, int first_unused_hpos)
143{
6ed8eeff
KL
144 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
145 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
ed8dad6b
KL
146}
147
148/* Output LEN glyphs starting at STRING at the nominal cursor position.
149 Advance the nominal cursor over the text. */
150
151void
152write_glyphs (struct frame *f, struct glyph *string, int len)
153{
6ed8eeff
KL
154 if (FRAME_TERMINAL (f)->write_glyphs_hook)
155 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
ed8dad6b
KL
156}
157
158/* Insert LEN glyphs from START at the nominal cursor position.
159
160 If start is zero, insert blanks instead of a string at start */
161
162void
163insert_glyphs (struct frame *f, struct glyph *start, int len)
164{
165 if (len <= 0)
166 return;
167
6ed8eeff
KL
168 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
169 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
ed8dad6b
KL
170}
171
172/* Delete N glyphs at the nominal cursor position. */
173
174void
175delete_glyphs (struct frame *f, int n)
176{
6ed8eeff
KL
177 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
178 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
ed8dad6b
KL
179}
180
181/* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
182
183void
184ins_del_lines (struct frame *f, int vpos, int n)
185{
6ed8eeff
KL
186 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
187 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
ed8dad6b
KL
188}
189
190
191\f
192
6ed8eeff
KL
193/* Return the terminal object specified by TERMINAL. TERMINAL may be a
194 terminal id, a frame, or nil for the terminal device of the current
ed8dad6b
KL
195 frame. If THROW is zero, return NULL for failure, otherwise throw
196 an error. */
197
6ed8eeff
KL
198struct terminal *
199get_terminal (Lisp_Object terminal, int throw)
ed8dad6b 200{
6ed8eeff 201 struct terminal *result = NULL;
ed8dad6b 202
6ed8eeff
KL
203 if (NILP (terminal))
204 terminal = selected_frame;
ed8dad6b 205
d30a25b3
SM
206 if (TERMINALP (terminal))
207 result = XTERMINAL (terminal);
208
6ed8eeff 209 else if (FRAMEP (terminal))
ed8dad6b 210 {
6ed8eeff 211 result = FRAME_TERMINAL (XFRAME (terminal));
ed8dad6b
KL
212 }
213
d30a25b3
SM
214 if (result && !result->name)
215 result = NULL;
216
ed8dad6b 217 if (result == NULL && throw)
6ed8eeff 218 wrong_type_argument (Qterminal_live_p, terminal);
ed8dad6b
KL
219
220 return result;
221}
222
223\f
224
6ed8eeff 225/* Create a new terminal object and add it to the terminal list. */
ed8dad6b 226
6ed8eeff
KL
227struct terminal *
228create_terminal (void)
ed8dad6b 229{
d30a25b3
SM
230 struct terminal *terminal = allocate_terminal ();
231
232 terminal->name = NULL;
6ed8eeff
KL
233 terminal->next_terminal = terminal_list;
234 terminal_list = terminal;
ed8dad6b 235
6ed8eeff 236 terminal->id = next_terminal_id++;
ed8dad6b 237
6ed8eeff 238 terminal->keyboard_coding =
ed8dad6b 239 (struct coding_system *) xmalloc (sizeof (struct coding_system));
6ed8eeff 240 terminal->terminal_coding =
ed8dad6b
KL
241 (struct coding_system *) xmalloc (sizeof (struct coding_system));
242
985773c9
MB
243 setup_coding_system (Qno_conversion, terminal->keyboard_coding);
244 setup_coding_system (Qundecided, terminal->terminal_coding);
ed8dad6b 245
6ed8eeff
KL
246 terminal->param_alist = Qnil;
247 return terminal;
ed8dad6b
KL
248}
249
ab797f65
KL
250/* Low-level function to close all frames on a terminal, remove it
251 from the terminal list and free its memory. */
ed8dad6b
KL
252
253void
6ed8eeff 254delete_terminal (struct terminal *terminal)
ed8dad6b 255{
6ed8eeff 256 struct terminal **tp;
ed8dad6b 257 Lisp_Object tail, frame;
a98f1617 258
56f2de10 259 /* Protect against recursive calls. delete_frame calls the
ab797f65 260 delete_terminal_hook when we delete our last frame. */
d30a25b3 261 if (!terminal->name)
a98f1617 262 return;
d30a25b3
SM
263 xfree (terminal->name);
264 terminal->name = NULL;
a98f1617 265
ab797f65 266 /* Check for live frames that are still on this terminal. */
ed8dad6b
KL
267 FOR_EACH_FRAME (tail, frame)
268 {
269 struct frame *f = XFRAME (frame);
6ed8eeff 270 if (FRAME_LIVE_P (f) && f->terminal == terminal)
ed8dad6b 271 {
56f2de10
MR
272 /* Pass Qnoelisp rather than Qt. */
273 delete_frame (frame, Qnoelisp);
ed8dad6b
KL
274 }
275 }
276
6ed8eeff
KL
277 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
278 if (! *tp)
ed8dad6b 279 abort ();
6ed8eeff
KL
280 *tp = terminal->next_terminal;
281
d30a25b3
SM
282 xfree (terminal->keyboard_coding);
283 terminal->keyboard_coding = NULL;
284 xfree (terminal->terminal_coding);
285 terminal->terminal_coding = NULL;
56f2de10 286
6ed8eeff 287 if (terminal->kboard && --terminal->kboard->reference_count == 0)
d30a25b3
SM
288 {
289 delete_kboard (terminal->kboard);
290 terminal->kboard = NULL;
291 }
ed8dad6b
KL
292}
293
58555d81
SM
294Lisp_Object Qrun_hook_with_args;
295static Lisp_Object Qdelete_terminal_functions;
296static Lisp_Object Vdelete_terminal_functions;
297
6ed8eeff
KL
298DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
299 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
300TERMINAL may be a terminal id, a frame, or nil (meaning the selected
301frame's terminal).
ed8dad6b
KL
302
303Normally, you may not delete a display if all other displays are suspended,
304but if the second argument FORCE is non-nil, you may do so. */)
6ed8eeff
KL
305 (terminal, force)
306 Lisp_Object terminal, force;
ed8dad6b 307{
5b8de9c5 308 struct terminal *t = get_terminal (terminal, 0);
ed8dad6b 309
6ed8eeff 310 if (!t)
ed8dad6b
KL
311 return Qnil;
312
5b8de9c5
SM
313 if (NILP (force))
314 {
315 struct terminal *p = terminal_list;
316 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
317 p = p->next_terminal;
56f2de10 318
5b8de9c5
SM
319 if (!p)
320 error ("Attempt to delete the sole active display terminal");
321 }
ed8dad6b 322
58555d81
SM
323 if (NILP (Vrun_hooks))
324 ;
325 else if (EQ (force, Qnoelisp))
326 pending_funcalls
327 = Fcons (list3 (Qrun_hook_with_args,
328 Qdelete_terminal_functions, terminal),
329 pending_funcalls);
330 else
331 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
332
6ed8eeff
KL
333 if (t->delete_terminal_hook)
334 (*t->delete_terminal_hook) (t);
ed8dad6b 335 else
6ed8eeff 336 delete_terminal (t);
ed8dad6b
KL
337
338 return Qnil;
339}
340
6ed8eeff
KL
341\f
342DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
343 doc: /* Return the terminal that FRAME is displayed on.
344If FRAME is nil, the selected frame is used.
345
346The terminal device is represented by its integer identifier. */)
347 (frame)
348 Lisp_Object frame;
349{
350 struct terminal *t;
351
352 if (NILP (frame))
353 frame = selected_frame;
354
355 CHECK_LIVE_FRAME (frame);
356
d30a25b3 357 t = FRAME_TERMINAL (XFRAME (frame));
6ed8eeff
KL
358
359 if (!t)
360 return Qnil;
361 else
d30a25b3
SM
362 {
363 Lisp_Object terminal;
364 XSETTERMINAL (terminal, t);
365 return terminal;
366 }
6ed8eeff
KL
367}
368
369DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
370 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
371Value is nil if OBJECT is not a live display terminal.
372If object is a live display terminal, the return value indicates what
373sort of output terminal it uses. See the documentation of `framep' for
d30a25b3 374possible return values. */)
ed8dad6b
KL
375 (object)
376 Lisp_Object object;
377{
6ed8eeff 378 struct terminal *t;
56f2de10 379
6ed8eeff 380 t = get_terminal (object, 0);
ed8dad6b 381
6ed8eeff 382 if (!t)
ed8dad6b
KL
383 return Qnil;
384
6ed8eeff 385 switch (t->type)
ed8dad6b
KL
386 {
387 case output_initial: /* The initial frame is like a termcap frame. */
388 case output_termcap:
389 return Qt;
390 case output_x_window:
391 return Qx;
392 case output_w32:
393 return Qw32;
394 case output_msdos_raw:
395 return Qpc;
396 case output_mac:
397 return Qmac;
edfda783
AR
398 case output_ns:
399 return Qns;
ed8dad6b
KL
400 default:
401 abort ();
402 }
403}
404
6ed8eeff 405DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
d30a25b3 406 doc: /* Return a list of all terminal devices. */)
ed8dad6b
KL
407 ()
408{
d30a25b3 409 Lisp_Object terminal, terminals = Qnil;
6ed8eeff 410 struct terminal *t;
ed8dad6b 411
6ed8eeff 412 for (t = terminal_list; t; t = t->next_terminal)
d30a25b3
SM
413 {
414 XSETTERMINAL (terminal, t);
415 terminals = Fcons (terminal, terminals);
416 }
ed8dad6b 417
6ed8eeff 418 return terminals;
ed8dad6b
KL
419}
420
6ed8eeff
KL
421DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
422 doc: /* Return the name of the terminal device TERMINAL.
ed8dad6b
KL
423It is not guaranteed that the returned value is unique among opened devices.
424
6ed8eeff
KL
425TERMINAL may be a terminal id, a frame, or nil (meaning the
426selected frame's terminal). */)
427 (terminal)
428 Lisp_Object terminal;
ed8dad6b 429{
c5911e55
SM
430 struct terminal *t
431 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
ed8dad6b 432
c5911e55 433 return t->name ? build_string (t->name) : Qnil;
ed8dad6b
KL
434}
435
436
437\f
6ed8eeff 438/* Return the value of terminal parameter PARAM in terminal T. */
ed8dad6b 439Lisp_Object
6ed8eeff
KL
440get_terminal_param (t, param)
441 struct terminal *t;
ed8dad6b
KL
442 Lisp_Object param;
443{
6ed8eeff 444 Lisp_Object tem = Fassq (param, t->param_alist);
ed8dad6b
KL
445 if (EQ (tem, Qnil))
446 return tem;
447 return Fcdr (tem);
448}
449
6ed8eeff 450/* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
ed8dad6b
KL
451 Return the previous value. */
452
453Lisp_Object
6ed8eeff
KL
454store_terminal_param (t, parameter, value)
455 struct terminal *t;
ed8dad6b
KL
456 Lisp_Object parameter;
457 Lisp_Object value;
458{
6ed8eeff 459 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
ed8dad6b
KL
460 if (EQ (old_alist_elt, Qnil))
461 {
6ed8eeff 462 t->param_alist = Fcons (Fcons (parameter, value), t->param_alist);
ed8dad6b
KL
463 return Qnil;
464 }
465 else
466 {
467 Lisp_Object result = Fcdr (old_alist_elt);
468 Fsetcdr (old_alist_elt, value);
469 return result;
470 }
471}
472
473
474DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
475 doc: /* Return the parameter-alist of terminal TERMINAL.
476The value is a list of elements of the form (PARM . VALUE), where PARM
477is a symbol.
478
6ed8eeff 479TERMINAL can be a terminal id, a frame or nil (meaning the selected
ed8dad6b
KL
480frame's terminal). */)
481 (terminal)
482 Lisp_Object terminal;
483{
c5911e55
SM
484 struct terminal *t
485 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
6ed8eeff 486 return Fcopy_alist (t->param_alist);
ed8dad6b
KL
487}
488
489DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
490 doc: /* Return TERMINAL's value for parameter PARAMETER.
6ed8eeff 491TERMINAL can be a terminal id, a frame or nil (meaning the selected
ed8dad6b
KL
492frame's terminal). */)
493 (terminal, parameter)
494 Lisp_Object terminal;
495 Lisp_Object parameter;
496{
497 Lisp_Object value;
c5911e55
SM
498 struct terminal *t
499 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
ed8dad6b 500 CHECK_SYMBOL (parameter);
6ed8eeff 501 value = Fcdr (Fassq (parameter, t->param_alist));
ed8dad6b
KL
502 return value;
503}
504
ed8dad6b
KL
505DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
506 Sset_terminal_parameter, 3, 3, 0,
507 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
508Return the previous value of PARAMETER.
509
6ed8eeff 510TERMINAL can be a terminal id, a frame or nil (meaning the selected
ed8dad6b
KL
511frame's terminal). */)
512 (terminal, parameter, value)
513 Lisp_Object terminal;
514 Lisp_Object parameter;
515 Lisp_Object value;
516{
c5911e55
SM
517 struct terminal *t
518 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
6ed8eeff 519 return store_terminal_param (t, parameter, value);
ed8dad6b
KL
520}
521
522\f
523
6ed8eeff
KL
524/* Create the bootstrap display terminal for the initial frame.
525 Returns a terminal of type output_initial. */
ed8dad6b 526
6ed8eeff
KL
527struct terminal *
528init_initial_terminal (void)
ed8dad6b 529{
6ed8eeff 530 if (initialized || terminal_list || tty_list)
ed8dad6b
KL
531 abort ();
532
6ed8eeff
KL
533 initial_terminal = create_terminal ();
534 initial_terminal->type = output_initial;
535 initial_terminal->name = xstrdup ("initial_terminal");
536 initial_terminal->kboard = initial_kboard;
6ed8eeff 537 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
ed8dad6b
KL
538 /* All other hooks are NULL. */
539
6ed8eeff 540 return initial_terminal;
ed8dad6b
KL
541}
542
6ed8eeff
KL
543/* Deletes the bootstrap terminal device.
544 Called through delete_terminal_hook. */
ed8dad6b 545
6ed8eeff
KL
546static void
547delete_initial_terminal (struct terminal *terminal)
ed8dad6b 548{
6ed8eeff 549 if (terminal != initial_terminal)
ed8dad6b
KL
550 abort ();
551
6ed8eeff
KL
552 delete_terminal (terminal);
553 initial_terminal = NULL;
ed8dad6b
KL
554}
555
556void
557syms_of_terminal ()
558{
559
560 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
561 doc: /* Non-nil means call this function to ring the bell.
562The function should accept no arguments. */);
563 Vring_bell_function = Qnil;
564
58555d81
SM
565 DEFVAR_LISP ("delete-terminal-functions", &Vdelete_terminal_functions,
566 doc: /* Special hook run when a terminal is deleted.
567Each function is called with argument, the terminal.
568This may be called just before actually deleting the terminal,
569or some time later. */);
570 Vdelete_terminal_functions = Qnil;
571 Qdelete_terminal_functions = intern ("delete-terminal-functions");
572 staticpro (&Qdelete_terminal_functions);
573 Qrun_hook_with_args = intern ("run-hook-with-args");
574 staticpro (&Qrun_hook_with_args);
575
6ed8eeff
KL
576 defsubr (&Sdelete_terminal);
577 defsubr (&Sframe_terminal);
578 defsubr (&Sterminal_live_p);
579 defsubr (&Sterminal_list);
580 defsubr (&Sterminal_name);
ed8dad6b
KL
581 defsubr (&Sterminal_parameters);
582 defsubr (&Sterminal_parameter);
ed8dad6b
KL
583 defsubr (&Sset_terminal_parameter);
584
585 Fprovide (intern ("multi-tty"), Qnil);
586}
587
588/* arch-tag: e9af6f27-b483-47dc-bb1a-730c1c5cab03
589 (do not change this comment) */