1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985-1987, 1993-1995, 1998, 2000-2012
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
32 #include "character.h"
36 #include "composite.h"
40 #include "termhooks.h"
41 #include "dispextern.h"
44 #include "blockinput.h"
45 #include "syssignal.h"
47 #include "intervals.h"
50 static int been_here
= -1;
66 /* The name of the default console device. */
68 #define DEV_TTY "CONOUT$"
70 #define DEV_TTY "/dev/tty"
73 static void tty_set_scroll_region (struct frame
*f
, int start
, int stop
);
74 static void turn_on_face (struct frame
*, int face_id
);
75 static void turn_off_face (struct frame
*, int face_id
);
76 static void tty_turn_off_highlight (struct tty_display_info
*);
77 static void tty_show_cursor (struct tty_display_info
*);
78 static void tty_hide_cursor (struct tty_display_info
*);
79 static void tty_background_highlight (struct tty_display_info
*tty
);
80 static struct terminal
*get_tty_terminal (Lisp_Object
, int);
81 static void clear_tty_hooks (struct terminal
*terminal
);
82 static void set_tty_hooks (struct terminal
*terminal
);
83 static void dissociate_if_controlling_tty (int fd
);
84 static void delete_tty (struct terminal
*);
85 static _Noreturn
void maybe_fatal (int must_succeed
, struct terminal
*terminal
,
86 const char *str1
, const char *str2
, ...)
87 ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
88 static _Noreturn
void vfatal (const char *str
, va_list ap
)
89 ATTRIBUTE_FORMAT_PRINTF (1, 0);
92 #define OUTPUT(tty, a) \
93 emacs_tputs ((tty), a, \
94 (int) (FRAME_LINES (XFRAME (selected_frame)) \
98 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
99 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
101 #define OUTPUT_IF(tty, a) \
107 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
109 /* Display space properties */
111 /* Chain of all tty device parameters. */
112 struct tty_display_info
*tty_list
;
114 /* Meaning of bits in no_color_video. Each bit set means that the
115 corresponding attribute cannot be combined with colors. */
119 NC_STANDOUT
= 1 << 0,
120 NC_UNDERLINE
= 1 << 1,
131 /* The largest frame width in any call to calculate_costs. */
133 static int max_frame_cols
;
135 /* Non-zero if we have dropped our controlling tty and therefore
136 should not open a frame on stdout. */
137 static int no_controlling_tty
;
142 #include <sys/fcntl.h>
144 /* The device for which we have enabled gpm support (or NULL). */
145 struct tty_display_info
*gpm_tty
= NULL
;
147 /* Last recorded mouse coordinates. */
148 static int last_mouse_x
, last_mouse_y
;
149 #endif /* HAVE_GPM */
151 /* Ring the bell on a tty. */
154 tty_ring_bell (struct frame
*f
)
156 struct tty_display_info
*tty
= FRAME_TTY (f
);
160 OUTPUT (tty
, (tty
->TS_visible_bell
&& visible_bell
161 ? tty
->TS_visible_bell
163 fflush (tty
->output
);
167 /* Set up termcap modes for Emacs. */
170 tty_set_terminal_modes (struct terminal
*terminal
)
172 struct tty_display_info
*tty
= terminal
->display_info
.tty
;
176 if (tty
->TS_termcap_modes
)
177 OUTPUT (tty
, tty
->TS_termcap_modes
);
180 /* Output enough newlines to scroll all the old screen contents
181 off the screen, so it won't be overwritten and lost. */
184 for (i
= 0; i
< FRAME_LINES (XFRAME (selected_frame
)); i
++)
188 OUTPUT_IF (tty
, visible_cursor
? tty
->TS_cursor_visible
: tty
->TS_cursor_normal
);
189 OUTPUT_IF (tty
, tty
->TS_keypad_mode
);
191 fflush (tty
->output
);
195 /* Reset termcap modes before exiting Emacs. */
198 tty_reset_terminal_modes (struct terminal
*terminal
)
200 struct tty_display_info
*tty
= terminal
->display_info
.tty
;
204 tty_turn_off_highlight (tty
);
205 tty_turn_off_insert (tty
);
206 OUTPUT_IF (tty
, tty
->TS_end_keypad_mode
);
207 OUTPUT_IF (tty
, tty
->TS_cursor_normal
);
208 OUTPUT_IF (tty
, tty
->TS_end_termcap_modes
);
209 OUTPUT_IF (tty
, tty
->TS_orig_pair
);
210 /* Output raw CR so kernel can track the cursor hpos. */
213 fflush (tty
->output
);
217 /* Flag the end of a display update on a termcap terminal. */
220 tty_update_end (struct frame
*f
)
222 struct tty_display_info
*tty
= FRAME_TTY (f
);
224 if (!XWINDOW (selected_window
)->cursor_off_p
)
225 tty_show_cursor (tty
);
226 tty_turn_off_insert (tty
);
227 tty_background_highlight (tty
);
230 /* The implementation of set_terminal_window for termcap frames. */
233 tty_set_terminal_window (struct frame
*f
, int size
)
235 struct tty_display_info
*tty
= FRAME_TTY (f
);
237 tty
->specified_window
= size
? size
: FRAME_LINES (f
);
238 if (FRAME_SCROLL_REGION_OK (f
))
239 tty_set_scroll_region (f
, 0, tty
->specified_window
);
243 tty_set_scroll_region (struct frame
*f
, int start
, int stop
)
246 struct tty_display_info
*tty
= FRAME_TTY (f
);
248 if (tty
->TS_set_scroll_region
)
249 buf
= tparam (tty
->TS_set_scroll_region
, 0, 0, start
, stop
- 1, 0, 0);
250 else if (tty
->TS_set_scroll_region_1
)
251 buf
= tparam (tty
->TS_set_scroll_region_1
, 0, 0,
252 FRAME_LINES (f
), start
,
253 FRAME_LINES (f
) - stop
,
256 buf
= tparam (tty
->TS_set_window
, 0, 0, start
, 0, stop
, FRAME_COLS (f
));
265 tty_turn_on_insert (struct tty_display_info
*tty
)
267 if (!tty
->insert_mode
)
268 OUTPUT (tty
, tty
->TS_insert_mode
);
269 tty
->insert_mode
= 1;
273 tty_turn_off_insert (struct tty_display_info
*tty
)
275 if (tty
->insert_mode
)
276 OUTPUT (tty
, tty
->TS_end_insert_mode
);
277 tty
->insert_mode
= 0;
280 /* Handle highlighting. */
283 tty_turn_off_highlight (struct tty_display_info
*tty
)
285 if (tty
->standout_mode
)
286 OUTPUT_IF (tty
, tty
->TS_end_standout_mode
);
287 tty
->standout_mode
= 0;
291 tty_turn_on_highlight (struct tty_display_info
*tty
)
293 if (!tty
->standout_mode
)
294 OUTPUT_IF (tty
, tty
->TS_standout_mode
);
295 tty
->standout_mode
= 1;
299 tty_toggle_highlight (struct tty_display_info
*tty
)
301 if (tty
->standout_mode
)
302 tty_turn_off_highlight (tty
);
304 tty_turn_on_highlight (tty
);
308 /* Make cursor invisible. */
311 tty_hide_cursor (struct tty_display_info
*tty
)
313 if (tty
->cursor_hidden
== 0)
315 tty
->cursor_hidden
= 1;
316 OUTPUT_IF (tty
, tty
->TS_cursor_invisible
);
321 /* Ensure that cursor is visible. */
324 tty_show_cursor (struct tty_display_info
*tty
)
326 if (tty
->cursor_hidden
)
328 tty
->cursor_hidden
= 0;
329 OUTPUT_IF (tty
, tty
->TS_cursor_normal
);
331 OUTPUT_IF (tty
, tty
->TS_cursor_visible
);
336 /* Set standout mode to the state it should be in for
337 empty space inside windows. What this is,
338 depends on the user option inverse-video. */
341 tty_background_highlight (struct tty_display_info
*tty
)
344 tty_turn_on_highlight (tty
);
346 tty_turn_off_highlight (tty
);
349 /* Set standout mode to the mode specified for the text to be output. */
352 tty_highlight_if_desired (struct tty_display_info
*tty
)
355 tty_turn_on_highlight (tty
);
357 tty_turn_off_highlight (tty
);
361 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
362 frame-relative coordinates. */
365 tty_cursor_to (struct frame
*f
, int vpos
, int hpos
)
367 struct tty_display_info
*tty
= FRAME_TTY (f
);
369 /* Detect the case where we are called from reset_sys_modes
370 and the costs have never been calculated. Do nothing. */
371 if (! tty
->costs_set
)
374 if (curY (tty
) == vpos
375 && curX (tty
) == hpos
)
377 if (!tty
->TF_standout_motion
)
378 tty_background_highlight (tty
);
379 if (!tty
->TF_insmode_motion
)
380 tty_turn_off_insert (tty
);
381 cmgoto (tty
, vpos
, hpos
);
384 /* Similar but don't take any account of the wasted characters. */
387 tty_raw_cursor_to (struct frame
*f
, int row
, int col
)
389 struct tty_display_info
*tty
= FRAME_TTY (f
);
391 if (curY (tty
) == row
392 && curX (tty
) == col
)
394 if (!tty
->TF_standout_motion
)
395 tty_background_highlight (tty
);
396 if (!tty
->TF_insmode_motion
)
397 tty_turn_off_insert (tty
);
398 cmgoto (tty
, row
, col
);
401 /* Erase operations */
403 /* Clear from cursor to end of frame on a termcap device. */
406 tty_clear_to_end (struct frame
*f
)
409 struct tty_display_info
*tty
= FRAME_TTY (f
);
411 if (tty
->TS_clr_to_bottom
)
413 tty_background_highlight (tty
);
414 OUTPUT (tty
, tty
->TS_clr_to_bottom
);
418 for (i
= curY (tty
); i
< FRAME_LINES (f
); i
++)
421 clear_end_of_line (f
, FRAME_COLS (f
));
426 /* Clear an entire termcap frame. */
429 tty_clear_frame (struct frame
*f
)
431 struct tty_display_info
*tty
= FRAME_TTY (f
);
433 if (tty
->TS_clr_frame
)
435 tty_background_highlight (tty
);
436 OUTPUT (tty
, tty
->TS_clr_frame
);
446 /* An implementation of clear_end_of_line for termcap frames.
448 Note that the cursor may be moved, on terminals lacking a `ce' string. */
451 tty_clear_end_of_line (struct frame
*f
, int first_unused_hpos
)
454 struct tty_display_info
*tty
= FRAME_TTY (f
);
456 /* Detect the case where we are called from reset_sys_modes
457 and the costs have never been calculated. Do nothing. */
458 if (! tty
->costs_set
)
461 if (curX (tty
) >= first_unused_hpos
)
463 tty_background_highlight (tty
);
464 if (tty
->TS_clr_line
)
466 OUTPUT1 (tty
, tty
->TS_clr_line
);
469 { /* have to do it the hard way */
470 tty_turn_off_insert (tty
);
472 /* Do not write in last row last col with Auto-wrap on. */
474 && curY (tty
) == FrameRows (tty
) - 1
475 && first_unused_hpos
== FrameCols (tty
))
478 for (i
= curX (tty
); i
< first_unused_hpos
; i
++)
481 fputc (' ', tty
->termscript
);
482 fputc (' ', tty
->output
);
484 cmplus (tty
, first_unused_hpos
- curX (tty
));
488 /* Buffers to store the source and result of code conversion for terminal. */
489 static unsigned char *encode_terminal_src
;
490 static unsigned char *encode_terminal_dst
;
491 /* Allocated sizes of the above buffers. */
492 static ptrdiff_t encode_terminal_src_size
;
493 static ptrdiff_t encode_terminal_dst_size
;
495 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
496 Set CODING->produced to the byte-length of the resulting byte
497 sequence, and return a pointer to that byte sequence. */
500 encode_terminal_code (struct glyph
*src
, int src_len
, struct coding_system
*coding
)
502 struct glyph
*src_end
= src
+ src_len
;
504 ptrdiff_t nchars
, nbytes
, required
;
505 ptrdiff_t tlen
= GLYPH_TABLE_LENGTH
;
506 register Lisp_Object
*tbase
= GLYPH_TABLE_BASE
;
507 Lisp_Object charset_list
;
509 /* Allocate sufficient size of buffer to store all characters in
510 multibyte-form. But, it may be enlarged on demand if
511 Vglyph_table contains a string or a composite glyph is
513 if (min (PTRDIFF_MAX
, SIZE_MAX
) / MAX_MULTIBYTE_LENGTH
< src_len
)
514 memory_full (SIZE_MAX
);
516 required
*= MAX_MULTIBYTE_LENGTH
;
517 if (encode_terminal_src_size
< required
)
519 encode_terminal_src
= xrealloc (encode_terminal_src
, required
);
520 encode_terminal_src_size
= required
;
523 charset_list
= coding_charset_list (coding
);
525 buf
= encode_terminal_src
;
527 while (src
< src_end
)
529 if (src
->type
== COMPOSITE_GLYPH
)
531 struct composition
*cmp
IF_LINT (= NULL
);
532 Lisp_Object gstring
IF_LINT (= Qnil
);
535 nbytes
= buf
- encode_terminal_src
;
536 if (src
->u
.cmp
.automatic
)
538 gstring
= composition_gstring_from_id (src
->u
.cmp
.id
);
539 required
= src
->slice
.cmp
.to
- src
->slice
.cmp
.from
+ 1;
543 cmp
= composition_table
[src
->u
.cmp
.id
];
544 required
= cmp
->glyph_len
;
545 required
*= MAX_MULTIBYTE_LENGTH
;
548 if (encode_terminal_src_size
- nbytes
< required
)
550 encode_terminal_src
=
551 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
552 required
- (encode_terminal_src_size
- nbytes
),
554 buf
= encode_terminal_src
+ nbytes
;
557 if (src
->u
.cmp
.automatic
)
558 for (i
= src
->slice
.cmp
.from
; i
<= src
->slice
.cmp
.to
; i
++)
560 Lisp_Object g
= LGSTRING_GLYPH (gstring
, i
);
561 int c
= LGLYPH_CHAR (g
);
563 if (! char_charset (c
, charset_list
, NULL
))
565 buf
+= CHAR_STRING (c
, buf
);
569 for (i
= 0; i
< cmp
->glyph_len
; i
++)
571 int c
= COMPOSITION_GLYPH (cmp
, i
);
573 /* TAB in a composition means display glyphs with
574 padding space on the left or right. */
577 if (char_charset (c
, charset_list
, NULL
))
579 if (CHAR_WIDTH (c
) == 0
580 && i
> 0 && COMPOSITION_GLYPH (cmp
, i
- 1) == '\t')
581 /* Should be left-padded */
583 buf
+= CHAR_STRING (' ', buf
);
589 buf
+= CHAR_STRING (c
, buf
);
593 /* We must skip glyphs to be padded for a wide character. */
594 else if (! CHAR_GLYPH_PADDING_P (*src
))
601 SET_GLYPH_FROM_CHAR_GLYPH (g
, src
[0]);
603 if (GLYPH_INVALID_P (g
) || GLYPH_SIMPLE_P (tbase
, tlen
, g
))
605 /* This glyph doesn't have an entry in Vglyph_table. */
610 /* This glyph has an entry in Vglyph_table,
611 so process any alias before testing for simpleness. */
612 GLYPH_FOLLOW_ALIASES (tbase
, tlen
, g
);
614 if (GLYPH_SIMPLE_P (tbase
, tlen
, g
))
615 /* We set the multi-byte form of a character in G
616 (that should be an ASCII character) at WORKBUF. */
619 /* We have a string in Vglyph_table. */
620 string
= tbase
[GLYPH_CHAR (g
)];
625 nbytes
= buf
- encode_terminal_src
;
626 if (encode_terminal_src_size
- nbytes
< MAX_MULTIBYTE_LENGTH
)
628 encode_terminal_src
=
629 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
630 MAX_MULTIBYTE_LENGTH
, -1, 1);
631 buf
= encode_terminal_src
+ nbytes
;
634 || char_charset (c
, charset_list
, NULL
))
636 /* Store the multibyte form of C at BUF. */
637 buf
+= CHAR_STRING (c
, buf
);
642 /* C is not encodable. */
645 while (src
+ 1 < src_end
&& CHAR_GLYPH_PADDING_P (src
[1]))
655 if (! STRING_MULTIBYTE (string
))
656 string
= string_to_multibyte (string
);
657 nbytes
= buf
- encode_terminal_src
;
658 if (encode_terminal_src_size
- nbytes
< SBYTES (string
))
660 encode_terminal_src
=
661 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
663 - (encode_terminal_src_size
- nbytes
)),
665 buf
= encode_terminal_src
+ nbytes
;
667 memcpy (buf
, SDATA (string
), SBYTES (string
));
668 buf
+= SBYTES (string
);
669 nchars
+= SCHARS (string
);
677 coding
->produced
= 0;
681 nbytes
= buf
- encode_terminal_src
;
682 coding
->source
= encode_terminal_src
;
683 if (encode_terminal_dst_size
== 0)
685 encode_terminal_dst
= xrealloc (encode_terminal_dst
,
686 encode_terminal_src_size
);
687 encode_terminal_dst_size
= encode_terminal_src_size
;
689 coding
->destination
= encode_terminal_dst
;
690 coding
->dst_bytes
= encode_terminal_dst_size
;
691 encode_coding_object (coding
, Qnil
, 0, 0, nchars
, nbytes
, Qnil
);
692 /* coding->destination may have been reallocated. */
693 encode_terminal_dst
= coding
->destination
;
694 encode_terminal_dst_size
= coding
->dst_bytes
;
696 return (encode_terminal_dst
);
701 /* An implementation of write_glyphs for termcap frames. */
704 tty_write_glyphs (struct frame
*f
, struct glyph
*string
, int len
)
706 unsigned char *conversion_buffer
;
707 struct coding_system
*coding
;
710 struct tty_display_info
*tty
= FRAME_TTY (f
);
712 tty_turn_off_insert (tty
);
713 tty_hide_cursor (tty
);
715 /* Don't dare write in last column of bottom line, if Auto-Wrap,
716 since that would scroll the whole frame on some terminals. */
719 && curY (tty
) + 1 == FRAME_LINES (f
)
720 && (curX (tty
) + len
) == FRAME_COLS (f
))
727 /* If terminal_coding does any conversion, use it, otherwise use
728 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
729 because it always return 1 if the member src_multibyte is 1. */
730 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
731 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
732 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
734 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
736 for (stringlen
= len
; stringlen
!= 0; stringlen
-= n
)
738 /* Identify a run of glyphs with the same face. */
739 int face_id
= string
->face_id
;
741 for (n
= 1; n
< stringlen
; ++n
)
742 if (string
[n
].face_id
!= face_id
)
745 /* Turn appearance modes of the face of the run on. */
746 tty_highlight_if_desired (tty
);
747 turn_on_face (f
, face_id
);
750 /* This is the last run. */
751 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
752 conversion_buffer
= encode_terminal_code (string
, n
, coding
);
753 if (coding
->produced
> 0)
756 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
757 if (ferror (tty
->output
))
758 clearerr (tty
->output
);
760 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
765 /* Turn appearance modes off. */
766 turn_off_face (f
, face_id
);
767 tty_turn_off_highlight (tty
);
773 #ifdef HAVE_GPM /* Only used by GPM code. */
776 tty_write_glyphs_with_face (register struct frame
*f
, register struct glyph
*string
,
777 register int len
, register int face_id
)
779 unsigned char *conversion_buffer
;
780 struct coding_system
*coding
;
782 struct tty_display_info
*tty
= FRAME_TTY (f
);
784 tty_turn_off_insert (tty
);
785 tty_hide_cursor (tty
);
787 /* Don't dare write in last column of bottom line, if Auto-Wrap,
788 since that would scroll the whole frame on some terminals. */
791 && curY (tty
) + 1 == FRAME_LINES (f
)
792 && (curX (tty
) + len
) == FRAME_COLS (f
))
799 /* If terminal_coding does any conversion, use it, otherwise use
800 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
801 because it always return 1 if the member src_multibyte is 1. */
802 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
803 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
804 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
806 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
808 /* Turn appearance modes of the face. */
809 tty_highlight_if_desired (tty
);
810 turn_on_face (f
, face_id
);
812 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
813 conversion_buffer
= encode_terminal_code (string
, len
, coding
);
814 if (coding
->produced
> 0)
817 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
818 if (ferror (tty
->output
))
819 clearerr (tty
->output
);
821 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
825 /* Turn appearance modes off. */
826 turn_off_face (f
, face_id
);
827 tty_turn_off_highlight (tty
);
833 /* An implementation of insert_glyphs for termcap frames. */
836 tty_insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
)
839 struct glyph
*glyph
= NULL
;
840 unsigned char *conversion_buffer
;
841 unsigned char space
[1];
842 struct coding_system
*coding
;
844 struct tty_display_info
*tty
= FRAME_TTY (f
);
846 if (tty
->TS_ins_multi_chars
)
848 buf
= tparam (tty
->TS_ins_multi_chars
, 0, 0, len
, 0, 0, 0);
852 write_glyphs (f
, start
, len
);
856 tty_turn_on_insert (tty
);
860 space
[0] = SPACEGLYPH
;
862 /* If terminal_coding does any conversion, use it, otherwise use
863 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
864 because it always return 1 if the member src_multibyte is 1. */
865 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
866 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
867 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
869 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
873 OUTPUT1_IF (tty
, tty
->TS_ins_char
);
876 conversion_buffer
= space
;
877 coding
->produced
= 1;
881 tty_highlight_if_desired (tty
);
882 turn_on_face (f
, start
->face_id
);
885 /* We must open sufficient space for a character which
886 occupies more than one column. */
887 while (len
&& CHAR_GLYPH_PADDING_P (*start
))
889 OUTPUT1_IF (tty
, tty
->TS_ins_char
);
894 /* This is the last glyph. */
895 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
897 conversion_buffer
= encode_terminal_code (glyph
, 1, coding
);
900 if (coding
->produced
> 0)
903 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
904 if (ferror (tty
->output
))
905 clearerr (tty
->output
);
907 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
911 OUTPUT1_IF (tty
, tty
->TS_pad_inserted_char
);
914 turn_off_face (f
, glyph
->face_id
);
915 tty_turn_off_highlight (tty
);
922 /* An implementation of delete_glyphs for termcap frames. */
925 tty_delete_glyphs (struct frame
*f
, int n
)
930 struct tty_display_info
*tty
= FRAME_TTY (f
);
932 if (tty
->delete_in_insert_mode
)
934 tty_turn_on_insert (tty
);
938 tty_turn_off_insert (tty
);
939 OUTPUT_IF (tty
, tty
->TS_delete_mode
);
942 if (tty
->TS_del_multi_chars
)
944 buf
= tparam (tty
->TS_del_multi_chars
, 0, 0, n
, 0, 0, 0);
949 for (i
= 0; i
< n
; i
++)
950 OUTPUT1 (tty
, tty
->TS_del_char
);
951 if (!tty
->delete_in_insert_mode
)
952 OUTPUT_IF (tty
, tty
->TS_end_delete_mode
);
955 /* An implementation of ins_del_lines for termcap frames. */
958 tty_ins_del_lines (struct frame
*f
, int vpos
, int n
)
960 struct tty_display_info
*tty
= FRAME_TTY (f
);
962 n
> 0 ? tty
->TS_ins_multi_lines
: tty
->TS_del_multi_lines
;
963 const char *single
= n
> 0 ? tty
->TS_ins_line
: tty
->TS_del_line
;
964 const char *scroll
= n
> 0 ? tty
->TS_rev_scroll
: tty
->TS_fwd_scroll
;
966 register int i
= n
> 0 ? n
: -n
;
969 /* If the lines below the insertion are being pushed
970 into the end of the window, this is the same as clearing;
971 and we know the lines are already clear, since the matching
972 deletion has already been done. So can ignore this. */
973 /* If the lines below the deletion are blank lines coming
974 out of the end of the window, don't bother,
975 as there will be a matching inslines later that will flush them. */
976 if (FRAME_SCROLL_REGION_OK (f
)
977 && vpos
+ i
>= tty
->specified_window
)
979 if (!FRAME_MEMORY_BELOW_FRAME (f
)
980 && vpos
+ i
>= FRAME_LINES (f
))
985 raw_cursor_to (f
, vpos
, 0);
986 tty_background_highlight (tty
);
987 buf
= tparam (multi
, 0, 0, i
, 0, 0, 0);
993 raw_cursor_to (f
, vpos
, 0);
994 tty_background_highlight (tty
);
996 OUTPUT (tty
, single
);
1002 tty_set_scroll_region (f
, vpos
, tty
->specified_window
);
1004 raw_cursor_to (f
, tty
->specified_window
- 1, 0);
1006 raw_cursor_to (f
, vpos
, 0);
1007 tty_background_highlight (tty
);
1009 OUTPUTL (tty
, scroll
, tty
->specified_window
- vpos
);
1010 tty_set_scroll_region (f
, 0, tty
->specified_window
);
1013 if (!FRAME_SCROLL_REGION_OK (f
)
1014 && FRAME_MEMORY_BELOW_FRAME (f
)
1017 cursor_to (f
, FRAME_LINES (f
) + n
, 0);
1022 /* Compute cost of sending "str", in characters,
1023 not counting any line-dependent padding. */
1026 string_cost (const char *str
)
1030 tputs (str
, 0, evalcost
);
1034 /* Compute cost of sending "str", in characters,
1035 counting any line-dependent padding at one line. */
1038 string_cost_one_line (const char *str
)
1042 tputs (str
, 1, evalcost
);
1046 /* Compute per line amount of line-dependent padding,
1047 in tenths of characters. */
1050 per_line_cost (const char *str
)
1054 tputs (str
, 0, evalcost
);
1057 tputs (str
, 10, evalcost
);
1061 /* char_ins_del_cost[n] is cost of inserting N characters.
1062 char_ins_del_cost[-n] is cost of deleting N characters.
1063 The length of this vector is based on max_frame_cols. */
1065 int *char_ins_del_vector
;
1067 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1071 calculate_ins_del_char_costs (struct frame
*f
)
1073 struct tty_display_info
*tty
= FRAME_TTY (f
);
1074 int ins_startup_cost
, del_startup_cost
;
1075 int ins_cost_per_char
, del_cost_per_char
;
1079 if (tty
->TS_ins_multi_chars
)
1081 ins_cost_per_char
= 0;
1082 ins_startup_cost
= string_cost_one_line (tty
->TS_ins_multi_chars
);
1084 else if (tty
->TS_ins_char
|| tty
->TS_pad_inserted_char
1085 || (tty
->TS_insert_mode
&& tty
->TS_end_insert_mode
))
1087 ins_startup_cost
= (30 * (string_cost (tty
->TS_insert_mode
)
1088 + string_cost (tty
->TS_end_insert_mode
))) / 100;
1089 ins_cost_per_char
= (string_cost_one_line (tty
->TS_ins_char
)
1090 + string_cost_one_line (tty
->TS_pad_inserted_char
));
1094 ins_startup_cost
= 9999;
1095 ins_cost_per_char
= 0;
1098 if (tty
->TS_del_multi_chars
)
1100 del_cost_per_char
= 0;
1101 del_startup_cost
= string_cost_one_line (tty
->TS_del_multi_chars
);
1103 else if (tty
->TS_del_char
)
1105 del_startup_cost
= (string_cost (tty
->TS_delete_mode
)
1106 + string_cost (tty
->TS_end_delete_mode
));
1107 if (tty
->delete_in_insert_mode
)
1108 del_startup_cost
/= 2;
1109 del_cost_per_char
= string_cost_one_line (tty
->TS_del_char
);
1113 del_startup_cost
= 9999;
1114 del_cost_per_char
= 0;
1117 /* Delete costs are at negative offsets */
1118 p
= &char_ins_del_cost (f
)[0];
1119 for (i
= FRAME_COLS (f
); --i
>= 0;)
1120 *--p
= (del_startup_cost
+= del_cost_per_char
);
1122 /* Doing nothing is free */
1123 p
= &char_ins_del_cost (f
)[0];
1126 /* Insert costs are at positive offsets */
1127 for (i
= FRAME_COLS (f
); --i
>= 0;)
1128 *p
++ = (ins_startup_cost
+= ins_cost_per_char
);
1132 calculate_costs (struct frame
*frame
)
1134 FRAME_COST_BAUD_RATE (frame
) = baud_rate
;
1136 if (FRAME_TERMCAP_P (frame
))
1138 struct tty_display_info
*tty
= FRAME_TTY (frame
);
1139 register const char *f
= (tty
->TS_set_scroll_region
1140 ? tty
->TS_set_scroll_region
1141 : tty
->TS_set_scroll_region_1
);
1143 FRAME_SCROLL_REGION_COST (frame
) = string_cost (f
);
1147 /* These variables are only used for terminal stuff. They are
1148 allocated once for the terminal frame of X-windows emacs, but not
1151 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1152 X turns off char_ins_del_ok. */
1154 max_frame_cols
= max (max_frame_cols
, FRAME_COLS (frame
));
1155 if ((min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (int) - 1) / 2
1157 memory_full (SIZE_MAX
);
1159 char_ins_del_vector
=
1160 xrealloc (char_ins_del_vector
,
1161 (sizeof (int) + 2 * sizeof (int) * max_frame_cols
));
1163 memset (char_ins_del_vector
, 0,
1164 (sizeof (int) + 2 * sizeof (int) * max_frame_cols
));
1167 if (f
&& (!tty
->TS_ins_line
&& !tty
->TS_del_line
))
1168 do_line_insertion_deletion_costs (frame
,
1169 tty
->TS_rev_scroll
, tty
->TS_ins_multi_lines
,
1170 tty
->TS_fwd_scroll
, tty
->TS_del_multi_lines
,
1173 do_line_insertion_deletion_costs (frame
,
1174 tty
->TS_ins_line
, tty
->TS_ins_multi_lines
,
1175 tty
->TS_del_line
, tty
->TS_del_multi_lines
,
1178 calculate_ins_del_char_costs (frame
);
1180 /* Don't use TS_repeat if its padding is worse than sending the chars */
1181 if (tty
->TS_repeat
&& per_line_cost (tty
->TS_repeat
) * baud_rate
< 9000)
1182 tty
->RPov
= string_cost (tty
->TS_repeat
);
1184 tty
->RPov
= FRAME_COLS (frame
) * 2;
1186 cmcostinit (FRAME_TTY (frame
)); /* set up cursor motion costs */
1191 const char *cap
, *name
;
1194 /* Termcap capability names that correspond directly to X keysyms.
1195 Some of these (marked "terminfo") aren't supplied by old-style
1196 (Berkeley) termcap entries. They're listed in X keysym order;
1197 except we put the keypad keys first, so that if they clash with
1198 other keys (as on the IBM PC keyboard) they get overridden.
1201 static const struct fkey_table keys
[] =
1203 {"kh", "home"}, /* termcap */
1204 {"kl", "left"}, /* termcap */
1205 {"ku", "up"}, /* termcap */
1206 {"kr", "right"}, /* termcap */
1207 {"kd", "down"}, /* termcap */
1208 {"%8", "prior"}, /* terminfo */
1209 {"%5", "next"}, /* terminfo */
1210 {"@7", "end"}, /* terminfo */
1211 {"@1", "begin"}, /* terminfo */
1212 {"*6", "select"}, /* terminfo */
1213 {"%9", "print"}, /* terminfo */
1214 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1216 * "insert" --- see below
1218 {"&8", "undo"}, /* terminfo */
1219 {"%0", "redo"}, /* terminfo */
1220 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1221 {"@0", "find"}, /* terminfo */
1222 {"@2", "cancel"}, /* terminfo */
1223 {"%1", "help"}, /* terminfo */
1225 * "break" goes here, but can't be reliably intercepted with termcap
1227 {"&4", "reset"}, /* terminfo --- actually `restart' */
1229 * "system" and "user" --- no termcaps
1231 {"kE", "clearline"}, /* terminfo */
1232 {"kA", "insertline"}, /* terminfo */
1233 {"kL", "deleteline"}, /* terminfo */
1234 {"kI", "insertchar"}, /* terminfo */
1235 {"kD", "deletechar"}, /* terminfo */
1236 {"kB", "backtab"}, /* terminfo */
1238 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1240 {"@8", "kp-enter"}, /* terminfo */
1242 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1243 * "kp-multiply", "kp-add", "kp-separator",
1244 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1245 * --- no termcaps for any of these.
1247 {"K4", "kp-1"}, /* terminfo */
1249 * "kp-2" --- no termcap
1251 {"K5", "kp-3"}, /* terminfo */
1253 * "kp-4" --- no termcap
1255 {"K2", "kp-5"}, /* terminfo */
1257 * "kp-6" --- no termcap
1259 {"K1", "kp-7"}, /* terminfo */
1261 * "kp-8" --- no termcap
1263 {"K3", "kp-9"}, /* terminfo */
1265 * "kp-equal" --- no termcap
1277 {"&0", "S-cancel"}, /*shifted cancel key*/
1278 {"&9", "S-begin"}, /*shifted begin key*/
1279 {"*0", "S-find"}, /*shifted find key*/
1280 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1281 {"*4", "S-delete"}, /*shifted delete-character key*/
1282 {"*7", "S-end"}, /*shifted end key*/
1283 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1284 {"#1", "S-help"}, /*shifted help key*/
1285 {"#2", "S-home"}, /*shifted home key*/
1286 {"#3", "S-insert"}, /*shifted insert-character key*/
1287 {"#4", "S-left"}, /*shifted left-arrow key*/
1288 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1289 {"%c", "S-next"}, /*shifted next key*/
1290 {"%e", "S-prior"}, /*shifted previous key*/
1291 {"%f", "S-print"}, /*shifted print key*/
1292 {"%g", "S-redo"}, /*shifted redo key*/
1293 {"%i", "S-right"}, /*shifted right-arrow key*/
1294 {"!3", "S-undo"} /*shifted undo key*/
1298 static char **term_get_fkeys_address
;
1299 static KBOARD
*term_get_fkeys_kboard
;
1300 static Lisp_Object
term_get_fkeys_1 (void);
1302 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1303 This function scans the termcap function key sequence entries, and
1304 adds entries to Vinput_decode_map for each function key it finds. */
1307 term_get_fkeys (char **address
, KBOARD
*kboard
)
1309 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1310 errors during the call. The only errors should be from Fdefine_key
1311 when given a key sequence containing an invalid prefix key. If the
1312 termcap defines function keys which use a prefix that is already bound
1313 to a command by the default bindings, we should silently ignore that
1314 function key specification, rather than giving the user an error and
1315 refusing to run at all on such a terminal. */
1317 term_get_fkeys_address
= address
;
1318 term_get_fkeys_kboard
= kboard
;
1319 internal_condition_case (term_get_fkeys_1
, Qerror
, Fidentity
);
1323 term_get_fkeys_1 (void)
1327 char **address
= term_get_fkeys_address
;
1328 KBOARD
*kboard
= term_get_fkeys_kboard
;
1330 /* This can happen if CANNOT_DUMP or with strange options. */
1331 if (!KEYMAPP (KVAR (kboard
, Vinput_decode_map
)))
1332 kset_input_decode_map (kboard
, Fmake_sparse_keymap (Qnil
));
1334 for (i
= 0; i
< (sizeof (keys
)/sizeof (keys
[0])); i
++)
1336 char *sequence
= tgetstr (keys
[i
].cap
, address
);
1338 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (sequence
),
1339 Fmake_vector (make_number (1),
1340 intern (keys
[i
].name
)));
1343 /* The uses of the "k0" capability are inconsistent; sometimes it
1344 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1345 We will attempt to politely accommodate both systems by testing for
1346 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1349 const char *k_semi
= tgetstr ("k;", address
);
1350 const char *k0
= tgetstr ("k0", address
);
1351 const char *k0_name
= "f10";
1356 /* Define f0 first, so that f10 takes precedence in case the
1357 key sequences happens to be the same. */
1358 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k0
),
1359 Fmake_vector (make_number (1), intern ("f0")));
1360 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k_semi
),
1361 Fmake_vector (make_number (1), intern ("f10")));
1364 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k0
),
1365 Fmake_vector (make_number (1), intern (k0_name
)));
1368 /* Set up cookies for numbered function keys above f10. */
1370 char fcap
[3], fkey
[4];
1372 fcap
[0] = 'F'; fcap
[2] = '\0';
1373 for (i
= 11; i
< 64; i
++)
1376 fcap
[1] = '1' + i
- 11;
1378 fcap
[1] = 'A' + i
- 20;
1380 fcap
[1] = 'a' + i
- 46;
1383 char *sequence
= tgetstr (fcap
, address
);
1386 sprintf (fkey
, "f%d", i
);
1387 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (sequence
),
1388 Fmake_vector (make_number (1),
1396 * Various mappings to try and get a better fit.
1399 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1400 if (!tgetstr (cap1, address)) \
1402 char *sequence = tgetstr (cap2, address); \
1404 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1405 Fmake_vector (make_number (1), \
1409 /* if there's no key_next keycap, map key_npage to `next' keysym */
1410 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1411 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1412 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1413 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1414 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1415 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1416 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1418 /* IBM has their own non-standard dialect of terminfo.
1419 If the standard name isn't found, try the IBM name. */
1420 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1421 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1422 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1423 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1424 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1425 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1426 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1427 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1428 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1429 #undef CONDITIONAL_REASSIGN
1434 #endif /* not DOS_NT */
1437 /***********************************************************************
1438 Character Display Information
1439 ***********************************************************************/
1440 static void append_glyph (struct it
*);
1441 static void append_composite_glyph (struct it
*);
1442 static void produce_composite_glyph (struct it
*);
1443 static void append_glyphless_glyph (struct it
*, int, const char *);
1444 static void produce_glyphless_glyph (struct it
*, int, Lisp_Object
);
1446 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1447 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1448 the character for which to produce glyphs; IT->face_id contains the
1449 character's face. Padding glyphs are appended if IT->c has a
1450 IT->pixel_width > 1. */
1453 append_glyph (struct it
*it
)
1455 struct glyph
*glyph
, *end
;
1458 eassert (it
->glyph_row
);
1459 glyph
= (it
->glyph_row
->glyphs
[it
->area
]
1460 + it
->glyph_row
->used
[it
->area
]);
1461 end
= it
->glyph_row
->glyphs
[1 + it
->area
];
1463 /* If the glyph row is reversed, we need to prepend the glyph rather
1465 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1468 int move_by
= it
->pixel_width
;
1470 /* Make room for the new glyphs. */
1471 if (move_by
> end
- glyph
) /* don't overstep end of this area */
1472 move_by
= end
- glyph
;
1473 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1475 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1476 end
= glyph
+ move_by
;
1479 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1480 right, even in the REVERSED_P case, since (a) all of its u.ch are
1481 identical, and (b) the PADDING_P flag needs to be set for the
1482 leftmost one, because we write to the terminal left-to-right. */
1484 i
< it
->pixel_width
&& glyph
< end
;
1487 glyph
->type
= CHAR_GLYPH
;
1488 glyph
->pixel_width
= 1;
1489 glyph
->u
.ch
= it
->char_to_display
;
1490 glyph
->face_id
= it
->face_id
;
1491 glyph
->padding_p
= i
> 0;
1492 glyph
->charpos
= CHARPOS (it
->position
);
1493 glyph
->object
= it
->object
;
1496 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1497 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1499 glyph
->bidi_type
= it
->bidi_it
.type
;
1503 glyph
->resolved_level
= 0;
1504 glyph
->bidi_type
= UNKNOWN_BT
;
1507 ++it
->glyph_row
->used
[it
->area
];
1512 /* For external use. */
1514 tty_append_glyph (struct it
*it
)
1520 /* Produce glyphs for the display element described by IT. *IT
1521 specifies what we want to produce a glyph for (character, image, ...),
1522 and where in the glyph matrix we currently are (glyph row and hpos).
1523 produce_glyphs fills in output fields of *IT with information such as the
1524 pixel width and height of a character, and maybe output actual glyphs at
1525 the same time if IT->glyph_row is non-null. For an overview, see
1526 the explanation in dispextern.h, before the definition of the
1527 display_element_type enumeration.
1529 produce_glyphs also stores the result of glyph width, ascent
1530 etc. computations in *IT.
1532 IT->glyph_row may be null, in which case produce_glyphs does not
1533 actually fill in the glyphs. This is used in the move_* functions
1534 in xdisp.c for text width and height computations.
1536 Callers usually don't call produce_glyphs directly;
1537 instead they use the macro PRODUCE_GLYPHS. */
1540 produce_glyphs (struct it
*it
)
1542 /* If a hook is installed, let it do the work. */
1544 /* Nothing but characters are supported on terminal frames. */
1545 eassert (it
->what
== IT_CHARACTER
1546 || it
->what
== IT_COMPOSITION
1547 || it
->what
== IT_STRETCH
1548 || it
->what
== IT_GLYPHLESS
);
1550 if (it
->what
== IT_STRETCH
)
1552 produce_stretch_glyph (it
);
1556 if (it
->what
== IT_COMPOSITION
)
1558 produce_composite_glyph (it
);
1562 if (it
->what
== IT_GLYPHLESS
)
1564 produce_glyphless_glyph (it
, 0, Qnil
);
1568 if (it
->char_to_display
>= 040 && it
->char_to_display
< 0177)
1570 it
->pixel_width
= it
->nglyphs
= 1;
1574 else if (it
->char_to_display
== '\n')
1575 it
->pixel_width
= it
->nglyphs
= 0;
1576 else if (it
->char_to_display
== '\t')
1578 int absolute_x
= (it
->current_x
1579 + it
->continuation_lines_width
);
1581 = (((1 + absolute_x
+ it
->tab_width
- 1)
1586 /* If part of the TAB has been displayed on the previous line
1587 which is continued now, continuation_lines_width will have
1588 been incremented already by the part that fitted on the
1589 continued line. So, we will get the right number of spaces
1591 nspaces
= next_tab_x
- absolute_x
;
1597 it
->char_to_display
= ' ';
1598 it
->pixel_width
= it
->len
= 1;
1604 it
->pixel_width
= nspaces
;
1605 it
->nglyphs
= nspaces
;
1607 else if (CHAR_BYTE8_P (it
->char_to_display
))
1609 /* Coming here means that we must send the raw 8-bit byte as is
1610 to the terminal. Although there's no way to know how many
1611 columns it occupies on a screen, it is a good assumption that
1612 a single byte code has 1-column width. */
1613 it
->pixel_width
= it
->nglyphs
= 1;
1619 Lisp_Object charset_list
= FRAME_TERMINAL (it
->f
)->charset_list
;
1621 if (char_charset (it
->char_to_display
, charset_list
, NULL
))
1623 it
->pixel_width
= CHAR_WIDTH (it
->char_to_display
);
1624 it
->nglyphs
= it
->pixel_width
;
1630 Lisp_Object acronym
= lookup_glyphless_char_display (-1, it
);
1632 eassert (it
->what
== IT_GLYPHLESS
);
1633 produce_glyphless_glyph (it
, 1, acronym
);
1638 /* Advance current_x by the pixel width as a convenience for
1640 if (it
->area
== TEXT_AREA
)
1641 it
->current_x
+= it
->pixel_width
;
1642 it
->ascent
= it
->max_ascent
= it
->phys_ascent
= it
->max_phys_ascent
= 0;
1643 it
->descent
= it
->max_descent
= it
->phys_descent
= it
->max_phys_descent
= 1;
1646 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1647 Called from produce_composite_glyph for terminal frames if
1648 IT->glyph_row != NULL. IT->face_id contains the character's
1652 append_composite_glyph (struct it
*it
)
1654 struct glyph
*glyph
;
1656 eassert (it
->glyph_row
);
1657 glyph
= it
->glyph_row
->glyphs
[it
->area
] + it
->glyph_row
->used
[it
->area
];
1658 if (glyph
< it
->glyph_row
->glyphs
[1 + it
->area
])
1660 /* If the glyph row is reversed, we need to prepend the glyph
1661 rather than append it. */
1662 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1666 /* Make room for the new glyph. */
1667 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1669 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1671 glyph
->type
= COMPOSITE_GLYPH
;
1672 glyph
->pixel_width
= it
->pixel_width
;
1673 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1674 if (it
->cmp_it
.ch
< 0)
1676 glyph
->u
.cmp
.automatic
= 0;
1677 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1681 glyph
->u
.cmp
.automatic
= 1;
1682 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1683 glyph
->slice
.cmp
.from
= it
->cmp_it
.from
;
1684 glyph
->slice
.cmp
.to
= it
->cmp_it
.to
- 1;
1687 glyph
->face_id
= it
->face_id
;
1688 glyph
->padding_p
= 0;
1689 glyph
->charpos
= CHARPOS (it
->position
);
1690 glyph
->object
= it
->object
;
1693 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1694 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1696 glyph
->bidi_type
= it
->bidi_it
.type
;
1700 glyph
->resolved_level
= 0;
1701 glyph
->bidi_type
= UNKNOWN_BT
;
1704 ++it
->glyph_row
->used
[it
->area
];
1710 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1711 the composition. We simply produces components of the composition
1712 assuming that the terminal has a capability to layout/render it
1716 produce_composite_glyph (struct it
*it
)
1718 if (it
->cmp_it
.ch
< 0)
1720 struct composition
*cmp
= composition_table
[it
->cmp_it
.id
];
1722 it
->pixel_width
= cmp
->width
;
1726 Lisp_Object gstring
= composition_gstring_from_id (it
->cmp_it
.id
);
1728 it
->pixel_width
= composition_gstring_width (gstring
, it
->cmp_it
.from
,
1729 it
->cmp_it
.to
, NULL
);
1733 append_composite_glyph (it
);
1737 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1738 is a face ID to be used for the glyph. What is actually appended
1739 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1740 comes from it->nglyphs bytes). */
1743 append_glyphless_glyph (struct it
*it
, int face_id
, const char *str
)
1745 struct glyph
*glyph
, *end
;
1748 eassert (it
->glyph_row
);
1749 glyph
= it
->glyph_row
->glyphs
[it
->area
] + it
->glyph_row
->used
[it
->area
];
1750 end
= it
->glyph_row
->glyphs
[1 + it
->area
];
1752 /* If the glyph row is reversed, we need to prepend the glyph rather
1754 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1757 int move_by
= it
->pixel_width
;
1759 /* Make room for the new glyphs. */
1760 if (move_by
> end
- glyph
) /* don't overstep end of this area */
1761 move_by
= end
- glyph
;
1762 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1764 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1765 end
= glyph
+ move_by
;
1770 glyph
->type
= CHAR_GLYPH
;
1771 glyph
->pixel_width
= 1;
1772 glyph
->face_id
= face_id
;
1773 glyph
->padding_p
= 0;
1774 glyph
->charpos
= CHARPOS (it
->position
);
1775 glyph
->object
= it
->object
;
1778 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1779 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1781 glyph
->bidi_type
= it
->bidi_it
.type
;
1785 glyph
->resolved_level
= 0;
1786 glyph
->bidi_type
= UNKNOWN_BT
;
1789 /* BIDI Note: we put the glyphs of characters left to right, even in
1790 the REVERSED_P case because we write to the terminal
1792 for (i
= 0; i
< it
->nglyphs
&& glyph
< end
; ++i
)
1795 glyph
[0] = glyph
[-1];
1796 glyph
->u
.ch
= str
[i
];
1797 ++it
->glyph_row
->used
[it
->area
];
1802 /* Produce glyphs for a glyphless character for iterator IT.
1803 IT->glyphless_method specifies which method to use for displaying
1804 the character. See the description of enum
1805 glyphless_display_method in dispextern.h for the details.
1807 FOR_NO_FONT is nonzero if and only if this is for a character that
1808 is not supported by the coding system of the terminal. ACRONYM, if
1809 non-nil, is an acronym string for the character.
1811 The glyphs actually produced are of type CHAR_GLYPH. */
1814 produce_glyphless_glyph (struct it
*it
, int for_no_font
, Lisp_Object acronym
)
1818 char buf
[sizeof "\\x" + max (6, (sizeof it
->c
* CHAR_BIT
+ 3) / 4)];
1819 char const *str
= " ";
1821 /* Get a face ID for the glyph by utilizing a cache (the same way as
1822 done for `escape-glyph' in get_next_display_element). */
1823 if (it
->f
== last_glyphless_glyph_frame
1824 && it
->face_id
== last_glyphless_glyph_face_id
)
1826 face_id
= last_glyphless_glyph_merged_face_id
;
1830 /* Merge the `glyphless-char' face into the current face. */
1831 face_id
= merge_faces (it
->f
, Qglyphless_char
, 0, it
->face_id
);
1832 last_glyphless_glyph_frame
= it
->f
;
1833 last_glyphless_glyph_face_id
= it
->face_id
;
1834 last_glyphless_glyph_merged_face_id
= face_id
;
1837 if (it
->glyphless_method
== GLYPHLESS_DISPLAY_THIN_SPACE
)
1839 /* As there's no way to produce a thin space, we produce a space
1840 of canonical width. */
1843 else if (it
->glyphless_method
== GLYPHLESS_DISPLAY_EMPTY_BOX
)
1845 len
= CHAR_WIDTH (it
->c
);
1850 len
= sprintf (buf
, "[%.*s]", len
, str
);
1855 if (it
->glyphless_method
== GLYPHLESS_DISPLAY_ACRONYM
)
1857 if (! STRINGP (acronym
) && CHAR_TABLE_P (Vglyphless_char_display
))
1858 acronym
= CHAR_TABLE_REF (Vglyphless_char_display
, it
->c
);
1859 if (CONSP (acronym
))
1860 acronym
= XCDR (acronym
);
1862 str
= STRINGP (acronym
) ? SSDATA (acronym
) : "";
1863 for (len
= 0; len
< 6 && str
[len
] && ASCII_BYTE_P (str
[len
]); len
++)
1864 buf
[1 + len
] = str
[len
];
1870 eassert (it
->glyphless_method
== GLYPHLESS_DISPLAY_HEX_CODE
);
1871 len
= (it
->c
< 0x10000 ? sprintf (buf
, "\\u%04X", it
->c
)
1872 : it
->c
<= MAX_UNICODE_CHAR
? sprintf (buf
, "\\U%06X", it
->c
)
1873 : sprintf (buf
, "\\x%06X", it
->c
));
1878 it
->pixel_width
= len
;
1881 append_glyphless_glyph (it
, face_id
, str
);
1885 /***********************************************************************
1887 ***********************************************************************/
1889 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1890 one of the enumerators from enum no_color_bit, or a bit set built
1891 from them. Some display attributes may not be used together with
1892 color; the termcap capability `NC' specifies which ones. */
1894 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1895 (tty->TN_max_colors > 0 \
1896 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1899 /* Turn appearances of face FACE_ID on tty frame F on.
1900 FACE_ID is a realized face ID number, in the face cache. */
1903 turn_on_face (struct frame
*f
, int face_id
)
1905 struct face
*face
= FACE_FROM_ID (f
, face_id
);
1906 long fg
= face
->foreground
;
1907 long bg
= face
->background
;
1908 struct tty_display_info
*tty
= FRAME_TTY (f
);
1910 /* Do this first because TS_end_standout_mode may be the same
1911 as TS_exit_attribute_mode, which turns all appearances off. */
1912 if (MAY_USE_WITH_COLORS_P (tty
, NC_REVERSE
))
1914 if (tty
->TN_max_colors
> 0)
1916 if (fg
>= 0 && bg
>= 0)
1918 /* If the terminal supports colors, we can set them
1919 below without using reverse video. The face's fg
1920 and bg colors are set as they should appear on
1921 the screen, i.e. they take the inverse-video'ness
1922 of the face already into account. */
1924 else if (inverse_video
)
1926 if (fg
== FACE_TTY_DEFAULT_FG_COLOR
1927 || bg
== FACE_TTY_DEFAULT_BG_COLOR
)
1928 tty_toggle_highlight (tty
);
1932 if (fg
== FACE_TTY_DEFAULT_BG_COLOR
1933 || bg
== FACE_TTY_DEFAULT_FG_COLOR
)
1934 tty_toggle_highlight (tty
);
1939 /* If we can't display colors, use reverse video
1940 if the face specifies that. */
1943 if (fg
== FACE_TTY_DEFAULT_FG_COLOR
1944 || bg
== FACE_TTY_DEFAULT_BG_COLOR
)
1945 tty_toggle_highlight (tty
);
1949 if (fg
== FACE_TTY_DEFAULT_BG_COLOR
1950 || bg
== FACE_TTY_DEFAULT_FG_COLOR
)
1951 tty_toggle_highlight (tty
);
1956 if (face
->tty_bold_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_BOLD
))
1957 OUTPUT1_IF (tty
, tty
->TS_enter_bold_mode
);
1959 if (face
->tty_italic_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_ITALIC
))
1961 if (tty
->TS_enter_italic_mode
)
1962 OUTPUT1 (tty
, tty
->TS_enter_italic_mode
);
1964 /* Italics mode is unavailable on many terminals. In that
1965 case, map slant to dimmed text; we want italic text to
1966 appear different and dimming is not otherwise used. */
1967 OUTPUT1 (tty
, tty
->TS_enter_dim_mode
);
1970 if (face
->tty_underline_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_UNDERLINE
))
1971 OUTPUT1_IF (tty
, tty
->TS_enter_underline_mode
);
1973 if (tty
->TN_max_colors
> 0)
1978 ts
= tty
->standout_mode
? tty
->TS_set_background
: tty
->TS_set_foreground
;
1981 p
= tparam (ts
, NULL
, 0, (int) fg
, 0, 0, 0);
1986 ts
= tty
->standout_mode
? tty
->TS_set_foreground
: tty
->TS_set_background
;
1989 p
= tparam (ts
, NULL
, 0, (int) bg
, 0, 0, 0);
1997 /* Turn off appearances of face FACE_ID on tty frame F. */
2000 turn_off_face (struct frame
*f
, int face_id
)
2002 struct face
*face
= FACE_FROM_ID (f
, face_id
);
2003 struct tty_display_info
*tty
= FRAME_TTY (f
);
2005 eassert (face
!= NULL
);
2007 if (tty
->TS_exit_attribute_mode
)
2009 /* Capability "me" will turn off appearance modes double-bright,
2010 half-bright, reverse-video, standout, underline. It may or
2011 may not turn off alt-char-mode. */
2012 if (face
->tty_bold_p
2013 || face
->tty_italic_p
2014 || face
->tty_reverse_p
2015 || face
->tty_underline_p
)
2017 OUTPUT1_IF (tty
, tty
->TS_exit_attribute_mode
);
2018 if (strcmp (tty
->TS_exit_attribute_mode
, tty
->TS_end_standout_mode
) == 0)
2019 tty
->standout_mode
= 0;
2024 /* If we don't have "me" we can only have those appearances
2025 that have exit sequences defined. */
2026 if (face
->tty_underline_p
)
2027 OUTPUT_IF (tty
, tty
->TS_exit_underline_mode
);
2030 /* Switch back to default colors. */
2031 if (tty
->TN_max_colors
> 0
2032 && ((face
->foreground
!= FACE_TTY_DEFAULT_COLOR
2033 && face
->foreground
!= FACE_TTY_DEFAULT_FG_COLOR
)
2034 || (face
->background
!= FACE_TTY_DEFAULT_COLOR
2035 && face
->background
!= FACE_TTY_DEFAULT_BG_COLOR
)))
2036 OUTPUT1_IF (tty
, tty
->TS_orig_pair
);
2040 /* Return non-zero if the terminal on frame F supports all of the
2041 capabilities in CAPS simultaneously, with foreground and background
2042 colors FG and BG. */
2045 tty_capable_p (struct tty_display_info
*tty
, unsigned int caps
,
2046 unsigned long fg
, unsigned long bg
)
2048 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2049 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2052 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_INVERSE
, tty
->TS_standout_mode
, NC_REVERSE
);
2053 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_UNDERLINE
, tty
->TS_enter_underline_mode
, NC_UNDERLINE
);
2054 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_BOLD
, tty
->TS_enter_bold_mode
, NC_BOLD
);
2055 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_DIM
, tty
->TS_enter_dim_mode
, NC_DIM
);
2056 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_ITALIC
, tty
->TS_enter_italic_mode
, NC_ITALIC
);
2062 /* Return non-zero if the terminal is capable to display colors. */
2064 DEFUN ("tty-display-color-p", Ftty_display_color_p
, Stty_display_color_p
,
2066 doc
: /* Return non-nil if the tty device TERMINAL can display colors.
2068 TERMINAL can be a terminal object, a frame, or nil (meaning the
2069 selected frame's terminal). This function always returns nil if
2070 TERMINAL does not refer to a text terminal. */)
2071 (Lisp_Object terminal
)
2073 struct terminal
*t
= get_tty_terminal (terminal
, 0);
2077 return t
->display_info
.tty
->TN_max_colors
> 0 ? Qt
: Qnil
;
2080 /* Return the number of supported colors. */
2081 DEFUN ("tty-display-color-cells", Ftty_display_color_cells
,
2082 Stty_display_color_cells
, 0, 1, 0,
2083 doc
: /* Return the number of colors supported by the tty device TERMINAL.
2085 TERMINAL can be a terminal object, a frame, or nil (meaning the
2086 selected frame's terminal). This function always returns 0 if
2087 TERMINAL does not refer to a text terminal. */)
2088 (Lisp_Object terminal
)
2090 struct terminal
*t
= get_tty_terminal (terminal
, 0);
2092 return make_number (0);
2094 return make_number (t
->display_info
.tty
->TN_max_colors
);
2099 /* Declare here rather than in the function, as in the rest of Emacs,
2100 to work around an HPUX compiler bug (?). See
2101 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2102 static int default_max_colors
;
2103 static int default_max_pairs
;
2104 static int default_no_color_video
;
2105 static char *default_orig_pair
;
2106 static char *default_set_foreground
;
2107 static char *default_set_background
;
2109 /* Save or restore the default color-related capabilities of this
2112 tty_default_color_capabilities (struct tty_display_info
*tty
, int save
)
2117 xfree (default_orig_pair
);
2118 default_orig_pair
= tty
->TS_orig_pair
? xstrdup (tty
->TS_orig_pair
) : NULL
;
2120 xfree (default_set_foreground
);
2121 default_set_foreground
= tty
->TS_set_foreground
? xstrdup (tty
->TS_set_foreground
)
2124 xfree (default_set_background
);
2125 default_set_background
= tty
->TS_set_background
? xstrdup (tty
->TS_set_background
)
2128 default_max_colors
= tty
->TN_max_colors
;
2129 default_max_pairs
= tty
->TN_max_pairs
;
2130 default_no_color_video
= tty
->TN_no_color_video
;
2134 tty
->TS_orig_pair
= default_orig_pair
;
2135 tty
->TS_set_foreground
= default_set_foreground
;
2136 tty
->TS_set_background
= default_set_background
;
2137 tty
->TN_max_colors
= default_max_colors
;
2138 tty
->TN_max_pairs
= default_max_pairs
;
2139 tty
->TN_no_color_video
= default_no_color_video
;
2143 /* Setup one of the standard tty color schemes according to MODE.
2144 MODE's value is generally the number of colors which we want to
2145 support; zero means set up for the default capabilities, the ones
2146 we saw at init_tty time; -1 means turn off color support. */
2148 tty_setup_colors (struct tty_display_info
*tty
, int mode
)
2150 /* Canonicalize all negative values of MODE. */
2156 case -1: /* no colors at all */
2157 tty
->TN_max_colors
= 0;
2158 tty
->TN_max_pairs
= 0;
2159 tty
->TN_no_color_video
= 0;
2160 tty
->TS_set_foreground
= tty
->TS_set_background
= tty
->TS_orig_pair
= NULL
;
2162 case 0: /* default colors, if any */
2164 tty_default_color_capabilities (tty
, 0);
2166 case 8: /* 8 standard ANSI colors */
2167 tty
->TS_orig_pair
= "\033[0m";
2169 tty
->TS_set_foreground
= "\033[3%p1%dm";
2170 tty
->TS_set_background
= "\033[4%p1%dm";
2172 tty
->TS_set_foreground
= "\033[3%dm";
2173 tty
->TS_set_background
= "\033[4%dm";
2175 tty
->TN_max_colors
= 8;
2176 tty
->TN_max_pairs
= 64;
2177 tty
->TN_no_color_video
= 0;
2183 set_tty_color_mode (struct tty_display_info
*tty
, struct frame
*f
)
2185 Lisp_Object tem
, val
;
2186 Lisp_Object color_mode
;
2188 Lisp_Object tty_color_mode_alist
2189 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil
);
2191 tem
= assq_no_quit (Qtty_color_mode
, f
->param_alist
);
2192 val
= CONSP (tem
) ? XCDR (tem
) : Qnil
;
2196 else if (SYMBOLP (tty_color_mode_alist
))
2198 tem
= Fassq (val
, Fsymbol_value (tty_color_mode_alist
));
2199 color_mode
= CONSP (tem
) ? XCDR (tem
) : Qnil
;
2204 mode
= TYPE_RANGED_INTEGERP (int, color_mode
) ? XINT (color_mode
) : 0;
2206 if (mode
!= tty
->previous_color_mode
)
2208 tty
->previous_color_mode
= mode
;
2209 tty_setup_colors (tty
, mode
);
2210 /* This recomputes all the faces given the new color definitions. */
2211 safe_call (1, intern ("tty-set-up-initial-frame-faces"));
2215 #endif /* !DOS_NT */
2219 /* Return the tty display object specified by TERMINAL. */
2221 static struct terminal
*
2222 get_tty_terminal (Lisp_Object terminal
, int throw)
2224 struct terminal
*t
= get_terminal (terminal
, throw);
2226 if (t
&& t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2229 error ("Device %d is not a termcap terminal device", t
->id
);
2237 /* Return an active termcap device that uses the tty device with the
2240 This function ignores suspended devices.
2242 Returns NULL if the named terminal device is not opened. */
2245 get_named_tty (const char *name
)
2252 for (t
= terminal_list
; t
; t
= t
->next_terminal
)
2254 if ((t
->type
== output_termcap
|| t
->type
== output_msdos_raw
)
2255 && !strcmp (t
->display_info
.tty
->name
, name
)
2256 && TERMINAL_ACTIVE_P (t
))
2264 DEFUN ("tty-type", Ftty_type
, Stty_type
, 0, 1, 0,
2265 doc
: /* Return the type of the tty device that TERMINAL uses.
2266 Returns nil if TERMINAL is not on a tty device.
2268 TERMINAL can be a terminal object, a frame, or nil (meaning the
2269 selected frame's terminal). */)
2270 (Lisp_Object terminal
)
2272 struct terminal
*t
= get_terminal (terminal
, 1);
2274 if (t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2277 if (t
->display_info
.tty
->type
)
2278 return build_string (t
->display_info
.tty
->type
);
2283 DEFUN ("controlling-tty-p", Fcontrolling_tty_p
, Scontrolling_tty_p
, 0, 1, 0,
2284 doc
: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2286 TERMINAL can be a terminal object, a frame, or nil (meaning the
2287 selected frame's terminal). This function always returns nil if
2288 TERMINAL is not on a tty device. */)
2289 (Lisp_Object terminal
)
2291 struct terminal
*t
= get_terminal (terminal
, 1);
2293 if ((t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2294 || strcmp (t
->display_info
.tty
->name
, DEV_TTY
) != 0)
2300 DEFUN ("tty-no-underline", Ftty_no_underline
, Stty_no_underline
, 0, 1, 0,
2301 doc
: /* Declare that the tty used by TERMINAL does not handle underlining.
2302 This is used to override the terminfo data, for certain terminals that
2303 do not really do underlining, but say that they do. This function has
2304 no effect if used on a non-tty terminal.
2306 TERMINAL can be a terminal object, a frame or nil (meaning the
2307 selected frame's terminal). This function always returns nil if
2308 TERMINAL does not refer to a text terminal. */)
2309 (Lisp_Object terminal
)
2311 struct terminal
*t
= get_terminal (terminal
, 1);
2313 if (t
->type
== output_termcap
)
2314 t
->display_info
.tty
->TS_enter_underline_mode
= 0;
2318 DEFUN ("tty-top-frame", Ftty_top_frame
, Stty_top_frame
, 0, 1, 0,
2319 doc
: /* Return the topmost terminal frame on TERMINAL.
2320 TERMINAL can be a terminal object, a frame or nil (meaning the
2321 selected frame's terminal). This function returns nil if TERMINAL
2322 does not refer to a text terminal. Otherwise, it returns the
2323 top-most frame on the text terminal. */)
2324 (Lisp_Object terminal
)
2326 struct terminal
*t
= get_terminal (terminal
, 1);
2328 if (t
->type
== output_termcap
)
2329 return t
->display_info
.tty
->top_frame
;
2335 DEFUN ("suspend-tty", Fsuspend_tty
, Ssuspend_tty
, 0, 1, 0,
2336 doc
: /* Suspend the terminal device TTY.
2338 The device is restored to its default state, and Emacs ceases all
2339 access to the tty device. Frames that use the device are not deleted,
2340 but input is not read from them and if they change, their display is
2343 TTY may be a terminal object, a frame, or nil for the terminal device
2344 of the currently selected frame.
2346 This function runs `suspend-tty-functions' after suspending the
2347 device. The functions are run with one arg, the id of the suspended
2350 `suspend-tty' does nothing if it is called on a device that is already
2353 A suspended tty may be resumed by calling `resume-tty' on it. */)
2356 struct terminal
*t
= get_tty_terminal (tty
, 1);
2360 error ("Unknown tty device");
2362 f
= t
->display_info
.tty
->input
;
2366 /* First run `suspend-tty-functions' and then clean up the tty
2367 state because `suspend-tty-functions' might need to change
2369 Lisp_Object args
[2];
2370 args
[0] = intern ("suspend-tty-functions");
2371 XSETTERMINAL (args
[1], t
);
2372 Frun_hook_with_args (2, args
);
2374 reset_sys_modes (t
->display_info
.tty
);
2375 delete_keyboard_wait_descriptor (fileno (f
));
2379 if (f
!= t
->display_info
.tty
->output
)
2380 fclose (t
->display_info
.tty
->output
);
2383 t
->display_info
.tty
->input
= 0;
2384 t
->display_info
.tty
->output
= 0;
2386 if (FRAMEP (t
->display_info
.tty
->top_frame
))
2387 FRAME_SET_VISIBLE (XFRAME (t
->display_info
.tty
->top_frame
), 0);
2391 /* Clear display hooks to prevent further output. */
2392 clear_tty_hooks (t
);
2397 DEFUN ("resume-tty", Fresume_tty
, Sresume_tty
, 0, 1, 0,
2398 doc
: /* Resume the previously suspended terminal device TTY.
2399 The terminal is opened and reinitialized. Frames that are on the
2400 suspended terminal are revived.
2402 It is an error to resume a terminal while another terminal is active
2405 This function runs `resume-tty-functions' after resuming the terminal.
2406 The functions are run with one arg, the id of the resumed terminal
2409 `resume-tty' does nothing if it is called on a device that is not
2412 TTY may be a terminal object, a frame, or nil (meaning the selected
2413 frame's terminal). */)
2416 struct terminal
*t
= get_tty_terminal (tty
, 1);
2420 error ("Unknown tty device");
2422 if (!t
->display_info
.tty
->input
)
2424 if (get_named_tty (t
->display_info
.tty
->name
))
2425 error ("Cannot resume display while another display is active on the same device");
2428 t
->display_info
.tty
->output
= stdout
;
2429 t
->display_info
.tty
->input
= stdin
;
2431 fd
= emacs_open (t
->display_info
.tty
->name
, O_RDWR
| O_NOCTTY
, 0);
2434 error ("Can not reopen tty device %s: %s", t
->display_info
.tty
->name
, strerror (errno
));
2436 if (strcmp (t
->display_info
.tty
->name
, DEV_TTY
))
2437 dissociate_if_controlling_tty (fd
);
2439 t
->display_info
.tty
->output
= fdopen (fd
, "w+");
2440 t
->display_info
.tty
->input
= t
->display_info
.tty
->output
;
2443 add_keyboard_wait_descriptor (fd
);
2445 if (FRAMEP (t
->display_info
.tty
->top_frame
))
2447 struct frame
*f
= XFRAME (t
->display_info
.tty
->top_frame
);
2449 int old_height
= FRAME_COLS (f
);
2450 int old_width
= FRAME_LINES (f
);
2452 /* Check if terminal/window size has changed while the frame
2454 get_tty_size (fileno (t
->display_info
.tty
->input
), &width
, &height
);
2455 if (width
!= old_width
|| height
!= old_height
)
2456 change_frame_size (f
, height
, width
, 0, 0, 0);
2457 FRAME_SET_VISIBLE (XFRAME (t
->display_info
.tty
->top_frame
), 1);
2461 init_sys_modes (t
->display_info
.tty
);
2464 /* Run `resume-tty-functions'. */
2465 Lisp_Object args
[2];
2466 args
[0] = intern ("resume-tty-functions");
2467 XSETTERMINAL (args
[1], t
);
2468 Frun_hook_with_args (2, args
);
2478 /***********************************************************************
2480 ***********************************************************************/
2484 #ifndef HAVE_WINDOW_SYSTEM
2486 term_mouse_moveto (int x
, int y
)
2488 /* TODO: how to set mouse position?
2491 name = (const char *) ttyname (0);
2492 fd = open (name, O_WRONLY);
2493 SOME_FUNCTION (x, y, fd);
2496 last_mouse_y = y; */
2498 #endif /* HAVE_WINDOW_SYSTEM */
2500 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2502 tty_draw_row_with_mouse_face (struct window
*w
, struct glyph_row
*row
,
2503 int start_hpos
, int end_hpos
,
2504 enum draw_glyphs_face draw
)
2506 int nglyphs
= end_hpos
- start_hpos
;
2507 struct frame
*f
= XFRAME (WINDOW_FRAME (w
));
2508 struct tty_display_info
*tty
= FRAME_TTY (f
);
2509 int face_id
= tty
->mouse_highlight
.mouse_face_face_id
;
2510 int save_x
, save_y
, pos_x
, pos_y
;
2512 if (end_hpos
>= row
->used
[TEXT_AREA
])
2513 nglyphs
= row
->used
[TEXT_AREA
] - start_hpos
;
2515 pos_y
= row
->y
+ WINDOW_TOP_EDGE_Y (w
);
2516 pos_x
= row
->used
[LEFT_MARGIN_AREA
] + start_hpos
+ WINDOW_LEFT_EDGE_X (w
);
2518 /* Save current cursor co-ordinates. */
2519 save_y
= curY (tty
);
2520 save_x
= curX (tty
);
2521 cursor_to (f
, pos_y
, pos_x
);
2523 if (draw
== DRAW_MOUSE_FACE
)
2524 tty_write_glyphs_with_face (f
, row
->glyphs
[TEXT_AREA
] + start_hpos
,
2526 else if (draw
== DRAW_NORMAL_TEXT
)
2527 write_glyphs (f
, row
->glyphs
[TEXT_AREA
] + start_hpos
, nglyphs
);
2529 cursor_to (f
, save_y
, save_x
);
2533 term_mouse_movement (FRAME_PTR frame
, Gpm_Event
*event
)
2535 /* Has the mouse moved off the glyph it was on at the last sighting? */
2536 if (event
->x
!= last_mouse_x
|| event
->y
!= last_mouse_y
)
2538 frame
->mouse_moved
= 1;
2539 note_mouse_highlight (frame
, event
->x
, event
->y
);
2540 /* Remember which glyph we're now on. */
2541 last_mouse_x
= event
->x
;
2542 last_mouse_y
= event
->y
;
2548 /* Return the Time that corresponds to T. Wrap around on overflow. */
2550 timeval_to_Time (struct timeval
const *t
)
2556 ms
= t
->tv_usec
/ 1000;
2560 /* Return the current position of the mouse.
2562 Set *f to the frame the mouse is in, or zero if the mouse is in no
2563 Emacs frame. If it is set to zero, all the other arguments are
2566 Set *bar_window to Qnil, and *x and *y to the column and
2567 row of the character cell the mouse is over.
2569 Set *timeptr to the time the mouse was at the returned position.
2571 This clears mouse_moved until the next motion
2574 term_mouse_position (FRAME_PTR
*fp
, int insist
, Lisp_Object
*bar_window
,
2575 enum scroll_bar_part
*part
, Lisp_Object
*x
,
2576 Lisp_Object
*y
, Time
*timeptr
)
2580 *fp
= SELECTED_FRAME ();
2581 (*fp
)->mouse_moved
= 0;
2586 XSETINT (*x
, last_mouse_x
);
2587 XSETINT (*y
, last_mouse_y
);
2588 gettimeofday(&now
, 0);
2589 *timeptr
= timeval_to_Time (&now
);
2592 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2594 If the event is a button press, then note that we have grabbed
2598 term_mouse_click (struct input_event
*result
, Gpm_Event
*event
,
2604 result
->kind
= GPM_CLICK_EVENT
;
2605 for (i
= 0, j
= GPM_B_LEFT
; i
< 3; i
++, j
>>= 1 )
2607 if (event
->buttons
& j
) {
2608 result
->code
= i
; /* button number */
2612 gettimeofday(&now
, 0);
2613 result
->timestamp
= timeval_to_Time (&now
);
2615 if (event
->type
& GPM_UP
)
2616 result
->modifiers
= up_modifier
;
2617 else if (event
->type
& GPM_DOWN
)
2618 result
->modifiers
= down_modifier
;
2620 result
->modifiers
= 0;
2622 if (event
->type
& GPM_SINGLE
)
2623 result
->modifiers
|= click_modifier
;
2625 if (event
->type
& GPM_DOUBLE
)
2626 result
->modifiers
|= double_modifier
;
2628 if (event
->type
& GPM_TRIPLE
)
2629 result
->modifiers
|= triple_modifier
;
2631 if (event
->type
& GPM_DRAG
)
2632 result
->modifiers
|= drag_modifier
;
2634 if (!(event
->type
& (GPM_MOVE
| GPM_DRAG
))) {
2637 if (event
->modifiers
& (1 << 0))
2638 result
->modifiers
|= shift_modifier
;
2641 if (event
->modifiers
& (1 << 2))
2642 result
->modifiers
|= ctrl_modifier
;
2644 /* 1 << KG_ALT || KG_ALTGR */
2645 if (event
->modifiers
& (1 << 3)
2646 || event
->modifiers
& (1 << 1))
2647 result
->modifiers
|= meta_modifier
;
2650 XSETINT (result
->x
, event
->x
);
2651 XSETINT (result
->y
, event
->y
);
2652 XSETFRAME (result
->frame_or_window
, f
);
2658 handle_one_term_event (struct tty_display_info
*tty
, Gpm_Event
*event
, struct input_event
* hold_quit
)
2660 struct frame
*f
= XFRAME (tty
->top_frame
);
2661 struct input_event ie
;
2669 if (event
->type
& (GPM_MOVE
| GPM_DRAG
)) {
2670 previous_help_echo_string
= help_echo_string
;
2671 help_echo_string
= Qnil
;
2673 Gpm_DrawPointer (event
->x
, event
->y
, fileno (tty
->output
));
2675 if (!term_mouse_movement (f
, event
))
2676 help_echo_string
= previous_help_echo_string
;
2678 /* If the contents of the global variable help_echo_string
2679 has changed, generate a HELP_EVENT. */
2680 if (!NILP (help_echo_string
)
2681 || !NILP (previous_help_echo_string
))
2688 term_mouse_click (&ie
, event
, f
);
2692 if (ie
.kind
!= NO_EVENT
)
2694 kbd_buffer_store_event_hold (&ie
, hold_quit
);
2699 && !(hold_quit
&& hold_quit
->kind
!= NO_EVENT
))
2704 XSETFRAME (frame
, f
);
2708 gen_help_event (help_echo_string
, frame
, help_echo_window
,
2709 help_echo_object
, help_echo_pos
);
2716 DEFUN ("gpm-mouse-start", Fgpm_mouse_start
, Sgpm_mouse_start
,
2718 doc
: /* Open a connection to Gpm.
2719 Gpm-mouse can only be activated for one tty at a time. */)
2722 struct frame
*f
= SELECTED_FRAME ();
2723 struct tty_display_info
*tty
2724 = ((f
)->output_method
== output_termcap
2725 ? (f
)->terminal
->display_info
.tty
: NULL
);
2726 Gpm_Connect connection
;
2729 error ("Gpm-mouse only works in the GNU/Linux console");
2731 return Qnil
; /* Already activated, nothing to do. */
2733 error ("Gpm-mouse can only be activated for one tty at a time");
2735 connection
.eventMask
= ~0;
2736 connection
.defaultMask
= ~GPM_HARD
;
2737 connection
.maxMod
= ~0;
2738 connection
.minMod
= 0;
2741 if (Gpm_Open (&connection
, 0) < 0)
2742 error ("Gpm-mouse failed to connect to the gpm daemon");
2746 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2747 to generate SIGIOs. Apparently we need to call reset_sys_modes
2748 before calling init_sys_modes. */
2749 reset_sys_modes (tty
);
2750 init_sys_modes (tty
);
2751 add_gpm_wait_descriptor (gpm_fd
);
2760 delete_gpm_wait_descriptor (fd
);
2761 while (Gpm_Close()); /* close all the stack */
2765 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop
, Sgpm_mouse_stop
,
2767 doc
: /* Close a connection to Gpm. */)
2770 struct frame
*f
= SELECTED_FRAME ();
2771 struct tty_display_info
*tty
2772 = ((f
)->output_method
== output_termcap
2773 ? (f
)->terminal
->display_info
.tty
: NULL
);
2775 if (!tty
|| gpm_tty
!= tty
)
2776 return Qnil
; /* Not activated on this terminal, nothing to do. */
2781 #endif /* HAVE_GPM */
2785 /***********************************************************************
2787 ***********************************************************************/
2789 /* Initialize the tty-dependent part of frame F. The frame must
2790 already have its device initialized. */
2793 create_tty_output (struct frame
*f
)
2795 struct tty_output
*t
= xzalloc (sizeof *t
);
2797 if (! FRAME_TERMCAP_P (f
))
2800 t
->display_info
= FRAME_TERMINAL (f
)->display_info
.tty
;
2802 f
->output_data
.tty
= t
;
2805 /* Delete frame F's face cache, and its tty-dependent part. */
2808 tty_free_frame_resources (struct frame
*f
)
2810 if (! FRAME_TERMCAP_P (f
))
2813 if (FRAME_FACE_CACHE (f
))
2814 free_frame_faces (f
);
2816 xfree (f
->output_data
.tty
);
2821 /* Delete frame F's face cache. */
2824 tty_free_frame_resources (struct frame
*f
)
2826 if (! FRAME_TERMCAP_P (f
) && ! FRAME_MSDOS_P (f
))
2829 if (FRAME_FACE_CACHE (f
))
2830 free_frame_faces (f
);
2834 /* Reset the hooks in TERMINAL. */
2837 clear_tty_hooks (struct terminal
*terminal
)
2840 terminal
->cursor_to_hook
= 0;
2841 terminal
->raw_cursor_to_hook
= 0;
2842 terminal
->clear_to_end_hook
= 0;
2843 terminal
->clear_frame_hook
= 0;
2844 terminal
->clear_end_of_line_hook
= 0;
2845 terminal
->ins_del_lines_hook
= 0;
2846 terminal
->insert_glyphs_hook
= 0;
2847 terminal
->write_glyphs_hook
= 0;
2848 terminal
->delete_glyphs_hook
= 0;
2849 terminal
->ring_bell_hook
= 0;
2850 terminal
->reset_terminal_modes_hook
= 0;
2851 terminal
->set_terminal_modes_hook
= 0;
2852 terminal
->update_begin_hook
= 0;
2853 terminal
->update_end_hook
= 0;
2854 terminal
->set_terminal_window_hook
= 0;
2855 terminal
->mouse_position_hook
= 0;
2856 terminal
->frame_rehighlight_hook
= 0;
2857 terminal
->frame_raise_lower_hook
= 0;
2858 terminal
->fullscreen_hook
= 0;
2859 terminal
->set_vertical_scroll_bar_hook
= 0;
2860 terminal
->condemn_scroll_bars_hook
= 0;
2861 terminal
->redeem_scroll_bar_hook
= 0;
2862 terminal
->judge_scroll_bars_hook
= 0;
2863 terminal
->read_socket_hook
= 0;
2864 terminal
->frame_up_to_date_hook
= 0;
2866 /* Leave these two set, or suspended frames are not deleted
2868 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
2869 terminal
->delete_terminal_hook
= &delete_tty
;
2872 /* Initialize hooks in TERMINAL with the values needed for a tty. */
2875 set_tty_hooks (struct terminal
*terminal
)
2877 terminal
->rif
= 0; /* ttys don't support window-based redisplay. */
2879 terminal
->cursor_to_hook
= &tty_cursor_to
;
2880 terminal
->raw_cursor_to_hook
= &tty_raw_cursor_to
;
2882 terminal
->clear_to_end_hook
= &tty_clear_to_end
;
2883 terminal
->clear_frame_hook
= &tty_clear_frame
;
2884 terminal
->clear_end_of_line_hook
= &tty_clear_end_of_line
;
2886 terminal
->ins_del_lines_hook
= &tty_ins_del_lines
;
2888 terminal
->insert_glyphs_hook
= &tty_insert_glyphs
;
2889 terminal
->write_glyphs_hook
= &tty_write_glyphs
;
2890 terminal
->delete_glyphs_hook
= &tty_delete_glyphs
;
2892 terminal
->ring_bell_hook
= &tty_ring_bell
;
2894 terminal
->reset_terminal_modes_hook
= &tty_reset_terminal_modes
;
2895 terminal
->set_terminal_modes_hook
= &tty_set_terminal_modes
;
2896 terminal
->update_begin_hook
= 0; /* Not needed. */
2897 terminal
->update_end_hook
= &tty_update_end
;
2898 terminal
->set_terminal_window_hook
= &tty_set_terminal_window
;
2900 terminal
->mouse_position_hook
= 0; /* Not needed. */
2901 terminal
->frame_rehighlight_hook
= 0; /* Not needed. */
2902 terminal
->frame_raise_lower_hook
= 0; /* Not needed. */
2904 terminal
->set_vertical_scroll_bar_hook
= 0; /* Not needed. */
2905 terminal
->condemn_scroll_bars_hook
= 0; /* Not needed. */
2906 terminal
->redeem_scroll_bar_hook
= 0; /* Not needed. */
2907 terminal
->judge_scroll_bars_hook
= 0; /* Not needed. */
2909 terminal
->read_socket_hook
= &tty_read_avail_input
; /* keyboard.c */
2910 terminal
->frame_up_to_date_hook
= 0; /* Not needed. */
2912 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
2913 terminal
->delete_terminal_hook
= &delete_tty
;
2916 /* Drop the controlling terminal if fd is the same device. */
2918 dissociate_if_controlling_tty (int fd
)
2921 int pgid
= tcgetpgrp (fd
); /* If tcgetpgrp succeeds, fd is the ctty. */
2926 no_controlling_tty
= 1;
2927 #elif defined (CYGWIN)
2929 no_controlling_tty
= 1;
2931 #ifdef TIOCNOTTY /* Try BSD ioctls. */
2933 sigemptyset (&blocked
);
2934 sigaddset (&blocked
, SIGTTOU
);
2935 pthread_sigmask (SIG_BLOCK
, &blocked
, 0);
2936 fd
= emacs_open (DEV_TTY
, O_RDWR
, 0);
2937 if (fd
!= -1 && ioctl (fd
, TIOCNOTTY
, 0) != -1)
2939 no_controlling_tty
= 1;
2943 pthread_sigmask (SIG_UNBLOCK
, &blocked
, 0);
2945 # error "Unknown system."
2946 #endif /* ! TIOCNOTTY */
2949 #endif /* !DOS_NT */
2952 /* Create a termcap display on the tty device with the given name and
2955 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
2956 Otherwise NAME should be a path to the tty device file,
2959 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
2961 If MUST_SUCCEED is true, then all errors are fatal. */
2964 init_tty (const char *name
, const char *terminal_type
, int must_succeed
)
2967 char **address
= &area
;
2968 int buffer_size
= 4096;
2970 struct tty_display_info
*tty
= NULL
;
2971 struct terminal
*terminal
= NULL
;
2972 int ctty
= 0; /* 1 if asked to open controlling tty. */
2975 maybe_fatal (must_succeed
, 0,
2976 "Unknown terminal type",
2977 "Unknown terminal type");
2981 if (!strcmp (name
, DEV_TTY
))
2984 /* If we already have a terminal on the given device, use that. If
2985 all such terminals are suspended, create a new one instead. */
2986 /* XXX Perhaps this should be made explicit by having init_tty
2987 always create a new terminal and separating terminal and frame
2988 creation on Lisp level. */
2989 terminal
= get_named_tty (name
);
2993 terminal
= create_terminal ();
2996 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
2999 tty
= &the_only_display_info
;
3001 tty
= xzalloc (sizeof *tty
);
3003 tty
->top_frame
= Qnil
;
3004 tty
->next
= tty_list
;
3007 terminal
->type
= output_termcap
;
3008 terminal
->display_info
.tty
= tty
;
3009 tty
->terminal
= terminal
;
3011 tty
->Wcm
= xmalloc (sizeof *tty
->Wcm
);
3014 encode_terminal_src_size
= 0;
3015 encode_terminal_dst_size
= 0;
3019 set_tty_hooks (terminal
);
3025 #ifdef O_IGNORE_CTTY
3027 /* Open the terminal device. Don't recognize it as our
3028 controlling terminal, and don't make it the controlling tty
3029 if we don't have one at the moment. */
3030 fd
= emacs_open (name
, O_RDWR
| O_IGNORE_CTTY
| O_NOCTTY
, 0);
3032 #endif /* O_IGNORE_CTTY */
3033 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3034 defined on Hurd. On other systems, we need to explicitly
3035 dissociate ourselves from the controlling tty when we want to
3036 open a frame on the same terminal. */
3037 fd
= emacs_open (name
, O_RDWR
| O_NOCTTY
, 0);
3039 tty
->name
= xstrdup (name
);
3040 terminal
->name
= xstrdup (name
);
3043 maybe_fatal (must_succeed
, terminal
,
3044 "Could not open file: %s",
3045 "Could not open file: %s",
3050 maybe_fatal (must_succeed
, terminal
,
3051 "Not a tty device: %s",
3052 "Not a tty device: %s",
3056 #ifndef O_IGNORE_CTTY
3058 dissociate_if_controlling_tty (fd
);
3061 file
= fdopen (fd
, "w+");
3066 tty
->type
= xstrdup (terminal_type
);
3068 add_keyboard_wait_descriptor (fileno (tty
->input
));
3072 tty
->termcap_term_buffer
= xmalloc (buffer_size
);
3074 /* On some systems, tgetent tries to access the controlling
3078 sigemptyset (&blocked
);
3079 sigaddset (&blocked
, SIGTTOU
);
3080 pthread_sigmask (SIG_BLOCK
, &blocked
, 0);
3081 status
= tgetent (tty
->termcap_term_buffer
, terminal_type
);
3082 pthread_sigmask (SIG_UNBLOCK
, &blocked
, 0);
3088 maybe_fatal (must_succeed
, terminal
,
3089 "Cannot open terminfo database file",
3090 "Cannot open terminfo database file");
3092 maybe_fatal (must_succeed
, terminal
,
3093 "Cannot open termcap database file",
3094 "Cannot open termcap database file");
3099 maybe_fatal (must_succeed
, terminal
,
3100 "Terminal type %s is not defined",
3101 "Terminal type %s is not defined.\n\
3102 If that is not the actual type of terminal you have,\n\
3103 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3104 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3106 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3108 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3114 if (strlen (tty
->termcap_term_buffer
) >= buffer_size
)
3116 buffer_size
= strlen (tty
->termcap_term_buffer
);
3118 tty
->termcap_strings_buffer
= area
= xmalloc (buffer_size
);
3119 tty
->TS_ins_line
= tgetstr ("al", address
);
3120 tty
->TS_ins_multi_lines
= tgetstr ("AL", address
);
3121 tty
->TS_bell
= tgetstr ("bl", address
);
3122 BackTab (tty
) = tgetstr ("bt", address
);
3123 tty
->TS_clr_to_bottom
= tgetstr ("cd", address
);
3124 tty
->TS_clr_line
= tgetstr ("ce", address
);
3125 tty
->TS_clr_frame
= tgetstr ("cl", address
);
3126 ColPosition (tty
) = NULL
; /* tgetstr ("ch", address); */
3127 AbsPosition (tty
) = tgetstr ("cm", address
);
3128 CR (tty
) = tgetstr ("cr", address
);
3129 tty
->TS_set_scroll_region
= tgetstr ("cs", address
);
3130 tty
->TS_set_scroll_region_1
= tgetstr ("cS", address
);
3131 RowPosition (tty
) = tgetstr ("cv", address
);
3132 tty
->TS_del_char
= tgetstr ("dc", address
);
3133 tty
->TS_del_multi_chars
= tgetstr ("DC", address
);
3134 tty
->TS_del_line
= tgetstr ("dl", address
);
3135 tty
->TS_del_multi_lines
= tgetstr ("DL", address
);
3136 tty
->TS_delete_mode
= tgetstr ("dm", address
);
3137 tty
->TS_end_delete_mode
= tgetstr ("ed", address
);
3138 tty
->TS_end_insert_mode
= tgetstr ("ei", address
);
3139 Home (tty
) = tgetstr ("ho", address
);
3140 tty
->TS_ins_char
= tgetstr ("ic", address
);
3141 tty
->TS_ins_multi_chars
= tgetstr ("IC", address
);
3142 tty
->TS_insert_mode
= tgetstr ("im", address
);
3143 tty
->TS_pad_inserted_char
= tgetstr ("ip", address
);
3144 tty
->TS_end_keypad_mode
= tgetstr ("ke", address
);
3145 tty
->TS_keypad_mode
= tgetstr ("ks", address
);
3146 LastLine (tty
) = tgetstr ("ll", address
);
3147 Right (tty
) = tgetstr ("nd", address
);
3148 Down (tty
) = tgetstr ("do", address
);
3150 Down (tty
) = tgetstr ("nl", address
); /* Obsolete name for "do" */
3151 if (tgetflag ("bs"))
3152 Left (tty
) = "\b"; /* can't possibly be longer! */
3153 else /* (Actually, "bs" is obsolete...) */
3154 Left (tty
) = tgetstr ("le", address
);
3156 Left (tty
) = tgetstr ("bc", address
); /* Obsolete name for "le" */
3157 tty
->TS_pad_char
= tgetstr ("pc", address
);
3158 tty
->TS_repeat
= tgetstr ("rp", address
);
3159 tty
->TS_end_standout_mode
= tgetstr ("se", address
);
3160 tty
->TS_fwd_scroll
= tgetstr ("sf", address
);
3161 tty
->TS_standout_mode
= tgetstr ("so", address
);
3162 tty
->TS_rev_scroll
= tgetstr ("sr", address
);
3163 tty
->Wcm
->cm_tab
= tgetstr ("ta", address
);
3164 tty
->TS_end_termcap_modes
= tgetstr ("te", address
);
3165 tty
->TS_termcap_modes
= tgetstr ("ti", address
);
3166 Up (tty
) = tgetstr ("up", address
);
3167 tty
->TS_visible_bell
= tgetstr ("vb", address
);
3168 tty
->TS_cursor_normal
= tgetstr ("ve", address
);
3169 tty
->TS_cursor_visible
= tgetstr ("vs", address
);
3170 tty
->TS_cursor_invisible
= tgetstr ("vi", address
);
3171 tty
->TS_set_window
= tgetstr ("wi", address
);
3173 tty
->TS_enter_underline_mode
= tgetstr ("us", address
);
3174 tty
->TS_exit_underline_mode
= tgetstr ("ue", address
);
3175 tty
->TS_enter_bold_mode
= tgetstr ("md", address
);
3176 tty
->TS_enter_italic_mode
= tgetstr ("ZH", address
);
3177 tty
->TS_enter_dim_mode
= tgetstr ("mh", address
);
3178 tty
->TS_enter_reverse_mode
= tgetstr ("mr", address
);
3179 tty
->TS_enter_alt_charset_mode
= tgetstr ("as", address
);
3180 tty
->TS_exit_alt_charset_mode
= tgetstr ("ae", address
);
3181 tty
->TS_exit_attribute_mode
= tgetstr ("me", address
);
3183 MultiUp (tty
) = tgetstr ("UP", address
);
3184 MultiDown (tty
) = tgetstr ("DO", address
);
3185 MultiLeft (tty
) = tgetstr ("LE", address
);
3186 MultiRight (tty
) = tgetstr ("RI", address
);
3188 /* SVr4/ANSI color support. If "op" isn't available, don't support
3189 color because we can't switch back to the default foreground and
3191 tty
->TS_orig_pair
= tgetstr ("op", address
);
3192 if (tty
->TS_orig_pair
)
3194 tty
->TS_set_foreground
= tgetstr ("AF", address
);
3195 tty
->TS_set_background
= tgetstr ("AB", address
);
3196 if (!tty
->TS_set_foreground
)
3199 tty
->TS_set_foreground
= tgetstr ("Sf", address
);
3200 tty
->TS_set_background
= tgetstr ("Sb", address
);
3203 tty
->TN_max_colors
= tgetnum ("Co");
3204 tty
->TN_max_pairs
= tgetnum ("pa");
3206 tty
->TN_no_color_video
= tgetnum ("NC");
3207 if (tty
->TN_no_color_video
== -1)
3208 tty
->TN_no_color_video
= 0;
3211 tty_default_color_capabilities (tty
, 1);
3213 MagicWrap (tty
) = tgetflag ("xn");
3214 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3215 the former flag imply the latter. */
3216 AutoWrap (tty
) = MagicWrap (tty
) || tgetflag ("am");
3217 terminal
->memory_below_frame
= tgetflag ("db");
3218 tty
->TF_hazeltine
= tgetflag ("hz");
3219 terminal
->must_write_spaces
= tgetflag ("in");
3220 tty
->meta_key
= tgetflag ("km") || tgetflag ("MT");
3221 tty
->TF_insmode_motion
= tgetflag ("mi");
3222 tty
->TF_standout_motion
= tgetflag ("ms");
3223 tty
->TF_underscore
= tgetflag ("ul");
3224 tty
->TF_teleray
= tgetflag ("xt");
3229 struct frame
*f
= XFRAME (selected_frame
);
3231 initialize_w32_display (terminal
);
3233 FrameRows (tty
) = FRAME_LINES (f
);
3234 FrameCols (tty
) = FRAME_COLS (f
);
3235 tty
->specified_window
= FRAME_LINES (f
);
3237 FRAME_CAN_HAVE_SCROLL_BARS (f
) = 0;
3238 FRAME_VERTICAL_SCROLL_BAR_TYPE (f
) = vertical_scroll_bar_none
;
3239 terminal
->char_ins_del_ok
= 1;
3245 if (strcmp (terminal_type
, "internal") == 0)
3246 terminal
->type
= output_msdos_raw
;
3247 initialize_msdos_display (terminal
);
3249 get_tty_size (fileno (tty
->input
), &width
, &height
);
3250 FrameCols (tty
) = width
;
3251 FrameRows (tty
) = height
;
3252 terminal
->char_ins_del_ok
= 0;
3253 init_baud_rate (fileno (tty
->input
));
3256 tty
->output
= stdout
;
3258 /* The following two are inaccessible from w32console.c. */
3259 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
3260 terminal
->delete_terminal_hook
= &delete_tty
;
3262 tty
->name
= xstrdup (name
);
3263 terminal
->name
= xstrdup (name
);
3264 tty
->type
= xstrdup (terminal_type
);
3266 add_keyboard_wait_descriptor (0);
3268 tty
->delete_in_insert_mode
= 1;
3271 terminal
->scroll_region_ok
= 0;
3273 /* Seems to insert lines when it's not supposed to, messing up the
3274 display. In doing a trace, it didn't seem to be called much, so I
3275 don't think we're losing anything by turning it off. */
3276 terminal
->line_ins_del_ok
= 0;
3278 tty
->TN_max_colors
= 16; /* Required to be non-zero for tty-display-color-p */
3282 terminal
->mouse_position_hook
= term_mouse_position
;
3283 tty
->mouse_highlight
.mouse_face_window
= Qnil
;
3286 terminal
->kboard
= xmalloc (sizeof *terminal
->kboard
);
3287 init_kboard (terminal
->kboard
);
3288 kset_window_system (terminal
->kboard
, Qnil
);
3289 terminal
->kboard
->next_kboard
= all_kboards
;
3290 all_kboards
= terminal
->kboard
;
3291 terminal
->kboard
->reference_count
++;
3292 /* Don't let the initial kboard remain current longer than necessary.
3293 That would cause problems if a file loaded on startup tries to
3294 prompt in the mini-buffer. */
3295 if (current_kboard
== initial_kboard
)
3296 current_kboard
= terminal
->kboard
;
3298 term_get_fkeys (address
, terminal
->kboard
);
3300 /* Get frame size from system, or else from termcap. */
3303 get_tty_size (fileno (tty
->input
), &width
, &height
);
3304 FrameCols (tty
) = width
;
3305 FrameRows (tty
) = height
;
3308 if (FrameCols (tty
) <= 0)
3309 FrameCols (tty
) = tgetnum ("co");
3310 if (FrameRows (tty
) <= 0)
3311 FrameRows (tty
) = tgetnum ("li");
3313 if (FrameRows (tty
) < 3 || FrameCols (tty
) < 3)
3314 maybe_fatal (must_succeed
, terminal
,
3315 "Screen size %dx%d is too small",
3316 "Screen size %dx%d is too small",
3317 FrameCols (tty
), FrameRows (tty
));
3319 TabWidth (tty
) = tgetnum ("tw");
3322 tty
->TS_bell
= "\07";
3324 if (!tty
->TS_fwd_scroll
)
3325 tty
->TS_fwd_scroll
= Down (tty
);
3327 PC
= tty
->TS_pad_char
? *tty
->TS_pad_char
: 0;
3329 if (TabWidth (tty
) < 0)
3332 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3333 and newer termcap doc does not seem to say there is a default.
3334 if (!tty->Wcm->cm_tab)
3335 tty->Wcm->cm_tab = "\t";
3338 /* We don't support standout modes that use `magic cookies', so
3339 turn off any that do. */
3340 if (tty
->TS_standout_mode
&& tgetnum ("sg") >= 0)
3342 tty
->TS_standout_mode
= 0;
3343 tty
->TS_end_standout_mode
= 0;
3345 if (tty
->TS_enter_underline_mode
&& tgetnum ("ug") >= 0)
3347 tty
->TS_enter_underline_mode
= 0;
3348 tty
->TS_exit_underline_mode
= 0;
3351 /* If there's no standout mode, try to use underlining instead. */
3352 if (tty
->TS_standout_mode
== 0)
3354 tty
->TS_standout_mode
= tty
->TS_enter_underline_mode
;
3355 tty
->TS_end_standout_mode
= tty
->TS_exit_underline_mode
;
3358 /* If no `se' string, try using a `me' string instead.
3359 If that fails, we can't use standout mode at all. */
3360 if (tty
->TS_end_standout_mode
== 0)
3362 char *s
= tgetstr ("me", address
);
3364 tty
->TS_end_standout_mode
= s
;
3366 tty
->TS_standout_mode
= 0;
3369 if (tty
->TF_teleray
)
3371 tty
->Wcm
->cm_tab
= 0;
3372 /* We can't support standout mode, because it uses magic cookies. */
3373 tty
->TS_standout_mode
= 0;
3374 /* But that means we cannot rely on ^M to go to column zero! */
3376 /* LF can't be trusted either -- can alter hpos */
3377 /* if move at column 0 thru a line with TS_standout_mode */
3381 tty
->specified_window
= FrameRows (tty
);
3383 if (Wcm_init (tty
) == -1) /* can't do cursor motion */
3385 maybe_fatal (must_succeed
, terminal
,
3386 "Terminal type \"%s\" is not powerful enough to run Emacs",
3387 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3388 It lacks the ability to position the cursor.\n\
3389 If that is not the actual type of terminal you have,\n\
3390 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3391 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3393 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3394 # else /* TERMCAP */
3395 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3396 # endif /* TERMINFO */
3400 if (FrameRows (tty
) <= 0 || FrameCols (tty
) <= 0)
3401 maybe_fatal (must_succeed
, terminal
,
3402 "Could not determine the frame size",
3403 "Could not determine the frame size");
3405 tty
->delete_in_insert_mode
3406 = tty
->TS_delete_mode
&& tty
->TS_insert_mode
3407 && !strcmp (tty
->TS_delete_mode
, tty
->TS_insert_mode
);
3409 tty
->se_is_so
= (tty
->TS_standout_mode
3410 && tty
->TS_end_standout_mode
3411 && !strcmp (tty
->TS_standout_mode
, tty
->TS_end_standout_mode
));
3413 UseTabs (tty
) = tabs_safe_p (fileno (tty
->input
)) && TabWidth (tty
) == 8;
3415 terminal
->scroll_region_ok
3417 && (tty
->TS_set_window
|| tty
->TS_set_scroll_region
|| tty
->TS_set_scroll_region_1
));
3419 terminal
->line_ins_del_ok
3420 = (((tty
->TS_ins_line
|| tty
->TS_ins_multi_lines
)
3421 && (tty
->TS_del_line
|| tty
->TS_del_multi_lines
))
3422 || (terminal
->scroll_region_ok
3423 && tty
->TS_fwd_scroll
&& tty
->TS_rev_scroll
));
3425 terminal
->char_ins_del_ok
3426 = ((tty
->TS_ins_char
|| tty
->TS_insert_mode
3427 || tty
->TS_pad_inserted_char
|| tty
->TS_ins_multi_chars
)
3428 && (tty
->TS_del_char
|| tty
->TS_del_multi_chars
));
3430 terminal
->fast_clear_end_of_line
= tty
->TS_clr_line
!= 0;
3432 init_baud_rate (fileno (tty
->input
));
3434 #endif /* not DOS_NT */
3436 /* Init system terminal modes (RAW or CBREAK, etc.). */
3437 init_sys_modes (tty
);
3444 vfatal (const char *str
, va_list ap
)
3446 fprintf (stderr
, "emacs: ");
3447 vfprintf (stderr
, str
, ap
);
3448 if (!(strlen (str
) > 0 && str
[strlen (str
) - 1] == '\n'))
3449 fprintf (stderr
, "\n");
3455 /* Auxiliary error-handling function for init_tty.
3456 Delete TERMINAL, then call error or fatal with str1 or str2,
3457 respectively, according to whether MUST_SUCCEED is zero or not. */
3460 maybe_fatal (int must_succeed
, struct terminal
*terminal
,
3461 const char *str1
, const char *str2
, ...)
3464 va_start (ap
, str2
);
3466 delete_tty (terminal
);
3478 fatal (const char *str
, ...)
3488 /* Delete the given tty terminal, closing all frames on it. */
3491 delete_tty (struct terminal
*terminal
)
3493 struct tty_display_info
*tty
;
3495 /* Protect against recursive calls. delete_frame in
3496 delete_terminal calls us back when it deletes our last frame. */
3497 if (!terminal
->name
)
3500 if (terminal
->type
!= output_termcap
)
3503 tty
= terminal
->display_info
.tty
;
3505 if (tty
== tty_list
)
3506 tty_list
= tty
->next
;
3509 struct tty_display_info
*p
;
3510 for (p
= tty_list
; p
&& p
->next
!= tty
; p
= p
->next
)
3514 /* This should not happen. */
3517 p
->next
= tty
->next
;
3521 /* reset_sys_modes needs a valid device, so this call needs to be
3522 before delete_terminal. */
3523 reset_sys_modes (tty
);
3525 delete_terminal (terminal
);
3532 delete_keyboard_wait_descriptor (fileno (tty
->input
));
3533 if (tty
->input
!= stdin
)
3534 fclose (tty
->input
);
3536 if (tty
->output
&& tty
->output
!= stdout
&& tty
->output
!= tty
->input
)
3537 fclose (tty
->output
);
3538 if (tty
->termscript
)
3539 fclose (tty
->termscript
);
3541 xfree (tty
->old_tty
);
3543 xfree (tty
->termcap_strings_buffer
);
3544 xfree (tty
->termcap_term_buffer
);
3552 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo
,
3553 doc
: /* Non-nil means the system uses terminfo rather than termcap.
3554 This variable can be used by terminal emulator packages. */);
3556 system_uses_terminfo
= 1;
3558 system_uses_terminfo
= 0;
3561 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions
,
3562 doc
: /* Functions run after suspending a tty.
3563 The functions are run with one argument, the terminal object to be suspended.
3564 See `suspend-tty'. */);
3565 Vsuspend_tty_functions
= Qnil
;
3568 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions
,
3569 doc
: /* Functions run after resuming a tty.
3570 The functions are run with one argument, the terminal object that was revived.
3571 See `resume-tty'. */);
3572 Vresume_tty_functions
= Qnil
;
3574 DEFVAR_BOOL ("visible-cursor", visible_cursor
,
3575 doc
: /* Non-nil means to make the cursor very visible.
3576 This only has an effect when running in a text terminal.
3577 What means \"very visible\" is up to your terminal. It may make the cursor
3578 bigger, or it may make it blink, or it may do nothing at all. */);
3581 defsubr (&Stty_display_color_p
);
3582 defsubr (&Stty_display_color_cells
);
3583 defsubr (&Stty_no_underline
);
3584 defsubr (&Stty_type
);
3585 defsubr (&Scontrolling_tty_p
);
3586 defsubr (&Stty_top_frame
);
3587 defsubr (&Ssuspend_tty
);
3588 defsubr (&Sresume_tty
);
3590 defsubr (&Sgpm_mouse_start
);
3591 defsubr (&Sgpm_mouse_stop
);
3592 #endif /* HAVE_GPM */
3595 default_orig_pair
= NULL
;
3596 default_set_foreground
= NULL
;
3597 default_set_background
= NULL
;
3598 #endif /* !DOS_NT */
3600 encode_terminal_src
= NULL
;
3601 encode_terminal_dst
= NULL
;