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