aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2014-10-26 14:55:22 +0300
committerZyX <kp-pav@yandex.ru>2016-04-18 03:37:23 +0300
commitfdb68e35e4c729c7ed097d8ade1da29e5b3f4b31 (patch)
treef68d5079f4dcaa936da97ce40127393827252177
parent5674546580643fab33f0d10babacbc8e7792e287 (diff)
downloadrneovim-fdb68e35e4c729c7ed097d8ade1da29e5b3f4b31.tar.gz
rneovim-fdb68e35e4c729c7ed097d8ade1da29e5b3f4b31.tar.bz2
rneovim-fdb68e35e4c729c7ed097d8ade1da29e5b3f4b31.zip
ex_getln: Make get_histtype return HIST_DEFAULT if needed
-rw-r--r--src/nvim/eval.c32
-rw-r--r--src/nvim/ex_getln.c79
-rw-r--r--src/nvim/ex_getln.h12
3 files changed, 75 insertions, 48 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index a193c82f9d..e6d2f69281 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -11144,7 +11144,7 @@ static void f_hasmapto(typval_T *argvars, typval_T *rettv)
*/
static void f_histadd(typval_T *argvars, typval_T *rettv)
{
- int histype;
+ HistoryType histype;
char_u *str;
char_u buf[NUMBUFLEN];
@@ -11152,8 +11152,8 @@ static void f_histadd(typval_T *argvars, typval_T *rettv)
if (check_restricted() || check_secure())
return;
str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
- histype = str != NULL ? get_histtype(str, STRLEN(str)) : -1;
- if (histype >= 0) {
+ histype = str != NULL ? get_histtype(str, STRLEN(str), false) : HIST_INVALID;
+ if (histype != HIST_INVALID) {
str = get_tv_string_buf(&argvars[1], buf);
if (*str != NUL) {
init_history();
@@ -11178,14 +11178,14 @@ static void f_histdel(typval_T *argvars, typval_T *rettv)
n = 0;
else if (argvars[1].v_type == VAR_UNKNOWN)
/* only one argument: clear entire history */
- n = clr_history(get_histtype(str, STRLEN(str)));
+ n = clr_history(get_histtype(str, STRLEN(str), false));
else if (argvars[1].v_type == VAR_NUMBER)
/* index given: remove that entry */
- n = del_history_idx(get_histtype(str, STRLEN(str)),
+ n = del_history_idx(get_histtype(str, STRLEN(str), false),
(int)get_tv_number(&argvars[1]));
else
/* string given: remove all matching entries */
- n = del_history_entry(get_histtype(str, STRLEN(str)),
+ n = del_history_entry(get_histtype(str, STRLEN(str), false),
get_tv_string_buf(&argvars[1], buf));
rettv->vval.v_number = n;
}
@@ -11195,20 +11195,21 @@ static void f_histdel(typval_T *argvars, typval_T *rettv)
*/
static void f_histget(typval_T *argvars, typval_T *rettv)
{
- int type;
+ HistoryType type;
int idx;
char_u *str;
str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
- if (str == NULL)
+ if (str == NULL) {
rettv->vval.v_string = NULL;
- else {
- type = get_histtype(str, STRLEN(str));
- if (argvars[1].v_type == VAR_UNKNOWN)
+ } else {
+ type = get_histtype(str, STRLEN(str), false);
+ if (argvars[1].v_type == VAR_UNKNOWN) {
idx = get_history_idx(type);
- else
+ } else {
idx = (int)get_tv_number_chk(&argvars[1], NULL);
- /* -1 on type error */
+ }
+ // -1 on type error
rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
}
rettv->v_type = VAR_STRING;
@@ -11223,8 +11224,9 @@ static void f_histnr(typval_T *argvars, typval_T *rettv)
char_u *history = get_tv_string_chk(&argvars[0]);
- i = history == NULL ? HIST_CMD - 1 : get_histtype(history, STRLEN(history));
- if (i >= HIST_CMD && i < HIST_COUNT)
+ i = history == NULL ? HIST_CMD - 1 : get_histtype(history, STRLEN(history),
+ false);
+ if (i != HIST_INVALID)
i = get_history_idx(i);
else
i = -1;
diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c
index 0c2848da98..a9e9ee76d5 100644
--- a/src/nvim/ex_getln.c
+++ b/src/nvim/ex_getln.c
@@ -4272,20 +4272,33 @@ void globpath(char_u *path, char_u *file, garray_T *ga, int expand_options)
* Command line history stuff *
*********************************/
-/*
- * Translate a history character to the associated type number.
- */
-static int hist_char2type(int c)
+/// Translate a history character to the associated type number
+static HistoryType hist_char2type(const int c)
+ FUNC_ATTR_CONST FUNC_ATTR_WARN_UNUSED_RESULT
{
- if (c == ':')
- return HIST_CMD;
- if (c == '=')
- return HIST_EXPR;
- if (c == '@')
- return HIST_INPUT;
- if (c == '>')
- return HIST_DEBUG;
- return HIST_SEARCH; /* must be '?' or '/' */
+ switch (c) {
+ case ':': {
+ return HIST_CMD;
+ }
+ case '=': {
+ return HIST_EXPR;
+ }
+ case '@': {
+ return HIST_INPUT;
+ }
+ case '>': {
+ return HIST_DEBUG;
+ }
+ case '/':
+ case '?': {
+ return HIST_SEARCH;
+ }
+ default: {
+ assert(false);
+ }
+ }
+ // Silence -Wreturn-type
+ return 0;
}
/*
@@ -4454,28 +4467,38 @@ in_history (
return false;
}
-/*
- * Convert history name (from table above) to its HIST_ equivalent.
- * When "name" is empty, return "cmd" history.
- * Returns -1 for unknown history name.
- */
-int get_histtype(char_u *name, size_t len)
+/// Convert history name to its HIST_ equivalent
+///
+/// Names are taken from the table above. When `name` is empty returns currently
+/// active history or HIST_DEFAULT, depending on `return_default` argument.
+///
+/// @param[in] name Converted name.
+/// @param[in] len Name length.
+/// @param[in] return_default Determines whether HIST_DEFAULT should be
+/// returned or value based on `ccline.cmdfirstc`.
+///
+/// @return Any value from HistoryType enum, including HIST_INVALID. May not
+/// return HIST_DEFAULT unless return_default is true.
+HistoryType get_histtype(const char_u *const name, const size_t len,
+ const bool return_default)
+ FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
{
- int i;
-
- /* No argument: use current history. */
- if (len == 0)
- return hist_char2type(ccline.cmdfirstc);
+ // No argument: use current history.
+ if (len == 0) {
+ return return_default ? HIST_DEFAULT :hist_char2type(ccline.cmdfirstc);
+ }
- for (i = 0; history_names[i] != NULL; ++i)
- if (STRNICMP(name, history_names[i], len) == 0)
+ for (HistoryType i = 0; history_names[i] != NULL; i++) {
+ if (STRNICMP(name, history_names[i], len) == 0) {
return i;
+ }
+ }
if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && len == 1) {
return hist_char2type(name[0]);
}
- return -1;
+ return HIST_INVALID;
}
static int last_maptick = -1; /* last seen maptick */
@@ -4847,7 +4870,7 @@ void ex_history(exarg_T *eap)
while (ASCII_ISALPHA(*end)
|| vim_strchr((char_u *)":=@>/?", *end) != NULL)
end++;
- histype1 = get_histtype(arg, end - arg);
+ histype1 = get_histtype(arg, end - arg, false);
if (histype1 == HIST_INVALID) {
if (STRNICMP(arg, "all", end - arg) == 0) {
histype1 = 0;
diff --git a/src/nvim/ex_getln.h b/src/nvim/ex_getln.h
index 21da8b9d42..24eebdc303 100644
--- a/src/nvim/ex_getln.h
+++ b/src/nvim/ex_getln.h
@@ -27,11 +27,13 @@
/// Present history tables
typedef enum {
- HIST_CMD, ///< Colon commands.
- HIST_SEARCH, ///< Search commands.
- HIST_EXPR, ///< Expressions (e.g. from entering = register).
- HIST_INPUT, ///< input() lines.
- HIST_DEBUG, ///< Debug commands.
+ HIST_DEFAULT = -2, ///< Default (current) history.
+ HIST_INVALID = -1, ///< Unknown history.
+ HIST_CMD = 0, ///< Colon commands.
+ HIST_SEARCH, ///< Search commands.
+ HIST_EXPR, ///< Expressions (e.g. from entering = register).
+ HIST_INPUT, ///< input() lines.
+ HIST_DEBUG, ///< Debug commands.
} HistoryType;
/// Number of history tables