Skip to content

Instantly share code, notes, and snippets.

@Shougo
Created May 29, 2015 03:32
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 Shougo/2c48cb4d49252afc745c to your computer and use it in GitHub Desktop.
Save Shougo/2c48cb4d49252afc745c to your computer and use it in GitHub Desktop.
diff -r ca3db36a6ed8 runtime/doc/autocmd.txt
--- a/runtime/doc/autocmd.txt Thu May 14 05:56:09 2015 +0200
+++ b/runtime/doc/autocmd.txt Fri May 29 12:31:07 2015 +0900
@@ -505,6 +505,8 @@
CompleteDone After Insert mode completion is done. Either
when something was completed or abandoning
completion. |ins-completion|
+ The |v:completed_item| variable contains the
+ completed item.
*CursorHold*
CursorHold When the user doesn't press a key for the time
diff -r ca3db36a6ed8 runtime/doc/eval.txt
--- a/runtime/doc/eval.txt Thu May 14 05:56:09 2015 +0200
+++ b/runtime/doc/eval.txt Fri May 29 12:31:07 2015 +0900
@@ -1330,6 +1330,11 @@
can only be used in autocommands. For user commands |<bang>|
can be used.
+ *v:completed_item* *completed_item-variable*
+v:completed_item
+ Dictionary containing the most recent |complete-items| after
+ |CompleteDone|. Empty if the completion failed.
+
*v:count* *count-variable*
v:count The count given for the last Normal mode command. Can be used
to get the count before a mapping. Read-only. Example: >
diff -r ca3db36a6ed8 src/edit.c
--- a/src/edit.c Thu May 14 05:56:09 2015 +0200
+++ b/src/edit.c Fri May 29 12:31:07 2015 +0900
@@ -3372,6 +3372,8 @@
vim_free(compl_orig_text);
compl_orig_text = NULL;
compl_enter_selects = FALSE;
+ /* clear v:completed_item */
+ set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
}
/*
@@ -4606,17 +4608,38 @@
/* TODO: is this sufficient for redrawing? Redrawing everything causes
* flicker, thus we can't do that. */
changed_cline_bef_curs();
+ /* clear v:completed_item */
+ set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
}
/* Insert the new text being completed. */
static void
ins_compl_insert()
{
+ dict_T *dict;
+
ins_bytes(compl_shown_match->cp_str + ins_compl_len());
if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
compl_used_match = FALSE;
else
compl_used_match = TRUE;
+
+ /* Set completed item. */
+ /* { word, abbr, menu, kind, info } */
+ dict = dict_alloc();
+ if (dict != NULL) {
+ dict_add_nr_str(dict, "word", 0L,
+ EMPTY_IF_NULL(compl_shown_match->cp_str));
+ dict_add_nr_str(dict, "abbr", 0L,
+ EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
+ dict_add_nr_str(dict, "menu", 0L,
+ EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
+ dict_add_nr_str(dict, "kind", 0L,
+ EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
+ dict_add_nr_str(dict, "info", 0L,
+ EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
+ }
+ set_vim_var_dict(VV_COMPLETED_ITEM, dict);
}
/*
diff -r ca3db36a6ed8 src/eval.c
--- a/src/eval.c Thu May 14 05:56:09 2015 +0200
+++ b/src/eval.c Fri May 29 12:31:07 2015 +0900
@@ -364,6 +364,7 @@
{VV_NAME("oldfiles", VAR_LIST), 0},
{VV_NAME("windowid", VAR_NUMBER), VV_RO},
{VV_NAME("progpath", VAR_STRING), VV_RO},
+ {VV_NAME("completed_item", VAR_DICT), VV_RO},
};
/* shorthand */
@@ -372,6 +373,7 @@
#define vv_float vv_di.di_tv.vval.v_float
#define vv_str vv_di.di_tv.vval.v_string
#define vv_list vv_di.di_tv.vval.v_list
+#define vv_dict vv_di.di_tv.vval.v_dict
#define vv_tv vv_di.di_tv
static dictitem_T vimvars_var; /* variable used for v: */
@@ -20446,6 +20448,17 @@
}
/*
+ * Get Dictionary v: variable value. Caller must take care of reference count when
+ * needed.
+ */
+ dict_T *
+get_vim_var_dict(idx)
+ int idx;
+{
+ return vimvars[idx].vv_dict;
+}
+
+/*
* Set v:char to character "c".
*/
void
@@ -20519,6 +20532,35 @@
}
/*
+ * Set Dictionary v: variable to "val".
+ */
+ void
+set_vim_var_dict(idx, val)
+ int idx;
+ dict_T *val;
+{
+ int todo;
+ hashitem_T *hi;
+
+ dict_unref(vimvars[idx].vv_dict);
+
+ /* Set readonly */
+ todo = (int)val->dv_hashtab.ht_used;
+ for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi) {
+ if (HASHITEM_EMPTY(hi)) {
+ continue;
+ }
+
+ --todo;
+ HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
+ }
+
+ vimvars[idx].vv_dict = val;
+ if (val != NULL)
+ ++val->dv_refcount;
+}
+
+/*
* Set v:register if needed.
*/
void
diff -r ca3db36a6ed8 src/macros.h
--- a/src/macros.h Thu May 14 05:56:09 2015 +0200
+++ b/src/macros.h Fri May 29 12:31:07 2015 +0900
@@ -118,6 +118,9 @@
# define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || VIM_ISDIGIT(c))
#endif
+/* Returns empty string if it is NULL. */
+#define EMPTY_IF_NULL(x) ((x) ? (x) : (u_char *)"")
+
/* macro version of chartab().
* Only works with values 0-255!
* Doesn't work for UTF-8 mode with chars >= 0x80. */
diff -r ca3db36a6ed8 src/main.c
--- a/src/main.c Thu May 14 05:56:09 2015 +0200
+++ b/src/main.c Fri May 29 12:31:07 2015 +0900
@@ -1628,6 +1628,7 @@
#ifdef FEAT_EVAL
set_vim_var_string(VV_PROGNAME, initstr, -1);
set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
+ set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
#endif
if (TOLOWER_ASC(initstr[0]) == 'r')
diff -r ca3db36a6ed8 src/vim.h
--- a/src/vim.h Thu May 14 05:56:09 2015 +0200
+++ b/src/vim.h Fri May 29 12:31:07 2015 +0900
@@ -1897,7 +1897,8 @@
#define VV_OLDFILES 55
#define VV_WINDOWID 56
#define VV_PROGPATH 57
-#define VV_LEN 58 /* number of v: vars */
+#define VV_COMPLETED_ITEM 58
+#define VV_LEN 59 /* number of v: vars */
#ifdef FEAT_CLIPBOARD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment