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