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