[LyX/master] Add possibility to manually specify longest label in nomenclature

Juergen Spitzmueller spitz at lyx.org
Sun Jul 21 16:06:09 UTC 2024


commit ae1e1cb06936ee744dfcf39e5cbb776f9c86cc4c
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sun Jul 21 17:59:34 2024 +0200

    Add possibility to manually specify longest label in nomenclature
    
    File format change
---
 development/FORMAT                   | 14 +++++++-
 lib/doc/UserGuide.lyx                | 63 +++++++++++++++++++++++++++++++++++-
 lib/doc/de/UserGuide.lyx             | 38 +++++++++++++++++++++-
 lib/lyx2lyx/lyx_2_5.py               | 43 ++++++++++++++++++++++--
 src/frontends/qt/GuiPrintNomencl.cpp | 36 ++++++++++++++-------
 src/insets/InsetNomencl.cpp          |  8 +++--
 src/tex2lyx/text.cpp                 | 32 ++++++++++++++----
 src/version.h                        |  4 +--
 8 files changed, 210 insertions(+), 28 deletions(-)

diff --git a/development/FORMAT b/development/FORMAT
index 34149fee02..a8617da9ca 100644
--- a/development/FORMAT
+++ b/development/FORMAT
@@ -7,9 +7,21 @@ changes happened in particular if possible. A good example would be
 
 -----------------------
 
+2024-07-21 Jürgen Spitzmüller <spitz at lyx.org> 
+	* Format incremented to 626: Support for manuel longest label in
+	  \printnomencl (via new param option set_width "textwidth").
+	  In LaTeX, this will be output as
+	  \settowidth{\nomlabelwidth}{<text>}
+	  before \printnomencl, which we will revert to ERT.
+
+2024-07-11 Jürgen Spitzmüller <spitz at lyx.org> 
+	* Format incremented to 625: Support for \nptextcite (biblatex-apa)
+	  and \autocite*/\Autocite* (biblatex-mla).
+	  We will revert new commands to ERT where necessary.
+
 2024-07-09 Jürgen Spitzmüller <spitz at lyx.org> 
 	* Format incremented to 624: Add biblatex-chicago cite engine.
-	  We will revert new commands to ERT where necessary
+	  We will revert new commands to ERT where necessary.
 
 2024-06-11 Udi Fogiel <ufogiel at lyx.org>
 	* Format incremented to 623: Remove hebrew letter document class.
diff --git a/lib/doc/UserGuide.lyx b/lib/doc/UserGuide.lyx
index 1bddb09500..1284d6bd0e 100644
--- a/lib/doc/UserGuide.lyx
+++ b/lib/doc/UserGuide.lyx
@@ -34065,11 +34065,15 @@ status open
 
 \change_inserted -712698321 1720530576
 biblatex-chicago
+\change_unchanged
+
 \end_layout
 
 \end_inset
 
 
+\change_unchanged
+
 \end_layout
 
 \end_inset
@@ -34088,6 +34092,8 @@ status collapsed
 
 \change_inserted -712698321 1720530576
 authordate
+\change_unchanged
+
 \end_layout
 
 \end_inset
@@ -34100,6 +34106,8 @@ status collapsed
 
 \change_inserted -712698321 1720530576
 notes-bibliography
+\change_unchanged
+
 \end_layout
 
 \end_inset
@@ -37390,7 +37398,60 @@ label
 \begin_inset space ~
 \end_inset
 
