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