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