Skip to content

Commit e01c661

Browse files
committed
fix elfmz#2588: more accurate adjustment of LeftPos in Viewer
1 parent b485805 commit e01c661

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

far2l/src/viewer.cpp

+13-8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6868
#include "wakeful.hpp"
6969
#include "WideMB.h"
7070
#include "UtfConvert.hpp"
71+
#include <algorithm>
7172

7273
#define MAX_VIEWLINE 0x2000
7374

@@ -577,16 +578,20 @@ void Viewer::ShowPage(int nMode)
577578
auto visualSelLength = printer->Length(StringI.Chars(StringI.nSelStart),
578579
StringI.nSelEnd - StringI.nSelStart);
579580

580-
if (!VM.Wrap && AdjustSelPosition
581-
&& (visualSelStart < LeftPos
582-
|| (visualSelStart > LeftPos
583-
&& visualSelStart + visualSelLength > LeftPos + XX2 - X1))) {
584-
LeftPos = visualSelStart > 1 ? visualSelStart - 1 : 0;
585-
AdjustSelPosition = FALSE;
586-
Show();
587-
return;
581+
if (!VM.Wrap && AdjustSelPosition) {
582+
const int correctedWidth = Width - (ViOpt.ShowArrows ? 2 : 0);
583+
if (visualSelStart < LeftPos || visualSelStart + visualSelLength > LeftPos + correctedWidth) {
584+
int newLeftPos = (visualSelLength <= correctedWidth)
585+
? (visualSelStart + visualSelLength / 2 - correctedWidth / 2)
586+
: (visualSelStart - (ViOpt.ShowArrows ? 1 : 0));
587+
LeftPos = std::clamp(newLeftPos, 0, std::max(StrLen - correctedWidth, 0));
588+
AdjustSelPosition = FALSE;
589+
Show();
590+
return;
591+
}
588592
}
589593

594+
590595
int SelX1 = X1, SelSkip = 0;
591596
if (visualSelStart > LeftPos)
592597
SelX1+= visualSelStart - LeftPos;

0 commit comments

Comments
 (0)