dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

homophones.py (4533B)


      1 from talon import Context, Module, app, clip, cron, imgui, actions, ui, fs
      2 import os
      3 
      4 ########################################################################
      5 # global settings
      6 ########################################################################
      7 
      8 # a list of homophones where each line is a comma separated list
      9 # e.g. where,wear,ware
     10 # a suitable one can be found here:
     11 # https://github.com/pimentel/homophones
     12 cwd = os.path.dirname(os.path.realpath(__file__))
     13 homophones_file = os.path.join(cwd, "homophones.csv")
     14 # if quick_replace, then when a word is selected and only one homophone exists,
     15 # replace it without bringing up the options
     16 quick_replace = True
     17 show_help = False
     18 ########################################################################
     19 
     20 ctx = Context()
     21 mod = Module()
     22 mod.mode("homophones")
     23 mod.list("homophones_canonicals", desc="list of words ")
     24 
     25 
     26 main_screen = ui.main_screen()
     27 
     28 
     29 def update_homophones(name, flags):
     30     if name != homophones_file:
     31         return
     32 
     33     phones = {}
     34     canonical_list = []
     35     with open(homophones_file, "r") as f:
     36         for line in f:
     37             words = line.rstrip().split(",")
     38             canonical_list.append(words[0])
     39             for word in words:
     40                 word = word.lower()
     41                 old_words = phones.get(word, [])
     42                 phones[word] = sorted(set(old_words + words))
     43 
     44     global all_homophones
     45     all_homophones = phones
     46     ctx.lists["self.homophones_canonicals"] = canonical_list
     47 
     48 
     49 update_homophones(homophones_file, None)
     50 fs.watch(cwd, update_homophones)
     51 active_word_list = None
     52 is_selection = False
     53 
     54 
     55 def close_homophones():
     56     gui.hide()
     57     actions.mode.disable("user.homophones")
     58 
     59 
     60 def raise_homophones(word, forced=False, selection=False):
     61     global quick_replace
     62     global active_word_list
     63     global show_help
     64     global force_raise
     65     global is_selection
     66 
     67     force_raise = forced
     68     is_selection = selection
     69 
     70     if is_selection:
     71         word = word.strip()
     72 
     73     is_capitalized = word == word.capitalize()
     74     is_upper = word.isupper()
     75 
     76     word = word.lower()
     77 
     78     if word not in all_homophones:
     79         app.notify("homophones.py", '"%s" not in homophones list' % word)
     80         return
     81 
     82     active_word_list = all_homophones[word]
     83     if (
     84         is_selection
     85         and len(active_word_list) == 2
     86         and quick_replace
     87         and not force_raise
     88     ):
     89         if word == active_word_list[0].lower():
     90             new = active_word_list[1]
     91         else:
     92             new = active_word_list[0]
     93 
     94         if is_capitalized:
     95             new = new.capitalize()
     96         elif is_upper:
     97             new = new.upper()
     98 
     99         clip.set(new)
    100         actions.edit.paste()
    101 
    102         return
    103 
    104     actions.mode.enable("user.homophones")
    105     show_help = False
    106     gui.show()
    107 
    108 
    109 @imgui.open(x=main_screen.x + main_screen.width / 2.6, y=main_screen.y)
    110 def gui(gui: imgui.GUI):
    111     global active_word_list
    112     if show_help:
    113         gui.text("Homephone help - todo")
    114     else:
    115         gui.text("Select a homophone")
    116         gui.line()
    117         index = 1
    118         for word in active_word_list:
    119             gui.text("Choose {}: {} ".format(index, word))
    120             index = index + 1
    121 
    122 
    123 def show_help_gui():
    124     global show_help
    125     show_help = True
    126     gui.show()
    127 
    128 
    129 @mod.capture(rule="{self.homophones_canonicals}")
    130 def homophones_canonical(m) -> str:
    131     "Returns a single string"
    132     return m.homophones_canonicals
    133 
    134 
    135 @mod.action_class
    136 class Actions:
    137     def homophones_hide():
    138         """Hides the homophones display"""
    139         close_homophones()
    140 
    141     def homophones_show(m: str):
    142         """Show the homophones display"""
    143         print(m)
    144         raise_homophones(m, False, False)
    145 
    146     def homophones_show_selection():
    147         """Show the homophones display for the selected text"""
    148         raise_homophones(actions.edit.selected_text(), False, True)
    149 
    150     def homophones_force_show(m: str):
    151         """Show the homophones display forcibly"""
    152         raise_homophones(m, True, False)
    153 
    154     def homophones_force_show_selection():
    155         """Show the homophones display for the selected text forcibly"""
    156         raise_homophones(actions.edit.selected_text(), True, True)
    157 
    158     def homophones_select(number: int) -> str:
    159         """selects the homophone by number"""
    160         if number <= len(active_word_list) and number > 0:
    161             return active_word_list[number - 1]
    162 
    163         error = "homophones.py index {} is out of range (1-{})".format(
    164             number, len(active_word_list)
    165         )
    166         app.notify(error)
    167         raise error