ChangeLog fix: standardize some bug refs.
[emacs.git] / src / terminal.c
blob0cd6a0bf60271d14c4379fcdea1c0c8a9fa924ea
1 /* Functions related to terminal devices.
2 Copyright (C) 2005-2014 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 #include <stdio.h>
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"
31 /* Chain of all terminals currently in use. */
32 struct terminal *terminal_list;
34 /* The first unallocated terminal id. */
35 static int next_terminal_id;
37 /* The initial terminal device, created by initial_term_init. */
38 struct terminal *initial_terminal;
40 Lisp_Object Qrun_hook_with_args;
41 static Lisp_Object Qterminal_live_p;
42 static Lisp_Object Qdelete_terminal_functions;
44 static void delete_initial_terminal (struct terminal *);
46 /* This setter is used only in this file, so it can be private. */
47 static void
48 tset_param_alist (struct terminal *t, Lisp_Object val)
50 t->param_alist = val;
55 void
56 ring_bell (struct frame *f)
58 if (!NILP (Vring_bell_function))
60 Lisp_Object function;
62 /* Temporarily set the global variable to nil
63 so that if we get an error, it stays nil
64 and we don't call it over and over.
66 We don't specbind it, because that would carefully
67 restore the bad value if there's an error
68 and make the loop of errors happen anyway. */
70 function = Vring_bell_function;
71 Vring_bell_function = Qnil;
73 call0 (function);
75 Vring_bell_function = function;
77 else if (FRAME_TERMINAL (f)->ring_bell_hook)
78 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
81 void
82 update_begin (struct frame *f)
84 if (FRAME_TERMINAL (f)->update_begin_hook)
85 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
88 void
89 update_end (struct frame *f)
91 if (FRAME_TERMINAL (f)->update_end_hook)
92 (*FRAME_TERMINAL (f)->update_end_hook) (f);
95 /* Specify how many text lines, from the top of the window,
96 should be affected by insert-lines and delete-lines operations.
97 This, and those operations, are used only within an update
98 that is bounded by calls to update_begin and update_end. */
100 void
101 set_terminal_window (struct frame *f, int size)
103 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
104 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
107 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
108 frame-relative coordinates. */
110 void
111 cursor_to (struct frame *f, int vpos, int hpos)
113 if (FRAME_TERMINAL (f)->cursor_to_hook)
114 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
117 /* Similar but don't take any account of the wasted characters. */
119 void
120 raw_cursor_to (struct frame *f, int row, int col)
122 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
123 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
126 /* Erase operations. */
128 /* Clear from cursor to end of frame. */
129 void
130 clear_to_end (struct frame *f)
132 if (FRAME_TERMINAL (f)->clear_to_end_hook)
133 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
136 /* Clear entire frame. */
138 void
139 clear_frame (struct frame *f)
141 if (FRAME_TERMINAL (f)->clear_frame_hook)
142 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
145 /* Clear from cursor to end of line.
146 Assume that the line is already clear starting at column first_unused_hpos.
148 Note that the cursor may be moved, on terminals lacking a `ce' string. */
150 void
151 clear_end_of_line (struct frame *f, int first_unused_hpos)
153 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
154 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
157 /* Output LEN glyphs starting at STRING at the nominal cursor position.
158 Advance the nominal cursor over the text. */
160 void
161 write_glyphs (struct frame *f, struct glyph *string, int len)
163 if (FRAME_TERMINAL (f)->write_glyphs_hook)
164 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
167 /* Insert LEN glyphs from START at the nominal cursor position.
169 If start is zero, insert blanks instead of a string at start */
171 void
172 insert_glyphs (struct frame *f, struct glyph *start, int len)
174 if (len <= 0)
175 return;
177 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
178 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
181 /* Delete N glyphs at the nominal cursor position. */
183 void
184 delete_glyphs (struct frame *f, int n)
186 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
187 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
190 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
192 void
193 ins_del_lines (struct frame *f, int vpos, int n)
195 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
196 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
199 /* Return the terminal object specified by TERMINAL. TERMINAL may
200 be a terminal object, a frame, or nil for the terminal device of
201 the current frame. If TERMINAL is neither from the above or the
202 resulting terminal object is deleted, return NULL. */
204 static struct terminal *
205 decode_terminal (Lisp_Object terminal)
207 struct terminal *t;
209 if (NILP (terminal))
210 terminal = selected_frame;
211 t = (TERMINALP (terminal)
212 ? XTERMINAL (terminal)
213 : FRAMEP (terminal) ? FRAME_TERMINAL (XFRAME (terminal)) : NULL);
214 return t && t->name ? t : NULL;
217 /* Like above, but throw an error if TERMINAL is not valid or deleted. */
219 struct terminal *
220 decode_live_terminal (Lisp_Object terminal)
222 struct terminal *t = decode_terminal (terminal);
224 if (!t)
225 wrong_type_argument (Qterminal_live_p, terminal);
226 return t;
229 /* Like decode_terminal, but ensure that the resulting terminal object refers
230 to a text-based terminal device. */
232 struct terminal *
233 decode_tty_terminal (Lisp_Object terminal)
235 struct terminal *t = decode_live_terminal (terminal);
237 return (t->type == output_termcap || t->type == output_msdos_raw) ? t : NULL;
240 /* Return an active (not suspended) text-based terminal device that uses
241 the tty device with the given NAME, or NULL if the named terminal device
242 is not opened. */
244 struct terminal *
245 get_named_terminal (const char *name)
247 struct terminal *t;
249 eassert (name);
251 for (t = terminal_list; t; t = t->next_terminal)
253 if ((t->type == output_termcap || t->type == output_msdos_raw)
254 && !strcmp (t->display_info.tty->name, name)
255 && TERMINAL_ACTIVE_P (t))
256 return t;
258 return NULL;
261 /* Create a new terminal object of TYPE and add it to the terminal list. RIF
262 may be NULL if this terminal type doesn't support window-based redisplay. */
264 struct terminal *
265 create_terminal (enum output_method type, struct redisplay_interface *rif)
267 struct terminal *terminal = allocate_terminal ();
268 Lisp_Object terminal_coding, keyboard_coding;
270 terminal->next_terminal = terminal_list;
271 terminal_list = terminal;
272 terminal->type = type;
273 terminal->rif = rif;
274 terminal->id = next_terminal_id++;
276 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
277 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
279 /* If default coding systems for the terminal and the keyboard are
280 already defined, use them in preference to the defaults. This is
281 needed when Emacs runs in daemon mode. */
282 keyboard_coding =
283 find_symbol_value (intern ("default-keyboard-coding-system"));
284 if (NILP (keyboard_coding)
285 || EQ (keyboard_coding, Qunbound)
286 || NILP (Fcoding_system_p (keyboard_coding)))
287 keyboard_coding = Qno_conversion;
288 terminal_coding =
289 find_symbol_value (intern ("default-terminal-coding-system"));
290 if (NILP (terminal_coding)
291 || EQ (terminal_coding, Qunbound)
292 || NILP (Fcoding_system_p (terminal_coding)))
293 terminal_coding = Qundecided;
295 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
296 setup_coding_system (terminal_coding, terminal->terminal_coding);
298 return terminal;
301 /* Low-level function to close all frames on a terminal, remove it
302 from the terminal list and free its memory. */
304 void
305 delete_terminal (struct terminal *terminal)
307 struct terminal **tp;
308 Lisp_Object tail, frame;
310 /* Protect against recursive calls. delete_frame calls the
311 delete_terminal_hook when we delete our last frame. */
312 if (!terminal->name)
313 return;
314 xfree (terminal->name);
315 terminal->name = NULL;
317 /* Check for live frames that are still on this terminal. */
318 FOR_EACH_FRAME (tail, frame)
320 struct frame *f = XFRAME (frame);
321 if (FRAME_LIVE_P (f) && f->terminal == terminal)
323 /* Pass Qnoelisp rather than Qt. */
324 delete_frame (frame, Qnoelisp);
328 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
329 if (! *tp)
330 emacs_abort ();
331 *tp = terminal->next_terminal;
333 xfree (terminal->keyboard_coding);
334 terminal->keyboard_coding = NULL;
335 xfree (terminal->terminal_coding);
336 terminal->terminal_coding = NULL;
338 if (terminal->kboard && --terminal->kboard->reference_count == 0)
340 delete_kboard (terminal->kboard);
341 terminal->kboard = NULL;
345 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
346 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
347 TERMINAL may be a terminal object, a frame, or nil (meaning the
348 selected frame's terminal).
350 Normally, you may not delete a display if all other displays are suspended,
351 but if the second argument FORCE is non-nil, you may do so. */)
352 (Lisp_Object terminal, Lisp_Object force)
354 struct terminal *t = decode_terminal (terminal);
356 if (!t)
357 return Qnil;
359 if (NILP (force))
361 struct terminal *p = terminal_list;
362 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
363 p = p->next_terminal;
365 if (!p)
366 error ("Attempt to delete the sole active display terminal");
369 if (NILP (Vrun_hooks))
371 else if (EQ (force, Qnoelisp))
372 pending_funcalls
373 = Fcons (list3 (Qrun_hook_with_args,
374 Qdelete_terminal_functions, terminal),
375 pending_funcalls);
376 else
377 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
379 if (t->delete_terminal_hook)
380 (*t->delete_terminal_hook) (t);
381 else
382 delete_terminal (t);
384 return Qnil;
388 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
389 doc: /* Return the terminal that FRAME is displayed on.
390 If FRAME is nil, the selected frame is used.
392 The terminal device is represented by its integer identifier. */)
393 (Lisp_Object frame)
395 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
397 if (!t)
398 return Qnil;
399 else
401 Lisp_Object terminal;
402 XSETTERMINAL (terminal, t);
403 return terminal;
407 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
408 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
409 Value is nil if OBJECT is not a live display terminal.
410 If object is a live display terminal, the return value indicates what
411 sort of output terminal it uses. See the documentation of `framep' for
412 possible return values. */)
413 (Lisp_Object object)
415 struct terminal *t = decode_terminal (object);
417 if (!t)
418 return Qnil;
420 switch (t->type)
422 case output_initial: /* The initial frame is like a termcap frame. */
423 case output_termcap:
424 return Qt;
425 case output_x_window:
426 return Qx;
427 case output_w32:
428 return Qw32;
429 case output_msdos_raw:
430 return Qpc;
431 case output_ns:
432 return Qns;
433 default:
434 emacs_abort ();
438 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
439 doc: /* Return a list of all terminal devices. */)
440 (void)
442 Lisp_Object terminal, terminals = Qnil;
443 struct terminal *t;
445 for (t = terminal_list; t; t = t->next_terminal)
447 XSETTERMINAL (terminal, t);
448 terminals = Fcons (terminal, terminals);
451 return terminals;
454 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
455 doc: /* Return the name of the terminal device TERMINAL.
456 It is not guaranteed that the returned value is unique among opened devices.
458 TERMINAL may be a terminal object, a frame, or nil (meaning the
459 selected frame's terminal). */)
460 (Lisp_Object terminal)
462 struct terminal *t = decode_live_terminal (terminal);
464 return t->name ? build_string (t->name) : Qnil;
469 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
470 Return the previous value. */
472 static Lisp_Object
473 store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
475 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
476 if (EQ (old_alist_elt, Qnil))
478 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
479 return Qnil;
481 else
483 Lisp_Object result = Fcdr (old_alist_elt);
484 Fsetcdr (old_alist_elt, value);
485 return result;
490 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
491 doc: /* Return the parameter-alist of terminal TERMINAL.
492 The value is a list of elements of the form (PARM . VALUE), where PARM
493 is a symbol.
495 TERMINAL can be a terminal object, a frame, or nil (meaning the
496 selected frame's terminal). */)
497 (Lisp_Object terminal)
499 return Fcopy_alist (decode_live_terminal (terminal)->param_alist);
502 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
503 doc: /* Return TERMINAL's value for parameter PARAMETER.
504 TERMINAL can be a terminal object, a frame, or nil (meaning the
505 selected frame's terminal). */)
506 (Lisp_Object terminal, Lisp_Object parameter)
508 CHECK_SYMBOL (parameter);
509 return Fcdr (Fassq (parameter, decode_live_terminal (terminal)->param_alist));
512 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
513 Sset_terminal_parameter, 3, 3, 0,
514 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
515 Return the previous value of PARAMETER.
517 TERMINAL can be a terminal object, a frame or nil (meaning the
518 selected frame's terminal). */)
519 (Lisp_Object terminal, Lisp_Object parameter, Lisp_Object value)
521 return store_terminal_param (decode_live_terminal (terminal), parameter, value);
524 /* Initial frame has no device-dependent output data, but has
525 face cache which should be freed when the frame is deleted. */
527 static void
528 initial_free_frame_resources (struct frame *f)
530 eassert (FRAME_INITIAL_P (f));
531 free_frame_faces (f);
534 /* Create the bootstrap display terminal for the initial frame.
535 Returns a terminal of type output_initial. */
537 struct terminal *
538 init_initial_terminal (void)
540 if (initialized || terminal_list || tty_list)
541 emacs_abort ();
543 initial_terminal = create_terminal (output_initial, NULL);
544 initial_terminal->name = xstrdup ("initial_terminal");
545 initial_terminal->kboard = initial_kboard;
546 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
547 initial_terminal->delete_frame_hook = &initial_free_frame_resources;
548 /* Other hooks are NULL by default. */
550 return initial_terminal;
553 /* Deletes the bootstrap terminal device.
554 Called through delete_terminal_hook. */
556 static void
557 delete_initial_terminal (struct terminal *terminal)
559 if (terminal != initial_terminal)
560 emacs_abort ();
562 delete_terminal (terminal);
563 initial_terminal = NULL;
566 void
567 syms_of_terminal (void)
570 DEFVAR_LISP ("ring-bell-function", Vring_bell_function,
571 doc: /* Non-nil means call this function to ring the bell.
572 The function should accept no arguments. */);
573 Vring_bell_function = Qnil;
575 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions,
576 doc: /* Special hook run when a terminal is deleted.
577 Each function is called with argument, the terminal.
578 This may be called just before actually deleting the terminal,
579 or some time later. */);
580 Vdelete_terminal_functions = Qnil;
582 DEFSYM (Qterminal_live_p, "terminal-live-p");
583 DEFSYM (Qdelete_terminal_functions, "delete-terminal-functions");
584 DEFSYM (Qrun_hook_with_args, "run-hook-with-args");
586 defsubr (&Sdelete_terminal);
587 defsubr (&Sframe_terminal);
588 defsubr (&Sterminal_live_p);
589 defsubr (&Sterminal_list);
590 defsubr (&Sterminal_name);
591 defsubr (&Sterminal_parameters);
592 defsubr (&Sterminal_parameter);
593 defsubr (&Sset_terminal_parameter);
595 Fprovide (intern_c_string ("multi-tty"), Qnil);