]> git.lyx.org Git - features.git/commitdiff
Truncate long citation label in the middle rather than the end.
authorJuergen Spitzmueller <spitz@lyx.org>
Tue, 9 Jul 2019 12:48:11 +0000 (14:48 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 18 Jun 2020 13:48:35 +0000 (15:48 +0200)
Fixes: #10769
src/BiblioInfo.cpp
src/insets/InsetCitation.cpp
src/support/lstrings.cpp
src/support/lstrings.h

index 80cafeb3595bf1a60b005084f43bf5c2bf295ac2..b0f64bd0e3e7af0db0ddefc95be89dcf5fe6a378 100644 (file)
@@ -1326,9 +1326,15 @@ docstring const BiblioInfo::getLabel(vector<docstring> keys,
        LASSERT(max_size >= 16, max_size = 16);
 
        // we can't display more than 10 of these, anyway
+       // but since we truncate in the middle,
+       // we need to split into two halfs.
        bool const too_many_keys = keys.size() > 10;
-       if (too_many_keys)
-               keys.resize(10);
+       vector<docstring> lkeys;
+       if (too_many_keys) {
+               lkeys.insert(lkeys.end(), keys.end() - 5, keys.end());
+               keys.resize(5);
+               keys.insert(keys.end(), lkeys.begin(), lkeys.end());
+       }
 
        CiteEngineType const engine_type = buf.params().citeEngineType();
        DocumentClass const & dc = buf.params().documentClass();
@@ -1353,9 +1359,8 @@ docstring const BiblioInfo::getLabel(vector<docstring> keys,
                ret = data.getLabel(xrefptrs, buf, ret, ci, key + 1 != ken, i == 1);
        }
 
-       if (too_many_keys)
-               ret.push_back(0x2026);//HORIZONTAL ELLIPSIS
-       support::truncateWithEllipsis(ret, max_size);
+       support::truncateWithEllipsis(ret, max_size, true);
+
        return ret;
 }
 
index ba45f691a37cbe0cef69fdab1313ca90e6e2c02a..6d5c381df89c0c22a4345b6272e2082387d81773 100644 (file)
@@ -458,8 +458,8 @@ void InsetCitation::updateBuffer(ParIterator const &, UpdateType)
        cache.recalculate = false;
        cache.generated_label = glabel;
        unsigned int const maxLabelChars = 45;
-       cache.screen_label = glabel.substr(0, maxLabelChars + 1);
-       support::truncateWithEllipsis(cache.screen_label, maxLabelChars);
+       cache.screen_label = glabel;
+       support::truncateWithEllipsis(cache.screen_label, maxLabelChars, true);
 }
 
 
index 68eabd9d2649c310ecf6324831e19dd324ecd2b0..e2f3d9c0848322c92fb985bd08b7c18713600080 100644 (file)
@@ -1233,13 +1233,21 @@ docstring const protectArgument(docstring & arg, char const l,
 }
 
 
-bool truncateWithEllipsis(docstring & str, size_t const len)
+bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid)
 {
        if (str.size() <= len)
                return false;
-       str.resize(len);
-       if (len > 0)
-               str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
+       if (mid && len > 0) {
+               size_t const hlen = len / 2;
+               docstring suffix = str.substr(str.size() - hlen);
+               str.resize(hlen);
+               str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS
+               str += suffix;
+       } else {
+               str.resize(len);
+               if (len > 0)
+                       str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS
+       }
        return true;
 }
 
index dde06e545419cd49d0ae0b7910d3deaa1e84264d..ff0ce1d20738cd92a40584668faa32341bf5b60c 100644 (file)
@@ -276,6 +276,8 @@ docstring const protectArgument(docstring & arg, char const l = '[',
 /// Truncates a string with an ellipsis at the end.  Leaves str unchanged and
 /// returns false if it is shorter than len. Otherwise resizes str to len, with
 /// U+2026 HORIZONTAL ELLIPSIS at the end, and returns true.
+/// If mid is true, the ellipsis will be put to the mid of the string, and the first
+/// and last half is appended/prepended. 
 ///
 /// Warning (Unicode): The cases where we want to truncate the text and it does
 /// not end up converted into a QString for UI display must be really
@@ -294,7 +296,8 @@ docstring const protectArgument(docstring & arg, char const l = '[',
 ///
 /// FIXME: apply those principles in the current code.
 ///
-bool truncateWithEllipsis(docstring & str, size_t const len);
+bool truncateWithEllipsis(docstring & str, size_t const len,
+                         bool const mid = false);
 
 /// Word-wraps the provided docstring, returning a line-broken string
 /// of width no wider than width, with the string broken at spaces.