(frame.o, keyboard.o, xdisp.o, xfaces.o): Depend on macgui.h.
[emacs.git] / src / term.c
blob8f7724efb602b83b370c631269b8c1ae6d374923
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, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
25 #include <config.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <string.h>
30 #include "termchar.h"
31 #include "termopts.h"
32 #include "lisp.h"
33 #include "charset.h"
34 #include "coding.h"
35 #include "keyboard.h"
36 #include "frame.h"
37 #include "disptab.h"
38 #include "termhooks.h"
39 #include "dispextern.h"
40 #include "window.h"
41 #include "keymap.h"
42 #include "blockinput.h"
44 /* For now, don't try to include termcap.h. On some systems,
45 configure finds a non-standard termcap.h that the main build
46 won't find. */
48 #if defined HAVE_TERMCAP_H && 0
49 #include <termcap.h>
50 #else
51 extern void tputs P_ ((const char *, int, int (*)(int)));
52 extern int tgetent P_ ((char *, const char *));
53 extern int tgetflag P_ ((char *id));
54 extern int tgetnum P_ ((char *id));
55 #endif
57 #include "cm.h"
58 #ifdef HAVE_X_WINDOWS
59 #include "xterm.h"
60 #endif
61 #ifdef MAC_OS
62 #include "macterm.h"
63 #endif
65 static void turn_on_face P_ ((struct frame *, int face_id));
66 static void turn_off_face P_ ((struct frame *, int face_id));
67 static void tty_show_cursor P_ ((void));
68 static void tty_hide_cursor P_ ((void));
70 #define OUTPUT(a) \
71 tputs (a, (int) (FRAME_LINES (XFRAME (selected_frame)) - curY), cmputc)
72 #define OUTPUT1(a) tputs (a, 1, cmputc)
73 #define OUTPUTL(a, lines) tputs (a, lines, cmputc)
75 #define OUTPUT_IF(a) \
76 do { \
77 if (a) \
78 tputs (a, (int) (FRAME_LINES (XFRAME (selected_frame)) \
79 - curY), cmputc); \
80 } while (0)
82 #define OUTPUT1_IF(a) do { if (a) tputs (a, 1, cmputc); } while (0)
84 /* Display space properties */
86 extern Lisp_Object Qspace, QCalign_to, QCwidth;
88 /* Function to use to ring the bell. */
90 Lisp_Object Vring_bell_function;
92 /* If true, use "vs", otherwise use "ve" to make the cursor visible. */
94 static int visible_cursor;
96 /* Terminal characteristics that higher levels want to look at.
97 These are all extern'd in termchar.h */
99 int must_write_spaces; /* Nonzero means spaces in the text
100 must actually be output; can't just skip
101 over some columns to leave them blank. */
102 int min_padding_speed; /* Speed below which no padding necessary */
104 int line_ins_del_ok; /* Terminal can insert and delete lines */
105 int char_ins_del_ok; /* Terminal can insert and delete chars */
106 int scroll_region_ok; /* Terminal supports setting the
107 scroll window */
108 int scroll_region_cost; /* Cost of setting a scroll window,
109 measured in characters */
110 int memory_below_frame; /* Terminal remembers lines
111 scrolled off bottom */
112 int fast_clear_end_of_line; /* Terminal has a `ce' string */
114 /* Nonzero means no need to redraw the entire frame on resuming
115 a suspended Emacs. This is useful on terminals with multiple pages,
116 where one page is used for Emacs and another for all else. */
118 int no_redraw_on_reenter;
120 /* Hook functions that you can set to snap out the functions in this file.
121 These are all extern'd in termhooks.h */
123 void (*cursor_to_hook) P_ ((int, int));
124 void (*raw_cursor_to_hook) P_ ((int, int));
125 void (*clear_to_end_hook) P_ ((void));
126 void (*clear_frame_hook) P_ ((void));
127 void (*clear_end_of_line_hook) P_ ((int));
129 void (*ins_del_lines_hook) P_ ((int, int));
131 void (*delete_glyphs_hook) P_ ((int));
133 void (*ring_bell_hook) P_ ((void));
135 void (*reset_terminal_modes_hook) P_ ((void));
136 void (*set_terminal_modes_hook) P_ ((void));
137 void (*update_begin_hook) P_ ((struct frame *));
138 void (*update_end_hook) P_ ((struct frame *));
139 void (*set_terminal_window_hook) P_ ((int));
140 void (*insert_glyphs_hook) P_ ((struct glyph *, int));
141 void (*write_glyphs_hook) P_ ((struct glyph *, int));
142 void (*delete_glyphs_hook) P_ ((int));
144 int (*read_socket_hook) P_ ((int, int, struct input_event *));
146 void (*frame_up_to_date_hook) P_ ((struct frame *));
148 /* Return the current position of the mouse.
150 Set *f to the frame the mouse is in, or zero if the mouse is in no
151 Emacs frame. If it is set to zero, all the other arguments are
152 garbage.
154 If the motion started in a scroll bar, set *bar_window to the
155 scroll bar's window, *part to the part the mouse is currently over,
156 *x to the position of the mouse along the scroll bar, and *y to the
157 overall length of the scroll bar.
159 Otherwise, set *bar_window to Qnil, and *x and *y to the column and
160 row of the character cell the mouse is over.
162 Set *time to the time the mouse was at the returned position.
164 This should clear mouse_moved until the next motion
165 event arrives. */
167 void (*mouse_position_hook) P_ ((FRAME_PTR *f, int insist,
168 Lisp_Object *bar_window,
169 enum scroll_bar_part *part,
170 Lisp_Object *x,
171 Lisp_Object *y,
172 unsigned long *time));
174 /* When reading from a minibuffer in a different frame, Emacs wants
175 to shift the highlight from the selected frame to the mini-buffer's
176 frame; under X, this means it lies about where the focus is.
177 This hook tells the window system code to re-decide where to put
178 the highlight. */
180 void (*frame_rehighlight_hook) P_ ((FRAME_PTR f));
182 /* If we're displaying frames using a window system that can stack
183 frames on top of each other, this hook allows you to bring a frame
184 to the front, or bury it behind all the other windows. If this
185 hook is zero, that means the device we're displaying on doesn't
186 support overlapping frames, so there's no need to raise or lower
187 anything.
189 If RAISE is non-zero, F is brought to the front, before all other
190 windows. If RAISE is zero, F is sent to the back, behind all other
191 windows. */
193 void (*frame_raise_lower_hook) P_ ((FRAME_PTR f, int raise));
195 /* If the value of the frame parameter changed, whis hook is called.
196 For example, if going from fullscreen to not fullscreen this hook
197 may do something OS dependent, like extended window manager hints on X11. */
198 void (*fullscreen_hook) P_ ((struct frame *f));
200 /* Set the vertical scroll bar for WINDOW to have its upper left corner
201 at (TOP, LEFT), and be LENGTH rows high. Set its handle to
202 indicate that we are displaying PORTION characters out of a total
203 of WHOLE characters, starting at POSITION. If WINDOW doesn't yet
204 have a scroll bar, create one for it. */
206 void (*set_vertical_scroll_bar_hook)
207 P_ ((struct window *window,
208 int portion, int whole, int position));
211 /* The following three hooks are used when we're doing a thorough
212 redisplay of the frame. We don't explicitly know which scroll bars
213 are going to be deleted, because keeping track of when windows go
214 away is a real pain - can you say set-window-configuration?
215 Instead, we just assert at the beginning of redisplay that *all*
216 scroll bars are to be removed, and then save scroll bars from the
217 fiery pit when we actually redisplay their window. */
219 /* Arrange for all scroll bars on FRAME to be removed at the next call
220 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
221 `*redeem_scroll_bar_hook' is applied to its window before the judgment.
223 This should be applied to each frame each time its window tree is
224 redisplayed, even if it is not displaying scroll bars at the moment;
225 if the HAS_SCROLL_BARS flag has just been turned off, only calling
226 this and the judge_scroll_bars_hook will get rid of them.
228 If non-zero, this hook should be safe to apply to any frame,
229 whether or not it can support scroll bars, and whether or not it is
230 currently displaying them. */
232 void (*condemn_scroll_bars_hook) P_ ((FRAME_PTR frame));
234 /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
235 Note that it's okay to redeem a scroll bar that is not condemned. */
237 void (*redeem_scroll_bar_hook) P_ ((struct window *window));
239 /* Remove all scroll bars on FRAME that haven't been saved since the
240 last call to `*condemn_scroll_bars_hook'.
242 This should be applied to each frame after each time its window
243 tree is redisplayed, even if it is not displaying scroll bars at the
244 moment; if the HAS_SCROLL_BARS flag has just been turned off, only
245 calling this and condemn_scroll_bars_hook will get rid of them.
247 If non-zero, this hook should be safe to apply to any frame,
248 whether or not it can support scroll bars, and whether or not it is
249 currently displaying them. */
251 void (*judge_scroll_bars_hook) P_ ((FRAME_PTR FRAME));
253 /* Strings, numbers and flags taken from the termcap entry. */
255 char *TS_ins_line; /* "al" */
256 char *TS_ins_multi_lines; /* "AL" (one parameter, # lines to insert) */
257 char *TS_bell; /* "bl" */
258 char *TS_clr_to_bottom; /* "cd" */
259 char *TS_clr_line; /* "ce", clear to end of line */
260 char *TS_clr_frame; /* "cl" */
261 char *TS_set_scroll_region; /* "cs" (2 params, first line and last line) */
262 char *TS_set_scroll_region_1; /* "cS" (4 params: total lines,
263 lines above scroll region, lines below it,
264 total lines again) */
265 char *TS_del_char; /* "dc" */
266 char *TS_del_multi_chars; /* "DC" (one parameter, # chars to delete) */
267 char *TS_del_line; /* "dl" */
268 char *TS_del_multi_lines; /* "DL" (one parameter, # lines to delete) */
269 char *TS_delete_mode; /* "dm", enter character-delete mode */
270 char *TS_end_delete_mode; /* "ed", leave character-delete mode */
271 char *TS_end_insert_mode; /* "ei", leave character-insert mode */
272 char *TS_ins_char; /* "ic" */
273 char *TS_ins_multi_chars; /* "IC" (one parameter, # chars to insert) */
274 char *TS_insert_mode; /* "im", enter character-insert mode */
275 char *TS_pad_inserted_char; /* "ip". Just padding, no commands. */
276 char *TS_end_keypad_mode; /* "ke" */
277 char *TS_keypad_mode; /* "ks" */
278 char *TS_pad_char; /* "pc", char to use as padding */
279 char *TS_repeat; /* "rp" (2 params, # times to repeat
280 and character to be repeated) */
281 char *TS_end_standout_mode; /* "se" */
282 char *TS_fwd_scroll; /* "sf" */
283 char *TS_standout_mode; /* "so" */
284 char *TS_rev_scroll; /* "sr" */
285 char *TS_end_termcap_modes; /* "te" */
286 char *TS_termcap_modes; /* "ti" */
287 char *TS_visible_bell; /* "vb" */
288 char *TS_cursor_normal; /* "ve" */
289 char *TS_cursor_visible; /* "vs" */
290 char *TS_cursor_invisible; /* "vi" */
291 char *TS_set_window; /* "wi" (4 params, start and end of window,
292 each as vpos and hpos) */
294 /* Value of the "NC" (no_color_video) capability, or 0 if not
295 present. */
297 static int TN_no_color_video;
299 /* Meaning of bits in no_color_video. Each bit set means that the
300 corresponding attribute cannot be combined with colors. */
302 enum no_color_bit
304 NC_STANDOUT = 1 << 0,
305 NC_UNDERLINE = 1 << 1,
306 NC_REVERSE = 1 << 2,
307 NC_BLINK = 1 << 3,
308 NC_DIM = 1 << 4,
309 NC_BOLD = 1 << 5,
310 NC_INVIS = 1 << 6,
311 NC_PROTECT = 1 << 7,
312 NC_ALT_CHARSET = 1 << 8
315 /* "md" -- turn on bold (extra bright mode). */
317 char *TS_enter_bold_mode;
319 /* "mh" -- turn on half-bright mode. */
321 char *TS_enter_dim_mode;
323 /* "mb" -- enter blinking mode. */
325 char *TS_enter_blink_mode;
327 /* "mr" -- enter reverse video mode. */
329 char *TS_enter_reverse_mode;
331 /* "us"/"ue" -- start/end underlining. */
333 char *TS_exit_underline_mode, *TS_enter_underline_mode;
335 /* "as"/"ae" -- start/end alternate character set. Not really
336 supported, yet. */
338 char *TS_enter_alt_charset_mode, *TS_exit_alt_charset_mode;
340 /* "me" -- switch appearances off. */
342 char *TS_exit_attribute_mode;
344 /* "Co" -- number of colors. */
346 int TN_max_colors;
348 /* "pa" -- max. number of color pairs on screen. Not handled yet.
349 Could be a problem if not equal to TN_max_colors * TN_max_colors. */
351 int TN_max_pairs;
353 /* "op" -- SVr4 set default pair to its original value. */
355 char *TS_orig_pair;
357 /* "AF"/"AB" or "Sf"/"Sb"-- set ANSI or SVr4 foreground/background color.
358 1 param, the color index. */
360 char *TS_set_foreground, *TS_set_background;
362 int TF_hazeltine; /* termcap hz flag. */
363 int TF_insmode_motion; /* termcap mi flag: can move while in insert mode. */
364 int TF_standout_motion; /* termcap mi flag: can move while in standout mode. */
365 int TF_underscore; /* termcap ul flag: _ underlines if over-struck on
366 non-blank position. Must clear before writing _. */
367 int TF_teleray; /* termcap xt flag: many weird consequences.
368 For t1061. */
370 static int RPov; /* # chars to start a TS_repeat */
372 static int delete_in_insert_mode; /* delete mode == insert mode */
374 static int se_is_so; /* 1 if same string both enters and leaves
375 standout mode */
377 /* internal state */
379 /* The largest frame width in any call to calculate_costs. */
381 int max_frame_cols;
383 /* The largest frame height in any call to calculate_costs. */
385 int max_frame_lines;
387 static int costs_set; /* Nonzero if costs have been calculated. */
389 int insert_mode; /* Nonzero when in insert mode. */
390 int standout_mode; /* Nonzero when in standout mode. */
392 /* Size of window specified by higher levels.
393 This is the number of lines, from the top of frame downwards,
394 which can participate in insert-line/delete-line operations.
396 Effectively it excludes the bottom frame_lines - specified_window_size
397 lines from those operations. */
399 int specified_window;
401 /* Frame currently being redisplayed; 0 if not currently redisplaying.
402 (Direct output does not count). */
404 FRAME_PTR updating_frame;
406 /* Provided for lisp packages. */
408 static int system_uses_terminfo;
410 /* Flag used in tty_show/hide_cursor. */
412 static int tty_cursor_hidden;
414 char *tparam ();
416 extern char *tgetstr ();
419 #ifdef WINDOWSNT
420 /* We aren't X windows, but we aren't termcap either. This makes me
421 uncertain as to what value to use for frame.output_method. For
422 this file, we'll define FRAME_TERMCAP_P to be zero so that our
423 output hooks get called instead of the termcap functions. Probably
424 the best long-term solution is to define an output_windows_nt... */
426 #undef FRAME_TERMCAP_P
427 #define FRAME_TERMCAP_P(_f_) 0
428 #endif /* WINDOWSNT */
430 void
431 ring_bell ()
433 if (!NILP (Vring_bell_function))
435 Lisp_Object function;
437 /* Temporarily set the global variable to nil
438 so that if we get an error, it stays nil
439 and we don't call it over and over.
441 We don't specbind it, because that would carefully
442 restore the bad value if there's an error
443 and make the loop of errors happen anyway. */
445 function = Vring_bell_function;
446 Vring_bell_function = Qnil;
448 call0 (function);
450 Vring_bell_function = function;
452 else if (!FRAME_TERMCAP_P (XFRAME (selected_frame)))
453 (*ring_bell_hook) ();
454 else
455 OUTPUT (TS_visible_bell && visible_bell ? TS_visible_bell : TS_bell);
458 void
459 set_terminal_modes ()
461 if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
463 if (TS_termcap_modes)
464 OUTPUT (TS_termcap_modes);
465 else
467 /* Output enough newlines to scroll all the old screen contents
468 off the screen, so it won't be overwritten and lost. */
469 int i;
470 for (i = 0; i < FRAME_LINES (XFRAME (selected_frame)); i++)
471 putchar ('\n');
474 OUTPUT_IF (visible_cursor ? TS_cursor_visible : TS_cursor_normal);
475 OUTPUT_IF (TS_keypad_mode);
476 losecursor ();
478 else
479 (*set_terminal_modes_hook) ();
482 void
483 reset_terminal_modes ()
485 if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
487 turn_off_highlight ();
488 turn_off_insert ();
489 OUTPUT_IF (TS_end_keypad_mode);
490 OUTPUT_IF (TS_cursor_normal);
491 OUTPUT_IF (TS_end_termcap_modes);
492 OUTPUT_IF (TS_orig_pair);
493 /* Output raw CR so kernel can track the cursor hpos. */
494 cmputc ('\r');
496 else if (reset_terminal_modes_hook)
497 (*reset_terminal_modes_hook) ();
500 void
501 update_begin (f)
502 struct frame *f;
504 updating_frame = f;
505 if (!FRAME_TERMCAP_P (f))
506 update_begin_hook (f);
509 void
510 update_end (f)
511 struct frame *f;
513 if (FRAME_TERMCAP_P (f))
515 if (!XWINDOW (selected_window)->cursor_off_p)
516 tty_show_cursor ();
517 turn_off_insert ();
518 background_highlight ();
520 else
521 update_end_hook (f);
523 updating_frame = NULL;
526 void
527 set_terminal_window (size)
528 int size;
530 if (FRAME_TERMCAP_P (updating_frame))
532 specified_window = size ? size : FRAME_LINES (updating_frame);
533 if (scroll_region_ok)
534 set_scroll_region (0, specified_window);
536 else
537 set_terminal_window_hook (size);
540 void
541 set_scroll_region (start, stop)
542 int start, stop;
544 char *buf;
545 struct frame *sf = XFRAME (selected_frame);
547 if (TS_set_scroll_region)
548 buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
549 else if (TS_set_scroll_region_1)
550 buf = tparam (TS_set_scroll_region_1, 0, 0,
551 FRAME_LINES (sf), start,
552 FRAME_LINES (sf) - stop,
553 FRAME_LINES (sf));
554 else
555 buf = tparam (TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (sf));
557 OUTPUT (buf);
558 xfree (buf);
559 losecursor ();
563 static void
564 turn_on_insert ()
566 if (!insert_mode)
567 OUTPUT (TS_insert_mode);
568 insert_mode = 1;
571 void
572 turn_off_insert ()
574 if (insert_mode)
575 OUTPUT (TS_end_insert_mode);
576 insert_mode = 0;
579 /* Handle highlighting. */
581 void
582 turn_off_highlight ()
584 if (standout_mode)
585 OUTPUT_IF (TS_end_standout_mode);
586 standout_mode = 0;
589 static void
590 turn_on_highlight ()
592 if (!standout_mode)
593 OUTPUT_IF (TS_standout_mode);
594 standout_mode = 1;
597 static void
598 toggle_highlight ()
600 if (standout_mode)
601 turn_off_highlight ();
602 else
603 turn_on_highlight ();
607 /* Make cursor invisible. */
609 static void
610 tty_hide_cursor ()
612 if (tty_cursor_hidden == 0)
614 tty_cursor_hidden = 1;
615 OUTPUT_IF (TS_cursor_invisible);
620 /* Ensure that cursor is visible. */
622 static void
623 tty_show_cursor ()
625 if (tty_cursor_hidden)
627 tty_cursor_hidden = 0;
628 OUTPUT_IF (TS_cursor_normal);
629 if (visible_cursor)
630 OUTPUT_IF (TS_cursor_visible);
635 /* Set standout mode to the state it should be in for
636 empty space inside windows. What this is,
637 depends on the user option inverse-video. */
639 void
640 background_highlight ()
642 if (inverse_video)
643 turn_on_highlight ();
644 else
645 turn_off_highlight ();
648 /* Set standout mode to the mode specified for the text to be output. */
650 static void
651 highlight_if_desired ()
653 if (inverse_video)
654 turn_on_highlight ();
655 else
656 turn_off_highlight ();
660 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
661 frame-relative coordinates. */
663 void
664 cursor_to (vpos, hpos)
665 int vpos, hpos;
667 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
669 if (! FRAME_TERMCAP_P (f) && cursor_to_hook)
671 (*cursor_to_hook) (vpos, hpos);
672 return;
675 /* Detect the case where we are called from reset_sys_modes
676 and the costs have never been calculated. Do nothing. */
677 if (! costs_set)
678 return;
680 if (curY == vpos && curX == hpos)
681 return;
682 if (!TF_standout_motion)
683 background_highlight ();
684 if (!TF_insmode_motion)
685 turn_off_insert ();
686 cmgoto (vpos, hpos);
689 /* Similar but don't take any account of the wasted characters. */
691 void
692 raw_cursor_to (row, col)
693 int row, col;
695 struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
696 if (! FRAME_TERMCAP_P (f))
698 (*raw_cursor_to_hook) (row, col);
699 return;
701 if (curY == row && curX == col)
702 return;
703 if (!TF_standout_motion)
704 background_highlight ();
705 if (!TF_insmode_motion)
706 turn_off_insert ();
707 cmgoto (row, col);
710 /* Erase operations */
712 /* clear from cursor to end of frame */
713 void
714 clear_to_end ()
716 register int i;
718 if (clear_to_end_hook && ! FRAME_TERMCAP_P (updating_frame))
720 (*clear_to_end_hook) ();
721 return;
723 if (TS_clr_to_bottom)
725 background_highlight ();
726 OUTPUT (TS_clr_to_bottom);
728 else
730 for (i = curY; i < FRAME_LINES (XFRAME (selected_frame)); i++)
732 cursor_to (i, 0);
733 clear_end_of_line (FRAME_COLS (XFRAME (selected_frame)));
738 /* Clear entire frame */
740 void
741 clear_frame ()
743 struct frame *sf = XFRAME (selected_frame);
745 if (clear_frame_hook
746 && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : sf)))
748 (*clear_frame_hook) ();
749 return;
751 if (TS_clr_frame)
753 background_highlight ();
754 OUTPUT (TS_clr_frame);
755 cmat (0, 0);
757 else
759 cursor_to (0, 0);
760 clear_to_end ();
764 /* Clear from cursor to end of line.
765 Assume that the line is already clear starting at column first_unused_hpos.
767 Note that the cursor may be moved, on terminals lacking a `ce' string. */
769 void
770 clear_end_of_line (first_unused_hpos)
771 int first_unused_hpos;
773 register int i;
775 if (clear_end_of_line_hook
776 && ! FRAME_TERMCAP_P ((updating_frame
777 ? updating_frame
778 : XFRAME (selected_frame))))
780 (*clear_end_of_line_hook) (first_unused_hpos);
781 return;
784 /* Detect the case where we are called from reset_sys_modes
785 and the costs have never been calculated. Do nothing. */
786 if (! costs_set)
787 return;
789 if (curX >= first_unused_hpos)
790 return;
791 background_highlight ();
792 if (TS_clr_line)
794 OUTPUT1 (TS_clr_line);
796 else
797 { /* have to do it the hard way */
798 struct frame *sf = XFRAME (selected_frame);
799 turn_off_insert ();
801 /* Do not write in last row last col with Auto-wrap on. */
802 if (AutoWrap && curY == FRAME_LINES (sf) - 1
803 && first_unused_hpos == FRAME_COLS (sf))
804 first_unused_hpos--;
806 for (i = curX; i < first_unused_hpos; i++)
808 if (termscript)
809 fputc (' ', termscript);
810 putchar (' ');
812 cmplus (first_unused_hpos - curX);
816 /* Buffer to store the source and result of code conversion for terminal. */
817 static unsigned char *encode_terminal_buf;
818 /* Allocated size of the above buffer. */
819 static int encode_terminal_bufsize;
821 /* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
822 Set CODING->produced to the byte-length of the resulting byte
823 sequence, and return a pointer to that byte sequence. */
825 unsigned char *
826 encode_terminal_code (src, src_len, coding)
827 struct glyph *src;
828 int src_len;
829 struct coding_system *coding;
831 struct glyph *src_end = src + src_len;
832 register GLYPH g;
833 unsigned char *buf;
834 int nchars, nbytes, required;
835 register int tlen = GLYPH_TABLE_LENGTH;
836 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
838 /* Allocate sufficient size of buffer to store all characters in
839 multibyte-form. But, it may be enlarged on demand if
840 Vglyph_table contains a string. */
841 required = MAX_MULTIBYTE_LENGTH * src_len;
842 if (encode_terminal_bufsize < required)
844 if (encode_terminal_bufsize == 0)
845 encode_terminal_buf = xmalloc (required);
846 else
847 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
848 encode_terminal_bufsize = required;
851 buf = encode_terminal_buf;
852 nchars = 0;
853 while (src < src_end)
855 /* We must skip glyphs to be padded for a wide character. */
856 if (! CHAR_GLYPH_PADDING_P (*src))
858 g = GLYPH_FROM_CHAR_GLYPH (src[0]);
860 if (g < 0 || g >= tlen)
862 /* This glyph doesn't has an entry in Vglyph_table. */
863 if (CHAR_VALID_P (src->u.ch, 0))
864 buf += CHAR_STRING (src->u.ch, buf);
865 else
866 *buf++ = SPACEGLYPH;
867 nchars++;
869 else
871 /* This glyph has an entry in Vglyph_table,
872 so process any alias before testing for simpleness. */
873 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
875 if (GLYPH_SIMPLE_P (tbase, tlen, g))
877 int c = FAST_GLYPH_CHAR (g);
879 if (CHAR_VALID_P (c, 0))
880 buf += CHAR_STRING (c, buf);
881 else
882 *buf++ = SPACEGLYPH;
883 nchars++;
885 else
887 /* We have a string in Vglyph_table. */
888 Lisp_Object string;
890 string = tbase[g];
891 if (! STRING_MULTIBYTE (string))
892 string = string_to_multibyte (string);
893 nbytes = buf - encode_terminal_buf;
894 if (encode_terminal_bufsize < nbytes + SBYTES (string))
896 encode_terminal_bufsize = nbytes + SBYTES (string);
897 encode_terminal_buf = xrealloc (encode_terminal_buf,
898 encode_terminal_bufsize);
899 buf = encode_terminal_buf + nbytes;
901 bcopy (SDATA (string), buf, SBYTES (string));
902 buf += SBYTES (string);
903 nchars += SCHARS (string);
907 src++;
910 nbytes = buf - encode_terminal_buf;
911 coding->src_multibyte = 1;
912 coding->dst_multibyte = 0;
913 if (SYMBOLP (coding->pre_write_conversion)
914 && ! NILP (Ffboundp (coding->pre_write_conversion)))
916 run_pre_write_conversin_on_c_str (&encode_terminal_buf,
917 &encode_terminal_bufsize,
918 nchars, nbytes, coding);
919 nchars = coding->produced_char;
920 nbytes = coding->produced;
922 required = nbytes + encoding_buffer_size (coding, nbytes);
923 if (encode_terminal_bufsize < required)
925 encode_terminal_bufsize = required;
926 encode_terminal_buf = xrealloc (encode_terminal_buf, required);
929 encode_coding (coding, encode_terminal_buf, encode_terminal_buf + nbytes,
930 nbytes, encode_terminal_bufsize - nbytes);
931 return encode_terminal_buf + nbytes;
934 void
935 write_glyphs (string, len)
936 register struct glyph *string;
937 register int len;
939 struct frame *sf = XFRAME (selected_frame);
940 struct frame *f = updating_frame ? updating_frame : sf;
941 unsigned char *conversion_buffer;
942 struct coding_system *coding;
944 if (write_glyphs_hook
945 && ! FRAME_TERMCAP_P (f))
947 (*write_glyphs_hook) (string, len);
948 return;
951 turn_off_insert ();
952 tty_hide_cursor ();
954 /* Don't dare write in last column of bottom line, if Auto-Wrap,
955 since that would scroll the whole frame on some terminals. */
957 if (AutoWrap
958 && curY + 1 == FRAME_LINES (sf)
959 && (curX + len) == FRAME_COLS (sf))
960 len --;
961 if (len <= 0)
962 return;
964 cmplus (len);
966 /* If terminal_coding does any conversion, use it, otherwise use
967 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
968 because it always return 1 if the member src_multibyte is 1. */
969 coding = (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
970 ? &terminal_coding : &safe_terminal_coding);
971 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
972 the tail. */
973 coding->mode &= ~CODING_MODE_LAST_BLOCK;
975 while (len > 0)
977 /* Identify a run of glyphs with the same face. */
978 int face_id = string->face_id;
979 int n;
981 for (n = 1; n < len; ++n)
982 if (string[n].face_id != face_id)
983 break;
985 /* Turn appearance modes of the face of the run on. */
986 highlight_if_desired ();
987 turn_on_face (f, face_id);
989 if (n == len)
990 /* This is the last run. */
991 coding->mode |= CODING_MODE_LAST_BLOCK;
992 conversion_buffer = encode_terminal_code (string, n, coding);
993 if (coding->produced > 0)
995 BLOCK_INPUT;
996 fwrite (conversion_buffer, 1, coding->produced, stdout);
997 if (ferror (stdout))
998 clearerr (stdout);
999 if (termscript)
1000 fwrite (conversion_buffer, 1, coding->produced, termscript);
1001 UNBLOCK_INPUT;
1003 len -= n;
1004 string += n;
1006 /* Turn appearance modes off. */
1007 turn_off_face (f, face_id);
1008 turn_off_highlight ();
1011 cmcheckmagic ();
1014 /* If start is zero, insert blanks instead of a string at start */
1016 void
1017 insert_glyphs (start, len)
1018 register struct glyph *start;
1019 register int len;
1021 char *buf;
1022 struct glyph *glyph = NULL;
1023 struct frame *f, *sf;
1024 unsigned char *conversion_buffer;
1025 unsigned char space[1];
1026 struct coding_system *coding;
1028 if (len <= 0)
1029 return;
1031 if (insert_glyphs_hook)
1033 (*insert_glyphs_hook) (start, len);
1034 return;
1037 sf = XFRAME (selected_frame);
1038 f = updating_frame ? updating_frame : sf;
1040 if (TS_ins_multi_chars)
1042 buf = tparam (TS_ins_multi_chars, 0, 0, len);
1043 OUTPUT1 (buf);
1044 xfree (buf);
1045 if (start)
1046 write_glyphs (start, len);
1047 return;
1050 turn_on_insert ();
1051 cmplus (len);
1053 if (! start)
1054 space[0] = SPACEGLYPH;
1056 /* If terminal_coding does any conversion, use it, otherwise use
1057 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
1058 because it always return 1 if the member src_multibyte is 1. */
1059 coding = (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
1060 ? &terminal_coding : &safe_terminal_coding);
1061 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
1062 the tail. */
1063 coding->mode &= ~CODING_MODE_LAST_BLOCK;
1065 while (len-- > 0)
1067 OUTPUT1_IF (TS_ins_char);
1068 if (!start)
1070 conversion_buffer = space;
1071 coding->produced = 1;
1073 else
1075 highlight_if_desired ();
1076 turn_on_face (f, start->face_id);
1077 glyph = start;
1078 ++start;
1079 /* We must open sufficient space for a character which
1080 occupies more than one column. */
1081 while (len && CHAR_GLYPH_PADDING_P (*start))
1083 OUTPUT1_IF (TS_ins_char);
1084 start++, len--;
1087 if (len <= 0)
1088 /* This is the last glyph. */
1089 coding->mode |= CODING_MODE_LAST_BLOCK;
1091 conversion_buffer = encode_terminal_code (glyph, 1, coding);
1094 if (coding->produced > 0)
1096 BLOCK_INPUT;
1097 fwrite (conversion_buffer, 1, coding->produced, stdout);
1098 if (ferror (stdout))
1099 clearerr (stdout);
1100 if (termscript)
1101 fwrite (conversion_buffer, 1, coding->produced, termscript);
1102 UNBLOCK_INPUT;
1105 OUTPUT1_IF (TS_pad_inserted_char);
1106 if (start)
1108 turn_off_face (f, glyph->face_id);
1109 turn_off_highlight ();
1113 cmcheckmagic ();
1116 void
1117 delete_glyphs (n)
1118 register int n;
1120 char *buf;
1121 register int i;
1123 if (delete_glyphs_hook && ! FRAME_TERMCAP_P (updating_frame))
1125 (*delete_glyphs_hook) (n);
1126 return;
1129 if (delete_in_insert_mode)
1131 turn_on_insert ();
1133 else
1135 turn_off_insert ();
1136 OUTPUT_IF (TS_delete_mode);
1139 if (TS_del_multi_chars)
1141 buf = tparam (TS_del_multi_chars, 0, 0, n);
1142 OUTPUT1 (buf);
1143 xfree (buf);
1145 else
1146 for (i = 0; i < n; i++)
1147 OUTPUT1 (TS_del_char);
1148 if (!delete_in_insert_mode)
1149 OUTPUT_IF (TS_end_delete_mode);
1152 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
1154 void
1155 ins_del_lines (vpos, n)
1156 int vpos, n;
1158 char *multi = n > 0 ? TS_ins_multi_lines : TS_del_multi_lines;
1159 char *single = n > 0 ? TS_ins_line : TS_del_line;
1160 char *scroll = n > 0 ? TS_rev_scroll : TS_fwd_scroll;
1161 struct frame *sf;
1163 register int i = n > 0 ? n : -n;
1164 register char *buf;
1166 if (ins_del_lines_hook && ! FRAME_TERMCAP_P (updating_frame))
1168 (*ins_del_lines_hook) (vpos, n);
1169 return;
1172 sf = XFRAME (selected_frame);
1174 /* If the lines below the insertion are being pushed
1175 into the end of the window, this is the same as clearing;
1176 and we know the lines are already clear, since the matching
1177 deletion has already been done. So can ignore this. */
1178 /* If the lines below the deletion are blank lines coming
1179 out of the end of the window, don't bother,
1180 as there will be a matching inslines later that will flush them. */
1181 if (scroll_region_ok && vpos + i >= specified_window)
1182 return;
1183 if (!memory_below_frame && vpos + i >= FRAME_LINES (sf))
1184 return;
1186 if (multi)
1188 raw_cursor_to (vpos, 0);
1189 background_highlight ();
1190 buf = tparam (multi, 0, 0, i);
1191 OUTPUT (buf);
1192 xfree (buf);
1194 else if (single)
1196 raw_cursor_to (vpos, 0);
1197 background_highlight ();
1198 while (--i >= 0)
1199 OUTPUT (single);
1200 if (TF_teleray)
1201 curX = 0;
1203 else
1205 set_scroll_region (vpos, specified_window);
1206 if (n < 0)
1207 raw_cursor_to (specified_window - 1, 0);
1208 else
1209 raw_cursor_to (vpos, 0);
1210 background_highlight ();
1211 while (--i >= 0)
1212 OUTPUTL (scroll, specified_window - vpos);
1213 set_scroll_region (0, specified_window);
1216 if (!scroll_region_ok && memory_below_frame && n < 0)
1218 cursor_to (FRAME_LINES (sf) + n, 0);
1219 clear_to_end ();
1223 /* Compute cost of sending "str", in characters,
1224 not counting any line-dependent padding. */
1227 string_cost (str)
1228 char *str;
1230 cost = 0;
1231 if (str)
1232 tputs (str, 0, evalcost);
1233 return cost;
1236 /* Compute cost of sending "str", in characters,
1237 counting any line-dependent padding at one line. */
1239 static int
1240 string_cost_one_line (str)
1241 char *str;
1243 cost = 0;
1244 if (str)
1245 tputs (str, 1, evalcost);
1246 return cost;
1249 /* Compute per line amount of line-dependent padding,
1250 in tenths of characters. */
1253 per_line_cost (str)
1254 register char *str;
1256 cost = 0;
1257 if (str)
1258 tputs (str, 0, evalcost);
1259 cost = - cost;
1260 if (str)
1261 tputs (str, 10, evalcost);
1262 return cost;
1265 #ifndef old
1266 /* char_ins_del_cost[n] is cost of inserting N characters.
1267 char_ins_del_cost[-n] is cost of deleting N characters.
1268 The length of this vector is based on max_frame_cols. */
1270 int *char_ins_del_vector;
1272 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
1273 #endif
1275 /* ARGSUSED */
1276 static void
1277 calculate_ins_del_char_costs (frame)
1278 FRAME_PTR frame;
1280 int ins_startup_cost, del_startup_cost;
1281 int ins_cost_per_char, del_cost_per_char;
1282 register int i;
1283 register int *p;
1285 if (TS_ins_multi_chars)
1287 ins_cost_per_char = 0;
1288 ins_startup_cost = string_cost_one_line (TS_ins_multi_chars);
1290 else if (TS_ins_char || TS_pad_inserted_char
1291 || (TS_insert_mode && TS_end_insert_mode))
1293 ins_startup_cost = (30 * (string_cost (TS_insert_mode)
1294 + string_cost (TS_end_insert_mode))) / 100;
1295 ins_cost_per_char = (string_cost_one_line (TS_ins_char)
1296 + string_cost_one_line (TS_pad_inserted_char));
1298 else
1300 ins_startup_cost = 9999;
1301 ins_cost_per_char = 0;
1304 if (TS_del_multi_chars)
1306 del_cost_per_char = 0;
1307 del_startup_cost = string_cost_one_line (TS_del_multi_chars);
1309 else if (TS_del_char)
1311 del_startup_cost = (string_cost (TS_delete_mode)
1312 + string_cost (TS_end_delete_mode));
1313 if (delete_in_insert_mode)
1314 del_startup_cost /= 2;
1315 del_cost_per_char = string_cost_one_line (TS_del_char);
1317 else
1319 del_startup_cost = 9999;
1320 del_cost_per_char = 0;
1323 /* Delete costs are at negative offsets */
1324 p = &char_ins_del_cost (frame)[0];
1325 for (i = FRAME_COLS (frame); --i >= 0;)
1326 *--p = (del_startup_cost += del_cost_per_char);
1328 /* Doing nothing is free */
1329 p = &char_ins_del_cost (frame)[0];
1330 *p++ = 0;
1332 /* Insert costs are at positive offsets */
1333 for (i = FRAME_COLS (frame); --i >= 0;)
1334 *p++ = (ins_startup_cost += ins_cost_per_char);
1337 void
1338 calculate_costs (frame)
1339 FRAME_PTR frame;
1341 register char *f = (TS_set_scroll_region
1342 ? TS_set_scroll_region
1343 : TS_set_scroll_region_1);
1345 FRAME_COST_BAUD_RATE (frame) = baud_rate;
1347 scroll_region_cost = string_cost (f);
1349 /* These variables are only used for terminal stuff. They are allocated
1350 once for the terminal frame of X-windows emacs, but not used afterwards.
1352 char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
1353 X turns off char_ins_del_ok. */
1355 max_frame_lines = max (max_frame_lines, FRAME_LINES (frame));
1356 max_frame_cols = max (max_frame_cols, FRAME_COLS (frame));
1358 costs_set = 1;
1360 if (char_ins_del_vector != 0)
1361 char_ins_del_vector
1362 = (int *) xrealloc (char_ins_del_vector,
1363 (sizeof (int)
1364 + 2 * max_frame_cols * sizeof (int)));
1365 else
1366 char_ins_del_vector
1367 = (int *) xmalloc (sizeof (int)
1368 + 2 * max_frame_cols * sizeof (int));
1370 bzero (char_ins_del_vector, (sizeof (int)
1371 + 2 * max_frame_cols * sizeof (int)));
1373 if (f && (!TS_ins_line && !TS_del_line))
1374 do_line_insertion_deletion_costs (frame,
1375 TS_rev_scroll, TS_ins_multi_lines,
1376 TS_fwd_scroll, TS_del_multi_lines,
1377 f, f, 1);
1378 else
1379 do_line_insertion_deletion_costs (frame,
1380 TS_ins_line, TS_ins_multi_lines,
1381 TS_del_line, TS_del_multi_lines,
1382 0, 0, 1);
1384 calculate_ins_del_char_costs (frame);
1386 /* Don't use TS_repeat if its padding is worse than sending the chars */
1387 if (TS_repeat && per_line_cost (TS_repeat) * baud_rate < 9000)
1388 RPov = string_cost (TS_repeat);
1389 else
1390 RPov = FRAME_COLS (frame) * 2;
1392 cmcostinit (); /* set up cursor motion costs */
1395 struct fkey_table {
1396 char *cap, *name;
1399 /* Termcap capability names that correspond directly to X keysyms.
1400 Some of these (marked "terminfo") aren't supplied by old-style
1401 (Berkeley) termcap entries. They're listed in X keysym order;
1402 except we put the keypad keys first, so that if they clash with
1403 other keys (as on the IBM PC keyboard) they get overridden.
1406 static struct fkey_table keys[] =
1408 {"kh", "home"}, /* termcap */
1409 {"kl", "left"}, /* termcap */
1410 {"ku", "up"}, /* termcap */
1411 {"kr", "right"}, /* termcap */
1412 {"kd", "down"}, /* termcap */
1413 {"%8", "prior"}, /* terminfo */
1414 {"%5", "next"}, /* terminfo */
1415 {"@7", "end"}, /* terminfo */
1416 {"@1", "begin"}, /* terminfo */
1417 {"*6", "select"}, /* terminfo */
1418 {"%9", "print"}, /* terminfo */
1419 {"@4", "execute"}, /* terminfo --- actually the `command' key */
1421 * "insert" --- see below
1423 {"&8", "undo"}, /* terminfo */
1424 {"%0", "redo"}, /* terminfo */
1425 {"%7", "menu"}, /* terminfo --- actually the `options' key */
1426 {"@0", "find"}, /* terminfo */
1427 {"@2", "cancel"}, /* terminfo */
1428 {"%1", "help"}, /* terminfo */
1430 * "break" goes here, but can't be reliably intercepted with termcap
1432 {"&4", "reset"}, /* terminfo --- actually `restart' */
1434 * "system" and "user" --- no termcaps
1436 {"kE", "clearline"}, /* terminfo */
1437 {"kA", "insertline"}, /* terminfo */
1438 {"kL", "deleteline"}, /* terminfo */
1439 {"kI", "insertchar"}, /* terminfo */
1440 {"kD", "deletechar"}, /* terminfo */
1441 {"kB", "backtab"}, /* terminfo */
1443 * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
1445 {"@8", "kp-enter"}, /* terminfo */
1447 * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
1448 * "kp-multiply", "kp-add", "kp-separator",
1449 * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
1450 * --- no termcaps for any of these.
1452 {"K4", "kp-1"}, /* terminfo */
1454 * "kp-2" --- no termcap
1456 {"K5", "kp-3"}, /* terminfo */
1458 * "kp-4" --- no termcap
1460 {"K2", "kp-5"}, /* terminfo */
1462 * "kp-6" --- no termcap
1464 {"K1", "kp-7"}, /* terminfo */
1466 * "kp-8" --- no termcap
1468 {"K3", "kp-9"}, /* terminfo */
1470 * "kp-equal" --- no termcap
1472 {"k1", "f1"},
1473 {"k2", "f2"},
1474 {"k3", "f3"},
1475 {"k4", "f4"},
1476 {"k5", "f5"},
1477 {"k6", "f6"},
1478 {"k7", "f7"},
1479 {"k8", "f8"},
1480 {"k9", "f9"},
1482 {"&0", "S-cancel"}, /*shifted cancel key*/
1483 {"&9", "S-begin"}, /*shifted begin key*/
1484 {"*0", "S-find"}, /*shifted find key*/
1485 {"*1", "S-execute"}, /*shifted execute? actually shifted command key*/
1486 {"*4", "S-delete"}, /*shifted delete-character key*/
1487 {"*7", "S-end"}, /*shifted end key*/
1488 {"*8", "S-clearline"}, /*shifted clear-to end-of-line key*/
1489 {"#1", "S-help"}, /*shifted help key*/
1490 {"#2", "S-home"}, /*shifted home key*/
1491 {"#3", "S-insert"}, /*shifted insert-character key*/
1492 {"#4", "S-left"}, /*shifted left-arrow key*/
1493 {"%d", "S-menu"}, /*shifted menu? actually shifted options key*/
1494 {"%c", "S-next"}, /*shifted next key*/
1495 {"%e", "S-prior"}, /*shifted previous key*/
1496 {"%f", "S-print"}, /*shifted print key*/
1497 {"%g", "S-redo"}, /*shifted redo key*/
1498 {"%i", "S-right"}, /*shifted right-arrow key*/
1499 {"!3", "S-undo"} /*shifted undo key*/
1502 static char **term_get_fkeys_arg;
1503 static Lisp_Object term_get_fkeys_1 ();
1505 /* Find the escape codes sent by the function keys for Vfunction_key_map.
1506 This function scans the termcap function key sequence entries, and
1507 adds entries to Vfunction_key_map for each function key it finds. */
1509 void
1510 term_get_fkeys (address)
1511 char **address;
1513 /* We run the body of the function (term_get_fkeys_1) and ignore all Lisp
1514 errors during the call. The only errors should be from Fdefine_key
1515 when given a key sequence containing an invalid prefix key. If the
1516 termcap defines function keys which use a prefix that is already bound
1517 to a command by the default bindings, we should silently ignore that
1518 function key specification, rather than giving the user an error and
1519 refusing to run at all on such a terminal. */
1521 extern Lisp_Object Fidentity ();
1522 term_get_fkeys_arg = address;
1523 internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
1526 static Lisp_Object
1527 term_get_fkeys_1 ()
1529 int i;
1531 char **address = term_get_fkeys_arg;
1533 /* This can happen if CANNOT_DUMP or with strange options. */
1534 if (!initialized)
1535 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
1537 for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
1539 char *sequence = tgetstr (keys[i].cap, address);
1540 if (sequence)
1541 Fdefine_key (Vfunction_key_map, build_string (sequence),
1542 Fmake_vector (make_number (1),
1543 intern (keys[i].name)));
1546 /* The uses of the "k0" capability are inconsistent; sometimes it
1547 describes F10, whereas othertimes it describes F0 and "k;" describes F10.
1548 We will attempt to politely accommodate both systems by testing for
1549 "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
1552 char *k_semi = tgetstr ("k;", address);
1553 char *k0 = tgetstr ("k0", address);
1554 char *k0_name = "f10";
1556 if (k_semi)
1558 if (k0)
1559 /* Define f0 first, so that f10 takes precedence in case the
1560 key sequences happens to be the same. */
1561 Fdefine_key (Vfunction_key_map, build_string (k0),
1562 Fmake_vector (make_number (1), intern ("f0")));
1563 Fdefine_key (Vfunction_key_map, build_string (k_semi),
1564 Fmake_vector (make_number (1), intern ("f10")));
1566 else if (k0)
1567 Fdefine_key (Vfunction_key_map, build_string (k0),
1568 Fmake_vector (make_number (1), intern (k0_name)));
1571 /* Set up cookies for numbered function keys above f10. */
1573 char fcap[3], fkey[4];
1575 fcap[0] = 'F'; fcap[2] = '\0';
1576 for (i = 11; i < 64; i++)
1578 if (i <= 19)
1579 fcap[1] = '1' + i - 11;
1580 else if (i <= 45)
1581 fcap[1] = 'A' + i - 20;
1582 else
1583 fcap[1] = 'a' + i - 46;
1586 char *sequence = tgetstr (fcap, address);
1587 if (sequence)
1589 sprintf (fkey, "f%d", i);
1590 Fdefine_key (Vfunction_key_map, build_string (sequence),
1591 Fmake_vector (make_number (1),
1592 intern (fkey)));
1599 * Various mappings to try and get a better fit.
1602 #define CONDITIONAL_REASSIGN(cap1, cap2, sym) \
1603 if (!tgetstr (cap1, address)) \
1605 char *sequence = tgetstr (cap2, address); \
1606 if (sequence) \
1607 Fdefine_key (Vfunction_key_map, build_string (sequence), \
1608 Fmake_vector (make_number (1), \
1609 intern (sym))); \
1612 /* if there's no key_next keycap, map key_npage to `next' keysym */
1613 CONDITIONAL_REASSIGN ("%5", "kN", "next");
1614 /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
1615 CONDITIONAL_REASSIGN ("%8", "kP", "prior");
1616 /* if there's no key_dc keycap, map key_ic to `insert' keysym */
1617 CONDITIONAL_REASSIGN ("kD", "kI", "insert");
1618 /* if there's no key_end keycap, map key_ll to 'end' keysym */
1619 CONDITIONAL_REASSIGN ("@7", "kH", "end");
1621 /* IBM has their own non-standard dialect of terminfo.
1622 If the standard name isn't found, try the IBM name. */
1623 CONDITIONAL_REASSIGN ("kB", "KO", "backtab");
1624 CONDITIONAL_REASSIGN ("@4", "kJ", "execute"); /* actually "action" */
1625 CONDITIONAL_REASSIGN ("@4", "kc", "execute"); /* actually "command" */
1626 CONDITIONAL_REASSIGN ("%7", "ki", "menu");
1627 CONDITIONAL_REASSIGN ("@7", "kw", "end");
1628 CONDITIONAL_REASSIGN ("F1", "k<", "f11");
1629 CONDITIONAL_REASSIGN ("F2", "k>", "f12");
1630 CONDITIONAL_REASSIGN ("%1", "kq", "help");
1631 CONDITIONAL_REASSIGN ("*6", "kU", "select");
1632 #undef CONDITIONAL_REASSIGN
1635 return Qnil;
1639 /***********************************************************************
1640 Character Display Information
1641 ***********************************************************************/
1643 /* Avoid name clash with functions defined in xterm.c */
1644 #ifdef static
1645 #define append_glyph append_glyph_term
1646 #define produce_stretch_glyph produce_stretch_glyph_term
1647 #endif
1649 static void append_glyph P_ ((struct it *));
1650 static void produce_stretch_glyph P_ ((struct it *));
1653 /* Append glyphs to IT's glyph_row. Called from produce_glyphs for
1654 terminal frames if IT->glyph_row != NULL. IT->char_to_display is
1655 the character for which to produce glyphs; IT->face_id contains the
1656 character's face. Padding glyphs are appended if IT->c has a
1657 IT->pixel_width > 1. */
1659 static void
1660 append_glyph (it)
1661 struct it *it;
1663 struct glyph *glyph, *end;
1664 int i;
1666 xassert (it->glyph_row);
1667 glyph = (it->glyph_row->glyphs[it->area]
1668 + it->glyph_row->used[it->area]);
1669 end = it->glyph_row->glyphs[1 + it->area];
1671 for (i = 0;
1672 i < it->pixel_width && glyph < end;
1673 ++i)
1675 glyph->type = CHAR_GLYPH;
1676 glyph->pixel_width = 1;
1677 glyph->u.ch = it->char_to_display;
1678 glyph->face_id = it->face_id;
1679 glyph->padding_p = i > 0;
1680 glyph->charpos = CHARPOS (it->position);
1681 glyph->object = it->object;
1683 ++it->glyph_row->used[it->area];
1684 ++glyph;
1689 /* Produce glyphs for the display element described by IT. *IT
1690 specifies what we want to produce a glyph for (character, image, ...),
1691 and where in the glyph matrix we currently are (glyph row and hpos).
1692 produce_glyphs fills in output fields of *IT with information such as the
1693 pixel width and height of a character, and maybe output actual glyphs at
1694 the same time if IT->glyph_row is non-null. See the explanation of
1695 struct display_iterator in dispextern.h for an overview.
1697 produce_glyphs also stores the result of glyph width, ascent
1698 etc. computations in *IT.
1700 IT->glyph_row may be null, in which case produce_glyphs does not
1701 actually fill in the glyphs. This is used in the move_* functions
1702 in xdisp.c for text width and height computations.
1704 Callers usually don't call produce_glyphs directly;
1705 instead they use the macro PRODUCE_GLYPHS. */
1707 void
1708 produce_glyphs (it)
1709 struct it *it;
1711 /* If a hook is installed, let it do the work. */
1712 xassert (it->what == IT_CHARACTER
1713 || it->what == IT_COMPOSITION
1714 || it->what == IT_STRETCH);
1716 if (it->what == IT_STRETCH)
1718 produce_stretch_glyph (it);
1719 goto done;
1722 /* Nothing but characters are supported on terminal frames. For a
1723 composition sequence, it->c is the first character of the
1724 sequence. */
1725 xassert (it->what == IT_CHARACTER
1726 || it->what == IT_COMPOSITION);
1728 /* Maybe translate single-byte characters to multibyte. */
1729 it->char_to_display = it->c;
1731 if (it->c >= 040 && it->c < 0177)
1733 it->pixel_width = it->nglyphs = 1;
1734 if (it->glyph_row)
1735 append_glyph (it);
1737 else if (it->c == '\n')
1738 it->pixel_width = it->nglyphs = 0;
1739 else if (it->c == '\t')
1741 int absolute_x = (it->current_x
1742 + it->continuation_lines_width);
1743 int next_tab_x
1744 = (((1 + absolute_x + it->tab_width - 1)
1745 / it->tab_width)
1746 * it->tab_width);
1747 int nspaces;
1749 /* If part of the TAB has been displayed on the previous line
1750 which is continued now, continuation_lines_width will have
1751 been incremented already by the part that fitted on the
1752 continued line. So, we will get the right number of spaces
1753 here. */
1754 nspaces = next_tab_x - absolute_x;
1756 if (it->glyph_row)
1758 int n = nspaces;
1760 it->char_to_display = ' ';
1761 it->pixel_width = it->len = 1;
1763 while (n--)
1764 append_glyph (it);
1767 it->pixel_width = nspaces;
1768 it->nglyphs = nspaces;
1770 else if (SINGLE_BYTE_CHAR_P (it->c))
1772 if (unibyte_display_via_language_environment
1773 && (it->c >= 0240
1774 || !NILP (Vnonascii_translation_table)))
1776 int charset;
1778 it->char_to_display = unibyte_char_to_multibyte (it->c);
1779 charset = CHAR_CHARSET (it->char_to_display);
1780 it->pixel_width = CHARSET_WIDTH (charset);
1781 it->nglyphs = it->pixel_width;
1782 if (it->glyph_row)
1783 append_glyph (it);
1785 else
1787 /* Coming here means that it->c is from display table, thus we
1788 must send the code as is to the terminal. Although there's
1789 no way to know how many columns it occupies on a screen, it
1790 is a good assumption that a single byte code has 1-column
1791 width. */
1792 it->pixel_width = it->nglyphs = 1;
1793 if (it->glyph_row)
1794 append_glyph (it);
1797 else
1799 /* A multi-byte character. The display width is fixed for all
1800 characters of the set. Some of the glyphs may have to be
1801 ignored because they are already displayed in a continued
1802 line. */
1803 int charset = CHAR_CHARSET (it->c);
1805 it->pixel_width = CHARSET_WIDTH (charset);
1806 it->nglyphs = it->pixel_width;
1808 if (it->glyph_row)
1809 append_glyph (it);
1812 done:
1813 /* Advance current_x by the pixel width as a convenience for
1814 the caller. */
1815 if (it->area == TEXT_AREA)
1816 it->current_x += it->pixel_width;
1817 it->ascent = it->max_ascent = it->phys_ascent = it->max_phys_ascent = 0;
1818 it->descent = it->max_descent = it->phys_descent = it->max_phys_descent = 1;
1822 /* Produce a stretch glyph for iterator IT. IT->object is the value
1823 of the glyph property displayed. The value must be a list
1824 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1825 being recognized:
1827 1. `:width WIDTH' specifies that the space should be WIDTH *
1828 canonical char width wide. WIDTH may be an integer or floating
1829 point number.
1831 2. `:align-to HPOS' specifies that the space should be wide enough
1832 to reach HPOS, a value in canonical character units. */
1834 static void
1835 produce_stretch_glyph (it)
1836 struct it *it;
1838 /* (space :width WIDTH ...) */
1839 Lisp_Object prop, plist;
1840 int width = 0, align_to = -1;
1841 int zero_width_ok_p = 0;
1842 double tem;
1844 /* List should start with `space'. */
1845 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1846 plist = XCDR (it->object);
1848 /* Compute the width of the stretch. */
1849 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
1850 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, 0))
1852 /* Absolute width `:width WIDTH' specified and valid. */
1853 zero_width_ok_p = 1;
1854 width = (int)(tem + 0.5);
1856 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
1857 && calc_pixel_width_or_height (&tem, it, prop, 0, 1, &align_to))
1859 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
1860 align_to = (align_to < 0
1862 : align_to - window_box_left_offset (it->w, TEXT_AREA));
1863 else if (align_to < 0)
1864 align_to = window_box_left_offset (it->w, TEXT_AREA);
1865 width = max (0, (int)(tem + 0.5) + align_to - it->current_x);
1866 zero_width_ok_p = 1;
1868 else
1869 /* Nothing specified -> width defaults to canonical char width. */
1870 width = FRAME_COLUMN_WIDTH (it->f);
1872 if (width <= 0 && (width < 0 || !zero_width_ok_p))
1873 width = 1;
1875 if (width > 0 && it->glyph_row)
1877 Lisp_Object o_object = it->object;
1878 Lisp_Object object = it->stack[it->sp - 1].string;
1879 int n = width;
1881 if (!STRINGP (object))
1882 object = it->w->buffer;
1883 it->object = object;
1884 it->char_to_display = ' ';
1885 it->pixel_width = it->len = 1;
1886 while (n--)
1887 append_glyph (it);
1888 it->object = o_object;
1890 it->pixel_width = width;
1891 it->nglyphs = width;
1895 /* Get information about special display element WHAT in an
1896 environment described by IT. WHAT is one of IT_TRUNCATION or
1897 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
1898 non-null glyph_row member. This function ensures that fields like
1899 face_id, c, len of IT are left untouched. */
1901 void
1902 produce_special_glyphs (it, what)
1903 struct it *it;
1904 enum display_element_type what;
1906 struct it temp_it;
1907 GLYPH glyph;
1909 temp_it = *it;
1910 temp_it.dp = NULL;
1911 temp_it.what = IT_CHARACTER;
1912 temp_it.len = 1;
1913 temp_it.object = make_number (0);
1914 bzero (&temp_it.current, sizeof temp_it.current);
1916 if (what == IT_CONTINUATION)
1918 /* Continuation glyph. */
1919 if (it->dp
1920 && INTEGERP (DISP_CONTINUE_GLYPH (it->dp))
1921 && GLYPH_CHAR_VALID_P (XINT (DISP_CONTINUE_GLYPH (it->dp))))
1923 glyph = XINT (DISP_CONTINUE_GLYPH (it->dp));
1924 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1926 else
1927 glyph = '\\';
1929 else if (what == IT_TRUNCATION)
1931 /* Truncation glyph. */
1932 if (it->dp
1933 && INTEGERP (DISP_TRUNC_GLYPH (it->dp))
1934 && GLYPH_CHAR_VALID_P (XINT (DISP_TRUNC_GLYPH (it->dp))))
1936 glyph = XINT (DISP_TRUNC_GLYPH (it->dp));
1937 glyph = spec_glyph_lookup_face (XWINDOW (it->window), glyph);
1939 else
1940 glyph = '$';
1942 else
1943 abort ();
1945 temp_it.c = FAST_GLYPH_CHAR (glyph);
1946 temp_it.face_id = FAST_GLYPH_FACE (glyph);
1947 temp_it.len = CHAR_BYTES (temp_it.c);
1949 produce_glyphs (&temp_it);
1950 it->pixel_width = temp_it.pixel_width;
1951 it->nglyphs = temp_it.pixel_width;
1956 /***********************************************************************
1957 Faces
1958 ***********************************************************************/
1960 /* Value is non-zero if attribute ATTR may be used. ATTR should be
1961 one of the enumerators from enum no_color_bit, or a bit set built
1962 from them. Some display attributes may not be used together with
1963 color; the termcap capability `NC' specifies which ones. */
1965 #define MAY_USE_WITH_COLORS_P(ATTR) \
1966 (TN_max_colors > 0 \
1967 ? (TN_no_color_video & (ATTR)) == 0 \
1968 : 1)
1970 /* Turn appearances of face FACE_ID on tty frame F on.
1971 FACE_ID is a realized face ID number, in the face cache. */
1973 static void
1974 turn_on_face (f, face_id)
1975 struct frame *f;
1976 int face_id;
1978 struct face *face = FACE_FROM_ID (f, face_id);
1979 long fg = face->foreground;
1980 long bg = face->background;
1982 /* Do this first because TS_end_standout_mode may be the same
1983 as TS_exit_attribute_mode, which turns all appearances off. */
1984 if (MAY_USE_WITH_COLORS_P (NC_REVERSE))
1986 if (TN_max_colors > 0)
1988 if (fg >= 0 && bg >= 0)
1990 /* If the terminal supports colors, we can set them
1991 below without using reverse video. The face's fg
1992 and bg colors are set as they should appear on
1993 the screen, i.e. they take the inverse-video'ness
1994 of the face already into account. */
1996 else if (inverse_video)
1998 if (fg == FACE_TTY_DEFAULT_FG_COLOR
1999 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2000 toggle_highlight ();
2002 else
2004 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2005 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2006 toggle_highlight ();
2009 else
2011 /* If we can't display colors, use reverse video
2012 if the face specifies that. */
2013 if (inverse_video)
2015 if (fg == FACE_TTY_DEFAULT_FG_COLOR
2016 || bg == FACE_TTY_DEFAULT_BG_COLOR)
2017 toggle_highlight ();
2019 else
2021 if (fg == FACE_TTY_DEFAULT_BG_COLOR
2022 || bg == FACE_TTY_DEFAULT_FG_COLOR)
2023 toggle_highlight ();
2028 if (face->tty_bold_p)
2030 if (MAY_USE_WITH_COLORS_P (NC_BOLD))
2031 OUTPUT1_IF (TS_enter_bold_mode);
2033 else if (face->tty_dim_p)
2034 if (MAY_USE_WITH_COLORS_P (NC_DIM))
2035 OUTPUT1_IF (TS_enter_dim_mode);
2037 /* Alternate charset and blinking not yet used. */
2038 if (face->tty_alt_charset_p
2039 && MAY_USE_WITH_COLORS_P (NC_ALT_CHARSET))
2040 OUTPUT1_IF (TS_enter_alt_charset_mode);
2042 if (face->tty_blinking_p
2043 && MAY_USE_WITH_COLORS_P (NC_BLINK))
2044 OUTPUT1_IF (TS_enter_blink_mode);
2046 if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (NC_UNDERLINE))
2047 OUTPUT1_IF (TS_enter_underline_mode);
2049 if (TN_max_colors > 0)
2051 char *ts, *p;
2053 ts = standout_mode ? TS_set_background : TS_set_foreground;
2054 if (fg >= 0 && ts)
2056 p = tparam (ts, NULL, 0, (int) fg);
2057 OUTPUT (p);
2058 xfree (p);
2061 ts = standout_mode ? TS_set_foreground : TS_set_background;
2062 if (bg >= 0 && ts)
2064 p = tparam (ts, NULL, 0, (int) bg);
2065 OUTPUT (p);
2066 xfree (p);
2072 /* Turn off appearances of face FACE_ID on tty frame F. */
2074 static void
2075 turn_off_face (f, face_id)
2076 struct frame *f;
2077 int face_id;
2079 struct face *face = FACE_FROM_ID (f, face_id);
2081 xassert (face != NULL);
2083 if (TS_exit_attribute_mode)
2085 /* Capability "me" will turn off appearance modes double-bright,
2086 half-bright, reverse-video, standout, underline. It may or
2087 may not turn off alt-char-mode. */
2088 if (face->tty_bold_p
2089 || face->tty_dim_p
2090 || face->tty_reverse_p
2091 || face->tty_alt_charset_p
2092 || face->tty_blinking_p
2093 || face->tty_underline_p)
2095 OUTPUT1_IF (TS_exit_attribute_mode);
2096 if (strcmp (TS_exit_attribute_mode, TS_end_standout_mode) == 0)
2097 standout_mode = 0;
2100 if (face->tty_alt_charset_p)
2101 OUTPUT_IF (TS_exit_alt_charset_mode);
2103 else
2105 /* If we don't have "me" we can only have those appearances
2106 that have exit sequences defined. */
2107 if (face->tty_alt_charset_p)
2108 OUTPUT_IF (TS_exit_alt_charset_mode);
2110 if (face->tty_underline_p)
2111 OUTPUT_IF (TS_exit_underline_mode);
2114 /* Switch back to default colors. */
2115 if (TN_max_colors > 0
2116 && ((face->foreground != FACE_TTY_DEFAULT_COLOR
2117 && face->foreground != FACE_TTY_DEFAULT_FG_COLOR)
2118 || (face->background != FACE_TTY_DEFAULT_COLOR
2119 && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
2120 OUTPUT1_IF (TS_orig_pair);
2124 /* Return non-zero if the terminal on frame F supports all of the
2125 capabilities in CAPS simultaneously, with foreground and background
2126 colors FG and BG. */
2129 tty_capable_p (f, caps, fg, bg)
2130 struct frame *f;
2131 unsigned caps;
2132 unsigned long fg, bg;
2134 #define TTY_CAPABLE_P_TRY(cap, TS, NC_bit) \
2135 if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(NC_bit))) \
2136 return 0;
2138 TTY_CAPABLE_P_TRY (TTY_CAP_INVERSE, TS_standout_mode, NC_REVERSE);
2139 TTY_CAPABLE_P_TRY (TTY_CAP_UNDERLINE, TS_enter_underline_mode, NC_UNDERLINE);
2140 TTY_CAPABLE_P_TRY (TTY_CAP_BOLD, TS_enter_bold_mode, NC_BOLD);
2141 TTY_CAPABLE_P_TRY (TTY_CAP_DIM, TS_enter_dim_mode, NC_DIM);
2142 TTY_CAPABLE_P_TRY (TTY_CAP_BLINK, TS_enter_blink_mode, NC_BLINK);
2143 TTY_CAPABLE_P_TRY (TTY_CAP_ALT_CHARSET, TS_enter_alt_charset_mode, NC_ALT_CHARSET);
2145 /* We can do it! */
2146 return 1;
2150 /* Return non-zero if the terminal is capable to display colors. */
2152 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
2153 0, 1, 0,
2154 doc: /* Return non-nil if TTY can display colors on DISPLAY. */)
2155 (display)
2156 Lisp_Object display;
2158 return TN_max_colors > 0 ? Qt : Qnil;
2161 /* Return the number of supported colors. */
2162 DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
2163 Stty_display_color_cells, 0, 1, 0,
2164 doc: /* Return the number of colors supported by TTY on DISPLAY. */)
2165 (display)
2166 Lisp_Object display;
2168 return make_number (TN_max_colors);
2171 #ifndef WINDOWSNT
2173 /* Declare here rather than in the function, as in the rest of Emacs,
2174 to work around an HPUX compiler bug (?). See
2175 http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00410.html */
2176 static int default_max_colors;
2177 static int default_max_pairs;
2178 static int default_no_color_video;
2179 static char *default_orig_pair;
2180 static char *default_set_foreground;
2181 static char *default_set_background;
2183 /* Save or restore the default color-related capabilities of this
2184 terminal. */
2185 static void
2186 tty_default_color_capabilities (save)
2187 int save;
2190 if (save)
2192 if (default_orig_pair)
2193 xfree (default_orig_pair);
2194 default_orig_pair = TS_orig_pair ? xstrdup (TS_orig_pair) : NULL;
2196 if (default_set_foreground)
2197 xfree (default_set_foreground);
2198 default_set_foreground = TS_set_foreground ? xstrdup (TS_set_foreground)
2199 : NULL;
2201 if (default_set_background)
2202 xfree (default_set_background);
2203 default_set_background = TS_set_background ? xstrdup (TS_set_background)
2204 : NULL;
2206 default_max_colors = TN_max_colors;
2207 default_max_pairs = TN_max_pairs;
2208 default_no_color_video = TN_no_color_video;
2210 else
2212 TS_orig_pair = default_orig_pair;
2213 TS_set_foreground = default_set_foreground;
2214 TS_set_background = default_set_background;
2215 TN_max_colors = default_max_colors;
2216 TN_max_pairs = default_max_pairs;
2217 TN_no_color_video = default_no_color_video;
2221 /* Setup one of the standard tty color schemes according to MODE.
2222 MODE's value is generally the number of colors which we want to
2223 support; zero means set up for the default capabilities, the ones
2224 we saw at term_init time; -1 means turn off color support. */
2225 void
2226 tty_setup_colors (mode)
2227 int mode;
2229 /* Canonicalize all negative values of MODE. */
2230 if (mode < -1)
2231 mode = -1;
2233 switch (mode)
2235 case -1: /* no colors at all */
2236 TN_max_colors = 0;
2237 TN_max_pairs = 0;
2238 TN_no_color_video = 0;
2239 TS_set_foreground = TS_set_background = TS_orig_pair = NULL;
2240 break;
2241 case 0: /* default colors, if any */
2242 default:
2243 tty_default_color_capabilities (0);
2244 break;
2245 case 8: /* 8 standard ANSI colors */
2246 TS_orig_pair = "\033[0m";
2247 #ifdef TERMINFO
2248 TS_set_foreground = "\033[3%p1%dm";
2249 TS_set_background = "\033[4%p1%dm";
2250 #else
2251 TS_set_foreground = "\033[3%dm";
2252 TS_set_background = "\033[4%dm";
2253 #endif
2254 TN_max_colors = 8;
2255 TN_max_pairs = 64;
2256 TN_no_color_video = 0;
2257 break;
2261 void
2262 set_tty_color_mode (f, val)
2263 struct frame *f;
2264 Lisp_Object val;
2266 Lisp_Object color_mode_spec, current_mode_spec;
2267 Lisp_Object color_mode, current_mode;
2268 int mode, old_mode;
2269 extern Lisp_Object Qtty_color_mode;
2270 Lisp_Object tty_color_mode_alist;
2272 tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
2273 Qnil);
2275 if (INTEGERP (val))
2276 color_mode = val;
2277 else
2279 if (NILP (tty_color_mode_alist))
2280 color_mode_spec = Qnil;
2281 else
2282 color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
2284 if (CONSP (color_mode_spec))
2285 color_mode = XCDR (color_mode_spec);
2286 else
2287 color_mode = Qnil;
2290 current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
2292 if (CONSP (current_mode_spec))
2293 current_mode = XCDR (current_mode_spec);
2294 else
2295 current_mode = Qnil;
2296 if (INTEGERP (color_mode))
2297 mode = XINT (color_mode);
2298 else
2299 mode = 0; /* meaning default */
2300 if (INTEGERP (current_mode))
2301 old_mode = XINT (current_mode);
2302 else
2303 old_mode = 0;
2305 if (mode != old_mode)
2307 tty_setup_colors (mode);
2308 /* This recomputes all the faces given the new color
2309 definitions. */
2310 call0 (intern ("tty-set-up-initial-frame-faces"));
2311 redraw_frame (f);
2315 #endif /* !WINDOWSNT */
2318 /***********************************************************************
2319 Initialization
2320 ***********************************************************************/
2322 void
2323 term_init (terminal_type)
2324 char *terminal_type;
2326 char *area;
2327 char **address = &area;
2328 char *buffer = NULL;
2329 int buffer_size = 4096;
2330 register char *p;
2331 int status;
2332 struct frame *sf = XFRAME (selected_frame);
2334 encode_terminal_bufsize = 0;
2336 #ifdef WINDOWSNT
2337 initialize_w32_display ();
2339 Wcm_clear ();
2341 area = (char *) xmalloc (2044);
2343 FrameRows = FRAME_LINES (sf);
2344 FrameCols = FRAME_COLS (sf);
2345 specified_window = FRAME_LINES (sf);
2347 delete_in_insert_mode = 1;
2349 UseTabs = 0;
2350 scroll_region_ok = 0;
2352 /* Seems to insert lines when it's not supposed to, messing
2353 up the display. In doing a trace, it didn't seem to be
2354 called much, so I don't think we're losing anything by
2355 turning it off. */
2357 line_ins_del_ok = 0;
2358 char_ins_del_ok = 1;
2360 baud_rate = 19200;
2362 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2363 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
2364 TN_max_colors = 16; /* Required to be non-zero for tty-display-color-p */
2366 return;
2367 #else /* not WINDOWSNT */
2369 Wcm_clear ();
2371 buffer = (char *) xmalloc (buffer_size);
2372 status = tgetent (buffer, terminal_type);
2373 if (status < 0)
2375 #ifdef TERMINFO
2376 fatal ("Cannot open terminfo database file");
2377 #else
2378 fatal ("Cannot open termcap database file");
2379 #endif
2381 if (status == 0)
2383 #ifdef TERMINFO
2384 fatal ("Terminal type %s is not defined.\n\
2385 If that is not the actual type of terminal you have,\n\
2386 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2387 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2388 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2389 terminal_type);
2390 #else
2391 fatal ("Terminal type %s is not defined.\n\
2392 If that is not the actual type of terminal you have,\n\
2393 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2394 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2395 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2396 terminal_type);
2397 #endif
2400 #ifndef TERMINFO
2401 if (strlen (buffer) >= buffer_size)
2402 abort ();
2403 buffer_size = strlen (buffer);
2404 #endif
2405 area = (char *) xmalloc (buffer_size);
2407 TS_ins_line = tgetstr ("al", address);
2408 TS_ins_multi_lines = tgetstr ("AL", address);
2409 TS_bell = tgetstr ("bl", address);
2410 BackTab = tgetstr ("bt", address);
2411 TS_clr_to_bottom = tgetstr ("cd", address);
2412 TS_clr_line = tgetstr ("ce", address);
2413 TS_clr_frame = tgetstr ("cl", address);
2414 ColPosition = NULL; /* tgetstr ("ch", address); */
2415 AbsPosition = tgetstr ("cm", address);
2416 CR = tgetstr ("cr", address);
2417 TS_set_scroll_region = tgetstr ("cs", address);
2418 TS_set_scroll_region_1 = tgetstr ("cS", address);
2419 RowPosition = tgetstr ("cv", address);
2420 TS_del_char = tgetstr ("dc", address);
2421 TS_del_multi_chars = tgetstr ("DC", address);
2422 TS_del_line = tgetstr ("dl", address);
2423 TS_del_multi_lines = tgetstr ("DL", address);
2424 TS_delete_mode = tgetstr ("dm", address);
2425 TS_end_delete_mode = tgetstr ("ed", address);
2426 TS_end_insert_mode = tgetstr ("ei", address);
2427 Home = tgetstr ("ho", address);
2428 TS_ins_char = tgetstr ("ic", address);
2429 TS_ins_multi_chars = tgetstr ("IC", address);
2430 TS_insert_mode = tgetstr ("im", address);
2431 TS_pad_inserted_char = tgetstr ("ip", address);
2432 TS_end_keypad_mode = tgetstr ("ke", address);
2433 TS_keypad_mode = tgetstr ("ks", address);
2434 LastLine = tgetstr ("ll", address);
2435 Right = tgetstr ("nd", address);
2436 Down = tgetstr ("do", address);
2437 if (!Down)
2438 Down = tgetstr ("nl", address); /* Obsolete name for "do" */
2439 #ifdef VMS
2440 /* VMS puts a carriage return before each linefeed,
2441 so it is not safe to use linefeeds. */
2442 if (Down && Down[0] == '\n' && Down[1] == '\0')
2443 Down = 0;
2444 #endif /* VMS */
2445 if (tgetflag ("bs"))
2446 Left = "\b"; /* can't possibly be longer! */
2447 else /* (Actually, "bs" is obsolete...) */
2448 Left = tgetstr ("le", address);
2449 if (!Left)
2450 Left = tgetstr ("bc", address); /* Obsolete name for "le" */
2451 TS_pad_char = tgetstr ("pc", address);
2452 TS_repeat = tgetstr ("rp", address);
2453 TS_end_standout_mode = tgetstr ("se", address);
2454 TS_fwd_scroll = tgetstr ("sf", address);
2455 TS_standout_mode = tgetstr ("so", address);
2456 TS_rev_scroll = tgetstr ("sr", address);
2457 Wcm.cm_tab = tgetstr ("ta", address);
2458 TS_end_termcap_modes = tgetstr ("te", address);
2459 TS_termcap_modes = tgetstr ("ti", address);
2460 Up = tgetstr ("up", address);
2461 TS_visible_bell = tgetstr ("vb", address);
2462 TS_cursor_normal = tgetstr ("ve", address);
2463 TS_cursor_visible = tgetstr ("vs", address);
2464 TS_cursor_invisible = tgetstr ("vi", address);
2465 TS_set_window = tgetstr ("wi", address);
2467 TS_enter_underline_mode = tgetstr ("us", address);
2468 TS_exit_underline_mode = tgetstr ("ue", address);
2469 TS_enter_bold_mode = tgetstr ("md", address);
2470 TS_enter_dim_mode = tgetstr ("mh", address);
2471 TS_enter_blink_mode = tgetstr ("mb", address);
2472 TS_enter_reverse_mode = tgetstr ("mr", address);
2473 TS_enter_alt_charset_mode = tgetstr ("as", address);
2474 TS_exit_alt_charset_mode = tgetstr ("ae", address);
2475 TS_exit_attribute_mode = tgetstr ("me", address);
2477 MultiUp = tgetstr ("UP", address);
2478 MultiDown = tgetstr ("DO", address);
2479 MultiLeft = tgetstr ("LE", address);
2480 MultiRight = tgetstr ("RI", address);
2482 /* SVr4/ANSI color suppert. If "op" isn't available, don't support
2483 color because we can't switch back to the default foreground and
2484 background. */
2485 TS_orig_pair = tgetstr ("op", address);
2486 if (TS_orig_pair)
2488 TS_set_foreground = tgetstr ("AF", address);
2489 TS_set_background = tgetstr ("AB", address);
2490 if (!TS_set_foreground)
2492 /* SVr4. */
2493 TS_set_foreground = tgetstr ("Sf", address);
2494 TS_set_background = tgetstr ("Sb", address);
2497 TN_max_colors = tgetnum ("Co");
2498 TN_max_pairs = tgetnum ("pa");
2500 TN_no_color_video = tgetnum ("NC");
2501 if (TN_no_color_video == -1)
2502 TN_no_color_video = 0;
2505 tty_default_color_capabilities (1);
2507 MagicWrap = tgetflag ("xn");
2508 /* Since we make MagicWrap terminals look like AutoWrap, we need to have
2509 the former flag imply the latter. */
2510 AutoWrap = MagicWrap || tgetflag ("am");
2511 memory_below_frame = tgetflag ("db");
2512 TF_hazeltine = tgetflag ("hz");
2513 must_write_spaces = tgetflag ("in");
2514 meta_key = tgetflag ("km") || tgetflag ("MT");
2515 TF_insmode_motion = tgetflag ("mi");
2516 TF_standout_motion = tgetflag ("ms");
2517 TF_underscore = tgetflag ("ul");
2518 TF_teleray = tgetflag ("xt");
2520 term_get_fkeys (address);
2522 /* Get frame size from system, or else from termcap. */
2524 int height, width;
2525 get_frame_size (&width, &height);
2526 FRAME_COLS (sf) = width;
2527 FRAME_LINES (sf) = height;
2530 if (FRAME_COLS (sf) <= 0)
2531 SET_FRAME_COLS (sf, tgetnum ("co"));
2532 else
2533 /* Keep width and external_width consistent */
2534 SET_FRAME_COLS (sf, FRAME_COLS (sf));
2535 if (FRAME_LINES (sf) <= 0)
2536 FRAME_LINES (sf) = tgetnum ("li");
2538 if (FRAME_LINES (sf) < 3 || FRAME_COLS (sf) < 3)
2539 fatal ("Screen size %dx%d is too small",
2540 FRAME_LINES (sf), FRAME_COLS (sf));
2542 min_padding_speed = tgetnum ("pb");
2543 TabWidth = tgetnum ("tw");
2545 #ifdef VMS
2546 /* These capabilities commonly use ^J.
2547 I don't know why, but sending them on VMS does not work;
2548 it causes following spaces to be lost, sometimes.
2549 For now, the simplest fix is to avoid using these capabilities ever. */
2550 if (Down && Down[0] == '\n')
2551 Down = 0;
2552 #endif /* VMS */
2554 if (!TS_bell)
2555 TS_bell = "\07";
2557 if (!TS_fwd_scroll)
2558 TS_fwd_scroll = Down;
2560 PC = TS_pad_char ? *TS_pad_char : 0;
2562 if (TabWidth < 0)
2563 TabWidth = 8;
2565 /* Turned off since /etc/termcap seems to have :ta= for most terminals
2566 and newer termcap doc does not seem to say there is a default.
2567 if (!Wcm.cm_tab)
2568 Wcm.cm_tab = "\t";
2571 /* We don't support standout modes that use `magic cookies', so
2572 turn off any that do. */
2573 if (TS_standout_mode && tgetnum ("sg") >= 0)
2575 TS_standout_mode = 0;
2576 TS_end_standout_mode = 0;
2578 if (TS_enter_underline_mode && tgetnum ("ug") >= 0)
2580 TS_enter_underline_mode = 0;
2581 TS_exit_underline_mode = 0;
2584 /* If there's no standout mode, try to use underlining instead. */
2585 if (TS_standout_mode == 0)
2587 TS_standout_mode = TS_enter_underline_mode;
2588 TS_end_standout_mode = TS_exit_underline_mode;
2591 /* If no `se' string, try using a `me' string instead.
2592 If that fails, we can't use standout mode at all. */
2593 if (TS_end_standout_mode == 0)
2595 char *s = tgetstr ("me", address);
2596 if (s != 0)
2597 TS_end_standout_mode = s;
2598 else
2599 TS_standout_mode = 0;
2602 if (TF_teleray)
2604 Wcm.cm_tab = 0;
2605 /* We can't support standout mode, because it uses magic cookies. */
2606 TS_standout_mode = 0;
2607 /* But that means we cannot rely on ^M to go to column zero! */
2608 CR = 0;
2609 /* LF can't be trusted either -- can alter hpos */
2610 /* if move at column 0 thru a line with TS_standout_mode */
2611 Down = 0;
2614 /* Special handling for certain terminal types known to need it */
2616 if (!strcmp (terminal_type, "supdup"))
2618 memory_below_frame = 1;
2619 Wcm.cm_losewrap = 1;
2621 if (!strncmp (terminal_type, "c10", 3)
2622 || !strcmp (terminal_type, "perq"))
2624 /* Supply a makeshift :wi string.
2625 This string is not valid in general since it works only
2626 for windows starting at the upper left corner;
2627 but that is all Emacs uses.
2629 This string works only if the frame is using
2630 the top of the video memory, because addressing is memory-relative.
2631 So first check the :ti string to see if that is true.
2633 It would be simpler if the :wi string could go in the termcap
2634 entry, but it can't because it is not fully valid.
2635 If it were in the termcap entry, it would confuse other programs. */
2636 if (!TS_set_window)
2638 p = TS_termcap_modes;
2639 while (*p && strcmp (p, "\033v "))
2640 p++;
2641 if (*p)
2642 TS_set_window = "\033v%C %C %C %C ";
2644 /* Termcap entry often fails to have :in: flag */
2645 must_write_spaces = 1;
2646 /* :ti string typically fails to have \E^G! in it */
2647 /* This limits scope of insert-char to one line. */
2648 strcpy (area, TS_termcap_modes);
2649 strcat (area, "\033\007!");
2650 TS_termcap_modes = area;
2651 area += strlen (area) + 1;
2652 p = AbsPosition;
2653 /* Change all %+ parameters to %C, to handle
2654 values above 96 correctly for the C100. */
2655 while (*p)
2657 if (p[0] == '%' && p[1] == '+')
2658 p[1] = 'C';
2659 p++;
2663 FrameRows = FRAME_LINES (sf);
2664 FrameCols = FRAME_COLS (sf);
2665 specified_window = FRAME_LINES (sf);
2667 if (Wcm_init () == -1) /* can't do cursor motion */
2668 #ifdef VMS
2669 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2670 It lacks the ability to position the cursor.\n\
2671 If that is not the actual type of terminal you have, use either the\n\
2672 DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
2673 or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.",
2674 terminal_type);
2675 #else /* not VMS */
2676 # ifdef TERMINFO
2677 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2678 It lacks the ability to position the cursor.\n\
2679 If that is not the actual type of terminal you have,\n\
2680 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2681 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2682 to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.",
2683 terminal_type);
2684 # else /* TERMCAP */
2685 fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
2686 It lacks the ability to position the cursor.\n\
2687 If that is not the actual type of terminal you have,\n\
2688 use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
2689 `setenv TERM ...') to specify the correct type. It may be necessary\n\
2690 to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
2691 terminal_type);
2692 # endif /* TERMINFO */
2693 #endif /*VMS */
2694 if (FRAME_LINES (sf) <= 0
2695 || FRAME_COLS (sf) <= 0)
2696 fatal ("The frame size has not been specified");
2698 delete_in_insert_mode
2699 = TS_delete_mode && TS_insert_mode
2700 && !strcmp (TS_delete_mode, TS_insert_mode);
2702 se_is_so = (TS_standout_mode
2703 && TS_end_standout_mode
2704 && !strcmp (TS_standout_mode, TS_end_standout_mode));
2706 UseTabs = tabs_safe_p () && TabWidth == 8;
2708 scroll_region_ok
2709 = (Wcm.cm_abs
2710 && (TS_set_window || TS_set_scroll_region || TS_set_scroll_region_1));
2712 line_ins_del_ok = (((TS_ins_line || TS_ins_multi_lines)
2713 && (TS_del_line || TS_del_multi_lines))
2714 || (scroll_region_ok && TS_fwd_scroll && TS_rev_scroll));
2716 char_ins_del_ok = ((TS_ins_char || TS_insert_mode
2717 || TS_pad_inserted_char || TS_ins_multi_chars)
2718 && (TS_del_char || TS_del_multi_chars));
2720 fast_clear_end_of_line = TS_clr_line != 0;
2722 init_baud_rate ();
2723 if (read_socket_hook) /* Baudrate is somewhat */
2724 /* meaningless in this case */
2725 baud_rate = 9600;
2727 FRAME_CAN_HAVE_SCROLL_BARS (sf) = 0;
2728 FRAME_VERTICAL_SCROLL_BAR_TYPE (sf) = vertical_scroll_bar_none;
2729 #endif /* WINDOWSNT */
2731 xfree (buffer);
2734 /* VARARGS 1 */
2735 void
2736 fatal (str, arg1, arg2)
2737 char *str, *arg1, *arg2;
2739 fprintf (stderr, "emacs: ");
2740 fprintf (stderr, str, arg1, arg2);
2741 fprintf (stderr, "\n");
2742 fflush (stderr);
2743 exit (1);
2746 DEFUN ("tty-no-underline", Ftty_no_underline, Stty_no_underline, 0, 0, 0,
2747 doc: /* Declare that this terminal does not handle underlining.
2748 This is used to override the terminfo data, for certain terminals that
2749 do not really do underlining, but say that they do. */)
2752 TS_enter_underline_mode = 0;
2753 return Qnil;
2756 void
2757 syms_of_term ()
2759 DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
2760 doc: /* Non-nil means the system uses terminfo rather than termcap.
2761 This variable can be used by terminal emulator packages. */);
2762 #ifdef TERMINFO
2763 system_uses_terminfo = 1;
2764 #else
2765 system_uses_terminfo = 0;
2766 #endif
2768 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
2769 doc: /* Non-nil means call this function to ring the bell.
2770 The function should accept no arguments. */);
2771 Vring_bell_function = Qnil;
2773 DEFVAR_BOOL ("visible-cursor", &visible_cursor,
2774 doc: /* Non-nil means to make the cursor very visible.
2775 This only has an effect when running in a text terminal.
2776 What means \"very visible\" is up to your terminal. It may make the cursor
2777 bigger, or it may make it blink, or it may do nothing at all. */);
2778 visible_cursor = 1;
2780 defsubr (&Stty_display_color_p);
2781 defsubr (&Stty_display_color_cells);
2782 defsubr (&Stty_no_underline);
2784 fullscreen_hook = NULL;
2787 /* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
2788 (do not change this comment) */