diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 695950fe82..39874ee017 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -20,7 +20,6 @@ #include "buffer_funcs.h" #include "BufferList.h" #include "BufferParams.h" -#include "bufferview_funcs.h" #include "callback.h" // added for Dispatch functions #include "CoordCache.h" #include "CutAndPaste.h" diff --git a/src/Cursor.cpp b/src/Cursor.cpp index 1f1ed77266..40d7c37e83 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -15,7 +15,6 @@ #include "Bidi.h" #include "BufferView.h" -#include "bufferview_funcs.h" #include "Buffer.h" #include "Cursor.h" #include "CoordCache.h" diff --git a/src/Font.cpp b/src/Font.cpp index b0073ebeb8..f4e9bd053b 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -27,11 +27,14 @@ #include "output_latex.h" #include "OutputParams.h" +#include "support/convert.h" #include "support/lstrings.h" using std::endl; using std::string; using std::ostream; +using std::ostringstream; +using std::istringstream; using std::pair; #ifndef CXX_GLOBAL_CSTD @@ -974,6 +977,99 @@ Color_color Font::realColor() const } +std::string Font::toString(bool const toggle) const +{ + string lang = "ignore"; + if (language()) + lang = language()->lang(); + + ostringstream os; + os << "family " << family() << '\n' + << "series " << series() << '\n' + << "shape " << shape() << '\n' + << "size " << size() << '\n' + << "emph " << emph() << '\n' + << "underbar " << underbar() << '\n' + << "noun " << noun() << '\n' + << "number " << number() << '\n' + << "color " << color() << '\n' + << "language " << lang << '\n' + << "toggleall " << convert(toggle); + return os.str(); +} + + +bool Font::fromString(string const & data, bool & toggle) +{ + istringstream is(data); + Lexer lex(0,0); + lex.setStream(is); + + int nset = 0; + while (lex.isOK()) { + string token; + if (lex.next()) + token = lex.getString(); + + if (token.empty() || !lex.next()) + break; + + if (token == "family") { + int const next = lex.getInteger(); + setFamily(FONT_FAMILY(next)); + + } else if (token == "series") { + int const next = lex.getInteger(); + setSeries(FONT_SERIES(next)); + + } else if (token == "shape") { + int const next = lex.getInteger(); + setShape(FONT_SHAPE(next)); + + } else if (token == "size") { + int const next = lex.getInteger(); + setSize(FONT_SIZE(next)); + + } else if (token == "emph" || token == "underbar" || + token == "noun" || token == "number") { + + int const next = lex.getInteger(); + FONT_MISC_STATE const misc = FONT_MISC_STATE(next); + + if (token == "emph") + setEmph(misc); + else if (token == "underbar") + setUnderbar(misc); + else if (token == "noun") + setNoun(misc); + else if (token == "number") + setNumber(misc); + + } else if (token == "color") { + int const next = lex.getInteger(); + setColor(Color::color(next)); + + } else if (token == "language") { + string const next = lex.getString(); + if (next == "ignore") + setLanguage(ignore_language); + else + setLanguage(languages.getLanguage(next)); + + } else if (token == "toggleall") { + toggle = lex.getBool(); + + } else { + // Unrecognised token + break; + } + + ++nset; + } + return (nset > 0); +} + + ostream & operator<<(ostream & os, Font::FONT_MISC_STATE fms) { return os << int(fms); diff --git a/src/Font.h b/src/Font.h index 18544f1716..48258b97e5 100644 --- a/src/Font.h +++ b/src/Font.h @@ -25,13 +25,11 @@ namespace lyx { - class Lexer; class BufferParams; class Language; class OutputParams; - /// class Font { public: @@ -340,6 +338,11 @@ public: return bits.shape; } + /// Set \param data using \param font and \param toggle. + std::string toString(bool toggle) const; + + /// Set \param font and \param toggle using \param data. Return success. + bool fromString(std::string const & data, bool & toggle); /** Compaq cxx 6.5 requires that the definition be public so that it can compile operator==() @@ -423,6 +426,11 @@ bool operator!=(Font const & font1, Font const & font2) return !(font1 == font2); } +/** Returns the current freefont, encoded as a std::string to be passed to the + * frontends. + */ +std::string const freefont2string(); + } // namespace lyx diff --git a/src/LyXFunc.cpp b/src/LyXFunc.cpp index a2541330df..e0eafe9b99 100644 --- a/src/LyXFunc.cpp +++ b/src/LyXFunc.cpp @@ -27,7 +27,6 @@ #include "Buffer.h" #include "BufferList.h" #include "BufferParams.h" -#include "bufferview_funcs.h" #include "BufferView.h" #include "callback.h" #include "Color.h" @@ -116,8 +115,6 @@ namespace fs = boost::filesystem; namespace lyx { -using bv_funcs::freefont2string; - using frontend::LyXView; using support::absolutePath; @@ -151,8 +148,7 @@ namespace Alert = frontend::Alert; namespace { -bool getLocalStatus(Cursor cursor, - FuncRequest const & cmd, FuncStatus & status) +bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status) { // Try to fix cursor in case it is broken. cursor.fixIfBroken(); diff --git a/src/Makefile.am b/src/Makefile.am index 7c90cf2cee..3c24d7aa1c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -94,8 +94,6 @@ liblyxcore_la_SOURCES = \ BufferParams.cpp \ BufferParams.h \ BufferView.cpp \ - bufferview_funcs.cpp \ - bufferview_funcs.h \ BufferView.h \ Bullet.cpp \ Bullet.h \ diff --git a/src/Text3.cpp b/src/Text3.cpp index 408405d760..d8607c4e36 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -25,7 +25,6 @@ #include "buffer_funcs.h" #include "BufferParams.h" #include "BufferView.h" -#include "bufferview_funcs.h" #include "Cursor.h" #include "CutAndPaste.h" #include "debug.h" @@ -90,112 +89,92 @@ namespace frontend { extern docstring current_layout; } -namespace { +// globals... +static Font freefont(Font::ALL_IGNORE); +static bool toggleall = false; - // globals... - Font freefont(Font::ALL_IGNORE); - bool toggleall = false; - - void toggleAndShow(Cursor & cur, Text * text, - Font const & font, bool toggleall = true) - { - text->toggleFree(cur, font, toggleall); - - if (font.language() != ignore_language || - font.number() != Font::IGNORE) { - TextMetrics const & tm = cur.bv().textMetrics(text); - if (cur.boundary() != tm.isRTLBoundary(cur.pit(), - cur.pos(), cur.real_current_font)) - text->setCursor(cur, cur.pit(), cur.pos(), - false, !cur.boundary()); - } - } - - - void moveCursor(Cursor & cur, bool selecting) - { - if (selecting || cur.mark()) - cur.setSelection(); - } - - - void finishChange(Cursor & cur, bool selecting) - { - finishUndo(); - moveCursor(cur, selecting); - } - - - void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display) - { - recordUndo(cur); - docstring sel = cur.selectionAsString(false); - - // It may happen that sel is empty but there is a selection - replaceSelection(cur); - - if (sel.empty()) { -#ifdef ENABLE_ASSERTIONS - const int old_pos = cur.pos(); -#endif - cur.insert(new InsetMathHull(hullSimple)); - BOOST_ASSERT(old_pos == cur.pos()); - cur.nextInset()->edit(cur, true); - // don't do that also for LFUN_MATH_MODE - // unless you want end up with always changing - // to mathrm when opening an inlined inset -- - // I really hate "LyXfunc overloading"... - if (display) - cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY)); - // Avoid an unnecessary undo step if cmd.argument - // is empty - if (!cmd.argument().empty()) - cur.dispatch(FuncRequest(LFUN_MATH_INSERT, - cmd.argument())); - } else { - // create a macro if we see "\\newcommand" - // somewhere, and an ordinary formula - // otherwise - if (sel.find(from_ascii("\\newcommand")) == string::npos - && sel.find(from_ascii("\\def")) == string::npos) - { - InsetMathHull * formula = new InsetMathHull; - istringstream is(to_utf8(sel)); - Lexer lex(0, 0); - lex.setStream(is); - formula->read(cur.buffer(), lex); - if (formula->getType() == hullNone) - // Don't create pseudo formulas if - // delimiters are left out - formula->mutate(hullSimple); - cur.insert(formula); - } else { - cur.insert(new MathMacroTemplate(sel)); - } - } - cur.message(from_utf8(N_("Math editor mode"))); - } - -} // namespace anon - - - -namespace bv_funcs { - -string const freefont2string() +static void toggleAndShow(Cursor & cur, Text * text, + Font const & font, bool toggleall = true) { - string data; - if (font2string(freefont, toggleall, data)) - return data; - return string(); -} + text->toggleFree(cur, font, toggleall); + if (font.language() != ignore_language || + font.number() != Font::IGNORE) { + TextMetrics const & tm = cur.bv().textMetrics(text); + if (cur.boundary() != tm.isRTLBoundary(cur.pit(), + cur.pos(), cur.real_current_font)) + text->setCursor(cur, cur.pit(), cur.pos(), + false, !cur.boundary()); + } } -namespace { +static void moveCursor(Cursor & cur, bool selecting) +{ + if (selecting || cur.mark()) + cur.setSelection(); +} -void specialChar(Cursor & cur, InsetSpecialChar::Kind kind) + +static void finishChange(Cursor & cur, bool selecting) +{ + finishUndo(); + moveCursor(cur, selecting); +} + + +static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display) +{ + recordUndo(cur); + docstring sel = cur.selectionAsString(false); + + // It may happen that sel is empty but there is a selection + replaceSelection(cur); + + if (sel.empty()) { +#ifdef ENABLE_ASSERTIONS + const int old_pos = cur.pos(); +#endif + cur.insert(new InsetMathHull(hullSimple)); + BOOST_ASSERT(old_pos == cur.pos()); + cur.nextInset()->edit(cur, true); + // don't do that also for LFUN_MATH_MODE + // unless you want end up with always changing + // to mathrm when opening an inlined inset -- + // I really hate "LyXfunc overloading"... + if (display) + cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY)); + // Avoid an unnecessary undo step if cmd.argument + // is empty + if (!cmd.argument().empty()) + cur.dispatch(FuncRequest(LFUN_MATH_INSERT, + cmd.argument())); + } else { + // create a macro if we see "\\newcommand" + // somewhere, and an ordinary formula + // otherwise + if (sel.find(from_ascii("\\newcommand")) == string::npos + && sel.find(from_ascii("\\def")) == string::npos) + { + InsetMathHull * formula = new InsetMathHull; + istringstream is(to_utf8(sel)); + Lexer lex(0, 0); + lex.setStream(is); + formula->read(cur.buffer(), lex); + if (formula->getType() == hullNone) + // Don't create pseudo formulas if + // delimiters are left out + formula->mutate(hullSimple); + cur.insert(formula); + } else { + cur.insert(new MathMacroTemplate(sel)); + } + } + cur.message(from_utf8(N_("Math editor mode"))); +} + + +static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind) { recordUndo(cur); cap::replaceSelection(cur); @@ -204,7 +183,7 @@ void specialChar(Cursor & cur, InsetSpecialChar::Kind kind) } -bool doInsertInset(Cursor & cur, Text * text, +static bool doInsertInset(Cursor & cur, Text * text, FuncRequest const & cmd, bool edit, bool pastesel) { Inset * inset = createInset(&cur.bv(), cmd); @@ -235,7 +214,10 @@ bool doInsertInset(Cursor & cur, Text * text, } -} // anon namespace +string const freefont2string() +{ + return freefont.toString(toggleall); +} void Text::number(Cursor & cur) @@ -1364,7 +1346,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd) case LFUN_FONT_FREE_UPDATE: { Font font; bool toggle; - if (bv_funcs::string2font(to_utf8(cmd.argument()), font, toggle)) { + if (font.fromString(to_utf8(cmd.argument()), toggle)) { freefont = font; toggleall = toggle; toggleAndShow(cur, this, freefont, toggleall); diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index 2403891392..8b77123985 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -24,7 +24,6 @@ #include "buffer_funcs.h" #include "BufferParams.h" #include "BufferView.h" -#include "bufferview_funcs.h" #include "Color.h" #include "CutAndPaste.h" #include "debug.h" diff --git a/src/bufferview_funcs.cpp b/src/bufferview_funcs.cpp deleted file mode 100644 index 3a3a45c8aa..0000000000 --- a/src/bufferview_funcs.cpp +++ /dev/null @@ -1,153 +0,0 @@ -/** - * \file bufferview_funcs.cpp - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Lars Gullik Bjønnes - * \author Jean-Marc Lasgouttes - * \author John Levon - * \author Angus Leeming - * \author Juergen Vigna - * - * Full author contact details are available in file CREDITS. - */ - -#include - -#include "bufferview_funcs.h" - -#include "Buffer.h" -#include "BufferParams.h" -#include "BufferView.h" -#include "Cursor.h" -#include "CoordCache.h" -#include "gettext.h" -#include "Language.h" -#include "Color.h" -#include "Lexer.h" - -#include "frontends/alert.h" - -#include "insets/InsetCommand.h" -#include "insets/InsetText.h" - -#include "support/convert.h" - -#include - -using std::istringstream; -using std::ostringstream; -using std::string; -using std::vector; -using std::find; - - -namespace lyx { - -using support::bformat; - -namespace bv_funcs { - -// Set data using font and toggle -// If successful, returns true -bool font2string(Font const & font, bool const toggle, string & data) -{ - string lang = "ignore"; - if (font.language()) - lang = font.language()->lang(); - - ostringstream os; - os << "family " << font.family() << '\n' - << "series " << font.series() << '\n' - << "shape " << font.shape() << '\n' - << "size " << font.size() << '\n' - << "emph " << font.emph() << '\n' - << "underbar " << font.underbar() << '\n' - << "noun " << font.noun() << '\n' - << "number " << font.number() << '\n' - << "color " << font.color() << '\n' - << "language " << lang << '\n' - << "toggleall " << convert(toggle); - data = os.str(); - return true; -} - - -// Set font and toggle using data -// If successful, returns true -bool string2font(string const & data, Font & font, bool & toggle) -{ - istringstream is(data); - Lexer lex(0,0); - lex.setStream(is); - - int nset = 0; - while (lex.isOK()) { - string token; - if (lex.next()) - token = lex.getString(); - - if (token.empty() || !lex.next()) - break; - - if (token == "family") { - int const next = lex.getInteger(); - font.setFamily(Font::FONT_FAMILY(next)); - - } else if (token == "series") { - int const next = lex.getInteger(); - font.setSeries(Font::FONT_SERIES(next)); - - } else if (token == "shape") { - int const next = lex.getInteger(); - font.setShape(Font::FONT_SHAPE(next)); - - } else if (token == "size") { - int const next = lex.getInteger(); - font.setSize(Font::FONT_SIZE(next)); - - } else if (token == "emph" || token == "underbar" || - token == "noun" || token == "number") { - - int const next = lex.getInteger(); - Font::FONT_MISC_STATE const misc = - Font::FONT_MISC_STATE(next); - - if (token == "emph") - font.setEmph(misc); - else if (token == "underbar") - font.setUnderbar(misc); - else if (token == "noun") - font.setNoun(misc); - else if (token == "number") - font.setNumber(misc); - - } else if (token == "color") { - int const next = lex.getInteger(); - font.setColor(Color::color(next)); - - } else if (token == "language") { - string const next = lex.getString(); - if (next == "ignore") - font.setLanguage(ignore_language); - else - font.setLanguage(languages.getLanguage(next)); - - } else if (token == "toggleall") { - toggle = lex.getBool(); - - } else { - // Unrecognised token - break; - } - - ++nset; - } - return (nset > 0); -} - - -} // namespace bv_funcs - - -} // namespace lyx diff --git a/src/bufferview_funcs.h b/src/bufferview_funcs.h deleted file mode 100644 index 0f45b766cc..0000000000 --- a/src/bufferview_funcs.h +++ /dev/null @@ -1,41 +0,0 @@ -// -*- C++ -*- -/** - * \file bufferview_funcs.h - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Lars Gullik Bjønnes - * \author Jean-Marc Lasgouttes - * \author Angus Leeming - * - * Full author contact details are available in file CREDITS. - */ - -#ifndef BUFFERVIEW_FUNCS_H -#define BUFFERVIEW_FUNCS_H - -#include - -namespace lyx { - -class Font; - -namespace bv_funcs { - -/// Set \param data using \param font and \param toggle. Return success. -bool font2string(Font const & font, bool toggle, std::string & data); - -/// Set \param font and \param toggle using \param data. Return success. -bool string2font(std::string const & data, Font & font, bool & toggle); - -/** Returns the current freefont, encoded as a std::string to be passed to the - * frontends. - */ -std::string const freefont2string(); - -} // namespace bv_funcs - - -} // namespace lyx - -#endif diff --git a/src/frontends/LyXView.cpp b/src/frontends/LyXView.cpp index 5a41bce238..7bb044961c 100644 --- a/src/frontends/LyXView.cpp +++ b/src/frontends/LyXView.cpp @@ -23,7 +23,6 @@ #include "BufferList.h" #include "BufferParams.h" #include "BufferView.h" -#include "bufferview_funcs.h" #include "Cursor.h" #include "debug.h" #include "ErrorList.h" diff --git a/src/frontends/controllers/ControlCharacter.cpp b/src/frontends/controllers/ControlCharacter.cpp index 7ee0b2308b..5e01685464 100644 --- a/src/frontends/controllers/ControlCharacter.cpp +++ b/src/frontends/controllers/ControlCharacter.cpp @@ -14,12 +14,10 @@ #include "Buffer.h" #include "BufferParams.h" -#include "bufferview_funcs.h" #include "FuncRequest.h" #include "Language.h" #include "Color.h" -using lyx::bv_funcs::font2string; using std::string; namespace lyx { @@ -62,17 +60,14 @@ void ControlCharacter::dispatchParams() if (!font_) return; - string data; - if (font2string(*font_, toggleall_, data)) - dispatch(FuncRequest(getLfun(), data)); + string data = font_->toString(toggleall_); + dispatch(FuncRequest(getLfun(), data)); } Font::FONT_FAMILY ControlCharacter::getFamily() const { - if (!font_) - return Font::IGNORE_FAMILY; - return font_->family(); + return font_ ? font_->family() : Font::IGNORE_FAMILY; } @@ -84,9 +79,7 @@ void ControlCharacter::setFamily(Font::FONT_FAMILY val) Font::FONT_SERIES ControlCharacter::getSeries() const { - if (!font_) - return Font::IGNORE_SERIES; - return font_->series(); + return font_ ? font_->series() : Font::IGNORE_SERIES; } @@ -98,9 +91,7 @@ void ControlCharacter::setSeries(Font::FONT_SERIES val) Font::FONT_SHAPE ControlCharacter::getShape() const { - if (!font_) - return Font::IGNORE_SHAPE; - return font_->shape(); + return font_ ? font_->shape() : Font::IGNORE_SHAPE; } @@ -112,9 +103,7 @@ void ControlCharacter::setShape(Font::FONT_SHAPE val) Font::FONT_SIZE ControlCharacter::getSize() const { - if (!font_) - return Font::IGNORE_SIZE; - return font_->size(); + return font_ ? font_->size() : Font::IGNORE_SIZE; } @@ -138,9 +127,9 @@ FONT_STATE ControlCharacter::getBar() const if (font_->noun() == Font::TOGGLE) return NOUN_TOGGLE; - if (font_->emph() == Font::IGNORE && - font_->underbar() == Font::IGNORE && - font_->noun() == Font::IGNORE) + if (font_->emph() == Font::IGNORE + && font_->underbar() == Font::IGNORE + && font_->noun() == Font::IGNORE) return IGNORE; return INHERIT; diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index f858281064..4fd0dc75e1 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -25,9 +25,6 @@ #include "MathSupport.h" #include "InsetMathRef.h" -#include "bufferview_funcs.h" -#include "Text.h" - #include "Buffer.h" #include "BufferParams.h" #include "BufferView.h" @@ -43,6 +40,7 @@ #include "LyXRC.h" #include "OutputParams.h" #include "sgml.h" +#include "Text.h" #include "TextPainter.h" #include "Undo.h" @@ -1304,7 +1302,7 @@ void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg) recordUndo(cur); Font font; bool b; - bv_funcs::string2font(to_utf8(arg), font, b); + font.fromString(to_utf8(arg), b); if (font.color() != Color::inherit) { MathAtom at = MathAtom(new InsetMathColor(true, font.color())); cur.handleNest(at, 0); diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index f027b326fe..72f8b4ad5e 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -34,7 +34,6 @@ #include "MathSupport.h" #include "Bidi.h" -#include "bufferview_funcs.h" #include "BufferView.h" #include "Color.h" #include "CoordCache.h" @@ -432,7 +431,7 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg) recordUndo(cur, Undo::ATOMIC); Font font; bool b; - bv_funcs::string2font(to_utf8(arg), font, b); + font.fromString(to_utf8(arg), b); if (font.color() != Color::inherit) { MathAtom at = MathAtom(new InsetMathColor(true, font.color())); cur.handleNest(at, 0);