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