forked from macvim-dev/macvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.c
7851 lines (6948 loc) · 187 KB
/
terminal.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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* Terminal window support, see ":help :terminal".
*
* There are three parts:
* 1. Generic code for all systems.
* Uses libvterm for the terminal emulator.
* 2. The MS-Windows implementation.
* Uses winpty.
* 3. The Unix-like implementation.
* Uses pseudo-tty's (pty's).
*
* For each terminal one VTerm is constructed. This uses libvterm. A copy of
* this library is in the libvterm directory.
*
* When a terminal window is opened, a job is started that will be connected to
* the terminal emulator.
*
* If the terminal window has keyboard focus, typed keys are converted to the
* terminal encoding and writing to the job over a channel.
*
* If the job produces output, it is written to the terminal emulator. The
* terminal emulator invokes callbacks when its screen content changes. The
* line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
* while, if the terminal window is visible, the screen contents is drawn.
*
* When the job ends the text is put in a buffer. Redrawing then happens from
* that buffer, attributes come from the scrollback buffer tl_scrollback.
* When the buffer is changed it is turned into a normal buffer, the attributes
* in tl_scrollback are no longer used.
*/
#include "vim.h"
#if defined(FEAT_TERMINAL) || defined(PROTO)
#ifndef MIN
# define MIN(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef MAX
# define MAX(x,y) ((x) > (y) ? (x) : (y))
#endif
#include "libvterm/include/vterm.h"
// This is VTermScreenCell without the characters, thus much smaller.
typedef struct {
VTermScreenCellAttrs attrs;
char width;
VTermColor fg;
VTermColor bg;
} cellattr_T;
typedef struct sb_line_S {
int sb_cols; // can differ per line
cellattr_T *sb_cells; // allocated
cellattr_T sb_fill_attr; // for short line
char_u *sb_text; // for tl_scrollback_postponed
} sb_line_T;
#ifdef MSWIN
# ifndef HPCON
# define HPCON VOID*
# endif
# ifndef EXTENDED_STARTUPINFO_PRESENT
# define EXTENDED_STARTUPINFO_PRESENT 0x00080000
# endif
# ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
# define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016
# endif
typedef struct _DYN_STARTUPINFOEXW
{
STARTUPINFOW StartupInfo;
LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
} DYN_STARTUPINFOEXW, *PDYN_STARTUPINFOEXW;
#endif
// typedef term_T in structs.h
struct terminal_S {
term_T *tl_next;
VTerm *tl_vterm;
job_T *tl_job;
buf_T *tl_buffer;
#if defined(FEAT_GUI)
int tl_system; // when non-zero used for :!cmd output
int tl_toprow; // row with first line of system terminal
#endif
// Set when setting the size of a vterm, reset after redrawing.
int tl_vterm_size_changed;
int tl_normal_mode; // TRUE: Terminal-Normal mode
int tl_channel_closing;
int tl_channel_closed;
int tl_channel_recently_closed; // still need to handle tl_finish
int tl_finish;
#define TL_FINISH_UNSET NUL
#define TL_FINISH_CLOSE 'c' // ++close or :terminal without argument
#define TL_FINISH_NOCLOSE 'n' // ++noclose
#define TL_FINISH_OPEN 'o' // ++open
char_u *tl_opencmd;
char_u *tl_eof_chars;
char_u *tl_api; // prefix for terminal API function
char_u *tl_arg0_cmd; // To format the status bar
#ifdef MSWIN
void *tl_winpty_config;
void *tl_winpty;
HPCON tl_conpty;
DYN_STARTUPINFOEXW tl_siex; // Structure that always needs to be hold
FILE *tl_out_fd;
#endif
#if defined(FEAT_SESSION)
char_u *tl_command;
#endif
char_u *tl_kill;
// last known vterm size
int tl_rows;
int tl_cols;
char_u *tl_title; // NULL or allocated
char_u *tl_status_text; // NULL or allocated
// Range of screen rows to update. Zero based.
int tl_dirty_row_start; // MAX_ROW if nothing dirty
int tl_dirty_row_end; // row below last one to update
int tl_dirty_snapshot; // text updated after making snapshot
#ifdef FEAT_TIMERS
int tl_timer_set;
proftime_T tl_timer_due;
#endif
int tl_postponed_scroll; // to be scrolled up
garray_T tl_scrollback;
int tl_scrollback_scrolled;
garray_T tl_scrollback_postponed;
char_u *tl_highlight_name; // replaces "Terminal"; allocated
cellattr_T tl_default_color;
linenr_T tl_top_diff_rows; // rows of top diff file or zero
linenr_T tl_bot_diff_rows; // rows of bottom diff file
VTermPos tl_cursor_pos;
int tl_cursor_visible;
int tl_cursor_blink;
int tl_cursor_shape; // 1: block, 2: underline, 3: bar
char_u *tl_cursor_color; // NULL or allocated
long_u *tl_palette; // array of 16 colors specified by term_start, can
// be NULL
int tl_using_altscreen;
garray_T tl_osc_buf; // incomplete OSC string
};
#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
#define TMODE_LOOP 2 // CTRL-W N used
/*
* List of all active terminals.
*/
static term_T *first_term = NULL;
// Terminal active in terminal_loop().
static term_T *in_terminal_loop = NULL;
#ifdef MSWIN
static BOOL has_winpty = FALSE;
static BOOL has_conpty = FALSE;
#endif
#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
#define KEY_BUF_LEN 200
#define FOR_ALL_TERMS(term) \
for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
/*
* Functions with separate implementation for MS-Windows and Unix-like systems.
*/
static int term_and_job_init(term_T *term, typval_T *argvar, char **argv, jobopt_T *opt, jobopt_T *orig_opt);
static int create_pty_only(term_T *term, jobopt_T *opt);
static void term_report_winsize(term_T *term, int rows, int cols);
static void term_free_vterm(term_T *term);
#ifdef FEAT_GUI
static void update_system_term(term_T *term);
#endif
static void handle_postponed_scrollback(term_T *term);
// The character that we know (or assume) that the terminal expects for the
// backspace key.
static int term_backspace_char = BS;
// Store the last set and the desired cursor properties, so that we only update
// them when needed. Doing it unnecessary may result in flicker.
static char_u *last_set_cursor_color = NULL;
static char_u *desired_cursor_color = NULL;
static int last_set_cursor_shape = -1;
static int desired_cursor_shape = -1;
static int last_set_cursor_blink = -1;
static int desired_cursor_blink = -1;
///////////////////////////////////////
// 1. Generic code for all systems.
static int
cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
{
if (lhs_color != NULL && rhs_color != NULL)
return STRCMP(lhs_color, rhs_color) == 0;
return lhs_color == NULL && rhs_color == NULL;
}
static void
cursor_color_copy(char_u **to_color, char_u *from_color)
{
// Avoid a free & alloc if the value is already right.
if (cursor_color_equal(*to_color, from_color))
return;
vim_free(*to_color);
*to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
}
static char_u *
cursor_color_get(char_u *color)
{
return (color == NULL) ? (char_u *)"" : color;
}
/*
* Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
* current window.
* Sets "rows" and/or "cols" to zero when it should follow the window size.
* Return TRUE if the size is the minimum size: "24*80".
*/
static int
parse_termwinsize(win_T *wp, int *rows, int *cols)
{
int minsize = FALSE;
*rows = 0;
*cols = 0;
if (*wp->w_p_tws == NUL)
return FALSE;
char_u *p = vim_strchr(wp->w_p_tws, 'x');
// Syntax of value was already checked when it's set.
if (p == NULL)
{
minsize = TRUE;
p = vim_strchr(wp->w_p_tws, '*');
}
*rows = atoi((char *)wp->w_p_tws);
*cols = atoi((char *)p + 1);
if (*rows > VTERM_MAX_ROWS)
*rows = VTERM_MAX_ROWS;
if (*cols > VTERM_MAX_COLS)
*cols = VTERM_MAX_COLS;
return minsize;
}
/*
* Determine the terminal size from 'termwinsize' and the current window.
*/
static void
set_term_and_win_size(term_T *term, jobopt_T *opt)
{
int rows, cols;
int minsize;
#ifdef FEAT_GUI
if (term->tl_system)
{
// Use the whole screen for the system command. However, it will start
// at the command line and scroll up as needed, using tl_toprow.
term->tl_rows = Rows;
term->tl_cols = Columns;
return;
}
#endif
term->tl_rows = curwin->w_height;
term->tl_cols = curwin->w_width;
minsize = parse_termwinsize(curwin, &rows, &cols);
if (minsize)
{
if (term->tl_rows < rows)
term->tl_rows = rows;
if (term->tl_cols < cols)
term->tl_cols = cols;
}
if ((opt->jo_set2 & JO2_TERM_ROWS))
term->tl_rows = opt->jo_term_rows;
else if (rows != 0)
term->tl_rows = rows;
if ((opt->jo_set2 & JO2_TERM_COLS))
term->tl_cols = opt->jo_term_cols;
else if (cols != 0)
term->tl_cols = cols;
if (!opt->jo_hidden)
{
if (term->tl_rows != curwin->w_height)
win_setheight_win(term->tl_rows, curwin);
if (term->tl_cols != curwin->w_width)
win_setwidth_win(term->tl_cols, curwin);
// Set 'winsize' now to avoid a resize at the next redraw.
if (!minsize && *curwin->w_p_tws != NUL)
{
char_u buf[100];
vim_snprintf((char *)buf, 100, "%dx%d",
term->tl_rows, term->tl_cols);
set_option_value_give_err((char_u *)"termwinsize",
0L, buf, OPT_LOCAL);
}
}
}
/*
* Initialize job options for a terminal job.
* Caller may overrule some of them.
*/
void
init_job_options(jobopt_T *opt)
{
clear_job_options(opt);
opt->jo_mode = CH_MODE_RAW;
opt->jo_out_mode = CH_MODE_RAW;
opt->jo_err_mode = CH_MODE_RAW;
opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
}
/*
* Set job options mandatory for a terminal job.
*/
static void
setup_job_options(jobopt_T *opt, int rows, int cols)
{
#ifndef MSWIN
// Win32: Redirecting the job output won't work, thus always connect stdout
// here.
if (!(opt->jo_set & JO_OUT_IO))
#endif
{
// Connect stdout to the terminal.
opt->jo_io[PART_OUT] = JIO_BUFFER;
opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
opt->jo_modifiable[PART_OUT] = 0;
opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
}
#ifndef MSWIN
// Win32: Redirecting the job output won't work, thus always connect stderr
// here.
if (!(opt->jo_set & JO_ERR_IO))
#endif
{
// Connect stderr to the terminal.
opt->jo_io[PART_ERR] = JIO_BUFFER;
opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
opt->jo_modifiable[PART_ERR] = 0;
opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
}
opt->jo_pty = TRUE;
if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
opt->jo_term_rows = rows;
if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
opt->jo_term_cols = cols;
}
/*
* Flush messages on channels.
*/
static void
term_flush_messages(void)
{
mch_check_messages();
parse_queued_messages();
}
/*
* Close a terminal buffer (and its window). Used when creating the terminal
* fails.
*/
static void
term_close_buffer(buf_T *buf, buf_T *old_curbuf)
{
free_terminal(buf);
if (old_curbuf != NULL)
{
--curbuf->b_nwindows;
curbuf = old_curbuf;
curwin->w_buffer = curbuf;
++curbuf->b_nwindows;
}
CHECK_CURBUF;
// Wiping out the buffer will also close the window and call
// free_terminal().
do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
}
/*
* Start a terminal window and return its buffer.
* Use either "argvar" or "argv", the other must be NULL.
* When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
* the window.
* Returns NULL when failed.
*/
buf_T *
term_start(
typval_T *argvar,
char **argv,
jobopt_T *opt,
int flags)
{
exarg_T split_ea;
win_T *old_curwin = curwin;
term_T *term;
buf_T *old_curbuf = NULL;
int res;
buf_T *newbuf;
int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
jobopt_T orig_opt; // only partly filled
pos_T save_cursor;
if (check_restricted() || check_secure())
return NULL;
if (cmdwin_type != 0)
{
emsg(_(e_cannot_open_terminal_from_command_line_window));
return NULL;
}
if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
== (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
|| (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
|| (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
|| (argvar != NULL
&& argvar->v_type == VAR_LIST
&& argvar->vval.v_list != NULL
&& argvar->vval.v_list->lv_first == &range_list_item))
{
emsg(_(e_invalid_argument));
return NULL;
}
term = ALLOC_CLEAR_ONE(term_T);
if (term == NULL)
return NULL;
term->tl_dirty_row_end = MAX_ROW;
term->tl_cursor_visible = TRUE;
term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
term->tl_finish = opt->jo_term_finish;
#ifdef FEAT_GUI
term->tl_system = (flags & TERM_START_SYSTEM);
#endif
ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
ga_init2(&term->tl_osc_buf, sizeof(char), 300);
setpcmark();
CLEAR_FIELD(split_ea);
if (opt->jo_curwin)
{
// Create a new buffer in the current window.
if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
{
no_write_message();
vim_free(term);
return NULL;
}
if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
(buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
+ ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
curwin) == FAIL)
{
vim_free(term);
return NULL;
}
}
else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
{
buf_T *buf;
// Create a new buffer without a window. Make it the current buffer for
// a moment to be able to do the initializations.
buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
BLN_NEW | BLN_LISTED);
if (buf == NULL || ml_open(buf) == FAIL)
{
vim_free(term);
return NULL;
}
old_curbuf = curbuf;
--curbuf->b_nwindows;
curbuf = buf;
save_cursor = curwin->w_cursor;
curwin->w_buffer = buf;
++curbuf->b_nwindows;
}
else
{
// Open a new window or tab.
split_ea.cmdidx = CMD_new;
split_ea.cmd = (char_u *)"new";
split_ea.arg = (char_u *)"";
if (opt->jo_term_rows > 0 && !vertical)
{
split_ea.line2 = opt->jo_term_rows;
split_ea.addr_count = 1;
}
if (opt->jo_term_cols > 0 && vertical)
{
split_ea.line2 = opt->jo_term_cols;
split_ea.addr_count = 1;
}
int cmod_split_modified = FALSE;
if (vertical)
{
if (!(cmdmod.cmod_split & WSP_VERT))
cmod_split_modified = TRUE;
cmdmod.cmod_split |= WSP_VERT;
}
ex_splitview(&split_ea);
if (cmod_split_modified)
cmdmod.cmod_split &= ~WSP_VERT;
if (curwin == old_curwin)
{
// split failed
vim_free(term);
return NULL;
}
}
term->tl_buffer = curbuf;
curbuf->b_term = term;
if (!opt->jo_hidden)
{
// Only one size was taken care of with :new, do the other one. With
// "curwin" both need to be done.
if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
win_setheight(opt->jo_term_rows);
if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
win_setwidth(opt->jo_term_cols);
}
// Link the new terminal in the list of active terminals.
term->tl_next = first_term;
first_term = term;
apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
if (opt->jo_term_name != NULL)
{
vim_free(curbuf->b_ffname);
curbuf->b_ffname = vim_strsave(opt->jo_term_name);
}
else if (argv != NULL)
{
vim_free(curbuf->b_ffname);
curbuf->b_ffname = vim_strsave((char_u *)"!system");
}
else
{
int i;
size_t len;
char_u *cmd, *p;
if (argvar->v_type == VAR_STRING)
{
cmd = argvar->vval.v_string;
if (cmd == NULL)
cmd = (char_u *)"";
else if (STRCMP(cmd, "NONE") == 0)
cmd = (char_u *)"pty";
}
else if (argvar->v_type != VAR_LIST
|| argvar->vval.v_list == NULL
|| argvar->vval.v_list->lv_len == 0
|| (cmd = tv_get_string_chk(
&argvar->vval.v_list->lv_first->li_tv)) == NULL)
cmd = (char_u*)"";
len = STRLEN(cmd) + 10;
p = alloc(len);
for (i = 0; p != NULL; ++i)
{
// Prepend a ! to the command name to avoid the buffer name equals
// the executable, otherwise ":w!" would overwrite it.
if (i == 0)
vim_snprintf((char *)p, len, "!%s", cmd);
else
vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
if (buflist_findname(p) == NULL)
{
vim_free(curbuf->b_ffname);
curbuf->b_ffname = p;
break;
}
}
}
vim_free(curbuf->b_sfname);
curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
curbuf->b_fname = curbuf->b_ffname;
apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
if (opt->jo_term_opencmd != NULL)
term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
if (opt->jo_eof_chars != NULL)
term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
set_string_option_direct((char_u *)"buftype", -1,
(char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
// Avoid that 'buftype' is reset when this buffer is entered.
curbuf->b_p_initialized = TRUE;
// Mark the buffer as not modifiable. It can only be made modifiable after
// the job finished.
curbuf->b_p_ma = FALSE;
set_term_and_win_size(term, opt);
#ifdef MSWIN
mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
#endif
setup_job_options(opt, term->tl_rows, term->tl_cols);
if (flags & TERM_START_NOJOB)
return curbuf;
#if defined(FEAT_SESSION)
// Remember the command for the session file.
if (opt->jo_term_norestore || argv != NULL)
term->tl_command = vim_strsave((char_u *)"NONE");
else if (argvar->v_type == VAR_STRING)
{
char_u *cmd = argvar->vval.v_string;
if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
term->tl_command = vim_strsave(cmd);
}
else if (argvar->v_type == VAR_LIST
&& argvar->vval.v_list != NULL
&& argvar->vval.v_list->lv_len > 0)
{
garray_T ga;
listitem_T *item;
ga_init2(&ga, 1, 100);
FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
{
char_u *s = tv_get_string_chk(&item->li_tv);
char_u *p;
if (s == NULL)
break;
p = vim_strsave_fnameescape(s, VSE_NONE);
if (p == NULL)
break;
ga_concat(&ga, p);
vim_free(p);
ga_append(&ga, ' ');
}
if (item == NULL)
{
ga_append(&ga, NUL);
term->tl_command = ga.ga_data;
}
else
ga_clear(&ga);
}
#endif
if (opt->jo_term_kill != NULL)
{
char_u *p = skiptowhite(opt->jo_term_kill);
term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
}
if (opt->jo_term_api != NULL)
{
char_u *p = skiptowhite(opt->jo_term_api);
term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
}
else
term->tl_api = vim_strsave((char_u *)"Tapi_");
if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
// Save the user-defined palette, it is only used in GUI (or 'tgc' is on).
if (opt->jo_set2 & JO2_ANSI_COLORS)
{
term->tl_palette = ALLOC_MULT(long_u, 16);
if (term->tl_palette == NULL)
{
vim_free(term);
return NULL;
}
memcpy(term->tl_palette, opt->jo_ansi_colors, sizeof(long_u) * 16);
}
#endif
// System dependent: setup the vterm and maybe start the job in it.
if (argv == NULL
&& argvar->v_type == VAR_STRING
&& argvar->vval.v_string != NULL
&& STRCMP(argvar->vval.v_string, "NONE") == 0)
res = create_pty_only(term, opt);
else
res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
newbuf = curbuf;
if (res == OK)
{
// Get and remember the size we ended up with. Update the pty.
vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
term_report_winsize(term, term->tl_rows, term->tl_cols);
#ifdef FEAT_GUI
if (term->tl_system)
{
// display first line below typed command
term->tl_toprow = msg_row + 1;
term->tl_dirty_row_end = 0;
}
#endif
// Make sure we don't get stuck on sending keys to the job, it leads to
// a deadlock if the job is waiting for Vim to read.
channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
if (old_curbuf != NULL)
{
--curbuf->b_nwindows;
curbuf = old_curbuf;
curwin->w_buffer = curbuf;
curwin->w_cursor = save_cursor;
++curbuf->b_nwindows;
}
else if (vgetc_busy
#ifdef FEAT_TIMERS
|| timer_busy
#endif
|| input_busy)
{
char_u ignore[4];
// When waiting for input need to return and possibly end up in
// terminal_loop() instead.
ignore[0] = K_SPECIAL;
ignore[1] = KS_EXTRA;
ignore[2] = KE_IGNORE;
ignore[3] = NUL;
ins_typebuf(ignore, REMAP_NONE, 0, TRUE, FALSE);
typebuf_was_filled = TRUE;
}
}
else
{
term_close_buffer(curbuf, old_curbuf);
return NULL;
}
apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
return newbuf;
}
/*
* ":terminal": open a terminal window and execute a job in it.
*/
void
ex_terminal(exarg_T *eap)
{
typval_T argvar[2];
jobopt_T opt;
int opt_shell = FALSE;
char_u *cmd;
char_u *tofree = NULL;
init_job_options(&opt);
cmd = eap->arg;
while (*cmd == '+' && *(cmd + 1) == '+')
{
char_u *p, *ep;
cmd += 2;
p = skiptowhite(cmd);
ep = vim_strchr(cmd, '=');
if (ep != NULL)
{
if (ep < p)
p = ep;
else
ep = NULL;
}
// Note: Keep this in sync with get_terminalopt_name.
# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
&& STRNICMP(cmd, name, sizeof(name) - 1) == 0)
if (OPTARG_HAS("close"))
opt.jo_term_finish = 'c';
else if (OPTARG_HAS("noclose"))
opt.jo_term_finish = 'n';
else if (OPTARG_HAS("open"))
opt.jo_term_finish = 'o';
else if (OPTARG_HAS("curwin"))
opt.jo_curwin = 1;
else if (OPTARG_HAS("hidden"))
opt.jo_hidden = 1;
else if (OPTARG_HAS("norestore"))
opt.jo_term_norestore = 1;
else if (OPTARG_HAS("shell"))
opt_shell = TRUE;
else if (OPTARG_HAS("kill") && ep != NULL)
{
opt.jo_set2 |= JO2_TERM_KILL;
opt.jo_term_kill = ep + 1;
p = skiptowhite(cmd);
}
else if (OPTARG_HAS("api"))
{
opt.jo_set2 |= JO2_TERM_API;
if (ep != NULL)
{
opt.jo_term_api = ep + 1;
p = skiptowhite(cmd);
}
else
opt.jo_term_api = NULL;
}
else if (OPTARG_HAS("rows") && ep != NULL && SAFE_isdigit(ep[1]))
{
opt.jo_set2 |= JO2_TERM_ROWS;
opt.jo_term_rows = atoi((char *)ep + 1);
p = skiptowhite(cmd);
}
else if (OPTARG_HAS("cols") && ep != NULL && SAFE_isdigit(ep[1]))
{
opt.jo_set2 |= JO2_TERM_COLS;
opt.jo_term_cols = atoi((char *)ep + 1);
p = skiptowhite(cmd);
}
else if (OPTARG_HAS("eof") && ep != NULL)
{
char_u *buf = NULL;
char_u *keys;
vim_free(opt.jo_eof_chars);
p = skiptowhite(cmd);
*p = NUL;
keys = replace_termcodes(ep + 1, &buf, 0,
REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
opt.jo_set2 |= JO2_EOF_CHARS;
opt.jo_eof_chars = vim_strsave(keys);
vim_free(buf);
*p = ' ';
}
#ifdef MSWIN
else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
&& ep != NULL)
{
int tty_type = NUL;
p = skiptowhite(cmd);
if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
tty_type = 'w';
else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
tty_type = 'c';
else
{
semsg(_(e_invalid_value_for_argument_str), "type");
goto theend;
}
opt.jo_set2 |= JO2_TTY_TYPE;
opt.jo_tty_type = tty_type;
}
#endif
else
{
if (*p)
*p = NUL;
semsg(_(e_invalid_attribute_str), cmd);
goto theend;
}
# undef OPTARG_HAS
cmd = skipwhite(p);
}
if (*cmd == NUL)
{
// Make a copy of 'shell', an autocommand may change the option.
tofree = cmd = vim_strsave(p_sh);
// default to close when the shell exits
if (opt.jo_term_finish == NUL)
opt.jo_term_finish = TL_FINISH_CLOSE;
}
if (eap->addr_count > 0)
{
// Write lines from current buffer to the job.
opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
opt.jo_io[PART_IN] = JIO_BUFFER;
opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
opt.jo_in_top = eap->line1;
opt.jo_in_bot = eap->line2;
}
if (opt_shell && tofree == NULL)
{
#ifdef UNIX
char **argv = NULL;
char_u *tofree1 = NULL;
char_u *tofree2 = NULL;
// :term ++shell command
if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
vim_free(argv);
vim_free(tofree1);
vim_free(tofree2);
goto theend;
#else
# ifdef MSWIN
long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
char_u *newcmd;
newcmd = alloc(cmdlen);
if (newcmd == NULL)
goto theend;
tofree = newcmd;
vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
cmd = newcmd;
# else
emsg(_(e_sorry_plusplusshell_not_supported_on_this_system));
goto theend;
# endif
#endif
}
argvar[0].v_type = VAR_STRING;
argvar[0].vval.v_string = cmd;
argvar[1].v_type = VAR_UNKNOWN;
term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
theend:
vim_free(tofree);
vim_free(opt.jo_eof_chars);
}
static char_u *
get_terminalopt_name(expand_T *xp UNUSED, int idx)
{
// Note: Keep this in sync with ex_terminal.
static char *(p_termopt_values[]) =
{
"close",
"noclose",
"open",
"curwin",
"hidden",
"norestore",
"shell",
"kill=",
"rows=",
"cols=",
"eof=",