Hello!

I would like to bring back a suggestion that was made in this list in
at least two occasions:

   Ron Yorgason, Oct 8, 2001
   http://groups.yahoo.com/group/vimdev/message/25618

   Bill McCarthy, Jun 27, 2005
   http://groups.yahoo.com/group/vimdev/message/40004

The suggestion is that the Vim thesaurus be capable of only matching
lines that have the searched pattern as its first word. I'm sending a
patch for this. To avoid incompatibility with the way people currently
use the thesaurus, I implemented a new option, 'threglookexp', which
means "thesaurus regular look up expression". It's a string with the
regular expression to be searched with the command Ctrl-X_Ctrl-T. The
default value of 'threglookexp' is "%s", what results in the current
Vim behavior. But changing the value to "^%s " Vim thesaurus behaves
as suggested.

I'm not a professional programmer, I don't know Vim source code very
well, and English isn't my first language. Thus, I will not be
surprised if my patch is in need of some fixes, including corrections
of grammar mistakes and a better name to the new option. But it's a
small patch and the patched Vim is working fine here.

I converted the OpenOffice thesaurus for Vim to test the patch:
http://br.geocities.com/alves_aq/th_ooo.tar.gz (3.1 MB)

I hope you developers will consider worthwhile the inclusion of this
new option in Vim and will be interested in testing the patch. I think
it's particularly important to be sure that the default value really
results in the same behavior of the current version of Vim and that
the compatibility with vi was treated correctly. Of course this patch
is only a suggestion and there is no problem if you prefer do not
change Vim's thesaurus behavior. 

Thanks for all Vim developers for this great text editor!

Jakson

Index: option.c
===================================================================
--- option.c    (revision 30)
+++ option.c    (working copy)
@@ -173,6 +173,7 @@
 #define PV_SW          OPT_BUF(BV_SW)
 #define PV_SWF         OPT_BUF(BV_SWF)
 #define PV_TAGS                OPT_BOTH(OPT_BUF(BV_TAGS))
+#define PV_THR         OPT_BUF(BV_THR)
 #define PV_TS          OPT_BUF(BV_TS)
 #define PV_TW          OPT_BUF(BV_TW)
 #define PV_TX          OPT_BUF(BV_TX)
@@ -353,6 +354,7 @@
 static char_u  *p_spf;
 static char_u  *p_spl;
 #endif
+static char_u  *p_thr;
 static long    p_ts;
 static long    p_tw;
 static int     p_tx;
@@ -2454,6 +2456,9 @@
                            (char_u *)NULL, PV_NONE,
 #endif
                            {(char_u *)"", (char_u *)0L}},
+    {"threglookexp", "thr", P_STRING|P_ALLOCED|P_VIM,
+                           (char_u *)&p_thr, PV_THR,
+                           {(char_u *)"%s", (char_u *)"%s"}},
     {"tildeop",            "top",  P_BOOL|P_VI_DEF|P_VIM,
                            (char_u *)&p_to, PV_NONE,
                            {(char_u *)FALSE, (char_u *)0L}},
@@ -5132,6 +5137,7 @@
     check_string_option(&buf->b_p_ep);
     check_string_option(&buf->b_p_path);
     check_string_option(&buf->b_p_tags);
+    check_string_option(&buf->b_p_thr);
 #ifdef FEAT_INS_EXPAND
     check_string_option(&buf->b_p_dict);
     check_string_option(&buf->b_p_tsr);
@@ -9092,6 +9098,7 @@
        case PV_SPL:    return (char_u *)&(curbuf->b_p_spl);
 #endif
        case PV_SW:     return (char_u *)&(curbuf->b_p_sw);
+       case PV_THR:    return (char_u *)&(curbuf->b_p_thr);
        case PV_TS:     return (char_u *)&(curbuf->b_p_ts);
        case PV_TW:     return (char_u *)&(curbuf->b_p_tw);
        case PV_TX:     return (char_u *)&(curbuf->b_p_tx);
@@ -9460,6 +9467,7 @@
            buf->b_p_dict = empty_option;
            buf->b_p_tsr = empty_option;
 #endif
+           buf->b_p_thr = vim_strsave(p_thr);
 #ifdef FEAT_TEXTOBJ
            buf->b_p_qe = vim_strsave(p_qe);
 #endif
Index: option.h
===================================================================
--- option.h    (revision 30)
+++ option.h    (working copy)
@@ -986,6 +986,7 @@
     , BV_SW
     , BV_SWF
     , BV_TAGS
+    , BV_THR
     , BV_TS
     , BV_TW
     , BV_TX
Index: structs.h
===================================================================
--- structs.h   (revision 30)
+++ structs.h   (working copy)
@@ -1445,6 +1445,7 @@
     char_u     *b_p_dict;      /* 'dictionary' local value */
     char_u     *b_p_tsr;       /* 'thesaurus' local value */
 #endif
