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; }
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(); } }
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(); } }
public static void CorrectWord() { ILine line = null; IEditor editor = null; 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; }