blob: ddf5d21d117ab8b5c848a8db5bd94882fa4c8a2b [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 }
580#ifdef FEAT_FLOAT
581 else if (this_var->di_tv.v_type == VAR_FLOAT
582 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
583 {
584 float_T f = this_var->di_tv.vval.v_float;
585 int sign = ' ';
586
587 if (f < 0)
588 {
589 f = -f;
590 sign = '-';
591 }
592 if ((fprintf(fd, "let %s = %c%f",
593 this_var->di_key, sign, f) < 0)
594 || put_eol(fd) == FAIL)
595 return FAIL;
596 }
597#endif
598 }
599 }
600 return OK;
601}
602#endif
603
604/*
605 * Write openfile commands for the current buffers to an .exrc file.
606 * Return FAIL on error, OK otherwise.
607 */
608 static int
609makeopens(
610 FILE *fd,
611 char_u *dirnow) // Current directory name
612{
613 buf_T *buf;
614 int only_save_windows = TRUE;
615 int nr;
616 int restore_size = TRUE;
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100617 int restore_height_width = FALSE;
Bram Moolenaar84538072019-07-28 14:15:42 +0200618 win_T *wp;
619 char_u *sname;
620 win_T *edited_win = NULL;
Bram Moolenaar84538072019-07-28 14:15:42 +0200621 int restore_stal = FALSE;
622 win_T *tab_firstwin;
623 frame_T *tab_topframe;
624 int cur_arg_idx = 0;
625 int next_arg_idx = 0;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200626 int ret = FAIL;
LemonBoyd7c95642022-04-30 16:10:27 +0100627 tabpage_T *tp;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200628#ifdef FEAT_TERMINAL
629 hashtab_T terminal_bufs;
630
631 hash_init(&terminal_bufs);
632#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200633
634 if (ssop_flags & SSOP_BUFFERS)
635 only_save_windows = FALSE; // Save ALL buffers
636
637 // Begin by setting the this_session variable, and then other
638 // sessionable variables.
639#ifdef FEAT_EVAL
640 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200641 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200642 if (ssop_flags & SSOP_GLOBALS)
643 if (store_session_globals(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200644 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200645#endif
646
647 // Close all windows and tabs but one.
648 if (put_line(fd, "silent only") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200649 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200650 if ((ssop_flags & SSOP_TABPAGES)
651 && put_line(fd, "silent tabonly") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200652 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200653
654 // Now a :cd command to the session directory or the current directory
655 if (ssop_flags & SSOP_SESDIR)
656 {
657 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
658 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200659 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200660 }
661 else if (ssop_flags & SSOP_CURDIR)
662 {
663 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
664 if (sname == NULL
665 || fputs("cd ", fd) < 0
666 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
667 || put_eol(fd) == FAIL)
668 {
669 vim_free(sname);
Bram Moolenaar0e655112020-09-11 20:36:36 +0200670 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200671 }
672 vim_free(sname);
673 }
674
675 // If there is an empty, unnamed buffer we will wipe it out later.
676 // Remember the buffer number.
677 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200678 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200679 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200680 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200681 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200682 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200683
James Chertifd012802022-03-29 12:02:57 +0100684 // save 'shortmess' if not storing options
685 if ((ssop_flags & SSOP_OPTIONS) == 0
686 && put_line(fd, "let s:shortmess_save = &shortmess") == FAIL)
687 goto fail;
688
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +0100689 // set 'shortmess' for the following. Add the 'A' flag if it was there
690 if (put_line(fd, "if &shortmess =~ 'A'") == FAIL
691 || put_line(fd, " set shortmess=aoOA") == FAIL
692 || put_line(fd, "else") == FAIL
693 || put_line(fd, " set shortmess=aoO") == FAIL
694 || put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200695 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200696
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +0100697 // Now save the current files, current buffer first.
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000698 // Put all buffers into the buffer list.
699 // Do it very early to preserve buffer order after loading session (which
700 // can be disrupted by prior `edit` or `tabedit` calls).
701 FOR_ALL_BUFFERS(buf)
702 {
703 if (!(only_save_windows && buf->b_nwindows == 0)
704 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000705 // Skip terminal buffers: finished ones are not useful, others
706 // will be resurrected and result in a new buffer.
707 && !bt_terminal(buf)
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000708 && buf->b_fname != NULL
709 && buf->b_p_bl)
710 {
711 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
712 : buf->b_wininfo->wi_fpos.lnum) < 0
713 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
714 goto fail;
715 }
716 }
717
Bram Moolenaar84538072019-07-28 14:15:42 +0200718 // the global argument list
719 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
720 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200721 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200722
723 if (ssop_flags & SSOP_RESIZE)
724 {
725 // Note: after the restore we still check it worked!
726 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
727 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200728 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200729 }
730
731#ifdef FEAT_GUI
732 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
733 {
734 int x, y;
735
736 if (gui_mch_get_winpos(&x, &y) == OK)
737 {
738 // Note: after the restore we still check it worked!
739 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200740 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200741 }
742 }
743#endif
744
745 // When there are two or more tabpages and 'showtabline' is 1 the tabline
746 // will be displayed when creating the next tab. That resizes the windows
747 // in the first tab, which may cause problems. Set 'showtabline' to 2
748 // temporarily to avoid that.
749 if (p_stal == 1 && first_tabpage->tp_next != NULL)
750 {
751 if (put_line(fd, "set stal=2") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200752 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200753 restore_stal = TRUE;
754 }
755
Bram Moolenaar84538072019-07-28 14:15:42 +0200756 if ((ssop_flags & SSOP_TABPAGES))
757 {
LemonBoyd7c95642022-04-30 16:10:27 +0100758 // "tabpages" is in 'sessionoptions': Similar to ses_win_rec() below,
759 // populate the tab pages first so later local options won't be copied
760 // to the new tabs.
Bram Moolenaar84538072019-07-28 14:15:42 +0200761 FOR_ALL_TABPAGES(tp)
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000762 // Use `bufhidden=wipe` to remove empty "placeholder" buffers once
763 // they are not needed. This prevents creating extra buffers (see
764 // cause of patch 8.1.0829)
765 if (tp->tp_next != NULL
766 && put_line(fd, "tabnew +setlocal\\ bufhidden=wipe") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200767 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200768 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200769 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200770 }
LemonBoyd7c95642022-04-30 16:10:27 +0100771
772 // Assume "tabpages" is in 'sessionoptions'. If not then we only do
773 // "curtab" and bail out of the loop.
774 FOR_ALL_TABPAGES(tp)
Bram Moolenaar84538072019-07-28 14:15:42 +0200775 {
Bram Moolenaar84538072019-07-28 14:15:42 +0200776 int need_tabnext = FALSE;
777 int cnr = 1;
778
LemonBoyd7c95642022-04-30 16:10:27 +0100779 // May repeat putting Windows for each tab, when "tabpages" is in
780 // 'sessionoptions'.
781 // Don't use goto_tabpage(), it may change directory and trigger
782 // autocommands.
Bram Moolenaar84538072019-07-28 14:15:42 +0200783 if ((ssop_flags & SSOP_TABPAGES))
784 {
Bram Moolenaar84538072019-07-28 14:15:42 +0200785 if (tp == curtab)
786 {
787 tab_firstwin = firstwin;
788 tab_topframe = topframe;
789 }
790 else
791 {
792 tab_firstwin = tp->tp_firstwin;
793 tab_topframe = tp->tp_topframe;
794 }
LemonBoyd7c95642022-04-30 16:10:27 +0100795 if (tp != first_tabpage)
Bram Moolenaar84538072019-07-28 14:15:42 +0200796 need_tabnext = TRUE;
797 }
LemonBoyd7c95642022-04-30 16:10:27 +0100798 else
799 {
800 tp = curtab;
801 tab_firstwin = firstwin;
802 tab_topframe = topframe;
803 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200804
805 // Before creating the window layout, try loading one file. If this
806 // is aborted we don't end up with a number of useless windows.
807 // This may have side effects! (e.g., compressed or network file).
808 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
809 {
810 if (ses_do_win(wp)
811 && wp->w_buffer->b_ffname != NULL
812 && !bt_help(wp->w_buffer)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100813 && !bt_nofilename(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200814 {
815 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200816 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200817 need_tabnext = FALSE;
818
819 if (fputs("edit ", fd) < 0
820 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
821 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200822 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200823 if (!wp->w_arg_idx_invalid)
824 edited_win = wp;
825 break;
826 }
827 }
828
829 // If no file got edited create an empty tab page.
830 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200831 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200832
Bram Moolenaar0995c812021-04-17 18:38:54 +0200833 if (tab_topframe->fr_layout != FR_LEAF)
834 {
835 // Save current window layout.
836 if (put_line(fd, "let s:save_splitbelow = &splitbelow") == FAIL
837 || put_line(fd, "let s:save_splitright = &splitright")
838 == FAIL)
839 goto fail;
840 if (put_line(fd, "set splitbelow splitright") == FAIL)
841 goto fail;
842 if (ses_win_rec(fd, tab_topframe) == FAIL)
843 goto fail;
844 if (put_line(fd, "let &splitbelow = s:save_splitbelow") == FAIL
845 || put_line(fd, "let &splitright = s:save_splitright")
846 == FAIL)
847 goto fail;
848 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200849
850 // Check if window sizes can be restored (no windows omitted).
851 // Remember the window number of the current window after restoring.
852 nr = 0;
853 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
854 {
855 if (ses_do_win(wp))
856 ++nr;
857 else
858 restore_size = FALSE;
859 if (curwin == wp)
860 cnr = nr;
861 }
862
Bram Moolenaar0995c812021-04-17 18:38:54 +0200863 if (tab_firstwin->w_next != NULL)
864 {
865 // Go to the first window.
866 if (put_line(fd, "wincmd t") == FAIL)
867 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200868
Bram Moolenaar0995c812021-04-17 18:38:54 +0200869 // If more than one window, see if sizes can be restored.
870 // First set 'winheight' and 'winwidth' to 1 to avoid the windows
871 // being resized when moving between windows.
872 // Do this before restoring the view, so that the topline and the
873 // cursor can be set. This is done again below.
874 // winminheight and winminwidth need to be set to avoid an error if
875 // the user has set winheight or winwidth.
876 if (put_line(fd, "let s:save_winminheight = &winminheight") == FAIL
877 || put_line(fd, "let s:save_winminwidth = &winminwidth")
878 == FAIL)
879 goto fail;
880 if (put_line(fd, "set winminheight=0") == FAIL
881 || put_line(fd, "set winheight=1") == FAIL
882 || put_line(fd, "set winminwidth=0") == FAIL
883 || put_line(fd, "set winwidth=1") == FAIL)
884 goto fail;
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100885 restore_height_width = TRUE;
Bram Moolenaar0995c812021-04-17 18:38:54 +0200886 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200887 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200888 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200889
890 // Restore the tab-local working directory if specified
891 // Do this before the windows, so that the window-local directory can
892 // override the tab-local directory.
LemonBoyd7c95642022-04-30 16:10:27 +0100893 if ((ssop_flags & SSOP_CURDIR) && tp->tp_localdir != NULL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200894 {
895 if (fputs("tcd ", fd) < 0
LemonBoyd7c95642022-04-30 16:10:27 +0100896 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
897 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200898 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200899 did_lcd = TRUE;
900 }
901
902 // Restore the view of the window (options, file, cursor, etc.).
903 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
904 {
905 if (!ses_do_win(wp))
906 continue;
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200907 if (put_view(fd, wp, wp != edited_win, &ssop_flags, cur_arg_idx,
Bram Moolenaar0e655112020-09-11 20:36:36 +0200908#ifdef FEAT_TERMINAL
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200909 &terminal_bufs
910#else
911 NULL
Bram Moolenaar0e655112020-09-11 20:36:36 +0200912#endif
913 ) == FAIL)
914 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200915 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200916 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200917 next_arg_idx = wp->w_arg_idx;
918 }
919
920 // The argument index in the first tab page is zero, need to set it in
921 // each window. For further tab pages it's the window where we do
922 // "tabedit".
923 cur_arg_idx = next_arg_idx;
924
925 // Restore cursor to the current window if it's not the first one.
926 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
927 || put_eol(fd) == FAIL))
Bram Moolenaar0e655112020-09-11 20:36:36 +0200928 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200929
930 // Restore window sizes again after jumping around in windows, because
931 // the current window has a minimum size while others may not.
932 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200933 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200934
935 // Don't continue in another tab page when doing only the current one
936 // or when at the last tab page.
937 if (!(ssop_flags & SSOP_TABPAGES))
938 break;
939 }
940
941 if (ssop_flags & SSOP_TABPAGES)
942 {
943 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
944 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200945 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200946 }
947 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200948 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200949
Bram Moolenaar84538072019-07-28 14:15:42 +0200950 // Wipe out an empty unnamed buffer we started in.
951 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
952 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200953 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200954 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200955 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200956 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200957 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200958 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200959 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200960
James Chertifd012802022-03-29 12:02:57 +0100961 // Re-apply 'winheight' and 'winwidth'.
962 if (fprintf(fd, "set winheight=%ld winwidth=%ld",
963 p_wh, p_wiw) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200964 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100965
966 // Restore 'shortmess'.
967 if (ssop_flags & SSOP_OPTIONS)
968 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100969 if (fprintf(fd, "set shortmess=%s", p_shm) < 0 || put_eol(fd) == FAIL)
970 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100971 }
972 else
973 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100974 if (put_line(fd, "let &shortmess = s:shortmess_save") == FAIL)
975 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100976 }
977
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100978 if (restore_height_width)
Bram Moolenaar0995c812021-04-17 18:38:54 +0200979 {
980 // Restore 'winminheight' and 'winminwidth'.
981 if (put_line(fd, "let &winminheight = s:save_winminheight") == FAIL
982 || put_line(fd, "let &winminwidth = s:save_winminwidth") == FAIL)
983 goto fail;
984 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200985
986 // Lastly, execute the x.vim file if it exists.
987 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
Bram Moolenaarc5f33db2020-04-16 21:04:41 +0200988 || put_line(fd, "if filereadable(s:sx)") == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200989 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
990 || put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200991 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200992
Bram Moolenaar0e655112020-09-11 20:36:36 +0200993 ret = OK;
994fail:
995#ifdef FEAT_TERMINAL
996 hash_clear_all(&terminal_bufs, 0);
997#endif
998 return ret;
Bram Moolenaar84538072019-07-28 14:15:42 +0200999}
1000
1001/*
1002 * Get the name of the view file for the current buffer.
1003 */
1004 static char_u *
1005get_view_file(int c)
1006{
1007 int len = 0;
1008 char_u *p, *s;
1009 char_u *retval;
1010 char_u *sname;
1011
1012 if (curbuf->b_ffname == NULL)
1013 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001014 emsg(_(e_no_file_name));
Bram Moolenaar84538072019-07-28 14:15:42 +02001015 return NULL;
1016 }
1017 sname = home_replace_save(NULL, curbuf->b_ffname);
1018 if (sname == NULL)
1019 return NULL;
1020
1021 // We want a file name without separators, because we're not going to make
1022 // a directory.
1023 // "normal" path separator -> "=+"
1024 // "=" -> "=="
1025 // ":" path separator -> "=-"
1026 for (p = sname; *p; ++p)
1027 if (*p == '=' || vim_ispathsep(*p))
1028 ++len;
1029 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
1030 if (retval != NULL)
1031 {
1032 STRCPY(retval, p_vdir);
1033 add_pathsep(retval);
1034 s = retval + STRLEN(retval);
1035 for (p = sname; *p; ++p)
1036 {
1037 if (*p == '=')
1038 {
1039 *s++ = '=';
1040 *s++ = '=';
1041 }
1042 else if (vim_ispathsep(*p))
1043 {
1044 *s++ = '=';
1045#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
1046 if (*p == ':')
1047 *s++ = '-';
1048 else
1049#endif
1050 *s++ = '+';
1051 }
1052 else
1053 *s++ = *p;
1054 }
1055 *s++ = '=';
1056 *s++ = c;
1057 STRCPY(s, ".vim");
1058 }
1059
1060 vim_free(sname);
1061 return retval;
1062}
1063
1064/*
1065 * ":loadview [nr]"
1066 */
1067 void
1068ex_loadview(exarg_T *eap)
1069{
1070 char_u *fname;
1071
1072 fname = get_view_file(*eap->arg);
1073 if (fname != NULL)
1074 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 do_source(fname, FALSE, DOSO_NONE, NULL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001076 vim_free(fname);
1077 }
1078}
1079
Bram Moolenaard17a57a2019-09-29 20:53:55 +02001080# if defined(FEAT_GUI_GNOME) \
Bram Moolenaarac02a632019-09-29 19:02:46 +02001081 || (defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)) \
1082 || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001083/*
1084 * Generate a script that can be used to restore the current editing session.
1085 * Save the value of v:this_session before running :mksession in order to make
1086 * automagic session save fully transparent. Return TRUE on success.
1087 */
1088 int
1089write_session_file(char_u *filename)
1090{
1091 char_u *escaped_filename;
1092 char *mksession_cmdline;
1093 unsigned int save_ssop_flags;
1094 int failed;
1095
1096 // Build an ex command line to create a script that restores the current
1097 // session if executed. Escape the filename to avoid nasty surprises.
1098 escaped_filename = vim_strsave_escaped(filename, escape_chars);
1099 if (escaped_filename == NULL)
1100 return FALSE;
1101 mksession_cmdline = alloc(10 + (int)STRLEN(escaped_filename) + 1);
1102 if (mksession_cmdline == NULL)
1103 {
1104 vim_free(escaped_filename);
1105 return FALSE;
1106 }
1107 strcpy(mksession_cmdline, "mksession ");
1108 STRCAT(mksession_cmdline, escaped_filename);
1109 vim_free(escaped_filename);
1110
1111 // Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
1112 // unpredictable effects when the session is saved automatically. Also,
1113 // we definitely need SSOP_GLOBALS to be able to restore v:this_session.
1114 // Don't use SSOP_BUFFERS to prevent the buffer list from becoming
1115 // enormously large if the GNOME session feature is used regularly.
1116 save_ssop_flags = ssop_flags;
1117 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
1118 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
1119
1120 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
1121 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
1122 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
1123 do_unlet((char_u *)"Save_VV_this_session", TRUE);
1124
1125 ssop_flags = save_ssop_flags;
1126 vim_free(mksession_cmdline);
1127
1128 // Reopen the file and append a command to restore v:this_session,
1129 // as if this save never happened. This is to avoid conflicts with
1130 // the user's own sessions. FIXME: It's probably less hackish to add
1131 // a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
1132 if (!failed)
1133 {
1134 FILE *fd;
1135
1136 fd = open_exfile(filename, TRUE, APPENDBIN);
1137
1138 failed = (fd == NULL
1139 || put_line(fd, "let v:this_session = Save_VV_this_session")
1140 == FAIL
1141 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
1142
1143 if (fd != NULL && fclose(fd) != 0)
1144 failed = TRUE;
1145
1146 if (failed)
1147 mch_remove(filename);
1148 }
1149
1150 return !failed;
1151}
1152# endif
1153
1154#endif // FEAT_SESSION
1155
1156#if defined(FEAT_SESSION) && defined(USE_CRNL)
1157# define MKSESSION_NL
1158static int mksession_nl = FALSE; // use NL only in put_eol()
1159#endif
1160
Bram Moolenaar84538072019-07-28 14:15:42 +02001161/*
1162 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
1163 */
1164 void
1165ex_mkrc(exarg_T *eap)
1166{
1167 FILE *fd;
1168 int failed = FALSE;
1169 char_u *fname;
1170#ifdef FEAT_BROWSE
1171 char_u *browseFile = NULL;
1172#endif
1173#ifdef FEAT_SESSION
1174 int view_session = FALSE;
1175 int using_vdir = FALSE; // using 'viewdir'?
1176 char_u *viewFile = NULL;
1177 unsigned *flagp;
1178#endif
1179
1180 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
1181 {
1182#ifdef FEAT_SESSION
1183 view_session = TRUE;
1184#else
1185 ex_ni(eap);
1186 return;
1187#endif
1188 }
1189
1190#ifdef FEAT_SESSION
1191 // Use the short file name until ":lcd" is used. We also don't use the
1192 // short file name when 'acd' is set, that is checked later.
1193 did_lcd = FALSE;
1194
1195 // ":mkview" or ":mkview 9": generate file name with 'viewdir'
1196 if (eap->cmdidx == CMD_mkview
1197 && (*eap->arg == NUL
1198 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
1199 {
1200 eap->forceit = TRUE;
1201 fname = get_view_file(*eap->arg);
1202 if (fname == NULL)
1203 return;
1204 viewFile = fname;
1205 using_vdir = TRUE;
1206 }
1207 else
1208#endif
1209 if (*eap->arg != NUL)
1210 fname = eap->arg;
1211 else if (eap->cmdidx == CMD_mkvimrc)
1212 fname = (char_u *)VIMRC_FILE;
1213#ifdef FEAT_SESSION
1214 else if (eap->cmdidx == CMD_mksession)
1215 fname = (char_u *)SESSION_FILE;
1216#endif
1217 else
1218 fname = (char_u *)EXRC_FILE;
1219
1220#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001221 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar84538072019-07-28 14:15:42 +02001222 {
1223 browseFile = do_browse(BROWSE_SAVE,
1224# ifdef FEAT_SESSION
1225 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
1226 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
1227# endif
1228 (char_u *)_("Save Setup"),
1229 fname, (char_u *)"vim", NULL,
1230 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1231 if (browseFile == NULL)
1232 goto theend;
1233 fname = browseFile;
1234 eap->forceit = TRUE; // since dialog already asked
1235 }
1236#endif
1237
1238#if defined(FEAT_SESSION) && defined(vim_mkdir)
1239 // When using 'viewdir' may have to create the directory.
1240 if (using_vdir && !mch_isdir(p_vdir))
1241 vim_mkdir_emsg(p_vdir, 0755);
1242#endif
1243
1244 fd = open_exfile(fname, eap->forceit, WRITEBIN);
1245 if (fd != NULL)
1246 {
1247#ifdef FEAT_SESSION
1248 if (eap->cmdidx == CMD_mkview)
1249 flagp = &vop_flags;
1250 else
1251 flagp = &ssop_flags;
1252#endif
1253
1254#ifdef MKSESSION_NL
1255 // "unix" in 'sessionoptions': use NL line separator
1256 if (view_session && (*flagp & SSOP_UNIX))
1257 mksession_nl = TRUE;
1258#endif
1259
1260 // Write the version command for :mkvimrc
1261 if (eap->cmdidx == CMD_mkvimrc)
1262 (void)put_line(fd, "version 6.0");
1263
1264#ifdef FEAT_SESSION
1265 if (eap->cmdidx == CMD_mksession)
1266 {
1267 if (put_line(fd, "let SessionLoad = 1") == FAIL)
1268 failed = TRUE;
1269 }
1270
1271 if (eap->cmdidx != CMD_mkview)
1272#endif
1273 {
1274 // Write setting 'compatible' first, because it has side effects.
1275 // For that same reason only do it when needed.
1276 if (p_cp)
1277 (void)put_line(fd, "if !&cp | set cp | endif");
1278 else
1279 (void)put_line(fd, "if &cp | set nocp | endif");
1280 }
1281
1282#ifdef FEAT_SESSION
1283 if (!view_session
1284 || (eap->cmdidx == CMD_mksession
1285 && (*flagp & SSOP_OPTIONS)))
1286#endif
Bram Moolenaar635bd602021-04-16 19:58:22 +02001287 {
1288 int flags = OPT_GLOBAL;
1289
1290#ifdef FEAT_SESSION
1291 if (eap->cmdidx == CMD_mksession && (*flagp & SSOP_SKIP_RTP))
1292 flags |= OPT_SKIPRTP;
1293#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001294 failed |= (makemap(fd, NULL) == FAIL
Bram Moolenaar635bd602021-04-16 19:58:22 +02001295 || makeset(fd, flags, FALSE) == FAIL);
1296 }
Bram Moolenaar84538072019-07-28 14:15:42 +02001297
1298#ifdef FEAT_SESSION
1299 if (!failed && view_session)
1300 {
Bram Moolenaar38890832020-11-01 17:40:54 +01001301 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 +02001302 failed = TRUE;
1303 if (eap->cmdidx == CMD_mksession)
1304 {
1305 char_u *dirnow; // current directory
1306
1307 dirnow = alloc(MAXPATHL);
1308 if (dirnow == NULL)
1309 failed = TRUE;
1310 else
1311 {
1312 // Change to session file's dir.
1313 if (mch_dirname(dirnow, MAXPATHL) == FAIL
1314 || mch_chdir((char *)dirnow) != 0)
1315 *dirnow = NUL;
1316 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
1317 {
1318 if (vim_chdirfile(fname, NULL) == OK)
1319 shorten_fnames(TRUE);
1320 }
1321 else if (*dirnow != NUL
1322 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
1323 {
1324 if (mch_chdir((char *)globaldir) == 0)
1325 shorten_fnames(TRUE);
1326 }
1327
1328 failed |= (makeopens(fd, dirnow) == FAIL);
1329
1330 // restore original dir
1331 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
1332 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
1333 {
1334 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001335 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar84538072019-07-28 14:15:42 +02001336 shorten_fnames(TRUE);
1337 }
1338 vim_free(dirnow);
1339 }
1340 }
1341 else
1342 {
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001343 failed |= (put_view(fd, curwin, !using_vdir, flagp, -1, NULL)
1344 == FAIL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001345 }
Bram Moolenaar38890832020-11-01 17:40:54 +01001346 if (put_line(fd, "let &g:so = s:so_save | let &g:siso = s:siso_save")
Bram Moolenaar84538072019-07-28 14:15:42 +02001347 == FAIL)
1348 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001349#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar84538072019-07-28 14:15:42 +02001350 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
1351 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001352#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001353 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
1354 failed = TRUE;
1355 if (eap->cmdidx == CMD_mksession)
1356 {
1357 if (put_line(fd, "unlet SessionLoad") == FAIL)
1358 failed = TRUE;
1359 }
1360 }
1361#endif
1362 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
1363 failed = TRUE;
1364
1365 failed |= fclose(fd);
1366
1367 if (failed)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001368 emsg(_(e_error_while_writing));
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001369#if defined(FEAT_SESSION)
Bram Moolenaar84538072019-07-28 14:15:42 +02001370 else if (eap->cmdidx == CMD_mksession)
1371 {
1372 // successful session write - set this_session var
1373 char_u *tbuf;
1374
1375 tbuf = alloc(MAXPATHL);
1376 if (tbuf != NULL)
1377 {
1378 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
1379 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
1380 vim_free(tbuf);
1381 }
1382 }
1383#endif
1384#ifdef MKSESSION_NL
1385 mksession_nl = FALSE;
1386#endif
1387 }
1388
1389#ifdef FEAT_BROWSE
1390theend:
1391 vim_free(browseFile);
1392#endif
1393#ifdef FEAT_SESSION
1394 vim_free(viewFile);
1395#endif
1396}
1397
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001398#if (defined(FEAT_VIMINFO) || defined(FEAT_SESSION)) || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001399 var_flavour_T
1400var_flavour(char_u *varname)
1401{
1402 char_u *p = varname;
1403
1404 if (ASCII_ISUPPER(*p))
1405 {
1406 while (*(++p))
1407 if (ASCII_ISLOWER(*p))
1408 return VAR_FLAVOUR_SESSION;
1409 return VAR_FLAVOUR_VIMINFO;
1410 }
1411 else
1412 return VAR_FLAVOUR_DEFAULT;
1413}
1414#endif
1415
1416/*
1417 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
1418 * Return FAIL for a write error.
1419 */
1420 int
1421put_eol(FILE *fd)
1422{
1423 if (
1424#ifdef USE_CRNL
1425 (
1426# ifdef MKSESSION_NL
1427 !mksession_nl &&
1428# endif
1429 (putc('\r', fd) < 0)) ||
1430#endif
1431 (putc('\n', fd) < 0))
1432 return FAIL;
1433 return OK;
1434}
1435
1436/*
1437 * Write a line to "fd".
1438 * Return FAIL for a write error.
1439 */
1440 int
1441put_line(FILE *fd, char *s)
1442{
1443 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
1444 return FAIL;
1445 return OK;
1446}