commit 723ab5b9c2d4467861244e9324ec363f12951cfa
Author: Juergen Spitzmueller <[email protected]>
Date: Wed Oct 29 06:07:08 2025 +0100
Improve InsetSpecialChar code
---
src/Text.cpp | 6 ++--
src/insets/InsetInfo.cpp | 6 ++--
src/insets/InsetSpecialChar.cpp | 64 +++++++++++++++++++++--------------------
src/insets/InsetSpecialChar.h | 8 ++++--
4 files changed, 43 insertions(+), 41 deletions(-)
diff --git a/src/Text.cpp b/src/Text.cpp
index d712252f0c..213baf7feb 100644
--- a/src/Text.cpp
+++ b/src/Text.cpp
@@ -931,11 +931,10 @@ void Text::insertStringAsLines(Cursor & cur, docstring
const & str,
space_inserted = true;
}
} else if (specialchars.find(ch) != specialchars.end()
- && (par.insertInset(pos, new
InsetSpecialChar(specialchars.find(ch)->second),
+ && (par.insertInset(pos, new
InsetSpecialChar(cur.buffer(), specialchars.find(ch)->second),
font, bparams.track_changes
? Change(Change::INSERTED)
: Change(Change::UNCHANGED)))) {
- par.getInset(pos)->setBuffer(*cur.buffer());
++pos;
space_inserted = false;
} else if (!isPrintable(ch)) {
@@ -3541,8 +3540,7 @@ void specialChar(Cursor & cur, string const kind)
{
cur.recordUndo();
cap::replaceSelection(cur);
- InsetSpecialChar * sc = new InsetSpecialChar(kind);
- sc->setBuffer(*cur.buffer());
+ InsetSpecialChar * sc = new InsetSpecialChar(cur.buffer(), kind);
sc->update();
cur.insert(sc);
cur.posForward();
diff --git a/src/insets/InsetInfo.cpp b/src/insets/InsetInfo.cpp
index 6dfba1ac77..f064c7dd4d 100644
--- a/src/insets/InsetInfo.cpp
+++ b/src/insets/InsetInfo.cpp
@@ -1080,11 +1080,9 @@ void InsetInfo::build()
//fu.fontInfo().setUnderbar(FONT_ON);
for (docstring const & name : names) {
// do not insert > for the top level menu item
- if (&name != &names.front()) {
- par.insertInset(par.size(), new
InsetSpecialChar("menuseparator"),
+ if (&name != &names.front())
+ par.insertInset(par.size(), new
InsetSpecialChar(&buffer(), "menuseparator"),
f, Change(Change::UNCHANGED));
- par.getInset(par.size() -
1)->setBuffer(buffer());
- }
//FIXME: add proper underlines here. This
// involves rewriting searchMenu used above to
// return a vector of menus. If we do not do
diff --git a/src/insets/InsetSpecialChar.cpp b/src/insets/InsetSpecialChar.cpp
index 46e113661f..98f6d1d9c5 100644
--- a/src/insets/InsetSpecialChar.cpp
+++ b/src/insets/InsetSpecialChar.cpp
@@ -23,7 +23,6 @@
#include "Language.h"
#include "LaTeXFeatures.h"
#include "MetricsInfo.h"
-#include "TextClass.h"
#include "xml.h"
#include "texstream.h"
@@ -46,9 +45,12 @@ namespace lyx {
using support::Lexer;
-InsetSpecialChar::InsetSpecialChar(string const k)
- : Inset(nullptr), kind_(k), unknown_(false)
-{}
+InsetSpecialChar::InsetSpecialChar(Buffer * buf, string const k)
+ : Inset(buf), kind_(k), unknown_(false)
+{
+ if (buf)
+ update();
+}
docstring InsetSpecialChar::toolTip(BufferView const &, int, int) const
@@ -56,13 +58,13 @@ docstring InsetSpecialChar::toolTip(BufferView const &,
int, int) const
if (unknown_)
return bformat(_("Unknown special character (%1$s)!"),
from_utf8(kind_));
- return buffer().params().documentClass().specialChars()[kind_].tooltip;
+ return sc_.tooltip;
}
int InsetSpecialChar::rowFlags() const
{
- if (!unknown_ &&
buffer().params().documentClass().specialChars()[kind_].can_break_after)
+ if (!unknown_ && sc_.can_break_after)
return CanBreakAfter;
return Inline;
@@ -182,7 +184,7 @@ void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension
& dim) const
// the width without code duplication.
drawLogo(pi, dim.wid, 0, kind_);
} else
- s =
buffer().params().documentClass().specialChars()[kind_].lyx_output;
+ s = sc_.lyx_output;
if (dim.wid == 0)
dim.wid = fm.width(s);
@@ -227,9 +229,8 @@ void InsetSpecialChar::draw(PainterInfo & pi, int x, int y)
const
return;
}
- SpecialChar const sc =
buffer().params().documentClass().specialChars()[kind_];
- font.setColor(sc.font.color());
- pi.pain.text(x, y, sc.lyx_output, font);
+ font.setColor(sc_.font.color());
+ pi.pain.text(x, y, sc_.lyx_output, font);
}
@@ -252,14 +253,13 @@ void InsetSpecialChar::latex(otexstream & os,
OutputParams const & rp) const
if (unknown_)
return;
- SpecialChar const sc =
buffer().params().documentClass().specialChars()[kind_];
bool const rtl = rp.local_font && rp.local_font->isRightToLeft();
bool const utf8 = rp.encoding->iconvName() == "UTF-8";
bool force_ltr = false;
string lswitch = "";
string lswitche = "";
if (rtl && !rp.use_polyglossia) {
- force_ltr = sc.force_ltr;
+ force_ltr = sc_.force_ltr;
lswitch = "\\L{";
lswitche = "}";
if (getLocalOrDefaultLang(rp)->lang() == "arabic_arabi"
@@ -267,20 +267,20 @@ void InsetSpecialChar::latex(otexstream & os,
OutputParams const & rp) const
lswitch = "\\textLR{";
}
- if (sc.need_protect && rp.moving_arg)
+ if (sc_.need_protect && rp.moving_arg)
os << "\\protect";
if (force_ltr)
os << lswitch;
- if (rtl && !sc.latex_output_rtl.empty())
- os << sc.latex_output_rtl;
- else if (utf8 && !sc.latex_output_utf8.empty())
- os << sc.latex_output_utf8;
+ if (rtl && !sc_.latex_output_rtl.empty())
+ os << sc_.latex_output_rtl;
+ else if (utf8 && !sc_.latex_output_utf8.empty())
+ os << sc_.latex_output_utf8;
else
- os << sc.latex_output;
+ os << sc_.latex_output;
if (force_ltr)
os << lswitche;
- else if (sc.latex_output_utf8.empty()
- && prefixIs(sc.latex_output, from_ascii("\\"))
+ else if (sc_.latex_output_utf8.empty()
+ && prefixIs(sc_.latex_output, from_ascii("\\"))
&& isAlphaASCII(os.lastChar()))
os << termcmd;
}
@@ -291,7 +291,7 @@ int InsetSpecialChar::plaintext(odocstringstream & os,
OutputParams const &, siz
if (unknown_)
return 0;
- docstring const res =
buffer().params().documentClass().specialChars()[kind_].plaintext_output;
+ docstring const res = sc_.plaintext_output;
os << res;
return res.size();
}
@@ -302,8 +302,7 @@ void InsetSpecialChar::docbook(XMLStream & xs, OutputParams
const &) const
if (unknown_)
return;
- xs << XMLStream::ESCAPE_NONE
- <<
buffer().params().documentClass().specialChars()[kind_].xhtml_output;
+ xs << XMLStream::ESCAPE_NONE << sc_.xhtml_output;
}
@@ -312,15 +311,19 @@ docstring InsetSpecialChar::xhtml(XMLStream & xs,
OutputParams const &) const
if (unknown_)
return docstring();
- xs << XMLStream::ESCAPE_NONE
- <<
buffer().params().documentClass().specialChars()[kind_].xhtml_output;
+ xs << XMLStream::ESCAPE_NONE << sc_.xhtml_output;
return docstring();
}
void InsetSpecialChar::update()
{
- unknown_ = !buffer().params().documentClass().isKnownSpecialChar(kind_);
+ if (!buffer().masterParams().documentClass().isKnownSpecialChar(kind_))
+ unknown_ = true;
+ else {
+ sc_ =
buffer().masterParams().documentClass().specialChars()[kind_];
+ unknown_ = false;
+ }
}
@@ -363,10 +366,9 @@ void InsetSpecialChar::validate(LaTeXFeatures & features)
const
if (unknown_)
return;
- SpecialChar const sc =
buffer().params().documentClass().specialChars()[kind_];
- if (sc.req.empty())
+ if (sc_.req.empty())
return;
- vector<string> const reqs = getVectorFromString(sc.req);
+ vector<string> const reqs = getVectorFromString(sc_.req);
for (auto const & s : reqs)
features.require(s);
}
@@ -377,7 +379,7 @@ bool InsetSpecialChar::isChar() const
if (unknown_)
return false;
- return buffer().params().documentClass().specialChars()[kind_].is_char;
+ return sc_.is_char;
}
@@ -386,7 +388,7 @@ bool InsetSpecialChar::isLetter() const
if (unknown_)
return false;
- return
buffer().params().documentClass().specialChars()[kind_].is_letter;
+ return sc_.is_letter;
}
diff --git a/src/insets/InsetSpecialChar.h b/src/insets/InsetSpecialChar.h
index 657570719a..22aef159ac 100644
--- a/src/insets/InsetSpecialChar.h
+++ b/src/insets/InsetSpecialChar.h
@@ -17,6 +17,8 @@
#include "Inset.h"
+#include "TextClass.h"
+
namespace lyx {
@@ -57,7 +59,7 @@ public:
///
InsetSpecialChar() : Inset(0), kind_("softhyphen") {}
///
- explicit InsetSpecialChar(std::string const k);
+ explicit InsetSpecialChar(Buffer * buf, std::string const k);
///
docstring toolTip(BufferView const & bv, int x, int y) const override;
/// some special chars allow line breaking after them
@@ -109,8 +111,10 @@ public:
private:
Inset * clone() const override { return new InsetSpecialChar(*this); }
- /// And which kind is this?
+ /// Special char type
std::string kind_;
+ ///
+ SpecialChar sc_;
/// Is this known?
bool unknown_;
};
--
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs