blob: 8dd0bc3642e29db1d0281e8023ad1e0ab71d33f9 [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
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000203 if (fr->fr_layout == FR_LEAF)
204 return OK;
Bram Moolenaar84538072019-07-28 14:15:42 +0200205
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000206 // Find first frame that's not skipped and then create a window for
207 // each following one (first frame is already there).
208 frc = ses_skipframe(fr->fr_child);
209 if (frc != NULL)
210 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200211 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000212 // Make window as big as possible so that we have lots of room
213 // to split.
214 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
215 || put_line(fd, fr->fr_layout == FR_COL
216 ? "split" : "vsplit") == FAIL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200217 return FAIL;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000218 ++count;
Bram Moolenaar84538072019-07-28 14:15:42 +0200219 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000220
221 // Go back to the first window.
222 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
223 ? "%dwincmd k" : "%dwincmd h", count) < 0
224 || put_eol(fd) == FAIL))
225 return FAIL;
226
227 // Recursively create frames/windows in each window of this column or
228 // row.
229 frc = ses_skipframe(fr->fr_child);
230 while (frc != NULL)
231 {
232 ses_win_rec(fd, frc);
233 frc = ses_skipframe(frc->fr_next);
234 // Go to next window.
235 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
236 return FAIL;
Bram Moolenaar84538072019-07-28 14:15:42 +0200237 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000238
Bram Moolenaar84538072019-07-28 14:15:42 +0200239 return OK;
240}
241
242 static int
243ses_winsizes(
244 FILE *fd,
245 int restore_size,
246 win_T *tab_firstwin)
247{
248 int n = 0;
249 win_T *wp;
250
251 if (restore_size && (ssop_flags & SSOP_WINSIZE))
252 {
253 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
254 {
255 if (!ses_do_win(wp))
256 continue;
257 ++n;
258
259 // restore height when not full height
260 if (wp->w_height + wp->w_status_height < topframe->fr_height
261 && (fprintf(fd,
262 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
263 n, (long)wp->w_height, Rows / 2, Rows) < 0
264 || put_eol(fd) == FAIL))
265 return FAIL;
266
267 // restore width when not full width
268 if (wp->w_width < Columns && (fprintf(fd,
269 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
270 n, (long)wp->w_width, Columns / 2, Columns) < 0
271 || put_eol(fd) == FAIL))
272 return FAIL;
273 }
274 }
275 else
276 {
277 // Just equalise window sizes
278 if (put_line(fd, "wincmd =") == FAIL)
279 return FAIL;
280 }
281 return OK;
282}
283
284 static int
285put_view_curpos(FILE *fd, win_T *wp, char *spaces)
286{
287 int r;
288
289 if (wp->w_curswant == MAXCOL)
290 r = fprintf(fd, "%snormal! $", spaces);
291 else
292 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
293 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
294}
295
296/*
297 * Write commands to "fd" to restore the view of a window.
298 * Caller must make sure 'scrolloff' is zero.
299 */
300 static int
301put_view(
302 FILE *fd,
303 win_T *wp,
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200304 int add_edit, // add ":edit" command to view
305 unsigned *flagp, // vop_flags or ssop_flags
306 int current_arg_idx, // current argument index of the window,
307 // use -1 if unknown
308 hashtab_T *terminal_bufs UNUSED) // already encountered terminal buffers,
309 // can be NULL
Bram Moolenaar84538072019-07-28 14:15:42 +0200310{
311 win_T *save_curwin;
312 int f;
313 int do_cursor;
314 int did_next = FALSE;
315
316 // Always restore cursor position for ":mksession". For ":mkview" only
317 // when 'viewoptions' contains "cursor".
318 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
319
320 // Local argument list.
321 if (wp->w_alist == &global_alist)
322 {
323 if (put_line(fd, "argglobal") == FAIL)
324 return FAIL;
325 }
326 else
327 {
328 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
329 flagp == &vop_flags
330 || !(*flagp & SSOP_CURDIR)
331 || wp->w_localdir != NULL, flagp) == FAIL)
332 return FAIL;
333 }
334
335 // Only when part of a session: restore the argument index. Some
336 // arguments may have been deleted, check if the index is valid.
337 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
338 && flagp == &ssop_flags)
339 {
340 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
341 || put_eol(fd) == FAIL)
342 return FAIL;
343 did_next = TRUE;
344 }
345
346 // Edit the file. Skip this when ":next" already did it.
347 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
348 {
matveyt8e7d9db2022-01-05 14:01:30 +0000349 if (bt_help(wp->w_buffer))
350 {
351 char *curtag = "";
352
353 // A help buffer needs some options to be set.
354 // First, create a new empty buffer with "buftype=help".
355 // Then ":help" will re-use both the buffer and the window and set
356 // the options, even when "options" is not in 'sessionoptions'.
357 if (0 < wp->w_tagstackidx
358 && wp->w_tagstackidx <= wp->w_tagstacklen)
359 curtag = (char *)wp->w_tagstack[wp->w_tagstackidx - 1].tagname;
360
361 if (put_line(fd, "enew | setl bt=help") == FAIL
362 || fprintf(fd, "help %s", curtag) < 0
363 || put_eol(fd) == FAIL)
364 return FAIL;
365 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200366# ifdef FEAT_TERMINAL
matveyt8e7d9db2022-01-05 14:01:30 +0000367 else if (bt_terminal(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200368 {
Bram Moolenaar0e655112020-09-11 20:36:36 +0200369 if (term_write_session(fd, wp, terminal_bufs) == FAIL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200370 return FAIL;
371 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200372# endif
373 // Load the file.
matveyt8e7d9db2022-01-05 14:01:30 +0000374 else if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100375 && !bt_nofilename(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200376 {
377 // Editing a file in this buffer: use ":edit file".
378 // This may have side effects! (e.g., compressed or network file).
379 //
380 // Note, if a buffer for that file already exists, use :badd to
381 // edit that buffer, to not lose folding information (:edit resets
382 // folds in other buffers)
James Cherti7d428402022-03-14 20:24:51 +0000383 if (fputs("if bufexists(fnamemodify(\"", fd) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200384 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
James Cherti7d428402022-03-14 20:24:51 +0000385 || fputs("\", \":p\")) | buffer ", fd) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200386 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
387 || fputs(" | else | edit ", fd) < 0
388 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
389 || fputs(" | endif", fd) < 0
390 || put_eol(fd) == FAIL)
391 return FAIL;
392 }
393 else
394 {
395 // No file in this buffer, just make it empty.
396 if (put_line(fd, "enew") == FAIL)
397 return FAIL;
398#ifdef FEAT_QUICKFIX
399 if (wp->w_buffer->b_ffname != NULL)
400 {
401 // The buffer does have a name, but it's not a file name.
402 if (fputs("file ", fd) < 0
403 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
404 return FAIL;
405 }
406#endif
407 do_cursor = FALSE;
408 }
409 }
410
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100411 if (wp->w_alt_fnum)
412 {
413 buf_T *alt = buflist_findnr(wp->w_alt_fnum);
414
Bram Moolenaar0756f752021-03-13 13:52:33 +0100415 // Set the alternate file if the buffer is listed.
Bram Moolenaar139348f2021-02-05 21:55:53 +0100416 if ((flagp == &ssop_flags)
417 && alt != NULL
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100418 && alt->b_fname != NULL
419 && *alt->b_fname != NUL
Bram Moolenaar0756f752021-03-13 13:52:33 +0100420 && alt->b_p_bl
Bram Moolenaar59d8e562020-11-07 18:41:10 +0100421 && (fputs("balt ", fd) < 0
422 || ses_fname(fd, alt, flagp, TRUE) == FAIL))
423 return FAIL;
424 }
425
Bram Moolenaar84538072019-07-28 14:15:42 +0200426 // Local mappings and abbreviations.
427 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
428 && makemap(fd, wp->w_buffer) == FAIL)
429 return FAIL;
430
431 // Local options. Need to go to the window temporarily.
432 // Store only local values when using ":mkview" and when ":mksession" is
433 // used and 'sessionoptions' doesn't include "options".
434 // Some folding options are always stored when "folds" is included,
435 // otherwise the folds would not be restored correctly.
436 save_curwin = curwin;
437 curwin = wp;
438 curbuf = curwin->w_buffer;
439 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
440 f = makeset(fd, OPT_LOCAL,
441 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
442#ifdef FEAT_FOLDING
443 else if (*flagp & SSOP_FOLDS)
444 f = makefoldset(fd);
445#endif
446 else
447 f = OK;
448 curwin = save_curwin;
449 curbuf = curwin->w_buffer;
450 if (f == FAIL)
451 return FAIL;
452
453#ifdef FEAT_FOLDING
454 // Save Folds when 'buftype' is empty and for help files.
455 if ((*flagp & SSOP_FOLDS)
456 && wp->w_buffer->b_ffname != NULL
457 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
458 {
459 if (put_folds(fd, wp) == FAIL)
460 return FAIL;
461 }
462#endif
463
464 // Set the cursor after creating folds, since that moves the cursor.
465 if (do_cursor)
466 {
467
468 // Restore the cursor line in the file and relatively in the
469 // window. Don't use "G", it changes the jumplist.
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200470 if (wp->w_height <= 0)
471 {
472 if (fprintf(fd, "let s:l = %ld", (long)wp->w_cursor.lnum) < 0)
473 return FAIL;
474 }
475 else if (fprintf(fd,
476 "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
Bram Moolenaar84538072019-07-28 14:15:42 +0200477 (long)wp->w_cursor.lnum,
478 (long)(wp->w_cursor.lnum - wp->w_topline),
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200479 (long)wp->w_height / 2, (long)wp->w_height) < 0)
480 return FAIL;
481
482 if (put_eol(fd) == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200483 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
Bram Moolenaar3482be62020-11-27 11:00:38 +0100484 || put_line(fd, "keepjumps exe s:l") == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200485 || put_line(fd, "normal! zt") == FAIL
Bram Moolenaar3482be62020-11-27 11:00:38 +0100486 || fprintf(fd, "keepjumps %ld", (long)wp->w_cursor.lnum) < 0
Bram Moolenaar84538072019-07-28 14:15:42 +0200487 || put_eol(fd) == FAIL)
488 return FAIL;
489 // Restore the cursor column and left offset when not wrapping.
490 if (wp->w_cursor.col == 0)
491 {
492 if (put_line(fd, "normal! 0") == FAIL)
493 return FAIL;
494 }
495 else
496 {
497 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
498 {
499 if (fprintf(fd,
500 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
501 (long)wp->w_virtcol + 1,
502 (long)(wp->w_virtcol - wp->w_leftcol),
503 (long)wp->w_width / 2, (long)wp->w_width) < 0
504 || put_eol(fd) == FAIL
505 || put_line(fd, "if s:c > 0") == FAIL
506 || fprintf(fd,
507 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
508 (long)wp->w_virtcol + 1) < 0
509 || put_eol(fd) == FAIL
510 || put_line(fd, "else") == FAIL
511 || put_view_curpos(fd, wp, " ") == FAIL
512 || put_line(fd, "endif") == FAIL)
513 return FAIL;
514 }
515 else if (put_view_curpos(fd, wp, "") == FAIL)
516 return FAIL;
517 }
518 }
519
520 // Local directory, if the current flag is not view options or the "curdir"
521 // option is included.
522 if (wp->w_localdir != NULL
523 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
524 {
525 if (fputs("lcd ", fd) < 0
526 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
527 || put_eol(fd) == FAIL)
528 return FAIL;
529 did_lcd = TRUE;
530 }
531
532 return OK;
533}
534
535#ifdef FEAT_EVAL
536 static int
537store_session_globals(FILE *fd)
538{
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200539 hashtab_T *gvht = get_globvar_ht();
Bram Moolenaar84538072019-07-28 14:15:42 +0200540 hashitem_T *hi;
541 dictitem_T *this_var;
542 int todo;
543 char_u *p, *t;
544
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200545 todo = (int)gvht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000546 FOR_ALL_HASHTAB_ITEMS(gvht, hi, todo)
Bram Moolenaar84538072019-07-28 14:15:42 +0200547 {
548 if (!HASHITEM_EMPTY(hi))
549 {
550 --todo;
551 this_var = HI2DI(hi);
552 if ((this_var->di_tv.v_type == VAR_NUMBER
553 || this_var->di_tv.v_type == VAR_STRING)
554 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
555 {
556 // Escape special characters with a backslash. Turn a LF and
557 // CR into \n and \r.
558 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
559 (char_u *)"\\\"\n\r");
560 if (p == NULL) // out of memory
561 break;
562 for (t = p; *t != NUL; ++t)
563 if (*t == '\n')
564 *t = 'n';
565 else if (*t == '\r')
566 *t = 'r';
567 if ((fprintf(fd, "let %s = %c%s%c",
568 this_var->di_key,
569 (this_var->di_tv.v_type == VAR_STRING) ? '"'
570 : ' ',
571 p,
572 (this_var->di_tv.v_type == VAR_STRING) ? '"'
573 : ' ') < 0)
574 || put_eol(fd) == FAIL)
575 {
576 vim_free(p);
577 return FAIL;
578 }
579 vim_free(p);
580 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200581 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 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200597 }
598 }
599 return OK;
600}
601#endif
602
603/*
604 * Write openfile commands for the current buffers to an .exrc file.
605 * Return FAIL on error, OK otherwise.
606 */
607 static int
608makeopens(
609 FILE *fd,
610 char_u *dirnow) // Current directory name
611{
612 buf_T *buf;
613 int only_save_windows = TRUE;
614 int nr;
615 int restore_size = TRUE;
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100616 int restore_height_width = FALSE;
Bram Moolenaar84538072019-07-28 14:15:42 +0200617 win_T *wp;
618 char_u *sname;
619 win_T *edited_win = NULL;
Bram Moolenaar84538072019-07-28 14:15:42 +0200620 int restore_stal = FALSE;
621 win_T *tab_firstwin;
622 frame_T *tab_topframe;
623 int cur_arg_idx = 0;
624 int next_arg_idx = 0;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200625 int ret = FAIL;
LemonBoyd7c95642022-04-30 16:10:27 +0100626 tabpage_T *tp;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200627#ifdef FEAT_TERMINAL
628 hashtab_T terminal_bufs;
629
630 hash_init(&terminal_bufs);
631#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200632
633 if (ssop_flags & SSOP_BUFFERS)
634 only_save_windows = FALSE; // Save ALL buffers
635
636 // Begin by setting the this_session variable, and then other
637 // sessionable variables.
638#ifdef FEAT_EVAL
639 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200640 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200641 if (ssop_flags & SSOP_GLOBALS)
642 if (store_session_globals(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200643 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200644#endif
645
646 // Close all windows and tabs but one.
647 if (put_line(fd, "silent only") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200648 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200649 if ((ssop_flags & SSOP_TABPAGES)
650 && put_line(fd, "silent tabonly") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200651 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200652
653 // Now a :cd command to the session directory or the current directory
654 if (ssop_flags & SSOP_SESDIR)
655 {
656 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
657 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200658 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200659 }
660 else if (ssop_flags & SSOP_CURDIR)
661 {
662 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
663 if (sname == NULL
664 || fputs("cd ", fd) < 0
665 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
666 || put_eol(fd) == FAIL)
667 {
668 vim_free(sname);
Bram Moolenaar0e655112020-09-11 20:36:36 +0200669 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200670 }
671 vim_free(sname);
672 }
673
674 // If there is an empty, unnamed buffer we will wipe it out later.
675 // Remember the buffer number.
676 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200677 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200678 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200679 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200680 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200681 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200682
zeertzjq8f751d52025-06-05 20:25:51 +0200683 // Save 'shortmess' if not storing options.
James Chertifd012802022-03-29 12:02:57 +0100684 if ((ssop_flags & SSOP_OPTIONS) == 0
685 && put_line(fd, "let s:shortmess_save = &shortmess") == FAIL)
686 goto fail;
687
zeertzjq8f751d52025-06-05 20:25:51 +0200688 // Set 'shortmess' for the following.
689 if (put_line(fd, "set shortmess+=aoO") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200690 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200691
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +0100692 // Now save the current files, current buffer first.
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000693 // Put all buffers into the buffer list.
694 // Do it very early to preserve buffer order after loading session (which
695 // can be disrupted by prior `edit` or `tabedit` calls).
696 FOR_ALL_BUFFERS(buf)
697 {
698 if (!(only_save_windows && buf->b_nwindows == 0)
699 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000700 // Skip terminal buffers: finished ones are not useful, others
701 // will be resurrected and result in a new buffer.
702 && !bt_terminal(buf)
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000703 && buf->b_fname != NULL
704 && buf->b_p_bl)
705 {
706 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
707 : buf->b_wininfo->wi_fpos.lnum) < 0
708 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
709 goto fail;
710 }
711 }
712
Bram Moolenaar84538072019-07-28 14:15:42 +0200713 // the global argument list
714 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
715 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200716 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200717
718 if (ssop_flags & SSOP_RESIZE)
719 {
720 // Note: after the restore we still check it worked!
721 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
722 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200723 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200724 }
725
726#ifdef FEAT_GUI
727 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
728 {
729 int x, y;
730
731 if (gui_mch_get_winpos(&x, &y) == OK)
732 {
733 // Note: after the restore we still check it worked!
734 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200735 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200736 }
737 }
738#endif
739
740 // When there are two or more tabpages and 'showtabline' is 1 the tabline
741 // will be displayed when creating the next tab. That resizes the windows
742 // in the first tab, which may cause problems. Set 'showtabline' to 2
743 // temporarily to avoid that.
744 if (p_stal == 1 && first_tabpage->tp_next != NULL)
745 {
746 if (put_line(fd, "set stal=2") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200747 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200748 restore_stal = TRUE;
749 }
750
Bram Moolenaar84538072019-07-28 14:15:42 +0200751 if ((ssop_flags & SSOP_TABPAGES))
752 {
LemonBoyd7c95642022-04-30 16:10:27 +0100753 // "tabpages" is in 'sessionoptions': Similar to ses_win_rec() below,
754 // populate the tab pages first so later local options won't be copied
755 // to the new tabs.
Bram Moolenaar84538072019-07-28 14:15:42 +0200756 FOR_ALL_TABPAGES(tp)
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000757 // Use `bufhidden=wipe` to remove empty "placeholder" buffers once
758 // they are not needed. This prevents creating extra buffers (see
759 // cause of patch 8.1.0829)
760 if (tp->tp_next != NULL
761 && put_line(fd, "tabnew +setlocal\\ bufhidden=wipe") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200762 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200763 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200764 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200765 }
LemonBoyd7c95642022-04-30 16:10:27 +0100766
767 // Assume "tabpages" is in 'sessionoptions'. If not then we only do
768 // "curtab" and bail out of the loop.
769 FOR_ALL_TABPAGES(tp)
Bram Moolenaar84538072019-07-28 14:15:42 +0200770 {
Bram Moolenaar84538072019-07-28 14:15:42 +0200771 int need_tabnext = FALSE;
772 int cnr = 1;
773
LemonBoyd7c95642022-04-30 16:10:27 +0100774 // May repeat putting Windows for each tab, when "tabpages" is in
775 // 'sessionoptions'.
776 // Don't use goto_tabpage(), it may change directory and trigger
777 // autocommands.
Bram Moolenaar84538072019-07-28 14:15:42 +0200778 if ((ssop_flags & SSOP_TABPAGES))
779 {
Bram Moolenaar84538072019-07-28 14:15:42 +0200780 if (tp == curtab)
781 {
782 tab_firstwin = firstwin;
783 tab_topframe = topframe;
784 }
785 else
786 {
787 tab_firstwin = tp->tp_firstwin;
788 tab_topframe = tp->tp_topframe;
789 }
LemonBoyd7c95642022-04-30 16:10:27 +0100790 if (tp != first_tabpage)
Bram Moolenaar84538072019-07-28 14:15:42 +0200791 need_tabnext = TRUE;
792 }
LemonBoyd7c95642022-04-30 16:10:27 +0100793 else
794 {
795 tp = curtab;
796 tab_firstwin = firstwin;
797 tab_topframe = topframe;
798 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200799
800 // Before creating the window layout, try loading one file. If this
801 // is aborted we don't end up with a number of useless windows.
802 // This may have side effects! (e.g., compressed or network file).
803 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
804 {
805 if (ses_do_win(wp)
806 && wp->w_buffer->b_ffname != NULL
807 && !bt_help(wp->w_buffer)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100808 && !bt_nofilename(wp->w_buffer))
Bram Moolenaar84538072019-07-28 14:15:42 +0200809 {
810 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200811 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200812 need_tabnext = FALSE;
813
814 if (fputs("edit ", fd) < 0
815 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
816 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200817 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200818 if (!wp->w_arg_idx_invalid)
819 edited_win = wp;
820 break;
821 }
822 }
823
824 // If no file got edited create an empty tab page.
825 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200826 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200827
Bram Moolenaar0995c812021-04-17 18:38:54 +0200828 if (tab_topframe->fr_layout != FR_LEAF)
829 {
830 // Save current window layout.
831 if (put_line(fd, "let s:save_splitbelow = &splitbelow") == FAIL
832 || put_line(fd, "let s:save_splitright = &splitright")
833 == FAIL)
834 goto fail;
835 if (put_line(fd, "set splitbelow splitright") == FAIL)
836 goto fail;
837 if (ses_win_rec(fd, tab_topframe) == FAIL)
838 goto fail;
839 if (put_line(fd, "let &splitbelow = s:save_splitbelow") == FAIL
840 || put_line(fd, "let &splitright = s:save_splitright")
841 == FAIL)
842 goto fail;
843 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200844
845 // Check if window sizes can be restored (no windows omitted).
846 // Remember the window number of the current window after restoring.
847 nr = 0;
848 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
849 {
850 if (ses_do_win(wp))
851 ++nr;
852 else
853 restore_size = FALSE;
854 if (curwin == wp)
855 cnr = nr;
856 }
857
Bram Moolenaar0995c812021-04-17 18:38:54 +0200858 if (tab_firstwin->w_next != NULL)
859 {
860 // Go to the first window.
861 if (put_line(fd, "wincmd t") == FAIL)
862 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200863
Bram Moolenaar0995c812021-04-17 18:38:54 +0200864 // If more than one window, see if sizes can be restored.
865 // First set 'winheight' and 'winwidth' to 1 to avoid the windows
866 // being resized when moving between windows.
867 // Do this before restoring the view, so that the topline and the
868 // cursor can be set. This is done again below.
869 // winminheight and winminwidth need to be set to avoid an error if
870 // the user has set winheight or winwidth.
871 if (put_line(fd, "let s:save_winminheight = &winminheight") == FAIL
872 || put_line(fd, "let s:save_winminwidth = &winminwidth")
873 == FAIL)
874 goto fail;
875 if (put_line(fd, "set winminheight=0") == FAIL
876 || put_line(fd, "set winheight=1") == FAIL
877 || put_line(fd, "set winminwidth=0") == FAIL
878 || put_line(fd, "set winwidth=1") == FAIL)
879 goto fail;
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100880 restore_height_width = TRUE;
Bram Moolenaar0995c812021-04-17 18:38:54 +0200881 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200882 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200883 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200884
885 // Restore the tab-local working directory if specified
886 // Do this before the windows, so that the window-local directory can
887 // override the tab-local directory.
LemonBoyd7c95642022-04-30 16:10:27 +0100888 if ((ssop_flags & SSOP_CURDIR) && tp->tp_localdir != NULL)
Bram Moolenaar84538072019-07-28 14:15:42 +0200889 {
890 if (fputs("tcd ", fd) < 0
LemonBoyd7c95642022-04-30 16:10:27 +0100891 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
892 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200893 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200894 did_lcd = TRUE;
895 }
896
897 // Restore the view of the window (options, file, cursor, etc.).
898 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
899 {
900 if (!ses_do_win(wp))
901 continue;
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200902 if (put_view(fd, wp, wp != edited_win, &ssop_flags, cur_arg_idx,
Bram Moolenaar0e655112020-09-11 20:36:36 +0200903#ifdef FEAT_TERMINAL
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200904 &terminal_bufs
905#else
906 NULL
Bram Moolenaar0e655112020-09-11 20:36:36 +0200907#endif
908 ) == FAIL)
909 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200910 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200911 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200912 next_arg_idx = wp->w_arg_idx;
913 }
914
915 // The argument index in the first tab page is zero, need to set it in
916 // each window. For further tab pages it's the window where we do
917 // "tabedit".
918 cur_arg_idx = next_arg_idx;
919
920 // Restore cursor to the current window if it's not the first one.
921 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
922 || put_eol(fd) == FAIL))
Bram Moolenaar0e655112020-09-11 20:36:36 +0200923 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200924
925 // Restore window sizes again after jumping around in windows, because
926 // the current window has a minimum size while others may not.
927 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200928 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200929
930 // Don't continue in another tab page when doing only the current one
931 // or when at the last tab page.
932 if (!(ssop_flags & SSOP_TABPAGES))
933 break;
934 }
935
936 if (ssop_flags & SSOP_TABPAGES)
937 {
938 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
939 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200940 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200941 }
942 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200943 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200944
Bram Moolenaar84538072019-07-28 14:15:42 +0200945 // Wipe out an empty unnamed buffer we started in.
946 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
947 == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200948 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200949 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200950 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200951 if (put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200952 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200953 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200954 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200955
James Chertifd012802022-03-29 12:02:57 +0100956 // Re-apply 'winheight' and 'winwidth'.
957 if (fprintf(fd, "set winheight=%ld winwidth=%ld",
958 p_wh, p_wiw) < 0 || put_eol(fd) == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200959 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100960
961 // Restore 'shortmess'.
962 if (ssop_flags & SSOP_OPTIONS)
963 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100964 if (fprintf(fd, "set shortmess=%s", p_shm) < 0 || put_eol(fd) == FAIL)
965 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100966 }
967 else
968 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100969 if (put_line(fd, "let &shortmess = s:shortmess_save") == FAIL)
970 goto fail;
James Chertifd012802022-03-29 12:02:57 +0100971 }
972
Bram Moolenaarb4011af2022-05-01 00:42:24 +0100973 if (restore_height_width)
Bram Moolenaar0995c812021-04-17 18:38:54 +0200974 {
975 // Restore 'winminheight' and 'winminwidth'.
976 if (put_line(fd, "let &winminheight = s:save_winminheight") == FAIL
977 || put_line(fd, "let &winminwidth = s:save_winminwidth") == FAIL)
978 goto fail;
979 }
Bram Moolenaar84538072019-07-28 14:15:42 +0200980
981 // Lastly, execute the x.vim file if it exists.
982 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
Bram Moolenaarc5f33db2020-04-16 21:04:41 +0200983 || put_line(fd, "if filereadable(s:sx)") == FAIL
Bram Moolenaar84538072019-07-28 14:15:42 +0200984 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
985 || put_line(fd, "endif") == FAIL)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200986 goto fail;
Bram Moolenaar84538072019-07-28 14:15:42 +0200987
Bram Moolenaar0e655112020-09-11 20:36:36 +0200988 ret = OK;
989fail:
990#ifdef FEAT_TERMINAL
991 hash_clear_all(&terminal_bufs, 0);
992#endif
993 return ret;
Bram Moolenaar84538072019-07-28 14:15:42 +0200994}
995
996/*
997 * Get the name of the view file for the current buffer.
998 */
999 static char_u *
1000get_view_file(int c)
1001{
1002 int len = 0;
1003 char_u *p, *s;
1004 char_u *retval;
1005 char_u *sname;
1006
1007 if (curbuf->b_ffname == NULL)
1008 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001009 emsg(_(e_no_file_name));
Bram Moolenaar84538072019-07-28 14:15:42 +02001010 return NULL;
1011 }
1012 sname = home_replace_save(NULL, curbuf->b_ffname);
1013 if (sname == NULL)
1014 return NULL;
1015
1016 // We want a file name without separators, because we're not going to make
1017 // a directory.
1018 // "normal" path separator -> "=+"
1019 // "=" -> "=="
1020 // ":" path separator -> "=-"
1021 for (p = sname; *p; ++p)
1022 if (*p == '=' || vim_ispathsep(*p))
1023 ++len;
1024 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
1025 if (retval != NULL)
1026 {
1027 STRCPY(retval, p_vdir);
1028 add_pathsep(retval);
1029 s = retval + STRLEN(retval);
1030 for (p = sname; *p; ++p)
1031 {
1032 if (*p == '=')
1033 {
1034 *s++ = '=';
1035 *s++ = '=';
1036 }
1037 else if (vim_ispathsep(*p))
1038 {
1039 *s++ = '=';
1040#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
1041 if (*p == ':')
1042 *s++ = '-';
1043 else
1044#endif
1045 *s++ = '+';
1046 }
1047 else
1048 *s++ = *p;
1049 }
1050 *s++ = '=';
1051 *s++ = c;
1052 STRCPY(s, ".vim");
1053 }
1054
1055 vim_free(sname);
1056 return retval;
1057}
1058
1059/*
1060 * ":loadview [nr]"
1061 */
1062 void
1063ex_loadview(exarg_T *eap)
1064{
1065 char_u *fname;
1066
1067 fname = get_view_file(*eap->arg);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001068 if (fname == NULL)
1069 return;
1070
Christian Brabandtd847b242025-04-22 20:04:28 +02001071 (void)do_source(fname, FALSE, DOSO_NONE, NULL);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001072 vim_free(fname);
Bram Moolenaar84538072019-07-28 14:15:42 +02001073}
1074
Bram Moolenaard17a57a2019-09-29 20:53:55 +02001075# if defined(FEAT_GUI_GNOME) \
Bram Moolenaarac02a632019-09-29 19:02:46 +02001076 || (defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)) \
1077 || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001078/*
1079 * Generate a script that can be used to restore the current editing session.
1080 * Save the value of v:this_session before running :mksession in order to make
1081 * automagic session save fully transparent. Return TRUE on success.
1082 */
1083 int
1084write_session_file(char_u *filename)
1085{
1086 char_u *escaped_filename;
1087 char *mksession_cmdline;
1088 unsigned int save_ssop_flags;
1089 int failed;
1090
1091 // Build an ex command line to create a script that restores the current
1092 // session if executed. Escape the filename to avoid nasty surprises.
1093 escaped_filename = vim_strsave_escaped(filename, escape_chars);
1094 if (escaped_filename == NULL)
1095 return FALSE;
1096 mksession_cmdline = alloc(10 + (int)STRLEN(escaped_filename) + 1);
1097 if (mksession_cmdline == NULL)
1098 {
1099 vim_free(escaped_filename);
1100 return FALSE;
1101 }
1102 strcpy(mksession_cmdline, "mksession ");
1103 STRCAT(mksession_cmdline, escaped_filename);
1104 vim_free(escaped_filename);
1105
1106 // Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
1107 // unpredictable effects when the session is saved automatically. Also,
1108 // we definitely need SSOP_GLOBALS to be able to restore v:this_session.
1109 // Don't use SSOP_BUFFERS to prevent the buffer list from becoming
1110 // enormously large if the GNOME session feature is used regularly.
1111 save_ssop_flags = ssop_flags;
1112 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
1113 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
1114
1115 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
1116 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
1117 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
1118 do_unlet((char_u *)"Save_VV_this_session", TRUE);
1119
1120 ssop_flags = save_ssop_flags;
1121 vim_free(mksession_cmdline);
1122
1123 // Reopen the file and append a command to restore v:this_session,
1124 // as if this save never happened. This is to avoid conflicts with
1125 // the user's own sessions. FIXME: It's probably less hackish to add
1126 // a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
1127 if (!failed)
1128 {
1129 FILE *fd;
1130
1131 fd = open_exfile(filename, TRUE, APPENDBIN);
1132
1133 failed = (fd == NULL
1134 || put_line(fd, "let v:this_session = Save_VV_this_session")
1135 == FAIL
1136 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
1137
1138 if (fd != NULL && fclose(fd) != 0)
1139 failed = TRUE;
1140
1141 if (failed)
1142 mch_remove(filename);
1143 }
1144
1145 return !failed;
1146}
1147# endif
1148
1149#endif // FEAT_SESSION
1150
1151#if defined(FEAT_SESSION) && defined(USE_CRNL)
1152# define MKSESSION_NL
1153static int mksession_nl = FALSE; // use NL only in put_eol()
1154#endif
1155
Bram Moolenaar84538072019-07-28 14:15:42 +02001156/*
1157 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
1158 */
1159 void
1160ex_mkrc(exarg_T *eap)
1161{
1162 FILE *fd;
1163 int failed = FALSE;
1164 char_u *fname;
1165#ifdef FEAT_BROWSE
1166 char_u *browseFile = NULL;
1167#endif
1168#ifdef FEAT_SESSION
1169 int view_session = FALSE;
1170 int using_vdir = FALSE; // using 'viewdir'?
1171 char_u *viewFile = NULL;
1172 unsigned *flagp;
1173#endif
1174
1175 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
1176 {
1177#ifdef FEAT_SESSION
1178 view_session = TRUE;
1179#else
1180 ex_ni(eap);
1181 return;
1182#endif
1183 }
1184
1185#ifdef FEAT_SESSION
1186 // Use the short file name until ":lcd" is used. We also don't use the
1187 // short file name when 'acd' is set, that is checked later.
1188 did_lcd = FALSE;
1189
1190 // ":mkview" or ":mkview 9": generate file name with 'viewdir'
1191 if (eap->cmdidx == CMD_mkview
1192 && (*eap->arg == NUL
1193 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
1194 {
1195 eap->forceit = TRUE;
1196 fname = get_view_file(*eap->arg);
1197 if (fname == NULL)
1198 return;
1199 viewFile = fname;
1200 using_vdir = TRUE;
1201 }
1202 else
1203#endif
1204 if (*eap->arg != NUL)
1205 fname = eap->arg;
1206 else if (eap->cmdidx == CMD_mkvimrc)
1207 fname = (char_u *)VIMRC_FILE;
1208#ifdef FEAT_SESSION
1209 else if (eap->cmdidx == CMD_mksession)
1210 fname = (char_u *)SESSION_FILE;
1211#endif
1212 else
1213 fname = (char_u *)EXRC_FILE;
1214
1215#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001216 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar84538072019-07-28 14:15:42 +02001217 {
1218 browseFile = do_browse(BROWSE_SAVE,
1219# ifdef FEAT_SESSION
1220 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
1221 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
1222# endif
1223 (char_u *)_("Save Setup"),
1224 fname, (char_u *)"vim", NULL,
1225 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
1226 if (browseFile == NULL)
1227 goto theend;
1228 fname = browseFile;
1229 eap->forceit = TRUE; // since dialog already asked
1230 }
1231#endif
1232
1233#if defined(FEAT_SESSION) && defined(vim_mkdir)
1234 // When using 'viewdir' may have to create the directory.
1235 if (using_vdir && !mch_isdir(p_vdir))
1236 vim_mkdir_emsg(p_vdir, 0755);
1237#endif
1238
1239 fd = open_exfile(fname, eap->forceit, WRITEBIN);
1240 if (fd != NULL)
1241 {
1242#ifdef FEAT_SESSION
1243 if (eap->cmdidx == CMD_mkview)
1244 flagp = &vop_flags;
1245 else
1246 flagp = &ssop_flags;
1247#endif
1248
1249#ifdef MKSESSION_NL
1250 // "unix" in 'sessionoptions': use NL line separator
1251 if (view_session && (*flagp & SSOP_UNIX))
1252 mksession_nl = TRUE;
1253#endif
1254
1255 // Write the version command for :mkvimrc
1256 if (eap->cmdidx == CMD_mkvimrc)
1257 (void)put_line(fd, "version 6.0");
1258
1259#ifdef FEAT_SESSION
1260 if (eap->cmdidx == CMD_mksession)
1261 {
1262 if (put_line(fd, "let SessionLoad = 1") == FAIL)
1263 failed = TRUE;
1264 }
1265
1266 if (eap->cmdidx != CMD_mkview)
1267#endif
1268 {
1269 // Write setting 'compatible' first, because it has side effects.
1270 // For that same reason only do it when needed.
1271 if (p_cp)
1272 (void)put_line(fd, "if !&cp | set cp | endif");
1273 else
1274 (void)put_line(fd, "if &cp | set nocp | endif");
1275 }
1276
1277#ifdef FEAT_SESSION
1278 if (!view_session
1279 || (eap->cmdidx == CMD_mksession
1280 && (*flagp & SSOP_OPTIONS)))
1281#endif
Bram Moolenaar635bd602021-04-16 19:58:22 +02001282 {
1283 int flags = OPT_GLOBAL;
1284
1285#ifdef FEAT_SESSION
1286 if (eap->cmdidx == CMD_mksession && (*flagp & SSOP_SKIP_RTP))
1287 flags |= OPT_SKIPRTP;
1288#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001289 failed |= (makemap(fd, NULL) == FAIL
Bram Moolenaar635bd602021-04-16 19:58:22 +02001290 || makeset(fd, flags, FALSE) == FAIL);
1291 }
Bram Moolenaar84538072019-07-28 14:15:42 +02001292
1293#ifdef FEAT_SESSION
1294 if (!failed && view_session)
1295 {
Bram Moolenaar38890832020-11-01 17:40:54 +01001296 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 +02001297 failed = TRUE;
1298 if (eap->cmdidx == CMD_mksession)
1299 {
1300 char_u *dirnow; // current directory
1301
1302 dirnow = alloc(MAXPATHL);
1303 if (dirnow == NULL)
1304 failed = TRUE;
1305 else
1306 {
1307 // Change to session file's dir.
1308 if (mch_dirname(dirnow, MAXPATHL) == FAIL
1309 || mch_chdir((char *)dirnow) != 0)
1310 *dirnow = NUL;
1311 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
1312 {
1313 if (vim_chdirfile(fname, NULL) == OK)
1314 shorten_fnames(TRUE);
1315 }
1316 else if (*dirnow != NUL
1317 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
1318 {
1319 if (mch_chdir((char *)globaldir) == 0)
1320 shorten_fnames(TRUE);
1321 }
1322
1323 failed |= (makeopens(fd, dirnow) == FAIL);
1324
1325 // restore original dir
1326 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
1327 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
1328 {
1329 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001330 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar84538072019-07-28 14:15:42 +02001331 shorten_fnames(TRUE);
1332 }
1333 vim_free(dirnow);
1334 }
1335 }
1336 else
1337 {
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001338 failed |= (put_view(fd, curwin, !using_vdir, flagp, -1, NULL)
1339 == FAIL);
Bram Moolenaar84538072019-07-28 14:15:42 +02001340 }
Bram Moolenaar38890832020-11-01 17:40:54 +01001341 if (put_line(fd, "let &g:so = s:so_save | let &g:siso = s:siso_save")
Bram Moolenaar84538072019-07-28 14:15:42 +02001342 == FAIL)
1343 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001344#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar84538072019-07-28 14:15:42 +02001345 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
1346 failed = TRUE;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001347#endif
Bram Moolenaar84538072019-07-28 14:15:42 +02001348 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
1349 failed = TRUE;
1350 if (eap->cmdidx == CMD_mksession)
1351 {
1352 if (put_line(fd, "unlet SessionLoad") == FAIL)
1353 failed = TRUE;
1354 }
1355 }
1356#endif
1357 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
1358 failed = TRUE;
1359
1360 failed |= fclose(fd);
1361
1362 if (failed)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001363 emsg(_(e_error_while_writing));
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001364#if defined(FEAT_SESSION)
Bram Moolenaar84538072019-07-28 14:15:42 +02001365 else if (eap->cmdidx == CMD_mksession)
1366 {
1367 // successful session write - set this_session var
1368 char_u *tbuf;
1369
1370 tbuf = alloc(MAXPATHL);
1371 if (tbuf != NULL)
1372 {
1373 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
1374 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
1375 vim_free(tbuf);
1376 }
1377 }
1378#endif
1379#ifdef MKSESSION_NL
1380 mksession_nl = FALSE;
1381#endif
1382 }
1383
1384#ifdef FEAT_BROWSE
1385theend:
1386 vim_free(browseFile);
1387#endif
1388#ifdef FEAT_SESSION
1389 vim_free(viewFile);
1390#endif
Colin Kennedye5f22802024-03-26 18:20:16 +01001391
1392 apply_autocmds(EVENT_SESSIONWRITEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar84538072019-07-28 14:15:42 +02001393}
1394
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02001395#if (defined(FEAT_VIMINFO) || defined(FEAT_SESSION)) || defined(PROTO)
Bram Moolenaar84538072019-07-28 14:15:42 +02001396 var_flavour_T
1397var_flavour(char_u *varname)
1398{
1399 char_u *p = varname;
1400
1401 if (ASCII_ISUPPER(*p))
1402 {
1403 while (*(++p))
1404 if (ASCII_ISLOWER(*p))
1405 return VAR_FLAVOUR_SESSION;
1406 return VAR_FLAVOUR_VIMINFO;
1407 }
1408 else
1409 return VAR_FLAVOUR_DEFAULT;
1410}
1411#endif
1412
1413/*
1414 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
1415 * Return FAIL for a write error.
1416 */
1417 int
1418put_eol(FILE *fd)
1419{
1420 if (
1421#ifdef USE_CRNL
1422 (
1423# ifdef MKSESSION_NL
1424 !mksession_nl &&
1425# endif
1426 (putc('\r', fd) < 0)) ||
1427#endif
1428 (putc('\n', fd) < 0))
1429 return FAIL;
1430 return OK;
1431}
1432
1433/*
1434 * Write a line to "fd".
1435 * Return FAIL for a write error.
1436 */
1437 int
1438put_line(FILE *fd, char *s)
1439{
1440 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
1441 return FAIL;
1442 return OK;
1443}