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