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