src/ChangeLog: Fix changers name.
[emacs.git] / src / terminal.c
blob2c0c60e73454a75bcd2defac7b00932d3f4a00e9
1 /* Functions related to terminal devices.
2 Copyright (C) 2005-2012 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 #include <config.h>
21 #define TERMHOOKS_INLINE EXTERN_INLINE
23 #include <stdio.h>
25 #include "lisp.h"
26 #include "frame.h"
27 #include "termchar.h"
28 #include "termhooks.h"
29 #include "charset.h"
30 #include "coding.h"
31 #include "keyboard.h"
33 /* Chain of all terminals currently in use. */
34 struct terminal *terminal_list;
36 /* The first unallocated terminal id. */
37 static int next_terminal_id;
39 /* The initial terminal device, created by initial_term_init. */
40 struct terminal *initial_terminal;
42 static void delete_initial_terminal (struct terminal *);
44 /* This setter is used only in this file, so it can be private. */
45 static void
46 tset_param_alist (struct terminal *t, Lisp_Object val)
48 t->param_alist = val;
53 void
54 ring_bell (struct frame *f)
56 if (!NILP (Vring_bell_function))
58 Lisp_Object function;
60 /* Temporarily set the global variable to nil
61 so that if we get an error, it stays nil
62 and we don't call it over and over.
64 We don't specbind it, because that would carefully
65 restore the bad value if there's an error
66 and make the loop of errors happen anyway. */
68 function = Vring_bell_function;
69 Vring_bell_function = Qnil;
71 call0 (function);
73 Vring_bell_function = function;
75 else if (FRAME_TERMINAL (f)->ring_bell_hook)
76 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
79 void
80 update_begin (struct frame *f)
82 if (FRAME_TERMINAL (f)->update_begin_hook)
83 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
86 void
87 update_end (struct frame *f)
89 if (FRAME_TERMINAL (f)->update_end_hook)
90 (*FRAME_TERMINAL (f)->update_end_hook) (f);
93 /* Specify how many text lines, from the top of the window,
94 should be affected by insert-lines and delete-lines operations.
95 This, and those operations, are used only within an update
96 that is bounded by calls to update_begin and update_end. */
98 void
99 set_terminal_window (struct frame *f, int size)
101 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
102 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
105 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
106 frame-relative coordinates. */
108 void
109 cursor_to (struct frame *f, int vpos, int hpos)
111 if (FRAME_TERMINAL (f)->cursor_to_hook)
112 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
115 /* Similar but don't take any account of the wasted characters. */
117 void
118 raw_cursor_to (struct frame *f, int row, int col)
120 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
121 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
124 /* Erase operations */
126 /* Clear from cursor to end of frame. */
127 void
128 clear_to_end (struct frame *f)
130 if (FRAME_TERMINAL (f)->clear_to_end_hook)
131 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
134 /* Clear entire frame */
136 void
137 clear_frame (struct frame *f)
139 if (FRAME_TERMINAL (f)->clear_frame_hook)
140 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
143 /* Clear from cursor to end of line.
144 Assume that the line is already clear starting at column first_unused_hpos.
146 Note that the cursor may be moved, on terminals lacking a `ce' string. */
148 void
149 clear_end_of_line (struct frame *f, int first_unused_hpos)
151 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
152 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
155 /* Output LEN glyphs starting at STRING at the nominal cursor position.
156 Advance the nominal cursor over the text. */
158 void
159 write_glyphs (struct frame *f, struct glyph *string, int len)
161 if (FRAME_TERMINAL (f)->write_glyphs_hook)
162 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
165 /* Insert LEN glyphs from START at the nominal cursor position.
167 If start is zero, insert blanks instead of a string at start */
169 void
170 insert_glyphs (struct frame *f, struct glyph *start, int len)
172 if (len <= 0)
173 return;
175 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
176 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
179 /* Delete N glyphs at the nominal cursor position. */
181 void
182 delete_glyphs (struct frame *f, int n)
184 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
185 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
188 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
190 void
191 ins_del_lines (struct frame *f, int vpos, int n)
193 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
194 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
200 /* Return the terminal object specified by TERMINAL. TERMINAL may be
201 a terminal object, a frame, or nil for the terminal device of the
202 current frame. If THROW is zero, return NULL for failure,
203 otherwise throw an error. */
205 struct terminal *
206 get_terminal (Lisp_Object terminal, int throw)
208 struct terminal *result = NULL;
210 if (NILP (terminal))
211 terminal = selected_frame;
213 if (TERMINALP (terminal))
214 result = XTERMINAL (terminal);
215 else if (FRAMEP (terminal))
216 result = FRAME_TERMINAL (XFRAME (terminal));
218 if (result && !result->name)
219 result = NULL;
221 if (result == NULL && throw)
222 wrong_type_argument (Qterminal_live_p, terminal);
224 return result;
229 /* Create a new terminal object and add it to the terminal list. */
231 struct terminal *
232 create_terminal (void)
234 struct terminal *terminal = allocate_terminal ();
235 Lisp_Object terminal_coding, keyboard_coding;
237 terminal->next_terminal = terminal_list;
238 terminal_list = terminal;
240 terminal->id = next_terminal_id++;
242 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
243 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
245 /* If default coding systems for the terminal and the keyboard are
246 already defined, use them in preference to the defaults. This is
247 needed when Emacs runs in daemon mode. */
248 keyboard_coding =
249 find_symbol_value (intern ("default-keyboard-coding-system"));
250 if (NILP (keyboard_coding)
251 || EQ (keyboard_coding, Qunbound)
252 || NILP (Fcoding_system_p (keyboard_coding)))
253 keyboard_coding = Qno_conversion;
254 terminal_coding =
255 find_symbol_value (intern ("default-terminal-coding-system"));
256 if (NILP (terminal_coding)
257 || EQ (terminal_coding, Qunbound)
258 || NILP (Fcoding_system_p (terminal_coding)))
259 terminal_coding = Qundecided;
261 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
262 setup_coding_system (terminal_coding, terminal->terminal_coding);
264 return terminal;
267 /* Low-level function to close all frames on a terminal, remove it
268 from the terminal list and free its memory. */
270 void
271 delete_terminal (struct terminal *terminal)
273 struct terminal **tp;
274 Lisp_Object tail, frame;
276 /* Protect against recursive calls. delete_frame calls the
277 delete_terminal_hook when we delete our last frame. */
278 if (!terminal->name)
279 return;
280 xfree (terminal->name);
281 terminal->name = NULL;
283 /* Check for live frames that are still on this terminal. */
284 FOR_EACH_FRAME (tail, frame)
286 struct frame *f = XFRAME (frame);
287 if (FRAME_LIVE_P (f) && f->terminal == terminal)
289 /* Pass Qnoelisp rather than Qt. */
290 delete_frame (frame, Qnoelisp);
294 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
295 if (! *tp)
296 emacs_abort ();
297 *tp = terminal->next_terminal;
299 xfree (terminal->keyboard_coding);
300 terminal->keyboard_coding = NULL;
301 xfree (terminal->terminal_coding);
302 terminal->terminal_coding = NULL;
304 if (terminal->kboard && --terminal->kboard->reference_count == 0)
306 delete_kboard (terminal->kboard);
307 terminal->kboard = NULL;
311 Lisp_Object Qrun_hook_with_args;
312 static Lisp_Object Qdelete_terminal_functions;
313 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
314 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
315 TERMINAL may be a terminal object, a frame, or nil (meaning the
316 selected frame's terminal).
318 Normally, you may not delete a display if all other displays are suspended,
319 but if the second argument FORCE is non-nil, you may do so. */)
320 (Lisp_Object terminal, Lisp_Object force)
322 struct terminal *t = get_terminal (terminal, 0);
324 if (!t)
325 return Qnil;
327 if (NILP (force))
329 struct terminal *p = terminal_list;
330 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
331 p = p->next_terminal;
333 if (!p)
334 error ("Attempt to delete the sole active display terminal");
337 if (NILP (Vrun_hooks))
339 else if (EQ (force, Qnoelisp))
340 pending_funcalls
341 = Fcons (list3 (Qrun_hook_with_args,
342 Qdelete_terminal_functions, terminal),
343 pending_funcalls);
344 else
345 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
347 if (t->delete_terminal_hook)
348 (*t->delete_terminal_hook) (t);
349 else
350 delete_terminal (t);
352 return Qnil;
356 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
357 doc: /* Return the terminal that FRAME is displayed on.
358 If FRAME is nil, the selected frame is used.
360 The terminal device is represented by its integer identifier. */)
361 (Lisp_Object frame)
363 struct terminal *t;
365 if (NILP (frame))
366 frame = selected_frame;
368 CHECK_LIVE_FRAME (frame);
370 t = FRAME_TERMINAL (XFRAME (frame));
372 if (!t)
373 return Qnil;
374 else
376 Lisp_Object terminal;
377 XSETTERMINAL (terminal, t);
378 return terminal;
382 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
383 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
384 Value is nil if OBJECT is not a live display terminal.
385 If object is a live display terminal, the return value indicates what
386 sort of output terminal it uses. See the documentation of `framep' for
387 possible return values. */)
388 (Lisp_Object object)
390 struct terminal *t;
392 t = get_terminal (object, 0);
394 if (!t)
395 return Qnil;
397 switch (t->type)
399 case output_initial: /* The initial frame is like a termcap frame. */
400 case output_termcap:
401 return Qt;
402 case output_x_window:
403 return Qx;
404 case output_w32:
405 return Qw32;
406 case output_msdos_raw:
407 return Qpc;
408 case output_mac:
409 return Qmac;
410 case output_ns:
411 return Qns;
412 default:
413 emacs_abort ();
417 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
418 doc: /* Return a list of all terminal devices. */)
419 (void)
421 Lisp_Object terminal, terminals = Qnil;
422 struct terminal *t;
424 for (t = terminal_list; t; t = t->next_terminal)
426 XSETTERMINAL (terminal, t);
427 terminals = Fcons (terminal, terminals);
430 return terminals;
433 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
434 doc: /* Return the name of the terminal device TERMINAL.
435 It is not guaranteed that the returned value is unique among opened devices.
437 TERMINAL may be a terminal object, a frame, or nil (meaning the
438 selected frame's terminal). */)
439 (Lisp_Object terminal)
441 struct terminal *t
442 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
444 return t->name ? build_string (t->name) : Qnil;
449 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
450 Return the previous value. */
452 static Lisp_Object
453 store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
455 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
456 if (EQ (old_alist_elt, Qnil))
458 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
459 return Qnil;
461 else
463 Lisp_Object result = Fcdr (old_alist_elt);
464 Fsetcdr (old_alist_elt, value);
465 return result;
470 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
471 doc: /* Return the parameter-alist of terminal TERMINAL.
472 The value is a list of elements of the form (PARM . VALUE), where PARM
473 is a symbol.
475 TERMINAL can be a terminal object, a frame, or nil (meaning the
476 selected frame's terminal). */)
477 (Lisp_Object terminal)
479 struct terminal *t
480 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
481 return Fcopy_alist (t->param_alist);
484 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
485 doc: /* Return TERMINAL's value for parameter PARAMETER.
486 TERMINAL can be a terminal object, a frame, or nil (meaning the
487 selected frame's terminal). */)
488 (Lisp_Object terminal, Lisp_Object parameter)
490 Lisp_Object value;
491 struct terminal *t
492 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
493 CHECK_SYMBOL (parameter);
494 value = Fcdr (Fassq (parameter, t->param_alist));
495 return value;
498 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
499 Sset_terminal_parameter, 3, 3, 0,
500 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
501 Return the previous value of PARAMETER.
503 TERMINAL can be a terminal object, a frame or nil (meaning the
504 selected frame's terminal). */)
505 (Lisp_Object terminal, Lisp_Object parameter, Lisp_Object value)
507 struct terminal *t
508 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
509 return store_terminal_param (t, parameter, value);
514 /* Create the bootstrap display terminal for the initial frame.
515 Returns a terminal of type output_initial. */
517 struct terminal *
518 init_initial_terminal (void)
520 if (initialized || terminal_list || tty_list)
521 emacs_abort ();
523 initial_terminal = create_terminal ();
524 initial_terminal->type = output_initial;
525 initial_terminal->name = xstrdup ("initial_terminal");
526 initial_terminal->kboard = initial_kboard;
527 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
528 /* All other hooks are NULL. */
530 return initial_terminal;
533 /* Deletes the bootstrap terminal device.
534 Called through delete_terminal_hook. */
536 static void
537 delete_initial_terminal (struct terminal *terminal)
539 if (terminal != initial_terminal)
540 emacs_abort ();
542 delete_terminal (terminal);
543 initial_terminal = NULL;
546 void
547 syms_of_terminal (void)
550 DEFVAR_LISP ("ring-bell-function", Vring_bell_function,
551 doc: /* Non-nil means call this function to ring the bell.
552 The function should accept no arguments. */);
553 Vring_bell_function = Qnil;
555 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions,
556 doc: /* Special hook run when a terminal is deleted.
557 Each function is called with argument, the terminal.
558 This may be called just before actually deleting the terminal,
559 or some time later. */);
560 Vdelete_terminal_functions = Qnil;
561 DEFSYM (Qdelete_terminal_functions, "delete-terminal-functions");
562 DEFSYM (Qrun_hook_with_args, "run-hook-with-args");
564 defsubr (&Sdelete_terminal);
565 defsubr (&Sframe_terminal);
566 defsubr (&Sterminal_live_p);
567 defsubr (&Sterminal_list);
568 defsubr (&Sterminal_name);
569 defsubr (&Sterminal_parameters);
570 defsubr (&Sterminal_parameter);
571 defsubr (&Sset_terminal_parameter);
573 Fprovide (intern_c_string ("multi-tty"), Qnil);