mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
term.c
4552 lines (3902 loc) · 128 KB
/
term.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Terminal control module for terminals described by TERMCAP
Copyright (C) 1985-1987, 1993-1995, 1998, 2000-2017 Free Software
Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
/* New redisplay, TTY faces by Gerd Moellmann <gerd@gnu.org>. */
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/time.h>
#include <unistd.h>
#include "lisp.h"
#include "termchar.h"
#include "tparam.h"
#include "character.h"
#include "buffer.h"
#include "charset.h"
#include "coding.h"
#include "composite.h"
#include "keyboard.h"
#include "frame.h"
#include "disptab.h"
#include "termhooks.h"
#include "dispextern.h"
#include "window.h"
#include "keymap.h"
#include "blockinput.h"
#include "syssignal.h"
#include "sysstdio.h"
#ifdef MSDOS
#include "msdos.h"
static int been_here = -1;
#endif
#ifdef USE_X_TOOLKIT
#include "../lwlib/lwlib.h"
#endif
#include "cm.h"
#include "menu.h"
/* The name of the default console device. */
#ifdef WINDOWSNT
#include "w32term.h"
#endif
static void tty_set_scroll_region (struct frame *f, int start, int stop);
static void turn_on_face (struct frame *, int face_id);
static void turn_off_face (struct frame *, int face_id);
static void tty_turn_off_highlight (struct tty_display_info *);
static void tty_show_cursor (struct tty_display_info *);
static void tty_hide_cursor (struct tty_display_info *);
static void tty_background_highlight (struct tty_display_info *tty);
static void clear_tty_hooks (struct terminal *terminal);
static void set_tty_hooks (struct terminal *terminal);
static void dissociate_if_controlling_tty (int fd);
static void delete_tty (struct terminal *);
static _Noreturn void maybe_fatal (bool, struct terminal *,
const char *, const char *, ...)
ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
static _Noreturn void vfatal (const char *str, va_list ap)
ATTRIBUTE_FORMAT_PRINTF (1, 0);
#define OUTPUT(tty, a) \
emacs_tputs ((tty), a, \
FRAME_TOTAL_LINES (XFRAME (selected_frame)) - curY (tty), \
cmputc)
#define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc)
#define OUTPUTL(tty, a, lines) emacs_tputs ((tty), a, lines, cmputc)
#define OUTPUT_IF(tty, a) \
do { \
if (a) \
OUTPUT (tty, a); \
} while (0)
#define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
/* Display space properties. */
/* Chain of all tty device parameters. */
struct tty_display_info *tty_list;
/* Meaning of bits in no_color_video. Each bit set means that the
corresponding attribute cannot be combined with colors. */
enum no_color_bit
{
NC_STANDOUT = 1 << 0,
NC_UNDERLINE = 1 << 1,
NC_REVERSE = 1 << 2,
NC_ITALIC = 1 << 3,
NC_DIM = 1 << 4,
NC_BOLD = 1 << 5,
NC_INVIS = 1 << 6,
NC_PROTECT = 1 << 7
};
/* internal state */
/* The largest frame width in any call to calculate_costs. */
static int max_frame_cols;
#ifdef HAVE_GPM
#include <sys/fcntl.h>
/* The device for which we have enabled gpm support (or NULL). */
struct tty_display_info *gpm_tty = NULL;
/* Last recorded mouse coordinates. */
static int last_mouse_x, last_mouse_y;
#endif /* HAVE_GPM */
/* Ring the bell on a tty. */
static void
tty_ring_bell (struct frame *f)
{
struct tty_display_info *tty = FRAME_TTY (f);
if (tty->output)
{
OUTPUT (tty, (tty->TS_visible_bell && visible_bell
? tty->TS_visible_bell
: tty->TS_bell));
fflush_unlocked (tty->output);
}
}
/* Set up termcap modes for Emacs. */
static void
tty_send_additional_strings (struct terminal *terminal, Lisp_Object sym)
{
Lisp_Object lisp_terminal;
Lisp_Object extra_codes;
struct tty_display_info *tty = terminal->display_info.tty;
XSETTERMINAL (lisp_terminal, terminal);
for (extra_codes = Fterminal_parameter (lisp_terminal, sym);
CONSP (extra_codes);
extra_codes = XCDR (extra_codes))
{
Lisp_Object string = XCAR (extra_codes);
if (STRINGP (string))
{
fwrite_unlocked (SDATA (string), 1, SBYTES (string), tty->output);
if (tty->termscript)
fwrite_unlocked (SDATA (string), 1, SBYTES (string),
tty->termscript);
}
}
}
static void
tty_set_terminal_modes (struct terminal *terminal)
{
struct tty_display_info *tty = terminal->display_info.tty;
if (tty->output)
{
if (tty->TS_termcap_modes)
OUTPUT (tty, tty->TS_termcap_modes);
else
{
/* Output enough newlines to scroll all the old screen contents
off the screen, so it won't be overwritten and lost. */
int i;
current_tty = tty;
for (i = 0; i < FRAME_TOTAL_LINES (XFRAME (selected_frame)); i++)
cmputc ('\n');
}
OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
OUTPUT_IF (tty, tty->TS_keypad_mode);
losecursor (tty);
tty_send_additional_strings (terminal, Qtty_mode_set_strings);
fflush_unlocked (tty->output);
}
}
/* Reset termcap modes before exiting Emacs. */
static void
tty_reset_terminal_modes (struct terminal *terminal)
{
struct tty_display_info *tty = terminal->display_info.tty;
if (tty->output)
{
tty_send_additional_strings (terminal, Qtty_mode_reset_strings);
tty_turn_off_highlight (tty);
tty_turn_off_insert (tty);
OUTPUT_IF (tty, tty->TS_end_keypad_mode);
OUTPUT_IF (tty, tty->TS_cursor_normal);
OUTPUT_IF (tty, tty->TS_end_termcap_modes);
OUTPUT_IF (tty, tty->TS_orig_pair);
/* Output raw CR so kernel can track the cursor hpos. */
current_tty = tty;
cmputc ('\r');
fflush_unlocked (tty->output);
}
}
/* Flag the end of a display update on a termcap terminal. */
static void
tty_update_end (struct frame *f)
{
struct tty_display_info *tty = FRAME_TTY (f);
if (!XWINDOW (selected_window)->cursor_off_p)
tty_show_cursor (tty);
tty_turn_off_insert (tty);
tty_background_highlight (tty);
fflush_unlocked (tty->output);
}
/* The implementation of set_terminal_window for termcap frames. */
static void
tty_set_terminal_window (struct frame *f, int size)
{
struct tty_display_info *tty = FRAME_TTY (f);
tty->specified_window = size ? size : FRAME_TOTAL_LINES (f);
if (FRAME_SCROLL_REGION_OK (f))
tty_set_scroll_region (f, 0, tty->specified_window);
}
static void
tty_set_scroll_region (struct frame *f, int start, int stop)
{
char *buf;
struct tty_display_info *tty = FRAME_TTY (f);
if (tty->TS_set_scroll_region)
buf = tparam (tty->TS_set_scroll_region, 0, 0, start, stop - 1, 0, 0);
else if (tty->TS_set_scroll_region_1)
buf = tparam (tty->TS_set_scroll_region_1, 0, 0,
FRAME_TOTAL_LINES (f), start,
FRAME_TOTAL_LINES (f) - stop,
FRAME_TOTAL_LINES (f));
else
buf = tparam (tty->TS_set_window, 0, 0, start, 0, stop, FRAME_COLS (f));
OUTPUT (tty, buf);
xfree (buf);
losecursor (tty);
}
static void
tty_turn_on_insert (struct tty_display_info *tty)
{
if (!tty->insert_mode)
OUTPUT (tty, tty->TS_insert_mode);
tty->insert_mode = 1;
}
void
tty_turn_off_insert (struct tty_display_info *tty)
{
if (tty->insert_mode)
OUTPUT (tty, tty->TS_end_insert_mode);
tty->insert_mode = 0;
}
/* Handle highlighting. */
static void
tty_turn_off_highlight (struct tty_display_info *tty)
{
if (tty->standout_mode)
OUTPUT_IF (tty, tty->TS_end_standout_mode);
tty->standout_mode = 0;
}
static void
tty_turn_on_highlight (struct tty_display_info *tty)
{
if (!tty->standout_mode)
OUTPUT_IF (tty, tty->TS_standout_mode);
tty->standout_mode = 1;
}
static void
tty_toggle_highlight (struct tty_display_info *tty)
{
if (tty->standout_mode)
tty_turn_off_highlight (tty);
else
tty_turn_on_highlight (tty);
}
/* Make cursor invisible. */
static void
tty_hide_cursor (struct tty_display_info *tty)
{
if (tty->cursor_hidden == 0)
{
tty->cursor_hidden = 1;
#ifdef WINDOWSNT
w32con_hide_cursor ();
#else
OUTPUT_IF (tty, tty->TS_cursor_invisible);
#endif
}
}
/* Ensure that cursor is visible. */
static void
tty_show_cursor (struct tty_display_info *tty)
{
if (tty->cursor_hidden)
{
tty->cursor_hidden = 0;
#ifdef WINDOWSNT
w32con_show_cursor ();
#else
OUTPUT_IF (tty, tty->TS_cursor_normal);
if (visible_cursor)
OUTPUT_IF (tty, tty->TS_cursor_visible);
#endif
}
}
/* Set standout mode to the state it should be in for
empty space inside windows. What this is,
depends on the user option inverse-video. */
static void
tty_background_highlight (struct tty_display_info *tty)
{
if (inverse_video)
tty_turn_on_highlight (tty);
else
tty_turn_off_highlight (tty);
}
/* Set standout mode to the mode specified for the text to be output. */
static void
tty_highlight_if_desired (struct tty_display_info *tty)
{
if (inverse_video)
tty_turn_on_highlight (tty);
else
tty_turn_off_highlight (tty);
}
/* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
frame-relative coordinates. */
static void
tty_cursor_to (struct frame *f, int vpos, int hpos)
{
struct tty_display_info *tty = FRAME_TTY (f);
/* Detect the case where we are called from reset_sys_modes
and the costs have never been calculated. Do nothing. */
if (! tty->costs_set)
return;
if (curY (tty) == vpos
&& curX (tty) == hpos)
return;
if (!tty->TF_standout_motion)
tty_background_highlight (tty);
if (!tty->TF_insmode_motion)
tty_turn_off_insert (tty);
cmgoto (tty, vpos, hpos);
}
/* Similar but don't take any account of the wasted characters. */
static void
tty_raw_cursor_to (struct frame *f, int row, int col)
{
struct tty_display_info *tty = FRAME_TTY (f);
if (curY (tty) == row
&& curX (tty) == col)
return;
if (!tty->TF_standout_motion)
tty_background_highlight (tty);
if (!tty->TF_insmode_motion)
tty_turn_off_insert (tty);
cmgoto (tty, row, col);
}
/* Erase operations */
/* Clear from cursor to end of frame on a termcap device. */
static void
tty_clear_to_end (struct frame *f)
{
register int i;
struct tty_display_info *tty = FRAME_TTY (f);
if (tty->TS_clr_to_bottom)
{
tty_background_highlight (tty);
OUTPUT (tty, tty->TS_clr_to_bottom);
}
else
{
for (i = curY (tty); i < FRAME_TOTAL_LINES (f); i++)
{
cursor_to (f, i, 0);
clear_end_of_line (f, FRAME_COLS (f));
}
}
}
/* Clear an entire termcap frame. */
static void
tty_clear_frame (struct frame *f)
{
struct tty_display_info *tty = FRAME_TTY (f);
if (tty->TS_clr_frame)
{
tty_background_highlight (tty);
OUTPUT (tty, tty->TS_clr_frame);
cmat (tty, 0, 0);
}
else
{
cursor_to (f, 0, 0);
clear_to_end (f);
}
}
/* An implementation of clear_end_of_line for termcap frames.
Note that the cursor may be moved, on terminals lacking a `ce' string. */
static void
tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
{
register int i;
struct tty_display_info *tty = FRAME_TTY (f);
/* Detect the case where we are called from reset_sys_modes
and the costs have never been calculated. Do nothing. */
if (! tty->costs_set)
return;
if (curX (tty) >= first_unused_hpos)
return;
tty_background_highlight (tty);
if (tty->TS_clr_line)
{
OUTPUT1 (tty, tty->TS_clr_line);
}
else
{ /* have to do it the hard way */
tty_turn_off_insert (tty);
/* Do not write in last row last col with Auto-wrap on. */
if (AutoWrap (tty)
&& curY (tty) == FrameRows (tty) - 1
&& first_unused_hpos == FrameCols (tty))
first_unused_hpos--;
for (i = curX (tty); i < first_unused_hpos; i++)
{
if (tty->termscript)
fputc_unlocked (' ', tty->termscript);
fputc_unlocked (' ', tty->output);
}
cmplus (tty, first_unused_hpos - curX (tty));
}
}
/* Buffers to store the source and result of code conversion for terminal. */
static unsigned char *encode_terminal_src;
static unsigned char *encode_terminal_dst;
/* Allocated sizes of the above buffers. */
static ptrdiff_t encode_terminal_src_size;
static ptrdiff_t encode_terminal_dst_size;
/* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
Set CODING->produced to the byte-length of the resulting byte
sequence, and return a pointer to that byte sequence. */
unsigned char *
encode_terminal_code (struct glyph *src, int src_len,
struct coding_system *coding)
{
struct glyph *src_end = src + src_len;
unsigned char *buf;
ptrdiff_t nchars, nbytes, required;
ptrdiff_t tlen = GLYPH_TABLE_LENGTH;
register Lisp_Object *tbase = GLYPH_TABLE_BASE;
Lisp_Object charset_list;
/* Allocate sufficient size of buffer to store all characters in
multibyte-form. But, it may be enlarged on demand if
Vglyph_table contains a string or a composite glyph is
encountered. */
if (INT_MULTIPLY_WRAPV (src_len, MAX_MULTIBYTE_LENGTH, &required))
memory_full (SIZE_MAX);
if (encode_terminal_src_size < required)
encode_terminal_src = xpalloc (encode_terminal_src,
&encode_terminal_src_size,
required - encode_terminal_src_size,
-1, sizeof *encode_terminal_src);
charset_list = coding_charset_list (coding);
buf = encode_terminal_src;
nchars = 0;
while (src < src_end)
{
if (src->type == COMPOSITE_GLYPH)
{
struct composition *cmp UNINIT;
Lisp_Object gstring UNINIT;
int i;
nbytes = buf - encode_terminal_src;
if (src->u.cmp.automatic)
{
gstring = composition_gstring_from_id (src->u.cmp.id);
required = src->slice.cmp.to - src->slice.cmp.from + 1;
}
else
{
cmp = composition_table[src->u.cmp.id];
required = cmp->glyph_len;
required *= MAX_MULTIBYTE_LENGTH;
}
if (encode_terminal_src_size - nbytes < required)
{
encode_terminal_src =
xpalloc (encode_terminal_src, &encode_terminal_src_size,
required - (encode_terminal_src_size - nbytes),
-1, 1);
buf = encode_terminal_src + nbytes;
}
if (src->u.cmp.automatic)
for (i = src->slice.cmp.from; i <= src->slice.cmp.to; i++)
{
Lisp_Object g = LGSTRING_GLYPH (gstring, i);
int c = LGLYPH_CHAR (g);
if (! char_charset (c, charset_list, NULL))
c = '?';
buf += CHAR_STRING (c, buf);
nchars++;
}
else
for (i = 0; i < cmp->glyph_len; i++)
{
int c = COMPOSITION_GLYPH (cmp, i);
/* TAB in a composition means display glyphs with
padding space on the left or right. */
if (c == '\t')
continue;
if (char_charset (c, charset_list, NULL))
{
if (CHARACTER_WIDTH (c) == 0
&& i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
/* Should be left-padded */
{
buf += CHAR_STRING (' ', buf);
nchars++;
}
}
else
c = '?';
buf += CHAR_STRING (c, buf);
nchars++;
}
}
/* We must skip glyphs to be padded for a wide character. */
else if (! CHAR_GLYPH_PADDING_P (*src))
{
GLYPH g;
int c UNINIT;
Lisp_Object string;
string = Qnil;
SET_GLYPH_FROM_CHAR_GLYPH (g, src[0]);
if (GLYPH_INVALID_P (g) || GLYPH_SIMPLE_P (tbase, tlen, g))
{
/* This glyph doesn't have an entry in Vglyph_table. */
c = src->u.ch;
}
else
{
/* This glyph has an entry in Vglyph_table,
so process any alias before testing for simpleness. */
GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
if (GLYPH_SIMPLE_P (tbase, tlen, g))
/* We set the multi-byte form of a character in G
(that should be an ASCII character) at WORKBUF. */
c = GLYPH_CHAR (g);
else
/* We have a string in Vglyph_table. */
string = tbase[GLYPH_CHAR (g)];
}
if (NILP (string))
{
nbytes = buf - encode_terminal_src;
if (encode_terminal_src_size - nbytes < MAX_MULTIBYTE_LENGTH)
{
encode_terminal_src =
xpalloc (encode_terminal_src, &encode_terminal_src_size,
MAX_MULTIBYTE_LENGTH, -1, 1);
buf = encode_terminal_src + nbytes;
}
if (CHAR_BYTE8_P (c)
|| char_charset (c, charset_list, NULL))
{
/* Store the multibyte form of C at BUF. */
buf += CHAR_STRING (c, buf);
nchars++;
}
else
{
/* C is not encodable. */
*buf++ = '?';
nchars++;
while (src + 1 < src_end && CHAR_GLYPH_PADDING_P (src[1]))
{
*buf++ = '?';
nchars++;
src++;
}
}
}
else
{
if (! STRING_MULTIBYTE (string))
string = string_to_multibyte (string);
nbytes = buf - encode_terminal_src;
if (encode_terminal_src_size - nbytes < SBYTES (string))
{
encode_terminal_src =
xpalloc (encode_terminal_src, &encode_terminal_src_size,
(SBYTES (string)
- (encode_terminal_src_size - nbytes)),
-1, 1);
buf = encode_terminal_src + nbytes;
}
memcpy (buf, SDATA (string), SBYTES (string));
buf += SBYTES (string);
nchars += SCHARS (string);
}
}
src++;
}
if (nchars == 0)
{
coding->produced = 0;
return NULL;
}
nbytes = buf - encode_terminal_src;
coding->source = encode_terminal_src;
if (encode_terminal_dst_size == 0)
{
encode_terminal_dst = xrealloc (encode_terminal_dst,
encode_terminal_src_size);
encode_terminal_dst_size = encode_terminal_src_size;
}
coding->destination = encode_terminal_dst;
coding->dst_bytes = encode_terminal_dst_size;
encode_coding_object (coding, Qnil, 0, 0, nchars, nbytes, Qnil);
/* coding->destination may have been reallocated. */
encode_terminal_dst = coding->destination;
encode_terminal_dst_size = coding->dst_bytes;
return (encode_terminal_dst);
}
/* An implementation of write_glyphs for termcap frames. */
static void
tty_write_glyphs (struct frame *f, struct glyph *string, int len)
{
unsigned char *conversion_buffer;
struct coding_system *coding;
int n, stringlen;
struct tty_display_info *tty = FRAME_TTY (f);
tty_turn_off_insert (tty);
tty_hide_cursor (tty);
/* Don't dare write in last column of bottom line, if Auto-Wrap,
since that would scroll the whole frame on some terminals. */
if (AutoWrap (tty)
&& curY (tty) + 1 == FRAME_TOTAL_LINES (f)
&& (curX (tty) + len) == FRAME_COLS (f))
len --;
if (len <= 0)
return;
cmplus (tty, len);
/* If terminal_coding does any conversion, use it, otherwise use
safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
because it always return 1 if the member src_multibyte is 1. */
coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
/* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
the tail. */
coding->mode &= ~CODING_MODE_LAST_BLOCK;
for (stringlen = len; stringlen != 0; stringlen -= n)
{
/* Identify a run of glyphs with the same face. */
int face_id = string->face_id;
for (n = 1; n < stringlen; ++n)
if (string[n].face_id != face_id)
break;
/* Turn appearance modes of the face of the run on. */
tty_highlight_if_desired (tty);
turn_on_face (f, face_id);
if (n == stringlen)
/* This is the last run. */
coding->mode |= CODING_MODE_LAST_BLOCK;
conversion_buffer = encode_terminal_code (string, n, coding);
if (coding->produced > 0)
{
block_input ();
fwrite_unlocked (conversion_buffer, 1, coding->produced, tty->output);
clearerr_unlocked (tty->output);
if (tty->termscript)
fwrite_unlocked (conversion_buffer, 1, coding->produced,
tty->termscript);
unblock_input ();
}
string += n;
/* Turn appearance modes off. */
turn_off_face (f, face_id);
tty_turn_off_highlight (tty);
}
cmcheckmagic (tty);
}
#ifdef HAVE_GPM /* Only used by GPM code. */
static void
tty_write_glyphs_with_face (register struct frame *f, register struct glyph *string,
register int len, register int face_id)
{
unsigned char *conversion_buffer;
struct coding_system *coding;
struct tty_display_info *tty = FRAME_TTY (f);
tty_turn_off_insert (tty);
tty_hide_cursor (tty);
/* Don't dare write in last column of bottom line, if Auto-Wrap,
since that would scroll the whole frame on some terminals. */
if (AutoWrap (tty)
&& curY (tty) + 1 == FRAME_TOTAL_LINES (f)
&& (curX (tty) + len) == FRAME_COLS (f))
len --;
if (len <= 0)
return;
cmplus (tty, len);
/* If terminal_coding does any conversion, use it, otherwise use
safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
because it always return 1 if the member src_multibyte is 1. */
coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
/* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
the tail. */
coding->mode &= ~CODING_MODE_LAST_BLOCK;
/* Turn appearance modes of the face. */
tty_highlight_if_desired (tty);
turn_on_face (f, face_id);
coding->mode |= CODING_MODE_LAST_BLOCK;
conversion_buffer = encode_terminal_code (string, len, coding);
if (coding->produced > 0)
{
block_input ();
fwrite_unlocked (conversion_buffer, 1, coding->produced, tty->output);
clearerr_unlocked (tty->output);
if (tty->termscript)
fwrite_unlocked (conversion_buffer, 1, coding->produced,
tty->termscript);
unblock_input ();
}
/* Turn appearance modes off. */
turn_off_face (f, face_id);
tty_turn_off_highlight (tty);
cmcheckmagic (tty);
}
#endif
/* An implementation of insert_glyphs for termcap frames. */
static void
tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
{
char *buf;
struct glyph *glyph = NULL;
unsigned char *conversion_buffer;
unsigned char space[1];
struct coding_system *coding;
struct tty_display_info *tty = FRAME_TTY (f);
if (tty->TS_ins_multi_chars)
{
buf = tparam (tty->TS_ins_multi_chars, 0, 0, len, 0, 0, 0);
OUTPUT1 (tty, buf);
xfree (buf);
if (start)
write_glyphs (f, start, len);
return;
}
tty_turn_on_insert (tty);
cmplus (tty, len);
if (! start)
space[0] = SPACEGLYPH;
/* If terminal_coding does any conversion, use it, otherwise use
safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
because it always return 1 if the member src_multibyte is 1. */
coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
/* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
the tail. */
coding->mode &= ~CODING_MODE_LAST_BLOCK;
while (len-- > 0)
{
OUTPUT1_IF (tty, tty->TS_ins_char);
if (!start)
{
conversion_buffer = space;
coding->produced = 1;
}
else
{
tty_highlight_if_desired (tty);
turn_on_face (f, start->face_id);
glyph = start;
++start;
/* We must open sufficient space for a character which
occupies more than one column. */
while (len && CHAR_GLYPH_PADDING_P (*start))
{
OUTPUT1_IF (tty, tty->TS_ins_char);
start++, len--;
}
if (len <= 0)
/* This is the last glyph. */
coding->mode |= CODING_MODE_LAST_BLOCK;
conversion_buffer = encode_terminal_code (glyph, 1, coding);
}
if (coding->produced > 0)
{
block_input ();
fwrite_unlocked (conversion_buffer, 1, coding->produced, tty->output);
clearerr_unlocked (tty->output);
if (tty->termscript)
fwrite_unlocked (conversion_buffer, 1, coding->produced,
tty->termscript);
unblock_input ();
}
OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
if (start)
{
turn_off_face (f, glyph->face_id);
tty_turn_off_highlight (tty);
}
}
cmcheckmagic (tty);
}
/* An implementation of delete_glyphs for termcap frames. */
static void
tty_delete_glyphs (struct frame *f, int n)
{
char *buf;
register int i;
struct tty_display_info *tty = FRAME_TTY (f);
if (tty->delete_in_insert_mode)
{
tty_turn_on_insert (tty);
}
else
{
tty_turn_off_insert (tty);
OUTPUT_IF (tty, tty->TS_delete_mode);
}
if (tty->TS_del_multi_chars)
{
buf = tparam (tty->TS_del_multi_chars, 0, 0, n, 0, 0, 0);
OUTPUT1 (tty, buf);
xfree (buf);
}
else
for (i = 0; i < n; i++)
OUTPUT1 (tty, tty->TS_del_char);
if (!tty->delete_in_insert_mode)
OUTPUT_IF (tty, tty->TS_end_delete_mode);
}
/* An implementation of ins_del_lines for termcap frames. */
static void
tty_ins_del_lines (struct frame *f, int vpos, int n)
{
struct tty_display_info *tty = FRAME_TTY (f);
const char *multi =
n > 0 ? tty->TS_ins_multi_lines : tty->TS_del_multi_lines;
const char *single = n > 0 ? tty->TS_ins_line : tty->TS_del_line;
const char *scroll = n > 0 ? tty->TS_rev_scroll : tty->TS_fwd_scroll;
int i = eabs (n);
char *buf;
/* If the lines below the insertion are being pushed
into the end of the window, this is the same as clearing;
and we know the lines are already clear, since the matching
deletion has already been done. So can ignore this. */
/* If the lines below the deletion are blank lines coming
out of the end of the window, don't bother,
as there will be a matching inslines later that will flush them. */
if (FRAME_SCROLL_REGION_OK (f)
&& vpos + i >= tty->specified_window)
return;
if (!FRAME_MEMORY_BELOW_FRAME (f)
&& vpos + i >= FRAME_TOTAL_LINES (f))
return;