1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985-1987, 1993-1995, 1998, 2000-2012
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
32 #include "character.h"
36 #include "composite.h"
40 #include "termhooks.h"
41 #include "dispextern.h"
44 #include "blockinput.h"
45 #include "syssignal.h"
47 #include "intervals.h"
50 static int been_here
= -1;
66 /* The name of the default console device. */
68 #define DEV_TTY "CONOUT$"
71 #define DEV_TTY "/dev/tty"
74 static void tty_set_scroll_region (struct frame
*f
, int start
, int stop
);
75 static void turn_on_face (struct frame
*, int face_id
);
76 static void turn_off_face (struct frame
*, int face_id
);
77 static void tty_turn_off_highlight (struct tty_display_info
*);
78 static void tty_show_cursor (struct tty_display_info
*);
79 static void tty_hide_cursor (struct tty_display_info
*);
80 static void tty_background_highlight (struct tty_display_info
*tty
);
81 static struct terminal
*get_tty_terminal (Lisp_Object
, int);
82 static void clear_tty_hooks (struct terminal
*terminal
);
83 static void set_tty_hooks (struct terminal
*terminal
);
84 static void dissociate_if_controlling_tty (int fd
);
85 static void delete_tty (struct terminal
*);
86 static _Noreturn
void maybe_fatal (int must_succeed
, struct terminal
*terminal
,
87 const char *str1
, const char *str2
, ...)
88 ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
89 static _Noreturn
void vfatal (const char *str
, va_list ap
)
90 ATTRIBUTE_FORMAT_PRINTF (1, 0);
93 #define OUTPUT(tty, a) \
94 emacs_tputs ((tty), a, \
95 (int) (FRAME_LINES (XFRAME (selected_frame)) \
99 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
100 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
102 #define OUTPUT_IF(tty, a) \
108 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
110 /* Display space properties */
112 /* Chain of all tty device parameters. */
113 struct tty_display_info
*tty_list
;
115 /* Meaning of bits in no_color_video. Each bit set means that the
116 corresponding attribute cannot be combined with colors. */
120 NC_STANDOUT
= 1 << 0,
121 NC_UNDERLINE
= 1 << 1,
132 /* The largest frame width in any call to calculate_costs. */
134 static int max_frame_cols
;
139 #include <sys/fcntl.h>
141 /* The device for which we have enabled gpm support (or NULL). */
142 struct tty_display_info
*gpm_tty
= NULL
;
144 /* Last recorded mouse coordinates. */
145 static int last_mouse_x
, last_mouse_y
;
146 #endif /* HAVE_GPM */
148 /* Ring the bell on a tty. */
151 tty_ring_bell (struct frame
*f
)
153 struct tty_display_info
*tty
= FRAME_TTY (f
);
157 OUTPUT (tty
, (tty
->TS_visible_bell
&& visible_bell
158 ? tty
->TS_visible_bell
160 fflush (tty
->output
);
164 /* Set up termcap modes for Emacs. */
167 tty_set_terminal_modes (struct terminal
*terminal
)
169 struct tty_display_info
*tty
= terminal
->display_info
.tty
;
173 if (tty
->TS_termcap_modes
)
174 OUTPUT (tty
, tty
->TS_termcap_modes
);
177 /* Output enough newlines to scroll all the old screen contents
178 off the screen, so it won't be overwritten and lost. */
181 for (i
= 0; i
< FRAME_LINES (XFRAME (selected_frame
)); i
++)
185 OUTPUT_IF (tty
, visible_cursor
? tty
->TS_cursor_visible
: tty
->TS_cursor_normal
);
186 OUTPUT_IF (tty
, tty
->TS_keypad_mode
);
188 fflush (tty
->output
);
192 /* Reset termcap modes before exiting Emacs. */
195 tty_reset_terminal_modes (struct terminal
*terminal
)
197 struct tty_display_info
*tty
= terminal
->display_info
.tty
;
201 tty_turn_off_highlight (tty
);
202 tty_turn_off_insert (tty
);
203 OUTPUT_IF (tty
, tty
->TS_end_keypad_mode
);
204 OUTPUT_IF (tty
, tty
->TS_cursor_normal
);
205 OUTPUT_IF (tty
, tty
->TS_end_termcap_modes
);
206 OUTPUT_IF (tty
, tty
->TS_orig_pair
);
207 /* Output raw CR so kernel can track the cursor hpos. */
210 fflush (tty
->output
);
214 /* Flag the end of a display update on a termcap terminal. */
217 tty_update_end (struct frame
*f
)
219 struct tty_display_info
*tty
= FRAME_TTY (f
);
221 if (!XWINDOW (selected_window
)->cursor_off_p
)
222 tty_show_cursor (tty
);
223 tty_turn_off_insert (tty
);
224 tty_background_highlight (tty
);
227 /* The implementation of set_terminal_window for termcap frames. */
230 tty_set_terminal_window (struct frame
*f
, int size
)
232 struct tty_display_info
*tty
= FRAME_TTY (f
);
234 tty
->specified_window
= size
? size
: FRAME_LINES (f
);
235 if (FRAME_SCROLL_REGION_OK (f
))
236 tty_set_scroll_region (f
, 0, tty
->specified_window
);
240 tty_set_scroll_region (struct frame
*f
, int start
, int stop
)
243 struct tty_display_info
*tty
= FRAME_TTY (f
);
245 if (tty
->TS_set_scroll_region
)
246 buf
= tparam (tty
->TS_set_scroll_region
, 0, 0, start
, stop
- 1, 0, 0);
247 else if (tty
->TS_set_scroll_region_1
)
248 buf
= tparam (tty
->TS_set_scroll_region_1
, 0, 0,
249 FRAME_LINES (f
), start
,
250 FRAME_LINES (f
) - stop
,
253 buf
= tparam (tty
->TS_set_window
, 0, 0, start
, 0, stop
, FRAME_COLS (f
));
262 tty_turn_on_insert (struct tty_display_info
*tty
)
264 if (!tty
->insert_mode
)
265 OUTPUT (tty
, tty
->TS_insert_mode
);
266 tty
->insert_mode
= 1;
270 tty_turn_off_insert (struct tty_display_info
*tty
)
272 if (tty
->insert_mode
)
273 OUTPUT (tty
, tty
->TS_end_insert_mode
);
274 tty
->insert_mode
= 0;
277 /* Handle highlighting. */
280 tty_turn_off_highlight (struct tty_display_info
*tty
)
282 if (tty
->standout_mode
)
283 OUTPUT_IF (tty
, tty
->TS_end_standout_mode
);
284 tty
->standout_mode
= 0;
288 tty_turn_on_highlight (struct tty_display_info
*tty
)
290 if (!tty
->standout_mode
)
291 OUTPUT_IF (tty
, tty
->TS_standout_mode
);
292 tty
->standout_mode
= 1;
296 tty_toggle_highlight (struct tty_display_info
*tty
)
298 if (tty
->standout_mode
)
299 tty_turn_off_highlight (tty
);
301 tty_turn_on_highlight (tty
);
305 /* Make cursor invisible. */
308 tty_hide_cursor (struct tty_display_info
*tty
)
310 if (tty
->cursor_hidden
== 0)
312 tty
->cursor_hidden
= 1;
313 OUTPUT_IF (tty
, tty
->TS_cursor_invisible
);
318 /* Ensure that cursor is visible. */
321 tty_show_cursor (struct tty_display_info
*tty
)
323 if (tty
->cursor_hidden
)
325 tty
->cursor_hidden
= 0;
326 OUTPUT_IF (tty
, tty
->TS_cursor_normal
);
328 OUTPUT_IF (tty
, tty
->TS_cursor_visible
);
333 /* Set standout mode to the state it should be in for
334 empty space inside windows. What this is,
335 depends on the user option inverse-video. */
338 tty_background_highlight (struct tty_display_info
*tty
)
341 tty_turn_on_highlight (tty
);
343 tty_turn_off_highlight (tty
);
346 /* Set standout mode to the mode specified for the text to be output. */
349 tty_highlight_if_desired (struct tty_display_info
*tty
)
352 tty_turn_on_highlight (tty
);
354 tty_turn_off_highlight (tty
);
358 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
359 frame-relative coordinates. */
362 tty_cursor_to (struct frame
*f
, int vpos
, int hpos
)
364 struct tty_display_info
*tty
= FRAME_TTY (f
);
366 /* Detect the case where we are called from reset_sys_modes
367 and the costs have never been calculated. Do nothing. */
368 if (! tty
->costs_set
)
371 if (curY (tty
) == vpos
372 && curX (tty
) == hpos
)
374 if (!tty
->TF_standout_motion
)
375 tty_background_highlight (tty
);
376 if (!tty
->TF_insmode_motion
)
377 tty_turn_off_insert (tty
);
378 cmgoto (tty
, vpos
, hpos
);
381 /* Similar but don't take any account of the wasted characters. */
384 tty_raw_cursor_to (struct frame
*f
, int row
, int col
)
386 struct tty_display_info
*tty
= FRAME_TTY (f
);
388 if (curY (tty
) == row
389 && curX (tty
) == col
)
391 if (!tty
->TF_standout_motion
)
392 tty_background_highlight (tty
);
393 if (!tty
->TF_insmode_motion
)
394 tty_turn_off_insert (tty
);
395 cmgoto (tty
, row
, col
);
398 /* Erase operations */
400 /* Clear from cursor to end of frame on a termcap device. */
403 tty_clear_to_end (struct frame
*f
)
406 struct tty_display_info
*tty
= FRAME_TTY (f
);
408 if (tty
->TS_clr_to_bottom
)
410 tty_background_highlight (tty
);
411 OUTPUT (tty
, tty
->TS_clr_to_bottom
);
415 for (i
= curY (tty
); i
< FRAME_LINES (f
); i
++)
418 clear_end_of_line (f
, FRAME_COLS (f
));
423 /* Clear an entire termcap frame. */
426 tty_clear_frame (struct frame
*f
)
428 struct tty_display_info
*tty
= FRAME_TTY (f
);
430 if (tty
->TS_clr_frame
)
432 tty_background_highlight (tty
);
433 OUTPUT (tty
, tty
->TS_clr_frame
);
443 /* An implementation of clear_end_of_line for termcap frames.
445 Note that the cursor may be moved, on terminals lacking a `ce' string. */
448 tty_clear_end_of_line (struct frame
*f
, int first_unused_hpos
)
451 struct tty_display_info
*tty
= FRAME_TTY (f
);
453 /* Detect the case where we are called from reset_sys_modes
454 and the costs have never been calculated. Do nothing. */
455 if (! tty
->costs_set
)
458 if (curX (tty
) >= first_unused_hpos
)
460 tty_background_highlight (tty
);
461 if (tty
->TS_clr_line
)
463 OUTPUT1 (tty
, tty
->TS_clr_line
);
466 { /* have to do it the hard way */
467 tty_turn_off_insert (tty
);
469 /* Do not write in last row last col with Auto-wrap on. */
471 && curY (tty
) == FrameRows (tty
) - 1
472 && first_unused_hpos
== FrameCols (tty
))
475 for (i
= curX (tty
); i
< first_unused_hpos
; i
++)
478 fputc (' ', tty
->termscript
);
479 fputc (' ', tty
->output
);
481 cmplus (tty
, first_unused_hpos
- curX (tty
));
485 /* Buffers to store the source and result of code conversion for terminal. */
486 static unsigned char *encode_terminal_src
;
487 static unsigned char *encode_terminal_dst
;
488 /* Allocated sizes of the above buffers. */
489 static ptrdiff_t encode_terminal_src_size
;
490 static ptrdiff_t encode_terminal_dst_size
;
492 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
493 Set CODING->produced to the byte-length of the resulting byte
494 sequence, and return a pointer to that byte sequence. */
497 encode_terminal_code (struct glyph
*src
, int src_len
, struct coding_system
*coding
)
499 struct glyph
*src_end
= src
+ src_len
;
501 ptrdiff_t nchars
, nbytes
, required
;
502 ptrdiff_t tlen
= GLYPH_TABLE_LENGTH
;
503 register Lisp_Object
*tbase
= GLYPH_TABLE_BASE
;
504 Lisp_Object charset_list
;
506 /* Allocate sufficient size of buffer to store all characters in
507 multibyte-form. But, it may be enlarged on demand if
508 Vglyph_table contains a string or a composite glyph is
510 if (min (PTRDIFF_MAX
, SIZE_MAX
) / MAX_MULTIBYTE_LENGTH
< src_len
)
511 memory_full (SIZE_MAX
);
513 required
*= MAX_MULTIBYTE_LENGTH
;
514 if (encode_terminal_src_size
< required
)
516 encode_terminal_src
= xrealloc (encode_terminal_src
, required
);
517 encode_terminal_src_size
= required
;
520 charset_list
= coding_charset_list (coding
);
522 buf
= encode_terminal_src
;
524 while (src
< src_end
)
526 if (src
->type
== COMPOSITE_GLYPH
)
528 struct composition
*cmp
IF_LINT (= NULL
);
529 Lisp_Object gstring
IF_LINT (= Qnil
);
532 nbytes
= buf
- encode_terminal_src
;
533 if (src
->u
.cmp
.automatic
)
535 gstring
= composition_gstring_from_id (src
->u
.cmp
.id
);
536 required
= src
->slice
.cmp
.to
- src
->slice
.cmp
.from
+ 1;
540 cmp
= composition_table
[src
->u
.cmp
.id
];
541 required
= cmp
->glyph_len
;
542 required
*= MAX_MULTIBYTE_LENGTH
;
545 if (encode_terminal_src_size
- nbytes
< required
)
547 encode_terminal_src
=
548 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
549 required
- (encode_terminal_src_size
- nbytes
),
551 buf
= encode_terminal_src
+ nbytes
;
554 if (src
->u
.cmp
.automatic
)
555 for (i
= src
->slice
.cmp
.from
; i
<= src
->slice
.cmp
.to
; i
++)
557 Lisp_Object g
= LGSTRING_GLYPH (gstring
, i
);
558 int c
= LGLYPH_CHAR (g
);
560 if (! char_charset (c
, charset_list
, NULL
))
562 buf
+= CHAR_STRING (c
, buf
);
566 for (i
= 0; i
< cmp
->glyph_len
; i
++)
568 int c
= COMPOSITION_GLYPH (cmp
, i
);
570 /* TAB in a composition means display glyphs with
571 padding space on the left or right. */
574 if (char_charset (c
, charset_list
, NULL
))
576 if (CHAR_WIDTH (c
) == 0
577 && i
> 0 && COMPOSITION_GLYPH (cmp
, i
- 1) == '\t')
578 /* Should be left-padded */
580 buf
+= CHAR_STRING (' ', buf
);
586 buf
+= CHAR_STRING (c
, buf
);
590 /* We must skip glyphs to be padded for a wide character. */
591 else if (! CHAR_GLYPH_PADDING_P (*src
))
598 SET_GLYPH_FROM_CHAR_GLYPH (g
, src
[0]);
600 if (GLYPH_INVALID_P (g
) || GLYPH_SIMPLE_P (tbase
, tlen
, g
))
602 /* This glyph doesn't have an entry in Vglyph_table. */
607 /* This glyph has an entry in Vglyph_table,
608 so process any alias before testing for simpleness. */
609 GLYPH_FOLLOW_ALIASES (tbase
, tlen
, g
);
611 if (GLYPH_SIMPLE_P (tbase
, tlen
, g
))
612 /* We set the multi-byte form of a character in G
613 (that should be an ASCII character) at WORKBUF. */
616 /* We have a string in Vglyph_table. */
617 string
= tbase
[GLYPH_CHAR (g
)];
622 nbytes
= buf
- encode_terminal_src
;
623 if (encode_terminal_src_size
- nbytes
< MAX_MULTIBYTE_LENGTH
)
625 encode_terminal_src
=
626 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
627 MAX_MULTIBYTE_LENGTH
, -1, 1);
628 buf
= encode_terminal_src
+ nbytes
;
631 || char_charset (c
, charset_list
, NULL
))
633 /* Store the multibyte form of C at BUF. */
634 buf
+= CHAR_STRING (c
, buf
);
639 /* C is not encodable. */
642 while (src
+ 1 < src_end
&& CHAR_GLYPH_PADDING_P (src
[1]))
652 if (! STRING_MULTIBYTE (string
))
653 string
= string_to_multibyte (string
);
654 nbytes
= buf
- encode_terminal_src
;
655 if (encode_terminal_src_size
- nbytes
< SBYTES (string
))
657 encode_terminal_src
=
658 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
660 - (encode_terminal_src_size
- nbytes
)),
662 buf
= encode_terminal_src
+ nbytes
;
664 memcpy (buf
, SDATA (string
), SBYTES (string
));
665 buf
+= SBYTES (string
);
666 nchars
+= SCHARS (string
);
674 coding
->produced
= 0;
678 nbytes
= buf
- encode_terminal_src
;
679 coding
->source
= encode_terminal_src
;
680 if (encode_terminal_dst_size
== 0)
682 encode_terminal_dst
= xrealloc (encode_terminal_dst
,
683 encode_terminal_src_size
);
684 encode_terminal_dst_size
= encode_terminal_src_size
;
686 coding
->destination
= encode_terminal_dst
;
687 coding
->dst_bytes
= encode_terminal_dst_size
;
688 encode_coding_object (coding
, Qnil
, 0, 0, nchars
, nbytes
, Qnil
);
689 /* coding->destination may have been reallocated. */
690 encode_terminal_dst
= coding
->destination
;
691 encode_terminal_dst_size
= coding
->dst_bytes
;
693 return (encode_terminal_dst
);
698 /* An implementation of write_glyphs for termcap frames. */
701 tty_write_glyphs (struct frame
*f
, struct glyph
*string
, int len
)
703 unsigned char *conversion_buffer
;
704 struct coding_system
*coding
;
707 struct tty_display_info
*tty
= FRAME_TTY (f
);
709 tty_turn_off_insert (tty
);
710 tty_hide_cursor (tty
);
712 /* Don't dare write in last column of bottom line, if Auto-Wrap,
713 since that would scroll the whole frame on some terminals. */
716 && curY (tty
) + 1 == FRAME_LINES (f
)
717 && (curX (tty
) + len
) == FRAME_COLS (f
))
724 /* If terminal_coding does any conversion, use it, otherwise use
725 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
726 because it always return 1 if the member src_multibyte is 1. */
727 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
728 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
729 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
731 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
733 for (stringlen
= len
; stringlen
!= 0; stringlen
-= n
)
735 /* Identify a run of glyphs with the same face. */
736 int face_id
= string
->face_id
;
738 for (n
= 1; n
< stringlen
; ++n
)
739 if (string
[n
].face_id
!= face_id
)
742 /* Turn appearance modes of the face of the run on. */
743 tty_highlight_if_desired (tty
);
744 turn_on_face (f
, face_id
);
747 /* This is the last run. */
748 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
749 conversion_buffer
= encode_terminal_code (string
, n
, coding
);
750 if (coding
->produced
> 0)
753 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
754 if (ferror (tty
->output
))
755 clearerr (tty
->output
);
757 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
762 /* Turn appearance modes off. */
763 turn_off_face (f
, face_id
);
764 tty_turn_off_highlight (tty
);
770 #ifdef HAVE_GPM /* Only used by GPM code. */
773 tty_write_glyphs_with_face (register struct frame
*f
, register struct glyph
*string
,
774 register int len
, register int face_id
)
776 unsigned char *conversion_buffer
;
777 struct coding_system
*coding
;
779 struct tty_display_info
*tty
= FRAME_TTY (f
);
781 tty_turn_off_insert (tty
);
782 tty_hide_cursor (tty
);
784 /* Don't dare write in last column of bottom line, if Auto-Wrap,
785 since that would scroll the whole frame on some terminals. */
788 && curY (tty
) + 1 == FRAME_LINES (f
)
789 && (curX (tty
) + len
) == FRAME_COLS (f
))
796 /* If terminal_coding does any conversion, use it, otherwise use
797 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
798 because it always return 1 if the member src_multibyte is 1. */
799 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
800 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
801 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
803 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
805 /* Turn appearance modes of the face. */
806 tty_highlight_if_desired (tty
);
807 turn_on_face (f
, face_id
);
809 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
810 conversion_buffer
= encode_terminal_code (string
, len
, coding
);
811 if (coding
->produced
> 0)
814 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
815 if (ferror (tty
->output
))
816 clearerr (tty
->output
);
818 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
822 /* Turn appearance modes off. */
823 turn_off_face (f
, face_id
);
824 tty_turn_off_highlight (tty
);
830 /* An implementation of insert_glyphs for termcap frames. */
833 tty_insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
)
836 struct glyph
*glyph
= NULL
;
837 unsigned char *conversion_buffer
;
838 unsigned char space
[1];
839 struct coding_system
*coding
;
841 struct tty_display_info
*tty
= FRAME_TTY (f
);
843 if (tty
->TS_ins_multi_chars
)
845 buf
= tparam (tty
->TS_ins_multi_chars
, 0, 0, len
, 0, 0, 0);
849 write_glyphs (f
, start
, len
);
853 tty_turn_on_insert (tty
);
857 space
[0] = SPACEGLYPH
;
859 /* If terminal_coding does any conversion, use it, otherwise use
860 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
861 because it always return 1 if the member src_multibyte is 1. */
862 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
863 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
864 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
866 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
870 OUTPUT1_IF (tty
, tty
->TS_ins_char
);
873 conversion_buffer
= space
;
874 coding
->produced
= 1;
878 tty_highlight_if_desired (tty
);
879 turn_on_face (f
, start
->face_id
);
882 /* We must open sufficient space for a character which
883 occupies more than one column. */
884 while (len
&& CHAR_GLYPH_PADDING_P (*start
))
886 OUTPUT1_IF (tty
, tty
->TS_ins_char
);
891 /* This is the last glyph. */
892 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
894 conversion_buffer
= encode_terminal_code (glyph
, 1, coding
);
897 if (coding
->produced
> 0)
900 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
901 if (ferror (tty
->output
))
902 clearerr (tty
->output
);
904 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
908 OUTPUT1_IF (tty
, tty
->TS_pad_inserted_char
);
911 turn_off_face (f
, glyph
->face_id
);
912 tty_turn_off_highlight (tty
);
919 /* An implementation of delete_glyphs for termcap frames. */
922 tty_delete_glyphs (struct frame
*f
, int n
)
927 struct tty_display_info
*tty
= FRAME_TTY (f
);
929 if (tty
->delete_in_insert_mode
)
931 tty_turn_on_insert (tty
);
935 tty_turn_off_insert (tty
);
936 OUTPUT_IF (tty
, tty
->TS_delete_mode
);
939 if (tty
->TS_del_multi_chars
)
941 buf
= tparam (tty
->TS_del_multi_chars
, 0, 0, n
, 0, 0, 0);
946 for (i
= 0; i
< n
; i
++)
947 OUTPUT1 (tty
, tty
->TS_del_char
);
948 if (!tty
->delete_in_insert_mode
)
949 OUTPUT_IF (tty
, tty
->TS_end_delete_mode
);
952 /* An implementation of ins_del_lines for termcap frames. */
955 tty_ins_del_lines (struct frame
*f
, int vpos
, int n
)
957 struct tty_display_info
*tty
= FRAME_TTY (f
);
959 n
> 0 ? tty
->TS_ins_multi_lines
: tty
->TS_del_multi_lines
;
960 const char *single
= n
> 0 ? tty
->TS_ins_line
: tty
->TS_del_line
;
961 const char *scroll
= n
> 0 ? tty
->TS_rev_scroll
: tty
->TS_fwd_scroll
;
963 register int i
= n
> 0 ? n
: -n
;
966 /* If the lines below the insertion are being pushed
967 into the end of the window, this is the same as clearing;
968 and we know the lines are already clear, since the matching
969 deletion has already been done. So can ignore this. */
970 /* If the lines below the deletion are blank lines coming
971 out of the end of the window, don't bother,
972 as there will be a matching inslines later that will flush them. */
973 if (FRAME_SCROLL_REGION_OK (f
)
974 && vpos
+ i
>= tty
->specified_window
)
976 if (!FRAME_MEMORY_BELOW_FRAME (f
)
977 && vpos
+ i
>= FRAME_LINES (f
))
982 raw_cursor_to (f
, vpos
, 0);
983 tty_background_highlight (tty
);
984 buf
= tparam (multi
, 0, 0, i
, 0, 0, 0);
990 raw_cursor_to (f
, vpos
, 0);
991 tty_background_highlight (tty
);
993 OUTPUT (tty
, single
);
999 tty_set_scroll_region (f
, vpos
, tty
->specified_window
);
1001 raw_cursor_to (f
, tty
->specified_window
- 1, 0);
1003 raw_cursor_to (f
, vpos
, 0);
1004 tty_background_highlight (tty
);
1006 OUTPUTL (tty
, scroll
, tty
->specified_window
- vpos
);
1007 tty_set_scroll_region (f
, 0, tty
->specified_window
);
1010 if (!FRAME_SCROLL_REGION_OK (f
)
1011 && FRAME_MEMORY_BELOW_FRAME (f
)
1014 cursor_to (f
, FRAME_LINES (f
) + n
, 0);
1019 /* Compute cost of sending "str", in characters,
1020 not counting any line-dependent padding. */
1023 string_cost (const char *str
)
1027 tputs (str
, 0, evalcost
);
1031 /* Compute cost of sending "str", in characters,
1032 counting any line-dependent padding at one line. */
1035 string_cost_one_line (const char *str
)
1039 tputs (str
, 1, evalcost
);
1043 /* Compute per line amount of line-dependent padding,
1044 in tenths of characters. */
1047 per_line_cost (const char *str
)
1051 tputs (str
, 0, evalcost
);
1054 tputs (str
, 10, evalcost
);
1058 /* char_ins_del_cost[n] is cost of inserting N characters.
1059 char_ins_del_cost[-n] is cost of deleting N characters.
1060 The length of this vector is based on max_frame_cols. */
1062 int *char_ins_del_vector
;
1064 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1068 calculate_ins_del_char_costs (struct frame
*f
)
1070 struct tty_display_info
*tty
= FRAME_TTY (f
);
1071 int ins_startup_cost
, del_startup_cost
;
1072 int ins_cost_per_char
, del_cost_per_char
;
1076 if (tty
->TS_ins_multi_chars
)
1078 ins_cost_per_char
= 0;
1079 ins_startup_cost
= string_cost_one_line (tty
->TS_ins_multi_chars
);
1081 else if (tty
->TS_ins_char
|| tty
->TS_pad_inserted_char
1082 || (tty
->TS_insert_mode
&& tty
->TS_end_insert_mode
))
1084 ins_startup_cost
= (30 * (string_cost (tty
->TS_insert_mode
)
1085 + string_cost (tty
->TS_end_insert_mode
))) / 100;
1086 ins_cost_per_char
= (string_cost_one_line (tty
->TS_ins_char
)
1087 + string_cost_one_line (tty
->TS_pad_inserted_char
));
1091 ins_startup_cost
= 9999;
1092 ins_cost_per_char
= 0;
1095 if (tty
->TS_del_multi_chars
)
1097 del_cost_per_char
= 0;
1098 del_startup_cost
= string_cost_one_line (tty
->TS_del_multi_chars
);
1100 else if (tty
->TS_del_char
)
1102 del_startup_cost
= (string_cost (tty
->TS_delete_mode
)
1103 + string_cost (tty
->TS_end_delete_mode
));
1104 if (tty
->delete_in_insert_mode
)
1105 del_startup_cost
/= 2;
1106 del_cost_per_char
= string_cost_one_line (tty
->TS_del_char
);
1110 del_startup_cost
= 9999;
1111 del_cost_per_char
= 0;
1114 /* Delete costs are at negative offsets */
1115 p
= &char_ins_del_cost (f
)[0];
1116 for (i
= FRAME_COLS (f
); --i
>= 0;)
1117 *--p
= (del_startup_cost
+= del_cost_per_char
);
1119 /* Doing nothing is free */
1120 p
= &char_ins_del_cost (f
)[0];
1123 /* Insert costs are at positive offsets */
1124 for (i
= FRAME_COLS (f
); --i
>= 0;)
1125 *p
++ = (ins_startup_cost
+= ins_cost_per_char
);
1129 calculate_costs (struct frame
*frame
)
1131 FRAME_COST_BAUD_RATE (frame
) = baud_rate
;
1133 if (FRAME_TERMCAP_P (frame
))
1135 struct tty_display_info
*tty
= FRAME_TTY (frame
);
1136 register const char *f
= (tty
->TS_set_scroll_region
1137 ? tty
->TS_set_scroll_region
1138 : tty
->TS_set_scroll_region_1
);
1140 FRAME_SCROLL_REGION_COST (frame
) = string_cost (f
);
1144 /* These variables are only used for terminal stuff. They are
1145 allocated once for the terminal frame of X-windows emacs, but not
1148 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1149 X turns off char_ins_del_ok. */
1151 max_frame_cols
= max (max_frame_cols
, FRAME_COLS (frame
));
1152 if ((min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (int) - 1) / 2
1154 memory_full (SIZE_MAX
);
1156 char_ins_del_vector
=
1157 xrealloc (char_ins_del_vector
,
1158 (sizeof (int) + 2 * sizeof (int) * max_frame_cols
));
1160 memset (char_ins_del_vector
, 0,
1161 (sizeof (int) + 2 * sizeof (int) * max_frame_cols
));
1164 if (f
&& (!tty
->TS_ins_line
&& !tty
->TS_del_line
))
1165 do_line_insertion_deletion_costs (frame
,
1166 tty
->TS_rev_scroll
, tty
->TS_ins_multi_lines
,
1167 tty
->TS_fwd_scroll
, tty
->TS_del_multi_lines
,
1170 do_line_insertion_deletion_costs (frame
,
1171 tty
->TS_ins_line
, tty
->TS_ins_multi_lines
,
1172 tty
->TS_del_line
, tty
->TS_del_multi_lines
,
1175 calculate_ins_del_char_costs (frame
);
1177 /* Don't use TS_repeat if its padding is worse than sending the chars */
1178 if (tty
->TS_repeat
&& per_line_cost (tty
->TS_repeat
) * baud_rate
< 9000)
1179 tty
->RPov
= string_cost (tty
->TS_repeat
);
1181 tty
->RPov
= FRAME_COLS (frame
) * 2;
1183 cmcostinit (FRAME_TTY (frame
)); /* set up cursor motion costs */
1188 const char *cap
, *name
;
1191 /* Termcap capability names that correspond directly to X keysyms.
1192 Some of these (marked "terminfo") aren't supplied by old-style
1193 (Berkeley) termcap entries. They're listed in X keysym order;
1194 except we put the keypad keys first, so that if they clash with
1195 other keys (as on the IBM PC keyboard) they get overridden.
1198 static const struct fkey_table keys
[] =
1200 {"kh", "home"}, /* termcap */
1201 {"kl", "left"}, /* termcap */
1202 {"ku", "up"}, /* termcap */
1203 {"kr", "right"}, /* termcap */
1204 {"kd", "down"}, /* termcap */
1205 {"%8", "prior"}, /* terminfo */
1206 {"%5", "next"}, /* terminfo */
1207 {"@7", "end"}, /* terminfo */
1208 {"@1", "begin"}, /* terminfo */
1209 {"*6", "select"}, /* terminfo */
1210 {"%9", "print"}, /* terminfo */
1211 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1213 * "insert" --- see below
1215 {"&8", "undo"}, /* terminfo */
1216 {"%0", "redo"}, /* terminfo */
1217 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1218 {"@0", "find"}, /* terminfo */
1219 {"@2", "cancel"}, /* terminfo */
1220 {"%1", "help"}, /* terminfo */
1222 * "break" goes here, but can't be reliably intercepted with termcap
1224 {"&4", "reset"}, /* terminfo --- actually `restart' */
1226 * "system" and "user" --- no termcaps
1228 {"kE", "clearline"}, /* terminfo */
1229 {"kA", "insertline"}, /* terminfo */
1230 {"kL", "deleteline"}, /* terminfo */
1231 {"kI", "insertchar"}, /* terminfo */
1232 {"kD", "deletechar"}, /* terminfo */
1233 {"kB", "backtab"}, /* terminfo */
1235 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1237 {"@8", "kp-enter"}, /* terminfo */
1239 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1240 * "kp-multiply", "kp-add", "kp-separator",
1241 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1242 * --- no termcaps for any of these.
1244 {"K4", "kp-1"}, /* terminfo */
1246 * "kp-2" --- no termcap
1248 {"K5", "kp-3"}, /* terminfo */
1250 * "kp-4" --- no termcap
1252 {"K2", "kp-5"}, /* terminfo */
1254 * "kp-6" --- no termcap
1256 {"K1", "kp-7"}, /* terminfo */
1258 * "kp-8" --- no termcap
1260 {"K3", "kp-9"}, /* terminfo */
1262 * "kp-equal" --- no termcap
1274 {"&0", "S-cancel"}, /*shifted cancel key*/
1275 {"&9", "S-begin"}, /*shifted begin key*/
1276 {"*0", "S-find"}, /*shifted find key*/
1277 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1278 {"*4", "S-delete"}, /*shifted delete-character key*/
1279 {"*7", "S-end"}, /*shifted end key*/
1280 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1281 {"#1", "S-help"}, /*shifted help key*/
1282 {"#2", "S-home"}, /*shifted home key*/
1283 {"#3", "S-insert"}, /*shifted insert-character key*/
1284 {"#4", "S-left"}, /*shifted left-arrow key*/
1285 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1286 {"%c", "S-next"}, /*shifted next key*/
1287 {"%e", "S-prior"}, /*shifted previous key*/
1288 {"%f", "S-print"}, /*shifted print key*/
1289 {"%g", "S-redo"}, /*shifted redo key*/
1290 {"%i", "S-right"}, /*shifted right-arrow key*/
1291 {"!3", "S-undo"} /*shifted undo key*/
1295 static char **term_get_fkeys_address
;
1296 static KBOARD
*term_get_fkeys_kboard
;
1297 static Lisp_Object
term_get_fkeys_1 (void);
1299 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1300 This function scans the termcap function key sequence entries, and
1301 adds entries to Vinput_decode_map for each function key it finds. */
1304 term_get_fkeys (char **address
, KBOARD
*kboard
)
1306 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1307 errors during the call. The only errors should be from Fdefine_key
1308 when given a key sequence containing an invalid prefix key. If the
1309 termcap defines function keys which use a prefix that is already bound
1310 to a command by the default bindings, we should silently ignore that
1311 function key specification, rather than giving the user an error and
1312 refusing to run at all on such a terminal. */
1314 term_get_fkeys_address
= address
;
1315 term_get_fkeys_kboard
= kboard
;
1316 internal_condition_case (term_get_fkeys_1
, Qerror
, Fidentity
);
1320 term_get_fkeys_1 (void)
1324 char **address
= term_get_fkeys_address
;
1325 KBOARD
*kboard
= term_get_fkeys_kboard
;
1327 /* This can happen if CANNOT_DUMP or with strange options. */
1328 if (!KEYMAPP (KVAR (kboard
, Vinput_decode_map
)))
1329 kset_input_decode_map (kboard
, Fmake_sparse_keymap (Qnil
));
1331 for (i
= 0; i
< (sizeof (keys
)/sizeof (keys
[0])); i
++)
1333 char *sequence
= tgetstr (keys
[i
].cap
, address
);
1335 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (sequence
),
1336 Fmake_vector (make_number (1),
1337 intern (keys
[i
].name
)));
1340 /* The uses of the "k0" capability are inconsistent; sometimes it
1341 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1342 We will attempt to politely accommodate both systems by testing for
1343 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1346 const char *k_semi
= tgetstr ("k;", address
);
1347 const char *k0
= tgetstr ("k0", address
);
1348 const char *k0_name
= "f10";
1353 /* Define f0 first, so that f10 takes precedence in case the
1354 key sequences happens to be the same. */
1355 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k0
),
1356 Fmake_vector (make_number (1), intern ("f0")));
1357 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k_semi
),
1358 Fmake_vector (make_number (1), intern ("f10")));
1361 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k0
),
1362 Fmake_vector (make_number (1), intern (k0_name
)));
1365 /* Set up cookies for numbered function keys above f10. */
1367 char fcap
[3], fkey
[4];
1369 fcap
[0] = 'F'; fcap
[2] = '\0';
1370 for (i
= 11; i
< 64; i
++)
1373 fcap
[1] = '1' + i
- 11;
1375 fcap
[1] = 'A' + i
- 20;
1377 fcap
[1] = 'a' + i
- 46;
1380 char *sequence
= tgetstr (fcap
, address
);
1383 sprintf (fkey
, "f%d", i
);
1384 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (sequence
),
1385 Fmake_vector (make_number (1),
1393 * Various mappings to try and get a better fit.
1396 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1397 if (!tgetstr (cap1, address)) \
1399 char *sequence = tgetstr (cap2, address); \
1401 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1402 Fmake_vector (make_number (1), \
1406 /* if there's no key_next keycap, map key_npage to `next' keysym */
1407 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1408 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1409 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1410 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1411 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1412 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1413 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1415 /* IBM has their own non-standard dialect of terminfo.
1416 If the standard name isn't found, try the IBM name. */
1417 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1418 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1419 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1420 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1421 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1422 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1423 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1424 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1425 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1426 #undef CONDITIONAL_REASSIGN
1431 #endif /* not DOS_NT */
1434 /***********************************************************************
1435 Character Display Information
1436 ***********************************************************************/
1437 static void append_glyph (struct it
*);
1438 static void append_composite_glyph (struct it
*);
1439 static void produce_composite_glyph (struct it
*);
1440 static void append_glyphless_glyph (struct it
*, int, const char *);
1441 static void produce_glyphless_glyph (struct it
*, int, Lisp_Object
);
1443 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1444 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1445 the character for which to produce glyphs; IT->face_id contains the
1446 character's face. Padding glyphs are appended if IT->c has a
1447 IT->pixel_width > 1. */
1450 append_glyph (struct it
*it
)
1452 struct glyph
*glyph
, *end
;
1455 eassert (it
->glyph_row
);
1456 glyph
= (it
->glyph_row
->glyphs
[it
->area
]
1457 + it
->glyph_row
->used
[it
->area
]);
1458 end
= it
->glyph_row
->glyphs
[1 + it
->area
];
1460 /* If the glyph row is reversed, we need to prepend the glyph rather
1462 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1465 int move_by
= it
->pixel_width
;
1467 /* Make room for the new glyphs. */
1468 if (move_by
> end
- glyph
) /* don't overstep end of this area */
1469 move_by
= end
- glyph
;
1470 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1472 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1473 end
= glyph
+ move_by
;
1476 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1477 right, even in the REVERSED_P case, since (a) all of its u.ch are
1478 identical, and (b) the PADDING_P flag needs to be set for the
1479 leftmost one, because we write to the terminal left-to-right. */
1481 i
< it
->pixel_width
&& glyph
< end
;
1484 glyph
->type
= CHAR_GLYPH
;
1485 glyph
->pixel_width
= 1;
1486 glyph
->u
.ch
= it
->char_to_display
;
1487 glyph
->face_id
= it
->face_id
;
1488 glyph
->padding_p
= i
> 0;
1489 glyph
->charpos
= CHARPOS (it
->position
);
1490 glyph
->object
= it
->object
;
1493 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1494 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1496 glyph
->bidi_type
= it
->bidi_it
.type
;
1500 glyph
->resolved_level
= 0;
1501 glyph
->bidi_type
= UNKNOWN_BT
;
1504 ++it
->glyph_row
->used
[it
->area
];
1509 /* For external use. */
1511 tty_append_glyph (struct it
*it
)
1517 /* Produce glyphs for the display element described by IT. *IT
1518 specifies what we want to produce a glyph for (character, image, ...),
1519 and where in the glyph matrix we currently are (glyph row and hpos).
1520 produce_glyphs fills in output fields of *IT with information such as the
1521 pixel width and height of a character, and maybe output actual glyphs at
1522 the same time if IT->glyph_row is non-null. For an overview, see
1523 the explanation in dispextern.h, before the definition of the
1524 display_element_type enumeration.
1526 produce_glyphs also stores the result of glyph width, ascent
1527 etc. computations in *IT.
1529 IT->glyph_row may be null, in which case produce_glyphs does not
1530 actually fill in the glyphs. This is used in the move_* functions
1531 in xdisp.c for text width and height computations.
1533 Callers usually don't call produce_glyphs directly;
1534 instead they use the macro PRODUCE_GLYPHS. */
1537 produce_glyphs (struct it
*it
)
1539 /* If a hook is installed, let it do the work. */
1541 /* Nothing but characters are supported on terminal frames. */
1542 eassert (it
->what
== IT_CHARACTER
1543 || it
->what
== IT_COMPOSITION
1544 || it
->what
== IT_STRETCH
1545 || it
->what
== IT_GLYPHLESS
);
1547 if (it
->what
== IT_STRETCH
)
1549 produce_stretch_glyph (it
);
1553 if (it
->what
== IT_COMPOSITION
)
1555 produce_composite_glyph (it
);
1559 if (it
->what
== IT_GLYPHLESS
)
1561 produce_glyphless_glyph (it
, 0, Qnil
);
1565 if (it
->char_to_display
>= 040 && it
->char_to_display
< 0177)
1567 it
->pixel_width
= it
->nglyphs
= 1;
1571 else if (it
->char_to_display
== '\n')
1572 it
->pixel_width
= it
->nglyphs
= 0;
1573 else if (it
->char_to_display
== '\t')
1575 int absolute_x
= (it
->current_x
1576 + it
->continuation_lines_width
);
1578 = (((1 + absolute_x
+ it
->tab_width
- 1)
1583 /* If part of the TAB has been displayed on the previous line
1584 which is continued now, continuation_lines_width will have
1585 been incremented already by the part that fitted on the
1586 continued line. So, we will get the right number of spaces
1588 nspaces
= next_tab_x
- absolute_x
;
1594 it
->char_to_display
= ' ';
1595 it
->pixel_width
= it
->len
= 1;
1601 it
->pixel_width
= nspaces
;
1602 it
->nglyphs
= nspaces
;
1604 else if (CHAR_BYTE8_P (it
->char_to_display
))
1606 /* Coming here means that we must send the raw 8-bit byte as is
1607 to the terminal. Although there's no way to know how many
1608 columns it occupies on a screen, it is a good assumption that
1609 a single byte code has 1-column width. */
1610 it
->pixel_width
= it
->nglyphs
= 1;
1616 Lisp_Object charset_list
= FRAME_TERMINAL (it
->f
)->charset_list
;
1618 if (char_charset (it
->char_to_display
, charset_list
, NULL
))
1620 it
->pixel_width
= CHAR_WIDTH (it
->char_to_display
);
1621 it
->nglyphs
= it
->pixel_width
;
1627 Lisp_Object acronym
= lookup_glyphless_char_display (-1, it
);
1629 eassert (it
->what
== IT_GLYPHLESS
);
1630 produce_glyphless_glyph (it
, 1, acronym
);
1635 /* Advance current_x by the pixel width as a convenience for
1637 if (it
->area
== TEXT_AREA
)
1638 it
->current_x
+= it
->pixel_width
;
1639 it
->ascent
= it
->max_ascent
= it
->phys_ascent
= it
->max_phys_ascent
= 0;
1640 it
->descent
= it
->max_descent
= it
->phys_descent
= it
->max_phys_descent
= 1;
1643 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1644 Called from produce_composite_glyph for terminal frames if
1645 IT->glyph_row != NULL. IT->face_id contains the character's
1649 append_composite_glyph (struct it
*it
)
1651 struct glyph
*glyph
;
1653 eassert (it
->glyph_row
);
1654 glyph
= it
->glyph_row
->glyphs
[it
->area
] + it
->glyph_row
->used
[it
->area
];
1655 if (glyph
< it
->glyph_row
->glyphs
[1 + it
->area
])
1657 /* If the glyph row is reversed, we need to prepend the glyph
1658 rather than append it. */
1659 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1663 /* Make room for the new glyph. */
1664 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1666 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1668 glyph
->type
= COMPOSITE_GLYPH
;
1669 glyph
->pixel_width
= it
->pixel_width
;
1670 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1671 if (it
->cmp_it
.ch
< 0)
1673 glyph
->u
.cmp
.automatic
= 0;
1674 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1678 glyph
->u
.cmp
.automatic
= 1;
1679 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1680 glyph
->slice
.cmp
.from
= it
->cmp_it
.from
;
1681 glyph
->slice
.cmp
.to
= it
->cmp_it
.to
- 1;
1684 glyph
->face_id
= it
->face_id
;
1685 glyph
->padding_p
= 0;
1686 glyph
->charpos
= CHARPOS (it
->position
);
1687 glyph
->object
= it
->object
;
1690 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1691 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1693 glyph
->bidi_type
= it
->bidi_it
.type
;
1697 glyph
->resolved_level
= 0;
1698 glyph
->bidi_type
= UNKNOWN_BT
;
1701 ++it
->glyph_row
->used
[it
->area
];
1707 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1708 the composition. We simply produces components of the composition
1709 assuming that the terminal has a capability to layout/render it
1713 produce_composite_glyph (struct it
*it
)
1715 if (it
->cmp_it
.ch
< 0)
1717 struct composition
*cmp
= composition_table
[it
->cmp_it
.id
];
1719 it
->pixel_width
= cmp
->width
;
1723 Lisp_Object gstring
= composition_gstring_from_id (it
->cmp_it
.id
);
1725 it
->pixel_width
= composition_gstring_width (gstring
, it
->cmp_it
.from
,
1726 it
->cmp_it
.to
, NULL
);
1730 append_composite_glyph (it
);
1734 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1735 is a face ID to be used for the glyph. What is actually appended
1736 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1737 comes from it->nglyphs bytes). */
1740 append_glyphless_glyph (struct it
*it
, int face_id
, const char *str
)
1742 struct glyph
*glyph
, *end
;
1745 eassert (it
->glyph_row
);
1746 glyph
= it
->glyph_row
->glyphs
[it
->area
] + it
->glyph_row
->used
[it
->area
];
1747 end
= it
->glyph_row
->glyphs
[1 + it
->area
];
1749 /* If the glyph row is reversed, we need to prepend the glyph rather
1751 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1754 int move_by
= it
->pixel_width
;
1756 /* Make room for the new glyphs. */
1757 if (move_by
> end
- glyph
) /* don't overstep end of this area */
1758 move_by
= end
- glyph
;
1759 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1761 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1762 end
= glyph
+ move_by
;
1767 glyph
->type
= CHAR_GLYPH
;
1768 glyph
->pixel_width
= 1;
1769 glyph
->face_id
= face_id
;
1770 glyph
->padding_p
= 0;
1771 glyph
->charpos
= CHARPOS (it
->position
);
1772 glyph
->object
= it
->object
;
1775 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1776 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1778 glyph
->bidi_type
= it
->bidi_it
.type
;
1782 glyph
->resolved_level
= 0;
1783 glyph
->bidi_type
= UNKNOWN_BT
;
1786 /* BIDI Note: we put the glyphs of characters left to right, even in
1787 the REVERSED_P case because we write to the terminal
1789 for (i
= 0; i
< it
->nglyphs
&& glyph
< end
; ++i
)
1792 glyph
[0] = glyph
[-1];
1793 glyph
->u
.ch
= str
[i
];
1794 ++it
->glyph_row
->used
[it
->area
];
1799 /* Produce glyphs for a glyphless character for iterator IT.
1800 IT->glyphless_method specifies which method to use for displaying
1801 the character. See the description of enum
1802 glyphless_display_method in dispextern.h for the details.
1804 FOR_NO_FONT is nonzero if and only if this is for a character that
1805 is not supported by the coding system of the terminal. ACRONYM, if
1806 non-nil, is an acronym string for the character.
1808 The glyphs actually produced are of type CHAR_GLYPH. */
1811 produce_glyphless_glyph (struct it
*it
, int for_no_font
, Lisp_Object acronym
)
1815 char buf
[sizeof "\\x" + max (6, (sizeof it
->c
* CHAR_BIT
+ 3) / 4)];
1816 char const *str
= " ";
1818 /* Get a face ID for the glyph by utilizing a cache (the same way as
1819 done for `escape-glyph' in get_next_display_element). */
1820 if (it
->f
== last_glyphless_glyph_frame
1821 && it
->face_id
== last_glyphless_glyph_face_id
)
1823 face_id
= last_glyphless_glyph_merged_face_id
;
1827 /* Merge the `glyphless-char' face into the current face. */
1828 face_id
= merge_faces (it
->f
, Qglyphless_char
, 0, it
->face_id
);
1829 last_glyphless_glyph_frame
= it
->f
;
1830 last_glyphless_glyph_face_id
= it
->face_id
;
1831 last_glyphless_glyph_merged_face_id
= face_id
;
1834 if (it
->glyphless_method
== GLYPHLESS_DISPLAY_THIN_SPACE
)
1836 /* As there's no way to produce a thin space, we produce a space
1837 of canonical width. */
1840 else if (it
->glyphless_method
== GLYPHLESS_DISPLAY_EMPTY_BOX
)
1842 len
= CHAR_WIDTH (it
->c
);
1847 len
= sprintf (buf
, "[%.*s]", len
, str
);
1852 if (it
->glyphless_method
== GLYPHLESS_DISPLAY_ACRONYM
)
1854 if (! STRINGP (acronym
) && CHAR_TABLE_P (Vglyphless_char_display
))
1855 acronym
= CHAR_TABLE_REF (Vglyphless_char_display
, it
->c
);
1856 if (CONSP (acronym
))
1857 acronym
= XCDR (acronym
);
1859 str
= STRINGP (acronym
) ? SSDATA (acronym
) : "";
1860 for (len
= 0; len
< 6 && str
[len
] && ASCII_BYTE_P (str
[len
]); len
++)
1861 buf
[1 + len
] = str
[len
];
1867 eassert (it
->glyphless_method
== GLYPHLESS_DISPLAY_HEX_CODE
);
1868 len
= (it
->c
< 0x10000 ? sprintf (buf
, "\\u%04X", it
->c
)
1869 : it
->c
<= MAX_UNICODE_CHAR
? sprintf (buf
, "\\U%06X", it
->c
)
1870 : sprintf (buf
, "\\x%06X", it
->c
));
1875 it
->pixel_width
= len
;
1878 append_glyphless_glyph (it
, face_id
, str
);
1882 /***********************************************************************
1884 ***********************************************************************/
1886 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1887 one of the enumerators from enum no_color_bit, or a bit set built
1888 from them. Some display attributes may not be used together with
1889 color; the termcap capability `NC' specifies which ones. */
1891 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1892 (tty->TN_max_colors > 0 \
1893 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1896 /* Turn appearances of face FACE_ID on tty frame F on.
1897 FACE_ID is a realized face ID number, in the face cache. */
1900 turn_on_face (struct frame
*f
, int face_id
)
1902 struct face
*face
= FACE_FROM_ID (f
, face_id
);
1903 long fg
= face
->foreground
;
1904 long bg
= face
->background
;
1905 struct tty_display_info
*tty
= FRAME_TTY (f
);
1907 /* Do this first because TS_end_standout_mode may be the same
1908 as TS_exit_attribute_mode, which turns all appearances off. */
1909 if (MAY_USE_WITH_COLORS_P (tty
, NC_REVERSE
))
1911 if (tty
->TN_max_colors
> 0)
1913 if (fg
>= 0 && bg
>= 0)
1915 /* If the terminal supports colors, we can set them
1916 below without using reverse video. The face's fg
1917 and bg colors are set as they should appear on
1918 the screen, i.e. they take the inverse-video'ness
1919 of the face already into account. */
1921 else if (inverse_video
)
1923 if (fg
== FACE_TTY_DEFAULT_FG_COLOR
1924 || bg
== FACE_TTY_DEFAULT_BG_COLOR
)
1925 tty_toggle_highlight (tty
);
1929 if (fg
== FACE_TTY_DEFAULT_BG_COLOR
1930 || bg
== FACE_TTY_DEFAULT_FG_COLOR
)
1931 tty_toggle_highlight (tty
);
1936 /* If we can't display colors, use reverse video
1937 if the face specifies that. */
1940 if (fg
== FACE_TTY_DEFAULT_FG_COLOR
1941 || bg
== FACE_TTY_DEFAULT_BG_COLOR
)
1942 tty_toggle_highlight (tty
);
1946 if (fg
== FACE_TTY_DEFAULT_BG_COLOR
1947 || bg
== FACE_TTY_DEFAULT_FG_COLOR
)
1948 tty_toggle_highlight (tty
);
1953 if (face
->tty_bold_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_BOLD
))
1954 OUTPUT1_IF (tty
, tty
->TS_enter_bold_mode
);
1956 if (face
->tty_italic_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_ITALIC
))
1958 if (tty
->TS_enter_italic_mode
)
1959 OUTPUT1 (tty
, tty
->TS_enter_italic_mode
);
1961 /* Italics mode is unavailable on many terminals. In that
1962 case, map slant to dimmed text; we want italic text to
1963 appear different and dimming is not otherwise used. */
1964 OUTPUT1 (tty
, tty
->TS_enter_dim_mode
);
1967 if (face
->tty_underline_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_UNDERLINE
))
1968 OUTPUT1_IF (tty
, tty
->TS_enter_underline_mode
);
1970 if (tty
->TN_max_colors
> 0)
1975 ts
= tty
->standout_mode
? tty
->TS_set_background
: tty
->TS_set_foreground
;
1978 p
= tparam (ts
, NULL
, 0, (int) fg
, 0, 0, 0);
1983 ts
= tty
->standout_mode
? tty
->TS_set_foreground
: tty
->TS_set_background
;
1986 p
= tparam (ts
, NULL
, 0, (int) bg
, 0, 0, 0);
1994 /* Turn off appearances of face FACE_ID on tty frame F. */
1997 turn_off_face (struct frame
*f
, int face_id
)
1999 struct face
*face
= FACE_FROM_ID (f
, face_id
);
2000 struct tty_display_info
*tty
= FRAME_TTY (f
);
2002 eassert (face
!= NULL
);
2004 if (tty
->TS_exit_attribute_mode
)
2006 /* Capability "me" will turn off appearance modes double-bright,
2007 half-bright, reverse-video, standout, underline. It may or
2008 may not turn off alt-char-mode. */
2009 if (face
->tty_bold_p
2010 || face
->tty_italic_p
2011 || face
->tty_reverse_p
2012 || face
->tty_underline_p
)
2014 OUTPUT1_IF (tty
, tty
->TS_exit_attribute_mode
);
2015 if (strcmp (tty
->TS_exit_attribute_mode
, tty
->TS_end_standout_mode
) == 0)
2016 tty
->standout_mode
= 0;
2021 /* If we don't have "me" we can only have those appearances
2022 that have exit sequences defined. */
2023 if (face
->tty_underline_p
)
2024 OUTPUT_IF (tty
, tty
->TS_exit_underline_mode
);
2027 /* Switch back to default colors. */
2028 if (tty
->TN_max_colors
> 0
2029 && ((face
->foreground
!= FACE_TTY_DEFAULT_COLOR
2030 && face
->foreground
!= FACE_TTY_DEFAULT_FG_COLOR
)
2031 || (face
->background
!= FACE_TTY_DEFAULT_COLOR
2032 && face
->background
!= FACE_TTY_DEFAULT_BG_COLOR
)))
2033 OUTPUT1_IF (tty
, tty
->TS_orig_pair
);
2037 /* Return non-zero if the terminal on frame F supports all of the
2038 capabilities in CAPS simultaneously, with foreground and background
2039 colors FG and BG. */
2042 tty_capable_p (struct tty_display_info
*tty
, unsigned int caps
,
2043 unsigned long fg
, unsigned long bg
)
2045 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2046 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2049 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_INVERSE
, tty
->TS_standout_mode
, NC_REVERSE
);
2050 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_UNDERLINE
, tty
->TS_enter_underline_mode
, NC_UNDERLINE
);
2051 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_BOLD
, tty
->TS_enter_bold_mode
, NC_BOLD
);
2052 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_DIM
, tty
->TS_enter_dim_mode
, NC_DIM
);
2053 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_ITALIC
, tty
->TS_enter_italic_mode
, NC_ITALIC
);
2059 /* Return non-zero if the terminal is capable to display colors. */
2061 DEFUN ("tty-display-color-p", Ftty_display_color_p
, Stty_display_color_p
,
2063 doc
: /* Return non-nil if the tty device TERMINAL can display colors.
2065 TERMINAL can be a terminal object, a frame, or nil (meaning the
2066 selected frame's terminal). This function always returns nil if
2067 TERMINAL does not refer to a text terminal. */)
2068 (Lisp_Object terminal
)
2070 struct terminal
*t
= get_tty_terminal (terminal
, 0);
2074 return t
->display_info
.tty
->TN_max_colors
> 0 ? Qt
: Qnil
;
2077 /* Return the number of supported colors. */
2078 DEFUN ("tty-display-color-cells", Ftty_display_color_cells
,
2079 Stty_display_color_cells
, 0, 1, 0,
2080 doc
: /* Return the number of colors supported by the tty device TERMINAL.
2082 TERMINAL can be a terminal object, a frame, or nil (meaning the
2083 selected frame's terminal). This function always returns 0 if
2084 TERMINAL does not refer to a text terminal. */)
2085 (Lisp_Object terminal
)
2087 struct terminal
*t
= get_tty_terminal (terminal
, 0);
2089 return make_number (0);
2091 return make_number (t
->display_info
.tty
->TN_max_colors
);
2096 /* Declare here rather than in the function, as in the rest of Emacs,
2097 to work around an HPUX compiler bug (?). See
2098 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2099 static int default_max_colors
;
2100 static int default_max_pairs
;
2101 static int default_no_color_video
;
2102 static char *default_orig_pair
;
2103 static char *default_set_foreground
;
2104 static char *default_set_background
;
2106 /* Save or restore the default color-related capabilities of this
2109 tty_default_color_capabilities (struct tty_display_info
*tty
, int save
)
2114 xfree (default_orig_pair
);
2115 default_orig_pair
= tty
->TS_orig_pair
? xstrdup (tty
->TS_orig_pair
) : NULL
;
2117 xfree (default_set_foreground
);
2118 default_set_foreground
= tty
->TS_set_foreground
? xstrdup (tty
->TS_set_foreground
)
2121 xfree (default_set_background
);
2122 default_set_background
= tty
->TS_set_background
? xstrdup (tty
->TS_set_background
)
2125 default_max_colors
= tty
->TN_max_colors
;
2126 default_max_pairs
= tty
->TN_max_pairs
;
2127 default_no_color_video
= tty
->TN_no_color_video
;
2131 tty
->TS_orig_pair
= default_orig_pair
;
2132 tty
->TS_set_foreground
= default_set_foreground
;
2133 tty
->TS_set_background
= default_set_background
;
2134 tty
->TN_max_colors
= default_max_colors
;
2135 tty
->TN_max_pairs
= default_max_pairs
;
2136 tty
->TN_no_color_video
= default_no_color_video
;
2140 /* Setup one of the standard tty color schemes according to MODE.
2141 MODE's value is generally the number of colors which we want to
2142 support; zero means set up for the default capabilities, the ones
2143 we saw at init_tty time; -1 means turn off color support. */
2145 tty_setup_colors (struct tty_display_info
*tty
, int mode
)
2147 /* Canonicalize all negative values of MODE. */
2153 case -1: /* no colors at all */
2154 tty
->TN_max_colors
= 0;
2155 tty
->TN_max_pairs
= 0;
2156 tty
->TN_no_color_video
= 0;
2157 tty
->TS_set_foreground
= tty
->TS_set_background
= tty
->TS_orig_pair
= NULL
;
2159 case 0: /* default colors, if any */
2161 tty_default_color_capabilities (tty
, 0);
2163 case 8: /* 8 standard ANSI colors */
2164 tty
->TS_orig_pair
= "\033[0m";
2166 tty
->TS_set_foreground
= "\033[3%p1%dm";
2167 tty
->TS_set_background
= "\033[4%p1%dm";
2169 tty
->TS_set_foreground
= "\033[3%dm";
2170 tty
->TS_set_background
= "\033[4%dm";
2172 tty
->TN_max_colors
= 8;
2173 tty
->TN_max_pairs
= 64;
2174 tty
->TN_no_color_video
= 0;
2180 set_tty_color_mode (struct tty_display_info
*tty
, struct frame
*f
)
2182 Lisp_Object tem
, val
;
2183 Lisp_Object color_mode
;
2185 Lisp_Object tty_color_mode_alist
2186 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil
);
2188 tem
= assq_no_quit (Qtty_color_mode
, f
->param_alist
);
2189 val
= CONSP (tem
) ? XCDR (tem
) : Qnil
;
2193 else if (SYMBOLP (tty_color_mode_alist
))
2195 tem
= Fassq (val
, Fsymbol_value (tty_color_mode_alist
));
2196 color_mode
= CONSP (tem
) ? XCDR (tem
) : Qnil
;
2201 mode
= TYPE_RANGED_INTEGERP (int, color_mode
) ? XINT (color_mode
) : 0;
2203 if (mode
!= tty
->previous_color_mode
)
2205 tty
->previous_color_mode
= mode
;
2206 tty_setup_colors (tty
, mode
);
2207 /* This recomputes all the faces given the new color definitions. */
2208 safe_call (1, intern ("tty-set-up-initial-frame-faces"));
2212 #endif /* !DOS_NT */
2216 /* Return the tty display object specified by TERMINAL. */
2218 static struct terminal
*
2219 get_tty_terminal (Lisp_Object terminal
, int throw)
2221 struct terminal
*t
= get_terminal (terminal
, throw);
2223 if (t
&& t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2226 error ("Device %d is not a termcap terminal device", t
->id
);
2234 /* Return an active termcap device that uses the tty device with the
2237 This function ignores suspended devices.
2239 Returns NULL if the named terminal device is not opened. */
2242 get_named_tty (const char *name
)
2249 for (t
= terminal_list
; t
; t
= t
->next_terminal
)
2251 if ((t
->type
== output_termcap
|| t
->type
== output_msdos_raw
)
2252 && !strcmp (t
->display_info
.tty
->name
, name
)
2253 && TERMINAL_ACTIVE_P (t
))
2261 DEFUN ("tty-type", Ftty_type
, Stty_type
, 0, 1, 0,
2262 doc
: /* Return the type of the tty device that TERMINAL uses.
2263 Returns nil if TERMINAL is not on a tty device.
2265 TERMINAL can be a terminal object, a frame, or nil (meaning the
2266 selected frame's terminal). */)
2267 (Lisp_Object terminal
)
2269 struct terminal
*t
= get_terminal (terminal
, 1);
2271 if (t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2274 if (t
->display_info
.tty
->type
)
2275 return build_string (t
->display_info
.tty
->type
);
2280 DEFUN ("controlling-tty-p", Fcontrolling_tty_p
, Scontrolling_tty_p
, 0, 1, 0,
2281 doc
: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2283 TERMINAL can be a terminal object, a frame, or nil (meaning the
2284 selected frame's terminal). This function always returns nil if
2285 TERMINAL is not on a tty device. */)
2286 (Lisp_Object terminal
)
2288 struct terminal
*t
= get_terminal (terminal
, 1);
2290 if ((t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2291 || strcmp (t
->display_info
.tty
->name
, DEV_TTY
) != 0)
2297 DEFUN ("tty-no-underline", Ftty_no_underline
, Stty_no_underline
, 0, 1, 0,
2298 doc
: /* Declare that the tty used by TERMINAL does not handle underlining.
2299 This is used to override the terminfo data, for certain terminals that
2300 do not really do underlining, but say that they do. This function has
2301 no effect if used on a non-tty terminal.
2303 TERMINAL can be a terminal object, a frame or nil (meaning the
2304 selected frame's terminal). This function always returns nil if
2305 TERMINAL does not refer to a text terminal. */)
2306 (Lisp_Object terminal
)
2308 struct terminal
*t
= get_terminal (terminal
, 1);
2310 if (t
->type
== output_termcap
)
2311 t
->display_info
.tty
->TS_enter_underline_mode
= 0;
2315 DEFUN ("tty-top-frame", Ftty_top_frame
, Stty_top_frame
, 0, 1, 0,
2316 doc
: /* Return the topmost terminal frame on TERMINAL.
2317 TERMINAL can be a terminal object, a frame or nil (meaning the
2318 selected frame's terminal). This function returns nil if TERMINAL
2319 does not refer to a text terminal. Otherwise, it returns the
2320 top-most frame on the text terminal. */)
2321 (Lisp_Object terminal
)
2323 struct terminal
*t
= get_terminal (terminal
, 1);
2325 if (t
->type
== output_termcap
)
2326 return t
->display_info
.tty
->top_frame
;
2332 DEFUN ("suspend-tty", Fsuspend_tty
, Ssuspend_tty
, 0, 1, 0,
2333 doc
: /* Suspend the terminal device TTY.
2335 The device is restored to its default state, and Emacs ceases all
2336 access to the tty device. Frames that use the device are not deleted,
2337 but input is not read from them and if they change, their display is
2340 TTY may be a terminal object, a frame, or nil for the terminal device
2341 of the currently selected frame.
2343 This function runs `suspend-tty-functions' after suspending the
2344 device. The functions are run with one arg, the id of the suspended
2347 `suspend-tty' does nothing if it is called on a device that is already
2350 A suspended tty may be resumed by calling `resume-tty' on it. */)
2353 struct terminal
*t
= get_tty_terminal (tty
, 1);
2357 error ("Unknown tty device");
2359 f
= t
->display_info
.tty
->input
;
2363 /* First run `suspend-tty-functions' and then clean up the tty
2364 state because `suspend-tty-functions' might need to change
2366 Lisp_Object args
[2];
2367 args
[0] = intern ("suspend-tty-functions");
2368 XSETTERMINAL (args
[1], t
);
2369 Frun_hook_with_args (2, args
);
2371 reset_sys_modes (t
->display_info
.tty
);
2372 delete_keyboard_wait_descriptor (fileno (f
));
2376 if (f
!= t
->display_info
.tty
->output
)
2377 fclose (t
->display_info
.tty
->output
);
2380 t
->display_info
.tty
->input
= 0;
2381 t
->display_info
.tty
->output
= 0;
2383 if (FRAMEP (t
->display_info
.tty
->top_frame
))
2384 FRAME_SET_VISIBLE (XFRAME (t
->display_info
.tty
->top_frame
), 0);
2388 /* Clear display hooks to prevent further output. */
2389 clear_tty_hooks (t
);
2394 DEFUN ("resume-tty", Fresume_tty
, Sresume_tty
, 0, 1, 0,
2395 doc
: /* Resume the previously suspended terminal device TTY.
2396 The terminal is opened and reinitialized. Frames that are on the
2397 suspended terminal are revived.
2399 It is an error to resume a terminal while another terminal is active
2402 This function runs `resume-tty-functions' after resuming the terminal.
2403 The functions are run with one arg, the id of the resumed terminal
2406 `resume-tty' does nothing if it is called on a device that is not
2409 TTY may be a terminal object, a frame, or nil (meaning the selected
2410 frame's terminal). */)
2413 struct terminal
*t
= get_tty_terminal (tty
, 1);
2417 error ("Unknown tty device");
2419 if (!t
->display_info
.tty
->input
)
2421 if (get_named_tty (t
->display_info
.tty
->name
))
2422 error ("Cannot resume display while another display is active on the same device");
2425 t
->display_info
.tty
->output
= stdout
;
2426 t
->display_info
.tty
->input
= stdin
;
2428 fd
= emacs_open (t
->display_info
.tty
->name
, O_RDWR
| O_NOCTTY
, 0);
2431 error ("Can not reopen tty device %s: %s", t
->display_info
.tty
->name
, strerror (errno
));
2433 if (strcmp (t
->display_info
.tty
->name
, DEV_TTY
))
2434 dissociate_if_controlling_tty (fd
);
2436 t
->display_info
.tty
->output
= fdopen (fd
, "w+");
2437 t
->display_info
.tty
->input
= t
->display_info
.tty
->output
;
2440 add_keyboard_wait_descriptor (fd
);
2442 if (FRAMEP (t
->display_info
.tty
->top_frame
))
2444 struct frame
*f
= XFRAME (t
->display_info
.tty
->top_frame
);
2446 int old_height
= FRAME_COLS (f
);
2447 int old_width
= FRAME_LINES (f
);
2449 /* Check if terminal/window size has changed while the frame
2451 get_tty_size (fileno (t
->display_info
.tty
->input
), &width
, &height
);
2452 if (width
!= old_width
|| height
!= old_height
)
2453 change_frame_size (f
, height
, width
, 0, 0, 0);
2454 FRAME_SET_VISIBLE (XFRAME (t
->display_info
.tty
->top_frame
), 1);
2458 init_sys_modes (t
->display_info
.tty
);
2461 /* Run `resume-tty-functions'. */
2462 Lisp_Object args
[2];
2463 args
[0] = intern ("resume-tty-functions");
2464 XSETTERMINAL (args
[1], t
);
2465 Frun_hook_with_args (2, args
);
2475 /***********************************************************************
2477 ***********************************************************************/
2481 #ifndef HAVE_WINDOW_SYSTEM
2483 term_mouse_moveto (int x
, int y
)
2485 /* TODO: how to set mouse position?
2488 name = (const char *) ttyname (0);
2489 fd = open (name, O_WRONLY);
2490 SOME_FUNCTION (x, y, fd);
2493 last_mouse_y = y; */
2495 #endif /* HAVE_WINDOW_SYSTEM */
2497 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2499 tty_draw_row_with_mouse_face (struct window
*w
, struct glyph_row
*row
,
2500 int start_hpos
, int end_hpos
,
2501 enum draw_glyphs_face draw
)
2503 int nglyphs
= end_hpos
- start_hpos
;
2504 struct frame
*f
= XFRAME (WINDOW_FRAME (w
));
2505 struct tty_display_info
*tty
= FRAME_TTY (f
);
2506 int face_id
= tty
->mouse_highlight
.mouse_face_face_id
;
2507 int save_x
, save_y
, pos_x
, pos_y
;
2509 if (end_hpos
>= row
->used
[TEXT_AREA
])
2510 nglyphs
= row
->used
[TEXT_AREA
] - start_hpos
;
2512 pos_y
= row
->y
+ WINDOW_TOP_EDGE_Y (w
);
2513 pos_x
= row
->used
[LEFT_MARGIN_AREA
] + start_hpos
+ WINDOW_LEFT_EDGE_X (w
);
2515 /* Save current cursor co-ordinates. */
2516 save_y
= curY (tty
);
2517 save_x
= curX (tty
);
2518 cursor_to (f
, pos_y
, pos_x
);
2520 if (draw
== DRAW_MOUSE_FACE
)
2521 tty_write_glyphs_with_face (f
, row
->glyphs
[TEXT_AREA
] + start_hpos
,
2523 else if (draw
== DRAW_NORMAL_TEXT
)
2524 write_glyphs (f
, row
->glyphs
[TEXT_AREA
] + start_hpos
, nglyphs
);
2526 cursor_to (f
, save_y
, save_x
);
2530 term_mouse_movement (FRAME_PTR frame
, Gpm_Event
*event
)
2532 /* Has the mouse moved off the glyph it was on at the last sighting? */
2533 if (event
->x
!= last_mouse_x
|| event
->y
!= last_mouse_y
)
2535 frame
->mouse_moved
= 1;
2536 note_mouse_highlight (frame
, event
->x
, event
->y
);
2537 /* Remember which glyph we're now on. */
2538 last_mouse_x
= event
->x
;
2539 last_mouse_y
= event
->y
;
2545 /* Return the Time that corresponds to T. Wrap around on overflow. */
2547 timeval_to_Time (struct timeval
const *t
)
2553 ms
= t
->tv_usec
/ 1000;
2557 /* Return the current position of the mouse.
2559 Set *f to the frame the mouse is in, or zero if the mouse is in no
2560 Emacs frame. If it is set to zero, all the other arguments are
2563 Set *bar_window to Qnil, and *x and *y to the column and
2564 row of the character cell the mouse is over.
2566 Set *timeptr to the time the mouse was at the returned position.
2568 This clears mouse_moved until the next motion
2571 term_mouse_position (FRAME_PTR
*fp
, int insist
, Lisp_Object
*bar_window
,
2572 enum scroll_bar_part
*part
, Lisp_Object
*x
,
2573 Lisp_Object
*y
, Time
*timeptr
)
2577 *fp
= SELECTED_FRAME ();
2578 (*fp
)->mouse_moved
= 0;
2583 XSETINT (*x
, last_mouse_x
);
2584 XSETINT (*y
, last_mouse_y
);
2585 gettimeofday(&now
, 0);
2586 *timeptr
= timeval_to_Time (&now
);
2589 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2591 If the event is a button press, then note that we have grabbed
2595 term_mouse_click (struct input_event
*result
, Gpm_Event
*event
,
2601 result
->kind
= GPM_CLICK_EVENT
;
2602 for (i
= 0, j
= GPM_B_LEFT
; i
< 3; i
++, j
>>= 1 )
2604 if (event
->buttons
& j
) {
2605 result
->code
= i
; /* button number */
2609 gettimeofday(&now
, 0);
2610 result
->timestamp
= timeval_to_Time (&now
);
2612 if (event
->type
& GPM_UP
)
2613 result
->modifiers
= up_modifier
;
2614 else if (event
->type
& GPM_DOWN
)
2615 result
->modifiers
= down_modifier
;
2617 result
->modifiers
= 0;
2619 if (event
->type
& GPM_SINGLE
)
2620 result
->modifiers
|= click_modifier
;
2622 if (event
->type
& GPM_DOUBLE
)
2623 result
->modifiers
|= double_modifier
;
2625 if (event
->type
& GPM_TRIPLE
)
2626 result
->modifiers
|= triple_modifier
;
2628 if (event
->type
& GPM_DRAG
)
2629 result
->modifiers
|= drag_modifier
;
2631 if (!(event
->type
& (GPM_MOVE
| GPM_DRAG
))) {
2634 if (event
->modifiers
& (1 << 0))
2635 result
->modifiers
|= shift_modifier
;
2638 if (event
->modifiers
& (1 << 2))
2639 result
->modifiers
|= ctrl_modifier
;
2641 /* 1 << KG_ALT || KG_ALTGR */
2642 if (event
->modifiers
& (1 << 3)
2643 || event
->modifiers
& (1 << 1))
2644 result
->modifiers
|= meta_modifier
;
2647 XSETINT (result
->x
, event
->x
);
2648 XSETINT (result
->y
, event
->y
);
2649 XSETFRAME (result
->frame_or_window
, f
);
2655 handle_one_term_event (struct tty_display_info
*tty
, Gpm_Event
*event
, struct input_event
* hold_quit
)
2657 struct frame
*f
= XFRAME (tty
->top_frame
);
2658 struct input_event ie
;
2666 if (event
->type
& (GPM_MOVE
| GPM_DRAG
)) {
2667 previous_help_echo_string
= help_echo_string
;
2668 help_echo_string
= Qnil
;
2670 Gpm_DrawPointer (event
->x
, event
->y
, fileno (tty
->output
));
2672 if (!term_mouse_movement (f
, event
))
2673 help_echo_string
= previous_help_echo_string
;
2675 /* If the contents of the global variable help_echo_string
2676 has changed, generate a HELP_EVENT. */
2677 if (!NILP (help_echo_string
)
2678 || !NILP (previous_help_echo_string
))
2685 term_mouse_click (&ie
, event
, f
);
2689 if (ie
.kind
!= NO_EVENT
)
2691 kbd_buffer_store_event_hold (&ie
, hold_quit
);
2696 && !(hold_quit
&& hold_quit
->kind
!= NO_EVENT
))
2701 XSETFRAME (frame
, f
);
2705 gen_help_event (help_echo_string
, frame
, help_echo_window
,
2706 help_echo_object
, help_echo_pos
);
2713 DEFUN ("gpm-mouse-start", Fgpm_mouse_start
, Sgpm_mouse_start
,
2715 doc
: /* Open a connection to Gpm.
2716 Gpm-mouse can only be activated for one tty at a time. */)
2719 struct frame
*f
= SELECTED_FRAME ();
2720 struct tty_display_info
*tty
2721 = ((f
)->output_method
== output_termcap
2722 ? (f
)->terminal
->display_info
.tty
: NULL
);
2723 Gpm_Connect connection
;
2726 error ("Gpm-mouse only works in the GNU/Linux console");
2728 return Qnil
; /* Already activated, nothing to do. */
2730 error ("Gpm-mouse can only be activated for one tty at a time");
2732 connection
.eventMask
= ~0;
2733 connection
.defaultMask
= ~GPM_HARD
;
2734 connection
.maxMod
= ~0;
2735 connection
.minMod
= 0;
2738 if (Gpm_Open (&connection
, 0) < 0)
2739 error ("Gpm-mouse failed to connect to the gpm daemon");
2743 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2744 to generate SIGIOs. Apparently we need to call reset_sys_modes
2745 before calling init_sys_modes. */
2746 reset_sys_modes (tty
);
2747 init_sys_modes (tty
);
2748 add_gpm_wait_descriptor (gpm_fd
);
2757 delete_gpm_wait_descriptor (fd
);
2758 while (Gpm_Close()); /* close all the stack */
2762 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop
, Sgpm_mouse_stop
,
2764 doc
: /* Close a connection to Gpm. */)
2767 struct frame
*f
= SELECTED_FRAME ();
2768 struct tty_display_info
*tty
2769 = ((f
)->output_method
== output_termcap
2770 ? (f
)->terminal
->display_info
.tty
: NULL
);
2772 if (!tty
|| gpm_tty
!= tty
)
2773 return Qnil
; /* Not activated on this terminal, nothing to do. */
2778 #endif /* HAVE_GPM */
2782 /***********************************************************************
2784 ***********************************************************************/
2786 /* Initialize the tty-dependent part of frame F. The frame must
2787 already have its device initialized. */
2790 create_tty_output (struct frame
*f
)
2792 struct tty_output
*t
= xzalloc (sizeof *t
);
2794 if (! FRAME_TERMCAP_P (f
))
2797 t
->display_info
= FRAME_TERMINAL (f
)->display_info
.tty
;
2799 f
->output_data
.tty
= t
;
2802 /* Delete frame F's face cache, and its tty-dependent part. */
2805 tty_free_frame_resources (struct frame
*f
)
2807 if (! FRAME_TERMCAP_P (f
))
2810 if (FRAME_FACE_CACHE (f
))
2811 free_frame_faces (f
);
2813 xfree (f
->output_data
.tty
);
2818 /* Delete frame F's face cache. */
2821 tty_free_frame_resources (struct frame
*f
)
2823 if (! FRAME_TERMCAP_P (f
) && ! FRAME_MSDOS_P (f
))
2826 if (FRAME_FACE_CACHE (f
))
2827 free_frame_faces (f
);
2831 /* Reset the hooks in TERMINAL. */
2834 clear_tty_hooks (struct terminal
*terminal
)
2837 terminal
->cursor_to_hook
= 0;
2838 terminal
->raw_cursor_to_hook
= 0;
2839 terminal
->clear_to_end_hook
= 0;
2840 terminal
->clear_frame_hook
= 0;
2841 terminal
->clear_end_of_line_hook
= 0;
2842 terminal
->ins_del_lines_hook
= 0;
2843 terminal
->insert_glyphs_hook
= 0;
2844 terminal
->write_glyphs_hook
= 0;
2845 terminal
->delete_glyphs_hook
= 0;
2846 terminal
->ring_bell_hook
= 0;
2847 terminal
->reset_terminal_modes_hook
= 0;
2848 terminal
->set_terminal_modes_hook
= 0;
2849 terminal
->update_begin_hook
= 0;
2850 terminal
->update_end_hook
= 0;
2851 terminal
->set_terminal_window_hook
= 0;
2852 terminal
->mouse_position_hook
= 0;
2853 terminal
->frame_rehighlight_hook
= 0;
2854 terminal
->frame_raise_lower_hook
= 0;
2855 terminal
->fullscreen_hook
= 0;
2856 terminal
->set_vertical_scroll_bar_hook
= 0;
2857 terminal
->condemn_scroll_bars_hook
= 0;
2858 terminal
->redeem_scroll_bar_hook
= 0;
2859 terminal
->judge_scroll_bars_hook
= 0;
2860 terminal
->read_socket_hook
= 0;
2861 terminal
->frame_up_to_date_hook
= 0;
2863 /* Leave these two set, or suspended frames are not deleted
2865 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
2866 terminal
->delete_terminal_hook
= &delete_tty
;
2869 /* Initialize hooks in TERMINAL with the values needed for a tty. */
2872 set_tty_hooks (struct terminal
*terminal
)
2874 terminal
->rif
= 0; /* ttys don't support window-based redisplay. */
2876 terminal
->cursor_to_hook
= &tty_cursor_to
;
2877 terminal
->raw_cursor_to_hook
= &tty_raw_cursor_to
;
2879 terminal
->clear_to_end_hook
= &tty_clear_to_end
;
2880 terminal
->clear_frame_hook
= &tty_clear_frame
;
2881 terminal
->clear_end_of_line_hook
= &tty_clear_end_of_line
;
2883 terminal
->ins_del_lines_hook
= &tty_ins_del_lines
;
2885 terminal
->insert_glyphs_hook
= &tty_insert_glyphs
;
2886 terminal
->write_glyphs_hook
= &tty_write_glyphs
;
2887 terminal
->delete_glyphs_hook
= &tty_delete_glyphs
;
2889 terminal
->ring_bell_hook
= &tty_ring_bell
;
2891 terminal
->reset_terminal_modes_hook
= &tty_reset_terminal_modes
;
2892 terminal
->set_terminal_modes_hook
= &tty_set_terminal_modes
;
2893 terminal
->update_begin_hook
= 0; /* Not needed. */
2894 terminal
->update_end_hook
= &tty_update_end
;
2895 terminal
->set_terminal_window_hook
= &tty_set_terminal_window
;
2897 terminal
->mouse_position_hook
= 0; /* Not needed. */
2898 terminal
->frame_rehighlight_hook
= 0; /* Not needed. */
2899 terminal
->frame_raise_lower_hook
= 0; /* Not needed. */
2901 terminal
->set_vertical_scroll_bar_hook
= 0; /* Not needed. */
2902 terminal
->condemn_scroll_bars_hook
= 0; /* Not needed. */
2903 terminal
->redeem_scroll_bar_hook
= 0; /* Not needed. */
2904 terminal
->judge_scroll_bars_hook
= 0; /* Not needed. */
2906 terminal
->read_socket_hook
= &tty_read_avail_input
; /* keyboard.c */
2907 terminal
->frame_up_to_date_hook
= 0; /* Not needed. */
2909 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
2910 terminal
->delete_terminal_hook
= &delete_tty
;
2913 /* Drop the controlling terminal if fd is the same device. */
2915 dissociate_if_controlling_tty (int fd
)
2917 pid_t pgid
= tcgetpgrp (fd
); /* If tcgetpgrp succeeds, fd is the ctty. */
2922 /* Create a termcap display on the tty device with the given name and
2925 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
2926 Otherwise NAME should be a path to the tty device file,
2929 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
2931 If MUST_SUCCEED is true, then all errors are fatal. */
2934 init_tty (const char *name
, const char *terminal_type
, int must_succeed
)
2937 char **address
= &area
;
2938 int buffer_size
= 4096;
2940 struct tty_display_info
*tty
= NULL
;
2941 struct terminal
*terminal
= NULL
;
2942 int ctty
= 0; /* 1 if asked to open controlling tty. */
2945 maybe_fatal (must_succeed
, 0,
2946 "Unknown terminal type",
2947 "Unknown terminal type");
2951 if (!strcmp (name
, DEV_TTY
))
2954 /* If we already have a terminal on the given device, use that. If
2955 all such terminals are suspended, create a new one instead. */
2956 /* XXX Perhaps this should be made explicit by having init_tty
2957 always create a new terminal and separating terminal and frame
2958 creation on Lisp level. */
2959 terminal
= get_named_tty (name
);
2963 terminal
= create_terminal ();
2966 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
2969 tty
= &the_only_display_info
;
2971 tty
= xzalloc (sizeof *tty
);
2973 tty
->top_frame
= Qnil
;
2974 tty
->next
= tty_list
;
2977 terminal
->type
= output_termcap
;
2978 terminal
->display_info
.tty
= tty
;
2979 tty
->terminal
= terminal
;
2981 tty
->Wcm
= xmalloc (sizeof *tty
->Wcm
);
2984 encode_terminal_src_size
= 0;
2985 encode_terminal_dst_size
= 0;
2989 set_tty_hooks (terminal
);
2997 /* Open the terminal device. Don't recognize it as our
2998 controlling terminal, and don't make it the controlling tty
2999 if we don't have one at the moment. */
3000 fd
= emacs_open (name
, O_RDWR
| O_IGNORE_CTTY
| O_NOCTTY
, 0);
3002 #endif /* O_IGNORE_CTTY */
3003 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3004 defined on Hurd. On other systems, we need to explicitly
3005 dissociate ourselves from the controlling tty when we want to
3006 open a frame on the same terminal. */
3007 fd
= emacs_open (name
, O_RDWR
| O_NOCTTY
, 0);
3009 tty
->name
= xstrdup (name
);
3010 terminal
->name
= xstrdup (name
);
3013 maybe_fatal (must_succeed
, terminal
,
3014 "Could not open file: %s",
3015 "Could not open file: %s",
3020 maybe_fatal (must_succeed
, terminal
,
3021 "Not a tty device: %s",
3022 "Not a tty device: %s",
3028 dissociate_if_controlling_tty (fd
);
3031 file
= fdopen (fd
, "w+");
3036 tty
->type
= xstrdup (terminal_type
);
3038 add_keyboard_wait_descriptor (fileno (tty
->input
));
3042 tty
->termcap_term_buffer
= xmalloc (buffer_size
);
3044 /* On some systems, tgetent tries to access the controlling
3048 sigemptyset (&blocked
);
3049 sigaddset (&blocked
, SIGTTOU
);
3050 pthread_sigmask (SIG_BLOCK
, &blocked
, 0);
3051 status
= tgetent (tty
->termcap_term_buffer
, terminal_type
);
3052 pthread_sigmask (SIG_UNBLOCK
, &blocked
, 0);
3058 maybe_fatal (must_succeed
, terminal
,
3059 "Cannot open terminfo database file",
3060 "Cannot open terminfo database file");
3062 maybe_fatal (must_succeed
, terminal
,
3063 "Cannot open termcap database file",
3064 "Cannot open termcap database file");
3069 maybe_fatal (must_succeed
, terminal
,
3070 "Terminal type %s is not defined",
3071 "Terminal type %s is not defined.\n\
3072 If that is not the actual type of terminal you have,\n\
3073 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3074 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3076 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3078 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3084 if (strlen (tty
->termcap_term_buffer
) >= buffer_size
)
3086 buffer_size
= strlen (tty
->termcap_term_buffer
);
3088 tty
->termcap_strings_buffer
= area
= xmalloc (buffer_size
);
3089 tty
->TS_ins_line
= tgetstr ("al", address
);
3090 tty
->TS_ins_multi_lines
= tgetstr ("AL", address
);
3091 tty
->TS_bell
= tgetstr ("bl", address
);
3092 BackTab (tty
) = tgetstr ("bt", address
);
3093 tty
->TS_clr_to_bottom
= tgetstr ("cd", address
);
3094 tty
->TS_clr_line
= tgetstr ("ce", address
);
3095 tty
->TS_clr_frame
= tgetstr ("cl", address
);
3096 ColPosition (tty
) = NULL
; /* tgetstr ("ch", address); */
3097 AbsPosition (tty
) = tgetstr ("cm", address
);
3098 CR (tty
) = tgetstr ("cr", address
);
3099 tty
->TS_set_scroll_region
= tgetstr ("cs", address
);
3100 tty
->TS_set_scroll_region_1
= tgetstr ("cS", address
);
3101 RowPosition (tty
) = tgetstr ("cv", address
);
3102 tty
->TS_del_char
= tgetstr ("dc", address
);
3103 tty
->TS_del_multi_chars
= tgetstr ("DC", address
);
3104 tty
->TS_del_line
= tgetstr ("dl", address
);
3105 tty
->TS_del_multi_lines
= tgetstr ("DL", address
);
3106 tty
->TS_delete_mode
= tgetstr ("dm", address
);
3107 tty
->TS_end_delete_mode
= tgetstr ("ed", address
);
3108 tty
->TS_end_insert_mode
= tgetstr ("ei", address
);
3109 Home (tty
) = tgetstr ("ho", address
);
3110 tty
->TS_ins_char
= tgetstr ("ic", address
);
3111 tty
->TS_ins_multi_chars
= tgetstr ("IC", address
);
3112 tty
->TS_insert_mode
= tgetstr ("im", address
);
3113 tty
->TS_pad_inserted_char
= tgetstr ("ip", address
);
3114 tty
->TS_end_keypad_mode
= tgetstr ("ke", address
);
3115 tty
->TS_keypad_mode
= tgetstr ("ks", address
);
3116 LastLine (tty
) = tgetstr ("ll", address
);
3117 Right (tty
) = tgetstr ("nd", address
);
3118 Down (tty
) = tgetstr ("do", address
);
3120 Down (tty
) = tgetstr ("nl", address
); /* Obsolete name for "do" */
3121 if (tgetflag ("bs"))
3122 Left (tty
) = "\b"; /* can't possibly be longer! */
3123 else /* (Actually, "bs" is obsolete...) */
3124 Left (tty
) = tgetstr ("le", address
);
3126 Left (tty
) = tgetstr ("bc", address
); /* Obsolete name for "le" */
3127 tty
->TS_pad_char
= tgetstr ("pc", address
);
3128 tty
->TS_repeat
= tgetstr ("rp", address
);
3129 tty
->TS_end_standout_mode
= tgetstr ("se", address
);
3130 tty
->TS_fwd_scroll
= tgetstr ("sf", address
);
3131 tty
->TS_standout_mode
= tgetstr ("so", address
);
3132 tty
->TS_rev_scroll
= tgetstr ("sr", address
);
3133 tty
->Wcm
->cm_tab
= tgetstr ("ta", address
);
3134 tty
->TS_end_termcap_modes
= tgetstr ("te", address
);
3135 tty
->TS_termcap_modes
= tgetstr ("ti", address
);
3136 Up (tty
) = tgetstr ("up", address
);
3137 tty
->TS_visible_bell
= tgetstr ("vb", address
);
3138 tty
->TS_cursor_normal
= tgetstr ("ve", address
);
3139 tty
->TS_cursor_visible
= tgetstr ("vs", address
);
3140 tty
->TS_cursor_invisible
= tgetstr ("vi", address
);
3141 tty
->TS_set_window
= tgetstr ("wi", address
);
3143 tty
->TS_enter_underline_mode
= tgetstr ("us", address
);
3144 tty
->TS_exit_underline_mode
= tgetstr ("ue", address
);
3145 tty
->TS_enter_bold_mode
= tgetstr ("md", address
);
3146 tty
->TS_enter_italic_mode
= tgetstr ("ZH", address
);
3147 tty
->TS_enter_dim_mode
= tgetstr ("mh", address
);
3148 tty
->TS_enter_reverse_mode
= tgetstr ("mr", address
);
3149 tty
->TS_enter_alt_charset_mode
= tgetstr ("as", address
);
3150 tty
->TS_exit_alt_charset_mode
= tgetstr ("ae", address
);
3151 tty
->TS_exit_attribute_mode
= tgetstr ("me", address
);
3153 MultiUp (tty
) = tgetstr ("UP", address
);
3154 MultiDown (tty
) = tgetstr ("DO", address
);
3155 MultiLeft (tty
) = tgetstr ("LE", address
);
3156 MultiRight (tty
) = tgetstr ("RI", address
);
3158 /* SVr4/ANSI color support. If "op" isn't available, don't support
3159 color because we can't switch back to the default foreground and
3161 tty
->TS_orig_pair
= tgetstr ("op", address
);
3162 if (tty
->TS_orig_pair
)
3164 tty
->TS_set_foreground
= tgetstr ("AF", address
);
3165 tty
->TS_set_background
= tgetstr ("AB", address
);
3166 if (!tty
->TS_set_foreground
)
3169 tty
->TS_set_foreground
= tgetstr ("Sf", address
);
3170 tty
->TS_set_background
= tgetstr ("Sb", address
);
3173 tty
->TN_max_colors
= tgetnum ("Co");
3174 tty
->TN_max_pairs
= tgetnum ("pa");
3176 tty
->TN_no_color_video
= tgetnum ("NC");
3177 if (tty
->TN_no_color_video
== -1)
3178 tty
->TN_no_color_video
= 0;
3181 tty_default_color_capabilities (tty
, 1);
3183 MagicWrap (tty
) = tgetflag ("xn");
3184 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3185 the former flag imply the latter. */
3186 AutoWrap (tty
) = MagicWrap (tty
) || tgetflag ("am");
3187 terminal
->memory_below_frame
= tgetflag ("db");
3188 tty
->TF_hazeltine
= tgetflag ("hz");
3189 terminal
->must_write_spaces
= tgetflag ("in");
3190 tty
->meta_key
= tgetflag ("km") || tgetflag ("MT");
3191 tty
->TF_insmode_motion
= tgetflag ("mi");
3192 tty
->TF_standout_motion
= tgetflag ("ms");
3193 tty
->TF_underscore
= tgetflag ("ul");
3194 tty
->TF_teleray
= tgetflag ("xt");
3199 struct frame
*f
= XFRAME (selected_frame
);
3201 initialize_w32_display (terminal
);
3203 FrameRows (tty
) = FRAME_LINES (f
);
3204 FrameCols (tty
) = FRAME_COLS (f
);
3205 tty
->specified_window
= FRAME_LINES (f
);
3207 FRAME_VERTICAL_SCROLL_BAR_TYPE (f
) = vertical_scroll_bar_none
;
3208 terminal
->char_ins_del_ok
= 1;
3214 if (strcmp (terminal_type
, "internal") == 0)
3215 terminal
->type
= output_msdos_raw
;
3216 initialize_msdos_display (terminal
);
3218 get_tty_size (fileno (tty
->input
), &width
, &height
);
3219 FrameCols (tty
) = width
;
3220 FrameRows (tty
) = height
;
3221 terminal
->char_ins_del_ok
= 0;
3222 init_baud_rate (fileno (tty
->input
));
3225 tty
->output
= stdout
;
3227 /* The following two are inaccessible from w32console.c. */
3228 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
3229 terminal
->delete_terminal_hook
= &delete_tty
;
3231 tty
->name
= xstrdup (name
);
3232 terminal
->name
= xstrdup (name
);
3233 tty
->type
= xstrdup (terminal_type
);
3235 add_keyboard_wait_descriptor (0);
3237 tty
->delete_in_insert_mode
= 1;
3240 terminal
->scroll_region_ok
= 0;
3242 /* Seems to insert lines when it's not supposed to, messing up the
3243 display. In doing a trace, it didn't seem to be called much, so I
3244 don't think we're losing anything by turning it off. */
3245 terminal
->line_ins_del_ok
= 0;
3247 tty
->TN_max_colors
= 16; /* Required to be non-zero for tty-display-color-p */
3251 terminal
->mouse_position_hook
= term_mouse_position
;
3252 tty
->mouse_highlight
.mouse_face_window
= Qnil
;
3255 terminal
->kboard
= xmalloc (sizeof *terminal
->kboard
);
3256 init_kboard (terminal
->kboard
);
3257 kset_window_system (terminal
->kboard
, Qnil
);
3258 terminal
->kboard
->next_kboard
= all_kboards
;
3259 all_kboards
= terminal
->kboard
;
3260 terminal
->kboard
->reference_count
++;
3261 /* Don't let the initial kboard remain current longer than necessary.
3262 That would cause problems if a file loaded on startup tries to
3263 prompt in the mini-buffer. */
3264 if (current_kboard
== initial_kboard
)
3265 current_kboard
= terminal
->kboard
;
3267 term_get_fkeys (address
, terminal
->kboard
);
3269 /* Get frame size from system, or else from termcap. */
3272 get_tty_size (fileno (tty
->input
), &width
, &height
);
3273 FrameCols (tty
) = width
;
3274 FrameRows (tty
) = height
;
3277 if (FrameCols (tty
) <= 0)
3278 FrameCols (tty
) = tgetnum ("co");
3279 if (FrameRows (tty
) <= 0)
3280 FrameRows (tty
) = tgetnum ("li");
3282 if (FrameRows (tty
) < 3 || FrameCols (tty
) < 3)
3283 maybe_fatal (must_succeed
, terminal
,
3284 "Screen size %dx%d is too small",
3285 "Screen size %dx%d is too small",
3286 FrameCols (tty
), FrameRows (tty
));
3288 TabWidth (tty
) = tgetnum ("tw");
3291 tty
->TS_bell
= "\07";
3293 if (!tty
->TS_fwd_scroll
)
3294 tty
->TS_fwd_scroll
= Down (tty
);
3296 PC
= tty
->TS_pad_char
? *tty
->TS_pad_char
: 0;
3298 if (TabWidth (tty
) < 0)
3301 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3302 and newer termcap doc does not seem to say there is a default.
3303 if (!tty->Wcm->cm_tab)
3304 tty->Wcm->cm_tab = "\t";
3307 /* We don't support standout modes that use `magic cookies', so
3308 turn off any that do. */
3309 if (tty
->TS_standout_mode
&& tgetnum ("sg") >= 0)
3311 tty
->TS_standout_mode
= 0;
3312 tty
->TS_end_standout_mode
= 0;
3314 if (tty
->TS_enter_underline_mode
&& tgetnum ("ug") >= 0)
3316 tty
->TS_enter_underline_mode
= 0;
3317 tty
->TS_exit_underline_mode
= 0;
3320 /* If there's no standout mode, try to use underlining instead. */
3321 if (tty
->TS_standout_mode
== 0)
3323 tty
->TS_standout_mode
= tty
->TS_enter_underline_mode
;
3324 tty
->TS_end_standout_mode
= tty
->TS_exit_underline_mode
;
3327 /* If no `se' string, try using a `me' string instead.
3328 If that fails, we can't use standout mode at all. */
3329 if (tty
->TS_end_standout_mode
== 0)
3331 char *s
= tgetstr ("me", address
);
3333 tty
->TS_end_standout_mode
= s
;
3335 tty
->TS_standout_mode
= 0;
3338 if (tty
->TF_teleray
)
3340 tty
->Wcm
->cm_tab
= 0;
3341 /* We can't support standout mode, because it uses magic cookies. */
3342 tty
->TS_standout_mode
= 0;
3343 /* But that means we cannot rely on ^M to go to column zero! */
3345 /* LF can't be trusted either -- can alter hpos */
3346 /* if move at column 0 thru a line with TS_standout_mode */
3350 tty
->specified_window
= FrameRows (tty
);
3352 if (Wcm_init (tty
) == -1) /* can't do cursor motion */
3354 maybe_fatal (must_succeed
, terminal
,
3355 "Terminal type \"%s\" is not powerful enough to run Emacs",
3356 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3357 It lacks the ability to position the cursor.\n\
3358 If that is not the actual type of terminal you have,\n\
3359 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3360 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3362 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3363 # else /* TERMCAP */
3364 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3365 # endif /* TERMINFO */
3369 if (FrameRows (tty
) <= 0 || FrameCols (tty
) <= 0)
3370 maybe_fatal (must_succeed
, terminal
,
3371 "Could not determine the frame size",
3372 "Could not determine the frame size");
3374 tty
->delete_in_insert_mode
3375 = tty
->TS_delete_mode
&& tty
->TS_insert_mode
3376 && !strcmp (tty
->TS_delete_mode
, tty
->TS_insert_mode
);
3378 tty
->se_is_so
= (tty
->TS_standout_mode
3379 && tty
->TS_end_standout_mode
3380 && !strcmp (tty
->TS_standout_mode
, tty
->TS_end_standout_mode
));
3382 UseTabs (tty
) = tabs_safe_p (fileno (tty
->input
)) && TabWidth (tty
) == 8;
3384 terminal
->scroll_region_ok
3386 && (tty
->TS_set_window
|| tty
->TS_set_scroll_region
|| tty
->TS_set_scroll_region_1
));
3388 terminal
->line_ins_del_ok
3389 = (((tty
->TS_ins_line
|| tty
->TS_ins_multi_lines
)
3390 && (tty
->TS_del_line
|| tty
->TS_del_multi_lines
))
3391 || (terminal
->scroll_region_ok
3392 && tty
->TS_fwd_scroll
&& tty
->TS_rev_scroll
));
3394 terminal
->char_ins_del_ok
3395 = ((tty
->TS_ins_char
|| tty
->TS_insert_mode
3396 || tty
->TS_pad_inserted_char
|| tty
->TS_ins_multi_chars
)
3397 && (tty
->TS_del_char
|| tty
->TS_del_multi_chars
));
3399 terminal
->fast_clear_end_of_line
= tty
->TS_clr_line
!= 0;
3401 init_baud_rate (fileno (tty
->input
));
3403 #endif /* not DOS_NT */
3405 /* Init system terminal modes (RAW or CBREAK, etc.). */
3406 init_sys_modes (tty
);
3413 vfatal (const char *str
, va_list ap
)
3415 fprintf (stderr
, "emacs: ");
3416 vfprintf (stderr
, str
, ap
);
3417 if (!(strlen (str
) > 0 && str
[strlen (str
) - 1] == '\n'))
3418 fprintf (stderr
, "\n");
3424 /* Auxiliary error-handling function for init_tty.
3425 Delete TERMINAL, then call error or fatal with str1 or str2,
3426 respectively, according to whether MUST_SUCCEED is zero or not. */
3429 maybe_fatal (int must_succeed
, struct terminal
*terminal
,
3430 const char *str1
, const char *str2
, ...)
3433 va_start (ap
, str2
);
3435 delete_tty (terminal
);
3447 fatal (const char *str
, ...)
3457 /* Delete the given tty terminal, closing all frames on it. */
3460 delete_tty (struct terminal
*terminal
)
3462 struct tty_display_info
*tty
;
3464 /* Protect against recursive calls. delete_frame in
3465 delete_terminal calls us back when it deletes our last frame. */
3466 if (!terminal
->name
)
3469 if (terminal
->type
!= output_termcap
)
3472 tty
= terminal
->display_info
.tty
;
3474 if (tty
== tty_list
)
3475 tty_list
= tty
->next
;
3478 struct tty_display_info
*p
;
3479 for (p
= tty_list
; p
&& p
->next
!= tty
; p
= p
->next
)
3483 /* This should not happen. */
3486 p
->next
= tty
->next
;
3490 /* reset_sys_modes needs a valid device, so this call needs to be
3491 before delete_terminal. */
3492 reset_sys_modes (tty
);
3494 delete_terminal (terminal
);
3501 delete_keyboard_wait_descriptor (fileno (tty
->input
));
3502 if (tty
->input
!= stdin
)
3503 fclose (tty
->input
);
3505 if (tty
->output
&& tty
->output
!= stdout
&& tty
->output
!= tty
->input
)
3506 fclose (tty
->output
);
3507 if (tty
->termscript
)
3508 fclose (tty
->termscript
);
3510 xfree (tty
->old_tty
);
3512 xfree (tty
->termcap_strings_buffer
);
3513 xfree (tty
->termcap_term_buffer
);
3521 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo
,
3522 doc
: /* Non-nil means the system uses terminfo rather than termcap.
3523 This variable can be used by terminal emulator packages. */);
3525 system_uses_terminfo
= 1;
3527 system_uses_terminfo
= 0;
3530 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions
,
3531 doc
: /* Functions run after suspending a tty.
3532 The functions are run with one argument, the terminal object to be suspended.
3533 See `suspend-tty'. */);
3534 Vsuspend_tty_functions
= Qnil
;
3537 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions
,
3538 doc
: /* Functions run after resuming a tty.
3539 The functions are run with one argument, the terminal object that was revived.
3540 See `resume-tty'. */);
3541 Vresume_tty_functions
= Qnil
;
3543 DEFVAR_BOOL ("visible-cursor", visible_cursor
,
3544 doc
: /* Non-nil means to make the cursor very visible.
3545 This only has an effect when running in a text terminal.
3546 What means \"very visible\" is up to your terminal. It may make the cursor
3547 bigger, or it may make it blink, or it may do nothing at all. */);
3550 defsubr (&Stty_display_color_p
);
3551 defsubr (&Stty_display_color_cells
);
3552 defsubr (&Stty_no_underline
);
3553 defsubr (&Stty_type
);
3554 defsubr (&Scontrolling_tty_p
);
3555 defsubr (&Stty_top_frame
);
3556 defsubr (&Ssuspend_tty
);
3557 defsubr (&Sresume_tty
);
3559 defsubr (&Sgpm_mouse_start
);
3560 defsubr (&Sgpm_mouse_stop
);
3561 #endif /* HAVE_GPM */
3564 default_orig_pair
= NULL
;
3565 default_set_foreground
= NULL
;
3566 default_set_background
= NULL
;
3567 #endif /* !DOS_NT */
3569 encode_terminal_src
= NULL
;
3570 encode_terminal_dst
= NULL
;