1 /* Functions related to terminal devices.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 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/>. */
26 #include "termhooks.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 /* Function to use to ring the bell. */
41 Lisp_Object Vring_bell_function
;
43 static void delete_initial_terminal
P_ ((struct terminal
*));
48 ring_bell (struct frame
*f
)
50 if (!NILP (Vring_bell_function
))
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.
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. */
62 function
= Vring_bell_function
;
63 Vring_bell_function
= Qnil
;
67 Vring_bell_function
= function
;
69 else if (FRAME_TERMINAL (f
)->ring_bell_hook
)
70 (*FRAME_TERMINAL (f
)->ring_bell_hook
) (f
);
74 update_begin (struct frame
*f
)
76 if (FRAME_TERMINAL (f
)->update_begin_hook
)
77 (*FRAME_TERMINAL (f
)->update_begin_hook
) (f
);
81 update_end (struct frame
*f
)
83 if (FRAME_TERMINAL (f
)->update_end_hook
)
84 (*FRAME_TERMINAL (f
)->update_end_hook
) (f
);
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. */
93 set_terminal_window (struct frame
*f
, int size
)
95 if (FRAME_TERMINAL (f
)->set_terminal_window_hook
)
96 (*FRAME_TERMINAL (f
)->set_terminal_window_hook
) (f
, size
);
99 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
100 frame-relative coordinates. */
103 cursor_to (struct frame
*f
, int vpos
, int hpos
)
105 if (FRAME_TERMINAL (f
)->cursor_to_hook
)
106 (*FRAME_TERMINAL (f
)->cursor_to_hook
) (f
, vpos
, hpos
);
109 /* Similar but don't take any account of the wasted characters. */
112 raw_cursor_to (struct frame
*f
, int row
, int col
)
114 if (FRAME_TERMINAL (f
)->raw_cursor_to_hook
)
115 (*FRAME_TERMINAL (f
)->raw_cursor_to_hook
) (f
, row
, col
);
118 /* Erase operations */
120 /* Clear from cursor to end of frame. */
122 clear_to_end (struct frame
*f
)
124 if (FRAME_TERMINAL (f
)->clear_to_end_hook
)
125 (*FRAME_TERMINAL (f
)->clear_to_end_hook
) (f
);
128 /* Clear entire frame */
131 clear_frame (struct frame
*f
)
133 if (FRAME_TERMINAL (f
)->clear_frame_hook
)
134 (*FRAME_TERMINAL (f
)->clear_frame_hook
) (f
);
137 /* Clear from cursor to end of line.
138 Assume that the line is already clear starting at column first_unused_hpos.
140 Note that the cursor may be moved, on terminals lacking a `ce' string. */
143 clear_end_of_line (struct frame
*f
, int first_unused_hpos
)
145 if (FRAME_TERMINAL (f
)->clear_end_of_line_hook
)
146 (*FRAME_TERMINAL (f
)->clear_end_of_line_hook
) (f
, first_unused_hpos
);
149 /* Output LEN glyphs starting at STRING at the nominal cursor position.
150 Advance the nominal cursor over the text. */
153 write_glyphs (struct frame
*f
, struct glyph
*string
, int len
)
155 if (FRAME_TERMINAL (f
)->write_glyphs_hook
)
156 (*FRAME_TERMINAL (f
)->write_glyphs_hook
) (f
, string
, len
);
159 /* Insert LEN glyphs from START at the nominal cursor position.
161 If start is zero, insert blanks instead of a string at start */
164 insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
)
169 if (FRAME_TERMINAL (f
)->insert_glyphs_hook
)
170 (*FRAME_TERMINAL (f
)->insert_glyphs_hook
) (f
, start
, len
);
173 /* Delete N glyphs at the nominal cursor position. */
176 delete_glyphs (struct frame
*f
, int n
)
178 if (FRAME_TERMINAL (f
)->delete_glyphs_hook
)
179 (*FRAME_TERMINAL (f
)->delete_glyphs_hook
) (f
, n
);
182 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
185 ins_del_lines (struct frame
*f
, int vpos
, int n
)
187 if (FRAME_TERMINAL (f
)->ins_del_lines_hook
)
188 (*FRAME_TERMINAL (f
)->ins_del_lines_hook
) (f
, vpos
, n
);
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. */
200 get_terminal (Lisp_Object terminal
, int throw)
202 struct terminal
*result
= NULL
;
205 terminal
= selected_frame
;
207 if (TERMINALP (terminal
))
208 result
= XTERMINAL (terminal
);
209 else if (FRAMEP (terminal
))
210 result
= FRAME_TERMINAL (XFRAME (terminal
));
212 if (result
&& !result
->name
)
215 if (result
== NULL
&& throw)
216 wrong_type_argument (Qterminal_live_p
, terminal
);
223 /* Create a new terminal object and add it to the terminal list. */
226 create_terminal (void)
228 struct terminal
*terminal
= allocate_terminal ();
229 Lisp_Object terminal_coding
, keyboard_coding
;
231 terminal
->name
= NULL
;
232 terminal
->next_terminal
= terminal_list
;
233 terminal_list
= terminal
;
235 terminal
->id
= next_terminal_id
++;
237 terminal
->keyboard_coding
=
238 (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
239 terminal
->terminal_coding
=
240 (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
242 /* If default coding systems for the terminal and the keyboard are
243 already defined, use them in preference to the defaults. This is
244 needed when Emacs runs in daemon mode. */
245 keyboard_coding
= SYMBOL_VALUE (intern ("default-keyboard-coding-system"));
246 if (NILP (keyboard_coding
)
247 || EQ (keyboard_coding
, Qunbound
)
248 || NILP (Fcoding_system_p (keyboard_coding
)))
249 keyboard_coding
= Qno_conversion
;
250 terminal_coding
= SYMBOL_VALUE (intern ("default-terminal-coding-system"));
251 if (NILP (terminal_coding
)
252 || EQ (terminal_coding
, Qunbound
)
253 || NILP (Fcoding_system_p (terminal_coding
)))
254 terminal_coding
= Qundecided
;
256 setup_coding_system (keyboard_coding
, terminal
->keyboard_coding
);
257 setup_coding_system (terminal_coding
, terminal
->terminal_coding
);
259 terminal
->param_alist
= Qnil
;
263 /* Low-level function to close all frames on a terminal, remove it
264 from the terminal list and free its memory. */
267 delete_terminal (struct terminal
*terminal
)
269 struct terminal
**tp
;
270 Lisp_Object tail
, frame
;
272 /* Protect against recursive calls. delete_frame calls the
273 delete_terminal_hook when we delete our last frame. */
276 xfree (terminal
->name
);
277 terminal
->name
= NULL
;
279 /* Check for live frames that are still on this terminal. */
280 FOR_EACH_FRAME (tail
, frame
)
282 struct frame
*f
= XFRAME (frame
);
283 if (FRAME_LIVE_P (f
) && f
->terminal
== terminal
)
285 /* Pass Qnoelisp rather than Qt. */
286 delete_frame (frame
, Qnoelisp
);
290 for (tp
= &terminal_list
; *tp
!= terminal
; tp
= &(*tp
)->next_terminal
)
293 *tp
= terminal
->next_terminal
;
295 xfree (terminal
->keyboard_coding
);
296 terminal
->keyboard_coding
= NULL
;
297 xfree (terminal
->terminal_coding
);
298 terminal
->terminal_coding
= NULL
;
300 if (terminal
->kboard
&& --terminal
->kboard
->reference_count
== 0)
302 delete_kboard (terminal
->kboard
);
303 terminal
->kboard
= NULL
;
307 Lisp_Object Qrun_hook_with_args
;
308 static Lisp_Object Qdelete_terminal_functions
;
309 static Lisp_Object Vdelete_terminal_functions
;
311 DEFUN ("delete-terminal", Fdelete_terminal
, Sdelete_terminal
, 0, 2, 0,
312 doc
: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
313 TERMINAL may be a terminal object, a frame, or nil (meaning the
314 selected frame's terminal).
316 Normally, you may not delete a display if all other displays are suspended,
317 but if the second argument FORCE is non-nil, you may do so. */)
319 Lisp_Object terminal
, force
;
321 struct terminal
*t
= get_terminal (terminal
, 0);
328 struct terminal
*p
= terminal_list
;
329 while (p
&& (p
== t
|| !TERMINAL_ACTIVE_P (p
)))
330 p
= p
->next_terminal
;
333 error ("Attempt to delete the sole active display terminal");
336 if (NILP (Vrun_hooks
))
338 else if (EQ (force
, Qnoelisp
))
340 = Fcons (list3 (Qrun_hook_with_args
,
341 Qdelete_terminal_functions
, terminal
),
344 safe_call2 (Qrun_hook_with_args
, Qdelete_terminal_functions
, terminal
);
346 if (t
->delete_terminal_hook
)
347 (*t
->delete_terminal_hook
) (t
);
355 DEFUN ("frame-terminal", Fframe_terminal
, Sframe_terminal
, 0, 1, 0,
356 doc
: /* Return the terminal that FRAME is displayed on.
357 If FRAME is nil, the selected frame is used.
359 The terminal device is represented by its integer identifier. */)
366 frame
= selected_frame
;
368 CHECK_LIVE_FRAME (frame
);
370 t
= FRAME_TERMINAL (XFRAME (frame
));
376 Lisp_Object terminal
;
377 XSETTERMINAL (terminal
, t
);
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. */)
393 t
= get_terminal (object
, 0);
400 case output_initial
: /* The initial frame is like a termcap frame. */
403 case output_x_window
:
407 case output_msdos_raw
:
418 DEFUN ("terminal-list", Fterminal_list
, Sterminal_list
, 0, 0, 0,
419 doc
: /* Return a list of all terminal devices. */)
422 Lisp_Object terminal
, terminals
= Qnil
;
425 for (t
= terminal_list
; t
; t
= t
->next_terminal
)
427 XSETTERMINAL (terminal
, t
);
428 terminals
= Fcons (terminal
, terminals
);
434 DEFUN ("terminal-name", Fterminal_name
, Sterminal_name
, 0, 1, 0,
435 doc
: /* Return the name of the terminal device TERMINAL.
436 It is not guaranteed that the returned value is unique among opened devices.
438 TERMINAL may be a terminal object, a frame, or nil (meaning the
439 selected frame's terminal). */)
441 Lisp_Object terminal
;
444 = TERMINALP (terminal
) ? XTERMINAL (terminal
) : get_terminal (terminal
, 1);
446 return t
->name
? build_string (t
->name
) : Qnil
;
451 /* Return the value of terminal parameter PARAM in terminal T. */
453 get_terminal_param (t
, param
)
457 Lisp_Object tem
= Fassq (param
, t
->param_alist
);
463 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
464 Return the previous value. */
467 store_terminal_param (t
, parameter
, value
)
469 Lisp_Object parameter
;
472 Lisp_Object old_alist_elt
= Fassq (parameter
, t
->param_alist
);
473 if (EQ (old_alist_elt
, Qnil
))
475 t
->param_alist
= Fcons (Fcons (parameter
, value
), t
->param_alist
);
480 Lisp_Object result
= Fcdr (old_alist_elt
);
481 Fsetcdr (old_alist_elt
, value
);
487 DEFUN ("terminal-parameters", Fterminal_parameters
, Sterminal_parameters
, 0, 1, 0,
488 doc
: /* Return the parameter-alist of terminal TERMINAL.
489 The value is a list of elements of the form (PARM . VALUE), where PARM
492 TERMINAL can be a terminal object, a frame, or nil (meaning the
493 selected frame's terminal). */)
495 Lisp_Object terminal
;
498 = TERMINALP (terminal
) ? XTERMINAL (terminal
) : get_terminal (terminal
, 1);
499 return Fcopy_alist (t
->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 (terminal
, parameter
)
507 Lisp_Object terminal
;
508 Lisp_Object parameter
;
512 = TERMINALP (terminal
) ? XTERMINAL (terminal
) : get_terminal (terminal
, 1);
513 CHECK_SYMBOL (parameter
);
514 value
= Fcdr (Fassq (parameter
, t
->param_alist
));
518 DEFUN ("set-terminal-parameter", Fset_terminal_parameter
,
519 Sset_terminal_parameter
, 3, 3, 0,
520 doc
: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
521 Return the previous value of PARAMETER.
523 TERMINAL can be a terminal object, a frame or nil (meaning the
524 selected frame's terminal). */)
525 (terminal
, parameter
, value
)
526 Lisp_Object terminal
;
527 Lisp_Object parameter
;
531 = TERMINALP (terminal
) ? XTERMINAL (terminal
) : get_terminal (terminal
, 1);
532 return store_terminal_param (t
, parameter
, value
);
537 /* Create the bootstrap display terminal for the initial frame.
538 Returns a terminal of type output_initial. */
541 init_initial_terminal (void)
543 if (initialized
|| terminal_list
|| tty_list
)
546 initial_terminal
= create_terminal ();
547 initial_terminal
->type
= output_initial
;
548 initial_terminal
->name
= xstrdup ("initial_terminal");
549 initial_terminal
->kboard
= initial_kboard
;
550 initial_terminal
->delete_terminal_hook
= &delete_initial_terminal
;
551 /* All other hooks are NULL. */
553 return initial_terminal
;
556 /* Deletes the bootstrap terminal device.
557 Called through delete_terminal_hook. */
560 delete_initial_terminal (struct terminal
*terminal
)
562 if (terminal
!= initial_terminal
)
565 delete_terminal (terminal
);
566 initial_terminal
= NULL
;
573 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function
,
574 doc
: /* Non-nil means call this function to ring the bell.
575 The function should accept no arguments. */);
576 Vring_bell_function
= Qnil
;
578 DEFVAR_LISP ("delete-terminal-functions", &Vdelete_terminal_functions
,
579 doc
: /* Special hook run when a terminal is deleted.
580 Each function is called with argument, the terminal.
581 This may be called just before actually deleting the terminal,
582 or some time later. */);
583 Vdelete_terminal_functions
= Qnil
;
584 Qdelete_terminal_functions
= intern_c_string ("delete-terminal-functions");
585 staticpro (&Qdelete_terminal_functions
);
586 Qrun_hook_with_args
= intern_c_string ("run-hook-with-args");
587 staticpro (&Qrun_hook_with_args
);
589 defsubr (&Sdelete_terminal
);
590 defsubr (&Sframe_terminal
);
591 defsubr (&Sterminal_live_p
);
592 defsubr (&Sterminal_list
);
593 defsubr (&Sterminal_name
);
594 defsubr (&Sterminal_parameters
);
595 defsubr (&Sterminal_parameter
);
596 defsubr (&Sset_terminal_parameter
);
598 Fprovide (intern_c_string ("multi-tty"), Qnil
);
601 /* arch-tag: e9af6f27-b483-47dc-bb1a-730c1c5cab03
602 (do not change this comment) */