Patch 8.2.3528

5 views
Skip to first unread message

Bram Moolenaar

unread,
Oct 17, 2021, 9:13:39 AM10/17/21
to vim...@googlegroups.com

Patch 8.2.3528
Problem: 'thesaurus' and 'thesaurusfunc' do not have the same scope.
Solution: Make 'thesaurusfunc' global-local.
Files: runtime/doc/options.txt, runtime/doc/insert.txt,
src/optiondefs.h, src/option.h, src/option.c, src/structs.h,
src/insexpand.c, src/testdir/test_edit.vim


*** ../vim-8.2.3527/runtime/doc/options.txt 2021-10-16 20:52:01.772842109 +0100
--- runtime/doc/options.txt 2021-10-17 13:52:53.965668217 +0100
***************
*** 8009,8043 ****
'thesaurus' 'tsr' string (default "")
global or local to buffer |global-local|
List of file names, separated by commas, that are used to lookup words
! for thesaurus completion commands |i_CTRL-X_CTRL-T|.

! Each line in the file should contain words with similar meaning,
! separated by non-keyword characters (white space is preferred).
! Maximum line length is 510 bytes.
!
! An English word list was added to this github issue:
! https://github.com/vim/vim/issues/629#issuecomment-443293282
! Unpack thesaurus_pkg.zip, put the thesaurus.txt file somewhere, e.g.
! ~/.vim/thesaurus/english.txt, and the 'thesaurus' option to this file
! name.

To include a comma in a file name precede it with a backslash. Spaces
after a comma are ignored, otherwise spaces are included in the file
! name. See |option-backslash| about using backslashes.
! The use of |:set+=| and |:set-=| is preferred when adding or removing
! directories from the list. This avoids problems when a future version
! uses another default.
! Backticks cannot be used in this option for security reasons.

*'thesaurusfunc'* *tsrfu'*
'thesaurusfunc' 'tsrfu' string (default: empty)
! local to buffer
{not available when compiled without the |+eval|
feature}
This option specifies a function to be used for thesaurus completion
! with CTRL-X CTRL-T. |i_CTRL-X_CTRL-T|
! See |complete-functions| for an explanation of how the function is
! invoked and what it should return.
This option cannot be set from a |modeline| or in the |sandbox|, for
security reasons.

--- 8027,8054 ----
'thesaurus' 'tsr' string (default "")
global or local to buffer |global-local|
List of file names, separated by commas, that are used to lookup words
! for thesaurus completion commands |i_CTRL-X_CTRL-T|. See
! |compl-thesaurus|.

! This option is not used if 'thesaurusfunc' is set, either for the
! buffer or globally.

To include a comma in a file name precede it with a backslash. Spaces
after a comma are ignored, otherwise spaces are included in the file
! name. See |option-backslash| about using backslashes. The use of
! |:set+=| and |:set-=| is preferred when adding or removing directories
! from the list. This avoids problems when a future version uses
! another default. Backticks cannot be used in this option for security
! reasons.

*'thesaurusfunc'* *tsrfu'*
'thesaurusfunc' 'tsrfu' string (default: empty)
! global or local to buffer |global-local|
{not available when compiled without the |+eval|
feature}
This option specifies a function to be used for thesaurus completion
! with CTRL-X CTRL-T. |i_CTRL-X_CTRL-T| See |compl-thesaurusfunc|.
!
This option cannot be set from a |modeline| or in the |sandbox|, for
security reasons.

*** ../vim-8.2.3527/runtime/doc/insert.txt 2021-10-16 15:41:25.378336694 +0100
--- runtime/doc/insert.txt 2021-10-17 13:36:08.557348706 +0100
***************
*** 824,829 ****
--- 824,832 ----
CTRL-P Search backwards for next matching keyword. This
keyword replaces the previous matching keyword.

