]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Properly restore the file encoding after a LaTeX environment with local scope.
[lyx.git] / src / Paragraph.cpp
index 1a89df078aab5dfd10607f73456bea4f9fc7221e..6a0c16a1887e6e8be9b4a9fecb40e26e4d959748 100644 (file)
 #include "insets/InsetBibitem.h"
 #include "insets/InsetLabel.h"
 
-#include "support/lassert.h"
 #include "support/debug.h"
+#include "support/docstring_list.h"
 #include "support/ExceptionMessage.h"
 #include "support/gettext.h"
+#include "support/lassert.h"
 #include "support/lstrings.h"
-#include "support/Messages.h"
 #include "support/textutils.h"
 
 #include <sstream>
@@ -95,7 +95,7 @@ public:
        /// Output the surrogate pair formed by \p c and \p next to \p os.
        /// \return the number of characters written.
        int latexSurrogatePair(odocstream & os, char_type c, char_type next,
-                              Encoding const &);
+                              OutputParams const &);
 
        /// Output a space in appropriate formatting (or a surrogate pair
        /// if the next character is a combining character).
@@ -362,8 +362,7 @@ Change const & Paragraph::lookupChange(pos_type pos) const
 }
 
 
-void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
-               pos_type end)
+void Paragraph::acceptChanges(pos_type start, pos_type end)
 {
        LASSERT(start >= 0 && start <= size(), /**/);
        LASSERT(end > start && end <= size() + 1, /**/);
@@ -373,14 +372,14 @@ void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
                        case Change::UNCHANGED:
                                // accept changes in nested inset
                                if (Inset * inset = getInset(pos))
-                                       inset->acceptChanges(bparams);
+                                       inset->acceptChanges();
                                break;
 
                        case Change::INSERTED:
                                d->changes_.set(Change(Change::UNCHANGED), pos);
                                // also accept changes in nested inset
                                if (Inset * inset = getInset(pos))
-                                       inset->acceptChanges(bparams);
+                                       inset->acceptChanges();
                                break;
 
                        case Change::DELETED:
@@ -398,8 +397,7 @@ void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
 }
 
 
