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