+
+ Completing words in 'thesaurus' *compl-thesaurus*
+
*i_CTRL-X_CTRL-T*
CTRL-X CTRL-T Works as CTRL-X CTRL-K, but in a special way. It uses
the 'thesaurus' option instead of 'dictionary'. If a
***************
*** 832,853 ****
matches, even though they don't complete the word.
Thus a word can be completely replaced.

- For an example, imagine the 'thesaurus' file has a
- line like this: >
- angry furious mad enraged
- < Placing the cursor after the letters "ang" and typing
- CTRL-X CTRL-T would complete the word "angry";
- subsequent presses would change the word to "furious",
- "mad" etc.
- Other uses include translation between two languages,
- or grouping API functions by keyword.
-
- If the 'thesaurusfunc' option is set, then the user
- specified function is invoked to get the list of
- completion matches and the 'thesaurus' option is not
- used. See |complete-functions| for an explanation of
- how the function is invoked and what it should return.
-
CTRL-T or
CTRL-N Search forward for next matching keyword. This
keyword replaces the previous matching keyword.
--- 835,840 ----
***************
*** 855,860 ****
--- 842,902 ----
CTRL-P Search backwards for next matching keyword. This
keyword replaces the previous matching keyword.

+ In the file used by the 'thesaurus' option each line in the file should
+ contain words with similar meaning, separated by non-keyword characters (white
+ space is preferred). Maximum line length is 510 bytes.
+
+ For an example, imagine the 'thesaurus' file has a line like this: >
+ angry furious mad enraged
+ <Placing the cursor after the letters "ang" and typing CTRL-X CTRL-T would
+ complete the word "angry"; subsequent presses would change the word to
+ "furious", "mad" etc.
+
+ Other uses include translation between two languages, or grouping API
+ functions by keyword.
+
+ An English word list was added to this github issue:
+ https://github.com/vim/vim/issues/629#issuecomment-443293282
+ Unpack thesaurus_pkg.zip, put the thesaurus.txt file somewhere, e.g.
+ ~/.vim/thesaurus/english.txt, and the 'thesaurus' option to this file name.
+
+
+ Completing keywords with 'thesaurusfunc' *compl-thesaurusfunc*
+
+ If the 'thesaurusfunc' option is set, then the user specified function is
+ invoked to get the list of completion matches and the 'thesaurus' option is
+ not used. See |complete-functions| for an explanation of how the function is
+ invoked and what it should return.
+
+ Here is an example that uses the "aiksaurus" command (provided by Magnus
+ Groß): >
+
+ func Thesaur(findstart, base)
+ if a:findstart
+ let line = getline('.')
+ let start = col('.') - 1
+ while start > 0 && line[start - 1] =~ '\a'
+ let start -= 1
+ endwhile
+ return start
+ else
+ let res = []
+ let h = ''
+ for l in split(system('aiksaurus '.shellescape(a:base)), '\n')
+ if l[:3] == '=== '
+ let h = substitute(l[4:], ' =*$', '', '')
+ elseif l[0] =~ '\a'
+ call extend(res, map(split(l, ', '), {_, val -> {'word': val, 'menu': '('.h.')'}}))
+ endif
+ endfor
+ return res
+ endif
+ endfunc
+
+ if has('patch-8.2.3520')
+ set thesaurusfunc=Thesaur
+ endif
+

Completing keywords in the current and included files *compl-keyword*

