aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/edit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/edit.c')
-rw-r--r--src/nvim/edit.c126
1 files changed, 46 insertions, 80 deletions
diff --git a/src/nvim/edit.c b/src/nvim/edit.c
index 440787643e..1dcc86b7db 100644
--- a/src/nvim/edit.c
+++ b/src/nvim/edit.c
@@ -1727,8 +1727,6 @@ change_indent (
/* Save new line */
new_line = vim_strsave(ml_get_curline());
- if (new_line == NULL)
- return;
/* We only put back the new line up to the cursor */
new_line[curwin->w_cursor.col] = NUL;
@@ -2128,10 +2126,7 @@ ins_compl_add (
match->cp_number = -1;
if (flags & ORIGINAL_TEXT)
match->cp_number = 0;
- if ((match->cp_str = vim_strnsave(str, len)) == NULL) {
- free(match);
- return FAIL;
- }
+ match->cp_str = vim_strnsave(str, len);
match->cp_icase = icase;
/* match-fname is:
@@ -2210,18 +2205,16 @@ static void ins_compl_longest_match(compl_T *match)
if (compl_leader == NULL) {
/* First match, use it as a whole. */
compl_leader = vim_strsave(match->cp_str);
- if (compl_leader != NULL) {
- had_match = (curwin->w_cursor.col > compl_col);
- ins_compl_delete();
- ins_bytes(compl_leader + ins_compl_len());
- ins_redraw(FALSE);
+ had_match = (curwin->w_cursor.col > compl_col);
+ ins_compl_delete();
+ ins_bytes(compl_leader + ins_compl_len());
+ ins_redraw(FALSE);
- /* When the match isn't there (to avoid matching itself) remove it
- * again after redrawing. */
- if (!had_match)
- ins_compl_delete();
- compl_used_match = FALSE;
- }
+ /* When the match isn't there (to avoid matching itself) remove it
+ * again after redrawing. */
+ if (!had_match)
+ ins_compl_delete();
+ compl_used_match = FALSE;
} else {
/* Reduce the text if this match differs from compl_leader. */
p = compl_leader;
@@ -2328,9 +2321,10 @@ void set_completion(colnr_T startcol, list_T *list)
compl_length = (int)curwin->w_cursor.col - (int)startcol;
/* compl_pattern doesn't need to be set */
compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
- if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
- -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
+ if (ins_compl_add(compl_orig_text, -1, p_ic, NULL, NULL, 0,
+ ORIGINAL_TEXT, FALSE) != OK) {
return;
+ }
/* Handle like dictionary completion. */
ctrl_x_mode = CTRL_X_WHOLE_LINE;
@@ -2873,14 +2867,12 @@ static int ins_compl_bs(void)
free(compl_leader);
compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
- if (compl_leader != NULL) {
- ins_compl_new_leader();
- if (compl_shown_match != NULL)
- /* Make sure current match is not a hidden item. */
- compl_curr_match = compl_shown_match;
- return NUL;
- }
- return K_BS;
+ ins_compl_new_leader();
+ if (compl_shown_match != NULL)
+ /* Make sure current match is not a hidden item. */
+ compl_curr_match = compl_shown_match;
+
+ return NUL;
}
/*
@@ -2980,8 +2972,7 @@ static void ins_compl_addleader(int c)
free(compl_leader);
compl_leader = vim_strnsave(ml_get_curline() + compl_col,
(int)(curwin->w_cursor.col - compl_col));
- if (compl_leader != NULL)
- ins_compl_new_leader();
+ ins_compl_new_leader();
}
}
@@ -3003,15 +2994,10 @@ static void ins_compl_restart(void)
*/
static void ins_compl_set_original_text(char_u *str)
{
- char_u *p;
-
/* Replace the original text entry. */
if (compl_first_match->cp_flags & ORIGINAL_TEXT) { /* safety check */
- p = vim_strsave(str);
- if (p != NULL) {
- free(compl_first_match->cp_str);
- compl_first_match->cp_str = p;
- }
+ free(compl_first_match->cp_str);
+ compl_first_match->cp_str = vim_strsave(str);
}
}
@@ -4356,8 +4342,6 @@ static int ins_complete(int c)
) {
/* Match any word of at least two chars */
compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
- if (compl_pattern == NULL)
- return FAIL;
compl_col += curs_col;
compl_length = 0;
} else {
@@ -4432,8 +4416,6 @@ static int ins_complete(int c)
return FAIL;
} else if (ctrl_x_mode == CTRL_X_CMDLINE) {
compl_pattern = vim_strnsave(line, curs_col);
- if (compl_pattern == NULL)
- return FAIL;
set_cmd_context(&compl_xp, compl_pattern,
(int)STRLEN(compl_pattern), curs_col);
if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
@@ -4513,8 +4495,6 @@ static int ins_complete(int c)
line = ml_get(curwin->w_cursor.lnum);
compl_length = curs_col - compl_col;
compl_pattern = vim_strnsave(line + compl_col, compl_length);
- if (compl_pattern == NULL)
- return FAIL;
} else if (ctrl_x_mode == CTRL_X_SPELL) {
if (spell_bad_len > 0)
compl_col = curs_col - spell_bad_len;
@@ -4530,8 +4510,6 @@ static int ins_complete(int c)
/* Need to obtain "line" again, it may have become invalid. */
line = ml_get(curwin->w_cursor.lnum);
compl_pattern = vim_strnsave(line + compl_col, compl_length);
- if (compl_pattern == NULL)
- return FAIL;
} else {
EMSG2(_(e_intern2), "ins_complete()");
return FAIL;
@@ -4568,8 +4546,8 @@ static int ins_complete(int c)
/* Always add completion for the original text. */
free(compl_orig_text);
compl_orig_text = vim_strnsave(line + compl_col, compl_length);
- if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
- -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) {
+ if (ins_compl_add(compl_orig_text, -1, p_ic, NULL, NULL, 0,
+ ORIGINAL_TEXT, FALSE) != OK) {
free(compl_pattern);
compl_pattern = NULL;
free(compl_orig_text);
@@ -6227,11 +6205,10 @@ char_u *get_last_insert_save(void)
if (last_insert == NULL)
return NULL;
s = vim_strsave(last_insert + last_insert_skip);
- if (s != NULL) {
- len = (int)STRLEN(s);
- if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
- s[len - 1] = NUL;
- }
+ len = (int)STRLEN(s);
+ if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
+ s[len - 1] = NUL;
+
return s;
}
@@ -6265,37 +6242,33 @@ static int echeck_abbr(int c)
* For a newline, there are two NUL headed lists. One contains the characters
* that the NL replaced. The extra one stores the characters after the cursor
* that were deleted (always white space).
- *
- * Replace_offset is normally 0, in which case replace_push will add a new
- * character at the end of the stack. If replace_offset is not 0, that many
- * characters will be left on the stack above the newly inserted character.
*/
static char_u *replace_stack = NULL;
static ssize_t replace_stack_nr = 0; /* next entry in replace stack */
static ssize_t replace_stack_len = 0; /* max. number of entries */
-void
-replace_push (
- int c /* character that is replaced (NUL is none) */
-)
+/// Push character that is replaced onto the the replace stack.
+///
+/// replace_offset is normally 0, in which case replace_push will add a new
+/// character at the end of the stack. If replace_offset is not 0, that many
+/// characters will be left on the stack above the newly inserted character.
+///
+/// @param c character that is replaced (NUL is none)
+void replace_push(int c)
{
- if (replace_stack_nr < replace_offset) /* nothing to do */
+ if (replace_stack_nr < replace_offset) { // nothing to do
return;
+ }
- // TODO(philix): use xrealloc in replace_push()
if (replace_stack_len <= replace_stack_nr) {
replace_stack_len += 50;
- void *aux = xmalloc(replace_stack_len);
- if (replace_stack != NULL) {
- memmove(aux, replace_stack, replace_stack_nr);
- free(replace_stack);
- }
- replace_stack = aux;
+ replace_stack = xrealloc(replace_stack, replace_stack_len);
}
char_u *p = replace_stack + replace_stack_nr - replace_offset;
- if (replace_offset)
+ if (replace_offset) {
memmove(p + 1, p, replace_offset);
+ }
*p = (char_u)c;
++replace_stack_nr;
}
@@ -6315,16 +6288,12 @@ int replace_push_mb(char_u *p)
return l;
}
-/*
- * Pop one item from the replace stack.
- * return -1 if stack empty
- * return replaced character or NUL otherwise
- */
+/// Pop one item from the replace stack.
+///
+/// @return -1 if stack is empty, replaced character or NUL otherwise
static int replace_pop(void)
{
- if (replace_stack_nr == 0)
- return -1;
- return (int)replace_stack[--replace_stack_nr];
+ return (replace_stack_nr == 0) ? -1 : (int)replace_stack[--replace_stack_nr];
}
/*
@@ -8006,8 +7975,6 @@ static int ins_tab(void)
pos = curwin->w_cursor;
cursor = &pos;
saved_line = vim_strsave(ml_get_curline());
- if (saved_line == NULL)
- return FALSE;
ptr = saved_line + pos.col;
} else {
ptr = ml_get_cursor();
@@ -8411,7 +8378,6 @@ static colnr_T get_nolist_virtcol(void)
*/
static char_u *do_insert_char_pre(int c)
{
- char_u *res;
char_u buf[MB_MAXBYTES + 1];
/* Return quickly when there is nothing to do. */
@@ -8429,7 +8395,7 @@ static char_u *do_insert_char_pre(int c)
++textlock;
set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
- res = NULL;
+ char_u *res = NULL;
if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf)) {
/* Get the value of v:char. It may be empty or more than one
* character. Only use it when changed, otherwise continue with the