]> code.delx.au - gnu-emacs/blob - src/term.c
07cc3a955f3621435a1f690072e6c8bdc081e77f
[gnu-emacs] / src / term.c
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985-1987, 1993-1995, 1998, 2000-2016 Free Software
3 Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
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
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
21
22 #include <config.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <sys/file.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29
30 #include "lisp.h"
31 #include "termchar.h"
32 #include "tparam.h"
33 #include "character.h"
34 #include "buffer.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include "composite.h"
38 #include "keyboard.h"
39 #include "frame.h"
40 #include "disptab.h"
41 #include "termhooks.h"
42 #include "dispextern.h"
43 #include "window.h"
44 #include "keymap.h"
45 #include "blockinput.h"
46 #include "syssignal.h"
47 #ifdef MSDOS
48 #include "msdos.h"
49 static int been_here = -1;
50 #endif
51
52 #ifdef USE_X_TOOLKIT
53 #include "../lwlib/lwlib.h"
54 #endif
55
56 #include "cm.h"
57 #include "menu.h"
58
59 /* The name of the default console device. */
60 #ifdef WINDOWSNT
61 #define DEV_TTY "CONOUT$"
62 #include "w32term.h"
63 #else
64 #define DEV_TTY "/dev/tty"
65 #endif
66
67 static void tty_set_scroll_region (struct frame *f, int start, int stop);
68 static void turn_on_face (struct frame *, int face_id);
69 static void turn_off_face (struct frame *, int face_id);
70 static void tty_turn_off_highlight (struct tty_display_info *);
71 static void tty_show_cursor (struct tty_display_info *);
72 static void tty_hide_cursor (struct tty_display_info *);
73 static void tty_background_highlight (struct tty_display_info *tty);
74 static void clear_tty_hooks (struct terminal *terminal);
75 static void set_tty_hooks (struct terminal *terminal);
76 static void dissociate_if_controlling_tty (int fd);
77 static void delete_tty (struct terminal *);
78 static _Noreturn void maybe_fatal (bool, struct terminal *,
79 const char *, const char *, ...)
80 ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
81 static _Noreturn void vfatal (const char *str, va_list ap)
82 ATTRIBUTE_FORMAT_PRINTF (1, 0);
83
84
85 #define OUTPUT(tty, a) \
86 emacs_tputs ((tty), a, \
87 FRAME_TOTAL_LINES (XFRAME (selected_frame)) - curY (tty), \
88 cmputc)
89
90 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
91 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
92
93 #define OUTPUT_IF(tty, a) \
94 do { \
95 if (a) \
96 OUTPUT (tty, a); \
97 } while (0)
98
99 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
100
101 /* Display space properties. */
102
103 /* Chain of all tty device parameters. */
104 struct tty_display_info *tty_list;
105
106 /* Meaning of bits in no_color_video. Each bit set means that the
107 corresponding attribute cannot be combined with colors. */
108
109 enum no_color_bit
110 {
111 NC_STANDOUT = 1 << 0,
112 NC_UNDERLINE = 1 << 1,
113 NC_REVERSE = 1 << 2,
114 NC_ITALIC = 1 << 3,
115 NC_DIM = 1 << 4,
116 NC_BOLD = 1 << 5,
117 NC_INVIS = 1 << 6,
118 NC_PROTECT = 1 << 7
119 };
120
121 /* internal state */
122
123 /* The largest frame width in any call to calculate_costs. */
124
125 static int max_frame_cols;
126
127 \f
128
129 #ifdef HAVE_GPM
130 #include <sys/fcntl.h>
131
132 /* The device for which we have enabled gpm support (or NULL). */
133 struct tty_display_info *gpm_tty = NULL;
134
135 /* Last recorded mouse coordinates. */
136 static int last_mouse_x, last_mouse_y;
137 #endif /* HAVE_GPM */
138
139 /* Ring the bell on a tty. */
140
141 static void
142 tty_ring_bell (struct frame *f)
143 {
144 struct tty_display_info *tty = FRAME_TTY (f);
145
146 if (tty->output)
147 {
148 OUTPUT (tty, (tty->TS_visible_bell && visible_bell
149 ? tty->TS_visible_bell
150 : tty->TS_bell));
151 fflush (tty->output);
152 }
153 }
154
155 /* Set up termcap modes for Emacs. */
156
157 static void
158 tty_send_additional_strings (struct terminal *terminal, Lisp_Object sym)
159 {
160 Lisp_Object lisp_terminal;
161 Lisp_Object extra_codes;
162 struct tty_display_info *tty = terminal->display_info.tty;
163
164 XSETTERMINAL (lisp_terminal, terminal);
165 for (extra_codes = Fterminal_parameter (lisp_terminal, sym);
166 CONSP (extra_codes);
167 extra_codes = XCDR (extra_codes))
168 {
169 Lisp_Object string = XCAR (extra_codes);
170 if (STRINGP (string))
171 {
172 fwrite (SDATA (string), 1, SBYTES (string), tty->output);
173 if (tty->termscript)
174 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
175 }
176 }
177 }
178
179 static void
180 tty_set_terminal_modes (struct terminal *terminal)
181 {
182 struct tty_display_info *tty = terminal->display_info.tty;
183
184 if (tty->output)
185 {
186 if (tty->TS_termcap_modes)
187 OUTPUT (tty, tty->TS_termcap_modes);
188 else
189 {
190 /* Output enough newlines to scroll all the old screen contents
191 off the screen, so it won't be overwritten and lost. */
192 int i;
193 current_tty = tty;
194 for (i = 0; i < FRAME_TOTAL_LINES (XFRAME (selected_frame)); i++)
195 cmputc ('\n');
196 }
197
198 OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
199 OUTPUT_IF (tty, tty->TS_keypad_mode);
200 losecursor (tty);
201 tty_send_additional_strings (terminal, Qtty_mode_set_strings);
202 fflush (tty->output);
203 }
204 }
205
206 /* Reset termcap modes before exiting Emacs. */
207
208 static void
209 tty_reset_terminal_modes (struct terminal *terminal)
210 {
211 struct tty_display_info *tty = terminal->display_info.tty;
212
213 if (tty->output)
214 {
215 tty_send_additional_strings (terminal, Qtty_mode_reset_strings);
216 tty_turn_off_highlight (tty);
217 tty_turn_off_insert (tty);
218 OUTPUT_IF (tty, tty->TS_end_keypad_mode);
219 OUTPUT_IF (tty, tty->TS_cursor_normal);
220 OUTPUT_IF (tty, tty->TS_end_termcap_modes);
221 OUTPUT_IF (tty, tty->TS_orig_pair);
222 /* Output raw CR so kernel can track the cursor hpos. */
223 current_tty = tty;
224 cmputc ('\r');
225 fflush (tty->output);
226 }
227 }
228
229 /* Flag the end of a display update on a termcap terminal. */
230
231 static void
232 tty_update_end (struct frame *f)
233 {
234 struct tty_display_info *tty = FRAME_TTY (f);
235
236 if (!XWINDOW (selected_window)->cursor_off_p)
237 tty_show_cursor (tty);
238 tty_turn_off_insert (tty);
239 tty_background_highlight (tty);
240 fflush (tty->output);
241 }
242
243 /* The implementation of set_terminal_window for termcap frames. */
244
245 static void
246 tty_set_terminal_window (struct frame *f, int size)
247 {
248 struct tty_display_info *tty = FRAME_TTY (f);
249
250 tty->specified_window = size ? size : FRAME_TOTAL_LINES (f);
251 if (FRAME_SCROLL_REGION_OK (f))
252 tty_set_scroll_region (f, 0, tty->specified_window);
253 }
254
255 static void
256 tty_set_scroll_region (struct frame *f, int start, int stop)
257 {
258 char *buf;
259 struct tty_display_info *tty = FRAME_TTY (f);
260
261 if (tty->TS_set_scroll_region)
262 buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1, 0, 0);
263 else if (tty->TS_set_scroll_region_1)
264 buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
265 FRAME_TOTAL_LINES (f), start,
266 FRAME_TOTAL_LINES (f) - stop,
267 FRAME_TOTAL_LINES (f));
268 else
269 buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
270
271 OUTPUT (tty, buf);
272 xfree (buf);
273 losecursor (tty);
274 }
275
276 \f
277 static void
278 tty_turn_on_insert (struct tty_display_info *tty)
279 {
280 if (!tty->insert_mode)
281 OUTPUT (tty, tty->TS_insert_mode);
282 tty->insert_mode = 1;
283 }
284
285 void
286 tty_turn_off_insert (struct tty_display_info *tty)
287 {
288 if (tty->insert_mode)
289 OUTPUT (tty, tty->TS_end_insert_mode);
290 tty->insert_mode = 0;
291 }
292 \f
293 /* Handle highlighting. */
294
295 static void
296 tty_turn_off_highlight (struct tty_display_info *tty)
297 {
298 if (tty->standout_mode)
299 OUTPUT_IF (tty, tty->TS_end_standout_mode);
300 tty->standout_mode = 0;
301 }
302
303 static void
304 tty_turn_on_highlight (struct tty_display_info *tty)
305 {
306 if (!tty->standout_mode)
307 OUTPUT_IF (tty, tty->TS_standout_mode);
308 tty->standout_mode = 1;
309 }
310
311 static void
312 tty_toggle_highlight (struct tty_display_info *tty)
313 {
314 if (tty->standout_mode)
315 tty_turn_off_highlight (tty);
316 else
317 tty_turn_on_highlight (tty);
318 }
319
320
321 /* Make cursor invisible. */
322
323 static void
324 tty_hide_cursor (struct tty_display_info *tty)
325 {
326 if (tty->cursor_hidden == 0)
327 {
328 tty->cursor_hidden = 1;
329 #ifdef WINDOWSNT
330 w32con_hide_cursor ();
331 #else
332 OUTPUT_IF (tty, tty->TS_cursor_invisible);
333 #endif
334 }
335 }
336
337
338 /* Ensure that cursor is visible. */
339
340 static void
341 tty_show_cursor (struct tty_display_info *tty)
342 {
343 if (tty->cursor_hidden)
344 {
345 tty->cursor_hidden = 0;
346 #ifdef WINDOWSNT
347 w32con_show_cursor ();
348 #else
349 OUTPUT_IF (tty, tty->TS_cursor_normal);
350 if (visible_cursor)
351 OUTPUT_IF (tty, tty->TS_cursor_visible);
352 #endif
353 }
354 }
355
356
357 /* Set standout mode to the state it should be in for
358 empty space inside windows. What this is,
359 depends on the user option inverse-video. */
360
361 static void
362 tty_background_highlight (struct tty_display_info *tty)
363 {
364 if (inverse_video)
365 tty_turn_on_highlight (tty);
366 else
367 tty_turn_off_highlight (tty);
368 }
369
370 /* Set standout mode to the mode specified for the text to be output. */
371
372 static void
373 tty_highlight_if_desired (struct tty_display_info *tty)
374 {
375 if (inverse_video)
376 tty_turn_on_highlight (tty);
377 else
378 tty_turn_off_highlight (tty);
379 }
380 \f
381
382 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
383 frame-relative coordinates. */
384
385 static void
386 tty_cursor_to (struct frame *f, int vpos, int hpos)
387 {
388 struct tty_display_info *tty = FRAME_TTY (f);
389
390 /* Detect the case where we are called from reset_sys_modes
391 and the costs have never been calculated. Do nothing. */
392 if (! tty->costs_set)
393 return;
394
395 if (curY (tty) == vpos
396 && curX (tty) == hpos)
397 return;
398 if (!tty->TF_standout_motion)
399 tty_background_highlight (tty);
400 if (!tty->TF_insmode_motion)
401 tty_turn_off_insert (tty);
402 cmgoto (tty, vpos, hpos);
403 }
404
405 /* Similar but don't take any account of the wasted characters. */
406
407 static void
408 tty_raw_cursor_to (struct frame *f, int row, int col)
409 {
410 struct tty_display_info *tty = FRAME_TTY (f);
411
412 if (curY (tty) == row
413 && curX (tty) == col)
414 return;
415 if (!tty->TF_standout_motion)
416 tty_background_highlight (tty);
417 if (!tty->TF_insmode_motion)
418 tty_turn_off_insert (tty);
419 cmgoto (tty, row, col);
420 }
421 \f
422 /* Erase operations */
423
424 /* Clear from cursor to end of frame on a termcap device. */
425
426 static void
427 tty_clear_to_end (struct frame *f)
428 {
429 register int i;
430 struct tty_display_info *tty = FRAME_TTY (f);
431
432 if (tty->TS_clr_to_bottom)
433 {
434 tty_background_highlight (tty);
435 OUTPUT (tty, tty->TS_clr_to_bottom);
436 }
437 else
438 {
439 for (i = curY (tty); i < FRAME_TOTAL_LINES (f); i++)
440 {
441 cursor_to (f, i, 0);
442 clear_end_of_line (f, FRAME_COLS (f));
443 }
444 }
445 }
446
447 /* Clear an entire termcap frame. */
448
449 static void
450 tty_clear_frame (struct frame *f)
451 {
452 struct tty_display_info *tty = FRAME_TTY (f);
453
454 if (tty->TS_clr_frame)
455 {
456 tty_background_highlight (tty);
457 OUTPUT (tty, tty->TS_clr_frame);
458 cmat (tty, 0, 0);
459 }
460 else
461 {
462 cursor_to (f, 0, 0);
463 clear_to_end (f);
464 }
465 }
466
467 /* An implementation of clear_end_of_line for termcap frames.
468
469 Note that the cursor may be moved, on terminals lacking a `ce' string. */
470
471 static void
472 tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
473 {
474 register int i;
475 struct tty_display_info *tty = FRAME_TTY (f);
476
477 /* Detect the case where we are called from reset_sys_modes
478 and the costs have never been calculated. Do nothing. */
479 if (! tty->costs_set)
480 return;
481
482 if (curX (tty) >= first_unused_hpos)
483 return;
484 tty_background_highlight (tty);
485 if (tty->TS_clr_line)
486 {
487 OUTPUT1 (tty, tty->TS_clr_line);
488 }
489 else
490 { /* have to do it the hard way */
491 tty_turn_off_insert (tty);
492
493 /* Do not write in last row last col with Auto-wrap on. */
494 if (AutoWrap (tty)
495 && curY (tty) == FrameRows (tty) - 1
496 && first_unused_hpos == FrameCols (tty))
497 first_unused_hpos--;
498
499 for (i = curX (tty); i < first_unused_hpos; i++)
500 {
501 if (tty->termscript)
502 fputc (' ', tty->termscript);
503 fputc (' ', tty->output);
504 }
505 cmplus (tty, first_unused_hpos - curX (tty));
506 }
507 }
508 \f
509 /* Buffers to store the source and result of code conversion for terminal. */
510 static unsigned char *encode_terminal_src;
511 static unsigned char *encode_terminal_dst;
512 /* Allocated sizes of the above buffers. */
513 static ptrdiff_t encode_terminal_src_size;
514 static ptrdiff_t encode_terminal_dst_size;
515
516 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
517 Set CODING->produced to the byte-length of the resulting byte
518 sequence, and return a pointer to that byte sequence. */
519
520 unsigned char *
521 encode_terminal_code (struct glyph *src, int src_len,
522 struct coding_system *coding)
523 {
524 struct glyph *src_end = src + src_len;
525 unsigned char *buf;
526 ptrdiff_t nchars, nbytes, required;
527 ptrdiff_t tlen = GLYPH_TABLE_LENGTH;
528 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
529 Lisp_Object charset_list;
530
531 /* Allocate sufficient size of buffer to store all characters in
532 multibyte-form. But, it may be enlarged on demand if
533 Vglyph_table contains a string or a composite glyph is
534 encountered. */
535 if (INT_MULTIPLY_WRAPV (src_len, MAX_MULTIBYTE_LENGTH, &required))
536 memory_full (SIZE_MAX);
537 if (encode_terminal_src_size < required)
538 encode_terminal_src = xpalloc (encode_terminal_src,
539 &encode_terminal_src_size,
540 required - encode_terminal_src_size,
541 -1, sizeof *encode_terminal_src);
542
543 charset_list = coding_charset_list (coding);
544
545 buf = encode_terminal_src;
546 nchars = 0;
547 while (src < src_end)
548 {
549 if (src->type == COMPOSITE_GLYPH)
550 {
551 struct composition *cmp IF_LINT (= NULL);
552 Lisp_Object gstring IF_LINT (= Qnil);
553 int i;
554
555 nbytes = buf - encode_terminal_src;
556 if (src->u.cmp.automatic)
557 {
558 gstring = composition_gstring_from_id (src->u.cmp.id);
559 required = src->slice.cmp.to - src->slice.cmp.from + 1;
560 }
561 else
562 {
563 cmp = composition_table[src->u.cmp.id];
564 required = cmp->glyph_len;
565 required *= MAX_MULTIBYTE_LENGTH;
566 }
567
568 if (encode_terminal_src_size - nbytes < required)
569 {
570 encode_terminal_src =
571 xpalloc (encode_terminal_src, &encode_terminal_src_size,
572 required - (encode_terminal_src_size - nbytes),
573 -1, 1);
574 buf = encode_terminal_src + nbytes;
575 }
576
577 if (src->u.cmp.automatic)
578 for (i = src->slice.cmp.from; i <= src->slice.cmp.to; i++)
579 {
580 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
581 int c = LGLYPH_CHAR (g);
582
583 if (! char_charset (c, charset_list, NULL))
584 c = '?';
585 buf += CHAR_STRING (c, buf);
586 nchars++;
587 }
588 else
589 for (i = 0; i < cmp->glyph_len; i++)
590 {
591 int c = COMPOSITION_GLYPH (cmp, i);
592
593 /* TAB in a composition means display glyphs with
594 padding space on the left or right. */
595 if (c == '\t')
596 continue;
597 if (char_charset (c, charset_list, NULL))
598 {
599 if (CHAR_WIDTH (c) == 0
600 && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
601 /* Should be left-padded */
602 {
603 buf += CHAR_STRING (' ', buf);
604 nchars++;
605 }
606 }
607 else
608 c = '?';
609 buf += CHAR_STRING (c, buf);
610 nchars++;
611 }
612 }
613 /* We must skip glyphs to be padded for a wide character. */
614 else if (! CHAR_GLYPH_PADDING_P (*src))
615 {
616 GLYPH g;
617 int c IF_LINT (= 0);
618 Lisp_Object string;
619
620 string = Qnil;
621 SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
622
623 if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
624 {
625 /* This glyph doesn't have an entry in Vglyph_table. */
626 c = src->u.ch;
627 }
628 else
629 {
630 /* This glyph has an entry in Vglyph_table,
631 so process any alias before testing for simpleness. */
632 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
633
634 if (GLYPH_SIMPLE_P (tbase, tlen, g))
635 /* We set the multi-byte form of a character in G
636 (that should be an ASCII character) at WORKBUF. */
637 c = GLYPH_CHAR (g);
638 else
639 /* We have a string in Vglyph_table. */
640 string = tbase[GLYPH_CHAR (g)];
641 }
642
643 if (NILP (string))
644 {
645 nbytes = buf - encode_terminal_src;
646 if (encode_terminal_src_size - nbytes < MAX_MULTIBYTE_LENGTH)
647 {
648 encode_terminal_src =
649 xpalloc (encode_terminal_src, &encode_terminal_src_size,
650 MAX_MULTIBYTE_LENGTH, -1, 1);
651 buf = encode_terminal_src + nbytes;
652 }
653 if (CHAR_BYTE8_P (c)
654 || char_charset (c, charset_list, NULL))
655 {
656 /* Store the multibyte form of C at BUF. */
657 buf += CHAR_STRING (c, buf);
658 nchars++;
659 }
660 else
661 {
662 /* C is not encodable. */
663 *buf++ = '?';
664 nchars++;
665 while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
666 {
667 *buf++ = '?';
668 nchars++;
669 src++;
670 }
671 }
672 }
673 else
674 {
675 if (! STRING_MULTIBYTE (string))
676 string = string_to_multibyte (string);
677 nbytes = buf - encode_terminal_src;
678 if (encode_terminal_src_size - nbytes < SBYTES (string))
679 {
680 encode_terminal_src =
681 xpalloc (encode_terminal_src, &encode_terminal_src_size,
682 (SBYTES (string)
683 - (encode_terminal_src_size - nbytes)),
684 -1, 1);
685 buf = encode_terminal_src + nbytes;
686 }
687 memcpy (buf, SDATA (string), SBYTES (string));
688 buf += SBYTES (string);
689 nchars += SCHARS (string);
690 }
691 }
692 src++;
693 }
694
695 if (nchars == 0)
696 {
697 coding->produced = 0;
698 return NULL;
699 }
700
701 nbytes = buf - encode_terminal_src;
702 coding->source = encode_terminal_src;
703 if (encode_terminal_dst_size == 0)
704 {
705 encode_terminal_dst = xrealloc (encode_terminal_dst,
706 encode_terminal_src_size);
707 encode_terminal_dst_size = encode_terminal_src_size;
708 }
709 coding->destination = encode_terminal_dst;
710 coding->dst_bytes = encode_terminal_dst_size;
711 encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
712 /* coding->destination may have been reallocated. */
713 encode_terminal_dst = coding->destination;
714 encode_terminal_dst_size = coding->dst_bytes;
715
716 return (encode_terminal_dst);
717 }
718
719
720
721 /* An implementation of write_glyphs for termcap frames. */
722
723 static void
724 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
725 {
726 unsigned char *conversion_buffer;
727 struct coding_system *coding;
728 int n, stringlen;
729
730 struct tty_display_info *tty = FRAME_TTY (f);
731
732 tty_turn_off_insert (tty);
733 tty_hide_cursor (tty);
734
735 /* Don't dare write in last column of bottom line, if Auto-Wrap,
736 since that would scroll the whole frame on some terminals. */
737
738 if (AutoWrap (tty)
739 && curY (tty) + 1 == FRAME_TOTAL_LINES (f)
740 && (curX (tty) + len) == FRAME_COLS (f))
741 len --;
742 if (len <= 0)
743 return;
744
745 cmplus (tty, len);
746
747 /* If terminal_coding does any conversion, use it, otherwise use
748 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
749 because it always return 1 if the member src_multibyte is 1. */
750 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
751 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
752 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
753 the tail. */
754 coding->mode &= ~CODING_MODE_LAST_BLOCK;
755
756 for (stringlen = len; stringlen != 0; stringlen -= n)
757 {
758 /* Identify a run of glyphs with the same face. */
759 int face_id = string->face_id;
760
761 for (n = 1; n < stringlen; ++n)
762 if (string[n].face_id != face_id)
763 break;
764
765 /* Turn appearance modes of the face of the run on. */
766 tty_highlight_if_desired (tty);
767 turn_on_face (f, face_id);
768
769 if (n == stringlen)
770 /* This is the last run. */
771 coding->mode |= CODING_MODE_LAST_BLOCK;
772 conversion_buffer = encode_terminal_code (string, n, coding);
773 if (coding->produced > 0)
774 {
775 block_input ();
776 fwrite (conversion_buffer, 1, coding->produced, tty->output);
777 if (ferror (tty->output))
778 clearerr (tty->output);
779 if (tty->termscript)
780 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
781 unblock_input ();
782 }
783 string += n;
784
785 /* Turn appearance modes off. */
786 turn_off_face (f, face_id);
787 tty_turn_off_highlight (tty);
788 }
789
790 cmcheckmagic (tty);
791 }
792
793 #ifdef HAVE_GPM /* Only used by GPM code. */
794
795 static void
796 tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
797 register int len, register int face_id)
798 {
799 unsigned char *conversion_buffer;
800 struct coding_system *coding;
801
802 struct tty_display_info *tty = FRAME_TTY (f);
803
804 tty_turn_off_insert (tty);
805 tty_hide_cursor (tty);
806
807 /* Don't dare write in last column of bottom line, if Auto-Wrap,
808 since that would scroll the whole frame on some terminals. */
809
810 if (AutoWrap (tty)
811 && curY (tty) + 1 == FRAME_TOTAL_LINES (f)
812 && (curX (tty) + len) == FRAME_COLS (f))
813 len --;
814 if (len <= 0)
815 return;
816
817 cmplus (tty, len);
818
819 /* If terminal_coding does any conversion, use it, otherwise use
820 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
821 because it always return 1 if the member src_multibyte is 1. */
822 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
823 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
824 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
825 the tail. */
826 coding->mode &= ~CODING_MODE_LAST_BLOCK;
827
828 /* Turn appearance modes of the face. */
829 tty_highlight_if_desired (tty);
830 turn_on_face (f, face_id);
831
832 coding->mode |= CODING_MODE_LAST_BLOCK;
833 conversion_buffer = encode_terminal_code (string, len, coding);
834 if (coding->produced > 0)
835 {
836 block_input ();
837 fwrite (conversion_buffer, 1, coding->produced, tty->output);
838 if (ferror (tty->output))
839 clearerr (tty->output);
840 if (tty->termscript)
841 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
842 unblock_input ();
843 }
844
845 /* Turn appearance modes off. */
846 turn_off_face (f, face_id);
847 tty_turn_off_highlight (tty);
848
849 cmcheckmagic (tty);
850 }
851 #endif
852
853 /* An implementation of insert_glyphs for termcap frames. */
854
855 static void
856 tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
857 {
858 char *buf;
859 struct glyph *glyph = NULL;
860 unsigned char *conversion_buffer;
861 unsigned char space[1];
862 struct coding_system *coding;
863
864 struct tty_display_info *tty = FRAME_TTY (f);
865
866 if (tty->TS_ins_multi_chars)
867 {
868 buf = tparam (tty->TS_ins_multi_chars, 0, 0, len, 0, 0, 0);
869 OUTPUT1 (tty, buf);
870 xfree (buf);
871 if (start)
872 write_glyphs (f, start, len);
873 return;
874 }
875
876 tty_turn_on_insert (tty);
877 cmplus (tty, len);
878
879 if (! start)
880 space[0] = SPACEGLYPH;
881
882 /* If terminal_coding does any conversion, use it, otherwise use
883 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
884 because it always return 1 if the member src_multibyte is 1. */
885 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
886 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
887 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
888 the tail. */
889 coding->mode &= ~CODING_MODE_LAST_BLOCK;
890
891 while (len-- > 0)
892 {
893 OUTPUT1_IF (tty, tty->TS_ins_char);
894 if (!start)
895 {
896 conversion_buffer = space;
897 coding->produced = 1;
898 }
899 else
900 {
901 tty_highlight_if_desired (tty);
902 turn_on_face (f, start->face_id);
903 glyph = start;
904 ++start;
905 /* We must open sufficient space for a character which
906 occupies more than one column. */
907 while (len && CHAR_GLYPH_PADDING_P (*start))
908 {
909 OUTPUT1_IF (tty, tty->TS_ins_char);
910 start++, len--;
911 }
912
913 if (len <= 0)
914 /* This is the last glyph. */
915 coding->mode |= CODING_MODE_LAST_BLOCK;
916
917 conversion_buffer = encode_terminal_code (glyph, 1, coding);
918 }
919
920 if (coding->produced > 0)
921 {
922 block_input ();
923 fwrite (conversion_buffer, 1, coding->produced, tty->output);
924 if (ferror (tty->output))
925 clearerr (tty->output);
926 if (tty->termscript)
927 fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
928 unblock_input ();
929 }
930
931 OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
932 if (start)
933 {
934 turn_off_face (f, glyph->face_id);
935 tty_turn_off_highlight (tty);
936 }
937 }
938
939 cmcheckmagic (tty);
940 }
941
942 /* An implementation of delete_glyphs for termcap frames. */
943
944 static void
945 tty_delete_glyphs (struct frame *f, int n)
946 {
947 char *buf;
948 register int i;
949
950 struct tty_display_info *tty = FRAME_TTY (f);
951
952 if (tty->delete_in_insert_mode)
953 {
954 tty_turn_on_insert (tty);
955 }
956 else
957 {
958 tty_turn_off_insert (tty);
959 OUTPUT_IF (tty, tty->TS_delete_mode);
960 }
961
962 if (tty->TS_del_multi_chars)
963 {
964 buf = tparam (tty->TS_del_multi_chars, 0, 0, n, 0, 0, 0);
965 OUTPUT1 (tty, buf);
966 xfree (buf);
967 }
968 else
969 for (i = 0; i < n; i++)
970 OUTPUT1 (tty, tty->TS_del_char);
971 if (!tty->delete_in_insert_mode)
972 OUTPUT_IF (tty, tty->TS_end_delete_mode);
973 }
974 \f
975 /* An implementation of ins_del_lines for termcap frames. */
976
977 static void
978 tty_ins_del_lines (struct frame *f, int vpos, int n)
979 {
980 struct tty_display_info *tty = FRAME_TTY (f);
981 const char *multi =
982 n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
983 const char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
984 const char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
985
986 int i = eabs (n);
987 char *buf;
988
989 /* If the lines below the insertion are being pushed
990 into the end of the window, this is the same as clearing;
991 and we know the lines are already clear, since the matching
992 deletion has already been done. So can ignore this. */
993 /* If the lines below the deletion are blank lines coming
994 out of the end of the window, don't bother,
995 as there will be a matching inslines later that will flush them. */
996 if (FRAME_SCROLL_REGION_OK (f)
997 && vpos + i >= tty->specified_window)
998 return;
999 if (!FRAME_MEMORY_BELOW_FRAME (f)
1000 && vpos + i >= FRAME_TOTAL_LINES (f))
1001 return;
1002
1003 if (multi)
1004 {
1005 raw_cursor_to (f, vpos, 0);
1006 tty_background_highlight (tty);
1007 buf = tparam (multi, 0, 0, i, 0, 0, 0);
1008 OUTPUT (tty, buf);
1009 xfree (buf);
1010 }
1011 else if (single)
1012 {
1013 raw_cursor_to (f, vpos, 0);
1014 tty_background_highlight (tty);
1015 while (--i >= 0)
1016 OUTPUT (tty, single);
1017 if (tty->TF_teleray)
1018 curX (tty) = 0;
1019 }
1020 else
1021 {
1022 tty_set_scroll_region (f, vpos, tty->specified_window);
1023 if (n < 0)
1024 raw_cursor_to (f, tty->specified_window - 1, 0);
1025 else
1026 raw_cursor_to (f, vpos, 0);
1027 tty_background_highlight (tty);
1028 while (--i >= 0)
1029 OUTPUTL (tty, scroll, tty->specified_window - vpos);
1030 tty_set_scroll_region (f, 0, tty->specified_window);
1031 }
1032
1033 if (!FRAME_SCROLL_REGION_OK (f)
1034 && FRAME_MEMORY_BELOW_FRAME (f)
1035 && n < 0)
1036 {
1037 cursor_to (f, FRAME_TOTAL_LINES (f) + n, 0);
1038 clear_to_end (f);
1039 }
1040 }
1041 \f
1042 /* Compute cost of sending "str", in characters,
1043 not counting any line-dependent padding. */
1044
1045 int
1046 string_cost (const char *str)
1047 {
1048 cost = 0;
1049 if (str)
1050 tputs (str, 0, evalcost);
1051 return cost;
1052 }
1053
1054 /* Compute cost of sending "str", in characters,
1055 counting any line-dependent padding at one line. */
1056
1057 static int
1058 string_cost_one_line (const char *str)
1059 {
1060 cost = 0;
1061 if (str)
1062 tputs (str, 1, evalcost);
1063 return cost;
1064 }
1065
1066 /* Compute per line amount of line-dependent padding,
1067 in tenths of characters. */
1068
1069 int
1070 per_line_cost (const char *str)
1071 {
1072 cost = 0;
1073 if (str)
1074 tputs (str, 0, evalcost);
1075 cost = - cost;
1076 if (str)
1077 tputs (str, 10, evalcost);
1078 return cost;
1079 }
1080
1081 /* char_ins_del_cost[n] is cost of inserting N characters.
1082 char_ins_del_cost[-n] is cost of deleting N characters.
1083 The length of this vector is based on max_frame_cols. */
1084
1085 int *char_ins_del_vector;
1086
1087 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1088
1089 /* ARGSUSED */
1090 static void
1091 calculate_ins_del_char_costs (struct frame *f)
1092 {
1093 struct tty_display_info *tty = FRAME_TTY (f);
1094 int ins_startup_cost, del_startup_cost;
1095 int ins_cost_per_char, del_cost_per_char;
1096 register int i;
1097 register int *p;
1098
1099 if (tty->TS_ins_multi_chars)
1100 {
1101 ins_cost_per_char = 0;
1102 ins_startup_cost = string_cost_one_line (tty->TS_ins_multi_chars);
1103 }
1104 else if (tty->TS_ins_char || tty->TS_pad_inserted_char
1105 || (tty->TS_insert_mode && tty->TS_end_insert_mode))
1106 {
1107 ins_startup_cost = (30 * (string_cost (tty->TS_insert_mode)
1108 + string_cost (tty->TS_end_insert_mode))) / 100;
1109 ins_cost_per_char = (string_cost_one_line (tty->TS_ins_char)
1110 + string_cost_one_line (tty->TS_pad_inserted_char));
1111 }
1112 else
1113 {
1114 ins_startup_cost = 9999;
1115 ins_cost_per_char = 0;
1116 }
1117
1118 if (tty->TS_del_multi_chars)
1119 {
1120 del_cost_per_char = 0;
1121 del_startup_cost = string_cost_one_line (tty->TS_del_multi_chars);
1122 }
1123 else if (tty->TS_del_char)
1124 {
1125 del_startup_cost = (string_cost (tty->TS_delete_mode)
1126 + string_cost (tty->TS_end_delete_mode));
1127 if (tty->delete_in_insert_mode)
1128 del_startup_cost /= 2;
1129 del_cost_per_char = string_cost_one_line (tty->TS_del_char);
1130 }
1131 else
1132 {
1133 del_startup_cost = 9999;
1134 del_cost_per_char = 0;
1135 }
1136
1137 /* Delete costs are at negative offsets */
1138 p = &char_ins_del_cost (f)[0];
1139 for (i = FRAME_COLS (f); --i >= 0;)
1140 *--p = (del_startup_cost += del_cost_per_char);
1141
1142 /* Doing nothing is free */
1143 p = &char_ins_del_cost (f)[0];
1144 *p++ = 0;
1145
1146 /* Insert costs are at positive offsets */
1147 for (i = FRAME_COLS (f); --i >= 0;)
1148 *p++ = (ins_startup_cost += ins_cost_per_char);
1149 }
1150
1151 void
1152 calculate_costs (struct frame *frame)
1153 {
1154 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1155
1156 if (FRAME_TERMCAP_P (frame))
1157 {
1158 struct tty_display_info *tty = FRAME_TTY (frame);
1159 register const char *f = (tty->TS_set_scroll_region
1160 ? tty->TS_set_scroll_region
1161 : tty->TS_set_scroll_region_1);
1162
1163 FRAME_SCROLL_REGION_COST (frame) = string_cost (f);
1164
1165 tty->costs_set = 1;
1166
1167 /* These variables are only used for terminal stuff. They are
1168 allocated once for the terminal frame of X-windows emacs, but not
1169 used afterwards.
1170
1171 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1172 X turns off char_ins_del_ok. */
1173
1174 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1175 if ((min (PTRDIFF_MAX, SIZE_MAX) / sizeof (int) - 1) / 2
1176 < max_frame_cols)
1177 memory_full (SIZE_MAX);
1178
1179 char_ins_del_vector =
1180 xrealloc (char_ins_del_vector,
1181 (sizeof (int) + 2 * sizeof (int) * max_frame_cols));
1182
1183 memset (char_ins_del_vector, 0,
1184 (sizeof (int) + 2 * sizeof (int) * max_frame_cols));
1185
1186
1187 if (f && (!tty->TS_ins_line && !tty->TS_del_line))
1188 do_line_insertion_deletion_costs (frame,
1189 tty->TS_rev_scroll, tty->TS_ins_multi_lines,
1190 tty->TS_fwd_scroll, tty->TS_del_multi_lines,
1191 f, f, 1);
1192 else
1193 do_line_insertion_deletion_costs (frame,
1194 tty->TS_ins_line, tty->TS_ins_multi_lines,
1195 tty->TS_del_line, tty->TS_del_multi_lines,
1196 0, 0, 1);
1197
1198 calculate_ins_del_char_costs (frame);
1199
1200 /* Don't use TS_repeat if its padding is worse than sending the chars */
1201 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000)
1202 tty->RPov = string_cost (tty->TS_repeat);
1203 else
1204 tty->RPov = FRAME_COLS (frame) * 2;
1205
1206 cmcostinit (FRAME_TTY (frame)); /* set up cursor motion costs */
1207 }
1208 }
1209 \f
1210 struct fkey_table {
1211 const char *cap, *name;
1212 };
1213
1214 /* Termcap capability names that correspond directly to X keysyms.
1215 Some of these (marked "terminfo") aren't supplied by old-style
1216 (Berkeley) termcap entries. They're listed in X keysym order;
1217 except we put the keypad keys first, so that if they clash with
1218 other keys (as on the IBM PC keyboard) they get overridden.
1219 */
1220
1221 static const struct fkey_table keys[] =
1222 {
1223 {"kh", "home"}, /* termcap */
1224 {"kl", "left"}, /* termcap */
1225 {"ku", "up"}, /* termcap */
1226 {"kr", "right"}, /* termcap */
1227 {"kd", "down"}, /* termcap */
1228 {"%8", "prior"}, /* terminfo */
1229 {"%5", "next"}, /* terminfo */
1230 {"@7", "end"}, /* terminfo */
1231 {"@1", "begin"}, /* terminfo */
1232 {"*6", "select"}, /* terminfo */
1233 {"%9", "print"}, /* terminfo */
1234 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1235 /*
1236 * "insert" --- see below
1237 */
1238 {"&8", "undo"}, /* terminfo */
1239 {"%0", "redo"}, /* terminfo */
1240 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1241 {"@0", "find"}, /* terminfo */
1242 {"@2", "cancel"}, /* terminfo */
1243 {"%1", "help"}, /* terminfo */
1244 /*
1245 * "break" goes here, but can't be reliably intercepted with termcap
1246 */
1247 {"&4", "reset"}, /* terminfo --- actually `restart' */
1248 /*
1249 * "system" and "user" --- no termcaps
1250 */
1251 {"kE", "clearline"}, /* terminfo */
1252 {"kA", "insertline"}, /* terminfo */
1253 {"kL", "deleteline"}, /* terminfo */
1254 {"kI", "insertchar"}, /* terminfo */
1255 {"kD", "deletechar"}, /* terminfo */
1256 {"kB", "backtab"}, /* terminfo */
1257 /*
1258 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1259 */
1260 {"@8", "kp-enter"}, /* terminfo */
1261 /*
1262 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1263 * "kp-multiply", "kp-add", "kp-separator",
1264 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1265 * --- no termcaps for any of these.
1266 */
1267 {"K4", "kp-1"}, /* terminfo */
1268 /*
1269 * "kp-2" --- no termcap
1270 */
1271 {"K5", "kp-3"}, /* terminfo */
1272 /*
1273 * "kp-4" --- no termcap
1274 */
1275 {"K2", "kp-5"}, /* terminfo */
1276 /*
1277 * "kp-6" --- no termcap
1278 */
1279 {"K1", "kp-7"}, /* terminfo */
1280 /*
1281 * "kp-8" --- no termcap
1282 */
1283 {"K3", "kp-9"}, /* terminfo */
1284 /*
1285 * "kp-equal" --- no termcap
1286 */
1287 {"k1", "f1"},
1288 {"k2", "f2"},
1289 {"k3", "f3"},
1290 {"k4", "f4"},
1291 {"k5", "f5"},
1292 {"k6", "f6"},
1293 {"k7", "f7"},
1294 {"k8", "f8"},
1295 {"k9", "f9"},
1296
1297 {"&0", "S-cancel"}, /*shifted cancel key*/
1298 {"&9", "S-begin"}, /*shifted begin key*/
1299 {"*0", "S-find"}, /*shifted find key*/
1300 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1301 {"*4", "S-delete"}, /*shifted delete-character key*/
1302 {"*7", "S-end"}, /*shifted end key*/
1303 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1304 {"#1", "S-help"}, /*shifted help key*/
1305 {"#2", "S-home"}, /*shifted home key*/
1306 {"#3", "S-insert"}, /*shifted insert-character key*/
1307 {"#4", "S-left"}, /*shifted left-arrow key*/
1308 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1309 {"%c", "S-next"}, /*shifted next key*/
1310 {"%e", "S-prior"}, /*shifted previous key*/
1311 {"%f", "S-print"}, /*shifted print key*/
1312 {"%g", "S-redo"}, /*shifted redo key*/
1313 {"%i", "S-right"}, /*shifted right-arrow key*/
1314 {"!3", "S-undo"} /*shifted undo key*/
1315 };
1316
1317 #ifndef DOS_NT
1318 static char **term_get_fkeys_address;
1319 static KBOARD *term_get_fkeys_kboard;
1320 static Lisp_Object term_get_fkeys_1 (void);
1321
1322 /* Find the escape codes sent by the function keys for Vinput_decode_map.
1323 This function scans the termcap function key sequence entries, and
1324 adds entries to Vinput_decode_map for each function key it finds. */
1325
1326 static void
1327 term_get_fkeys (char **address, KBOARD *kboard)
1328 {
1329 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1330 errors during the call. The only errors should be from Fdefine_key
1331 when given a key sequence containing an invalid prefix key. If the
1332 termcap defines function keys which use a prefix that is already bound
1333 to a command by the default bindings, we should silently ignore that
1334 function key specification, rather than giving the user an error and
1335 refusing to run at all on such a terminal. */
1336
1337 term_get_fkeys_address = address;
1338 term_get_fkeys_kboard = kboard;
1339 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1340 }
1341
1342 static Lisp_Object
1343 term_get_fkeys_1 (void)
1344 {
1345 int i;
1346
1347 char **address = term_get_fkeys_address;
1348 KBOARD *kboard = term_get_fkeys_kboard;
1349
1350 /* This can happen if CANNOT_DUMP or with strange options. */
1351 if (!KEYMAPP (KVAR (kboard, Vinput_decode_map)))
1352 kset_input_decode_map (kboard, Fmake_sparse_keymap (Qnil));
1353
1354 for (i = 0; i < ARRAYELTS (keys); i++)
1355 {
1356 char *sequence = tgetstr (keys[i].cap, address);
1357 if (sequence)
1358 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1359 Fmake_vector (make_number (1),
1360 intern (keys[i].name)));
1361 }
1362
1363 /* The uses of the "k0" capability are inconsistent; sometimes it
1364 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1365 We will attempt to politely accommodate both systems by testing for
1366 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1367 */
1368 {
1369 const char *k_semi = tgetstr ("k;", address);
1370 const char *k0 = tgetstr ("k0", address);
1371 const char *k0_name = "f10";
1372
1373 if (k_semi)
1374 {
1375 if (k0)
1376 /* Define f0 first, so that f10 takes precedence in case the
1377 key sequences happens to be the same. */
1378 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1379 Fmake_vector (make_number (1), intern ("f0")));
1380 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k_semi),
1381 Fmake_vector (make_number (1), intern ("f10")));
1382 }
1383 else if (k0)
1384 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (k0),
1385 Fmake_vector (make_number (1), intern (k0_name)));
1386 }
1387
1388 /* Set up cookies for numbered function keys above f10. */
1389 {
1390 char fcap[3], fkey[4];
1391
1392 fcap[0] = 'F'; fcap[2] = '\0';
1393 for (i = 11; i < 64; i++)
1394 {
1395 if (i <= 19)
1396 fcap[1] = '1' + i - 11;
1397 else if (i <= 45)
1398 fcap[1] = 'A' + i - 20;
1399 else
1400 fcap[1] = 'a' + i - 46;
1401
1402 {
1403 char *sequence = tgetstr (fcap, address);
1404 if (sequence)
1405 {
1406 sprintf (fkey, "f%d", i);
1407 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence),
1408 Fmake_vector (make_number (1),
1409 intern (fkey)));
1410 }
1411 }
1412 }
1413 }
1414
1415 /*
1416 * Various mappings to try and get a better fit.
1417 */
1418 {
1419 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1420 if (!tgetstr (cap1, address)) \
1421 { \
1422 char *sequence = tgetstr (cap2, address); \
1423 if (sequence) \
1424 Fdefine_key (KVAR (kboard, Vinput_decode_map), build_string (sequence), \
1425 Fmake_vector (make_number (1), \
1426 intern (sym))); \
1427 }
1428
1429 /* if there's no key_next keycap, map key_npage to `next' keysym */
1430 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1431 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1432 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1433 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1434 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1435 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1436 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1437 #undef CONDITIONAL_REASSIGN
1438 }
1439
1440 return Qnil;
1441 }
1442 #endif /* not DOS_NT */
1443
1444 \f
1445 /***********************************************************************
1446 Character Display Information
1447 ***********************************************************************/
1448 static void append_glyph (struct it *);
1449 static void append_composite_glyph (struct it *);
1450 static void produce_composite_glyph (struct it *);
1451 static void append_glyphless_glyph (struct it *, int, const char *);
1452 static void produce_glyphless_glyph (struct it *, Lisp_Object);
1453
1454 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1455 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1456 the character for which to produce glyphs; IT->face_id contains the
1457 character's face. Padding glyphs are appended if IT->c has a
1458 IT->pixel_width > 1. */
1459
1460 static void
1461 append_glyph (struct it *it)
1462 {
1463 struct glyph *glyph, *end;
1464 int i;
1465
1466 eassert (it->glyph_row);
1467 glyph = (it->glyph_row->glyphs[it->area]
1468 + it->glyph_row->used[it->area]);
1469 end = it->glyph_row->glyphs[1 + it->area];
1470
1471 /* If the glyph row is reversed, we need to prepend the glyph rather
1472 than append it. */
1473 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1474 {
1475 struct glyph *g;
1476 int move_by = it->pixel_width;
1477
1478 /* Make room for the new glyphs. */
1479 if (move_by > end - glyph) /* don't overstep end of this area */
1480 move_by = end - glyph;
1481 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1482 g[move_by] = *g;
1483 glyph = it->glyph_row->glyphs[it->area];
1484 end = glyph + move_by;
1485 }
1486
1487 /* BIDI Note: we put the glyphs of a "multi-pixel" character left to
1488 right, even in the REVERSED_P case, since (a) all of its u.ch are
1489 identical, and (b) the PADDING_P flag needs to be set for the
1490 leftmost one, because we write to the terminal left-to-right. */
1491 for (i = 0;
1492 i < it->pixel_width && glyph < end;
1493 ++i)
1494 {
1495 glyph->type = CHAR_GLYPH;
1496 glyph->pixel_width = 1;
1497 glyph->u.ch = it->char_to_display;
1498 glyph->face_id = it->face_id;
1499 glyph->padding_p = i > 0;
1500 glyph->charpos = CHARPOS (it->position);
1501 glyph->object = it->object;
1502 if (it->bidi_p)
1503 {
1504 glyph->resolved_level = it->bidi_it.resolved_level;
1505 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
1506 glyph->bidi_type = it->bidi_it.type;
1507 }
1508 else
1509 {
1510 glyph->resolved_level = 0;
1511 glyph->bidi_type = UNKNOWN_BT;
1512 }
1513
1514 ++it->glyph_row->used[it->area];
1515 ++glyph;
1516 }
1517 }
1518
1519 /* For external use. */
1520 void
1521 tty_append_glyph (struct it *it)
1522 {
1523 append_glyph (it);
1524 }
1525
1526
1527 /* Produce glyphs for the display element described by IT. *IT
1528 specifies what we want to produce a glyph for (character, image, ...),
1529 and where in the glyph matrix we currently are (glyph row and hpos).
1530 produce_glyphs fills in output fields of *IT with information such as the
1531 pixel width and height of a character, and maybe output actual glyphs at
1532 the same time if IT->glyph_row is non-null. For an overview, see
1533 the explanation in dispextern.h, before the definition of the
1534 display_element_type enumeration.
1535
1536 produce_glyphs also stores the result of glyph width, ascent
1537 etc. computations in *IT.
1538
1539 IT->glyph_row may be null, in which case produce_glyphs does not
1540 actually fill in the glyphs. This is used in the move_* functions
1541 in xdisp.c for text width and height computations.
1542
1543 Callers usually don't call produce_glyphs directly;
1544 instead they use the macro PRODUCE_GLYPHS. */
1545
1546 void
1547 produce_glyphs (struct it *it)
1548 {
1549 /* If a hook is installed, let it do the work. */
1550
1551 /* Nothing but characters are supported on terminal frames. */
1552 eassert (it->what == IT_CHARACTER
1553 || it->what == IT_COMPOSITION
1554 || it->what == IT_STRETCH
1555 || it->what == IT_GLYPHLESS);
1556
1557 if (it->what == IT_STRETCH)
1558 {
1559 produce_stretch_glyph (it);
1560 goto done;
1561 }
1562
1563 if (it->what == IT_COMPOSITION)
1564 {
1565 produce_composite_glyph (it);
1566 goto done;
1567 }
1568
1569 if (it->what == IT_GLYPHLESS)
1570 {
1571 produce_glyphless_glyph (it, Qnil);
1572 goto done;
1573 }
1574
1575 if (it->char_to_display >= 040 && it->char_to_display < 0177)
1576 {
1577 it->pixel_width = it->nglyphs = 1;
1578 if (it->glyph_row)
1579 append_glyph (it);
1580 }
1581 else if (it->char_to_display == '\n')
1582 it->pixel_width = it->nglyphs = 0;
1583 else if (it->char_to_display == '\t')
1584 {
1585 int absolute_x = (it->current_x
1586 + it->continuation_lines_width);
1587 int next_tab_x
1588 = (((1 + absolute_x + it->tab_width - 1)
1589 / it->tab_width)
1590 * it->tab_width);
1591 int nspaces;
1592
1593 /* If part of the TAB has been displayed on the previous line
1594 which is continued now, continuation_lines_width will have
1595 been incremented already by the part that fitted on the
1596 continued line. So, we will get the right number of spaces
1597 here. */
1598 nspaces = next_tab_x - absolute_x;
1599
1600 if (it->glyph_row)
1601 {
1602 int n = nspaces;
1603
1604 it->char_to_display = ' ';
1605 it->pixel_width = it->len = 1;
1606
1607 while (n--)
1608 append_glyph (it);
1609 }
1610
1611 it->pixel_width = nspaces;
1612 it->nglyphs = nspaces;
1613 }
1614 else if (CHAR_BYTE8_P (it->char_to_display))
1615 {
1616 /* Coming here means that we must send the raw 8-bit byte as is
1617 to the terminal. Although there's no way to know how many
1618 columns it occupies on a screen, it is a good assumption that
1619 a single byte code has 1-column width. */
1620 it->pixel_width = it->nglyphs = 1;
1621 if (it->glyph_row)
1622 append_glyph (it);
1623 }
1624 else
1625 {
1626 Lisp_Object charset_list = FRAME_TERMINAL (it->f)->charset_list;
1627
1628 if (char_charset (it->char_to_display, charset_list, NULL))
1629 {
1630 it->pixel_width = CHAR_WIDTH (it->char_to_display);
1631 it->nglyphs = it->pixel_width;
1632 if (it->glyph_row)
1633 append_glyph (it);
1634 }
1635 else
1636 {
1637 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
1638
1639 eassert (it->what == IT_GLYPHLESS);
1640 produce_glyphless_glyph (it, acronym);
1641 }
1642 }
1643
1644 done:
1645 /* Advance current_x by the pixel width as a convenience for
1646 the caller. */
1647 if (it->area == TEXT_AREA)
1648 it->current_x += it->pixel_width;
1649 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1650 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1651 }
1652
1653 /* Append glyphs to IT's glyph_row for the composition IT->cmp_id.
1654 Called from produce_composite_glyph for terminal frames if
1655 IT->glyph_row != NULL. IT->face_id contains the character's
1656 face. */
1657
1658 static void
1659 append_composite_glyph (struct it *it)
1660 {
1661 struct glyph *glyph;
1662
1663 eassert (it->glyph_row);
1664 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1665 if (glyph < it->glyph_row->glyphs[1 + it->area])
1666 {
1667 /* If the glyph row is reversed, we need to prepend the glyph
1668 rather than append it. */
1669 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1670 {
1671 struct glyph *g;
1672
1673 /* Make room for the new glyph. */
1674 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1675 g[1] = *g;
1676 glyph = it->glyph_row->glyphs[it->area];
1677 }
1678 glyph->type = COMPOSITE_GLYPH;
1679 eassert (it->pixel_width <= SHRT_MAX);
1680 glyph->pixel_width = it->pixel_width;
1681 glyph->u.cmp.id = it->cmp_it.id;
1682 if (it->cmp_it.ch < 0)
1683 {
1684 glyph->u.cmp.automatic = 0;
1685 glyph->u.cmp.id = it->cmp_it.id;
1686 }
1687 else
1688 {
1689 glyph->u.cmp.automatic = 1;
1690 glyph->u.cmp.id = it->cmp_it.id;
1691 glyph->slice.cmp.from = it->cmp_it.from;
1692 glyph->slice.cmp.to = it->cmp_it.to - 1;
1693 }
1694
1695 glyph->face_id = it->face_id;
1696 glyph->padding_p = 0;
1697 glyph->charpos = CHARPOS (it->position);
1698 glyph->object = it->object;
1699 if (it->bidi_p)
1700 {
1701 glyph->resolved_level = it->bidi_it.resolved_level;
1702 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
1703 glyph->bidi_type = it->bidi_it.type;
1704 }
1705 else
1706 {
1707 glyph->resolved_level = 0;
1708 glyph->bidi_type = UNKNOWN_BT;
1709 }
1710
1711 ++it->glyph_row->used[it->area];
1712 ++glyph;
1713 }
1714 }
1715
1716
1717 /* Produce a composite glyph for iterator IT. IT->cmp_id is the ID of
1718 the composition. We simply produces components of the composition
1719 assuming that the terminal has a capability to layout/render it
1720 correctly. */
1721
1722 static void
1723 produce_composite_glyph (struct it *it)
1724 {
1725 if (it->cmp_it.ch < 0)
1726 {
1727 struct composition *cmp = composition_table[it->cmp_it.id];
1728
1729 it->pixel_width = cmp->width;
1730 }
1731 else
1732 {
1733 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
1734
1735 it->pixel_width = composition_gstring_width (gstring, it->cmp_it.from,
1736 it->cmp_it.to, NULL);
1737 }
1738 it->nglyphs = 1;
1739 if (it->glyph_row)
1740 append_composite_glyph (it);
1741 }
1742
1743
1744 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
1745 is a face ID to be used for the glyph. What is actually appended
1746 are glyphs of type CHAR_GLYPH whose characters are in STR (which
1747 comes from it->nglyphs bytes). */
1748
1749 static void
1750 append_glyphless_glyph (struct it *it, int face_id, const char *str)
1751 {
1752 struct glyph *glyph, *end;
1753 int i;
1754
1755 eassert (it->glyph_row);
1756 glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
1757 end = it->glyph_row->glyphs[1 + it->area];
1758
1759 /* If the glyph row is reversed, we need to prepend the glyph rather
1760 than append it. */
1761 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
1762 {
1763 struct glyph *g;
1764 int move_by = it->pixel_width;
1765
1766 /* Make room for the new glyphs. */
1767 if (move_by > end - glyph) /* don't overstep end of this area */
1768 move_by = end - glyph;
1769 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
1770 g[move_by] = *g;
1771 glyph = it->glyph_row->glyphs[it->area];
1772 end = glyph + move_by;
1773 }
1774
1775 if (glyph >= end)
1776 return;
1777 glyph->type = CHAR_GLYPH;
1778 glyph->pixel_width = 1;
1779 glyph->face_id = face_id;
1780 glyph->padding_p = 0;
1781 glyph->charpos = CHARPOS (it->position);
1782 glyph->object = it->object;
1783 if (it->bidi_p)
1784 {
1785 glyph->resolved_level = it->bidi_it.resolved_level;
1786 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
1787 glyph->bidi_type = it->bidi_it.type;
1788 }
1789 else
1790 {
1791 glyph->resolved_level = 0;
1792 glyph->bidi_type = UNKNOWN_BT;
1793 }
1794
1795 /* BIDI Note: we put the glyphs of characters left to right, even in
1796 the REVERSED_P case because we write to the terminal
1797 left-to-right. */
1798 for (i = 0; i < it->nglyphs && glyph < end; ++i)
1799 {
1800 if (i > 0)
1801 glyph[0] = glyph[-1];
1802 glyph->u.ch = str[i];
1803 ++it->glyph_row->used[it->area];
1804 ++glyph;
1805 }
1806 }
1807
1808 /* Produce glyphs for a glyphless character for iterator IT.
1809 IT->glyphless_method specifies which method to use for displaying
1810 the character. See the description of enum
1811 glyphless_display_method in dispextern.h for the details.
1812
1813 ACRONYM, if non-nil, is an acronym string for the character.
1814
1815 The glyphs actually produced are of type CHAR_GLYPH. */
1816
1817 static void
1818 produce_glyphless_glyph (struct it *it, Lisp_Object acronym)
1819 {
1820 int len, face_id = merge_glyphless_glyph_face (it);
1821 char buf[sizeof "\\x" + max (6, (sizeof it->c * CHAR_BIT + 3) / 4)];
1822 char const *str = " ";
1823
1824 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
1825 {
1826 /* As there's no way to produce a thin space, we produce a space
1827 of canonical width. */
1828 len = 1;
1829 }
1830 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
1831 {
1832 len = CHAR_WIDTH (it->c);
1833 if (len == 0)
1834 len = 1;
1835 else if (len > 4)
1836 len = 4;
1837 len = sprintf (buf, "[%.*s]", len, str);
1838 str = buf;
1839 }
1840 else
1841 {
1842 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
1843 {
1844 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
1845 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
1846 if (CONSP (acronym))
1847 acronym = XCDR (acronym);
1848 buf[0] = '[';
1849 str = STRINGP (acronym) ? SSDATA (acronym) : "";
1850 for (len = 0; len < 6 && str[len] && ASCII_CHAR_P (str[len]); len++)
1851 buf[1 + len] = str[len];
1852 buf[1 + len] = ']';
1853 len += 2;
1854 }
1855 else
1856 {
1857 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
1858 len = sprintf (buf,
1859 (it->c < 0x10000 ? "\\u%04X"
1860 : it->c <= MAX_UNICODE_CHAR ? "\\U%06X"
1861 : "\\x%06X"),
1862 it->c + 0u);
1863 }
1864 str = buf;
1865 }
1866
1867 it->pixel_width = len;
1868 it->nglyphs = len;
1869 if (it->glyph_row)
1870 append_glyphless_glyph (it, face_id, str);
1871 }
1872
1873 \f
1874 /***********************************************************************
1875 Faces
1876 ***********************************************************************/
1877
1878 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1879 one of the enumerators from enum no_color_bit, or a bit set built
1880 from them. Some display attributes may not be used together with
1881 color; the termcap capability `NC' specifies which ones. */
1882
1883 #define MAY_USE_WITH_COLORS_P(tty, ATTR) \
1884 (tty->TN_max_colors > 0 \
1885 ? (tty->TN_no_color_video & (ATTR)) == 0 \
1886 : 1)
1887
1888 /* Turn appearances of face FACE_ID on tty frame F on.
1889 FACE_ID is a realized face ID number, in the face cache. */
1890
1891 static void
1892 turn_on_face (struct frame *f, int face_id)
1893 {
1894 struct face *face = FACE_FROM_ID (f, face_id);
1895 unsigned long fg = face->foreground;
1896 unsigned long bg = face->background;
1897 struct tty_display_info *tty = FRAME_TTY (f);
1898
1899 /* Use reverse video if the face specifies that.
1900 Do this first because TS_end_standout_mode may be the same
1901 as TS_exit_attribute_mode, which turns all appearances off. */
1902 if (MAY_USE_WITH_COLORS_P (tty, NC_REVERSE)
1903 && (inverse_video
1904 ? fg == FACE_TTY_DEFAULT_FG_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR
1905 : fg == FACE_TTY_DEFAULT_BG_COLOR || bg == FACE_TTY_DEFAULT_FG_COLOR))
1906 tty_toggle_highlight (tty);
1907
1908 if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
1909 OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
1910
1911 if (face->tty_italic_p && MAY_USE_WITH_COLORS_P (tty, NC_ITALIC))
1912 {
1913 if (tty->TS_enter_italic_mode)
1914 OUTPUT1 (tty, tty->TS_enter_italic_mode);
1915 else
1916 /* Italics mode is unavailable on many terminals. In that
1917 case, map slant to dimmed text; we want italic text to
1918 appear different and dimming is not otherwise used. */
1919 OUTPUT1 (tty, tty->TS_enter_dim_mode);
1920 }
1921
1922 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
1923 OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
1924
1925 if (tty->TN_max_colors > 0)
1926 {
1927 const char *ts;
1928 char *p;
1929
1930 ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
1931 if (face_tty_specified_color (fg) && ts)
1932 {
1933 p = tparam (ts, NULL, 0, fg, 0, 0, 0);
1934 OUTPUT (tty, p);
1935 xfree (p);
1936 }
1937
1938 ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
1939 if (face_tty_specified_color (bg) && ts)
1940 {
1941 p = tparam (ts, NULL, 0, bg, 0, 0, 0);
1942 OUTPUT (tty, p);
1943 xfree (p);
1944 }
1945 }
1946 }
1947
1948
1949 /* Turn off appearances of face FACE_ID on tty frame F. */
1950
1951 static void
1952 turn_off_face (struct frame *f, int face_id)
1953 {
1954 struct face *face = FACE_FROM_ID (f, face_id);
1955 struct tty_display_info *tty = FRAME_TTY (f);
1956
1957 if (tty->TS_exit_attribute_mode)
1958 {
1959 /* Capability "me" will turn off appearance modes double-bright,
1960 half-bright, reverse-video, standout, underline. It may or
1961 may not turn off alt-char-mode. */
1962 if (face->tty_bold_p
1963 || face->tty_italic_p
1964 || face->tty_reverse_p
1965 || face->tty_underline_p)
1966 {
1967 OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
1968 if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
1969 tty->standout_mode = 0;
1970 }
1971 }
1972 else
1973 {
1974 /* If we don't have "me" we can only have those appearances
1975 that have exit sequences defined. */
1976 if (face->tty_underline_p)
1977 OUTPUT_IF (tty, tty->TS_exit_underline_mode);
1978 }
1979
1980 /* Switch back to default colors. */
1981 if (tty->TN_max_colors > 0
1982 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
1983 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
1984 || (face->background != FACE_TTY_DEFAULT_COLOR
1985 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
1986 OUTPUT1_IF (tty, tty->TS_orig_pair);
1987 }
1988
1989
1990 /* Return true if the terminal on frame F supports all of the
1991 capabilities in CAPS simultaneously. */
1992
1993 bool
1994 tty_capable_p (struct tty_display_info *tty, unsigned int caps)
1995 {
1996 #define TTY_CAPABLE_P_TRY(tty, cap, TS, NC_bit) \
1997 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(tty, NC_bit))) \
1998 return 0;
1999
2000 TTY_CAPABLE_P_TRY (tty, TTY_CAP_INVERSE, tty->TS_standout_mode, NC_REVERSE);
2001 TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE, tty->TS_enter_underline_mode, NC_UNDERLINE);
2002 TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD, tty->TS_enter_bold_mode, NC_BOLD);
2003 TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM, tty->TS_enter_dim_mode, NC_DIM);
2004 TTY_CAPABLE_P_TRY (tty, TTY_CAP_ITALIC, tty->TS_enter_italic_mode, NC_ITALIC);
2005
2006 /* We can do it! */
2007 return 1;
2008 }
2009
2010 /* Return non-zero if the terminal is capable to display colors. */
2011
2012 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2013 0, 1, 0,
2014 doc: /* Return non-nil if the tty device TERMINAL can display colors.
2015
2016 TERMINAL can be a terminal object, a frame, or nil (meaning the
2017 selected frame's terminal). This function always returns nil if
2018 TERMINAL does not refer to a text terminal. */)
2019 (Lisp_Object terminal)
2020 {
2021 struct terminal *t = decode_tty_terminal (terminal);
2022
2023 return (t && t->display_info.tty->TN_max_colors > 0) ? Qt : Qnil;
2024 }
2025
2026 /* Return the number of supported colors. */
2027 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2028 Stty_display_color_cells, 0, 1, 0,
2029 doc: /* Return the number of colors supported by the tty device TERMINAL.
2030
2031 TERMINAL can be a terminal object, a frame, or nil (meaning the
2032 selected frame's terminal). This function always returns 0 if
2033 TERMINAL does not refer to a text terminal. */)
2034 (Lisp_Object terminal)
2035 {
2036 struct terminal *t = decode_tty_terminal (terminal);
2037
2038 return make_number (t ? t->display_info.tty->TN_max_colors : 0);
2039 }
2040
2041 #ifndef DOS_NT
2042
2043 /* Declare here rather than in the function, as in the rest of Emacs,
2044 to work around an HPUX compiler bug (?). See
2045 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2046 static int default_max_colors;
2047 static int default_max_pairs;
2048 static int default_no_color_video;
2049 static char *default_orig_pair;
2050 static char *default_set_foreground;
2051 static char *default_set_background;
2052
2053 /* Save or restore the default color-related capabilities of this
2054 terminal. */
2055 static void
2056 tty_default_color_capabilities (struct tty_display_info *tty, bool save)
2057 {
2058
2059 if (save)
2060 {
2061 dupstring (&default_orig_pair, tty->TS_orig_pair);
2062 dupstring (&default_set_foreground, tty->TS_set_foreground);
2063 dupstring (&default_set_background, tty->TS_set_background);
2064 default_max_colors = tty->TN_max_colors;
2065 default_max_pairs = tty->TN_max_pairs;
2066 default_no_color_video = tty->TN_no_color_video;
2067 }
2068 else
2069 {
2070 tty->TS_orig_pair = default_orig_pair;
2071 tty->TS_set_foreground = default_set_foreground;
2072 tty->TS_set_background = default_set_background;
2073 tty->TN_max_colors = default_max_colors;
2074 tty->TN_max_pairs = default_max_pairs;
2075 tty->TN_no_color_video = default_no_color_video;
2076 }
2077 }
2078
2079 /* Setup one of the standard tty color schemes according to MODE.
2080 MODE's value is generally the number of colors which we want to
2081 support; zero means set up for the default capabilities, the ones
2082 we saw at init_tty time; -1 means turn off color support. */
2083 static void
2084 tty_setup_colors (struct tty_display_info *tty, int mode)
2085 {
2086 /* Canonicalize all negative values of MODE. */
2087 if (mode < -1)
2088 mode = -1;
2089
2090 switch (mode)
2091 {
2092 case -1: /* no colors at all */
2093 tty->TN_max_colors = 0;
2094 tty->TN_max_pairs = 0;
2095 tty->TN_no_color_video = 0;
2096 tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
2097 break;
2098 case 0: /* default colors, if any */
2099 default:
2100 tty_default_color_capabilities (tty, 0);
2101 break;
2102 case 8: /* 8 standard ANSI colors */
2103 tty->TS_orig_pair = "\033[0m";
2104 #ifdef TERMINFO
2105 tty->TS_set_foreground = "\033[3%p1%dm";
2106 tty->TS_set_background = "\033[4%p1%dm";
2107 #else
2108 tty->TS_set_foreground = "\033[3%dm";
2109 tty->TS_set_background = "\033[4%dm";
2110 #endif
2111 tty->TN_max_colors = 8;
2112 tty->TN_max_pairs = 64;
2113 tty->TN_no_color_video = 0;
2114 break;
2115 }
2116 }
2117
2118 void
2119 set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
2120 {
2121 Lisp_Object tem, val;
2122 Lisp_Object color_mode;
2123 int mode;
2124 Lisp_Object tty_color_mode_alist
2125 = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
2126
2127 tem = assq_no_quit (Qtty_color_mode, f->param_alist);
2128 val = CONSP (tem) ? XCDR (tem) : Qnil;
2129
2130 if (INTEGERP (val))
2131 color_mode = val;
2132 else if (SYMBOLP (tty_color_mode_alist))
2133 {
2134 tem = Fassq (val, Fsymbol_value (tty_color_mode_alist));
2135 color_mode = CONSP (tem) ? XCDR (tem) : Qnil;
2136 }
2137 else
2138 color_mode = Qnil;
2139
2140 mode = TYPE_RANGED_INTEGERP (int, color_mode) ? XINT (color_mode) : 0;
2141
2142 if (mode != tty->previous_color_mode)
2143 {
2144 tty->previous_color_mode = mode;
2145 tty_setup_colors (tty , mode);
2146 /* This recomputes all the faces given the new color definitions. */
2147 safe_call (1, intern ("tty-set-up-initial-frame-faces"));
2148 }
2149 }
2150
2151 #endif /* !DOS_NT */
2152
2153 DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
2154 doc: /* Return the type of the tty device that TERMINAL uses.
2155 Returns nil if TERMINAL is not on a tty device.
2156
2157 TERMINAL can be a terminal object, a frame, or nil (meaning the
2158 selected frame's terminal). */)
2159 (Lisp_Object terminal)
2160 {
2161 struct terminal *t = decode_tty_terminal (terminal);
2162
2163 return (t && t->display_info.tty->type
2164 ? build_string (t->display_info.tty->type) : Qnil);
2165 }
2166
2167 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
2168 doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
2169
2170 TERMINAL can be a terminal object, a frame, or nil (meaning the
2171 selected frame's terminal). This function always returns nil if
2172 TERMINAL is not on a tty device. */)
2173 (Lisp_Object terminal)
2174 {
2175 struct terminal *t = decode_tty_terminal (terminal);
2176
2177 return (t && !strcmp (t->display_info.tty->name, DEV_TTY) ? Qt : Qnil);
2178 }
2179
2180 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 1, 0,
2181 doc: /* Declare that the tty used by TERMINAL does not handle underlining.
2182 This is used to override the terminfo data, for certain terminals that
2183 do not really do underlining, but say that they do. This function has
2184 no effect if used on a non-tty terminal.
2185
2186 TERMINAL can be a terminal object, a frame or nil (meaning the
2187 selected frame's terminal). This function always returns nil if
2188 TERMINAL does not refer to a text terminal. */)
2189 (Lisp_Object terminal)
2190 {
2191 struct terminal *t = decode_live_terminal (terminal);
2192
2193 if (t->type == output_termcap)
2194 t->display_info.tty->TS_enter_underline_mode = 0;
2195 return Qnil;
2196 }
2197
2198 DEFUN ("tty-top-frame", Ftty_top_frame, Stty_top_frame, 0, 1, 0,
2199 doc: /* Return the topmost terminal frame on TERMINAL.
2200 TERMINAL can be a terminal object, a frame or nil (meaning the
2201 selected frame's terminal). This function returns nil if TERMINAL
2202 does not refer to a text terminal. Otherwise, it returns the
2203 top-most frame on the text terminal. */)
2204 (Lisp_Object terminal)
2205 {
2206 struct terminal *t = decode_live_terminal (terminal);
2207
2208 if (t->type == output_termcap)
2209 return t->display_info.tty->top_frame;
2210 return Qnil;
2211 }
2212
2213 \f
2214
2215 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
2216 doc: /* Suspend the terminal device TTY.
2217
2218 The device is restored to its default state, and Emacs ceases all
2219 access to the tty device. Frames that use the device are not deleted,
2220 but input is not read from them and if they change, their display is
2221 not updated.
2222
2223 TTY may be a terminal object, a frame, or nil for the terminal device
2224 of the currently selected frame.
2225
2226 This function runs `suspend-tty-functions' after suspending the
2227 device. The functions are run with one arg, the id of the suspended
2228 terminal device.
2229
2230 `suspend-tty' does nothing if it is called on a device that is already
2231 suspended.
2232
2233 A suspended tty may be resumed by calling `resume-tty' on it. */)
2234 (Lisp_Object tty)
2235 {
2236 struct terminal *t = decode_tty_terminal (tty);
2237 FILE *f;
2238
2239 if (!t)
2240 error ("Attempt to suspend a non-text terminal device");
2241
2242 f = t->display_info.tty->input;
2243
2244 if (f)
2245 {
2246 /* First run `suspend-tty-functions' and then clean up the tty
2247 state because `suspend-tty-functions' might need to change
2248 the tty state. */
2249 Lisp_Object term;
2250 XSETTERMINAL (term, t);
2251 CALLN (Frun_hook_with_args, intern ("suspend-tty-functions"), term);
2252
2253 reset_sys_modes (t->display_info.tty);
2254 delete_keyboard_wait_descriptor (fileno (f));
2255
2256 #ifndef MSDOS
2257 fclose (f);
2258 if (f != t->display_info.tty->output)
2259 fclose (t->display_info.tty->output);
2260 #endif
2261
2262 t->display_info.tty->input = 0;
2263 t->display_info.tty->output = 0;
2264
2265 if (FRAMEP (t->display_info.tty->top_frame))
2266 SET_FRAME_VISIBLE (XFRAME (t->display_info.tty->top_frame), 0);
2267
2268 }
2269
2270 /* Clear display hooks to prevent further output. */
2271 clear_tty_hooks (t);
2272
2273 return Qnil;
2274 }
2275
2276 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
2277 doc: /* Resume the previously suspended terminal device TTY.
2278 The terminal is opened and reinitialized. Frames that are on the
2279 suspended terminal are revived.
2280
2281 It is an error to resume a terminal while another terminal is active
2282 on the same device.
2283
2284 This function runs `resume-tty-functions' after resuming the terminal.
2285 The functions are run with one arg, the id of the resumed terminal
2286 device.
2287
2288 `resume-tty' does nothing if it is called on a device that is not
2289 suspended.
2290
2291 TTY may be a terminal object, a frame, or nil (meaning the selected
2292 frame's terminal). */)
2293 (Lisp_Object tty)
2294 {
2295 struct terminal *t = decode_tty_terminal (tty);
2296 int fd;
2297
2298 if (!t)
2299 error ("Attempt to resume a non-text terminal device");
2300
2301 if (!t->display_info.tty->input)
2302 {
2303 if (get_named_terminal (t->display_info.tty->name))
2304 error ("Cannot resume display while another display is active on the same device");
2305
2306 #ifdef MSDOS
2307 t->display_info.tty->output = stdout;
2308 t->display_info.tty->input = stdin;
2309 #else /* !MSDOS */
2310 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2311 t->display_info.tty->input = t->display_info.tty->output
2312 = fd < 0 ? 0 : fdopen (fd, "w+");
2313
2314 if (! t->display_info.tty->input)
2315 {
2316 int open_errno = errno;
2317 emacs_close (fd);
2318 report_file_errno ("Cannot reopen tty device",
2319 build_string (t->display_info.tty->name),
2320 open_errno);
2321 }
2322
2323 if (!O_IGNORE_CTTY && strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2324 dissociate_if_controlling_tty (fd);
2325 #endif
2326
2327 add_keyboard_wait_descriptor (fd);
2328
2329 if (FRAMEP (t->display_info.tty->top_frame))
2330 {
2331 struct frame *f = XFRAME (t->display_info.tty->top_frame);
2332 int width, height;
2333 int old_height = FRAME_COLS (f);
2334 int old_width = FRAME_TOTAL_LINES (f);
2335
2336 /* Check if terminal/window size has changed while the frame
2337 was suspended. */
2338 get_tty_size (fileno (t->display_info.tty->input), &width, &height);
2339 if (width != old_width || height != old_height)
2340 change_frame_size (f, width, height - FRAME_MENU_BAR_LINES (f),
2341 0, 0, 0, 0);
2342 SET_FRAME_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
2343 }
2344
2345 set_tty_hooks (t);
2346 init_sys_modes (t->display_info.tty);
2347
2348 /* Run `resume-tty-functions'. */
2349 Lisp_Object term;
2350 XSETTERMINAL (term, t);
2351 CALLN (Frun_hook_with_args, intern ("resume-tty-functions"), term);
2352 }
2353
2354 set_tty_hooks (t);
2355
2356 return Qnil;
2357 }
2358
2359 \f
2360 /***********************************************************************
2361 Mouse
2362 ***********************************************************************/
2363
2364 #ifdef HAVE_GPM
2365
2366 #ifndef HAVE_WINDOW_SYSTEM
2367 void
2368 term_mouse_moveto (int x, int y)
2369 {
2370 /* TODO: how to set mouse position?
2371 const char *name;
2372 int fd;
2373 name = (const char *) ttyname (0);
2374 fd = emacs_open (name, O_WRONLY, 0);
2375 SOME_FUNCTION (x, y, fd);
2376 emacs_close (fd);
2377 last_mouse_x = x;
2378 last_mouse_y = y; */
2379 }
2380 #endif /* HAVE_WINDOW_SYSTEM */
2381
2382 /* Implementation of draw_row_with_mouse_face for TTY/GPM. */
2383 void
2384 tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
2385 int start_hpos, int end_hpos,
2386 enum draw_glyphs_face draw)
2387 {
2388 int nglyphs = end_hpos - start_hpos;
2389 struct frame *f = XFRAME (WINDOW_FRAME (w));
2390 struct tty_display_info *tty = FRAME_TTY (f);
2391 int face_id = tty->mouse_highlight.mouse_face_face_id;
2392 int save_x, save_y, pos_x, pos_y;
2393
2394 if (end_hpos >= row->used[TEXT_AREA])
2395 nglyphs = row->used[TEXT_AREA] - start_hpos;
2396
2397 pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
2398 pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos + WINDOW_LEFT_EDGE_X (w);
2399
2400 /* Save current cursor co-ordinates. */
2401 save_y = curY (tty);
2402 save_x = curX (tty);
2403 cursor_to (f, pos_y, pos_x);
2404
2405 if (draw == DRAW_MOUSE_FACE)
2406 tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
2407 nglyphs, face_id);
2408 else if (draw == DRAW_NORMAL_TEXT)
2409 write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
2410
2411 cursor_to (f, save_y, save_x);
2412 }
2413
2414 static bool
2415 term_mouse_movement (struct frame *frame, Gpm_Event *event)
2416 {
2417 /* Has the mouse moved off the glyph it was on at the last sighting? */
2418 if (event->x != last_mouse_x || event->y != last_mouse_y)
2419 {
2420 frame->mouse_moved = 1;
2421 note_mouse_highlight (frame, event->x, event->y);
2422 /* Remember which glyph we're now on. */
2423 last_mouse_x = event->x;
2424 last_mouse_y = event->y;
2425 return 1;
2426 }
2427 return 0;
2428 }
2429
2430 /* Return the Time that corresponds to T. Wrap around on overflow. */
2431 static Time
2432 timeval_to_Time (struct timeval const *t)
2433 {
2434 Time s_1000, ms;
2435
2436 s_1000 = t->tv_sec;
2437 s_1000 *= 1000;
2438 ms = t->tv_usec / 1000;
2439 return s_1000 + ms;
2440 }
2441
2442 /* Return the current position of the mouse.
2443
2444 Set *f to the frame the mouse is in, or zero if the mouse is in no
2445 Emacs frame. If it is set to zero, all the other arguments are
2446 garbage.
2447
2448 Set *bar_window to Qnil, and *x and *y to the column and
2449 row of the character cell the mouse is over.
2450
2451 Set *timeptr to the time the mouse was at the returned position.
2452
2453 This clears mouse_moved until the next motion
2454 event arrives. */
2455 static void
2456 term_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
2457 enum scroll_bar_part *part, Lisp_Object *x,
2458 Lisp_Object *y, Time *timeptr)
2459 {
2460 struct timeval now;
2461
2462 *fp = SELECTED_FRAME ();
2463 (*fp)->mouse_moved = 0;
2464
2465 *bar_window = Qnil;
2466 *part = scroll_bar_above_handle;
2467
2468 XSETINT (*x, last_mouse_x);
2469 XSETINT (*y, last_mouse_y);
2470 gettimeofday(&now, 0);
2471 *timeptr = timeval_to_Time (&now);
2472 }
2473
2474 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2475
2476 If the event is a button press, then note that we have grabbed
2477 the mouse. */
2478
2479 static Lisp_Object
2480 term_mouse_click (struct input_event *result, Gpm_Event *event,
2481 struct frame *f)
2482 {
2483 struct timeval now;
2484 int i, j;
2485
2486 result->kind = GPM_CLICK_EVENT;
2487 for (i = 0, j = GPM_B_LEFT; i < 3; i++, j >>= 1 )
2488 {
2489 if (event->buttons & j) {
2490 result->code = i; /* button number */
2491 break;
2492 }
2493 }
2494 gettimeofday(&now, 0);
2495 result->timestamp = timeval_to_Time (&now);
2496
2497 if (event->type & GPM_UP)
2498 result->modifiers = up_modifier;
2499 else if (event->type & GPM_DOWN)
2500 result->modifiers = down_modifier;
2501 else
2502 result->modifiers = 0;
2503
2504 if (event->type & GPM_SINGLE)
2505 result->modifiers |= click_modifier;
2506
2507 if (event->type & GPM_DOUBLE)
2508 result->modifiers |= double_modifier;
2509
2510 if (event->type & GPM_TRIPLE)
2511 result->modifiers |= triple_modifier;
2512
2513 if (event->type & GPM_DRAG)
2514 result->modifiers |= drag_modifier;
2515
2516 if (!(event->type & (GPM_MOVE | GPM_DRAG))) {
2517
2518 /* 1 << KG_SHIFT */
2519 if (event->modifiers & (1 << 0))
2520 result->modifiers |= shift_modifier;
2521
2522 /* 1 << KG_CTRL */
2523 if (event->modifiers & (1 << 2))
2524 result->modifiers |= ctrl_modifier;
2525
2526 /* 1 << KG_ALT || KG_ALTGR */
2527 if (event->modifiers & (1 << 3)
2528 || event->modifiers & (1 << 1))
2529 result->modifiers |= meta_modifier;
2530 }
2531
2532 XSETINT (result->x, event->x);
2533 XSETINT (result->y, event->y);
2534 XSETFRAME (result->frame_or_window, f);
2535 result->arg = Qnil;
2536 return Qnil;
2537 }
2538
2539 int
2540 handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct input_event* hold_quit)
2541 {
2542 struct frame *f = XFRAME (tty->top_frame);
2543 struct input_event ie;
2544 bool do_help = 0;
2545 int count = 0;
2546
2547 EVENT_INIT (ie);
2548 ie.kind = NO_EVENT;
2549 ie.arg = Qnil;
2550
2551 if (event->type & (GPM_MOVE | GPM_DRAG)) {
2552 previous_help_echo_string = help_echo_string;
2553 help_echo_string = Qnil;
2554
2555 Gpm_DrawPointer (event->x, event->y, fileno (tty->output));
2556
2557 if (!term_mouse_movement (f, event))
2558 help_echo_string = previous_help_echo_string;
2559
2560 /* If the contents of the global variable help_echo_string
2561 has changed, generate a HELP_EVENT. */
2562 if (!NILP (help_echo_string)
2563 || !NILP (previous_help_echo_string))
2564 do_help = 1;
2565
2566 goto done;
2567 }
2568 else {
2569 f->mouse_moved = 0;
2570 term_mouse_click (&ie, event, f);
2571 }
2572
2573 done:
2574 if (ie.kind != NO_EVENT)
2575 {
2576 kbd_buffer_store_event_hold (&ie, hold_quit);
2577 count++;
2578 }
2579
2580 if (do_help
2581 && !(hold_quit && hold_quit->kind != NO_EVENT))
2582 {
2583 Lisp_Object frame;
2584
2585 if (f)
2586 XSETFRAME (frame, f);
2587 else
2588 frame = Qnil;
2589
2590 gen_help_event (help_echo_string, frame, help_echo_window,
2591 help_echo_object, help_echo_pos);
2592 count++;
2593 }
2594
2595 return count;
2596 }
2597
2598 DEFUN ("gpm-mouse-start", Fgpm_mouse_start, Sgpm_mouse_start,
2599 0, 0, 0,
2600 doc: /* Open a connection to Gpm.
2601 Gpm-mouse can only be activated for one tty at a time. */)
2602 (void)
2603 {
2604 struct frame *f = SELECTED_FRAME ();
2605 struct tty_display_info *tty
2606 = ((f)->output_method == output_termcap
2607 ? (f)->terminal->display_info.tty : NULL);
2608 Gpm_Connect connection;
2609
2610 if (!tty)
2611 error ("Gpm-mouse only works in the GNU/Linux console");
2612 if (gpm_tty == tty)
2613 return Qnil; /* Already activated, nothing to do. */
2614 if (gpm_tty)
2615 error ("Gpm-mouse can only be activated for one tty at a time");
2616
2617 connection.eventMask = ~0;
2618 connection.defaultMask = ~GPM_HARD;
2619 connection.maxMod = ~0;
2620 connection.minMod = 0;
2621 gpm_zerobased = 1;
2622
2623 if (Gpm_Open (&connection, 0) < 0)
2624 error ("Gpm-mouse failed to connect to the gpm daemon");
2625 else
2626 {
2627 gpm_tty = tty;
2628 /* `init_sys_modes' arranges for mouse movements sent through gpm_fd
2629 to generate SIGIOs. Apparently we need to call reset_sys_modes
2630 before calling init_sys_modes. */
2631 reset_sys_modes (tty);
2632 init_sys_modes (tty);
2633 add_gpm_wait_descriptor (gpm_fd);
2634 return Qnil;
2635 }
2636 }
2637
2638 void
2639 close_gpm (int fd)
2640 {
2641 if (fd >= 0)
2642 delete_gpm_wait_descriptor (fd);
2643 while (Gpm_Close()); /* close all the stack */
2644 gpm_tty = NULL;
2645 }
2646
2647 DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
2648 0, 0, 0,
2649 doc: /* Close a connection to Gpm. */)
2650 (void)
2651 {
2652 struct frame *f = SELECTED_FRAME ();
2653 struct tty_display_info *tty
2654 = ((f)->output_method == output_termcap
2655 ? (f)->terminal->display_info.tty : NULL);
2656
2657 if (!tty || gpm_tty != tty)
2658 return Qnil; /* Not activated on this terminal, nothing to do. */
2659
2660 close_gpm (gpm_fd);
2661 return Qnil;
2662 }
2663 #endif /* HAVE_GPM */
2664
2665 \f
2666 /***********************************************************************
2667 Menus
2668 ***********************************************************************/
2669
2670 #if !defined (MSDOS)
2671
2672 /* TTY menu implementation and main ideas are borrowed from msdos.c.
2673
2674 However, unlike on MSDOS, where the menu text is drawn directly to
2675 the display video memory, on a TTY we use display_string (see
2676 display_tty_menu_item in xdisp.c) to put the glyphs produced from
2677 the menu items into the frame's 'desired_matrix' glyph matrix, and
2678 then call update_frame_with_menu to deliver the results to the
2679 glass. The previous contents of the screen, in the form of the
2680 current_matrix, is stashed away, and used to restore screen
2681 contents when the menu selection changes or when the final
2682 selection is made and the menu should be popped down.
2683
2684 The idea of this implementation was suggested by Gerd Moellmann. */
2685
2686 #define TTYM_FAILURE -1
2687 #define TTYM_SUCCESS 1
2688 #define TTYM_NO_SELECT 2
2689 #define TTYM_IA_SELECT 3
2690 #define TTYM_NEXT 4
2691 #define TTYM_PREV 5
2692
2693 /* These hold text of the current and the previous menu help messages. */
2694 static const char *menu_help_message, *prev_menu_help_message;
2695 /* Pane number and item number of the menu item which generated the
2696 last menu help message. */
2697 static int menu_help_paneno, menu_help_itemno;
2698
2699 typedef struct tty_menu_struct
2700 {
2701 int count;
2702 char **text;
2703 struct tty_menu_struct **submenu;
2704 int *panenumber; /* Also used as enabled flag. */
2705 ptrdiff_t allocated;
2706 int panecount;
2707 int width;
2708 const char **help_text;
2709 } tty_menu;
2710
2711 /* Create a brand new menu structure. */
2712
2713 static tty_menu *
2714 tty_menu_create (void)
2715 {
2716 return xzalloc (sizeof *tty_menu_create ());
2717 }
2718
2719 /* Allocate some (more) memory for MENU ensuring that there is room for one
2720 more item. */
2721
2722 static void
2723 tty_menu_make_room (tty_menu *menu)
2724 {
2725 if (menu->allocated == menu->count)
2726 {
2727 ptrdiff_t allocated = menu->allocated;
2728 menu->text = xpalloc (menu->text, &allocated, 1, -1, sizeof *menu->text);
2729 menu->text = xrealloc (menu->text, allocated * sizeof *menu->text);
2730 menu->submenu = xrealloc (menu->submenu,
2731 allocated * sizeof *menu->submenu);
2732 menu->panenumber = xrealloc (menu->panenumber,
2733 allocated * sizeof *menu->panenumber);
2734 menu->help_text = xrealloc (menu->help_text,
2735 allocated * sizeof *menu->help_text);
2736 menu->allocated = allocated;
2737 }
2738 }
2739
2740 /* Search the given menu structure for a given pane number. */
2741
2742 static tty_menu *
2743 tty_menu_search_pane (tty_menu *menu, int pane)
2744 {
2745 int i;
2746 tty_menu *try;
2747
2748 for (i = 0; i < menu->count; i++)
2749 if (menu->submenu[i])
2750 {
2751 if (pane == menu->panenumber[i])
2752 return menu->submenu[i];
2753 try = tty_menu_search_pane (menu->submenu[i], pane);
2754 if (try)
2755 return try;
2756 }
2757 return (tty_menu *) 0;
2758 }
2759
2760 /* Determine how much screen space a given menu needs. */
2761
2762 static void
2763 tty_menu_calc_size (tty_menu *menu, int *width, int *height)
2764 {
2765 int i, h2, w2, maxsubwidth, maxheight;
2766
2767 maxsubwidth = menu->width;
2768 maxheight = menu->count;
2769 for (i = 0; i < menu->count; i++)
2770 {
2771 if (menu->submenu[i])
2772 {
2773 tty_menu_calc_size (menu->submenu[i], &w2, &h2);
2774 if (w2 > maxsubwidth) maxsubwidth = w2;
2775 if (i + h2 > maxheight) maxheight = i + h2;
2776 }
2777 }
2778 *width = maxsubwidth;
2779 *height = maxheight;
2780 }
2781
2782 static void
2783 mouse_get_xy (int *x, int *y)
2784 {
2785 struct frame *sf = SELECTED_FRAME ();
2786 Lisp_Object lmx = Qnil, lmy = Qnil, lisp_dummy;
2787 enum scroll_bar_part part_dummy;
2788 Time time_dummy;
2789
2790 if (FRAME_TERMINAL (sf)->mouse_position_hook)
2791 (*FRAME_TERMINAL (sf)->mouse_position_hook) (&sf, -1,
2792 &lisp_dummy, &part_dummy,
2793 &lmx, &lmy,
2794 &time_dummy);
2795 if (!NILP (lmx))
2796 {
2797 *x = XINT (lmx);
2798 *y = XINT (lmy);
2799 }
2800 }
2801
2802 /* Display MENU at (X,Y) using FACES, starting with FIRST_ITEM
2803 (zero-based). */
2804
2805 static void
2806 tty_menu_display (tty_menu *menu, int x, int y, int pn, int *faces,
2807 int mx, int my, int first_item, bool disp_help)
2808 {
2809 int i, face, width, enabled, mousehere, row, col;
2810 struct frame *sf = SELECTED_FRAME ();
2811 struct tty_display_info *tty = FRAME_TTY (sf);
2812 /* Don't try to display more menu items than the console can display
2813 using the available screen lines. Exclude the echo area line, as
2814 it will be overwritten by the help-echo anyway. */
2815 int max_items = min (menu->count - first_item, FRAME_TOTAL_LINES (sf) - 1 - y);
2816
2817 menu_help_message = NULL;
2818
2819 width = menu->width;
2820 col = cursorX (tty);
2821 row = cursorY (tty);
2822 for (i = 0; i < max_items; i++)
2823 {
2824 int max_width = width + 2; /* +2 for padding blanks on each side */
2825 int j = i + first_item;
2826
2827 if (menu->submenu[j])
2828 max_width += 2; /* for displaying " >" after the item */
2829 enabled
2830 = (!menu->submenu[j] && menu->panenumber[j]) || (menu->submenu[j]);
2831 mousehere = (y + i == my && x <= mx && mx < x + max_width);
2832 face = faces[enabled + mousehere * 2];
2833 /* Display the menu help string for the i-th menu item even if
2834 the menu item is currently disabled. That's what the GUI
2835 code does. */
2836 if (disp_help && enabled + mousehere * 2 >= 2)
2837 {
2838 menu_help_message = menu->help_text[j];
2839 menu_help_paneno = pn - 1;
2840 menu_help_itemno = j;
2841 }
2842 /* Take note of the coordinates of the active menu item, to
2843 display the cursor there. */
2844 if (mousehere)
2845 {
2846 row = y + i;
2847 col = x;
2848 }
2849 display_tty_menu_item (menu->text[j], max_width, face, x, y + i,
2850 menu->submenu[j] != NULL);
2851 }
2852 update_frame_with_menu (sf, row, col);
2853 }
2854
2855 /* --------------------------- X Menu emulation ---------------------- */
2856
2857 /* Create a new pane and place it on the outer-most level. */
2858
2859 static int
2860 tty_menu_add_pane (tty_menu *menu, const char *txt)
2861 {
2862 int len;
2863
2864 tty_menu_make_room (menu);
2865 menu->submenu[menu->count] = tty_menu_create ();
2866 menu->text[menu->count] = (char *)txt;
2867 menu->panenumber[menu->count] = ++menu->panecount;
2868 menu->help_text[menu->count] = NULL;
2869 menu->count++;
2870
2871 /* Update the menu width, if necessary. */
2872 len = menu_item_width ((const unsigned char *) txt);
2873 if (len > menu->width)
2874 menu->width = len;
2875
2876 return menu->panecount;
2877 }
2878
2879 /* Create a new item in a menu pane. */
2880
2881 static bool
2882 tty_menu_add_selection (tty_menu *menu, int pane,
2883 char *txt, bool enable, char const *help_text)
2884 {
2885 int len;
2886
2887 if (pane)
2888 {
2889 menu = tty_menu_search_pane (menu, pane);
2890 if (! menu)
2891 return 0;
2892 }
2893 tty_menu_make_room (menu);
2894 menu->submenu[menu->count] = (tty_menu *) 0;
2895 menu->text[menu->count] = txt;
2896 menu->panenumber[menu->count] = enable;
2897 menu->help_text[menu->count] = help_text;
2898 menu->count++;
2899
2900 /* Update the menu width, if necessary. */
2901 len = menu_item_width ((const unsigned char *) txt);
2902 if (len > menu->width)
2903 menu->width = len;
2904
2905 return 1;
2906 }
2907
2908 /* Decide where the menu would be placed if requested at (X,Y). */
2909
2910 static void
2911 tty_menu_locate (tty_menu *menu, int x, int y,
2912 int *ulx, int *uly, int *width, int *height)
2913 {
2914 tty_menu_calc_size (menu, width, height);
2915 *ulx = x + 1;
2916 *uly = y;
2917 *width += 2;
2918 }
2919
2920 struct tty_menu_state
2921 {
2922 struct glyph_matrix *screen_behind;
2923 tty_menu *menu;
2924 int pane;
2925 int x, y;
2926 };
2927
2928 /* Save away the contents of frame F's current frame matrix, and
2929 enable all its rows. Value is a glyph matrix holding the contents
2930 of F's current frame matrix with all its glyph rows enabled. */
2931
2932 static struct glyph_matrix *
2933 save_and_enable_current_matrix (struct frame *f)
2934 {
2935 int i;
2936 struct glyph_matrix *saved = xzalloc (sizeof *saved);
2937 saved->nrows = f->current_matrix->nrows;
2938 saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
2939
2940 for (i = 0; i < saved->nrows; ++i)
2941 {
2942 struct glyph_row *from = f->current_matrix->rows + i;
2943 struct glyph_row *to = saved->rows + i;
2944 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2945
2946 to->glyphs[TEXT_AREA] = xmalloc (nbytes);
2947 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2948 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2949 /* Make sure every row is enabled, or else update_frame will not
2950 redraw them. (Rows that are identical to what is already on
2951 screen will not be redrawn anyway.) */
2952 to->enabled_p = true;
2953 to->hash = from->hash;
2954 }
2955
2956 return saved;
2957 }
2958
2959 /* Restore the contents of frame F's desired frame matrix from SAVED,
2960 and free memory associated with SAVED. */
2961
2962 static void
2963 restore_desired_matrix (struct frame *f, struct glyph_matrix *saved)
2964 {
2965 int i;
2966
2967 for (i = 0; i < saved->nrows; ++i)
2968 {
2969 struct glyph_row *from = saved->rows + i;
2970 struct glyph_row *to = f->desired_matrix->rows + i;
2971 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2972
2973 eassert (to->glyphs[TEXT_AREA] != from->glyphs[TEXT_AREA]);
2974 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2975 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2976 to->enabled_p = from->enabled_p;
2977 to->hash = from->hash;
2978 }
2979 }
2980
2981 static void
2982 free_saved_screen (struct glyph_matrix *saved)
2983 {
2984 int i;
2985
2986 if (!saved)
2987 return; /* Already freed! */
2988
2989 for (i = 0; i < saved->nrows; ++i)
2990 {
2991 struct glyph_row *from = saved->rows + i;
2992
2993 xfree (from->glyphs[TEXT_AREA]);
2994 }
2995
2996 xfree (saved->rows);
2997 xfree (saved);
2998 }
2999
3000 /* Update the display of frame F from its saved contents. */
3001 static void
3002 screen_update (struct frame *f, struct glyph_matrix *mtx)
3003 {
3004 restore_desired_matrix (f, mtx);
3005 update_frame_with_menu (f, -1, -1);
3006 }
3007
3008 typedef enum {
3009 MI_QUIT_MENU = -1,
3010 MI_CONTINUE = 0,
3011 MI_ITEM_SELECTED = 1,
3012 MI_NEXT_ITEM = 2,
3013 MI_PREV_ITEM = 3,
3014 MI_SCROLL_FORWARD = 4,
3015 MI_SCROLL_BACK = 5
3016 } mi_result;
3017
3018 /* Read user input and return X and Y coordinates where that input
3019 puts us. We only consider mouse movement and click events, and
3020 keyboard movement commands; the rest are ignored. */
3021 static mi_result
3022 read_menu_input (struct frame *sf, int *x, int *y, int min_y, int max_y,
3023 bool *first_time)
3024 {
3025 if (*first_time)
3026 {
3027 *first_time = false;
3028 sf->mouse_moved = 1;
3029 }
3030 else
3031 {
3032 Lisp_Object cmd;
3033 bool usable_input = 1;
3034 mi_result st = MI_CONTINUE;
3035 struct tty_display_info *tty = FRAME_TTY (sf);
3036 Lisp_Object saved_mouse_tracking = do_mouse_tracking;
3037
3038 /* Signal the keyboard reading routines we are displaying a menu
3039 on this terminal. */
3040 tty->showing_menu = 1;
3041 /* We want mouse movements be reported by read_menu_command. */
3042 do_mouse_tracking = Qt;
3043 do {
3044 cmd = read_menu_command ();
3045 } while (NILP (cmd));
3046 tty->showing_menu = 0;
3047 do_mouse_tracking = saved_mouse_tracking;
3048
3049 if (EQ (cmd, Qt) || EQ (cmd, Qtty_menu_exit)
3050 /* If some input switched frames under our feet, exit the
3051 menu, since the menu faces are no longer valid, and the
3052 menu is no longer relevant anyway. */
3053 || sf != SELECTED_FRAME ())
3054 return MI_QUIT_MENU;
3055 if (EQ (cmd, Qtty_menu_mouse_movement))
3056 mouse_get_xy (x, y);
3057 else if (EQ (cmd, Qtty_menu_next_menu))
3058 {
3059 usable_input = 0;
3060 st = MI_NEXT_ITEM;
3061 }
3062 else if (EQ (cmd, Qtty_menu_prev_menu))
3063 {
3064 usable_input = 0;
3065 st = MI_PREV_ITEM;
3066 }
3067 else if (EQ (cmd, Qtty_menu_next_item))
3068 {
3069 if (*y < max_y)
3070 *y += 1;
3071 else
3072 st = MI_SCROLL_FORWARD;
3073 }
3074 else if (EQ (cmd, Qtty_menu_prev_item))
3075 {
3076 if (*y > min_y)
3077 *y -= 1;
3078 else
3079 st = MI_SCROLL_BACK;
3080 }
3081 else if (EQ (cmd, Qtty_menu_select))
3082 st = MI_ITEM_SELECTED;
3083 else if (!EQ (cmd, Qtty_menu_ignore))
3084 usable_input = 0;
3085 if (usable_input)
3086 sf->mouse_moved = 1;
3087 return st;
3088 }
3089 return MI_CONTINUE;
3090 }
3091
3092 /* Display menu, wait for user's response, and return that response. */
3093 static int
3094 tty_menu_activate (tty_menu *menu, int *pane, int *selidx,
3095 int x0, int y0, char **txt,
3096 void (*help_callback)(char const *, int, int),
3097 bool kbd_navigation)
3098 {
3099 struct tty_menu_state *state;
3100 int statecount, x, y, i;
3101 bool leave, onepane;
3102 int result IF_LINT (= 0);
3103 int title_faces[4]; /* Face to display the menu title. */
3104 int faces[4], buffers_num_deleted = 0;
3105 struct frame *sf = SELECTED_FRAME ();
3106 struct tty_display_info *tty = FRAME_TTY (sf);
3107 bool first_time;
3108 Lisp_Object selectface;
3109 int first_item = 0;
3110 int col, row;
3111 Lisp_Object prev_inhibit_redisplay = Vinhibit_redisplay;
3112 USE_SAFE_ALLOCA;
3113
3114 /* Don't allow non-positive x0 and y0, lest the menu will wrap
3115 around the display. */
3116 if (x0 <= 0)
3117 x0 = 1;
3118 if (y0 <= 0)
3119 y0 = 1;
3120
3121 SAFE_NALLOCA (state, 1, menu->panecount);
3122 memset (state, 0, sizeof (*state));
3123 faces[0]
3124 = lookup_derived_face (sf, intern ("tty-menu-disabled-face"),
3125 DEFAULT_FACE_ID, 1);
3126 faces[1]
3127 = lookup_derived_face (sf, intern ("tty-menu-enabled-face"),
3128 DEFAULT_FACE_ID, 1);
3129 selectface = intern ("tty-menu-selected-face");
3130 faces[2] = lookup_derived_face (sf, selectface,
3131 faces[0], 1);
3132 faces[3] = lookup_derived_face (sf, selectface,
3133 faces[1], 1);
3134
3135 /* Make sure the menu title is always displayed with
3136 `tty-menu-selected-face', no matter where the mouse pointer is. */
3137 for (i = 0; i < 4; i++)
3138 title_faces[i] = faces[3];
3139
3140 statecount = 1;
3141
3142 /* Don't let the title for the "Buffers" popup menu include a
3143 digit (which is ugly).
3144
3145 This is a terrible kludge, but I think the "Buffers" case is
3146 the only one where the title includes a number, so it doesn't
3147 seem to be necessary to make this more general. */
3148 if (strncmp (menu->text[0], "Buffers 1", 9) == 0)
3149 {
3150 menu->text[0][7] = '\0';
3151 buffers_num_deleted = 1;
3152 }
3153
3154 /* Inhibit redisplay for as long as the menu is active, to avoid
3155 messing the screen if some timer calls sit-for or a similar
3156 function. */
3157 Vinhibit_redisplay = Qt;
3158
3159 /* Force update of the current frame, so that the desired and the
3160 current matrices are identical. */
3161 update_frame_with_menu (sf, -1, -1);
3162 state[0].menu = menu;
3163 state[0].screen_behind = save_and_enable_current_matrix (sf);
3164
3165 /* Display the menu title. We subtract 1 from x0 and y0 because we
3166 want to interpret them as zero-based column and row coordinates,
3167 and also because we want the first item of the menu, not its
3168 title, to appear at x0,y0. */
3169 tty_menu_display (menu, x0 - 1, y0 - 1, 1, title_faces, x0 - 1, y0 - 1, 0, 0);
3170
3171 /* Turn off the cursor. Otherwise it shows through the menu
3172 panes, which is ugly. */
3173 col = cursorX (tty);
3174 row = cursorY (tty);
3175 tty_hide_cursor (tty);
3176
3177 if (buffers_num_deleted)
3178 menu->text[0][7] = ' ';
3179 onepane = menu->count == 1 && menu->submenu[0];
3180 if (onepane)
3181 {
3182 menu->width = menu->submenu[0]->width;
3183 state[0].menu = menu->submenu[0];
3184 }
3185 else
3186 {
3187 state[0].menu = menu;
3188 }
3189 state[0].x = x0 - 1;
3190 state[0].y = y0;
3191 state[0].pane = onepane;
3192
3193 x = state[0].x;
3194 y = state[0].y;
3195 first_time = true;
3196
3197 leave = 0;
3198 while (!leave)
3199 {
3200 mi_result input_status;
3201 int min_y = state[0].y;
3202 int max_y = min (min_y + state[0].menu->count, FRAME_TOTAL_LINES (sf) - 1) - 1;
3203
3204 input_status = read_menu_input (sf, &x, &y, min_y, max_y, &first_time);
3205 if (input_status)
3206 {
3207 leave = 1;
3208 switch (input_status)
3209 {
3210 case MI_QUIT_MENU:
3211 /* Remove the last help-echo, so that it doesn't
3212 re-appear after "Quit". */
3213 show_help_echo (Qnil, Qnil, Qnil, Qnil);
3214 result = TTYM_NO_SELECT;
3215 break;
3216 case MI_NEXT_ITEM:
3217 if (kbd_navigation)
3218 result = TTYM_NEXT;
3219 else
3220 leave = 0;
3221 break;
3222 case MI_PREV_ITEM:
3223 if (kbd_navigation)
3224 result = TTYM_PREV;
3225 else
3226 leave = 0;
3227 break;
3228 case MI_SCROLL_FORWARD:
3229 if (y - min_y == state[0].menu->count - 1 - first_item)
3230 {
3231 y = min_y;
3232 first_item = 0;
3233 }
3234 else
3235 first_item++;
3236 leave = 0;
3237 break;
3238 case MI_SCROLL_BACK:
3239 if (first_item == 0)
3240 {
3241 y = max_y;
3242 first_item = state[0].menu->count - 1 - (y - min_y);
3243 }
3244 else
3245 first_item--;
3246 leave = 0;
3247 break;
3248 default:
3249 /* MI_ITEM_SELECTED is handled below, so nothing to do. */
3250 break;
3251 }
3252 }
3253 if (sf->mouse_moved && input_status != MI_QUIT_MENU)
3254 {
3255 sf->mouse_moved = 0;
3256 result = TTYM_IA_SELECT;
3257 for (i = 0; i < statecount; i++)
3258 if (state[i].x <= x && x < state[i].x + state[i].menu->width + 2)
3259 {
3260 int dy = y - state[i].y + first_item;
3261 if (0 <= dy && dy < state[i].menu->count)
3262 {
3263 if (!state[i].menu->submenu[dy])
3264 {
3265 if (state[i].menu->panenumber[dy])
3266 result = TTYM_SUCCESS;
3267 else
3268 result = TTYM_IA_SELECT;
3269 }
3270 *pane = state[i].pane - 1;
3271 *selidx = dy;
3272 /* We hit some part of a menu, so drop extra menus that
3273 have been opened. That does not include an open and
3274 active submenu. */
3275 if (i != statecount - 2
3276 || state[i].menu->submenu[dy] != state[i + 1].menu)
3277 while (i != statecount - 1)
3278 {
3279 statecount--;
3280 screen_update (sf, state[statecount].screen_behind);
3281 state[statecount].screen_behind = NULL;
3282 }
3283 if (i == statecount - 1 && state[i].menu->submenu[dy])
3284 {
3285 tty_menu_display (state[i].menu,
3286 state[i].x,
3287 state[i].y,
3288 state[i].pane,
3289 faces, x, y, first_item, 1);
3290 state[statecount].menu = state[i].menu->submenu[dy];
3291 state[statecount].pane = state[i].menu->panenumber[dy];
3292 state[statecount].screen_behind
3293 = save_and_enable_current_matrix (sf);
3294 state[statecount].x
3295 = state[i].x + state[i].menu->width + 2;
3296 state[statecount].y = y;
3297 statecount++;
3298 }
3299 }
3300 }
3301 tty_menu_display (state[statecount - 1].menu,
3302 state[statecount - 1].x,
3303 state[statecount - 1].y,
3304 state[statecount - 1].pane,
3305 faces, x, y, first_item, 1);
3306 /* The call to display help-echo below will move the cursor,
3307 so remember its current position as computed by
3308 tty_menu_display. */
3309 col = cursorX (tty);
3310 row = cursorY (tty);
3311 }
3312
3313 /* Display the help-echo message for the currently-selected menu
3314 item. */
3315 if ((menu_help_message || prev_menu_help_message)
3316 && menu_help_message != prev_menu_help_message)
3317 {
3318 help_callback (menu_help_message,
3319 menu_help_paneno, menu_help_itemno);
3320 /* Move the cursor to the beginning of the current menu
3321 item, so that screen readers and other accessibility aids
3322 know where the active region is. */
3323 cursor_to (sf, row, col);
3324 prev_menu_help_message = menu_help_message;
3325 }
3326 /* Both tty_menu_display and help_callback invoke update_end,
3327 which calls tty_show_cursor. Re-hide it, so it doesn't show
3328 through the menus. */
3329 tty_hide_cursor (tty);
3330 fflush (tty->output);
3331 }
3332
3333 sf->mouse_moved = 0;
3334 screen_update (sf, state[0].screen_behind);
3335 while (statecount--)
3336 free_saved_screen (state[statecount].screen_behind);
3337 tty_show_cursor (tty); /* Turn cursor back on. */
3338 fflush (tty->output);
3339
3340 /* Clean up any mouse events that are waiting inside Emacs event queue.
3341 These events are likely to be generated before the menu was even
3342 displayed, probably because the user pressed and released the button
3343 (which invoked the menu) too quickly. If we don't remove these events,
3344 Emacs will process them after we return and surprise the user. */
3345 discard_mouse_events ();
3346 if (!kbd_buffer_events_waiting ())
3347 clear_input_pending ();
3348 SAFE_FREE ();
3349 Vinhibit_redisplay = prev_inhibit_redisplay;
3350 return result;
3351 }
3352
3353 /* Dispose of a menu. */
3354
3355 static void
3356 tty_menu_destroy (tty_menu *menu)
3357 {
3358 int i;
3359 if (menu->allocated)
3360 {
3361 for (i = 0; i < menu->count; i++)
3362 if (menu->submenu[i])
3363 tty_menu_destroy (menu->submenu[i]);
3364 xfree (menu->text);
3365 xfree (menu->submenu);
3366 xfree (menu->panenumber);
3367 xfree (menu->help_text);
3368 }
3369 xfree (menu);
3370 menu_help_message = prev_menu_help_message = NULL;
3371 }
3372
3373 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3374
3375 PANE is the pane number, and ITEM is the menu item number in
3376 the menu (currently not used). */
3377
3378 static void
3379 tty_menu_help_callback (char const *help_string, int pane, int item)
3380 {
3381 Lisp_Object *first_item;
3382 Lisp_Object pane_name;
3383 Lisp_Object menu_object;
3384
3385 first_item = XVECTOR (menu_items)->contents;
3386 if (EQ (first_item[0], Qt))
3387 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3388 else if (EQ (first_item[0], Qquote))
3389 /* This shouldn't happen, see xmenu_show. */
3390 pane_name = empty_unibyte_string;
3391 else
3392 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3393
3394 /* (menu-item MENU-NAME PANE-NUMBER) */
3395 menu_object = list3 (Qmenu_item, pane_name, make_number (pane));
3396 show_help_echo (help_string ? build_string (help_string) : Qnil,
3397 Qnil, menu_object, make_number (item));
3398 }
3399
3400 static void
3401 tty_pop_down_menu (Lisp_Object arg)
3402 {
3403 tty_menu *menu = XSAVE_POINTER (arg, 0);
3404 struct buffer *orig_buffer = XSAVE_POINTER (arg, 1);
3405
3406 block_input ();
3407 tty_menu_destroy (menu);
3408 set_buffer_internal (orig_buffer);
3409 unblock_input ();
3410 }
3411
3412 /* Return the zero-based index of the last menu-bar item on frame F. */
3413 static int
3414 tty_menu_last_menubar_item (struct frame *f)
3415 {
3416 int i = 0;
3417
3418 eassert (FRAME_TERMCAP_P (f) && FRAME_LIVE_P (f));
3419 if (FRAME_TERMCAP_P (f) && FRAME_LIVE_P (f))
3420 {
3421 Lisp_Object items = FRAME_MENU_BAR_ITEMS (f);
3422
3423 while (i < ASIZE (items))
3424 {
3425 Lisp_Object str;
3426
3427 str = AREF (items, i + 1);
3428 if (NILP (str))
3429 break;
3430 i += 4;
3431 }
3432 i -= 4; /* Went one too far! */
3433 }
3434 return i;
3435 }
3436
3437 /* Find in frame F's menu bar the menu item that is next or previous
3438 to the item at X/Y, and return that item's position in X/Y. WHICH
3439 says which one--next or previous--item to look for. X and Y are
3440 measured in character cells. This should only be called on TTY
3441 frames. */
3442 static void
3443 tty_menu_new_item_coords (struct frame *f, int which, int *x, int *y)
3444 {
3445 eassert (FRAME_TERMCAP_P (f) && FRAME_LIVE_P (f));
3446 if (FRAME_TERMCAP_P (f) && FRAME_LIVE_P (f))
3447 {
3448 Lisp_Object items = FRAME_MENU_BAR_ITEMS (f);
3449 int last_i = tty_menu_last_menubar_item (f);
3450 int i, prev_x;
3451
3452 /* This loop assumes a single menu-bar line, and will fail to
3453 find an item if it is not in the first line. Note that
3454 make_lispy_event in keyboard.c makes the same assumption. */
3455 for (i = 0, prev_x = -1; i < ASIZE (items); i += 4)
3456 {
3457 Lisp_Object pos, str;
3458 int ix;
3459
3460 str = AREF (items, i + 1);
3461 pos = AREF (items, i + 3);
3462 if (NILP (str))
3463 return;
3464 ix = XINT (pos);
3465 if (ix <= *x
3466 /* We use <= so the blank between 2 items on a TTY is
3467 considered part of the previous item. */
3468 && *x <= ix + menu_item_width (SDATA (str)))
3469 {
3470 /* Found current item. Now compute the X coordinate of
3471 the previous or next item. */
3472 if (which == TTYM_NEXT)
3473 {
3474 if (i < last_i)
3475 *x = XINT (AREF (items, i + 4 + 3));
3476 else
3477 *x = 0; /* Wrap around to the first item. */
3478 }
3479 else if (prev_x < 0)
3480 {
3481 /* Wrap around to the last item. */
3482 *x = XINT (AREF (items, last_i + 3));
3483 }
3484 else
3485 *x = prev_x;
3486 return;
3487 }
3488 prev_x = ix;
3489 }
3490 }
3491 }
3492
3493 /* WINDOWSNT uses this as menu_show_hook, see w32console.c. */
3494 Lisp_Object
3495 tty_menu_show (struct frame *f, int x, int y, int menuflags,
3496 Lisp_Object title, const char **error_name)
3497 {
3498 tty_menu *menu;
3499 int pane, selidx, lpane, status;
3500 Lisp_Object entry, pane_prefix;
3501 char *datap;
3502 int ulx, uly, width, height;
3503 int item_x, item_y;
3504 int dispwidth, dispheight;
3505 int i, j, lines, maxlines;
3506 int maxwidth;
3507 ptrdiff_t specpdl_count;
3508
3509 eassert (FRAME_TERMCAP_P (f));
3510
3511 *error_name = 0;
3512 if (menu_items_n_panes == 0)
3513 return Qnil;
3514
3515 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3516 {
3517 *error_name = "Empty menu";
3518 return Qnil;
3519 }
3520
3521 /* Make the menu on that window. */
3522 menu = tty_menu_create ();
3523
3524 /* Don't GC while we prepare and show the menu, because we give the
3525 menu functions pointers to the contents of strings. */
3526 specpdl_count = inhibit_garbage_collection ();
3527
3528 /* Avoid crashes if, e.g., another client will connect while we
3529 are in a menu. */
3530 temporarily_switch_to_single_kboard (f);
3531
3532 /* Adjust coordinates to be root-window-relative. */
3533 item_x = x += f->left_pos;
3534 item_y = y += f->top_pos;
3535
3536 /* Create all the necessary panes and their items. */
3537 USE_SAFE_ALLOCA;
3538 maxwidth = maxlines = lines = i = 0;
3539 lpane = TTYM_FAILURE;
3540 while (i < menu_items_used)
3541 {
3542 if (EQ (AREF (menu_items, i), Qt))
3543 {
3544 /* Create a new pane. */
3545 Lisp_Object pane_name, prefix;
3546 const char *pane_string;
3547
3548 maxlines = max (maxlines, lines);
3549 lines = 0;
3550 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
3551 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
3552 pane_string = (NILP (pane_name)
3553 ? "" : SSDATA (pane_name));
3554 if ((menuflags & MENU_KEYMAPS) && !NILP (prefix))
3555 pane_string++;
3556
3557 lpane = tty_menu_add_pane (menu, pane_string);
3558 if (lpane == TTYM_FAILURE)
3559 {
3560 tty_menu_destroy (menu);
3561 *error_name = "Can't create pane";
3562 entry = Qnil;
3563 goto tty_menu_end;
3564 }
3565 i += MENU_ITEMS_PANE_LENGTH;
3566
3567 /* Find the width of the widest item in this pane. */
3568 j = i;
3569 while (j < menu_items_used)
3570 {
3571 Lisp_Object item;
3572 item = AREF (menu_items, j);
3573 if (EQ (item, Qt))
3574 break;
3575 if (NILP (item))
3576 {
3577 j++;
3578 continue;
3579 }
3580 width = SBYTES (item);
3581 if (width > maxwidth)
3582 maxwidth = width;
3583
3584 j += MENU_ITEMS_ITEM_LENGTH;
3585 }
3586 }
3587 /* Ignore a nil in the item list.
3588 It's meaningful only for dialog boxes. */
3589 else if (EQ (AREF (menu_items, i), Qquote))
3590 i += 1;
3591 else
3592 {
3593 /* Create a new item within current pane. */
3594 Lisp_Object item_name, enable, descrip, help;
3595 char *item_data;
3596 char const *help_string;
3597
3598 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
3599 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
3600 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
3601 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3602 help_string = STRINGP (help) ? SSDATA (help) : NULL;
3603
3604 if (!NILP (descrip))
3605 {
3606 item_data = SAFE_ALLOCA (maxwidth + SBYTES (descrip) + 1);
3607 memcpy (item_data, SSDATA (item_name), SBYTES (item_name));
3608 for (j = SCHARS (item_name); j < maxwidth; j++)
3609 item_data[j] = ' ';
3610 memcpy (item_data + j, SSDATA (descrip), SBYTES (descrip));
3611 item_data[j + SBYTES (descrip)] = 0;
3612 }
3613 else
3614 item_data = SSDATA (item_name);
3615
3616 if (lpane == TTYM_FAILURE
3617 || (! tty_menu_add_selection (menu, lpane, item_data,
3618 !NILP (enable), help_string)))
3619 {
3620 tty_menu_destroy (menu);
3621 *error_name = "Can't add selection to menu";
3622 entry = Qnil;
3623 goto tty_menu_end;
3624 }
3625 i += MENU_ITEMS_ITEM_LENGTH;
3626 lines++;
3627 }
3628 }
3629
3630 maxlines = max (maxlines, lines);
3631
3632 /* All set and ready to fly. */
3633 dispwidth = f->text_cols;
3634 dispheight = f->text_lines;
3635 x = min (x, dispwidth);
3636 y = min (y, dispheight);
3637 x = max (x, 1);
3638 y = max (y, 1);
3639 tty_menu_locate (menu, x, y, &ulx, &uly, &width, &height);
3640 if (ulx + width > dispwidth)
3641 {
3642 x -= (ulx + width) - dispwidth;
3643 ulx = dispwidth - width;
3644 }
3645 if (uly + height > dispheight)
3646 {
3647 y -= (uly + height) - dispheight;
3648 uly = dispheight - height;
3649 }
3650
3651 if (FRAME_HAS_MINIBUF_P (f) && uly + height > dispheight - 2)
3652 {
3653 /* Move the menu away of the echo area, to avoid overwriting the
3654 menu with help echo messages or vice versa. */
3655 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
3656 {
3657 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window)) + 1;
3658 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window)) + 1;
3659 }
3660 else
3661 {
3662 y -= 2;
3663 uly -= 2;
3664 }
3665 }
3666
3667 if (ulx < 0) x -= ulx;
3668 if (uly < 0) y -= uly;
3669
3670 #if 0
3671 /* This code doesn't make sense on a TTY, since it can easily annul
3672 the adjustments above that carefully avoid truncation of the menu
3673 items. I think it was written to fix some problem that only
3674 happens on X11. */
3675 if (! for_click)
3676 {
3677 /* If position was not given by a mouse click, adjust so upper left
3678 corner of the menu as a whole ends up at given coordinates. This
3679 is what x-popup-menu says in its documentation. */
3680 x += width / 2;
3681 y += 1.5 * height / (maxlines + 2);
3682 }
3683 #endif
3684
3685 pane = selidx = 0;
3686
3687 /* We save and restore the current buffer because tty_menu_activate
3688 triggers redisplay, which switches buffers at will. */
3689 record_unwind_protect (tty_pop_down_menu,
3690 make_save_ptr_ptr (menu, current_buffer));
3691
3692 specbind (Qoverriding_terminal_local_map,
3693 Fsymbol_value (Qtty_menu_navigation_map));
3694 status = tty_menu_activate (menu, &pane, &selidx, x, y, &datap,
3695 tty_menu_help_callback,
3696 menuflags & MENU_KBD_NAVIGATION);
3697 entry = pane_prefix = Qnil;
3698
3699 switch (status)
3700 {
3701 case TTYM_SUCCESS:
3702 /* Find the item number SELIDX in pane number PANE. */
3703 i = 0;
3704 while (i < menu_items_used)
3705 {
3706 if (EQ (AREF (menu_items, i), Qt))
3707 {
3708 if (pane == 0)
3709 pane_prefix
3710 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
3711 pane--;
3712 i += MENU_ITEMS_PANE_LENGTH;
3713 }
3714 else
3715 {
3716 if (pane == -1)
3717 {
3718 if (selidx == 0)
3719 {
3720 entry
3721 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
3722 if (menuflags & MENU_KEYMAPS)
3723 {
3724 entry = Fcons (entry, Qnil);
3725 if (!NILP (pane_prefix))
3726 entry = Fcons (pane_prefix, entry);
3727 }
3728 break;
3729 }
3730 selidx--;
3731 }
3732 i += MENU_ITEMS_ITEM_LENGTH;
3733 }
3734 }
3735 break;
3736
3737 case TTYM_NEXT:
3738 case TTYM_PREV:
3739 tty_menu_new_item_coords (f, status, &item_x, &item_y);
3740 entry = Fcons (make_number (item_x), make_number (item_y));
3741 break;
3742
3743 case TTYM_FAILURE:
3744 *error_name = "Can't activate menu";
3745 case TTYM_IA_SELECT:
3746 break;
3747 case TTYM_NO_SELECT:
3748 /* If the selected frame was changed while we displayed a menu,
3749 throw to top level in order to undo any temporary settings
3750 made by TTY menu code. */
3751 if (f != SELECTED_FRAME ())
3752 Ftop_level ();
3753 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3754 the menu was invoked with a mouse event as POSITION). */
3755 if (!(menuflags & MENU_FOR_CLICK))
3756 Fsignal (Qquit, Qnil);
3757 break;
3758 }
3759
3760 tty_menu_end:
3761
3762 SAFE_FREE ();
3763 unbind_to (specpdl_count, Qnil);
3764 return entry;
3765 }
3766
3767 #endif /* !MSDOS */
3768
3769 \f
3770 #ifndef MSDOS
3771 /***********************************************************************
3772 Initialization
3773 ***********************************************************************/
3774
3775 /* Initialize the tty-dependent part of frame F. The frame must
3776 already have its device initialized. */
3777
3778 void
3779 create_tty_output (struct frame *f)
3780 {
3781 struct tty_output *t = xzalloc (sizeof *t);
3782
3783 eassert (FRAME_TERMCAP_P (f));
3784
3785 t->display_info = FRAME_TERMINAL (f)->display_info.tty;
3786
3787 f->output_data.tty = t;
3788 }
3789
3790 /* Delete frame F's face cache, and its tty-dependent part. */
3791
3792 static void
3793 tty_free_frame_resources (struct frame *f)
3794 {
3795 eassert (FRAME_TERMCAP_P (f));
3796 free_frame_faces (f);
3797 xfree (f->output_data.tty);
3798 }
3799
3800 #else /* MSDOS */
3801
3802 /* Delete frame F's face cache. */
3803
3804 static void
3805 tty_free_frame_resources (struct frame *f)
3806 {
3807 eassert (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
3808 free_frame_faces (f);
3809 }
3810 #endif /* MSDOS */
3811 \f
3812 /* Reset the hooks in TERMINAL. */
3813
3814 static void
3815 clear_tty_hooks (struct terminal *terminal)
3816 {
3817 terminal->rif = 0;
3818 terminal->cursor_to_hook = 0;
3819 terminal->raw_cursor_to_hook = 0;
3820 terminal->clear_to_end_hook = 0;
3821 terminal->clear_frame_hook = 0;
3822 terminal->clear_end_of_line_hook = 0;
3823 terminal->ins_del_lines_hook = 0;
3824 terminal->insert_glyphs_hook = 0;
3825 terminal->write_glyphs_hook = 0;
3826 terminal->delete_glyphs_hook = 0;
3827 terminal->ring_bell_hook = 0;
3828 terminal->reset_terminal_modes_hook = 0;
3829 terminal->set_terminal_modes_hook = 0;
3830 terminal->update_begin_hook = 0;
3831 terminal->update_end_hook = 0;
3832 terminal->set_terminal_window_hook = 0;
3833 terminal->mouse_position_hook = 0;
3834 terminal->frame_rehighlight_hook = 0;
3835 terminal->frame_raise_lower_hook = 0;
3836 terminal->fullscreen_hook = 0;
3837 terminal->menu_show_hook = 0;
3838 terminal->set_vertical_scroll_bar_hook = 0;
3839 terminal->set_horizontal_scroll_bar_hook = 0;
3840 terminal->condemn_scroll_bars_hook = 0;
3841 terminal->redeem_scroll_bar_hook = 0;
3842 terminal->judge_scroll_bars_hook = 0;
3843 terminal->read_socket_hook = 0;
3844 terminal->frame_up_to_date_hook = 0;
3845
3846 /* Leave these two set, or suspended frames are not deleted
3847 correctly. */
3848 terminal->delete_frame_hook = &tty_free_frame_resources;
3849 terminal->delete_terminal_hook = &delete_tty;
3850 }
3851
3852 /* Initialize hooks in TERMINAL with the values needed for a tty. */
3853
3854 static void
3855 set_tty_hooks (struct terminal *terminal)
3856 {
3857 terminal->cursor_to_hook = &tty_cursor_to;
3858 terminal->raw_cursor_to_hook = &tty_raw_cursor_to;
3859 terminal->clear_to_end_hook = &tty_clear_to_end;
3860 terminal->clear_frame_hook = &tty_clear_frame;
3861 terminal->clear_end_of_line_hook = &tty_clear_end_of_line;
3862 terminal->ins_del_lines_hook = &tty_ins_del_lines;
3863 terminal->insert_glyphs_hook = &tty_insert_glyphs;
3864 terminal->write_glyphs_hook = &tty_write_glyphs;
3865 terminal->delete_glyphs_hook = &tty_delete_glyphs;
3866 terminal->ring_bell_hook = &tty_ring_bell;
3867 terminal->reset_terminal_modes_hook = &tty_reset_terminal_modes;
3868 terminal->set_terminal_modes_hook = &tty_set_terminal_modes;
3869 terminal->update_end_hook = &tty_update_end;
3870 #ifdef MSDOS
3871 terminal->menu_show_hook = &x_menu_show;
3872 #else
3873 terminal->menu_show_hook = &tty_menu_show;
3874 #endif
3875 terminal->set_terminal_window_hook = &tty_set_terminal_window;
3876 terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */
3877 terminal->delete_frame_hook = &tty_free_frame_resources;
3878 terminal->delete_terminal_hook = &delete_tty;
3879 /* Other hooks are NULL by default. */
3880 }
3881
3882 /* If FD is the controlling terminal, drop it. */
3883 static void
3884 dissociate_if_controlling_tty (int fd)
3885 {
3886 /* If tcgetpgrp succeeds, fd is the controlling terminal,
3887 so dissociate it by invoking setsid. */
3888 if (tcgetpgrp (fd) >= 0 && setsid () < 0)
3889 {
3890 #ifdef TIOCNOTTY
3891 /* setsid failed, presumably because Emacs is already a process
3892 group leader. Fall back on the obsolescent way to dissociate
3893 a controlling tty. */
3894 sigset_t oldset;
3895 block_tty_out_signal (&oldset);
3896 ioctl (fd, TIOCNOTTY, 0);
3897 unblock_tty_out_signal (&oldset);
3898 #endif
3899 }
3900 }
3901
3902 /* Create a termcap display on the tty device with the given name and
3903 type.
3904
3905 If NAME is NULL, then use the controlling tty, i.e., "/dev/tty".
3906 Otherwise NAME should be a path to the tty device file,
3907 e.g. "/dev/pts/7".
3908
3909 TERMINAL_TYPE is the termcap type of the device, e.g. "vt100".
3910
3911 If MUST_SUCCEED is true, then all errors are fatal. */
3912
3913 struct terminal *
3914 init_tty (const char *name, const char *terminal_type, bool must_succeed)
3915 {
3916 char *area;
3917 char **address = &area;
3918 int status;
3919 struct tty_display_info *tty = NULL;
3920 struct terminal *terminal = NULL;
3921 sigset_t oldset;
3922 bool ctty = false; /* True if asked to open controlling tty. */
3923
3924 if (!terminal_type)
3925 maybe_fatal (must_succeed, 0,
3926 "Unknown terminal type",
3927 "Unknown terminal type");
3928
3929 if (name == NULL)
3930 name = DEV_TTY;
3931 if (!strcmp (name, DEV_TTY))
3932 ctty = 1;
3933
3934 /* If we already have a terminal on the given device, use that. If
3935 all such terminals are suspended, create a new one instead. */
3936 /* XXX Perhaps this should be made explicit by having init_tty
3937 always create a new terminal and separating terminal and frame
3938 creation on Lisp level. */
3939 terminal = get_named_terminal (name);
3940 if (terminal)
3941 return terminal;
3942
3943 terminal = create_terminal (output_termcap, NULL);
3944 #ifdef MSDOS
3945 if (been_here > 0)
3946 maybe_fatal (0, 0, "Attempt to create another terminal %s", "",
3947 name, "");
3948 been_here = 1;
3949 tty = &the_only_display_info;
3950 #else
3951 tty = xzalloc (sizeof *tty);
3952 #endif
3953 tty->top_frame = Qnil;
3954 tty->next = tty_list;
3955 tty_list = tty;
3956
3957 terminal->display_info.tty = tty;
3958 tty->terminal = terminal;
3959
3960 tty->Wcm = xmalloc (sizeof *tty->Wcm);
3961 Wcm_clear (tty);
3962
3963 encode_terminal_src_size = 0;
3964 encode_terminal_dst_size = 0;
3965
3966
3967 #ifndef DOS_NT
3968 set_tty_hooks (terminal);
3969
3970 {
3971 /* Open the terminal device. */
3972
3973 /* If !ctty, don't recognize it as our controlling terminal, and
3974 don't make it the controlling tty if we don't have one now.
3975
3976 Alas, O_IGNORE_CTTY is a GNU extension that seems to be only
3977 defined on Hurd. On other systems, we need to explicitly
3978 dissociate ourselves from the controlling tty when we want to
3979 open a frame on the same terminal. */
3980 int flags = O_RDWR | O_NOCTTY | (ctty ? 0 : O_IGNORE_CTTY);
3981 int fd = emacs_open (name, flags, 0);
3982 tty->input = tty->output
3983 = ((fd < 0 || ! isatty (fd))
3984 ? NULL
3985 : fdopen (fd, "w+"));
3986
3987 if (! tty->input)
3988 {
3989 char const *diagnostic
3990 = (fd < 0) ? "Could not open file: %s" : "Not a tty device: %s";
3991 emacs_close (fd);
3992 maybe_fatal (must_succeed, terminal, diagnostic, diagnostic, name);
3993 }
3994
3995 tty->name = xstrdup (name);
3996 terminal->name = xstrdup (name);
3997
3998 if (!O_IGNORE_CTTY && !ctty)
3999 dissociate_if_controlling_tty (fd);
4000 }
4001
4002 tty->type = xstrdup (terminal_type);
4003
4004 add_keyboard_wait_descriptor (fileno (tty->input));
4005
4006 Wcm_clear (tty);
4007
4008 /* On some systems, tgetent tries to access the controlling
4009 terminal. */
4010 block_tty_out_signal (&oldset);
4011 status = tgetent (tty->termcap_term_buffer, terminal_type);
4012 if (tty->termcap_term_buffer[TERMCAP_BUFFER_SIZE - 1])
4013 emacs_abort ();
4014 unblock_tty_out_signal (&oldset);
4015
4016 if (status < 0)
4017 {
4018 #ifdef TERMINFO
4019 maybe_fatal (must_succeed, terminal,
4020 "Cannot open terminfo database file",
4021 "Cannot open terminfo database file");
4022 #else
4023 maybe_fatal (must_succeed, terminal,
4024 "Cannot open termcap database file",
4025 "Cannot open termcap database file");
4026 #endif
4027 }
4028 if (status == 0)
4029 {
4030 maybe_fatal (must_succeed, terminal,
4031 "Terminal type %s is not defined",
4032 "Terminal type %s is not defined.\n\
4033 If that is not the actual type of terminal you have,\n\
4034 use the Bourne shell command 'TERM=...; export TERM' (C-shell:\n\
4035 'setenv TERM ...') to specify the correct type. It may be necessary\n"
4036 #ifdef TERMINFO
4037 "to do 'unset TERMINFO' (C-shell: 'unsetenv TERMINFO') as well.",
4038 #else
4039 "to do 'unset TERMCAP' (C-shell: 'unsetenv TERMCAP') as well.",
4040 #endif
4041 terminal_type);
4042 }
4043
4044 area = tty->termcap_strings_buffer;
4045 tty->TS_ins_line = tgetstr ("al", address);
4046 tty->TS_ins_multi_lines = tgetstr ("AL", address);
4047 tty->TS_bell = tgetstr ("bl", address);
4048 BackTab (tty) = tgetstr ("bt", address);
4049 tty->TS_clr_to_bottom = tgetstr ("cd", address);
4050 tty->TS_clr_line = tgetstr ("ce", address);
4051 tty->TS_clr_frame = tgetstr ("cl", address);
4052 ColPosition (tty) = NULL; /* tgetstr ("ch", address); */
4053 AbsPosition (tty) = tgetstr ("cm", address);
4054 CR (tty) = tgetstr ("cr", address);
4055 tty->TS_set_scroll_region = tgetstr ("cs", address);
4056 tty->TS_set_scroll_region_1 = tgetstr ("cS", address);
4057 RowPosition (tty) = tgetstr ("cv", address);
4058 tty->TS_del_char = tgetstr ("dc", address);
4059 tty->TS_del_multi_chars = tgetstr ("DC", address);
4060 tty->TS_del_line = tgetstr ("dl", address);
4061 tty->TS_del_multi_lines = tgetstr ("DL", address);
4062 tty->TS_delete_mode = tgetstr ("dm", address);
4063 tty->TS_end_delete_mode = tgetstr ("ed", address);
4064 tty->TS_end_insert_mode = tgetstr ("ei", address);
4065 Home (tty) = tgetstr ("ho", address);
4066 tty->TS_ins_char = tgetstr ("ic", address);
4067 tty->TS_ins_multi_chars = tgetstr ("IC", address);
4068 tty->TS_insert_mode = tgetstr ("im", address);
4069 tty->TS_pad_inserted_char = tgetstr ("ip", address);
4070 tty->TS_end_keypad_mode = tgetstr ("ke", address);
4071 tty->TS_keypad_mode = tgetstr ("ks", address);
4072 LastLine (tty) = tgetstr ("ll", address);
4073 Right (tty) = tgetstr ("nd", address);
4074 Down (tty) = tgetstr ("do", address);
4075 if (!Down (tty))
4076 Down (tty) = tgetstr ("nl", address); /* Obsolete name for "do". */
4077 if (tgetflag ("bs"))
4078 Left (tty) = "\b"; /* Can't possibly be longer! */
4079 else /* (Actually, "bs" is obsolete...) */
4080 Left (tty) = tgetstr ("le", address);
4081 if (!Left (tty))
4082 Left (tty) = tgetstr ("bc", address); /* Obsolete name for "le". */
4083 tty->TS_pad_char = tgetstr ("pc", address);
4084 tty->TS_repeat = tgetstr ("rp", address);
4085 tty->TS_end_standout_mode = tgetstr ("se", address);
4086 tty->TS_fwd_scroll = tgetstr ("sf", address);
4087 tty->TS_standout_mode = tgetstr ("so", address);
4088 tty->TS_rev_scroll = tgetstr ("sr", address);
4089 tty->Wcm->cm_tab = tgetstr ("ta", address);
4090 tty->TS_end_termcap_modes = tgetstr ("te", address);
4091 tty->TS_termcap_modes = tgetstr ("ti", address);
4092 Up (tty) = tgetstr ("up", address);
4093 tty->TS_visible_bell = tgetstr ("vb", address);
4094 tty->TS_cursor_normal = tgetstr ("ve", address);
4095 tty->TS_cursor_visible = tgetstr ("vs", address);
4096 tty->TS_cursor_invisible = tgetstr ("vi", address);
4097 tty->TS_set_window = tgetstr ("wi", address);
4098
4099 tty->TS_enter_underline_mode = tgetstr ("us", address);
4100 tty->TS_exit_underline_mode = tgetstr ("ue", address);
4101 tty->TS_enter_bold_mode = tgetstr ("md", address);
4102 tty->TS_enter_italic_mode = tgetstr ("ZH", address);
4103 tty->TS_enter_dim_mode = tgetstr ("mh", address);
4104 tty->TS_enter_reverse_mode = tgetstr ("mr", address);
4105 tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
4106 tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
4107 tty->TS_exit_attribute_mode = tgetstr ("me", address);
4108
4109 MultiUp (tty) = tgetstr ("UP", address);
4110 MultiDown (tty) = tgetstr ("DO", address);
4111 MultiLeft (tty) = tgetstr ("LE", address);
4112 MultiRight (tty) = tgetstr ("RI", address);
4113
4114 /* SVr4/ANSI color support. If "op" isn't available, don't support
4115 color because we can't switch back to the default foreground and
4116 background. */
4117 tty->TS_orig_pair = tgetstr ("op", address);
4118 if (tty->TS_orig_pair)
4119 {
4120 tty->TS_set_foreground = tgetstr ("AF", address);
4121 tty->TS_set_background = tgetstr ("AB", address);
4122 if (!tty->TS_set_foreground)
4123 {
4124 /* SVr4. */
4125 tty->TS_set_foreground = tgetstr ("Sf", address);
4126 tty->TS_set_background = tgetstr ("Sb", address);
4127 }
4128
4129 tty->TN_max_colors = tgetnum ("Co");
4130 tty->TN_max_pairs = tgetnum ("pa");
4131
4132 tty->TN_no_color_video = tgetnum ("NC");
4133 if (tty->TN_no_color_video == -1)
4134 tty->TN_no_color_video = 0;
4135 }
4136
4137 tty_default_color_capabilities (tty, 1);
4138
4139 MagicWrap (tty) = tgetflag ("xn");
4140 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
4141 the former flag imply the latter. */
4142 AutoWrap (tty) = MagicWrap (tty) || tgetflag ("am");
4143 tty->memory_below_frame = tgetflag ("db");
4144 tty->TF_hazeltine = tgetflag ("hz");
4145 tty->must_write_spaces = tgetflag ("in");
4146 tty->meta_key = tgetflag ("km") || tgetflag ("MT");
4147 tty->TF_insmode_motion = tgetflag ("mi");
4148 tty->TF_standout_motion = tgetflag ("ms");
4149 tty->TF_underscore = tgetflag ("ul");
4150 tty->TF_teleray = tgetflag ("xt");
4151
4152 #else /* DOS_NT */
4153 #ifdef WINDOWSNT
4154 {
4155 struct frame *f = XFRAME (selected_frame);
4156 int height, width;
4157
4158 initialize_w32_display (terminal, &width, &height);
4159
4160 FrameRows (tty) = height;
4161 FrameCols (tty) = width;
4162 tty->specified_window = height;
4163
4164 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
4165 FRAME_HAS_HORIZONTAL_SCROLL_BARS (f) = 0;
4166 tty->char_ins_del_ok = 1;
4167 baud_rate = 19200;
4168 }
4169 #else /* MSDOS */
4170 {
4171 int height, width;
4172 if (strcmp (terminal_type, "internal") == 0)
4173 terminal->type = output_msdos_raw;
4174 initialize_msdos_display (terminal);
4175
4176 get_tty_size (fileno (tty->input), &width, &height);
4177 FrameCols (tty) = width;
4178 FrameRows (tty) = height;
4179 tty->char_ins_del_ok = 0;
4180 init_baud_rate (fileno (tty->input));
4181 }
4182 #endif /* MSDOS */
4183 tty->output = stdout;
4184 tty->input = stdin;
4185 /* The following two are inaccessible from w32console.c. */
4186 terminal->delete_frame_hook = &tty_free_frame_resources;
4187 terminal->delete_terminal_hook = &delete_tty;
4188
4189 tty->name = xstrdup (name);
4190 terminal->name = xstrdup (name);
4191 tty->type = xstrdup (terminal_type);
4192
4193 add_keyboard_wait_descriptor (0);
4194
4195 tty->delete_in_insert_mode = 1;
4196
4197 UseTabs (tty) = 0;
4198 tty->scroll_region_ok = 0;
4199
4200 /* Seems to insert lines when it's not supposed to, messing up the
4201 display. In doing a trace, it didn't seem to be called much, so I
4202 don't think we're losing anything by turning it off. */
4203 tty->line_ins_del_ok = 0;
4204
4205 tty->TN_max_colors = 16; /* Must be non-zero for tty-display-color-p. */
4206 #endif /* DOS_NT */
4207
4208 #ifdef HAVE_GPM
4209 terminal->mouse_position_hook = term_mouse_position;
4210 tty->mouse_highlight.mouse_face_window = Qnil;
4211 #endif
4212
4213 terminal->kboard = allocate_kboard (Qnil);
4214 terminal->kboard->reference_count++;
4215 /* Don't let the initial kboard remain current longer than necessary.
4216 That would cause problems if a file loaded on startup tries to
4217 prompt in the mini-buffer. */
4218 if (current_kboard == initial_kboard)
4219 current_kboard = terminal->kboard;
4220 #ifndef DOS_NT
4221 term_get_fkeys (address, terminal->kboard);
4222
4223 /* Get frame size from system, or else from termcap. */
4224 {
4225 int height, width;
4226 get_tty_size (fileno (tty->input), &width, &height);
4227 FrameCols (tty) = width;
4228 FrameRows (tty) = height;
4229 }
4230
4231 if (FrameCols (tty) <= 0)
4232 FrameCols (tty) = tgetnum ("co");
4233 if (FrameRows (tty) <= 0)
4234 FrameRows (tty) = tgetnum ("li");
4235
4236 if (FrameRows (tty) < 3 || FrameCols (tty) < 3)
4237 maybe_fatal (must_succeed, terminal,
4238 "Screen size %dx%d is too small",
4239 "Screen size %dx%d is too small",
4240 FrameCols (tty), FrameRows (tty));
4241
4242 TabWidth (tty) = tgetnum ("tw");
4243
4244 if (!tty->TS_bell)
4245 tty->TS_bell = "\07";
4246
4247 if (!tty->TS_fwd_scroll)
4248 tty->TS_fwd_scroll = Down (tty);
4249
4250 PC = tty->TS_pad_char ? *tty->TS_pad_char : 0;
4251
4252 if (TabWidth (tty) < 0)
4253 TabWidth (tty) = 8;
4254
4255 /* Turned off since /etc/termcap seems to have :ta= for most terminals
4256 and newer termcap doc does not seem to say there is a default.
4257 if (!tty->Wcm->cm_tab)
4258 tty->Wcm->cm_tab = "\t";
4259 */
4260
4261 /* We don't support standout modes that use `magic cookies', so
4262 turn off any that do. */
4263 if (tty->TS_standout_mode && tgetnum ("sg") >= 0)
4264 {
4265 tty->TS_standout_mode = 0;
4266 tty->TS_end_standout_mode = 0;
4267 }
4268 if (tty->TS_enter_underline_mode && tgetnum ("ug") >= 0)
4269 {
4270 tty->TS_enter_underline_mode = 0;
4271 tty->TS_exit_underline_mode = 0;
4272 }
4273
4274 /* If there's no standout mode, try to use underlining instead. */
4275 if (tty->TS_standout_mode == 0)
4276 {
4277 tty->TS_standout_mode = tty->TS_enter_underline_mode;
4278 tty->TS_end_standout_mode = tty->TS_exit_underline_mode;
4279 }
4280
4281 /* If no `se' string, try using a `me' string instead.
4282 If that fails, we can't use standout mode at all. */
4283 if (tty->TS_end_standout_mode == 0)
4284 {
4285 char *s = tgetstr ("me", address);
4286 if (s != 0)
4287 tty->TS_end_standout_mode = s;
4288 else
4289 tty->TS_standout_mode = 0;
4290 }
4291
4292 if (tty->TF_teleray)
4293 {
4294 tty->Wcm->cm_tab = 0;
4295 /* We can't support standout mode, because it uses magic cookies. */
4296 tty->TS_standout_mode = 0;
4297 /* But that means we cannot rely on ^M to go to column zero! */
4298 CR (tty) = 0;
4299 /* LF can't be trusted either -- can alter hpos. */
4300 /* If move at column 0 thru a line with TS_standout_mode. */
4301 Down (tty) = 0;
4302 }
4303
4304 tty->specified_window = FrameRows (tty);
4305
4306 if (Wcm_init (tty) == -1) /* Can't do cursor motion. */
4307 {
4308 maybe_fatal (must_succeed, terminal,
4309 "Terminal type \"%s\" is not powerful enough to run Emacs",
4310 "Terminal type \"%s\" is not powerful enough to run Emacs.\n\
4311 It lacks the ability to position the cursor.\n\
4312 If that is not the actual type of terminal you have,\n\
4313 use the Bourne shell command 'TERM=...; export TERM' (C-shell:\n\
4314 'setenv TERM ...') to specify the correct type. It may be necessary\n"
4315 # ifdef TERMINFO
4316 "to do 'unset TERMINFO' (C-shell: 'unsetenv TERMINFO') as well.",
4317 # else /* TERMCAP */
4318 "to do 'unset TERMCAP' (C-shell: 'unsetenv TERMCAP') as well.",
4319 # endif /* TERMINFO */
4320 terminal_type);
4321 }
4322
4323 if (FrameRows (tty) <= 0 || FrameCols (tty) <= 0)
4324 maybe_fatal (must_succeed, terminal,
4325 "Could not determine the frame size",
4326 "Could not determine the frame size");
4327
4328 tty->delete_in_insert_mode
4329 = tty->TS_delete_mode && tty->TS_insert_mode
4330 && !strcmp (tty->TS_delete_mode, tty->TS_insert_mode);
4331
4332 UseTabs (tty) = tabs_safe_p (fileno (tty->input)) && TabWidth (tty) == 8;
4333
4334 tty->scroll_region_ok
4335 = (tty->Wcm->cm_abs
4336 && (tty->TS_set_window || tty->TS_set_scroll_region || tty->TS_set_scroll_region_1));
4337
4338 tty->line_ins_del_ok
4339 = (((tty->TS_ins_line || tty->TS_ins_multi_lines)
4340 && (tty->TS_del_line || tty->TS_del_multi_lines))
4341 || (tty->scroll_region_ok
4342 && tty->TS_fwd_scroll && tty->TS_rev_scroll));
4343
4344 tty->char_ins_del_ok
4345 = ((tty->TS_ins_char || tty->TS_insert_mode
4346 || tty->TS_pad_inserted_char || tty->TS_ins_multi_chars)
4347 && (tty->TS_del_char || tty->TS_del_multi_chars));
4348
4349 init_baud_rate (fileno (tty->input));
4350
4351 #endif /* not DOS_NT */
4352
4353 /* Init system terminal modes (RAW or CBREAK, etc.). */
4354 init_sys_modes (tty);
4355
4356 return terminal;
4357 }
4358
4359
4360 static void
4361 vfatal (const char *str, va_list ap)
4362 {
4363 fprintf (stderr, "emacs: ");
4364 vfprintf (stderr, str, ap);
4365 if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
4366 fprintf (stderr, "\n");
4367 fflush (stderr);
4368 exit (1);
4369 }
4370
4371
4372 /* Auxiliary error-handling function for init_tty.
4373 Delete TERMINAL, then call error or fatal with str1 or str2,
4374 respectively, according to whether MUST_SUCCEED is true. */
4375
4376 static void
4377 maybe_fatal (bool must_succeed, struct terminal *terminal,
4378 const char *str1, const char *str2, ...)
4379 {
4380 va_list ap;
4381 va_start (ap, str2);
4382 if (terminal)
4383 delete_tty (terminal);
4384
4385 if (must_succeed)
4386 vfatal (str2, ap);
4387 else
4388 verror (str1, ap);
4389 }
4390
4391 void
4392 fatal (const char *str, ...)
4393 {
4394 va_list ap;
4395 va_start (ap, str);
4396 vfatal (str, ap);
4397 }
4398
4399 \f
4400
4401 /* Delete the given tty terminal, closing all frames on it. */
4402
4403 static void
4404 delete_tty (struct terminal *terminal)
4405 {
4406 struct tty_display_info *tty;
4407
4408 /* Protect against recursive calls. delete_frame in
4409 delete_terminal calls us back when it deletes our last frame. */
4410 if (!terminal->name)
4411 return;
4412
4413 eassert (terminal->type == output_termcap);
4414
4415 tty = terminal->display_info.tty;
4416
4417 if (tty == tty_list)
4418 tty_list = tty->next;
4419 else
4420 {
4421 struct tty_display_info *p;
4422 for (p = tty_list; p && p->next != tty; p = p->next)
4423 ;
4424
4425 if (! p)
4426 /* This should not happen. */
4427 emacs_abort ();
4428
4429 p->next = tty->next;
4430 tty->next = 0;
4431 }
4432
4433 /* reset_sys_modes needs a valid device, so this call needs to be
4434 before delete_terminal. */
4435 reset_sys_modes (tty);
4436
4437 delete_terminal (terminal);
4438
4439 xfree (tty->name);
4440 xfree (tty->type);
4441
4442 if (tty->input)
4443 {
4444 delete_keyboard_wait_descriptor (fileno (tty->input));
4445 if (tty->input != stdin)
4446 fclose (tty->input);
4447 }
4448 if (tty->output && tty->output != stdout && tty->output != tty->input)
4449 fclose (tty->output);
4450 if (tty->termscript)
4451 fclose (tty->termscript);
4452
4453 xfree (tty->old_tty);
4454 xfree (tty->Wcm);
4455 xfree (tty);
4456 }
4457
4458 void
4459 syms_of_term (void)
4460 {
4461 DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo,
4462 doc: /* Non-nil means the system uses terminfo rather than termcap.
4463 This variable can be used by terminal emulator packages. */);
4464 #ifdef TERMINFO
4465 system_uses_terminfo = 1;
4466 #else
4467 system_uses_terminfo = 0;
4468 #endif
4469
4470 DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions,
4471 doc: /* Functions run after suspending a tty.
4472 The functions are run with one argument, the terminal object to be suspended.
4473 See `suspend-tty'. */);
4474 Vsuspend_tty_functions = Qnil;
4475
4476
4477 DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions,
4478 doc: /* Functions run after resuming a tty.
4479 The functions are run with one argument, the terminal object that was revived.
4480 See `resume-tty'. */);
4481 Vresume_tty_functions = Qnil;
4482
4483 DEFVAR_BOOL ("visible-cursor", visible_cursor,
4484 doc: /* Non-nil means to make the cursor very visible.
4485 This only has an effect when running in a text terminal.
4486 What means \"very visible\" is up to your terminal. It may make the cursor
4487 bigger, or it may make it blink, or it may do nothing at all. */);
4488 visible_cursor = 1;
4489
4490 defsubr (&Stty_display_color_p);
4491 defsubr (&Stty_display_color_cells);
4492 defsubr (&Stty_no_underline);
4493 defsubr (&Stty_type);
4494 defsubr (&Scontrolling_tty_p);
4495 defsubr (&Stty_top_frame);
4496 defsubr (&Ssuspend_tty);
4497 defsubr (&Sresume_tty);
4498 #ifdef HAVE_GPM
4499 defsubr (&Sgpm_mouse_start);
4500 defsubr (&Sgpm_mouse_stop);
4501 #endif /* HAVE_GPM */
4502
4503 #ifndef DOS_NT
4504 default_orig_pair = NULL;
4505 default_set_foreground = NULL;
4506 default_set_background = NULL;
4507 #endif /* !DOS_NT */
4508
4509 encode_terminal_src = NULL;
4510 encode_terminal_dst = NULL;
4511
4512 DEFSYM (Qtty_mode_set_strings, "tty-mode-set-strings");
4513 DEFSYM (Qtty_mode_reset_strings, "tty-mode-reset-strings");
4514
4515 #ifndef MSDOS
4516 DEFSYM (Qtty_menu_next_item, "tty-menu-next-item");
4517 DEFSYM (Qtty_menu_prev_item, "tty-menu-prev-item");
4518 DEFSYM (Qtty_menu_next_menu, "tty-menu-next-menu");
4519 DEFSYM (Qtty_menu_prev_menu, "tty-menu-prev-menu");
4520 DEFSYM (Qtty_menu_select, "tty-menu-select");
4521 DEFSYM (Qtty_menu_ignore, "tty-menu-ignore");
4522 DEFSYM (Qtty_menu_exit, "tty-menu-exit");
4523 DEFSYM (Qtty_menu_mouse_movement, "tty-menu-mouse-movement");
4524 DEFSYM (Qtty_menu_navigation_map, "tty-menu-navigation-map");
4525 #endif
4526 }