blob: 5e6f0b53e87205e0e0554f8a28d62bb0a2701af2 [file] [log] [blame]
Bram Moolenaar84538072019-07-28 14:15:42 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * session.c: session related functions
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_SESSION) || defined(PROTO)
17
18static int did_lcd; // whether ":lcd" was produced for a session
19
20/*
21 * Write a file name to the session file.
22 * Takes care of the "slash" option in 'sessionoptions' and escapes special
23 * characters.
24 * Returns FAIL if writing fails or out of memory.
25 */
26 static int
27ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
28{
29 char_u *sname;
30 char_u *p;
31 int retval = OK;
32
33 sname = home_replace_save(NULL, name);
34 if (sname == NULL)
35 return FAIL;
36
37 if (*flagp & SSOP_SLASH)
38 {
39 // change all backslashes to forward slashes
40 for (p = sname; *p != NUL; MB_PTR_ADV(p))
41 if (*p == '\\')
42 *p = '/';
43 }
44
45 // escape special characters
46 p = vim_strsave_fnameescape(sname, FALSE);
47 vim_free(sname);
48 if (p == NULL)
49 return FAIL;
50
51 // write the result
52 if (fputs((char *)p, fd) < 0)
53 retval = FAIL;
54
55 vim_free(p);
56 return retval;
57}
58
59/*
60 * Write a buffer name to the session file.
61 * Also ends the line, if "add_eol" is TRUE.
62 * Returns FAIL if writing fails.
63 */
64 static int
65ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
66{
67 char_u *name;
68
69 // Use the short file name if the current directory is known at the time
70 // the session file will be sourced.
71 // Don't do this for ":mkview", we don't know the current directory.
72 // Don't do this after ":lcd", we don't keep track of what the current
73 // directory is.
74 if (buf->b_sfname != NULL
75 && flagp == &ssop_flags
76 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
77#ifdef FEAT_AUTOCHDIR
78 && !p_acd
79#endif
80 && !did_lcd)
81 name = buf->b_sfname;
82 else
83 name = buf->b_ffname;
84 if (ses_put_fname(fd, name, flagp) == FAIL
85 || (add_eol && put_eol(fd) == FAIL))
86 return FAIL;
87 return OK;
88}
89
90/*
91 * Write an argument list to the session file.
92 * Returns FAIL if writing fails.
93 */
94 static int
95ses_arglist(
96 FILE *fd,
97 char *cmd,
98 garray_T *gap,
99 int fullname, // TRUE: use full path name
100 unsigned *flagp)
101{
102 int i;
103 char_u *buf = NULL;
104 char_u *s;
105
106 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
107 return FAIL;
108 if (put_line(fd, "%argdel") == FAIL)
109 return FAIL;
110 for (i = 0; i < gap->ga_len; ++i)
111 {
112 // NULL file names are skipped (only happens when out of memory).
113 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
114 if (s != NULL)
115 {
116 if (fullname)
117 {
118 buf = alloc(MAXPATHL);
119 if (buf != NULL)
120 {
121 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
122 s = buf;
123 }
124 }
125 if (fputs("$argadd ", fd) < 0
126 || ses_put_fname(fd, s, flagp) == FAIL
127 || put_eol(fd) == FAIL)
128 {
129 vim_free(buf);
130 return FAIL;
131 }
132 vim_free(buf);
133 }
134 }
135 return OK;
136}
137
138/*
139 * Return non-zero if window "wp" is to be stored in the Session.
140 */
141 static int
142ses_do_win(win_T *wp)
143{
144#ifdef FEAT_TERMINAL
145 if (bt_terminal(wp->w_buffer))
146 return !term_is_finished(wp->w_buffer)
147 && (ssop_flags & SSOP_TERMINAL)
148 && term_should_restore(wp->w_buffer);
149#endif
150 if (wp->w_buffer->b_fname == NULL
151#ifdef FEAT_QUICKFIX
152 // When 'buftype' is "nofile" can't restore the window contents.
153 || bt_nofilename(wp->w_buffer)
154#endif
155 )
156 return (ssop_flags & SSOP_BLANK);
157 if (bt_help(wp->w_buffer))
158 return (ssop_flags & SSOP_HELP);
159 return TRUE;
160}
161
162/*
163 * Return TRUE if frame "fr" has a window somewhere that we want to save in
164 * the Session.
165 */
166 static int
167ses_do_frame(frame_T *fr)
168{
169 frame_T *frc;
170
171 if (fr->fr_layout == FR_LEAF)
172 return ses_do_win(fr->fr_win);
173 FOR_ALL_FRAMES(frc, fr->fr_child)
174 if (ses_do_frame(frc))
175 return TRUE;
176 return FALSE;
177}
178
179/*
180 * Skip frames that don't contain windows we want to save in the Session.
181 * Returns NULL when there none.
182 */
183 static frame_T *
184ses_skipframe(frame_T *fr)
185{
186 frame_T *frc;
187
188 FOR_ALL_FRAMES(frc, fr)
189 if (ses_do_frame(frc))
190 break;
191 return frc;
192}
193
194/*
195 * Write commands to "fd" to recursively create windows for frame "fr",
196 * horizontally and vertically split.
197 * After the commands the last window in the frame is the current window.
198 * Returns FAIL when writing the commands to "fd" fails.
199 */
200 static int
201ses_win_rec(FILE *fd, frame_T *fr)
202{
203 frame_T *frc;
204 int count = 0;
205
206 if (fr->fr_layout != FR_LEAF)
207 {
208 // Find first frame that's not skipped and then create a window for
209 // each following one (first frame is already there).
210 frc = ses_skipframe(fr->fr_child);
211 if (frc != NULL)
212 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
213 {
214 // Make window as big as possible so that we have lots of room
215 // to split.
216 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
217 || put_line(fd, fr->fr_layout == FR_COL
218 ? "split" : "vsplit") == FAIL)
219 return FAIL;
220 ++count;
221 }
222
223 // Go back to the first window.
224 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
225 ? "%dwincmd k" : "%dwincmd h", count) < 0
226 || put_eol(fd) == FAIL))
227 return FAIL;
228
229 // Recursively create frames/windows in each window of this column or
230 // row.
231 frc = ses_skipframe(fr->fr_child);
232 while (frc != NULL)
233 {
234 ses_win_rec(fd, frc);
235 frc = ses_skipframe(frc->fr_next);
236 // Go to next window.
237 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
238 return FAIL;
239 }
240 }
241 return OK;
242}
243
244 static int
245ses_winsizes(
246 FILE *fd,
247 int restore_size,
248 win_T *tab_firstwin)
249{
250 int n = 0;
251 win_T *wp;
252
253 if (restore_size && (ssop_flags & SSOP_WINSIZE))
254 {
255 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
256 {
257 if (!ses_do_win(wp))
258 continue;
259 ++n;
260
261 // restore height when not full height
262 if (wp->w_height + wp->w_status_height < topframe->fr_height
263 && (fprintf(fd,
264 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
265 n, (long)wp->w_height, Rows / 2, Rows) < 0
266 || put_eol(fd) == FAIL))
267 return FAIL;
268
269 // restore width when not full width
270 if (wp->w_width < Columns && (fprintf(fd,
271 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
272 n, (long)wp->w_width, Columns / 2, Columns) < 0
273 || put_eol(fd) == FAIL))
274 return FAIL;
275 }
276 }
277 else
278 {
279 // Just equalise window sizes
280 if (put_line(fd, "wincmd =") == FAIL)
281 return FAIL;
282 }
283 return OK;
284}
285
286 static int
287put_view_curpos(FILE *fd, win_T *wp, char *spaces)
288{
289 int r;
290
291 if (wp->w_curswant == MAXCOL)
292 r = fprintf(fd, "%snormal! $", spaces);
293 else
294 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
295 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
296}
297
298/*
299 * Write commands to "fd" to restore the view of a window.
300 * Caller must make sure 'scrolloff' is zero.
301 */
302 static int
303put_view(
304 FILE *fd,
305 win_T *wp,
306 int add_edit, // add ":edit" command to view
307 unsigned *flagp, // vop_flags or ssop_flags
Bram Moolenaar0e655112020-09-11 20:36:36 +0200308 int current_arg_idx // current argument index of the window, use
309 // -1 if unknown
310#ifdef FEAT_TERMINAL
311 , hashtab_T *terminal_bufs
312#endif
313 )
Bram Moolenaar84538072019-07-28 14:15:42 +0200314{
315 win_T *save_curwin;
316 int f;
317 int do_cursor;
318 int did_next = FALSE;
319
320 // Always restore cursor position for ":mksession". For ":mkview" only
321 // when 'viewoptions' contains "cursor".
322 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
323
324 // Local argument list.
325 if (wp->w_alist == &global_alist)
326 {
327 if (put_line(fd, "argglobal") == FAIL)
328 return FAIL;
329 }
330 else
331 {
332 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
333 flagp == &vop_flags
334 || !(*flagp & SSOP_CURDIR)
335 || wp->w_localdir != NULL, flagp) == FAIL)
336 return FAIL;
337 }
338
339 // Only when part of a session: restore the argument index. Some
340 // arguments may have been deleted, check if the index is valid.
341 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
342 && flagp == &ssop_flags)
343 {
344 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
345 || put_eol(fd) == FAIL)
346 return FAIL;
347 did_next = TRUE;
348 }
349
350 // Edit the file. Skip this when ":next" already did it.
351 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
352 {
353# ifdef FEAT_TERMINAL
354 if (bt_terminal(wp->w_buffer))
355 {
Bram Moolenaar0e655112020-09-11 20:36:36 +0200356 if (term_write_session(fd, wp, terminal_bufs) == FAIL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200357 return FAIL;
358 }
359 else
360# endif
361 // Load the file.
362 if (wp->w_buffer->b_ffname != NULL
363# ifdef FEAT_QUICKFIX
364 && !bt_nofilename(wp->w_buffer)
365# endif
366 )
367 {
368 // Editing a file in this buffer: use ":edit file".
369 // This may have side effects! (e.g., compressed or network file).
370 //
371 // Note, if a buffer for that file already exists, use :badd to
372 // edit that buffer, to not lose folding information (:edit resets
373 // folds in other buffers)
374 if (fputs("if bufexists(\"", fd) < 0
375 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
376 || fputs("\") | buffer ", fd) < 0
377 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
378 || fputs(" | else | edit ", fd) < 0
379 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
380 || fputs(" | endif", fd) < 0
381 || put_eol(fd) == FAIL)
382 return FAIL;
383 }
384 else
385 {
386 // No file in this buffer, just make it empty.
387 if (put_line(fd, "enew") == FAIL)
388 return FAIL;
389#ifdef FEAT_QUICKFIX
390 if (wp->w_buffer->b_ffname != NULL)
391 {
392 // The buffer does have a name, but it's not a file name.
393 if (fputs("file ", fd) < 0
394 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
395 return FAIL;
396 }
397#endif
398 do_cursor = FALSE;
399 }
400 }
401
402 // Local mappings and abbreviations.
403 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
404 && makemap(fd, wp->w_buffer) == FAIL)
405 return FAIL;
406
407 // Local options. Need to go to the window temporarily.
408 // Store only local values when using ":mkview" and when ":mksession" is
409 // used and 'sessionoptions' doesn't include "options".
410 // Some folding options are always stored when "folds" is included,
411 // otherwise the folds would not be restored correctly.
412 save_curwin = curwin;
413 curwin = wp;
414 curbuf = curwin->w_buffer;
415 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
416 f = makeset(fd, OPT_LOCAL,
417 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
418#ifdef FEAT_FOLDING
419 else if (*flagp & SSOP_FOLDS)
420 f = makefoldset(fd);
421#endif
422 else
423 f = OK;
424 curwin = save_curwin;
425 curbuf = curwin->w_buffer;
426 if (f == FAIL)
427 return FAIL;
428
429#ifdef FEAT_FOLDING
430 // Save Folds when 'buftype' is empty and for help files.
431 if ((*flagp & SSOP_FOLDS)
432 && wp->w_buffer->b_ffname != NULL
433 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
434 {
435 if (put_folds(fd, wp) == FAIL)
436 return FAIL;
437 }
438#endif
439
440 // Set the cursor after creating folds, since that moves the cursor.
441 if (do_cursor)
442 {
443
444 // Restore the cursor line in the file and relatively in the
445 // window. Don't use "G", it changes the jumplist.
446 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
447 (long)wp->w_cursor.lnum,
448 (long)(wp->w_cursor.lnum - wp->w_topline),
449 (long)wp->w_height / 2, (long)wp->w_height) < 0
450 || put_eol(fd) == FAIL
451 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
452 || put_line(fd, "exe s:l") == FAIL
453 || put_line(fd, "normal! zt") == FAIL
454 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
455 || put_eol(fd) == FAIL)
456 return FAIL;
457 // Restore the cursor column and left offset when not wrapping.
458 if (wp->w_cursor.col == 0)
459 {
460 if (put_line(fd, "normal! 0") == FAIL)
461 return FAIL;
462 }
463 else
464 {
465 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
466 {
467 if (fprintf(fd,
468 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
469 (long)wp->w_virtcol + 1,
470 (long)(wp->w_virtcol - wp->w_leftcol),
471 (long)wp->w_width / 2, (long)wp->w_width) < 0
472 || put_eol(fd) == FAIL
473 || put_line(fd, "if s:c > 0") == FAIL
474 || fprintf(fd,
475 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
476 (long)wp->w_virtcol + 1) < 0
477 || put_eol(fd) == FAIL
478 || put_line(fd, "else") == FAIL
479 || put_view_curpos(fd, wp, " ") == FAIL
480 || put_line(fd, "endif") == FAIL)
481 return FAIL;
482 }
483 else if (put_view_curpos(fd, wp, "") == FAIL)
484 return FAIL;
485 }
486 }
487
488 // Local directory, if the current flag is not view options or the "curdir"
489 // option is included.
490 if (wp->w_localdir != NULL
491 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
492 {
493 if (fputs("lcd ", fd) < 0
494 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
495 || put_eol(fd) == FAIL)
496 return FAIL;
497 did_lcd = TRUE;
498 }
499
500 return OK;
501}
502
503#ifdef FEAT_EVAL
504 static int
505store_session_globals(FILE *fd)
506{
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200507 hashtab_T *gvht = get_globvar_ht();
Bram Moolenaar84538072019-07-28 14:15:42 +0200508 hashitem_T *hi;
509 dictitem_T *this_var;
510 int todo;
511 char_u *p, *t;
512
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200513 todo = (int)gvht->ht_used;
514 for (hi = gvht->ht_array; todo > 0; ++hi)
Bram Moolenaar84538072019-07-28 14:15:42 +0200515 {
516 if (!HASHITEM_EMPTY(hi))
517 {
518 --todo;
519 this_var = HI2DI(hi);
520 if ((this_var->di_tv.v_type == VAR_NUMBER
521 || this_var->di_tv.v_type == VAR_STRING)
522 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
523 {
524 // Escape special characters with a backslash. Turn a LF and
525 // CR into \n and \r.
526 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
527 (char_u *)"\\\"\n\r");
528 if (p == NULL) // out of memory
529 break;
530 for (t = p; *t != NUL; ++t)
531 if (*t == '\n')
532 *t = 'n';
533 else if (*t == '\r')
534 *t = 'r';
535 if ((fprintf(fd, "let %s = %c%s%c",
536 this_var->di_key,
537 (this_var->di_tv.v_type == VAR_STRING) ? '"'
538 : ' ',
539 p,
540 (this_var->di_tv.v_type == VAR_STRING) ? '"'
541 : ' ') < 0)
542 || put_eol(fd) == FAIL)
543 {
544 vim_free(p);
545 return FAIL;
546 }
547 vim_free(p);
548 }
549#ifdef FEAT_FLOAT
550 else if (this_var->di_tv.v_type == VAR_FLOAT
551 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
552 {
553 float_T f = this_var->di_tv.vval.v_float;
554 int sign = ' ';
555
556 if (f < 0)
557 {
558 f = -f;
559 sign = '-';
560 }
561 if ((fprintf(fd, "let %s = %c%f",
562 this_var->di_key, sign, f) < 0)
563 || put_eol(fd) == FAIL)
564 return FAIL;
565 }
566#endif
567 }
568 }
569 return OK;
570}
571#endif
572
573/*
574 * Write openfile commands for the current buffers to an .exrc file.
575 * Return FAIL on error, OK otherwise.
576 */
577 static int
578makeopens(
579 FILE *fd,
580 char_u *dirnow) // Current directory name
581{
582 buf_T *buf;
583 int only_save_windows = TRUE;
584 int nr;
585 int restore_size = TRUE;
586 win_T *wp;
587 char_u *sname;
588 win_T *edited_win = NULL;
589 int tabnr;
590 int restore_stal = FALSE;
591 win_T *tab_firstwin;
592 frame_T *tab_topframe;
593 int cur_arg_idx = 0;
594 int next_arg_idx = 0;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200595 int ret = FAIL;
596#ifdef FEAT_TERMINAL
597 hashtab_T terminal_bufs;
598
599 hash_init(&terminal_bufs);
600#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200601
602 if (ssop_flags & SSOP_BUFFERS)
603 only_save_windows = FALSE; // Save ALL buffers
604
605 // Begin by setting the this_session variable, and then other
606 // sessionable variables.
607#ifdef FEAT_EVAL
608 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200609 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200610 if (ssop_flags & SSOP_GLOBALS)
611 if (store_session_globals(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200612 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200613#endif
614
615 // Close all windows and tabs but one.
616 if (put_line(fd, "silent only") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200617 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200618 if ((ssop_flags & SSOP_TABPAGES)
619 && put_line(fd, "silent tabonly") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200620 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200621
622 // Now a :cd command to the session directory or the current directory
623 if (ssop_flags & SSOP_SESDIR)
624 {
625 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
626 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200627 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200628 }
629 else if (ssop_flags & SSOP_CURDIR)
630 {
631 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
632 if (sname == NULL
633 || fputs("cd ", fd) < 0
634 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
635 || put_eol(fd) == FAIL)
636 {
637 vim_free(sname);
Bram Moolenaar0e655112020-09-11 20:36:36 +0200638 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200639 }
640 vim_free(sname);
641 }
642
643 // If there is an empty, unnamed buffer we will wipe it out later.
644 // Remember the buffer number.
645 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200646 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200647 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200648 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200649 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200650 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200651
652 // Now save the current files, current buffer first.
653 if (put_line(fd, "set shortmess=aoO") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200654 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200655
656 // the global argument list
657 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
658 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200659 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200660
661 if (ssop_flags & SSOP_RESIZE)
662 {
663 // Note: after the restore we still check it worked!
664 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
665 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200666 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200667 }
668
669#ifdef FEAT_GUI
670 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
671 {
672 int x, y;
673
674 if (gui_mch_get_winpos(&x, &y) == OK)
675 {
676 // Note: after the restore we still check it worked!
677 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200678 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200679 }
680 }
681#endif
682
683 // When there are two or more tabpages and 'showtabline' is 1 the tabline
684 // will be displayed when creating the next tab. That resizes the windows
685 // in the first tab, which may cause problems. Set 'showtabline' to 2
686 // temporarily to avoid that.
687 if (p_stal == 1 && first_tabpage->tp_next != NULL)
688 {
689 if (put_line(fd, "set stal=2") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200690 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200691 restore_stal = TRUE;
692 }
693
694 // May repeat putting Windows for each tab, when "tabpages" is in
695 // 'sessionoptions'.
696 // Don't use goto_tabpage(), it may change directory and trigger
697 // autocommands.
698 tab_firstwin = firstwin; // first window in tab page "tabnr"
699 tab_topframe = topframe;
700 if ((ssop_flags & SSOP_TABPAGES))
701 {
702 tabpage_T *tp;
703
704 // Similar to ses_win_rec() below, populate the tab pages first so
705 // later local options won't be copied to the new tabs.
706 FOR_ALL_TABPAGES(tp)
707 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200708 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200709 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200710 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200711 }
712 for (tabnr = 1; ; ++tabnr)
713 {
714 tabpage_T *tp = NULL;
715 int need_tabnext = FALSE;
716 int cnr = 1;
717
718 if ((ssop_flags & SSOP_TABPAGES))
719 {
720 tp = find_tabpage(tabnr);
721
722 if (tp == NULL)
723 break; // done all tab pages
724 if (tp == curtab)
725 {
726 tab_firstwin = firstwin;
727 tab_topframe = topframe;
728 }
729 else
730 {
731 tab_firstwin = tp->tp_firstwin;
732 tab_topframe = tp->tp_topframe;
733 }
734 if (tabnr > 1)
735 need_tabnext = TRUE;
736 }
737
738 // Before creating the window layout, try loading one file. If this
739 // is aborted we don't end up with a number of useless windows.
740 // This may have side effects! (e.g., compressed or network file).
741 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
742 {
743 if (ses_do_win(wp)
744 && wp->w_buffer->b_ffname != NULL
745 && !bt_help(wp->w_buffer)
746#ifdef FEAT_QUICKFIX
747 && !bt_nofilename(wp->w_buffer)
748#endif
749 )
750 {
751 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200752 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200753 need_tabnext = FALSE;
754
755 if (fputs("edit ", fd) < 0
756 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
757 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200758 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200759 if (!wp->w_arg_idx_invalid)
760 edited_win = wp;
761 break;
762 }
763 }
764
765 // If no file got edited create an empty tab page.
766 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200767 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200768
769 // Save current window layout.
770 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200771 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200772 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200773 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200774 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200775 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200776 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200777 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200778
779 // Check if window sizes can be restored (no windows omitted).
780 // Remember the window number of the current window after restoring.
781 nr = 0;
782 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
783 {
784 if (ses_do_win(wp))
785 ++nr;
786 else
787 restore_size = FALSE;
788 if (curwin == wp)
789 cnr = nr;
790 }
791
792 // Go to the first window.
793 if (put_line(fd, "wincmd t") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200794 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200795
796 // If more than one window, see if sizes can be restored.
797 // First set 'winheight' and 'winwidth' to 1 to avoid the windows being
798 // resized when moving between windows.
799 // Do this before restoring the view, so that the topline and the
800 // cursor can be set. This is done again below.
801 // winminheight and winminwidth need to be set to avoid an error if the
802 // user has set winheight or winwidth.
803 if (put_line(fd, "set winminheight=0") == FAIL
804 || put_line(fd, "set winheight=1") == FAIL
805 || put_line(fd, "set winminwidth=0") == FAIL
806 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200807 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200808 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200809 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200810
811 // Restore the tab-local working directory if specified
812 // Do this before the windows, so that the window-local directory can
813 // override the tab-local directory.
814 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
815 {
816 if (fputs("tcd ", fd) < 0
817 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
818 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200819 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200820 did_lcd = TRUE;
821 }
822
823 // Restore the view of the window (options, file, cursor, etc.).
824 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
825 {
826 if (!ses_do_win(wp))
827 continue;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200828 if (put_view(fd, wp, wp != edited_win, &ssop_flags, cur_arg_idx
829#ifdef FEAT_TERMINAL
830 , &terminal_bufs
831#endif
832 ) == FAIL)
833 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200834 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200835 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200836 next_arg_idx = wp->w_arg_idx;
837 }
838
839 // The argument index in the first tab page is zero, need to set it in
840 // each window. For further tab pages it's the window where we do
841 // "tabedit".
842 cur_arg_idx = next_arg_idx;
843
844 // Restore cursor to the current window if it's not the first one.
845 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
846 || put_eol(fd) == FAIL))
Bram Moolenaar0e655112020-09-11 20:36:36 +0200847 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200848
849 // Restore window sizes again after jumping around in windows, because
850 // the current window has a minimum size while others may not.
851 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200852 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200853
854 // Don't continue in another tab page when doing only the current one
855 // or when at the last tab page.
856 if (!(ssop_flags & SSOP_TABPAGES))
857 break;
858 }
859
860 if (ssop_flags & SSOP_TABPAGES)
861 {
862 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
863 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200864 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200865 }
866 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200867 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200868
869 // Now put the remaining buffers into the buffer list.
870 // This is near the end, so that when 'hidden' is set we don't create extra
871 // buffers. If the buffer was already created with another command the
872 // ":badd" will have no effect.
873 FOR_ALL_BUFFERS(buf)
874 {
875 if (!(only_save_windows && buf->b_nwindows == 0)
876 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
877#ifdef FEAT_TERMINAL
878 // Skip terminal buffers: finished ones are not useful, others
879 // will be resurrected and result in a new buffer.
880 && !bt_terminal(buf)
881#endif
882 && buf->b_fname != NULL
883 && buf->b_p_bl)
884 {
885 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
886 : buf->b_wininfo->wi_fpos.lnum) < 0
887 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200888 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200889 }
890 }
891
892 // Wipe out an empty unnamed buffer we started in.
893 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
894 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200895 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200896 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200897 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200898 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200899 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200900 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200901 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200902
903 // Re-apply 'winheight', 'winwidth' and 'shortmess'.
904 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
905 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200906 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200907 // Re-apply 'winminheight' and 'winminwidth'.
908 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
909 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200910 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200911
912 // Lastly, execute the x.vim file if it exists.
913 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
Bram Moolenaarc5f33db2020-04-16 21:04:41 +0200914 || put_line(fd, "if filereadable(s:sx)") == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200915 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
916 || put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200917 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200918
Bram Moolenaar0e655112020-09-11 20:36:36 +0200919 ret = OK;
920fail:
921#ifdef FEAT_TERMINAL
922 hash_clear_all(&terminal_bufs, 0);
923#endif
924 return ret;
Bram Moolenaar84538072019-07-28 14:15:42 +0200925}
926
927/*
928 * Get the name of the view file for the current buffer.
929 */
930 static char_u *
931get_view_file(int c)
932{
933 int len = 0;
934 char_u *p, *s;
935 char_u *retval;
936 char_u *sname;
937
938 if (curbuf->b_ffname == NULL)
939 {
940 emsg(_(e_noname));
941 return NULL;
942 }
943 sname = home_replace_save(NULL, curbuf->b_ffname);
944 if (sname == NULL)
945 return NULL;
946
947 // We want a file name without separators, because we're not going to make
948 // a directory.
949 // "normal" path separator -> "=+"
950 // "=" -> "=="
951 // ":" path separator -> "=-"
952 for (p = sname; *p; ++p)
953 if (*p == '=' || vim_ispathsep(*p))
954 ++len;
955 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
956 if (retval != NULL)
957 {
958 STRCPY(retval, p_vdir);
959 add_pathsep(retval);
960 s = retval + STRLEN(retval);
961 for (p = sname; *p; ++p)
962 {
963 if (*p == '=')
964 {
965 *s++ = '=';
966 *s++ = '=';
967 }
968 else if (vim_ispathsep(*p))
969 {
970 *s++ = '=';
971#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
972 if (*p == ':')
973 *s++ = '-';
974 else
975#endif
976 *s++ = '+';
977 }
978 else
979 *s++ = *p;
980 }
981 *s++ = '=';
982 *s++ = c;
983 STRCPY(s, ".vim");
984 }
985
986 vim_free(sname);
987 return retval;
988}
989
990/*
991 * ":loadview [nr]"
992 */
993 void
994ex_loadview(exarg_T *eap)
995{
996 char_u *fname;
997
998 fname = get_view_file(*eap->arg);
999 if (fname != NULL)
1000 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 do_source(fname, FALSE, DOSO_NONE, NULL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001002 vim_free(fname);
1003 }
1004}
1005
Bram Moolenaard17a57a2019-09-29 20:53:55 +02001006# if defined(FEAT_GUI_GNOME) \
Bram Moolenaarac02a632019-09-29 19:02:46 +02001007 || (defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)) \
1008 || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001009/*
1010 * Generate a script that can be used to restore the current editing session.
1011 * Save the value of v:this_session before running :mksession in order to make
1012 * automagic session save fully transparent. Return TRUE on success.
1013 */
1014 int
1015write_session_file(char_u *filename)
1016{
1017 char_u *escaped_filename;
1018 char *mksession_cmdline;
1019 unsigned int save_ssop_flags;
1020 int failed;
1021
1022 // Build an ex command line to create a script that restores the current
1023 // session if executed. Escape the filename to avoid nasty surprises.
1024 escaped_filename = vim_strsave_escaped(filename, escape_chars);
1025 if (escaped_filename == NULL)
1026 return FALSE;
1027 mksession_cmdline = alloc(10 + (int)STRLEN(escaped_filename) + 1);
1028 if (mksession_cmdline == NULL)
1029 {
1030 vim_free(escaped_filename);
1031 return FALSE;
1032 }
1033 strcpy(mksession_cmdline, "mksession ");
1034 STRCAT(mksession_cmdline, escaped_filename);
1035 vim_free(escaped_filename);
1036
1037 // Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
1038 // unpredictable effects when the session is saved automatically. Also,
1039 // we definitely need SSOP_GLOBALS to be able to restore v:this_session.
1040 // Don't use SSOP_BUFFERS to prevent the buffer list from becoming
1041 // enormously large if the GNOME session feature is used regularly.
1042 save_ssop_flags = ssop_flags;
1043 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
1044 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
1045
1046 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
1047 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
1048 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
1049 do_unlet((char_u *)"Save_VV_this_session", TRUE);
1050
1051 ssop_flags = save_ssop_flags;
1052 vim_free(mksession_cmdline);
1053
1054 // Reopen the file and append a command to restore v:this_session,
1055 // as if this save never happened. This is to avoid conflicts with
1056 // the user's own sessions. FIXME: It's probably less hackish to add
1057 // a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
1058 if (!failed)
1059 {
1060 FILE *fd;
1061
1062 fd = open_exfile(filename, TRUE, APPENDBIN);
1063
1064 failed = (fd == NULL
1065 || put_line(fd, "let v:this_session = Save_VV_this_session")
1066 == FAIL
1067 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
1068
1069 if (fd != NULL && fclose(fd) != 0)
1070 failed = TRUE;
1071
1072 if (failed)
1073 mch_remove(filename);
1074 }
1075
1076 return !failed;
1077}
1078# endif
1079
1080#endif // FEAT_SESSION
1081
1082#if defined(FEAT_SESSION) && defined(USE_CRNL)
1083# define MKSESSION_NL
1084static int mksession_nl = FALSE; // use NL only in put_eol()
1085#endif
1086
Bram Moolenaar84538072019-07-28 14:15:42 +02001087/*
1088 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
1089 */
1090 void
1091ex_mkrc(exarg_T *eap)
1092{
1093 FILE *fd;
1094 int failed = FALSE;
1095 char_u *fname;
1096#ifdef FEAT_BROWSE
1097 char_u *browseFile = NULL;
1098#endif
1099#ifdef FEAT_SESSION
1100 int view_session = FALSE;
1101 int using_vdir = FALSE; // using 'viewdir'?
1102 char_u *viewFile = NULL;
1103 unsigned *flagp;
1104#endif
Bram Moolenaar0e655112020-09-11 20:36:36 +02001105#ifdef FEAT_TERMINAL
1106 hashtab_T terminal_bufs;
1107
1108 hash_init(&terminal_bufs);
1109#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001110
1111 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
1112 {
1113#ifdef FEAT_SESSION
1114 view_session = TRUE;
1115#else
1116 ex_ni(eap);
1117 return;
1118#endif
1119 }
1120
1121#ifdef FEAT_SESSION
1122 // Use the short file name until ":lcd" is used. We also don't use the
1123 // short file name when 'acd' is set, that is checked later.
1124 did_lcd = FALSE;
1125
1126 // ":mkview" or ":mkview 9": generate file name with 'viewdir'
1127 if (eap->cmdidx == CMD_mkview
1128 && (*eap->arg == NUL
1129 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
1130 {
1131 eap->forceit = TRUE;
1132 fname = get_view_file(*eap->arg);
1133 if (fname == NULL)
1134 return;
1135 viewFile = fname;
1136 using_vdir = TRUE;
1137 }
1138 else
1139#endif
1140 if (*eap->arg != NUL)
1141 fname = eap->arg;
1142 else if (eap->cmdidx == CMD_mkvimrc)
1143 fname = (char_u *)VIMRC_FILE;
1144#ifdef FEAT_SESSION
1145 else if (eap->cmdidx == CMD_mksession)
1146 fname = (char_u *)SESSION_FILE;
1147#endif
1148 else
1149 fname = (char_u *)EXRC_FILE;
1150
1151#ifdef FEAT_BROWSE
1152 if (cmdmod.browse)
1153 {
1154 browseFile = do_browse(BROWSE_SAVE,
1155# ifdef FEAT_SESSION
1156 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
1157 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
1158# endif
1159 (char_u *)_("Save Setup"),
1160 fname, (char_u *)"vim", NULL,
1161 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1162 if (browseFile == NULL)
1163 goto theend;
1164 fname = browseFile;
1165 eap->forceit = TRUE; // since dialog already asked
1166 }
1167#endif
1168
1169#if defined(FEAT_SESSION) && defined(vim_mkdir)
1170 // When using 'viewdir' may have to create the directory.
1171 if (using_vdir && !mch_isdir(p_vdir))
1172 vim_mkdir_emsg(p_vdir, 0755);
1173#endif
1174
1175 fd = open_exfile(fname, eap->forceit, WRITEBIN);
1176 if (fd != NULL)
1177 {
1178#ifdef FEAT_SESSION
1179 if (eap->cmdidx == CMD_mkview)
1180 flagp = &vop_flags;
1181 else
1182 flagp = &ssop_flags;
1183#endif
1184
1185#ifdef MKSESSION_NL
1186 // "unix" in 'sessionoptions': use NL line separator
1187 if (view_session && (*flagp & SSOP_UNIX))
1188 mksession_nl = TRUE;
1189#endif
1190
1191 // Write the version command for :mkvimrc
1192 if (eap->cmdidx == CMD_mkvimrc)
1193 (void)put_line(fd, "version 6.0");
1194
1195#ifdef FEAT_SESSION
1196 if (eap->cmdidx == CMD_mksession)
1197 {
1198 if (put_line(fd, "let SessionLoad = 1") == FAIL)
1199 failed = TRUE;
1200 }
1201
1202 if (eap->cmdidx != CMD_mkview)
1203#endif
1204 {
1205 // Write setting 'compatible' first, because it has side effects.
1206 // For that same reason only do it when needed.
1207 if (p_cp)
1208 (void)put_line(fd, "if !&cp | set cp | endif");
1209 else
1210 (void)put_line(fd, "if &cp | set nocp | endif");
1211 }
1212
1213#ifdef FEAT_SESSION
1214 if (!view_session
1215 || (eap->cmdidx == CMD_mksession
1216 && (*flagp & SSOP_OPTIONS)))
1217#endif
1218 failed |= (makemap(fd, NULL) == FAIL
1219 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
1220
1221#ifdef FEAT_SESSION
1222 if (!failed && view_session)
1223 {
1224 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
1225 failed = TRUE;
1226 if (eap->cmdidx == CMD_mksession)
1227 {
1228 char_u *dirnow; // current directory
1229
1230 dirnow = alloc(MAXPATHL);
1231 if (dirnow == NULL)
1232 failed = TRUE;
1233 else
1234 {
1235 // Change to session file's dir.
1236 if (mch_dirname(dirnow, MAXPATHL) == FAIL
1237 || mch_chdir((char *)dirnow) != 0)
1238 *dirnow = NUL;
1239 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
1240 {
1241 if (vim_chdirfile(fname, NULL) == OK)
1242 shorten_fnames(TRUE);
1243 }
1244 else if (*dirnow != NUL
1245 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
1246 {
1247 if (mch_chdir((char *)globaldir) == 0)
1248 shorten_fnames(TRUE);
1249 }
1250
1251 failed |= (makeopens(fd, dirnow) == FAIL);
1252
1253 // restore original dir
1254 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
1255 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
1256 {
1257 if (mch_chdir((char *)dirnow) != 0)
1258 emsg(_(e_prev_dir));
1259 shorten_fnames(TRUE);
1260 }
1261 vim_free(dirnow);
1262 }
1263 }
1264 else
1265 {
Bram Moolenaar0e655112020-09-11 20:36:36 +02001266 failed |= (put_view(fd, curwin, !using_vdir, flagp, -1
1267#ifdef FEAT_TERMINAL
1268 , &terminal_bufs
1269#endif
1270 ) == FAIL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001271 }
1272 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
1273 == FAIL)
1274 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001275#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar84538072019-07-28 14:15:42 +02001276 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
1277 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001278#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001279 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
1280 failed = TRUE;
1281 if (eap->cmdidx == CMD_mksession)
1282 {
1283 if (put_line(fd, "unlet SessionLoad") == FAIL)
1284 failed = TRUE;
1285 }
1286 }
1287#endif
1288 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
1289 failed = TRUE;
1290
1291 failed |= fclose(fd);
1292
1293 if (failed)
1294 emsg(_(e_write));
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001295#if defined(FEAT_SESSION)
Bram Moolenaar84538072019-07-28 14:15:42 +02001296 else if (eap->cmdidx == CMD_mksession)
1297 {
1298 // successful session write - set this_session var
1299 char_u *tbuf;
1300
1301 tbuf = alloc(MAXPATHL);
1302 if (tbuf != NULL)
1303 {
1304 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
1305 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
1306 vim_free(tbuf);
1307 }
1308 }
1309#endif
1310#ifdef MKSESSION_NL
1311 mksession_nl = FALSE;
1312#endif
1313 }
1314
1315#ifdef FEAT_BROWSE
1316theend:
1317 vim_free(browseFile);
1318#endif
1319#ifdef FEAT_SESSION
1320 vim_free(viewFile);
1321#endif
1322}
1323
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001324#if (defined(FEAT_VIMINFO) || defined(FEAT_SESSION)) || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001325 var_flavour_T
1326var_flavour(char_u *varname)
1327{
1328 char_u *p = varname;
1329
1330 if (ASCII_ISUPPER(*p))
1331 {
1332 while (*(++p))
1333 if (ASCII_ISLOWER(*p))
1334 return VAR_FLAVOUR_SESSION;
1335 return VAR_FLAVOUR_VIMINFO;
1336 }
1337 else
1338 return VAR_FLAVOUR_DEFAULT;
1339}
1340#endif
1341
1342/*
1343 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
1344 * Return FAIL for a write error.
1345 */
1346 int
1347put_eol(FILE *fd)
1348{
1349 if (
1350#ifdef USE_CRNL
1351 (
1352# ifdef MKSESSION_NL
1353 !mksession_nl &&
1354# endif
1355 (putc('\r', fd) < 0)) ||
1356#endif
1357 (putc('\n', fd) < 0))
1358 return FAIL;
1359 return OK;
1360}
1361
1362/*
1363 * Write a line to "fd".
1364 * Return FAIL for a write error.
1365 */
1366 int
1367put_line(FILE *fd, char *s)
1368{
1369 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
1370 return FAIL;
1371 return OK;
1372}