diff --git a/CHANGELOG.org b/CHANGELOG.org index ec3be95..1a60c06 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -7,7 +7,10 @@ ** V0.2 *** Added - datamuse query routine + - implement backend enable/disable by =g:thesaurus_query#enabled_backends= + - add query system reset function =:ThesaurusQueryReset= *** Depreciated + - use of variable =g:thesaurus_query#use_local_thesaurus_source_as_primary= *** Removed - variable =g:thesaurus_query#use_alternative_backend= *** Fixed diff --git a/doc/thesaurus_query.txt b/doc/thesaurus_query.txt index ed11d09..4b7c07f 100644 --- a/doc/thesaurus_query.txt +++ b/doc/thesaurus_query.txt @@ -9,6 +9,7 @@ CONTENTS *thesaurus_query* 4. Customization ......................... |thesaurus_query-Customization| |g:thesaurus_query#display_list_all_time| |g:thesaurus_query#map_keys| + |g:thesaurus_query#enabled_backends| |g:thesaurus_query#use_local_thesaurus_source_as_primary| |g:thesaurus_query#mthesaur_file| |g:raise_backend_priority_if_synonym_found| @@ -62,6 +63,10 @@ The plugin provides three ways of using. :Thesaurus |:Thesaurus| This routine take one argument as target word and then query its synonyms. Query result would be displayed in a split buffer. +:ThesaurusQueryReset |:ThesaurusQueryReset| + Reset ThesaurusQuery, empty cached history in current session, restore + backend priority sequence to default/user defined in + |g:thesaurus_query#enabled_backends|. The functionalities are also mapped to key-bindings nnoremap cs :ThesaurusQueryReplaceCurrentWord @@ -85,7 +90,16 @@ Currently there are 5 global variables used to customize the behavior. 1: do not use default mapped keys. default=1 +*g:thesaurus_query#enabled_backends* + this variable is offered by core query handler. It's a list of + query_backends user want to enable, with the sequence of user prefered + priority. + * Please be careful not to mis-spell when setting this variable. + * if !exists("g:thesaurus_query#enabled_backends") let + default=["thesaurus_com","datamuse_com","mthesaur_txt"] + *g:thesaurus_query#use_local_thesaurus_source_as_primary* + DEPRECIATED!!! DO NOT USE THIS VARIABLE ANY MORE~ This variable is used when initiating core query handler. It determine the priority between online thesaurus backend(based on Thesaurus.com) and local thesaurus backend(based on mthesaur.txt). If diff --git a/plugin/thesaurus_query.vim b/plugin/thesaurus_query.vim index 3379e6f..a8d3b4d 100644 --- a/plugin/thesaurus_query.vim +++ b/plugin/thesaurus_query.vim @@ -53,6 +53,11 @@ tq_framework = thesaurus_query.Thesaurus_Query_Handler() endOfPython endfunction + +function! s:Thesaurus_Query_Restore_Handler() + py tq_framework.restore_thesaurus_query_handler() +endfunction + function! s:Thesaurus_Query_Lookup(word, replace) " a:word word to be looked up " a:replace flag: @@ -153,6 +158,14 @@ if !exists("g:raise_backend_priority_if_synonym_found") let g:raise_backend_priority_if_synonym_found=0 endif +" this variable is offered by core query handler. It's a list of +" query_backends user want to enable, with the sequence of user prefered +" priority. +" * Please be careful not to mis-spell when setting this variable. +if !exists("g:thesaurus_query#enabled_backends") + let g:thesaurus_query#enabled_backends=["thesaurus_com","datamuse_com","mthesaur_txt"] +endif + call g:Thesaurus_Query_Init() @@ -161,6 +174,8 @@ call g:Thesaurus_Query_Init() " -------------------------------- command! ThesaurusQueryReplaceCurrentWord :call Thesaurus_Query_Lookup(expand(''), 1) command! ThesaurusQueryLookupCurrentWord :call Thesaurus_Query_Lookup(expand(''), 0) +command! ThesaurusQueryReset :call Thesaurus_Query_Restore_Handler() + command! -nargs=1 Thesaurus :call Thesaurus_Query_Lookup(, 0) diff --git a/plugin/thesaurus_query/thesaurus_query.py b/plugin/thesaurus_query/thesaurus_query.py index 671b16e..ce9bf73 100644 --- a/plugin/thesaurus_query/thesaurus_query.py +++ b/plugin/thesaurus_query/thesaurus_query.py @@ -13,11 +13,8 @@ class Thesaurus_Query_Handler: ''' def __init__(self, cache_size_max=100): - self.word_list = {} # hold wordlist obtained in previous query - self.word_list_keys = [] # hold all keys for wordlist - # in old->new order self.wordlist_size_max = cache_size_max - self.query_source_cmd = '' + self.restore_thesaurus_query_handler() self.query_backend_define() # self.raise_backend_priority_if_synonym_found = # self.truncate_definition = -1 # number of definitions retained in output @@ -30,16 +27,14 @@ def query_backend_define(self): from mthesaur_lookup import word_query_mthesaur_lookup from thesaurus_com_lookup import word_query_handler_thesaurus_lookup as word_query_thesaurus_com_lookup import datamuse_com_lookup - self.query_backends = [] - local_as_primary = vim.eval("g:thesaurus_query#use_local_thesaurus_source_as_primary") -# use_fallback = vim.eval("g:thesaurus_query#use_alternative_backend") - self.query_backends.append(word_query_thesaurus_com_lookup()) - self.query_backends.append(datamuse_com_lookup) - if local_as_primary=="1": - self.query_backends.insert(0,word_query_mthesaur_lookup()) - else: - self.query_backends.append(word_query_mthesaur_lookup()) -# self.use_fallback=int(use_fallback) + self.query_backends = {} + # initiate all available backends and load them to self.query_backends + backend_thesaurus_com=word_query_thesaurus_com_lookup() + backend_datamuse_com=datamuse_com_lookup + backend_mthesaur_txt=word_query_mthesaur_lookup() + self.query_backends[backend_thesaurus_com.identifier] = backend_thesaurus_com + self.query_backends[backend_datamuse_com.identifier] = backend_datamuse_com + self.query_backends[backend_mthesaur_txt.identifier] = backend_mthesaur_txt def query(self, word): @@ -49,22 +44,22 @@ def query(self, word): error_encountered = 0 good_backends=[] faulty_backends=[] - for query_backend_curr in self.query_backends: # query each of the backend list till found - [state, synonym_list]=query_backend_curr.query(word) + for query_backend_curr in self.query_backend_priority: # query each of the backend list till found + [state, synonym_list]=self.query_backends[query_backend_curr].query(word) if state == -1: error_encountered = 1 - faulty_backends.append(query_backend_curr) + faulty_backends.append(self.query_backends[query_backend_curr].identifier) continue if state == 0: - good_backends.append(query_backend_curr) + good_backends.append(self.query_backends[query_backend_curr].identifier) break for faulty in faulty_backends: - self.query_backends.remove(faulty) - self.query_backends+=faulty_backends + self.query_backend_priority.remove(faulty) + self.query_backend_priority+=faulty_backends if int(vim.eval("g:raise_backend_priority_if_synonym_found")) == 1: for good in good_backends: - self.query_backends.remove(good) - self.query_backends=good_backends+self.query_backends + self.query_backend_priority.remove(good) + self.query_backend_priority=good_backends+self.query_backend_priority if error_encountered == 1: vim.command('echohl WarningMSG | echon "WARNING: " | echohl None | echon "one or more query backends report error. Please check on thesaurus source(s).\n"') if state == 0: # save to word_list buffer only when synonym is found @@ -74,6 +69,21 @@ def query(self, word): del self.word_list[self.word_list_keys.pop(0)] return synonym_list + def restore_thesaurus_query_handler(self): + self.query_backend_priority = vim.eval("g:thesaurus_query#enabled_backends") + self.word_list = {} # hold wordlist obtained in previous query + self.word_list_keys = [] # hold all keys for wordlist + # in old->new order + # depreciated variable + if vim.eval('exists("g:thesaurus_query#use_local_thesaurus_source_as_primary")'): + local_as_primary = vim.eval("g:thesaurus_query#use_local_thesaurus_source_as_primary") + else: + local_as_primary = None + if local_as_primary=="1": + self.query_backend_priority.remove("mthesaur_txt") + self.query_backend_priority.insert(0,"mthesaur_txt") + + def truncate_synonym_list(synonym_list): truncated_flag = 0 truncate_on_definition = int(vim.eval("g:thesaurus_query#truncation_on_definition_num")) # number of definitions retained in output @@ -150,7 +160,7 @@ def tq_replace_cursor_word_from_candidates(candidate_list): vim.command("echon \"In line: ... \"|echohl Keyword|echon \"{}\"|echohl None |echon \" ...\n\"".format(vim.current.line.replace('\\','\\\\').replace('"','\\"'))) vim.command("call g:TQ_echo_HL(\"None|Candidates for |WarningMSG|{}\\n|None\")".format(vim.eval("l:trimmed_word"))) - + candidate_list_printing(syno_result_IDed) if truncated_flag==0: