Merged from emacs@sv.gnu.org. Last-minute emacsclient rewrites be damned!
[bpt/emacs.git] / src / term.c
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1998, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
23
24 #include <config.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/file.h>
30 #include <unistd.h> /* For isatty. */
31
32 #if HAVE_TERMIOS_H
33 #include <termios.h> /* For TIOCNOTTY. */
34 #endif
35
36 #include <signal.h>
37
38 #include "lisp.h"
39 #include "termchar.h"
40 #include "termopts.h"
41 #include "charset.h"
42 #include "coding.h"
43 #include "keyboard.h"
44 #include "frame.h"
45 #include "disptab.h"
46 #include "termhooks.h"
47 #include "dispextern.h"
48 #include "window.h"
49 #include "keymap.h"
50 #include "syssignal.h"
51 #include "systty.h"
52
53 /* For now, don't try to include termcap.h. On some systems,
54 configure finds a non-standard termcap.h that the main build
55 won't find. */
56
57 #if defined HAVE_TERMCAP_H && 0
58 #include <termcap.h>
59 #else
60 extern void tputs P_ ((const char *, int, int (*)(int)));
61 extern int tgetent P_ ((char *, const char *));
62 extern int tgetflag P_ ((char *id));
63 extern int tgetnum P_ ((char *id));
64 #endif
65
66 #include "cm.h"
67 #ifdef HAVE_X_WINDOWS
68 #include "xterm.h"
69 #endif
70 #ifdef MAC_OS
71 #include "macterm.h"
72 #endif
73
74 #ifndef O_RDWR
75 #define O_RDWR 2
76 #endif
77
78 #ifndef O_NOCTTY
79 #define O_NOCTTY 0
80 #endif
81
82 static void tty_set_scroll_region P_ ((struct frame *f, int start, int stop));
83 static void turn_on_face P_ ((struct frame *, int face_id));
84 static void turn_off_face P_ ((struct frame *, int face_id));
85 static void tty_show_cursor P_ ((struct tty_display_info *));
86 static void tty_hide_cursor P_ ((struct tty_display_info *));
87 static void tty_background_highlight P_ ((struct tty_display_info *tty));
88 static void clear_tty_hooks P_ ((struct terminal *terminal));
89 static void set_tty_hooks P_ ((struct terminal *terminal));
90 static void dissociate_if_controlling_tty P_ ((int fd));
91 static void delete_tty P_ ((struct terminal *));
92
93 #define OUTPUT(tty, a) \
94 emacs_tputs ((tty), a, \
95 (int) (FRAME_LINES (XFRAME (selected_frame)) \
96 - curY (tty)), \
97 cmputc)
98
99 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
100 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
101
102 #define OUTPUT_IF(tty, a) \
103 do { \
104 if (a) \
105 emacs_tputs ((tty), a, \
106 (int) (FRAME_LINES (XFRAME (selected_frame)) \
107 - curY (tty) ), \
108 cmputc); \
109 } while (0)
110
111 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
112
113 /* If true, use "vs", otherwise use "ve" to make the cursor visible. */
114
115 static int visible_cursor;
116
117 /* Display space properties */
118
119 extern Lisp_Object Qspace, QCalign_to, QCwidth;
120
121 /* Functions to call after suspending a tty. */
122 Lisp_Object Vsuspend_tty_functions;
123
124 /* Functions to call after resuming a tty. */
125 Lisp_Object Vresume_tty_functions;
126
127 /* Chain of all tty device parameters. */
128 struct tty_display_info *tty_list;
129
130 /* Nonzero means no need to redraw the entire frame on resuming a
131 suspended Emacs. This is useful on terminals with multiple
132 pages, where one page is used for Emacs and another for all
133 else. */
134 int no_redraw_on_reenter;
135
136 /* Meaning of bits in no_color_video. Each bit set means that the
137 corresponding attribute cannot be combined with colors. */
138
139 enum no_color_bit
140 {
141 NC_STANDOUT = 1 << 0,
142 NC_UNDERLINE = 1 << 1,
143 NC_REVERSE = 1 << 2,
144 NC_BLINK = 1 << 3,
145 NC_DIM = 1 << 4,
146 NC_BOLD = 1 << 5,
147 NC_INVIS = 1 << 6,
148 NC_PROTECT = 1 << 7,
149 NC_ALT_CHARSET = 1 << 8
150 };
151
152 /* internal state */
153
154 /* The largest frame width in any call to calculate_costs. */
155
156 int max_frame_cols;
157
158 /* The largest frame height in any call to calculate_costs. */
159
160 int max_frame_lines;
161
162 /* Non-zero if we have dropped our controlling tty and therefore
163 should not open a frame on stdout. */
164 static int no_controlling_tty;
165
166 /* Provided for lisp packages. */
167
168 static int system_uses_terminfo;
169
170 char *tparam ();
171
172 extern char *tgetstr ();
173 \f
174
175 #ifdef WINDOWSNT
176 /* We aren't X windows, but we aren't termcap either. This makes me
177 uncertain as to what value to use for frame.output_method. For
178 this file, we'll define FRAME_TERMCAP_P to be zero so that our
179 output hooks get called instead of the termcap functions. Probably
180 the best long-term solution is to define an output_windows_nt... */
181
182 #undef FRAME_TERMCAP_P
183 #define FRAME_TERMCAP_P(_f_) 0
184 #endif /* WINDOWSNT */
185
186
187 /* Ring the bell on a tty. */
188
189 static void
190 tty_ring_bell (struct frame *f)
191 {
192 struct tty_display_info *tty = FRAME_TTY (f);
193
194 if (tty->output)
195 {
196 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
197 ? tty->TS_visible_bell
198 : tty->TS_bell));
199 fflush (tty->output);
200 }
201 }
202
203 /* Set up termcap modes for Emacs. */
204
205 void
206 tty_set_terminal_modes (struct terminal *terminal)
207 {
208 struct tty_display_info *tty = terminal->display_info.tty;
209
210 if (tty->output)
211 {
212 if (tty->TS_termcap_modes)
213 OUTPUT (tty, tty->TS_termcap_modes);
214 else
215 {
216 /* Output enough newlines to scroll all the old screen contents
217 off the screen, so it won't be overwritten and lost. */
218 int i;
219 current_tty = tty;
220 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
221 cmputc ('\n');
222 }
223
224 OUTPUT_IF (tty, tty->TS_termcap_modes);
225 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
226 OUTPUT_IF (tty, tty->TS_keypad_mode);
227 losecursor (tty);
228 fflush (tty->output);
229 }
230 }
231
232 /* Reset termcap modes before exiting Emacs. */
233
234 void
235 tty_reset_terminal_modes (struct terminal *terminal)
236 {
237 struct tty_display_info *tty = terminal->display_info.tty;
238
239 if (tty->output)
240 {
241 tty_turn_off_highlight (tty);
242 tty_turn_off_insert (tty);
243 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
244 OUTPUT_IF (tty, tty->TS_cursor_normal);
245 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
246 OUTPUT_IF (tty, tty->TS_orig_pair);
247 /* Output raw CR so kernel can track the cursor hpos. */
248 current_tty = tty;
249 cmputc ('\r');
250 fflush (tty->output);
251 }
252 }
253
254 /* Flag the end of a display update on a termcap terminal. */
255
256 static void
257 tty_update_end (struct frame *f)
258 {
259 struct tty_display_info *tty = FRAME_TTY (f);
260
261 if (!XWINDOW (selected_window)->cursor_off_p)
262 tty_show_cursor (tty);
263 tty_turn_off_insert (tty);
264 tty_background_highlight (tty);
265 }
266
267 /* The implementation of set_terminal_window for termcap frames. */
268
269 static void
270 tty_set_terminal_window (struct frame *f, int size)
271 {
272 struct tty_display_info *tty = FRAME_TTY (f);
273
274 tty->specified_window = size ? size : FRAME_LINES (f);
275 if (FRAME_SCROLL_REGION_OK (f))
276 tty_set_scroll_region (f, 0, tty->specified_window);
277 }
278
279 static void
280 tty_set_scroll_region (struct frame *f, int start, int stop)
281 {
282 char *buf;
283 struct tty_display_info *tty = FRAME_TTY (f);
284
285 if (tty->TS_set_scroll_region)
286 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1);
287 else if (tty->TS_set_scroll_region_1)
288 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
289 FRAME_LINES (f), start,
290 FRAME_LINES (f) - stop,
291 FRAME_LINES (f));
292 else
293 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
294
295 OUTPUT (tty, buf);
296 xfree (buf);
297 losecursor (tty);
298 }
299
300 \f
301 static void
302 tty_turn_on_insert (struct tty_display_info *tty)
303 {
304 if (!tty->insert_mode)
305 OUTPUT (tty, tty->TS_insert_mode);
306 tty->insert_mode = 1;
307 }
308
309 void
310 tty_turn_off_insert (struct tty_display_info *tty)
311 {
312 if (tty->insert_mode)
313 OUTPUT (tty, tty->TS_end_insert_mode);
314 tty->insert_mode = 0;
315 }
316 \f
317 /* Handle highlighting. */
318
319 void
320 tty_turn_off_highlight (struct tty_display_info *tty)
321 {
322 if (tty->standout_mode)
323 OUTPUT_IF (tty, tty->TS_end_standout_mode);
324 tty->standout_mode = 0;
325 }
326
327 static void
328 tty_turn_on_highlight (struct tty_display_info *tty)
329 {
330 if (!tty->standout_mode)
331 OUTPUT_IF (tty, tty->TS_standout_mode);
332 tty->standout_mode = 1;
333 }
334
335 static void
336 tty_toggle_highlight (struct tty_display_info *tty)
337 {
338 if (tty->standout_mode)
339 tty_turn_off_highlight (tty);
340 else
341 tty_turn_on_highlight (tty);
342 }
343
344
345 /* Make cursor invisible. */
346
347 static void
348 tty_hide_cursor (struct tty_display_info *tty)
349 {
350 if (tty->cursor_hidden == 0)
351 {
352 tty->cursor_hidden = 1;
353 OUTPUT_IF (tty, tty->TS_cursor_invisible);
354 }
355 }
356
357
358 /* Ensure that cursor is visible. */
359
360 static void
361 tty_show_cursor (struct tty_display_info *tty)
362 {
363 if (tty->cursor_hidden)
364 {
365 tty->cursor_hidden = 0;
366 OUTPUT_IF (tty, tty->TS_cursor_normal);
367 if (visible_cursor)
368 OUTPUT_IF (tty, tty->TS_cursor_visible);
369 }
370 }
371
372
373 /* Set standout mode to the state it should be in for
374 empty space inside windows. What this is,
375 depends on the user option inverse-video. */
376
377 static void
378 tty_background_highlight (struct tty_display_info *tty)
379 {
380 if (inverse_video)
381 tty_turn_on_highlight (tty);
382 else
383 tty_turn_off_highlight (tty);
384 }
385
386 /* Set standout mode to the mode specified for the text to be output. */
387
388 static void
389 tty_highlight_if_desired (struct tty_display_info *tty)
390 {
391 if (inverse_video)
392 tty_turn_on_highlight (tty);
393 else
394 tty_turn_off_highlight (tty);
395 }
396 \f
397
398 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
399 frame-relative coordinates. */
400
401 static void
402 tty_cursor_to (struct frame *f, int vpos, int hpos)
403 {
404 struct tty_display_info *tty = FRAME_TTY (f);
405
406 /* Detect the case where we are called from reset_sys_modes
407 and the costs have never been calculated. Do nothing. */
408 if (! tty->costs_set)
409 return;
410
411 if (curY (tty) == vpos
412 && curX (tty) == hpos)
413 return;
414 if (!tty->TF_standout_motion)
415 tty_background_highlight (tty);
416 if (!tty->TF_insmode_motion)
417 tty_turn_off_insert (tty);
418 cmgoto (tty, vpos, hpos);
419 }
420
421 /* Similar but don't take any account of the wasted characters. */
422
423 static void
424 tty_raw_cursor_to (struct frame *f, int row, int col)
425 {
426 struct tty_display_info *tty = FRAME_TTY (f);
427
428 if (curY (tty) == row
429 && curX (tty) == col)
430 return;
431 if (!tty->TF_standout_motion)
432 tty_background_highlight (tty);
433 if (!tty->TF_insmode_motion)
434 tty_turn_off_insert (tty);
435 cmgoto (tty, row, col);
436 }
437 \f
438 /* Erase operations */
439
440 /* Clear from cursor to end of frame on a termcap device. */
441
442 static void
443 tty_clear_to_end (struct frame *f)
444 {
445 register int i;
446 struct tty_display_info *tty = FRAME_TTY (f);
447
448 if (tty->TS_clr_to_bottom)
449 {
450 tty_background_highlight (tty);
451 OUTPUT (tty, tty->TS_clr_to_bottom);
452 }
453 else
454 {
455 for (i = curY (tty); i < FRAME_LINES (f); i++)
456 {
457 cursor_to (f, i, 0);
458 clear_end_of_line (f, FRAME_COLS (f));
459 }
460 }
461 }
462
463 /* Clear an entire termcap frame. */
464
465 static void
466 tty_clear_frame (struct frame *f)
467 {
468 struct tty_display_info *tty = FRAME_TTY (f);
469
470 if (tty->TS_clr_frame)
471 {
472 tty_background_highlight (tty);
473 OUTPUT (tty, tty->TS_clr_frame);
474 cmat (tty, 0, 0);
475 }
476 else
477 {
478 cursor_to (f, 0, 0);
479 clear_to_end (f);
480 }
481 }
482
483 /* An implementation of clear_end_of_line for termcap frames.
484
485 Note that the cursor may be moved, on terminals lacking a `ce' string. */
486
487 static void
488 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
489 {
490 register int i;
491 struct tty_display_info *tty = FRAME_TTY (f);
492
493 /* Detect the case where we are called from reset_sys_modes
494 and the costs have never been calculated. Do nothing. */
495 if (! tty->costs_set)
496 return;
497
498 if (curX (tty) >= first_unused_hpos)
499 return;
500 tty_background_highlight (tty);
501 if (tty->TS_clr_line)
502 {
503 OUTPUT1 (tty, tty->TS_clr_line);
504 }
505 else
506 { /* have to do it the hard way */
507 tty_turn_off_insert (tty);
508
509 /* Do not write in last row last col with Auto-wrap on. */
510 if (AutoWrap (tty)
511 && curY (tty) == FrameRows (tty) - 1
512 && first_unused_hpos == FrameCols (tty))
513 first_unused_hpos--;
514
515 for (i = curX (tty); i < first_unused_hpos; i++)
516 {
517 if (tty->termscript)
518 fputc (' ', tty->termscript);
519 fputc (' ', tty->output);
520 }
521 cmplus (tty, first_unused_hpos - curX (tty));
522 }
523 }
524 \f
525 /* Buffer to store the source and result of code conversion for terminal. */
526 static unsigned char *encode_terminal_buf;
527 /* Allocated size of the above buffer. */
528 static int encode_terminal_bufsize;
529
530 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
531 Set CODING->produced to the byte-length of the resulting byte
532 sequence, and return a pointer to that byte sequence. */
533
534 unsigned char *
535 encode_terminal_code (src, src_len, coding)
536 struct glyph *src;
537 int src_len;
538 struct coding_system *coding;
539 {
540 struct glyph *src_end = src + src_len;
541 register GLYPH g;
542 unsigned char *buf;
543 int nchars, nbytes, required;
544 register int tlen = GLYPH_TABLE_LENGTH;
545 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
546
547 /* Allocate sufficient size of buffer to store all characters in
548 multibyte-form. But, it may be enlarged on demand if
549 Vglyph_table contains a string. */
550 required = MAX_MULTIBYTE_LENGTH * src_len;
551 if (encode_terminal_bufsize < required)
552 {
553 if (encode_terminal_bufsize == 0)
554 encode_terminal_buf = xmalloc (required);
555 else
556 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
557 encode_terminal_bufsize = required;
558 }
559
560 buf = encode_terminal_buf;
561 nchars = 0;
562 while (src < src_end)
563 {
564 /* We must skip glyphs to be padded for a wide character. */
565 if (! CHAR_GLYPH_PADDING_P (*src))
566 {
567 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
568
569 if (g < 0 || g >= tlen)
570 {
571 /* This glyph doesn't has an entry in Vglyph_table. */
572 if (CHAR_VALID_P (src->u.ch, 0))
573 buf += CHAR_STRING (src->u.ch, buf);
574 else
575 *buf++ = SPACEGLYPH;
576 nchars++;
577 }
578 else
579 {
580 /* This glyph has an entry in Vglyph_table,
581 so process any alias before testing for simpleness. */
582 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
583
584 if (GLYPH_SIMPLE_P (tbase, tlen, g))
585 {
586 int c = FAST_GLYPH_CHAR (g);
587
588 if (CHAR_VALID_P (c, 0))
589 buf += CHAR_STRING (c, buf);
590 else
591 *buf++ = SPACEGLYPH;
592 nchars++;
593 }
594 else
595 {
596 /* We have a string in Vglyph_table. */
597 Lisp_Object string;
598
599 string = tbase[g];
600 if (! STRING_MULTIBYTE (string))
601 string = string_to_multibyte (string);
602 nbytes = buf - encode_terminal_buf;
603 if (encode_terminal_bufsize < nbytes + SBYTES (string))
604 {
605 encode_terminal_bufsize = nbytes + SBYTES (string);
606 encode_terminal_buf = xrealloc (encode_terminal_buf,
607 encode_terminal_bufsize);
608 buf = encode_terminal_buf + nbytes;
609 }
610 bcopy (SDATA (string), buf, SBYTES (string));
611 buf += SBYTES (string);
612 nchars += SCHARS (string);
613 }
614 }
615 }
616 src++;
617 }
618
619 nbytes = buf - encode_terminal_buf;
620 coding->src_multibyte = 1;
621 coding->dst_multibyte = 0;
622 if (SYMBOLP (coding->pre_write_conversion)
623 && ! NILP (Ffboundp (coding->pre_write_conversion)))
624 {
625 run_pre_write_conversin_on_c_str (&encode_terminal_buf,
626 &encode_terminal_bufsize,
627 nchars, nbytes, coding);
628 nchars = coding->produced_char;
629 nbytes = coding->produced;
630 }
631 required = nbytes + encoding_buffer_size (coding, nbytes);
632 if (encode_terminal_bufsize < required)
633 {
634 encode_terminal_bufsize = required;
635 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
636 }
637
638 encode_coding (coding, encode_terminal_buf, encode_terminal_buf + nbytes,
639 nbytes, encode_terminal_bufsize - nbytes);
640 return encode_terminal_buf + nbytes;
641 }
642
643
644 /* An implementation of write_glyphs for termcap frames. */
645
646 static void
647 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
648 {
649 unsigned char *conversion_buffer;
650 struct coding_system *coding;
651
652 struct tty_display_info *tty = FRAME_TTY (f);
653
654 tty_turn_off_insert (tty);
655 tty_hide_cursor (tty);
656
657 /* Don't dare write in last column of bottom line, if Auto-Wrap,
658 since that would scroll the whole frame on some terminals. */
659
660 if (AutoWrap (tty)
661 && curY (tty) + 1 == FRAME_LINES (f)
662 && (curX (tty) + len) == FRAME_COLS (f))
663 len --;
664 if (len <= 0)
665 return;
666
667 cmplus (tty, len);
668
669 /* If terminal_coding does any conversion, use it, otherwise use
670 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
671 because it always return 1 if the member src_multibyte is 1. */
672 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
673 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
674 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
675 the tail. */
676 coding->mode &= ~CODING_MODE_LAST_BLOCK;
677
678 while (len > 0)
679 {
680 /* Identify a run of glyphs with the same face. */
681 int face_id = string->face_id;
682 int n;
683
684 for (n = 1; n < len; ++n)
685 if (string[n].face_id != face_id)
686 break;
687
688 /* Turn appearance modes of the face of the run on. */
689 tty_highlight_if_desired (tty);
690 turn_on_face (f, face_id);
691
692 if (n == len)
693 /* This is the last run. */
694 coding->mode |= CODING_MODE_LAST_BLOCK;
695 conversion_buffer = encode_terminal_code (string, n, coding);
696 if (coding->produced > 0)
697 {
698 fwrite (conversion_buffer, 1, coding->produced, tty->output);
699 if (ferror (tty->output))
700 clearerr (tty->output);
701 if (tty->termscript)
702 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
703 }
704 len -= n;
705 string += n;
706
707 /* Turn appearance modes off. */
708 turn_off_face (f, face_id);
709 tty_turn_off_highlight (tty);
710 }
711
712 cmcheckmagic (tty);
713 }
714
715 /* An implementation of insert_glyphs for termcap frames. */
716
717 static void
718 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
719 {
720 char *buf;
721 struct glyph *glyph = NULL;
722 unsigned char *conversion_buffer;
723 unsigned char space[1];
724 struct coding_system *coding;
725
726 struct tty_display_info *tty = FRAME_TTY (f);
727
728 if (tty->TS_ins_multi_chars)
729 {
730 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len);
731 OUTPUT1 (tty, buf);
732 xfree (buf);
733 if (start)
734 write_glyphs (f, start, len);
735 return;
736 }
737
738 tty_turn_on_insert (tty);
739 cmplus (tty, len);
740
741 if (! start)
742 space[0] = SPACEGLYPH;
743
744 /* If terminal_coding does any conversion, use it, otherwise use
745 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
746 because it always return 1 if the member src_multibyte is 1. */
747 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
748 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
749 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
750 the tail. */
751 coding->mode &= ~CODING_MODE_LAST_BLOCK;
752
753 while (len-- > 0)
754 {
755 OUTPUT1_IF (tty, tty->TS_ins_char);
756 if (!start)
757 {
758 conversion_buffer = space;
759 coding->produced = 1;
760 }
761 else
762 {
763 tty_highlight_if_desired (tty);
764 turn_on_face (f, start->face_id);
765 glyph = start;
766 ++start;
767 /* We must open sufficient space for a character which
768 occupies more than one column. */
769 while (len && CHAR_GLYPH_PADDING_P (*start))
770 {
771 OUTPUT1_IF (tty, tty->TS_ins_char);
772 start++, len--;
773 }
774
775 if (len <= 0)
776 /* This is the last glyph. */
777 coding->mode |= CODING_MODE_LAST_BLOCK;
778
779 conversion_buffer = encode_terminal_code (glyph, 1, coding);
780 }
781
782 if (coding->produced > 0)
783 {
784 fwrite (conversion_buffer, 1, coding->produced, tty->output);
785 if (ferror (tty->output))
786 clearerr (tty->output);
787 if (tty->termscript)
788 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
789 }
790
791 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
792 if (start)
793 {
794 turn_off_face (f, glyph->face_id);
795 tty_turn_off_highlight (tty);
796 }
797 }
798
799 cmcheckmagic (tty);
800 }
801
802 /* An implementation of delete_glyphs for termcap frames. */
803
804 static void
805 tty_delete_glyphs (struct frame *f, int n)
806 {
807 char *buf;
808 register int i;
809
810 struct tty_display_info *tty = FRAME_TTY (f);
811
812 if (tty->delete_in_insert_mode)
813 {
814 tty_turn_on_insert (tty);
815 }
816 else
817 {
818 tty_turn_off_insert (tty);
819 OUTPUT_IF (tty, tty->TS_delete_mode);
820 }
821
822 if (tty->TS_del_multi_chars)
823 {
824 buf = tparam (tty->TS_del_multi_chars, 0, 0, n);
825 OUTPUT1 (tty, buf);
826 xfree (buf);
827 }
828 else
829 for (i = 0; i < n; i++)
830 OUTPUT1 (tty, tty->TS_del_char);
831 if (!tty->delete_in_insert_mode)
832 OUTPUT_IF (tty, tty->TS_end_delete_mode);
833 }
834 \f
835 /* An implementation of ins_del_lines for termcap frames. */
836
837 static void
838 tty_ins_del_lines (struct frame *f, int vpos, int n)
839 {
840 struct tty_display_info *tty = FRAME_TTY (f);
841 char *multi = n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
842 char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
843 char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
844
845 register int i = n > 0 ? n : -n;
846 register char *buf;
847
848 /* If the lines below the insertion are being pushed
849 into the end of the window, this is the same as clearing;
850 and we know the lines are already clear, since the matching
851 deletion has already been done. So can ignore this. */
852 /* If the lines below the deletion are blank lines coming
853 out of the end of the window, don't bother,
854 as there will be a matching inslines later that will flush them. */
855 if (FRAME_SCROLL_REGION_OK (f)
856 && vpos + i >= tty->specified_window)
857 return;
858 if (!FRAME_MEMORY_BELOW_FRAME (f)
859 && vpos + i >= FRAME_LINES (f))
860 return;
861
862 if (multi)
863 {
864 raw_cursor_to (f, vpos, 0);
865 tty_background_highlight (tty);
866 buf = tparam (multi, 0, 0, i);
867 OUTPUT (tty, buf);
868 xfree (buf);
869 }
870 else if (single)
871 {
872 raw_cursor_to (f, vpos, 0);
873 tty_background_highlight (tty);
874 while (--i >= 0)
875 OUTPUT (tty, single);
876 if (tty->TF_teleray)
877 curX (tty) = 0;
878 }
879 else
880 {
881 tty_set_scroll_region (f, vpos, tty->specified_window);
882 if (n < 0)
883 raw_cursor_to (f, tty->specified_window - 1, 0);
884 else
885 raw_cursor_to (f, vpos, 0);
886 tty_background_highlight (tty);
887 while (--i >= 0)
888 OUTPUTL (tty, scroll, tty->specified_window - vpos);
889 tty_set_scroll_region (f, 0, tty->specified_window);
890 }
891
892 if (!FRAME_SCROLL_REGION_OK (f)
893 && FRAME_MEMORY_BELOW_FRAME (f)
894 && n < 0)
895 {
896 cursor_to (f, FRAME_LINES (f) + n, 0);
897 clear_to_end (f);
898 }
899 }
900 \f
901 /* Compute cost of sending "str", in characters,
902 not counting any line-dependent padding. */
903
904 int
905 string_cost (char *str)
906 {
907 cost = 0;
908 if (str)
909 tputs (str, 0, evalcost);
910 return cost;
911 }
912
913 /* Compute cost of sending "str", in characters,
914 counting any line-dependent padding at one line. */
915
916 static int
917 string_cost_one_line (char *str)
918 {
919 cost = 0;
920 if (str)
921 tputs (str, 1, evalcost);
922 return cost;
923 }
924
925 /* Compute per line amount of line-dependent padding,
926 in tenths of characters. */
927
928 int
929 per_line_cost (char *str)
930 {
931 cost = 0;
932 if (str)
933 tputs (str, 0, evalcost);
934 cost = - cost;
935 if (str)
936 tputs (str, 10, evalcost);
937 return cost;
938 }
939
940 #ifndef old
941 /* char_ins_del_cost[n] is cost of inserting N characters.
942 char_ins_del_cost[-n] is cost of deleting N characters.
943 The length of this vector is based on max_frame_cols. */
944
945 int *char_ins_del_vector;
946
947 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
948 #endif
949
950 /* ARGSUSED */
951 static void
952 calculate_ins_del_char_costs (struct frame *f)
953 {
954 struct tty_display_info *tty = FRAME_TTY (f);
955 int ins_startup_cost, del_startup_cost;
956 int ins_cost_per_char, del_cost_per_char;
957 register int i;
958 register int *p;
959
960 if (tty->TS_ins_multi_chars)
961 {
962 ins_cost_per_char = 0;
963 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
964 }
965 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
966 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
967 {
968 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
969 + string_cost (tty->TS_end_insert_mode))) / 100;
970 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
971 + string_cost_one_line (tty->TS_pad_inserted_char));
972 }
973 else
974 {
975 ins_startup_cost = 9999;
976 ins_cost_per_char = 0;
977 }
978
979 if (tty->TS_del_multi_chars)
980 {
981 del_cost_per_char = 0;
982 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
983 }
984 else if (tty->TS_del_char)
985 {
986 del_startup_cost = (string_cost (tty->TS_delete_mode)
987 + string_cost (tty->TS_end_delete_mode));
988 if (tty->delete_in_insert_mode)
989 del_startup_cost /= 2;
990 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
991 }
992 else
993 {
994 del_startup_cost = 9999;
995 del_cost_per_char = 0;
996 }
997
998 /* Delete costs are at negative offsets */
999 p = &char_ins_del_cost (f)[0];
1000 for (i = FRAME_COLS (f); --i >= 0;)
1001 *--p = (del_startup_cost += del_cost_per_char);
1002
1003 /* Doing nothing is free */
1004 p = &char_ins_del_cost (f)[0];
1005 *p++ = 0;
1006
1007 /* Insert costs are at positive offsets */
1008 for (i = FRAME_COLS (f); --i >= 0;)
1009 *p++ = (ins_startup_cost += ins_cost_per_char);
1010 }
1011
1012 void
1013 calculate_costs (struct frame *frame)
1014 {
1015 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1016
1017 if (FRAME_TERMCAP_P (frame))
1018 {
1019 struct tty_display_info *tty = FRAME_TTY (frame);
1020 register char *f = (tty->TS_set_scroll_region
1021 ? tty->TS_set_scroll_region
1022 : tty->TS_set_scroll_region_1);
1023
1024 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1025
1026 tty->costs_set = 1;
1027
1028 /* These variables are only used for terminal stuff. They are
1029 allocated once for the terminal frame of X-windows emacs, but not
1030 used afterwards.
1031
1032 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1033 X turns off char_ins_del_ok. */
1034
1035 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1036 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1037
1038 if (char_ins_del_vector != 0)
1039 char_ins_del_vector
1040 = (int *) xrealloc (char_ins_del_vector,
1041 (sizeof (int)
1042 + 2 * max_frame_cols * sizeof (int)));
1043 else
1044 char_ins_del_vector
1045 = (int *) xmalloc (sizeof (int)
1046 + 2 * max_frame_cols * sizeof (int));
1047
1048 bzero (char_ins_del_vector, (sizeof (int)
1049 + 2 * max_frame_cols * sizeof (int)));
1050
1051
1052 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1053 do_line_insertion_deletion_costs (frame,
1054 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1055 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1056 f, f, 1);
1057 else
1058 do_line_insertion_deletion_costs (frame,
1059 tty->TS_ins_line, tty->TS_ins_multi_lines,
1060 tty->TS_del_line, tty->TS_del_multi_lines,
1061 0, 0, 1);
1062
1063 calculate_ins_del_char_costs (frame);
1064
1065 /* Don't use TS_repeat if its padding is worse than sending the chars */
1066 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1067 tty->RPov = string_cost (tty->TS_repeat);
1068 else
1069 tty->RPov = FRAME_COLS (frame) * 2;
1070
1071 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1072 }
1073 }
1074 \f
1075 struct fkey_table {
1076 char *cap, *name;
1077 };
1078
1079 /* Termcap capability names that correspond directly to X keysyms.
1080 Some of these (marked "terminfo") aren't supplied by old-style
1081 (Berkeley) termcap entries. They're listed in X keysym order;
1082 except we put the keypad keys first, so that if they clash with
1083 other keys (as on the IBM PC keyboard) they get overridden.
1084 */
1085
1086 static struct fkey_table keys[] =
1087 {
1088 {"kh", "home"}, /* termcap */
1089 {"kl", "left"}, /* termcap */
1090 {"ku", "up"}, /* termcap */
1091 {"kr", "right"}, /* termcap */
1092 {"kd", "down"}, /* termcap */
1093 {"%8", "prior"}, /* terminfo */
1094 {"%5", "next"}, /* terminfo */
1095 {"@7", "end"}, /* terminfo */
1096 {"@1", "begin"}, /* terminfo */
1097 {"*6", "select"}, /* terminfo */
1098 {"%9", "print"}, /* terminfo */
1099 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1100 /*
1101 * "insert" --- see below
1102 */
1103 {"&8", "undo"}, /* terminfo */
1104 {"%0", "redo"}, /* terminfo */
1105 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1106 {"@0", "find"}, /* terminfo */
1107 {"@2", "cancel"}, /* terminfo */
1108 {"%1", "help"}, /* terminfo */
1109 /*
1110 * "break" goes here, but can't be reliably intercepted with termcap
1111 */
1112 {"&4", "reset"}, /* terminfo --- actually `restart' */
1113 /*
1114 * "system" and "user" --- no termcaps
1115 */
1116 {"kE", "clearline"}, /* terminfo */
1117 {"kA", "insertline"}, /* terminfo */
1118 {"kL", "deleteline"}, /* terminfo */
1119 {"kI", "insertchar"}, /* terminfo */
1120 {"kD", "deletechar"}, /* terminfo */
1121 {"kB", "backtab"}, /* terminfo */
1122 /*
1123 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1124 */
1125 {"@8", "kp-enter"}, /* terminfo */
1126 /*
1127 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1128 * "kp-multiply", "kp-add", "kp-separator",
1129 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1130 * --- no termcaps for any of these.
1131 */
1132 {"K4", "kp-1"}, /* terminfo */
1133 /*
1134 * "kp-2" --- no termcap
1135 */
1136 {"K5", "kp-3"}, /* terminfo */
1137 /*
1138 * "kp-4" --- no termcap
1139 */
1140 {"K2", "kp-5"}, /* terminfo */
1141 /*
1142 * "kp-6" --- no termcap
1143 */
1144 {"K1", "kp-7"}, /* terminfo */
1145 /*
1146 * "kp-8" --- no termcap
1147 */
1148 {"K3", "kp-9"}, /* terminfo */
1149 /*
1150 * "kp-equal" --- no termcap
1151 */
1152 {"k1", "f1"},
1153 {"k2", "f2"},
1154 {"k3", "f3"},
1155 {"k4", "f4"},
1156 {"k5", "f5"},
1157 {"k6", "f6"},
1158 {"k7", "f7"},
1159 {"k8", "f8"},
1160 {"k9", "f9"},
1161
1162 {"&0", "S-cancel"}, /*shifted cancel key*/
1163 {"&9", "S-begin"}, /*shifted begin key*/
1164 {"*0", "S-find"}, /*shifted find key*/
1165 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1166 {"*4", "S-delete"}, /*shifted delete-character key*/
1167 {"*7", "S-end"}, /*shifted end key*/
1168 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1169 {"#1", "S-help"}, /*shifted help key*/
1170 {"#2", "S-home"}, /*shifted home key*/
1171 {"#3", "S-insert"}, /*shifted insert-character key*/
1172 {"#4", "S-left"}, /*shifted left-arrow key*/
1173 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1174 {"%c", "S-next"}, /*shifted next key*/
1175 {"%e", "S-prior"}, /*shifted previous key*/
1176 {"%f", "S-print"}, /*shifted print key*/
1177 {"%g", "S-redo"}, /*shifted redo key*/
1178 {"%i", "S-right"}, /*shifted right-arrow key*/
1179 {"!3", "S-undo"} /*shifted undo key*/
1180 };
1181
1182 static char **term_get_fkeys_address;
1183 static KBOARD *term_get_fkeys_kboard;
1184 static Lisp_Object term_get_fkeys_1 ();
1185
1186 /* Find the escape codes sent by the function keys for Vfunction_key_map.
1187 This function scans the termcap function key sequence entries, and
1188 adds entries to Vfunction_key_map for each function key it finds. */
1189
1190 static void
1191 term_get_fkeys (address, kboard)
1192 char **address;
1193 KBOARD *kboard;
1194 {
1195 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1196 errors during the call. The only errors should be from Fdefine_key
1197 when given a key sequence containing an invalid prefix key. If the
1198 termcap defines function keys which use a prefix that is already bound
1199 to a command by the default bindings, we should silently ignore that
1200 function key specification, rather than giving the user an error and
1201 refusing to run at all on such a terminal. */
1202
1203 extern Lisp_Object Fidentity ();
1204 term_get_fkeys_address = address;
1205 term_get_fkeys_kboard = kboard;
1206 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1207 }
1208
1209 static Lisp_Object
1210 term_get_fkeys_1 ()
1211 {
1212 int i;
1213
1214 char **address = term_get_fkeys_address;
1215 KBOARD *kboard = term_get_fkeys_kboard;
1216
1217 /* This can happen if CANNOT_DUMP or with strange options. */
1218 if (!initialized)
1219 kboard->Vlocal_function_key_map = Fmake_sparse_keymap (Qnil);
1220
1221 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1222 {
1223 char *sequence = tgetstr (keys[i].cap, address);
1224 if (sequence)
1225 Fdefine_key (kboard->Vlocal_function_key_map, build_string (sequence),
1226 Fmake_vector (make_number (1),
1227 intern (keys[i].name)));
1228 }
1229
1230 /* The uses of the "k0" capability are inconsistent; sometimes it
1231 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1232 We will attempt to politely accommodate both systems by testing for
1233 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1234 */
1235 {
1236 char *k_semi = tgetstr ("k;", address);
1237 char *k0 = tgetstr ("k0", address);
1238 char *k0_name = "f10";
1239
1240 if (k_semi)
1241 {
1242 if (k0)
1243 /* Define f0 first, so that f10 takes precedence in case the
1244 key sequences happens to be the same. */
1245 Fdefine_key (kboard->Vlocal_function_key_map, build_string (k0),
1246 Fmake_vector (make_number (1), intern ("f0")));
1247 Fdefine_key (kboard->Vlocal_function_key_map, build_string (k_semi),
1248 Fmake_vector (make_number (1), intern ("f10")));
1249 }
1250 else if (k0)
1251 Fdefine_key (kboard->Vlocal_function_key_map, build_string (k0),
1252 Fmake_vector (make_number (1), intern (k0_name)));
1253 }
1254
1255 /* Set up cookies for numbered function keys above f10. */
1256 {
1257 char fcap[3], fkey[4];
1258
1259 fcap[0] = 'F'; fcap[2] = '\0';
1260 for (i = 11; i < 64; i++)
1261 {
1262 if (i <= 19)
1263 fcap[1] = '1' + i - 11;
1264 else if (i <= 45)
1265 fcap[1] = 'A' + i - 20;
1266 else
1267 fcap[1] = 'a' + i - 46;
1268
1269 {
1270 char *sequence = tgetstr (fcap, address);
1271 if (sequence)
1272 {
1273 sprintf (fkey, "f%d", i);
1274 Fdefine_key (kboard->Vlocal_function_key_map, build_string (sequence),
1275 Fmake_vector (make_number (1),
1276 intern (fkey)));
1277 }
1278 }
1279 }
1280 }
1281
1282 /*
1283 * Various mappings to try and get a better fit.
1284 */
1285 {
1286 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1287 if (!tgetstr (cap1, address)) \
1288 { \
1289 char *sequence = tgetstr (cap2, address); \
1290 if (sequence) \
1291 Fdefine_key (kboard->Vlocal_function_key_map, build_string (sequence), \
1292 Fmake_vector (make_number (1), \
1293 intern (sym))); \
1294 }
1295
1296 /* if there's no key_next keycap, map key_npage to `next' keysym */
1297 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1298 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1299 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1300 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1301 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1302 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1303 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1304
1305 /* IBM has their own non-standard dialect of terminfo.
1306 If the standard name isn't found, try the IBM name. */
1307 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1308 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1309 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1310 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1311 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1312 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1313 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1314 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1315 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1316 #undef CONDITIONAL_REASSIGN
1317 }
1318
1319 return Qnil;
1320 }
1321
1322 \f
1323 /***********************************************************************
1324 Character Display Information
1325 ***********************************************************************/
1326
1327 /* Avoid name clash with functions defined in xterm.c */
1328 #ifdef static
1329 #define append_glyph append_glyph_term
1330 #define produce_stretch_glyph produce_stretch_glyph_term
1331 #endif
1332
1333 static void append_glyph P_ ((struct it *));
1334 static void produce_stretch_glyph P_ ((struct it *));
1335
1336
1337 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1338 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1339 the character for which to produce glyphs; IT->face_id contains the
1340 character's face. Padding glyphs are appended if IT->c has a
1341 IT->pixel_width > 1. */
1342
1343 static void
1344 append_glyph (it)
1345 struct it *it;
1346 {
1347 struct glyph *glyph, *end;
1348 int i;
1349
1350 xassert (it->glyph_row);
1351 glyph = (it->glyph_row->glyphs[it->area]
1352 + it->glyph_row->used[it->area]);
1353 end = it->glyph_row->glyphs[1 + it->area];
1354
1355 for (i = 0;
1356 i < it->pixel_width && glyph < end;
1357 ++i)
1358 {
1359 glyph->type = CHAR_GLYPH;
1360 glyph->pixel_width = 1;
1361 glyph->u.ch = it->char_to_display;
1362 glyph->face_id = it->face_id;
1363 glyph->padding_p = i > 0;
1364 glyph->charpos = CHARPOS (it->position);
1365 glyph->object = it->object;
1366
1367 ++it->glyph_row->used[it->area];
1368 ++glyph;
1369 }
1370 }
1371
1372
1373 /* Produce glyphs for the display element described by IT. *IT
1374 specifies what we want to produce a glyph for (character, image, ...),
1375 and where in the glyph matrix we currently are (glyph row and hpos).
1376 produce_glyphs fills in output fields of *IT with information such as the
1377 pixel width and height of a character, and maybe output actual glyphs at
1378 the same time if IT->glyph_row is non-null. See the explanation of
1379 struct display_iterator in dispextern.h for an overview.
1380
1381 produce_glyphs also stores the result of glyph width, ascent
1382 etc. computations in *IT.
1383
1384 IT->glyph_row may be null, in which case produce_glyphs does not
1385 actually fill in the glyphs. This is used in the move_* functions
1386 in xdisp.c for text width and height computations.
1387
1388 Callers usually don't call produce_glyphs directly;
1389 instead they use the macro PRODUCE_GLYPHS. */
1390
1391 void
1392 produce_glyphs (it)
1393 struct it *it;
1394 {
1395 /* If a hook is installed, let it do the work. */
1396 xassert (it->what == IT_CHARACTER
1397 || it->what == IT_COMPOSITION
1398 || it->what == IT_STRETCH);
1399
1400 if (it->what == IT_STRETCH)
1401 {
1402 produce_stretch_glyph (it);
1403 goto done;
1404 }
1405
1406 /* Nothing but characters are supported on terminal frames. For a
1407 composition sequence, it->c is the first character of the
1408 sequence. */
1409 xassert (it->what == IT_CHARACTER
1410 || it->what == IT_COMPOSITION);
1411
1412 /* Maybe translate single-byte characters to multibyte. */
1413 it->char_to_display = it->c;
1414
1415 if (it->c >= 040 && it->c < 0177)
1416 {
1417 it->pixel_width = it->nglyphs = 1;
1418 if (it->glyph_row)
1419 append_glyph (it);
1420 }
1421 else if (it->c == '\n')
1422 it->pixel_width = it->nglyphs = 0;
1423 else if (it->c == '\t')
1424 {
1425 int absolute_x = (it->current_x
1426 + it->continuation_lines_width);
1427 int next_tab_x
1428 = (((1 + absolute_x + it->tab_width - 1)
1429 / it->tab_width)
1430 * it->tab_width);
1431 int nspaces;
1432
1433 /* If part of the TAB has been displayed on the previous line
1434 which is continued now, continuation_lines_width will have
1435 been incremented already by the part that fitted on the
1436 continued line. So, we will get the right number of spaces
1437 here. */
1438 nspaces = next_tab_x - absolute_x;
1439
1440 if (it->glyph_row)
1441 {
1442 int n = nspaces;
1443
1444 it->char_to_display = ' ';
1445 it->pixel_width = it->len = 1;
1446
1447 while (n--)
1448 append_glyph (it);
1449 }
1450
1451 it->pixel_width = nspaces;
1452 it->nglyphs = nspaces;
1453 }
1454 else if (SINGLE_BYTE_CHAR_P (it->c))
1455 {
1456 if (unibyte_display_via_language_environment
1457 && (it->c >= 0240
1458 || !NILP (Vnonascii_translation_table)))
1459 {
1460 int charset;
1461
1462 it->char_to_display = unibyte_char_to_multibyte (it->c);
1463 charset = CHAR_CHARSET (it->char_to_display);
1464 it->pixel_width = CHARSET_WIDTH (charset);
1465 it->nglyphs = it->pixel_width;
1466 if (it->glyph_row)
1467 append_glyph (it);
1468 }
1469 else
1470 {
1471 /* Coming here means that it->c is from display table, thus we
1472 must send the code as is to the terminal. Although there's
1473 no way to know how many columns it occupies on a screen, it
1474 is a good assumption that a single byte code has 1-column
1475 width. */
1476 it->pixel_width = it->nglyphs = 1;
1477 if (it->glyph_row)
1478 append_glyph (it);
1479 }
1480 }
1481 else
1482 {
1483 /* A multi-byte character. The display width is fixed for all
1484 characters of the set. Some of the glyphs may have to be
1485 ignored because they are already displayed in a continued
1486 line. */
1487 int charset = CHAR_CHARSET (it->c);
1488
1489 it->pixel_width = CHARSET_WIDTH (charset);
1490 it->nglyphs = it->pixel_width;
1491
1492 if (it->glyph_row)
1493 append_glyph (it);
1494 }
1495
1496 done:
1497 /* Advance current_x by the pixel width as a convenience for
1498 the caller. */
1499 if (it->area == TEXT_AREA)
1500 it->current_x += it->pixel_width;
1501 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1502 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1503 }
1504
1505
1506 /* Produce a stretch glyph for iterator IT. IT->object is the value
1507 of the glyph property displayed. The value must be a list
1508 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1509 being recognized:
1510
1511 1. `:width WIDTH' specifies that the space should be WIDTH *
1512 canonical char width wide. WIDTH may be an integer or floating
1513 point number.
1514
1515 2. `:align-to HPOS' specifies that the space should be wide enough
1516 to reach HPOS, a value in canonical character units. */
1517
1518 static void
1519 produce_stretch_glyph (it)
1520 struct it *it;
1521 {
1522 /* (space :width WIDTH ...) */
1523 Lisp_Object prop, plist;
1524 int width = 0, align_to = -1;
1525 int zero_width_ok_p = 0;
1526 double tem;
1527
1528 /* List should start with `space'. */
1529 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1530 plist = XCDR (it->object);
1531
1532 /* Compute the width of the stretch. */
1533 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1534 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1535 {
1536 /* Absolute width `:width WIDTH' specified and valid. */
1537 zero_width_ok_p = 1;
1538 width = (int)(tem + 0.5);
1539 }
1540 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1541 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1542 {
1543 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1544 align_to = (align_to < 0
1545 ? 0
1546 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1547 else if (align_to < 0)
1548 align_to = window_box_left_offset (it->w, TEXT_AREA);
1549 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1550 zero_width_ok_p = 1;
1551 }
1552 else
1553 /* Nothing specified -> width defaults to canonical char width. */
1554 width = FRAME_COLUMN_WIDTH (it->f);
1555
1556 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1557 width = 1;
1558
1559 if (width > 0 && it->glyph_row)
1560 {
1561 Lisp_Object o_object = it->object;
1562 Lisp_Object object = it->stack[it->sp - 1].string;
1563 int n = width;
1564
1565 if (!STRINGP (object))
1566 object = it->w->buffer;
1567 it->object = object;
1568 it->char_to_display = ' ';
1569 it->pixel_width = it->len = 1;
1570 while (n--)
1571 append_glyph (it);
1572 it->object = o_object;
1573 }
1574 it->pixel_width = width;
1575 it->nglyphs = width;
1576 }
1577
1578
1579 /* Get information about special display element WHAT in an
1580 environment described by IT. WHAT is one of IT_TRUNCATION or
1581 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1582 non-null glyph_row member. This function ensures that fields like
1583 face_id, c, len of IT are left untouched. */
1584
1585 void
1586 produce_special_glyphs (it, what)
1587 struct it *it;
1588 enum display_element_type what;
1589 {
1590 struct it temp_it;
1591 GLYPH glyph;
1592
1593 temp_it = *it;
1594 temp_it.dp = NULL;
1595 temp_it.what = IT_CHARACTER;
1596 temp_it.len = 1;
1597 temp_it.object = make_number (0);
1598 bzero (&temp_it.current, sizeof temp_it.current);
1599
1600 if (what == IT_CONTINUATION)
1601 {
1602 /* Continuation glyph. */
1603 if (it->dp
1604 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1605 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1606 {
1607 glyph = XINT (DISP_CONTINUE_GLYPH (it->dp));
1608 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1609 }
1610 else
1611 glyph = '\\';
1612 }
1613 else if (what == IT_TRUNCATION)
1614 {
1615 /* Truncation glyph. */
1616 if (it->dp
1617 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1618 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1619 {
1620 glyph = XINT (DISP_TRUNC_GLYPH (it->dp));
1621 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1622 }
1623 else
1624 glyph = '$';
1625 }
1626 else
1627 abort ();
1628
1629 temp_it.c = FAST_GLYPH_CHAR (glyph);
1630 temp_it.face_id = FAST_GLYPH_FACE (glyph);
1631 temp_it.len = CHAR_BYTES (temp_it.c);
1632
1633 produce_glyphs (&temp_it);
1634 it->pixel_width = temp_it.pixel_width;
1635 it->nglyphs = temp_it.pixel_width;
1636 }
1637
1638
1639 \f
1640 /***********************************************************************
1641 Faces
1642 ***********************************************************************/
1643
1644 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1645 one of the enumerators from enum no_color_bit, or a bit set built
1646 from them. Some display attributes may not be used together with
1647 color; the termcap capability `NC' specifies which ones. */
1648
1649 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1650 (tty->TN_max_colors > 0 \
1651 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1652 : 1)
1653
1654 /* Turn appearances of face FACE_ID on tty frame F on.
1655 FACE_ID is a realized face ID number, in the face cache. */
1656
1657 static void
1658 turn_on_face (f, face_id)
1659 struct frame *f;
1660 int face_id;
1661 {
1662 struct face *face = FACE_FROM_ID (f, face_id);
1663 long fg = face->foreground;
1664 long bg = face->background;
1665 struct tty_display_info *tty = FRAME_TTY (f);
1666
1667 /* Do this first because TS_end_standout_mode may be the same
1668 as TS_exit_attribute_mode, which turns all appearances off. */
1669 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE))
1670 {
1671 if (tty->TN_max_colors > 0)
1672 {
1673 if (fg >= 0 && bg >= 0)
1674 {
1675 /* If the terminal supports colors, we can set them
1676 below without using reverse video. The face's fg
1677 and bg colors are set as they should appear on
1678 the screen, i.e. they take the inverse-video'ness
1679 of the face already into account. */
1680 }
1681 else if (inverse_video)
1682 {
1683 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1684 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1685 tty_toggle_highlight (tty);
1686 }
1687 else
1688 {
1689 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1690 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1691 tty_toggle_highlight (tty);
1692 }
1693 }
1694 else
1695 {
1696 /* If we can't display colors, use reverse video
1697 if the face specifies that. */
1698 if (inverse_video)
1699 {
1700 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1701 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1702 tty_toggle_highlight (tty);
1703 }
1704 else
1705 {
1706 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1707 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1708 tty_toggle_highlight (tty);
1709 }
1710 }
1711 }
1712
1713 if (face->tty_bold_p)
1714 {
1715 if (MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1716 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1717 }
1718 else if (face->tty_dim_p)
1719 if (MAY_USE_WITH_COLORS_P (tty, NC_DIM))
1720 OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
1721
1722 /* Alternate charset and blinking not yet used. */
1723 if (face->tty_alt_charset_p
1724 && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
1725 OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
1726
1727 if (face->tty_blinking_p
1728 && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
1729 OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
1730
1731 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1732 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
1733
1734 if (tty->TN_max_colors > 0)
1735 {
1736 char *ts, *p;
1737
1738 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
1739 if (fg >= 0 && ts)
1740 {
1741 p = tparam (ts, NULL, 0, (int) fg);
1742 OUTPUT (tty, p);
1743 xfree (p);
1744 }
1745
1746 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
1747 if (bg >= 0 && ts)
1748 {
1749 p = tparam (ts, NULL, 0, (int) bg);
1750 OUTPUT (tty, p);
1751 xfree (p);
1752 }
1753 }
1754 }
1755
1756
1757 /* Turn off appearances of face FACE_ID on tty frame F. */
1758
1759 static void
1760 turn_off_face (f, face_id)
1761 struct frame *f;
1762 int face_id;
1763 {
1764 struct face *face = FACE_FROM_ID (f, face_id);
1765 struct tty_display_info *tty = FRAME_TTY (f);
1766
1767 xassert (face != NULL);
1768
1769 if (tty->TS_exit_attribute_mode)
1770 {
1771 /* Capability "me" will turn off appearance modes double-bright,
1772 half-bright, reverse-video, standout, underline. It may or
1773 may not turn off alt-char-mode. */
1774 if (face->tty_bold_p
1775 || face->tty_dim_p
1776 || face->tty_reverse_p
1777 || face->tty_alt_charset_p
1778 || face->tty_blinking_p
1779 || face->tty_underline_p)
1780 {
1781 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
1782 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
1783 tty->standout_mode = 0;
1784 }
1785
1786 if (face->tty_alt_charset_p)
1787 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1788 }
1789 else
1790 {
1791 /* If we don't have "me" we can only have those appearances
1792 that have exit sequences defined. */
1793 if (face->tty_alt_charset_p)
1794 OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
1795
1796 if (face->tty_underline_p)
1797 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
1798 }
1799
1800 /* Switch back to default colors. */
1801 if (tty->TN_max_colors > 0
1802 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
1803 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
1804 || (face->background != FACE_TTY_DEFAULT_COLOR
1805 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
1806 OUTPUT1_IF (tty, tty->TS_orig_pair);
1807 }
1808
1809
1810 /* Return non-zero if the terminal on frame F supports all of the
1811 capabilities in CAPS simultaneously, with foreground and background
1812 colors FG and BG. */
1813
1814 int
1815 tty_capable_p (tty, caps, fg, bg)
1816 struct tty_display_info *tty;
1817 unsigned caps;
1818 unsigned long fg, bg;
1819 {
1820 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
1821 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
1822 return 0;
1823
1824 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
1825 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
1826 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
1827 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
1828 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK, tty->TS_enter_blink_mode, NC_BLINK);
1829 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET, tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
1830
1831 /* We can do it! */
1832 return 1;
1833 }
1834
1835 /* Return non-zero if the terminal is capable to display colors. */
1836
1837 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
1838 0, 1, 0,
1839 doc: /* Return non-nil if the tty device TERMINAL can display colors.
1840
1841 TERMINAL can be a terminal id, a frame or nil (meaning the selected
1842 frame's terminal). This function always returns nil if TERMINAL
1843 is not on a tty device. */)
1844 (terminal)
1845 Lisp_Object terminal;
1846 {
1847 struct terminal *t = get_tty_terminal (terminal, 0);
1848 if (!t)
1849 return Qnil;
1850 else
1851 return t->display_info.tty->TN_max_colors > 0 ? Qt : Qnil;
1852 }
1853
1854 /* Return the number of supported colors. */
1855 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
1856 Stty_display_color_cells, 0, 1, 0,
1857 doc: /* Return the number of colors supported by the tty device TERMINAL.
1858
1859 TERMINAL can be a terminal id, a frame or nil (meaning the selected
1860 frame's terminal). This function always returns 0 if TERMINAL
1861 is not on a tty device. */)
1862 (terminal)
1863 Lisp_Object terminal;
1864 {
1865 struct terminal *t = get_tty_terminal (terminal, 0);
1866 if (!t)
1867 return make_number (0);
1868 else
1869 return make_number (t->display_info.tty->TN_max_colors);
1870 }
1871
1872 #ifndef WINDOWSNT
1873
1874 /* Save or restore the default color-related capabilities of this
1875 terminal. */
1876 static void
1877 tty_default_color_capabilities (struct tty_display_info *tty, int save)
1878 {
1879 static char
1880 *default_orig_pair, *default_set_foreground, *default_set_background;
1881 static int default_max_colors, default_max_pairs, default_no_color_video;
1882
1883 if (save)
1884 {
1885 if (default_orig_pair)
1886 xfree (default_orig_pair);
1887 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL;
1888
1889 if (default_set_foreground)
1890 xfree (default_set_foreground);
1891 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
1892 : NULL;
1893
1894 if (default_set_background)
1895 xfree (default_set_background);
1896 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
1897 : NULL;
1898
1899 default_max_colors = tty->TN_max_colors;
1900 default_max_pairs = tty->TN_max_pairs;
1901 default_no_color_video = tty->TN_no_color_video;
1902 }
1903 else
1904 {
1905 tty->TS_orig_pair = default_orig_pair;
1906 tty->TS_set_foreground = default_set_foreground;
1907 tty->TS_set_background = default_set_background;
1908 tty->TN_max_colors = default_max_colors;
1909 tty->TN_max_pairs = default_max_pairs;
1910 tty->TN_no_color_video = default_no_color_video;
1911 }
1912 }
1913
1914 /* Setup one of the standard tty color schemes according to MODE.
1915 MODE's value is generally the number of colors which we want to
1916 support; zero means set up for the default capabilities, the ones
1917 we saw at init_tty time; -1 means turn off color support. */
1918 static void
1919 tty_setup_colors (struct tty_display_info *tty, int mode)
1920 {
1921 /* Canonicalize all negative values of MODE. */
1922 if (mode < -1)
1923 mode = -1;
1924
1925 switch (mode)
1926 {
1927 case -1: /* no colors at all */
1928 tty->TN_max_colors = 0;
1929 tty->TN_max_pairs = 0;
1930 tty->TN_no_color_video = 0;
1931 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
1932 break;
1933 case 0: /* default colors, if any */
1934 default:
1935 tty_default_color_capabilities (tty, 0);
1936 break;
1937 case 8: /* 8 standard ANSI colors */
1938 tty->TS_orig_pair = "\033[0m";
1939 #ifdef TERMINFO
1940 tty->TS_set_foreground = "\033[3%p1%dm";
1941 tty->TS_set_background = "\033[4%p1%dm";
1942 #else
1943 tty->TS_set_foreground = "\033[3%dm";
1944 tty->TS_set_background = "\033[4%dm";
1945 #endif
1946 tty->TN_max_colors = 8;
1947 tty->TN_max_pairs = 64;
1948 tty->TN_no_color_video = 0;
1949 break;
1950 }
1951 }
1952
1953 void
1954 set_tty_color_mode (f, val)
1955 struct frame *f;
1956 Lisp_Object val;
1957 {
1958 Lisp_Object color_mode_spec, current_mode_spec;
1959 Lisp_Object color_mode, current_mode;
1960 int mode, old_mode;
1961 extern Lisp_Object Qtty_color_mode;
1962 Lisp_Object tty_color_mode_alist;
1963
1964 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
1965 Qnil);
1966
1967 if (INTEGERP (val))
1968 color_mode = val;
1969 else
1970 {
1971 if (NILP (tty_color_mode_alist))
1972 color_mode_spec = Qnil;
1973 else
1974 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
1975
1976 if (CONSP (color_mode_spec))
1977 color_mode = XCDR (color_mode_spec);
1978 else
1979 color_mode = Qnil;
1980 }
1981
1982 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
1983
1984 if (CONSP (current_mode_spec))
1985 current_mode = XCDR (current_mode_spec);
1986 else
1987 current_mode = Qnil;
1988 if (INTEGERP (color_mode))
1989 mode = XINT (color_mode);
1990 else
1991 mode = 0; /* meaning default */
1992 if (INTEGERP (current_mode))
1993 old_mode = XINT (current_mode);
1994 else
1995 old_mode = 0;
1996
1997 if (mode != old_mode)
1998 {
1999 tty_setup_colors (FRAME_TTY (f), mode);
2000 /* This recomputes all the faces given the new color
2001 definitions. */
2002 call0 (intern ("tty-set-up-initial-frame-faces"));
2003 redraw_frame (f);
2004 }
2005 }
2006
2007 #endif /* !WINDOWSNT */
2008
2009 \f
2010
2011 /* Return the tty display object specified by TERMINAL. */
2012
2013 struct terminal *
2014 get_tty_terminal (Lisp_Object terminal, int throw)
2015 {
2016 struct terminal *t = get_terminal (terminal, throw);
2017
2018 if (t && t->type == output_initial)
2019 return NULL;
2020
2021 if (t && t->type != output_termcap)
2022 {
2023 if (throw)
2024 error ("Device %d is not a termcap terminal device", t->id);
2025 else
2026 return NULL;
2027 }
2028
2029 return t;
2030 }
2031
2032 /* Return an active termcap device that uses the tty device with the
2033 given name.
2034
2035 This function ignores suspended devices.
2036
2037 Returns NULL if the named terminal device is not opened. */
2038
2039 struct terminal *
2040 get_named_tty (name)
2041 char *name;
2042 {
2043 struct terminal *t;
2044
2045 if (!name)
2046 abort ();
2047
2048 for (t = terminal_list; t; t = t->next_terminal)
2049 {
2050 if (t->type == output_termcap
2051 && !strcmp (t->display_info.tty->name, name)
2052 && TERMINAL_ACTIVE_P (t))
2053 return t;
2054 }
2055
2056 return 0;
2057 }
2058
2059 \f
2060 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2061 doc: /* Return the type of the tty device that TERMINAL uses.
2062 Returns nil if TERMINAL is not on a tty device.
2063
2064 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2065 frame's terminal). */)
2066 (terminal)
2067 Lisp_Object terminal;
2068 {
2069 struct terminal *t = get_terminal (terminal, 1);
2070
2071 if (t->type != output_termcap)
2072 return Qnil;
2073
2074 if (t->display_info.tty->type)
2075 return build_string (t->display_info.tty->type);
2076 else
2077 return Qnil;
2078 }
2079
2080 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2081 doc: /* Return non-nil if TERMINAL is on the controlling tty of the Emacs process.
2082
2083 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2084 frame's terminal). This function always returns nil if TERMINAL
2085 is not on a tty device. */)
2086 (terminal)
2087 Lisp_Object terminal;
2088 {
2089 struct terminal *t = get_terminal (terminal, 1);
2090
2091 if (t->type != output_termcap || strcmp (t->display_info.tty->name, "/dev/tty"))
2092 return Qnil;
2093 else
2094 return Qt;
2095 }
2096
2097 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2098 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2099 This is used to override the terminfo data, for certain terminals that
2100 do not really do underlining, but say that they do. This function has
2101 no effect if used on a non-tty terminal.
2102
2103 TERMINAL can be a terminal id, a frame or nil (meaning the selected
2104 frame's terminal). This function always returns nil if TERMINAL
2105 is not on a tty device. */)
2106 (terminal)
2107 Lisp_Object terminal;
2108 {
2109 struct terminal *t = get_terminal (terminal, 1);
2110
2111 if (t->type == output_termcap)
2112 t->display_info.tty->TS_enter_underline_mode = 0;
2113 return Qnil;
2114 }
2115
2116 \f
2117
2118 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2119 doc: /* Suspend the terminal device TTY.
2120
2121 The device is restored to its default state, and Emacs ceases all
2122 access to the tty device. Frames that use the device are not deleted,
2123 but input is not read from them and if they change, their display is
2124 not updated.
2125
2126 TTY may be a terminal id, a frame, or nil for the terminal device of
2127 the currently selected frame.
2128
2129 This function runs `suspend-tty-functions' after suspending the
2130 device. The functions are run with one arg, the id of the suspended
2131 terminal device.
2132
2133 `suspend-tty' does nothing if it is called on a device that is already
2134 suspended.
2135
2136 A suspended tty may be resumed by calling `resume-tty' on it. */)
2137 (tty)
2138 Lisp_Object tty;
2139 {
2140 struct terminal *t = get_tty_terminal (tty, 1);
2141 FILE *f;
2142
2143 if (!t)
2144 error ("Unknown tty device");
2145
2146 f = t->display_info.tty->input;
2147
2148 if (f)
2149 {
2150 reset_sys_modes (t->display_info.tty);
2151
2152 delete_keyboard_wait_descriptor (fileno (f));
2153
2154 fclose (f);
2155 if (f != t->display_info.tty->output)
2156 fclose (t->display_info.tty->output);
2157
2158 t->display_info.tty->input = 0;
2159 t->display_info.tty->output = 0;
2160
2161 if (FRAMEP (t->display_info.tty->top_frame))
2162 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2163
2164 /* Run `suspend-tty-functions'. */
2165 if (!NILP (Vrun_hooks))
2166 {
2167 Lisp_Object args[2];
2168 args[0] = intern ("suspend-tty-functions");
2169 args[1] = make_number (t->id);
2170 Frun_hook_with_args (2, args);
2171 }
2172 }
2173
2174 /* Clear display hooks to prevent further output. */
2175 clear_tty_hooks (t);
2176
2177 return Qnil;
2178 }
2179
2180 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2181 doc: /* Resume the previously suspended terminal device TTY.
2182 The terminal is opened and reinitialized. Frames that are on the
2183 suspended terminal are revived.
2184
2185 It is an error to resume a terminal while another terminal is active
2186 on the same device.
2187
2188 This function runs `resume-tty-functions' after resuming the terminal.
2189 The functions are run with one arg, the id of the resumed terminal
2190 device.
2191
2192 `resume-tty' does nothing if it is called on a device that is not
2193 suspended.
2194
2195 TTY may be a terminal id, a frame, or nil for the terminal device of
2196 the currently selected frame. */)
2197 (tty)
2198 Lisp_Object tty;
2199 {
2200 struct terminal *t = get_tty_terminal (tty, 1);
2201 int fd;
2202
2203 if (!t)
2204 error ("Unknown tty device");
2205
2206 if (!t->display_info.tty->input)
2207 {
2208 if (get_named_tty (t->display_info.tty->name))
2209 error ("Cannot resume display while another display is active on the same device");
2210
2211 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2212
2213 if (fd == -1)
2214 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno));
2215
2216 if (strcmp (t->display_info.tty->name, "/dev/tty"))
2217 dissociate_if_controlling_tty (fd);
2218
2219 t->display_info.tty->output = fdopen (fd, "w+");
2220 t->display_info.tty->input = t->display_info.tty->output;
2221
2222 add_keyboard_wait_descriptor (fd);
2223
2224 if (FRAMEP (t->display_info.tty->top_frame))
2225 FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2226
2227 init_sys_modes (t->display_info.tty);
2228
2229 /* Run `suspend-tty-functions'. */
2230 if (!NILP (Vrun_hooks))
2231 {
2232 Lisp_Object args[2];
2233 args[0] = intern ("resume-tty-functions");
2234 args[1] = make_number (t->id);
2235 Frun_hook_with_args (2, args);
2236 }
2237 }
2238
2239 set_tty_hooks (t);
2240
2241 return Qnil;
2242 }
2243
2244 \f
2245 /***********************************************************************
2246 Initialization
2247 ***********************************************************************/
2248
2249 /* Initialize the tty-dependent part of frame F. The frame must
2250 already have its device initialized. */
2251
2252 void
2253 create_tty_output (struct frame *f)
2254 {
2255 struct tty_output *t;
2256
2257 if (! FRAME_TERMCAP_P (f))
2258 abort ();
2259
2260 t = xmalloc (sizeof (struct tty_output));
2261 bzero (t, sizeof (struct tty_output));
2262
2263 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
2264
2265 f->output_data.tty = t;
2266 }
2267
2268 /* Delete the tty-dependent part of frame F. */
2269
2270 static void
2271 delete_tty_output (struct frame *f)
2272 {
2273 if (! FRAME_TERMCAP_P (f))
2274 abort ();
2275
2276 xfree (f->output_data.tty);
2277 }
2278
2279 \f
2280
2281 static void
2282 clear_tty_hooks (struct terminal *terminal)
2283 {
2284 terminal->rif = 0;
2285 terminal->cursor_to_hook = 0;
2286 terminal->raw_cursor_to_hook = 0;
2287 terminal->clear_to_end_hook = 0;
2288 terminal->clear_frame_hook = 0;
2289 terminal->clear_end_of_line_hook = 0;
2290 terminal->ins_del_lines_hook = 0;
2291 terminal->insert_glyphs_hook = 0;
2292 terminal->write_glyphs_hook = 0;
2293 terminal->delete_glyphs_hook = 0;
2294 terminal->ring_bell_hook = 0;
2295 terminal->reset_terminal_modes_hook = 0;
2296 terminal->set_terminal_modes_hook = 0;
2297 terminal->update_begin_hook = 0;
2298 terminal->update_end_hook = 0;
2299 terminal->set_terminal_window_hook = 0;
2300 terminal->mouse_position_hook = 0;
2301 terminal->frame_rehighlight_hook = 0;
2302 terminal->frame_raise_lower_hook = 0;
2303 terminal->fullscreen_hook = 0;
2304 terminal->set_vertical_scroll_bar_hook = 0;
2305 terminal->condemn_scroll_bars_hook = 0;
2306 terminal->redeem_scroll_bar_hook = 0;
2307 terminal->judge_scroll_bars_hook = 0;
2308 terminal->read_socket_hook = 0;
2309 terminal->frame_up_to_date_hook = 0;
2310
2311 /* Leave these two set, or suspended frames are not deleted
2312 correctly. */
2313 terminal->delete_frame_hook = &delete_tty_output;
2314 terminal->delete_terminal_hook = &delete_tty;
2315 }
2316
2317 static void
2318 set_tty_hooks (struct terminal *terminal)
2319 {
2320 terminal->rif = 0; /* ttys don't support window-based redisplay. */
2321
2322 terminal->cursor_to_hook = &tty_cursor_to;
2323 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
2324
2325 terminal->clear_to_end_hook = &tty_clear_to_end;
2326 terminal->clear_frame_hook = &tty_clear_frame;
2327 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
2328
2329 terminal->ins_del_lines_hook = &tty_ins_del_lines;
2330
2331 terminal->insert_glyphs_hook = &tty_insert_glyphs;
2332 terminal->write_glyphs_hook = &tty_write_glyphs;
2333 terminal->delete_glyphs_hook = &tty_delete_glyphs;
2334
2335 terminal->ring_bell_hook = &tty_ring_bell;
2336
2337 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
2338 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
2339 terminal->update_begin_hook = 0; /* Not needed. */
2340 terminal->update_end_hook = &tty_update_end;
2341 terminal->set_terminal_window_hook = &tty_set_terminal_window;
2342
2343 terminal->mouse_position_hook = 0; /* Not needed. */
2344 terminal->frame_rehighlight_hook = 0; /* Not needed. */
2345 terminal->frame_raise_lower_hook = 0; /* Not needed. */
2346
2347 terminal->set_vertical_scroll_bar_hook = 0; /* Not needed. */
2348 terminal->condemn_scroll_bars_hook = 0; /* Not needed. */
2349 terminal->redeem_scroll_bar_hook = 0; /* Not needed. */
2350 terminal->judge_scroll_bars_hook = 0; /* Not needed. */
2351
2352 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
2353 terminal->frame_up_to_date_hook = 0; /* Not needed. */
2354
2355 terminal->delete_frame_hook = &delete_tty_output;
2356 terminal->delete_terminal_hook = &delete_tty;
2357 }
2358
2359 /* Drop the controlling terminal if fd is the same device. */
2360 static void
2361 dissociate_if_controlling_tty (int fd)
2362 {
2363 int pgid;
2364 EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
2365 if (pgid != -1)
2366 {
2367 #if defined (USG) && !defined (BSD_PGRPS)
2368 setpgrp ();
2369 no_controlling_tty = 1;
2370 #else
2371 #ifdef TIOCNOTTY /* Try BSD ioctls. */
2372 sigblock (sigmask (SIGTTOU));
2373 fd = emacs_open ("/dev/tty", O_RDWR, 0);
2374 if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
2375 {
2376 no_controlling_tty = 1;
2377 }
2378 if (fd != -1)
2379 emacs_close (fd);
2380 sigunblock (sigmask (SIGTTOU));
2381 #else
2382 /* Unknown system. */
2383 croak ();
2384 #endif /* ! TIOCNOTTY */
2385 #endif /* ! USG */
2386 }
2387 }
2388
2389 static void maybe_fatal();
2390
2391 /* Create a termcap display on the tty device with the given name and
2392 type.
2393
2394 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
2395 Otherwise NAME should be a path to the tty device file,
2396 e.g. "/dev/pts/7".
2397
2398 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
2399
2400 If MUST_SUCCEED is true, then all errors are fatal. */
2401
2402 struct terminal *
2403 init_tty (char *name, char *terminal_type, int must_succeed)
2404 {
2405 char *area = NULL;
2406 char **address = &area;
2407 char *buffer = NULL;
2408 int buffer_size = 4096;
2409 register char *p = NULL;
2410 int status;
2411 struct tty_display_info *tty = NULL;
2412 struct terminal *terminal = NULL;
2413 int ctty = 0; /* 1 if asked to open controlling tty. */
2414
2415 if (!terminal_type)
2416 maybe_fatal (must_succeed, 0, 0,
2417 "Unknown terminal type",
2418 "Unknown terminal type");
2419
2420 if (name == NULL)
2421 name = "/dev/tty";
2422 if (!strcmp (name, "/dev/tty"))
2423 ctty = 1;
2424
2425 /* If we already have a terminal on the given device, use that. If
2426 all such terminals are suspended, create a new one instead. */
2427 /* XXX Perhaps this should be made explicit by having init_tty
2428 always create a new terminal and separating terminal and frame
2429 creation on Lisp level. */
2430 terminal = get_named_tty (name);
2431 if (terminal)
2432 return terminal;
2433
2434 terminal = create_terminal ();
2435 tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
2436 bzero (tty, sizeof (struct tty_display_info));
2437 tty->next = tty_list;
2438 tty_list = tty;
2439
2440 terminal->type = output_termcap;
2441 terminal->display_info.tty = tty;
2442 tty->terminal = terminal;
2443
2444 tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
2445 Wcm_clear (tty);
2446
2447 set_tty_hooks (terminal);
2448
2449 {
2450 int fd;
2451 FILE *file;
2452
2453 #ifdef O_IGNORE_CTTY
2454 if (!ctty)
2455 /* Open the terminal device. Don't recognize it as our
2456 controlling terminal, and don't make it the controlling tty
2457 if we don't have one at the moment. */
2458 fd = emacs_open (name, O_RDWR | O_IGNORE_CTTY | O_NOCTTY, 0);
2459 else
2460 #else
2461 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
2462 defined on Hurd. On other systems, we need to explicitly
2463 dissociate ourselves from the controlling tty when we want to
2464 open a frame on the same terminal. */
2465 fd = emacs_open (name, O_RDWR | O_NOCTTY, 0);
2466 #endif /* O_IGNORE_CTTY */
2467
2468 if (fd < 0)
2469 maybe_fatal (must_succeed, buffer, terminal,
2470 "Could not open file: %s",
2471 "Could not open file: %s",
2472 name);
2473 if (!isatty (fd))
2474 {
2475 close (fd);
2476 maybe_fatal (must_succeed, buffer, terminal,
2477 "Not a tty device: %s",
2478 "Not a tty device: %s",
2479 name);
2480 }
2481
2482 #ifndef O_IGNORE_CTTY
2483 if (!ctty)
2484 dissociate_if_controlling_tty (fd);
2485 #endif
2486
2487 file = fdopen (fd, "w+");
2488 tty->name = xstrdup (name);
2489 terminal->name = xstrdup (name);
2490 tty->input = file;
2491 tty->output = file;
2492 }
2493
2494 tty->type = xstrdup (terminal_type);
2495
2496 add_keyboard_wait_descriptor (fileno (tty->input));
2497
2498 encode_terminal_bufsize = 0;
2499
2500 #ifdef WINDOWSNT
2501 initialize_w32_display ();
2502
2503 Wcm_clear (tty);
2504
2505 area = (char *) xmalloc (2044);
2506
2507 FrameRows (tty) = FRAME_LINES (f); /* XXX */
2508 FrameCols (tty) = FRAME_COLS (f); /* XXX */
2509 tty->specified_window = FRAME_LINES (f); /* XXX */
2510
2511 tty->terminal->delete_in_insert_mode = 1;
2512
2513 UseTabs (tty) = 0;
2514 terminal->scroll_region_ok = 0;
2515
2516 /* Seems to insert lines when it's not supposed to, messing up the
2517 display. In doing a trace, it didn't seem to be called much, so I
2518 don't think we're losing anything by turning it off. */
2519 terminal->line_ins_del_ok = 0;
2520 terminal->char_ins_del_ok = 1;
2521
2522 baud_rate = 19200;
2523
2524 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0; /* XXX */
2525 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none; /* XXX */
2526 TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
2527
2528 return terminal;
2529 #else /* not WINDOWSNT */
2530
2531 Wcm_clear (tty);
2532
2533 buffer = (char *) xmalloc (buffer_size);
2534
2535 /* On some systems, tgetent tries to access the controlling
2536 terminal. */
2537 sigblock (sigmask (SIGTTOU));
2538 status = tgetent (buffer, terminal_type);
2539 sigunblock (sigmask (SIGTTOU));
2540
2541 if (status < 0)
2542 {
2543 #ifdef TERMINFO
2544 maybe_fatal (must_succeed, buffer, terminal,
2545 "Cannot open terminfo database file",
2546 "Cannot open terminfo database file");
2547 #else
2548 maybe_fatal (must_succeed, buffer, terminal,
2549 "Cannot open termcap database file",
2550 "Cannot open termcap database file");
2551 #endif
2552 }
2553 if (status == 0)
2554 {
2555 #ifdef TERMINFO
2556 maybe_fatal (must_succeed, buffer, terminal,
2557 "Terminal type %s is not defined",
2558 "Terminal type %s is not defined.\n\
2559 If that is not the actual type of terminal you have,\n\
2560 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2561 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2562 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2563 terminal_type);
2564 #else
2565 maybe_fatal (must_succeed, buffer, terminal,
2566 "Terminal type %s is not defined",
2567 "Terminal type %s is not defined.\n\
2568 If that is not the actual type of terminal you have,\n\
2569 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2570 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2571 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2572 terminal_type);
2573 #endif
2574 }
2575
2576 #ifndef TERMINFO
2577 if (strlen (buffer) >= buffer_size)
2578 abort ();
2579 buffer_size = strlen (buffer);
2580 #endif
2581 area = (char *) xmalloc (buffer_size);
2582
2583 tty->TS_ins_line = tgetstr ("al", address);
2584 tty->TS_ins_multi_lines = tgetstr ("AL", address);
2585 tty->TS_bell = tgetstr ("bl", address);
2586 BackTab (tty) = tgetstr ("bt", address);
2587 tty->TS_clr_to_bottom = tgetstr ("cd", address);
2588 tty->TS_clr_line = tgetstr ("ce", address);
2589 tty->TS_clr_frame = tgetstr ("cl", address);
2590 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
2591 AbsPosition (tty) = tgetstr ("cm", address);
2592 CR (tty) = tgetstr ("cr", address);
2593 tty->TS_set_scroll_region = tgetstr ("cs", address);
2594 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
2595 RowPosition (tty) = tgetstr ("cv", address);
2596 tty->TS_del_char = tgetstr ("dc", address);
2597 tty->TS_del_multi_chars = tgetstr ("DC", address);
2598 tty->TS_del_line = tgetstr ("dl", address);
2599 tty->TS_del_multi_lines = tgetstr ("DL", address);
2600 tty->TS_delete_mode = tgetstr ("dm", address);
2601 tty->TS_end_delete_mode = tgetstr ("ed", address);
2602 tty->TS_end_insert_mode = tgetstr ("ei", address);
2603 Home (tty) = tgetstr ("ho", address);
2604 tty->TS_ins_char = tgetstr ("ic", address);
2605 tty->TS_ins_multi_chars = tgetstr ("IC", address);
2606 tty->TS_insert_mode = tgetstr ("im", address);
2607 tty->TS_pad_inserted_char = tgetstr ("ip", address);
2608 tty->TS_end_keypad_mode = tgetstr ("ke", address);
2609 tty->TS_keypad_mode = tgetstr ("ks", address);
2610 LastLine (tty) = tgetstr ("ll", address);
2611 Right (tty) = tgetstr ("nd", address);
2612 Down (tty) = tgetstr ("do", address);
2613 if (!Down (tty))
2614 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do" */
2615 #ifdef VMS
2616 /* VMS puts a carriage return before each linefeed,
2617 so it is not safe to use linefeeds. */
2618 if (Down (tty) && Down (tty)[0] == '\n' && Down (tty)[1] == '\0')
2619 Down (tty) = 0;
2620 #endif /* VMS */
2621 if (tgetflag ("bs"))
2622 Left (tty) = "\b"; /* can't possibly be longer! */
2623 else /* (Actually, "bs" is obsolete...) */
2624 Left (tty) = tgetstr ("le", address);
2625 if (!Left (tty))
2626 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le" */
2627 tty->TS_pad_char = tgetstr ("pc", address);
2628 tty->TS_repeat = tgetstr ("rp", address);
2629 tty->TS_end_standout_mode = tgetstr ("se", address);
2630 tty->TS_fwd_scroll = tgetstr ("sf", address);
2631 tty->TS_standout_mode = tgetstr ("so", address);
2632 tty->TS_rev_scroll = tgetstr ("sr", address);
2633 tty->Wcm->cm_tab = tgetstr ("ta", address);
2634 tty->TS_end_termcap_modes = tgetstr ("te", address);
2635 tty->TS_termcap_modes = tgetstr ("ti", address);
2636 Up (tty) = tgetstr ("up", address);
2637 tty->TS_visible_bell = tgetstr ("vb", address);
2638 tty->TS_cursor_normal = tgetstr ("ve", address);
2639 tty->TS_cursor_visible = tgetstr ("vs", address);
2640 tty->TS_cursor_invisible = tgetstr ("vi", address);
2641 tty->TS_set_window = tgetstr ("wi", address);
2642
2643 tty->TS_enter_underline_mode = tgetstr ("us", address);
2644 tty->TS_exit_underline_mode = tgetstr ("ue", address);
2645 tty->TS_enter_bold_mode = tgetstr ("md", address);
2646 tty->TS_enter_dim_mode = tgetstr ("mh", address);
2647 tty->TS_enter_blink_mode = tgetstr ("mb", address);
2648 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
2649 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
2650 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
2651 tty->TS_exit_attribute_mode = tgetstr ("me", address);
2652
2653 MultiUp (tty) = tgetstr ("UP", address);
2654 MultiDown (tty) = tgetstr ("DO", address);
2655 MultiLeft (tty) = tgetstr ("LE", address);
2656 MultiRight (tty) = tgetstr ("RI", address);
2657
2658 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
2659 color because we can't switch back to the default foreground and
2660 background. */
2661 tty->TS_orig_pair = tgetstr ("op", address);
2662 if (tty->TS_orig_pair)
2663 {
2664 tty->TS_set_foreground = tgetstr ("AF", address);
2665 tty->TS_set_background = tgetstr ("AB", address);
2666 if (!tty->TS_set_foreground)
2667 {
2668 /* SVr4. */
2669 tty->TS_set_foreground = tgetstr ("Sf", address);
2670 tty->TS_set_background = tgetstr ("Sb", address);
2671 }
2672
2673 tty->TN_max_colors = tgetnum ("Co");
2674 tty->TN_max_pairs = tgetnum ("pa");
2675
2676 tty->TN_no_color_video = tgetnum ("NC");
2677 if (tty->TN_no_color_video == -1)
2678 tty->TN_no_color_video = 0;
2679 }
2680
2681 tty_default_color_capabilities (tty, 1);
2682
2683 MagicWrap (tty) = tgetflag ("xn");
2684 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
2685 the former flag imply the latter. */
2686 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
2687 terminal->memory_below_frame = tgetflag ("db");
2688 tty->TF_hazeltine = tgetflag ("hz");
2689 terminal->must_write_spaces = tgetflag ("in");
2690 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
2691 tty->TF_insmode_motion = tgetflag ("mi");
2692 tty->TF_standout_motion = tgetflag ("ms");
2693 tty->TF_underscore = tgetflag ("ul");
2694 tty->TF_teleray = tgetflag ("xt");
2695
2696 #ifdef MULTI_KBOARD
2697 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
2698 init_kboard (terminal->kboard);
2699 terminal->kboard->next_kboard = all_kboards;
2700 all_kboards = terminal->kboard;
2701 terminal->kboard->reference_count++;
2702 /* Don't let the initial kboard remain current longer than necessary.
2703 That would cause problems if a file loaded on startup tries to
2704 prompt in the mini-buffer. */
2705 if (current_kboard == initial_kboard)
2706 current_kboard = terminal->kboard;
2707 #endif
2708
2709 term_get_fkeys (address, terminal->kboard);
2710
2711 /* Get frame size from system, or else from termcap. */
2712 {
2713 int height, width;
2714 get_tty_size (fileno (tty->input), &width, &height);
2715 FrameCols (tty) = width;
2716 FrameRows (tty) = height;
2717 }
2718
2719 if (FrameCols (tty) <= 0)
2720 FrameCols (tty) = tgetnum ("co");
2721 if (FrameRows (tty) <= 0)
2722 FrameRows (tty) = tgetnum ("li");
2723
2724 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
2725 maybe_fatal (must_succeed, NULL, terminal,
2726 "Screen size %dx%d is too small"
2727 "Screen size %dx%d is too small",
2728 FrameCols (tty), FrameRows (tty));
2729
2730 #if 0 /* This is not used anywhere. */
2731 tty->terminal->min_padding_speed = tgetnum ("pb");
2732 #endif
2733
2734 TabWidth (tty) = tgetnum ("tw");
2735
2736 #ifdef VMS
2737 /* These capabilities commonly use ^J.
2738 I don't know why, but sending them on VMS does not work;
2739 it causes following spaces to be lost, sometimes.
2740 For now, the simplest fix is to avoid using these capabilities ever. */
2741 if (Down (tty) && Down (tty)[0] == '\n')
2742 Down (tty) = 0;
2743 #endif /* VMS */
2744
2745 if (!tty->TS_bell)
2746 tty->TS_bell = "\07";
2747
2748 if (!tty->TS_fwd_scroll)
2749 tty->TS_fwd_scroll = Down (tty);
2750
2751 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
2752
2753 if (TabWidth (tty) < 0)
2754 TabWidth (tty) = 8;
2755
2756 /* Turned off since /etc/termcap seems to have :ta= for most terminals
2757 and newer termcap doc does not seem to say there is a default.
2758 if (!tty->Wcm->cm_tab)
2759 tty->Wcm->cm_tab = "\t";
2760 */
2761
2762 /* We don't support standout modes that use `magic cookies', so
2763 turn off any that do. */
2764 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
2765 {
2766 tty->TS_standout_mode = 0;
2767 tty->TS_end_standout_mode = 0;
2768 }
2769 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
2770 {
2771 tty->TS_enter_underline_mode = 0;
2772 tty->TS_exit_underline_mode = 0;
2773 }
2774
2775 /* If there's no standout mode, try to use underlining instead. */
2776 if (tty->TS_standout_mode == 0)
2777 {
2778 tty->TS_standout_mode = tty->TS_enter_underline_mode;
2779 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
2780 }
2781
2782 /* If no `se' string, try using a `me' string instead.
2783 If that fails, we can't use standout mode at all. */
2784 if (tty->TS_end_standout_mode == 0)
2785 {
2786 char *s = tgetstr ("me", address);
2787 if (s != 0)
2788 tty->TS_end_standout_mode = s;
2789 else
2790 tty->TS_standout_mode = 0;
2791 }
2792
2793 if (tty->TF_teleray)
2794 {
2795 tty->Wcm->cm_tab = 0;
2796 /* We can't support standout mode, because it uses magic cookies. */
2797 tty->TS_standout_mode = 0;
2798 /* But that means we cannot rely on ^M to go to column zero! */
2799 CR (tty) = 0;
2800 /* LF can't be trusted either -- can alter hpos */
2801 /* if move at column 0 thru a line with TS_standout_mode */
2802 Down (tty) = 0;
2803 }
2804
2805 /* Special handling for certain terminal types known to need it */
2806
2807 if (!strcmp (terminal_type, "supdup"))
2808 {
2809 terminal->memory_below_frame = 1;
2810 tty->Wcm->cm_losewrap = 1;
2811 }
2812 if (!strncmp (terminal_type, "c10", 3)
2813 || !strcmp (terminal_type, "perq"))
2814 {
2815 /* Supply a makeshift :wi string.
2816 This string is not valid in general since it works only
2817 for windows starting at the upper left corner;
2818 but that is all Emacs uses.
2819
2820 This string works only if the frame is using
2821 the top of the video memory, because addressing is memory-relative.
2822 So first check the :ti string to see if that is true.
2823
2824 It would be simpler if the :wi string could go in the termcap
2825 entry, but it can't because it is not fully valid.
2826 If it were in the termcap entry, it would confuse other programs. */
2827 if (!tty->TS_set_window)
2828 {
2829 p = tty->TS_termcap_modes;
2830 while (*p && strcmp (p, "\033v "))
2831 p++;
2832 if (*p)
2833 tty->TS_set_window = "\033v%C %C %C %C ";
2834 }
2835 /* Termcap entry often fails to have :in: flag */
2836 terminal->must_write_spaces = 1;
2837 /* :ti string typically fails to have \E^G! in it */
2838 /* This limits scope of insert-char to one line. */
2839 strcpy (area, tty->TS_termcap_modes);
2840 strcat (area, "\033\007!");
2841 tty->TS_termcap_modes = area;
2842 area += strlen (area) + 1;
2843 p = AbsPosition (tty);
2844 /* Change all %+ parameters to %C, to handle
2845 values above 96 correctly for the C100. */
2846 while (*p)
2847 {
2848 if (p[0] == '%' && p[1] == '+')
2849 p[1] = 'C';
2850 p++;
2851 }
2852 }
2853
2854 tty->specified_window = FrameRows (tty);
2855
2856 if (Wcm_init (tty) == -1) /* can't do cursor motion */
2857 {
2858 maybe_fatal (must_succeed, NULL, terminal,
2859 "Terminal type \"%s\" is not powerful enough to run Emacs",
2860 #ifdef VMS
2861 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2862 It lacks the ability to position the cursor.\n\
2863 If that is not the actual type of terminal you have, use either the\n\
2864 DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
2865 or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
2866 #else /* not VMS */
2867 # ifdef TERMINFO
2868 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2869 It lacks the ability to position the cursor.\n\
2870 If that is not the actual type of terminal you have,\n\
2871 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2872 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2873 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2874 # else /* TERMCAP */
2875 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2876 It lacks the ability to position the cursor.\n\
2877 If that is not the actual type of terminal you have,\n\
2878 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2879 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2880 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2881 # endif /* TERMINFO */
2882 #endif /*VMS */
2883 terminal_type);
2884 }
2885
2886 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
2887 maybe_fatal (must_succeed, NULL, terminal,
2888 "Could not determine the frame size",
2889 "Could not determine the frame size");
2890
2891 tty->delete_in_insert_mode
2892 = tty->TS_delete_mode && tty->TS_insert_mode
2893 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
2894
2895 tty->se_is_so = (tty->TS_standout_mode
2896 && tty->TS_end_standout_mode
2897 && !strcmp (tty->TS_standout_mode, tty->TS_end_standout_mode));
2898
2899 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
2900
2901 terminal->scroll_region_ok
2902 = (tty->Wcm->cm_abs
2903 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
2904
2905 terminal->line_ins_del_ok
2906 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
2907 && (tty->TS_del_line || tty->TS_del_multi_lines))
2908 || (terminal->scroll_region_ok
2909 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
2910
2911 terminal->char_ins_del_ok
2912 = ((tty->TS_ins_char || tty->TS_insert_mode
2913 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
2914 && (tty->TS_del_char || tty->TS_del_multi_chars));
2915
2916 terminal->fast_clear_end_of_line = tty->TS_clr_line != 0;
2917
2918 init_baud_rate (fileno (tty->input));
2919
2920 #ifdef AIXHFT
2921 /* The HFT system on AIX doesn't optimize for scrolling, so it's
2922 really ugly at times. */
2923 terminal->line_ins_del_ok = 0;
2924 terminal->char_ins_del_ok = 0;
2925 #endif
2926
2927 /* Don't do this. I think termcap may still need the buffer. */
2928 /* xfree (buffer); */
2929
2930 /* Init system terminal modes (RAW or CBREAK, etc.). */
2931 init_sys_modes (tty);
2932
2933 return terminal;
2934 #endif /* not WINDOWSNT */
2935 }
2936
2937 /* Auxiliary error-handling function for init_tty.
2938 Free BUFFER and delete TERMINAL, then call error or fatal
2939 with str1 or str2, respectively, according to MUST_SUCCEED. */
2940
2941 static void
2942 maybe_fatal (must_succeed, buffer, terminal, str1, str2, arg1, arg2)
2943 int must_succeed;
2944 char *buffer;
2945 struct terminal *terminal;
2946 char *str1, *str2, *arg1, *arg2;
2947 {
2948 if (buffer)
2949 xfree (buffer);
2950
2951 if (terminal)
2952 delete_tty (terminal);
2953
2954 if (must_succeed)
2955 fatal (str2, arg1, arg2);
2956 else
2957 error (str1, arg1, arg2);
2958
2959 abort ();
2960 }
2961
2962 /* VARARGS 1 */
2963 void
2964 fatal (str, arg1, arg2)
2965 char *str, *arg1, *arg2;
2966 {
2967 fprintf (stderr, "emacs: ");
2968 fprintf (stderr, str, arg1, arg2);
2969 fprintf (stderr, "\n");
2970 fflush (stderr);
2971 exit (1);
2972 }
2973
2974 \f
2975
2976 /* Delete the given tty terminal, closing all frames on it. */
2977
2978 static void
2979 delete_tty (struct terminal *terminal)
2980 {
2981 struct tty_display_info *tty;
2982 Lisp_Object tail, frame;
2983 int last_terminal;
2984
2985 /* Protect against recursive calls. Fdelete_frame in
2986 delete_terminal calls us back when it deletes our last frame. */
2987 if (terminal->deleted)
2988 return;
2989
2990 if (terminal->type != output_termcap)
2991 abort ();
2992
2993 tty = terminal->display_info.tty;
2994
2995 last_terminal = 1;
2996 FOR_EACH_FRAME (tail, frame)
2997 {
2998 struct frame *f = XFRAME (frame);
2999 if (FRAME_LIVE_P (f) && (!FRAME_TERMCAP_P (f) || FRAME_TTY (f) != tty))
3000 {
3001 last_terminal = 0;
3002 break;
3003 }
3004 }
3005 if (last_terminal)
3006 error ("Attempt to delete the sole terminal device with live frames");
3007
3008 if (tty == tty_list)
3009 tty_list = tty->next;
3010 else
3011 {
3012 struct tty_display_info *p;
3013 for (p = tty_list; p && p->next != tty; p = p->next)
3014 ;
3015
3016 if (! p)
3017 /* This should not happen. */
3018 abort ();
3019
3020 p->next = tty->next;
3021 tty->next = 0;
3022 }
3023
3024 /* reset_sys_modes needs a valid device, so this call needs to be
3025 before delete_terminal. */
3026 reset_sys_modes (tty);
3027
3028 delete_terminal (terminal);
3029
3030 if (tty->name)
3031 xfree (tty->name);
3032
3033 if (tty->type)
3034 xfree (tty->type);
3035
3036 if (tty->input)
3037 {
3038 delete_keyboard_wait_descriptor (fileno (tty->input));
3039 if (tty->input != stdin)
3040 fclose (tty->input);
3041 }
3042 if (tty->output && tty->output != stdout && tty->output != tty->input)
3043 fclose (tty->output);
3044 if (tty->termscript)
3045 fclose (tty->termscript);
3046
3047 if (tty->old_tty)
3048 xfree (tty->old_tty);
3049
3050 if (tty->Wcm)
3051 xfree (tty->Wcm);
3052
3053 bzero (tty, sizeof (struct tty_display_info));
3054 xfree (tty);
3055 }
3056
3057 \f
3058
3059 /* Mark the pointers in the tty_display_info objects.
3060 Called by the Fgarbage_collector. */
3061
3062 void
3063 mark_ttys (void)
3064 {
3065 struct tty_display_info *tty;
3066
3067 for (tty = tty_list; tty; tty = tty->next)
3068 {
3069 if (tty->top_frame)
3070 mark_object (tty->top_frame);
3071 }
3072 }
3073
3074 \f
3075
3076 void
3077 syms_of_term ()
3078 {
3079 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
3080 doc: /* Non-nil means the system uses terminfo rather than termcap.
3081 This variable can be used by terminal emulator packages. */);
3082 #ifdef TERMINFO
3083 system_uses_terminfo = 1;
3084 #else
3085 system_uses_terminfo = 0;
3086 #endif
3087
3088 DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
3089 doc: /* Functions to be run after suspending a tty.
3090 The functions are run with one argument, the terminal id to be suspended.
3091 See `suspend-tty'. */);
3092 Vsuspend_tty_functions = Qnil;
3093
3094
3095 DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
3096 doc: /* Functions to be run after resuming a tty.
3097 The functions are run with one argument, the terminal id that was revived.
3098 See `resume-tty'. */);
3099 Vresume_tty_functions = Qnil;
3100
3101 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
3102 doc: /* Non-nil means to make the cursor very visible.
3103 This only has an effect when running in a text terminal.
3104 What means \"very visible\" is up to your terminal. It may make the cursor
3105 bigger, or it may make it blink, or it may do nothing at all. */);
3106 visible_cursor = 1;
3107
3108 defsubr (&Stty_display_color_p);
3109 defsubr (&Stty_display_color_cells);
3110 defsubr (&Stty_no_underline);
3111 defsubr (&Stty_type);
3112 defsubr (&Scontrolling_tty_p);
3113 defsubr (&Ssuspend_tty);
3114 defsubr (&Sresume_tty);
3115 }
3116
3117
3118
3119 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
3120 (do not change this comment) */