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