From a6de144c3e5cc888ab3cb7c2034a762b23566919 Mon Sep 17 00:00:00 2001 From: Eric Roberts Date: Fri, 27 Oct 2017 10:48:24 -0400 Subject: [PATCH] 'viewoptions': add "curdir" flag #7447 The flag enables the current local directory set by ":lcd" to be saved to views which is the current default behaviour. The option can be removed to disable this behaviour. closes #7435 vim-patch:8.0.1289 --- runtime/doc/options.txt | 3 +- runtime/doc/starting.txt | 2 +- src/nvim/ex_docmd.c | 15 +++--- src/nvim/option_defs.h | 21 ++++---- src/nvim/options.lua | 2 +- test/functional/ex_cmds/mkview_spec.lua | 67 +++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 test/functional/ex_cmds/mkview_spec.lua diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 026cfbe2eb..8a8ad58efd 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -6453,7 +6453,7 @@ A jump table for the options with a short description can be found at |Q_op|. security reasons. *'viewoptions'* *'vop'* -'viewoptions' 'vop' string (default: "folds,options,cursor") +'viewoptions' 'vop' string (default: "folds,options,cursor,curdir") global {not available when compiled without the |+mksession| feature} @@ -6461,6 +6461,7 @@ A jump table for the options with a short description can be found at |Q_op|. list of words. Each word enables saving and restoring something: word save and restore ~ cursor cursor position in file and in window + curdir local current directory, if set with |:lcd| folds manually created folds, opened/closed folds and local fold options options options and mappings local to a window or buffer (not diff --git a/runtime/doc/starting.txt b/runtime/doc/starting.txt index 7aba84b454..05e3f72b8d 100644 --- a/runtime/doc/starting.txt +++ b/runtime/doc/starting.txt @@ -885,7 +885,7 @@ The output of ":mkview" contains these items: 5. The scroll position and the cursor position in the file. Doesn't work very well when there are closed folds. 6. The local current directory, if it is different from the global current - directory. + directory and 'viewoptions' contains "curdir". Note that Views and Sessions are not perfect: - They don't restore everything. For example, defined functions, autocommands diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 7a2b0328df..5180420eff 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -9356,15 +9356,18 @@ put_view ( } } - /* - * Local directory. - */ - if (wp->w_localdir != NULL) { + // + // Local directory, if the current flag is not view options or the "curdir" + // option is included. + // + if (wp->w_localdir != NULL + && (flagp != &vop_flags || (*flagp & SSOP_CURDIR))) { if (fputs("lcd ", fd) < 0 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL - || put_eol(fd) == FAIL) + || put_eol(fd) == FAIL) { return FAIL; - did_lcd = TRUE; + } + did_lcd = true; } return OK; diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index a2fe90b3c2..1f62490ab9 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -539,7 +539,7 @@ static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize", "localoptions", "options", "help", "blank", "globals", "slash", "unix", "sesdir", "curdir", "folds", "cursor", - "tabpages", NULL}; + "tabpages", NULL }; # endif # define SSOP_BUFFERS 0x001 # define SSOP_WINPOS 0x002 @@ -557,16 +557,17 @@ static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize", # define SSOP_FOLDS 0x2000 # define SSOP_CURSOR 0x4000 # define SSOP_TABPAGES 0x8000 -EXTERN char_u *p_sh; /* 'shell' */ -EXTERN char_u *p_shcf; /* 'shellcmdflag' */ -EXTERN char_u *p_sp; /* 'shellpipe' */ -EXTERN char_u *p_shq; /* 'shellquote' */ -EXTERN char_u *p_sxq; /* 'shellxquote' */ -EXTERN char_u *p_sxe; /* 'shellxescape' */ -EXTERN char_u *p_srr; /* 'shellredir' */ -EXTERN int p_stmp; /* 'shelltemp' */ + +EXTERN char_u *p_sh; // 'shell' +EXTERN char_u *p_shcf; // 'shellcmdflag' +EXTERN char_u *p_sp; // 'shellpipe' +EXTERN char_u *p_shq; // 'shellquote' +EXTERN char_u *p_sxq; // 'shellxquote' +EXTERN char_u *p_sxe; // 'shellxescape' +EXTERN char_u *p_srr; // 'shellredir' +EXTERN int p_stmp; // 'shelltemp' #ifdef BACKSLASH_IN_FILENAME -EXTERN int p_ssl; /* 'shellslash' */ +EXTERN int p_ssl; // 'shellslash' #endif EXTERN char_u *p_stl; // 'statusline' EXTERN int p_sr; // 'shiftround' diff --git a/src/nvim/options.lua b/src/nvim/options.lua index 29220a6b50..52e478e78a 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -2610,7 +2610,7 @@ return { deny_duplicates=true, vi_def=true, varname='p_vop', - defaults={if_true={vi="folds,options,cursor"}} + defaults={if_true={vi="folds,options,cursor,curdir"}} }, { full_name='viminfo', abbreviation='vi', diff --git a/test/functional/ex_cmds/mkview_spec.lua b/test/functional/ex_cmds/mkview_spec.lua new file mode 100644 index 0000000000..97a49dbbd5 --- /dev/null +++ b/test/functional/ex_cmds/mkview_spec.lua @@ -0,0 +1,67 @@ +local lfs = require('lfs') +local helpers = require('test.functional.helpers')(after_each) + +local clear = helpers.clear +local command = helpers.command +local get_pathsep = helpers.get_pathsep +local eq = helpers.eq +local funcs = helpers.funcs +local rmdir = helpers.rmdir + +local file_prefix = 'Xtest-functional-ex_cmds-mkview_spec' + +describe(':mkview', function() + local tmp_file_base = file_prefix .. '-tmpfile' + local local_dir = file_prefix .. '.d' + local view_dir = file_prefix .. '.view.d' + + before_each(function() + clear() + lfs.mkdir(view_dir) + lfs.mkdir(local_dir) + end) + + after_each(function() + -- Remove any views created in the view directory + rmdir(view_dir) + lfs.rmdir(local_dir) + end) + + it('viewoption curdir restores local current directory', function() + local cwd_dir = funcs.getcwd() + local set_view_dir_command = 'set viewdir=' .. cwd_dir .. + get_pathsep() .. view_dir + + -- By default the local current directory should save + command(set_view_dir_command) + command('edit ' .. tmp_file_base .. '1') + command('lcd ' .. local_dir) + command('mkview') + + -- Create a new instance of Nvim to remove the 'lcd' + clear() + + -- Disable saving the local current directory for the second view + command(set_view_dir_command) + command('set viewoptions-=curdir') + command('edit ' .. tmp_file_base .. '2') + command('lcd ' .. local_dir) + command('mkview') + + -- Create a new instance of Nvim to test saved 'lcd' option + clear() + command(set_view_dir_command) + + -- Load the view without a saved local current directory + command('edit ' .. tmp_file_base .. '2') + command('loadview') + -- The view's current directory should not have changed + eq(cwd_dir, funcs.getcwd()) + -- Load the view with a saved local current directory + command('edit ' .. tmp_file_base .. '1') + command('loadview') + -- The view's local directory should have been saved + eq(cwd_dir .. get_pathsep() .. local_dir, funcs.getcwd()) + end) + +end)