I have updated it a bit.

Instead of just defaulting the dict selection based on the locale I add an entry to config.ini (is this o.k., or is there a better way to do this) to save the last used dict.

I work with three languages myself, so when I spell check I will often want to spell check many documents in a particular language, so instead of each time having to select the dict to use it is much easier to select it whenever one uses another language.

Find new version of the plugin attached.

Werner
#   Programmer: limodou
#   E-mail:     [email protected]
#
#   Copyleft 2009 limodou
#  
#   Adapted from the original plugin and the wxSpelCheckerDialog 
#           included with Enchant v 1.5+ by Werner F. Bruhin
#
#   Distributed under the terms of the GPL (GNU Public License)
#
#   NewEdit is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   $Id$
import locale

import wx
import re
import enchant
from enchant.checker import SpellChecker
from modules import Globals
from modules import common
import modules.meide as ui

class SpellCheck(wx.Panel):
    def __init__(self, parent):
        self.parent = parent
        wx.Panel.__init__(self, parent, -1)
        
        self._numContext = 60
        
        self.ini = common.get_config_file_obj()

        self.sizer = sizer = ui.VBox(padding=0, namebinding='widget').create(self).auto_layout()
        h = sizer.add(ui.HBox)
        h.add(ui.Label(tr("Unrecognized text") + ':'))
        ts = sizer.add(ui.HBox)
        ts.add(ui.MultiTextReadOnly('', size=(500, 150)), name='error_text')
      
        bs = ts.add(ui.VBox)
        bs.add(ui.Button(tr('Start')), name='btnRun').bind('click', self.OnRun)
        bs.add(ui.Button(tr('Replace')), name='btnReplace').bind('click', self.OnReplace)
        bs.add(ui.Button(tr('Replace All')), name='btnReplaceAll').bind('click', self.OnReplaceAll)
        bs.add(ui.Button(tr('Ignore')), name='btnIgnore').bind('click', self.OnIgnore)
        bs.add(ui.Button(tr('Ignore All')), name='btnIgnoreAll').bind('click', self.OnIgnoreAll)
        bs.add(ui.Button(tr('Add')), name='btnAdd').bind('click', self.OnAdd)

        rs = sizer.add(ui.HBox)
        rs.add(ui.Label(tr("Replace with") + ':'))
        rs.add(ui.Text('', size=(500, -1)), name='replace_text')

        h = sizer.add(ui.HBox, proportion=1)
        h.add(ui.Label(tr("Suggest") + ':'))
        h.add(ui.ListBox(size=(250, -1)), name='replace_list').binds(
                (wx.EVT_LISTBOX, self._OnReplSelect),
                (wx.EVT_LISTBOX_DCLICK, self.OnReplace),
            )
        h.add(ui.Label(tr("Available Dict") + ':'))
        h.add(ui.ListBox(size=(100, -1), choices=enchant.list_languages()), name='dict_list').bind(
            wx.EVT_LISTBOX, self.OnDictSelect
            )

        sizer.auto_fit(0)

        self.init()

        self._DisableButtons()

    def init(self):
        # default to user default local
        defLoc = locale.getdefaultlocale()[0]
        # override default to config.ini setting
        if self.ini.defaultDict:
            defLoc = self.ini.defaultDict
        else:
            self.ini.defaultDict = defLoc
            self.ini.save()
            
        index = self.dict_list.FindString(defLoc)
        if index > -1:
            self.dict_list.SetSelection(index)
        else:
            defLoc = "en_US"      
        
        self.chkr = SpellChecker(defLoc)
        
        self.mainframe = Globals.mainframe
        self._buttonsEnabled = True
        self.running = False

    def OnRun(self, event):
        if self.running:
            self.running = False
            self._DisableButtons()
        else:
            self.running = True
            self.document = self.mainframe.document
            if self.document.edittype != 'edit':
                common.showerror(self, tr("This document can't be spell checked"))
                return
            self.ignore_list = []
            self.chkr.set_text(self.document.GetText())
            self._Advance()

    def _Advance(self):
        """Advance to the next error.

        This method advances the SpellChecker to the next error, if
        any.  It then displays the error and some surrounding context,
        and well as listing the suggested replacements.
        """
        # Disable interaction if no checker
        if self.chkr is None:
            self._DisableButtons()
            return False
        # Advance to next error, disable if not available
        try:
            self.chkr.next()
        except StopIteration:
            self._DisableButtons()
            self.error_text.SetValue("")
            self.replace_list.Clear()
            self.replace_text.SetValue("")
            return False
        self._EnableButtons()
        # Display error context with erroneous word in red.
        # Restoring default style was misbehaving under win32, so
        # I am forcing the rest of the text to be black.
        self.error_text.SetValue("")
        self.error_text.SetDefaultStyle(wx.TextAttr(wx.BLACK))
        lContext = self.chkr.leading_context(self._numContext)
        self.error_text.AppendText(lContext)
        self.error_text.SetDefaultStyle(wx.TextAttr(wx.RED))
        self.error_text.AppendText(self.chkr.word)
        self.error_text.SetDefaultStyle(wx.TextAttr(wx.BLACK))
        tContext = self.chkr.trailing_context(self._numContext)
        self.error_text.AppendText(tContext)
        # Display suggestions in the replacements list
        suggs = self.chkr.suggest()
        self.replace_list.Set(suggs)
        self.replace_text.SetValue(suggs and suggs[0] or '')
        return True

    def OnIgnore(self, evnt=None):
        """Callback for the "ignore" button.
        This simply advances to the next error.
        """
        self._Advance()

    def OnIgnoreAll(self, evnt=None):
        """Callback for the "ignore all" button."""
        self.chkr.ignore_always()
        self._Advance()

    def OnReplace(self, evnt=None):
        """Callback for the "replace" button."""
        repl = self._GetRepl()
        if repl:
            self.chkr.replace(repl)
            
            # Maybe this should be done at the end, i.e. use a button
            self.document.SetText(self.chkr.get_text())
        self._Advance()

    def OnReplaceAll(self, evnt=None):
        """Callback for the "replace all" button."""
        repl = self._GetRepl()

        self.chkr.replace_always(repl)
        # Maybe this should be done at the end, i.e. use a button
        self.document.SetText(self.chkr.get_text())
        self._Advance()

    def _OnReplSelect(self,evnt=None):
        """Callback when a new replacement option is selected."""
        sel = self.replace_list.GetSelection()
        if sel == -1:
            return
        opt = self.replace_list.GetString(sel)
        self.replace_text.SetValue(opt)

    def OnDictSelect(self, evnt=None):
        sel = self.dict_list.GetSelection()
        if sel == -1:
            return
        opt = self.dict_list.GetString(sel)
        self.chkr = SpellChecker(str(opt))
        
        self.ini.defaultDict = opt
        self.ini.save()

    def _GetRepl(self):
        """Get the chosen replacement string."""
        repl = self.replace_text.GetValue()
        return repl

    def _EnableButtons(self):
        """Enable the checking-related buttons"""
        if self._buttonsEnabled:
            return
        self.btnIgnore.Enable(True)
        self.btnIgnoreAll.Enable(True)
        self.btnReplace.Enable(True)
        self.btnReplaceAll.Enable(True)
        self.btnAdd.Enable(True)
        self.replace_list.Enable()
        self.dict_list.Disable()
        self.btnRun.SetLabel(tr("Stop"))
        self._buttonsEnabled = True

    def _DisableButtons(self):
        """Disable the checking-related buttons"""
        if not self._buttonsEnabled:
            return
        self.btnIgnore.Disable()
        self.btnIgnoreAll.Disable()
        self.btnReplace.Disable()
        self.btnReplaceAll.Disable()
        self.btnAdd.Disable()
        self.replace_list.Disable()
        self.dict_list.Enable()
        self.btnRun.SetLabel(tr("Start"))
        self._buttonsEnabled = False
        
    def OnAdd(self, evnt=None):
        """Callback for the "add" button."""
        self.chkr.add()
        self._Advance()

Reply via email to