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>. */
36 #include "character.h"
40 #include "composite.h"
44 #include "termhooks.h"
45 #include "dispextern.h"
48 #include "blockinput.h"
49 #include "syssignal.h"
51 #include "intervals.h"
54 static int been_here
= -1;
70 /* The name of the default console device. */
72 #define DEV_TTY "CONOUT$"
74 #define DEV_TTY "/dev/tty"
77 static void tty_set_scroll_region (struct frame
*f
, int start
, int stop
);
78 static void turn_on_face (struct frame
*, int face_id
);
79 static void turn_off_face (struct frame
*, int face_id
);
80 static void tty_turn_off_highlight (struct tty_display_info
*);
81 static void tty_show_cursor (struct tty_display_info
*);
82 static void tty_hide_cursor (struct tty_display_info
*);
83 static void tty_background_highlight (struct tty_display_info
*tty
);
84 static struct terminal
*get_tty_terminal (Lisp_Object
, int);
85 static void clear_tty_hooks (struct terminal
*terminal
);
86 static void set_tty_hooks (struct terminal
*terminal
);
87 static void dissociate_if_controlling_tty (int fd
);
88 static void delete_tty (struct terminal
*);
89 static _Noreturn
void maybe_fatal (int must_succeed
, struct terminal
*terminal
,
90 const char *str1
, const char *str2
, ...)
91 ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
92 static _Noreturn
void vfatal (const char *str
, va_list ap
)
93 ATTRIBUTE_FORMAT_PRINTF (1, 0);
96 #define OUTPUT(tty, a) \
97 emacs_tputs ((tty), a, \
98 (int) (FRAME_LINES (XFRAME (selected_frame)) \
102 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
103 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
105 #define OUTPUT_IF(tty, a) \
111 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
113 /* Display space properties */
115 /* Chain of all tty device parameters. */
116 struct tty_display_info
*tty_list
;
118 /* Meaning of bits in no_color_video. Each bit set means that the
119 corresponding attribute cannot be combined with colors. */
123 NC_STANDOUT
= 1 << 0,
124 NC_UNDERLINE
= 1 << 1,
135 /* The largest frame width in any call to calculate_costs. */
137 static int max_frame_cols
;
139 /* Non-zero if we have dropped our controlling tty and therefore
140 should not open a frame on stdout. */
141 static int no_controlling_tty
;
146 #include <sys/fcntl.h>
148 /* The device for which we have enabled gpm support (or NULL). */
149 struct tty_display_info
*gpm_tty
= NULL
;
151 /* Last recorded mouse coordinates. */
152 static int last_mouse_x
, last_mouse_y
;
153 #endif /* HAVE_GPM */
155 /* Ring the bell on a tty. */
158 tty_ring_bell (struct frame
*f
)
160 struct tty_display_info
*tty
= FRAME_TTY (f
);
164 OUTPUT (tty
, (tty
->TS_visible_bell
&& visible_bell
165 ? tty
->TS_visible_bell
167 fflush (tty
->output
);
171 /* Set up termcap modes for Emacs. */
174 tty_set_terminal_modes (struct terminal
*terminal
)
176 struct tty_display_info
*tty
= terminal
->display_info
.tty
;
180 if (tty
->TS_termcap_modes
)
181 OUTPUT (tty
, tty
->TS_termcap_modes
);
184 /* Output enough newlines to scroll all the old screen contents
185 off the screen, so it won't be overwritten and lost. */
188 for (i
= 0; i
< FRAME_LINES (XFRAME (selected_frame
)); i
++)
192 OUTPUT_IF (tty
, visible_cursor
? tty
->TS_cursor_visible
: tty
->TS_cursor_normal
);
193 OUTPUT_IF (tty
, tty
->TS_keypad_mode
);
195 fflush (tty
->output
);
199 /* Reset termcap modes before exiting Emacs. */
202 tty_reset_terminal_modes (struct terminal
*terminal
)
204 struct tty_display_info
*tty
= terminal
->display_info
.tty
;
208 tty_turn_off_highlight (tty
);
209 tty_turn_off_insert (tty
);
210 OUTPUT_IF (tty
, tty
->TS_end_keypad_mode
);
211 OUTPUT_IF (tty
, tty
->TS_cursor_normal
);
212 OUTPUT_IF (tty
, tty
->TS_end_termcap_modes
);
213 OUTPUT_IF (tty
, tty
->TS_orig_pair
);
214 /* Output raw CR so kernel can track the cursor hpos. */
217 fflush (tty
->output
);
221 /* Flag the end of a display update on a termcap terminal. */
224 tty_update_end (struct frame
*f
)
226 struct tty_display_info
*tty
= FRAME_TTY (f
);
228 if (!XWINDOW (selected_window
)->cursor_off_p
)
229 tty_show_cursor (tty
);
230 tty_turn_off_insert (tty
);
231 tty_background_highlight (tty
);
234 /* The implementation of set_terminal_window for termcap frames. */
237 tty_set_terminal_window (struct frame
*f
, int size
)
239 struct tty_display_info
*tty
= FRAME_TTY (f
);
241 tty
->specified_window
= size
? size
: FRAME_LINES (f
);
242 if (FRAME_SCROLL_REGION_OK (f
))
243 tty_set_scroll_region (f
, 0, tty
->specified_window
);
247 tty_set_scroll_region (struct frame
*f
, int start
, int stop
)
250 struct tty_display_info
*tty
= FRAME_TTY (f
);
252 if (tty
->TS_set_scroll_region
)
253 buf
= tparam (tty
->TS_set_scroll_region
, 0, 0, start
, stop
- 1, 0, 0);
254 else if (tty
->TS_set_scroll_region_1
)
255 buf
= tparam (tty
->TS_set_scroll_region_1
, 0, 0,
256 FRAME_LINES (f
), start
,
257 FRAME_LINES (f
) - stop
,
260 buf
= tparam (tty
->TS_set_window
, 0, 0, start
, 0, stop
, FRAME_COLS (f
));
269 tty_turn_on_insert (struct tty_display_info
*tty
)
271 if (!tty
->insert_mode
)
272 OUTPUT (tty
, tty
->TS_insert_mode
);
273 tty
->insert_mode
= 1;
277 tty_turn_off_insert (struct tty_display_info
*tty
)
279 if (tty
->insert_mode
)
280 OUTPUT (tty
, tty
->TS_end_insert_mode
);
281 tty
->insert_mode
= 0;
284 /* Handle highlighting. */
287 tty_turn_off_highlight (struct tty_display_info
*tty
)
289 if (tty
->standout_mode
)
290 OUTPUT_IF (tty
, tty
->TS_end_standout_mode
);
291 tty
->standout_mode
= 0;
295 tty_turn_on_highlight (struct tty_display_info
*tty
)
297 if (!tty
->standout_mode
)
298 OUTPUT_IF (tty
, tty
->TS_standout_mode
);
299 tty
->standout_mode
= 1;
303 tty_toggle_highlight (struct tty_display_info
*tty
)
305 if (tty
->standout_mode
)
306 tty_turn_off_highlight (tty
);
308 tty_turn_on_highlight (tty
);
312 /* Make cursor invisible. */
315 tty_hide_cursor (struct tty_display_info
*tty
)
317 if (tty
->cursor_hidden
== 0)
319 tty
->cursor_hidden
= 1;
320 OUTPUT_IF (tty
, tty
->TS_cursor_invisible
);
325 /* Ensure that cursor is visible. */
328 tty_show_cursor (struct tty_display_info
*tty
)
330 if (tty
->cursor_hidden
)
332 tty
->cursor_hidden
= 0;
333 OUTPUT_IF (tty
, tty
->TS_cursor_normal
);
335 OUTPUT_IF (tty
, tty
->TS_cursor_visible
);
340 /* Set standout mode to the state it should be in for
341 empty space inside windows. What this is,
342 depends on the user option inverse-video. */
345 tty_background_highlight (struct tty_display_info
*tty
)
348 tty_turn_on_highlight (tty
);
350 tty_turn_off_highlight (tty
);
353 /* Set standout mode to the mode specified for the text to be output. */
356 tty_highlight_if_desired (struct tty_display_info
*tty
)
359 tty_turn_on_highlight (tty
);
361 tty_turn_off_highlight (tty
);
365 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
366 frame-relative coordinates. */
369 tty_cursor_to (struct frame
*f
, int vpos
, int hpos
)
371 struct tty_display_info
*tty
= FRAME_TTY (f
);
373 /* Detect the case where we are called from reset_sys_modes
374 and the costs have never been calculated. Do nothing. */
375 if (! tty
->costs_set
)
378 if (curY (tty
) == vpos
379 && curX (tty
) == hpos
)
381 if (!tty
->TF_standout_motion
)
382 tty_background_highlight (tty
);
383 if (!tty
->TF_insmode_motion
)
384 tty_turn_off_insert (tty
);
385 cmgoto (tty
, vpos
, hpos
);
388 /* Similar but don't take any account of the wasted characters. */
391 tty_raw_cursor_to (struct frame
*f
, int row
, int col
)
393 struct tty_display_info
*tty
= FRAME_TTY (f
);
395 if (curY (tty
) == row
396 && curX (tty
) == col
)
398 if (!tty
->TF_standout_motion
)
399 tty_background_highlight (tty
);
400 if (!tty
->TF_insmode_motion
)
401 tty_turn_off_insert (tty
);
402 cmgoto (tty
, row
, col
);
405 /* Erase operations */
407 /* Clear from cursor to end of frame on a termcap device. */
410 tty_clear_to_end (struct frame
*f
)
413 struct tty_display_info
*tty
= FRAME_TTY (f
);
415 if (tty
->TS_clr_to_bottom
)
417 tty_background_highlight (tty
);
418 OUTPUT (tty
, tty
->TS_clr_to_bottom
);
422 for (i
= curY (tty
); i
< FRAME_LINES (f
); i
++)
425 clear_end_of_line (f
, FRAME_COLS (f
));
430 /* Clear an entire termcap frame. */
433 tty_clear_frame (struct frame
*f
)
435 struct tty_display_info
*tty
= FRAME_TTY (f
);
437 if (tty
->TS_clr_frame
)
439 tty_background_highlight (tty
);
440 OUTPUT (tty
, tty
->TS_clr_frame
);
450 /* An implementation of clear_end_of_line for termcap frames.
452 Note that the cursor may be moved, on terminals lacking a `ce' string. */
455 tty_clear_end_of_line (struct frame
*f
, int first_unused_hpos
)
458 struct tty_display_info
*tty
= FRAME_TTY (f
);
460 /* Detect the case where we are called from reset_sys_modes
461 and the costs have never been calculated. Do nothing. */
462 if (! tty
->costs_set
)
465 if (curX (tty
) >= first_unused_hpos
)
467 tty_background_highlight (tty
);
468 if (tty
->TS_clr_line
)
470 OUTPUT1 (tty
, tty
->TS_clr_line
);
473 { /* have to do it the hard way */
474 tty_turn_off_insert (tty
);
476 /* Do not write in last row last col with Auto-wrap on. */
478 && curY (tty
) == FrameRows (tty
) - 1
479 && first_unused_hpos
== FrameCols (tty
))
482 for (i
= curX (tty
); i
< first_unused_hpos
; i
++)
485 fputc (' ', tty
->termscript
);
486 fputc (' ', tty
->output
);
488 cmplus (tty
, first_unused_hpos
- curX (tty
));
492 /* Buffers to store the source and result of code conversion for terminal. */
493 static unsigned char *encode_terminal_src
;
494 static unsigned char *encode_terminal_dst
;
495 /* Allocated sizes of the above buffers. */
496 static ptrdiff_t encode_terminal_src_size
;
497 static ptrdiff_t encode_terminal_dst_size
;
499 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
500 Set CODING->produced to the byte-length of the resulting byte
501 sequence, and return a pointer to that byte sequence. */
504 encode_terminal_code (struct glyph
*src
, int src_len
, struct coding_system
*coding
)
506 struct glyph
*src_end
= src
+ src_len
;
508 ptrdiff_t nchars
, nbytes
, required
;
509 ptrdiff_t tlen
= GLYPH_TABLE_LENGTH
;
510 register Lisp_Object
*tbase
= GLYPH_TABLE_BASE
;
511 Lisp_Object charset_list
;
513 /* Allocate sufficient size of buffer to store all characters in
514 multibyte-form. But, it may be enlarged on demand if
515 Vglyph_table contains a string or a composite glyph is
517 if (min (PTRDIFF_MAX
, SIZE_MAX
) / MAX_MULTIBYTE_LENGTH
< src_len
)
518 memory_full (SIZE_MAX
);
520 required
*= MAX_MULTIBYTE_LENGTH
;
521 if (encode_terminal_src_size
< required
)
523 encode_terminal_src
= xrealloc (encode_terminal_src
, required
);
524 encode_terminal_src_size
= required
;
527 charset_list
= coding_charset_list (coding
);
529 buf
= encode_terminal_src
;
531 while (src
< src_end
)
533 if (src
->type
== COMPOSITE_GLYPH
)
535 struct composition
*cmp
IF_LINT (= NULL
);
536 Lisp_Object gstring
IF_LINT (= Qnil
);
539 nbytes
= buf
- encode_terminal_src
;
540 if (src
->u
.cmp
.automatic
)
542 gstring
= composition_gstring_from_id (src
->u
.cmp
.id
);
543 required
= src
->slice
.cmp
.to
- src
->slice
.cmp
.from
+ 1;
547 cmp
= composition_table
[src
->u
.cmp
.id
];
548 required
= cmp
->glyph_len
;
549 required
*= MAX_MULTIBYTE_LENGTH
;
552 if (encode_terminal_src_size
- nbytes
< required
)
554 encode_terminal_src
=
555 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
556 required
- (encode_terminal_src_size
- nbytes
),
558 buf
= encode_terminal_src
+ nbytes
;
561 if (src
->u
.cmp
.automatic
)
562 for (i
= src
->slice
.cmp
.from
; i
<= src
->slice
.cmp
.to
; i
++)
564 Lisp_Object g
= LGSTRING_GLYPH (gstring
, i
);
565 int c
= LGLYPH_CHAR (g
);
567 if (! char_charset (c
, charset_list
, NULL
))
569 buf
+= CHAR_STRING (c
, buf
);
573 for (i
= 0; i
< cmp
->glyph_len
; i
++)
575 int c
= COMPOSITION_GLYPH (cmp
, i
);
577 /* TAB in a composition means display glyphs with
578 padding space on the left or right. */
581 if (char_charset (c
, charset_list
, NULL
))
583 if (CHAR_WIDTH (c
) == 0
584 && i
> 0 && COMPOSITION_GLYPH (cmp
, i
- 1) == '\t')
585 /* Should be left-padded */
587 buf
+= CHAR_STRING (' ', buf
);
593 buf
+= CHAR_STRING (c
, buf
);
597 /* We must skip glyphs to be padded for a wide character. */
598 else if (! CHAR_GLYPH_PADDING_P (*src
))
605 SET_GLYPH_FROM_CHAR_GLYPH (g
, src
[0]);
607 if (GLYPH_INVALID_P (g
) || GLYPH_SIMPLE_P (tbase
, tlen
, g
))
609 /* This glyph doesn't have an entry in Vglyph_table. */
614 /* This glyph has an entry in Vglyph_table,
615 so process any alias before testing for simpleness. */
616 GLYPH_FOLLOW_ALIASES (tbase
, tlen
, g
);
618 if (GLYPH_SIMPLE_P (tbase
, tlen
, g
))
619 /* We set the multi-byte form of a character in G
620 (that should be an ASCII character) at WORKBUF. */
623 /* We have a string in Vglyph_table. */
624 string
= tbase
[GLYPH_CHAR (g
)];
629 nbytes
= buf
- encode_terminal_src
;
630 if (encode_terminal_src_size
- nbytes
< MAX_MULTIBYTE_LENGTH
)
632 encode_terminal_src
=
633 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
634 MAX_MULTIBYTE_LENGTH
, -1, 1);
635 buf
= encode_terminal_src
+ nbytes
;
638 || char_charset (c
, charset_list
, NULL
))
640 /* Store the multibyte form of C at BUF. */
641 buf
+= CHAR_STRING (c
, buf
);
646 /* C is not encodable. */
649 while (src
+ 1 < src_end
&& CHAR_GLYPH_PADDING_P (src
[1]))
659 if (! STRING_MULTIBYTE (string
))
660 string
= string_to_multibyte (string
);
661 nbytes
= buf
- encode_terminal_src
;
662 if (encode_terminal_src_size
- nbytes
< SBYTES (string
))
664 encode_terminal_src
=
665 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
667 - (encode_terminal_src_size
- nbytes
)),
669 buf
= encode_terminal_src
+ nbytes
;
671 memcpy (buf
, SDATA (string
), SBYTES (string
));
672 buf
+= SBYTES (string
);
673 nchars
+= SCHARS (string
);
681 coding
->produced
= 0;
685 nbytes
= buf
- encode_terminal_src
;
686 coding
->source
= encode_terminal_src
;
687 if (encode_terminal_dst_size
== 0)
689 encode_terminal_dst
= xrealloc (encode_terminal_dst
,
690 encode_terminal_src_size
);
691 encode_terminal_dst_size
= encode_terminal_src_size
;
693 coding
->destination
= encode_terminal_dst
;
694 coding
->dst_bytes
= encode_terminal_dst_size
;
695 encode_coding_object (coding
, Qnil
, 0, 0, nchars
, nbytes
, Qnil
);
696 /* coding->destination may have been reallocated. */
697 encode_terminal_dst
= coding
->destination
;
698 encode_terminal_dst_size
= coding
->dst_bytes
;
700 return (encode_terminal_dst
);
705 /* An implementation of write_glyphs for termcap frames. */
708 tty_write_glyphs (struct frame
*f
, struct glyph
*string
, int len
)
710 unsigned char *conversion_buffer
;
711 struct coding_system
*coding
;
714 struct tty_display_info
*tty
= FRAME_TTY (f
);
716 tty_turn_off_insert (tty
);
717 tty_hide_cursor (tty
);
719 /* Don't dare write in last column of bottom line, if Auto-Wrap,
720 since that would scroll the whole frame on some terminals. */
723 && curY (tty
) + 1 == FRAME_LINES (f
)
724 && (curX (tty
) + len
) == FRAME_COLS (f
))
731 /* If terminal_coding does any conversion, use it, otherwise use
732 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
733 because it always return 1 if the member src_multibyte is 1. */
734 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
735 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
736 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
738 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
740 for (stringlen
= len
; stringlen
!= 0; stringlen
-= n
)
742 /* Identify a run of glyphs with the same face. */
743 int face_id
= string
->face_id
;
745 for (n
= 1; n
< stringlen
; ++n
)
746 if (string
[n
].face_id
!= face_id
)
749 /* Turn appearance modes of the face of the run on. */
750 tty_highlight_if_desired (tty
);
751 turn_on_face (f
, face_id
);
754 /* This is the last run. */
755 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
756 conversion_buffer
= encode_terminal_code (string
, n
, coding
);
757 if (coding
->produced
> 0)
760 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
761 if (ferror (tty
->output
))
762 clearerr (tty
->output
);
764 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
769 /* Turn appearance modes off. */
770 turn_off_face (f
, face_id
);
771 tty_turn_off_highlight (tty
);
777 #ifdef HAVE_GPM /* Only used by GPM code. */
780 tty_write_glyphs_with_face (register struct frame
*f
, register struct glyph
*string
,
781 register int len
, register int face_id
)
783 unsigned char *conversion_buffer
;
784 struct coding_system
*coding
;
786 struct tty_display_info
*tty
= FRAME_TTY (f
);
788 tty_turn_off_insert (tty
);
789 tty_hide_cursor (tty
);
791 /* Don't dare write in last column of bottom line, if Auto-Wrap,
792 since that would scroll the whole frame on some terminals. */
795 && curY (tty
) + 1 == FRAME_LINES (f
)
796 && (curX (tty
) + len
) == FRAME_COLS (f
))
803 /* If terminal_coding does any conversion, use it, otherwise use
804 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
805 because it always return 1 if the member src_multibyte is 1. */
806 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
807 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
808 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
810 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
812 /* Turn appearance modes of the face. */
813 tty_highlight_if_desired (tty
);
814 turn_on_face (f
, face_id
);
816 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
817 conversion_buffer
= encode_terminal_code (string
, len
, coding
);
818 if (coding
->produced
> 0)
821 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
822 if (ferror (tty
->output
))
823 clearerr (tty
->output
);
825 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
829 /* Turn appearance modes off. */
830 turn_off_face (f
, face_id
);
831 tty_turn_off_highlight (tty
);
837 /* An implementation of insert_glyphs for termcap frames. */
840 tty_insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
)
843 struct glyph
*glyph
= NULL
;
844 unsigned char *conversion_buffer
;
845 unsigned char space
[1];
846 struct coding_system
*coding
;
848 struct tty_display_info
*tty
= FRAME_TTY (f
);
850 if (tty
->TS_ins_multi_chars
)
852 buf
= tparam (tty
->TS_ins_multi_chars
, 0, 0, len
, 0, 0, 0);
856 write_glyphs (f
, start
, len
);
860 tty_turn_on_insert (tty
);
864 space
[0] = SPACEGLYPH
;
866 /* If terminal_coding does any conversion, use it, otherwise use
867 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
868 because it always return 1 if the member src_multibyte is 1. */
869 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
870 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
871 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
873 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
877 OUTPUT1_IF (tty
, tty
->TS_ins_char
);
880 conversion_buffer
= space
;
881 coding
->produced
= 1;
885 tty_highlight_if_desired (tty
);
886 turn_on_face (f
, start
->face_id
);
889 /* We must open sufficient space for a character which
890 occupies more than one column. */
891 while (len
&& CHAR_GLYPH_PADDING_P (*start
))
893 OUTPUT1_IF (tty
, tty
->TS_ins_char
);
898 /* This is the last glyph. */
899 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
901 conversion_buffer
= encode_terminal_code (glyph
, 1, coding
);
904 if (coding
->produced
> 0)
907 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
908 if (ferror (tty
->output
))
909 clearerr (tty
->output
);
911 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
915 OUTPUT1_IF (tty
, tty
->TS_pad_inserted_char
);
918 turn_off_face (f
, glyph
->face_id
);
919 tty_turn_off_highlight (tty
);
926 /* An implementation of delete_glyphs for termcap frames. */
929 tty_delete_glyphs (struct frame
*f
, int n
)
934 struct tty_display_info
*tty
= FRAME_TTY (f
);
936 if (tty
->delete_in_insert_mode
)
938 tty_turn_on_insert (tty
);
942 tty_turn_off_insert (tty
);
943 OUTPUT_IF (tty
, tty
->TS_delete_mode
);
946 if (tty
->TS_del_multi_chars
)
948 buf
= tparam (tty
->TS_del_multi_chars
, 0, 0, n
, 0, 0, 0);
953 for (i
= 0; i
< n
; i
++)
954 OUTPUT1 (tty
, tty
->TS_del_char
);
955 if (!tty
->delete_in_insert_mode
)
956 OUTPUT_IF (tty
, tty
->TS_end_delete_mode
);
959 /* An implementation of ins_del_lines for termcap frames. */
962 tty_ins_del_lines (struct frame
*f
, int vpos
, int n
)
964 struct tty_display_info
*tty
= FRAME_TTY (f
);
966 n
> 0 ? tty
->TS_ins_multi_lines
: tty
->TS_del_multi_lines
;
967 const char *single
= n
> 0 ? tty
->TS_ins_line
: tty
->TS_del_line
;
968 const char *scroll
= n
> 0 ? tty
->TS_rev_scroll
: tty
->TS_fwd_scroll
;
970 register int i
= n
> 0 ? n
: -n
;
973 /* If the lines below the insertion are being pushed
974 into the end of the window, this is the same as clearing;
975 and we know the lines are already clear, since the matching
976 deletion has already been done. So can ignore this. */
977 /* If the lines below the deletion are blank lines coming
978 out of the end of the window, don't bother,
979 as there will be a matching inslines later that will flush them. */
980 if (FRAME_SCROLL_REGION_OK (f
)
981 && vpos
+ i
>= tty
->specified_window
)
983 if (!FRAME_MEMORY_BELOW_FRAME (f
)
984 && vpos
+ i
>= FRAME_LINES (f
))
989 raw_cursor_to (f
, vpos
, 0);
990 tty_background_highlight (tty
);
991 buf
= tparam (multi
, 0, 0, i
, 0, 0, 0);
997 raw_cursor_to (f
, vpos
, 0);
998 tty_background_highlight (tty
);
1000 OUTPUT (tty
, single
);
1001 if (tty
->TF_teleray
)
1006 tty_set_scroll_region (f
, vpos
, tty
->specified_window
);
1008 raw_cursor_to (f
, tty
->specified_window
- 1, 0);
1010 raw_cursor_to (f
, vpos
, 0);
1011 tty_background_highlight (tty
);
1013 OUTPUTL (tty
, scroll
, tty
->specified_window
- vpos
);
1014 tty_set_scroll_region (f
, 0, tty
->specified_window
);
1017 if (!FRAME_SCROLL_REGION_OK (f
)
1018 && FRAME_MEMORY_BELOW_FRAME (f
)
1021 cursor_to (f
, FRAME_LINES (f
) + n
, 0);
1026 /* Compute cost of sending "str", in characters,
1027 not counting any line-dependent padding. */
1030 string_cost (const char *str
)
1034 tputs (str
, 0, evalcost
);
1038 /* Compute cost of sending "str", in characters,
1039 counting any line-dependent padding at one line. */
1042 string_cost_one_line (const char *str
)
1046 tputs (str
, 1, evalcost
);
1050 /* Compute per line amount of line-dependent padding,
1051 in tenths of characters. */
1054 per_line_cost (const char *str
)
1058 tputs (str
, 0, evalcost
);
1061 tputs (str
, 10, evalcost
);
1065 /* char_ins_del_cost[n] is cost of inserting N characters.
1066 char_ins_del_cost[-n] is cost of deleting N characters.
1067 The length of this vector is based on max_frame_cols. */
1069 int *char_ins_del_vector
;
1071 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1075 calculate_ins_del_char_costs (struct frame
*f
)
1077 struct tty_display_info
*tty
= FRAME_TTY (f
);
1078 int ins_startup_cost
, del_startup_cost
;
1079 int ins_cost_per_char
, del_cost_per_char
;
1083 if (tty
->TS_ins_multi_chars
)
1085 ins_cost_per_char
= 0;
1086 ins_startup_cost
= string_cost_one_line (tty
->TS_ins_multi_chars
);
1088 else if (tty
->TS_ins_char
|| tty
->TS_pad_inserted_char
1089 || (tty
->TS_insert_mode
&& tty
->TS_end_insert_mode
))
1091 ins_startup_cost
= (30 * (string_cost (tty
->TS_insert_mode
)
1092 + string_cost (tty
->TS_end_insert_mode
))) / 100;
1093 ins_cost_per_char
= (string_cost_one_line (tty
->TS_ins_char
)
1094 + string_cost_one_line (tty
->TS_pad_inserted_char
));
1098 ins_startup_cost
= 9999;
1099 ins_cost_per_char
= 0;
1102 if (tty
->TS_del_multi_chars
)
1104 del_cost_per_char
= 0;
1105 del_startup_cost
= string_cost_one_line (tty
->TS_del_multi_chars
);
1107 else if (tty
->TS_del_char
)
1109 del_startup_cost
= (string_cost (tty
->TS_delete_mode
)
1110 + string_cost (tty
->TS_end_delete_mode
));
1111 if (tty
->delete_in_insert_mode
)
1112 del_startup_cost
/= 2;
1113 del_cost_per_char
= string_cost_one_line (tty
->TS_del_char
);
1117 del_startup_cost
= 9999;
1118 del_cost_per_char
= 0;
1121 /* Delete costs are at negative offsets */
1122 p
= &char_ins_del_cost (f
)[0];
1123 for (i
= FRAME_COLS (f
); --i
>= 0;)
1124 *--p
= (del_startup_cost
+= del_cost_per_char
);
1126 /* Doing nothing is free */
1127 p
= &char_ins_del_cost (f
)[0];
1130 /* Insert costs are at positive offsets */
1131 for (i
= FRAME_COLS (f
); --i
>= 0;)
1132 *p
++ = (ins_startup_cost
+= ins_cost_per_char
);
1136 calculate_costs (struct frame
*frame
)
1138 FRAME_COST_BAUD_RATE (frame
) = baud_rate
;
1140 if (FRAME_TERMCAP_P (frame
))
1142 struct tty_display_info
*tty
= FRAME_TTY (frame
);
1143 register const char *f
= (tty
->TS_set_scroll_region
1144 ? tty
->TS_set_scroll_region
1145 : tty
->TS_set_scroll_region_1
);
1147 FRAME_SCROLL_REGION_COST (frame
) = string_cost (f
);
1151 /* These variables are only used for terminal stuff. They are
1152 allocated once for the terminal frame of X-windows emacs, but not
1155 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1156 X turns off char_ins_del_ok. */
1158 max_frame_cols
= max (max_frame_cols
, FRAME_COLS (frame
));
1159 if ((min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (int) - 1) / 2
1161 memory_full (SIZE_MAX
);
1163 char_ins_del_vector
=
1164 xrealloc (char_ins_del_vector
,
1165 (sizeof (int) + 2 * sizeof (int) * max_frame_cols
));
1167 memset (char_ins_del_vector
, 0,
1168 (sizeof (int) + 2 * sizeof (int) * max_frame_cols
));
1171 if (f
&& (!tty
->TS_ins_line
&& !tty
->TS_del_line
))
1172 do_line_insertion_deletion_costs (frame
,
1173 tty
->TS_rev_scroll
, tty
->TS_ins_multi_lines
,
1174 tty
->TS_fwd_scroll
, tty
->TS_del_multi_lines
,
1177 do_line_insertion_deletion_costs (frame
,
1178 tty
->TS_ins_line
, tty
->TS_ins_multi_lines
,
1179 tty
->TS_del_line
, tty
->TS_del_multi_lines
,
1182 calculate_ins_del_char_costs (frame
);
1184 /* Don't use TS_repeat if its padding is worse than sending the chars */
1185 if (tty
->TS_repeat
&& per_line_cost (tty
->TS_repeat
) * baud_rate
< 9000)
1186 tty
->RPov
= string_cost (tty
->TS_repeat
);
1188 tty
->RPov
= FRAME_COLS (frame
) * 2;
1190 cmcostinit (FRAME_TTY (frame
)); /* set up cursor motion costs */
1195 const char *cap
, *name
;
1198 /* Termcap capability names that correspond directly to X keysyms.
1199 Some of these (marked "terminfo") aren't supplied by old-style
1200 (Berkeley) termcap entries. They're listed in X keysym order;
1201 except we put the keypad keys first, so that if they clash with
1202 other keys (as on the IBM PC keyboard) they get overridden.
1205 static const struct fkey_table keys
[] =
1207 {"kh", "home"}, /* termcap */
1208 {"kl", "left"}, /* termcap */
1209 {"ku", "up"}, /* termcap */
1210 {"kr", "right"}, /* termcap */
1211 {"kd", "down"}, /* termcap */
1212 {"%8", "prior"}, /* terminfo */
1213 {"%5", "next"}, /* terminfo */
1214 {"@7", "end"}, /* terminfo */
1215 {"@1", "begin"}, /* terminfo */
1216 {"*6", "select"}, /* terminfo */
1217 {"%9", "print"}, /* terminfo */
1218 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1220 * "insert" --- see below
1222 {"&8", "undo"}, /* terminfo */
1223 {"%0", "redo"}, /* terminfo */
1224 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1225 {"@0", "find"}, /* terminfo */
1226 {"@2", "cancel"}, /* terminfo */
1227 {"%1", "help"}, /* terminfo */
1229 * "break" goes here, but can't be reliably intercepted with termcap
1231 {"&4", "reset"}, /* terminfo --- actually `restart' */
1233 * "system" and "user" --- no termcaps
1235 {"kE", "clearline"}, /* terminfo */
1236 {"kA", "insertline"}, /* terminfo */
1237 {"kL", "deleteline"}, /* terminfo */
1238 {"kI", "insertchar"}, /* terminfo */
1239 {"kD", "deletechar"}, /* terminfo */
1240 {"kB", "backtab"}, /* terminfo */
1242 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1244 {"@8", "kp-enter"}, /* terminfo */
1246 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1247 * "kp-multiply", "kp-add", "kp-separator",
1248 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1249 * --- no termcaps for any of these.
1251 {"K4", "kp-1"}, /* terminfo */
1253 * "kp-2" --- no termcap
1255 {"K5", "kp-3"}, /* terminfo */
1257 * "kp-4" --- no termcap
1259 {"K2", "kp-5"}, /* terminfo */
1261 * "kp-6" --- no termcap
1263 {"K1", "kp-7"}, /* terminfo */
1265 * "kp-8" --- no termcap
1267 {"K3", "kp-9"}, /* terminfo */
1269 * "kp-equal" --- no termcap
1281 {"&0", "S-cancel"}, /*shifted cancel key*/
1282 {"&9", "S-begin"}, /*shifted begin key*/
1283 {"*0", "S-find"}, /*shifted find key*/
1284 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1285 {"*4", "S-delete"}, /*shifted delete-character key*/
1286 {"*7", "S-end"}, /*shifted end key*/
1287 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1288 {"#1", "S-help"}, /*shifted help key*/
1289 {"#2", "S-home"}, /*shifted home key*/
1290 {"#3", "S-insert"}, /*shifted insert-character key*/
1291 {"#4", "S-left"}, /*shifted left-arrow key*/
1292 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1293 {"%c", "S-next"}, /*shifted next key*/
1294 {"%e", "S-prior"}, /*shifted previous key*/
1295 {"%f", "S-print"}, /*shifted print key*/
1296 {"%g", "S-redo"}, /*shifted redo key*/
1297 {"%i", "S-right"}, /*shifted right-arrow key*/
1298 {"!3", "S-undo"} /*shifted undo key*/
1302 static char **term_get_fkeys_address
;
1303 static KBOARD
*term_get_fkeys_kboard
;
1304 static Lisp_Object
term_get_fkeys_1 (void);
1306 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1307 This function scans the termcap function key sequence entries, and
1308 adds entries to Vinput_decode_map for each function key it finds. */
1311 term_get_fkeys (char **address
, KBOARD
*kboard
)
1313 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1314 errors during the call. The only errors should be from Fdefine_key
1315 when given a key sequence containing an invalid prefix key. If the
1316 termcap defines function keys which use a prefix that is already bound
1317 to a command by the default bindings, we should silently ignore that
1318 function key specification, rather than giving the user an error and
1319 refusing to run at all on such a terminal. */
1321 term_get_fkeys_address
= address
;
1322 term_get_fkeys_kboard
= kboard
;
1323 internal_condition_case (term_get_fkeys_1
, Qerror
, Fidentity
);
1327 term_get_fkeys_1 (void)
1331 char **address
= term_get_fkeys_address
;
1332 KBOARD
*kboard
= term_get_fkeys_kboard
;
1334 /* This can happen if CANNOT_DUMP or with strange options. */
1335 if (!KEYMAPP (KVAR (kboard
, Vinput_decode_map
)))
1336 KVAR (kboard
, Vinput_decode_map
) = Fmake_sparse_keymap (Qnil
);
1338 for (i
= 0; i
< (sizeof (keys
)/sizeof (keys
[0])); i
++)
1340 char *sequence
= tgetstr (keys
[i
].cap
, address
);
1342 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (sequence
),
1343 Fmake_vector (make_number (1),
1344 intern (keys
[i
].name
)));
1347 /* The uses of the "k0" capability are inconsistent; sometimes it
1348 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1349 We will attempt to politely accommodate both systems by testing for
1350 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1353 const char *k_semi
= tgetstr ("k;", address
);
1354 const char *k0
= tgetstr ("k0", address
);
1355 const char *k0_name
= "f10";
1360 /* Define f0 first, so that f10 takes precedence in case the
1361 key sequences happens to be the same. */
1362 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k0
),
1363 Fmake_vector (make_number (1), intern ("f0")));
1364 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k_semi
),
1365 Fmake_vector (make_number (1), intern ("f10")));
1368 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k0
),
1369 Fmake_vector (make_number (1), intern (k0_name
)));
1372 /* Set up cookies for numbered function keys above f10. */
1374 char fcap
[3], fkey
[4];
1376 fcap
[0] = 'F'; fcap
[2] = '\0';
1377 for (i
= 11; i
< 64; i
++)
1380 fcap
[1] = '1' + i
- 11;
1382 fcap
[1] = 'A' + i
- 20;
1384 fcap
[1] = 'a' + i
- 46;
1387 char *sequence
= tgetstr (fcap
, address
);
1390 sprintf (fkey
, "f%d", i
);
1391 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (sequence
),
1392 Fmake_vector (make_number (1),
1400 * Various mappings to try and get a better fit.
1403 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1404 if (!tgetstr (cap1, address)) \
1406 char *sequence = tgetstr (cap2, address); \
1408 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1409 Fmake_vector (make_number (1), \
1413 /* if there's no key_next keycap, map key_npage to `next' keysym */
1414 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1415 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1416 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1417 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1418 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1419 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1420 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1422 /* IBM has their own non-standard dialect of terminfo.
1423 If the standard name isn't found, try the IBM name. */
1424 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1425 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1426 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1427 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1428 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1429 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1430 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1431 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1432 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1433 #undef CONDITIONAL_REASSIGN
1438 #endif /* not DOS_NT */
1441 /***********************************************************************
1442 Character Display Information
1443 ***********************************************************************/
1444 static void append_glyph (struct it
*);
1445 static void append_composite_glyph (struct it
*);
1446 static void produce_composite_glyph (struct it
*);
1447 static void append_glyphless_glyph (struct it
*, int, const char *);
1448 static void produce_glyphless_glyph (struct it
*, int, Lisp_Object
);
1450 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1451 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1452 the character for which to produce glyphs; IT->face_id contains the
1453 character's face. Padding glyphs are appended if IT->c has a
1454 IT->pixel_width > 1. */
1457 append_glyph (struct it
*it
)
1459 struct glyph
*glyph
, *end
;
1462 xassert (it
->glyph_row
);
1463 glyph
= (it
->glyph_row
->glyphs
[it
->area
]
1464 + it
->glyph_row
->used
[it
->area
]);
1465 end
= it
->glyph_row
->glyphs
[1 + it
->area
];
1467 /* If the glyph row is reversed, we need to prepend the glyph rather
1469 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1472 int move_by
= it
->pixel_width
;
1474 /* Make room for the new glyphs. */
1475 if (move_by
> end
- glyph
) /* don't overstep end of this area */
1476 move_by
= end
- glyph
;
1477 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1479 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1480 end
= glyph
+ move_by
;
1483 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1484 right, even in the REVERSED_P case, since (a) all of its u.ch are
1485 identical, and (b) the PADDING_P flag needs to be set for the
1486 leftmost one, because we write to the terminal left-to-right. */
1488 i
< it
->pixel_width
&& glyph
< end
;
1491 glyph
->type
= CHAR_GLYPH
;
1492 glyph
->pixel_width
= 1;
1493 glyph
->u
.ch
= it
->char_to_display
;
1494 glyph
->face_id
= it
->face_id
;
1495 glyph
->padding_p
= i
> 0;
1496 glyph
->charpos
= CHARPOS (it
->position
);
1497 glyph
->object
= it
->object
;
1500 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1501 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1503 glyph
->bidi_type
= it
->bidi_it
.type
;
1507 glyph
->resolved_level
= 0;
1508 glyph
->bidi_type
= UNKNOWN_BT
;
1511 ++it
->glyph_row
->used
[it
->area
];
1516 /* For external use. */
1518 tty_append_glyph (struct it
*it
)
1524 /* Produce glyphs for the display element described by IT. *IT
1525 specifies what we want to produce a glyph for (character, image, ...),
1526 and where in the glyph matrix we currently are (glyph row and hpos).
1527 produce_glyphs fills in output fields of *IT with information such as the
1528 pixel width and height of a character, and maybe output actual glyphs at
1529 the same time if IT->glyph_row is non-null. For an overview, see
1530 the explanation in dispextern.h, before the definition of the
1531 display_element_type enumeration.
1533 produce_glyphs also stores the result of glyph width, ascent
1534 etc. computations in *IT.
1536 IT->glyph_row may be null, in which case produce_glyphs does not
1537 actually fill in the glyphs. This is used in the move_* functions
1538 in xdisp.c for text width and height computations.
1540 Callers usually don't call produce_glyphs directly;
1541 instead they use the macro PRODUCE_GLYPHS. */
1544 produce_glyphs (struct it
*it
)
1546 /* If a hook is installed, let it do the work. */
1548 /* Nothing but characters are supported on terminal frames. */
1549 xassert (it
->what
== IT_CHARACTER
1550 || it
->what
== IT_COMPOSITION
1551 || it
->what
== IT_STRETCH
1552 || it
->what
== IT_GLYPHLESS
);
1554 if (it
->what
== IT_STRETCH
)
1556 produce_stretch_glyph (it
);
1560 if (it
->what
== IT_COMPOSITION
)
1562 produce_composite_glyph (it
);
1566 if (it
->what
== IT_GLYPHLESS
)
1568 produce_glyphless_glyph (it
, 0, Qnil
);
1572 if (it
->char_to_display
>= 040 && it
->char_to_display
< 0177)
1574 it
->pixel_width
= it
->nglyphs
= 1;
1578 else if (it
->char_to_display
== '\n')
1579 it
->pixel_width
= it
->nglyphs
= 0;
1580 else if (it
->char_to_display
== '\t')
1582 int absolute_x
= (it
->current_x
1583 + it
->continuation_lines_width
);
1585 = (((1 + absolute_x
+ it
->tab_width
- 1)
1590 /* If part of the TAB has been displayed on the previous line
1591 which is continued now, continuation_lines_width will have
1592 been incremented already by the part that fitted on the
1593 continued line. So, we will get the right number of spaces
1595 nspaces
= next_tab_x
- absolute_x
;
1601 it
->char_to_display
= ' ';
1602 it
->pixel_width
= it
->len
= 1;
1608 it
->pixel_width
= nspaces
;
1609 it
->nglyphs
= nspaces
;
1611 else if (CHAR_BYTE8_P (it
->char_to_display
))
1613 /* Coming here means that we must send the raw 8-bit byte as is
1614 to the terminal. Although there's no way to know how many
1615 columns it occupies on a screen, it is a good assumption that
1616 a single byte code has 1-column width. */
1617 it
->pixel_width
= it
->nglyphs
= 1;
1623 Lisp_Object charset_list
= FRAME_TERMINAL (it
->f
)->charset_list
;
1625 if (char_charset (it
->char_to_display
, charset_list
, NULL
))
1627 it
->pixel_width
= CHAR_WIDTH (it
->char_to_display
);
1628 it
->nglyphs
= it
->pixel_width
;
1634 Lisp_Object acronym
= lookup_glyphless_char_display (-1, it
);
1636 xassert (it
->what
== IT_GLYPHLESS
);
1637 produce_glyphless_glyph (it
, 1, acronym
);
1642 /* Advance current_x by the pixel width as a convenience for
1644 if (it
->area
== TEXT_AREA
)
1645 it
->current_x
+= it
->pixel_width
;
1646 it
->ascent
= it
->max_ascent
= it
->phys_ascent
= it
->max_phys_ascent
= 0;
1647 it
->descent
= it
->max_descent
= it
->phys_descent
= it
->max_phys_descent
= 1;
1650 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1651 Called from produce_composite_glyph for terminal frames if
1652 IT->glyph_row != NULL. IT->face_id contains the character's
1656 append_composite_glyph (struct it
*it
)
1658 struct glyph
*glyph
;
1660 xassert (it
->glyph_row
);
1661 glyph
= it
->glyph_row
->glyphs
[it
->area
] + it
->glyph_row
->used
[it
->area
];
1662 if (glyph
< it
->glyph_row
->glyphs
[1 + it
->area
])
1664 /* If the glyph row is reversed, we need to prepend the glyph
1665 rather than append it. */
1666 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1670 /* Make room for the new glyph. */
1671 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1673 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1675 glyph
->type
= COMPOSITE_GLYPH
;
1676 glyph
->pixel_width
= it
->pixel_width
;
1677 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1678 if (it
->cmp_it
.ch
< 0)
1680 glyph
->u
.cmp
.automatic
= 0;
1681 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1685 glyph
->u
.cmp
.automatic
= 1;
1686 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1687 glyph
->slice
.cmp
.from
= it
->cmp_it
.from
;
1688 glyph
->slice
.cmp
.to
= it
->cmp_it
.to
- 1;
1691 glyph
->face_id
= it
->face_id
;
1692 glyph
->padding_p
= 0;
1693 glyph
->charpos
= CHARPOS (it
->position
);
1694 glyph
->object
= it
->object
;
1697 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1698 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1700 glyph
->bidi_type
= it
->bidi_it
.type
;
1704 glyph
->resolved_level
= 0;
1705 glyph
->bidi_type
= UNKNOWN_BT
;
1708 ++it
->glyph_row
->used
[it
->area
];
1714 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1715 the composition. We simply produces components of the composition
1716 assuming that the terminal has a capability to layout/render it
1720 produce_composite_glyph (struct it
*it
)
1722 if (it
->cmp_it
.ch
< 0)
1724 struct composition
*cmp
= composition_table
[it
->cmp_it
.id
];
1726 it
->pixel_width
= cmp
->width
;
1730 Lisp_Object gstring
= composition_gstring_from_id (it
->cmp_it
.id
);
1732 it
->pixel_width
= composition_gstring_width (gstring
, it
->cmp_it
.from
,
1733 it
->cmp_it
.to
, NULL
);
1737 append_composite_glyph (it
);
1741 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1742 is a face ID to be used for the glyph. What is actually appended
1743 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1744 comes from it->nglyphs bytes). */
1747 append_glyphless_glyph (struct it
*it
, int face_id
, const char *str
)
1749 struct glyph
*glyph
, *end
;
1752 xassert (it
->glyph_row
);
1753 glyph
= it
->glyph_row
->glyphs
[it
->area
] + it
->glyph_row
->used
[it
->area
];
1754 end
= it
->glyph_row
->glyphs
[1 + it
->area
];
1756 /* If the glyph row is reversed, we need to prepend the glyph rather
1758 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1761 int move_by
= it
->pixel_width
;
1763 /* Make room for the new glyphs. */
1764 if (move_by
> end
- glyph
) /* don't overstep end of this area */
1765 move_by
= end
- glyph
;
1766 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1768 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1769 end
= glyph
+ move_by
;
1774 glyph
->type
= CHAR_GLYPH
;
1775 glyph
->pixel_width
= 1;
1776 glyph
->face_id
= face_id
;
1777 glyph
->padding_p
= 0;
1778 glyph
->charpos
= CHARPOS (it
->position
);
1779 glyph
->object
= it
->object
;
1782 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1783 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1785 glyph
->bidi_type
= it
->bidi_it
.type
;
1789 glyph
->resolved_level
= 0;
1790 glyph
->bidi_type
= UNKNOWN_BT
;
1793 /* BIDI Note: we put the glyphs of characters left to right, even in
1794 the REVERSED_P case because we write to the terminal
1796 for (i
= 0; i
< it
->nglyphs
&& glyph
< end
; ++i
)
1799 glyph
[0] = glyph
[-1];
1800 glyph
->u
.ch
= str
[i
];
1801 ++it
->glyph_row
->used
[it
->area
];
1806 /* Produce glyphs for a glyphless character for iterator IT.
1807 IT->glyphless_method specifies which method to use for displaying
1808 the character. See the description of enum
1809 glyphless_display_method in dispextern.h for the details.
1811 FOR_NO_FONT is nonzero if and only if this is for a character that
1812 is not supported by the coding system of the terminal. ACRONYM, if
1813 non-nil, is an acronym string for the character.
1815 The glyphs actually produced are of type CHAR_GLYPH. */
1818 produce_glyphless_glyph (struct it
*it
, int for_no_font
, Lisp_Object acronym
)
1822 char buf
[sizeof "\\x" + max (6, (sizeof it
->c
* CHAR_BIT
+ 3) / 4)];
1823 char const *str
= " ";
1825 /* Get a face ID for the glyph by utilizing a cache (the same way as
1826 done for `escape-glyph' in get_next_display_element). */
1827 if (it
->f
== last_glyphless_glyph_frame
1828 && it
->face_id
== last_glyphless_glyph_face_id
)
1830 face_id
= last_glyphless_glyph_merged_face_id
;
1834 /* Merge the `glyphless-char' face into the current face. */
1835 face_id
= merge_faces (it
->f
, Qglyphless_char
, 0, it
->face_id
);
1836 last_glyphless_glyph_frame
= it
->f
;
1837 last_glyphless_glyph_face_id
= it
->face_id
;
1838 last_glyphless_glyph_merged_face_id
= face_id
;
1841 if (it
->glyphless_method
== GLYPHLESS_DISPLAY_THIN_SPACE
)
1843 /* As there's no way to produce a thin space, we produce a space
1844 of canonical width. */
1847 else if (it
->glyphless_method
== GLYPHLESS_DISPLAY_EMPTY_BOX
)
1849 len
= CHAR_WIDTH (it
->c
);
1854 len
= sprintf (buf
, "[%.*s]", len
, str
);
1859 if (it
->glyphless_method
== GLYPHLESS_DISPLAY_ACRONYM
)
1861 if (! STRINGP (acronym
) && CHAR_TABLE_P (Vglyphless_char_display
))
1862 acronym
= CHAR_TABLE_REF (Vglyphless_char_display
, it
->c
);
1863 if (CONSP (acronym
))
1864 acronym
= XCDR (acronym
);
1866 str
= STRINGP (acronym
) ? SSDATA (acronym
) : "";
1867 for (len
= 0; len
< 6 && str
[len
] && ASCII_BYTE_P (str
[len
]); len
++)
1868 buf
[1 + len
] = str
[len
];
1874 xassert (it
->glyphless_method
== GLYPHLESS_DISPLAY_HEX_CODE
);
1875 len
= (it
->c
< 0x10000 ? sprintf (buf
, "\\u%04X", it
->c
)
1876 : it
->c
<= MAX_UNICODE_CHAR
? sprintf (buf
, "\\U%06X", it
->c
)
1877 : sprintf (buf
, "\\x%06X", it
->c
));
1882 it
->pixel_width
= len
;
1885 append_glyphless_glyph (it
, face_id
, str
);
1889 /* Get information about special display element WHAT in an
1890 environment described by IT. WHAT is one of IT_TRUNCATION or
1891 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1892 non-null glyph_row member. This function ensures that fields like
1893 face_id, c, len of IT are left untouched. */
1896 produce_special_glyphs (struct it
*it
, enum display_element_type what
)
1904 temp_it
.what
= IT_CHARACTER
;
1906 temp_it
.object
= make_number (0);
1907 memset (&temp_it
.current
, 0, sizeof temp_it
.current
);
1909 if (what
== IT_CONTINUATION
)
1911 /* Continuation glyph. For R2L lines, we mirror it by hand. */
1912 if (it
->bidi_it
.paragraph_dir
== R2L
)
1913 SET_GLYPH_FROM_CHAR (glyph
, '/');
1915 SET_GLYPH_FROM_CHAR (glyph
, '\\');
1917 && (gc
= DISP_CONTINUE_GLYPH (it
->dp
), GLYPH_CODE_P (gc
)))
1919 /* FIXME: Should we mirror GC for R2L lines? */
1920 SET_GLYPH_FROM_GLYPH_CODE (glyph
, gc
);
1921 spec_glyph_lookup_face (XWINDOW (it
->window
), &glyph
);
1924 else if (what
== IT_TRUNCATION
)
1926 /* Truncation glyph. */
1927 SET_GLYPH_FROM_CHAR (glyph
, '$');
1929 && (gc
= DISP_TRUNC_GLYPH (it
->dp
), GLYPH_CODE_P (gc
)))
1931 /* FIXME: Should we mirror GC for R2L lines? */
1932 SET_GLYPH_FROM_GLYPH_CODE (glyph
, gc
);
1933 spec_glyph_lookup_face (XWINDOW (it
->window
), &glyph
);
1939 temp_it
.c
= temp_it
.char_to_display
= GLYPH_CHAR (glyph
);
1940 temp_it
.face_id
= GLYPH_FACE (glyph
);
1941 temp_it
.len
= CHAR_BYTES (temp_it
.c
);
1943 produce_glyphs (&temp_it
);
1944 it
->pixel_width
= temp_it
.pixel_width
;
1945 it
->nglyphs
= temp_it
.pixel_width
;
1950 /***********************************************************************
1952 ***********************************************************************/
1954 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1955 one of the enumerators from enum no_color_bit, or a bit set built
1956 from them. Some display attributes may not be used together with
1957 color; the termcap capability `NC' specifies which ones. */
1959 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1960 (tty->TN_max_colors > 0 \
1961 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1964 /* Turn appearances of face FACE_ID on tty frame F on.
1965 FACE_ID is a realized face ID number, in the face cache. */
1968 turn_on_face (struct frame
*f
, int face_id
)
1970 struct face
*face
= FACE_FROM_ID (f
, face_id
);
1971 long fg
= face
->foreground
;
1972 long bg
= face
->background
;
1973 struct tty_display_info
*tty
= FRAME_TTY (f
);
1975 /* Do this first because TS_end_standout_mode may be the same
1976 as TS_exit_attribute_mode, which turns all appearances off. */
1977 if (MAY_USE_WITH_COLORS_P (tty
, NC_REVERSE
))
1979 if (tty
->TN_max_colors
> 0)
1981 if (fg
>= 0 && bg
>= 0)
1983 /* If the terminal supports colors, we can set them
1984 below without using reverse video. The face's fg
1985 and bg colors are set as they should appear on
1986 the screen, i.e. they take the inverse-video'ness
1987 of the face already into account. */
1989 else if (inverse_video
)
1991 if (fg
== FACE_TTY_DEFAULT_FG_COLOR
1992 || bg
== FACE_TTY_DEFAULT_BG_COLOR
)
1993 tty_toggle_highlight (tty
);
1997 if (fg
== FACE_TTY_DEFAULT_BG_COLOR
1998 || bg
== FACE_TTY_DEFAULT_FG_COLOR
)
1999 tty_toggle_highlight (tty
);
2004 /* If we can't display colors, use reverse video
2005 if the face specifies that. */
2008 if (fg
== FACE_TTY_DEFAULT_FG_COLOR
2009 || bg
== FACE_TTY_DEFAULT_BG_COLOR
)
2010 tty_toggle_highlight (tty
);
2014 if (fg
== FACE_TTY_DEFAULT_BG_COLOR
2015 || bg
== FACE_TTY_DEFAULT_FG_COLOR
)
2016 tty_toggle_highlight (tty
);
2021 if (face
->tty_bold_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_BOLD
))
2022 OUTPUT1_IF (tty
, tty
->TS_enter_bold_mode
);
2024 if (face
->tty_italic_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_ITALIC
))
2026 if (tty
->TS_enter_italic_mode
)
2027 OUTPUT1 (tty
, tty
->TS_enter_italic_mode
);
2029 /* Italics mode is unavailable on many terminals. In that
2030 case, map slant to dimmed text; we want italic text to
2031 appear different and dimming is not otherwise used. */
2032 OUTPUT1 (tty
, tty
->TS_enter_dim_mode
);
2035 if (face
->tty_underline_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_UNDERLINE
))
2036 OUTPUT1_IF (tty
, tty
->TS_enter_underline_mode
);
2038 if (tty
->TN_max_colors
> 0)
2043 ts
= tty
->standout_mode
? tty
->TS_set_background
: tty
->TS_set_foreground
;
2046 p
= tparam (ts
, NULL
, 0, (int) fg
, 0, 0, 0);
2051 ts
= tty
->standout_mode
? tty
->TS_set_foreground
: tty
->TS_set_background
;
2054 p
= tparam (ts
, NULL
, 0, (int) bg
, 0, 0, 0);
2062 /* Turn off appearances of face FACE_ID on tty frame F. */
2065 turn_off_face (struct frame
*f
, int face_id
)
2067 struct face
*face
= FACE_FROM_ID (f
, face_id
);
2068 struct tty_display_info
*tty
= FRAME_TTY (f
);
2070 xassert (face
!= NULL
);
2072 if (tty
->TS_exit_attribute_mode
)
2074 /* Capability "me" will turn off appearance modes double-bright,
2075 half-bright, reverse-video, standout, underline. It may or
2076 may not turn off alt-char-mode. */
2077 if (face
->tty_bold_p
2078 || face
->tty_italic_p
2079 || face
->tty_reverse_p
2080 || face
->tty_underline_p
)
2082 OUTPUT1_IF (tty
, tty
->TS_exit_attribute_mode
);
2083 if (strcmp (tty
->TS_exit_attribute_mode
, tty
->TS_end_standout_mode
) == 0)
2084 tty
->standout_mode
= 0;
2089 /* If we don't have "me" we can only have those appearances
2090 that have exit sequences defined. */
2091 if (face
->tty_underline_p
)
2092 OUTPUT_IF (tty
, tty
->TS_exit_underline_mode
);
2095 /* Switch back to default colors. */
2096 if (tty
->TN_max_colors
> 0
2097 && ((face
->foreground
!= FACE_TTY_DEFAULT_COLOR
2098 && face
->foreground
!= FACE_TTY_DEFAULT_FG_COLOR
)
2099 || (face
->background
!= FACE_TTY_DEFAULT_COLOR
2100 && face
->background
!= FACE_TTY_DEFAULT_BG_COLOR
)))
2101 OUTPUT1_IF (tty
, tty
->TS_orig_pair
);
2105 /* Return non-zero if the terminal on frame F supports all of the
2106 capabilities in CAPS simultaneously, with foreground and background
2107 colors FG and BG. */
2110 tty_capable_p (struct tty_display_info
*tty
, unsigned int caps
,
2111 unsigned long fg
, unsigned long bg
)
2113 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2114 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2117 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_INVERSE
, tty
->TS_standout_mode
, NC_REVERSE
);
2118 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_UNDERLINE
, tty
->TS_enter_underline_mode
, NC_UNDERLINE
);
2119 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_BOLD
, tty
->TS_enter_bold_mode
, NC_BOLD
);
2120 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_DIM
, tty
->TS_enter_dim_mode
, NC_DIM
);
2121 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_ITALIC
, tty
->TS_enter_italic_mode
, NC_ITALIC
);
2127 /* Return non-zero if the terminal is capable to display colors. */
2129 DEFUN ("tty-display-color-p", Ftty_display_color_p
, Stty_display_color_p
,
2131 doc
: /* Return non-nil if the tty device TERMINAL can display colors.
2133 TERMINAL can be a terminal object, a frame, or nil (meaning the
2134 selected frame's terminal). This function always returns nil if
2135 TERMINAL does not refer to a text terminal. */)
2136 (Lisp_Object terminal
)
2138 struct terminal
*t
= get_tty_terminal (terminal
, 0);
2142 return t
->display_info
.tty
->TN_max_colors
> 0 ? Qt
: Qnil
;
2145 /* Return the number of supported colors. */
2146 DEFUN ("tty-display-color-cells", Ftty_display_color_cells
,
2147 Stty_display_color_cells
, 0, 1, 0,
2148 doc
: /* Return the number of colors supported by the tty device TERMINAL.
2150 TERMINAL can be a terminal object, a frame, or nil (meaning the
2151 selected frame's terminal). This function always returns 0 if
2152 TERMINAL does not refer to a text terminal. */)
2153 (Lisp_Object terminal
)
2155 struct terminal
*t
= get_tty_terminal (terminal
, 0);
2157 return make_number (0);
2159 return make_number (t
->display_info
.tty
->TN_max_colors
);
2164 /* Declare here rather than in the function, as in the rest of Emacs,
2165 to work around an HPUX compiler bug (?). See
2166 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2167 static int default_max_colors
;
2168 static int default_max_pairs
;
2169 static int default_no_color_video
;
2170 static char *default_orig_pair
;
2171 static char *default_set_foreground
;
2172 static char *default_set_background
;
2174 /* Save or restore the default color-related capabilities of this
2177 tty_default_color_capabilities (struct tty_display_info
*tty
, int save
)
2182 xfree (default_orig_pair
);
2183 default_orig_pair
= tty
->TS_orig_pair
? xstrdup (tty
->TS_orig_pair
) : NULL
;
2185 xfree (default_set_foreground
);
2186 default_set_foreground
= tty
->TS_set_foreground
? xstrdup (tty
->TS_set_foreground
)
2189 xfree (default_set_background
);
2190 default_set_background
= tty
->TS_set_background
? xstrdup (tty
->TS_set_background
)
2193 default_max_colors
= tty
->TN_max_colors
;
2194 default_max_pairs
= tty
->TN_max_pairs
;
2195 default_no_color_video
= tty
->TN_no_color_video
;
2199 tty
->TS_orig_pair
= default_orig_pair
;
2200 tty
->TS_set_foreground
= default_set_foreground
;
2201 tty
->TS_set_background
= default_set_background
;
2202 tty
->TN_max_colors
= default_max_colors
;
2203 tty
->TN_max_pairs
= default_max_pairs
;
2204 tty
->TN_no_color_video
= default_no_color_video
;
2208 /* Setup one of the standard tty color schemes according to MODE.
2209 MODE's value is generally the number of colors which we want to
2210 support; zero means set up for the default capabilities, the ones
2211 we saw at init_tty time; -1 means turn off color support. */
2213 tty_setup_colors (struct tty_display_info
*tty
, int mode
)
2215 /* Canonicalize all negative values of MODE. */
2221 case -1: /* no colors at all */
2222 tty
->TN_max_colors
= 0;
2223 tty
->TN_max_pairs
= 0;
2224 tty
->TN_no_color_video
= 0;
2225 tty
->TS_set_foreground
= tty
->TS_set_background
= tty
->TS_orig_pair
= NULL
;
2227 case 0: /* default colors, if any */
2229 tty_default_color_capabilities (tty
, 0);
2231 case 8: /* 8 standard ANSI colors */
2232 tty
->TS_orig_pair
= "\033[0m";
2234 tty
->TS_set_foreground
= "\033[3%p1%dm";
2235 tty
->TS_set_background
= "\033[4%p1%dm";
2237 tty
->TS_set_foreground
= "\033[3%dm";
2238 tty
->TS_set_background
= "\033[4%dm";
2240 tty
->TN_max_colors
= 8;
2241 tty
->TN_max_pairs
= 64;
2242 tty
->TN_no_color_video
= 0;
2248 set_tty_color_mode (struct tty_display_info
*tty
, struct frame
*f
)
2250 Lisp_Object tem
, val
;
2251 Lisp_Object color_mode
;
2253 Lisp_Object tty_color_mode_alist
2254 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil
);
2256 tem
= assq_no_quit (Qtty_color_mode
, f
->param_alist
);
2257 val
= CONSP (tem
) ? XCDR (tem
) : Qnil
;
2261 else if (SYMBOLP (tty_color_mode_alist
))
2263 tem
= Fassq (val
, Fsymbol_value (tty_color_mode_alist
));
2264 color_mode
= CONSP (tem
) ? XCDR (tem
) : Qnil
;
2269 mode
= TYPE_RANGED_INTEGERP (int, color_mode
) ? XINT (color_mode
) : 0;
2271 if (mode
!= tty
->previous_color_mode
)
2273 Lisp_Object funsym
= intern ("tty-set-up-initial-frame-faces");
2274 tty
->previous_color_mode
= mode
;
2275 tty_setup_colors (tty
, mode
);
2276 /* This recomputes all the faces given the new color definitions. */
2277 safe_call (1, &funsym
);
2281 #endif /* !DOS_NT */
2285 /* Return the tty display object specified by TERMINAL. */
2287 static struct terminal
*
2288 get_tty_terminal (Lisp_Object terminal
, int throw)
2290 struct terminal
*t
= get_terminal (terminal
, throw);
2292 if (t
&& t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2295 error ("Device %d is not a termcap terminal device", t
->id
);
2303 /* Return an active termcap device that uses the tty device with the
2306 This function ignores suspended devices.
2308 Returns NULL if the named terminal device is not opened. */
2311 get_named_tty (const char *name
)
2318 for (t
= terminal_list
; t
; t
= t
->next_terminal
)
2320 if ((t
->type
== output_termcap
|| t
->type
== output_msdos_raw
)
2321 && !strcmp (t
->display_info
.tty
->name
, name
)
2322 && TERMINAL_ACTIVE_P (t
))
2330 DEFUN ("tty-type", Ftty_type
, Stty_type
, 0, 1, 0,
2331 doc
: /* Return the type of the tty device that TERMINAL uses.
2332 Returns nil if TERMINAL is not on a tty device.
2334 TERMINAL can be a terminal object, a frame, or nil (meaning the
2335 selected frame's terminal). */)
2336 (Lisp_Object terminal
)
2338 struct terminal
*t
= get_terminal (terminal
, 1);
2340 if (t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2343 if (t
->display_info
.tty
->type
)
2344 return build_string (t
->display_info
.tty
->type
);
2349 DEFUN ("controlling-tty-p", Fcontrolling_tty_p
, Scontrolling_tty_p
, 0, 1, 0,
2350 doc
: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2352 TERMINAL can be a terminal object, a frame, or nil (meaning the
2353 selected frame's terminal). This function always returns nil if
2354 TERMINAL is not on a tty device. */)
2355 (Lisp_Object terminal
)
2357 struct terminal
*t
= get_terminal (terminal
, 1);
2359 if ((t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2360 || strcmp (t
->display_info
.tty
->name
, DEV_TTY
) != 0)
2366 DEFUN ("tty-no-underline", Ftty_no_underline
, Stty_no_underline
, 0, 1, 0,
2367 doc
: /* Declare that the tty used by TERMINAL does not handle underlining.
2368 This is used to override the terminfo data, for certain terminals that
2369 do not really do underlining, but say that they do. This function has
2370 no effect if used on a non-tty terminal.
2372 TERMINAL can be a terminal object, a frame or nil (meaning the
2373 selected frame's terminal). This function always returns nil if
2374 TERMINAL does not refer to a text terminal. */)
2375 (Lisp_Object terminal
)
2377 struct terminal
*t
= get_terminal (terminal
, 1);
2379 if (t
->type
== output_termcap
)
2380 t
->display_info
.tty
->TS_enter_underline_mode
= 0;
2384 DEFUN ("tty-top-frame", Ftty_top_frame
, Stty_top_frame
, 0, 1, 0,
2385 doc
: /* Return the topmost terminal frame on TERMINAL.
2386 TERMINAL can be a terminal object, a frame or nil (meaning the
2387 selected frame's terminal). This function returns nil if TERMINAL
2388 does not refer to a text terminal. Otherwise, it returns the
2389 top-most frame on the text terminal. */)
2390 (Lisp_Object terminal
)
2392 struct terminal
*t
= get_terminal (terminal
, 1);
2394 if (t
->type
== output_termcap
)
2395 return t
->display_info
.tty
->top_frame
;
2401 DEFUN ("suspend-tty", Fsuspend_tty
, Ssuspend_tty
, 0, 1, 0,
2402 doc
: /* Suspend the terminal device TTY.
2404 The device is restored to its default state, and Emacs ceases all
2405 access to the tty device. Frames that use the device are not deleted,
2406 but input is not read from them and if they change, their display is
2409 TTY may be a terminal object, a frame, or nil for the terminal device
2410 of the currently selected frame.
2412 This function runs `suspend-tty-functions' after suspending the
2413 device. The functions are run with one arg, the id of the suspended
2416 `suspend-tty' does nothing if it is called on a device that is already
2419 A suspended tty may be resumed by calling `resume-tty' on it. */)
2422 struct terminal
*t
= get_tty_terminal (tty
, 1);
2426 error ("Unknown tty device");
2428 f
= t
->display_info
.tty
->input
;
2432 /* First run `suspend-tty-functions' and then clean up the tty
2433 state because `suspend-tty-functions' might need to change
2435 Lisp_Object args
[2];
2436 args
[0] = intern ("suspend-tty-functions");
2437 XSETTERMINAL (args
[1], t
);
2438 Frun_hook_with_args (2, args
);
2440 reset_sys_modes (t
->display_info
.tty
);
2441 delete_keyboard_wait_descriptor (fileno (f
));
2445 if (f
!= t
->display_info
.tty
->output
)
2446 fclose (t
->display_info
.tty
->output
);
2449 t
->display_info
.tty
->input
= 0;
2450 t
->display_info
.tty
->output
= 0;
2452 if (FRAMEP (t
->display_info
.tty
->top_frame
))
2453 FRAME_SET_VISIBLE (XFRAME (t
->display_info
.tty
->top_frame
), 0);
2457 /* Clear display hooks to prevent further output. */
2458 clear_tty_hooks (t
);
2463 DEFUN ("resume-tty", Fresume_tty
, Sresume_tty
, 0, 1, 0,
2464 doc
: /* Resume the previously suspended terminal device TTY.
2465 The terminal is opened and reinitialized. Frames that are on the
2466 suspended terminal are revived.
2468 It is an error to resume a terminal while another terminal is active
2471 This function runs `resume-tty-functions' after resuming the terminal.
2472 The functions are run with one arg, the id of the resumed terminal
2475 `resume-tty' does nothing if it is called on a device that is not
2478 TTY may be a terminal object, a frame, or nil (meaning the selected
2479 frame's terminal). */)
2482 struct terminal
*t
= get_tty_terminal (tty
, 1);
2486 error ("Unknown tty device");
2488 if (!t
->display_info
.tty
->input
)
2490 if (get_named_tty (t
->display_info
.tty
->name
))
2491 error ("Cannot resume display while another display is active on the same device");
2494 t
->display_info
.tty
->output
= stdout
;
2495 t
->display_info
.tty
->input
= stdin
;
2497 fd
= emacs_open (t
->display_info
.tty
->name
, O_RDWR
| O_NOCTTY
, 0);
2500 error ("Can not reopen tty device %s: %s", t
->display_info
.tty
->name
, strerror (errno
));
2502 if (strcmp (t
->display_info
.tty
->name
, DEV_TTY
))
2503 dissociate_if_controlling_tty (fd
);
2505 t
->display_info
.tty
->output
= fdopen (fd
, "w+");
2506 t
->display_info
.tty
->input
= t
->display_info
.tty
->output
;
2509 add_keyboard_wait_descriptor (fd
);
2511 if (FRAMEP (t
->display_info
.tty
->top_frame
))
2513 struct frame
*f
= XFRAME (t
->display_info
.tty
->top_frame
);
2515 int old_height
= FRAME_COLS (f
);
2516 int old_width
= FRAME_LINES (f
);
2518 /* Check if terminal/window size has changed while the frame
2520 get_tty_size (fileno (t
->display_info
.tty
->input
), &width
, &height
);
2521 if (width
!= old_width
|| height
!= old_height
)
2522 change_frame_size (f
, height
, width
, 0, 0, 0);
2523 FRAME_SET_VISIBLE (XFRAME (t
->display_info
.tty
->top_frame
), 1);
2527 init_sys_modes (t
->display_info
.tty
);
2530 /* Run `resume-tty-functions'. */
2531 Lisp_Object args
[2];
2532 args
[0] = intern ("resume-tty-functions");
2533 XSETTERMINAL (args
[1], t
);
2534 Frun_hook_with_args (2, args
);
2544 /***********************************************************************
2546 ***********************************************************************/
2550 #ifndef HAVE_WINDOW_SYSTEM
2552 term_mouse_moveto (int x
, int y
)
2554 /* TODO: how to set mouse position?
2557 name = (const char *) ttyname (0);
2558 fd = open (name, O_WRONLY);
2559 SOME_FUNCTION (x, y, fd);
2562 last_mouse_y = y; */
2564 #endif /* HAVE_WINDOW_SYSTEM */
2566 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2568 tty_draw_row_with_mouse_face (struct window
*w
, struct glyph_row
*row
,
2569 int start_hpos
, int end_hpos
,
2570 enum draw_glyphs_face draw
)
2572 int nglyphs
= end_hpos
- start_hpos
;
2573 struct frame
*f
= XFRAME (WINDOW_FRAME (w
));
2574 struct tty_display_info
*tty
= FRAME_TTY (f
);
2575 int face_id
= tty
->mouse_highlight
.mouse_face_face_id
;
2576 int save_x
, save_y
, pos_x
, pos_y
;
2578 if (end_hpos
>= row
->used
[TEXT_AREA
])
2579 nglyphs
= row
->used
[TEXT_AREA
] - start_hpos
;
2581 pos_y
= row
->y
+ WINDOW_TOP_EDGE_Y (w
);
2582 pos_x
= row
->used
[LEFT_MARGIN_AREA
] + start_hpos
+ WINDOW_LEFT_EDGE_X (w
);
2584 /* Save current cursor co-ordinates. */
2585 save_y
= curY (tty
);
2586 save_x
= curX (tty
);
2587 cursor_to (f
, pos_y
, pos_x
);
2589 if (draw
== DRAW_MOUSE_FACE
)
2590 tty_write_glyphs_with_face (f
, row
->glyphs
[TEXT_AREA
] + start_hpos
,
2592 else if (draw
== DRAW_NORMAL_TEXT
)
2593 write_glyphs (f
, row
->glyphs
[TEXT_AREA
] + start_hpos
, nglyphs
);
2595 cursor_to (f
, save_y
, save_x
);
2599 term_mouse_movement (FRAME_PTR frame
, Gpm_Event
*event
)
2601 /* Has the mouse moved off the glyph it was on at the last sighting? */
2602 if (event
->x
!= last_mouse_x
|| event
->y
!= last_mouse_y
)
2604 frame
->mouse_moved
= 1;
2605 note_mouse_highlight (frame
, event
->x
, event
->y
);
2606 /* Remember which glyph we're now on. */
2607 last_mouse_x
= event
->x
;
2608 last_mouse_y
= event
->y
;
2614 /* Return the Time that corresponds to T. Wrap around on overflow. */
2616 timeval_to_Time (struct timeval
const *t
)
2622 ms
= t
->tv_usec
/ 1000;
2626 /* Return the current position of the mouse.
2628 Set *f to the frame the mouse is in, or zero if the mouse is in no
2629 Emacs frame. If it is set to zero, all the other arguments are
2632 Set *bar_window to Qnil, and *x and *y to the column and
2633 row of the character cell the mouse is over.
2635 Set *timeptr to the time the mouse was at the returned position.
2637 This clears mouse_moved until the next motion
2640 term_mouse_position (FRAME_PTR
*fp
, int insist
, Lisp_Object
*bar_window
,
2641 enum scroll_bar_part
*part
, Lisp_Object
*x
,
2642 Lisp_Object
*y
, Time
*timeptr
)
2646 *fp
= SELECTED_FRAME ();
2647 (*fp
)->mouse_moved
= 0;
2652 XSETINT (*x
, last_mouse_x
);
2653 XSETINT (*y
, last_mouse_y
);
2654 gettimeofday(&now
, 0);
2655 *timeptr
= timeval_to_Time (&now
);
2658 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2660 If the event is a button press, then note that we have grabbed
2664 term_mouse_click (struct input_event
*result
, Gpm_Event
*event
,
2670 result
->kind
= GPM_CLICK_EVENT
;
2671 for (i
= 0, j
= GPM_B_LEFT
; i
< 3; i
++, j
>>= 1 )
2673 if (event
->buttons
& j
) {
2674 result
->code
= i
; /* button number */
2678 gettimeofday(&now
, 0);
2679 result
->timestamp
= timeval_to_Time (&now
);
2681 if (event
->type
& GPM_UP
)
2682 result
->modifiers
= up_modifier
;
2683 else if (event
->type
& GPM_DOWN
)
2684 result
->modifiers
= down_modifier
;
2686 result
->modifiers
= 0;
2688 if (event
->type
& GPM_SINGLE
)
2689 result
->modifiers
|= click_modifier
;
2691 if (event
->type
& GPM_DOUBLE
)
2692 result
->modifiers
|= double_modifier
;
2694 if (event
->type
& GPM_TRIPLE
)
2695 result
->modifiers
|= triple_modifier
;
2697 if (event
->type
& GPM_DRAG
)
2698 result
->modifiers
|= drag_modifier
;
2700 if (!(event
->type
& (GPM_MOVE
| GPM_DRAG
))) {
2703 if (event
->modifiers
& (1 << 0))
2704 result
->modifiers
|= shift_modifier
;
2707 if (event
->modifiers
& (1 << 2))
2708 result
->modifiers
|= ctrl_modifier
;
2710 /* 1 << KG_ALT || KG_ALTGR */
2711 if (event
->modifiers
& (1 << 3)
2712 || event
->modifiers
& (1 << 1))
2713 result
->modifiers
|= meta_modifier
;
2716 XSETINT (result
->x
, event
->x
);
2717 XSETINT (result
->y
, event
->y
);
2718 XSETFRAME (result
->frame_or_window
, f
);
2724 handle_one_term_event (struct tty_display_info
*tty
, Gpm_Event
*event
, struct input_event
* hold_quit
)
2726 struct frame
*f
= XFRAME (tty
->top_frame
);
2727 struct input_event ie
;
2735 if (event
->type
& (GPM_MOVE
| GPM_DRAG
)) {
2736 previous_help_echo_string
= help_echo_string
;
2737 help_echo_string
= Qnil
;
2739 Gpm_DrawPointer (event
->x
, event
->y
, fileno (tty
->output
));
2741 if (!term_mouse_movement (f
, event
))
2742 help_echo_string
= previous_help_echo_string
;
2744 /* If the contents of the global variable help_echo_string
2745 has changed, generate a HELP_EVENT. */
2746 if (!NILP (help_echo_string
)
2747 || !NILP (previous_help_echo_string
))
2754 term_mouse_click (&ie
, event
, f
);
2758 if (ie
.kind
!= NO_EVENT
)
2760 kbd_buffer_store_event_hold (&ie
, hold_quit
);
2765 && !(hold_quit
&& hold_quit
->kind
!= NO_EVENT
))
2770 XSETFRAME (frame
, f
);
2774 gen_help_event (help_echo_string
, frame
, help_echo_window
,
2775 help_echo_object
, help_echo_pos
);
2782 DEFUN ("gpm-mouse-start", Fgpm_mouse_start
, Sgpm_mouse_start
,
2784 doc
: /* Open a connection to Gpm.
2785 Gpm-mouse can only be activated for one tty at a time. */)
2788 struct frame
*f
= SELECTED_FRAME ();
2789 struct tty_display_info
*tty
2790 = ((f
)->output_method
== output_termcap
2791 ? (f
)->terminal
->display_info
.tty
: NULL
);
2792 Gpm_Connect connection
;
2795 error ("Gpm-mouse only works in the GNU/Linux console");
2797 return Qnil
; /* Already activated, nothing to do. */
2799 error ("Gpm-mouse can only be activated for one tty at a time");
2801 connection
.eventMask
= ~0;
2802 connection
.defaultMask
= ~GPM_HARD
;
2803 connection
.maxMod
= ~0;
2804 connection
.minMod
= 0;
2807 if (Gpm_Open (&connection
, 0) < 0)
2808 error ("Gpm-mouse failed to connect to the gpm daemon");
2812 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2813 to generate SIGIOs. Apparently we need to call reset_sys_modes
2814 before calling init_sys_modes. */
2815 reset_sys_modes (tty
);
2816 init_sys_modes (tty
);
2817 add_gpm_wait_descriptor (gpm_fd
);
2826 delete_gpm_wait_descriptor (fd
);
2827 while (Gpm_Close()); /* close all the stack */
2831 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop
, Sgpm_mouse_stop
,
2833 doc
: /* Close a connection to Gpm. */)
2836 struct frame
*f
= SELECTED_FRAME ();
2837 struct tty_display_info
*tty
2838 = ((f
)->output_method
== output_termcap
2839 ? (f
)->terminal
->display_info
.tty
: NULL
);
2841 if (!tty
|| gpm_tty
!= tty
)
2842 return Qnil
; /* Not activated on this terminal, nothing to do. */
2847 #endif /* HAVE_GPM */
2851 /***********************************************************************
2853 ***********************************************************************/
2855 /* Initialize the tty-dependent part of frame F. The frame must
2856 already have its device initialized. */
2859 create_tty_output (struct frame
*f
)
2861 struct tty_output
*t
;
2863 if (! FRAME_TERMCAP_P (f
))
2866 t
= xmalloc (sizeof (struct tty_output
));
2867 memset (t
, 0, sizeof (struct tty_output
));
2869 t
->display_info
= FRAME_TERMINAL (f
)->display_info
.tty
;
2871 f
->output_data
.tty
= t
;
2874 /* Delete frame F's face cache, and its tty-dependent part. */
2877 tty_free_frame_resources (struct frame
*f
)
2879 if (! FRAME_TERMCAP_P (f
))
2882 if (FRAME_FACE_CACHE (f
))
2883 free_frame_faces (f
);
2885 xfree (f
->output_data
.tty
);
2890 /* Delete frame F's face cache. */
2893 tty_free_frame_resources (struct frame
*f
)
2895 if (! FRAME_TERMCAP_P (f
) && ! FRAME_MSDOS_P (f
))
2898 if (FRAME_FACE_CACHE (f
))
2899 free_frame_faces (f
);
2903 /* Reset the hooks in TERMINAL. */
2906 clear_tty_hooks (struct terminal
*terminal
)
2909 terminal
->cursor_to_hook
= 0;
2910 terminal
->raw_cursor_to_hook
= 0;
2911 terminal
->clear_to_end_hook
= 0;
2912 terminal
->clear_frame_hook
= 0;
2913 terminal
->clear_end_of_line_hook
= 0;
2914 terminal
->ins_del_lines_hook
= 0;
2915 terminal
->insert_glyphs_hook
= 0;
2916 terminal
->write_glyphs_hook
= 0;
2917 terminal
->delete_glyphs_hook
= 0;
2918 terminal
->ring_bell_hook
= 0;
2919 terminal
->reset_terminal_modes_hook
= 0;
2920 terminal
->set_terminal_modes_hook
= 0;
2921 terminal
->update_begin_hook
= 0;
2922 terminal
->update_end_hook
= 0;
2923 terminal
->set_terminal_window_hook
= 0;
2924 terminal
->mouse_position_hook
= 0;
2925 terminal
->frame_rehighlight_hook
= 0;
2926 terminal
->frame_raise_lower_hook
= 0;
2927 terminal
->fullscreen_hook
= 0;
2928 terminal
->set_vertical_scroll_bar_hook
= 0;
2929 terminal
->condemn_scroll_bars_hook
= 0;
2930 terminal
->redeem_scroll_bar_hook
= 0;
2931 terminal
->judge_scroll_bars_hook
= 0;
2932 terminal
->read_socket_hook
= 0;
2933 terminal
->frame_up_to_date_hook
= 0;
2935 /* Leave these two set, or suspended frames are not deleted
2937 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
2938 terminal
->delete_terminal_hook
= &delete_tty
;
2941 /* Initialize hooks in TERMINAL with the values needed for a tty. */
2944 set_tty_hooks (struct terminal
*terminal
)
2946 terminal
->rif
= 0; /* ttys don't support window-based redisplay. */
2948 terminal
->cursor_to_hook
= &tty_cursor_to
;
2949 terminal
->raw_cursor_to_hook
= &tty_raw_cursor_to
;
2951 terminal
->clear_to_end_hook
= &tty_clear_to_end
;
2952 terminal
->clear_frame_hook
= &tty_clear_frame
;
2953 terminal
->clear_end_of_line_hook
= &tty_clear_end_of_line
;
2955 terminal
->ins_del_lines_hook
= &tty_ins_del_lines
;
2957 terminal
->insert_glyphs_hook
= &tty_insert_glyphs
;
2958 terminal
->write_glyphs_hook
= &tty_write_glyphs
;
2959 terminal
->delete_glyphs_hook
= &tty_delete_glyphs
;
2961 terminal
->ring_bell_hook
= &tty_ring_bell
;
2963 terminal
->reset_terminal_modes_hook
= &tty_reset_terminal_modes
;
2964 terminal
->set_terminal_modes_hook
= &tty_set_terminal_modes
;
2965 terminal
->update_begin_hook
= 0; /* Not needed. */
2966 terminal
->update_end_hook
= &tty_update_end
;
2967 terminal
->set_terminal_window_hook
= &tty_set_terminal_window
;
2969 terminal
->mouse_position_hook
= 0; /* Not needed. */
2970 terminal
->frame_rehighlight_hook
= 0; /* Not needed. */
2971 terminal
->frame_raise_lower_hook
= 0; /* Not needed. */
2973 terminal
->set_vertical_scroll_bar_hook
= 0; /* Not needed. */
2974 terminal
->condemn_scroll_bars_hook
= 0; /* Not needed. */
2975 terminal
->redeem_scroll_bar_hook
= 0; /* Not needed. */
2976 terminal
->judge_scroll_bars_hook
= 0; /* Not needed. */
2978 terminal
->read_socket_hook
= &tty_read_avail_input
; /* keyboard.c */
2979 terminal
->frame_up_to_date_hook
= 0; /* Not needed. */
2981 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
2982 terminal
->delete_terminal_hook
= &delete_tty
;
2985 /* Drop the controlling terminal if fd is the same device. */
2987 dissociate_if_controlling_tty (int fd
)
2990 int pgid
= tcgetpgrp (fd
); /* If tcgetpgrp succeeds, fd is the ctty. */
2995 no_controlling_tty
= 1;
2996 #elif defined (CYGWIN)
2998 no_controlling_tty
= 1;
3000 #ifdef TIOCNOTTY /* Try BSD ioctls. */
3001 sigblock (sigmask (SIGTTOU
));
3002 fd
= emacs_open (DEV_TTY
, O_RDWR
, 0);
3003 if (fd
!= -1 && ioctl (fd
, TIOCNOTTY
, 0) != -1)
3005 no_controlling_tty
= 1;
3009 sigunblock (sigmask (SIGTTOU
));
3011 /* Unknown system. */
3013 #endif /* ! TIOCNOTTY */
3016 #endif /* !DOS_NT */
3019 /* Create a termcap display on the tty device with the given name and
3022 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3023 Otherwise NAME should be a path to the tty device file,
3026 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3028 If MUST_SUCCEED is true, then all errors are fatal. */
3031 init_tty (const char *name
, const char *terminal_type
, int must_succeed
)
3034 char **address
= &area
;
3035 int buffer_size
= 4096;
3037 struct tty_display_info
*tty
= NULL
;
3038 struct terminal
*terminal
= NULL
;
3039 int ctty
= 0; /* 1 if asked to open controlling tty. */
3042 maybe_fatal (must_succeed
, 0,
3043 "Unknown terminal type",
3044 "Unknown terminal type");
3048 if (!strcmp (name
, DEV_TTY
))
3051 /* If we already have a terminal on the given device, use that. If
3052 all such terminals are suspended, create a new one instead. */
3053 /* XXX Perhaps this should be made explicit by having init_tty
3054 always create a new terminal and separating terminal and frame
3055 creation on Lisp level. */
3056 terminal
= get_named_tty (name
);
3060 terminal
= create_terminal ();
3063 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
3066 tty
= &the_only_display_info
;
3068 tty
= (struct tty_display_info
*) xmalloc (sizeof (struct tty_display_info
));
3070 memset (tty
, 0, sizeof (struct tty_display_info
));
3071 tty
->next
= tty_list
;
3074 terminal
->type
= output_termcap
;
3075 terminal
->display_info
.tty
= tty
;
3076 tty
->terminal
= terminal
;
3078 tty
->Wcm
= (struct cm
*) xmalloc (sizeof (struct cm
));
3081 encode_terminal_src_size
= 0;
3082 encode_terminal_dst_size
= 0;
3086 set_tty_hooks (terminal
);
3092 #ifdef O_IGNORE_CTTY
3094 /* Open the terminal device. Don't recognize it as our
3095 controlling terminal, and don't make it the controlling tty
3096 if we don't have one at the moment. */
3097 fd
= emacs_open (name
, O_RDWR
| O_IGNORE_CTTY
| O_NOCTTY
, 0);
3099 #endif /* O_IGNORE_CTTY */
3100 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3101 defined on Hurd. On other systems, we need to explicitly
3102 dissociate ourselves from the controlling tty when we want to
3103 open a frame on the same terminal. */
3104 fd
= emacs_open (name
, O_RDWR
| O_NOCTTY
, 0);
3106 tty
->name
= xstrdup (name
);
3107 terminal
->name
= xstrdup (name
);
3110 maybe_fatal (must_succeed
, terminal
,
3111 "Could not open file: %s",
3112 "Could not open file: %s",
3117 maybe_fatal (must_succeed
, terminal
,
3118 "Not a tty device: %s",
3119 "Not a tty device: %s",
3123 #ifndef O_IGNORE_CTTY
3125 dissociate_if_controlling_tty (fd
);
3128 file
= fdopen (fd
, "w+");
3133 tty
->type
= xstrdup (terminal_type
);
3135 add_keyboard_wait_descriptor (fileno (tty
->input
));
3139 tty
->termcap_term_buffer
= (char *) xmalloc (buffer_size
);
3141 /* On some systems, tgetent tries to access the controlling
3143 sigblock (sigmask (SIGTTOU
));
3144 status
= tgetent (tty
->termcap_term_buffer
, terminal_type
);
3145 sigunblock (sigmask (SIGTTOU
));
3150 maybe_fatal (must_succeed
, terminal
,
3151 "Cannot open terminfo database file",
3152 "Cannot open terminfo database file");
3154 maybe_fatal (must_succeed
, terminal
,
3155 "Cannot open termcap database file",
3156 "Cannot open termcap database file");
3161 maybe_fatal (must_succeed
, terminal
,
3162 "Terminal type %s is not defined",
3163 "Terminal type %s is not defined.\n\
3164 If that is not the actual type of terminal you have,\n\
3165 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3166 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3168 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3170 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3176 if (strlen (tty
->termcap_term_buffer
) >= buffer_size
)
3178 buffer_size
= strlen (tty
->termcap_term_buffer
);
3180 tty
->termcap_strings_buffer
= area
= (char *) xmalloc (buffer_size
);
3181 tty
->TS_ins_line
= tgetstr ("al", address
);
3182 tty
->TS_ins_multi_lines
= tgetstr ("AL", address
);
3183 tty
->TS_bell
= tgetstr ("bl", address
);
3184 BackTab (tty
) = tgetstr ("bt", address
);
3185 tty
->TS_clr_to_bottom
= tgetstr ("cd", address
);
3186 tty
->TS_clr_line
= tgetstr ("ce", address
);
3187 tty
->TS_clr_frame
= tgetstr ("cl", address
);
3188 ColPosition (tty
) = NULL
; /* tgetstr ("ch", address); */
3189 AbsPosition (tty
) = tgetstr ("cm", address
);
3190 CR (tty
) = tgetstr ("cr", address
);
3191 tty
->TS_set_scroll_region
= tgetstr ("cs", address
);
3192 tty
->TS_set_scroll_region_1
= tgetstr ("cS", address
);
3193 RowPosition (tty
) = tgetstr ("cv", address
);
3194 tty
->TS_del_char
= tgetstr ("dc", address
);
3195 tty
->TS_del_multi_chars
= tgetstr ("DC", address
);
3196 tty
->TS_del_line
= tgetstr ("dl", address
);
3197 tty
->TS_del_multi_lines
= tgetstr ("DL", address
);
3198 tty
->TS_delete_mode
= tgetstr ("dm", address
);
3199 tty
->TS_end_delete_mode
= tgetstr ("ed", address
);
3200 tty
->TS_end_insert_mode
= tgetstr ("ei", address
);
3201 Home (tty
) = tgetstr ("ho", address
);
3202 tty
->TS_ins_char
= tgetstr ("ic", address
);
3203 tty
->TS_ins_multi_chars
= tgetstr ("IC", address
);
3204 tty
->TS_insert_mode
= tgetstr ("im", address
);
3205 tty
->TS_pad_inserted_char
= tgetstr ("ip", address
);
3206 tty
->TS_end_keypad_mode
= tgetstr ("ke", address
);
3207 tty
->TS_keypad_mode
= tgetstr ("ks", address
);
3208 LastLine (tty
) = tgetstr ("ll", address
);
3209 Right (tty
) = tgetstr ("nd", address
);
3210 Down (tty
) = tgetstr ("do", address
);
3212 Down (tty
) = tgetstr ("nl", address
); /* Obsolete name for "do" */
3213 if (tgetflag ("bs"))
3214 Left (tty
) = "\b"; /* can't possibly be longer! */
3215 else /* (Actually, "bs" is obsolete...) */
3216 Left (tty
) = tgetstr ("le", address
);
3218 Left (tty
) = tgetstr ("bc", address
); /* Obsolete name for "le" */
3219 tty
->TS_pad_char
= tgetstr ("pc", address
);
3220 tty
->TS_repeat
= tgetstr ("rp", address
);
3221 tty
->TS_end_standout_mode
= tgetstr ("se", address
);
3222 tty
->TS_fwd_scroll
= tgetstr ("sf", address
);
3223 tty
->TS_standout_mode
= tgetstr ("so", address
);
3224 tty
->TS_rev_scroll
= tgetstr ("sr", address
);
3225 tty
->Wcm
->cm_tab
= tgetstr ("ta", address
);
3226 tty
->TS_end_termcap_modes
= tgetstr ("te", address
);
3227 tty
->TS_termcap_modes
= tgetstr ("ti", address
);
3228 Up (tty
) = tgetstr ("up", address
);
3229 tty
->TS_visible_bell
= tgetstr ("vb", address
);
3230 tty
->TS_cursor_normal
= tgetstr ("ve", address
);
3231 tty
->TS_cursor_visible
= tgetstr ("vs", address
);
3232 tty
->TS_cursor_invisible
= tgetstr ("vi", address
);
3233 tty
->TS_set_window
= tgetstr ("wi", address
);
3235 tty
->TS_enter_underline_mode
= tgetstr ("us", address
);
3236 tty
->TS_exit_underline_mode
= tgetstr ("ue", address
);
3237 tty
->TS_enter_bold_mode
= tgetstr ("md", address
);
3238 tty
->TS_enter_italic_mode
= tgetstr ("ZH", address
);
3239 tty
->TS_enter_dim_mode
= tgetstr ("mh", address
);
3240 tty
->TS_enter_reverse_mode
= tgetstr ("mr", address
);
3241 tty
->TS_enter_alt_charset_mode
= tgetstr ("as", address
);
3242 tty
->TS_exit_alt_charset_mode
= tgetstr ("ae", address
);
3243 tty
->TS_exit_attribute_mode
= tgetstr ("me", address
);
3245 MultiUp (tty
) = tgetstr ("UP", address
);
3246 MultiDown (tty
) = tgetstr ("DO", address
);
3247 MultiLeft (tty
) = tgetstr ("LE", address
);
3248 MultiRight (tty
) = tgetstr ("RI", address
);
3250 /* SVr4/ANSI color support. If "op" isn't available, don't support
3251 color because we can't switch back to the default foreground and
3253 tty
->TS_orig_pair
= tgetstr ("op", address
);
3254 if (tty
->TS_orig_pair
)
3256 tty
->TS_set_foreground
= tgetstr ("AF", address
);
3257 tty
->TS_set_background
= tgetstr ("AB", address
);
3258 if (!tty
->TS_set_foreground
)
3261 tty
->TS_set_foreground
= tgetstr ("Sf", address
);
3262 tty
->TS_set_background
= tgetstr ("Sb", address
);
3265 tty
->TN_max_colors
= tgetnum ("Co");
3266 tty
->TN_max_pairs
= tgetnum ("pa");
3268 tty
->TN_no_color_video
= tgetnum ("NC");
3269 if (tty
->TN_no_color_video
== -1)
3270 tty
->TN_no_color_video
= 0;
3273 tty_default_color_capabilities (tty
, 1);
3275 MagicWrap (tty
) = tgetflag ("xn");
3276 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3277 the former flag imply the latter. */
3278 AutoWrap (tty
) = MagicWrap (tty
) || tgetflag ("am");
3279 terminal
->memory_below_frame
= tgetflag ("db");
3280 tty
->TF_hazeltine
= tgetflag ("hz");
3281 terminal
->must_write_spaces
= tgetflag ("in");
3282 tty
->meta_key
= tgetflag ("km") || tgetflag ("MT");
3283 tty
->TF_insmode_motion
= tgetflag ("mi");
3284 tty
->TF_standout_motion
= tgetflag ("ms");
3285 tty
->TF_underscore
= tgetflag ("ul");
3286 tty
->TF_teleray
= tgetflag ("xt");
3291 struct frame
*f
= XFRAME (selected_frame
);
3293 initialize_w32_display (terminal
);
3295 FrameRows (tty
) = FRAME_LINES (f
);
3296 FrameCols (tty
) = FRAME_COLS (f
);
3297 tty
->specified_window
= FRAME_LINES (f
);
3299 FRAME_CAN_HAVE_SCROLL_BARS (f
) = 0;
3300 FRAME_VERTICAL_SCROLL_BAR_TYPE (f
) = vertical_scroll_bar_none
;
3301 terminal
->char_ins_del_ok
= 1;
3307 if (strcmp (terminal_type
, "internal") == 0)
3308 terminal
->type
= output_msdos_raw
;
3309 initialize_msdos_display (terminal
);
3311 get_tty_size (fileno (tty
->input
), &width
, &height
);
3312 FrameCols (tty
) = width
;
3313 FrameRows (tty
) = height
;
3314 terminal
->char_ins_del_ok
= 0;
3315 init_baud_rate (fileno (tty
->input
));
3318 tty
->output
= stdout
;
3320 /* The following two are inaccessible from w32console.c. */
3321 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
3322 terminal
->delete_terminal_hook
= &delete_tty
;
3324 tty
->name
= xstrdup (name
);
3325 terminal
->name
= xstrdup (name
);
3326 tty
->type
= xstrdup (terminal_type
);
3328 add_keyboard_wait_descriptor (0);
3330 tty
->delete_in_insert_mode
= 1;
3333 terminal
->scroll_region_ok
= 0;
3335 /* Seems to insert lines when it's not supposed to, messing up the
3336 display. In doing a trace, it didn't seem to be called much, so I
3337 don't think we're losing anything by turning it off. */
3338 terminal
->line_ins_del_ok
= 0;
3340 tty
->TN_max_colors
= 16; /* Required to be non-zero for tty-display-color-p */
3344 terminal
->mouse_position_hook
= term_mouse_position
;
3345 tty
->mouse_highlight
.mouse_face_window
= Qnil
;
3348 terminal
->kboard
= (KBOARD
*) xmalloc (sizeof (KBOARD
));
3349 init_kboard (terminal
->kboard
);
3350 KVAR (terminal
->kboard
, Vwindow_system
) = Qnil
;
3351 terminal
->kboard
->next_kboard
= all_kboards
;
3352 all_kboards
= terminal
->kboard
;
3353 terminal
->kboard
->reference_count
++;
3354 /* Don't let the initial kboard remain current longer than necessary.
3355 That would cause problems if a file loaded on startup tries to
3356 prompt in the mini-buffer. */
3357 if (current_kboard
== initial_kboard
)
3358 current_kboard
= terminal
->kboard
;
3360 term_get_fkeys (address
, terminal
->kboard
);
3362 /* Get frame size from system, or else from termcap. */
3365 get_tty_size (fileno (tty
->input
), &width
, &height
);
3366 FrameCols (tty
) = width
;
3367 FrameRows (tty
) = height
;
3370 if (FrameCols (tty
) <= 0)
3371 FrameCols (tty
) = tgetnum ("co");
3372 if (FrameRows (tty
) <= 0)
3373 FrameRows (tty
) = tgetnum ("li");
3375 if (FrameRows (tty
) < 3 || FrameCols (tty
) < 3)
3376 maybe_fatal (must_succeed
, terminal
,
3377 "Screen size %dx%d is too small",
3378 "Screen size %dx%d is too small",
3379 FrameCols (tty
), FrameRows (tty
));
3381 TabWidth (tty
) = tgetnum ("tw");
3384 tty
->TS_bell
= "\07";
3386 if (!tty
->TS_fwd_scroll
)
3387 tty
->TS_fwd_scroll
= Down (tty
);
3389 PC
= tty
->TS_pad_char
? *tty
->TS_pad_char
: 0;
3391 if (TabWidth (tty
) < 0)
3394 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3395 and newer termcap doc does not seem to say there is a default.
3396 if (!tty->Wcm->cm_tab)
3397 tty->Wcm->cm_tab = "\t";
3400 /* We don't support standout modes that use `magic cookies', so
3401 turn off any that do. */
3402 if (tty
->TS_standout_mode
&& tgetnum ("sg") >= 0)
3404 tty
->TS_standout_mode
= 0;
3405 tty
->TS_end_standout_mode
= 0;
3407 if (tty
->TS_enter_underline_mode
&& tgetnum ("ug") >= 0)
3409 tty
->TS_enter_underline_mode
= 0;
3410 tty
->TS_exit_underline_mode
= 0;
3413 /* If there's no standout mode, try to use underlining instead. */
3414 if (tty
->TS_standout_mode
== 0)
3416 tty
->TS_standout_mode
= tty
->TS_enter_underline_mode
;
3417 tty
->TS_end_standout_mode
= tty
->TS_exit_underline_mode
;
3420 /* If no `se' string, try using a `me' string instead.
3421 If that fails, we can't use standout mode at all. */
3422 if (tty
->TS_end_standout_mode
== 0)
3424 char *s
= tgetstr ("me", address
);
3426 tty
->TS_end_standout_mode
= s
;
3428 tty
->TS_standout_mode
= 0;
3431 if (tty
->TF_teleray
)
3433 tty
->Wcm
->cm_tab
= 0;
3434 /* We can't support standout mode, because it uses magic cookies. */
3435 tty
->TS_standout_mode
= 0;
3436 /* But that means we cannot rely on ^M to go to column zero! */
3438 /* LF can't be trusted either -- can alter hpos */
3439 /* if move at column 0 thru a line with TS_standout_mode */
3443 tty
->specified_window
= FrameRows (tty
);
3445 if (Wcm_init (tty
) == -1) /* can't do cursor motion */
3447 maybe_fatal (must_succeed
, terminal
,
3448 "Terminal type \"%s\" is not powerful enough to run Emacs",
3449 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3450 It lacks the ability to position the cursor.\n\
3451 If that is not the actual type of terminal you have,\n\
3452 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3453 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3455 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3456 # else /* TERMCAP */
3457 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3458 # endif /* TERMINFO */
3462 if (FrameRows (tty
) <= 0 || FrameCols (tty
) <= 0)
3463 maybe_fatal (must_succeed
, terminal
,
3464 "Could not determine the frame size",
3465 "Could not determine the frame size");
3467 tty
->delete_in_insert_mode
3468 = tty
->TS_delete_mode
&& tty
->TS_insert_mode
3469 && !strcmp (tty
->TS_delete_mode
, tty
->TS_insert_mode
);
3471 tty
->se_is_so
= (tty
->TS_standout_mode
3472 && tty
->TS_end_standout_mode
3473 && !strcmp (tty
->TS_standout_mode
, tty
->TS_end_standout_mode
));
3475 UseTabs (tty
) = tabs_safe_p (fileno (tty
->input
)) && TabWidth (tty
) == 8;
3477 terminal
->scroll_region_ok
3479 && (tty
->TS_set_window
|| tty
->TS_set_scroll_region
|| tty
->TS_set_scroll_region_1
));
3481 terminal
->line_ins_del_ok
3482 = (((tty
->TS_ins_line
|| tty
->TS_ins_multi_lines
)
3483 && (tty
->TS_del_line
|| tty
->TS_del_multi_lines
))
3484 || (terminal
->scroll_region_ok
3485 && tty
->TS_fwd_scroll
&& tty
->TS_rev_scroll
));
3487 terminal
->char_ins_del_ok
3488 = ((tty
->TS_ins_char
|| tty
->TS_insert_mode
3489 || tty
->TS_pad_inserted_char
|| tty
->TS_ins_multi_chars
)
3490 && (tty
->TS_del_char
|| tty
->TS_del_multi_chars
));
3492 terminal
->fast_clear_end_of_line
= tty
->TS_clr_line
!= 0;
3494 init_baud_rate (fileno (tty
->input
));
3496 #endif /* not DOS_NT */
3498 /* Init system terminal modes (RAW or CBREAK, etc.). */
3499 init_sys_modes (tty
);
3506 vfatal (const char *str
, va_list ap
)
3508 fprintf (stderr
, "emacs: ");
3509 vfprintf (stderr
, str
, ap
);
3510 if (!(strlen (str
) > 0 && str
[strlen (str
) - 1] == '\n'))
3511 fprintf (stderr
, "\n");
3517 /* Auxiliary error-handling function for init_tty.
3518 Delete TERMINAL, then call error or fatal with str1 or str2,
3519 respectively, according to whether MUST_SUCCEED is zero or not. */
3522 maybe_fatal (int must_succeed
, struct terminal
*terminal
,
3523 const char *str1
, const char *str2
, ...)
3526 va_start (ap
, str2
);
3528 delete_tty (terminal
);
3540 fatal (const char *str
, ...)
3550 /* Delete the given tty terminal, closing all frames on it. */
3553 delete_tty (struct terminal
*terminal
)
3555 struct tty_display_info
*tty
;
3557 /* Protect against recursive calls. delete_frame in
3558 delete_terminal calls us back when it deletes our last frame. */
3559 if (!terminal
->name
)
3562 if (terminal
->type
!= output_termcap
)
3565 tty
= terminal
->display_info
.tty
;
3567 if (tty
== tty_list
)
3568 tty_list
= tty
->next
;
3571 struct tty_display_info
*p
;
3572 for (p
= tty_list
; p
&& p
->next
!= tty
; p
= p
->next
)
3576 /* This should not happen. */
3579 p
->next
= tty
->next
;
3583 /* reset_sys_modes needs a valid device, so this call needs to be
3584 before delete_terminal. */
3585 reset_sys_modes (tty
);
3587 delete_terminal (terminal
);
3594 delete_keyboard_wait_descriptor (fileno (tty
->input
));
3595 if (tty
->input
!= stdin
)
3596 fclose (tty
->input
);
3598 if (tty
->output
&& tty
->output
!= stdout
&& tty
->output
!= tty
->input
)
3599 fclose (tty
->output
);
3600 if (tty
->termscript
)
3601 fclose (tty
->termscript
);
3603 xfree (tty
->old_tty
);
3605 xfree (tty
->termcap_strings_buffer
);
3606 xfree (tty
->termcap_term_buffer
);
3608 memset (tty
, 0, sizeof (struct tty_display_info
));
3614 /* Mark the pointers in the tty_display_info objects.
3615 Called by Fgarbage_collect. */
3620 struct tty_display_info
*tty
;
3622 for (tty
= tty_list
; tty
; tty
= tty
->next
)
3623 mark_object (tty
->top_frame
);
3631 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo
,
3632 doc
: /* Non-nil means the system uses terminfo rather than termcap.
3633 This variable can be used by terminal emulator packages. */);
3635 system_uses_terminfo
= 1;
3637 system_uses_terminfo
= 0;
3640 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions
,
3641 doc
: /* Functions to be run after suspending a tty.
3642 The functions are run with one argument, the terminal object to be suspended.
3643 See `suspend-tty'. */);
3644 Vsuspend_tty_functions
= Qnil
;
3647 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions
,
3648 doc
: /* Functions to be run after resuming a tty.
3649 The functions are run with one argument, the terminal object that was revived.
3650 See `resume-tty'. */);
3651 Vresume_tty_functions
= Qnil
;
3653 DEFVAR_BOOL ("visible-cursor", visible_cursor
,
3654 doc
: /* Non-nil means to make the cursor very visible.
3655 This only has an effect when running in a text terminal.
3656 What means \"very visible\" is up to your terminal. It may make the cursor
3657 bigger, or it may make it blink, or it may do nothing at all. */);
3660 defsubr (&Stty_display_color_p
);
3661 defsubr (&Stty_display_color_cells
);
3662 defsubr (&Stty_no_underline
);
3663 defsubr (&Stty_type
);
3664 defsubr (&Scontrolling_tty_p
);
3665 defsubr (&Stty_top_frame
);
3666 defsubr (&Ssuspend_tty
);
3667 defsubr (&Sresume_tty
);
3669 defsubr (&Sgpm_mouse_start
);
3670 defsubr (&Sgpm_mouse_stop
);
3671 #endif /* HAVE_GPM */
3674 default_orig_pair
= NULL
;
3675 default_set_foreground
= NULL
;
3676 default_set_background
= NULL
;
3677 #endif /* !DOS_NT */
3679 encode_terminal_src
= NULL
;
3680 encode_terminal_dst
= NULL
;