On Wed, Jul 19, 2023 at 02:50:50PM +0200, Pavel Sanda wrote:
> it seems that paragraph settings are not applied to the last paragraph of 
> selected text.
> 
> To reproduce:
> 1. Have two paragraphs
> 2. Make selection starting *in the middle* of the first paragraph and ending 
> at the end of the second paragraph
>    (Strangely if selection starts in the begining of the first paragraph 
> everything works.)
> 3. Apply paragraph settings, e.g. apply centering to the selection.
> 4. The second paragraph is not centered although fully selected.

As it turns out it's more weird: the last paragraph in the selection gets
processed only in case the relative cursor position (within paragraph) of
selection start is less than position in the selection end.

My analysis is this: the iteration in setParagraphs is not right.

The piece of the code is:
...
  Cursor c(cur.bv());
  c.setCursor(cur.selectionBegin());
  for ( ; c <= cur.selectionEnd() ; ++c.pit()) {
...

Clearly the idea was to iterate over all paragraphs in the range of
[cur.selectionBegin();cur.selectionEnd()]. However ++c.pit() increases only
paragraph number (c.slices_[0].pit_) and let the relative cursor position
within paragraph c.slices_[0].pos_) unchanged. So if in the selectionBegin()
paragraph you have the cursor pos_ beyond the selectionEnd () pos_ the while 
condition
 c <= cur.selectionEnd() 
for the last paragraph fails.

The solution is to set pos_ taken from selectionBegin to 0 so the while
condition is met. Attached is patch which seems to work, but I admit I don't
understand the dociterator structure well enough to be confident in what I am 
doing.
(In particular I do not know what slices_ array exactly mean and whether
c.pos() = 0; will always get me to the begining. The documentation in
CursorSlice.h isn't much helpful...) 

JMarc, would you mind checking the patch?

Pavel
diff --git a/src/Text.cpp b/src/Text.cpp
index dfef748640..bb2d883169 100644
--- a/src/Text.cpp
+++ b/src/Text.cpp
@@ -2908,6 +2908,9 @@ void Text::setParagraphs(Cursor const & cur, docstring 
const & arg, bool merge)
        Layout priorlayout;
        Cursor c(cur.bv());
        c.setCursor(cur.selectionBegin());
+       // the last paragraph of selection might not be set if the position
+       // in the selection of the first paragraph is beyond selectionEnd.pos()
+       c.pos() = 0;
        for ( ; c <= cur.selectionEnd() ; ++c.pit()) {
                Paragraph & par = c.paragraph();
                ParagraphParameters params = par.params();
@@ -2934,6 +2937,7 @@ void Text::setParagraphs(Cursor const & cur, 
ParagraphParameters const & p)
        Layout priorlayout;
        Cursor c(cur.bv());
        c.setCursor(cur.selectionBegin());
+       c.pos() = 0;
        for ( ; c < cur.selectionEnd() ; ++c.pit()) {
                Paragraph & par = c.paragraph();
                // Changes to label width string apply to all paragraphs
-- 
lyx-devel mailing list
lyx-devel@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-devel

Reply via email to