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