X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FText.cpp;h=187e3872312f2ff66f3edb5705906c85703f5cfe;hb=f1a388584fb2c043d17127d7db49c36cb8427cfa;hp=a2ae1aed8f33b772ea527dbb824c2ff9b874bc4f;hpb=8f821dba70647b1547473aa44926eb1e6819b7a0;p=lyx.git diff --git a/src/Text.cpp b/src/Text.cpp index a2ae1aed8f..187e387231 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -66,11 +66,9 @@ #include "support/gettext.h" #include "support/lassert.h" #include "support/lstrings.h" +#include "support/lyxalgo.h" #include "support/textutils.h" -#include - -#include #include @@ -137,7 +135,7 @@ void breakParagraphConservative(BufferParams const & bparams, ParagraphList & pars, pit_type par_offset, pos_type pos) { // create a new paragraph - Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1), + Paragraph & tmp = *pars.insert(lyx::next(pars.begin(), par_offset + 1), Paragraph()); Paragraph & par = pars[par_offset]; @@ -193,12 +191,12 @@ void mergeParagraph(BufferParams const & bparams, // move the change of the end-of-paragraph character par.setChange(par.size(), change); - pars.erase(boost::next(pars.begin(), par_offset + 1)); + pars.erase(lyx::next(pars.begin(), par_offset + 1)); } Text::Text(InsetText * owner, bool use_default_layout) - : owner_(owner), autoBreakRows_(false), undo_counter_(0) + : owner_(owner) { pars_.push_back(Paragraph()); Paragraph & par = pars_.back(); @@ -212,7 +210,7 @@ Text::Text(InsetText * owner, bool use_default_layout) Text::Text(InsetText * owner, Text const & text) - : owner_(owner), autoBreakRows_(text.autoBreakRows_), undo_counter_(0) + : owner_(owner) { pars_ = text.pars_; ParagraphList::iterator const end = pars_.end(); @@ -362,7 +360,7 @@ void Text::readParToken(Paragraph & par, Lexer & lex, string const & token, Font & font, Change & change, ErrorList & errorList) { Buffer * buf = const_cast(&owner_->buffer()); - BufferParams const & bp = buf->params(); + BufferParams & bp = buf->params(); if (token[0] != '\\') { docstring dstr = lex.getDocString(); @@ -486,18 +484,42 @@ void Text::readParToken(Paragraph & par, Lexer & lex, } else if (token == "\\color") { lex.next(); setLyXColor(lex.getString(), font.fontInfo()); - } else if (token == "\\SpecialChar") { + } else if (token == "\\SpecialChar" || + (token == "\\SpecialCharNoPassThru" && + !par.layout().pass_thru && !inset().isPassThru())) { auto_ptr inset; inset.reset(new InsetSpecialChar); inset->read(lex); inset->setBuffer(*buf); par.insertInset(par.size(), inset.release(), font, change); + } else if (token == "\\SpecialCharNoPassThru") { + lex.next(); + docstring const s = ltrim(lex.getDocString(), "\\"); + par.insert(par.size(), s, font, change); } else if (token == "\\IPAChar") { auto_ptr inset; inset.reset(new InsetIPAChar); inset->read(lex); inset->setBuffer(*buf); par.insertInset(par.size(), inset.release(), font, change); + } else if (token == "\\twohyphens" || token == "\\threehyphens") { + // Ideally, this should be done by lyx2lyx, but lyx2lyx does not know the + // running font and does not know anything about layouts (and CopyStyle). + Layout const & layout(par.layout()); + FontInfo info = font.fontInfo(); + info.realize(layout.resfont); + if (layout.pass_thru || inset().isPassThru() || + info.family() == TYPEWRITER_FAMILY) { + if (token == "\\twohyphens") + par.insert(par.size(), from_ascii("--"), font, change); + else + par.insert(par.size(), from_ascii("---"), font, change); + } else { + if (token == "\\twohyphens") + par.insertChar(par.size(), 0x2013, font, change); + else + par.insertChar(par.size(), 0x2014, font, change); + } } else if (token == "\\backslash") { par.appendChar('\\', font, change); } else if (token == "\\LyXTable") { @@ -512,18 +534,25 @@ void Text::readParToken(Paragraph & par, Lexer & lex, int aid; time_t ct; is >> aid >> ct; - BufferParams::AuthorMap const & am = bp.author_map; + BufferParams::AuthorMap const & am = bp.author_map_; if (am.find(aid) == am.end()) { - errorList.push_back(ErrorItem(_("Change tracking error"), - bformat(_("Unknown author index for change: %1$d\n"), aid), - par.id(), 0, par.size())); - change = Change(Change::UNCHANGED); - } else { - if (token == "\\change_inserted") - change = Change(Change::INSERTED, am.find(aid)->second, ct); - else - change = Change(Change::DELETED, am.find(aid)->second, ct); + errorList.push_back(ErrorItem( + _("Change tracking author index missing"), + bformat(_("A change tracking author information for index " + "%1$d is missing. This can happen after a wrong " + "merge by a version control system. In this case, " + "either fix the merge, or have this information " + "missing until the corresponding tracked changes " + "are merged or this user edits the file again.\n"), + aid), + par.id(), par.size(), par.size() + 1 + )); + bp.addAuthor(Author(aid)); } + if (token == "\\change_inserted") + change = Change(Change::INSERTED, am.find(aid)->second, ct); + else + change = Change(Change::DELETED, am.find(aid)->second, ct); } else { lex.eatLine(); errorList.push_back(ErrorItem(_("Unknown token"), @@ -639,7 +668,7 @@ static void breakParagraph(Text & text, pit_type par_offset, pos_type pos, ParagraphList & pars = text.paragraphs(); // create a new paragraph, and insert into the list ParagraphList::iterator tmp = - pars.insert(boost::next(pars.begin(), par_offset + 1), + pars.insert(lyx::next(pars.begin(), par_offset + 1), Paragraph()); Paragraph & par = pars[par_offset]; @@ -815,7 +844,7 @@ void Text::insertStringAsLines(Cursor & cur, docstring const & str, cit != str.end(); ++cit) { Paragraph & par = pars_[pit]; if (*cit == '\n') { - if (autoBreakRows_ && (!par.empty() || par.allowEmpty())) { + if (inset().allowMultiPar() && (!par.empty() || par.allowEmpty())) { lyx::breakParagraph(*this, pit, pos, par.layout().isEnvironment()); ++pit; @@ -824,7 +853,7 @@ void Text::insertStringAsLines(Cursor & cur, docstring const & str, } else { continue; } - // do not insert consecutive spaces if !free_spacing + // do not insert consecutive spaces if !free_spacing } else if ((*cit == ' ' || *cit == '\t') && space_inserted && !par.isFreeSpacing()) { continue; @@ -1019,14 +1048,36 @@ void Text::insertChar(Cursor & cur, char_type c) } } - par.insertChar(cur.pos(), c, cur.current_font, + pos_type pos = cur.pos(); + if (!cur.paragraph().isPassThru() && owner_->lyxCode() != IPA_CODE && + cur.current_font.fontInfo().family() != TYPEWRITER_FAMILY && + c == '-' && pos > 0) { + if (par.getChar(pos - 1) == '-') { + // convert "--" to endash + par.eraseChar(pos - 1, cur.buffer()->params().track_changes); + c = 0x2013; + pos--; + } else if (par.getChar(pos - 1) == 0x2013) { + // convert "---" to emdash + par.eraseChar(pos - 1, cur.buffer()->params().track_changes); + c = 0x2014; + pos--; + } else if (par.getChar(pos - 1) == 0x2014) { + // convert "----" to "-" + par.eraseChar(pos - 1, cur.buffer()->params().track_changes); + c = '-'; + pos--; + } + } + + par.insertChar(pos, c, cur.current_font, cur.buffer()->params().track_changes); cur.checkBufferStructure(); // cur.screenUpdateFlags(Update::Force); bool boundary = cur.boundary() - || tm.isRTLBoundary(cur.pit(), cur.pos() + 1); - setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary); + || tm.isRTLBoundary(cur.pit(), pos + 1); + setCursor(cur, cur.pit(), pos + 1, false, boundary); charInserted(cur); } @@ -1035,15 +1086,6 @@ void Text::charInserted(Cursor & cur) { Paragraph & par = cur.paragraph(); - // Here we call finishUndo for every 20 characters inserted. - // This is from my experience how emacs does it. (Lgb) - if (undo_counter_ < 20) { - ++undo_counter_; - } else { - cur.finishUndo(); - undo_counter_ = 0; - } - // register word if a non-letter was entered if (cur.pos() > 1 && !par.isWordSeparator(cur.pos() - 2) @@ -1078,7 +1120,7 @@ bool Text::cursorForwardOneWord(Cursor & cur) if (lyxrc.mac_like_cursor_movement) { // Skip through trailing punctuation and spaces. while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos))) - ++pos; + ++pos; // Skip over either a non-char inset or a full word if (pos != lastpos && par.isWordSeparator(pos)) @@ -1151,7 +1193,6 @@ bool Text::cursorVisLeftOneWord(Cursor & cur) LBUFERR(this == cur.text()); pos_type left_pos, right_pos; - bool left_is_letter, right_is_letter; Cursor temp_cur = cur; @@ -1160,9 +1201,9 @@ bool Text::cursorVisLeftOneWord(Cursor & cur) // collect some information about current cursor position temp_cur.getSurroundingPos(left_pos, right_pos); - left_is_letter = + bool left_is_letter = (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false); - right_is_letter = + bool right_is_letter = (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false); // if we're not at a letter/non-letter boundary, continue moving @@ -1188,7 +1229,6 @@ bool Text::cursorVisRightOneWord(Cursor & cur) LBUFERR(this == cur.text()); pos_type left_pos, right_pos; - bool left_is_letter, right_is_letter; Cursor temp_cur = cur; @@ -1197,9 +1237,9 @@ bool Text::cursorVisRightOneWord(Cursor & cur) // collect some information about current cursor position temp_cur.getSurroundingPos(left_pos, right_pos); - left_is_letter = + bool left_is_letter = (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false); - right_is_letter = + bool right_is_letter = (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false); // if we're not at a letter/non-letter boundary, continue moving @@ -1226,7 +1266,7 @@ void Text::selectWord(Cursor & cur, word_location loc) { LBUFERR(this == cur.text()); CursorSlice from = cur.top(); - CursorSlice to = cur.top(); + CursorSlice to; getWord(from, to, loc); if (cur.top() != from) setCursor(cur, from.pit(), from.pos()); @@ -1454,20 +1494,19 @@ void Text::deleteWordBackward(Cursor & cur) // Kill to end of line. -void Text::changeCase(Cursor & cur, TextCase action) +void Text::changeCase(Cursor & cur, TextCase action, bool partial) { LBUFERR(this == cur.text()); CursorSlice from; CursorSlice to; - bool gotsel = false; - if (cur.selection()) { + bool const gotsel = cur.selection(); + if (gotsel) { from = cur.selBegin(); to = cur.selEnd(); - gotsel = true; } else { from = cur.top(); - getWord(from, to, PARTIAL_WORD); + getWord(from, to, partial ? PARTIAL_WORD : WHOLE_WORD); cursorForwardOneWord(cur); } @@ -1521,7 +1560,7 @@ bool Text::handleBibitems(Cursor & cur) // if a bibitem is deleted, merge with previous paragraph // if this is a bibliography item as well if (cur.pit() > 0 && par.layout() == prevpar.layout()) { - cur.recordUndo(ATOMIC_UNDO, prevcur.pit()); + cur.recordUndo(prevcur.pit()); mergeParagraph(bufparams, cur.text()->paragraphs(), prevcur.pit()); cur.forceBufferUpdate(); @@ -1603,15 +1642,15 @@ bool Text::backspacePos0(Cursor & cur) // is it an empty paragraph? if (cur.lastpos() == 0 || (cur.lastpos() == 1 && par.isSeparator(0))) { - cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit()); - plist.erase(boost::next(plist.begin(), cur.pit())); + cur.recordUndo(prevcur.pit()); + plist.erase(lyx::next(plist.begin(), cur.pit())); needsUpdate = true; } // is previous par empty? else if (prevcur.lastpos() == 0 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) { - cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit()); - plist.erase(boost::next(plist.begin(), prevcur.pit())); + cur.recordUndo(prevcur.pit()); + plist.erase(lyx::next(plist.begin(), prevcur.pit())); needsUpdate = true; } // Pasting is not allowed, if the paragraphs have different @@ -1622,7 +1661,7 @@ bool Text::backspacePos0(Cursor & cur) else if (par.layout() == prevpar.layout() || tclass.isDefaultLayout(par.layout()) || tclass.isPlainLayout(par.layout())) { - cur.recordUndo(ATOMIC_UNDO, prevcur.pit()); + cur.recordUndo(prevcur.pit()); mergeParagraph(bufparams, plist, prevcur.pit()); needsUpdate = true; } @@ -1648,7 +1687,7 @@ bool Text::backspace(Cursor & cur) --prev_cur.pit(); if (!prev_cur.paragraph().isMergedOnEndOfParDeletion(cur.buffer()->params().track_changes)) { - cur.recordUndo(ATOMIC_UNDO, prev_cur.pit(), prev_cur.pit()); + cur.recordUndo(prev_cur.pit(), prev_cur.pit()); prev_cur.paragraph().setChange(prev_cur.lastpos(), Change(Change::DELETED)); setCursorIntern(cur, prev_cur.pit(), prev_cur.lastpos()); return true; @@ -2011,16 +2050,25 @@ docstring Text::asString(pit_type beg, pit_type end, int options) const } -void Text::forOutliner(docstring & os, size_t maxlen, bool shorten) const +void Text::shortenForOutliner(docstring & str, size_t const maxlen) { - if (maxlen == 0) - maxlen = std::numeric_limits::max(); - else - LASSERT(maxlen >= 8, maxlen = TOC_ENTRY_LENGTH); - for (size_t i = 0; i != pars_.size() && os.length() < maxlen; ++i) - pars_[i].forOutliner(os, maxlen); - if (shorten && os.length() >= maxlen) - os = os.substr(0, maxlen - 3) + from_ascii("..."); + support::truncateWithEllipsis(str, maxlen); + docstring::iterator it = str.begin(); + docstring::iterator end = str.end(); + for (; it != end; ++it) + if ((*it) == L'\n' || (*it) == L'\t') + (*it) = L' '; +} + + +void Text::forOutliner(docstring & os, size_t const maxlen, + bool const shorten) const +{ + size_t tmplen = shorten ? maxlen + 1 : maxlen; + for (size_t i = 0; i != pars_.size() && os.length() < tmplen; ++i) + pars_[i].forOutliner(os, tmplen, false); + if (shorten) + shortenForOutliner(os, maxlen); }