-width the width of the widest symbol of all nomenclature entries is used
+width
+\change_inserted -712698321 1721576787
+
+\begin_inset space ~
+\end_inset
+
+(guessed)
+\change_unchanged
+ 
+\change_inserted -712698321 1721576880
+\SpecialChar LyX
+ tries to guess the 
+\change_deleted -712698321 1721576836
+the width of the 
+\change_unchanged
+widest symbol of all nomenclature entries 
+\change_deleted -712698321 1721576839
+is
+\change_inserted -712698321 1721576839
+and
+\change_unchanged
+ use
+\change_inserted -712698321 1721576845
+ this.
+\change_deleted -712698321 1721576844
+d
+\change_inserted -712698321 1721576994
+ Note that this guessing might go wrong,
+ as \SpecialChar LyX
+ does not know how how the formatted output in your PDF will look like exactly.
+\change_unchanged
+
+\end_layout
+
+\begin_layout Description
+
+\change_inserted -712698321 1721577537
+Longest
+\begin_inset space ~
+\end_inset
+
+label
+\begin_inset space ~
+\end_inset
+
+width
+\begin_inset space ~
+\end_inset
+
+(set) here you can enter the widest symbol yourself to use its width.
+ While this is more accurate than the second method,
+ the disadvantage is that you need to keep this up-to date if new (longer) entries are added to the nomenclature.
+\change_unchanged
+
 \end_layout
 
 \begin_layout Description
diff --git a/lib/doc/de/UserGuide.lyx b/lib/doc/de/UserGuide.lyx
index da99830808..bcaa112677 100644
--- a/lib/doc/de/UserGuide.lyx
+++ b/lib/doc/de/UserGuide.lyx
@@ -36707,7 +36707,43 @@ längsten
 \begin_inset space ~
 \end_inset
 
-Marke die Breite des breitesten Symbols aller Nomenklatureinträge wird verwendet
+Marke
+\begin_inset space ~
+\end_inset
+
+(geschätzt) \SpecialChar LyX
+ versucht zu schätzen,
+ welches das breiteste Symbol aller Nomenklatureinträge ist und verwendet dessen Breite.
+ Beachten Sie,
+ dass diese Schätzung falsch sein kann,
+ da \SpecialChar LyX
+ nicht genau weiß,
+ wie Ihr Text in der Ausgabe formatiert sein wird.
+\end_layout
+
+\begin_layout Description
+Breite
+\begin_inset space ~
+\end_inset
+
+der
+\begin_inset space ~
+\end_inset
+
+längsten
+\begin_inset space ~
+\end_inset
+
+Marke
+\begin_inset space ~
+\end_inset
+
+(gesetzt) Hier können Sie selbst das breiteste Symbol angeben,
+ dessen Breite dann verwendet wird.
+ Das ist zuverlässiger als die zweite Methode.
+ Der Nachteil allerdings ist,
+ dass Sie dies selbst aktualisieren müssen,
+ wenn neue (längere) Einträge hinzukommen.
 \end_layout
 
 \begin_layout Description
diff --git a/lib/lyx2lyx/lyx_2_5.py b/lib/lyx2lyx/lyx_2_5.py
index e76bed5d37..2afba6e813 100644
--- a/lib/lyx2lyx/lyx_2_5.py
+++ b/lib/lyx2lyx/lyx_2_5.py
@@ -31,13 +31,14 @@ from lyx2lyx_tools import (
 )
 
 # Uncomment only what you need to import, please (parser_tools):