-void Paragraph::rejectChanges(BufferParams const & bparams,
-               pos_type start, pos_type end)
+void Paragraph::rejectChanges(pos_type start, pos_type end)
 {
        LASSERT(start >= 0 && start <= size(), /**/);
        LASSERT(end > start && end <= size() + 1, /**/);
@@ -409,7 +407,7 @@ void Paragraph::rejectChanges(BufferParams const & bparams,
                        case Change::UNCHANGED:
                                // reject changes in nested inset
                                if (Inset * inset = getInset(pos))
-                                               inset->rejectChanges(bparams);
+                                               inset->rejectChanges();
                                break;
 
                        case Change::INSERTED:
@@ -544,7 +542,7 @@ int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
 
 
 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
-               char_type next, Encoding const & encoding)
+               char_type next, OutputParams const & runparams)
 {
        // Writing next here may circumvent a possible font change between
        // c and next. Since next is only output if it forms a surrogate pair
@@ -553,12 +551,18 @@ int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
        // hopefully impossible to input.
        // FIXME: change tracking
        // Is this correct WRT change tracking?
+       Encoding const & encoding = *(runparams.encoding);
        docstring const latex1 = encoding.latexChar(next);
        docstring const latex2 = encoding.latexChar(c);
        if (docstring(1, next) == latex1) {
                // the encoding supports the combination
                os << latex2 << latex1;
                return latex1.length() + latex2.length();
+       } else if (runparams.local_font &&
+                  runparams.local_font->language()->lang() == "polutonikogreek") {
+               // polutonikogreek only works without the brackets
+               os << latex1 << latex2;
+               return latex1.length() + latex2.length();
        } else
                os << latex1 << '{' << latex2 << '}';
        return latex1.length() + latex2.length() + 2;
@@ -578,9 +582,8 @@ bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
        if (i + 1 < int(text_.size())) {
                char_type next = text_[i + 1];
                if (Encodings::isCombiningChar(next)) {
-                       Encoding const & encoding = *(runparams.encoding);
                        // This space has an accent, so we must always output it.
-                       column += latexSurrogatePair(os, ' ', next, encoding) - 1;
+                       column += latexSurrogatePair(os, ' ', next, runparams) - 1;
                        return true;
                }
        }
@@ -770,10 +773,11 @@ void Paragraph::Private::latexInset(
        bool close = false;
        odocstream::pos_type const len = os.tellp();
 
-       if (inset->forceLTR() 
+       if (inset->forceLTR()
            && running_font.isRightToLeft()
-               // ERT is an exception, it should be output with no decorations at all
-               && inset->lyxCode() != ERT_CODE) {
+           // ERT is an exception, it should be output with no
+           // decorations at all
+           && inset->lyxCode() != ERT_CODE) {
                if (running_font.language()->lang() == "farsi")
                        os << "\\beginL{}";
                else
@@ -962,7 +966,7 @@ void Paragraph::Private::latexSpecialChar(
                if (i + 1 < int(text_.size())) {
                        char_type next = text_[i + 1];
                        if (Encodings::isCombiningChar(next)) {
-                               column += latexSurrogatePair(os, c, next, encoding) - 1;
+                               column += latexSurrogatePair(os, c, next, runparams) - 1;
                                ++i;
                                break;
                        }
@@ -1190,7 +1194,7 @@ void Paragraph::write(ostream & os, BufferParams const & bparams,
        for (pos_type i = 0; i <= size(); ++i) {
 
                Change const change = lookupChange(i);
-               Changes::lyxMarkChange(os, column, running_change, change);
+               Changes::lyxMarkChange(os, bparams, column, running_change, change);
                running_change = change;
 
                if (i == size())
@@ -1605,35 +1609,17 @@ void Paragraph::setLabelWidthString(docstring const & s)
 }
 
 
-docstring const Paragraph::translateIfPossible(docstring const & s,
-               BufferParams const & bparams) const
-{
-       if (!isAscii(s) || s.empty()) {
-               // This must be a user defined layout. We cannot translate
-               // this, since gettext accepts only ascii keys.
-               return s;
-       }
-       // Probably standard layout, try to translate
-       Messages & m = getMessages(getParLanguage(bparams)->code());
-       return m.get(to_ascii(s));
-}
-
-
 docstring Paragraph::expandLabel(Layout const & layout,
                BufferParams const & bparams, bool process_appendix) const
 {
        DocumentClass const & tclass = bparams.documentClass();
-
-       docstring fmt;
-       if (process_appendix && d->params_.appendix())
-               fmt = translateIfPossible(layout.labelstring_appendix(),
-                       bparams);
-       else
-               fmt = translateIfPossible(layout.labelstring(), bparams);
+       string const & lang = getParLanguage(bparams)->code();
+       bool const in_appendix = process_appendix && d->params_.appendix();
+       docstring fmt = translateIfPossible(layout.labelstring(in_appendix), lang);
 
        if (fmt.empty() && layout.labeltype == LABEL_COUNTER 
            && !layout.counter.empty())
-               return tclass.counters().theCounter(layout.counter);
+               return tclass.counters().theCounter(layout.counter, lang);
 
        // handle 'inherited level parts' in 'fmt',
        // i.e. the stuff between '@' in   '@Section@.\arabic{subsection}'
@@ -1651,7 +1637,7 @@ docstring Paragraph::expandLabel(Layout const & layout,
                }
        }
 
-       return tclass.counters().counterLabel(fmt);
+       return tclass.counters().counterLabel(fmt, lang);
 }
 
 
@@ -1974,6 +1960,8 @@ bool Paragraph::latex(BufferParams const & bparams,
 
        Change runningChange = Change(Change::UNCHANGED);
 
+       Encoding const * const prev_encoding = runparams.encoding;
+
        texrow.start(id(), 0);
 
        // if the paragraph is empty, the loop will not be entered at all
@@ -2095,9 +2083,14 @@ bool Paragraph::latex(BufferParams const & bparams,
                        running_font = font;
                        open_font = true;
                        docstring fontchange = ods.str();
+                       // check whether the fontchange ends with a \\textcolor
+                       // modifier and the text starts with a space (bug 4473)
+                       docstring const last_modifier = rsplit(fontchange, '\\');
+                       if (prefixIs(last_modifier, from_ascii("textcolor")) && c == ' ')
+                               os << fontchange << from_ascii("{}");
                        // check if the fontchange ends with a trailing blank
                        // (like "\small " (see bug 3382)
-                       if (suffixIs(fontchange, ' ') && c == ' ')
+                       else if (suffixIs(fontchange, ' ') && c == ' ')
                                os << fontchange.substr(0, fontchange.size() - 1) 
                                   << from_ascii("{}");
                        else
@@ -2195,10 +2188,8 @@ bool Paragraph::latex(BufferParams const & bparams,
                return_value = false;
        }
 
-       if (allowcust) {
-               column += d->endTeXParParams(bparams, os, texrow,
-                                         runparams);
-       }
+       if (allowcust && d->endTeXParParams(bparams, os, texrow,runparams))
+               runparams.encoding = prev_encoding;
 
        LYXERR(Debug::LATEX, "Paragraph::latex... done " << this);
        return return_value;
@@ -2210,6 +2201,8 @@ bool Paragraph::emptyTag() const
        for (pos_type i = 0; i < size(); ++i) {
                if (Inset const * inset = getInset(i)) {
                        InsetCode lyx_code = inset->lyxCode();
+                       // FIXME testing like that is wrong. What is
+                       // the intent?
                        if (lyx_code != TOC_CODE &&
                            lyx_code != INCLUDE_CODE &&
                            lyx_code != GRAPHICS_CODE &&
@@ -2464,17 +2457,15 @@ bool Paragraph::isLineSeparator(pos_type pos) const
 }
 
 
-/// Used by the spellchecker
-bool Paragraph::isLetter(pos_type pos) const
+bool Paragraph::isWordSeparator(pos_type pos) const
 {
        if (Inset const * inset = getInset(pos))
-               return inset->isLetter();
+               return !inset->isLetter();
        char_type const c = d->text_[pos];
-    // We want to pass the ' and escape chars to the spellchecker
+       // We want to pass the ' and escape chars to the spellchecker
        static docstring const quote = from_utf8(lyxrc.spellchecker_esc_chars + '\'');
-       return (isLetterChar(c) || isDigit(c) || contains(quote, c))
-               && pos != size()
-               && !isDeleted(pos);
+       return (!isLetterChar(c) && !isDigit(c) && !contains(quote, c))
+               || pos == size();
 }
 
 
@@ -2510,8 +2501,7 @@ bool Paragraph::isRTL(BufferParams const & bparams) const
 {
        return lyxrc.rtl_support
                && getParLanguage(bparams)->rightToLeft()
-               && ownerCode() != ERT_CODE
-               && ownerCode() != LISTINGS_CODE;
+               && !inInset().getLayout().forceLTR();
 }
 
 
@@ -2564,8 +2554,11 @@ docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
                if (isPrintable(c) || c == '\t'
                    || (c == '\n' && options & AS_STR_NEWLINES))
                        os.put(c);
-               else if (c == META_INSET && options & AS_STR_INSETS)
+               else if (c == META_INSET && options & AS_STR_INSETS) {
                        getInset(i)->tocString(os);
+                       if (getInset(i)->asInsetMath())
+                               os << " ";
+               }
        }
 
        return os.str();
@@ -2885,33 +2878,32 @@ void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
                        }
                }
 
-               if (!isLetter(pos) || isDeleted(pos)) {
+               if (isWordSeparator(pos) || isDeleted(pos)) {
                        // permit capitalization again
                        capitalize = true;
                }
 
-               if (oldChar != newChar)
+               if (oldChar != newChar) {
                        changes += newChar;
+                       if (pos != right - 1)
+                               continue;
+                       // step behind the changing area
+                       pos++;
+               }
 
-               if (oldChar == newChar || pos == right - 1) {
-                       if (oldChar != newChar) {
-                               // step behind the changing area
-                               pos++;
+               int erasePos = pos - changes.size();
+               for (size_t i = 0; i < changes.size(); i++) {
+                       insertChar(pos, changes[i],
+                                  getFontSettings(bparams,
+                                                  erasePos),
+                                  trackChanges);
+                       if (!eraseChar(erasePos, trackChanges)) {
+                               ++erasePos;
+                               ++pos; // advance
+                               ++right; // expand selection
                        }
-                       int erasePos = pos - changes.size();
-                       for (size_t i = 0; i < changes.size(); i++) {
-                               insertChar(pos, changes[i],
-                                       getFontSettings(bparams,
-                                       erasePos),
-                                       trackChanges);
-                               if (!eraseChar(erasePos, trackChanges)) {
-                                       ++erasePos;
-                                       ++pos; // advance
-                                       ++right; // expand selection
-                               }
-                       }
-                       changes.clear();
                }
+               changes.clear();
        }
 }
 
@@ -2938,10 +2930,10 @@ bool Paragraph::find(docstring const & str, bool cs, bool mw,
 
        // if necessary, check whether string matches word
        if (mw) {
-               if (pos > 0 && isLetter(pos - 1))
+               if (pos > 0 && !isWordSeparator(pos - 1))
                        return false;
                if (pos + strsize < parsize
-                       && isLetter(pos + strsize))
+                       && !isWordSeparator(pos + strsize))
                        return false;
        }
 
@@ -2996,8 +2988,8 @@ void Paragraph::locateWord(pos_type & from, pos_type & to,
        switch (loc) {
        case WHOLE_WORD_STRICT:
                if (from == 0 || from == size()
-                   || !isLetter(from)
-                   || !isLetter(from - 1)) {
+                   || isWordSeparator(from)
+                   || isWordSeparator(from - 1)) {
                        to = from;
                        return;
                }
@@ -3005,13 +2997,13 @@ void Paragraph::locateWord(pos_type & from, pos_type & to,
 
        case WHOLE_WORD:
                // If we are already at the beginning of a word, do nothing
-               if (!from || !isLetter(from - 1))
+               if (!from || isWordSeparator(from - 1))
                        break;
                // no break here, we go to the next
 
        case PREVIOUS_WORD:
                // always move the cursor to the beginning of previous word
-               while (from && isLetter(from - 1))
+               while (from && !isWordSeparator(from - 1))
                        --from;
                break;
        case NEXT_WORD:
@@ -3022,47 +3014,31 @@ void Paragraph::locateWord(pos_type & from, pos_type & to,
                break;
        }
        to = from;
-       while (to < size() && isLetter(to))
+       while (to < size() && !isWordSeparator(to))
                ++to;
 }
 
 
 void Paragraph::collectWords()
 {
-       SpellChecker * speller = theSpellChecker();
-
-       //lyxerr << "Words: ";
        pos_type n = size();
+       WordLangTuple wl;
+       docstring_list suggestions;
        for (pos_type pos = 0; pos < n; ++pos) {
-               if (!isLetter(pos))
+               if (isWordSeparator(pos))
                        continue;
                pos_type from = pos;
                locateWord(from, pos, WHOLE_WORD);
-               if (!lyxrc.spellcheck_continuously && pos - from < 6)
-                       continue;
-
-               docstring word = asString(from, pos, false);
-               if (pos - from >= 6)
+               if (pos - from >= 6) {
+                       docstring word = asString(from, pos, AS_STR_NONE);
                        d->words_.insert(word);
-
-               if (!lyxrc.spellcheck_continuously || !speller)
-                       continue;
-               
-               string const lang_code = lyxrc.spellchecker_alt_lang.empty()
-                       ? getFontSettings(d->inset_owner_->buffer().params(), from).language()->code()
-                       : lyxrc.spellchecker_alt_lang;
-               WordLangTuple wl(word, lang_code);
-               SpellChecker::Result res = speller->check(wl);
-               // ... just ignore any error that the spellchecker reports.
-               if (!speller->error().empty())
-                       continue;
-               bool const misspelled = res != SpellChecker::OK
-                       && res != SpellChecker::IGNORED_WORD;
-               d->fontlist_.setMisspelled(from, pos, misspelled);
-
-               //lyxerr << word << " ";
+               }
+               if (lyxrc.spellcheck_continuously
+                   && spellCheck(from, pos, wl, suggestions)) {
+                       for (size_t i = 0; i != suggestions.size(); ++i)
+                               d->words_.insert(suggestions[i]);
+               }
        }
-       //lyxerr << std::endl;
 }
 
 
@@ -3083,31 +3059,52 @@ void Paragraph::updateWords()
 }
 
 
-bool Paragraph::isMisspelled(pos_type pos) const
+bool Paragraph::spellCheck(pos_type & from, pos_type & to, WordLangTuple & wl,
+       docstring_list & suggestions, bool do_suggestion) const
 {
        SpellChecker * speller = theSpellChecker();
-       pos_type from = pos;
-       pos_type to = pos;
-       locateWord(from, to, WHOLE_WORD);
-       docstring word = asString(from, to, false);
        if (!speller)
                return false;
-               
+
+       locateWord(from, to, WHOLE_WORD);
+       if (from == to || from >= pos_type(d->text_.size()))
+               return false;
+
+       docstring word = asString(from, to, AS_STR_INSETS);
        string const lang_code = lyxrc.spellchecker_alt_lang.empty()
                ? getFontSettings(d->inset_owner_->buffer().params(), from).language()->code()
                : lyxrc.spellchecker_alt_lang;
-       WordLangTuple wl(word, lang_code);
+       wl = WordLangTuple(word, lang_code);
        SpellChecker::Result res = speller->check(wl);
-       // ... just ignore any error that the spellchecker reports.
+       // Just ignore any error that the spellchecker reports.
+       // FIXME: we should through out an exception and catch it in the GUI to
+       // display the error.
        if (!speller->error().empty())
                return false;
 
        bool const misspelled = res != SpellChecker::OK
                && res != SpellChecker::IGNORED_WORD;
+
        if (lyxrc.spellcheck_continuously)
-               d->fontlist_.setMisspelled(from, pos, misspelled);
+               d->fontlist_.setMisspelled(from, to, misspelled);
+
+       if (misspelled && do_suggestion)
+               speller->suggest(wl, suggestions);
+       else
+               suggestions.clear();
+
        return misspelled;
 }
 
 
+bool Paragraph::isMisspelled(pos_type pos) const
+{
+       pos_type from = pos;
+       pos_type to = pos;
+       WordLangTuple wl;
+       docstring_list suggestions;
+       return spellCheck(from, to, wl, suggestions, false);
+}
+
+
 } // namespace lyx