#9496 factor out getting language from LFUN args or cursor position in speller LFUNs

This commit is contained in:
Stephan Witt 2015-07-16 21:26:41 +02:00
parent 5fe5ae3e80
commit 7d420669e6
2 changed files with 17 additions and 25 deletions

View File

@ -486,6 +486,15 @@ bool Text::isRTL(Paragraph const & par) const
return par.isRTL(buffer.params()); return par.isRTL(buffer.params());
} }
namespace {
Language const * getLanguage(Cursor const & cur, string const & lang) {
return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
}
}
void Text::dispatch(Cursor & cur, FuncRequest & cmd) void Text::dispatch(Cursor & cur, FuncRequest & cmd)
{ {
@ -2343,8 +2352,8 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
} }
case LFUN_SPELLING_ADD: { case LFUN_SPELLING_ADD: {
Language const * language = getLanguage(cur, cmd.getArg(1));
docstring word = from_utf8(cmd.getArg(0)); docstring word = from_utf8(cmd.getArg(0));
Language * lang;
if (word.empty()) { if (word.empty()) {
word = cur.selectionAsString(false); word = cur.selectionAsString(false);
// FIXME // FIXME
@ -2353,22 +2362,15 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
selectWordWhenUnderCursor(cur, WHOLE_WORD); selectWordWhenUnderCursor(cur, WHOLE_WORD);
word = cur.selectionAsString(false); word = cur.selectionAsString(false);
} }
lang = const_cast<Language *>(cur.getFont().language());
} else if (cmd.getArg(1).empty()) {
// optional language argument is missing
// use the language at cursor position
lang = const_cast<Language *>(cur.getFont().language());
} else {
lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
} }
WordLangTuple wl(word, lang); WordLangTuple wl(word, language);
theSpellChecker()->insert(wl); theSpellChecker()->insert(wl);
break; break;
} }
case LFUN_SPELLING_IGNORE: { case LFUN_SPELLING_IGNORE: {
Language const * language = getLanguage(cur, cmd.getArg(1));
docstring word = from_utf8(cmd.getArg(0)); docstring word = from_utf8(cmd.getArg(0));
Language * lang;
if (word.empty()) { if (word.empty()) {
word = cur.selectionAsString(false); word = cur.selectionAsString(false);
// FIXME // FIXME
@ -2377,20 +2379,15 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
selectWordWhenUnderCursor(cur, WHOLE_WORD); selectWordWhenUnderCursor(cur, WHOLE_WORD);
word = cur.selectionAsString(false); word = cur.selectionAsString(false);
} }
lang = const_cast<Language *>(cur.getFont().language());
} else if (cmd.getArg(1).empty()) {
lang = const_cast<Language *>(cur.getFont().language());
} else {
lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
} }
WordLangTuple wl(word, lang); WordLangTuple wl(word, language);
theSpellChecker()->accept(wl); theSpellChecker()->accept(wl);
break; break;
} }
case LFUN_SPELLING_REMOVE: { case LFUN_SPELLING_REMOVE: {
Language const * language = getLanguage(cur, cmd.getArg(1));
docstring word = from_utf8(cmd.getArg(0)); docstring word = from_utf8(cmd.getArg(0));
Language * lang;
if (word.empty()) { if (word.empty()) {
word = cur.selectionAsString(false); word = cur.selectionAsString(false);
// FIXME // FIXME
@ -2399,13 +2396,8 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
selectWordWhenUnderCursor(cur, WHOLE_WORD); selectWordWhenUnderCursor(cur, WHOLE_WORD);
word = cur.selectionAsString(false); word = cur.selectionAsString(false);
} }
lang = const_cast<Language *>(cur.getFont().language());
} else if (cmd.getArg(1).empty()) {
lang = const_cast<Language *>(cur.getFont().language());
} else {
lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
} }
WordLangTuple wl(word, lang); WordLangTuple wl(word, language);
theSpellChecker()->remove(wl); theSpellChecker()->remove(wl);
break; break;
} }

View File

@ -29,7 +29,7 @@ class WordLangTuple {
public: public:
WordLangTuple() : lang_(0) {} WordLangTuple() : lang_(0) {}
WordLangTuple(docstring const & w, Language * l) WordLangTuple(docstring const & w, Language const * l)
: word_(w), lang_(l) : word_(w), lang_(l)
{} {}
@ -47,7 +47,7 @@ private:
/// the word /// the word
docstring word_; docstring word_;
/// language of word /// language of word
Language * lang_; Language const * lang_;
}; };