[vim/vim] popup horizontal scroll (Issue #19360)

2 views
Skip to first unread message

ubaldot

unread,
3:21 AM (8 hours ago) 3:21 AM
to vim/vim, Subscribed
ubaldot created an issue (vim/vim#19360)

Sometimes, the text displayed in a popup goes over the popup maxwidth and it is not possible to horizontally scroll. Wrapping text may work in a number of case, but in some other case wrapping text is not a good idea (think for example to large tables).

To better understand the feature request, consider the following example: it is possible to vertically scroll the popup through j and k, but it is not possible to horizontally scroll it.

vim9script

var some_text = ['This is a very long string that clearly does not fit the popup max width',
  'This is another line',
  'This is a third line'
]

def PopupFilter(id: number, key: string): bool
  if key == 'q' || key == "\<esc>"
    popup_close(id, -1)
  # Move down
  elseif key == "j"
    win_execute(id, "normal! j")
  # Move up
  elseif key == "k"
    win_execute(id, "normal! k")
  # Wanna scroll right
  elseif key == "l"
    win_execute(id, "normal! zl")
  # Wanna scroll left
  elseif key == "h"
    win_execute(id, "normal! zh")
  elseif key == "$"
    win_execute(id, "normal! $")
  elseif key == "0"
    win_execute(id, "normal! 0")
  else
    return false
  endif
  return true
enddef


var opts = {
  border: [1, 1, 1, 1],
  borderchars: ['', '', '', '', '', '', '', ''],
  filter: PopupFilter,
  maxheight: 1,
  maxwidth: 30,
  scrollbar: 0,
}

popup_create(some_text, opts)


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/19360@github.com>

Mao-Yining

unread,
4:43 AM (7 hours ago) 4:43 AM
to vim/vim, Subscribed
mao-yining left a comment (vim/vim#19360)

Maybe you can try this:

vim9script

const POPUP_ROWS = 10
const POPUP_COLS = 50

var original_text =<< trim END
It was the best of times, it was the worst of times, it was the age of wisdom,
it was the age offoolishness, it was the epoch of belief, it was the epoch of
incredulity, it was the season of Light, it was the season of Darkness, it was
the spring of hope, it was the winter of despair, we had everything before us,
we had nothing before us, we were all going direct to Heaven, we were all going
direct the other way--in short, the period was so. far like the present period,
that some of its noisiest authorities insisted on its being received, for good
or for evil, in the superlative degree of comparison only.

There were a king with a large jaw and a queen with a plain face, on the throne
of England; there were a king with a large jaw and a queen with a fair face, on
the throne of France. In both countries it was clearer than crystal to the lords
of the State preserves of loaves and fishes, that things in general were settled
for ever.

It was the year of Our Lord one thousand seven hundred and seventy-five.
Spiritual revelations were conceded to England at that favoured period, a sat
this. Mrs. Southcott had recently attained her five-and-twentieth blessed
birthday, of whom a prophetic private in the Life Guards had heralded the
sublime appearance by announcing that arrangements were made for the swallowing
up of London and Westminster. Even the Cock-lane ghost had been laid only a
round dozen of years, after rapping out its messages, as the spirits of this
very year last past (supernaturally deficient in originality) rapped out theirs.
Mere messages in the earthly order of events had lately come to the English
Crown and People, from a congress of British subjects in America: which, strange
to relate, have proved more important to the human race than any communications
yet received through any of the chickens of the Cock-lane brood.
END

var view_pos = {line: 0, col: 0}
var popup_id: number = 0

def GetVisibleText(): list<string>
	var visible_lines: list<string> = []

	if view_pos.line < 0
		view_pos.line = 0
	endif
	if view_pos.line > len(original_text) - POPUP_ROWS
		view_pos.line = max([0, len(original_text) - POPUP_ROWS])
	endif
	if view_pos.col < 0
		view_pos.col = 0
	endif

	for i in range(view_pos.line, min([view_pos.line + POPUP_ROWS - 1, len(original_text) - 1]))
		var line = original_text[i]

		if view_pos.col >= len(line)
			visible_lines->add('')
		else
			var end_col = min([view_pos.col + POPUP_COLS, len(line)])
			var visible_part = line[view_pos.col : end_col - 1]
			while len(visible_part) < POPUP_COLS
				visible_part ..= ' '
			endwhile
			visible_lines->add(visible_part)
		endif
	endfor

	var empty_line = repeat(' ', POPUP_COLS)
	while len(visible_lines) < POPUP_ROWS
		visible_lines->add(empty_line)
	endwhile

	return visible_lines
enddef

def UpdatePopup()
	if popup_id > 0
		var visible_text = GetVisibleText()
		popup_settext(popup_id, visible_text)

		var info = printf('Line:%2d Col:%2d', view_pos.line + 1, view_pos.col + 1)
		popup_setoptions(popup_id, {title: info})
	endif
enddef

def PopupFilter(id: number, key: string
): bool
	var max_line = len(original_text) - 1
	var max_col = 0
	for line in original_text
		max_col = max([max_col, len(line)])
	endfor

	max_col = max([0, max_col - POPUP_COLS])

	
if key == 'q' || key == "\<esc>"
		popup_close(id, -1
)
		popup_id = 0
	elseif key == "j"
		if view_pos.line < max_line - (POPUP_ROWS - 1)
			view_pos.line += 1
			UpdatePopup()
		endif
	elseif key == "k"
		if view_pos.line > 0
			view_pos.line -= 1
			UpdatePopup()
		endif
	elseif key == "l"
		if view_pos.col < max_col
			view_pos.col += 1
			UpdatePopup()
		endif
	elseif key == "h"
		if view_pos.col > 0
			view_pos.col -= 1
			UpdatePopup()
		endif
	elseif key == "0"
		view_pos.col = 0
		UpdatePopup()
	elseif key == "$"
		var current_line_len = len(original_text[view_pos.line])
		view_pos.col = max([0, current_line_len - POPUP_COLS])
		UpdatePopup()
	elseif key == "\<c-d>" || key == "\<PageDown>"
		view_pos.line = min([view_pos.line + POPUP_ROWS, max_line - (POPUP_ROWS - 1)])
		UpdatePopup()
	elseif key == "\<c-u>" || key == "\<PageUp>"
		view_pos.line = max([0, view_pos.line - POPUP_ROWS])
		UpdatePopup()
	
else
		return false
	endif

	return true
enddef

def OpenPopup()
	view_pos = {line: 0, col: 0}

	var initial_text = GetVisibleText()

	
var opts = {
		border: [1, 1, 1, 1],
		borderchars: ['', '', '', '', '', '', '', ''],
		filter
: PopupFilter,
		minheight: POPUP_ROWS,
		maxheight: POPUP_ROWS,
		minwidth: POPUP_COLS,
		maxwidth: POPUP_COLS,
		wrap: 0,
		scrollbar: 0,
		title: printf('Line:%2d Col:%2d', view_pos.line + 1, view_pos.col + 1),
		padding: [0, 1, 0, 1],
	}

	popup_id = popup_create(initial_text, opts)
enddef

OpenPopup()


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/19360/3866770651@github.com>

ubaldot

unread,
6:58 AM (4 hours ago) 6:58 AM
to vim/vim, Subscribed
ubaldot left a comment (vim/vim#19360)

Thanks for proposing a workaround, but TBH I think that having a native horizontal scrolling would be way better.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/19360/3867060165@github.com>

Reply all
Reply to author
Forward
0 new messages