Skip to content

Instantly share code, notes, and snippets.

@chrisbra
Created June 23, 2017 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbra/231eb00e48caf163fd34278b73194225 to your computer and use it in GitHub Desktop.
Save chrisbra/231eb00e48caf163fd34278b73194225 to your computer and use it in GitHub Desktop.
vim issue #1788
From 24b4a451e95c58479ff84d2e169673db963cfebf Mon Sep 17 00:00:00 2001
From: Christian Brabandt <cb@256bit.org>
Date: Fri, 23 Jun 2017 11:09:43 +0200
Subject: [PATCH] Fix issue #1788
hitting ctrl-n in insert mode when the cursor is at the first position,
the order of completion items won't be that one as expected (e.g. the
order of words should be in the order of appearance in the buffer).
Internally, Vim decrements the cursor position by 1 byte so that the
search will catch all candidates following the cursor position. However
this fails when the cursor is already at the start of the buffer,
therefore set it to the last byte in the buffer, so that search can wrap
around and catch successfully the very first word.
---
src/edit.c | 10 +++++++++-
src/testdir/test_popup.vim | 15 +++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/src/edit.c b/src/edit.c
index d8e1585ca..4a2e117f4 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -4310,7 +4310,15 @@ ins_compl_get_exp(pos_T *ini)
first_match_pos = *ini;
/* So that ^N can match word immediately after cursor */
if (ctrl_x_mode == 0)
- dec(&first_match_pos);
+ {
+ if (dec(&first_match_pos) < 0)
+ {
+ /* set to last position in buffer, so that word at
+ * start of buffer is found correctly */
+ first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
+ first_match_pos.col = (colnr_T)STRLEN(ml_get(ins_buf->b_ml.ml_line_count));
+ }
+ }
last_match_pos = first_match_pos;
type = 0;
diff --git a/src/testdir/test_popup.vim b/src/testdir/test_popup.vim
index 25d766f7b..99af66f5a 100644
--- a/src/testdir/test_popup.vim
+++ b/src/testdir/test_popup.vim
@@ -612,5 +612,20 @@ func Test_complete_func_mess()
set completefunc=
endfunc
+func Test_complete_CTRLN_startofbuffer()
+ new
+ let expected=['cupboard.organize(3, 2);',
+ \ 'bureau.prioritize(8, 7);',
+ \ 'bannister.realize(4, 4);',
+ \ 'railing.moralize(3,9);']
+ call setline(1, [ 'organize(cupboard, 3, 2);',
+ \ 'prioritize(bureau, 8, 7);',
+ \ 'realize(bannister, 4, 4);',
+ \ 'moralize(railing, 3,9);'])
+ call feedkeys("qai\<c-n>\<c-n>.\<esc>3wdW\<cr>q3@a", 'tx')
+ call assert_equal(expected, getline(1,'$'))
+ bwipe!
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
--
2.11.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment