예제 #1
0
        public static MultiSpell Get()
        {
            var spell = (MultiSpell)_instance.Target;

            if (spell == null)
            {
                spell            = new MultiSpell(Actor.Dictionaries);
                _instance.Target = spell;
            }
            return(spell);
        }
예제 #2
0
        static void AddRightWord(HashSet <string> words, string word)
        {
            // language names, unique, sorted
            var languages = new List <string>();

            foreach (var dic in Dictionaries)
            {
                if (!languages.Contains(dic.Language))
                {
                    languages.Add(dic.Language);
                }
            }
            languages.Sort();

            // dictionary menu
            var menu = Far.Api.CreateMenu();

            menu.Title             = My.AddToDictionary;
            menu.HelpTopic         = My.AddToDictionaryHelp;
            menu.AutoAssignHotkeys = true;
            menu.Add(My.Common);
            foreach (string name in languages)
            {
                menu.Add(name);
            }

            // repeat the menu
            MultiSpell multiSpell = null;

            while (menu.Show())
            {
                // common:
                if (menu.Selected == 0)
                {
                    string[] newWords = ShowMenuAddWord(word);
                    if (newWords == null)
                    {
                        continue;
                    }

                    if (words == null)
                    {
                        words = GetCommonWords();
                    }

                    // write/add
                    var path = Path.Combine(GetUserDictionaryDirectory(true), Settings.UserFile);
                    using (var writer = File.AppendText(path))
                    {
                        foreach (var newWord in newWords)
                        {
                            if (words.Contains(newWord))
                            {
                                continue;
                            }

                            writer.WriteLine(newWord);
                            words.Add(newWord);
                        }
                    }

                    return;
                }

                // language:
                var language = menu.Items[menu.Selected].Text;
                var spell    = (multiSpell ?? (multiSpell = MultiSpell.Get())).GetSpell(language);

                // dialog
                var dialog = new UIWordDialog(word, string.Empty);
                while (dialog.Show())
                {
                    var stem1 = dialog.Stem1.Trim();
                    if (stem1.Length == 0)
                    {
                        continue;
                    }

                    var stem2 = dialog.Stem2.Trim();

                    bool ok = (stem2.Length == 0) ? spell.Add(stem1) : spell.AddWithAffix(stem1, stem2);
                    if (!ok)
                    {
                        var stems = spell.Stem(stem2);
                        if (stems.Count == 0 || stems.Count == 1 && stems[0] == stem2)
                        {
                            continue;
                        }

                        var menu2 = Far.Api.CreateMenu();
                        menu2.Title = My.ExampleStem;
                        foreach (var it in stems)
                        {
                            menu2.Add(it);
                        }

                        if (menu2.Show())
                        {
                            dialog.Stem2 = menu2.Items[menu2.Selected].Text;
                        }

                        continue;
                    }

                    var path = Actor.GetUserDictionaryPath(language, true);
                    using (var writer = File.AppendText(path))
                    {
                        if (stem2.Length == 0)
                        {
                            writer.WriteLine(stem1);
                        }
                        else
                        {
                            writer.WriteLine(stem1 + " " + stem2);
                        }
                    }

                    return;
                }
            }
        }
예제 #3
0
        public static void CorrectWord()
        {
            ILine   line;
            IEditor editor;

            var kind = Far.Api.Window.Kind;

            if (kind == WindowKind.Editor)
            {
                editor = Far.Api.Editor;
                line   = editor[-1];
            }
            else
            {
                line = Far.Api.Line;
                if (line == null)
                {
                    return;
                }
            }

            // search for the current word
            var match = MatchCaret(new Regex(Settings.Default.WordPattern, RegexOptions.IgnorePatternWhitespace), line.Text, line.Caret, false);

            if (match == null)
            {
                return;
            }

            // the current word
            var word = MatchToWord(match);

            // get suggestions with check
            List <string> words = null;
            var           spell = MultiSpell.Get();

            if (!spell.Spell(word))
            {
                words = spell.Suggest(word);
            }

            // it is correct or nothing is suggested
            if (words == null || words.Count == 0)
            {
                // move caret to the end of word
                line.Caret = match.Index + match.Length;
                return;
            }

            // show suggestions
            var cursor = Far.Api.UI.WindowCursor;
            var menu   = new UIWordMenu(words, word, cursor.X, cursor.Y + 1);

            // cancel or ignore:
            if (!menu.Show() || menu.IsIgnore)
            {
                return;
            }

            // ignore all:
            if (menu.IsIgnoreAll)
            {
                IgnoreWords.Add(word);
                return;
            }

            // add to dictionary:
            if (menu.IsAddToDictionary)
            {
                AddRightWord(null, word);
                return;
            }

            // replace the word with the suggestion
            word = menu.Word;
            line.SelectText(match.Index, match.Index + match.Length);
            line.SelectedText = word;
            line.UnselectText();
            line.Caret = match.Index + word.Length;
        }
