* frames.texi (Font and Color Parameters): Rename from Color
[emacs.git] / src / terminal.c
blobd9951dba464bd176f71697ae9e9c9c56c3098599
1 /* Functions related to terminal devices.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 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>
20 #include <stdio.h>
22 #include "lisp.h"
23 #include "frame.h"
24 #include "termchar.h"
25 #include "termhooks.h"
26 #include "charset.h"
27 #include "coding.h"
28 #include "keyboard.h"
30 /* Chain of all terminals currently in use. */
31 struct terminal *terminal_list;
33 /* The first unallocated terminal id. */
34 static int next_terminal_id;
36 /* The initial terminal device, created by initial_term_init. */
37 struct terminal *initial_terminal;
39 /* Function to use to ring the bell. */
40 Lisp_Object Vring_bell_function;
42 static void delete_initial_terminal P_ ((struct terminal *));
46 void
47 ring_bell (struct frame *f)
49 if (!NILP (Vring_bell_function))
51 Lisp_Object function;
53 /* Temporarily set the global variable to nil
54 so that if we get an error, it stays nil
55 and we don't call it over and over.
57 We don't specbind it, because that would carefully
58 restore the bad value if there's an error
59 and make the loop of errors happen anyway. */
61 function = Vring_bell_function;
62 Vring_bell_function = Qnil;
64 call0 (function);
66 Vring_bell_function = function;
68 else if (FRAME_TERMINAL (f)->ring_bell_hook)
69 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
72 void
73 update_begin (struct frame *f)
75 if (FRAME_TERMINAL (f)->update_begin_hook)
76 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
79 void
80 update_end (struct frame *f)
82 if (FRAME_TERMINAL (f)->update_end_hook)
83 (*FRAME_TERMINAL (f)->update_end_hook) (f);
86 /* Specify how many text lines, from the top of the window,
87 should be affected by insert-lines and delete-lines operations.
88 This, and those operations, are used only within an update
89 that is bounded by calls to update_begin and update_end. */
91 void
92 set_terminal_window (struct frame *f, int size)
94 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
95 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
98 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
99 frame-relative coordinates. */
101 void
102 cursor_to (struct frame *f, int vpos, int hpos)
104 if (FRAME_TERMINAL (f)->cursor_to_hook)
105 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
108 /* Similar but don't take any account of the wasted characters. */
110 void
111 raw_cursor_to (struct frame *f, int row, int col)
113 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
114 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
117 /* Erase operations */
119 /* Clear from cursor to end of frame. */
120 void
121 clear_to_end (struct frame *f)
123 if (FRAME_TERMINAL (f)->clear_to_end_hook)
124 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
127 /* Clear entire frame */
129 void
130 clear_frame (struct frame *f)
132 if (FRAME_TERMINAL (f)->clear_frame_hook)
133 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
136 /* Clear from cursor to end of line.
137 Assume that the line is already clear starting at column first_unused_hpos.
139 Note that the cursor may be moved, on terminals lacking a `ce' string. */
141 void
142 clear_end_of_line (struct frame *f, int first_unused_hpos)
144 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
145 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
148 /* Output LEN glyphs starting at STRING at the nominal cursor position.
149 Advance the nominal cursor over the text. */
151 void
152 write_glyphs (struct frame *f, struct glyph *string, int len)
154 if (FRAME_TERMINAL (f)->write_glyphs_hook)
155 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
158 /* Insert LEN glyphs from START at the nominal cursor position.
160 If start is zero, insert blanks instead of a string at start */
162 void
163 insert_glyphs (struct frame *f, struct glyph *start, int len)
165 if (len <= 0)
166 return;
168 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
169 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
172 /* Delete N glyphs at the nominal cursor position. */
174 void
175 delete_glyphs (struct frame *f, int n)
177 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
178 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
181 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
183 void
184 ins_del_lines (struct frame *f, int vpos, int n)
186 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
187 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
193 /* Return the terminal object specified by TERMINAL. TERMINAL may be
194 a terminal object, a frame, or nil for the terminal device of the
195 current frame. If THROW is zero, return NULL for failure,
196 otherwise throw an error. */
198 struct terminal *
199 get_terminal (Lisp_Object terminal, int throw)
201 struct terminal *result = NULL;
203 if (NILP (terminal))
204 terminal = selected_frame;
206 if (TERMINALP (terminal))
207 result = XTERMINAL (terminal);
208 else if (FRAMEP (terminal))
209 result = FRAME_TERMINAL (XFRAME (terminal));
211 if (result && !result->name)
212 result = NULL;
214 if (result == NULL && throw)
215 wrong_type_argument (Qterminal_live_p, terminal);
217 return result;
222 /* Create a new terminal object and add it to the terminal list. */
224 struct terminal *
225 create_terminal (void)
227 struct terminal *terminal = allocate_terminal ();
229 terminal->name = NULL;
230 terminal->next_terminal = terminal_list;
231 terminal_list = terminal;
233 terminal->id = next_terminal_id++;
235 terminal->keyboard_coding =
236 (struct coding_system *) xmalloc (sizeof (struct coding_system));
237 terminal->terminal_coding =
238 (struct coding_system *) xmalloc (sizeof (struct coding_system));
240 setup_coding_system (Qno_conversion, terminal->keyboard_coding);
241 setup_coding_system (Qundecided, terminal->terminal_coding);
243 terminal->param_alist = Qnil;
244 return terminal;
247 /* Low-level function to close all frames on a terminal, remove it
248 from the terminal list and free its memory. */
250 void
251 delete_terminal (struct terminal *terminal)
253 struct terminal **tp;
254 Lisp_Object tail, frame;
256 /* Protect against recursive calls. delete_frame calls the
257 delete_terminal_hook when we delete our last frame. */
258 if (!terminal->name)
259 return;
260 xfree (terminal->name);
261 terminal->name = NULL;
263 /* Check for live frames that are still on this terminal. */
264 FOR_EACH_FRAME (tail, frame)
266 struct frame *f = XFRAME (frame);
267 if (FRAME_LIVE_P (f) && f->terminal == terminal)
269 /* Pass Qnoelisp rather than Qt. */
270 delete_frame (frame, Qnoelisp);
274 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
275 if (! *tp)
276 abort ();
277 *tp = terminal->next_terminal;
279 xfree (terminal->keyboard_coding);
280 terminal->keyboard_coding = NULL;
281 xfree (terminal->terminal_coding);
282 terminal->terminal_coding = NULL;
284 if (terminal->kboard && --terminal->kboard->reference_count == 0)
286 delete_kboard (terminal->kboard);
287 terminal->kboard = NULL;
291 Lisp_Object Qrun_hook_with_args;
292 static Lisp_Object Qdelete_terminal_functions;
293 static Lisp_Object Vdelete_terminal_functions;
295 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
296 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
297 TERMINAL may be a terminal object, a frame, or nil (meaning the
298 selected frame's terminal).
300 Normally, you may not delete a display if all other displays are suspended,
301 but if the second argument FORCE is non-nil, you may do so. */)
302 (terminal, force)
303 Lisp_Object terminal, force;
305 struct terminal *t = get_terminal (terminal, 0);
307 if (!t)
308 return Qnil;
310 if (NILP (force))
312 struct terminal *p = terminal_list;
313 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
314 p = p->next_terminal;
316 if (!p)
317 error ("Attempt to delete the sole active display terminal");
320 if (NILP (Vrun_hooks))
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);
330 if (t->delete_terminal_hook)
331 (*t->delete_terminal_hook) (t);
332 else
333 delete_terminal (t);
335 return Qnil;
339 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
340 doc: /* Return the terminal that FRAME is displayed on.
341 If FRAME is nil, the selected frame is used.
343 The terminal device is represented by its integer identifier. */)
344 (frame)
345 Lisp_Object frame;
347 struct terminal *t;
349 if (NILP (frame))
350 frame = selected_frame;
352 CHECK_LIVE_FRAME (frame);
354 t = FRAME_TERMINAL (XFRAME (frame));
356 if (!t)
357 return Qnil;
358 else
360 Lisp_Object terminal;
361 XSETTERMINAL (terminal, t);
362 return terminal;
366 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
367 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
368 Value is nil if OBJECT is not a live display terminal.
369 If object is a live display terminal, the return value indicates what
370 sort of output terminal it uses. See the documentation of `framep' for
371 possible return values. */)
372 (object)
373 Lisp_Object object;
375 struct terminal *t;
377 t = get_terminal (object, 0);
379 if (!t)
380 return Qnil;
382 switch (t->type)
384 case output_initial: /* The initial frame is like a termcap frame. */
385 case output_termcap:
386 return Qt;
387 case output_x_window:
388 return Qx;
389 case output_w32:
390 return Qw32;
391 case output_msdos_raw:
392 return Qpc;
393 case output_mac:
394 return Qmac;
395 case output_ns:
396 return Qns;
397 default:
398 abort ();
402 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
403 doc: /* Return a list of all terminal devices. */)
406 Lisp_Object terminal, terminals = Qnil;
407 struct terminal *t;
409 for (t = terminal_list; t; t = t->next_terminal)
411 XSETTERMINAL (terminal, t);
412 terminals = Fcons (terminal, terminals);
415 return terminals;
418 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
419 doc: /* Return the name of the terminal device TERMINAL.
420 It is not guaranteed that the returned value is unique among opened devices.
422 TERMINAL may be a terminal object, a frame, or nil (meaning the
423 selected frame's terminal). */)
424 (terminal)
425 Lisp_Object terminal;
427 struct terminal *t
428 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
430 return t->name ? build_string (t->name) : Qnil;
435 /* Return the value of terminal parameter PARAM in terminal T. */
436 Lisp_Object
437 get_terminal_param (t, param)
438 struct terminal *t;
439 Lisp_Object param;
441 Lisp_Object tem = Fassq (param, t->param_alist);
442 if (EQ (tem, Qnil))
443 return tem;
444 return Fcdr (tem);
447 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
448 Return the previous value. */
450 Lisp_Object
451 store_terminal_param (t, parameter, value)
452 struct terminal *t;
453 Lisp_Object parameter;
454 Lisp_Object value;
456 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
457 if (EQ (old_alist_elt, Qnil))
459 t->param_alist = Fcons (Fcons (parameter, value), t->param_alist);
460 return Qnil;
462 else
464 Lisp_Object result = Fcdr (old_alist_elt);
465 Fsetcdr (old_alist_elt, value);
466 return result;
471 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
472 doc: /* Return the parameter-alist of terminal TERMINAL.
473 The value is a list of elements of the form (PARM . VALUE), where PARM
474 is a symbol.
476 TERMINAL can be a terminal object, a frame, or nil (meaning the
477 selected frame's terminal). */)
478 (terminal)
479 Lisp_Object terminal;
481 struct terminal *t
482 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
483 return Fcopy_alist (t->param_alist);
486 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
487 doc: /* Return TERMINAL's value for parameter PARAMETER.
488 TERMINAL can be a terminal object, a frame, or nil (meaning the
489 selected frame's terminal). */)
490 (terminal, parameter)
491 Lisp_Object terminal;
492 Lisp_Object parameter;
494 Lisp_Object value;
495 struct terminal *t
496 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
497 CHECK_SYMBOL (parameter);
498 value = Fcdr (Fassq (parameter, t->param_alist));
499 return value;
502 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
503 Sset_terminal_parameter, 3, 3, 0,
504 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
505 Return the previous value of PARAMETER.
507 TERMINAL can be a terminal object, a frame or nil (meaning the
508 selected frame's terminal). */)
509 (terminal, parameter, value)
510 Lisp_Object terminal;
511 Lisp_Object parameter;
512 Lisp_Object value;
514 struct terminal *t
515 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
516 return store_terminal_param (t, parameter, value);
521 /* Create the bootstrap display terminal for the initial frame.
522 Returns a terminal of type output_initial. */
524 struct terminal *
525 init_initial_terminal (void)
527 if (initialized || terminal_list || tty_list)
528 abort ();
530 initial_terminal = create_terminal ();
531 initial_terminal->type = output_initial;
532 initial_terminal->name = xstrdup ("initial_terminal");
533 initial_terminal->kboard = initial_kboard;
534 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
535 /* All other hooks are NULL. */
537 return initial_terminal;
540 /* Deletes the bootstrap terminal device.
541 Called through delete_terminal_hook. */
543 static void
544 delete_initial_terminal (struct terminal *terminal)
546 if (terminal != initial_terminal)
547 abort ();
549 delete_terminal (terminal);
550 initial_terminal = NULL;
553 void
554 syms_of_terminal ()
557 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
558 doc: /* Non-nil means call this function to ring the bell.
559 The function should accept no arguments. */);
560 Vring_bell_function = Qnil;
562 DEFVAR_LISP ("delete-terminal-functions", &Vdelete_terminal_functions,
563 doc: /* Special hook run when a terminal is deleted.
564 Each function is called with argument, the terminal.
565 This may be called just before actually deleting the terminal,
566 or some time later. */);
567 Vdelete_terminal_functions = Qnil;
568 Qdelete_terminal_functions = intern ("delete-terminal-functions");
569 staticpro (&Qdelete_terminal_functions);
570 Qrun_hook_with_args = intern ("run-hook-with-args");
571 staticpro (&Qrun_hook_with_args);
573 defsubr (&Sdelete_terminal);
574 defsubr (&Sframe_terminal);
575 defsubr (&Sterminal_live_p);
576 defsubr (&Sterminal_list);
577 defsubr (&Sterminal_name);
578 defsubr (&Sterminal_parameters);
579 defsubr (&Sterminal_parameter);
580 defsubr (&Sset_terminal_parameter);
582 Fprovide (intern ("multi-tty"), Qnil);
585 /* arch-tag: e9af6f27-b483-47dc-bb1a-730c1c5cab03
586 (do not change this comment) */