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