comparison src/misc2.c @ 17536:e00d12c085a5 v8.1.1766

patch 8.1.1766: code for writing session file is spread out commit https://github.com/vim/vim/commit/845380791196aec7f991987ebf7b22de3779d106 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 28 14:15:42 2019 +0200 patch 8.1.1766: code for writing session file is spread out Problem: Code for writing session file is spread out. Solution: Put it in one file. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/4728)
author Bram Moolenaar <Bram@vim.org>
date Sun, 28 Jul 2019 14:30:07 +0200
parents e24bbd061233
children 9efb4dda9720
comparison
equal deleted inserted replaced
17535:3015901aaaa6 17536:e00d12c085a5
4676 (*argv)[*argc] = NULL; 4676 (*argv)[*argc] = NULL;
4677 return OK; 4677 return OK;
4678 } 4678 }
4679 # endif 4679 # endif
4680 #endif 4680 #endif
4681
4682 #if defined(FEAT_SESSION) || defined(PROTO)
4683 /*
4684 * Generate a script that can be used to restore the current editing session.
4685 * Save the value of v:this_session before running :mksession in order to make
4686 * automagic session save fully transparent. Return TRUE on success.
4687 */
4688 int
4689 write_session_file(char_u *filename)
4690 {
4691 char_u *escaped_filename;
4692 char *mksession_cmdline;
4693 unsigned int save_ssop_flags;
4694 int failed;
4695
4696 /*
4697 * Build an ex command line to create a script that restores the current
4698 * session if executed. Escape the filename to avoid nasty surprises.
4699 */
4700 escaped_filename = vim_strsave_escaped(filename, escape_chars);
4701 if (escaped_filename == NULL)
4702 return FALSE;
4703 mksession_cmdline = alloc(10 + (int)STRLEN(escaped_filename) + 1);
4704 if (mksession_cmdline == NULL)
4705 {
4706 vim_free(escaped_filename);
4707 return FALSE;
4708 }
4709 strcpy(mksession_cmdline, "mksession ");
4710 STRCAT(mksession_cmdline, escaped_filename);
4711 vim_free(escaped_filename);
4712
4713 /*
4714 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
4715 * unpredictable effects when the session is saved automatically. Also,
4716 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
4717 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
4718 * enormously large if the GNOME session feature is used regularly.
4719 */
4720 save_ssop_flags = ssop_flags;
4721 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
4722 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
4723
4724 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
4725 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
4726 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
4727 do_unlet((char_u *)"Save_VV_this_session", TRUE);
4728
4729 ssop_flags = save_ssop_flags;
4730 vim_free(mksession_cmdline);
4731
4732 /*
4733 * Reopen the file and append a command to restore v:this_session,
4734 * as if this save never happened. This is to avoid conflicts with
4735 * the user's own sessions. FIXME: It's probably less hackish to add
4736 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
4737 */
4738 if (!failed)
4739 {
4740 FILE *fd;
4741
4742 fd = open_exfile(filename, TRUE, APPENDBIN);
4743
4744 failed = (fd == NULL
4745 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
4746 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
4747
4748 if (fd != NULL && fclose(fd) != 0)
4749 failed = TRUE;
4750
4751 if (failed)
4752 mch_remove(filename);
4753 }
4754
4755 return !failed;
4756 }
4757 #endif