예제 #4
0
        public static void CorrectText()
        {
            // regular expressions
            var   regexWord = new Regex(Settings.Default.WordPattern, RegexOptions.IgnorePatternWhitespace);
            Regex regexSkip = GetRegexSkip();

            // right words
            var rightWords = GetCommonWords();

            // initial editor data
            var editor = Far.Api.Editor;
            var caret0 = editor.Caret;
            int iLine1, iLine2;

            if (editor.SelectionExists)
            {
                var rect = editor.SelectionPlace;
                iLine1 = rect.First.Y;
                iLine2 = rect.Last.Y;
                if (rect.Last.X < 0)
                {
                    --iLine2;
                }
            }
            else
            {
                iLine1 = caret0.Y;
                iLine2 = editor.Count - 1;
            }

            // use the spell checker
            var spell = MultiSpell.Get();

            try
            {
                // loop through words and lines
                int iLine = iLine1;
                for (; ;)
                {
NextWord:

                    // the line and its text now
                    var line = editor[iLine];
                    var text = line.Text;

                    // the first word
                    Match match = MatchCaret(regexWord, text, line.Caret, true);
                    if (match == null)
                    {
                        goto NextLine;
                    }

                    // loop through line words (matches) with no changes
                    MatchCollection skip = null;
                    for (; match.Success; match = match.NextMatch())
                    {
                        // the target word
                        var word = MatchToWord(match);

                        // check cheap skip lists
                        if (rightWords.Contains(word) || IgnoreWords.Contains(word))
                        {
                            continue;
                        }

                        // check spelling, expensive but better before the skip pattern
                        if (spell.Spell(word))
                        {
                            continue;
                        }

                        // expensive skip pattern
                        if (Actor.HasMatch(skip ?? (skip = Actor.GetMatches(regexSkip, text)), match))
                        {
                            continue;
                        }

                        // check spelling and get suggestions
                        List <string> words = spell.Suggest(word);

                        // next match on success or no suggestions
                        if (words.Count == 0)
                        {
                            continue;
                        }

                        // new caret and selection is at the word end
                        int column = match.Index + match.Length;

                        // 1) select the word, !! set the caret now
                        line.SelectText(match.Index, column);
                        line.Caret = column;

                        // 2) reframe vertically (!! horisontal is sloppy), !! keep the caret
                        var frame = editor.Frame;
                        frame.VisibleLine = frame.CaretLine - Far.Api.UI.WindowSize.Y / 3;
                        editor.Frame      = frame;

                        // commit
                        editor.Redraw();

                        // menu
                        var point = editor.ConvertPointEditorToScreen(new Point(column, iLine));
                        var menu  = new UIWordMenu(words, word, point.X, point.Y + 1);

                        // cancel:
                        if (!menu.Show())
                        {
                            return;
                        }

                        // ignore:
                        if (menu.IsIgnore)
                        {
                            continue;
                        }

                        // ignore all:
                        if (menu.IsIgnoreAll)
                        {
                            IgnoreWords.Add(word);
                            continue;
                        }

                        // add to dictionary:
                        if (menu.IsAddToDictionary)
                        {
                            AddRightWord(rightWords, word);
                            continue;
                        }

                        // replace editor selection with correction
                        var correction = menu.Word;
                        line.SelectedText = correction;
                        line.UnselectText();

                        // advance in the same line
                        int caret = match.Index + correction.Length + 1;
                        if (caret < line.Text.Length)
                        {
                            line.Caret = caret;
                            goto NextWord;
                        }

                        // next line
                        break;
                    }

NextLine:

                    ++iLine;
                    if (iLine > iLine2)
                    {
                        break;
                    }

                    editor.GoTo(0, iLine);
                }
            }
            finally
            {
                editor.UnselectText();
            }
        }