Skip to content

Commit

Permalink
add backend enable/disable & reset function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron89 committed Mar 23, 2016
1 parent f83f54d commit 51d3872
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions doc/thesaurus_query.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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 <unique> <LocalLeader>cs :ThesaurusQueryReplaceCurrentWord<CR>
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions plugin/thesaurus_query.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()


Expand All @@ -161,6 +174,8 @@ call g:Thesaurus_Query_Init()
" --------------------------------
command! ThesaurusQueryReplaceCurrentWord :call <SID>Thesaurus_Query_Lookup(expand('<cword>'), 1)
command! ThesaurusQueryLookupCurrentWord :call <SID>Thesaurus_Query_Lookup(expand('<cword>'), 0)
command! ThesaurusQueryReset :call <SID>Thesaurus_Query_Restore_Handler()

command! -nargs=1 Thesaurus :call <SID>Thesaurus_Query_Lookup(<q-args>, 0)


Expand Down
56 changes: 33 additions & 23 deletions plugin/thesaurus_query/thesaurus_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 51d3872

Please sign in to comment.