def replace_word(self, word, idx, repword): bfr = self.current_buffer() itr = bfr.get_iter_at_offset(idx - len(word)) bfr.delete(itr, bfr.get_iter_at_offset(idx)) bfr.insert(itr, repword) def spellcheck(*a): ''' on windows, try to COM script Word. on posix, try to pass the file to aspell. ''' if flauxtext.os.name != 'nt': d = self.mk_dialog("spellcheck", (flauxtext.gtk.STOCK_OK, 1, flauxtext.gtk.STOCK_CANCEL, 0), False) d.vbox.pack_start(flauxtext.gtk.Label("use what command to check this document?")) e = flauxtext.gtk.Entry() d.vbox.pack_start(e) e.connect('activate', lambda x=None,y=None:d.response(1)) e.set_text(flauxtext.termcmd + 'aspell -c ' + self.current_filename()) if d.run(): cmd = e.get_text() d.destroy() self.save() flauxtext.os.system(e.get_text()) self.callback_reload() else: d.destroy() else: try: flauxtext.imp0rt('win32com.client') except: self.flash_info("sorry, but i can't spellcheck this document because you don't have the win32all package.",4000) return try: # to find a dict in a dict in config.flauxtext to tell me how to handle certain kinds of words--see the huge for loop after this except stmt userrules = c.get('nt', 'spellcheck') fn = self.current_filename() userrule = None for k, v in userrules.items(): if fnmatch.fnmatch(fn, k): userrule = v break assert userrule != None except: userrule = {} winword = flauxtext.win32com.client.Dispatch("Word.Application") winword.Documents.Add() # .Range().Text = self.current_text() ignoreall = [] replaceall = {} tv = self.current_textview() bfr = tv.get_buffer() d = flauxtext.gtk.Dialog("spellcheck", self.w, flauxtext.gtk.DIALOG_NO_SEPARATOR, ('_Replace', 0, 'Replace _All', 1, '_Ignore', 2, 'I_gnore All', 3, '_Done', 4)) self.response_on_esc(d, 4) d.connect('delete-event', lambda d_:d.destroy()) d.connect('destroy-event', lambda d_:d.destroy()) e = flauxtext.gtk.Entry() e.connect('activate', lambda x=None,y=None:d.response(0)) d.vbox.pack_start(e, False, False) d.vbox.set_property('homogeneous', False) el = flauxtext.gtk.ListStore(str) ss = flauxtext.gtk.TreeView(el) d.vbox.pack_start(ss) sscol = flauxtext.gtk.TreeViewColumn('Suggestions') ss.append_column(sscol) sscr = flauxtext.gtk.CellRendererText() sscol.pack_start(sscr) sscol.add_attribute(sscr, 'text', 0) ss.connect('row-activated', lambda a=None, b=None, c=None: d.response(1)) suggestions = [] ss.connect('cursor-changed', lambda ss_=ss: e.set_text(suggestions[ss.get_cursor()[0][0]])) idx_offset = 0 # the generator below works off an aging string, so idx might be off if you replace for word, idx in self.text_words_indices(): idx += idx_offset if not word in ignoreall and not winword.CheckSpelling(word): if word in replaceall.keys(): replace_word(self, word, idx, replaceall[word]) idx_offset += len(replaceall[word]) - len(word) else: rs = [v for k,v in userrule.items() if flauxtext.re.match(k, word)] # check to see if user defined a rule for dealing with this kind of word if len(rs): r = rs[0] else: # show user the context itr = bfr.get_iter_at_offset(idx - len(word)) bfr.select_range(itr, bfr.get_iter_at_offset(idx)) tv.scroll_to_iter(itr, 0.499) # update the dialog e.set_text(word) # if word offers any suggestions, this will be replaced with the first suggestion at ss.grab_focus() el.clear() suggestions = [] for i in winword.GetSpellingSuggestions(word): el.append([i.Name]) suggestions.append(i.Name) ss.grab_focus() d.show_all() r = d.run() print r if r < 2: rep = e.get_text() replace_word(self, word, idx, rep) idx_offset += len(rep) - len(word) if r: replaceall[word] = rep elif r == 3: ignoreall.append(word) elif r in [4, flauxtext.gtk.RESPONSE_DELETE_EVENT]: break winword.Quit(False) # close without saving d.destroy() self.flash_info('spell check done.', 5000)