+    char_u     *b_p_thr;       /* thesaurus reg. expr. to lookup words */
 
     /* end of buffer options */
 
Index: buffer.c
===================================================================
--- buffer.c    (revision 30)
+++ buffer.c    (working copy)
@@ -1769,6 +1769,7 @@
     clear_string_option(&buf->b_p_dict);
     clear_string_option(&buf->b_p_tsr);
 #endif
+    clear_string_option(&buf->b_p_thr);
 #ifdef FEAT_TEXTOBJ
     clear_string_option(&buf->b_p_qe);
 #endif
Index: edit.c
===================================================================
--- edit.c      (revision 30)
+++ edit.c      (working copy)
@@ -2708,7 +2708,16 @@
     }
     else
     {
-       regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
+       if(thesaurus){
+           i = (int)STRLEN(pat) + strlen((char *)curbuf->b_p_thr) + 2;
+           ptr = alloc(i);
+           if (ptr == NULL)
+               return;
+           vim_snprintf((char *)ptr, i, (char *)curbuf->b_p_thr, pat);
+           regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
+       } else{
+           regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
+       }
        if (regmatch.regprog == NULL)
            goto theend;
     }
Index: insert.txt
===================================================================
--- insert.txt  (revision 30)
+++ insert.txt  (working copy)
@@ -778,6 +778,16 @@
                        "mad" etc.
                        Other uses include translation between two languages,
                        or grouping API functions by keyword.
+                       By default, CTRL-X CTRL-T looks for the regular
+                       expression "%s", and, if the searched word is found
+                       anywhere in a line, all remaining words in this line
+                       are added to the list of suggestions. If you prefer
+                       you can change this behavior by setting 'threglookexp'
+                       to something different. If the field separator in the
+                       'thesaurus' is a empty space, setting 'threglookexp'
+                       to "^%s " will match only whole words in the beginning
+                       of an line. Please, look at 'threglookexp' for
+                       details.
 
        CTRL-T  or
        CTRL-N          Search forward for next matching keyword.  This
Index: quickref.txt
===================================================================
--- quickref.txt        (revision 30)
+++ quickref.txt        (working copy)
@@ -881,6 +881,7 @@
 'textmode'       'tx'      obsolete, use 'fileformat'
 'textwidth'      'tw'      maximum width of text that is being inserted
 'thesaurus'      'tsr'     list of thesaurus files for keyword completion
+'threglookexp'   'thr'     regular expression looked up in 'thesaurus'
 'tildeop'        'top'     tilde command "~" behaves like an operator
 'timeout'        'to'      time out on mappings and key codes
 'timeoutlen'     'tm'      time out time in milliseconds
Index: tags
===================================================================
--- tags        (revision 30)
+++ tags        (working copy)
@@ -940,6 +940,8 @@
 'tf'   options.txt     /*'tf'*
 'tgst' options.txt     /*'tgst'*
 'thesaurus'    options.txt     /*'thesaurus'*
+'thr'  options.txt     /*'thr'*
+'threglookexp' options.txt     /*'threglookexp'*
 'tildeop'      options.txt     /*'tildeop'*
 'timeout'      options.txt     /*'timeout'*
 'timeoutlen'   options.txt     /*'timeoutlen'*
Index: options.txt
===================================================================
--- options.txt (revision 30)
+++ options.txt (working copy)
@@ -6743,6 +6743,26 @@
        uses another default.
        Backticks cannot be used in this option for security reasons.
 
+                                               *'threglookexp'* *'thr'*
+'threglookexp' 'thr'   string  (default "%s")
+                       global or local to buffer |global-local|
+                       {not in Vi}
+       The thesaurus regular look up expression defines the regular
+       expression that will be used to lookup words in 'thesaurus' during the
+       completion command |i_CTRL-X_CTRL-T|.  The default regular expression
+       matches any line that has the searched word anywhere in it.  If no
+       match is found, CTRL-X CTRL-T will first complete the search pattern
+       to a word existing in the 'thesaurus' and, then, look up for
+       suggestions.  Since all words after the searched word are added to the
+       list of suggestions, in some cases this might result in a very long
+       list. Changing the value of 'threglookexp' might solve this problem.
+       For example, if the field separator in 'thesaurus' is an empty space
+       and you define 'threglookexp' to "^%s ", only the lines containing the
+       searched pattern at their beginning will be matched.  One disadvantage
+       of this regular expression is that only whole words will find a match.
+       If you want to preserve the ability to find matches for patterns that
+       represent incomplete words, use "^%s" instead.
+
                             *'tildeop'* *'top'* *'notildeop'* *'notop'*
 'tildeop' 'top'                boolean (default off)
                        global

Reply via email to