From d2db74f9ee1c2fe566c2af7a3d62dccaecec08cb Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Sat, 1 Jun 2024 13:47:39 +0200 Subject: [PATCH] Add support for glue length in parskip (#12867) --- development/FORMAT | 8 ++++++ lib/lyx2lyx/lyx_2_5.py | 42 ++++++++++++++++++++++++----- src/BufferParams.cpp | 9 +++++-- src/frontends/qt/GuiDocument.cpp | 6 +++-- src/tex2lyx/Preamble.cpp | 9 +++++-- src/tex2lyx/tex2lyx.h | 1 + src/tex2lyx/text.cpp | 45 +++++++++++++++++++------------- src/version.h | 4 +-- 8 files changed, 91 insertions(+), 33 deletions(-) diff --git a/development/FORMAT b/development/FORMAT index 2834dd5d75..58bcc65e51 100644 --- a/development/FORMAT +++ b/development/FORMAT @@ -7,6 +7,14 @@ changes happened in particular if possible. A good example would be ----------------------- +2024-06-01 Jürgen Spitzmüller + * Format incremented to 622: Allow glue length (skip) in parskip setting. + +2024-05-13 Jürgen Spitzmüller + * Format incremented to 621: Escape some special chars (#, %, \) in URL + with hyperref. Previous to this change, the user had to escape this + themselves. + 2023-09-29 Jürgen Spitzmüller * Format incremented to 620: Add InsetBox "default" framecolor ("foreground" rather than "black" in GUI). This aligns better with dark mode. diff --git a/lib/lyx2lyx/lyx_2_5.py b/lib/lyx2lyx/lyx_2_5.py index 17deb820c7..f60370a1b6 100644 --- a/lib/lyx2lyx/lyx_2_5.py +++ b/lib/lyx2lyx/lyx_2_5.py @@ -26,10 +26,10 @@ from datetime import (datetime, date, time) # Uncomment only what you need to import, please. -from parser_tools import (find_end_of_inset, find_end_of_layout, find_token, find_re) +from parser_tools import (find_end_of_inset, find_end_of_layout, find_token, find_re, get_value) # count_pars_in_inset, del_complete_lines, del_token, find_end_of, # find_token_backwards, find_token_exact, get_bool_value, -# get_containing_inset, get_containing_layout, get_option_value, get_value, +# get_containing_inset, get_containing_layout, get_option_value, # get_quoted_value, is_in_inset, # del_value, # find_complete_lines, @@ -37,9 +37,10 @@ from parser_tools import (find_end_of_inset, find_end_of_layout, find_token, fin # set_bool_value # find_tokens, check_token -#from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble, insert_to_preamble, lyx2latex, -# revert_language, revert_flex_inset, str2bool) -# revert_font_attrs, latex_length +from lyx2lyx_tools import (add_to_preamble, latex_length) +# put_cmd_in_ert, insert_to_preamble, lyx2latex, +# revert_language, revert_flex_inset, str2bool, +# revert_font_attrs, # get_ert, lyx2verbatim, length_in_bp, convert_info_insets # revert_flex_inset, hex2ratio @@ -167,17 +168,44 @@ def revert_url_escapes2(document): document.body[bs] = "\\backslash\\backslash" i = bs + 1 + +def revert_glue_parskip(document): + """Revert parskip with glue length to user preamble.""" + + i = find_token(document.header, "\\paragraph_separation skip", 0) + if i == -1: + return + + j = find_token(document.header, "\\defskip", 0) + if j == -1: + document.warning("Malformed LyX document! Missing \\defskip.") + return + + val = get_value(document.header, "\\defskip", j) + + if val.find("+") == -1 and val.find("-", 1) == -1: + # not a glue length + return + + add_to_preamble(document, ["\\usepackage[skip={" + latex_length(val)[1] + "}]{parskip}"]) + + document.header[i] = "\\paragraph_separation indent" + document.header[j] = "\\paragraph_indentation default" + + ## # Conversion hub # supported_versions = ["2.5.0", "2.5"] convert = [ - [621, [convert_url_escapes, convert_url_escapes2]] + [621, [convert_url_escapes, convert_url_escapes2]], + [622, []] ] -revert = [[620, [revert_url_escapes2, revert_url_escapes]] +revert = [[621, [revert_glue_parskip]], + [620, [revert_url_escapes2, revert_url_escapes]] ] diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index 1324aaac9e..2e000e29c4 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -2189,8 +2189,13 @@ bool BufferParams::writeLaTeX(otexstream & os, LaTeXFeatures & features, } else { // load parskip package with required options string psopts; - if (!psopt.empty()) - psopts = "skip=" + psopt; + if (!psopt.empty()) { + if (contains(psopt, ' ')) + // glue length has spaces: embrace + psopts = "skip={" + psopt + "}"; + else + psopts = "skip=" + psopt; + } string const xpsopts = getPackageOptions("parskip"); if (!xpsopts.empty()) { if (!psopts.empty()) diff --git a/src/frontends/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp index e020ab92c2..42af55a4df 100644 --- a/src/frontends/qt/GuiDocument.cpp +++ b/src/frontends/qt/GuiDocument.cpp @@ -892,8 +892,10 @@ GuiDocument::GuiDocument(GuiView & lv) textLayoutModule->lspacingLE)); textLayoutModule->indentLE->setValidator(new LengthValidator( textLayoutModule->indentLE, false)); - textLayoutModule->skipLE->setValidator(new LengthValidator( - textLayoutModule->skipLE, false)); + // parskip accepts glue length + LengthValidator * skipLEValidator = new LengthValidator(textLayoutModule->skipLE, false); + skipLEValidator->setBottom(GlueLength()); + textLayoutModule->skipLE->setValidator(skipLEValidator); textLayoutModule->indentCO->addItem(qt_("Default"), toqstr("default")); textLayoutModule->indentCO->addItem(qt_("Custom"), toqstr("custom")); diff --git a/src/tex2lyx/Preamble.cpp b/src/tex2lyx/Preamble.cpp index 33885556b5..a774a230a2 100644 --- a/src/tex2lyx/Preamble.cpp +++ b/src/tex2lyx/Preamble.cpp @@ -1745,8 +1745,13 @@ void Preamble::handle_package(Parser &p, string const & name, h_defskip = "bigskip"; else if (opts == "skip=\\baselineskip") h_defskip = "fullline"; - else - h_defskip = "opts"; + else { + // get value, unbraced + string length = rtrim(ltrim(token(opts, '=', 1), "{"), "}"); + // transform glue length if we have one + is_glue_length(length); + h_defskip = length; + } h_paragraph_separation = "skip"; } } diff --git a/src/tex2lyx/tex2lyx.h b/src/tex2lyx/tex2lyx.h index d1fd33a59c..374cb27c08 100644 --- a/src/tex2lyx/tex2lyx.h +++ b/src/tex2lyx/tex2lyx.h @@ -46,6 +46,7 @@ extern std::string rgbcolor2code(std::string const & name); /// in text.cpp std::string translate_len(std::string const &); +bool is_glue_length(std::string & length); void parse_text(Parser & p, std::ostream & os, unsigned flags, bool outer, Context & context, std::string const & rdelim = "", diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index 7c63b7679f..c525de11d4 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -608,6 +608,32 @@ string translate_len(string const & length) return length; } +bool is_glue_length(string & length) +{ + // check for glue lengths + bool is_gluelength = false; + string gluelength = length; + string::size_type i = length.find(" minus"); + if (i == string::npos) { + i = length.find(" plus"); + if (i != string::npos) + is_gluelength = true; + } else + is_gluelength = true; + // if yes transform "9xx minus 8yy plus 7zz" + // to "9xx-8yy+7zz" + if (is_gluelength) { + i = gluelength.find(" minus"); + if (i != string::npos) + gluelength.replace(i, 7, "-"); + i = gluelength.find(" plus"); + if (i != string::npos) + gluelength.replace(i, 6, "+"); + length = gluelength; + } + return is_gluelength; +} + namespace { @@ -6039,25 +6065,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, } // check for glue lengths - bool is_gluelength = false; string gluelength = length; - string::size_type i = length.find(" minus"); - if (i == string::npos) { - i = length.find(" plus"); - if (i != string::npos) - is_gluelength = true; - } else - is_gluelength = true; - // if yes transform "9xx minus 8yy plus 7zz" - // to "9xx-8yy+7zz" - if (is_gluelength) { - i = gluelength.find(" minus"); - if (i != string::npos) - gluelength.replace(i, 7, "-"); - i = gluelength.find(" plus"); - if (i != string::npos) - gluelength.replace(i, 6, "+"); - } + bool is_gluelength = is_glue_length(gluelength); if (t.cs()[0] == 'h' && (known_unit || known_hspace || is_gluelength)) { // Literal horizontal length or known variable diff --git a/src/version.h b/src/version.h index 1393d28733..ab38adf622 100644 --- a/src/version.h +++ b/src/version.h @@ -32,8 +32,8 @@ extern char const * const lyx_version_info; // Do not remove the comment below, so we get merge conflict in // independent branches. Instead add your own. -#define LYX_FORMAT_LYX 621 // spitz: handle #% in frame urls -#define LYX_FORMAT_TEX2LYX 621 +#define LYX_FORMAT_LYX 622 // spitz: support glue length in parskip +#define LYX_FORMAT_TEX2LYX 622 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX #ifndef _MSC_VER