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