blob: b65f5ae77097f5cd26b6a1df3dc6a84f71b42b9a [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
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +010046 p = vim_strsave_fnameescape(sname, VSE_NONE);
Bram Moolenaar84538072019-07-28 14:15:42 +020047 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
Bram Moolenaar84538072019-07-28 14:15:42 +0200151 // When 'buftype' is "nofile" can't restore the window contents.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100152 || bt_nofilename(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200153 return (ssop_flags & SSOP_BLANK);
154 if (bt_help(wp->w_buffer))
155 return (ssop_flags & SSOP_HELP);
156 return TRUE;
157}
158
159/*
160 * Return TRUE if frame "fr" has a window somewhere that we want to save in
161 * the Session.
162 */
163 static int
164ses_do_frame(frame_T *fr)
165{
166 frame_T *frc;
167
168 if (fr->fr_layout == FR_LEAF)
169 return ses_do_win(fr->fr_win);
170 FOR_ALL_FRAMES(frc, fr->fr_child)
171 if (ses_do_frame(frc))
172 return TRUE;
173 return FALSE;
174}
175
176/*
177 * Skip frames that don't contain windows we want to save in the Session.
178 * Returns NULL when there none.
179 */
180 static frame_T *
181ses_skipframe(frame_T *fr)
182{
183 frame_T *frc;
184
185 FOR_ALL_FRAMES(frc, fr)
186 if (ses_do_frame(frc))
187 break;
188 return frc;
189}
190
191/*
192 * Write commands to "fd" to recursively create windows for frame "fr",
193 * horizontally and vertically split.
194 * After the commands the last window in the frame is the current window.
195 * Returns FAIL when writing the commands to "fd" fails.
196 */
197 static int
198ses_win_rec(FILE *fd, frame_T *fr)
199{
200 frame_T *frc;
201 int count = 0;
202
203 if (fr->fr_layout != FR_LEAF)
204 {
205 // Find first frame that's not skipped and then create a window for
206 // each following one (first frame is already there).
207 frc = ses_skipframe(fr->fr_child);
208 if (frc != NULL)
209 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
210 {
211 // Make window as big as possible so that we have lots of room
212 // to split.
213 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
214 || put_line(fd, fr->fr_layout == FR_COL
215 ? "split" : "vsplit") == FAIL)
216 return FAIL;
217 ++count;
218 }
219
220 // Go back to the first window.
221 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
222 ? "%dwincmd k" : "%dwincmd h", count) < 0
223 || put_eol(fd) == FAIL))
224 return FAIL;
225
226 // Recursively create frames/windows in each window of this column or
227 // row.
228 frc = ses_skipframe(fr->fr_child);
229 while (frc != NULL)
230 {
231 ses_win_rec(fd, frc);
232 frc = ses_skipframe(frc->fr_next);
233 // Go to next window.
234 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
235 return FAIL;
236 }
237 }
238 return OK;
239}
240
241 static int
242ses_winsizes(
243 FILE *fd,
244 int restore_size,
245 win_T *tab_firstwin)
246{
247 int n = 0;
248 win_T *wp;
249
250 if (restore_size && (ssop_flags & SSOP_WINSIZE))
251 {
252 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
253 {
254 if (!ses_do_win(wp))
255 continue;
256 ++n;
257
258 // restore height when not full height
259 if (wp->w_height + wp->w_status_height < topframe->fr_height
260 && (fprintf(fd,
261 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
262 n, (long)wp->w_height, Rows / 2, Rows) < 0
263 || put_eol(fd) == FAIL))
264 return FAIL;
265
266 // restore width when not full width
267 if (wp->w_width < Columns && (fprintf(fd,
268 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
269 n, (long)wp->w_width, Columns / 2, Columns) < 0
270 || put_eol(fd) == FAIL))
271 return FAIL;
272 }
273 }
274 else
275 {
276 // Just equalise window sizes
277 if (put_line(fd, "wincmd =") == FAIL)
278 return FAIL;
279 }
280 return OK;
281}
282
283 static int
284put_view_curpos(FILE *fd, win_T *wp, char *spaces)
285{
286 int r;
287
288 if (wp->w_curswant == MAXCOL)
289 r = fprintf(fd, "%snormal! $", spaces);
290 else
291 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
292 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
293}
294
295/*
296 * Write commands to "fd" to restore the view of a window.
297 * Caller must make sure 'scrolloff' is zero.
298 */
299 static int
300put_view(
301 FILE *fd,
302 win_T *wp,
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200303 int add_edit, // add ":edit" command to view
304 unsigned *flagp, // vop_flags or ssop_flags
305 int current_arg_idx, // current argument index of the window,
306 // use -1 if unknown
307 hashtab_T *terminal_bufs UNUSED) // already encountered terminal buffers,
308 // can be NULL
Bram Moolenaar84538072019-07-28 14:15:42 +0200309{
310 win_T *save_curwin;
311 int f;
312 int do_cursor;
313 int did_next = FALSE;
314
315 // Always restore cursor position for ":mksession". For ":mkview" only
316 // when 'viewoptions' contains "cursor".
317 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
318
319 // Local argument list.
320 if (wp->w_alist == &global_alist)
321 {
322 if (put_line(fd, "argglobal") == FAIL)
323 return FAIL;
324 }
325 else
326 {
327 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
328 flagp == &vop_flags
329 || !(*flagp & SSOP_CURDIR)
330 || wp->w_localdir != NULL, flagp) == FAIL)
331 return FAIL;
332 }
333
334 // Only when part of a session: restore the argument index. Some
335 // arguments may have been deleted, check if the index is valid.
336 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
337 && flagp == &ssop_flags)
338 {
339 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
340 || put_eol(fd) == FAIL)
341 return FAIL;
342 did_next = TRUE;
343 }
344
345 // Edit the file. Skip this when ":next" already did it.
346 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
347 {
matveyt8e7d9db2022-01-05 14:01:30 +0000348 if (bt_help(wp->w_buffer))
349 {
350 char *curtag = "";
351
352 // A help buffer needs some options to be set.
353 // First, create a new empty buffer with "buftype=help".
354 // Then ":help" will re-use both the buffer and the window and set
355 // the options, even when "options" is not in 'sessionoptions'.
356 if (0 < wp->w_tagstackidx
357 && wp->w_tagstackidx <= wp->w_tagstacklen)
358 curtag = (char *)wp->w_tagstack[wp->w_tagstackidx - 1].tagname;
359
360 if (put_line(fd, "enew | setl bt=help") == FAIL
361 || fprintf(fd, "help %s", curtag) < 0
362 || put_eol(fd) == FAIL)
363 return FAIL;
364 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200365# ifdef FEAT_TERMINAL
matveyt8e7d9db2022-01-05 14:01:30 +0000366 else if (bt_terminal(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200367 {
Bram Moolenaar0e655112020-09-11 20:36:36 +0200368 if (term_write_session(fd, wp, terminal_bufs) == FAIL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200369 return FAIL;
370 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200371# endif
372 // Load the file.
matveyt8e7d9db2022-01-05 14:01:30 +0000373 else if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100374 && !bt_nofilename(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200375 {
376 // Editing a file in this buffer: use ":edit file".
377 // This may have side effects! (e.g., compressed or network file).
378 //
379 // Note, if a buffer for that file already exists, use :badd to
380 // edit that buffer, to not lose folding information (:edit resets
381 // folds in other buffers)
James Cherti7d428402022-03-14 20:24:51 +0000382 if (fputs("if bufexists(fnamemodify(\"", fd) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200383 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
James Cherti7d428402022-03-14 20:24:51 +0000384 || fputs("\", \":p\")) | buffer ", fd) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200385 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
386 || fputs(" | else | edit ", fd) < 0
387 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
388 || fputs(" | endif", fd) < 0
389 || put_eol(fd) == FAIL)
390 return FAIL;
391 }
392 else
393 {
394 // No file in this buffer, just make it empty.
395 if (put_line(fd, "enew") == FAIL)
396 return FAIL;
397#ifdef FEAT_QUICKFIX
398 if (wp->w_buffer->b_ffname != NULL)
399 {
400 // The buffer does have a name, but it's not a file name.
401 if (fputs("file ", fd) < 0
402 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
403 return FAIL;
404 }
405#endif
406 do_cursor = FALSE;
407 }
408 }
409
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100410 if (wp->w_alt_fnum)
411 {
412 buf_T *alt = buflist_findnr(wp->w_alt_fnum);
413
Bram Moolenaar0756f752021-03-13 13:52:33 +0100414 // Set the alternate file if the buffer is listed.
Bram Moolenaar139348f2021-02-05 21:55:53 +0100415 if ((flagp == &ssop_flags)
416 && alt != NULL
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100417 && alt->b_fname != NULL
418 && *alt->b_fname != NUL
Bram Moolenaar0756f752021-03-13 13:52:33 +0100419 && alt->b_p_bl
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100420 && (fputs("balt ", fd) < 0
421 || ses_fname(fd, alt, flagp, TRUE) == FAIL))
422 return FAIL;
423 }
424
Bram Moolenaar84538072019-07-28 14:15:42 +0200425 // Local mappings and abbreviations.
426 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
427 && makemap(fd, wp->w_buffer) == FAIL)
428 return FAIL;
429
430 // Local options. Need to go to the window temporarily.
431 // Store only local values when using ":mkview" and when ":mksession" is
432 // used and 'sessionoptions' doesn't include "options".
433 // Some folding options are always stored when "folds" is included,
434 // otherwise the folds would not be restored correctly.
435 save_curwin = curwin;
436 curwin = wp;
437 curbuf = curwin->w_buffer;
438 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
439 f = makeset(fd, OPT_LOCAL,
440 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
441#ifdef FEAT_FOLDING
442 else if (*flagp & SSOP_FOLDS)
443 f = makefoldset(fd);
444#endif
445 else
446 f = OK;
447 curwin = save_curwin;
448 curbuf = curwin->w_buffer;
449 if (f == FAIL)
450 return FAIL;
451
452#ifdef FEAT_FOLDING
453 // Save Folds when 'buftype' is empty and for help files.
454 if ((*flagp & SSOP_FOLDS)
455 && wp->w_buffer->b_ffname != NULL
456 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
457 {
458 if (put_folds(fd, wp) == FAIL)
459 return FAIL;
460 }
461#endif
462
463 // Set the cursor after creating folds, since that moves the cursor.
464 if (do_cursor)
465 {
466
467 // Restore the cursor line in the file and relatively in the
468 // window. Don't use "G", it changes the jumplist.
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200469 if (wp->w_height <= 0)
470 {
471 if (fprintf(fd, "let s:l = %ld", (long)wp->w_cursor.lnum) < 0)
472 return FAIL;
473 }
474 else if (fprintf(fd,
475 "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
Bram Moolenaar84538072019-07-28 14:15:42 +0200476 (long)wp->w_cursor.lnum,
477 (long)(wp->w_cursor.lnum - wp->w_topline),
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200478 (long)wp->w_height / 2, (long)wp->w_height) < 0)
479 return FAIL;
480
481 if (put_eol(fd) == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200482 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
Bram Moolenaar3482be62020-11-27 11:00:38 +0100483 || put_line(fd, "keepjumps exe s:l") == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200484 || put_line(fd, "normal! zt") == FAIL
Bram Moolenaar3482be62020-11-27 11:00:38 +0100485 || fprintf(fd, "keepjumps %ld", (long)wp->w_cursor.lnum) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200486 || put_eol(fd) == FAIL)
487 return FAIL;
488 // Restore the cursor column and left offset when not wrapping.
489 if (wp->w_cursor.col == 0)
490 {
491 if (put_line(fd, "normal! 0") == FAIL)
492 return FAIL;
493 }
494 else
495 {
496 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
497 {
498 if (fprintf(fd,
499 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
500 (long)wp->w_virtcol + 1,
501 (long)(wp->w_virtcol - wp->w_leftcol),
502 (long)wp->w_width / 2, (long)wp->w_width) < 0
503 || put_eol(fd) == FAIL
504 || put_line(fd, "if s:c > 0") == FAIL
505 || fprintf(fd,
506 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
507 (long)wp->w_virtcol + 1) < 0
508 || put_eol(fd) == FAIL
509 || put_line(fd, "else") == FAIL
510 || put_view_curpos(fd, wp, " ") == FAIL
511 || put_line(fd, "endif") == FAIL)
512 return FAIL;
513 }
514 else if (put_view_curpos(fd, wp, "") == FAIL)
515 return FAIL;
516 }
517 }
518
519 // Local directory, if the current flag is not view options or the "curdir"
520 // option is included.
521 if (wp->w_localdir != NULL
522 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
523 {
524 if (fputs("lcd ", fd) < 0
525 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
526 || put_eol(fd) == FAIL)
527 return FAIL;
528 did_lcd = TRUE;
529 }
530
531 return OK;
532}
533
534#ifdef FEAT_EVAL
535 static int
536store_session_globals(FILE *fd)
537{
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200538 hashtab_T *gvht = get_globvar_ht();
Bram Moolenaar84538072019-07-28 14:15:42 +0200539 hashitem_T *hi;
540 dictitem_T *this_var;
541 int todo;
542 char_u *p, *t;
543
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200544 todo = (int)gvht->ht_used;
545 for (hi = gvht->ht_array; todo > 0; ++hi)
Bram Moolenaar84538072019-07-28 14:15:42 +0200546 {
547 if (!HASHITEM_EMPTY(hi))
548 {
549 --todo;
550 this_var = HI2DI(hi);
551 if ((this_var->di_tv.v_type == VAR_NUMBER
552 || this_var->di_tv.v_type == VAR_STRING)
553 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
554 {
555 // Escape special characters with a backslash. Turn a LF and
556 // CR into \n and \r.
557 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
558 (char_u *)"\\\"\n\r");
559 if (p == NULL) // out of memory
560 break;
561 for (t = p; *t != NUL; ++t)
562 if (*t == '\n')
563 *t = 'n';
564 else if (*t == '\r')
565 *t = 'r';
566 if ((fprintf(fd, "let %s = %c%s%c",
567 this_var->di_key,
568 (this_var->di_tv.v_type == VAR_STRING) ? '"'
569 : ' ',
570 p,
571 (this_var->di_tv.v_type == VAR_STRING) ? '"'
572 : ' ') < 0)
573 || put_eol(fd) == FAIL)
574 {
575 vim_free(p);
576 return FAIL;
577 }
578 vim_free(p);
579 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200580 else if (this_var->di_tv.v_type == VAR_FLOAT
581 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
582 {
583 float_T f = this_var->di_tv.vval.v_float;
584 int sign = ' ';
585
586 if (f < 0)
587 {
588 f = -f;
589 sign = '-';
590 }
591 if ((fprintf(fd, "let %s = %c%f",
592 this_var->di_key, sign, f) < 0)
593 || put_eol(fd) == FAIL)
594 return FAIL;
595 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200596 }
597 }
598 return OK;
599}
600#endif
601
602/*
603 * Write openfile commands for the current buffers to an .exrc file.
604 * Return FAIL on error, OK otherwise.
605 */
606 static int
607makeopens(
608 FILE *fd,
609 char_u *dirnow) // Current directory name
610{
611 buf_T *buf;
612 int only_save_windows = TRUE;
613 int nr;
614 int restore_size = TRUE;
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100615 int restore_height_width = FALSE;
Bram Moolenaar84538072019-07-28 14:15:42 +0200616 win_T *wp;
617 char_u *sname;
618 win_T *edited_win = NULL;
Bram Moolenaar84538072019-07-28 14:15:42 +0200619 int restore_stal = FALSE;
620 win_T *tab_firstwin;
621 frame_T *tab_topframe;
622 int cur_arg_idx = 0;
623 int next_arg_idx = 0;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200624 int ret = FAIL;
LemonBoyd7c95642022-04-30 16:10:27 +0100625 tabpage_T *tp;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200626#ifdef FEAT_TERMINAL
627 hashtab_T terminal_bufs;
628
629 hash_init(&terminal_bufs);
630#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200631
632 if (ssop_flags & SSOP_BUFFERS)
633 only_save_windows = FALSE; // Save ALL buffers
634
635 // Begin by setting the this_session variable, and then other
636 // sessionable variables.
637#ifdef FEAT_EVAL
638 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200639 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200640 if (ssop_flags & SSOP_GLOBALS)
641 if (store_session_globals(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200642 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200643#endif
644
645 // Close all windows and tabs but one.
646 if (put_line(fd, "silent only") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200647 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200648 if ((ssop_flags & SSOP_TABPAGES)
649 && put_line(fd, "silent tabonly") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200650 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200651
652 // Now a :cd command to the session directory or the current directory
653 if (ssop_flags & SSOP_SESDIR)
654 {
655 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
656 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200657 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200658 }
659 else if (ssop_flags & SSOP_CURDIR)
660 {
661 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
662 if (sname == NULL
663 || fputs("cd ", fd) < 0
664 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
665 || put_eol(fd) == FAIL)
666 {
667 vim_free(sname);
Bram Moolenaar0e655112020-09-11 20:36:36 +0200668 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200669 }
670 vim_free(sname);
671 }
672
673 // If there is an empty, unnamed buffer we will wipe it out later.
674 // Remember the buffer number.
675 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200676 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200677 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200678 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200679 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200680 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200681
James Chertifd012802022-03-29 12:02:57 +0100682 // save 'shortmess' if not storing options
683 if ((ssop_flags & SSOP_OPTIONS) == 0
684 && put_line(fd, "let s:shortmess_save = &shortmess") == FAIL)
685 goto fail;
686
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +0100687 // set 'shortmess' for the following. Add the 'A' flag if it was there
688 if (put_line(fd, "if &shortmess =~ 'A'") == FAIL
689 || put_line(fd, " set shortmess=aoOA") == FAIL
690 || put_line(fd, "else") == FAIL
691 || put_line(fd, " set shortmess=aoO") == FAIL
692 || put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200693 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200694
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +0100695 // Now save the current files, current buffer first.
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000696 // Put all buffers into the buffer list.
697 // Do it very early to preserve buffer order after loading session (which
698 // can be disrupted by prior `edit` or `tabedit` calls).
699 FOR_ALL_BUFFERS(buf)
700 {
701 if (!(only_save_windows && buf->b_nwindows == 0)
702 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000703 // Skip terminal buffers: finished ones are not useful, others
704 // will be resurrected and result in a new buffer.
705 && !bt_terminal(buf)
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000706 && buf->b_fname != NULL
707 && buf->b_p_bl)
708 {
709 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
710 : buf->b_wininfo->wi_fpos.lnum) < 0
711 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
712 goto fail;
713 }
714 }
715
Bram Moolenaar84538072019-07-28 14:15:42 +0200716 // the global argument list
717 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
718 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200719 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200720
721 if (ssop_flags & SSOP_RESIZE)
722 {
723 // Note: after the restore we still check it worked!
724 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
725 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200726 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200727 }
728
729#ifdef FEAT_GUI
730 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
731 {
732 int x, y;
733
734 if (gui_mch_get_winpos(&x, &y) == OK)
735 {
736 // Note: after the restore we still check it worked!
737 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200738 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200739 }
740 }
741#endif
742
743 // When there are two or more tabpages and 'showtabline' is 1 the tabline
744 // will be displayed when creating the next tab. That resizes the windows
745 // in the first tab, which may cause problems. Set 'showtabline' to 2
746 // temporarily to avoid that.
747 if (p_stal == 1 && first_tabpage->tp_next != NULL)
748 {
749 if (put_line(fd, "set stal=2") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200750 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200751 restore_stal = TRUE;
752 }
753
Bram Moolenaar84538072019-07-28 14:15:42 +0200754 if ((ssop_flags & SSOP_TABPAGES))
755 {
LemonBoyd7c95642022-04-30 16:10:27 +0100756 // "tabpages" is in 'sessionoptions': Similar to ses_win_rec() below,
757 // populate the tab pages first so later local options won't be copied
758 // to the new tabs.
Bram Moolenaar84538072019-07-28 14:15:42 +0200759 FOR_ALL_TABPAGES(tp)
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000760 // Use `bufhidden=wipe` to remove empty "placeholder" buffers once
761 // they are not needed. This prevents creating extra buffers (see
762 // cause of patch 8.1.0829)
763 if (tp->tp_next != NULL
764 && put_line(fd, "tabnew +setlocal\\ bufhidden=wipe") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200765 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200766 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200767 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200768 }
LemonBoyd7c95642022-04-30 16:10:27 +0100769
770 // Assume "tabpages" is in 'sessionoptions'. If not then we only do
771 // "curtab" and bail out of the loop.
772 FOR_ALL_TABPAGES(tp)
Bram Moolenaar84538072019-07-28 14:15:42 +0200773 {
Bram Moolenaar84538072019-07-28 14:15:42 +0200774 int need_tabnext = FALSE;
775 int cnr = 1;
776
LemonBoyd7c95642022-04-30 16:10:27 +0100777 // May repeat putting Windows for each tab, when "tabpages" is in
778 // 'sessionoptions'.
779 // Don't use goto_tabpage(), it may change directory and trigger
780 // autocommands.
Bram Moolenaar84538072019-07-28 14:15:42 +0200781 if ((ssop_flags & SSOP_TABPAGES))
782 {
Bram Moolenaar84538072019-07-28 14:15:42 +0200783 if (tp == curtab)
784 {
785 tab_firstwin = firstwin;
786 tab_topframe = topframe;
787 }
788 else
789 {
790 tab_firstwin = tp->tp_firstwin;
791 tab_topframe = tp->tp_topframe;
792 }
LemonBoyd7c95642022-04-30 16:10:27 +0100793 if (tp != first_tabpage)
Bram Moolenaar84538072019-07-28 14:15:42 +0200794 need_tabnext = TRUE;
795 }
LemonBoyd7c95642022-04-30 16:10:27 +0100796 else
797 {
798 tp = curtab;
799 tab_firstwin = firstwin;
800 tab_topframe = topframe;
801 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200802
803 // Before creating the window layout, try loading one file. If this
804 // is aborted we don't end up with a number of useless windows.
805 // This may have side effects! (e.g., compressed or network file).
806 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
807 {
808 if (ses_do_win(wp)
809 && wp->w_buffer->b_ffname != NULL
810 && !bt_help(wp->w_buffer)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100811 && !bt_nofilename(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200812 {
813 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200814 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200815 need_tabnext = FALSE;
816
817 if (fputs("edit ", fd) < 0
818 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
819 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200820 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200821 if (!wp->w_arg_idx_invalid)
822 edited_win = wp;
823 break;
824 }
825 }
826
827 // If no file got edited create an empty tab page.
828 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200829 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200830
Bram Moolenaar0995c812021-04-17 18:38:54 +0200831 if (tab_topframe->fr_layout != FR_LEAF)
832 {
833 // Save current window layout.
834 if (put_line(fd, "let s:save_splitbelow = &splitbelow") == FAIL
835 || put_line(fd, "let s:save_splitright = &splitright")
836 == FAIL)
837 goto fail;
838 if (put_line(fd, "set splitbelow splitright") == FAIL)
839 goto fail;
840 if (ses_win_rec(fd, tab_topframe) == FAIL)
841 goto fail;
842 if (put_line(fd, "let &splitbelow = s:save_splitbelow") == FAIL
843 || put_line(fd, "let &splitright = s:save_splitright")
844 == FAIL)
845 goto fail;
846 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200847
848 // Check if window sizes can be restored (no windows omitted).
849 // Remember the window number of the current window after restoring.
850 nr = 0;
851 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
852 {
853 if (ses_do_win(wp))
854 ++nr;
855 else
856 restore_size = FALSE;
857 if (curwin == wp)
858 cnr = nr;
859 }
860
Bram Moolenaar0995c812021-04-17 18:38:54 +0200861 if (tab_firstwin->w_next != NULL)
862 {
863 // Go to the first window.
864 if (put_line(fd, "wincmd t") == FAIL)
865 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200866
Bram Moolenaar0995c812021-04-17 18:38:54 +0200867 // If more than one window, see if sizes can be restored.
868 // First set 'winheight' and 'winwidth' to 1 to avoid the windows
869 // being resized when moving between windows.
870 // Do this before restoring the view, so that the topline and the
871 // cursor can be set. This is done again below.
872 // winminheight and winminwidth need to be set to avoid an error if
873 // the user has set winheight or winwidth.
874 if (put_line(fd, "let s:save_winminheight = &winminheight") == FAIL
875 || put_line(fd, "let s:save_winminwidth = &winminwidth")
876 == FAIL)
877 goto fail;
878 if (put_line(fd, "set winminheight=0") == FAIL
879 || put_line(fd, "set winheight=1") == FAIL
880 || put_line(fd, "set winminwidth=0") == FAIL
881 || put_line(fd, "set winwidth=1") == FAIL)
882 goto fail;
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100883 restore_height_width = TRUE;
Bram Moolenaar0995c812021-04-17 18:38:54 +0200884 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200885 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200886 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200887
888 // Restore the tab-local working directory if specified
889 // Do this before the windows, so that the window-local directory can
890 // override the tab-local directory.
LemonBoyd7c95642022-04-30 16:10:27 +0100891 if ((ssop_flags & SSOP_CURDIR) && tp->tp_localdir != NULL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200892 {
893 if (fputs("tcd ", fd) < 0
LemonBoyd7c95642022-04-30 16:10:27 +0100894 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
895 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200896 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200897 did_lcd = TRUE;
898 }
899
900 // Restore the view of the window (options, file, cursor, etc.).
901 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
902 {
903 if (!ses_do_win(wp))
904 continue;
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200905 if (put_view(fd, wp, wp != edited_win, &ssop_flags, cur_arg_idx,
Bram Moolenaar0e655112020-09-11 20:36:36 +0200906#ifdef FEAT_TERMINAL
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200907 &terminal_bufs
908#else
909 NULL
Bram Moolenaar0e655112020-09-11 20:36:36 +0200910#endif
911 ) == FAIL)
912 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200913 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200914 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200915 next_arg_idx = wp->w_arg_idx;
916 }
917
918 // The argument index in the first tab page is zero, need to set it in
919 // each window. For further tab pages it's the window where we do
920 // "tabedit".
921 cur_arg_idx = next_arg_idx;
922
923 // Restore cursor to the current window if it's not the first one.
924 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
925 || put_eol(fd) == FAIL))
Bram Moolenaar0e655112020-09-11 20:36:36 +0200926 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200927
928 // Restore window sizes again after jumping around in windows, because
929 // the current window has a minimum size while others may not.
930 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200931 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200932
933 // Don't continue in another tab page when doing only the current one
934 // or when at the last tab page.
935 if (!(ssop_flags & SSOP_TABPAGES))
936 break;
937 }
938
939 if (ssop_flags & SSOP_TABPAGES)
940 {
941 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
942 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200943 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200944 }
945 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200946 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200947
Bram Moolenaar84538072019-07-28 14:15:42 +0200948 // Wipe out an empty unnamed buffer we started in.
949 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
950 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200951 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200952 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200953 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200954 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200955 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200956 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200957 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200958
James Chertifd012802022-03-29 12:02:57 +0100959 // Re-apply 'winheight' and 'winwidth'.
960 if (fprintf(fd, "set winheight=%ld winwidth=%ld",
961 p_wh, p_wiw) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200962 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100963
964 // Restore 'shortmess'.
965 if (ssop_flags & SSOP_OPTIONS)
966 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100967 if (fprintf(fd, "set shortmess=%s", p_shm) < 0 || put_eol(fd) == FAIL)
968 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100969 }
970 else
971 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100972 if (put_line(fd, "let &shortmess = s:shortmess_save") == FAIL)
973 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100974 }
975
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100976 if (restore_height_width)
Bram Moolenaar0995c812021-04-17 18:38:54 +0200977 {
978 // Restore 'winminheight' and 'winminwidth'.
979 if (put_line(fd, "let &winminheight = s:save_winminheight") == FAIL
980 || put_line(fd, "let &winminwidth = s:save_winminwidth") == FAIL)
981 goto fail;
982 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200983
984 // Lastly, execute the x.vim file if it exists.
985 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
Bram Moolenaarc5f33db2020-04-16 21:04:41 +0200986 || put_line(fd, "if filereadable(s:sx)") == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200987 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
988 || put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200989 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200990
Bram Moolenaar0e655112020-09-11 20:36:36 +0200991 ret = OK;
992fail:
993#ifdef FEAT_TERMINAL
994 hash_clear_all(&terminal_bufs, 0);
995#endif
996 return ret;
Bram Moolenaar84538072019-07-28 14:15:42 +0200997}
998
999/*
1000 * Get the name of the view file for the current buffer.
1001 */
1002 static char_u *
1003get_view_file(int c)
1004{
1005 int len = 0;
1006 char_u *p, *s;
1007 char_u *retval;
1008 char_u *sname;
1009
1010 if (curbuf->b_ffname == NULL)
1011 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001012 emsg(_(e_no_file_name));
Bram Moolenaar84538072019-07-28 14:15:42 +02001013 return NULL;
1014 }
1015 sname = home_replace_save(NULL, curbuf->b_ffname);
1016 if (sname == NULL)
1017 return NULL;
1018
1019 // We want a file name without separators, because we're not going to make
1020 // a directory.
1021 // "normal" path separator -> "=+"
1022 // "=" -> "=="
1023 // ":" path separator -> "=-"
1024 for (p = sname; *p; ++p)
1025 if (*p == '=' || vim_ispathsep(*p))
1026 ++len;
1027 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
1028 if (retval != NULL)
1029 {
1030 STRCPY(retval, p_vdir);
1031 add_pathsep(retval);
1032 s = retval + STRLEN(retval);
1033 for (p = sname; *p; ++p)
1034 {
1035 if (*p == '=')
1036 {
1037 *s++ = '=';
1038 *s++ = '=';
1039 }
1040 else if (vim_ispathsep(*p))
1041 {
1042 *s++ = '=';
1043#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
1044 if (*p == ':')
1045 *s++ = '-';
1046 else
1047#endif
1048 *s++ = '+';
1049 }
1050 else
1051 *s++ = *p;
1052 }
1053 *s++ = '=';
1054 *s++ = c;
1055 STRCPY(s, ".vim");
1056 }
1057
1058 vim_free(sname);
1059 return retval;
1060}
1061
1062/*
1063 * ":loadview [nr]"
1064 */
1065 void
1066ex_loadview(exarg_T *eap)
1067{
1068 char_u *fname;
1069
1070 fname = get_view_file(*eap->arg);
1071 if (fname != NULL)
1072 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 do_source(fname, FALSE, DOSO_NONE, NULL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001074 vim_free(fname);
1075 }
1076}
1077
Bram Moolenaard17a57a2019-09-29 20:53:55 +02001078# if defined(FEAT_GUI_GNOME) \
Bram Moolenaarac02a632019-09-29 19:02:46 +02001079 || (defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)) \
1080 || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001081/*
1082 * Generate a script that can be used to restore the current editing session.
1083 * Save the value of v:this_session before running :mksession in order to make
1084 * automagic session save fully transparent. Return TRUE on success.
1085 */
1086 int
1087write_session_file(char_u *filename)
1088{
1089 char_u *escaped_filename;
1090 char *mksession_cmdline;
1091 unsigned int save_ssop_flags;
1092 int failed;
1093
1094 // Build an ex command line to create a script that restores the current
1095 // session if executed. Escape the filename to avoid nasty surprises.
1096 escaped_filename = vim_strsave_escaped(filename, escape_chars);
1097 if (escaped_filename == NULL)
1098 return FALSE;
1099 mksession_cmdline = alloc(10 + (int)STRLEN(escaped_filename) + 1);
1100 if (mksession_cmdline == NULL)
1101 {
1102 vim_free(escaped_filename);
1103 return FALSE;
1104 }
1105 strcpy(mksession_cmdline, "mksession ");
1106 STRCAT(mksession_cmdline, escaped_filename);
1107 vim_free(escaped_filename);
1108
1109 // Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
1110 // unpredictable effects when the session is saved automatically. Also,
1111 // we definitely need SSOP_GLOBALS to be able to restore v:this_session.
1112 // Don't use SSOP_BUFFERS to prevent the buffer list from becoming
1113 // enormously large if the GNOME session feature is used regularly.
1114 save_ssop_flags = ssop_flags;
1115 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
1116 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
1117
1118 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
1119 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
1120 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
1121 do_unlet((char_u *)"Save_VV_this_session", TRUE);
1122
1123 ssop_flags = save_ssop_flags;
1124 vim_free(mksession_cmdline);
1125
1126 // Reopen the file and append a command to restore v:this_session,
1127 // as if this save never happened. This is to avoid conflicts with
1128 // the user's own sessions. FIXME: It's probably less hackish to add
1129 // a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
1130 if (!failed)
1131 {
1132 FILE *fd;
1133
1134 fd = open_exfile(filename, TRUE, APPENDBIN);
1135
1136 failed = (fd == NULL
1137 || put_line(fd, "let v:this_session = Save_VV_this_session")
1138 == FAIL
1139 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
1140
1141 if (fd != NULL && fclose(fd) != 0)
1142 failed = TRUE;
1143
1144 if (failed)
1145 mch_remove(filename);
1146 }
1147
1148 return !failed;
1149}
1150# endif
1151
1152#endif // FEAT_SESSION
1153
1154#if defined(FEAT_SESSION) && defined(USE_CRNL)
1155# define MKSESSION_NL
1156static int mksession_nl = FALSE; // use NL only in put_eol()
1157#endif
1158
Bram Moolenaar84538072019-07-28 14:15:42 +02001159/*
1160 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
1161 */
1162 void
1163ex_mkrc(exarg_T *eap)
1164{
1165 FILE *fd;
1166 int failed = FALSE;
1167 char_u *fname;
1168#ifdef FEAT_BROWSE
1169 char_u *browseFile = NULL;
1170#endif
1171#ifdef FEAT_SESSION
1172 int view_session = FALSE;
1173 int using_vdir = FALSE; // using 'viewdir'?
1174 char_u *viewFile = NULL;
1175 unsigned *flagp;
1176#endif
1177
1178 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
1179 {
1180#ifdef FEAT_SESSION
1181 view_session = TRUE;
1182#else
1183 ex_ni(eap);
1184 return;
1185#endif
1186 }
1187
1188#ifdef FEAT_SESSION
1189 // Use the short file name until ":lcd" is used. We also don't use the
1190 // short file name when 'acd' is set, that is checked later.
1191 did_lcd = FALSE;
1192
1193 // ":mkview" or ":mkview 9": generate file name with 'viewdir'
1194 if (eap->cmdidx == CMD_mkview
1195 && (*eap->arg == NUL
1196 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
1197 {
1198 eap->forceit = TRUE;
1199 fname = get_view_file(*eap->arg);
1200 if (fname == NULL)
1201 return;
1202 viewFile = fname;
1203 using_vdir = TRUE;
1204 }
1205 else
1206#endif
1207 if (*eap->arg != NUL)
1208 fname = eap->arg;
1209 else if (eap->cmdidx == CMD_mkvimrc)
1210 fname = (char_u *)VIMRC_FILE;
1211#ifdef FEAT_SESSION
1212 else if (eap->cmdidx == CMD_mksession)
1213 fname = (char_u *)SESSION_FILE;
1214#endif
1215 else
1216 fname = (char_u *)EXRC_FILE;
1217
1218#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001219 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar84538072019-07-28 14:15:42 +02001220 {
1221 browseFile = do_browse(BROWSE_SAVE,
1222# ifdef FEAT_SESSION
1223 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
1224 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
1225# endif
1226 (char_u *)_("Save Setup"),
1227 fname, (char_u *)"vim", NULL,
1228 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1229 if (browseFile == NULL)
1230 goto theend;
1231 fname = browseFile;
1232 eap->forceit = TRUE; // since dialog already asked
1233 }
1234#endif
1235
1236#if defined(FEAT_SESSION) && defined(vim_mkdir)
1237 // When using 'viewdir' may have to create the directory.
1238 if (using_vdir && !mch_isdir(p_vdir))
1239 vim_mkdir_emsg(p_vdir, 0755);
1240#endif
1241
1242 fd = open_exfile(fname, eap->forceit, WRITEBIN);
1243 if (fd != NULL)
1244 {
1245#ifdef FEAT_SESSION
1246 if (eap->cmdidx == CMD_mkview)
1247 flagp = &vop_flags;
1248 else
1249 flagp = &ssop_flags;
1250#endif
1251
1252#ifdef MKSESSION_NL
1253 // "unix" in 'sessionoptions': use NL line separator
1254 if (view_session && (*flagp & SSOP_UNIX))
1255 mksession_nl = TRUE;
1256#endif
1257
1258 // Write the version command for :mkvimrc
1259 if (eap->cmdidx == CMD_mkvimrc)
1260 (void)put_line(fd, "version 6.0");
1261
1262#ifdef FEAT_SESSION
1263 if (eap->cmdidx == CMD_mksession)
1264 {
1265 if (put_line(fd, "let SessionLoad = 1") == FAIL)
1266 failed = TRUE;
1267 }
1268
1269 if (eap->cmdidx != CMD_mkview)
1270#endif
1271 {
1272 // Write setting 'compatible' first, because it has side effects.
1273 // For that same reason only do it when needed.
1274 if (p_cp)
1275 (void)put_line(fd, "if !&cp | set cp | endif");
1276 else
1277 (void)put_line(fd, "if &cp | set nocp | endif");
1278 }
1279
1280#ifdef FEAT_SESSION
1281 if (!view_session
1282 || (eap->cmdidx == CMD_mksession
1283 && (*flagp & SSOP_OPTIONS)))
1284#endif
Bram Moolenaar635bd602021-04-16 19:58:22 +02001285 {
1286 int flags = OPT_GLOBAL;
1287
1288#ifdef FEAT_SESSION
1289 if (eap->cmdidx == CMD_mksession && (*flagp & SSOP_SKIP_RTP))
1290 flags |= OPT_SKIPRTP;
1291#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001292 failed |= (makemap(fd, NULL) == FAIL
Bram Moolenaar635bd602021-04-16 19:58:22 +02001293 || makeset(fd, flags, FALSE) == FAIL);
1294 }
Bram Moolenaar84538072019-07-28 14:15:42 +02001295
1296#ifdef FEAT_SESSION
1297 if (!failed && view_session)
1298 {
Bram Moolenaar38890832020-11-01 17:40:54 +01001299 if (put_line(fd, "let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1") == FAIL)
Bram Moolenaar84538072019-07-28 14:15:42 +02001300 failed = TRUE;
1301 if (eap->cmdidx == CMD_mksession)
1302 {
1303 char_u *dirnow; // current directory
1304
1305 dirnow = alloc(MAXPATHL);
1306 if (dirnow == NULL)
1307 failed = TRUE;
1308 else
1309 {
1310 // Change to session file's dir.
1311 if (mch_dirname(dirnow, MAXPATHL) == FAIL
1312 || mch_chdir((char *)dirnow) != 0)
1313 *dirnow = NUL;
1314 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
1315 {
1316 if (vim_chdirfile(fname, NULL) == OK)
1317 shorten_fnames(TRUE);
1318 }
1319 else if (*dirnow != NUL
1320 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
1321 {
1322 if (mch_chdir((char *)globaldir) == 0)
1323 shorten_fnames(TRUE);
1324 }
1325
1326 failed |= (makeopens(fd, dirnow) == FAIL);
1327
1328 // restore original dir
1329 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
1330 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
1331 {
1332 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001333 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar84538072019-07-28 14:15:42 +02001334 shorten_fnames(TRUE);
1335 }
1336 vim_free(dirnow);
1337 }
1338 }
1339 else
1340 {
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001341 failed |= (put_view(fd, curwin, !using_vdir, flagp, -1, NULL)
1342 == FAIL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001343 }
Bram Moolenaar38890832020-11-01 17:40:54 +01001344 if (put_line(fd, "let &g:so = s:so_save | let &g:siso = s:siso_save")
Bram Moolenaar84538072019-07-28 14:15:42 +02001345 == FAIL)
1346 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001347#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar84538072019-07-28 14:15:42 +02001348 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
1349 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001350#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001351 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
1352 failed = TRUE;
1353 if (eap->cmdidx == CMD_mksession)
1354 {
1355 if (put_line(fd, "unlet SessionLoad") == FAIL)
1356 failed = TRUE;
1357 }
1358 }
1359#endif
1360 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
1361 failed = TRUE;
1362
1363 failed |= fclose(fd);
1364
1365 if (failed)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001366 emsg(_(e_error_while_writing));
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001367#if defined(FEAT_SESSION)
Bram Moolenaar84538072019-07-28 14:15:42 +02001368 else if (eap->cmdidx == CMD_mksession)
1369 {
1370 // successful session write - set this_session var
1371 char_u *tbuf;
1372
1373 tbuf = alloc(MAXPATHL);
1374 if (tbuf != NULL)
1375 {
1376 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
1377 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
1378 vim_free(tbuf);
1379 }
1380 }
1381#endif
1382#ifdef MKSESSION_NL
1383 mksession_nl = FALSE;
1384#endif
1385 }
1386
1387#ifdef FEAT_BROWSE
1388theend:
1389 vim_free(browseFile);
1390#endif
1391#ifdef FEAT_SESSION
1392 vim_free(viewFile);
1393#endif
1394}
1395
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001396#if (defined(FEAT_VIMINFO) || defined(FEAT_SESSION)) || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001397 var_flavour_T
1398var_flavour(char_u *varname)
1399{
1400 char_u *p = varname;
1401
1402 if (ASCII_ISUPPER(*p))
1403 {
1404 while (*(++p))
1405 if (ASCII_ISLOWER(*p))
1406 return VAR_FLAVOUR_SESSION;
1407 return VAR_FLAVOUR_VIMINFO;
1408 }
1409 else
1410 return VAR_FLAVOUR_DEFAULT;
1411}
1412#endif
1413
1414/*
1415 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
1416 * Return FAIL for a write error.
1417 */
1418 int
1419put_eol(FILE *fd)
1420{
1421 if (
1422#ifdef USE_CRNL
1423 (
1424# ifdef MKSESSION_NL
1425 !mksession_nl &&
1426# endif
1427 (putc('\r', fd) < 0)) ||
1428#endif
1429 (putc('\n', fd) < 0))
1430 return FAIL;
1431 return OK;
1432}
1433
1434/*
1435 * Write a line to "fd".
1436 * Return FAIL for a write error.
1437 */
1438 int
1439put_line(FILE *fd, char *s)
1440{
1441 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
1442 return FAIL;
1443 return OK;
1444}