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>. */
35 #include "character.h"
39 #include "composite.h"
43 #include "termhooks.h"
44 #include "dispextern.h"
47 #include "blockinput.h"
48 #include "syssignal.h"
50 #include "intervals.h"
53 static int been_here
= -1;
69 /* The name of the default console device. */
71 #define DEV_TTY "CONOUT$"
73 #define DEV_TTY "/dev/tty"
76 static void tty_set_scroll_region (struct frame
*f
, int start
, int stop
);
77 static void turn_on_face (struct frame
*, int face_id
);
78 static void turn_off_face (struct frame
*, int face_id
);
79 static void tty_turn_off_highlight (struct tty_display_info
*);
80 static void tty_show_cursor (struct tty_display_info
*);
81 static void tty_hide_cursor (struct tty_display_info
*);
82 static void tty_background_highlight (struct tty_display_info
*tty
);
83 static struct terminal
*get_tty_terminal (Lisp_Object
, int);
84 static void clear_tty_hooks (struct terminal
*terminal
);
85 static void set_tty_hooks (struct terminal
*terminal
);
86 static void dissociate_if_controlling_tty (int fd
);
87 static void delete_tty (struct terminal
*);
88 static _Noreturn
void maybe_fatal (int must_succeed
, struct terminal
*terminal
,
89 const char *str1
, const char *str2
, ...)
90 ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
91 static _Noreturn
void vfatal (const char *str
, va_list ap
)
92 ATTRIBUTE_FORMAT_PRINTF (1, 0);
95 #define OUTPUT(tty, a) \
96 emacs_tputs ((tty), a, \
97 (int) (FRAME_LINES (XFRAME (selected_frame)) \
101 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
102 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
104 #define OUTPUT_IF(tty, a) \
110 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
112 /* Display space properties */
114 /* Chain of all tty device parameters. */
115 struct tty_display_info
*tty_list
;
117 /* Meaning of bits in no_color_video. Each bit set means that the
118 corresponding attribute cannot be combined with colors. */
122 NC_STANDOUT
= 1 << 0,
123 NC_UNDERLINE
= 1 << 1,
134 /* The largest frame width in any call to calculate_costs. */
136 static int max_frame_cols
;
138 /* Non-zero if we have dropped our controlling tty and therefore
139 should not open a frame on stdout. */
140 static int no_controlling_tty
;
145 #include <sys/fcntl.h>
147 /* The device for which we have enabled gpm support (or NULL). */
148 struct tty_display_info
*gpm_tty
= NULL
;
150 /* Last recorded mouse coordinates. */
151 static int last_mouse_x
, last_mouse_y
;
152 #endif /* HAVE_GPM */
154 /* Ring the bell on a tty. */
157 tty_ring_bell (struct frame
*f
)
159 struct tty_display_info
*tty
= FRAME_TTY (f
);
163 OUTPUT (tty
, (tty
->TS_visible_bell
&& visible_bell
164 ? tty
->TS_visible_bell
166 fflush (tty
->output
);
170 /* Set up termcap modes for Emacs. */
173 tty_set_terminal_modes (struct terminal
*terminal
)
175 struct tty_display_info
*tty
= terminal
->display_info
.tty
;
179 if (tty
->TS_termcap_modes
)
180 OUTPUT (tty
, tty
->TS_termcap_modes
);
183 /* Output enough newlines to scroll all the old screen contents
184 off the screen, so it won't be overwritten and lost. */
187 for (i
= 0; i
< FRAME_LINES (XFRAME (selected_frame
)); i
++)
191 OUTPUT_IF (tty
, visible_cursor
? tty
->TS_cursor_visible
: tty
->TS_cursor_normal
);
192 OUTPUT_IF (tty
, tty
->TS_keypad_mode
);
194 fflush (tty
->output
);
198 /* Reset termcap modes before exiting Emacs. */
201 tty_reset_terminal_modes (struct terminal
*terminal
)
203 struct tty_display_info
*tty
= terminal
->display_info
.tty
;
207 tty_turn_off_highlight (tty
);
208 tty_turn_off_insert (tty
);
209 OUTPUT_IF (tty
, tty
->TS_end_keypad_mode
);
210 OUTPUT_IF (tty
, tty
->TS_cursor_normal
);
211 OUTPUT_IF (tty
, tty
->TS_end_termcap_modes
);
212 OUTPUT_IF (tty
, tty
->TS_orig_pair
);
213 /* Output raw CR so kernel can track the cursor hpos. */
216 fflush (tty
->output
);
220 /* Flag the end of a display update on a termcap terminal. */
223 tty_update_end (struct frame
*f
)
225 struct tty_display_info
*tty
= FRAME_TTY (f
);
227 if (!XWINDOW (selected_window
)->cursor_off_p
)
228 tty_show_cursor (tty
);
229 tty_turn_off_insert (tty
);
230 tty_background_highlight (tty
);
233 /* The implementation of set_terminal_window for termcap frames. */
236 tty_set_terminal_window (struct frame
*f
, int size
)
238 struct tty_display_info
*tty
= FRAME_TTY (f
);
240 tty
->specified_window
= size
? size
: FRAME_LINES (f
);
241 if (FRAME_SCROLL_REGION_OK (f
))
242 tty_set_scroll_region (f
, 0, tty
->specified_window
);
246 tty_set_scroll_region (struct frame
*f
, int start
, int stop
)
249 struct tty_display_info
*tty
= FRAME_TTY (f
);
251 if (tty
->TS_set_scroll_region
)
252 buf
= tparam (tty
->TS_set_scroll_region
, 0, 0, start
, stop
- 1, 0, 0);
253 else if (tty
->TS_set_scroll_region_1
)
254 buf
= tparam (tty
->TS_set_scroll_region_1
, 0, 0,
255 FRAME_LINES (f
), start
,
256 FRAME_LINES (f
) - stop
,
259 buf
= tparam (tty
->TS_set_window
, 0, 0, start
, 0, stop
, FRAME_COLS (f
));
268 tty_turn_on_insert (struct tty_display_info
*tty
)
270 if (!tty
->insert_mode
)
271 OUTPUT (tty
, tty
->TS_insert_mode
);
272 tty
->insert_mode
= 1;
276 tty_turn_off_insert (struct tty_display_info
*tty
)
278 if (tty
->insert_mode
)
279 OUTPUT (tty
, tty
->TS_end_insert_mode
);
280 tty
->insert_mode
= 0;
283 /* Handle highlighting. */
286 tty_turn_off_highlight (struct tty_display_info
*tty
)
288 if (tty
->standout_mode
)
289 OUTPUT_IF (tty
, tty
->TS_end_standout_mode
);
290 tty
->standout_mode
= 0;
294 tty_turn_on_highlight (struct tty_display_info
*tty
)
296 if (!tty
->standout_mode
)
297 OUTPUT_IF (tty
, tty
->TS_standout_mode
);
298 tty
->standout_mode
= 1;
302 tty_toggle_highlight (struct tty_display_info
*tty
)
304 if (tty
->standout_mode
)
305 tty_turn_off_highlight (tty
);
307 tty_turn_on_highlight (tty
);
311 /* Make cursor invisible. */
314 tty_hide_cursor (struct tty_display_info
*tty
)
316 if (tty
->cursor_hidden
== 0)
318 tty
->cursor_hidden
= 1;
319 OUTPUT_IF (tty
, tty
->TS_cursor_invisible
);
324 /* Ensure that cursor is visible. */
327 tty_show_cursor (struct tty_display_info
*tty
)
329 if (tty
->cursor_hidden
)
331 tty
->cursor_hidden
= 0;
332 OUTPUT_IF (tty
, tty
->TS_cursor_normal
);
334 OUTPUT_IF (tty
, tty
->TS_cursor_visible
);
339 /* Set standout mode to the state it should be in for
340 empty space inside windows. What this is,
341 depends on the user option inverse-video. */
344 tty_background_highlight (struct tty_display_info
*tty
)
347 tty_turn_on_highlight (tty
);
349 tty_turn_off_highlight (tty
);
352 /* Set standout mode to the mode specified for the text to be output. */
355 tty_highlight_if_desired (struct tty_display_info
*tty
)
358 tty_turn_on_highlight (tty
);
360 tty_turn_off_highlight (tty
);
364 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
365 frame-relative coordinates. */
368 tty_cursor_to (struct frame
*f
, int vpos
, int hpos
)
370 struct tty_display_info
*tty
= FRAME_TTY (f
);
372 /* Detect the case where we are called from reset_sys_modes
373 and the costs have never been calculated. Do nothing. */
374 if (! tty
->costs_set
)
377 if (curY (tty
) == vpos
378 && curX (tty
) == hpos
)
380 if (!tty
->TF_standout_motion
)
381 tty_background_highlight (tty
);
382 if (!tty
->TF_insmode_motion
)
383 tty_turn_off_insert (tty
);
384 cmgoto (tty
, vpos
, hpos
);
387 /* Similar but don't take any account of the wasted characters. */
390 tty_raw_cursor_to (struct frame
*f
, int row
, int col
)
392 struct tty_display_info
*tty
= FRAME_TTY (f
);
394 if (curY (tty
) == row
395 && curX (tty
) == col
)
397 if (!tty
->TF_standout_motion
)
398 tty_background_highlight (tty
);
399 if (!tty
->TF_insmode_motion
)
400 tty_turn_off_insert (tty
);
401 cmgoto (tty
, row
, col
);
404 /* Erase operations */
406 /* Clear from cursor to end of frame on a termcap device. */
409 tty_clear_to_end (struct frame
*f
)
412 struct tty_display_info
*tty
= FRAME_TTY (f
);
414 if (tty
->TS_clr_to_bottom
)
416 tty_background_highlight (tty
);
417 OUTPUT (tty
, tty
->TS_clr_to_bottom
);
421 for (i
= curY (tty
); i
< FRAME_LINES (f
); i
++)
424 clear_end_of_line (f
, FRAME_COLS (f
));
429 /* Clear an entire termcap frame. */
432 tty_clear_frame (struct frame
*f
)
434 struct tty_display_info
*tty
= FRAME_TTY (f
);
436 if (tty
->TS_clr_frame
)
438 tty_background_highlight (tty
);
439 OUTPUT (tty
, tty
->TS_clr_frame
);
449 /* An implementation of clear_end_of_line for termcap frames.
451 Note that the cursor may be moved, on terminals lacking a `ce' string. */
454 tty_clear_end_of_line (struct frame
*f
, int first_unused_hpos
)
457 struct tty_display_info
*tty
= FRAME_TTY (f
);
459 /* Detect the case where we are called from reset_sys_modes
460 and the costs have never been calculated. Do nothing. */
461 if (! tty
->costs_set
)
464 if (curX (tty
) >= first_unused_hpos
)
466 tty_background_highlight (tty
);
467 if (tty
->TS_clr_line
)
469 OUTPUT1 (tty
, tty
->TS_clr_line
);
472 { /* have to do it the hard way */
473 tty_turn_off_insert (tty
);
475 /* Do not write in last row last col with Auto-wrap on. */
477 && curY (tty
) == FrameRows (tty
) - 1
478 && first_unused_hpos
== FrameCols (tty
))
481 for (i
= curX (tty
); i
< first_unused_hpos
; i
++)
484 fputc (' ', tty
->termscript
);
485 fputc (' ', tty
->output
);
487 cmplus (tty
, first_unused_hpos
- curX (tty
));
491 /* Buffers to store the source and result of code conversion for terminal. */
492 static unsigned char *encode_terminal_src
;
493 static unsigned char *encode_terminal_dst
;
494 /* Allocated sizes of the above buffers. */
495 static ptrdiff_t encode_terminal_src_size
;
496 static ptrdiff_t encode_terminal_dst_size
;
498 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
499 Set CODING->produced to the byte-length of the resulting byte
500 sequence, and return a pointer to that byte sequence. */
503 encode_terminal_code (struct glyph
*src
, int src_len
, struct coding_system
*coding
)
505 struct glyph
*src_end
= src
+ src_len
;
507 ptrdiff_t nchars
, nbytes
, required
;
508 ptrdiff_t tlen
= GLYPH_TABLE_LENGTH
;
509 register Lisp_Object
*tbase
= GLYPH_TABLE_BASE
;
510 Lisp_Object charset_list
;
512 /* Allocate sufficient size of buffer to store all characters in
513 multibyte-form. But, it may be enlarged on demand if
514 Vglyph_table contains a string or a composite glyph is
516 if (min (PTRDIFF_MAX
, SIZE_MAX
) / MAX_MULTIBYTE_LENGTH
< src_len
)
517 memory_full (SIZE_MAX
);
519 required
*= MAX_MULTIBYTE_LENGTH
;
520 if (encode_terminal_src_size
< required
)
522 encode_terminal_src
= xrealloc (encode_terminal_src
, required
);
523 encode_terminal_src_size
= required
;
526 charset_list
= coding_charset_list (coding
);
528 buf
= encode_terminal_src
;
530 while (src
< src_end
)
532 if (src
->type
== COMPOSITE_GLYPH
)
534 struct composition
*cmp
IF_LINT (= NULL
);
535 Lisp_Object gstring
IF_LINT (= Qnil
);
538 nbytes
= buf
- encode_terminal_src
;
539 if (src
->u
.cmp
.automatic
)
541 gstring
= composition_gstring_from_id (src
->u
.cmp
.id
);
542 required
= src
->slice
.cmp
.to
- src
->slice
.cmp
.from
+ 1;
546 cmp
= composition_table
[src
->u
.cmp
.id
];
547 required
= cmp
->glyph_len
;
548 required
*= MAX_MULTIBYTE_LENGTH
;
551 if (encode_terminal_src_size
- nbytes
< required
)
553 encode_terminal_src
=
554 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
555 required
- (encode_terminal_src_size
- nbytes
),
557 buf
= encode_terminal_src
+ nbytes
;
560 if (src
->u
.cmp
.automatic
)
561 for (i
= src
->slice
.cmp
.from
; i
<= src
->slice
.cmp
.to
; i
++)
563 Lisp_Object g
= LGSTRING_GLYPH (gstring
, i
);
564 int c
= LGLYPH_CHAR (g
);
566 if (! char_charset (c
, charset_list
, NULL
))
568 buf
+= CHAR_STRING (c
, buf
);
572 for (i
= 0; i
< cmp
->glyph_len
; i
++)
574 int c
= COMPOSITION_GLYPH (cmp
, i
);
576 /* TAB in a composition means display glyphs with
577 padding space on the left or right. */
580 if (char_charset (c
, charset_list
, NULL
))
582 if (CHAR_WIDTH (c
) == 0
583 && i
> 0 && COMPOSITION_GLYPH (cmp
, i
- 1) == '\t')
584 /* Should be left-padded */
586 buf
+= CHAR_STRING (' ', buf
);
592 buf
+= CHAR_STRING (c
, buf
);
596 /* We must skip glyphs to be padded for a wide character. */
597 else if (! CHAR_GLYPH_PADDING_P (*src
))
604 SET_GLYPH_FROM_CHAR_GLYPH (g
, src
[0]);
606 if (GLYPH_INVALID_P (g
) || GLYPH_SIMPLE_P (tbase
, tlen
, g
))
608 /* This glyph doesn't have an entry in Vglyph_table. */
613 /* This glyph has an entry in Vglyph_table,
614 so process any alias before testing for simpleness. */
615 GLYPH_FOLLOW_ALIASES (tbase
, tlen
, g
);
617 if (GLYPH_SIMPLE_P (tbase
, tlen
, g
))
618 /* We set the multi-byte form of a character in G
619 (that should be an ASCII character) at WORKBUF. */
622 /* We have a string in Vglyph_table. */
623 string
= tbase
[GLYPH_CHAR (g
)];
628 nbytes
= buf
- encode_terminal_src
;
629 if (encode_terminal_src_size
- nbytes
< MAX_MULTIBYTE_LENGTH
)
631 encode_terminal_src
=
632 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
633 MAX_MULTIBYTE_LENGTH
, -1, 1);
634 buf
= encode_terminal_src
+ nbytes
;
637 || char_charset (c
, charset_list
, NULL
))
639 /* Store the multibyte form of C at BUF. */
640 buf
+= CHAR_STRING (c
, buf
);
645 /* C is not encodable. */
648 while (src
+ 1 < src_end
&& CHAR_GLYPH_PADDING_P (src
[1]))
658 if (! STRING_MULTIBYTE (string
))
659 string
= string_to_multibyte (string
);
660 nbytes
= buf
- encode_terminal_src
;
661 if (encode_terminal_src_size
- nbytes
< SBYTES (string
))
663 encode_terminal_src
=
664 xpalloc (encode_terminal_src
, &encode_terminal_src_size
,
666 - (encode_terminal_src_size
- nbytes
)),
668 buf
= encode_terminal_src
+ nbytes
;
670 memcpy (buf
, SDATA (string
), SBYTES (string
));
671 buf
+= SBYTES (string
);
672 nchars
+= SCHARS (string
);
680 coding
->produced
= 0;
684 nbytes
= buf
- encode_terminal_src
;
685 coding
->source
= encode_terminal_src
;
686 if (encode_terminal_dst_size
== 0)
688 encode_terminal_dst
= xrealloc (encode_terminal_dst
,
689 encode_terminal_src_size
);
690 encode_terminal_dst_size
= encode_terminal_src_size
;
692 coding
->destination
= encode_terminal_dst
;
693 coding
->dst_bytes
= encode_terminal_dst_size
;
694 encode_coding_object (coding
, Qnil
, 0, 0, nchars
, nbytes
, Qnil
);
695 /* coding->destination may have been reallocated. */
696 encode_terminal_dst
= coding
->destination
;
697 encode_terminal_dst_size
= coding
->dst_bytes
;
699 return (encode_terminal_dst
);
704 /* An implementation of write_glyphs for termcap frames. */
707 tty_write_glyphs (struct frame
*f
, struct glyph
*string
, int len
)
709 unsigned char *conversion_buffer
;
710 struct coding_system
*coding
;
713 struct tty_display_info
*tty
= FRAME_TTY (f
);
715 tty_turn_off_insert (tty
);
716 tty_hide_cursor (tty
);
718 /* Don't dare write in last column of bottom line, if Auto-Wrap,
719 since that would scroll the whole frame on some terminals. */
722 && curY (tty
) + 1 == FRAME_LINES (f
)
723 && (curX (tty
) + len
) == FRAME_COLS (f
))
730 /* If terminal_coding does any conversion, use it, otherwise use
731 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
732 because it always return 1 if the member src_multibyte is 1. */
733 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
734 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
735 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
737 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
739 for (stringlen
= len
; stringlen
!= 0; stringlen
-= n
)
741 /* Identify a run of glyphs with the same face. */
742 int face_id
= string
->face_id
;
744 for (n
= 1; n
< stringlen
; ++n
)
745 if (string
[n
].face_id
!= face_id
)
748 /* Turn appearance modes of the face of the run on. */
749 tty_highlight_if_desired (tty
);
750 turn_on_face (f
, face_id
);
753 /* This is the last run. */
754 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
755 conversion_buffer
= encode_terminal_code (string
, n
, coding
);
756 if (coding
->produced
> 0)
759 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
760 if (ferror (tty
->output
))
761 clearerr (tty
->output
);
763 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
768 /* Turn appearance modes off. */
769 turn_off_face (f
, face_id
);
770 tty_turn_off_highlight (tty
);
776 #ifdef HAVE_GPM /* Only used by GPM code. */
779 tty_write_glyphs_with_face (register struct frame
*f
, register struct glyph
*string
,
780 register int len
, register int face_id
)
782 unsigned char *conversion_buffer
;
783 struct coding_system
*coding
;
785 struct tty_display_info
*tty
= FRAME_TTY (f
);
787 tty_turn_off_insert (tty
);
788 tty_hide_cursor (tty
);
790 /* Don't dare write in last column of bottom line, if Auto-Wrap,
791 since that would scroll the whole frame on some terminals. */
794 && curY (tty
) + 1 == FRAME_LINES (f
)
795 && (curX (tty
) + len
) == FRAME_COLS (f
))
802 /* If terminal_coding does any conversion, use it, otherwise use
803 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
804 because it always return 1 if the member src_multibyte is 1. */
805 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
806 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
807 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
809 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
811 /* Turn appearance modes of the face. */
812 tty_highlight_if_desired (tty
);
813 turn_on_face (f
, face_id
);
815 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
816 conversion_buffer
= encode_terminal_code (string
, len
, coding
);
817 if (coding
->produced
> 0)
820 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
821 if (ferror (tty
->output
))
822 clearerr (tty
->output
);
824 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
828 /* Turn appearance modes off. */
829 turn_off_face (f
, face_id
);
830 tty_turn_off_highlight (tty
);
836 /* An implementation of insert_glyphs for termcap frames. */
839 tty_insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
)
842 struct glyph
*glyph
= NULL
;
843 unsigned char *conversion_buffer
;
844 unsigned char space
[1];
845 struct coding_system
*coding
;
847 struct tty_display_info
*tty
= FRAME_TTY (f
);
849 if (tty
->TS_ins_multi_chars
)
851 buf
= tparam (tty
->TS_ins_multi_chars
, 0, 0, len
, 0, 0, 0);
855 write_glyphs (f
, start
, len
);
859 tty_turn_on_insert (tty
);
863 space
[0] = SPACEGLYPH
;
865 /* If terminal_coding does any conversion, use it, otherwise use
866 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
867 because it always return 1 if the member src_multibyte is 1. */
868 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
869 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
870 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
872 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
876 OUTPUT1_IF (tty
, tty
->TS_ins_char
);
879 conversion_buffer
= space
;
880 coding
->produced
= 1;
884 tty_highlight_if_desired (tty
);
885 turn_on_face (f
, start
->face_id
);
888 /* We must open sufficient space for a character which
889 occupies more than one column. */
890 while (len
&& CHAR_GLYPH_PADDING_P (*start
))
892 OUTPUT1_IF (tty
, tty
->TS_ins_char
);
897 /* This is the last glyph. */
898 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
900 conversion_buffer
= encode_terminal_code (glyph
, 1, coding
);
903 if (coding
->produced
> 0)
906 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->output
);
907 if (ferror (tty
->output
))
908 clearerr (tty
->output
);
910 fwrite (conversion_buffer
, 1, coding
->produced
, tty
->termscript
);
914 OUTPUT1_IF (tty
, tty
->TS_pad_inserted_char
);
917 turn_off_face (f
, glyph
->face_id
);
918 tty_turn_off_highlight (tty
);
925 /* An implementation of delete_glyphs for termcap frames. */
928 tty_delete_glyphs (struct frame
*f
, int n
)
933 struct tty_display_info
*tty
= FRAME_TTY (f
);
935 if (tty
->delete_in_insert_mode
)
937 tty_turn_on_insert (tty
);
941 tty_turn_off_insert (tty
);
942 OUTPUT_IF (tty
, tty
->TS_delete_mode
);
945 if (tty
->TS_del_multi_chars
)
947 buf
= tparam (tty
->TS_del_multi_chars
, 0, 0, n
, 0, 0, 0);
952 for (i
= 0; i
< n
; i
++)
953 OUTPUT1 (tty
, tty
->TS_del_char
);
954 if (!tty
->delete_in_insert_mode
)
955 OUTPUT_IF (tty
, tty
->TS_end_delete_mode
);
958 /* An implementation of ins_del_lines for termcap frames. */
961 tty_ins_del_lines (struct frame
*f
, int vpos
, int n
)
963 struct tty_display_info
*tty
= FRAME_TTY (f
);
965 n
> 0 ? tty
->TS_ins_multi_lines
: tty
->TS_del_multi_lines
;
966 const char *single
= n
> 0 ? tty
->TS_ins_line
: tty
->TS_del_line
;
967 const char *scroll
= n
> 0 ? tty
->TS_rev_scroll
: tty
->TS_fwd_scroll
;
969 register int i
= n
> 0 ? n
: -n
;
972 /* If the lines below the insertion are being pushed
973 into the end of the window, this is the same as clearing;
974 and we know the lines are already clear, since the matching
975 deletion has already been done. So can ignore this. */
976 /* If the lines below the deletion are blank lines coming
977 out of the end of the window, don't bother,
978 as there will be a matching inslines later that will flush them. */
979 if (FRAME_SCROLL_REGION_OK (f
)
980 && vpos
+ i
>= tty
->specified_window
)
982 if (!FRAME_MEMORY_BELOW_FRAME (f
)
983 && vpos
+ i
>= FRAME_LINES (f
))
988 raw_cursor_to (f
, vpos
, 0);
989 tty_background_highlight (tty
);
990 buf
= tparam (multi
, 0, 0, i
, 0, 0, 0);
996 raw_cursor_to (f
, vpos
, 0);
997 tty_background_highlight (tty
);
999 OUTPUT (tty
, single
);
1000 if (tty
->TF_teleray
)
1005 tty_set_scroll_region (f
, vpos
, tty
->specified_window
);
1007 raw_cursor_to (f
, tty
->specified_window
- 1, 0);
1009 raw_cursor_to (f
, vpos
, 0);
1010 tty_background_highlight (tty
);
1012 OUTPUTL (tty
, scroll
, tty
->specified_window
- vpos
);
1013 tty_set_scroll_region (f
, 0, tty
->specified_window
);
1016 if (!FRAME_SCROLL_REGION_OK (f
)
1017 && FRAME_MEMORY_BELOW_FRAME (f
)
1020 cursor_to (f
, FRAME_LINES (f
) + n
, 0);
1025 /* Compute cost of sending "str", in characters,
1026 not counting any line-dependent padding. */
1029 string_cost (const char *str
)
1033 tputs (str
, 0, evalcost
);
1037 /* Compute cost of sending "str", in characters,
1038 counting any line-dependent padding at one line. */
1041 string_cost_one_line (const char *str
)
1045 tputs (str
, 1, evalcost
);
1049 /* Compute per line amount of line-dependent padding,
1050 in tenths of characters. */
1053 per_line_cost (const char *str
)
1057 tputs (str
, 0, evalcost
);
1060 tputs (str
, 10, evalcost
);
1064 /* char_ins_del_cost[n] is cost of inserting N characters.
1065 char_ins_del_cost[-n] is cost of deleting N characters.
1066 The length of this vector is based on max_frame_cols. */
1068 int *char_ins_del_vector
;
1070 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1074 calculate_ins_del_char_costs (struct frame
*f
)
1076 struct tty_display_info
*tty
= FRAME_TTY (f
);
1077 int ins_startup_cost
, del_startup_cost
;
1078 int ins_cost_per_char
, del_cost_per_char
;
1082 if (tty
->TS_ins_multi_chars
)
1084 ins_cost_per_char
= 0;
1085 ins_startup_cost
= string_cost_one_line (tty
->TS_ins_multi_chars
);
1087 else if (tty
->TS_ins_char
|| tty
->TS_pad_inserted_char
1088 || (tty
->TS_insert_mode
&& tty
->TS_end_insert_mode
))
1090 ins_startup_cost
= (30 * (string_cost (tty
->TS_insert_mode
)
1091 + string_cost (tty
->TS_end_insert_mode
))) / 100;
1092 ins_cost_per_char
= (string_cost_one_line (tty
->TS_ins_char
)
1093 + string_cost_one_line (tty
->TS_pad_inserted_char
));
1097 ins_startup_cost
= 9999;
1098 ins_cost_per_char
= 0;
1101 if (tty
->TS_del_multi_chars
)
1103 del_cost_per_char
= 0;
1104 del_startup_cost
= string_cost_one_line (tty
->TS_del_multi_chars
);
1106 else if (tty
->TS_del_char
)
1108 del_startup_cost
= (string_cost (tty
->TS_delete_mode
)
1109 + string_cost (tty
->TS_end_delete_mode
));
1110 if (tty
->delete_in_insert_mode
)
1111 del_startup_cost
/= 2;
1112 del_cost_per_char
= string_cost_one_line (tty
->TS_del_char
);
1116 del_startup_cost
= 9999;
1117 del_cost_per_char
= 0;
1120 /* Delete costs are at negative offsets */
1121 p
= &char_ins_del_cost (f
)[0];
1122 for (i
= FRAME_COLS (f
); --i
>= 0;)
1123 *--p
= (del_startup_cost
+= del_cost_per_char
);
1125 /* Doing nothing is free */
1126 p
= &char_ins_del_cost (f
)[0];
1129 /* Insert costs are at positive offsets */
1130 for (i
= FRAME_COLS (f
); --i
>= 0;)
1131 *p
++ = (ins_startup_cost
+= ins_cost_per_char
);
1135 calculate_costs (struct frame
*frame
)
1137 FRAME_COST_BAUD_RATE (frame
) = baud_rate
;
1139 if (FRAME_TERMCAP_P (frame
))
1141 struct tty_display_info
*tty
= FRAME_TTY (frame
);
1142 register const char *f
= (tty
->TS_set_scroll_region
1143 ? tty
->TS_set_scroll_region
1144 : tty
->TS_set_scroll_region_1
);
1146 FRAME_SCROLL_REGION_COST (frame
) = string_cost (f
);
1150 /* These variables are only used for terminal stuff. They are
1151 allocated once for the terminal frame of X-windows emacs, but not
1154 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1155 X turns off char_ins_del_ok. */
1157 max_frame_cols
= max (max_frame_cols
, FRAME_COLS (frame
));
1158 if ((min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (int) - 1) / 2
1160 memory_full (SIZE_MAX
);
1162 char_ins_del_vector
=
1163 xrealloc (char_ins_del_vector
,
1164 (sizeof (int) + 2 * sizeof (int) * max_frame_cols
));
1166 memset (char_ins_del_vector
, 0,
1167 (sizeof (int) + 2 * sizeof (int) * max_frame_cols
));
1170 if (f
&& (!tty
->TS_ins_line
&& !tty
->TS_del_line
))
1171 do_line_insertion_deletion_costs (frame
,
1172 tty
->TS_rev_scroll
, tty
->TS_ins_multi_lines
,
1173 tty
->TS_fwd_scroll
, tty
->TS_del_multi_lines
,
1176 do_line_insertion_deletion_costs (frame
,
1177 tty
->TS_ins_line
, tty
->TS_ins_multi_lines
,
1178 tty
->TS_del_line
, tty
->TS_del_multi_lines
,
1181 calculate_ins_del_char_costs (frame
);
1183 /* Don't use TS_repeat if its padding is worse than sending the chars */
1184 if (tty
->TS_repeat
&& per_line_cost (tty
->TS_repeat
) * baud_rate
< 9000)
1185 tty
->RPov
= string_cost (tty
->TS_repeat
);
1187 tty
->RPov
= FRAME_COLS (frame
) * 2;
1189 cmcostinit (FRAME_TTY (frame
)); /* set up cursor motion costs */
1194 const char *cap
, *name
;
1197 /* Termcap capability names that correspond directly to X keysyms.
1198 Some of these (marked "terminfo") aren't supplied by old-style
1199 (Berkeley) termcap entries. They're listed in X keysym order;
1200 except we put the keypad keys first, so that if they clash with
1201 other keys (as on the IBM PC keyboard) they get overridden.
1204 static const struct fkey_table keys
[] =
1206 {"kh", "home"}, /* termcap */
1207 {"kl", "left"}, /* termcap */
1208 {"ku", "up"}, /* termcap */
1209 {"kr", "right"}, /* termcap */
1210 {"kd", "down"}, /* termcap */
1211 {"%8", "prior"}, /* terminfo */
1212 {"%5", "next"}, /* terminfo */
1213 {"@7", "end"}, /* terminfo */
1214 {"@1", "begin"}, /* terminfo */
1215 {"*6", "select"}, /* terminfo */
1216 {"%9", "print"}, /* terminfo */
1217 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1219 * "insert" --- see below
1221 {"&8", "undo"}, /* terminfo */
1222 {"%0", "redo"}, /* terminfo */
1223 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1224 {"@0", "find"}, /* terminfo */
1225 {"@2", "cancel"}, /* terminfo */
1226 {"%1", "help"}, /* terminfo */
1228 * "break" goes here, but can't be reliably intercepted with termcap
1230 {"&4", "reset"}, /* terminfo --- actually `restart' */
1232 * "system" and "user" --- no termcaps
1234 {"kE", "clearline"}, /* terminfo */
1235 {"kA", "insertline"}, /* terminfo */
1236 {"kL", "deleteline"}, /* terminfo */
1237 {"kI", "insertchar"}, /* terminfo */
1238 {"kD", "deletechar"}, /* terminfo */
1239 {"kB", "backtab"}, /* terminfo */
1241 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1243 {"@8", "kp-enter"}, /* terminfo */
1245 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1246 * "kp-multiply", "kp-add", "kp-separator",
1247 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1248 * --- no termcaps for any of these.
1250 {"K4", "kp-1"}, /* terminfo */
1252 * "kp-2" --- no termcap
1254 {"K5", "kp-3"}, /* terminfo */
1256 * "kp-4" --- no termcap
1258 {"K2", "kp-5"}, /* terminfo */
1260 * "kp-6" --- no termcap
1262 {"K1", "kp-7"}, /* terminfo */
1264 * "kp-8" --- no termcap
1266 {"K3", "kp-9"}, /* terminfo */
1268 * "kp-equal" --- no termcap
1280 {"&0", "S-cancel"}, /*shifted cancel key*/
1281 {"&9", "S-begin"}, /*shifted begin key*/
1282 {"*0", "S-find"}, /*shifted find key*/
1283 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1284 {"*4", "S-delete"}, /*shifted delete-character key*/
1285 {"*7", "S-end"}, /*shifted end key*/
1286 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1287 {"#1", "S-help"}, /*shifted help key*/
1288 {"#2", "S-home"}, /*shifted home key*/
1289 {"#3", "S-insert"}, /*shifted insert-character key*/
1290 {"#4", "S-left"}, /*shifted left-arrow key*/
1291 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1292 {"%c", "S-next"}, /*shifted next key*/
1293 {"%e", "S-prior"}, /*shifted previous key*/
1294 {"%f", "S-print"}, /*shifted print key*/
1295 {"%g", "S-redo"}, /*shifted redo key*/
1296 {"%i", "S-right"}, /*shifted right-arrow key*/
1297 {"!3", "S-undo"} /*shifted undo key*/
1301 static char **term_get_fkeys_address
;
1302 static KBOARD
*term_get_fkeys_kboard
;
1303 static Lisp_Object
term_get_fkeys_1 (void);
1305 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1306 This function scans the termcap function key sequence entries, and
1307 adds entries to Vinput_decode_map for each function key it finds. */
1310 term_get_fkeys (char **address
, KBOARD
*kboard
)
1312 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1313 errors during the call. The only errors should be from Fdefine_key
1314 when given a key sequence containing an invalid prefix key. If the
1315 termcap defines function keys which use a prefix that is already bound
1316 to a command by the default bindings, we should silently ignore that
1317 function key specification, rather than giving the user an error and
1318 refusing to run at all on such a terminal. */
1320 term_get_fkeys_address
= address
;
1321 term_get_fkeys_kboard
= kboard
;
1322 internal_condition_case (term_get_fkeys_1
, Qerror
, Fidentity
);
1326 term_get_fkeys_1 (void)
1330 char **address
= term_get_fkeys_address
;
1331 KBOARD
*kboard
= term_get_fkeys_kboard
;
1333 /* This can happen if CANNOT_DUMP or with strange options. */
1334 if (!KEYMAPP (KVAR (kboard
, Vinput_decode_map
)))
1335 kset_input_decode_map (kboard
, Fmake_sparse_keymap (Qnil
));
1337 for (i
= 0; i
< (sizeof (keys
)/sizeof (keys
[0])); i
++)
1339 char *sequence
= tgetstr (keys
[i
].cap
, address
);
1341 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (sequence
),
1342 Fmake_vector (make_number (1),
1343 intern (keys
[i
].name
)));
1346 /* The uses of the "k0" capability are inconsistent; sometimes it
1347 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1348 We will attempt to politely accommodate both systems by testing for
1349 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1352 const char *k_semi
= tgetstr ("k;", address
);
1353 const char *k0
= tgetstr ("k0", address
);
1354 const char *k0_name
= "f10";
1359 /* Define f0 first, so that f10 takes precedence in case the
1360 key sequences happens to be the same. */
1361 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k0
),
1362 Fmake_vector (make_number (1), intern ("f0")));
1363 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k_semi
),
1364 Fmake_vector (make_number (1), intern ("f10")));
1367 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (k0
),
1368 Fmake_vector (make_number (1), intern (k0_name
)));
1371 /* Set up cookies for numbered function keys above f10. */
1373 char fcap
[3], fkey
[4];
1375 fcap
[0] = 'F'; fcap
[2] = '\0';
1376 for (i
= 11; i
< 64; i
++)
1379 fcap
[1] = '1' + i
- 11;
1381 fcap
[1] = 'A' + i
- 20;
1383 fcap
[1] = 'a' + i
- 46;
1386 char *sequence
= tgetstr (fcap
, address
);
1389 sprintf (fkey
, "f%d", i
);
1390 Fdefine_key (KVAR (kboard
, Vinput_decode_map
), build_string (sequence
),
1391 Fmake_vector (make_number (1),
1399 * Various mappings to try and get a better fit.
1402 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1403 if (!tgetstr (cap1, address)) \
1405 char *sequence = tgetstr (cap2, address); \
1407 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1408 Fmake_vector (make_number (1), \
1412 /* if there's no key_next keycap, map key_npage to `next' keysym */
1413 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1414 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1415 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1416 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1417 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1418 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1419 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1421 /* IBM has their own non-standard dialect of terminfo.
1422 If the standard name isn't found, try the IBM name. */
1423 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1424 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1425 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1426 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1427 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1428 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1429 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1430 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1431 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1432 #undef CONDITIONAL_REASSIGN
1437 #endif /* not DOS_NT */
1440 /***********************************************************************
1441 Character Display Information
1442 ***********************************************************************/
1443 static void append_glyph (struct it
*);
1444 static void append_composite_glyph (struct it
*);
1445 static void produce_composite_glyph (struct it
*);
1446 static void append_glyphless_glyph (struct it
*, int, const char *);
1447 static void produce_glyphless_glyph (struct it
*, int, Lisp_Object
);
1449 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1450 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1451 the character for which to produce glyphs; IT->face_id contains the
1452 character's face. Padding glyphs are appended if IT->c has a
1453 IT->pixel_width > 1. */
1456 append_glyph (struct it
*it
)
1458 struct glyph
*glyph
, *end
;
1461 eassert (it
->glyph_row
);
1462 glyph
= (it
->glyph_row
->glyphs
[it
->area
]
1463 + it
->glyph_row
->used
[it
->area
]);
1464 end
= it
->glyph_row
->glyphs
[1 + it
->area
];
1466 /* If the glyph row is reversed, we need to prepend the glyph rather
1468 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1471 int move_by
= it
->pixel_width
;
1473 /* Make room for the new glyphs. */
1474 if (move_by
> end
- glyph
) /* don't overstep end of this area */
1475 move_by
= end
- glyph
;
1476 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1478 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1479 end
= glyph
+ move_by
;
1482 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1483 right, even in the REVERSED_P case, since (a) all of its u.ch are
1484 identical, and (b) the PADDING_P flag needs to be set for the
1485 leftmost one, because we write to the terminal left-to-right. */
1487 i
< it
->pixel_width
&& glyph
< end
;
1490 glyph
->type
= CHAR_GLYPH
;
1491 glyph
->pixel_width
= 1;
1492 glyph
->u
.ch
= it
->char_to_display
;
1493 glyph
->face_id
= it
->face_id
;
1494 glyph
->padding_p
= i
> 0;
1495 glyph
->charpos
= CHARPOS (it
->position
);
1496 glyph
->object
= it
->object
;
1499 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1500 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1502 glyph
->bidi_type
= it
->bidi_it
.type
;
1506 glyph
->resolved_level
= 0;
1507 glyph
->bidi_type
= UNKNOWN_BT
;
1510 ++it
->glyph_row
->used
[it
->area
];
1515 /* For external use. */
1517 tty_append_glyph (struct it
*it
)
1523 /* Produce glyphs for the display element described by IT. *IT
1524 specifies what we want to produce a glyph for (character, image, ...),
1525 and where in the glyph matrix we currently are (glyph row and hpos).
1526 produce_glyphs fills in output fields of *IT with information such as the
1527 pixel width and height of a character, and maybe output actual glyphs at
1528 the same time if IT->glyph_row is non-null. For an overview, see
1529 the explanation in dispextern.h, before the definition of the
1530 display_element_type enumeration.
1532 produce_glyphs also stores the result of glyph width, ascent
1533 etc. computations in *IT.
1535 IT->glyph_row may be null, in which case produce_glyphs does not
1536 actually fill in the glyphs. This is used in the move_* functions
1537 in xdisp.c for text width and height computations.
1539 Callers usually don't call produce_glyphs directly;
1540 instead they use the macro PRODUCE_GLYPHS. */
1543 produce_glyphs (struct it
*it
)
1545 /* If a hook is installed, let it do the work. */
1547 /* Nothing but characters are supported on terminal frames. */
1548 eassert (it
->what
== IT_CHARACTER
1549 || it
->what
== IT_COMPOSITION
1550 || it
->what
== IT_STRETCH
1551 || it
->what
== IT_GLYPHLESS
);
1553 if (it
->what
== IT_STRETCH
)
1555 produce_stretch_glyph (it
);
1559 if (it
->what
== IT_COMPOSITION
)
1561 produce_composite_glyph (it
);
1565 if (it
->what
== IT_GLYPHLESS
)
1567 produce_glyphless_glyph (it
, 0, Qnil
);
1571 if (it
->char_to_display
>= 040 && it
->char_to_display
< 0177)
1573 it
->pixel_width
= it
->nglyphs
= 1;
1577 else if (it
->char_to_display
== '\n')
1578 it
->pixel_width
= it
->nglyphs
= 0;
1579 else if (it
->char_to_display
== '\t')
1581 int absolute_x
= (it
->current_x
1582 + it
->continuation_lines_width
);
1584 = (((1 + absolute_x
+ it
->tab_width
- 1)
1589 /* If part of the TAB has been displayed on the previous line
1590 which is continued now, continuation_lines_width will have
1591 been incremented already by the part that fitted on the
1592 continued line. So, we will get the right number of spaces
1594 nspaces
= next_tab_x
- absolute_x
;
1600 it
->char_to_display
= ' ';
1601 it
->pixel_width
= it
->len
= 1;
1607 it
->pixel_width
= nspaces
;
1608 it
->nglyphs
= nspaces
;
1610 else if (CHAR_BYTE8_P (it
->char_to_display
))
1612 /* Coming here means that we must send the raw 8-bit byte as is
1613 to the terminal. Although there's no way to know how many
1614 columns it occupies on a screen, it is a good assumption that
1615 a single byte code has 1-column width. */
1616 it
->pixel_width
= it
->nglyphs
= 1;
1622 Lisp_Object charset_list
= FRAME_TERMINAL (it
->f
)->charset_list
;
1624 if (char_charset (it
->char_to_display
, charset_list
, NULL
))
1626 it
->pixel_width
= CHAR_WIDTH (it
->char_to_display
);
1627 it
->nglyphs
= it
->pixel_width
;
1633 Lisp_Object acronym
= lookup_glyphless_char_display (-1, it
);
1635 eassert (it
->what
== IT_GLYPHLESS
);
1636 produce_glyphless_glyph (it
, 1, acronym
);
1641 /* Advance current_x by the pixel width as a convenience for
1643 if (it
->area
== TEXT_AREA
)
1644 it
->current_x
+= it
->pixel_width
;
1645 it
->ascent
= it
->max_ascent
= it
->phys_ascent
= it
->max_phys_ascent
= 0;
1646 it
->descent
= it
->max_descent
= it
->phys_descent
= it
->max_phys_descent
= 1;
1649 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1650 Called from produce_composite_glyph for terminal frames if
1651 IT->glyph_row != NULL. IT->face_id contains the character's
1655 append_composite_glyph (struct it
*it
)
1657 struct glyph
*glyph
;
1659 eassert (it
->glyph_row
);
1660 glyph
= it
->glyph_row
->glyphs
[it
->area
] + it
->glyph_row
->used
[it
->area
];
1661 if (glyph
< it
->glyph_row
->glyphs
[1 + it
->area
])
1663 /* If the glyph row is reversed, we need to prepend the glyph
1664 rather than append it. */
1665 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1669 /* Make room for the new glyph. */
1670 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1672 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1674 glyph
->type
= COMPOSITE_GLYPH
;
1675 glyph
->pixel_width
= it
->pixel_width
;
1676 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1677 if (it
->cmp_it
.ch
< 0)
1679 glyph
->u
.cmp
.automatic
= 0;
1680 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1684 glyph
->u
.cmp
.automatic
= 1;
1685 glyph
->u
.cmp
.id
= it
->cmp_it
.id
;
1686 glyph
->slice
.cmp
.from
= it
->cmp_it
.from
;
1687 glyph
->slice
.cmp
.to
= it
->cmp_it
.to
- 1;
1690 glyph
->face_id
= it
->face_id
;
1691 glyph
->padding_p
= 0;
1692 glyph
->charpos
= CHARPOS (it
->position
);
1693 glyph
->object
= it
->object
;
1696 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1697 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1699 glyph
->bidi_type
= it
->bidi_it
.type
;
1703 glyph
->resolved_level
= 0;
1704 glyph
->bidi_type
= UNKNOWN_BT
;
1707 ++it
->glyph_row
->used
[it
->area
];
1713 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1714 the composition. We simply produces components of the composition
1715 assuming that the terminal has a capability to layout/render it
1719 produce_composite_glyph (struct it
*it
)
1721 if (it
->cmp_it
.ch
< 0)
1723 struct composition
*cmp
= composition_table
[it
->cmp_it
.id
];
1725 it
->pixel_width
= cmp
->width
;
1729 Lisp_Object gstring
= composition_gstring_from_id (it
->cmp_it
.id
);
1731 it
->pixel_width
= composition_gstring_width (gstring
, it
->cmp_it
.from
,
1732 it
->cmp_it
.to
, NULL
);
1736 append_composite_glyph (it
);
1740 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1741 is a face ID to be used for the glyph. What is actually appended
1742 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1743 comes from it->nglyphs bytes). */
1746 append_glyphless_glyph (struct it
*it
, int face_id
, const char *str
)
1748 struct glyph
*glyph
, *end
;
1751 eassert (it
->glyph_row
);
1752 glyph
= it
->glyph_row
->glyphs
[it
->area
] + it
->glyph_row
->used
[it
->area
];
1753 end
= it
->glyph_row
->glyphs
[1 + it
->area
];
1755 /* If the glyph row is reversed, we need to prepend the glyph rather
1757 if (it
->glyph_row
->reversed_p
&& it
->area
== TEXT_AREA
)
1760 int move_by
= it
->pixel_width
;
1762 /* Make room for the new glyphs. */
1763 if (move_by
> end
- glyph
) /* don't overstep end of this area */
1764 move_by
= end
- glyph
;
1765 for (g
= glyph
- 1; g
>= it
->glyph_row
->glyphs
[it
->area
]; g
--)
1767 glyph
= it
->glyph_row
->glyphs
[it
->area
];
1768 end
= glyph
+ move_by
;
1773 glyph
->type
= CHAR_GLYPH
;
1774 glyph
->pixel_width
= 1;
1775 glyph
->face_id
= face_id
;
1776 glyph
->padding_p
= 0;
1777 glyph
->charpos
= CHARPOS (it
->position
);
1778 glyph
->object
= it
->object
;
1781 glyph
->resolved_level
= it
->bidi_it
.resolved_level
;
1782 if ((it
->bidi_it
.type
& 7) != it
->bidi_it
.type
)
1784 glyph
->bidi_type
= it
->bidi_it
.type
;
1788 glyph
->resolved_level
= 0;
1789 glyph
->bidi_type
= UNKNOWN_BT
;
1792 /* BIDI Note: we put the glyphs of characters left to right, even in
1793 the REVERSED_P case because we write to the terminal
1795 for (i
= 0; i
< it
->nglyphs
&& glyph
< end
; ++i
)
1798 glyph
[0] = glyph
[-1];
1799 glyph
->u
.ch
= str
[i
];
1800 ++it
->glyph_row
->used
[it
->area
];
1805 /* Produce glyphs for a glyphless character for iterator IT.
1806 IT->glyphless_method specifies which method to use for displaying
1807 the character. See the description of enum
1808 glyphless_display_method in dispextern.h for the details.
1810 FOR_NO_FONT is nonzero if and only if this is for a character that
1811 is not supported by the coding system of the terminal. ACRONYM, if
1812 non-nil, is an acronym string for the character.
1814 The glyphs actually produced are of type CHAR_GLYPH. */
1817 produce_glyphless_glyph (struct it
*it
, int for_no_font
, Lisp_Object acronym
)
1821 char buf
[sizeof "\\x" + max (6, (sizeof it
->c
* CHAR_BIT
+ 3) / 4)];
1822 char const *str
= " ";
1824 /* Get a face ID for the glyph by utilizing a cache (the same way as
1825 done for `escape-glyph' in get_next_display_element). */
1826 if (it
->f
== last_glyphless_glyph_frame
1827 && it
->face_id
== last_glyphless_glyph_face_id
)
1829 face_id
= last_glyphless_glyph_merged_face_id
;
1833 /* Merge the `glyphless-char' face into the current face. */
1834 face_id
= merge_faces (it
->f
, Qglyphless_char
, 0, it
->face_id
);
1835 last_glyphless_glyph_frame
= it
->f
;
1836 last_glyphless_glyph_face_id
= it
->face_id
;
1837 last_glyphless_glyph_merged_face_id
= face_id
;
1840 if (it
->glyphless_method
== GLYPHLESS_DISPLAY_THIN_SPACE
)
1842 /* As there's no way to produce a thin space, we produce a space
1843 of canonical width. */
1846 else if (it
->glyphless_method
== GLYPHLESS_DISPLAY_EMPTY_BOX
)
1848 len
= CHAR_WIDTH (it
->c
);
1853 len
= sprintf (buf
, "[%.*s]", len
, str
);
1858 if (it
->glyphless_method
== GLYPHLESS_DISPLAY_ACRONYM
)
1860 if (! STRINGP (acronym
) && CHAR_TABLE_P (Vglyphless_char_display
))
1861 acronym
= CHAR_TABLE_REF (Vglyphless_char_display
, it
->c
);
1862 if (CONSP (acronym
))
1863 acronym
= XCDR (acronym
);
1865 str
= STRINGP (acronym
) ? SSDATA (acronym
) : "";
1866 for (len
= 0; len
< 6 && str
[len
] && ASCII_BYTE_P (str
[len
]); len
++)
1867 buf
[1 + len
] = str
[len
];
1873 eassert (it
->glyphless_method
== GLYPHLESS_DISPLAY_HEX_CODE
);
1874 len
= (it
->c
< 0x10000 ? sprintf (buf
, "\\u%04X", it
->c
)
1875 : it
->c
<= MAX_UNICODE_CHAR
? sprintf (buf
, "\\U%06X", it
->c
)
1876 : sprintf (buf
, "\\x%06X", it
->c
));
1881 it
->pixel_width
= len
;
1884 append_glyphless_glyph (it
, face_id
, str
);
1888 /***********************************************************************
1890 ***********************************************************************/
1892 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1893 one of the enumerators from enum no_color_bit, or a bit set built
1894 from them. Some display attributes may not be used together with
1895 color; the termcap capability `NC' specifies which ones. */
1897 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1898 (tty->TN_max_colors > 0 \
1899 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1902 /* Turn appearances of face FACE_ID on tty frame F on.
1903 FACE_ID is a realized face ID number, in the face cache. */
1906 turn_on_face (struct frame
*f
, int face_id
)
1908 struct face
*face
= FACE_FROM_ID (f
, face_id
);
1909 long fg
= face
->foreground
;
1910 long bg
= face
->background
;
1911 struct tty_display_info
*tty
= FRAME_TTY (f
);
1913 /* Do this first because TS_end_standout_mode may be the same
1914 as TS_exit_attribute_mode, which turns all appearances off. */
1915 if (MAY_USE_WITH_COLORS_P (tty
, NC_REVERSE
))
1917 if (tty
->TN_max_colors
> 0)
1919 if (fg
>= 0 && bg
>= 0)
1921 /* If the terminal supports colors, we can set them
1922 below without using reverse video. The face's fg
1923 and bg colors are set as they should appear on
1924 the screen, i.e. they take the inverse-video'ness
1925 of the face already into account. */
1927 else if (inverse_video
)
1929 if (fg
== FACE_TTY_DEFAULT_FG_COLOR
1930 || bg
== FACE_TTY_DEFAULT_BG_COLOR
)
1931 tty_toggle_highlight (tty
);
1935 if (fg
== FACE_TTY_DEFAULT_BG_COLOR
1936 || bg
== FACE_TTY_DEFAULT_FG_COLOR
)
1937 tty_toggle_highlight (tty
);
1942 /* If we can't display colors, use reverse video
1943 if the face specifies that. */
1946 if (fg
== FACE_TTY_DEFAULT_FG_COLOR
1947 || bg
== FACE_TTY_DEFAULT_BG_COLOR
)
1948 tty_toggle_highlight (tty
);
1952 if (fg
== FACE_TTY_DEFAULT_BG_COLOR
1953 || bg
== FACE_TTY_DEFAULT_FG_COLOR
)
1954 tty_toggle_highlight (tty
);
1959 if (face
->tty_bold_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_BOLD
))
1960 OUTPUT1_IF (tty
, tty
->TS_enter_bold_mode
);
1962 if (face
->tty_italic_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_ITALIC
))
1964 if (tty
->TS_enter_italic_mode
)
1965 OUTPUT1 (tty
, tty
->TS_enter_italic_mode
);
1967 /* Italics mode is unavailable on many terminals. In that
1968 case, map slant to dimmed text; we want italic text to
1969 appear different and dimming is not otherwise used. */
1970 OUTPUT1 (tty
, tty
->TS_enter_dim_mode
);
1973 if (face
->tty_underline_p
&& MAY_USE_WITH_COLORS_P (tty
, NC_UNDERLINE
))
1974 OUTPUT1_IF (tty
, tty
->TS_enter_underline_mode
);
1976 if (tty
->TN_max_colors
> 0)
1981 ts
= tty
->standout_mode
? tty
->TS_set_background
: tty
->TS_set_foreground
;
1984 p
= tparam (ts
, NULL
, 0, (int) fg
, 0, 0, 0);
1989 ts
= tty
->standout_mode
? tty
->TS_set_foreground
: tty
->TS_set_background
;
1992 p
= tparam (ts
, NULL
, 0, (int) bg
, 0, 0, 0);
2000 /* Turn off appearances of face FACE_ID on tty frame F. */
2003 turn_off_face (struct frame
*f
, int face_id
)
2005 struct face
*face
= FACE_FROM_ID (f
, face_id
);
2006 struct tty_display_info
*tty
= FRAME_TTY (f
);
2008 eassert (face
!= NULL
);
2010 if (tty
->TS_exit_attribute_mode
)
2012 /* Capability "me" will turn off appearance modes double-bright,
2013 half-bright, reverse-video, standout, underline. It may or
2014 may not turn off alt-char-mode. */
2015 if (face
->tty_bold_p
2016 || face
->tty_italic_p
2017 || face
->tty_reverse_p
2018 || face
->tty_underline_p
)
2020 OUTPUT1_IF (tty
, tty
->TS_exit_attribute_mode
);
2021 if (strcmp (tty
->TS_exit_attribute_mode
, tty
->TS_end_standout_mode
) == 0)
2022 tty
->standout_mode
= 0;
2027 /* If we don't have "me" we can only have those appearances
2028 that have exit sequences defined. */
2029 if (face
->tty_underline_p
)
2030 OUTPUT_IF (tty
, tty
->TS_exit_underline_mode
);
2033 /* Switch back to default colors. */
2034 if (tty
->TN_max_colors
> 0
2035 && ((face
->foreground
!= FACE_TTY_DEFAULT_COLOR
2036 && face
->foreground
!= FACE_TTY_DEFAULT_FG_COLOR
)
2037 || (face
->background
!= FACE_TTY_DEFAULT_COLOR
2038 && face
->background
!= FACE_TTY_DEFAULT_BG_COLOR
)))
2039 OUTPUT1_IF (tty
, tty
->TS_orig_pair
);
2043 /* Return non-zero if the terminal on frame F supports all of the
2044 capabilities in CAPS simultaneously, with foreground and background
2045 colors FG and BG. */
2048 tty_capable_p (struct tty_display_info
*tty
, unsigned int caps
,
2049 unsigned long fg
, unsigned long bg
)
2051 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
2052 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
2055 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_INVERSE
, tty
->TS_standout_mode
, NC_REVERSE
);
2056 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_UNDERLINE
, tty
->TS_enter_underline_mode
, NC_UNDERLINE
);
2057 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_BOLD
, tty
->TS_enter_bold_mode
, NC_BOLD
);
2058 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_DIM
, tty
->TS_enter_dim_mode
, NC_DIM
);
2059 TTY_CAPABLE_P_TRY (tty
, TTY_CAP_ITALIC
, tty
->TS_enter_italic_mode
, NC_ITALIC
);
2065 /* Return non-zero if the terminal is capable to display colors. */
2067 DEFUN ("tty-display-color-p", Ftty_display_color_p
, Stty_display_color_p
,
2069 doc
: /* Return non-nil if the tty device TERMINAL can display colors.
2071 TERMINAL can be a terminal object, a frame, or nil (meaning the
2072 selected frame's terminal). This function always returns nil if
2073 TERMINAL does not refer to a text terminal. */)
2074 (Lisp_Object terminal
)
2076 struct terminal
*t
= get_tty_terminal (terminal
, 0);
2080 return t
->display_info
.tty
->TN_max_colors
> 0 ? Qt
: Qnil
;
2083 /* Return the number of supported colors. */
2084 DEFUN ("tty-display-color-cells", Ftty_display_color_cells
,
2085 Stty_display_color_cells
, 0, 1, 0,
2086 doc
: /* Return the number of colors supported by the tty device TERMINAL.
2088 TERMINAL can be a terminal object, a frame, or nil (meaning the
2089 selected frame's terminal). This function always returns 0 if
2090 TERMINAL does not refer to a text terminal. */)
2091 (Lisp_Object terminal
)
2093 struct terminal
*t
= get_tty_terminal (terminal
, 0);
2095 return make_number (0);
2097 return make_number (t
->display_info
.tty
->TN_max_colors
);
2102 /* Declare here rather than in the function, as in the rest of Emacs,
2103 to work around an HPUX compiler bug (?). See
2104 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2105 static int default_max_colors
;
2106 static int default_max_pairs
;
2107 static int default_no_color_video
;
2108 static char *default_orig_pair
;
2109 static char *default_set_foreground
;
2110 static char *default_set_background
;
2112 /* Save or restore the default color-related capabilities of this
2115 tty_default_color_capabilities (struct tty_display_info
*tty
, int save
)
2120 xfree (default_orig_pair
);
2121 default_orig_pair
= tty
->TS_orig_pair
? xstrdup (tty
->TS_orig_pair
) : NULL
;
2123 xfree (default_set_foreground
);
2124 default_set_foreground
= tty
->TS_set_foreground
? xstrdup (tty
->TS_set_foreground
)
2127 xfree (default_set_background
);
2128 default_set_background
= tty
->TS_set_background
? xstrdup (tty
->TS_set_background
)
2131 default_max_colors
= tty
->TN_max_colors
;
2132 default_max_pairs
= tty
->TN_max_pairs
;
2133 default_no_color_video
= tty
->TN_no_color_video
;
2137 tty
->TS_orig_pair
= default_orig_pair
;
2138 tty
->TS_set_foreground
= default_set_foreground
;
2139 tty
->TS_set_background
= default_set_background
;
2140 tty
->TN_max_colors
= default_max_colors
;
2141 tty
->TN_max_pairs
= default_max_pairs
;
2142 tty
->TN_no_color_video
= default_no_color_video
;
2146 /* Setup one of the standard tty color schemes according to MODE.
2147 MODE's value is generally the number of colors which we want to
2148 support; zero means set up for the default capabilities, the ones
2149 we saw at init_tty time; -1 means turn off color support. */
2151 tty_setup_colors (struct tty_display_info
*tty
, int mode
)
2153 /* Canonicalize all negative values of MODE. */
2159 case -1: /* no colors at all */
2160 tty
->TN_max_colors
= 0;
2161 tty
->TN_max_pairs
= 0;
2162 tty
->TN_no_color_video
= 0;
2163 tty
->TS_set_foreground
= tty
->TS_set_background
= tty
->TS_orig_pair
= NULL
;
2165 case 0: /* default colors, if any */
2167 tty_default_color_capabilities (tty
, 0);
2169 case 8: /* 8 standard ANSI colors */
2170 tty
->TS_orig_pair
= "\033[0m";
2172 tty
->TS_set_foreground
= "\033[3%p1%dm";
2173 tty
->TS_set_background
= "\033[4%p1%dm";
2175 tty
->TS_set_foreground
= "\033[3%dm";
2176 tty
->TS_set_background
= "\033[4%dm";
2178 tty
->TN_max_colors
= 8;
2179 tty
->TN_max_pairs
= 64;
2180 tty
->TN_no_color_video
= 0;
2186 set_tty_color_mode (struct tty_display_info
*tty
, struct frame
*f
)
2188 Lisp_Object tem
, val
;
2189 Lisp_Object color_mode
;
2191 Lisp_Object tty_color_mode_alist
2192 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil
);
2194 tem
= assq_no_quit (Qtty_color_mode
, f
->param_alist
);
2195 val
= CONSP (tem
) ? XCDR (tem
) : Qnil
;
2199 else if (SYMBOLP (tty_color_mode_alist
))
2201 tem
= Fassq (val
, Fsymbol_value (tty_color_mode_alist
));
2202 color_mode
= CONSP (tem
) ? XCDR (tem
) : Qnil
;
2207 mode
= TYPE_RANGED_INTEGERP (int, color_mode
) ? XINT (color_mode
) : 0;
2209 if (mode
!= tty
->previous_color_mode
)
2211 tty
->previous_color_mode
= mode
;
2212 tty_setup_colors (tty
, mode
);
2213 /* This recomputes all the faces given the new color definitions. */
2214 safe_call (1, intern ("tty-set-up-initial-frame-faces"));
2218 #endif /* !DOS_NT */
2222 /* Return the tty display object specified by TERMINAL. */
2224 static struct terminal
*
2225 get_tty_terminal (Lisp_Object terminal
, int throw)
2227 struct terminal
*t
= get_terminal (terminal
, throw);
2229 if (t
&& t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2232 error ("Device %d is not a termcap terminal device", t
->id
);
2240 /* Return an active termcap device that uses the tty device with the
2243 This function ignores suspended devices.
2245 Returns NULL if the named terminal device is not opened. */
2248 get_named_tty (const char *name
)
2255 for (t
= terminal_list
; t
; t
= t
->next_terminal
)
2257 if ((t
->type
== output_termcap
|| t
->type
== output_msdos_raw
)
2258 && !strcmp (t
->display_info
.tty
->name
, name
)
2259 && TERMINAL_ACTIVE_P (t
))
2267 DEFUN ("tty-type", Ftty_type
, Stty_type
, 0, 1, 0,
2268 doc
: /* Return the type of the tty device that TERMINAL uses.
2269 Returns nil if TERMINAL is not on a tty device.
2271 TERMINAL can be a terminal object, a frame, or nil (meaning the
2272 selected frame's terminal). */)
2273 (Lisp_Object terminal
)
2275 struct terminal
*t
= get_terminal (terminal
, 1);
2277 if (t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2280 if (t
->display_info
.tty
->type
)
2281 return build_string (t
->display_info
.tty
->type
);
2286 DEFUN ("controlling-tty-p", Fcontrolling_tty_p
, Scontrolling_tty_p
, 0, 1, 0,
2287 doc
: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2289 TERMINAL can be a terminal object, a frame, or nil (meaning the
2290 selected frame's terminal). This function always returns nil if
2291 TERMINAL is not on a tty device. */)
2292 (Lisp_Object terminal
)
2294 struct terminal
*t
= get_terminal (terminal
, 1);
2296 if ((t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
2297 || strcmp (t
->display_info
.tty
->name
, DEV_TTY
) != 0)
2303 DEFUN ("tty-no-underline", Ftty_no_underline
, Stty_no_underline
, 0, 1, 0,
2304 doc
: /* Declare that the tty used by TERMINAL does not handle underlining.
2305 This is used to override the terminfo data, for certain terminals that
2306 do not really do underlining, but say that they do. This function has
2307 no effect if used on a non-tty terminal.
2309 TERMINAL can be a terminal object, a frame or nil (meaning the
2310 selected frame's terminal). This function always returns nil if
2311 TERMINAL does not refer to a text terminal. */)
2312 (Lisp_Object terminal
)
2314 struct terminal
*t
= get_terminal (terminal
, 1);
2316 if (t
->type
== output_termcap
)
2317 t
->display_info
.tty
->TS_enter_underline_mode
= 0;
2321 DEFUN ("tty-top-frame", Ftty_top_frame
, Stty_top_frame
, 0, 1, 0,
2322 doc
: /* Return the topmost terminal frame on TERMINAL.
2323 TERMINAL can be a terminal object, a frame or nil (meaning the
2324 selected frame's terminal). This function returns nil if TERMINAL
2325 does not refer to a text terminal. Otherwise, it returns the
2326 top-most frame on the text terminal. */)
2327 (Lisp_Object terminal
)
2329 struct terminal
*t
= get_terminal (terminal
, 1);
2331 if (t
->type
== output_termcap
)
2332 return t
->display_info
.tty
->top_frame
;
2338 DEFUN ("suspend-tty", Fsuspend_tty
, Ssuspend_tty
, 0, 1, 0,
2339 doc
: /* Suspend the terminal device TTY.
2341 The device is restored to its default state, and Emacs ceases all
2342 access to the tty device. Frames that use the device are not deleted,
2343 but input is not read from them and if they change, their display is
2346 TTY may be a terminal object, a frame, or nil for the terminal device
2347 of the currently selected frame.
2349 This function runs `suspend-tty-functions' after suspending the
2350 device. The functions are run with one arg, the id of the suspended
2353 `suspend-tty' does nothing if it is called on a device that is already
2356 A suspended tty may be resumed by calling `resume-tty' on it. */)
2359 struct terminal
*t
= get_tty_terminal (tty
, 1);
2363 error ("Unknown tty device");
2365 f
= t
->display_info
.tty
->input
;
2369 /* First run `suspend-tty-functions' and then clean up the tty
2370 state because `suspend-tty-functions' might need to change
2372 Lisp_Object args
[2];
2373 args
[0] = intern ("suspend-tty-functions");
2374 XSETTERMINAL (args
[1], t
);
2375 Frun_hook_with_args (2, args
);
2377 reset_sys_modes (t
->display_info
.tty
);
2378 delete_keyboard_wait_descriptor (fileno (f
));
2382 if (f
!= t
->display_info
.tty
->output
)
2383 fclose (t
->display_info
.tty
->output
);
2386 t
->display_info
.tty
->input
= 0;
2387 t
->display_info
.tty
->output
= 0;
2389 if (FRAMEP (t
->display_info
.tty
->top_frame
))
2390 FRAME_SET_VISIBLE (XFRAME (t
->display_info
.tty
->top_frame
), 0);
2394 /* Clear display hooks to prevent further output. */
2395 clear_tty_hooks (t
);
2400 DEFUN ("resume-tty", Fresume_tty
, Sresume_tty
, 0, 1, 0,
2401 doc
: /* Resume the previously suspended terminal device TTY.
2402 The terminal is opened and reinitialized. Frames that are on the
2403 suspended terminal are revived.
2405 It is an error to resume a terminal while another terminal is active
2408 This function runs `resume-tty-functions' after resuming the terminal.
2409 The functions are run with one arg, the id of the resumed terminal
2412 `resume-tty' does nothing if it is called on a device that is not
2415 TTY may be a terminal object, a frame, or nil (meaning the selected
2416 frame's terminal). */)
2419 struct terminal
*t
= get_tty_terminal (tty
, 1);
2423 error ("Unknown tty device");
2425 if (!t
->display_info
.tty
->input
)
2427 if (get_named_tty (t
->display_info
.tty
->name
))
2428 error ("Cannot resume display while another display is active on the same device");
2431 t
->display_info
.tty
->output
= stdout
;
2432 t
->display_info
.tty
->input
= stdin
;
2434 fd
= emacs_open (t
->display_info
.tty
->name
, O_RDWR
| O_NOCTTY
, 0);
2437 error ("Can not reopen tty device %s: %s", t
->display_info
.tty
->name
, strerror (errno
));
2439 if (strcmp (t
->display_info
.tty
->name
, DEV_TTY
))
2440 dissociate_if_controlling_tty (fd
);
2442 t
->display_info
.tty
->output
= fdopen (fd
, "w+");
2443 t
->display_info
.tty
->input
= t
->display_info
.tty
->output
;
2446 add_keyboard_wait_descriptor (fd
);
2448 if (FRAMEP (t
->display_info
.tty
->top_frame
))
2450 struct frame
*f
= XFRAME (t
->display_info
.tty
->top_frame
);
2452 int old_height
= FRAME_COLS (f
);
2453 int old_width
= FRAME_LINES (f
);
2455 /* Check if terminal/window size has changed while the frame
2457 get_tty_size (fileno (t
->display_info
.tty
->input
), &width
, &height
);
2458 if (width
!= old_width
|| height
!= old_height
)
2459 change_frame_size (f
, height
, width
, 0, 0, 0);
2460 FRAME_SET_VISIBLE (XFRAME (t
->display_info
.tty
->top_frame
), 1);
2464 init_sys_modes (t
->display_info
.tty
);
2467 /* Run `resume-tty-functions'. */
2468 Lisp_Object args
[2];
2469 args
[0] = intern ("resume-tty-functions");
2470 XSETTERMINAL (args
[1], t
);
2471 Frun_hook_with_args (2, args
);
2481 /***********************************************************************
2483 ***********************************************************************/
2487 #ifndef HAVE_WINDOW_SYSTEM
2489 term_mouse_moveto (int x
, int y
)
2491 /* TODO: how to set mouse position?
2494 name = (const char *) ttyname (0);
2495 fd = open (name, O_WRONLY);
2496 SOME_FUNCTION (x, y, fd);
2499 last_mouse_y = y; */
2501 #endif /* HAVE_WINDOW_SYSTEM */
2503 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2505 tty_draw_row_with_mouse_face (struct window
*w
, struct glyph_row
*row
,
2506 int start_hpos
, int end_hpos
,
2507 enum draw_glyphs_face draw
)
2509 int nglyphs
= end_hpos
- start_hpos
;
2510 struct frame
*f
= XFRAME (WINDOW_FRAME (w
));
2511 struct tty_display_info
*tty
= FRAME_TTY (f
);
2512 int face_id
= tty
->mouse_highlight
.mouse_face_face_id
;
2513 int save_x
, save_y
, pos_x
, pos_y
;
2515 if (end_hpos
>= row
->used
[TEXT_AREA
])
2516 nglyphs
= row
->used
[TEXT_AREA
] - start_hpos
;
2518 pos_y
= row
->y
+ WINDOW_TOP_EDGE_Y (w
);
2519 pos_x
= row
->used
[LEFT_MARGIN_AREA
] + start_hpos
+ WINDOW_LEFT_EDGE_X (w
);
2521 /* Save current cursor co-ordinates. */
2522 save_y
= curY (tty
);
2523 save_x
= curX (tty
);
2524 cursor_to (f
, pos_y
, pos_x
);
2526 if (draw
== DRAW_MOUSE_FACE
)
2527 tty_write_glyphs_with_face (f
, row
->glyphs
[TEXT_AREA
] + start_hpos
,
2529 else if (draw
== DRAW_NORMAL_TEXT
)
2530 write_glyphs (f
, row
->glyphs
[TEXT_AREA
] + start_hpos
, nglyphs
);
2532 cursor_to (f
, save_y
, save_x
);
2536 term_mouse_movement (FRAME_PTR frame
, Gpm_Event
*event
)
2538 /* Has the mouse moved off the glyph it was on at the last sighting? */
2539 if (event
->x
!= last_mouse_x
|| event
->y
!= last_mouse_y
)
2541 frame
->mouse_moved
= 1;
2542 note_mouse_highlight (frame
, event
->x
, event
->y
);
2543 /* Remember which glyph we're now on. */
2544 last_mouse_x
= event
->x
;
2545 last_mouse_y
= event
->y
;
2551 /* Return the Time that corresponds to T. Wrap around on overflow. */
2553 timeval_to_Time (struct timeval
const *t
)
2559 ms
= t
->tv_usec
/ 1000;
2563 /* Return the current position of the mouse.
2565 Set *f to the frame the mouse is in, or zero if the mouse is in no
2566 Emacs frame. If it is set to zero, all the other arguments are
2569 Set *bar_window to Qnil, and *x and *y to the column and
2570 row of the character cell the mouse is over.
2572 Set *timeptr to the time the mouse was at the returned position.
2574 This clears mouse_moved until the next motion
2577 term_mouse_position (FRAME_PTR
*fp
, int insist
, Lisp_Object
*bar_window
,
2578 enum scroll_bar_part
*part
, Lisp_Object
*x
,
2579 Lisp_Object
*y
, Time
*timeptr
)
2583 *fp
= SELECTED_FRAME ();
2584 (*fp
)->mouse_moved
= 0;
2589 XSETINT (*x
, last_mouse_x
);
2590 XSETINT (*y
, last_mouse_y
);
2591 gettimeofday(&now
, 0);
2592 *timeptr
= timeval_to_Time (&now
);
2595 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2597 If the event is a button press, then note that we have grabbed
2601 term_mouse_click (struct input_event
*result
, Gpm_Event
*event
,
2607 result
->kind
= GPM_CLICK_EVENT
;
2608 for (i
= 0, j
= GPM_B_LEFT
; i
< 3; i
++, j
>>= 1 )
2610 if (event
->buttons
& j
) {
2611 result
->code
= i
; /* button number */
2615 gettimeofday(&now
, 0);
2616 result
->timestamp
= timeval_to_Time (&now
);
2618 if (event
->type
& GPM_UP
)
2619 result
->modifiers
= up_modifier
;
2620 else if (event
->type
& GPM_DOWN
)
2621 result
->modifiers
= down_modifier
;
2623 result
->modifiers
= 0;
2625 if (event
->type
& GPM_SINGLE
)
2626 result
->modifiers
|= click_modifier
;
2628 if (event
->type
& GPM_DOUBLE
)
2629 result
->modifiers
|= double_modifier
;
2631 if (event
->type
& GPM_TRIPLE
)
2632 result
->modifiers
|= triple_modifier
;
2634 if (event
->type
& GPM_DRAG
)
2635 result
->modifiers
|= drag_modifier
;
2637 if (!(event
->type
& (GPM_MOVE
| GPM_DRAG
))) {
2640 if (event
->modifiers
& (1 << 0))
2641 result
->modifiers
|= shift_modifier
;
2644 if (event
->modifiers
& (1 << 2))
2645 result
->modifiers
|= ctrl_modifier
;
2647 /* 1 << KG_ALT || KG_ALTGR */
2648 if (event
->modifiers
& (1 << 3)
2649 || event
->modifiers
& (1 << 1))
2650 result
->modifiers
|= meta_modifier
;
2653 XSETINT (result
->x
, event
->x
);
2654 XSETINT (result
->y
, event
->y
);
2655 XSETFRAME (result
->frame_or_window
, f
);
2661 handle_one_term_event (struct tty_display_info
*tty
, Gpm_Event
*event
, struct input_event
* hold_quit
)
2663 struct frame
*f
= XFRAME (tty
->top_frame
);
2664 struct input_event ie
;
2672 if (event
->type
& (GPM_MOVE
| GPM_DRAG
)) {
2673 previous_help_echo_string
= help_echo_string
;
2674 help_echo_string
= Qnil
;
2676 Gpm_DrawPointer (event
->x
, event
->y
, fileno (tty
->output
));
2678 if (!term_mouse_movement (f
, event
))
2679 help_echo_string
= previous_help_echo_string
;
2681 /* If the contents of the global variable help_echo_string
2682 has changed, generate a HELP_EVENT. */
2683 if (!NILP (help_echo_string
)
2684 || !NILP (previous_help_echo_string
))
2691 term_mouse_click (&ie
, event
, f
);
2695 if (ie
.kind
!= NO_EVENT
)
2697 kbd_buffer_store_event_hold (&ie
, hold_quit
);
2702 && !(hold_quit
&& hold_quit
->kind
!= NO_EVENT
))
2707 XSETFRAME (frame
, f
);
2711 gen_help_event (help_echo_string
, frame
, help_echo_window
,
2712 help_echo_object
, help_echo_pos
);
2719 DEFUN ("gpm-mouse-start", Fgpm_mouse_start
, Sgpm_mouse_start
,
2721 doc
: /* Open a connection to Gpm.
2722 Gpm-mouse can only be activated for one tty at a time. */)
2725 struct frame
*f
= SELECTED_FRAME ();
2726 struct tty_display_info
*tty
2727 = ((f
)->output_method
== output_termcap
2728 ? (f
)->terminal
->display_info
.tty
: NULL
);
2729 Gpm_Connect connection
;
2732 error ("Gpm-mouse only works in the GNU/Linux console");
2734 return Qnil
; /* Already activated, nothing to do. */
2736 error ("Gpm-mouse can only be activated for one tty at a time");
2738 connection
.eventMask
= ~0;
2739 connection
.defaultMask
= ~GPM_HARD
;
2740 connection
.maxMod
= ~0;
2741 connection
.minMod
= 0;
2744 if (Gpm_Open (&connection
, 0) < 0)
2745 error ("Gpm-mouse failed to connect to the gpm daemon");
2749 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2750 to generate SIGIOs. Apparently we need to call reset_sys_modes
2751 before calling init_sys_modes. */
2752 reset_sys_modes (tty
);
2753 init_sys_modes (tty
);
2754 add_gpm_wait_descriptor (gpm_fd
);
2763 delete_gpm_wait_descriptor (fd
);
2764 while (Gpm_Close()); /* close all the stack */
2768 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop
, Sgpm_mouse_stop
,
2770 doc
: /* Close a connection to Gpm. */)
2773 struct frame
*f
= SELECTED_FRAME ();
2774 struct tty_display_info
*tty
2775 = ((f
)->output_method
== output_termcap
2776 ? (f
)->terminal
->display_info
.tty
: NULL
);
2778 if (!tty
|| gpm_tty
!= tty
)
2779 return Qnil
; /* Not activated on this terminal, nothing to do. */
2784 #endif /* HAVE_GPM */
2788 /***********************************************************************
2790 ***********************************************************************/
2792 /* Initialize the tty-dependent part of frame F. The frame must
2793 already have its device initialized. */
2796 create_tty_output (struct frame
*f
)
2798 struct tty_output
*t
= xzalloc (sizeof *t
);
2800 if (! FRAME_TERMCAP_P (f
))
2803 t
->display_info
= FRAME_TERMINAL (f
)->display_info
.tty
;
2805 f
->output_data
.tty
= t
;
2808 /* Delete frame F's face cache, and its tty-dependent part. */
2811 tty_free_frame_resources (struct frame
*f
)
2813 if (! FRAME_TERMCAP_P (f
))
2816 if (FRAME_FACE_CACHE (f
))
2817 free_frame_faces (f
);
2819 xfree (f
->output_data
.tty
);
2824 /* Delete frame F's face cache. */
2827 tty_free_frame_resources (struct frame
*f
)
2829 if (! FRAME_TERMCAP_P (f
) && ! FRAME_MSDOS_P (f
))
2832 if (FRAME_FACE_CACHE (f
))
2833 free_frame_faces (f
);
2837 /* Reset the hooks in TERMINAL. */
2840 clear_tty_hooks (struct terminal
*terminal
)
2843 terminal
->cursor_to_hook
= 0;
2844 terminal
->raw_cursor_to_hook
= 0;
2845 terminal
->clear_to_end_hook
= 0;
2846 terminal
->clear_frame_hook
= 0;
2847 terminal
->clear_end_of_line_hook
= 0;
2848 terminal
->ins_del_lines_hook
= 0;
2849 terminal
->insert_glyphs_hook
= 0;
2850 terminal
->write_glyphs_hook
= 0;
2851 terminal
->delete_glyphs_hook
= 0;
2852 terminal
->ring_bell_hook
= 0;
2853 terminal
->reset_terminal_modes_hook
= 0;
2854 terminal
->set_terminal_modes_hook
= 0;
2855 terminal
->update_begin_hook
= 0;
2856 terminal
->update_end_hook
= 0;
2857 terminal
->set_terminal_window_hook
= 0;
2858 terminal
->mouse_position_hook
= 0;
2859 terminal
->frame_rehighlight_hook
= 0;
2860 terminal
->frame_raise_lower_hook
= 0;
2861 terminal
->fullscreen_hook
= 0;
2862 terminal
->set_vertical_scroll_bar_hook
= 0;
2863 terminal
->condemn_scroll_bars_hook
= 0;
2864 terminal
->redeem_scroll_bar_hook
= 0;
2865 terminal
->judge_scroll_bars_hook
= 0;
2866 terminal
->read_socket_hook
= 0;
2867 terminal
->frame_up_to_date_hook
= 0;
2869 /* Leave these two set, or suspended frames are not deleted
2871 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
2872 terminal
->delete_terminal_hook
= &delete_tty
;
2875 /* Initialize hooks in TERMINAL with the values needed for a tty. */
2878 set_tty_hooks (struct terminal
*terminal
)
2880 terminal
->rif
= 0; /* ttys don't support window-based redisplay. */
2882 terminal
->cursor_to_hook
= &tty_cursor_to
;
2883 terminal
->raw_cursor_to_hook
= &tty_raw_cursor_to
;
2885 terminal
->clear_to_end_hook
= &tty_clear_to_end
;
2886 terminal
->clear_frame_hook
= &tty_clear_frame
;
2887 terminal
->clear_end_of_line_hook
= &tty_clear_end_of_line
;
2889 terminal
->ins_del_lines_hook
= &tty_ins_del_lines
;
2891 terminal
->insert_glyphs_hook
= &tty_insert_glyphs
;
2892 terminal
->write_glyphs_hook
= &tty_write_glyphs
;
2893 terminal
->delete_glyphs_hook
= &tty_delete_glyphs
;
2895 terminal
->ring_bell_hook
= &tty_ring_bell
;
2897 terminal
->reset_terminal_modes_hook
= &tty_reset_terminal_modes
;
2898 terminal
->set_terminal_modes_hook
= &tty_set_terminal_modes
;
2899 terminal
->update_begin_hook
= 0; /* Not needed. */
2900 terminal
->update_end_hook
= &tty_update_end
;
2901 terminal
->set_terminal_window_hook
= &tty_set_terminal_window
;
2903 terminal
->mouse_position_hook
= 0; /* Not needed. */
2904 terminal
->frame_rehighlight_hook
= 0; /* Not needed. */
2905 terminal
->frame_raise_lower_hook
= 0; /* Not needed. */
2907 terminal
->set_vertical_scroll_bar_hook
= 0; /* Not needed. */
2908 terminal
->condemn_scroll_bars_hook
= 0; /* Not needed. */
2909 terminal
->redeem_scroll_bar_hook
= 0; /* Not needed. */
2910 terminal
->judge_scroll_bars_hook
= 0; /* Not needed. */
2912 terminal
->read_socket_hook
= &tty_read_avail_input
; /* keyboard.c */
2913 terminal
->frame_up_to_date_hook
= 0; /* Not needed. */
2915 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
2916 terminal
->delete_terminal_hook
= &delete_tty
;
2919 /* Drop the controlling terminal if fd is the same device. */
2921 dissociate_if_controlling_tty (int fd
)
2924 int pgid
= tcgetpgrp (fd
); /* If tcgetpgrp succeeds, fd is the ctty. */
2929 no_controlling_tty
= 1;
2930 #elif defined (CYGWIN)
2932 no_controlling_tty
= 1;
2934 #ifdef TIOCNOTTY /* Try BSD ioctls. */
2935 sigblock (sigmask (SIGTTOU
));
2936 fd
= emacs_open (DEV_TTY
, O_RDWR
, 0);
2937 if (fd
!= -1 && ioctl (fd
, TIOCNOTTY
, 0) != -1)
2939 no_controlling_tty
= 1;
2943 sigunblock (sigmask (SIGTTOU
));
2945 /* Unknown system. */
2947 #endif /* ! TIOCNOTTY */
2950 #endif /* !DOS_NT */
2953 /* Create a termcap display on the tty device with the given name and
2956 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
2957 Otherwise NAME should be a path to the tty device file,
2960 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
2962 If MUST_SUCCEED is true, then all errors are fatal. */
2965 init_tty (const char *name
, const char *terminal_type
, int must_succeed
)
2968 char **address
= &area
;
2969 int buffer_size
= 4096;
2971 struct tty_display_info
*tty
= NULL
;
2972 struct terminal
*terminal
= NULL
;
2973 int ctty
= 0; /* 1 if asked to open controlling tty. */
2976 maybe_fatal (must_succeed
, 0,
2977 "Unknown terminal type",
2978 "Unknown terminal type");
2982 if (!strcmp (name
, DEV_TTY
))
2985 /* If we already have a terminal on the given device, use that. If
2986 all such terminals are suspended, create a new one instead. */
2987 /* XXX Perhaps this should be made explicit by having init_tty
2988 always create a new terminal and separating terminal and frame
2989 creation on Lisp level. */
2990 terminal
= get_named_tty (name
);
2994 terminal
= create_terminal ();
2997 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
3000 tty
= &the_only_display_info
;
3002 tty
= xzalloc (sizeof *tty
);
3004 tty
->top_frame
= Qnil
;
3005 tty
->next
= tty_list
;
3008 terminal
->type
= output_termcap
;
3009 terminal
->display_info
.tty
= tty
;
3010 tty
->terminal
= terminal
;
3012 tty
->Wcm
= xmalloc (sizeof *tty
->Wcm
);
3015 encode_terminal_src_size
= 0;
3016 encode_terminal_dst_size
= 0;
3020 set_tty_hooks (terminal
);
3026 #ifdef O_IGNORE_CTTY
3028 /* Open the terminal device. Don't recognize it as our
3029 controlling terminal, and don't make it the controlling tty
3030 if we don't have one at the moment. */
3031 fd
= emacs_open (name
, O_RDWR
| O_IGNORE_CTTY
| O_NOCTTY
, 0);
3033 #endif /* O_IGNORE_CTTY */
3034 /* Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3035 defined on Hurd. On other systems, we need to explicitly
3036 dissociate ourselves from the controlling tty when we want to
3037 open a frame on the same terminal. */
3038 fd
= emacs_open (name
, O_RDWR
| O_NOCTTY
, 0);
3040 tty
->name
= xstrdup (name
);
3041 terminal
->name
= xstrdup (name
);
3044 maybe_fatal (must_succeed
, terminal
,
3045 "Could not open file: %s",
3046 "Could not open file: %s",
3051 maybe_fatal (must_succeed
, terminal
,
3052 "Not a tty device: %s",
3053 "Not a tty device: %s",
3057 #ifndef O_IGNORE_CTTY
3059 dissociate_if_controlling_tty (fd
);
3062 file
= fdopen (fd
, "w+");
3067 tty
->type
= xstrdup (terminal_type
);
3069 add_keyboard_wait_descriptor (fileno (tty
->input
));
3073 tty
->termcap_term_buffer
= xmalloc (buffer_size
);
3075 /* On some systems, tgetent tries to access the controlling
3077 sigblock (sigmask (SIGTTOU
));
3078 status
= tgetent (tty
->termcap_term_buffer
, terminal_type
);
3079 sigunblock (sigmask (SIGTTOU
));
3084 maybe_fatal (must_succeed
, terminal
,
3085 "Cannot open terminfo database file",
3086 "Cannot open terminfo database file");
3088 maybe_fatal (must_succeed
, terminal
,
3089 "Cannot open termcap database file",
3090 "Cannot open termcap database file");
3095 maybe_fatal (must_succeed
, terminal
,
3096 "Terminal type %s is not defined",
3097 "Terminal type %s is not defined.\n\
3098 If that is not the actual type of terminal you have,\n\
3099 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3100 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3102 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3104 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3110 if (strlen (tty
->termcap_term_buffer
) >= buffer_size
)
3112 buffer_size
= strlen (tty
->termcap_term_buffer
);
3114 tty
->termcap_strings_buffer
= area
= xmalloc (buffer_size
);
3115 tty
->TS_ins_line
= tgetstr ("al", address
);
3116 tty
->TS_ins_multi_lines
= tgetstr ("AL", address
);
3117 tty
->TS_bell
= tgetstr ("bl", address
);
3118 BackTab (tty
) = tgetstr ("bt", address
);
3119 tty
->TS_clr_to_bottom
= tgetstr ("cd", address
);
3120 tty
->TS_clr_line
= tgetstr ("ce", address
);
3121 tty
->TS_clr_frame
= tgetstr ("cl", address
);
3122 ColPosition (tty
) = NULL
; /* tgetstr ("ch", address); */
3123 AbsPosition (tty
) = tgetstr ("cm", address
);
3124 CR (tty
) = tgetstr ("cr", address
);
3125 tty
->TS_set_scroll_region
= tgetstr ("cs", address
);
3126 tty
->TS_set_scroll_region_1
= tgetstr ("cS", address
);
3127 RowPosition (tty
) = tgetstr ("cv", address
);
3128 tty
->TS_del_char
= tgetstr ("dc", address
);
3129 tty
->TS_del_multi_chars
= tgetstr ("DC", address
);
3130 tty
->TS_del_line
= tgetstr ("dl", address
);
3131 tty
->TS_del_multi_lines
= tgetstr ("DL", address
);
3132 tty
->TS_delete_mode
= tgetstr ("dm", address
);
3133 tty
->TS_end_delete_mode
= tgetstr ("ed", address
);
3134 tty
->TS_end_insert_mode
= tgetstr ("ei", address
);
3135 Home (tty
) = tgetstr ("ho", address
);
3136 tty
->TS_ins_char
= tgetstr ("ic", address
);
3137 tty
->TS_ins_multi_chars
= tgetstr ("IC", address
);
3138 tty
->TS_insert_mode
= tgetstr ("im", address
);
3139 tty
->TS_pad_inserted_char
= tgetstr ("ip", address
);
3140 tty
->TS_end_keypad_mode
= tgetstr ("ke", address
);
3141 tty
->TS_keypad_mode
= tgetstr ("ks", address
);
3142 LastLine (tty
) = tgetstr ("ll", address
);
3143 Right (tty
) = tgetstr ("nd", address
);
3144 Down (tty
) = tgetstr ("do", address
);
3146 Down (tty
) = tgetstr ("nl", address
); /* Obsolete name for "do" */
3147 if (tgetflag ("bs"))
3148 Left (tty
) = "\b"; /* can't possibly be longer! */
3149 else /* (Actually, "bs" is obsolete...) */
3150 Left (tty
) = tgetstr ("le", address
);
3152 Left (tty
) = tgetstr ("bc", address
); /* Obsolete name for "le" */
3153 tty
->TS_pad_char
= tgetstr ("pc", address
);
3154 tty
->TS_repeat
= tgetstr ("rp", address
);
3155 tty
->TS_end_standout_mode
= tgetstr ("se", address
);
3156 tty
->TS_fwd_scroll
= tgetstr ("sf", address
);
3157 tty
->TS_standout_mode
= tgetstr ("so", address
);
3158 tty
->TS_rev_scroll
= tgetstr ("sr", address
);
3159 tty
->Wcm
->cm_tab
= tgetstr ("ta", address
);
3160 tty
->TS_end_termcap_modes
= tgetstr ("te", address
);
3161 tty
->TS_termcap_modes
= tgetstr ("ti", address
);
3162 Up (tty
) = tgetstr ("up", address
);
3163 tty
->TS_visible_bell
= tgetstr ("vb", address
);
3164 tty
->TS_cursor_normal
= tgetstr ("ve", address
);
3165 tty
->TS_cursor_visible
= tgetstr ("vs", address
);
3166 tty
->TS_cursor_invisible
= tgetstr ("vi", address
);
3167 tty
->TS_set_window
= tgetstr ("wi", address
);
3169 tty
->TS_enter_underline_mode
= tgetstr ("us", address
);
3170 tty
->TS_exit_underline_mode
= tgetstr ("ue", address
);
3171 tty
->TS_enter_bold_mode
= tgetstr ("md", address
);
3172 tty
->TS_enter_italic_mode
= tgetstr ("ZH", address
);
3173 tty
->TS_enter_dim_mode
= tgetstr ("mh", address
);
3174 tty
->TS_enter_reverse_mode
= tgetstr ("mr", address
);
3175 tty
->TS_enter_alt_charset_mode
= tgetstr ("as", address
);
3176 tty
->TS_exit_alt_charset_mode
= tgetstr ("ae", address
);
3177 tty
->TS_exit_attribute_mode
= tgetstr ("me", address
);
3179 MultiUp (tty
) = tgetstr ("UP", address
);
3180 MultiDown (tty
) = tgetstr ("DO", address
);
3181 MultiLeft (tty
) = tgetstr ("LE", address
);
3182 MultiRight (tty
) = tgetstr ("RI", address
);
3184 /* SVr4/ANSI color support. If "op" isn't available, don't support
3185 color because we can't switch back to the default foreground and
3187 tty
->TS_orig_pair
= tgetstr ("op", address
);
3188 if (tty
->TS_orig_pair
)
3190 tty
->TS_set_foreground
= tgetstr ("AF", address
);
3191 tty
->TS_set_background
= tgetstr ("AB", address
);
3192 if (!tty
->TS_set_foreground
)
3195 tty
->TS_set_foreground
= tgetstr ("Sf", address
);
3196 tty
->TS_set_background
= tgetstr ("Sb", address
);
3199 tty
->TN_max_colors
= tgetnum ("Co");
3200 tty
->TN_max_pairs
= tgetnum ("pa");
3202 tty
->TN_no_color_video
= tgetnum ("NC");
3203 if (tty
->TN_no_color_video
== -1)
3204 tty
->TN_no_color_video
= 0;
3207 tty_default_color_capabilities (tty
, 1);
3209 MagicWrap (tty
) = tgetflag ("xn");
3210 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
3211 the former flag imply the latter. */
3212 AutoWrap (tty
) = MagicWrap (tty
) || tgetflag ("am");
3213 terminal
->memory_below_frame
= tgetflag ("db");
3214 tty
->TF_hazeltine
= tgetflag ("hz");
3215 terminal
->must_write_spaces
= tgetflag ("in");
3216 tty
->meta_key
= tgetflag ("km") || tgetflag ("MT");
3217 tty
->TF_insmode_motion
= tgetflag ("mi");
3218 tty
->TF_standout_motion
= tgetflag ("ms");
3219 tty
->TF_underscore
= tgetflag ("ul");
3220 tty
->TF_teleray
= tgetflag ("xt");
3225 struct frame
*f
= XFRAME (selected_frame
);
3227 initialize_w32_display (terminal
);
3229 FrameRows (tty
) = FRAME_LINES (f
);
3230 FrameCols (tty
) = FRAME_COLS (f
);
3231 tty
->specified_window
= FRAME_LINES (f
);
3233 FRAME_CAN_HAVE_SCROLL_BARS (f
) = 0;
3234 FRAME_VERTICAL_SCROLL_BAR_TYPE (f
) = vertical_scroll_bar_none
;
3235 terminal
->char_ins_del_ok
= 1;
3241 if (strcmp (terminal_type
, "internal") == 0)
3242 terminal
->type
= output_msdos_raw
;
3243 initialize_msdos_display (terminal
);
3245 get_tty_size (fileno (tty
->input
), &width
, &height
);
3246 FrameCols (tty
) = width
;
3247 FrameRows (tty
) = height
;
3248 terminal
->char_ins_del_ok
= 0;
3249 init_baud_rate (fileno (tty
->input
));
3252 tty
->output
= stdout
;
3254 /* The following two are inaccessible from w32console.c. */
3255 terminal
->delete_frame_hook
= &tty_free_frame_resources
;
3256 terminal
->delete_terminal_hook
= &delete_tty
;
3258 tty
->name
= xstrdup (name
);
3259 terminal
->name
= xstrdup (name
);
3260 tty
->type
= xstrdup (terminal_type
);
3262 add_keyboard_wait_descriptor (0);
3264 tty
->delete_in_insert_mode
= 1;
3267 terminal
->scroll_region_ok
= 0;
3269 /* Seems to insert lines when it's not supposed to, messing up the
3270 display. In doing a trace, it didn't seem to be called much, so I
3271 don't think we're losing anything by turning it off. */
3272 terminal
->line_ins_del_ok
= 0;
3274 tty
->TN_max_colors
= 16; /* Required to be non-zero for tty-display-color-p */
3278 terminal
->mouse_position_hook
= term_mouse_position
;
3279 tty
->mouse_highlight
.mouse_face_window
= Qnil
;
3282 terminal
->kboard
= xmalloc (sizeof *terminal
->kboard
);
3283 init_kboard (terminal
->kboard
);
3284 kset_window_system (terminal
->kboard
, Qnil
);
3285 terminal
->kboard
->next_kboard
= all_kboards
;
3286 all_kboards
= terminal
->kboard
;
3287 terminal
->kboard
->reference_count
++;
3288 /* Don't let the initial kboard remain current longer than necessary.
3289 That would cause problems if a file loaded on startup tries to
3290 prompt in the mini-buffer. */
3291 if (current_kboard
== initial_kboard
)
3292 current_kboard
= terminal
->kboard
;
3294 term_get_fkeys (address
, terminal
->kboard
);
3296 /* Get frame size from system, or else from termcap. */
3299 get_tty_size (fileno (tty
->input
), &width
, &height
);
3300 FrameCols (tty
) = width
;
3301 FrameRows (tty
) = height
;
3304 if (FrameCols (tty
) <= 0)
3305 FrameCols (tty
) = tgetnum ("co");
3306 if (FrameRows (tty
) <= 0)
3307 FrameRows (tty
) = tgetnum ("li");
3309 if (FrameRows (tty
) < 3 || FrameCols (tty
) < 3)
3310 maybe_fatal (must_succeed
, terminal
,
3311 "Screen size %dx%d is too small",
3312 "Screen size %dx%d is too small",
3313 FrameCols (tty
), FrameRows (tty
));
3315 TabWidth (tty
) = tgetnum ("tw");
3318 tty
->TS_bell
= "\07";
3320 if (!tty
->TS_fwd_scroll
)
3321 tty
->TS_fwd_scroll
= Down (tty
);
3323 PC
= tty
->TS_pad_char
? *tty
->TS_pad_char
: 0;
3325 if (TabWidth (tty
) < 0)
3328 /* Turned off since /etc/termcap seems to have :ta= for most terminals
3329 and newer termcap doc does not seem to say there is a default.
3330 if (!tty->Wcm->cm_tab)
3331 tty->Wcm->cm_tab = "\t";
3334 /* We don't support standout modes that use `magic cookies', so
3335 turn off any that do. */
3336 if (tty
->TS_standout_mode
&& tgetnum ("sg") >= 0)
3338 tty
->TS_standout_mode
= 0;
3339 tty
->TS_end_standout_mode
= 0;
3341 if (tty
->TS_enter_underline_mode
&& tgetnum ("ug") >= 0)
3343 tty
->TS_enter_underline_mode
= 0;
3344 tty
->TS_exit_underline_mode
= 0;
3347 /* If there's no standout mode, try to use underlining instead. */
3348 if (tty
->TS_standout_mode
== 0)
3350 tty
->TS_standout_mode
= tty
->TS_enter_underline_mode
;
3351 tty
->TS_end_standout_mode
= tty
->TS_exit_underline_mode
;
3354 /* If no `se' string, try using a `me' string instead.
3355 If that fails, we can't use standout mode at all. */
3356 if (tty
->TS_end_standout_mode
== 0)
3358 char *s
= tgetstr ("me", address
);
3360 tty
->TS_end_standout_mode
= s
;
3362 tty
->TS_standout_mode
= 0;
3365 if (tty
->TF_teleray
)
3367 tty
->Wcm
->cm_tab
= 0;
3368 /* We can't support standout mode, because it uses magic cookies. */
3369 tty
->TS_standout_mode
= 0;
3370 /* But that means we cannot rely on ^M to go to column zero! */
3372 /* LF can't be trusted either -- can alter hpos */
3373 /* if move at column 0 thru a line with TS_standout_mode */
3377 tty
->specified_window
= FrameRows (tty
);
3379 if (Wcm_init (tty
) == -1) /* can't do cursor motion */
3381 maybe_fatal (must_succeed
, terminal
,
3382 "Terminal type \"%s\" is not powerful enough to run Emacs",
3383 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
3384 It lacks the ability to position the cursor.\n\
3385 If that is not the actual type of terminal you have,\n\
3386 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
3387 `setenv TERM ...') to specify the correct type. It may be necessary\n"
3389 "to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
3390 # else /* TERMCAP */
3391 "to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
3392 # endif /* TERMINFO */
3396 if (FrameRows (tty
) <= 0 || FrameCols (tty
) <= 0)
3397 maybe_fatal (must_succeed
, terminal
,
3398 "Could not determine the frame size",
3399 "Could not determine the frame size");
3401 tty
->delete_in_insert_mode
3402 = tty
->TS_delete_mode
&& tty
->TS_insert_mode
3403 && !strcmp (tty
->TS_delete_mode
, tty
->TS_insert_mode
);
3405 tty
->se_is_so
= (tty
->TS_standout_mode
3406 && tty
->TS_end_standout_mode
3407 && !strcmp (tty
->TS_standout_mode
, tty
->TS_end_standout_mode
));
3409 UseTabs (tty
) = tabs_safe_p (fileno (tty
->input
)) && TabWidth (tty
) == 8;
3411 terminal
->scroll_region_ok
3413 && (tty
->TS_set_window
|| tty
->TS_set_scroll_region
|| tty
->TS_set_scroll_region_1
));
3415 terminal
->line_ins_del_ok
3416 = (((tty
->TS_ins_line
|| tty
->TS_ins_multi_lines
)
3417 && (tty
->TS_del_line
|| tty
->TS_del_multi_lines
))
3418 || (terminal
->scroll_region_ok
3419 && tty
->TS_fwd_scroll
&& tty
->TS_rev_scroll
));
3421 terminal
->char_ins_del_ok
3422 = ((tty
->TS_ins_char
|| tty
->TS_insert_mode
3423 || tty
->TS_pad_inserted_char
|| tty
->TS_ins_multi_chars
)
3424 && (tty
->TS_del_char
|| tty
->TS_del_multi_chars
));
3426 terminal
->fast_clear_end_of_line
= tty
->TS_clr_line
!= 0;
3428 init_baud_rate (fileno (tty
->input
));
3430 #endif /* not DOS_NT */
3432 /* Init system terminal modes (RAW or CBREAK, etc.). */
3433 init_sys_modes (tty
);
3440 vfatal (const char *str
, va_list ap
)
3442 fprintf (stderr
, "emacs: ");
3443 vfprintf (stderr
, str
, ap
);
3444 if (!(strlen (str
) > 0 && str
[strlen (str
) - 1] == '\n'))
3445 fprintf (stderr
, "\n");
3451 /* Auxiliary error-handling function for init_tty.
3452 Delete TERMINAL, then call error or fatal with str1 or str2,
3453 respectively, according to whether MUST_SUCCEED is zero or not. */
3456 maybe_fatal (int must_succeed
, struct terminal
*terminal
,
3457 const char *str1
, const char *str2
, ...)
3460 va_start (ap
, str2
);
3462 delete_tty (terminal
);
3474 fatal (const char *str
, ...)
3484 /* Delete the given tty terminal, closing all frames on it. */
3487 delete_tty (struct terminal
*terminal
)
3489 struct tty_display_info
*tty
;
3491 /* Protect against recursive calls. delete_frame in
3492 delete_terminal calls us back when it deletes our last frame. */
3493 if (!terminal
->name
)
3496 if (terminal
->type
!= output_termcap
)
3499 tty
= terminal
->display_info
.tty
;
3501 if (tty
== tty_list
)
3502 tty_list
= tty
->next
;
3505 struct tty_display_info
*p
;
3506 for (p
= tty_list
; p
&& p
->next
!= tty
; p
= p
->next
)
3510 /* This should not happen. */
3513 p
->next
= tty
->next
;
3517 /* reset_sys_modes needs a valid device, so this call needs to be
3518 before delete_terminal. */
3519 reset_sys_modes (tty
);
3521 delete_terminal (terminal
);
3528 delete_keyboard_wait_descriptor (fileno (tty
->input
));
3529 if (tty
->input
!= stdin
)
3530 fclose (tty
->input
);
3532 if (tty
->output
&& tty
->output
!= stdout
&& tty
->output
!= tty
->input
)
3533 fclose (tty
->output
);
3534 if (tty
->termscript
)
3535 fclose (tty
->termscript
);
3537 xfree (tty
->old_tty
);
3539 xfree (tty
->termcap_strings_buffer
);
3540 xfree (tty
->termcap_term_buffer
);
3548 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo
,
3549 doc
: /* Non-nil means the system uses terminfo rather than termcap.
3550 This variable can be used by terminal emulator packages. */);
3552 system_uses_terminfo
= 1;
3554 system_uses_terminfo
= 0;
3557 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions
,
3558 doc
: /* Functions run after suspending a tty.
3559 The functions are run with one argument, the terminal object to be suspended.
3560 See `suspend-tty'. */);
3561 Vsuspend_tty_functions
= Qnil
;
3564 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions
,
3565 doc
: /* Functions run after resuming a tty.
3566 The functions are run with one argument, the terminal object that was revived.
3567 See `resume-tty'. */);
3568 Vresume_tty_functions
= Qnil
;
3570 DEFVAR_BOOL ("visible-cursor", visible_cursor
,
3571 doc
: /* Non-nil means to make the cursor very visible.
3572 This only has an effect when running in a text terminal.
3573 What means \"very visible\" is up to your terminal. It may make the cursor
3574 bigger, or it may make it blink, or it may do nothing at all. */);
3577 defsubr (&Stty_display_color_p
);
3578 defsubr (&Stty_display_color_cells
);
3579 defsubr (&Stty_no_underline
);
3580 defsubr (&Stty_type
);
3581 defsubr (&Scontrolling_tty_p
);
3582 defsubr (&Stty_top_frame
);
3583 defsubr (&Ssuspend_tty
);
3584 defsubr (&Sresume_tty
);
3586 defsubr (&Sgpm_mouse_start
);
3587 defsubr (&Sgpm_mouse_stop
);
3588 #endif /* HAVE_GPM */
3591 default_orig_pair
= NULL
;
3592 default_set_foreground
= NULL
;
3593 default_set_background
= NULL
;
3594 #endif /* !DOS_NT */
3596 encode_terminal_src
= NULL
;
3597 encode_terminal_dst
= NULL
;