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