From c2dd07448ff1b2922e8ea14f909a9cbde481e5fc Mon Sep 17 00:00:00 2001 From: Keerthan Jaic Date: Tue, 4 Aug 2015 21:41:38 -0400 Subject: option,main: Partial support of XDG base directory specification - Add functions that are able to query XDG. - Replace defaults for - &runtimepath. Does not follow #78. - &viewdir. - &undodir. - &directory. - &backupdir. Does not follow #78. - vimrc location. - Remove user vimrc file line from :version message. --- src/nvim/option.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index a578f2bb01..c419e58f7e 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -301,6 +301,15 @@ static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", # include "option.c.generated.h" #endif +static void set_runtimepath_default(void) +{ + garray_T rtp_ga; + ga_init(&rtp_ga, (int)sizeof(const char *), 1); + GA_APPEND(const char *, &rtp_ga, get_user_conf_dir()); + GA_APPEND(const char *, &rtp_ga, concat_fnames(get_user_conf_dir(), "after", true)); + set_string_default("runtimepath", ga_concat_strings(&rtp_ga)); +} + /* * Initialize the options, first part. * @@ -437,6 +446,12 @@ void set_init_1(void) "system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error" ); + set_string_default("viewdir", (char_u *)get_from_user_data("view")); + set_string_default("backupdir", (char_u *)get_from_user_data("backup")); + set_string_default("directory", (char_u *)get_from_user_data("swap")); + set_string_default("undodir", (char_u *)get_from_user_data("undo")); + set_runtimepath_default(); + /* * Set all the options (except the terminal options) to their default * value. Also set the global value for local options. -- cgit From 76e2788d87f8474055536efbbc4827ddf290bc89 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 1 Aug 2015 03:04:28 +0300 Subject: option: Use different default value for &runtimepath --- src/nvim/option.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 5 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index c419e58f7e..5d614e4367 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -301,13 +301,87 @@ static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", # include "option.c.generated.h" #endif +/// Set &runtimepath to default value static void set_runtimepath_default(void) { - garray_T rtp_ga; - ga_init(&rtp_ga, (int)sizeof(const char *), 1); - GA_APPEND(const char *, &rtp_ga, get_user_conf_dir()); - GA_APPEND(const char *, &rtp_ga, concat_fnames(get_user_conf_dir(), "after", true)); - set_string_default("runtimepath", ga_concat_strings(&rtp_ga)); + size_t rtp_size = 0; + char *const data_home = vim_getenv("XDG_DATA_HOME"); + char *const config_home = vim_getenv("XDG_CONFIG_HOME"); + char *const vimruntime = vim_getenv("VIMRUNTIME"); + char *const data_dirs = vim_getenv("XDG_DATA_DIRS"); + char *const config_dirs = vim_getenv("XDG_CONFIG_DIRS"); + assert(data_home != NULL); + assert(config_home != NULL); + assert(vimruntime != NULL); + assert(data_dirs != NULL); + assert(config_dirs != NULL); +#define NVIM_SIZE (sizeof("/nvim") - 1) +#define AFTER_SIZE (sizeof("/after") - 1) + const size_t data_len = strlen(data_home); + const size_t config_len = strlen(config_home); + const size_t vimruntime_len = strlen(vimruntime); + rtp_size += ((data_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; + rtp_size += ((config_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; + rtp_size += vimruntime_len; +#define COMPUTE_COLON_LEN(rtp_size, val) \ + do { \ + const void *iter = NULL; \ + do { \ + size_t dir_len; \ + const char *dir; \ + iter = vim_colon_env_iter(val, iter, &dir, &dir_len); \ + if (dir != NULL && dir_len > 0) { \ + rtp_size += ((dir_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; \ + } \ + } while (iter != NULL); \ + } while (0) + COMPUTE_COLON_LEN(rtp_size, data_dirs); + COMPUTE_COLON_LEN(rtp_size, config_dirs); +#undef COMPUTE_COLON_LEN + char *const rtp = xmallocz(rtp_size); + char *rtp_cur = rtp; +#define ADD_STRING(tgt, src, len) \ + do { memmove(tgt, src, len); tgt += len; } while (0) +#define ADD_STATIC_STRING(tgt, str) \ + ADD_STRING(tgt, str, sizeof(str) - 1) +#define ADD_COLON_DIRS(tgt, val, suffix, revsuffix) \ + do { \ + const void *iter = NULL; \ + do { \ + size_t dir_len; \ + const char *dir; \ + iter = vim_colon_env_iter##revsuffix(val, iter, &dir, &dir_len); \ + if (dir != NULL && dir_len > 0) { \ + ADD_STRING(rtp_cur, dir, dir_len); \ + ADD_STATIC_STRING(rtp_cur, "/nvim" suffix ","); \ + } \ + } while (iter != NULL); \ + } while (0) + ADD_STRING(rtp_cur, config_home, config_len); + ADD_STATIC_STRING(rtp_cur, "/nvim,"); + ADD_STRING(rtp_cur, data_home, data_len); + ADD_STATIC_STRING(rtp_cur, "/nvim,"); + ADD_COLON_DIRS(rtp_cur, config_dirs, "", ); + ADD_COLON_DIRS(rtp_cur, data_dirs, "", ); + ADD_STRING(rtp_cur, vimruntime, vimruntime_len); + *rtp_cur++ = ','; + ADD_COLON_DIRS(rtp_cur, data_dirs, "/after", _rev); + ADD_COLON_DIRS(rtp_cur, config_dirs, "/after", _rev); + ADD_STRING(rtp_cur, data_home, data_len); + ADD_STATIC_STRING(rtp_cur, "/nvim/after,"); + ADD_STRING(rtp_cur, config_home, config_len); + ADD_STATIC_STRING(rtp_cur, "/nvim/after"); +#undef ADD_COLON_DIRS +#undef ADD_STATIC_STRING +#undef ADD_STRING +#undef NVIM_SIZE +#undef AFTER_SIZE + set_string_default("runtimepath", (char_u *)rtp); + xfree(data_dirs); + xfree(config_dirs); + xfree(data_home); + xfree(config_home); + xfree(vimruntime); } /* @@ -450,6 +524,8 @@ void set_init_1(void) set_string_default("backupdir", (char_u *)get_from_user_data("backup")); set_string_default("directory", (char_u *)get_from_user_data("swap")); set_string_default("undodir", (char_u *)get_from_user_data("undo")); + // Set default for &runtimepath. All necessary expansions are performed in + // this function. set_runtimepath_default(); /* -- cgit From efb6045a00d6da77aea51547dcd8f01754b5179c Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 1 Aug 2015 03:14:58 +0300 Subject: option: Add /site subdirectory to data directories --- src/nvim/option.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 5d614e4367..e038205101 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -316,14 +316,15 @@ static void set_runtimepath_default(void) assert(data_dirs != NULL); assert(config_dirs != NULL); #define NVIM_SIZE (sizeof("/nvim") - 1) +#define SITE_SIZE (sizeof("/site") - 1) #define AFTER_SIZE (sizeof("/after") - 1) const size_t data_len = strlen(data_home); const size_t config_len = strlen(config_home); const size_t vimruntime_len = strlen(vimruntime); - rtp_size += ((data_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; + rtp_size += ((data_len + NVIM_SIZE + SITE_SIZE) * 2 + AFTER_SIZE) + 2; rtp_size += ((config_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; rtp_size += vimruntime_len; -#define COMPUTE_COLON_LEN(rtp_size, val) \ +#define COMPUTE_COLON_LEN(rtp_size, additional_size, val) \ do { \ const void *iter = NULL; \ do { \ @@ -331,12 +332,13 @@ static void set_runtimepath_default(void) const char *dir; \ iter = vim_colon_env_iter(val, iter, &dir, &dir_len); \ if (dir != NULL && dir_len > 0) { \ - rtp_size += ((dir_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; \ + rtp_size += ((dir_len + NVIM_SIZE + additional_size) * 2 \ + + AFTER_SIZE) + 2; \ } \ } while (iter != NULL); \ } while (0) - COMPUTE_COLON_LEN(rtp_size, data_dirs); - COMPUTE_COLON_LEN(rtp_size, config_dirs); + COMPUTE_COLON_LEN(rtp_size, SITE_SIZE, data_dirs); + COMPUTE_COLON_LEN(rtp_size, 0, config_dirs); #undef COMPUTE_COLON_LEN char *const rtp = xmallocz(rtp_size); char *rtp_cur = rtp; @@ -360,21 +362,22 @@ static void set_runtimepath_default(void) ADD_STRING(rtp_cur, config_home, config_len); ADD_STATIC_STRING(rtp_cur, "/nvim,"); ADD_STRING(rtp_cur, data_home, data_len); - ADD_STATIC_STRING(rtp_cur, "/nvim,"); + ADD_STATIC_STRING(rtp_cur, "/nvim/site,"); ADD_COLON_DIRS(rtp_cur, config_dirs, "", ); - ADD_COLON_DIRS(rtp_cur, data_dirs, "", ); + ADD_COLON_DIRS(rtp_cur, data_dirs, "/site", ); ADD_STRING(rtp_cur, vimruntime, vimruntime_len); *rtp_cur++ = ','; - ADD_COLON_DIRS(rtp_cur, data_dirs, "/after", _rev); + ADD_COLON_DIRS(rtp_cur, data_dirs, "/site/after", _rev); ADD_COLON_DIRS(rtp_cur, config_dirs, "/after", _rev); ADD_STRING(rtp_cur, data_home, data_len); - ADD_STATIC_STRING(rtp_cur, "/nvim/after,"); + ADD_STATIC_STRING(rtp_cur, "/nvim/site/after,"); ADD_STRING(rtp_cur, config_home, config_len); ADD_STATIC_STRING(rtp_cur, "/nvim/after"); #undef ADD_COLON_DIRS #undef ADD_STATIC_STRING #undef ADD_STRING #undef NVIM_SIZE +#undef SITE_SIZE #undef AFTER_SIZE set_string_default("runtimepath", (char_u *)rtp); xfree(data_dirs); -- cgit From ee95f818a66d8d46ebb237260d6edcb20b10ac61 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 1 Aug 2015 16:32:44 +0300 Subject: option: Move all data directories after all config directories --- src/nvim/option.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index e038205101..2e17708f93 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -361,16 +361,16 @@ static void set_runtimepath_default(void) } while (0) ADD_STRING(rtp_cur, config_home, config_len); ADD_STATIC_STRING(rtp_cur, "/nvim,"); + ADD_COLON_DIRS(rtp_cur, config_dirs, "", ); ADD_STRING(rtp_cur, data_home, data_len); ADD_STATIC_STRING(rtp_cur, "/nvim/site,"); - ADD_COLON_DIRS(rtp_cur, config_dirs, "", ); ADD_COLON_DIRS(rtp_cur, data_dirs, "/site", ); ADD_STRING(rtp_cur, vimruntime, vimruntime_len); *rtp_cur++ = ','; ADD_COLON_DIRS(rtp_cur, data_dirs, "/site/after", _rev); - ADD_COLON_DIRS(rtp_cur, config_dirs, "/after", _rev); ADD_STRING(rtp_cur, data_home, data_len); ADD_STATIC_STRING(rtp_cur, "/nvim/site/after,"); + ADD_COLON_DIRS(rtp_cur, config_dirs, "/after", _rev); ADD_STRING(rtp_cur, config_home, config_len); ADD_STATIC_STRING(rtp_cur, "/nvim/after"); #undef ADD_COLON_DIRS -- cgit From ab2944f46cbe53d66ae7edf7b21eaf9bec4e7b22 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 1 Aug 2015 18:53:39 +0300 Subject: option: Prepare for all environment variables being NULL. --- src/nvim/option.c | 108 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 41 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 2e17708f93..ae1c083f00 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -310,36 +310,47 @@ static void set_runtimepath_default(void) char *const vimruntime = vim_getenv("VIMRUNTIME"); char *const data_dirs = vim_getenv("XDG_DATA_DIRS"); char *const config_dirs = vim_getenv("XDG_CONFIG_DIRS"); - assert(data_home != NULL); - assert(config_home != NULL); - assert(vimruntime != NULL); - assert(data_dirs != NULL); - assert(config_dirs != NULL); #define NVIM_SIZE (sizeof("/nvim") - 1) #define SITE_SIZE (sizeof("/site") - 1) #define AFTER_SIZE (sizeof("/after") - 1) - const size_t data_len = strlen(data_home); - const size_t config_len = strlen(config_home); - const size_t vimruntime_len = strlen(vimruntime); - rtp_size += ((data_len + NVIM_SIZE + SITE_SIZE) * 2 + AFTER_SIZE) + 2; - rtp_size += ((config_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; - rtp_size += vimruntime_len; + size_t data_len; + size_t config_len; + size_t vimruntime_len; + if (data_home != NULL) { + data_len = strlen(data_home); + rtp_size += ((data_len + NVIM_SIZE + SITE_SIZE) * 2 + AFTER_SIZE) + 2; + } + if (config_home != NULL) { + config_len = strlen(config_home); + rtp_size += ((config_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; + } + if (vimruntime != NULL) { + vimruntime_len = strlen(vimruntime); + rtp_size += vimruntime_len + 1; + } #define COMPUTE_COLON_LEN(rtp_size, additional_size, val) \ do { \ - const void *iter = NULL; \ - do { \ - size_t dir_len; \ - const char *dir; \ - iter = vim_colon_env_iter(val, iter, &dir, &dir_len); \ - if (dir != NULL && dir_len > 0) { \ - rtp_size += ((dir_len + NVIM_SIZE + additional_size) * 2 \ - + AFTER_SIZE) + 2; \ - } \ - } while (iter != NULL); \ + if (val != NULL) { \ + const void *iter = NULL; \ + do { \ + size_t dir_len; \ + const char *dir; \ + iter = vim_colon_env_iter(val, iter, &dir, &dir_len); \ + if (dir != NULL && dir_len > 0) { \ + rtp_size += ((dir_len + NVIM_SIZE + additional_size) * 2 \ + + AFTER_SIZE) + 2; \ + } \ + } while (iter != NULL); \ + } \ } while (0) COMPUTE_COLON_LEN(rtp_size, SITE_SIZE, data_dirs); COMPUTE_COLON_LEN(rtp_size, 0, config_dirs); #undef COMPUTE_COLON_LEN + if (rtp_size == 0) { + return; + } + // All additions were including comma. + rtp_size--; char *const rtp = xmallocz(rtp_size); char *rtp_cur = rtp; #define ADD_STRING(tgt, src, len) \ @@ -348,31 +359,46 @@ static void set_runtimepath_default(void) ADD_STRING(tgt, str, sizeof(str) - 1) #define ADD_COLON_DIRS(tgt, val, suffix, revsuffix) \ do { \ - const void *iter = NULL; \ - do { \ - size_t dir_len; \ - const char *dir; \ - iter = vim_colon_env_iter##revsuffix(val, iter, &dir, &dir_len); \ - if (dir != NULL && dir_len > 0) { \ - ADD_STRING(rtp_cur, dir, dir_len); \ - ADD_STATIC_STRING(rtp_cur, "/nvim" suffix ","); \ - } \ - } while (iter != NULL); \ + if (val != NULL) { \ + const void *iter = NULL; \ + do { \ + size_t dir_len; \ + const char *dir; \ + iter = vim_colon_env_iter##revsuffix(val, iter, &dir, &dir_len); \ + if (dir != NULL && dir_len > 0) { \ + ADD_STRING(rtp_cur, dir, dir_len); \ + ADD_STATIC_STRING(rtp_cur, "/nvim" suffix ","); \ + } \ + } while (iter != NULL); \ + } \ } while (0) - ADD_STRING(rtp_cur, config_home, config_len); - ADD_STATIC_STRING(rtp_cur, "/nvim,"); + if (config_home != NULL) { + ADD_STRING(rtp_cur, config_home, config_len); + ADD_STATIC_STRING(rtp_cur, "/nvim,"); + } ADD_COLON_DIRS(rtp_cur, config_dirs, "", ); - ADD_STRING(rtp_cur, data_home, data_len); - ADD_STATIC_STRING(rtp_cur, "/nvim/site,"); + if (data_home != NULL) { + ADD_STRING(rtp_cur, data_home, data_len); + ADD_STATIC_STRING(rtp_cur, "/nvim/site,"); + } ADD_COLON_DIRS(rtp_cur, data_dirs, "/site", ); - ADD_STRING(rtp_cur, vimruntime, vimruntime_len); - *rtp_cur++ = ','; + if (vimruntime != NULL) { + ADD_STRING(rtp_cur, vimruntime, vimruntime_len); + *rtp_cur++ = ','; + } ADD_COLON_DIRS(rtp_cur, data_dirs, "/site/after", _rev); - ADD_STRING(rtp_cur, data_home, data_len); - ADD_STATIC_STRING(rtp_cur, "/nvim/site/after,"); + if (data_home != NULL) { + ADD_STRING(rtp_cur, data_home, data_len); + ADD_STATIC_STRING(rtp_cur, "/nvim/site/after,"); + } ADD_COLON_DIRS(rtp_cur, config_dirs, "/after", _rev); - ADD_STRING(rtp_cur, config_home, config_len); - ADD_STATIC_STRING(rtp_cur, "/nvim/after"); + if (config_home != NULL) { + ADD_STRING(rtp_cur, config_home, config_len); + ADD_STATIC_STRING(rtp_cur, "/nvim/after"); + } else { + // Strip trailing comma. + rtp[rtp_size - 1] = NUL; + } #undef ADD_COLON_DIRS #undef ADD_STATIC_STRING #undef ADD_STRING -- cgit From 8e2c0fdba5cf8d7c0fe00ddda1ebe702e405d918 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 2 Aug 2015 09:35:37 +0300 Subject: option: Append with escaped commas --- src/nvim/option.c | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index ae1c083f00..97dc88833c 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -301,6 +301,33 @@ static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", # include "option.c.generated.h" #endif +/// Count commas in the given string +static size_t count_commas(const char *const s, size_t len) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + size_t ret = 0; + for (size_t i = 0; i < len; i++) { + if (s[i] == ',') { + ret++; + } + } + return ret; +} + +/// Append string with escaped commas +static char *strcpy_comma_escaped(char *dest, const char *src, const size_t len) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +{ + size_t shift = 0; + for (size_t i = 0; i < len; i++) { + if (src[i] == ',') { + dest[i + shift++] = '\\'; + } + dest[i + shift] = src[i]; + } + return &dest[len + shift]; +} + /// Set &runtimepath to default value static void set_runtimepath_default(void) { @@ -318,15 +345,17 @@ static void set_runtimepath_default(void) size_t vimruntime_len; if (data_home != NULL) { data_len = strlen(data_home); - rtp_size += ((data_len + NVIM_SIZE + SITE_SIZE) * 2 + AFTER_SIZE) + 2; + rtp_size += ((data_len + count_commas(data_home, data_len) + + NVIM_SIZE + SITE_SIZE) * 2 + AFTER_SIZE) + 2; } if (config_home != NULL) { config_len = strlen(config_home); - rtp_size += ((config_len + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; + rtp_size += ((config_len + count_commas(config_home, config_len) + + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; } if (vimruntime != NULL) { vimruntime_len = strlen(vimruntime); - rtp_size += vimruntime_len + 1; + rtp_size += vimruntime_len + count_commas(vimruntime, vimruntime_len) + 1; } #define COMPUTE_COLON_LEN(rtp_size, additional_size, val) \ do { \ @@ -337,7 +366,8 @@ static void set_runtimepath_default(void) const char *dir; \ iter = vim_colon_env_iter(val, iter, &dir, &dir_len); \ if (dir != NULL && dir_len > 0) { \ - rtp_size += ((dir_len + NVIM_SIZE + additional_size) * 2 \ + rtp_size += ((dir_len + count_commas(dir, dir_len) \ + + NVIM_SIZE + additional_size) * 2 \ + AFTER_SIZE) + 2; \ } \ } while (iter != NULL); \ @@ -354,9 +384,9 @@ static void set_runtimepath_default(void) char *const rtp = xmallocz(rtp_size); char *rtp_cur = rtp; #define ADD_STRING(tgt, src, len) \ - do { memmove(tgt, src, len); tgt += len; } while (0) -#define ADD_STATIC_STRING(tgt, str) \ - ADD_STRING(tgt, str, sizeof(str) - 1) + tgt = strcpy_comma_escaped(tgt, src, len) +#define ADD_STATIC_STRING(tgt, src) \ + do { memmove(tgt, src, sizeof(src) - 1); tgt += sizeof(src) - 1; } while (0) #define ADD_COLON_DIRS(tgt, val, suffix, revsuffix) \ do { \ if (val != NULL) { \ @@ -397,7 +427,7 @@ static void set_runtimepath_default(void) ADD_STATIC_STRING(rtp_cur, "/nvim/after"); } else { // Strip trailing comma. - rtp[rtp_size - 1] = NUL; + rtp[rtp_size] = NUL; } #undef ADD_COLON_DIRS #undef ADD_STATIC_STRING -- cgit From afb0f2f9b14f0ebcb5c53a47338a6a4359019e38 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 14:40:27 +0300 Subject: stdpaths: Rename export functions so that they have common prefix --- src/nvim/option.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 97dc88833c..c2236114d6 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -579,11 +579,12 @@ void set_init_1(void) "system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error" ); - set_string_default("viewdir", (char_u *)get_from_user_data("view")); - set_string_default("backupdir", (char_u *)get_from_user_data("backup")); - set_string_default("directory", (char_u *)get_from_user_data("swap")); - set_string_default("undodir", (char_u *)get_from_user_data("undo")); - // Set default for &runtimepath. All necessary expansions are performed in + set_string_default("viewdir", (char_u *)stdpaths_user_data_subpath("view")); + set_string_default("backupdir", + (char_u *)stdpaths_user_data_subpath("backup")); + set_string_default("directory", (char_u *)stdpaths_user_data_subpath("swap")); + set_string_default("undodir", (char_u *)stdpaths_user_data_subpath("undo")); + // Set default for &runtimepath. All necessary expansions are performed in // this function. set_runtimepath_default(); -- cgit From be91bc1e1a110da938201eab9b43736e64a7392e Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 14:45:53 +0300 Subject: stdpaths: Export get_xdg function (renamed) and use it for runtimepath --- src/nvim/option.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index c2236114d6..b879580ff1 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -332,11 +332,11 @@ static char *strcpy_comma_escaped(char *dest, const char *src, const size_t len) static void set_runtimepath_default(void) { size_t rtp_size = 0; - char *const data_home = vim_getenv("XDG_DATA_HOME"); - char *const config_home = vim_getenv("XDG_CONFIG_HOME"); + char *const data_home = stdpaths_get_xdg_var(kXDGDataHome); + char *const config_home = stdpaths_get_xdg_var(kXDGConfigHome); char *const vimruntime = vim_getenv("VIMRUNTIME"); - char *const data_dirs = vim_getenv("XDG_DATA_DIRS"); - char *const config_dirs = vim_getenv("XDG_CONFIG_DIRS"); + char *const data_dirs = stdpaths_get_xdg_var(kXDGDataDirs); + char *const config_dirs = stdpaths_get_xdg_var(kXDGConfigDirs); #define NVIM_SIZE (sizeof("/nvim") - 1) #define SITE_SIZE (sizeof("/site") - 1) #define AFTER_SIZE (sizeof("/after") - 1) -- cgit From 642a07ce2770428e5114200b4292f5a76bb90fa2 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 15:04:44 +0300 Subject: option: Remove some memory leaks --- src/nvim/option.c | 72 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 34 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index b879580ff1..7000fc4067 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -435,7 +435,7 @@ static void set_runtimepath_default(void) #undef NVIM_SIZE #undef SITE_SIZE #undef AFTER_SIZE - set_string_default("runtimepath", (char_u *)rtp); + set_string_default("runtimepath", rtp, true); xfree(data_dirs); xfree(config_dirs); xfree(data_home); @@ -450,7 +450,6 @@ static void set_runtimepath_default(void) */ void set_init_1(void) { - char_u *p; int opt_idx; langmap_init(); @@ -462,8 +461,12 @@ void set_init_1(void) * Find default value for 'shell' option. * Don't use it if it is empty. */ - if ((p = (char_u *)os_getenv("SHELL")) != NULL) - set_string_default("sh", p); + { + const char *shell = os_getenv("SHELL"); + if (shell != NULL) { + set_string_default("sh", (char *) shell, false); + } + } /* * Set the default for 'backupskip' to include environment variables for @@ -481,17 +484,18 @@ void set_init_1(void) ga_init(&ga, 1, 100); for (size_t n = 0; n < ARRAY_SIZE(names); ++n) { bool mustfree = true; + char *p; # ifdef UNIX if (*names[n] == NUL) { - p = (char_u *)"/tmp"; + p = "/tmp"; mustfree = false; } else # endif - p = (char_u *)vim_getenv(names[n]); + p = vim_getenv(names[n]); if (p != NULL && *p != NUL) { // First time count the NUL, otherwise count the ','. - len = (int)STRLEN(p) + 3; + len = (int)strlen(p) + 3; ga_grow(&ga, len); if (!GA_EMPTY(&ga)) STRCAT(ga.ga_data, ","); @@ -505,8 +509,7 @@ void set_init_1(void) } } if (ga.ga_data != NULL) { - set_string_default("bsk", ga.ga_data); - xfree(ga.ga_data); + set_string_default("bsk", ga.ga_data, true); } } @@ -567,23 +570,17 @@ void set_init_1(void) #if defined(MSWIN) || defined(MAC) /* Set print encoding on platforms that don't default to latin1 */ - set_string_default("penc", - (char_u *)"hp-roman8" - ); + set_string_default("penc", "hp-roman8", false); #endif /* 'printexpr' must be allocated to be able to evaluate it. */ - set_string_default( - "pexpr", - (char_u *) - "system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error" - ); - - set_string_default("viewdir", (char_u *)stdpaths_user_data_subpath("view")); - set_string_default("backupdir", - (char_u *)stdpaths_user_data_subpath("backup")); - set_string_default("directory", (char_u *)stdpaths_user_data_subpath("swap")); - set_string_default("undodir", (char_u *)stdpaths_user_data_subpath("undo")); + set_string_default("pexpr", "system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error", false); + + + set_string_default("viewdir", stdpaths_user_data_subpath("view"), true); + set_string_default("backupdir", stdpaths_user_data_subpath("backup"), true); + set_string_default("directory", stdpaths_user_data_subpath("swap"), true); + set_string_default("undodir", stdpaths_user_data_subpath("undo"), true); // Set default for &runtimepath. All necessary expansions are performed in // this function. set_runtimepath_default(); @@ -629,14 +626,16 @@ void set_init_1(void) * default. */ for (opt_idx = 0; options[opt_idx].fullname; opt_idx++) { + char *p; if ((options[opt_idx].flags & P_GETTEXT) - && options[opt_idx].var != NULL) - p = (char_u *)_(*(char **)options[opt_idx].var); - else - p = option_expand(opt_idx, NULL); + && options[opt_idx].var != NULL) { + p = _(*(char **)options[opt_idx].var); + } else { + p = (char *) option_expand(opt_idx, NULL); + } if (p != NULL) { - p = vim_strsave(p); - *(char_u **)options[opt_idx].var = p; + p = xstrdup(p); + *(char **)options[opt_idx].var = p; /* VIMEXP * Defaults for all expanded options are currently the same for Vi * and Vim. When this changes, add some code here! Also need to @@ -644,7 +643,7 @@ void set_init_1(void) */ if (options[opt_idx].flags & P_DEF_ALLOCED) xfree(options[opt_idx].def_val[VI_DEFAULT]); - options[opt_idx].def_val[VI_DEFAULT] = p; + options[opt_idx].def_val[VI_DEFAULT] = (char_u *) p; options[opt_idx].flags |= P_DEF_ALLOCED; } } @@ -673,14 +672,14 @@ void set_init_1(void) (void)set_chars_option(&p_lcs); /* enc_locale() will try to find the encoding of the current locale. */ - p = enc_locale(); + char_u *p = enc_locale(); if (p != NULL) { char_u *save_enc; /* Try setting 'encoding' and check if the value is valid. * If not, go back to the default "utf-8". */ save_enc = p_enc; - p_enc = p; + p_enc = (char_u *) p; if (STRCMP(p_enc, "gb18030") == 0) { /* We don't support "gb18030", but "cp936" is a good substitute * for practical purposes, thus use that. It's not an alias to @@ -825,7 +824,9 @@ set_options_default ( /// /// @param name The name of the option /// @param val The value of the option -void set_string_default(const char *name, const char_u *val) +/// @param allocated If true, do not copy default as it was already allocated. +static void set_string_default(const char *name, char *val, bool allocated) + FUNC_ATTR_NONNULL_ALL { int opt_idx = findoption((char_u *)name); if (opt_idx >= 0) { @@ -833,7 +834,10 @@ void set_string_default(const char *name, const char_u *val) xfree(options[opt_idx].def_val[VI_DEFAULT]); } - options[opt_idx].def_val[VI_DEFAULT] = (char_u *) xstrdup((char *) val); + options[opt_idx].def_val[VI_DEFAULT] = (char_u *) ( + allocated + ? (char_u *) val + : (char_u *) xstrdup(val)); options[opt_idx].flags |= P_DEF_ALLOCED; } } -- cgit From 0a59c969cc2850940f0791ce2944fa7f4ee3de02 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 15:09:50 +0300 Subject: option: Use proper printexpr --- src/nvim/option.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 7000fc4067..13ac1503d5 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -570,12 +570,25 @@ void set_init_1(void) #if defined(MSWIN) || defined(MAC) /* Set print encoding on platforms that don't default to latin1 */ - set_string_default("penc", "hp-roman8", false); + set_string_default("printencoding", "hp-roman8", false); #endif - /* 'printexpr' must be allocated to be able to evaluate it. */ - set_string_default("pexpr", "system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error", false); - + // 'printexpr' must be allocated to be able to evaluate it. + set_string_default("printexpr", +#ifdef UNIX + "system(['lpr'] " + "+ (empty(&printdevice)?[]:['-P', &printdevice]) " + "+ [v:fname_in])" + ". delete(v:fname_in)" + "+ v:shell_error", +#elif defined(MSWIN) + "system(['copy', v:fname_in, " + "empty(&printdevice)?'LPT1':&printdevice])" + ". delete(v:fname_in)", +#else + "", +#endif + false); set_string_default("viewdir", stdpaths_user_data_subpath("view"), true); set_string_default("backupdir", stdpaths_user_data_subpath("backup"), true); -- cgit From 1cdc3298cfb8389b2bc3203da16abc74abb0a0c0 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 17:25:53 +0300 Subject: documentation: Update documentation Note about ~/.local/share/nvim/site used in one usr_\* file: this one talks about user-local installation of third-party plugins, and ~/.local/share/nvim/site is the proper place for them. Most other files talk about user own configuration and this is ~/.config. --- src/nvim/option.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 13ac1503d5..b4b0ff0c43 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -6116,7 +6116,7 @@ static void paste_option_changed(void) old_p_paste = p_paste; } -/// vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found. +/// vimrc_found() - Called when a vimrc or "VIMINIT" has been found. /// /// Set the values for options that didn't get set yet to the Vim defaults. /// When "fname" is not NULL, use it to set $"envname" when it wasn't set yet. -- cgit From 86a6ff7b9d2c669ea711ba53432300c017f8a3b1 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 19:31:31 +0300 Subject: option: Move macros to functions, use PATHSEP in place of / --- src/nvim/option.c | 247 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 170 insertions(+), 77 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index b4b0ff0c43..a449deb014 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -328,6 +328,146 @@ static char *strcpy_comma_escaped(char *dest, const char *src, const size_t len) return &dest[len + shift]; } +/// Compute length of a colon-separated value, doubled and with some suffixes +/// +/// @param[in] val Colon-separated array value. +/// @param[in] common_suf_len Length of the common suffix which is appended to +/// each item in the array, twice. +/// @param[in] single_suf_len Length of the suffix which is appended to each +/// item in the array once. +/// +/// @return Length of the comma-separated string array that contains each item +/// in the original array twice with suffixes with given length +/// (common_suf is present after each new item, single_suf is present +/// after half of the new items) and with commas after each item, commas +/// inside the values are escaped. +static inline size_t compute_double_colon_len(const char *const val, + const size_t common_suf_len, + const size_t single_suf_len) + FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE +{ + if (val == NULL) { + return 0; + } + size_t ret = 0; + const void *iter = NULL; + do { + size_t dir_len; + const char *dir; + iter = vim_colon_env_iter(val, iter, &dir, &dir_len); + if (dir != NULL && dir_len > 0) { + ret += ((dir_len + count_commas(dir, dir_len) + common_suf_len + 1) * 2 + + single_suf_len); + } + } while (iter != NULL); + return ret; +} + +#define NVIM_SIZE (sizeof("nvim") - 1) + +/// Add directories to a comma-separated array from a colon-separated one +/// +/// Commas are escaped in process. To each item PATHSEP "nvim" is appended in +/// addition to suf1 and suf2. +/// +/// @param[in,out] dest Destination comma-separated array. +/// @param[in] val Source colon-separated array. +/// @param[in] suf1 If not NULL, suffix appended to destination. Prior to it +/// directory separator is appended. Suffix must not contain +/// commas. +/// @param[in] len1 Length of the suf1. +/// @param[in] suf2 If not NULL, another suffix appended to destination. Again +/// with directory separator behind. Suffix must not contain +/// commas. +/// @param[in] len2 Length of the suf2. +/// @param[in] forward If true, iterate over val in forward direction. +/// Otherwise in reverse. +/// +/// @return (dest + appended_characters_length) +static inline char *add_colon_dirs(char *dest, const char *const val, + const char *const suf1, const size_t len1, + const char *const suf2, const size_t len2, + const bool forward) + FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ARG(1) +{ + if (val == NULL) { + return dest; + } + const void *iter = NULL; + do { + size_t dir_len; + const char *dir; + iter = (forward ? vim_colon_env_iter : vim_colon_env_iter_rev)( + val, iter, &dir, &dir_len); + if (dir != NULL && dir_len > 0) { + dest = strcpy_comma_escaped(dest, dir, dir_len); + *dest++ = PATHSEP; + memmove(dest, "nvim", NVIM_SIZE); + dest += NVIM_SIZE; + if (suf1 != NULL) { + *dest++ = PATHSEP; + memmove(dest, suf1, len1); + dest += len1; + if (suf2 != NULL) { + *dest++ = PATHSEP; + memmove(dest, suf2, len2); + dest += len2; + } + } + *dest++ = ','; + } + } while (iter != NULL); + return dest; +} + +/// Add directory to a comma-separated list of directories +/// +/// In the added directory comma is escaped. +/// +/// @param[in,out] dest Destination comma-separated array. +/// @param[in] dir Directory to append. +/// @param[in] append_nvim If true, append "nvim" as the very first suffix. +/// @param[in] suf1 If not NULL, suffix appended to destination. Prior to it +/// directory separator is appended. Suffix must not contain +/// commas. +/// @param[in] len1 Length of the suf1. +/// @param[in] suf2 If not NULL, another suffix appended to destination. Again +/// with directory separator behind. Suffix must not contain +/// commas. +/// @param[in] len2 Length of the suf2. +/// @param[in] forward If true, iterate over val in forward direction. +/// Otherwise in reverse. +/// +/// @return (dest + appended_characters_length) +static inline char *add_dir(char *dest, const char *const dir, + const size_t dir_len, const bool append_nvim, + const char *const suf1, const size_t len1, + const char *const suf2, const size_t len2) + FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_WARN_UNUSED_RESULT +{ + if (dir == NULL) { + return dest; + } + dest = strcpy_comma_escaped(dest, dir, dir_len); + if (append_nvim) { + *dest++ = PATHSEP; + memmove(dest, "nvim", NVIM_SIZE); + dest += NVIM_SIZE; + if (suf1 != NULL) { + *dest++ = PATHSEP; + memmove(dest, suf1, len1); + dest += len1; + if (suf2 != NULL) { + *dest++ = PATHSEP; + memmove(dest, suf2, len2); + dest += len2; + } + } + } + *dest++ = ','; + return dest; +} + /// Set &runtimepath to default value static void set_runtimepath_default(void) { @@ -337,102 +477,53 @@ static void set_runtimepath_default(void) char *const vimruntime = vim_getenv("VIMRUNTIME"); char *const data_dirs = stdpaths_get_xdg_var(kXDGDataDirs); char *const config_dirs = stdpaths_get_xdg_var(kXDGConfigDirs); -#define NVIM_SIZE (sizeof("/nvim") - 1) -#define SITE_SIZE (sizeof("/site") - 1) -#define AFTER_SIZE (sizeof("/after") - 1) +#define SITE_SIZE (sizeof("site") - 1) +#define AFTER_SIZE (sizeof("after") - 1) size_t data_len; size_t config_len; size_t vimruntime_len; if (data_home != NULL) { data_len = strlen(data_home); rtp_size += ((data_len + count_commas(data_home, data_len) - + NVIM_SIZE + SITE_SIZE) * 2 + AFTER_SIZE) + 2; + + NVIM_SIZE + 1 + SITE_SIZE + 1 + 1) * 2 + AFTER_SIZE + 1); } if (config_home != NULL) { config_len = strlen(config_home); rtp_size += ((config_len + count_commas(config_home, config_len) - + NVIM_SIZE) * 2 + AFTER_SIZE) + 2; + + NVIM_SIZE + 1 + 1) * 2 + AFTER_SIZE + 1); } if (vimruntime != NULL) { vimruntime_len = strlen(vimruntime); rtp_size += vimruntime_len + count_commas(vimruntime, vimruntime_len) + 1; } -#define COMPUTE_COLON_LEN(rtp_size, additional_size, val) \ - do { \ - if (val != NULL) { \ - const void *iter = NULL; \ - do { \ - size_t dir_len; \ - const char *dir; \ - iter = vim_colon_env_iter(val, iter, &dir, &dir_len); \ - if (dir != NULL && dir_len > 0) { \ - rtp_size += ((dir_len + count_commas(dir, dir_len) \ - + NVIM_SIZE + additional_size) * 2 \ - + AFTER_SIZE) + 2; \ - } \ - } while (iter != NULL); \ - } \ - } while (0) - COMPUTE_COLON_LEN(rtp_size, SITE_SIZE, data_dirs); - COMPUTE_COLON_LEN(rtp_size, 0, config_dirs); -#undef COMPUTE_COLON_LEN + rtp_size += compute_double_colon_len(data_dirs, NVIM_SIZE + 1 + SITE_SIZE + 1, + AFTER_SIZE + 1); + rtp_size += compute_double_colon_len(config_dirs, NVIM_SIZE + 1, + AFTER_SIZE + 1); if (rtp_size == 0) { return; } - // All additions were including comma. - rtp_size--; - char *const rtp = xmallocz(rtp_size); + char *const rtp = xmalloc(rtp_size); char *rtp_cur = rtp; -#define ADD_STRING(tgt, src, len) \ - tgt = strcpy_comma_escaped(tgt, src, len) -#define ADD_STATIC_STRING(tgt, src) \ - do { memmove(tgt, src, sizeof(src) - 1); tgt += sizeof(src) - 1; } while (0) -#define ADD_COLON_DIRS(tgt, val, suffix, revsuffix) \ - do { \ - if (val != NULL) { \ - const void *iter = NULL; \ - do { \ - size_t dir_len; \ - const char *dir; \ - iter = vim_colon_env_iter##revsuffix(val, iter, &dir, &dir_len); \ - if (dir != NULL && dir_len > 0) { \ - ADD_STRING(rtp_cur, dir, dir_len); \ - ADD_STATIC_STRING(rtp_cur, "/nvim" suffix ","); \ - } \ - } while (iter != NULL); \ - } \ - } while (0) - if (config_home != NULL) { - ADD_STRING(rtp_cur, config_home, config_len); - ADD_STATIC_STRING(rtp_cur, "/nvim,"); - } - ADD_COLON_DIRS(rtp_cur, config_dirs, "", ); - if (data_home != NULL) { - ADD_STRING(rtp_cur, data_home, data_len); - ADD_STATIC_STRING(rtp_cur, "/nvim/site,"); - } - ADD_COLON_DIRS(rtp_cur, data_dirs, "/site", ); - if (vimruntime != NULL) { - ADD_STRING(rtp_cur, vimruntime, vimruntime_len); - *rtp_cur++ = ','; - } - ADD_COLON_DIRS(rtp_cur, data_dirs, "/site/after", _rev); - if (data_home != NULL) { - ADD_STRING(rtp_cur, data_home, data_len); - ADD_STATIC_STRING(rtp_cur, "/nvim/site/after,"); - } - ADD_COLON_DIRS(rtp_cur, config_dirs, "/after", _rev); - if (config_home != NULL) { - ADD_STRING(rtp_cur, config_home, config_len); - ADD_STATIC_STRING(rtp_cur, "/nvim/after"); - } else { - // Strip trailing comma. - rtp[rtp_size] = NUL; - } -#undef ADD_COLON_DIRS -#undef ADD_STATIC_STRING -#undef ADD_STRING -#undef NVIM_SIZE + rtp_cur = add_dir(rtp_cur, config_home, config_len, true, NULL, 0, NULL, 0); + rtp_cur = add_colon_dirs(rtp_cur, config_dirs, NULL, 0, NULL, 0, true); + rtp_cur = add_dir(rtp_cur, data_home, data_len, true, "site", SITE_SIZE, + NULL, 0); + rtp_cur = add_colon_dirs(rtp_cur, data_dirs, "site", SITE_SIZE, NULL, 0, + true); + rtp_cur = add_dir(rtp_cur, vimruntime, vimruntime_len, false, NULL, 0, + NULL, 0); + rtp_cur = add_colon_dirs(rtp_cur, data_dirs, "site", SITE_SIZE, + "after", AFTER_SIZE, false); + rtp_cur = add_dir(rtp_cur, data_home, data_len, true, "site", SITE_SIZE, + "after", AFTER_SIZE); + rtp_cur = add_colon_dirs(rtp_cur, config_dirs, "after", AFTER_SIZE, NULL, 0, + false); + rtp_cur = add_dir(rtp_cur, config_home, config_len, true, + "after", AFTER_SIZE, NULL, 0); + // Strip trailing comma. + rtp_cur[-1] = NUL; + assert((size_t) (rtp_cur - rtp) == rtp_size); #undef SITE_SIZE #undef AFTER_SIZE set_string_default("runtimepath", rtp, true); @@ -443,6 +534,8 @@ static void set_runtimepath_default(void) xfree(vimruntime); } +#undef NVIM_SIZE + /* * Initialize the options, first part. * -- cgit From d1ed658c4470d1f15ce668253cd87340610c555f Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 19:37:43 +0300 Subject: option: Do not add unneeded path separator, protect against zero len --- src/nvim/option.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index a449deb014..54c934ef7c 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -346,7 +346,7 @@ static inline size_t compute_double_colon_len(const char *const val, const size_t single_suf_len) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE { - if (val == NULL) { + if (val == NULL && *val) { return 0; } size_t ret = 0; @@ -356,7 +356,8 @@ static inline size_t compute_double_colon_len(const char *const val, const char *dir; iter = vim_colon_env_iter(val, iter, &dir, &dir_len); if (dir != NULL && dir_len > 0) { - ret += ((dir_len + count_commas(dir, dir_len) + common_suf_len + 1) * 2 + ret += ((dir_len + count_commas(dir, dir_len) + common_suf_len + + !after_pathsep(dir, dir + dir_len)) * 2 + single_suf_len); } } while (iter != NULL); @@ -390,7 +391,7 @@ static inline char *add_colon_dirs(char *dest, const char *const val, const bool forward) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ARG(1) { - if (val == NULL) { + if (val == NULL && *val) { return dest; } const void *iter = NULL; @@ -401,7 +402,9 @@ static inline char *add_colon_dirs(char *dest, const char *const val, val, iter, &dir, &dir_len); if (dir != NULL && dir_len > 0) { dest = strcpy_comma_escaped(dest, dir, dir_len); - *dest++ = PATHSEP; + if (!after_pathsep(dest - 1, dest)) { + *dest++ = PATHSEP; + } memmove(dest, "nvim", NVIM_SIZE); dest += NVIM_SIZE; if (suf1 != NULL) { @@ -445,12 +448,14 @@ static inline char *add_dir(char *dest, const char *const dir, const char *const suf2, const size_t len2) FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_WARN_UNUSED_RESULT { - if (dir == NULL) { + if (dir == NULL && dir_len != 0) { return dest; } dest = strcpy_comma_escaped(dest, dir, dir_len); if (append_nvim) { - *dest++ = PATHSEP; + if (!after_pathsep(dest - 1, dest)) { + *dest++ = PATHSEP; + } memmove(dest, "nvim", NVIM_SIZE); dest += NVIM_SIZE; if (suf1 != NULL) { @@ -484,17 +489,27 @@ static void set_runtimepath_default(void) size_t vimruntime_len; if (data_home != NULL) { data_len = strlen(data_home); - rtp_size += ((data_len + count_commas(data_home, data_len) - + NVIM_SIZE + 1 + SITE_SIZE + 1 + 1) * 2 + AFTER_SIZE + 1); + if (data_len != 0) { + rtp_size += ((data_len + count_commas(data_home, data_len) + + NVIM_SIZE + 1 + SITE_SIZE + 1 + + !after_pathsep(data_home, data_home + data_len)) * 2 + + AFTER_SIZE + 1); + } } if (config_home != NULL) { config_len = strlen(config_home); - rtp_size += ((config_len + count_commas(config_home, config_len) - + NVIM_SIZE + 1 + 1) * 2 + AFTER_SIZE + 1); + if (config_len != 0) { + rtp_size += ((config_len + count_commas(config_home, config_len) + + NVIM_SIZE + 1 + + !after_pathsep(config_home, config_home + config_len)) * 2 + + AFTER_SIZE + 1); + } } if (vimruntime != NULL) { vimruntime_len = strlen(vimruntime); - rtp_size += vimruntime_len + count_commas(vimruntime, vimruntime_len) + 1; + if (vimruntime_len != 0) { + rtp_size += vimruntime_len + count_commas(vimruntime, vimruntime_len) + 1; + } } rtp_size += compute_double_colon_len(data_dirs, NVIM_SIZE + 1 + SITE_SIZE + 1, AFTER_SIZE + 1); -- cgit From 2018389871c8c973005601a63a2b6c97fa5417e2 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 19:39:04 +0300 Subject: option: Remove new trailing spaces --- src/nvim/option.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 54c934ef7c..9fa8a6539e 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -331,15 +331,15 @@ static char *strcpy_comma_escaped(char *dest, const char *src, const size_t len) /// Compute length of a colon-separated value, doubled and with some suffixes /// /// @param[in] val Colon-separated array value. -/// @param[in] common_suf_len Length of the common suffix which is appended to +/// @param[in] common_suf_len Length of the common suffix which is appended to /// each item in the array, twice. -/// @param[in] single_suf_len Length of the suffix which is appended to each +/// @param[in] single_suf_len Length of the suffix which is appended to each /// item in the array once. /// -/// @return Length of the comma-separated string array that contains each item -/// in the original array twice with suffixes with given length -/// (common_suf is present after each new item, single_suf is present -/// after half of the new items) and with commas after each item, commas +/// @return Length of the comma-separated string array that contains each item +/// in the original array twice with suffixes with given length +/// (common_suf is present after each new item, single_suf is present +/// after half of the new items) and with commas after each item, commas /// inside the values are escaped. static inline size_t compute_double_colon_len(const char *const val, const size_t common_suf_len, @@ -368,20 +368,20 @@ static inline size_t compute_double_colon_len(const char *const val, /// Add directories to a comma-separated array from a colon-separated one /// -/// Commas are escaped in process. To each item PATHSEP "nvim" is appended in +/// Commas are escaped in process. To each item PATHSEP "nvim" is appended in /// addition to suf1 and suf2. /// /// @param[in,out] dest Destination comma-separated array. /// @param[in] val Source colon-separated array. -/// @param[in] suf1 If not NULL, suffix appended to destination. Prior to it -/// directory separator is appended. Suffix must not contain +/// @param[in] suf1 If not NULL, suffix appended to destination. Prior to it +/// directory separator is appended. Suffix must not contain /// commas. /// @param[in] len1 Length of the suf1. -/// @param[in] suf2 If not NULL, another suffix appended to destination. Again -/// with directory separator behind. Suffix must not contain +/// @param[in] suf2 If not NULL, another suffix appended to destination. Again +/// with directory separator behind. Suffix must not contain /// commas. /// @param[in] len2 Length of the suf2. -/// @param[in] forward If true, iterate over val in forward direction. +/// @param[in] forward If true, iterate over val in forward direction. /// Otherwise in reverse. /// /// @return (dest + appended_characters_length) @@ -430,15 +430,15 @@ static inline char *add_colon_dirs(char *dest, const char *const val, /// @param[in,out] dest Destination comma-separated array. /// @param[in] dir Directory to append. /// @param[in] append_nvim If true, append "nvim" as the very first suffix. -/// @param[in] suf1 If not NULL, suffix appended to destination. Prior to it -/// directory separator is appended. Suffix must not contain +/// @param[in] suf1 If not NULL, suffix appended to destination. Prior to it +/// directory separator is appended. Suffix must not contain /// commas. /// @param[in] len1 Length of the suf1. -/// @param[in] suf2 If not NULL, another suffix appended to destination. Again -/// with directory separator behind. Suffix must not contain +/// @param[in] suf2 If not NULL, another suffix appended to destination. Again +/// with directory separator behind. Suffix must not contain /// commas. /// @param[in] len2 Length of the suf2. -/// @param[in] forward If true, iterate over val in forward direction. +/// @param[in] forward If true, iterate over val in forward direction. /// Otherwise in reverse. /// /// @return (dest + appended_characters_length) -- cgit From a82a059921f36fb580026ef4cc31f64c110d07b7 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 17 Oct 2015 21:46:30 +0300 Subject: option: Add `//` to the end of default `&directory` --- src/nvim/option.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 9fa8a6539e..9089947803 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -698,10 +698,11 @@ void set_init_1(void) #endif false); - set_string_default("viewdir", stdpaths_user_data_subpath("view"), true); - set_string_default("backupdir", stdpaths_user_data_subpath("backup"), true); - set_string_default("directory", stdpaths_user_data_subpath("swap"), true); - set_string_default("undodir", stdpaths_user_data_subpath("undo"), true); + set_string_default("viewdir", stdpaths_user_data_subpath("view", 0), true); + set_string_default("backupdir", stdpaths_user_data_subpath("backup", 0), + true); + set_string_default("directory", stdpaths_user_data_subpath("swap", 2), true); + set_string_default("undodir", stdpaths_user_data_subpath("undo", 0), true); // Set default for &runtimepath. All necessary expansions are performed in // this function. set_runtimepath_default(); -- cgit From 25bb9c9f7d467563f90e1c492f62132ac23ab97b Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 18 Oct 2015 00:29:54 +0300 Subject: option: Silence “may be used unitialized” errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/nvim/option.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 9089947803..ab170facf6 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -484,9 +484,9 @@ static void set_runtimepath_default(void) char *const config_dirs = stdpaths_get_xdg_var(kXDGConfigDirs); #define SITE_SIZE (sizeof("site") - 1) #define AFTER_SIZE (sizeof("after") - 1) - size_t data_len; - size_t config_len; - size_t vimruntime_len; + size_t data_len = 0; + size_t config_len = 0; + size_t vimruntime_len = 0; if (data_home != NULL) { data_len = strlen(data_home); if (data_len != 0) { -- cgit From 030c608b7dee6662a9771a3acadb069b5eaab218 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 18 Oct 2015 13:32:45 +0300 Subject: option: Use memcnt for counting commas --- src/nvim/option.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index ab170facf6..a4cfe45f10 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -301,19 +301,6 @@ static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", # include "option.c.generated.h" #endif -/// Count commas in the given string -static size_t count_commas(const char *const s, size_t len) - FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT -{ - size_t ret = 0; - for (size_t i = 0; i < len; i++) { - if (s[i] == ',') { - ret++; - } - } - return ret; -} - /// Append string with escaped commas static char *strcpy_comma_escaped(char *dest, const char *src, const size_t len) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT @@ -356,7 +343,7 @@ static inline size_t compute_double_colon_len(const char *const val, const char *dir; iter = vim_colon_env_iter(val, iter, &dir, &dir_len); if (dir != NULL && dir_len > 0) { - ret += ((dir_len + count_commas(dir, dir_len) + common_suf_len + ret += ((dir_len + memcnt(dir, ',', dir_len) + common_suf_len + !after_pathsep(dir, dir + dir_len)) * 2 + single_suf_len); } @@ -490,7 +477,7 @@ static void set_runtimepath_default(void) if (data_home != NULL) { data_len = strlen(data_home); if (data_len != 0) { - rtp_size += ((data_len + count_commas(data_home, data_len) + rtp_size += ((data_len + memcnt(data_home, ',', data_len) + NVIM_SIZE + 1 + SITE_SIZE + 1 + !after_pathsep(data_home, data_home + data_len)) * 2 + AFTER_SIZE + 1); @@ -499,7 +486,7 @@ static void set_runtimepath_default(void) if (config_home != NULL) { config_len = strlen(config_home); if (config_len != 0) { - rtp_size += ((config_len + count_commas(config_home, config_len) + rtp_size += ((config_len + memcnt(config_home, ',', config_len) + NVIM_SIZE + 1 + !after_pathsep(config_home, config_home + config_len)) * 2 + AFTER_SIZE + 1); @@ -508,7 +495,7 @@ static void set_runtimepath_default(void) if (vimruntime != NULL) { vimruntime_len = strlen(vimruntime); if (vimruntime_len != 0) { - rtp_size += vimruntime_len + count_commas(vimruntime, vimruntime_len) + 1; + rtp_size += vimruntime_len + memcnt(vimruntime, ',', vimruntime_len) + 1; } } rtp_size += compute_double_colon_len(data_dirs, NVIM_SIZE + 1 + SITE_SIZE + 1, -- cgit