*** ../vim-8.2.3527/src/optiondefs.h 2021-10-16 20:52:01.772842109 +0100
--- src/optiondefs.h 2021-10-17 13:48:40.099037628 +0100
***************
*** 141,147 ****
# define PV_TFU OPT_BUF(BV_TFU)
#endif
#ifdef FEAT_COMPL_FUNC
! # define PV_THSFU OPT_BUF(BV_THSFU)
#endif
#define PV_TAGS OPT_BOTH(OPT_BUF(BV_TAGS))
#define PV_TC OPT_BOTH(OPT_BUF(BV_TC))
--- 141,147 ----
# define PV_TFU OPT_BUF(BV_TFU)
#endif
#ifdef FEAT_COMPL_FUNC
! # define PV_TSRFU OPT_BOTH(OPT_BUF(BV_TSRFU))
#endif
#define PV_TAGS OPT_BOTH(OPT_BUF(BV_TAGS))
#define PV_TC OPT_BOTH(OPT_BUF(BV_TC))
***************
*** 2634,2640 ****
{(char_u *)"", (char_u *)0L} SCTX_INIT},
{"thesaurusfunc", "tsrfu", P_STRING|P_ALLOCED|P_VI_DEF|P_SECURE,
#ifdef FEAT_COMPL_FUNC
! (char_u *)&p_thsfu, PV_THSFU,
{(char_u *)"", (char_u *)0L}
#else
(char_u *)NULL, PV_NONE,
--- 2634,2640 ----
{(char_u *)"", (char_u *)0L} SCTX_INIT},
{"thesaurusfunc", "tsrfu", P_STRING|P_ALLOCED|P_VI_DEF|P_SECURE,
#ifdef FEAT_COMPL_FUNC
! (char_u *)&p_tsrfu, PV_TSRFU,
{(char_u *)"", (char_u *)0L}
#else
(char_u *)NULL, PV_NONE,
*** ../vim-8.2.3527/src/option.h 2021-10-16 20:52:01.772842109 +0100
--- src/option.h 2021-10-17 13:46:50.594033385 +0100
***************
*** 404,410 ****
#ifdef FEAT_COMPL_FUNC
EXTERN char_u *p_cfu; // 'completefunc'
EXTERN char_u *p_ofu; // 'omnifunc'
! EXTERN char_u *p_thsfu; // 'thesaurusfunc'
#endif
EXTERN int p_ci; // 'copyindent'
#if defined(FEAT_GUI) && defined(MACOS_X)
--- 404,410 ----
#ifdef FEAT_COMPL_FUNC
EXTERN char_u *p_cfu; // 'completefunc'
EXTERN char_u *p_ofu; // 'omnifunc'
! EXTERN char_u *p_tsrfu; // 'thesaurusfunc'
#endif
EXTERN int p_ci; // 'copyindent'
#if defined(FEAT_GUI) && defined(MACOS_X)
***************
*** 1222,1228 ****
, BV_TAGS
, BV_TC
#ifdef FEAT_COMPL_FUNC
! , BV_THSFU
#endif
, BV_TS
, BV_TW
--- 1222,1228 ----
, BV_TAGS
, BV_TC
#ifdef FEAT_COMPL_FUNC
! , BV_TSRFU
#endif
, BV_TS
, BV_TW
*** ../vim-8.2.3527/src/option.c 2021-10-16 21:14:07.495196461 +0100
--- src/option.c 2021-10-17 13:56:13.239939449 +0100
***************
*** 5131,5136 ****
--- 5131,5141 ----
case PV_TSR:
clear_string_option(&buf->b_p_tsr);
break;
+ #ifdef FEAT_COMPL_FUNC
+ case PV_TSRFU:
+ clear_string_option(&buf->b_p_tsrfu);
+ break;
+ #endif
case PV_FP:
clear_string_option(&buf->b_p_fp);
break;
***************
*** 5225,5230 ****
--- 5230,5238 ----
#endif
case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
+ #ifdef FEAT_COMPL_FUNC
+ case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
+ #endif
#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
#endif
***************
*** 5305,5310 ****
--- 5313,5322 ----
? (char_u *)&(curbuf->b_p_dict) : p->var;
case PV_TSR: return *curbuf->b_p_tsr != NUL
? (char_u *)&(curbuf->b_p_tsr) : p->var;
+ #ifdef FEAT_COMPL_FUNC
+ case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
+ ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
+ #endif
case PV_FP: return *curbuf->b_p_fp != NUL
? (char_u *)&(curbuf->b_p_fp) : p->var;
#ifdef FEAT_QUICKFIX
***************
*** 5433,5439 ****
#ifdef FEAT_COMPL_FUNC
case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
- case PV_THSFU: return (char_u *)&(curbuf->b_p_tsrfu);
#endif
#ifdef FEAT_EVAL
case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
--- 5445,5450 ----
***************
*** 5936,5943 ****
COPY_OPT_SCTX(buf, BV_CFU);
buf->b_p_ofu = vim_strsave(p_ofu);
COPY_OPT_SCTX(buf, BV_OFU);
- buf->b_p_tsrfu = vim_strsave(p_thsfu);
- COPY_OPT_SCTX(buf, BV_THSFU);
#endif
#ifdef FEAT_EVAL
buf->b_p_tfu = vim_strsave(p_tfu);
--- 5947,5952 ----
***************
*** 6080,6085 ****
--- 6089,6097 ----
#endif
buf->b_p_dict = empty_option;
buf->b_p_tsr = empty_option;
+ #ifdef FEAT_COMPL_FUNC
+ buf->b_p_tsrfu = empty_option;
+ #endif
#ifdef FEAT_TEXTOBJ
buf->b_p_qe = vim_strsave(p_qe);
COPY_OPT_SCTX(buf, BV_QE);
*** ../vim-8.2.3527/src/structs.h 2021-10-16 21:14:07.495196461 +0100
--- src/structs.h 2021-10-17 13:50:42.900265008 +0100
***************
*** 2864,2870 ****
#ifdef FEAT_COMPL_FUNC
char_u *b_p_cfu; // 'completefunc'
char_u *b_p_ofu; // 'omnifunc'
- char_u *b_p_tsrfu; // 'thesaurusfunc'
#endif
#ifdef FEAT_EVAL
char_u *b_p_tfu; // 'tagfunc'
--- 2864,2869 ----
***************
*** 2967,2972 ****
--- 2966,2974 ----
unsigned b_tc_flags; // flags for 'tagcase'
char_u *b_p_dict; // 'dictionary' local value
char_u *b_p_tsr; // 'thesaurus' local value
+ #ifdef FEAT_COMPL_FUNC
+ char_u *b_p_tsrfu; // 'thesaurusfunc' local value
+ #endif
long b_p_ul; // 'undolevels' local value
#ifdef FEAT_PERSISTENT_UNDO
int b_p_udf; // 'undofile'
*** ../vim-8.2.3527/src/insexpand.c 2021-10-16 21:14:07.495196461 +0100
--- src/insexpand.c 2021-10-17 14:07:15.696149040 +0100
***************
*** 301,307 ****
)
: (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
#ifdef FEAT_COMPL_FUNC
! && *curbuf->b_p_tsrfu == NUL
#endif
))
{
--- 301,307 ----
)
: (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
#ifdef FEAT_COMPL_FUNC
! && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
#endif
))
{
***************
*** 2246,2252 ****
case CTRL_X_OMNI:
return curbuf->b_p_ofu;
case CTRL_X_THESAURUS:
! return curbuf->b_p_tsrfu;
default:
return (char_u *)"";
}
--- 2246,2252 ----
case CTRL_X_OMNI:
return curbuf->b_p_ofu;
case CTRL_X_THESAURUS:
! return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu;
default:
return (char_u *)"";
}
***************
*** 2750,2758 ****
thesaurus_func_complete(int type UNUSED)
{
#ifdef FEAT_COMPL_FUNC
! return (type == CTRL_X_THESAURUS
! && curbuf->b_p_tsrfu != NULL
! && *curbuf->b_p_tsrfu != NUL);
#else
return FALSE;
#endif
--- 2750,2757 ----
thesaurus_func_complete(int type UNUSED)
{
#ifdef FEAT_COMPL_FUNC
! return type == CTRL_X_THESAURUS
! && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL);
#else
return FALSE;
#endif
*** ../vim-8.2.3527/src/testdir/test_edit.vim 2021-10-16 15:41:25.382336753 +0100
--- src/testdir/test_edit.vim 2021-10-17 14:10:49.306904380 +0100
***************
*** 920,935 ****

func Test_thesaurus_func()
new
! set thesaurus=
! set thesaurusfunc=MyThesaurus
call setline(1, "an ki")
call cursor(1, 1)
call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
call assert_equal(['an amiable', ''], getline(1, '$'))
set thesaurusfunc=NonExistingFunc
call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
- set thesaurusfunc&
%bw!
endfunc

func Test_edit_CTRL_U()
--- 920,943 ----

func Test_thesaurus_func()
new
! set thesaurus=notused
! set thesaurusfunc=NotUsed
! setlocal thesaurusfunc=MyThesaurus
call setline(1, "an ki")
call cursor(1, 1)
call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
call assert_equal(['an amiable', ''], getline(1, '$'))
+
+ setlocal thesaurusfunc=NonExistingFunc
+ call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
+
+ setlocal thesaurusfunc=
set thesaurusfunc=NonExistingFunc
call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
%bw!
+
+ set thesaurusfunc=
+ set thesaurus=
endfunc

func Test_edit_CTRL_U()
*** ../vim-8.2.3527/src/version.c 2021-10-17 11:33:44.510907324 +0100
--- src/version.c 2021-10-17 14:11:16.271254149 +0100
***************
*** 759,760 ****
--- 759,762 ----
{ /* Add new patch number below this line */
+ /**/
+ 3528,
/**/

--
If Microsoft would build a car...
... The airbag system would ask "are you SURE?" before deploying.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

James McCoy

unread,
Oct 17, 2021, 9:10:33 PM10/17/21
to vim...@googlegroups.com
On Sun, Oct 17, 2021 at 02:13:32PM +0100, Bram Moolenaar wrote:
> *** ../vim-8.2.3527/runtime/doc/insert.txt 2021-10-16 15:41:25.378336694 +0100
> --- runtime/doc/insert.txt 2021-10-17 13:36:08.557348706 +0100
> ***************
> *** 855,860 ****
> --- 842,902 ----
> [‥]
> +
> + func Thesaur(findstart, base)
> + if a:findstart
> + let line = getline('.')
> + let start = col('.') - 1
> + while start > 0 && line[start - 1] =~ '\a'
> + let start -= 1
> + endwhile
> + return start
> + else
> + let res = []
> + let h = ''
> + for l in split(system('aiksaurus '.shellescape(a:base)), '\n')
> + if l[:3] == '=== '
> + let h = substitute(l[4:], ' =*$', '', '')
> + elseif l[0] =~ '\a'
> + call extend(res, map(split(l, ', '), {_, val -> {'word': val, 'menu': '('.h.')'}}))
> + endif
> + endfor
> + return res
> + endif
> + endfunc
> +
> + if has('patch-8.2.3520')

Why not use "exists('+thesaurusfunc')" here? IMHO, patch number checks
should be reserved for things that don't have more explicit checks.

> + set thesaurusfunc=Thesaur
> + endif
> +

Cheers,
--
James
GPG Key: 4096R/91BF BF4D 6956 BD5D F7B7 2D23 DFE6 91AE 331B A3DB

Bram Moolenaar

unread,
Oct 18, 2021, 5:05:10 AM10/18/21
to vim...@googlegroups.com, James McCoy

James McCoy wrote:

> > + if has('patch-8.2.3520')
>
> Why not use "exists('+thesaurusfunc')" here? IMHO, patch number checks
> should be reserved for things that don't have more explicit checks.

Yes, that is better.

--
hundred-and-one symptoms of being an internet addict:
182. You may not know what is happening in the world, but you know
every bit of net-gossip there is.
Reply all
Reply to author
Forward
0 new messages