*** empty log message ***
[emacs.git] / src / term.c
blobe407aec2fe1e12fcddbdf294f14f5f38a38b18fe
1 /* Terminal control module for terminals described by TERMCAP
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1998, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
29 #include "termchar.h"
30 #include "termopts.h"
31 #include "lisp.h"
32 #include "charset.h"
33 #include "coding.h"
34 #include "keyboard.h"
35 #include "frame.h"
36 #include "disptab.h"
37 #include "termhooks.h"
38 #include "dispextern.h"
39 #include "window.h"
40 #include "keymap.h"
42 /* For now, don't try to include termcap.h. On some systems,
43 configure finds a non-standard termcap.h that the main build
44 won't find. */
46 #if defined HAVE_TERMCAP_H && 0
47 #include <termcap.h>
48 #else
49 extern void tputs P_ ((const char *, int, int (*)(int)));
50 extern int tgetent P_ ((char *, const char *));
51 extern int tgetflag P_ ((char *id));
52 extern int tgetnum P_ ((char *id));
53 #endif
55 #include "cm.h"
56 #ifdef HAVE_X_WINDOWS
57 #include "xterm.h"
58 #endif
59 #ifdef MAC_OS
60 #include "macterm.h"
61 #endif
63 static void turn_on_face P_ ((struct frame *, int face_id));
64 static void turn_off_face P_ ((struct frame *, int face_id));
65 static void tty_show_cursor P_ ((void));
66 static void tty_hide_cursor P_ ((void));
68 #define OUTPUT(a) \
69 tputs (a, (int) (FRAME_LINES (XFRAME (selected_frame)) - curY), cmputc)
70 #define OUTPUT1(a) tputs (a, 1, cmputc)
71 #define OUTPUTL(a, lines) tputs (a, lines, cmputc)
73 #define OUTPUT_IF(a) \
74 do { \
75 if (a) \
76 tputs (a, (int) (FRAME_LINES (XFRAME (selected_frame)) \
77 - curY), cmputc); \
78 } while (0)
80 #define OUTPUT1_IF(a) do { if (a) tputs (a, 1, cmputc); } while (0)
82 /* Display space properties */
84 extern Lisp_Object Qspace, QCalign_to, QCwidth;
86 /* Function to use to ring the bell. */
88 Lisp_Object Vring_bell_function;
90 /* If true, use "vs", otherwise use "ve" to make the cursor visible. */
92 static int visible_cursor;
94 /* Terminal characteristics that higher levels want to look at.
95 These are all extern'd in termchar.h */
97 int must_write_spaces; /* Nonzero means spaces in the text
98 must actually be output; can't just skip
99 over some columns to leave them blank. */
100 int min_padding_speed; /* Speed below which no padding necessary */
102 int line_ins_del_ok; /* Terminal can insert and delete lines */
103 int char_ins_del_ok; /* Terminal can insert and delete chars */
104 int scroll_region_ok; /* Terminal supports setting the
105 scroll window */
106 int scroll_region_cost; /* Cost of setting a scroll window,
107 measured in characters */
108 int memory_below_frame; /* Terminal remembers lines
109 scrolled off bottom */
110 int fast_clear_end_of_line; /* Terminal has a `ce' string */
112 /* Nonzero means no need to redraw the entire frame on resuming
113 a suspended Emacs. This is useful on terminals with multiple pages,
114 where one page is used for Emacs and another for all else. */
116 int no_redraw_on_reenter;
118 /* Hook functions that you can set to snap out the functions in this file.
119 These are all extern'd in termhooks.h */
121 void (*cursor_to_hook) P_ ((int, int));
122 void (*raw_cursor_to_hook) P_ ((int, int));
123 void (*clear_to_end_hook) P_ ((void));
124 void (*clear_frame_hook) P_ ((void));
125 void (*clear_end_of_line_hook) P_ ((int));
127 void (*ins_del_lines_hook) P_ ((int, int));
129 void (*delete_glyphs_hook) P_ ((int));
131 void (*ring_bell_hook) P_ ((void));
133 void (*reset_terminal_modes_hook) P_ ((void));
134 void (*set_terminal_modes_hook) P_ ((void));
135 void (*update_begin_hook) P_ ((struct frame *));
136 void (*update_end_hook) P_ ((struct frame *));
137 void (*set_terminal_window_hook) P_ ((int));
138 void (*insert_glyphs_hook) P_ ((struct glyph *, int));
139 void (*write_glyphs_hook) P_ ((struct glyph *, int));
140 void (*delete_glyphs_hook) P_ ((int));
142 int (*read_socket_hook) P_ ((int, int, struct input_event *));
144 void (*frame_up_to_date_hook) P_ ((struct frame *));
146 /* Return the current position of the mouse.
148 Set *f to the frame the mouse is in, or zero if the mouse is in no
149 Emacs frame. If it is set to zero, all the other arguments are
150 garbage.
152 If the motion started in a scroll bar, set *bar_window to the
153 scroll bar's window, *part to the part the mouse is currently over,
154 *x to the position of the mouse along the scroll bar, and *y to the
155 overall length of the scroll bar.
157 Otherwise, set *bar_window to Qnil, and *x and *y to the column and
158 row of the character cell the mouse is over.
160 Set *time to the time the mouse was at the returned position.
162 This should clear mouse_moved until the next motion
163 event arrives. */
165 void (*mouse_position_hook) P_ ((FRAME_PTR *f, int insist,
166 Lisp_Object *bar_window,
167 enum scroll_bar_part *part,
168 Lisp_Object *x,
169 Lisp_Object *y,
170 unsigned long *time));
172 /* When reading from a minibuffer in a different frame, Emacs wants
173 to shift the highlight from the selected frame to the mini-buffer's
174 frame; under X, this means it lies about where the focus is.
175 This hook tells the window system code to re-decide where to put
176 the highlight. */
178 void (*frame_rehighlight_hook) P_ ((FRAME_PTR f));
180 /* If we're displaying frames using a window system that can stack
181 frames on top of each other, this hook allows you to bring a frame
182 to the front, or bury it behind all the other windows. If this
183 hook is zero, that means the device we're displaying on doesn't
184 support overlapping frames, so there's no need to raise or lower
185 anything.
187 If RAISE is non-zero, F is brought to the front, before all other
188 windows. If RAISE is zero, F is sent to the back, behind all other
189 windows. */
191 void (*frame_raise_lower_hook) P_ ((FRAME_PTR f, int raise));
193 /* If the value of the frame parameter changed, whis hook is called.
194 For example, if going from fullscreen to not fullscreen this hook
195 may do something OS dependent, like extended window manager hints on X11. */
196 void (*fullscreen_hook) P_ ((struct frame *f));
198 /* Set the vertical scroll bar for WINDOW to have its upper left corner
199 at (TOP, LEFT), and be LENGTH rows high. Set its handle to
200 indicate that we are displaying PORTION characters out of a total
201 of WHOLE characters, starting at POSITION. If WINDOW doesn't yet
202 have a scroll bar, create one for it. */
204 void (*set_vertical_scroll_bar_hook)
205 P_ ((struct window *window,
206 int portion, int whole, int position));
209 /* The following three hooks are used when we're doing a thorough
210 redisplay of the frame. We don't explicitly know which scroll bars
211 are going to be deleted, because keeping track of when windows go
212 away is a real pain - can you say set-window-configuration?
213 Instead, we just assert at the beginning of redisplay that *all*
214 scroll bars are to be removed, and then save scroll bars from the
215 fiery pit when we actually redisplay their window. */
217 /* Arrange for all scroll bars on FRAME to be removed at the next call
218 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
219 `*redeem_scroll_bar_hook' is applied to its window before the judgment.
221 This should be applied to each frame each time its window tree is
222 redisplayed, even if it is not displaying scroll bars at the moment;
223 if the HAS_SCROLL_BARS flag has just been turned off, only calling
224 this and the judge_scroll_bars_hook will get rid of them.
226 If non-zero, this hook should be safe to apply to any frame,
227 whether or not it can support scroll bars, and whether or not it is
228 currently displaying them. */
230 void (*condemn_scroll_bars_hook) P_ ((FRAME_PTR frame));
232 /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
233 Note that it's okay to redeem a scroll bar that is not condemned. */
235 void (*redeem_scroll_bar_hook) P_ ((struct window *window));
237 /* Remove all scroll bars on FRAME that haven't been saved since the
238 last call to `*condemn_scroll_bars_hook'.
240 This should be applied to each frame after each time its window
241 tree is redisplayed, even if it is not displaying scroll bars at the
242 moment; if the HAS_SCROLL_BARS flag has just been turned off, only
243 calling this and condemn_scroll_bars_hook will get rid of them.
245 If non-zero, this hook should be safe to apply to any frame,
246 whether or not it can support scroll bars, and whether or not it is
247 currently displaying them. */
249 void (*judge_scroll_bars_hook) P_ ((FRAME_PTR FRAME));
251 /* Strings, numbers and flags taken from the termcap entry. */
253 char *TS_ins_line; /* "al" */
254 char *TS_ins_multi_lines; /* "AL" (one parameter, # lines to insert) */
255 char *TS_bell; /* "bl" */
256 char *TS_clr_to_bottom; /* "cd" */
257 char *TS_clr_line; /* "ce", clear to end of line */
258 char *TS_clr_frame; /* "cl" */
259 char *TS_set_scroll_region; /* "cs" (2 params, first line and last line) */
260 char *TS_set_scroll_region_1; /* "cS" (4 params: total lines,
261 lines above scroll region, lines below it,
262 total lines again) */
263 char *TS_del_char; /* "dc" */
264 char *TS_del_multi_chars; /* "DC" (one parameter, # chars to delete) */
265 char *TS_del_line; /* "dl" */
266 char *TS_del_multi_lines; /* "DL" (one parameter, # lines to delete) */
267 char *TS_delete_mode; /* "dm", enter character-delete mode */
268 char *TS_end_delete_mode; /* "ed", leave character-delete mode */
269 char *TS_end_insert_mode; /* "ei", leave character-insert mode */
270 char *TS_ins_char; /* "ic" */
271 char *TS_ins_multi_chars; /* "IC" (one parameter, # chars to insert) */
272 char *TS_insert_mode; /* "im", enter character-insert mode */
273 char *TS_pad_inserted_char; /* "ip". Just padding, no commands. */
274 char *TS_end_keypad_mode; /* "ke" */
275 char *TS_keypad_mode; /* "ks" */
276 char *TS_pad_char; /* "pc", char to use as padding */
277 char *TS_repeat; /* "rp" (2 params, # times to repeat
278 and character to be repeated) */
279 char *TS_end_standout_mode; /* "se" */
280 char *TS_fwd_scroll; /* "sf" */
281 char *TS_standout_mode; /* "so" */
282 char *TS_rev_scroll; /* "sr" */
283 char *TS_end_termcap_modes; /* "te" */
284 char *TS_termcap_modes; /* "ti" */
285 char *TS_visible_bell; /* "vb" */
286 char *TS_cursor_normal; /* "ve" */
287 char *TS_cursor_visible; /* "vs" */
288 char *TS_cursor_invisible; /* "vi" */
289 char *TS_set_window; /* "wi" (4 params, start and end of window,
290 each as vpos and hpos) */
292 /* Value of the "NC" (no_color_video) capability, or 0 if not
293 present. */
295 static int TN_no_color_video;
297 /* Meaning of bits in no_color_video. Each bit set means that the
298 corresponding attribute cannot be combined with colors. */
300 enum no_color_bit
302 NC_STANDOUT = 1 << 0,
303 NC_UNDERLINE = 1 << 1,
304 NC_REVERSE = 1 << 2,
305 NC_BLINK = 1 << 3,
306 NC_DIM = 1 << 4,
307 NC_BOLD = 1 << 5,
308 NC_INVIS = 1 << 6,
309 NC_PROTECT = 1 << 7,
310 NC_ALT_CHARSET = 1 << 8
313 /* "md" -- turn on bold (extra bright mode). */
315 char *TS_enter_bold_mode;
317 /* "mh" -- turn on half-bright mode. */
319 char *TS_enter_dim_mode;
321 /* "mb" -- enter blinking mode. */
323 char *TS_enter_blink_mode;
325 /* "mr" -- enter reverse video mode. */
327 char *TS_enter_reverse_mode;
329 /* "us"/"ue" -- start/end underlining. */
331 char *TS_exit_underline_mode, *TS_enter_underline_mode;
333 /* "as"/"ae" -- start/end alternate character set. Not really
334 supported, yet. */
336 char *TS_enter_alt_charset_mode, *TS_exit_alt_charset_mode;
338 /* "me" -- switch appearances off. */
340 char *TS_exit_attribute_mode;
342 /* "Co" -- number of colors. */
344 int TN_max_colors;
346 /* "pa" -- max. number of color pairs on screen. Not handled yet.
347 Could be a problem if not equal to TN_max_colors * TN_max_colors. */
349 int TN_max_pairs;
351 /* "op" -- SVr4 set default pair to its original value. */
353 char *TS_orig_pair;
355 /* "AF"/"AB" or "Sf"/"Sb"-- set ANSI or SVr4 foreground/background color.
356 1 param, the color index. */
358 char *TS_set_foreground, *TS_set_background;
360 int TF_hazeltine; /* termcap hz flag. */
361 int TF_insmode_motion; /* termcap mi flag: can move while in insert mode. */
362 int TF_standout_motion; /* termcap mi flag: can move while in standout mode. */
363 int TF_underscore; /* termcap ul flag: _ underlines if over-struck on
364 non-blank position. Must clear before writing _. */
365 int TF_teleray; /* termcap xt flag: many weird consequences.
366 For t1061. */
368 static int RPov; /* # chars to start a TS_repeat */
370 static int delete_in_insert_mode; /* delete mode == insert mode */
372 static int se_is_so; /* 1 if same string both enters and leaves
373 standout mode */
375 /* internal state */
377 /* The largest frame width in any call to calculate_costs. */
379 int max_frame_cols;
381 /* The largest frame height in any call to calculate_costs. */
383 int max_frame_lines;
385 static int costs_set; /* Nonzero if costs have been calculated. */
387 int insert_mode; /* Nonzero when in insert mode. */
388 int standout_mode; /* Nonzero when in standout mode. */
390 /* Size of window specified by higher levels.
391 This is the number of lines, from the top of frame downwards,
392 which can participate in insert-line/delete-line operations.
394 Effectively it excludes the bottom frame_lines - specified_window_size
395 lines from those operations. */
397 int specified_window;
399 /* Frame currently being redisplayed; 0 if not currently redisplaying.
400 (Direct output does not count). */
402 FRAME_PTR updating_frame;
404 /* Provided for lisp packages. */
406 static int system_uses_terminfo;
408 /* Flag used in tty_show/hide_cursor. */
410 static int tty_cursor_hidden;
412 char *tparam ();
414 extern char *tgetstr ();
417 #ifdef WINDOWSNT
418 /* We aren't X windows, but we aren't termcap either. This makes me
419 uncertain as to what value to use for frame.output_method. For
420 this file, we'll define FRAME_TERMCAP_P to be zero so that our
421 output hooks get called instead of the termcap functions. Probably
422 the best long-term solution is to define an output_windows_nt... */
424 #undef FRAME_TERMCAP_P
425 #define FRAME_TERMCAP_P(_f_) 0
426 #endif /* WINDOWSNT */
428 void
429 ring_bell ()
431 if (!NILP (Vring_bell_function))
433 Lisp_Object function;
435 /* Temporarily set the global variable to nil
436 so that if we get an error, it stays nil
437 and we don't call it over and over.
439 We don't specbind it, because that would carefully
440 restore the bad value if there's an error
441 and make the loop of errors happen anyway. */
443 function = Vring_bell_function;
444 Vring_bell_function = Qnil;
446 call0 (function);
448 Vring_bell_function = function;
450 else if (!FRAME_TERMCAP_P (XFRAME (selected_frame)))
451 (*ring_bell_hook) ();
452 else
453 OUTPUT (TS_visible_bell && visible_bell ? TS_visible_bell : TS_bell);
456 void
457 set_terminal_modes ()
459 if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
461 if (TS_termcap_modes)
462 OUTPUT (TS_termcap_modes);
463 else
465 /* Output enough newlines to scroll all the old screen contents
466 off the screen, so it won't be overwritten and lost. */
467 int i;
468 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
469 putchar ('\n');
472 OUTPUT_IF (visible_cursor ? TS_cursor_visible : TS_cursor_normal);
473 OUTPUT_IF (TS_keypad_mode);
474 losecursor ();
476 else
477 (*set_terminal_modes_hook) ();
480 void
481 reset_terminal_modes ()
483 if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
485 turn_off_highlight ();
486 turn_off_insert ();
487 OUTPUT_IF (TS_end_keypad_mode);
488 OUTPUT_IF (TS_cursor_normal);
489 OUTPUT_IF (TS_end_termcap_modes);
490 OUTPUT_IF (TS_orig_pair);
491 /* Output raw CR so kernel can track the cursor hpos. */
492 cmputc ('\r');
494 else if (reset_terminal_modes_hook)
495 (*reset_terminal_modes_hook) ();
498 void
499 update_begin (f)
500 struct frame *f;
502 updating_frame = f;
503 if (!FRAME_TERMCAP_P (f))
504 update_begin_hook (f);
507 void
508 update_end (f)
509 struct frame *f;
511 if (FRAME_TERMCAP_P (f))
513 if (!XWINDOW (selected_window)->cursor_off_p)
514 tty_show_cursor ();
515 turn_off_insert ();
516 background_highlight ();
518 else
519 update_end_hook (f);
521 updating_frame = NULL;
524 void
525 set_terminal_window (size)
526 int size;
528 if (FRAME_TERMCAP_P (updating_frame))
530 specified_window = size ? size : FRAME_LINES (updating_frame);
531 if (scroll_region_ok)
532 set_scroll_region (0, specified_window);
534 else
535 set_terminal_window_hook (size);
538 void
539 set_scroll_region (start, stop)
540 int start, stop;
542 char *buf;
543 struct frame *sf = XFRAME (selected_frame);
545 if (TS_set_scroll_region)
546 buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
547 else if (TS_set_scroll_region_1)
548 buf = tparam (TS_set_scroll_region_1, 0, 0,
549 FRAME_LINES (sf), start,
550 FRAME_LINES (sf) - stop,
551 FRAME_LINES (sf));
552 else
553 buf = tparam (TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (sf));
555 OUTPUT (buf);
556 xfree (buf);
557 losecursor ();
561 static void
562 turn_on_insert ()
564 if (!insert_mode)
565 OUTPUT (TS_insert_mode);
566 insert_mode = 1;
569 void
570 turn_off_insert ()
572 if (insert_mode)
573 OUTPUT (TS_end_insert_mode);
574 insert_mode = 0;
577 /* Handle highlighting. */
579 void
580 turn_off_highlight ()
582 if (standout_mode)
583 OUTPUT_IF (TS_end_standout_mode);
584 standout_mode = 0;
587 static void
588 turn_on_highlight ()
590 if (!standout_mode)
591 OUTPUT_IF (TS_standout_mode);
592 standout_mode = 1;
595 static void
596 toggle_highlight ()
598 if (standout_mode)
599 turn_off_highlight ();
600 else
601 turn_on_highlight ();
605 /* Make cursor invisible. */
607 static void
608 tty_hide_cursor ()
610 if (tty_cursor_hidden == 0)
612 tty_cursor_hidden = 1;
613 OUTPUT_IF (TS_cursor_invisible);
618 /* Ensure that cursor is visible. */
620 static void
621 tty_show_cursor ()
623 if (tty_cursor_hidden)
625 tty_cursor_hidden = 0;
626 OUTPUT_IF (TS_cursor_normal);
627 if (visible_cursor)
628 OUTPUT_IF (TS_cursor_visible);
633 /* Set standout mode to the state it should be in for
634 empty space inside windows. What this is,
635 depends on the user option inverse-video. */
637 void
638 background_highlight ()
640 if (inverse_video)
641 turn_on_highlight ();
642 else
643 turn_off_highlight ();
646 /* Set standout mode to the mode specified for the text to be output. */
648 static void
649 highlight_if_desired ()
651 if (inverse_video)
652 turn_on_highlight ();
653 else
654 turn_off_highlight ();
658 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
659 frame-relative coordinates. */
661 void
662 cursor_to (vpos, hpos)
663 int vpos, hpos;
665 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
667 if (! FRAME_TERMCAP_P (f) && cursor_to_hook)
669 (*cursor_to_hook) (vpos, hpos);
670 return;
673 /* Detect the case where we are called from reset_sys_modes
674 and the costs have never been calculated. Do nothing. */
675 if (! costs_set)
676 return;
678 if (curY == vpos && curX == hpos)
679 return;
680 if (!TF_standout_motion)
681 background_highlight ();
682 if (!TF_insmode_motion)
683 turn_off_insert ();
684 cmgoto (vpos, hpos);
687 /* Similar but don't take any account of the wasted characters. */
689 void
690 raw_cursor_to (row, col)
691 int row, col;
693 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
694 if (! FRAME_TERMCAP_P (f))
696 (*raw_cursor_to_hook) (row, col);
697 return;
699 if (curY == row && curX == col)
700 return;
701 if (!TF_standout_motion)
702 background_highlight ();
703 if (!TF_insmode_motion)
704 turn_off_insert ();
705 cmgoto (row, col);
708 /* Erase operations */
710 /* clear from cursor to end of frame */
711 void
712 clear_to_end ()
714 register int i;
716 if (clear_to_end_hook && ! FRAME_TERMCAP_P (updating_frame))
718 (*clear_to_end_hook) ();
719 return;
721 if (TS_clr_to_bottom)
723 background_highlight ();
724 OUTPUT (TS_clr_to_bottom);
726 else
728 for (i = curY; i < FRAME_LINES (XFRAME (selected_frame)); i++)
730 cursor_to (i, 0);
731 clear_end_of_line (FRAME_COLS (XFRAME (selected_frame)));
736 /* Clear entire frame */
738 void
739 clear_frame ()
741 struct frame *sf = XFRAME (selected_frame);
743 if (clear_frame_hook
744 && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : sf)))
746 (*clear_frame_hook) ();
747 return;
749 if (TS_clr_frame)
751 background_highlight ();
752 OUTPUT (TS_clr_frame);
753 cmat (0, 0);
755 else
757 cursor_to (0, 0);
758 clear_to_end ();
762 /* Clear from cursor to end of line.
763 Assume that the line is already clear starting at column first_unused_hpos.
765 Note that the cursor may be moved, on terminals lacking a `ce' string. */
767 void
768 clear_end_of_line (first_unused_hpos)
769 int first_unused_hpos;
771 register int i;
773 if (clear_end_of_line_hook
774 && ! FRAME_TERMCAP_P ((updating_frame
775 ? updating_frame
776 : XFRAME (selected_frame))))
778 (*clear_end_of_line_hook) (first_unused_hpos);
779 return;
782 /* Detect the case where we are called from reset_sys_modes
783 and the costs have never been calculated. Do nothing. */
784 if (! costs_set)
785 return;
787 if (curX >= first_unused_hpos)
788 return;
789 background_highlight ();
790 if (TS_clr_line)
792 OUTPUT1 (TS_clr_line);
794 else
795 { /* have to do it the hard way */
796 struct frame *sf = XFRAME (selected_frame);
797 turn_off_insert ();
799 /* Do not write in last row last col with Auto-wrap on. */
800 if (AutoWrap && curY == FRAME_LINES (sf) - 1
801 && first_unused_hpos == FRAME_COLS (sf))
802 first_unused_hpos--;
804 for (i = curX; i < first_unused_hpos; i++)
806 if (termscript)
807 fputc (' ', termscript);
808 putchar (' ');
810 cmplus (first_unused_hpos - curX);
814 /* Buffer to store the source and result of code conversion for terminal. */
815 static unsigned char *encode_terminal_buf;
816 /* Allocated size of the above buffer. */
817 static int encode_terminal_bufsize;
819 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
820 Set CODING->produced to the byte-length of the resulting byte
821 sequence, and return a pointer to that byte sequence. */
823 unsigned char *
824 encode_terminal_code (src, src_len, coding)
825 struct glyph *src;
826 int src_len;
827 struct coding_system *coding;
829 struct glyph *src_end = src + src_len;
830 register GLYPH g;
831 unsigned char *buf;
832 int nchars, nbytes, required;
833 register int tlen = GLYPH_TABLE_LENGTH;
834 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
836 /* Allocate sufficient size of buffer to store all characters in
837 multibyte-form. But, it may be enlarged on demand if
838 Vglyph_table contains a string. */
839 required = MAX_MULTIBYTE_LENGTH * src_len;
840 if (encode_terminal_bufsize < required)
842 if (encode_terminal_bufsize == 0)
843 encode_terminal_buf = xmalloc (required);
844 else
845 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
846 encode_terminal_bufsize = required;
849 buf = encode_terminal_buf;
850 nchars = 0;
851 while (src < src_end)
853 /* We must skip glyphs to be padded for a wide character. */
854 if (! CHAR_GLYPH_PADDING_P (*src))
856 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
858 if (g < 0 || g >= tlen)
860 /* This glyph doesn't has an entry in Vglyph_table. */
861 if (CHAR_VALID_P (src->u.ch, 0))
862 buf += CHAR_STRING (src->u.ch, buf);
863 else
864 *buf++ = SPACEGLYPH;
865 nchars++;
867 else
869 /* This glyph has an entry in Vglyph_table,
870 so process any alias before testing for simpleness. */
871 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
873 if (GLYPH_SIMPLE_P (tbase, tlen, g))
875 int c = FAST_GLYPH_CHAR (g);
877 if (CHAR_VALID_P (c, 0))
878 buf += CHAR_STRING (c, buf);
879 else
880 *buf++ = SPACEGLYPH;
881 nchars++;
883 else
885 /* We have a string in Vglyph_table. */
886 Lisp_Object string;
888 string = tbase[g];
889 if (! STRING_MULTIBYTE (string))
890 string = string_to_multibyte (string);
891 nbytes = buf - encode_terminal_buf;
892 if (encode_terminal_bufsize < nbytes + SBYTES (string))
894 encode_terminal_bufsize = nbytes + SBYTES (string);
895 encode_terminal_buf = xrealloc (encode_terminal_buf,
896 encode_terminal_bufsize);
897 buf = encode_terminal_buf + nbytes;
899 bcopy (SDATA (string), buf, SBYTES (string));
900 buf += SBYTES (string);
901 nchars += SCHARS (string);
905 src++;
908 nbytes = buf - encode_terminal_buf;
909 coding->src_multibyte = 1;
910 coding->dst_multibyte = 0;
911 if (SYMBOLP (coding->pre_write_conversion)
912 && ! NILP (Ffboundp (coding->pre_write_conversion)))
914 run_pre_write_conversin_on_c_str (&encode_terminal_buf,
915 &encode_terminal_bufsize,
916 nchars, nbytes, coding);
917 nchars = coding->produced_char;
918 nbytes = coding->produced;
920 required = nbytes + encoding_buffer_size (coding, nbytes);
921 if (encode_terminal_bufsize < required)
923 encode_terminal_bufsize = required;
924 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
927 encode_coding (coding, encode_terminal_buf, encode_terminal_buf + nbytes,
928 nbytes, encode_terminal_bufsize - nbytes);
929 return encode_terminal_buf + nbytes;
932 void
933 write_glyphs (string, len)
934 register struct glyph *string;
935 register int len;
937 struct frame *sf = XFRAME (selected_frame);
938 struct frame *f = updating_frame ? updating_frame : sf;
939 unsigned char *conversion_buffer;
940 struct coding_system *coding;
942 if (write_glyphs_hook
943 && ! FRAME_TERMCAP_P (f))
945 (*write_glyphs_hook) (string, len);
946 return;
949 turn_off_insert ();
950 tty_hide_cursor ();
952 /* Don't dare write in last column of bottom line, if Auto-Wrap,
953 since that would scroll the whole frame on some terminals. */
955 if (AutoWrap
956 && curY + 1 == FRAME_LINES (sf)
957 && (curX + len) == FRAME_COLS (sf))
958 len --;
959 if (len <= 0)
960 return;
962 cmplus (len);
964 /* If terminal_coding does any conversion, use it, otherwise use
965 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
966 because it always return 1 if the member src_multibyte is 1. */
967 coding = (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
968 ? &terminal_coding : &safe_terminal_coding);
969 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
970 the tail. */
971 coding->mode &= ~CODING_MODE_LAST_BLOCK;
973 while (len > 0)
975 /* Identify a run of glyphs with the same face. */
976 int face_id = string->face_id;
977 int n;
979 for (n = 1; n < len; ++n)
980 if (string[n].face_id != face_id)
981 break;
983 /* Turn appearance modes of the face of the run on. */
984 highlight_if_desired ();
985 turn_on_face (f, face_id);
987 if (n == len)
988 /* This is the last run. */
989 coding->mode |= CODING_MODE_LAST_BLOCK;
990 conversion_buffer = encode_terminal_code (string, n, coding);
991 if (coding->produced > 0)
993 fwrite (conversion_buffer, 1, coding->produced, stdout);
994 if (ferror (stdout))
995 clearerr (stdout);
996 if (termscript)
997 fwrite (conversion_buffer, 1, coding->produced, termscript);
999 len -= n;
1000 string += n;
1002 /* Turn appearance modes off. */
1003 turn_off_face (f, face_id);
1004 turn_off_highlight ();
1007 cmcheckmagic ();
1010 /* If start is zero, insert blanks instead of a string at start */
1012 void
1013 insert_glyphs (start, len)
1014 register struct glyph *start;
1015 register int len;
1017 char *buf;
1018 struct glyph *glyph = NULL;
1019 struct frame *f, *sf;
1020 unsigned char *conversion_buffer;
1021 unsigned char space[1];
1022 struct coding_system *coding;
1024 if (len <= 0)
1025 return;
1027 if (insert_glyphs_hook)
1029 (*insert_glyphs_hook) (start, len);
1030 return;
1033 sf = XFRAME (selected_frame);
1034 f = updating_frame ? updating_frame : sf;
1036 if (TS_ins_multi_chars)
1038 buf = tparam (TS_ins_multi_chars, 0, 0, len);
1039 OUTPUT1 (buf);
1040 xfree (buf);
1041 if (start)
1042 write_glyphs (start, len);
1043 return;
1046 turn_on_insert ();
1047 cmplus (len);
1049 if (! start)
1050 space[0] = SPACEGLYPH;
1052 /* If terminal_coding does any conversion, use it, otherwise use
1053 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
1054 because it always return 1 if the member src_multibyte is 1. */
1055 coding = (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
1056 ? &terminal_coding : &safe_terminal_coding);
1057 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
1058 the tail. */
1059 coding->mode &= ~CODING_MODE_LAST_BLOCK;
1061 while (len-- > 0)
1063 OUTPUT1_IF (TS_ins_char);
1064 if (!start)
1066 conversion_buffer = space;
1067 coding->produced = 1;
1069 else
1071 highlight_if_desired ();
1072 turn_on_face (f, start->face_id);
1073 glyph = start;
1074 ++start;
1075 /* We must open sufficient space for a character which
1076 occupies more than one column. */
1077 while (len && CHAR_GLYPH_PADDING_P (*start))
1079 OUTPUT1_IF (TS_ins_char);
1080 start++, len--;
1083 if (len <= 0)
1084 /* This is the last glyph. */
1085 coding->mode |= CODING_MODE_LAST_BLOCK;
1087 conversion_buffer = encode_terminal_code (glyph, 1, coding);
1090 if (coding->produced > 0)
1092 fwrite (conversion_buffer, 1, coding->produced, stdout);
1093 if (ferror (stdout))
1094 clearerr (stdout);
1095 if (termscript)
1096 fwrite (conversion_buffer, 1, coding->produced, termscript);
1099 OUTPUT1_IF (TS_pad_inserted_char);
1100 if (start)
1102 turn_off_face (f, glyph->face_id);
1103 turn_off_highlight ();
1107 cmcheckmagic ();
1110 void
1111 delete_glyphs (n)
1112 register int n;
1114 char *buf;
1115 register int i;
1117 if (delete_glyphs_hook && ! FRAME_TERMCAP_P (updating_frame))
1119 (*delete_glyphs_hook) (n);
1120 return;
1123 if (delete_in_insert_mode)
1125 turn_on_insert ();
1127 else
1129 turn_off_insert ();
1130 OUTPUT_IF (TS_delete_mode);
1133 if (TS_del_multi_chars)
1135 buf = tparam (TS_del_multi_chars, 0, 0, n);
1136 OUTPUT1 (buf);
1137 xfree (buf);
1139 else
1140 for (i = 0; i < n; i++)
1141 OUTPUT1 (TS_del_char);
1142 if (!delete_in_insert_mode)
1143 OUTPUT_IF (TS_end_delete_mode);
1146 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
1148 void
1149 ins_del_lines (vpos, n)
1150 int vpos, n;
1152 char *multi = n > 0 ? TS_ins_multi_lines : TS_del_multi_lines;
1153 char *single = n > 0 ? TS_ins_line : TS_del_line;
1154 char *scroll = n > 0 ? TS_rev_scroll : TS_fwd_scroll;
1155 struct frame *sf;
1157 register int i = n > 0 ? n : -n;
1158 register char *buf;
1160 if (ins_del_lines_hook && ! FRAME_TERMCAP_P (updating_frame))
1162 (*ins_del_lines_hook) (vpos, n);
1163 return;
1166 sf = XFRAME (selected_frame);
1168 /* If the lines below the insertion are being pushed
1169 into the end of the window, this is the same as clearing;
1170 and we know the lines are already clear, since the matching
1171 deletion has already been done. So can ignore this. */
1172 /* If the lines below the deletion are blank lines coming
1173 out of the end of the window, don't bother,
1174 as there will be a matching inslines later that will flush them. */
1175 if (scroll_region_ok && vpos + i >= specified_window)
1176 return;
1177 if (!memory_below_frame && vpos + i >= FRAME_LINES (sf))
1178 return;
1180 if (multi)
1182 raw_cursor_to (vpos, 0);
1183 background_highlight ();
1184 buf = tparam (multi, 0, 0, i);
1185 OUTPUT (buf);
1186 xfree (buf);
1188 else if (single)
1190 raw_cursor_to (vpos, 0);
1191 background_highlight ();
1192 while (--i >= 0)
1193 OUTPUT (single);
1194 if (TF_teleray)
1195 curX = 0;
1197 else
1199 set_scroll_region (vpos, specified_window);
1200 if (n < 0)
1201 raw_cursor_to (specified_window - 1, 0);
1202 else
1203 raw_cursor_to (vpos, 0);
1204 background_highlight ();
1205 while (--i >= 0)
1206 OUTPUTL (scroll, specified_window - vpos);
1207 set_scroll_region (0, specified_window);
1210 if (!scroll_region_ok && memory_below_frame && n < 0)
1212 cursor_to (FRAME_LINES (sf) + n, 0);
1213 clear_to_end ();
1217 /* Compute cost of sending "str", in characters,
1218 not counting any line-dependent padding. */
1221 string_cost (str)
1222 char *str;
1224 cost = 0;
1225 if (str)
1226 tputs (str, 0, evalcost);
1227 return cost;
1230 /* Compute cost of sending "str", in characters,
1231 counting any line-dependent padding at one line. */
1233 static int
1234 string_cost_one_line (str)
1235 char *str;
1237 cost = 0;
1238 if (str)
1239 tputs (str, 1, evalcost);
1240 return cost;
1243 /* Compute per line amount of line-dependent padding,
1244 in tenths of characters. */
1247 per_line_cost (str)
1248 register char *str;
1250 cost = 0;
1251 if (str)
1252 tputs (str, 0, evalcost);
1253 cost = - cost;
1254 if (str)
1255 tputs (str, 10, evalcost);
1256 return cost;
1259 #ifndef old
1260 /* char_ins_del_cost[n] is cost of inserting N characters.
1261 char_ins_del_cost[-n] is cost of deleting N characters.
1262 The length of this vector is based on max_frame_cols. */
1264 int *char_ins_del_vector;
1266 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1267 #endif
1269 /* ARGSUSED */
1270 static void
1271 calculate_ins_del_char_costs (frame)
1272 FRAME_PTR frame;
1274 int ins_startup_cost, del_startup_cost;
1275 int ins_cost_per_char, del_cost_per_char;
1276 register int i;
1277 register int *p;
1279 if (TS_ins_multi_chars)
1281 ins_cost_per_char = 0;
1282 ins_startup_cost = string_cost_one_line (TS_ins_multi_chars);
1284 else if (TS_ins_char || TS_pad_inserted_char
1285 || (TS_insert_mode && TS_end_insert_mode))
1287 ins_startup_cost = (30 * (string_cost (TS_insert_mode)
1288 + string_cost (TS_end_insert_mode))) / 100;
1289 ins_cost_per_char = (string_cost_one_line (TS_ins_char)
1290 + string_cost_one_line (TS_pad_inserted_char));
1292 else
1294 ins_startup_cost = 9999;
1295 ins_cost_per_char = 0;
1298 if (TS_del_multi_chars)
1300 del_cost_per_char = 0;
1301 del_startup_cost = string_cost_one_line (TS_del_multi_chars);
1303 else if (TS_del_char)
1305 del_startup_cost = (string_cost (TS_delete_mode)
1306 + string_cost (TS_end_delete_mode));
1307 if (delete_in_insert_mode)
1308 del_startup_cost /= 2;
1309 del_cost_per_char = string_cost_one_line (TS_del_char);
1311 else
1313 del_startup_cost = 9999;
1314 del_cost_per_char = 0;
1317 /* Delete costs are at negative offsets */
1318 p = &char_ins_del_cost (frame)[0];
1319 for (i = FRAME_COLS (frame); --i >= 0;)
1320 *--p = (del_startup_cost += del_cost_per_char);
1322 /* Doing nothing is free */
1323 p = &char_ins_del_cost (frame)[0];
1324 *p++ = 0;
1326 /* Insert costs are at positive offsets */
1327 for (i = FRAME_COLS (frame); --i >= 0;)
1328 *p++ = (ins_startup_cost += ins_cost_per_char);
1331 void
1332 calculate_costs (frame)
1333 FRAME_PTR frame;
1335 register char *f = (TS_set_scroll_region
1336 ? TS_set_scroll_region
1337 : TS_set_scroll_region_1);
1339 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1341 scroll_region_cost = string_cost (f);
1343 /* These variables are only used for terminal stuff. They are allocated
1344 once for the terminal frame of X-windows emacs, but not used afterwards.
1346 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1347 X turns off char_ins_del_ok. */
1349 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1350 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1352 costs_set = 1;
1354 if (char_ins_del_vector != 0)
1355 char_ins_del_vector
1356 = (int *) xrealloc (char_ins_del_vector,
1357 (sizeof (int)
1358 + 2 * max_frame_cols * sizeof (int)));
1359 else
1360 char_ins_del_vector
1361 = (int *) xmalloc (sizeof (int)
1362 + 2 * max_frame_cols * sizeof (int));
1364 bzero (char_ins_del_vector, (sizeof (int)
1365 + 2 * max_frame_cols * sizeof (int)));
1367 if (f && (!TS_ins_line && !TS_del_line))
1368 do_line_insertion_deletion_costs (frame,
1369 TS_rev_scroll, TS_ins_multi_lines,
1370 TS_fwd_scroll, TS_del_multi_lines,
1371 f, f, 1);
1372 else
1373 do_line_insertion_deletion_costs (frame,
1374 TS_ins_line, TS_ins_multi_lines,
1375 TS_del_line, TS_del_multi_lines,
1376 0, 0, 1);
1378 calculate_ins_del_char_costs (frame);
1380 /* Don't use TS_repeat if its padding is worse than sending the chars */
1381 if (TS_repeat && per_line_cost (TS_repeat) * baud_rate < 9000)
1382 RPov = string_cost (TS_repeat);
1383 else
1384 RPov = FRAME_COLS (frame) * 2;
1386 cmcostinit (); /* set up cursor motion costs */
1389 struct fkey_table {
1390 char *cap, *name;
1393 /* Termcap capability names that correspond directly to X keysyms.
1394 Some of these (marked "terminfo") aren't supplied by old-style
1395 (Berkeley) termcap entries. They're listed in X keysym order;
1396 except we put the keypad keys first, so that if they clash with
1397 other keys (as on the IBM PC keyboard) they get overridden.
1400 static struct fkey_table keys[] =
1402 {"kh", "home"}, /* termcap */
1403 {"kl", "left"}, /* termcap */
1404 {"ku", "up"}, /* termcap */
1405 {"kr", "right"}, /* termcap */
1406 {"kd", "down"}, /* termcap */
1407 {"%8", "prior"}, /* terminfo */
1408 {"%5", "next"}, /* terminfo */
1409 {"@7", "end"}, /* terminfo */
1410 {"@1", "begin"}, /* terminfo */
1411 {"*6", "select"}, /* terminfo */
1412 {"%9", "print"}, /* terminfo */
1413 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1415 * "insert" --- see below
1417 {"&8", "undo"}, /* terminfo */
1418 {"%0", "redo"}, /* terminfo */
1419 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1420 {"@0", "find"}, /* terminfo */
1421 {"@2", "cancel"}, /* terminfo */
1422 {"%1", "help"}, /* terminfo */
1424 * "break" goes here, but can't be reliably intercepted with termcap
1426 {"&4", "reset"}, /* terminfo --- actually `restart' */
1428 * "system" and "user" --- no termcaps
1430 {"kE", "clearline"}, /* terminfo */
1431 {"kA", "insertline"}, /* terminfo */
1432 {"kL", "deleteline"}, /* terminfo */
1433 {"kI", "insertchar"}, /* terminfo */
1434 {"kD", "deletechar"}, /* terminfo */
1435 {"kB", "backtab"}, /* terminfo */
1437 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1439 {"@8", "kp-enter"}, /* terminfo */
1441 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1442 * "kp-multiply", "kp-add", "kp-separator",
1443 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1444 * --- no termcaps for any of these.
1446 {"K4", "kp-1"}, /* terminfo */
1448 * "kp-2" --- no termcap
1450 {"K5", "kp-3"}, /* terminfo */
1452 * "kp-4" --- no termcap
1454 {"K2", "kp-5"}, /* terminfo */
1456 * "kp-6" --- no termcap
1458 {"K1", "kp-7"}, /* terminfo */
1460 * "kp-8" --- no termcap
1462 {"K3", "kp-9"}, /* terminfo */
1464 * "kp-equal" --- no termcap
1466 {"k1", "f1"},
1467 {"k2", "f2"},
1468 {"k3", "f3"},
1469 {"k4", "f4"},
1470 {"k5", "f5"},
1471 {"k6", "f6"},
1472 {"k7", "f7"},
1473 {"k8", "f8"},
1474 {"k9", "f9"},
1476 {"&0", "S-cancel"}, /*shifted cancel key*/
1477 {"&9", "S-begin"}, /*shifted begin key*/
1478 {"*0", "S-find"}, /*shifted find key*/
1479 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1480 {"*4", "S-delete"}, /*shifted delete-character key*/
1481 {"*7", "S-end"}, /*shifted end key*/
1482 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1483 {"#1", "S-help"}, /*shifted help key*/
1484 {"#2", "S-home"}, /*shifted home key*/
1485 {"#3", "S-insert"}, /*shifted insert-character key*/
1486 {"#4", "S-left"}, /*shifted left-arrow key*/
1487 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1488 {"%c", "S-next"}, /*shifted next key*/
1489 {"%e", "S-prior"}, /*shifted previous key*/
1490 {"%f", "S-print"}, /*shifted print key*/
1491 {"%g", "S-redo"}, /*shifted redo key*/
1492 {"%i", "S-right"}, /*shifted right-arrow key*/
1493 {"!3", "S-undo"} /*shifted undo key*/
1496 static char **term_get_fkeys_arg;
1497 static Lisp_Object term_get_fkeys_1 ();
1499 /* Find the escape codes sent by the function keys for Vfunction_key_map.
1500 This function scans the termcap function key sequence entries, and
1501 adds entries to Vfunction_key_map for each function key it finds. */
1503 void
1504 term_get_fkeys (address)
1505 char **address;
1507 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1508 errors during the call. The only errors should be from Fdefine_key
1509 when given a key sequence containing an invalid prefix key. If the
1510 termcap defines function keys which use a prefix that is already bound
1511 to a command by the default bindings, we should silently ignore that
1512 function key specification, rather than giving the user an error and
1513 refusing to run at all on such a terminal. */
1515 extern Lisp_Object Fidentity ();
1516 term_get_fkeys_arg = address;
1517 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1520 static Lisp_Object
1521 term_get_fkeys_1 ()
1523 int i;
1525 char **address = term_get_fkeys_arg;
1527 /* This can happen if CANNOT_DUMP or with strange options. */
1528 if (!initialized)
1529 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
1531 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1533 char *sequence = tgetstr (keys[i].cap, address);
1534 if (sequence)
1535 Fdefine_key (Vfunction_key_map, build_string (sequence),
1536 Fmake_vector (make_number (1),
1537 intern (keys[i].name)));
1540 /* The uses of the "k0" capability are inconsistent; sometimes it
1541 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1542 We will attempt to politely accommodate both systems by testing for
1543 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1546 char *k_semi = tgetstr ("k;", address);
1547 char *k0 = tgetstr ("k0", address);
1548 char *k0_name = "f10";
1550 if (k_semi)
1552 if (k0)
1553 /* Define f0 first, so that f10 takes precedence in case the
1554 key sequences happens to be the same. */
1555 Fdefine_key (Vfunction_key_map, build_string (k0),
1556 Fmake_vector (make_number (1), intern ("f0")));
1557 Fdefine_key (Vfunction_key_map, build_string (k_semi),
1558 Fmake_vector (make_number (1), intern ("f10")));
1560 else if (k0)
1561 Fdefine_key (Vfunction_key_map, build_string (k0),
1562 Fmake_vector (make_number (1), intern (k0_name)));
1565 /* Set up cookies for numbered function keys above f10. */
1567 char fcap[3], fkey[4];
1569 fcap[0] = 'F'; fcap[2] = '\0';
1570 for (i = 11; i < 64; i++)
1572 if (i <= 19)
1573 fcap[1] = '1' + i - 11;
1574 else if (i <= 45)
1575 fcap[1] = 'A' + i - 20;
1576 else
1577 fcap[1] = 'a' + i - 46;
1580 char *sequence = tgetstr (fcap, address);
1581 if (sequence)
1583 sprintf (fkey, "f%d", i);
1584 Fdefine_key (Vfunction_key_map, build_string (sequence),
1585 Fmake_vector (make_number (1),
1586 intern (fkey)));
1593 * Various mappings to try and get a better fit.
1596 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1597 if (!tgetstr (cap1, address)) \
1599 char *sequence = tgetstr (cap2, address); \
1600 if (sequence) \
1601 Fdefine_key (Vfunction_key_map, build_string (sequence), \
1602 Fmake_vector (make_number (1), \
1603 intern (sym))); \
1606 /* if there's no key_next keycap, map key_npage to `next' keysym */
1607 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1608 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1609 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1610 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1611 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1612 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1613 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1615 /* IBM has their own non-standard dialect of terminfo.
1616 If the standard name isn't found, try the IBM name. */
1617 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1618 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1619 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1620 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1621 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1622 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1623 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1624 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1625 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1626 #undef CONDITIONAL_REASSIGN
1629 return Qnil;
1633 /***********************************************************************
1634 Character Display Information
1635 ***********************************************************************/
1637 /* Avoid name clash with functions defined in xterm.c */
1638 #ifdef static
1639 #define append_glyph append_glyph_term
1640 #define produce_stretch_glyph produce_stretch_glyph_term
1641 #endif
1643 static void append_glyph P_ ((struct it *));
1644 static void produce_stretch_glyph P_ ((struct it *));
1647 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1648 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1649 the character for which to produce glyphs; IT->face_id contains the
1650 character's face. Padding glyphs are appended if IT->c has a
1651 IT->pixel_width > 1. */
1653 static void
1654 append_glyph (it)
1655 struct it *it;
1657 struct glyph *glyph, *end;
1658 int i;
1660 xassert (it->glyph_row);
1661 glyph = (it->glyph_row->glyphs[it->area]
1662 + it->glyph_row->used[it->area]);
1663 end = it->glyph_row->glyphs[1 + it->area];
1665 for (i = 0;
1666 i < it->pixel_width && glyph < end;
1667 ++i)
1669 glyph->type = CHAR_GLYPH;
1670 glyph->pixel_width = 1;
1671 glyph->u.ch = it->char_to_display;
1672 glyph->face_id = it->face_id;
1673 glyph->padding_p = i > 0;
1674 glyph->charpos = CHARPOS (it->position);
1675 glyph->object = it->object;
1677 ++it->glyph_row->used[it->area];
1678 ++glyph;
1683 /* Produce glyphs for the display element described by IT. *IT
1684 specifies what we want to produce a glyph for (character, image, ...),
1685 and where in the glyph matrix we currently are (glyph row and hpos).
1686 produce_glyphs fills in output fields of *IT with information such as the
1687 pixel width and height of a character, and maybe output actual glyphs at
1688 the same time if IT->glyph_row is non-null. See the explanation of
1689 struct display_iterator in dispextern.h for an overview.
1691 produce_glyphs also stores the result of glyph width, ascent
1692 etc. computations in *IT.
1694 IT->glyph_row may be null, in which case produce_glyphs does not
1695 actually fill in the glyphs. This is used in the move_* functions
1696 in xdisp.c for text width and height computations.
1698 Callers usually don't call produce_glyphs directly;
1699 instead they use the macro PRODUCE_GLYPHS. */
1701 void
1702 produce_glyphs (it)
1703 struct it *it;
1705 /* If a hook is installed, let it do the work. */
1706 xassert (it->what == IT_CHARACTER
1707 || it->what == IT_COMPOSITION
1708 || it->what == IT_STRETCH);
1710 if (it->what == IT_STRETCH)
1712 produce_stretch_glyph (it);
1713 goto done;
1716 /* Nothing but characters are supported on terminal frames. For a
1717 composition sequence, it->c is the first character of the
1718 sequence. */
1719 xassert (it->what == IT_CHARACTER
1720 || it->what == IT_COMPOSITION);
1722 /* Maybe translate single-byte characters to multibyte. */
1723 it->char_to_display = it->c;
1725 if (it->c >= 040 && it->c < 0177)
1727 it->pixel_width = it->nglyphs = 1;
1728 if (it->glyph_row)
1729 append_glyph (it);
1731 else if (it->c == '\n')
1732 it->pixel_width = it->nglyphs = 0;
1733 else if (it->c == '\t')
1735 int absolute_x = (it->current_x
1736 + it->continuation_lines_width);
1737 int next_tab_x
1738 = (((1 + absolute_x + it->tab_width - 1)
1739 / it->tab_width)
1740 * it->tab_width);
1741 int nspaces;
1743 /* If part of the TAB has been displayed on the previous line
1744 which is continued now, continuation_lines_width will have
1745 been incremented already by the part that fitted on the
1746 continued line. So, we will get the right number of spaces
1747 here. */
1748 nspaces = next_tab_x - absolute_x;
1750 if (it->glyph_row)
1752 int n = nspaces;
1754 it->char_to_display = ' ';
1755 it->pixel_width = it->len = 1;
1757 while (n--)
1758 append_glyph (it);
1761 it->pixel_width = nspaces;
1762 it->nglyphs = nspaces;
1764 else if (SINGLE_BYTE_CHAR_P (it->c))
1766 if (unibyte_display_via_language_environment
1767 && (it->c >= 0240
1768 || !NILP (Vnonascii_translation_table)))
1770 int charset;
1772 it->char_to_display = unibyte_char_to_multibyte (it->c);
1773 charset = CHAR_CHARSET (it->char_to_display);
1774 it->pixel_width = CHARSET_WIDTH (charset);
1775 it->nglyphs = it->pixel_width;
1776 if (it->glyph_row)
1777 append_glyph (it);
1779 else
1781 /* Coming here means that it->c is from display table, thus we
1782 must send the code as is to the terminal. Although there's
1783 no way to know how many columns it occupies on a screen, it
1784 is a good assumption that a single byte code has 1-column
1785 width. */
1786 it->pixel_width = it->nglyphs = 1;
1787 if (it->glyph_row)
1788 append_glyph (it);
1791 else
1793 /* A multi-byte character. The display width is fixed for all
1794 characters of the set. Some of the glyphs may have to be
1795 ignored because they are already displayed in a continued
1796 line. */
1797 int charset = CHAR_CHARSET (it->c);
1799 it->pixel_width = CHARSET_WIDTH (charset);
1800 it->nglyphs = it->pixel_width;
1802 if (it->glyph_row)
1803 append_glyph (it);
1806 done:
1807 /* Advance current_x by the pixel width as a convenience for
1808 the caller. */
1809 if (it->area == TEXT_AREA)
1810 it->current_x += it->pixel_width;
1811 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1812 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1816 /* Produce a stretch glyph for iterator IT. IT->object is the value
1817 of the glyph property displayed. The value must be a list
1818 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1819 being recognized:
1821 1. `:width WIDTH' specifies that the space should be WIDTH *
1822 canonical char width wide. WIDTH may be an integer or floating
1823 point number.
1825 2. `:align-to HPOS' specifies that the space should be wide enough
1826 to reach HPOS, a value in canonical character units. */
1828 static void
1829 produce_stretch_glyph (it)
1830 struct it *it;
1832 /* (space :width WIDTH ...) */
1833 Lisp_Object prop, plist;
1834 int width = 0, align_to = -1;
1835 int zero_width_ok_p = 0;
1836 double tem;
1838 /* List should start with `space'. */
1839 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1840 plist = XCDR (it->object);
1842 /* Compute the width of the stretch. */
1843 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1844 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1846 /* Absolute width `:width WIDTH' specified and valid. */
1847 zero_width_ok_p = 1;
1848 width = (int)(tem + 0.5);
1850 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1851 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1853 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1854 align_to = (align_to < 0
1856 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1857 else if (align_to < 0)
1858 align_to = window_box_left_offset (it->w, TEXT_AREA);
1859 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1860 zero_width_ok_p = 1;
1862 else
1863 /* Nothing specified -> width defaults to canonical char width. */
1864 width = FRAME_COLUMN_WIDTH (it->f);
1866 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1867 width = 1;
1869 if (width > 0 && it->glyph_row)
1871 Lisp_Object o_object = it->object;
1872 Lisp_Object object = it->stack[it->sp - 1].string;
1873 int n = width;
1875 if (!STRINGP (object))
1876 object = it->w->buffer;
1877 it->object = object;
1878 it->char_to_display = ' ';
1879 it->pixel_width = it->len = 1;
1880 while (n--)
1881 append_glyph (it);
1882 it->object = o_object;
1884 it->pixel_width = width;
1885 it->nglyphs = width;
1889 /* Get information about special display element WHAT in an
1890 environment described by IT. WHAT is one of IT_TRUNCATION or
1891 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1892 non-null glyph_row member. This function ensures that fields like
1893 face_id, c, len of IT are left untouched. */
1895 void
1896 produce_special_glyphs (it, what)
1897 struct it *it;
1898 enum display_element_type what;
1900 struct it temp_it;
1901 GLYPH glyph;
1903 temp_it = *it;
1904 temp_it.dp = NULL;
1905 temp_it.what = IT_CHARACTER;
1906 temp_it.len = 1;
1907 temp_it.object = make_number (0);
1908 bzero (&temp_it.current, sizeof temp_it.current);
1910 if (what == IT_CONTINUATION)
1912 /* Continuation glyph. */
1913 if (it->dp
1914 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1915 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1917 glyph = XINT (DISP_CONTINUE_GLYPH (it->dp));
1918 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1920 else
1921 glyph = '\\';
1923 else if (what == IT_TRUNCATION)
1925 /* Truncation glyph. */
1926 if (it->dp
1927 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1928 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1930 glyph = XINT (DISP_TRUNC_GLYPH (it->dp));
1931 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1933 else
1934 glyph = '$';
1936 else
1937 abort ();
1939 temp_it.c = FAST_GLYPH_CHAR (glyph);
1940 temp_it.face_id = FAST_GLYPH_FACE (glyph);
1941 temp_it.len = CHAR_BYTES (temp_it.c);
1943 produce_glyphs (&temp_it);
1944 it->pixel_width = temp_it.pixel_width;
1945 it->nglyphs = temp_it.pixel_width;
1950 /***********************************************************************
1951 Faces
1952 ***********************************************************************/
1954 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1955 one of the enumerators from enum no_color_bit, or a bit set built
1956 from them. Some display attributes may not be used together with
1957 color; the termcap capability `NC' specifies which ones. */
1959 #define MAY_USE_WITH_COLORS_P(ATTR) \
1960 (TN_max_colors > 0 \
1961 ? (TN_no_color_video & (ATTR)) == 0 \
1962 : 1)
1964 /* Turn appearances of face FACE_ID on tty frame F on.
1965 FACE_ID is a realized face ID number, in the face cache. */
1967 static void
1968 turn_on_face (f, face_id)
1969 struct frame *f;
1970 int face_id;
1972 struct face *face = FACE_FROM_ID (f, face_id);
1973 long fg = face->foreground;
1974 long bg = face->background;
1976 /* Do this first because TS_end_standout_mode may be the same
1977 as TS_exit_attribute_mode, which turns all appearances off. */
1978 if (MAY_USE_WITH_COLORS_P (NC_REVERSE))
1980 if (TN_max_colors > 0)
1982 if (fg >= 0 && bg >= 0)
1984 /* If the terminal supports colors, we can set them
1985 below without using reverse video. The face's fg
1986 and bg colors are set as they should appear on
1987 the screen, i.e. they take the inverse-video'ness
1988 of the face already into account. */
1990 else if (inverse_video)
1992 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1993 || bg == FACE_TTY_DEFAULT_BG_COLOR)
1994 toggle_highlight ();
1996 else
1998 if (fg == FACE_TTY_DEFAULT_BG_COLOR
1999 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2000 toggle_highlight ();
2003 else
2005 /* If we can't display colors, use reverse video
2006 if the face specifies that. */
2007 if (inverse_video)
2009 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2010 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2011 toggle_highlight ();
2013 else
2015 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2016 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2017 toggle_highlight ();
2022 if (face->tty_bold_p)
2024 if (MAY_USE_WITH_COLORS_P (NC_BOLD))
2025 OUTPUT1_IF (TS_enter_bold_mode);
2027 else if (face->tty_dim_p)
2028 if (MAY_USE_WITH_COLORS_P (NC_DIM))
2029 OUTPUT1_IF (TS_enter_dim_mode);
2031 /* Alternate charset and blinking not yet used. */
2032 if (face->tty_alt_charset_p
2033 && MAY_USE_WITH_COLORS_P (NC_ALT_CHARSET))
2034 OUTPUT1_IF (TS_enter_alt_charset_mode);
2036 if (face->tty_blinking_p
2037 && MAY_USE_WITH_COLORS_P (NC_BLINK))
2038 OUTPUT1_IF (TS_enter_blink_mode);
2040 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (NC_UNDERLINE))
2041 OUTPUT1_IF (TS_enter_underline_mode);
2043 if (TN_max_colors > 0)
2045 char *ts, *p;
2047 ts = standout_mode ? TS_set_background : TS_set_foreground;
2048 if (fg >= 0 && ts)
2050 p = tparam (ts, NULL, 0, (int) fg);
2051 OUTPUT (p);
2052 xfree (p);
2055 ts = standout_mode ? TS_set_foreground : TS_set_background;
2056 if (bg >= 0 && ts)
2058 p = tparam (ts, NULL, 0, (int) bg);
2059 OUTPUT (p);
2060 xfree (p);
2066 /* Turn off appearances of face FACE_ID on tty frame F. */
2068 static void
2069 turn_off_face (f, face_id)
2070 struct frame *f;
2071 int face_id;
2073 struct face *face = FACE_FROM_ID (f, face_id);
2075 xassert (face != NULL);
2077 if (TS_exit_attribute_mode)
2079 /* Capability "me" will turn off appearance modes double-bright,
2080 half-bright, reverse-video, standout, underline. It may or
2081 may not turn off alt-char-mode. */
2082 if (face->tty_bold_p
2083 || face->tty_dim_p
2084 || face->tty_reverse_p
2085 || face->tty_alt_charset_p
2086 || face->tty_blinking_p
2087 || face->tty_underline_p)
2089 OUTPUT1_IF (TS_exit_attribute_mode);
2090 if (strcmp (TS_exit_attribute_mode, TS_end_standout_mode) == 0)
2091 standout_mode = 0;
2094 if (face->tty_alt_charset_p)
2095 OUTPUT_IF (TS_exit_alt_charset_mode);
2097 else
2099 /* If we don't have "me" we can only have those appearances
2100 that have exit sequences defined. */
2101 if (face->tty_alt_charset_p)
2102 OUTPUT_IF (TS_exit_alt_charset_mode);
2104 if (face->tty_underline_p)
2105 OUTPUT_IF (TS_exit_underline_mode);
2108 /* Switch back to default colors. */
2109 if (TN_max_colors > 0
2110 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2111 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2112 || (face->background != FACE_TTY_DEFAULT_COLOR
2113 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2114 OUTPUT1_IF (TS_orig_pair);
2118 /* Return non-zero if the terminal on frame F supports all of the
2119 capabilities in CAPS simultaneously, with foreground and background
2120 colors FG and BG. */
2123 tty_capable_p (f, caps, fg, bg)
2124 struct frame *f;
2125 unsigned caps;
2126 unsigned long fg, bg;
2128 #define TTY_CAPABLE_P_TRY(cap, TS, NC_bit) \
2129 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(NC_bit))) \
2130 return 0;
2132 TTY_CAPABLE_P_TRY (TTY_CAP_INVERSE, TS_standout_mode, NC_REVERSE);
2133 TTY_CAPABLE_P_TRY (TTY_CAP_UNDERLINE, TS_enter_underline_mode, NC_UNDERLINE);
2134 TTY_CAPABLE_P_TRY (TTY_CAP_BOLD, TS_enter_bold_mode, NC_BOLD);
2135 TTY_CAPABLE_P_TRY (TTY_CAP_DIM, TS_enter_dim_mode, NC_DIM);
2136 TTY_CAPABLE_P_TRY (TTY_CAP_BLINK, TS_enter_blink_mode, NC_BLINK);
2137 TTY_CAPABLE_P_TRY (TTY_CAP_ALT_CHARSET, TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2139 /* We can do it! */
2140 return 1;
2144 /* Return non-zero if the terminal is capable to display colors. */
2146 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2147 0, 1, 0,
2148 doc: /* Return non-nil if TTY can display colors on DISPLAY. */)
2149 (display)
2150 Lisp_Object display;
2152 return TN_max_colors > 0 ? Qt : Qnil;
2155 /* Return the number of supported colors. */
2156 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2157 Stty_display_color_cells, 0, 1, 0,
2158 doc: /* Return the number of colors supported by TTY on DISPLAY. */)
2159 (display)
2160 Lisp_Object display;
2162 return make_number (TN_max_colors);
2165 #ifndef WINDOWSNT
2167 /* Save or restore the default color-related capabilities of this
2168 terminal. */
2169 static void
2170 tty_default_color_capabilities (save)
2171 int save;
2173 static char
2174 *default_orig_pair, *default_set_foreground, *default_set_background;
2175 static int default_max_colors, default_max_pairs, default_no_color_video;
2177 if (save)
2179 if (default_orig_pair)
2180 xfree (default_orig_pair);
2181 default_orig_pair = TS_orig_pair ? xstrdup (TS_orig_pair) : NULL;
2183 if (default_set_foreground)
2184 xfree (default_set_foreground);
2185 default_set_foreground = TS_set_foreground ? xstrdup (TS_set_foreground)
2186 : NULL;
2188 if (default_set_background)
2189 xfree (default_set_background);
2190 default_set_background = TS_set_background ? xstrdup (TS_set_background)
2191 : NULL;
2193 default_max_colors = TN_max_colors;
2194 default_max_pairs = TN_max_pairs;
2195 default_no_color_video = TN_no_color_video;
2197 else
2199 TS_orig_pair = default_orig_pair;
2200 TS_set_foreground = default_set_foreground;
2201 TS_set_background = default_set_background;
2202 TN_max_colors = default_max_colors;
2203 TN_max_pairs = default_max_pairs;
2204 TN_no_color_video = default_no_color_video;
2208 /* Setup one of the standard tty color schemes according to MODE.
2209 MODE's value is generally the number of colors which we want to
2210 support; zero means set up for the default capabilities, the ones
2211 we saw at term_init time; -1 means turn off color support. */
2212 void
2213 tty_setup_colors (mode)
2214 int mode;
2216 /* Canonicalize all negative values of MODE. */
2217 if (mode < -1)
2218 mode = -1;
2220 switch (mode)
2222 case -1: /* no colors at all */
2223 TN_max_colors = 0;
2224 TN_max_pairs = 0;
2225 TN_no_color_video = 0;
2226 TS_set_foreground = TS_set_background = TS_orig_pair = NULL;
2227 break;
2228 case 0: /* default colors, if any */
2229 default:
2230 tty_default_color_capabilities (0);
2231 break;
2232 case 8: /* 8 standard ANSI colors */
2233 TS_orig_pair = "\033[0m";
2234 #ifdef TERMINFO
2235 TS_set_foreground = "\033[3%p1%dm";
2236 TS_set_background = "\033[4%p1%dm";
2237 #else
2238 TS_set_foreground = "\033[3%dm";
2239 TS_set_background = "\033[4%dm";
2240 #endif
2241 TN_max_colors = 8;
2242 TN_max_pairs = 64;
2243 TN_no_color_video = 0;
2244 break;
2248 void
2249 set_tty_color_mode (f, val)
2250 struct frame *f;
2251 Lisp_Object val;
2253 Lisp_Object color_mode_spec, current_mode_spec;
2254 Lisp_Object color_mode, current_mode;
2255 int mode, old_mode;
2256 extern Lisp_Object Qtty_color_mode;
2257 Lisp_Object tty_color_mode_alist;
2259 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
2260 Qnil);
2262 if (INTEGERP (val))
2263 color_mode = val;
2264 else
2266 if (NILP (tty_color_mode_alist))
2267 color_mode_spec = Qnil;
2268 else
2269 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
2271 if (CONSP (color_mode_spec))
2272 color_mode = XCDR (color_mode_spec);
2273 else
2274 color_mode = Qnil;
2277 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
2279 if (CONSP (current_mode_spec))
2280 current_mode = XCDR (current_mode_spec);
2281 else
2282 current_mode = Qnil;
2283 if (INTEGERP (color_mode))
2284 mode = XINT (color_mode);
2285 else
2286 mode = 0; /* meaning default */
2287 if (INTEGERP (current_mode))
2288 old_mode = XINT (current_mode);
2289 else
2290 old_mode = 0;
2292 if (mode != old_mode)
2294 tty_setup_colors (mode);
2295 /* This recomputes all the faces given the new color
2296 definitions. */
2297 call0 (intern ("tty-set-up-initial-frame-faces"));
2298 redraw_frame (f);
2302 #endif /* !WINDOWSNT */
2305 /***********************************************************************
2306 Initialization
2307 ***********************************************************************/
2309 void
2310 term_init (terminal_type)
2311 char *terminal_type;
2313 char *area;
2314 char **address = &area;
2315 char *buffer = NULL;
2316 int buffer_size = 4096;
2317 register char *p;
2318 int status;
2319 struct frame *sf = XFRAME (selected_frame);
2321 encode_terminal_bufsize = 0;
2323 #ifdef WINDOWSNT
2324 initialize_w32_display ();
2326 Wcm_clear ();
2328 area = (char *) xmalloc (2044);
2330 FrameRows = FRAME_LINES (sf);
2331 FrameCols = FRAME_COLS (sf);
2332 specified_window = FRAME_LINES (sf);
2334 delete_in_insert_mode = 1;
2336 UseTabs = 0;
2337 scroll_region_ok = 0;
2339 /* Seems to insert lines when it's not supposed to, messing
2340 up the display. In doing a trace, it didn't seem to be
2341 called much, so I don't think we're losing anything by
2342 turning it off. */
2344 line_ins_del_ok = 0;
2345 char_ins_del_ok = 1;
2347 baud_rate = 19200;
2349 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2350 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
2351 TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
2353 return;
2354 #else /* not WINDOWSNT */
2356 Wcm_clear ();
2358 buffer = (char *) xmalloc (buffer_size);
2359 status = tgetent (buffer, terminal_type);
2360 if (status < 0)
2362 #ifdef TERMINFO
2363 fatal ("Cannot open terminfo database file");
2364 #else
2365 fatal ("Cannot open termcap database file");
2366 #endif
2368 if (status == 0)
2370 #ifdef TERMINFO
2371 fatal ("Terminal type %s is not defined.\n\
2372 If that is not the actual type of terminal you have,\n\
2373 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2374 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2375 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2376 terminal_type);
2377 #else
2378 fatal ("Terminal type %s is not defined.\n\
2379 If that is not the actual type of terminal you have,\n\
2380 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2381 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2382 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2383 terminal_type);
2384 #endif
2387 #ifndef TERMINFO
2388 if (strlen (buffer) >= buffer_size)
2389 abort ();
2390 buffer_size = strlen (buffer);
2391 #endif
2392 area = (char *) xmalloc (buffer_size);
2394 TS_ins_line = tgetstr ("al", address);
2395 TS_ins_multi_lines = tgetstr ("AL", address);
2396 TS_bell = tgetstr ("bl", address);
2397 BackTab = tgetstr ("bt", address);
2398 TS_clr_to_bottom = tgetstr ("cd", address);
2399 TS_clr_line = tgetstr ("ce", address);
2400 TS_clr_frame = tgetstr ("cl", address);
2401 ColPosition = NULL; /* tgetstr ("ch", address); */
2402 AbsPosition = tgetstr ("cm", address);
2403 CR = tgetstr ("cr", address);
2404 TS_set_scroll_region = tgetstr ("cs", address);
2405 TS_set_scroll_region_1 = tgetstr ("cS", address);
2406 RowPosition = tgetstr ("cv", address);
2407 TS_del_char = tgetstr ("dc", address);
2408 TS_del_multi_chars = tgetstr ("DC", address);
2409 TS_del_line = tgetstr ("dl", address);
2410 TS_del_multi_lines = tgetstr ("DL", address);
2411 TS_delete_mode = tgetstr ("dm", address);
2412 TS_end_delete_mode = tgetstr ("ed", address);
2413 TS_end_insert_mode = tgetstr ("ei", address);
2414 Home = tgetstr ("ho", address);
2415 TS_ins_char = tgetstr ("ic", address);
2416 TS_ins_multi_chars = tgetstr ("IC", address);
2417 TS_insert_mode = tgetstr ("im", address);
2418 TS_pad_inserted_char = tgetstr ("ip", address);
2419 TS_end_keypad_mode = tgetstr ("ke", address);
2420 TS_keypad_mode = tgetstr ("ks", address);
2421 LastLine = tgetstr ("ll", address);
2422 Right = tgetstr ("nd", address);
2423 Down = tgetstr ("do", address);
2424 if (!Down)
2425 Down = tgetstr ("nl", address); /* Obsolete name for "do" */
2426 #ifdef VMS
2427 /* VMS puts a carriage return before each linefeed,
2428 so it is not safe to use linefeeds. */
2429 if (Down && Down[0] == '\n' && Down[1] == '\0')
2430 Down = 0;
2431 #endif /* VMS */
2432 if (tgetflag ("bs"))
2433 Left = "\b"; /* can't possibly be longer! */
2434 else /* (Actually, "bs" is obsolete...) */
2435 Left = tgetstr ("le", address);
2436 if (!Left)
2437 Left = tgetstr ("bc", address); /* Obsolete name for "le" */
2438 TS_pad_char = tgetstr ("pc", address);
2439 TS_repeat = tgetstr ("rp", address);
2440 TS_end_standout_mode = tgetstr ("se", address);
2441 TS_fwd_scroll = tgetstr ("sf", address);
2442 TS_standout_mode = tgetstr ("so", address);
2443 TS_rev_scroll = tgetstr ("sr", address);
2444 Wcm.cm_tab = tgetstr ("ta", address);
2445 TS_end_termcap_modes = tgetstr ("te", address);
2446 TS_termcap_modes = tgetstr ("ti", address);
2447 Up = tgetstr ("up", address);
2448 TS_visible_bell = tgetstr ("vb", address);
2449 TS_cursor_normal = tgetstr ("ve", address);
2450 TS_cursor_visible = tgetstr ("vs", address);
2451 TS_cursor_invisible = tgetstr ("vi", address);
2452 TS_set_window = tgetstr ("wi", address);
2454 TS_enter_underline_mode = tgetstr ("us", address);
2455 TS_exit_underline_mode = tgetstr ("ue", address);
2456 TS_enter_bold_mode = tgetstr ("md", address);
2457 TS_enter_dim_mode = tgetstr ("mh", address);
2458 TS_enter_blink_mode = tgetstr ("mb", address);
2459 TS_enter_reverse_mode = tgetstr ("mr", address);
2460 TS_enter_alt_charset_mode = tgetstr ("as", address);
2461 TS_exit_alt_charset_mode = tgetstr ("ae", address);
2462 TS_exit_attribute_mode = tgetstr ("me", address);
2464 MultiUp = tgetstr ("UP", address);
2465 MultiDown = tgetstr ("DO", address);
2466 MultiLeft = tgetstr ("LE", address);
2467 MultiRight = tgetstr ("RI", address);
2469 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
2470 color because we can't switch back to the default foreground and
2471 background. */
2472 TS_orig_pair = tgetstr ("op", address);
2473 if (TS_orig_pair)
2475 TS_set_foreground = tgetstr ("AF", address);
2476 TS_set_background = tgetstr ("AB", address);
2477 if (!TS_set_foreground)
2479 /* SVr4. */
2480 TS_set_foreground = tgetstr ("Sf", address);
2481 TS_set_background = tgetstr ("Sb", address);
2484 TN_max_colors = tgetnum ("Co");
2485 TN_max_pairs = tgetnum ("pa");
2487 TN_no_color_video = tgetnum ("NC");
2488 if (TN_no_color_video == -1)
2489 TN_no_color_video = 0;
2492 tty_default_color_capabilities (1);
2494 MagicWrap = tgetflag ("xn");
2495 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
2496 the former flag imply the latter. */
2497 AutoWrap = MagicWrap || tgetflag ("am");
2498 memory_below_frame = tgetflag ("db");
2499 TF_hazeltine = tgetflag ("hz");
2500 must_write_spaces = tgetflag ("in");
2501 meta_key = tgetflag ("km") || tgetflag ("MT");
2502 TF_insmode_motion = tgetflag ("mi");
2503 TF_standout_motion = tgetflag ("ms");
2504 TF_underscore = tgetflag ("ul");
2505 TF_teleray = tgetflag ("xt");
2507 term_get_fkeys (address);
2509 /* Get frame size from system, or else from termcap. */
2511 int height, width;
2512 get_frame_size (&width, &height);
2513 FRAME_COLS (sf) = width;
2514 FRAME_LINES (sf) = height;
2517 if (FRAME_COLS (sf) <= 0)
2518 SET_FRAME_COLS (sf, tgetnum ("co"));
2519 else
2520 /* Keep width and external_width consistent */
2521 SET_FRAME_COLS (sf, FRAME_COLS (sf));
2522 if (FRAME_LINES (sf) <= 0)
2523 FRAME_LINES (sf) = tgetnum ("li");
2525 if (FRAME_LINES (sf) < 3 || FRAME_COLS (sf) < 3)
2526 fatal ("Screen size %dx%d is too small",
2527 FRAME_LINES (sf), FRAME_COLS (sf));
2529 min_padding_speed = tgetnum ("pb");
2530 TabWidth = tgetnum ("tw");
2532 #ifdef VMS
2533 /* These capabilities commonly use ^J.
2534 I don't know why, but sending them on VMS does not work;
2535 it causes following spaces to be lost, sometimes.
2536 For now, the simplest fix is to avoid using these capabilities ever. */
2537 if (Down && Down[0] == '\n')
2538 Down = 0;
2539 #endif /* VMS */
2541 if (!TS_bell)
2542 TS_bell = "\07";
2544 if (!TS_fwd_scroll)
2545 TS_fwd_scroll = Down;
2547 PC = TS_pad_char ? *TS_pad_char : 0;
2549 if (TabWidth < 0)
2550 TabWidth = 8;
2552 /* Turned off since /etc/termcap seems to have :ta= for most terminals
2553 and newer termcap doc does not seem to say there is a default.
2554 if (!Wcm.cm_tab)
2555 Wcm.cm_tab = "\t";
2558 /* We don't support standout modes that use `magic cookies', so
2559 turn off any that do. */
2560 if (TS_standout_mode && tgetnum ("sg") >= 0)
2562 TS_standout_mode = 0;
2563 TS_end_standout_mode = 0;
2565 if (TS_enter_underline_mode && tgetnum ("ug") >= 0)
2567 TS_enter_underline_mode = 0;
2568 TS_exit_underline_mode = 0;
2571 /* If there's no standout mode, try to use underlining instead. */
2572 if (TS_standout_mode == 0)
2574 TS_standout_mode = TS_enter_underline_mode;
2575 TS_end_standout_mode = TS_exit_underline_mode;
2578 /* If no `se' string, try using a `me' string instead.
2579 If that fails, we can't use standout mode at all. */
2580 if (TS_end_standout_mode == 0)
2582 char *s = tgetstr ("me", address);
2583 if (s != 0)
2584 TS_end_standout_mode = s;
2585 else
2586 TS_standout_mode = 0;
2589 if (TF_teleray)
2591 Wcm.cm_tab = 0;
2592 /* We can't support standout mode, because it uses magic cookies. */
2593 TS_standout_mode = 0;
2594 /* But that means we cannot rely on ^M to go to column zero! */
2595 CR = 0;
2596 /* LF can't be trusted either -- can alter hpos */
2597 /* if move at column 0 thru a line with TS_standout_mode */
2598 Down = 0;
2601 /* Special handling for certain terminal types known to need it */
2603 if (!strcmp (terminal_type, "supdup"))
2605 memory_below_frame = 1;
2606 Wcm.cm_losewrap = 1;
2608 if (!strncmp (terminal_type, "c10", 3)
2609 || !strcmp (terminal_type, "perq"))
2611 /* Supply a makeshift :wi string.
2612 This string is not valid in general since it works only
2613 for windows starting at the upper left corner;
2614 but that is all Emacs uses.
2616 This string works only if the frame is using
2617 the top of the video memory, because addressing is memory-relative.
2618 So first check the :ti string to see if that is true.
2620 It would be simpler if the :wi string could go in the termcap
2621 entry, but it can't because it is not fully valid.
2622 If it were in the termcap entry, it would confuse other programs. */
2623 if (!TS_set_window)
2625 p = TS_termcap_modes;
2626 while (*p && strcmp (p, "\033v "))
2627 p++;
2628 if (*p)
2629 TS_set_window = "\033v%C %C %C %C ";
2631 /* Termcap entry often fails to have :in: flag */
2632 must_write_spaces = 1;
2633 /* :ti string typically fails to have \E^G! in it */
2634 /* This limits scope of insert-char to one line. */
2635 strcpy (area, TS_termcap_modes);
2636 strcat (area, "\033\007!");
2637 TS_termcap_modes = area;
2638 area += strlen (area) + 1;
2639 p = AbsPosition;
2640 /* Change all %+ parameters to %C, to handle
2641 values above 96 correctly for the C100. */
2642 while (*p)
2644 if (p[0] == '%' && p[1] == '+')
2645 p[1] = 'C';
2646 p++;
2650 FrameRows = FRAME_LINES (sf);
2651 FrameCols = FRAME_COLS (sf);
2652 specified_window = FRAME_LINES (sf);
2654 if (Wcm_init () == -1) /* can't do cursor motion */
2655 #ifdef VMS
2656 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2657 It lacks the ability to position the cursor.\n\
2658 If that is not the actual type of terminal you have, use either the\n\
2659 DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
2660 or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
2661 terminal_type);
2662 #else /* not VMS */
2663 # ifdef TERMINFO
2664 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2665 It lacks the ability to position the cursor.\n\
2666 If that is not the actual type of terminal you have,\n\
2667 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2668 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2669 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2670 terminal_type);
2671 # else /* TERMCAP */
2672 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2673 It lacks the ability to position the cursor.\n\
2674 If that is not the actual type of terminal you have,\n\
2675 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2676 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2677 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2678 terminal_type);
2679 # endif /* TERMINFO */
2680 #endif /*VMS */
2681 if (FRAME_LINES (sf) <= 0
2682 || FRAME_COLS (sf) <= 0)
2683 fatal ("The frame size has not been specified");
2685 delete_in_insert_mode
2686 = TS_delete_mode && TS_insert_mode
2687 && !strcmp (TS_delete_mode, TS_insert_mode);
2689 se_is_so = (TS_standout_mode
2690 && TS_end_standout_mode
2691 && !strcmp (TS_standout_mode, TS_end_standout_mode));
2693 UseTabs = tabs_safe_p () && TabWidth == 8;
2695 scroll_region_ok
2696 = (Wcm.cm_abs
2697 && (TS_set_window || TS_set_scroll_region || TS_set_scroll_region_1));
2699 line_ins_del_ok = (((TS_ins_line || TS_ins_multi_lines)
2700 && (TS_del_line || TS_del_multi_lines))
2701 || (scroll_region_ok && TS_fwd_scroll && TS_rev_scroll));
2703 char_ins_del_ok = ((TS_ins_char || TS_insert_mode
2704 || TS_pad_inserted_char || TS_ins_multi_chars)
2705 && (TS_del_char || TS_del_multi_chars));
2707 fast_clear_end_of_line = TS_clr_line != 0;
2709 init_baud_rate ();
2710 if (read_socket_hook) /* Baudrate is somewhat */
2711 /* meaningless in this case */
2712 baud_rate = 9600;
2714 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2715 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
2716 #endif /* WINDOWSNT */
2718 xfree (buffer);
2721 /* VARARGS 1 */
2722 void
2723 fatal (str, arg1, arg2)
2724 char *str, *arg1, *arg2;
2726 fprintf (stderr, "emacs: ");
2727 fprintf (stderr, str, arg1, arg2);
2728 fprintf (stderr, "\n");
2729 fflush (stderr);
2730 exit (1);
2733 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 0, 0,
2734 doc: /* Declare that this terminal does not handle underlining.
2735 This is used to override the terminfo data, for certain terminals that
2736 do not really do underlining, but say that they do. */)
2739 TS_enter_underline_mode = 0;
2740 return Qnil;
2743 void
2744 syms_of_term ()
2746 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
2747 doc: /* Non-nil means the system uses terminfo rather than termcap.
2748 This variable can be used by terminal emulator packages. */);
2749 #ifdef TERMINFO
2750 system_uses_terminfo = 1;
2751 #else
2752 system_uses_terminfo = 0;
2753 #endif
2755 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
2756 doc: /* Non-nil means call this function to ring the bell.
2757 The function should accept no arguments. */);
2758 Vring_bell_function = Qnil;
2760 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
2761 doc: /* Non-nil means to make the cursor very visible.
2762 This only has an effect when running in a text terminal.
2763 What means \"very visible\" is up to your terminal. It may make the cursor
2764 bigger, or it may make it blink, or it may do nothing at all. */);
2765 visible_cursor = 1;
2767 defsubr (&Stty_display_color_p);
2768 defsubr (&Stty_display_color_cells);
2769 defsubr (&Stty_no_underline);
2771 fullscreen_hook = NULL;
2774 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
2775 (do not change this comment) */