Commit d743b1fd authored by twanvl's avatar twanvl

apply spelling corrections from the context menu.

parent dfa3debf
...@@ -60,6 +60,8 @@ menu: ...@@ -60,6 +60,8 @@ menu:
symbols: &Symbols Ctrl+M symbols: &Symbols Ctrl+M
reminder text: &Reminder Text Ctrl+R reminder text: &Reminder Text Ctrl+R
insert symbol: I&nsert Symbol insert symbol: I&nsert Symbol
# spelling
no spelling suggestions: (no suggestions)
graph: &Graph graph: &Graph
pie: &Pie 1 pie: &Pie 1
...@@ -168,6 +170,8 @@ help: ...@@ -168,6 +170,8 @@ help:
italic: Makes the selected text italic italic: Makes the selected text italic
symbols: Draws the selected text with symbols symbols: Draws the selected text with symbols
reminder text: Show reminder text for the selected keyword reminder text: Show reminder text for the selected keyword
# spelling
no spelling suggestions: There are no suggestions for correcting this error
graph: graph:
pie: A pie graph, the size of the slice indicates the number of cards pie: A pie graph, the size of the slice indicates the number of cards
...@@ -669,6 +673,7 @@ action: ...@@ -669,6 +673,7 @@ action:
cut: Cut cut: Cut
paste: Paste paste: Paste
auto replace: Auto Replace auto replace: Auto Replace
correct: Spelling Correction
# Choice/color editors # Choice/color editors
change: Change %s change: Change %s
...@@ -704,17 +709,6 @@ action: ...@@ -704,17 +709,6 @@ action:
############################################################## Error messages ############################################################## Error messages
error: error:
# General
internal error:
An internal error occured:
%s
Please save your work (use 'save as' to so you don't overwrite things)
and restart Magic Set Editor.
You should leave a bug report on http://magicseteditor.sourceforge.net/
# File related # File related
file not found: File not found: '%s' in package '%s' file not found: File not found: '%s' in package '%s'
file not found package like: file not found package like:
......
...@@ -91,6 +91,14 @@ void IconMenu::Append(wxMenuItem* item) { ...@@ -91,6 +91,14 @@ void IconMenu::Append(wxMenuItem* item) {
wxMenu::Append(item); wxMenu::Append(item);
} }
void IconMenu::Insert(size_t pos, int id, const String& resource, const String& text, const String& help, int style, wxMenu* submenu) {
// create menu, load icon
wxMenuItem* item = new wxMenuItem(this, id, text, help, style, submenu);
set_menu_item_image(item, resource);
// add to menu
wxMenu::Insert(pos,item);
}
void IconMenu::Insert(size_t pos, int id, const String& text, const String& help) { void IconMenu::Insert(size_t pos, int id, const String& text, const String& help) {
wxMenuItem* item = new wxMenuItem (this, id, text, help); wxMenuItem* item = new wxMenuItem (this, id, text, help);
item->SetBitmap(wxNullBitmap); item->SetBitmap(wxNullBitmap);
......
...@@ -29,6 +29,8 @@ class IconMenu : public wxMenu { ...@@ -29,6 +29,8 @@ class IconMenu : public wxMenu {
void Append(int id, const String& text, const String& help, wxMenu* submenu); void Append(int id, const String& text, const String& help, wxMenu* submenu);
/// Append a menu item, without an image /// Append a menu item, without an image
void Append(wxMenuItem* item); void Append(wxMenuItem* item);
/// Insert a menu item, with an image (loaded from a resource)
void Insert(size_t pos, int id, const String& resource, const String& text, const String& help, int style = wxITEM_NORMAL, wxMenu* submenu = nullptr);
/// Insert a menu item, without an image /// Insert a menu item, without an image
void Insert(size_t pos, int id, const String& text, const String& help); void Insert(size_t pos, int id, const String& text, const String& help);
}; };
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <data/action/value.hpp> #include <data/action/value.hpp>
#include <util/tagged_string.hpp> #include <util/tagged_string.hpp>
#include <util/find_replace.hpp> #include <util/find_replace.hpp>
#include <util/spell_checker.hpp>
#include <util/window_id.hpp> #include <util/window_id.hpp>
#include <wx/clipbrd.h> #include <wx/clipbrd.h>
#include <wx/caret.h> #include <wx/caret.h>
...@@ -508,6 +509,43 @@ bool TextValueEditor::onChar(wxKeyEvent& ev) { ...@@ -508,6 +509,43 @@ bool TextValueEditor::onChar(wxKeyEvent& ev) {
return true; return true;
} }
// ----------------------------------------------------------------------------- : Spellchecking
String spellcheck_word_at(const String& str, size_t start, String& before, String& after) {
size_t end = min(match_close_tag(str,start), str.size());
String word = untag(str.substr(start,end-start));
size_t start_u = 0, end_u = String::npos;
trim_punctuation(word, start_u, end_u);
before = word.substr(0,start_u);
after = word.substr(end_u,String::npos);
word.erase(end_u,String::npos);
word.erase(0,start_u);
return word;
}
void spellcheck_language_at(const String& str, size_t error_pos, SpellChecker** out) {
String tag = tag_at(str,error_pos);
size_t pos = min(tag.find_first_of(_(':')), tag.size()-1);
size_t pos2 = min(tag.find_first_of(_(':'),pos+1), tag.size()-1);
String language = tag.substr(pos+1,pos2-pos-1);
if (language.empty()) return;
out[0] = &SpellChecker::get(language);
String extra = tag.substr(pos2+1);
if (extra.empty()) return;
out[1] = &SpellChecker::get(extra,language);
}
void get_spelling_suggestions(const String& str, size_t error_pos, vector<String>& suggestions_out, String& before, String& after) {
String word = spellcheck_word_at(str, error_pos, before, after);
// find dictionaries
SpellChecker* checkers[3] = {nullptr};
spellcheck_language_at(str, error_pos, checkers);
// suggestions
for (size_t i = 0 ; checkers[i] ; ++i) {
checkers[i]->suggest(word, suggestions_out);
}
}
// ----------------------------------------------------------------------------- : Other events // ----------------------------------------------------------------------------- : Other events
void TextValueEditor::onFocus() { void TextValueEditor::onFocus() {
...@@ -527,12 +565,35 @@ bool TextValueEditor::onContextMenu(IconMenu& m, wxContextMenuEvent& ev) { ...@@ -527,12 +565,35 @@ bool TextValueEditor::onContextMenu(IconMenu& m, wxContextMenuEvent& ev) {
// in a keword? => "reminder text" option // in a keword? => "reminder text" option
size_t kwpos = in_tag(value().value(), _("<kw-"), selection_start_i, selection_start_i); size_t kwpos = in_tag(value().value(), _("<kw-"), selection_start_i, selection_start_i);
if (kwpos != String::npos) { if (kwpos != String::npos) {
m.AppendSeparator(); m.InsertSeparator(0);
m.Append(ID_FORMAT_REMINDER, _("reminder"), _MENU_("reminder text"), _HELP_("reminder text"), wxITEM_CHECK); m.Insert(0,ID_FORMAT_REMINDER, _("reminder"), _MENU_("reminder text"), _HELP_("reminder text"), wxITEM_CHECK);
}
// in a spelling error? => show suggestions and "add to dictionary"
size_t error_pos = in_tag(value().value(), _("<error-spelling"), selection_start_i, selection_start_i);
if (error_pos != String::npos) {
// TODO: "add to dictionary"
//%m.InsertSeparator(0);
//%m.Insert(0,ID_SPELLING_ADD_TO_DICT, _MENU_("add to dictionary"), _HELP_("add to dictionary"));
// suggestions
String before,after;
vector<String> suggestions;
get_spelling_suggestions(value().value(), error_pos, suggestions,before,after);
// add suggestions to menu
m.InsertSeparator(0);
if (suggestions.empty()) {
m.Insert(0,ID_SPELLING_NO_SUGGEST, _MENU_("no spelling suggestions"), _HELP_("no spelling suggestions"));
} else {
int i = 0;
FOR_EACH(s,suggestions) {
m.Insert(i, ID_SPELLING_SUGGEST + i, before+s+after, wxEmptyString);
i++;
}
}
} }
// always show the menu // always show the menu
return true; return true;
} }
bool TextValueEditor::onCommand(int id) { bool TextValueEditor::onCommand(int id) {
if (id >= ID_INSERT_SYMBOL_MENU_MIN && id <= ID_INSERT_SYMBOL_MENU_MAX) { if (id >= ID_INSERT_SYMBOL_MENU_MIN && id <= ID_INSERT_SYMBOL_MENU_MAX) {
// Insert a symbol // Insert a symbol
...@@ -544,9 +605,23 @@ bool TextValueEditor::onCommand(int id) { ...@@ -544,9 +605,23 @@ bool TextValueEditor::onCommand(int id) {
replaceSelection(code, _ACTION_("insert symbol")); replaceSelection(code, _ACTION_("insert symbol"));
return true; return true;
} }
} else if (id == ID_SPELLING_ADD_TO_DICT) {
// TODO
} else if (id >= ID_SPELLING_SUGGEST && id <= ID_SPELLING_SUGGEST_MAX) {
size_t error_pos = in_tag(value().value(), _("<error-spelling"), selection_start_i, selection_start_i);
if (error_pos == String::npos) throw InternalError(_("Unexpected spelling suggestion")); // wrong
String before,after;
vector<String> suggestions;
get_spelling_suggestions(value().value(), error_pos, suggestions,before,after);
selection_start_i = error_pos;
selection_end_i = match_close_tag(value().value(), error_pos);
fixSelection(TYPE_INDEX);
replaceSelection(before + suggestions.at(id - ID_SPELLING_SUGGEST) + after, _ACTION_("correct"));
return true;
} }
return false; return false;
} }
wxMenu* TextValueEditor::getMenu(int type) const { wxMenu* TextValueEditor::getMenu(int type) const {
if (type == ID_INSERT_SYMBOL && (style().always_symbol || style().allow_formating) if (type == ID_INSERT_SYMBOL && (style().always_symbol || style().allow_formating)
&& style().symbol_font.valid()) { && style().symbol_font.valid()) {
......
This diff is collapsed.
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
// ----------------------------------------------------------------------------- : Functions // ----------------------------------------------------------------------------- : Functions
inline bool spelled_correctly(const String& input, size_t start, size_t end, SpellChecker** checkers, const ScriptValueP& extra_test, Context& ctx) { inline size_t spelled_correctly(const String& input, size_t start, size_t end, SpellChecker** checkers, const ScriptValueP& extra_test, Context& ctx) {
// untag // untag
String word = untag(input.substr(start,end-start)); String word = untag(input.substr(start,end-start));
if (word.empty()) return true; if (word.empty()) return true;
...@@ -36,8 +36,8 @@ inline bool spelled_correctly(const String& input, size_t start, size_t end, Spe ...@@ -36,8 +36,8 @@ inline bool spelled_correctly(const String& input, size_t start, size_t end, Spe
// run through spellchecker(s) // run through spellchecker(s)
word.erase(end_u,String::npos); word.erase(end_u,String::npos);
word.erase(0,start_u); word.erase(0,start_u);
for (SpellChecker** c = checkers ; *c ; ++c) { for (size_t i = 0 ; checkers[i] ; ++i) {
if ((*c)->spell(word)) { if (checkers[i]->spell(word)) {
return true; return true;
} }
} }
...@@ -51,12 +51,12 @@ inline bool spelled_correctly(const String& input, size_t start, size_t end, Spe ...@@ -51,12 +51,12 @@ inline bool spelled_correctly(const String& input, size_t start, size_t end, Spe
return false; return false;
} }
void check_word(const String& input, String& out, size_t start, size_t end, SpellChecker** checkers, const ScriptValueP& extra_test, Context& ctx) { void check_word(const String& tag, const String& input, String& out, size_t start, size_t end, SpellChecker** checkers, const ScriptValueP& extra_test, Context& ctx) {
if (start >= end) return; if (start >= end) return;
bool good = spelled_correctly(input, start, end, checkers, extra_test, ctx); bool good = spelled_correctly(input, start, end, checkers, extra_test, ctx);
if (!good) out += _("<error-spelling>"); if (!good) out += _("<") + tag;
out.append(input, start, end-start); out.append(input, start, end-start);
if (!good) out += _("</error-spelling>"); if (!good) out += _("</") + tag;
} }
SCRIPT_FUNCTION(check_spelling) { SCRIPT_FUNCTION(check_spelling) {
...@@ -75,6 +75,13 @@ SCRIPT_FUNCTION(check_spelling) { ...@@ -75,6 +75,13 @@ SCRIPT_FUNCTION(check_spelling) {
if (!extra_dictionary.empty()) { if (!extra_dictionary.empty()) {
checkers[1] = &SpellChecker::get(extra_dictionary,language); checkers[1] = &SpellChecker::get(extra_dictionary,language);
} }
// what will the missspelling tag be?
String tag = _("error-spelling:");
tag += language;
if (!extra_dictionary.empty()) {
tag += _(":") + extra_dictionary;
}
tag += _(">");
// now walk over the words in the input, and mark misspellings // now walk over the words in the input, and mark misspellings
String result; String result;
size_t word_start = 0, word_end = 0, pos = 0; size_t word_start = 0, word_end = 0, pos = 0;
...@@ -91,7 +98,7 @@ SCRIPT_FUNCTION(check_spelling) { ...@@ -91,7 +98,7 @@ SCRIPT_FUNCTION(check_spelling) {
} }
} else if (isSpace(c) || c == EM_DASH || c == EN_DASH) { } else if (isSpace(c) || c == EM_DASH || c == EN_DASH) {
// word boundary -> check word // word boundary -> check word
check_word(input, result, word_start, word_end, checkers, extra_match, ctx); check_word(tag, input, result, word_start, word_end, checkers, extra_match, ctx);
// non-word characters // non-word characters
result.append(input, word_end, pos - word_end + 1); result.append(input, word_end, pos - word_end + 1);
// next // next
...@@ -101,7 +108,7 @@ SCRIPT_FUNCTION(check_spelling) { ...@@ -101,7 +108,7 @@ SCRIPT_FUNCTION(check_spelling) {
} }
} }
// last word // last word
check_word(input, result, word_start, word_end, checkers, extra_match, ctx); check_word(tag, input, result, word_start, word_end, checkers, extra_match, ctx);
result.append(input, word_end, String::npos); result.append(input, word_end, String::npos);
// done // done
SCRIPT_RETURN(result); SCRIPT_RETURN(result);
......
...@@ -71,8 +71,7 @@ void SpellChecker::destroyAll() { ...@@ -71,8 +71,7 @@ void SpellChecker::destroyAll() {
// ----------------------------------------------------------------------------- : Spell checker : use // ----------------------------------------------------------------------------- : Spell checker : use
bool SpellChecker::spell(const String& word) { bool SpellChecker::convert_encoding(const String& word, CharBuffer& out) {
if (word.empty()) return true; // empty word is okay
// fix curly quotes, especially apstrophes // fix curly quotes, especially apstrophes
String fixed; String fixed;
FOR_EACH_CONST(c,word) { FOR_EACH_CONST(c,word) {
...@@ -96,16 +95,20 @@ bool SpellChecker::spell(const String& word) { ...@@ -96,16 +95,20 @@ bool SpellChecker::spell(const String& word) {
} }
} }
// convert encoding // convert encoding
#ifdef UNICODE out = fixed.mb_str(encoding);
wxCharBuffer str = fixed.mb_str(encoding); if (*out == '\0') {
#else
wxCharBuffer str = fixed.mb_str(encoding);
#endif
if (*str == '\0') {
// If encoding fails we get an empty string, since the word was not empty this can never happen // If encoding fails we get an empty string, since the word was not empty this can never happen
// words that can't be encoded are not in the dictionary, so they are wrong. // words that can't be encoded are not in the dictionary, so they are wrong.
return false; return false;
} else {
return true;
} }
}
bool SpellChecker::spell(const String& word) {
if (word.empty()) return true; // empty word is okay
CharBuffer str;
if (!convert_encoding(word,str)) return false;
return Hunspell::spell(str); return Hunspell::spell(str);
} }
...@@ -115,3 +118,16 @@ bool SpellChecker::spell_with_punctuation(const String& word) { ...@@ -115,3 +118,16 @@ bool SpellChecker::spell_with_punctuation(const String& word) {
if (start >= end) return true; // just punctuation is wrong if (start >= end) return true; // just punctuation is wrong
return spell(word.substr(start,end-start)); return spell(word.substr(start,end-start));
} }
void SpellChecker::suggest(const String& word, vector<String>& suggestions_out) {
CharBuffer str;
if (!convert_encoding(word,str)) return;
// call Hunspell
char** suggestions;
int num_suggestions = Hunspell::suggest(&suggestions, str);
// copy sugestions
for (int i = 0 ; i < num_suggestions ; ++i) {
suggestions_out.push_back(String(suggestions[i],encoding));
}
free(suggestions);
}
...@@ -14,6 +14,12 @@ ...@@ -14,6 +14,12 @@
DECLARE_POINTER_TYPE(SpellChecker); DECLARE_POINTER_TYPE(SpellChecker);
#ifdef UNICODE
typedef wxCharBuffer CharBuffer;
#else
typedef char* CharBuffer;
#endif
// ----------------------------------------------------------------------------- : Spell checker // ----------------------------------------------------------------------------- : Spell checker
/// A spelling checker for a particular language /// A spelling checker for a particular language
...@@ -33,9 +39,13 @@ class SpellChecker : public Hunspell, public IntrusivePtrBase<SpellChecker> { ...@@ -33,9 +39,13 @@ class SpellChecker : public Hunspell, public IntrusivePtrBase<SpellChecker> {
/// Check the spelling of a single word, ignore punctuation /// Check the spelling of a single word, ignore punctuation
bool spell_with_punctuation(const String& word); bool spell_with_punctuation(const String& word);
/// Give spelling suggestions
void suggest(const String& word, vector<String>& suggestions_out);
private: private:
/// Convert between String and dictionary encoding /// Convert between String and dictionary encoding
wxCSConv encoding; wxCSConv encoding;
bool convert_encoding(const String& word, CharBuffer& out);
SpellChecker(const char* aff_path, const char* dic_path); SpellChecker(const char* aff_path, const char* dic_path);
static map<String,SpellCheckerP> spellers; //< Cached checkers for each language static map<String,SpellCheckerP> spellers; //< Cached checkers for each language
......
...@@ -120,8 +120,14 @@ enum ChildMenuID { ...@@ -120,8 +120,14 @@ enum ChildMenuID {
, ID_FORMAT_REMINDER , ID_FORMAT_REMINDER
, ID_INSERT_SYMBOL , ID_INSERT_SYMBOL
// Spelling errors
, ID_SPELLING_ADD_TO_DICT = 6301
, ID_SPELLING_NO_SUGGEST
, ID_SPELLING_SUGGEST
, ID_SPELLING_SUGGEST_MAX = 6399
// Graph menu // Graph menu
, ID_GRAPH_PIE = 6301 // corresponds to GraphType , ID_GRAPH_PIE = 6401 // corresponds to GraphType
, ID_GRAPH_BAR , ID_GRAPH_BAR
, ID_GRAPH_STACK , ID_GRAPH_STACK
, ID_GRAPH_SCATTER , ID_GRAPH_SCATTER
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment