comparison src/insexpand.c @ 25974:416237f1de22 v8.2.3520

patch 8.2.3520: cannot define a function for thesaurus completion Commit: https://github.com/vim/vim/commit/160e994d768d03a3c826b58115cde94df8fce607 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sat Oct 16 15:41:29 2021 +0100 patch 8.2.3520: cannot define a function for thesaurus completion Problem: Cannot define a function for thesaurus completion. Solution: Add 'thesaurusfunc'. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/8987, closes 8950)
author Bram Moolenaar <Bram@vim.org>
date Sat, 16 Oct 2021 16:45:04 +0200
parents 6776d3fbf13b
children c8fcea636252
comparison
equal deleted inserted replaced
25973:3b34837f4538 25974:416237f1de22
297 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL 297 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
298 #ifdef FEAT_SPELL 298 #ifdef FEAT_SPELL
299 && !curwin->w_p_spell 299 && !curwin->w_p_spell
300 #endif 300 #endif
301 ) 301 )
302 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) 302 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
303 #ifdef FEAT_COMPL_FUNC
304 && *curbuf->b_p_thsfu == NUL
305 #endif
306 ))
303 { 307 {
304 ctrl_x_mode = CTRL_X_NORMAL; 308 ctrl_x_mode = CTRL_X_NORMAL;
305 edit_submode = NULL; 309 edit_submode = NULL;
306 msg_attr(dict_opt ? _("'dictionary' option is empty") 310 msg_attr(dict_opt ? _("'dictionary' option is empty")
307 : _("'thesaurus' option is empty"), 311 : _("'thesaurus' option is empty"),
2228 return buf; 2232 return buf;
2229 } 2233 }
2230 2234
2231 #ifdef FEAT_COMPL_FUNC 2235 #ifdef FEAT_COMPL_FUNC
2232 /* 2236 /*
2237 * Get the user-defined completion function name for completion 'type'
2238 */
2239 static char_u *
2240 get_complete_funcname(int type)
2241 {
2242 switch (type)
2243 {
2244 case CTRL_X_FUNCTION:
2245 return curbuf->b_p_cfu;
2246 case CTRL_X_OMNI:
2247 return curbuf->b_p_ofu;
2248 case CTRL_X_THESAURUS:
2249 return curbuf->b_p_thsfu;
2250 default:
2251 return (char_u *)"";
2252 }
2253 }
2254
2255 /*
2233 * Execute user defined complete function 'completefunc' or 'omnifunc', and 2256 * Execute user defined complete function 'completefunc' or 'omnifunc', and
2234 * get matches in "matches". 2257 * get matches in "matches".
2235 */ 2258 */
2236 static void 2259 static void
2237 expand_by_function( 2260 expand_by_function(
2244 char_u *funcname; 2267 char_u *funcname;
2245 pos_T pos; 2268 pos_T pos;
2246 typval_T rettv; 2269 typval_T rettv;
2247 int save_State = State; 2270 int save_State = State;
2248 2271
2249 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; 2272 funcname = get_complete_funcname(type);
2250 if (*funcname == NUL) 2273 if (*funcname == NUL)
2251 return; 2274 return;
2252 2275
2253 // Call 'completefunc' to obtain the list of matches. 2276 // Call 'completefunc' to obtain the list of matches.
2254 args[0].v_type = VAR_NUMBER; 2277 args[0].v_type = VAR_NUMBER;
2717 what_list = argvars[0].vval.v_list; 2740 what_list = argvars[0].vval.v_list;
2718 } 2741 }
2719 get_complete_info(what_list, rettv->vval.v_dict); 2742 get_complete_info(what_list, rettv->vval.v_dict);
2720 } 2743 }
2721 #endif 2744 #endif
2745
2746 /*
2747 * Returns TRUE when using a user-defined function for thesaurus completion.
2748 */
2749 static int
2750 thesaurus_func_complete(int type UNUSED)
2751 {
2752 #ifdef FEAT_COMPL_FUNC
2753 return (type == CTRL_X_THESAURUS
2754 && curbuf->b_p_thsfu != NULL
2755 && *curbuf->b_p_thsfu != NUL);
2756 #else
2757 return FALSE;
2758 #endif
2759 }
2722 2760
2723 /* 2761 /*
2724 * Get the next expansion(s), using "compl_pattern". 2762 * Get the next expansion(s), using "compl_pattern".
2725 * The search starts at position "ini" in curbuf and in the direction 2763 * The search starts at position "ini" in curbuf and in the direction
2726 * compl_direction. 2764 * compl_direction.
2904 break; 2942 break;
2905 #endif 2943 #endif
2906 2944
2907 case CTRL_X_DICTIONARY: 2945 case CTRL_X_DICTIONARY:
2908 case CTRL_X_THESAURUS: 2946 case CTRL_X_THESAURUS:
2909 ins_compl_dictionaries( 2947 #ifdef FEAT_COMPL_FUNC
2948 if (thesaurus_func_complete(type))
2949 expand_by_function(type, compl_pattern);
2950 else
2951 #endif
2952 ins_compl_dictionaries(
2910 dict != NULL ? dict 2953 dict != NULL ? dict
2911 : (type == CTRL_X_THESAURUS 2954 : (type == CTRL_X_THESAURUS
2912 ? (*curbuf->b_p_tsr == NUL 2955 ? (*curbuf->b_p_tsr == NUL
2913 ? p_tsr 2956 ? p_tsr
2914 : curbuf->b_p_tsr) 2957 : curbuf->b_p_tsr)
3758 startcol = (int)curs_col; 3801 startcol = (int)curs_col;
3759 compl_col = 0; 3802 compl_col = 0;
3760 } 3803 }
3761 3804
3762 // Work out completion pattern and original text -- webb 3805 // Work out completion pattern and original text -- webb
3763 if (ctrl_x_mode == CTRL_X_NORMAL || (ctrl_x_mode & CTRL_X_WANT_IDENT)) 3806 if (ctrl_x_mode == CTRL_X_NORMAL
3807 || (ctrl_x_mode & CTRL_X_WANT_IDENT
3808 && !thesaurus_func_complete(ctrl_x_mode)))
3764 { 3809 {
3765 if ((compl_cont_status & CONT_SOL) 3810 if ((compl_cont_status & CONT_SOL)
3766 || ctrl_x_mode == CTRL_X_PATH_DEFINES) 3811 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3767 { 3812 {
3768 if (!(compl_cont_status & CONT_ADDING)) 3813 if (!(compl_cont_status & CONT_ADDING))
3908 compl_col = curs_col; 3953 compl_col = curs_col;
3909 else 3954 else
3910 compl_col = (int)(compl_xp.xp_pattern - compl_pattern); 3955 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
3911 compl_length = curs_col - compl_col; 3956 compl_length = curs_col - compl_col;
3912 } 3957 }
3913 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) 3958 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI
3959 || thesaurus_func_complete(ctrl_x_mode))
3914 { 3960 {
3915 #ifdef FEAT_COMPL_FUNC 3961 #ifdef FEAT_COMPL_FUNC
3916 // Call user defined function 'completefunc' with "a:findstart" 3962 // Call user defined function 'completefunc' with "a:findstart"
3917 // set to 1 to obtain the length of text to use for completion. 3963 // set to 1 to obtain the length of text to use for completion.
3918 typval_T args[3]; 3964 typval_T args[3];
3921 pos_T pos; 3967 pos_T pos;
3922 int save_State = State; 3968 int save_State = State;
3923 3969
3924 // Call 'completefunc' or 'omnifunc' and get pattern length as a 3970 // Call 'completefunc' or 'omnifunc' and get pattern length as a
3925 // string 3971 // string
3926 funcname = ctrl_x_mode == CTRL_X_FUNCTION 3972 funcname = get_complete_funcname(ctrl_x_mode);
3927 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3928 if (*funcname == NUL) 3973 if (*funcname == NUL)
3929 { 3974 {
3930 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION 3975 semsg(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3931 ? "completefunc" : "omnifunc"); 3976 ? "completefunc" : "omnifunc");
3932 // restore did_ai, so that adding comment leader works 3977 // restore did_ai, so that adding comment leader works