blob: bc259986ba0a33b4facc17c5e41d501b225049aa [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
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,
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200306 int add_edit, // add ":edit" command to view
307 unsigned *flagp, // vop_flags or ssop_flags
308 int current_arg_idx, // current argument index of the window,
309 // use -1 if unknown
310 hashtab_T *terminal_bufs UNUSED) // already encountered terminal buffers,
311 // can be NULL
Bram Moolenaar84538072019-07-28 14:15:42 +0200312{
313 win_T *save_curwin;
314 int f;
315 int do_cursor;
316 int did_next = FALSE;
317
318 // Always restore cursor position for ":mksession". For ":mkview" only
319 // when 'viewoptions' contains "cursor".
320 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
321
322 // Local argument list.
323 if (wp->w_alist == &global_alist)
324 {
325 if (put_line(fd, "argglobal") == FAIL)
326 return FAIL;
327 }
328 else
329 {
330 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
331 flagp == &vop_flags
332 || !(*flagp & SSOP_CURDIR)
333 || wp->w_localdir != NULL, flagp) == FAIL)
334 return FAIL;
335 }
336
337 // Only when part of a session: restore the argument index. Some
338 // arguments may have been deleted, check if the index is valid.
339 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
340 && flagp == &ssop_flags)
341 {
342 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
343 || put_eol(fd) == FAIL)
344 return FAIL;
345 did_next = TRUE;
346 }
347
348 // Edit the file. Skip this when ":next" already did it.
349 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
350 {
matveyt8e7d9db2022-01-05 14:01:30 +0000351 if (bt_help(wp->w_buffer))
352 {
353 char *curtag = "";
354
355 // A help buffer needs some options to be set.
356 // First, create a new empty buffer with "buftype=help".
357 // Then ":help" will re-use both the buffer and the window and set
358 // the options, even when "options" is not in 'sessionoptions'.
359 if (0 < wp->w_tagstackidx
360 && wp->w_tagstackidx <= wp->w_tagstacklen)
361 curtag = (char *)wp->w_tagstack[wp->w_tagstackidx - 1].tagname;
362
363 if (put_line(fd, "enew | setl bt=help") == FAIL
364 || fprintf(fd, "help %s", curtag) < 0
365 || put_eol(fd) == FAIL)
366 return FAIL;
367 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200368# ifdef FEAT_TERMINAL
matveyt8e7d9db2022-01-05 14:01:30 +0000369 else if (bt_terminal(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200370 {
Bram Moolenaar0e655112020-09-11 20:36:36 +0200371 if (term_write_session(fd, wp, terminal_bufs) == FAIL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200372 return FAIL;
373 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200374# endif
375 // Load the file.
matveyt8e7d9db2022-01-05 14:01:30 +0000376 else if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar84538072019-07-28 14:15:42 +0200377# ifdef FEAT_QUICKFIX
378 && !bt_nofilename(wp->w_buffer)
379# endif
380 )
381 {
382 // Editing a file in this buffer: use ":edit file".
383 // This may have side effects! (e.g., compressed or network file).
384 //
385 // Note, if a buffer for that file already exists, use :badd to
386 // edit that buffer, to not lose folding information (:edit resets
387 // folds in other buffers)
James Cherti7d428402022-03-14 20:24:51 +0000388 if (fputs("if bufexists(fnamemodify(\"", fd) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200389 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
James Cherti7d428402022-03-14 20:24:51 +0000390 || fputs("\", \":p\")) | buffer ", fd) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200391 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
392 || fputs(" | else | edit ", fd) < 0
393 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
394 || fputs(" | endif", fd) < 0
395 || put_eol(fd) == FAIL)
396 return FAIL;
397 }
398 else
399 {
400 // No file in this buffer, just make it empty.
401 if (put_line(fd, "enew") == FAIL)
402 return FAIL;
403#ifdef FEAT_QUICKFIX
404 if (wp->w_buffer->b_ffname != NULL)
405 {
406 // The buffer does have a name, but it's not a file name.
407 if (fputs("file ", fd) < 0
408 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
409 return FAIL;
410 }
411#endif
412 do_cursor = FALSE;
413 }
414 }
415
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100416 if (wp->w_alt_fnum)
417 {
418 buf_T *alt = buflist_findnr(wp->w_alt_fnum);
419
Bram Moolenaar0756f752021-03-13 13:52:33 +0100420 // Set the alternate file if the buffer is listed.
Bram Moolenaar139348f2021-02-05 21:55:53 +0100421 if ((flagp == &ssop_flags)
422 && alt != NULL
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100423 && alt->b_fname != NULL
424 && *alt->b_fname != NUL
Bram Moolenaar0756f752021-03-13 13:52:33 +0100425 && alt->b_p_bl
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100426 && (fputs("balt ", fd) < 0
427 || ses_fname(fd, alt, flagp, TRUE) == FAIL))
428 return FAIL;
429 }
430
Bram Moolenaar84538072019-07-28 14:15:42 +0200431 // Local mappings and abbreviations.
432 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
433 && makemap(fd, wp->w_buffer) == FAIL)
434 return FAIL;
435
436 // Local options. Need to go to the window temporarily.
437 // Store only local values when using ":mkview" and when ":mksession" is
438 // used and 'sessionoptions' doesn't include "options".
439 // Some folding options are always stored when "folds" is included,
440 // otherwise the folds would not be restored correctly.
441 save_curwin = curwin;
442 curwin = wp;
443 curbuf = curwin->w_buffer;
444 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
445 f = makeset(fd, OPT_LOCAL,
446 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
447#ifdef FEAT_FOLDING
448 else if (*flagp & SSOP_FOLDS)
449 f = makefoldset(fd);
450#endif
451 else
452 f = OK;
453 curwin = save_curwin;
454 curbuf = curwin->w_buffer;
455 if (f == FAIL)
456 return FAIL;
457
458#ifdef FEAT_FOLDING
459 // Save Folds when 'buftype' is empty and for help files.
460 if ((*flagp & SSOP_FOLDS)
461 && wp->w_buffer->b_ffname != NULL
462 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
463 {
464 if (put_folds(fd, wp) == FAIL)
465 return FAIL;
466 }
467#endif
468
469 // Set the cursor after creating folds, since that moves the cursor.
470 if (do_cursor)
471 {
472
473 // Restore the cursor line in the file and relatively in the
474 // window. Don't use "G", it changes the jumplist.
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200475 if (wp->w_height <= 0)
476 {
477 if (fprintf(fd, "let s:l = %ld", (long)wp->w_cursor.lnum) < 0)
478 return FAIL;
479 }
480 else if (fprintf(fd,
481 "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
Bram Moolenaar84538072019-07-28 14:15:42 +0200482 (long)wp->w_cursor.lnum,
483 (long)(wp->w_cursor.lnum - wp->w_topline),
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200484 (long)wp->w_height / 2, (long)wp->w_height) < 0)
485 return FAIL;
486
487 if (put_eol(fd) == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200488 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
Bram Moolenaar3482be62020-11-27 11:00:38 +0100489 || put_line(fd, "keepjumps exe s:l") == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200490 || put_line(fd, "normal! zt") == FAIL
Bram Moolenaar3482be62020-11-27 11:00:38 +0100491 || fprintf(fd, "keepjumps %ld", (long)wp->w_cursor.lnum) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200492 || put_eol(fd) == FAIL)
493 return FAIL;
494 // Restore the cursor column and left offset when not wrapping.
495 if (wp->w_cursor.col == 0)
496 {
497 if (put_line(fd, "normal! 0") == FAIL)
498 return FAIL;
499 }
500 else
501 {
502 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
503 {
504 if (fprintf(fd,
505 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
506 (long)wp->w_virtcol + 1,
507 (long)(wp->w_virtcol - wp->w_leftcol),
508 (long)wp->w_width / 2, (long)wp->w_width) < 0
509 || put_eol(fd) == FAIL
510 || put_line(fd, "if s:c > 0") == FAIL
511 || fprintf(fd,
512 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
513 (long)wp->w_virtcol + 1) < 0
514 || put_eol(fd) == FAIL
515 || put_line(fd, "else") == FAIL
516 || put_view_curpos(fd, wp, " ") == FAIL
517 || put_line(fd, "endif") == FAIL)
518 return FAIL;
519 }
520 else if (put_view_curpos(fd, wp, "") == FAIL)
521 return FAIL;
522 }
523 }
524
525 // Local directory, if the current flag is not view options or the "curdir"
526 // option is included.
527 if (wp->w_localdir != NULL
528 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
529 {
530 if (fputs("lcd ", fd) < 0
531 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
532 || put_eol(fd) == FAIL)
533 return FAIL;
534 did_lcd = TRUE;
535 }
536
537 return OK;
538}
539
540#ifdef FEAT_EVAL
541 static int
542store_session_globals(FILE *fd)
543{
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200544 hashtab_T *gvht = get_globvar_ht();
Bram Moolenaar84538072019-07-28 14:15:42 +0200545 hashitem_T *hi;
546 dictitem_T *this_var;
547 int todo;
548 char_u *p, *t;
549
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200550 todo = (int)gvht->ht_used;
551 for (hi = gvht->ht_array; todo > 0; ++hi)
Bram Moolenaar84538072019-07-28 14:15:42 +0200552 {
553 if (!HASHITEM_EMPTY(hi))
554 {
555 --todo;
556 this_var = HI2DI(hi);
557 if ((this_var->di_tv.v_type == VAR_NUMBER
558 || this_var->di_tv.v_type == VAR_STRING)
559 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
560 {
561 // Escape special characters with a backslash. Turn a LF and
562 // CR into \n and \r.
563 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
564 (char_u *)"\\\"\n\r");
565 if (p == NULL) // out of memory
566 break;
567 for (t = p; *t != NUL; ++t)
568 if (*t == '\n')
569 *t = 'n';
570 else if (*t == '\r')
571 *t = 'r';
572 if ((fprintf(fd, "let %s = %c%s%c",
573 this_var->di_key,
574 (this_var->di_tv.v_type == VAR_STRING) ? '"'
575 : ' ',
576 p,
577 (this_var->di_tv.v_type == VAR_STRING) ? '"'
578 : ' ') < 0)
579 || put_eol(fd) == FAIL)
580 {
581 vim_free(p);
582 return FAIL;
583 }
584 vim_free(p);
585 }
586#ifdef FEAT_FLOAT
587 else if (this_var->di_tv.v_type == VAR_FLOAT
588 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
589 {
590 float_T f = this_var->di_tv.vval.v_float;
591 int sign = ' ';
592
593 if (f < 0)
594 {
595 f = -f;
596 sign = '-';
597 }
598 if ((fprintf(fd, "let %s = %c%f",
599 this_var->di_key, sign, f) < 0)
600 || put_eol(fd) == FAIL)
601 return FAIL;
602 }
603#endif
604 }
605 }
606 return OK;
607}
608#endif
609
610/*
611 * Write openfile commands for the current buffers to an .exrc file.
612 * Return FAIL on error, OK otherwise.
613 */
614 static int
615makeopens(
616 FILE *fd,
617 char_u *dirnow) // Current directory name
618{
619 buf_T *buf;
620 int only_save_windows = TRUE;
621 int nr;
622 int restore_size = TRUE;
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100623 int restore_height_width = FALSE;
Bram Moolenaar84538072019-07-28 14:15:42 +0200624 win_T *wp;
625 char_u *sname;
626 win_T *edited_win = NULL;
Bram Moolenaar84538072019-07-28 14:15:42 +0200627 int restore_stal = FALSE;
628 win_T *tab_firstwin;
629 frame_T *tab_topframe;
630 int cur_arg_idx = 0;
631 int next_arg_idx = 0;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200632 int ret = FAIL;
LemonBoyd7c95642022-04-30 16:10:27 +0100633 tabpage_T *tp;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200634#ifdef FEAT_TERMINAL
635 hashtab_T terminal_bufs;
636
637 hash_init(&terminal_bufs);
638#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200639
640 if (ssop_flags & SSOP_BUFFERS)
641 only_save_windows = FALSE; // Save ALL buffers
642
643 // Begin by setting the this_session variable, and then other
644 // sessionable variables.
645#ifdef FEAT_EVAL
646 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200647 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200648 if (ssop_flags & SSOP_GLOBALS)
649 if (store_session_globals(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200650 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200651#endif
652
653 // Close all windows and tabs but one.
654 if (put_line(fd, "silent only") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200655 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200656 if ((ssop_flags & SSOP_TABPAGES)
657 && put_line(fd, "silent tabonly") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200658 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200659
660 // Now a :cd command to the session directory or the current directory
661 if (ssop_flags & SSOP_SESDIR)
662 {
663 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
664 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200665 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200666 }
667 else if (ssop_flags & SSOP_CURDIR)
668 {
669 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
670 if (sname == NULL
671 || fputs("cd ", fd) < 0
672 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
673 || put_eol(fd) == FAIL)
674 {
675 vim_free(sname);
Bram Moolenaar0e655112020-09-11 20:36:36 +0200676 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200677 }
678 vim_free(sname);
679 }
680
681 // If there is an empty, unnamed buffer we will wipe it out later.
682 // Remember the buffer number.
683 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200684 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200685 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200686 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200687 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200688 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200689
James Chertifd012802022-03-29 12:02:57 +0100690 // save 'shortmess' if not storing options
691 if ((ssop_flags & SSOP_OPTIONS) == 0
692 && put_line(fd, "let s:shortmess_save = &shortmess") == FAIL)
693 goto fail;
694
Bram Moolenaar84538072019-07-28 14:15:42 +0200695 // Now save the current files, current buffer first.
696 if (put_line(fd, "set shortmess=aoO") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200697 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200698
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000699 // Put all buffers into the buffer list.
700 // Do it very early to preserve buffer order after loading session (which
701 // can be disrupted by prior `edit` or `tabedit` calls).
702 FOR_ALL_BUFFERS(buf)
703 {
704 if (!(only_save_windows && buf->b_nwindows == 0)
705 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
706#ifdef FEAT_TERMINAL
707 // Skip terminal buffers: finished ones are not useful, others
708 // will be resurrected and result in a new buffer.
709 && !bt_terminal(buf)
710#endif
711 && buf->b_fname != NULL
712 && buf->b_p_bl)
713 {
714 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
715 : buf->b_wininfo->wi_fpos.lnum) < 0
716 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
717 goto fail;
718 }
719 }
720
Bram Moolenaar84538072019-07-28 14:15:42 +0200721 // the global argument list
722 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
723 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200724 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200725
726 if (ssop_flags & SSOP_RESIZE)
727 {
728 // Note: after the restore we still check it worked!
729 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
730 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200731 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200732 }
733
734#ifdef FEAT_GUI
735 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
736 {
737 int x, y;
738
739 if (gui_mch_get_winpos(&x, &y) == OK)
740 {
741 // Note: after the restore we still check it worked!
742 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200743 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200744 }
745 }
746#endif
747
748 // When there are two or more tabpages and 'showtabline' is 1 the tabline
749 // will be displayed when creating the next tab. That resizes the windows
750 // in the first tab, which may cause problems. Set 'showtabline' to 2
751 // temporarily to avoid that.
752 if (p_stal == 1 && first_tabpage->tp_next != NULL)
753 {
754 if (put_line(fd, "set stal=2") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200755 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200756 restore_stal = TRUE;
757 }
758
Bram Moolenaar84538072019-07-28 14:15:42 +0200759 if ((ssop_flags & SSOP_TABPAGES))
760 {
LemonBoyd7c95642022-04-30 16:10:27 +0100761 // "tabpages" is in 'sessionoptions': Similar to ses_win_rec() below,
762 // populate the tab pages first so later local options won't be copied
763 // to the new tabs.
Bram Moolenaar84538072019-07-28 14:15:42 +0200764 FOR_ALL_TABPAGES(tp)
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000765 // Use `bufhidden=wipe` to remove empty "placeholder" buffers once
766 // they are not needed. This prevents creating extra buffers (see
767 // cause of patch 8.1.0829)
768 if (tp->tp_next != NULL
769 && put_line(fd, "tabnew +setlocal\\ bufhidden=wipe") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200770 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200771 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200772 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200773 }
LemonBoyd7c95642022-04-30 16:10:27 +0100774
775 // Assume "tabpages" is in 'sessionoptions'. If not then we only do
776 // "curtab" and bail out of the loop.
777 FOR_ALL_TABPAGES(tp)
Bram Moolenaar84538072019-07-28 14:15:42 +0200778 {
Bram Moolenaar84538072019-07-28 14:15:42 +0200779 int need_tabnext = FALSE;
780 int cnr = 1;
781
LemonBoyd7c95642022-04-30 16:10:27 +0100782 // May repeat putting Windows for each tab, when "tabpages" is in
783 // 'sessionoptions'.
784 // Don't use goto_tabpage(), it may change directory and trigger
785 // autocommands.
Bram Moolenaar84538072019-07-28 14:15:42 +0200786 if ((ssop_flags & SSOP_TABPAGES))
787 {
Bram Moolenaar84538072019-07-28 14:15:42 +0200788 if (tp == curtab)
789 {
790 tab_firstwin = firstwin;
791 tab_topframe = topframe;
792 }
793 else
794 {
795 tab_firstwin = tp->tp_firstwin;
796 tab_topframe = tp->tp_topframe;
797 }
LemonBoyd7c95642022-04-30 16:10:27 +0100798 if (tp != first_tabpage)
Bram Moolenaar84538072019-07-28 14:15:42 +0200799 need_tabnext = TRUE;
800 }
LemonBoyd7c95642022-04-30 16:10:27 +0100801 else
802 {
803 tp = curtab;
804 tab_firstwin = firstwin;
805 tab_topframe = topframe;
806 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200807
808 // Before creating the window layout, try loading one file. If this
809 // is aborted we don't end up with a number of useless windows.
810 // This may have side effects! (e.g., compressed or network file).
811 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
812 {
813 if (ses_do_win(wp)
814 && wp->w_buffer->b_ffname != NULL
815 && !bt_help(wp->w_buffer)
816#ifdef FEAT_QUICKFIX
817 && !bt_nofilename(wp->w_buffer)
818#endif
819 )
820 {
821 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200822 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200823 need_tabnext = FALSE;
824
825 if (fputs("edit ", fd) < 0
826 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
827 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200828 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200829 if (!wp->w_arg_idx_invalid)
830 edited_win = wp;
831 break;
832 }
833 }
834
835 // If no file got edited create an empty tab page.
836 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200837 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200838
Bram Moolenaar0995c812021-04-17 18:38:54 +0200839 if (tab_topframe->fr_layout != FR_LEAF)
840 {
841 // Save current window layout.
842 if (put_line(fd, "let s:save_splitbelow = &splitbelow") == FAIL
843 || put_line(fd, "let s:save_splitright = &splitright")
844 == FAIL)
845 goto fail;
846 if (put_line(fd, "set splitbelow splitright") == FAIL)
847 goto fail;
848 if (ses_win_rec(fd, tab_topframe) == FAIL)
849 goto fail;
850 if (put_line(fd, "let &splitbelow = s:save_splitbelow") == FAIL
851 || put_line(fd, "let &splitright = s:save_splitright")
852 == FAIL)
853 goto fail;
854 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200855
856 // Check if window sizes can be restored (no windows omitted).
857 // Remember the window number of the current window after restoring.
858 nr = 0;
859 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
860 {
861 if (ses_do_win(wp))
862 ++nr;
863 else
864 restore_size = FALSE;
865 if (curwin == wp)
866 cnr = nr;
867 }
868
Bram Moolenaar0995c812021-04-17 18:38:54 +0200869 if (tab_firstwin->w_next != NULL)
870 {
871 // Go to the first window.
872 if (put_line(fd, "wincmd t") == FAIL)
873 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200874
Bram Moolenaar0995c812021-04-17 18:38:54 +0200875 // If more than one window, see if sizes can be restored.
876 // First set 'winheight' and 'winwidth' to 1 to avoid the windows
877 // being resized when moving between windows.
878 // Do this before restoring the view, so that the topline and the
879 // cursor can be set. This is done again below.
880 // winminheight and winminwidth need to be set to avoid an error if
881 // the user has set winheight or winwidth.
882 if (put_line(fd, "let s:save_winminheight = &winminheight") == FAIL
883 || put_line(fd, "let s:save_winminwidth = &winminwidth")
884 == FAIL)
885 goto fail;
886 if (put_line(fd, "set winminheight=0") == FAIL
887 || put_line(fd, "set winheight=1") == FAIL
888 || put_line(fd, "set winminwidth=0") == FAIL
889 || put_line(fd, "set winwidth=1") == FAIL)
890 goto fail;
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100891 restore_height_width = TRUE;
Bram Moolenaar0995c812021-04-17 18:38:54 +0200892 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200893 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200894 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200895
896 // Restore the tab-local working directory if specified
897 // Do this before the windows, so that the window-local directory can
898 // override the tab-local directory.
LemonBoyd7c95642022-04-30 16:10:27 +0100899 if ((ssop_flags & SSOP_CURDIR) && tp->tp_localdir != NULL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200900 {
901 if (fputs("tcd ", fd) < 0
LemonBoyd7c95642022-04-30 16:10:27 +0100902 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
903 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200904 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200905 did_lcd = TRUE;
906 }
907
908 // Restore the view of the window (options, file, cursor, etc.).
909 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
910 {
911 if (!ses_do_win(wp))
912 continue;
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200913 if (put_view(fd, wp, wp != edited_win, &ssop_flags, cur_arg_idx,
Bram Moolenaar0e655112020-09-11 20:36:36 +0200914#ifdef FEAT_TERMINAL
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200915 &terminal_bufs
916#else
917 NULL
Bram Moolenaar0e655112020-09-11 20:36:36 +0200918#endif
919 ) == FAIL)
920 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200921 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200922 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200923 next_arg_idx = wp->w_arg_idx;
924 }
925
926 // The argument index in the first tab page is zero, need to set it in
927 // each window. For further tab pages it's the window where we do
928 // "tabedit".
929 cur_arg_idx = next_arg_idx;
930
931 // Restore cursor to the current window if it's not the first one.
932 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
933 || put_eol(fd) == FAIL))
Bram Moolenaar0e655112020-09-11 20:36:36 +0200934 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200935
936 // Restore window sizes again after jumping around in windows, because
937 // the current window has a minimum size while others may not.
938 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200939 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200940
941 // Don't continue in another tab page when doing only the current one
942 // or when at the last tab page.
943 if (!(ssop_flags & SSOP_TABPAGES))
944 break;
945 }
946
947 if (ssop_flags & SSOP_TABPAGES)
948 {
949 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
950 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200951 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200952 }
953 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200954 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200955
Bram Moolenaar84538072019-07-28 14:15:42 +0200956 // Wipe out an empty unnamed buffer we started in.
957 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
958 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200959 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200960 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200961 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200962 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200963 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200964 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200965 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200966
James Chertifd012802022-03-29 12:02:57 +0100967 // Re-apply 'winheight' and 'winwidth'.
968 if (fprintf(fd, "set winheight=%ld winwidth=%ld",
969 p_wh, p_wiw) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200970 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100971
972 // Restore 'shortmess'.
973 if (ssop_flags & SSOP_OPTIONS)
974 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100975 if (fprintf(fd, "set shortmess=%s", p_shm) < 0 || put_eol(fd) == FAIL)
976 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100977 }
978 else
979 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100980 if (put_line(fd, "let &shortmess = s:shortmess_save") == FAIL)
981 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100982 }
983
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100984 if (restore_height_width)
Bram Moolenaar0995c812021-04-17 18:38:54 +0200985 {
986 // Restore 'winminheight' and 'winminwidth'.
987 if (put_line(fd, "let &winminheight = s:save_winminheight") == FAIL
988 || put_line(fd, "let &winminwidth = s:save_winminwidth") == FAIL)
989 goto fail;
990 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200991
992 // Lastly, execute the x.vim file if it exists.
993 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
Bram Moolenaarc5f33db2020-04-16 21:04:41 +0200994 || put_line(fd, "if filereadable(s:sx)") == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200995 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
996 || put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200997 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200998
Bram Moolenaar0e655112020-09-11 20:36:36 +0200999 ret = OK;
1000fail:
1001#ifdef FEAT_TERMINAL
1002 hash_clear_all(&terminal_bufs, 0);
1003#endif
1004 return ret;
Bram Moolenaar84538072019-07-28 14:15:42 +02001005}
1006
1007/*
1008 * Get the name of the view file for the current buffer.
1009 */
1010 static char_u *
1011get_view_file(int c)
1012{
1013 int len = 0;
1014 char_u *p, *s;
1015 char_u *retval;
1016 char_u *sname;
1017
1018 if (curbuf->b_ffname == NULL)
1019 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001020 emsg(_(e_no_file_name));
Bram Moolenaar84538072019-07-28 14:15:42 +02001021 return NULL;
1022 }
1023 sname = home_replace_save(NULL, curbuf->b_ffname);
1024 if (sname == NULL)
1025 return NULL;
1026
1027 // We want a file name without separators, because we're not going to make
1028 // a directory.
1029 // "normal" path separator -> "=+"
1030 // "=" -> "=="
1031 // ":" path separator -> "=-"
1032 for (p = sname; *p; ++p)
1033 if (*p == '=' || vim_ispathsep(*p))
1034 ++len;
1035 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
1036 if (retval != NULL)
1037 {
1038 STRCPY(retval, p_vdir);
1039 add_pathsep(retval);
1040 s = retval + STRLEN(retval);
1041 for (p = sname; *p; ++p)
1042 {
1043 if (*p == '=')
1044 {
1045 *s++ = '=';
1046 *s++ = '=';
1047 }
1048 else if (vim_ispathsep(*p))
1049 {
1050 *s++ = '=';
1051#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
1052 if (*p == ':')
1053 *s++ = '-';
1054 else
1055#endif
1056 *s++ = '+';
1057 }
1058 else
1059 *s++ = *p;
1060 }
1061 *s++ = '=';
1062 *s++ = c;
1063 STRCPY(s, ".vim");
1064 }
1065
1066 vim_free(sname);
1067 return retval;
1068}
1069
1070/*
1071 * ":loadview [nr]"
1072 */
1073 void
1074ex_loadview(exarg_T *eap)
1075{
1076 char_u *fname;
1077
1078 fname = get_view_file(*eap->arg);
1079 if (fname != NULL)
1080 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 do_source(fname, FALSE, DOSO_NONE, NULL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001082 vim_free(fname);
1083 }
1084}
1085
Bram Moolenaard17a57a2019-09-29 20:53:55 +02001086# if defined(FEAT_GUI_GNOME) \
Bram Moolenaarac02a632019-09-29 19:02:46 +02001087 || (defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)) \
1088 || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001089/*
1090 * Generate a script that can be used to restore the current editing session.
1091 * Save the value of v:this_session before running :mksession in order to make
1092 * automagic session save fully transparent. Return TRUE on success.
1093 */
1094 int
1095write_session_file(char_u *filename)
1096{
1097 char_u *escaped_filename;
1098 char *mksession_cmdline;
1099 unsigned int save_ssop_flags;
1100 int failed;
1101
1102 // Build an ex command line to create a script that restores the current
1103 // session if executed. Escape the filename to avoid nasty surprises.
1104 escaped_filename = vim_strsave_escaped(filename, escape_chars);
1105 if (escaped_filename == NULL)
1106 return FALSE;
1107 mksession_cmdline = alloc(10 + (int)STRLEN(escaped_filename) + 1);
1108 if (mksession_cmdline == NULL)
1109 {
1110 vim_free(escaped_filename);
1111 return FALSE;
1112 }
1113 strcpy(mksession_cmdline, "mksession ");
1114 STRCAT(mksession_cmdline, escaped_filename);
1115 vim_free(escaped_filename);
1116
1117 // Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
1118 // unpredictable effects when the session is saved automatically. Also,
1119 // we definitely need SSOP_GLOBALS to be able to restore v:this_session.
1120 // Don't use SSOP_BUFFERS to prevent the buffer list from becoming
1121 // enormously large if the GNOME session feature is used regularly.
1122 save_ssop_flags = ssop_flags;
1123 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
1124 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
1125
1126 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
1127 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
1128 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
1129 do_unlet((char_u *)"Save_VV_this_session", TRUE);
1130
1131 ssop_flags = save_ssop_flags;
1132 vim_free(mksession_cmdline);
1133
1134 // Reopen the file and append a command to restore v:this_session,
1135 // as if this save never happened. This is to avoid conflicts with
1136 // the user's own sessions. FIXME: It's probably less hackish to add
1137 // a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
1138 if (!failed)
1139 {
1140 FILE *fd;
1141
1142 fd = open_exfile(filename, TRUE, APPENDBIN);
1143
1144 failed = (fd == NULL
1145 || put_line(fd, "let v:this_session = Save_VV_this_session")
1146 == FAIL
1147 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
1148
1149 if (fd != NULL && fclose(fd) != 0)
1150 failed = TRUE;
1151
1152 if (failed)
1153 mch_remove(filename);
1154 }
1155
1156 return !failed;
1157}
1158# endif
1159
1160#endif // FEAT_SESSION
1161
1162#if defined(FEAT_SESSION) && defined(USE_CRNL)
1163# define MKSESSION_NL
1164static int mksession_nl = FALSE; // use NL only in put_eol()
1165#endif
1166
Bram Moolenaar84538072019-07-28 14:15:42 +02001167/*
1168 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
1169 */
1170 void
1171ex_mkrc(exarg_T *eap)
1172{
1173 FILE *fd;
1174 int failed = FALSE;
1175 char_u *fname;
1176#ifdef FEAT_BROWSE
1177 char_u *browseFile = NULL;
1178#endif
1179#ifdef FEAT_SESSION
1180 int view_session = FALSE;
1181 int using_vdir = FALSE; // using 'viewdir'?
1182 char_u *viewFile = NULL;
1183 unsigned *flagp;
1184#endif
1185
1186 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
1187 {
1188#ifdef FEAT_SESSION
1189 view_session = TRUE;
1190#else
1191 ex_ni(eap);
1192 return;
1193#endif
1194 }
1195
1196#ifdef FEAT_SESSION
1197 // Use the short file name until ":lcd" is used. We also don't use the
1198 // short file name when 'acd' is set, that is checked later.
1199 did_lcd = FALSE;
1200
1201 // ":mkview" or ":mkview 9": generate file name with 'viewdir'
1202 if (eap->cmdidx == CMD_mkview
1203 && (*eap->arg == NUL
1204 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
1205 {
1206 eap->forceit = TRUE;
1207 fname = get_view_file(*eap->arg);
1208 if (fname == NULL)
1209 return;
1210 viewFile = fname;
1211 using_vdir = TRUE;
1212 }
1213 else
1214#endif
1215 if (*eap->arg != NUL)
1216 fname = eap->arg;
1217 else if (eap->cmdidx == CMD_mkvimrc)
1218 fname = (char_u *)VIMRC_FILE;
1219#ifdef FEAT_SESSION
1220 else if (eap->cmdidx == CMD_mksession)
1221 fname = (char_u *)SESSION_FILE;
1222#endif
1223 else
1224 fname = (char_u *)EXRC_FILE;
1225
1226#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001227 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar84538072019-07-28 14:15:42 +02001228 {
1229 browseFile = do_browse(BROWSE_SAVE,
1230# ifdef FEAT_SESSION
1231 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
1232 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
1233# endif
1234 (char_u *)_("Save Setup"),
1235 fname, (char_u *)"vim", NULL,
1236 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1237 if (browseFile == NULL)
1238 goto theend;
1239 fname = browseFile;
1240 eap->forceit = TRUE; // since dialog already asked
1241 }
1242#endif
1243
1244#if defined(FEAT_SESSION) && defined(vim_mkdir)
1245 // When using 'viewdir' may have to create the directory.
1246 if (using_vdir && !mch_isdir(p_vdir))
1247 vim_mkdir_emsg(p_vdir, 0755);
1248#endif
1249
1250 fd = open_exfile(fname, eap->forceit, WRITEBIN);
1251 if (fd != NULL)
1252 {
1253#ifdef FEAT_SESSION
1254 if (eap->cmdidx == CMD_mkview)
1255 flagp = &vop_flags;
1256 else
1257 flagp = &ssop_flags;
1258#endif
1259
1260#ifdef MKSESSION_NL
1261 // "unix" in 'sessionoptions': use NL line separator
1262 if (view_session && (*flagp & SSOP_UNIX))
1263 mksession_nl = TRUE;
1264#endif
1265
1266 // Write the version command for :mkvimrc
1267 if (eap->cmdidx == CMD_mkvimrc)
1268 (void)put_line(fd, "version 6.0");
1269
1270#ifdef FEAT_SESSION
1271 if (eap->cmdidx == CMD_mksession)
1272 {
1273 if (put_line(fd, "let SessionLoad = 1") == FAIL)
1274 failed = TRUE;
1275 }
1276
1277 if (eap->cmdidx != CMD_mkview)
1278#endif
1279 {
1280 // Write setting 'compatible' first, because it has side effects.
1281 // For that same reason only do it when needed.
1282 if (p_cp)
1283 (void)put_line(fd, "if !&cp | set cp | endif");
1284 else
1285 (void)put_line(fd, "if &cp | set nocp | endif");
1286 }
1287
1288#ifdef FEAT_SESSION
1289 if (!view_session
1290 || (eap->cmdidx == CMD_mksession
1291 && (*flagp & SSOP_OPTIONS)))
1292#endif
Bram Moolenaar635bd602021-04-16 19:58:22 +02001293 {
1294 int flags = OPT_GLOBAL;
1295
1296#ifdef FEAT_SESSION
1297 if (eap->cmdidx == CMD_mksession && (*flagp & SSOP_SKIP_RTP))
1298 flags |= OPT_SKIPRTP;
1299#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001300 failed |= (makemap(fd, NULL) == FAIL
Bram Moolenaar635bd602021-04-16 19:58:22 +02001301 || makeset(fd, flags, FALSE) == FAIL);
1302 }
Bram Moolenaar84538072019-07-28 14:15:42 +02001303
1304#ifdef FEAT_SESSION
1305 if (!failed && view_session)
1306 {
Bram Moolenaar38890832020-11-01 17:40:54 +01001307 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 +02001308 failed = TRUE;
1309 if (eap->cmdidx == CMD_mksession)
1310 {
1311 char_u *dirnow; // current directory
1312
1313 dirnow = alloc(MAXPATHL);
1314 if (dirnow == NULL)
1315 failed = TRUE;
1316 else
1317 {
1318 // Change to session file's dir.
1319 if (mch_dirname(dirnow, MAXPATHL) == FAIL
1320 || mch_chdir((char *)dirnow) != 0)
1321 *dirnow = NUL;
1322 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
1323 {
1324 if (vim_chdirfile(fname, NULL) == OK)
1325 shorten_fnames(TRUE);
1326 }
1327 else if (*dirnow != NUL
1328 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
1329 {
1330 if (mch_chdir((char *)globaldir) == 0)
1331 shorten_fnames(TRUE);
1332 }
1333
1334 failed |= (makeopens(fd, dirnow) == FAIL);
1335
1336 // restore original dir
1337 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
1338 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
1339 {
1340 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001341 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar84538072019-07-28 14:15:42 +02001342 shorten_fnames(TRUE);
1343 }
1344 vim_free(dirnow);
1345 }
1346 }
1347 else
1348 {
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001349 failed |= (put_view(fd, curwin, !using_vdir, flagp, -1, NULL)
1350 == FAIL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001351 }
Bram Moolenaar38890832020-11-01 17:40:54 +01001352 if (put_line(fd, "let &g:so = s:so_save | let &g:siso = s:siso_save")
Bram Moolenaar84538072019-07-28 14:15:42 +02001353 == FAIL)
1354 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001355#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar84538072019-07-28 14:15:42 +02001356 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
1357 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001358#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001359 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
1360 failed = TRUE;
1361 if (eap->cmdidx == CMD_mksession)
1362 {
1363 if (put_line(fd, "unlet SessionLoad") == FAIL)
1364 failed = TRUE;
1365 }
1366 }
1367#endif
1368 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
1369 failed = TRUE;
1370
1371 failed |= fclose(fd);
1372
1373 if (failed)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001374 emsg(_(e_error_while_writing));
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001375#if defined(FEAT_SESSION)
Bram Moolenaar84538072019-07-28 14:15:42 +02001376 else if (eap->cmdidx == CMD_mksession)
1377 {
1378 // successful session write - set this_session var
1379 char_u *tbuf;
1380
1381 tbuf = alloc(MAXPATHL);
1382 if (tbuf != NULL)
1383 {
1384 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
1385 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
1386 vim_free(tbuf);
1387 }
1388 }
1389#endif
1390#ifdef MKSESSION_NL
1391 mksession_nl = FALSE;
1392#endif
1393 }
1394
1395#ifdef FEAT_BROWSE
1396theend:
1397 vim_free(browseFile);
1398#endif
1399#ifdef FEAT_SESSION
1400 vim_free(viewFile);
1401#endif
1402}
1403
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001404#if (defined(FEAT_VIMINFO) || defined(FEAT_SESSION)) || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001405 var_flavour_T
1406var_flavour(char_u *varname)
1407{
1408 char_u *p = varname;
1409
1410 if (ASCII_ISUPPER(*p))
1411 {
1412 while (*(++p))
1413 if (ASCII_ISLOWER(*p))
1414 return VAR_FLAVOUR_SESSION;
1415 return VAR_FLAVOUR_VIMINFO;
1416 }
1417 else
1418 return VAR_FLAVOUR_DEFAULT;
1419}
1420#endif
1421
1422/*
1423 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
1424 * Return FAIL for a write error.
1425 */
1426 int
1427put_eol(FILE *fd)
1428{
1429 if (
1430#ifdef USE_CRNL
1431 (
1432# ifdef MKSESSION_NL
1433 !mksession_nl &&
1434# endif
1435 (putc('\r', fd) < 0)) ||
1436#endif
1437 (putc('\n', fd) < 0))
1438 return FAIL;
1439 return OK;
1440}
1441
1442/*
1443 * Write a line to "fd".
1444 * Return FAIL for a write error.
1445 */
1446 int
1447put_line(FILE *fd, char *s)
1448{
1449 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
1450 return FAIL;
1451 return OK;
1452}