commit a9f9ba14967efc4b678e993efcd21be31f2c2581 Author: Juergen Spitzmueller <sp...@lyx.org> Date: Mon Apr 14 12:19:18 2025 +0200
Store used cross-reference type in Session and use it as default (#10975) --- src/Session.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ src/Session.h | 31 +++++++++++++++++++++++++++++ src/frontends/qt/GuiRef.cpp | 8 +++++++- src/insets/InsetLabel.cpp | 12 +++++++++-- src/insets/InsetRef.cpp | 11 +++++++++-- src/mathed/InsetMathHull.cpp | 7 ++++++- 6 files changed, 110 insertions(+), 6 deletions(-) diff --git a/src/Session.cpp b/src/Session.cpp index 0fc063bcd5..54a6949c15 100644 --- a/src/Session.cpp +++ b/src/Session.cpp @@ -16,6 +16,7 @@ #include "support/debug.h" #include "support/filetools.h" #include "support/Package.h" +#include "support/lstrings.h" #include <fstream> #include <sstream> @@ -34,6 +35,7 @@ string const sec_bookmarks = "[bookmarks]"; string const sec_lastcommands = "[last commands]"; string const sec_authfiles = "[auth files]"; string const sec_shellescape = "[shell escape files]"; +string const sec_ui = "[user interface]"; // currently unused: //string const sec_session = "[session info]"; //string const sec_toolbars = "[toolbars]"; @@ -461,6 +463,8 @@ void Session::readFile() authFiles().read(is); else if (tmp == sec_shellescape) shellescapeFiles().read(is); + else if (tmp == sec_ui) + uiSettings().read(is); else LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp); @@ -482,6 +486,7 @@ void Session::writeFile() const bookmarks().write(os); authFiles().write(os); shellescapeFiles().write(os); + uiSettings().write(os); } else LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: " << session_file); @@ -606,4 +611,46 @@ void ShellEscapeSection::remove(string const & name) } +void UISection::read(istream & is) +{ + string s; + do { + char c = is.peek(); + if (c == '[') + break; + getline(is, s); + if (s.empty() || s[0] == '#' || s[0] == ' ' || !support::contains(s, " ")) + continue; + + // read UI settings + string key; + string const value = support::split(s, key, ' '); + if (!key.empty()) + ui_settings_[key] = value; + else + LYXERR(Debug::INIT, "LyX: Warning: Ignore UI setting: " << s); + } while (is.good()); +} + + +void UISection::write(ostream & os) const +{ + os << '\n' << sec_ui << '\n'; + for (auto const & s : ui_settings_) + os << s.first << " " << s.second << "\n"; +} + + +string UISection::value(string const & key) +{ + return ui_settings_[key]; +} + + +void UISection::insert(string const & key, string const & value) +{ + ui_settings_[key] = value; +} + + } // namespace lyx diff --git a/src/Session.h b/src/Session.h index a433cbaf59..dde76cad79 100644 --- a/src/Session.h +++ b/src/Session.h @@ -17,6 +17,7 @@ #include "support/types.h" #include <list> +#include <map> #include <string> #include <vector> @@ -387,6 +388,30 @@ private: }; +class UISection : SessionSection +{ +public: + /// + explicit UISection() {} + + /// + void read(std::istream & is) override; + + /// + void write(std::ostream & os) const override; + + /// + std::string value(std::string const & key); + + /// + void insert(std::string const & key, std::string const & value); + +private: + /// map with UI session settings + std::map<std::string, std::string> ui_settings_; +}; + + class Session { public: @@ -423,6 +448,10 @@ public: ShellEscapeSection & shellescapeFiles() { return shellescape_files; } /// ShellEscapeSection const & shellescapeFiles() const { return shellescape_files; } + /// + UISection & uiSettings() { return ui_settings; } + /// + UISection const & uiSettings() const { return ui_settings; } private: friend class LyX; @@ -454,6 +483,8 @@ private: AuthFilesSection auth_files; /// ShellEscapeSection shellescape_files; + /// + UISection ui_settings; }; /// This is a singleton class. Get the instance. diff --git a/src/frontends/qt/GuiRef.cpp b/src/frontends/qt/GuiRef.cpp index b75c11c279..f2cf95b724 100644 --- a/src/frontends/qt/GuiRef.cpp +++ b/src/frontends/qt/GuiRef.cpp @@ -28,6 +28,8 @@ #include "GuiView.h" #include "PDFOptions.h" +#include "Session.h" + #include "TocModel.h" #include "TocBackend.h" #include "qt_helpers.h" @@ -597,7 +599,10 @@ void GuiRef::updateContents() // restore type settings for new insets bool const new_inset = params_["reference"].empty(); if (new_inset) { - int index = typeCO->findData(orig_type); + QString const use_type = orig_type.isEmpty() + ? toqstr(theSession().uiSettings().value("default_crossreftype")) + : orig_type; + int index = typeCO->findData(use_type); if (index == -1) index = 0; typeCO->setCurrentIndex(index); @@ -1096,6 +1101,7 @@ void GuiRef::dispatchParams() std::string const lfun = InsetCommand::params2string(params_); dispatch(FuncRequest(getLfun(), lfun)); connectToNewInset(); + theSession().uiSettings().insert("default_crossreftype", params_.getCmdName()); } diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index 9bc371d341..d58eb597ba 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -29,6 +29,8 @@ #include "TextClass.h" #include "TocBackend.h" +#include "Session.h" + #include "mathed/InsetMathRef.h" #include "frontends/alert.h" @@ -300,7 +302,10 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd) } case LFUN_LABEL_COPY_AS_REFERENCE: { - InsetCommandParams p(REF_CODE, "ref"); + string type = theSession().uiSettings().value("default_crossreftype"); + if (type.empty()) + type = "ref"; + InsetCommandParams p(REF_CODE, type); p["reference"] = getParam("name"); p["filenames"] = getParam("name") + "@" + from_utf8(buffer().absFileName()); cap::clearSelection(); @@ -309,7 +314,10 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd) } case LFUN_LABEL_INSERT_AS_REFERENCE: { - InsetCommandParams p(REF_CODE, "ref"); + string type = theSession().uiSettings().value("default_crossreftype"); + if (type.empty()) + type = "ref"; + InsetCommandParams p(REF_CODE, type); p["reference"] = getParam("name"); string const data = InsetCommand::params2string(p); lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data)); diff --git a/src/insets/InsetRef.cpp b/src/insets/InsetRef.cpp index afa71fb0f5..7c9fc9ff59 100644 --- a/src/insets/InsetRef.cpp +++ b/src/insets/InsetRef.cpp @@ -29,6 +29,8 @@ #include "texstream.h" #include "TocBackend.h" +#include "Session.h" + #include "frontends/alert.h" #include "support/debug.h" @@ -146,8 +148,13 @@ void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd) } // otherwise not for us - if (pstring.empty()) - return InsetCommand::doDispatch(cur, cmd); + if (pstring.empty()) { + InsetCommand::doDispatch(cur, cmd); + if (cmd.action() == LFUN_INSET_MODIFY && cmd.getArg(0) == "changetype") + // store last used type in session + theSession().uiSettings().insert("default_crossreftype", getCmdName()); + return; + } bool const isSet = (getParam(pstring) == "true"); setParam(pstring, from_ascii(isSet ? "false" : "true")); diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index a0f22f5674..83508402ad 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -46,6 +46,8 @@ #include "TextPainter.h" #include "TocBackend.h" +#include "Session.h" + #include "insets/InsetLabel.h" #include "insets/InsetRef.h" #include "insets/RenderPreview.h" @@ -1985,7 +1987,10 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd) if (row == nrows()) break; - InsetCommandParams p(REF_CODE, "ref"); + string type = theSession().uiSettings().value("default_crossreftype"); + if (type.empty()) + type = "eqref"; + InsetCommandParams p(REF_CODE, type); p["reference"] = label(row); p["filenames"] = label(row) + "@" + from_utf8(buffer().absFileName()); cap::clearSelection(); -- lyx-cvs mailing list lyx-cvs@lists.lyx.org https://lists.lyx.org/mailman/listinfo/lyx-cvs