Implemented multiple tty support.
[bpt/emacs.git] / src / term.c
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 98, 2000, 2001, 2002
3 Free Software 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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
23
24 #include <config.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
28
29 #include <sys/file.h>
30
31 #include "systty.h" /* For emacs_tty in termchar.h */
32 #include "termchar.h"
33 #include "termopts.h"
34 #include "lisp.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include "keyboard.h"
38 #include "frame.h"
39 #include "disptab.h"
40 #include "termhooks.h"
41 #include "dispextern.h"
42 #include "window.h"
43 #include "keymap.h"
44
45 /* For now, don't try to include termcap.h. On some systems,
46 configure finds a non-standard termcap.h that the main build
47 won't find. */
48
49 #if defined HAVE_TERMCAP_H && 0
50 #include <termcap.h>
51 #else
52 extern void tputs P_ ((const char *, int, int (*)(int)));
53 extern int tgetent P_ ((char *, const char *));
54 extern int tgetflag P_ ((char *id));
55 extern int tgetnum P_ ((char *id));
56 #endif
57
58 #include "cm.h"
59 #ifdef HAVE_X_WINDOWS
60 #include "xterm.h"
61 #endif
62 #ifdef MAC_OS
63 #include "macterm.h"
64 #endif
65
66 #ifndef O_RDWR
67 #define O_RDWR 2
68 #endif
69
70 static void turn_on_face P_ ((struct frame *, int face_id));
71 static void turn_off_face P_ ((struct frame *, int face_id));
72 static void tty_show_cursor P_ ((void));
73 static void tty_hide_cursor P_ ((void));
74
75 #define OUTPUT(tty, a) \
76 emacs_tputs ((tty), a, (int) (FRAME_LINES (XFRAME (selected_frame)) - curY), cmputc)
77 #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
78 #define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
79
80 #define OUTPUT_IF(tty, a) \
81 do { \
82 if (a) \
83 emacs_tputs ((tty), a, (int) (FRAME_LINES (XFRAME (selected_frame)) \
84 - curY), cmputc); \
85 } while (0)
86
87 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
88
89 /* Function to use to ring the bell. */
90
91 Lisp_Object Vring_bell_function;
92
93 /* Terminal characteristics that higher levels want to look at. */
94
95 struct tty_output *tty_list;
96
97 /* Nonzero means no need to redraw the entire frame on resuming
98 a suspended Emacs. This is useful on terminals with multiple pages,
99 where one page is used for Emacs and another for all else. */
100 int no_redraw_on_reenter;
101
102 /* Hook functions that you can set to snap out the functions in this file.
103 These are all extern'd in termhooks.h */
104
105 void (*cursor_to_hook) P_ ((int, int));
106 void (*raw_cursor_to_hook) P_ ((int, int));
107 void (*clear_to_end_hook) P_ ((void));
108 void (*clear_frame_hook) P_ ((void));
109 void (*clear_end_of_line_hook) P_ ((int));
110
111 void (*ins_del_lines_hook) P_ ((int, int));
112
113 void (*delete_glyphs_hook) P_ ((int));
114
115 void (*ring_bell_hook) P_ ((void));
116
117 void (*reset_terminal_modes_hook) P_ ((void));
118 void (*set_terminal_modes_hook) P_ ((void));
119 void (*update_begin_hook) P_ ((struct frame *));
120 void (*update_end_hook) P_ ((struct frame *));
121 void (*set_terminal_window_hook) P_ ((int));
122 void (*insert_glyphs_hook) P_ ((struct glyph *, int));
123 void (*write_glyphs_hook) P_ ((struct glyph *, int));
124 void (*delete_glyphs_hook) P_ ((int));
125
126 int (*read_socket_hook) P_ ((int, struct input_event *, int, int));
127
128 void (*frame_up_to_date_hook) P_ ((struct frame *));
129
130 /* Return the current position of the mouse.
131
132 Set *f to the frame the mouse is in, or zero if the mouse is in no
133 Emacs frame. If it is set to zero, all the other arguments are
134 garbage.
135
136 If the motion started in a scroll bar, set *bar_window to the
137 scroll bar's window, *part to the part the mouse is currently over,
138 *x to the position of the mouse along the scroll bar, and *y to the
139 overall length of the scroll bar.
140
141 Otherwise, set *bar_window to Qnil, and *x and *y to the column and
142 row of the character cell the mouse is over.
143
144 Set *time to the time the mouse was at the returned position.
145
146 This should clear mouse_moved until the next motion
147 event arrives. */
148
149 void (*mouse_position_hook) P_ ((FRAME_PTR *f, int insist,
150 Lisp_Object *bar_window,
151 enum scroll_bar_part *part,
152 Lisp_Object *x,
153 Lisp_Object *y,
154 unsigned long *time));
155
156 /* When reading from a minibuffer in a different frame, Emacs wants
157 to shift the highlight from the selected frame to the mini-buffer's
158 frame; under X, this means it lies about where the focus is.
159 This hook tells the window system code to re-decide where to put
160 the highlight. */
161
162 void (*frame_rehighlight_hook) P_ ((FRAME_PTR f));
163
164 /* If we're displaying frames using a window system that can stack
165 frames on top of each other, this hook allows you to bring a frame
166 to the front, or bury it behind all the other windows. If this
167 hook is zero, that means the device we're displaying on doesn't
168 support overlapping frames, so there's no need to raise or lower
169 anything.
170
171 If RAISE is non-zero, F is brought to the front, before all other
172 windows. If RAISE is zero, F is sent to the back, behind all other
173 windows. */
174
175 void (*frame_raise_lower_hook) P_ ((FRAME_PTR f, int raise));
176
177 /* Set the vertical scroll bar for WINDOW to have its upper left corner
178 at (TOP, LEFT), and be LENGTH rows high. Set its handle to
179 indicate that we are displaying PORTION characters out of a total
180 of WHOLE characters, starting at POSITION. If WINDOW doesn't yet
181 have a scroll bar, create one for it. */
182
183 void (*set_vertical_scroll_bar_hook)
184 P_ ((struct window *window,
185 int portion, int whole, int position));
186
187
188 /* The following three hooks are used when we're doing a thorough
189 redisplay of the frame. We don't explicitly know which scroll bars
190 are going to be deleted, because keeping track of when windows go
191 away is a real pain - can you say set-window-configuration?
192 Instead, we just assert at the beginning of redisplay that *all*
193 scroll bars are to be removed, and then save scroll bars from the
194 fiery pit when we actually redisplay their window. */
195
196 /* Arrange for all scroll bars on FRAME to be removed at the next call
197 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
198 `*redeem_scroll_bar_hook' is applied to its window before the judgment.
199
200 This should be applied to each frame each time its window tree is
201 redisplayed, even if it is not displaying scroll bars at the moment;
202 if the HAS_SCROLL_BARS flag has just been turned off, only calling
203 this and the judge_scroll_bars_hook will get rid of them.
204
205 If non-zero, this hook should be safe to apply to any frame,
206 whether or not it can support scroll bars, and whether or not it is
207 currently displaying them. */
208
209 void (*condemn_scroll_bars_hook) P_ ((FRAME_PTR frame));
210
211 /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
212 Note that it's okay to redeem a scroll bar that is not condemned. */
213
214 void (*redeem_scroll_bar_hook) P_ ((struct window *window));
215
216 /* Remove all scroll bars on FRAME that haven't been saved since the
217 last call to `*condemn_scroll_bars_hook'.
218
219 This should be applied to each frame after each time its window
220 tree is redisplayed, even if it is not displaying scroll bars at the
221 moment; if the HAS_SCROLL_BARS flag has just been turned off, only
222 calling this and condemn_scroll_bars_hook will get rid of them.
223
224 If non-zero, this hook should be safe to apply to any frame,
225 whether or not it can support scroll bars, and whether or not it is
226 currently displaying them. */
227
228 void (*judge_scroll_bars_hook) P_ ((FRAME_PTR FRAME));
229
230 /* Strings, numbers and flags taken from the termcap entry. */
231
232 char *TS_ins_line; /* "al" */
233 char *TS_ins_multi_lines; /* "AL" (one parameter, # lines to insert) */
234 char *TS_bell; /* "bl" */
235 char *TS_clr_to_bottom; /* "cd" */
236 char *TS_clr_line; /* "ce", clear to end of line */
237 char *TS_clr_frame; /* "cl" */
238 char *TS_set_scroll_region; /* "cs" (2 params, first line and last line) */
239 char *TS_set_scroll_region_1; /* "cS" (4 params: total lines,
240 lines above scroll region, lines below it,
241 total lines again) */
242 char *TS_del_char; /* "dc" */
243 char *TS_del_multi_chars; /* "DC" (one parameter, # chars to delete) */
244 char *TS_del_line; /* "dl" */
245 char *TS_del_multi_lines; /* "DL" (one parameter, # lines to delete) */
246 char *TS_delete_mode; /* "dm", enter character-delete mode */
247 char *TS_end_delete_mode; /* "ed", leave character-delete mode */
248 char *TS_end_insert_mode; /* "ei", leave character-insert mode */
249 char *TS_ins_char; /* "ic" */
250 char *TS_ins_multi_chars; /* "IC" (one parameter, # chars to insert) */
251 char *TS_insert_mode; /* "im", enter character-insert mode */
252 char *TS_pad_inserted_char; /* "ip". Just padding, no commands. */
253 char *TS_end_keypad_mode; /* "ke" */
254 char *TS_keypad_mode; /* "ks" */
255 char *TS_pad_char; /* "pc", char to use as padding */
256 char *TS_repeat; /* "rp" (2 params, # times to repeat
257 and character to be repeated) */
258 char *TS_end_standout_mode; /* "se" */
259 char *TS_fwd_scroll; /* "sf" */
260 char *TS_standout_mode; /* "so" */
261 char *TS_rev_scroll; /* "sr" */
262 char *TS_end_termcap_modes; /* "te" */
263 char *TS_termcap_modes; /* "ti" */
264 char *TS_visible_bell; /* "vb" */
265 char *TS_cursor_normal; /* "ve" */
266 char *TS_cursor_visible; /* "vs" */
267 char *TS_cursor_invisible; /* "vi" */
268 char *TS_set_window; /* "wi" (4 params, start and end of window,
269 each as vpos and hpos) */
270
271 /* Value of the "NC" (no_color_video) capability, or 0 if not
272 present. */
273
274 static int TN_no_color_video;
275
276 /* Meaning of bits in no_color_video. Each bit set means that the
277 corresponding attribute cannot be combined with colors. */
278
279 enum no_color_bit
280 {
281 NC_STANDOUT = 1 << 0,
282 NC_UNDERLINE = 1 << 1,
283 NC_REVERSE = 1 << 2,
284 NC_BLINK = 1 << 3,
285 NC_DIM = 1 << 4,
286 NC_BOLD = 1 << 5,
287 NC_INVIS = 1 << 6,
288 NC_PROTECT = 1 << 7,
289 NC_ALT_CHARSET = 1 << 8
290 };
291
292 /* "md" -- turn on bold (extra bright mode). */
293
294 char *TS_enter_bold_mode;
295
296 /* "mh" -- turn on half-bright mode. */
297
298 char *TS_enter_dim_mode;
299
300 /* "mb" -- enter blinking mode. */
301
302 char *TS_enter_blink_mode;
303
304 /* "mr" -- enter reverse video mode. */
305
306 char *TS_enter_reverse_mode;
307
308 /* "us"/"ue" -- start/end underlining. */
309
310 char *TS_exit_underline_mode, *TS_enter_underline_mode;
311
312 /* "as"/"ae" -- start/end alternate character set. Not really
313 supported, yet. */
314
315 char *TS_enter_alt_charset_mode, *TS_exit_alt_charset_mode;
316
317 /* "me" -- switch appearances off. */
318
319 char *TS_exit_attribute_mode;
320
321 /* "Co" -- number of colors. */
322
323 int TN_max_colors;
324
325 /* "pa" -- max. number of color pairs on screen. Not handled yet.
326 Could be a problem if not equal to TN_max_colors * TN_max_colors. */
327
328 int TN_max_pairs;
329
330 /* "op" -- SVr4 set default pair to its original value. */
331
332 char *TS_orig_pair;
333
334 /* "AF"/"AB" or "Sf"/"Sb"-- set ANSI or SVr4 foreground/background color.
335 1 param, the color index. */
336
337 char *TS_set_foreground, *TS_set_background;
338
339 int TF_hazeltine; /* termcap hz flag. */
340 int TF_insmode_motion; /* termcap mi flag: can move while in insert mode. */
341 int TF_standout_motion; /* termcap mi flag: can move while in standout mode. */
342 int TF_underscore; /* termcap ul flag: _ underlines if over-struck on
343 non-blank position. Must clear before writing _. */
344 int TF_teleray; /* termcap xt flag: many weird consequences.
345 For t1061. */
346
347 static int RPov; /* # chars to start a TS_repeat */
348
349 static int delete_in_insert_mode; /* delete mode == insert mode */
350
351 static int se_is_so; /* 1 if same string both enters and leaves
352 standout mode */
353
354 /* internal state */
355
356 /* The largest frame width in any call to calculate_costs. */
357
358 int max_frame_cols;
359
360 /* The largest frame height in any call to calculate_costs. */
361
362 int max_frame_lines;
363
364 static int costs_set; /* Nonzero if costs have been calculated. */
365
366 int insert_mode; /* Nonzero when in insert mode. */
367 int standout_mode; /* Nonzero when in standout mode. */
368
369 /* Size of window specified by higher levels.
370 This is the number of lines, from the top of frame downwards,
371 which can participate in insert-line/delete-line operations.
372
373 Effectively it excludes the bottom frame_lines - specified_window_size
374 lines from those operations. */
375
376 int specified_window;
377
378 /* Frame currently being redisplayed; 0 if not currently redisplaying.
379 (Direct output does not count). */
380
381 FRAME_PTR updating_frame;
382
383 /* Provided for lisp packages. */
384
385 static int system_uses_terminfo;
386
387 /* Flag used in tty_show/hide_cursor. */
388
389 static int tty_cursor_hidden;
390
391 char *tparam ();
392
393 extern char *tgetstr ();
394 \f
395
396 #ifdef WINDOWSNT
397 /* We aren't X windows, but we aren't termcap either. This makes me
398 uncertain as to what value to use for frame.output_method. For
399 this file, we'll define FRAME_TERMCAP_P to be zero so that our
400 output hooks get called instead of the termcap functions. Probably
401 the best long-term solution is to define an output_windows_nt... */
402
403 #undef FRAME_TERMCAP_P
404 #define FRAME_TERMCAP_P(_f_) 0
405 #endif /* WINDOWSNT */
406
407 void
408 ring_bell ()
409 {
410 if (!NILP (Vring_bell_function))
411 {
412 Lisp_Object function;
413
414 /* Temporarily set the global variable to nil
415 so that if we get an error, it stays nil
416 and we don't call it over and over.
417
418 We don't specbind it, because that would carefully
419 restore the bad value if there's an error
420 and make the loop of errors happen anyway. */
421
422 function = Vring_bell_function;
423 Vring_bell_function = Qnil;
424
425 call0 (function);
426
427 Vring_bell_function = function;
428 }
429 else if (!FRAME_TERMCAP_P (XFRAME (selected_frame)))
430 (*ring_bell_hook) ();
431 else {
432 OUTPUT (CURTTY (), TS_visible_bell && visible_bell ? TS_visible_bell : TS_bell);
433 }
434 }
435
436 void
437 set_terminal_modes (struct tty_output *tty)
438 {
439 if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
440 {
441 OUTPUT_IF (tty, TS_termcap_modes);
442 OUTPUT_IF (tty, TS_cursor_visible);
443 OUTPUT_IF (tty, TS_keypad_mode);
444 losecursor ();
445 }
446 else
447 (*set_terminal_modes_hook) ();
448 }
449
450 void
451 reset_terminal_modes (struct tty_output *tty)
452 {
453 if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
454 {
455 turn_off_highlight ();
456 turn_off_insert ();
457 OUTPUT_IF (tty, TS_end_keypad_mode);
458 OUTPUT_IF (tty, TS_cursor_normal);
459 OUTPUT_IF (tty, TS_end_termcap_modes);
460 OUTPUT_IF (tty, TS_orig_pair);
461 /* Output raw CR so kernel can track the cursor hpos. */
462 current_tty = tty;
463 cmputc ('\r');
464 }
465 else if (reset_terminal_modes_hook)
466 (*reset_terminal_modes_hook) ();
467 }
468
469 void
470 update_begin (f)
471 struct frame *f;
472 {
473 updating_frame = f;
474 if (!FRAME_TERMCAP_P (f))
475 update_begin_hook (f);
476 }
477
478 void
479 update_end (f)
480 struct frame *f;
481 {
482 if (FRAME_TERMCAP_P (f))
483 {
484 if (!XWINDOW (selected_window)->cursor_off_p)
485 tty_show_cursor ();
486 turn_off_insert ();
487 background_highlight ();
488 }
489 else
490 update_end_hook (f);
491
492 updating_frame = NULL;
493 }
494
495 void
496 set_terminal_window (size)
497 int size;
498 {
499 if (FRAME_TERMCAP_P (updating_frame))
500 {
501 specified_window = size ? size : FRAME_LINES (updating_frame);
502 if (TTY_SCROLL_REGION_OK (FRAME_TTY (updating_frame)))
503 set_scroll_region (0, specified_window);
504 }
505 else
506 set_terminal_window_hook (size);
507 }
508
509 void
510 set_scroll_region (start, stop)
511 int start, stop;
512 {
513 char *buf;
514 struct frame *sf = XFRAME (selected_frame);
515
516 if (TS_set_scroll_region)
517 buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
518 else if (TS_set_scroll_region_1)
519 buf = tparam (TS_set_scroll_region_1, 0, 0,
520 FRAME_LINES (sf), start,
521 FRAME_LINES (sf) - stop,
522 FRAME_LINES (sf));
523 else
524 buf = tparam (TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (sf));
525
526 OUTPUT (CURTTY (), buf);
527 xfree (buf);
528 losecursor ();
529 }
530
531 \f
532 static void
533 turn_on_insert ()
534 {
535 if (!insert_mode)
536 OUTPUT (CURTTY (), TS_insert_mode);
537 insert_mode = 1;
538 }
539
540 void
541 turn_off_insert ()
542 {
543 if (insert_mode)
544 OUTPUT (CURTTY (), TS_end_insert_mode);
545 insert_mode = 0;
546 }
547 \f
548 /* Handle highlighting. */
549
550 void
551 turn_off_highlight ()
552 {
553 if (standout_mode)
554 OUTPUT_IF (CURTTY (), TS_end_standout_mode);
555 standout_mode = 0;
556 }
557
558 static void
559 turn_on_highlight ()
560 {
561 if (!standout_mode)
562 OUTPUT_IF (CURTTY(), TS_standout_mode);
563 standout_mode = 1;
564 }
565
566 static void
567 toggle_highlight ()
568 {
569 if (standout_mode)
570 turn_off_highlight ();
571 else
572 turn_on_highlight ();
573 }
574
575
576 /* Make cursor invisible. */
577
578 static void
579 tty_hide_cursor ()
580 {
581 if (tty_cursor_hidden == 0)
582 {
583 tty_cursor_hidden = 1;
584 OUTPUT_IF (CURTTY (), TS_cursor_invisible);
585 }
586 }
587
588
589 /* Ensure that cursor is visible. */
590
591 static void
592 tty_show_cursor ()
593 {
594 if (tty_cursor_hidden)
595 {
596 tty_cursor_hidden = 0;
597 OUTPUT_IF (CURTTY (), TS_cursor_normal);
598 OUTPUT_IF (CURTTY (), TS_cursor_visible);
599 }
600 }
601
602
603 /* Set standout mode to the state it should be in for
604 empty space inside windows. What this is,
605 depends on the user option inverse-video. */
606
607 void
608 background_highlight ()
609 {
610 if (inverse_video)
611 turn_on_highlight ();
612 else
613 turn_off_highlight ();
614 }
615
616 /* Set standout mode to the mode specified for the text to be output. */
617
618 static void
619 highlight_if_desired ()
620 {
621 if (inverse_video)
622 turn_on_highlight ();
623 else
624 turn_off_highlight ();
625 }
626 \f
627
628 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
629 frame-relative coordinates. */
630
631 void
632 cursor_to (vpos, hpos)
633 int vpos, hpos;
634 {
635 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
636
637 if (! FRAME_TERMCAP_P (f) && cursor_to_hook)
638 {
639 (*cursor_to_hook) (vpos, hpos);
640 return;
641 }
642
643 /* Detect the case where we are called from reset_sys_modes
644 and the costs have never been calculated. Do nothing. */
645 if (! costs_set)
646 return;
647
648 if (curY == vpos && curX == hpos)
649 return;
650 if (!TF_standout_motion)
651 background_highlight ();
652 if (!TF_insmode_motion)
653 turn_off_insert ();
654 cmgoto (FRAME_TTY (f), vpos, hpos);
655 }
656
657 /* Similar but don't take any account of the wasted characters. */
658
659 void
660 raw_cursor_to (row, col)
661 int row, col;
662 {
663 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
664 if (! FRAME_TERMCAP_P (f))
665 {
666 (*raw_cursor_to_hook) (row, col);
667 return;
668 }
669 if (curY == row && curX == col)
670 return;
671 if (!TF_standout_motion)
672 background_highlight ();
673 if (!TF_insmode_motion)
674 turn_off_insert ();
675 cmgoto (FRAME_TTY (f), row, col);
676 }
677 \f
678 /* Erase operations */
679
680 /* clear from cursor to end of frame */
681 void
682 clear_to_end ()
683 {
684 register int i;
685
686 if (clear_to_end_hook && ! FRAME_TERMCAP_P (updating_frame))
687 {
688 (*clear_to_end_hook) ();
689 return;
690 }
691 if (TS_clr_to_bottom)
692 {
693 background_highlight ();
694 OUTPUT (CURTTY (), TS_clr_to_bottom);
695 }
696 else
697 {
698 for (i = curY; i < FRAME_LINES (XFRAME (selected_frame)); i++)
699 {
700 cursor_to (i, 0);
701 clear_end_of_line (FRAME_COLS (XFRAME (selected_frame)));
702 }
703 }
704 }
705
706 /* Clear entire frame */
707
708 void
709 clear_frame ()
710 {
711 struct frame *sf = XFRAME (selected_frame);
712
713 if (clear_frame_hook
714 && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : sf)))
715 {
716 (*clear_frame_hook) ();
717 return;
718 }
719 if (TS_clr_frame)
720 {
721 background_highlight ();
722 OUTPUT (FRAME_TTY (updating_frame ? updating_frame : sf), TS_clr_frame);
723 cmat (0, 0);
724 }
725 else
726 {
727 cursor_to (0, 0);
728 clear_to_end ();
729 }
730 }
731
732 /* Clear from cursor to end of line.
733 Assume that the line is already clear starting at column first_unused_hpos.
734
735 Note that the cursor may be moved, on terminals lacking a `ce' string. */
736
737 void
738 clear_end_of_line (first_unused_hpos)
739 int first_unused_hpos;
740 {
741 register int i;
742
743 if (clear_end_of_line_hook
744 && ! FRAME_TERMCAP_P ((updating_frame
745 ? updating_frame
746 : XFRAME (selected_frame))))
747 {
748 (*clear_end_of_line_hook) (first_unused_hpos);
749 return;
750 }
751
752 /* Detect the case where we are called from reset_sys_modes
753 and the costs have never been calculated. Do nothing. */
754 if (! costs_set)
755 return;
756
757 if (curX >= first_unused_hpos)
758 return;
759 background_highlight ();
760 if (TS_clr_line)
761 {
762 OUTPUT1 (CURTTY (), TS_clr_line);
763 }
764 else
765 { /* have to do it the hard way */
766 struct frame *sf = XFRAME (selected_frame);
767 turn_off_insert ();
768
769 /* Do not write in last row last col with Auto-wrap on. */
770 if (AutoWrap && curY == FRAME_LINES (sf) - 1
771 && first_unused_hpos == FRAME_COLS (sf))
772 first_unused_hpos--;
773
774 for (i = curX; i < first_unused_hpos; i++)
775 {
776 if (TTY_TERMSCRIPT (CURTTY ()))
777 fputc (' ', TTY_TERMSCRIPT (CURTTY ()));
778 fputc (' ', TTY_OUTPUT (CURTTY ()));
779 }
780 cmplus (first_unused_hpos - curX);
781 }
782 }
783 \f
784 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes and
785 store them at DST. Do not write more than DST_LEN bytes. That may
786 require stopping before all SRC_LEN input glyphs have been
787 converted.
788
789 We store the number of glyphs actually converted in *CONSUMED. The
790 return value is the number of bytes store in DST. */
791
792 int
793 encode_terminal_code (src, dst, src_len, dst_len, consumed)
794 struct glyph *src;
795 int src_len;
796 unsigned char *dst;
797 int dst_len, *consumed;
798 {
799 struct glyph *src_start = src, *src_end = src + src_len;
800 unsigned char *dst_start = dst, *dst_end = dst + dst_len;
801 register GLYPH g;
802 unsigned char workbuf[MAX_MULTIBYTE_LENGTH];
803 const unsigned char *buf;
804 int len;
805 register int tlen = GLYPH_TABLE_LENGTH;
806 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
807 int result;
808 struct coding_system *coding;
809
810 /* If terminal_coding does any conversion, use it, otherwise use
811 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
812 because it always return 1 if the member src_multibyte is 1. */
813 coding = (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
814 ? &terminal_coding
815 : &safe_terminal_coding);
816
817 while (src < src_end)
818 {
819 /* We must skip glyphs to be padded for a wide character. */
820 if (! CHAR_GLYPH_PADDING_P (*src))
821 {
822 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
823
824 if (g < 0 || g >= tlen)
825 {
826 /* This glyph doesn't has an entry in Vglyph_table. */
827 if (! CHAR_VALID_P (src->u.ch, 0))
828 {
829 len = 1;
830 buf = " ";
831 coding->src_multibyte = 0;
832 }
833 else
834 {
835 len = CHAR_STRING (src->u.ch, workbuf);
836 buf = workbuf;
837 coding->src_multibyte = 1;
838 }
839 }
840 else
841 {
842 /* This glyph has an entry in Vglyph_table,
843 so process any alias before testing for simpleness. */
844 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
845
846 if (GLYPH_SIMPLE_P (tbase, tlen, g))
847 {
848 /* We set the multi-byte form of a character in G
849 (that should be an ASCII character) at
850 WORKBUF. */
851 workbuf[0] = FAST_GLYPH_CHAR (g);
852 len = 1;
853 buf = workbuf;
854 coding->src_multibyte = 0;
855 }
856 else
857 {
858 /* We have a string in Vglyph_table. */
859 len = GLYPH_LENGTH (tbase, g);
860 buf = GLYPH_STRING (tbase, g);
861 coding->src_multibyte = STRING_MULTIBYTE (tbase[g]);
862 }
863 }
864
865 result = encode_coding (coding, buf, dst, len, dst_end - dst);
866 len -= coding->consumed;
867 dst += coding->produced;
868 if (result == CODING_FINISH_INSUFFICIENT_DST
869 || (result == CODING_FINISH_INSUFFICIENT_SRC
870 && len > dst_end - dst))
871 /* The remaining output buffer is too short. We must
872 break the loop here without increasing SRC so that the
873 next call of this function starts from the same glyph. */
874 break;
875
876 if (len > 0)
877 {
878 /* This is the case that a code of the range 0200..0237
879 exists in buf. We must just write out such a code. */
880 buf += coding->consumed;
881 while (len--)
882 *dst++ = *buf++;
883 }
884 }
885 src++;
886 }
887
888 *consumed = src - src_start;
889 return (dst - dst_start);
890 }
891
892
893 void
894 write_glyphs (string, len)
895 register struct glyph *string;
896 register int len;
897 {
898 int produced, consumed;
899 struct frame *sf = XFRAME (selected_frame);
900 struct frame *f = updating_frame ? updating_frame : sf;
901 unsigned char conversion_buffer[1024];
902 int conversion_buffer_size = sizeof conversion_buffer;
903
904 if (write_glyphs_hook
905 && ! FRAME_TERMCAP_P (f))
906 {
907 (*write_glyphs_hook) (string, len);
908 return;
909 }
910
911 turn_off_insert ();
912 tty_hide_cursor ();
913
914 /* Don't dare write in last column of bottom line, if Auto-Wrap,
915 since that would scroll the whole frame on some terminals. */
916
917 if (AutoWrap
918 && curY + 1 == FRAME_LINES (sf)
919 && (curX + len) == FRAME_COLS (sf))
920 len --;
921 if (len <= 0)
922 return;
923
924 cmplus (len);
925
926 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
927 the tail. */
928 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
929
930 while (len > 0)
931 {
932 /* Identify a run of glyphs with the same face. */
933 int face_id = string->face_id;
934 int n;
935
936 for (n = 1; n < len; ++n)
937 if (string[n].face_id != face_id)
938 break;
939
940 /* Turn appearance modes of the face of the run on. */
941 highlight_if_desired ();
942 turn_on_face (f, face_id);
943
944 while (n > 0)
945 {
946 /* We use a fixed size (1024 bytes) of conversion buffer.
947 Usually it is sufficient, but if not, we just repeat the
948 loop. */
949 produced = encode_terminal_code (string, conversion_buffer,
950 n, conversion_buffer_size,
951 &consumed);
952 if (produced > 0)
953 {
954 fwrite (conversion_buffer, 1, produced,
955 TTY_OUTPUT (FRAME_TTY (f)));
956 if (ferror (TTY_OUTPUT (FRAME_TTY (f))))
957 clearerr (TTY_OUTPUT (FRAME_TTY (f)));
958 if (TTY_TERMSCRIPT (FRAME_TTY (f)))
959 fwrite (conversion_buffer, 1, produced,
960 TTY_TERMSCRIPT (FRAME_TTY (f)));
961 }
962 len -= consumed;
963 n -= consumed;
964 string += consumed;
965 }
966
967 /* Turn appearance modes off. */
968 turn_off_face (f, face_id);
969 turn_off_highlight ();
970 }
971
972 /* We may have to output some codes to terminate the writing. */
973 if (CODING_REQUIRE_FLUSHING (&terminal_coding))
974 {
975 terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
976 encode_coding (&terminal_coding, "", conversion_buffer,
977 0, conversion_buffer_size);
978 if (terminal_coding.produced > 0)
979 {
980 fwrite (conversion_buffer, 1, terminal_coding.produced,
981 TTY_OUTPUT (FRAME_TTY (f)));
982 if (ferror (TTY_OUTPUT (FRAME_TTY (f))))
983 clearerr (TTY_OUTPUT (FRAME_TTY (f)));
984 if (TTY_TERMSCRIPT (FRAME_TTY (f)))
985 fwrite (conversion_buffer, 1, terminal_coding.produced,
986 TTY_TERMSCRIPT (FRAME_TTY (f)));
987 }
988 }
989
990 cmcheckmagic (FRAME_TTY (f));
991 }
992
993 /* If start is zero, insert blanks instead of a string at start */
994
995 void
996 insert_glyphs (start, len)
997 register struct glyph *start;
998 register int len;
999 {
1000 char *buf;
1001 struct glyph *glyph = NULL;
1002 struct frame *f, *sf;
1003
1004 if (len <= 0)
1005 return;
1006
1007 if (insert_glyphs_hook)
1008 {
1009 (*insert_glyphs_hook) (start, len);
1010 return;
1011 }
1012
1013 sf = XFRAME (selected_frame);
1014 f = updating_frame ? updating_frame : sf;
1015
1016 if (TS_ins_multi_chars)
1017 {
1018 buf = tparam (TS_ins_multi_chars, 0, 0, len);
1019 OUTPUT1 (FRAME_TTY (f), buf);
1020 xfree (buf);
1021 if (start)
1022 write_glyphs (start, len);
1023 return;
1024 }
1025
1026 turn_on_insert ();
1027 cmplus (len);
1028 /* The bit CODING_MODE_LAST_BLOCK should be set to 1 only at the tail. */
1029 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
1030 while (len-- > 0)
1031 {
1032 int produced, consumed;
1033 unsigned char conversion_buffer[1024];
1034 int conversion_buffer_size = sizeof conversion_buffer;
1035
1036 OUTPUT1_IF (FRAME_TTY (f), TS_ins_char);
1037 if (!start)
1038 {
1039 conversion_buffer[0] = SPACEGLYPH;
1040 produced = 1;
1041 }
1042 else
1043 {
1044 highlight_if_desired ();
1045 turn_on_face (f, start->face_id);
1046 glyph = start;
1047 ++start;
1048 /* We must open sufficient space for a character which
1049 occupies more than one column. */
1050 while (len && CHAR_GLYPH_PADDING_P (*start))
1051 {
1052 OUTPUT1_IF (FRAME_TTY (f), TS_ins_char);
1053 start++, len--;
1054 }
1055
1056 if (len <= 0)
1057 /* This is the last glyph. */
1058 terminal_coding.mode |= CODING_MODE_LAST_BLOCK;
1059
1060 /* The size of conversion buffer (1024 bytes) is surely
1061 sufficient for just one glyph. */
1062 produced = encode_terminal_code (glyph, conversion_buffer, 1,
1063 conversion_buffer_size, &consumed);
1064 }
1065
1066 if (produced > 0)
1067 {
1068 fwrite (conversion_buffer, 1, produced,
1069 TTY_OUTPUT (FRAME_TTY (f)));
1070 if (ferror (TTY_OUTPUT (FRAME_TTY (f))))
1071 clearerr (TTY_OUTPUT (FRAME_TTY (f)));
1072 if (TTY_TERMSCRIPT (FRAME_TTY (f)))
1073 fwrite (conversion_buffer, 1, produced,
1074 TTY_TERMSCRIPT (FRAME_TTY (f)));
1075 }
1076
1077 OUTPUT1_IF (FRAME_TTY (f), TS_pad_inserted_char);
1078 if (start)
1079 {
1080 turn_off_face (f, glyph->face_id);
1081 turn_off_highlight ();
1082 }
1083 }
1084
1085 cmcheckmagic (FRAME_TTY (f));
1086 }
1087
1088 void
1089 delete_glyphs (n)
1090 register int n;
1091 {
1092 char *buf;
1093 register int i;
1094
1095 if (delete_glyphs_hook && ! FRAME_TERMCAP_P (updating_frame))
1096 {
1097 (*delete_glyphs_hook) (n);
1098 return;
1099 }
1100
1101 if (delete_in_insert_mode)
1102 {
1103 turn_on_insert ();
1104 }
1105 else
1106 {
1107 turn_off_insert ();
1108 OUTPUT_IF (FRAME_TTY (updating_frame), TS_delete_mode);
1109 }
1110
1111 if (TS_del_multi_chars)
1112 {
1113 buf = tparam (TS_del_multi_chars, 0, 0, n);
1114 OUTPUT1 (FRAME_TTY (updating_frame), buf);
1115 xfree (buf);
1116 }
1117 else
1118 for (i = 0; i < n; i++)
1119 OUTPUT1 (FRAME_TTY (updating_frame), TS_del_char);
1120 if (!delete_in_insert_mode)
1121 OUTPUT_IF (FRAME_TTY (updating_frame), TS_end_delete_mode);
1122 }
1123 \f
1124 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
1125
1126 void
1127 ins_del_lines (vpos, n)
1128 int vpos, n;
1129 {
1130 char *multi = n > 0 ? TS_ins_multi_lines : TS_del_multi_lines;
1131 char *single = n > 0 ? TS_ins_line : TS_del_line;
1132 char *scroll = n > 0 ? TS_rev_scroll : TS_fwd_scroll;
1133 struct frame *sf;
1134
1135 register int i = n > 0 ? n : -n;
1136 register char *buf;
1137
1138 if (ins_del_lines_hook && ! FRAME_TERMCAP_P (updating_frame))
1139 {
1140 (*ins_del_lines_hook) (vpos, n);
1141 return;
1142 }
1143
1144 sf = XFRAME (selected_frame);
1145
1146 /* If the lines below the insertion are being pushed
1147 into the end of the window, this is the same as clearing;
1148 and we know the lines are already clear, since the matching
1149 deletion has already been done. So can ignore this. */
1150 /* If the lines below the deletion are blank lines coming
1151 out of the end of the window, don't bother,
1152 as there will be a matching inslines later that will flush them. */
1153 if (TTY_SCROLL_REGION_OK (FRAME_TTY (sf))
1154 && vpos + i >= specified_window)
1155 return;
1156 if (!TTY_MEMORY_BELOW_FRAME (FRAME_TTY (sf))
1157 && vpos + i >= FRAME_LINES (sf))
1158 return;
1159
1160 if (multi)
1161 {
1162 raw_cursor_to (vpos, 0);
1163 background_highlight ();
1164 buf = tparam (multi, 0, 0, i);
1165 OUTPUT (FRAME_TTY (sf), buf);
1166 xfree (buf);
1167 }
1168 else if (single)
1169 {
1170 raw_cursor_to (vpos, 0);
1171 background_highlight ();
1172 while (--i >= 0)
1173 OUTPUT (FRAME_TTY (sf), single);
1174 if (TF_teleray)
1175 curX = 0;
1176 }
1177 else
1178 {
1179 set_scroll_region (vpos, specified_window);
1180 if (n < 0)
1181 raw_cursor_to (specified_window - 1, 0);
1182 else
1183 raw_cursor_to (vpos, 0);
1184 background_highlight ();
1185 while (--i >= 0)
1186 OUTPUTL (FRAME_TTY (sf), scroll, specified_window - vpos);
1187 set_scroll_region (0, specified_window);
1188 }
1189
1190 if (!TTY_SCROLL_REGION_OK (FRAME_TTY (sf))
1191 && TTY_MEMORY_BELOW_FRAME (FRAME_TTY (sf))
1192 && n < 0)
1193 {
1194 cursor_to (FRAME_LINES (sf) + n, 0);
1195 clear_to_end ();
1196 }
1197 }
1198 \f
1199 /* Compute cost of sending "str", in characters,
1200 not counting any line-dependent padding. */
1201
1202 int
1203 string_cost (str)
1204 char *str;
1205 {
1206 cost = 0;
1207 if (str)
1208 tputs (str, 0, evalcost);
1209 return cost;
1210 }
1211
1212 /* Compute cost of sending "str", in characters,
1213 counting any line-dependent padding at one line. */
1214
1215 static int
1216 string_cost_one_line (str)
1217 char *str;
1218 {
1219 cost = 0;
1220 if (str)
1221 tputs (str, 1, evalcost);
1222 return cost;
1223 }
1224
1225 /* Compute per line amount of line-dependent padding,
1226 in tenths of characters. */
1227
1228 int
1229 per_line_cost (str)
1230 register char *str;
1231 {
1232 cost = 0;
1233 if (str)
1234 tputs (str, 0, evalcost);
1235 cost = - cost;
1236 if (str)
1237 tputs (str, 10, evalcost);
1238 return cost;
1239 }
1240
1241 #ifndef old
1242 /* char_ins_del_cost[n] is cost of inserting N characters.
1243 char_ins_del_cost[-n] is cost of deleting N characters.
1244 The length of this vector is based on max_frame_cols. */
1245
1246 int *char_ins_del_vector;
1247
1248 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1249 #endif
1250
1251 /* ARGSUSED */
1252 static void
1253 calculate_ins_del_char_costs (frame)
1254 FRAME_PTR frame;
1255 {
1256 int ins_startup_cost, del_startup_cost;
1257 int ins_cost_per_char, del_cost_per_char;
1258 register int i;
1259 register int *p;
1260
1261 if (TS_ins_multi_chars)
1262 {
1263 ins_cost_per_char = 0;
1264 ins_startup_cost = string_cost_one_line (TS_ins_multi_chars);
1265 }
1266 else if (TS_ins_char || TS_pad_inserted_char
1267 || (TS_insert_mode && TS_end_insert_mode))
1268 {
1269 ins_startup_cost = (30 * (string_cost (TS_insert_mode)
1270 + string_cost (TS_end_insert_mode))) / 100;
1271 ins_cost_per_char = (string_cost_one_line (TS_ins_char)
1272 + string_cost_one_line (TS_pad_inserted_char));
1273 }
1274 else
1275 {
1276 ins_startup_cost = 9999;
1277 ins_cost_per_char = 0;
1278 }
1279
1280 if (TS_del_multi_chars)
1281 {
1282 del_cost_per_char = 0;
1283 del_startup_cost = string_cost_one_line (TS_del_multi_chars);
1284 }
1285 else if (TS_del_char)
1286 {
1287 del_startup_cost = (string_cost (TS_delete_mode)
1288 + string_cost (TS_end_delete_mode));
1289 if (delete_in_insert_mode)
1290 del_startup_cost /= 2;
1291 del_cost_per_char = string_cost_one_line (TS_del_char);
1292 }
1293 else
1294 {
1295 del_startup_cost = 9999;
1296 del_cost_per_char = 0;
1297 }
1298
1299 /* Delete costs are at negative offsets */
1300 p = &char_ins_del_cost (frame)[0];
1301 for (i = FRAME_COLS (frame); --i >= 0;)
1302 *--p = (del_startup_cost += del_cost_per_char);
1303
1304 /* Doing nothing is free */
1305 p = &char_ins_del_cost (frame)[0];
1306 *p++ = 0;
1307
1308 /* Insert costs are at positive offsets */
1309 for (i = FRAME_COLS (frame); --i >= 0;)
1310 *p++ = (ins_startup_cost += ins_cost_per_char);
1311 }
1312
1313 void
1314 calculate_costs (frame)
1315 FRAME_PTR frame;
1316 {
1317 register char *f = (TS_set_scroll_region
1318 ? TS_set_scroll_region
1319 : TS_set_scroll_region_1);
1320
1321 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1322
1323 if (FRAME_TERMCAP_P (frame))
1324 TTY_SCROLL_REGION_COST (frame->output_data.tty) = string_cost (f);
1325
1326 /* These variables are only used for terminal stuff. They are allocated
1327 once for the terminal frame of X-windows emacs, but not used afterwards.
1328
1329 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1330 X turns off char_ins_del_ok. */
1331
1332 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1333 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1334
1335 costs_set = 1;
1336
1337 if (char_ins_del_vector != 0)
1338 char_ins_del_vector
1339 = (int *) xrealloc (char_ins_del_vector,
1340 (sizeof (int)
1341 + 2 * max_frame_cols * sizeof (int)));
1342 else
1343 char_ins_del_vector
1344 = (int *) xmalloc (sizeof (int)
1345 + 2 * max_frame_cols * sizeof (int));
1346
1347 bzero (char_ins_del_vector, (sizeof (int)
1348 + 2 * max_frame_cols * sizeof (int)));
1349
1350 if (f && (!TS_ins_line && !TS_del_line))
1351 do_line_insertion_deletion_costs (frame,
1352 TS_rev_scroll, TS_ins_multi_lines,
1353 TS_fwd_scroll, TS_del_multi_lines,
1354 f, f, 1);
1355 else
1356 do_line_insertion_deletion_costs (frame,
1357 TS_ins_line, TS_ins_multi_lines,
1358 TS_del_line, TS_del_multi_lines,
1359 0, 0, 1);
1360
1361 calculate_ins_del_char_costs (frame);
1362
1363 /* Don't use TS_repeat if its padding is worse than sending the chars */
1364 if (TS_repeat && per_line_cost (TS_repeat) * baud_rate < 9000)
1365 RPov = string_cost (TS_repeat);
1366 else
1367 RPov = FRAME_COLS (frame) * 2;
1368
1369 cmcostinit (); /* set up cursor motion costs */
1370 }
1371 \f
1372 struct fkey_table {
1373 char *cap, *name;
1374 };
1375
1376 /* Termcap capability names that correspond directly to X keysyms.
1377 Some of these (marked "terminfo") aren't supplied by old-style
1378 (Berkeley) termcap entries. They're listed in X keysym order;
1379 except we put the keypad keys first, so that if they clash with
1380 other keys (as on the IBM PC keyboard) they get overridden.
1381 */
1382
1383 static struct fkey_table keys[] =
1384 {
1385 {"kh", "home"}, /* termcap */
1386 {"kl", "left"}, /* termcap */
1387 {"ku", "up"}, /* termcap */
1388 {"kr", "right"}, /* termcap */
1389 {"kd", "down"}, /* termcap */
1390 {"%8", "prior"}, /* terminfo */
1391 {"%5", "next"}, /* terminfo */
1392 {"@7", "end"}, /* terminfo */
1393 {"@1", "begin"}, /* terminfo */
1394 {"*6", "select"}, /* terminfo */
1395 {"%9", "print"}, /* terminfo */
1396 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1397 /*
1398 * "insert" --- see below
1399 */
1400 {"&8", "undo"}, /* terminfo */
1401 {"%0", "redo"}, /* terminfo */
1402 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1403 {"@0", "find"}, /* terminfo */
1404 {"@2", "cancel"}, /* terminfo */
1405 {"%1", "help"}, /* terminfo */
1406 /*
1407 * "break" goes here, but can't be reliably intercepted with termcap
1408 */
1409 {"&4", "reset"}, /* terminfo --- actually `restart' */
1410 /*
1411 * "system" and "user" --- no termcaps
1412 */
1413 {"kE", "clearline"}, /* terminfo */
1414 {"kA", "insertline"}, /* terminfo */
1415 {"kL", "deleteline"}, /* terminfo */
1416 {"kI", "insertchar"}, /* terminfo */
1417 {"kD", "deletechar"}, /* terminfo */
1418 {"kB", "backtab"}, /* terminfo */
1419 /*
1420 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1421 */
1422 {"@8", "kp-enter"}, /* terminfo */
1423 /*
1424 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1425 * "kp-multiply", "kp-add", "kp-separator",
1426 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1427 * --- no termcaps for any of these.
1428 */
1429 {"K4", "kp-1"}, /* terminfo */
1430 /*
1431 * "kp-2" --- no termcap
1432 */
1433 {"K5", "kp-3"}, /* terminfo */
1434 /*
1435 * "kp-4" --- no termcap
1436 */
1437 {"K2", "kp-5"}, /* terminfo */
1438 /*
1439 * "kp-6" --- no termcap
1440 */
1441 {"K1", "kp-7"}, /* terminfo */
1442 /*
1443 * "kp-8" --- no termcap
1444 */
1445 {"K3", "kp-9"}, /* terminfo */
1446 /*
1447 * "kp-equal" --- no termcap
1448 */
1449 {"k1", "f1"},
1450 {"k2", "f2"},
1451 {"k3", "f3"},
1452 {"k4", "f4"},
1453 {"k5", "f5"},
1454 {"k6", "f6"},
1455 {"k7", "f7"},
1456 {"k8", "f8"},
1457 {"k9", "f9"}
1458 };
1459
1460 static char **term_get_fkeys_arg;
1461 static Lisp_Object term_get_fkeys_1 ();
1462
1463 /* Find the escape codes sent by the function keys for Vfunction_key_map.
1464 This function scans the termcap function key sequence entries, and
1465 adds entries to Vfunction_key_map for each function key it finds. */
1466
1467 void
1468 term_get_fkeys (address)
1469 char **address;
1470 {
1471 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1472 errors during the call. The only errors should be from Fdefine_key
1473 when given a key sequence containing an invalid prefix key. If the
1474 termcap defines function keys which use a prefix that is already bound
1475 to a command by the default bindings, we should silently ignore that
1476 function key specification, rather than giving the user an error and
1477 refusing to run at all on such a terminal. */
1478
1479 extern Lisp_Object Fidentity ();
1480 term_get_fkeys_arg = address;
1481 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1482 }
1483
1484 static Lisp_Object
1485 term_get_fkeys_1 ()
1486 {
1487 int i;
1488
1489 char **address = term_get_fkeys_arg;
1490
1491 /* This can happen if CANNOT_DUMP or with strange options. */
1492 if (!initialized)
1493 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
1494
1495 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1496 {
1497 char *sequence = tgetstr (keys[i].cap, address);
1498 if (sequence)
1499 Fdefine_key (Vfunction_key_map, build_string (sequence),
1500 Fmake_vector (make_number (1),
1501 intern (keys[i].name)));
1502 }
1503
1504 /* The uses of the "k0" capability are inconsistent; sometimes it
1505 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1506 We will attempt to politely accommodate both systems by testing for
1507 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1508 */
1509 {
1510 char *k_semi = tgetstr ("k;", address);
1511 char *k0 = tgetstr ("k0", address);
1512 char *k0_name = "f10";
1513
1514 if (k_semi)
1515 {
1516 if (k0)
1517 /* Define f0 first, so that f10 takes precedence in case the
1518 key sequences happens to be the same. */
1519 Fdefine_key (Vfunction_key_map, build_string (k0),
1520 Fmake_vector (make_number (1), intern ("f0")));
1521 Fdefine_key (Vfunction_key_map, build_string (k_semi),
1522 Fmake_vector (make_number (1), intern ("f10")));
1523 }
1524 else if (k0)
1525 Fdefine_key (Vfunction_key_map, build_string (k0),
1526 Fmake_vector (make_number (1), intern (k0_name)));
1527 }
1528
1529 /* Set up cookies for numbered function keys above f10. */
1530 {
1531 char fcap[3], fkey[4];
1532
1533 fcap[0] = 'F'; fcap[2] = '\0';
1534 for (i = 11; i < 64; i++)
1535 {
1536 if (i <= 19)
1537 fcap[1] = '1' + i - 11;
1538 else if (i <= 45)
1539 fcap[1] = 'A' + i - 20;
1540 else
1541 fcap[1] = 'a' + i - 46;
1542
1543 {
1544 char *sequence = tgetstr (fcap, address);
1545 if (sequence)
1546 {
1547 sprintf (fkey, "f%d", i);
1548 Fdefine_key (Vfunction_key_map, build_string (sequence),
1549 Fmake_vector (make_number (1),
1550 intern (fkey)));
1551 }
1552 }
1553 }
1554 }
1555
1556 /*
1557 * Various mappings to try and get a better fit.
1558 */
1559 {
1560 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1561 if (!tgetstr (cap1, address)) \
1562 { \
1563 char *sequence = tgetstr (cap2, address); \
1564 if (sequence) \
1565 Fdefine_key (Vfunction_key_map, build_string (sequence), \
1566 Fmake_vector (make_number (1), \
1567 intern (sym))); \
1568 }
1569
1570 /* if there's no key_next keycap, map key_npage to `next' keysym */
1571 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1572 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1573 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1574 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1575 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1576 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1577 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1578
1579 /* IBM has their own non-standard dialect of terminfo.
1580 If the standard name isn't found, try the IBM name. */
1581 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1582 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1583 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1584 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1585 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1586 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1587 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1588 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1589 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1590 #undef CONDITIONAL_REASSIGN
1591 }
1592
1593 return Qnil;
1594 }
1595
1596 \f
1597 /***********************************************************************
1598 Character Display Information
1599 ***********************************************************************/
1600
1601 static void append_glyph P_ ((struct it *));
1602
1603
1604 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1605 terminal frames if IT->glyph_row != NULL. IT->c is the character
1606 for which to produce glyphs; IT->face_id contains the character's
1607 face. Padding glyphs are appended if IT->c has a IT->pixel_width >
1608 1. */
1609
1610 static void
1611 append_glyph (it)
1612 struct it *it;
1613 {
1614 struct glyph *glyph, *end;
1615 int i;
1616
1617 xassert (it->glyph_row);
1618 glyph = (it->glyph_row->glyphs[it->area]
1619 + it->glyph_row->used[it->area]);
1620 end = it->glyph_row->glyphs[1 + it->area];
1621
1622 for (i = 0;
1623 i < it->pixel_width && glyph < end;
1624 ++i)
1625 {
1626 glyph->type = CHAR_GLYPH;
1627 glyph->pixel_width = 1;
1628 glyph->u.ch = it->c;
1629 glyph->face_id = it->face_id;
1630 glyph->padding_p = i > 0;
1631 glyph->charpos = CHARPOS (it->position);
1632 glyph->object = it->object;
1633
1634 ++it->glyph_row->used[it->area];
1635 ++glyph;
1636 }
1637 }
1638
1639
1640 /* Produce glyphs for the display element described by IT. *IT
1641 specifies what we want to produce a glyph for (character, image, ...),
1642 and where in the glyph matrix we currently are (glyph row and hpos).
1643 produce_glyphs fills in output fields of *IT with information such as the
1644 pixel width and height of a character, and maybe output actual glyphs at
1645 the same time if IT->glyph_row is non-null. See the explanation of
1646 struct display_iterator in dispextern.h for an overview.
1647
1648 produce_glyphs also stores the result of glyph width, ascent
1649 etc. computations in *IT.
1650
1651 IT->glyph_row may be null, in which case produce_glyphs does not
1652 actually fill in the glyphs. This is used in the move_* functions
1653 in xdisp.c for text width and height computations.
1654
1655 Callers usually don't call produce_glyphs directly;
1656 instead they use the macro PRODUCE_GLYPHS. */
1657
1658 void
1659 produce_glyphs (it)
1660 struct it *it;
1661 {
1662 /* If a hook is installed, let it do the work. */
1663 xassert (it->what == IT_CHARACTER
1664 || it->what == IT_COMPOSITION
1665 || it->what == IT_IMAGE
1666 || it->what == IT_STRETCH);
1667
1668 /* Nothing but characters are supported on terminal frames. For a
1669 composition sequence, it->c is the first character of the
1670 sequence. */
1671 xassert (it->what == IT_CHARACTER
1672 || it->what == IT_COMPOSITION);
1673
1674 if (it->c >= 040 && it->c < 0177)
1675 {
1676 it->pixel_width = it->nglyphs = 1;
1677 if (it->glyph_row)
1678 append_glyph (it);
1679 }
1680 else if (it->c == '\n')
1681 it->pixel_width = it->nglyphs = 0;
1682 else if (it->c == '\t')
1683 {
1684 int absolute_x = (it->current_x
1685 + it->continuation_lines_width);
1686 int next_tab_x
1687 = (((1 + absolute_x + it->tab_width - 1)
1688 / it->tab_width)
1689 * it->tab_width);
1690 int nspaces;
1691
1692 /* If part of the TAB has been displayed on the previous line
1693 which is continued now, continuation_lines_width will have
1694 been incremented already by the part that fitted on the
1695 continued line. So, we will get the right number of spaces
1696 here. */
1697 nspaces = next_tab_x - absolute_x;
1698
1699 if (it->glyph_row)
1700 {
1701 int n = nspaces;
1702
1703 it->c = ' ';
1704 it->pixel_width = it->len = 1;
1705
1706 while (n--)
1707 append_glyph (it);
1708
1709 it->c = '\t';
1710 }
1711
1712 it->pixel_width = nspaces;
1713 it->nglyphs = nspaces;
1714 }
1715 else if (SINGLE_BYTE_CHAR_P (it->c))
1716 {
1717 /* Coming here means that it->c is from display table, thus we
1718 must send the code as is to the terminal. Although there's
1719 no way to know how many columns it occupies on a screen, it
1720 is a good assumption that a single byte code has 1-column
1721 width. */
1722 it->pixel_width = it->nglyphs = 1;
1723 if (it->glyph_row)
1724 append_glyph (it);
1725 }
1726 else
1727 {
1728 /* A multi-byte character. The display width is fixed for all
1729 characters of the set. Some of the glyphs may have to be
1730 ignored because they are already displayed in a continued
1731 line. */
1732 int charset = CHAR_CHARSET (it->c);
1733
1734 it->pixel_width = CHARSET_WIDTH (charset);
1735 it->nglyphs = it->pixel_width;
1736
1737 if (it->glyph_row)
1738 append_glyph (it);
1739 }
1740
1741 /* Advance current_x by the pixel width as a convenience for
1742 the caller. */
1743 if (it->area == TEXT_AREA)
1744 it->current_x += it->pixel_width;
1745 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1746 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1747 }
1748
1749
1750 /* Get information about special display element WHAT in an
1751 environment described by IT. WHAT is one of IT_TRUNCATION or
1752 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1753 non-null glyph_row member. This function ensures that fields like
1754 face_id, c, len of IT are left untouched. */
1755
1756 void
1757 produce_special_glyphs (it, what)
1758 struct it *it;
1759 enum display_element_type what;
1760 {
1761 struct it temp_it;
1762
1763 temp_it = *it;
1764 temp_it.dp = NULL;
1765 temp_it.what = IT_CHARACTER;
1766 temp_it.len = 1;
1767 temp_it.object = make_number (0);
1768 bzero (&temp_it.current, sizeof temp_it.current);
1769
1770 if (what == IT_CONTINUATION)
1771 {
1772 /* Continuation glyph. */
1773 if (it->dp
1774 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1775 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1776 {
1777 temp_it.c = FAST_GLYPH_CHAR (XINT (DISP_CONTINUE_GLYPH (it->dp)));
1778 temp_it.len = CHAR_BYTES (temp_it.c);
1779 }
1780 else
1781 temp_it.c = '\\';
1782
1783 produce_glyphs (&temp_it);
1784 it->pixel_width = temp_it.pixel_width;
1785 it->nglyphs = temp_it.pixel_width;
1786 }
1787 else if (what == IT_TRUNCATION)
1788 {
1789 /* Truncation glyph. */
1790 if (it->dp
1791 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1792 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1793 {
1794 temp_it.c = FAST_GLYPH_CHAR (XINT (DISP_TRUNC_GLYPH (it->dp)));
1795 temp_it.len = CHAR_BYTES (temp_it.c);
1796 }
1797 else
1798 temp_it.c = '$';
1799
1800 produce_glyphs (&temp_it);
1801 it->pixel_width = temp_it.pixel_width;
1802 it->nglyphs = temp_it.pixel_width;
1803 }
1804 else
1805 abort ();
1806 }
1807
1808
1809 \f
1810 /***********************************************************************
1811 Faces
1812 ***********************************************************************/
1813
1814 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1815 one of the enumerators from enum no_color_bit, or a bit set built
1816 from them. Some display attributes may not be used together with
1817 color; the termcap capability `NC' specifies which ones. */
1818
1819 #define MAY_USE_WITH_COLORS_P(ATTR) \
1820 (TN_max_colors > 0 \
1821 ? (TN_no_color_video & (ATTR)) == 0 \
1822 : 1)
1823
1824 /* Turn appearances of face FACE_ID on tty frame F on. */
1825
1826 static void
1827 turn_on_face (f, face_id)
1828 struct frame *f;
1829 int face_id;
1830 {
1831 struct face *face = FACE_FROM_ID (f, face_id);
1832 long fg = face->foreground;
1833 long bg = face->background;
1834
1835 /* Do this first because TS_end_standout_mode may be the same
1836 as TS_exit_attribute_mode, which turns all appearances off. */
1837 if (MAY_USE_WITH_COLORS_P (NC_REVERSE))
1838 {
1839 if (TN_max_colors > 0)
1840 {
1841 if (fg >= 0 && bg >= 0)
1842 {
1843 /* If the terminal supports colors, we can set them
1844 below without using reverse video. The face's fg
1845 and bg colors are set as they should appear on
1846 the screen, i.e. they take the inverse-video'ness
1847 of the face already into account. */
1848 }
1849 else if (inverse_video)
1850 {
1851 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1852 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1853 toggle_highlight ();
1854 }
1855 else
1856 {
1857 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1858 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1859 toggle_highlight ();
1860 }
1861 }
1862 else
1863 {
1864 /* If we can't display colors, use reverse video
1865 if the face specifies that. */
1866 if (inverse_video)
1867 {
1868 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1869 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1870 toggle_highlight ();
1871 }
1872 else
1873 {
1874 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1875 || bg == FACE_TTY_DEFAULT_FG_COLOR)
1876 toggle_highlight ();
1877 }
1878 }
1879 }
1880
1881 if (face->tty_bold_p)
1882 {
1883 if (MAY_USE_WITH_COLORS_P (NC_BOLD))
1884 OUTPUT1_IF (FRAME_TTY (f), TS_enter_bold_mode);
1885 }
1886 else if (face->tty_dim_p)
1887 if (MAY_USE_WITH_COLORS_P (NC_DIM))
1888 OUTPUT1_IF (FRAME_TTY (f), TS_enter_dim_mode);
1889
1890 /* Alternate charset and blinking not yet used. */
1891 if (face->tty_alt_charset_p
1892 && MAY_USE_WITH_COLORS_P (NC_ALT_CHARSET))
1893 OUTPUT1_IF (FRAME_TTY (f), TS_enter_alt_charset_mode);
1894
1895 if (face->tty_blinking_p
1896 && MAY_USE_WITH_COLORS_P (NC_BLINK))
1897 OUTPUT1_IF (FRAME_TTY (f), TS_enter_blink_mode);
1898
1899 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (NC_UNDERLINE))
1900 OUTPUT1_IF (FRAME_TTY (f), TS_enter_underline_mode);
1901
1902 if (TN_max_colors > 0)
1903 {
1904 char *p;
1905
1906 if (fg >= 0 && TS_set_foreground)
1907 {
1908 p = tparam (TS_set_foreground, NULL, 0, (int) fg);
1909 OUTPUT (FRAME_TTY (f), p);
1910 xfree (p);
1911 }
1912
1913 if (bg >= 0 && TS_set_background)
1914 {
1915 p = tparam (TS_set_background, NULL, 0, (int) bg);
1916 OUTPUT (FRAME_TTY (f), p);
1917 xfree (p);
1918 }
1919 }
1920 }
1921
1922
1923 /* Turn off appearances of face FACE_ID on tty frame F. */
1924
1925 static void
1926 turn_off_face (f, face_id)
1927 struct frame *f;
1928 int face_id;
1929 {
1930 struct face *face = FACE_FROM_ID (f, face_id);
1931
1932 xassert (face != NULL);
1933
1934 if (TS_exit_attribute_mode)
1935 {
1936 /* Capability "me" will turn off appearance modes double-bright,
1937 half-bright, reverse-video, standout, underline. It may or
1938 may not turn off alt-char-mode. */
1939 if (face->tty_bold_p
1940 || face->tty_dim_p
1941 || face->tty_reverse_p
1942 || face->tty_alt_charset_p
1943 || face->tty_blinking_p
1944 || face->tty_underline_p)
1945 {
1946 OUTPUT1_IF (FRAME_TTY (f), TS_exit_attribute_mode);
1947 if (strcmp (TS_exit_attribute_mode, TS_end_standout_mode) == 0)
1948 standout_mode = 0;
1949 }
1950
1951 if (face->tty_alt_charset_p)
1952 OUTPUT_IF (FRAME_TTY (f), TS_exit_alt_charset_mode);
1953 }
1954 else
1955 {
1956 /* If we don't have "me" we can only have those appearances
1957 that have exit sequences defined. */
1958 if (face->tty_alt_charset_p)
1959 OUTPUT_IF (FRAME_TTY (f), TS_exit_alt_charset_mode);
1960
1961 if (face->tty_underline_p)
1962 OUTPUT_IF (FRAME_TTY (f), TS_exit_underline_mode);
1963 }
1964
1965 /* Switch back to default colors. */
1966 if (TN_max_colors > 0
1967 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
1968 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
1969 || (face->background != FACE_TTY_DEFAULT_COLOR
1970 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
1971 OUTPUT1_IF (FRAME_TTY (f), TS_orig_pair);
1972 }
1973
1974
1975 /* Return non-zero if the terminal on frame F supports all of the
1976 capabilities in CAPS simultaneously, with foreground and background
1977 colors FG and BG. */
1978
1979 int
1980 tty_capable_p (f, caps, fg, bg)
1981 struct frame *f;
1982 unsigned caps;
1983 unsigned long fg, bg;
1984 {
1985 #define TTY_CAPABLE_P_TRY(cap, TS, NC_bit) \
1986 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(NC_bit))) \
1987 return 0;
1988
1989 TTY_CAPABLE_P_TRY (TTY_CAP_INVERSE, TS_standout_mode, NC_REVERSE);
1990 TTY_CAPABLE_P_TRY (TTY_CAP_UNDERLINE, TS_enter_underline_mode, NC_UNDERLINE);
1991 TTY_CAPABLE_P_TRY (TTY_CAP_BOLD, TS_enter_bold_mode, NC_BOLD);
1992 TTY_CAPABLE_P_TRY (TTY_CAP_DIM, TS_enter_dim_mode, NC_DIM);
1993 TTY_CAPABLE_P_TRY (TTY_CAP_BLINK, TS_enter_blink_mode, NC_BLINK);
1994 TTY_CAPABLE_P_TRY (TTY_CAP_ALT_CHARSET, TS_enter_alt_charset_mode, NC_ALT_CHARSET);
1995
1996 /* We can do it! */
1997 return 1;
1998 }
1999
2000
2001 /* Return non-zero if the terminal is capable to display colors. */
2002
2003 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2004 0, 1, 0,
2005 doc: /* Return non-nil if TTY can display colors on DISPLAY. */)
2006 (display)
2007 Lisp_Object display;
2008 {
2009 return TN_max_colors > 0 ? Qt : Qnil;
2010 }
2011
2012 /* Return the number of supported colors. */
2013 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2014 Stty_display_color_cells, 0, 1, 0,
2015 doc: /* Return the number of colors supported by TTY on DISPLAY. */)
2016 (display)
2017 Lisp_Object display;
2018 {
2019 return make_number (TN_max_colors);
2020 }
2021
2022 #ifndef WINDOWSNT
2023
2024 /* Save or restore the default color-related capabilities of this
2025 terminal. */
2026 static void
2027 tty_default_color_capabilities (save)
2028 int save;
2029 {
2030 static char
2031 *default_orig_pair, *default_set_foreground, *default_set_background;
2032 static int default_max_colors, default_max_pairs, default_no_color_video;
2033
2034 if (save)
2035 {
2036 if (default_orig_pair)
2037 xfree (default_orig_pair);
2038 default_orig_pair = TS_orig_pair ? xstrdup (TS_orig_pair) : NULL;
2039
2040 if (default_set_foreground)
2041 xfree (default_set_foreground);
2042 default_set_foreground = TS_set_foreground ? xstrdup (TS_set_foreground)
2043 : NULL;
2044
2045 if (default_set_background)
2046 xfree (default_set_background);
2047 default_set_background = TS_set_background ? xstrdup (TS_set_background)
2048 : NULL;
2049
2050 default_max_colors = TN_max_colors;
2051 default_max_pairs = TN_max_pairs;
2052 default_no_color_video = TN_no_color_video;
2053 }
2054 else
2055 {
2056 TS_orig_pair = default_orig_pair;
2057 TS_set_foreground = default_set_foreground;
2058 TS_set_background = default_set_background;
2059 TN_max_colors = default_max_colors;
2060 TN_max_pairs = default_max_pairs;
2061 TN_no_color_video = default_no_color_video;
2062 }
2063 }
2064
2065 /* Setup one of the standard tty color schemes according to MODE.
2066 MODE's value is generally the number of colors which we want to
2067 support; zero means set up for the default capabilities, the ones
2068 we saw at term_init time; -1 means turn off color support. */
2069 void
2070 tty_setup_colors (mode)
2071 int mode;
2072 {
2073 /* Canonicalize all negative values of MODE. */
2074 if (mode < -1)
2075 mode = -1;
2076
2077 switch (mode)
2078 {
2079 case -1: /* no colors at all */
2080 TN_max_colors = 0;
2081 TN_max_pairs = 0;
2082 TN_no_color_video = 0;
2083 TS_set_foreground = TS_set_background = TS_orig_pair = NULL;
2084 break;
2085 case 0: /* default colors, if any */
2086 default:
2087 tty_default_color_capabilities (0);
2088 break;
2089 case 8: /* 8 standard ANSI colors */
2090 TS_orig_pair = "\033[0m";
2091 #ifdef TERMINFO
2092 TS_set_foreground = "\033[3%p1%dm";
2093 TS_set_background = "\033[4%p1%dm";
2094 #else
2095 TS_set_foreground = "\033[3%dm";
2096 TS_set_background = "\033[4%dm";
2097 #endif
2098 TN_max_colors = 8;
2099 TN_max_pairs = 64;
2100 TN_no_color_video = 0;
2101 break;
2102 }
2103 }
2104
2105 void
2106 set_tty_color_mode (f, val)
2107 struct frame *f;
2108 Lisp_Object val;
2109 {
2110 Lisp_Object color_mode_spec, current_mode_spec;
2111 Lisp_Object color_mode, current_mode;
2112 int mode, old_mode;
2113 extern Lisp_Object Qtty_color_mode;
2114 Lisp_Object tty_color_mode_alist;
2115
2116 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
2117 Qnil);
2118
2119 if (INTEGERP (val))
2120 color_mode = val;
2121 else
2122 {
2123 if (NILP (tty_color_mode_alist))
2124 color_mode_spec = Qnil;
2125 else
2126 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
2127
2128 if (CONSP (color_mode_spec))
2129 color_mode = XCDR (color_mode_spec);
2130 else
2131 color_mode = Qnil;
2132 }
2133
2134 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
2135
2136 if (CONSP (current_mode_spec))
2137 current_mode = XCDR (current_mode_spec);
2138 else
2139 current_mode = Qnil;
2140 if (INTEGERP (color_mode))
2141 mode = XINT (color_mode);
2142 else
2143 mode = 0; /* meaning default */
2144 if (INTEGERP (current_mode))
2145 old_mode = XINT (current_mode);
2146 else
2147 old_mode = 0;
2148
2149 if (mode != old_mode)
2150 {
2151 tty_setup_colors (mode);
2152 /* This recomputes all the faces given the new color
2153 definitions. */
2154 call0 (intern ("tty-set-up-initial-frame-faces"));
2155 redraw_frame (f);
2156 }
2157 }
2158
2159 #endif /* !WINDOWSNT */
2160
2161 \f
2162
2163 struct tty_output *
2164 get_named_tty (name)
2165 char *name;
2166 {
2167 struct tty_output *tty = tty_list;
2168
2169 while (tty) {
2170 if ((tty->name == 0 && name == 0)
2171 || (name && tty->name && !strcmp (tty->name, name)))
2172 return tty;
2173 tty = tty->next;
2174 };
2175
2176 return 0;
2177 }
2178
2179 \f
2180 /***********************************************************************
2181 Initialization
2182 ***********************************************************************/
2183
2184 struct tty_output *
2185 term_dummy_init (void)
2186 {
2187 if (initialized || tty_list)
2188 error ("tty already initialized");
2189
2190 tty_list = xmalloc (sizeof (struct tty_output));
2191 bzero (tty_list, sizeof (struct tty_output));
2192 TTY_NAME (tty_list) = 0;
2193 TTY_INPUT (tty_list) = stdin;
2194 TTY_OUTPUT (tty_list) = stdout;
2195 return tty_list;
2196 }
2197
2198
2199 struct tty_output *
2200 term_init (name, terminal_type)
2201 char *name;
2202 char *terminal_type;
2203 {
2204 char *area;
2205 char **address = &area;
2206 char *buffer = NULL;
2207 int buffer_size = 4096;
2208 register char *p;
2209 int status;
2210 struct frame *sf = XFRAME (selected_frame);
2211
2212 struct tty_output *tty;
2213
2214 tty = get_named_tty (name);
2215 if (tty)
2216 {
2217 /* Return the previously initialized terminal, except if it is the dummy
2218 terminal created for the initial frame. */
2219 if (tty->type)
2220 return tty;
2221 }
2222 else
2223 {
2224 if (!terminal_type)
2225 error ("Unknown terminal type");
2226
2227 tty = (struct tty_output *) xmalloc (sizeof (struct tty_output));
2228 bzero (tty, sizeof (struct tty_output));
2229 tty->next = tty_list;
2230 tty_list = tty;
2231 }
2232
2233 if (name)
2234 {
2235 int fd;
2236 FILE *f;
2237 fd = emacs_open (name, O_RDWR, 0);
2238 if (fd < 0)
2239 {
2240 tty_list = tty->next;
2241 xfree (tty);
2242 error ("could not open file: %s", name);
2243 }
2244 f = fdopen (fd, "w+");
2245 TTY_NAME (tty) = xstrdup (name);
2246 TTY_INPUT (tty) = f;
2247 TTY_OUTPUT (tty) = f;
2248 }
2249 else
2250 {
2251 TTY_NAME (tty) = 0;
2252 TTY_INPUT (tty) = stdin;
2253 TTY_OUTPUT (tty) = stdout;
2254 }
2255
2256 init_sys_modes (tty);
2257
2258 #ifdef WINDOWSNT
2259 initialize_w32_display ();
2260
2261 Wcm_clear ();
2262
2263 area = (char *) xmalloc (2044);
2264
2265 FrameRows = FRAME_LINES (sf);
2266 FrameCols = FRAME_COLS (sf);
2267 specified_window = FRAME_LINES (sf);
2268
2269 delete_in_insert_mode = 1;
2270
2271 UseTabs = 0;
2272 TTY_SCROLL_REGION_OK (tty) = 0;
2273
2274 /* Seems to insert lines when it's not supposed to, messing
2275 up the display. In doing a trace, it didn't seem to be
2276 called much, so I don't think we're losing anything by
2277 turning it off. */
2278 TTY_LINE_INS_DEL_OK (tty) = 0;
2279
2280 TTY_CHAR_INS_DEL_OK (tty) = 1;
2281
2282 baud_rate = 19200;
2283
2284 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2285 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
2286 TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
2287
2288 return tty;
2289 #else /* not WINDOWSNT */
2290
2291 Wcm_clear ();
2292
2293 TTY_TYPE (tty) = xstrdup (terminal_type);
2294
2295 buffer = (char *) xmalloc (buffer_size);
2296 status = tgetent (buffer, terminal_type);
2297 if (status < 0)
2298 {
2299 #ifdef TERMINFO
2300 fatal ("Cannot open terminfo database file");
2301 #else
2302 fatal ("Cannot open termcap database file");
2303 #endif
2304 }
2305 if (status == 0)
2306 {
2307 #ifdef TERMINFO
2308 fatal ("Terminal type %s is not defined.\n\
2309 If that is not the actual type of terminal you have,\n\
2310 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2311 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2312 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2313 terminal_type);
2314 #else
2315 fatal ("Terminal type %s is not defined.\n\
2316 If that is not the actual type of terminal you have,\n\
2317 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2318 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2319 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2320 terminal_type);
2321 #endif
2322 }
2323
2324 #ifndef TERMINFO
2325 if (strlen (buffer) >= buffer_size)
2326 abort ();
2327 buffer_size = strlen (buffer);
2328 #endif
2329 area = (char *) xmalloc (buffer_size);
2330
2331 TS_ins_line = tgetstr ("al", address);
2332 TS_ins_multi_lines = tgetstr ("AL", address);
2333 TS_bell = tgetstr ("bl", address);
2334 BackTab = tgetstr ("bt", address);
2335 TS_clr_to_bottom = tgetstr ("cd", address);
2336 TS_clr_line = tgetstr ("ce", address);
2337 TS_clr_frame = tgetstr ("cl", address);
2338 ColPosition = NULL; /* tgetstr ("ch", address); */
2339 AbsPosition = tgetstr ("cm", address);
2340 CR = tgetstr ("cr", address);
2341 TS_set_scroll_region = tgetstr ("cs", address);
2342 TS_set_scroll_region_1 = tgetstr ("cS", address);
2343 RowPosition = tgetstr ("cv", address);
2344 TS_del_char = tgetstr ("dc", address);
2345 TS_del_multi_chars = tgetstr ("DC", address);
2346 TS_del_line = tgetstr ("dl", address);
2347 TS_del_multi_lines = tgetstr ("DL", address);
2348 TS_delete_mode = tgetstr ("dm", address);
2349 TS_end_delete_mode = tgetstr ("ed", address);
2350 TS_end_insert_mode = tgetstr ("ei", address);
2351 Home = tgetstr ("ho", address);
2352 TS_ins_char = tgetstr ("ic", address);
2353 TS_ins_multi_chars = tgetstr ("IC", address);
2354 TS_insert_mode = tgetstr ("im", address);
2355 TS_pad_inserted_char = tgetstr ("ip", address);
2356 TS_end_keypad_mode = tgetstr ("ke", address);
2357 TS_keypad_mode = tgetstr ("ks", address);
2358 LastLine = tgetstr ("ll", address);
2359 Right = tgetstr ("nd", address);
2360 Down = tgetstr ("do", address);
2361 if (!Down)
2362 Down = tgetstr ("nl", address); /* Obsolete name for "do" */
2363 #ifdef VMS
2364 /* VMS puts a carriage return before each linefeed,
2365 so it is not safe to use linefeeds. */
2366 if (Down && Down[0] == '\n' && Down[1] == '\0')
2367 Down = 0;
2368 #endif /* VMS */
2369 if (tgetflag ("bs"))
2370 Left = "\b"; /* can't possibly be longer! */
2371 else /* (Actually, "bs" is obsolete...) */
2372 Left = tgetstr ("le", address);
2373 if (!Left)
2374 Left = tgetstr ("bc", address); /* Obsolete name for "le" */
2375 TS_pad_char = tgetstr ("pc", address);
2376 TS_repeat = tgetstr ("rp", address);
2377 TS_end_standout_mode = tgetstr ("se", address);
2378 TS_fwd_scroll = tgetstr ("sf", address);
2379 TS_standout_mode = tgetstr ("so", address);
2380 TS_rev_scroll = tgetstr ("sr", address);
2381 Wcm.cm_tab = tgetstr ("ta", address);
2382 TS_end_termcap_modes = tgetstr ("te", address);
2383 TS_termcap_modes = tgetstr ("ti", address);
2384 Up = tgetstr ("up", address);
2385 TS_visible_bell = tgetstr ("vb", address);
2386 TS_cursor_normal = tgetstr ("ve", address);
2387 TS_cursor_visible = tgetstr ("vs", address);
2388 TS_cursor_invisible = tgetstr ("vi", address);
2389 TS_set_window = tgetstr ("wi", address);
2390
2391 TS_enter_underline_mode = tgetstr ("us", address);
2392 TS_exit_underline_mode = tgetstr ("ue", address);
2393 TS_enter_bold_mode = tgetstr ("md", address);
2394 TS_enter_dim_mode = tgetstr ("mh", address);
2395 TS_enter_blink_mode = tgetstr ("mb", address);
2396 TS_enter_reverse_mode = tgetstr ("mr", address);
2397 TS_enter_alt_charset_mode = tgetstr ("as", address);
2398 TS_exit_alt_charset_mode = tgetstr ("ae", address);
2399 TS_exit_attribute_mode = tgetstr ("me", address);
2400
2401 MultiUp = tgetstr ("UP", address);
2402 MultiDown = tgetstr ("DO", address);
2403 MultiLeft = tgetstr ("LE", address);
2404 MultiRight = tgetstr ("RI", address);
2405
2406 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
2407 color because we can't switch back to the default foreground and
2408 background. */
2409 TS_orig_pair = tgetstr ("op", address);
2410 if (TS_orig_pair)
2411 {
2412 TS_set_foreground = tgetstr ("AF", address);
2413 TS_set_background = tgetstr ("AB", address);
2414 if (!TS_set_foreground)
2415 {
2416 /* SVr4. */
2417 TS_set_foreground = tgetstr ("Sf", address);
2418 TS_set_background = tgetstr ("Sb", address);
2419 }
2420
2421 TN_max_colors = tgetnum ("Co");
2422 TN_max_pairs = tgetnum ("pa");
2423
2424 TN_no_color_video = tgetnum ("NC");
2425 if (TN_no_color_video == -1)
2426 TN_no_color_video = 0;
2427 }
2428
2429 tty_default_color_capabilities (1);
2430
2431 MagicWrap = tgetflag ("xn");
2432 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
2433 the former flag imply the latter. */
2434 AutoWrap = MagicWrap || tgetflag ("am");
2435 TTY_MEMORY_BELOW_FRAME (tty) = tgetflag ("db");
2436 TF_hazeltine = tgetflag ("hz");
2437 TTY_MUST_WRITE_SPACES (tty) = tgetflag ("in");
2438 meta_key = tgetflag ("km") || tgetflag ("MT");
2439 TF_insmode_motion = tgetflag ("mi");
2440 TF_standout_motion = tgetflag ("ms");
2441 TF_underscore = tgetflag ("ul");
2442 TF_teleray = tgetflag ("xt");
2443
2444 term_get_fkeys (address);
2445
2446 /* Get frame size from system, or else from termcap. */
2447 {
2448 int height, width;
2449 get_frame_size (&width, &height);
2450 FRAME_COLS (sf) = width;
2451 FRAME_LINES (sf) = height;
2452 }
2453
2454 if (FRAME_COLS (sf) <= 0)
2455 SET_FRAME_COLS (sf, tgetnum ("co"));
2456 else
2457 /* Keep width and external_width consistent */
2458 SET_FRAME_COLS (sf, FRAME_COLS (sf));
2459 if (FRAME_LINES (sf) <= 0)
2460 FRAME_LINES (sf) = tgetnum ("li");
2461
2462 if (FRAME_LINES (sf) < 3 || FRAME_COLS (sf) < 3)
2463 fatal ("Screen size %dx%d is too small",
2464 FRAME_LINES (sf), FRAME_COLS (sf));
2465
2466 #if 0 /* This is not used anywhere. */
2467 TTY_MIN_PADDING_SPEED (tty) = tgetnum ("pb");
2468 #endif
2469
2470 TabWidth = tgetnum ("tw");
2471
2472 #ifdef VMS
2473 /* These capabilities commonly use ^J.
2474 I don't know why, but sending them on VMS does not work;
2475 it causes following spaces to be lost, sometimes.
2476 For now, the simplest fix is to avoid using these capabilities ever. */
2477 if (Down && Down[0] == '\n')
2478 Down = 0;
2479 #endif /* VMS */
2480
2481 if (!TS_bell)
2482 TS_bell = "\07";
2483
2484 if (!TS_fwd_scroll)
2485 TS_fwd_scroll = Down;
2486
2487 PC = TS_pad_char ? *TS_pad_char : 0;
2488
2489 if (TabWidth < 0)
2490 TabWidth = 8;
2491
2492 /* Turned off since /etc/termcap seems to have :ta= for most terminals
2493 and newer termcap doc does not seem to say there is a default.
2494 if (!Wcm.cm_tab)
2495 Wcm.cm_tab = "\t";
2496 */
2497
2498 /* We don't support standout modes that use `magic cookies', so
2499 turn off any that do. */
2500 if (TS_standout_mode && tgetnum ("sg") >= 0)
2501 {
2502 TS_standout_mode = 0;
2503 TS_end_standout_mode = 0;
2504 }
2505 if (TS_enter_underline_mode && tgetnum ("ug") >= 0)
2506 {
2507 TS_enter_underline_mode = 0;
2508 TS_exit_underline_mode = 0;
2509 }
2510
2511 /* If there's no standout mode, try to use underlining instead. */
2512 if (TS_standout_mode == 0)
2513 {
2514 TS_standout_mode = TS_enter_underline_mode;
2515 TS_end_standout_mode = TS_exit_underline_mode;
2516 }
2517
2518 /* If no `se' string, try using a `me' string instead.
2519 If that fails, we can't use standout mode at all. */
2520 if (TS_end_standout_mode == 0)
2521 {
2522 char *s = tgetstr ("me", address);
2523 if (s != 0)
2524 TS_end_standout_mode = s;
2525 else
2526 TS_standout_mode = 0;
2527 }
2528
2529 if (TF_teleray)
2530 {
2531 Wcm.cm_tab = 0;
2532 /* We can't support standout mode, because it uses magic cookies. */
2533 TS_standout_mode = 0;
2534 /* But that means we cannot rely on ^M to go to column zero! */
2535 CR = 0;
2536 /* LF can't be trusted either -- can alter hpos */
2537 /* if move at column 0 thru a line with TS_standout_mode */
2538 Down = 0;
2539 }
2540
2541 /* Special handling for certain terminal types known to need it */
2542
2543 if (!strcmp (terminal_type, "supdup"))
2544 {
2545 TTY_MEMORY_BELOW_FRAME (tty) = 1;
2546 Wcm.cm_losewrap = 1;
2547 }
2548 if (!strncmp (terminal_type, "c10", 3)
2549 || !strcmp (terminal_type, "perq"))
2550 {
2551 /* Supply a makeshift :wi string.
2552 This string is not valid in general since it works only
2553 for windows starting at the upper left corner;
2554 but that is all Emacs uses.
2555
2556 This string works only if the frame is using
2557 the top of the video memory, because addressing is memory-relative.
2558 So first check the :ti string to see if that is true.
2559
2560 It would be simpler if the :wi string could go in the termcap
2561 entry, but it can't because it is not fully valid.
2562 If it were in the termcap entry, it would confuse other programs. */
2563 if (!TS_set_window)
2564 {
2565 p = TS_termcap_modes;
2566 while (*p && strcmp (p, "\033v "))
2567 p++;
2568 if (*p)
2569 TS_set_window = "\033v%C %C %C %C ";
2570 }
2571 /* Termcap entry often fails to have :in: flag */
2572 TTY_MUST_WRITE_SPACES (tty) = 1;
2573 /* :ti string typically fails to have \E^G! in it */
2574 /* This limits scope of insert-char to one line. */
2575 strcpy (area, TS_termcap_modes);
2576 strcat (area, "\033\007!");
2577 TS_termcap_modes = area;
2578 area += strlen (area) + 1;
2579 p = AbsPosition;
2580 /* Change all %+ parameters to %C, to handle
2581 values above 96 correctly for the C100. */
2582 while (*p)
2583 {
2584 if (p[0] == '%' && p[1] == '+')
2585 p[1] = 'C';
2586 p++;
2587 }
2588 }
2589
2590 FrameRows = FRAME_LINES (sf);
2591 FrameCols = FRAME_COLS (sf);
2592 specified_window = FRAME_LINES (sf);
2593
2594 if (Wcm_init () == -1) /* can't do cursor motion */
2595 #ifdef VMS
2596 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2597 It lacks the ability to position the cursor.\n\
2598 If that is not the actual type of terminal you have, use either the\n\
2599 DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
2600 or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
2601 terminal_type);
2602 #else /* not VMS */
2603 # ifdef TERMINFO
2604 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2605 It lacks the ability to position the cursor.\n\
2606 If that is not the actual type of terminal you have,\n\
2607 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2608 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2609 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2610 terminal_type);
2611 # else /* TERMCAP */
2612 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2613 It lacks the ability to position the cursor.\n\
2614 If that is not the actual type of terminal you have,\n\
2615 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2616 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2617 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2618 terminal_type);
2619 # endif /* TERMINFO */
2620 #endif /*VMS */
2621 if (FRAME_LINES (sf) <= 0
2622 || FRAME_COLS (sf) <= 0)
2623 fatal ("The frame size has not been specified");
2624
2625 delete_in_insert_mode
2626 = TS_delete_mode && TS_insert_mode
2627 && !strcmp (TS_delete_mode, TS_insert_mode);
2628
2629 se_is_so = (TS_standout_mode
2630 && TS_end_standout_mode
2631 && !strcmp (TS_standout_mode, TS_end_standout_mode));
2632
2633 UseTabs = tabs_safe_p () && TabWidth == 8;
2634
2635 TTY_SCROLL_REGION_OK (tty)
2636 = (Wcm.cm_abs
2637 && (TS_set_window || TS_set_scroll_region || TS_set_scroll_region_1));
2638
2639 TTY_LINE_INS_DEL_OK (tty)
2640 = (((TS_ins_line || TS_ins_multi_lines)
2641 && (TS_del_line || TS_del_multi_lines))
2642 || (TTY_SCROLL_REGION_OK (tty)
2643 && TS_fwd_scroll && TS_rev_scroll));
2644
2645 TTY_CHAR_INS_DEL_OK (tty)
2646 = ((TS_ins_char || TS_insert_mode
2647 || TS_pad_inserted_char || TS_ins_multi_chars)
2648 && (TS_del_char || TS_del_multi_chars));
2649
2650 TTY_FAST_CLEAR_END_OF_LINE (tty) = TS_clr_line != 0;
2651
2652 init_baud_rate ();
2653 if (read_socket_hook) /* Baudrate is somewhat
2654 meaningless in this case */
2655 baud_rate = 9600;
2656
2657 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2658 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
2659 #endif /* WINDOWSNT */
2660
2661 xfree (buffer);
2662
2663 return tty;
2664 }
2665
2666 /* VARARGS 1 */
2667 void
2668 fatal (str, arg1, arg2)
2669 char *str, *arg1, *arg2;
2670 {
2671 fprintf (stderr, "emacs: ");
2672 fprintf (stderr, str, arg1, arg2);
2673 fprintf (stderr, "\n");
2674 fflush (stderr);
2675 exit (1);
2676 }
2677
2678 void
2679 syms_of_term ()
2680 {
2681 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
2682 doc: /* Non-nil means the system uses terminfo rather than termcap.
2683 This variable can be used by terminal emulator packages. */);
2684 #ifdef TERMINFO
2685 system_uses_terminfo = 1;
2686 #else
2687 system_uses_terminfo = 0;
2688 #endif
2689
2690 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
2691 doc: /* Non-nil means call this function to ring the bell.
2692 The function should accept no arguments. */);
2693 Vring_bell_function = Qnil;
2694
2695 defsubr (&Stty_display_color_p);
2696 defsubr (&Stty_display_color_cells);
2697 }
2698
2699 struct tty_output *
2700 get_current_tty ()
2701 {
2702 return CURTTY();
2703 }
2704
2705
2706 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
2707 (do not change this comment) */