Example #1
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;
                }
            }
        }
Example #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.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;
                }
            }
        }