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