-#    check_token, count_pars_in_inset, del_complete_lines, del_token,
+#    check_token, count_pars_in_inset, del_complete_lines, 
 #    del_value, find_complete_lines, find_end_of, find_end_of_layout,
 #    find_re, find_substring, find_token_backwards, find_token_exact,
 #    find_tokens, get_bool_value, get_containing_inset,
 #    get_containing_layout, get_option_value,
 #    is_in_inset, set_bool_value
 from parser_tools import (
+    del_token,
     find_end_of_inset,
     find_re,
     find_token,
@@ -442,6 +443,42 @@ def revert_nptextcite(document):
             document.body[i : j + 1] = put_cmd_in_ert([res])
         i = j + 1
 
+
+def revert_nomencl_textwidth(document):
+    """Revert nomencl textwidth parameter to ERT."""
+
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
+        if i == -1:
+            return
+
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning(
+                "Malformed LyX document: Can't find end of command inset at line %d" % i
+            )
+            i += 1
+            continue
+
+        sw = get_quoted_value(document.body, "set_width", i, j)
+        if sw != "textwidth":
+            i += 1
+            continue
+
+        # change set_width to "none"
+        k = find_token(document.body, "set_width", i, j)
+        if k != -1:
+            document.body[k] = "set_width \"none\""
+        tw = get_quoted_value(document.body, "width", i, j)
+        # delete width
+        del_token(document.body, "width", i, j)
+        # Insert ERT
+        res = "\\settowidth{\\nomlabelwidth}{" + tw + "}"
+        document.body[i : i] = put_cmd_in_ert([res])
+        i = j
+
+
 ##
 # Conversion hub
 #
@@ -452,11 +489,13 @@ convert = [
     [622, []],
     [623, [convert_he_letter]],
     [624, [convert_biblatex_chicago]],
-    [625, []]
+    [625, []],
+    [626, []]
 ]
 
 
 revert = [
+    [625, [revert_nomencl_textwidth]],
     [624, [revert_nptextcite]],
     [623, [revert_biblatex_chicago]],
     [622, []],
diff --git a/src/frontends/qt/GuiPrintNomencl.cpp b/src/frontends/qt/GuiPrintNomencl.cpp
index a70a9e091c..6533f89a4d 100644
--- a/src/frontends/qt/GuiPrintNomencl.cpp
+++ b/src/frontends/qt/GuiPrintNomencl.cpp
@@ -46,8 +46,10 @@ GuiPrintNomencl::GuiPrintNomencl(QWidget * parent) : InsetParamsWidget(parent)
 
 	setWidthCO->addItem(qt_("Default"),
 		QVariant(toqstr("none")));
-	setWidthCO->addItem(qt_("Longest label width"),
+	setWidthCO->addItem(qt_("Longest label width (guessed)"),
 		QVariant(toqstr("auto")));
+	setWidthCO->addItem(qt_("Longest label width (set)"),
+		QVariant(toqstr("textwidth")));
 	setWidthCO->addItem(qt_("Custom"),
 		QVariant(toqstr("custom")));
 }
@@ -61,11 +63,15 @@ void GuiPrintNomencl::on_setWidthCO_activated(int /*i*/)
 
 void GuiPrintNomencl::paramsToDialog(InsetCommandParams const & params)
 {
-	setWidthCO->setCurrentIndex(
-		setWidthCO->findData(toqstr(params["set_width"])));
+	QString const set_width = toqstr(params["set_width"]);
 
-	lengthToWidgets(valueLE, unitLC,
-			params["width"], Length::defaultUnit());
+	setWidthCO->setCurrentIndex(setWidthCO->findData(set_width));
+
+	if (set_width == "textwidth")
+		valueLE->setText(toqstr(params["width"]));
+	else
+		lengthToWidgets(valueLE, unitLC,
+				params["width"], Length::defaultUnit());
 }
 
 
@@ -85,6 +91,8 @@ docstring GuiPrintNomencl::dialogToParams() const
 	docstring width;
 	if (set_width == from_ascii("custom"))
 		width = from_utf8(widgetsToLength(valueLE, unitLC));
+	else if (set_width == from_ascii("textwidth"))
+		width = qstring_to_ucs4(valueLE->text());
 	params["width"] = width;
 	return from_ascii(InsetNomencl::params2string(params));
 }
@@ -92,23 +100,29 @@ docstring GuiPrintNomencl::dialogToParams() const
 
 bool GuiPrintNomencl::checkWidgets(bool readonly) const
 {
+	QString const set_width =
+		setWidthCO->itemData(setWidthCO->currentIndex()).toString();
+	bool const custom = (set_width == "custom");
+	bool const textwidth = (set_width == "textwidth");
 	valueLE->setReadOnly(readonly);
 	if (readonly) {
 		setWidthCO->setEnabled(false);
 		unitLC->setEnabled(false);
 		valueLA->setEnabled(false);
 	} else {
-		bool const custom =
-			(setWidthCO->itemData(setWidthCO->currentIndex()).toString() == "custom");
-		valueLE->setEnabled(custom);
+		valueLE->setEnabled(custom || textwidth);
 		unitLC->setEnabled(custom);
-		valueLA->setEnabled(custom);
+		valueLA->setEnabled(custom || textwidth);
 	}
+	if (textwidth)
+		// opt-out validator
+		valueLE->setValidator(nullptr);
+	else
+		valueLE->setValidator(unsignedLengthValidator(valueLE));
 
 	if (!InsetParamsWidget::checkWidgets())
 		return false;
-	return setWidthCO->itemData(
-			setWidthCO->currentIndex()).toString() != "custom"
+	return (!custom && !textwidth)
 		|| !valueLE->text().isEmpty();
 }
 
diff --git a/src/insets/InsetNomencl.cpp b/src/insets/InsetNomencl.cpp
index da628442cd..9762f8bae9 100644
--- a/src/insets/InsetNomencl.cpp
+++ b/src/insets/InsetNomencl.cpp
@@ -153,7 +153,7 @@ ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
 	static ParamInfo param_info_;
 	if (param_info_.empty()) {
 		// how is the width set?
-		// values: none|auto|custom
+		// values: none|auto|custom|textwidth
 		param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
 		// custom width
 		param_info_.add("width", ParamInfo::LYX_INTERNAL);
@@ -424,8 +424,10 @@ docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
 {
 	OutputParams runparams = runparams_in;
-	if (getParam("set_width") == "auto") {
-		docstring widest = nomenclWidest(buffer(), runparams);
+	bool const autowidth = getParam("set_width") == "auto";
+	if (autowidth || getParam("set_width") == "textwidth") {
+		docstring widest = autowidth ? nomenclWidest(buffer(), runparams)
+					     : getParam("width");
 		// Set the label width via nomencl's command \nomlabelwidth.
 		// This must be output before the command \printnomenclature
 		if (!widest.empty()) {
diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp
index 44de49635f..cd16531d18 100644
--- a/src/tex2lyx/text.cpp
+++ b/src/tex2lyx/text.cpp
@@ -144,6 +144,7 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
 string fboxrule = "";
 string fboxsep = "";
 string shadow_size = "";
+string printnomencl_textwidth = "";
 
 char const * const known_babel_shorthands[] = { "\"", "|", "-", "~", "=", "/",
  "~", "*", ":", "_", "x", "'", "`", "<", ">", 0 };
@@ -5083,6 +5084,24 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 			continue;
 		}
 
+		if (t.cs() == "settowidth") {
+			context.check_layout(os);
+			string arg = p.getArg('{', '}');
+			// we are only interested in this:
+			if (arg == "\\nomlabelwidth") {
+				printnomencl_textwidth = p.getArg('{', '}');
+				eat_whitespace(p, os, context, false);
+				continue;
+			}
+			// for any other length, do ERT
+			string arg2 = p.getArg('{', '}');
+			string const ert = t.asInput()
+					+ '{' + arg + '}'
+					+ '{' + arg2 + '}';
+			output_ert_inset(os, ert, context);
+			continue;
+		}
+
 		if (t.cs() == "printnomenclature") {
 			string width = "";
 			string width_type = "";
@@ -5094,16 +5113,15 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 				width = translate_len(width);
 				width_type = "custom";
 			}
-			// case of no custom width
-			// the case of no custom width but the width set
-			// via \settowidth{\nomlabelwidth}{***} cannot be supported
-			// because the user could have set anything, not only the width
-			// of the longest label (which would be width_type = "auto")
 			string label = convert_literate_command_inset_arg(p.getArg('{', '}'));
-			if (label.empty() && width_type.empty())
+			if (!printnomencl_textwidth.empty()) {
+				width_type = "textwidth";
+				width = printnomencl_textwidth;
+			}
+			else if (label.empty() && width_type.empty())
 				width_type = "none";
 			os << "set_width \"" << width_type << "\"\n";
-			if (width_type == "custom")
+			if (width_type == "custom" || width_type == "textwidth")
 				os << "width \"" << width << '\"';
 			end_inset(os);
 			skip_spaces_braces(p);
diff --git a/src/version.h b/src/version.h
index 5fea4b1464..d1523c6054 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 625 // spitz: \nptextcite (APA)
-#define LYX_FORMAT_TEX2LYX 625
+#define LYX_FORMAT_LYX 626 // spitz: printnomencl setwidth
+#define LYX_FORMAT_TEX2LYX 626
 
 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
 #ifndef _MSC_VER


More information about the lyx-cvs mailing list