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