Пример #1
0
/*
 * Show the popup menu with items "array[size]".
 * "array" must remain valid until pum_undisplay() is called!
 * When possible the leftmost character is aligned with cursor column.
 * The menu appears above the screen line "row" or at "row" + "height" - 1.
 */
    void
pum_display(
    pumitem_T	*array,
    int		size,
    int		selected)	/* index of initially selected item, none if
				   out of range */
{
    int		def_width;
    int		max_width;
    int		row;
    int		context_lines;
    int		cursor_col;
    int		above_row;
    int		below_row;
    int		redo_count = 0;
#if defined(FEAT_QUICKFIX)
    win_T	*pvwin;
#endif

    do
    {
	def_width = p_pw;
	above_row = 0;
	below_row = cmdline_row;

	/* Pretend the pum is already there to avoid that must_redraw is set
	 * when 'cuc' is on. */
	pum_array = (pumitem_T *)1;
	validate_cursor_col();
	pum_array = NULL;

	row = curwin->w_wrow + W_WINROW(curwin);

#if defined(FEAT_QUICKFIX)
	FOR_ALL_WINDOWS(pvwin)
	    if (pvwin->w_p_pvw)
		break;
	if (pvwin != NULL)
	{
	    if (W_WINROW(pvwin) < W_WINROW(curwin))
		above_row = W_WINROW(pvwin) + pvwin->w_height;
	    else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
		below_row = W_WINROW(pvwin);
	}
#endif

	/*
	 * Figure out the size and position of the pum.
	 */
	if (size < PUM_DEF_HEIGHT)
	    pum_height = size;
	else
	    pum_height = PUM_DEF_HEIGHT;
	if (p_ph > 0 && pum_height > p_ph)
	    pum_height = p_ph;

	/* Put the pum below "row" if possible.  If there are few lines decide
	 * on where there is more room. */
	if (row + 2 >= below_row - pum_height
			      && row - above_row > (below_row - above_row) / 2)
	{
	    /* pum above "row" */

	    /* Leave two lines of context if possible */
	    if (curwin->w_wrow - curwin->w_cline_row >= 2)
		context_lines = 2;
	    else
		context_lines = curwin->w_wrow - curwin->w_cline_row;

	    if (row >= size + context_lines)
	    {
		pum_row = row - size - context_lines;
		pum_height = size;
	    }
	    else
	    {
		pum_row = 0;
		pum_height = row - context_lines;
	    }
	    if (p_ph > 0 && pum_height > p_ph)
	    {
		pum_row += pum_height - p_ph;
		pum_height = p_ph;
	    }
	}
	else
	{
	    /* pum below "row" */

	    /* Leave two lines of context if possible */
	    if (curwin->w_cline_row
				+ curwin->w_cline_height - curwin->w_wrow >= 3)
		context_lines = 3;
	    else
		context_lines = curwin->w_cline_row
				    + curwin->w_cline_height - curwin->w_wrow;

	    pum_row = row + context_lines;
	    if (size > below_row - pum_row)
		pum_height = below_row - pum_row;
	    else
		pum_height = size;
	    if (p_ph > 0 && pum_height > p_ph)
		pum_height = p_ph;
	}

	/* don't display when we only have room for one line */
	if (pum_height < 1 || (pum_height == 1 && size > 1))
	    return;

#if defined(FEAT_QUICKFIX)
	/* If there is a preview window at the above avoid drawing over it. */
	if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
	{
	    pum_row += above_row;
	    pum_height -= above_row;
	}
#endif

	pum_array = array;
	pum_size = size;
	pum_compute_size();
	max_width = pum_base_width;

	/* Calculate column */
#ifdef FEAT_RIGHTLEFT
	if (curwin->w_p_rl)
	    cursor_col = curwin->w_wincol + curwin->w_width
							  - curwin->w_wcol - 1;
	else
#endif
	    cursor_col = curwin->w_wincol + curwin->w_wcol;

	/* if there are more items than room we need a scrollbar */
	if (pum_height < size)
	{
	    pum_scrollbar = 1;
	    ++max_width;
	}
	else
	    pum_scrollbar = 0;

	if (def_width < max_width)
	    def_width = max_width;

	if (((cursor_col < Columns - p_pw
					   || cursor_col < Columns - max_width)
#ifdef FEAT_RIGHTLEFT
		    && !curwin->w_p_rl)
	       || (curwin->w_p_rl
			       && (cursor_col > p_pw || cursor_col > max_width)
#endif
	   ))
	{
	    /* align pum with "cursor_col" */
	    pum_col = cursor_col;

	    /* start with the maximum space available */
#ifdef FEAT_RIGHTLEFT
	    if (curwin->w_p_rl)
		pum_width = pum_col - pum_scrollbar + 1;
	    else
#endif
		pum_width = Columns - pum_col - pum_scrollbar;

	    if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
						&& pum_width > p_pw)
	    {
		/* the width is more than needed for the items, make it
		 * narrower */
		pum_width = max_width + pum_kind_width + pum_extra_width + 1;
		if (pum_width < p_pw)
		    pum_width = p_pw;
	    }
	    else if (((cursor_col > p_pw || cursor_col > max_width)
#ifdef FEAT_RIGHTLEFT
			&& !curwin->w_p_rl)
		|| (curwin->w_p_rl && (cursor_col < Columns - p_pw
			|| cursor_col < Columns - max_width)
#endif
		    ))
	    {
		/* align pum edge with "cursor_col" */
#ifdef FEAT_RIGHTLEFT
		if (curwin->w_p_rl
			&& W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
		{
		    pum_col = cursor_col + max_width + pum_scrollbar + 1;
		    if (pum_col >= Columns)
			pum_col = Columns - 1;
		}
		else if (!curwin->w_p_rl)
#endif
		{
		    if (curwin->w_wincol > Columns - max_width - pum_scrollbar
							  && max_width <= p_pw)
		    {
			/* use full width to end of the screen */
			pum_col = Columns - max_width - pum_scrollbar;
			if (pum_col < 0)
			    pum_col = 0;
		    }
		}

#ifdef FEAT_RIGHTLEFT
		if (curwin->w_p_rl)
		    pum_width = pum_col - pum_scrollbar + 1;
		else
#endif
		    pum_width = Columns - pum_col - pum_scrollbar;

		if (pum_width < p_pw)
		{
		    pum_width = p_pw;
#ifdef FEAT_RIGHTLEFT
		    if (curwin->w_p_rl)
		    {
			if (pum_width > pum_col)
			    pum_width = pum_col;
		    }
		    else
#endif
		    {
			if (pum_width >= Columns - pum_col)
			    pum_width = Columns - pum_col - 1;
		    }
		}
		else if (pum_width > max_width + pum_kind_width
							  + pum_extra_width + 1
			    && pum_width > p_pw)
		{
		    pum_width = max_width + pum_kind_width
							 + pum_extra_width + 1;
		    if (pum_width < p_pw)
			pum_width = p_pw;
		}
	    }

	}
	else if (Columns < def_width)
	{
	    /* not enough room, will use what we have */
#ifdef FEAT_RIGHTLEFT
	    if (curwin->w_p_rl)
		pum_col = Columns - 1;
	    else
#endif
		pum_col = 0;
	    pum_width = Columns - 1;
	}
	else
	{
	    if (max_width > p_pw)
		max_width = p_pw;	/* truncate */
#ifdef FEAT_RIGHTLEFT
	    if (curwin->w_p_rl)
		pum_col = max_width - 1;
	    else
#endif
		pum_col = Columns - max_width;
	    pum_width = max_width - pum_scrollbar;
	}

	/* Set selected item and redraw.  If the window size changed need to
	 * redo the positioning.  Limit this to two times, when there is not
	 * much room the window size will keep changing. */
    } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
}
Пример #2
0
/// Show the popup menu with items "array[size]".
/// "array" must remain valid until pum_undisplay() is called!
/// When possible the leftmost character is aligned with screen column "col".
/// The menu appears above the screen line "row" or at "row" + "height" - 1.
///
/// @param array
/// @param size
/// @param selected index of initially selected item, none if out of range
/// @param array_changed if true, array contains different items since last call
///                      if false, a new item is selected, but the array
///                      is the same
void pum_display(pumitem_T *array, int size, int selected, bool array_changed)
{
  int w;
  int def_width;
  int max_width;
  int kind_width;
  int extra_width;
  int i;
  int top_clear;
  int row;
  int context_lines;
  int col;
  int above_row = cmdline_row;
  int redo_count = 0;

  if (!pum_is_visible) {
    // To keep the code simple, we only allow changing the
    // draw mode when the popup menu is not being displayed
    pum_external = pum_wants_external;
  }

redo:
  // Mark the pum as visible already here,
  // to avoid that must_redraw is set when 'cursorcolumn' is on.
  pum_is_visible = true;
  validate_cursor_col();

  // anchor position: the start of the completed word
  row = curwin->w_wrow + curwin->w_winrow;
  if (curwin->w_p_rl) {
    col = curwin->w_wincol + curwin->w_width - curwin->w_wcol - 1;
  } else {
    col = curwin->w_wincol + curwin->w_wcol;
  }

  if (pum_external) {
    Array args = ARRAY_DICT_INIT;
    if (array_changed) {
      Array arr = ARRAY_DICT_INIT;
      for (i = 0; i < size; i++) {
        Array item = ARRAY_DICT_INIT;
        ADD(item, STRING_OBJ(cstr_to_string((char *)array[i].pum_text)));
        ADD(item, STRING_OBJ(cstr_to_string((char *)array[i].pum_kind)));
        ADD(item, STRING_OBJ(cstr_to_string((char *)array[i].pum_extra)));
        ADD(item, STRING_OBJ(cstr_to_string((char *)array[i].pum_info)));
        ADD(arr, ARRAY_OBJ(item));
      }
      ADD(args, ARRAY_OBJ(arr));
      ADD(args, INTEGER_OBJ(selected));
      ADD(args, INTEGER_OBJ(row));
      ADD(args, INTEGER_OBJ(col));
      ui_event("popupmenu_show", args);
    } else {
      ADD(args, INTEGER_OBJ(selected));
      ui_event("popupmenu_select", args);
    }
    return;
  }

  def_width = PUM_DEF_WIDTH;
  max_width = 0;
  kind_width = 0;
  extra_width = 0;

  if (firstwin->w_p_pvw) {
    top_clear = firstwin->w_height;
  } else {
    top_clear = 0;
  }

  // When the preview window is at the bottom stop just above it.  Also
  // avoid drawing over the status line so that it's clear there is a window
  // boundary.
  if (lastwin->w_p_pvw) {
    above_row -= lastwin->w_height + lastwin->w_status_height + 1;
  }

  // Figure out the size and position of the pum.
  if (size < PUM_DEF_HEIGHT) {
    pum_height = size;
  } else {
    pum_height = PUM_DEF_HEIGHT;
  }

  if ((p_ph > 0) && (pum_height > p_ph)) {
    pum_height = (int)p_ph;
  }

  // Put the pum below "row" if possible.  If there are few lines decide on
  // where there is more room.
  if ((row  + 2 >= above_row - pum_height)
      && (row > (above_row - top_clear) / 2)) {
    // pum above "row"

    // Leave two lines of context if possible
    if (curwin->w_wrow - curwin->w_cline_row >= 2) {
      context_lines = 2;
    } else {
      context_lines = curwin->w_wrow - curwin->w_cline_row;
    }

    if (row >= size + context_lines) {
      pum_row = row - size - context_lines;
      pum_height = size;
    } else {
      pum_row = 0;
      pum_height = row - context_lines;
    }

    if ((p_ph > 0) && (pum_height > p_ph)) {
      pum_row += pum_height - (int)p_ph;
      pum_height = (int)p_ph;
    }
  } else {
    // pum below "row"

    // Leave two lines of context if possible
    if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3) {
      context_lines = 3;
    } else {
      context_lines = curwin->w_cline_row
                      + curwin->w_cline_height - curwin->w_wrow;
    }

    pum_row = row + context_lines;
    if (size > above_row - pum_row) {
      pum_height = above_row - pum_row;
    } else {
      pum_height = size;
    }

    if ((p_ph > 0) && (pum_height > p_ph)) {
      pum_height = (int)p_ph;
    }
  }

  // don't display when we only have room for one line
  if ((pum_height < 1) || ((pum_height == 1) && (size > 1))) {
    return;
  }

  // If there is a preview window at the top avoid drawing over it.
  if (firstwin->w_p_pvw
      && (pum_row < firstwin->w_height)
      && (pum_height > firstwin->w_height + 4)) {
    pum_row += firstwin->w_height;
    pum_height -= firstwin->w_height;
  }

  // Compute the width of the widest match and the widest extra.
  for (i = 0; i < size; ++i) {
    w = vim_strsize(array[i].pum_text);

    if (max_width < w) {
      max_width = w;
    }

    if (array[i].pum_kind != NULL) {
      w = vim_strsize(array[i].pum_kind) + 1;

      if (kind_width < w) {
        kind_width = w;
      }
    }

    if (array[i].pum_extra != NULL) {
      w = vim_strsize(array[i].pum_extra) + 1;

      if (extra_width < w) {
        extra_width = w;
      }
    }
  }
  pum_base_width = max_width;
  pum_kind_width = kind_width;

  // if there are more items than room we need a scrollbar
  if (pum_height < size) {
    pum_scrollbar = 1;
    max_width++;
  } else {
    pum_scrollbar = 0;
  }

  if (def_width < max_width) {
    def_width = max_width;
  }

  if ((((col < Columns - PUM_DEF_WIDTH) || (col < Columns - max_width))
       && !curwin->w_p_rl)
      || (curwin->w_p_rl && ((col > PUM_DEF_WIDTH) || (col > max_width)))) {
    // align pum column with "col"
    pum_col = col;
    if (curwin->w_p_rl) {
      pum_width = pum_col - pum_scrollbar + 1;
    } else {
      assert(Columns - pum_col - pum_scrollbar >= INT_MIN
             && Columns - pum_col - pum_scrollbar <= INT_MAX);
      pum_width = (int)(Columns - pum_col - pum_scrollbar);
    }

    if ((pum_width > max_width + kind_width + extra_width + 1)
        && (pum_width > PUM_DEF_WIDTH)) {
      pum_width = max_width + kind_width + extra_width + 1;

      if (pum_width < PUM_DEF_WIDTH) {
        pum_width = PUM_DEF_WIDTH;
      }
    }
  } else if (Columns < def_width) {
    // not enough room, will use what we have
    if (curwin->w_p_rl) {
      assert(Columns - 1 >= INT_MIN);
      pum_col = (int)(Columns - 1);
    } else {
      pum_col = 0;
    }
    assert(Columns - 1 >= INT_MIN);
    pum_width = (int)(Columns - 1);
  } else {
    if (max_width > PUM_DEF_WIDTH) {
      // truncate
      max_width = PUM_DEF_WIDTH;
    }

    if (curwin->w_p_rl) {
      pum_col = max_width - 1;
    } else {
      assert(Columns - max_width >= INT_MIN && Columns - max_width <= INT_MAX);
      pum_col = (int)(Columns - max_width);
    }
    pum_width = max_width - pum_scrollbar;
  }

  pum_array = array;
  pum_size = size;

  // Set selected item and redraw.  If the window size changed need to redo
  // the positioning.  Limit this to two times, when there is not much
  // room the window size will keep changing.
  if (pum_set_selected(selected, redo_count) && (++redo_count <= 2)) {
    goto redo;
  }
}
Пример #3
0
/// Show the popup menu with items "array[size]".
/// "array" must remain valid until pum_undisplay() is called!
/// When possible the leftmost character is aligned with screen column "col".
/// The menu appears above the screen line "row" or at "row" + "height" - 1.
///
/// @param array
/// @param size
/// @param selected index of initially selected item, none if out of range
void pum_display(pumitem_T *array, int size, int selected)
{
  int w;
  int def_width;
  int max_width;
  int kind_width;
  int extra_width;
  int i;
  int top_clear;
  int row;
  int context_lines;
  int col;
  int above_row = cmdline_row;
  int redo_count = 0;

redo:
  def_width = PUM_DEF_WIDTH;
  max_width = 0;
  kind_width = 0;
  extra_width = 0;

  // Pretend the pum is already there to avoid that must_redraw is set when
  // 'cuc' is on.
  pum_array = (pumitem_T *)1;
  validate_cursor_col();
  pum_array = NULL;

  row = curwin->w_wrow + curwin->w_winrow;

  if (firstwin->w_p_pvw) {
    top_clear = firstwin->w_height;
  } else {
    top_clear = 0;
  }

  // When the preview window is at the bottom stop just above it.  Also
  // avoid drawing over the status line so that it's clear there is a window
  // boundary.
  if (lastwin->w_p_pvw) {
    above_row -= lastwin->w_height + lastwin->w_status_height + 1;
  }

  // Figure out the size and position of the pum.
  if (size < PUM_DEF_HEIGHT) {
    pum_height = size;
  } else {
    pum_height = PUM_DEF_HEIGHT;
  }

  if ((p_ph > 0) && (pum_height > p_ph)) {
    pum_height = p_ph;
  }

  // Put the pum below "row" if possible.  If there are few lines decide on
  // where there is more room.
  if ((row  + 2 >= above_row - pum_height)
      && (row > (above_row - top_clear) / 2)) {
    // pum above "row"

    // Leave two lines of context if possible
    if (curwin->w_wrow - curwin->w_cline_row >= 2) {
      context_lines = 2;
    } else {
      context_lines = curwin->w_wrow - curwin->w_cline_row;
    }

    if (row >= size + context_lines) {
      pum_row = row - size - context_lines;
      pum_height = size;
    } else {
      pum_row = 0;
      pum_height = row - context_lines;
    }

    if ((p_ph > 0) && (pum_height > p_ph)) {
      pum_row += pum_height - p_ph;
      pum_height = p_ph;
    }
  } else {
    // pum below "row"

    // Leave two lines of context if possible
    if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3) {
      context_lines = 3;
    } else {
      context_lines = curwin->w_cline_row
                      + curwin->w_cline_height - curwin->w_wrow;
    }

    pum_row = row + context_lines;
    if (size > above_row - pum_row) {
      pum_height = above_row - pum_row;
    } else {
      pum_height = size;
    }

    if ((p_ph > 0) && (pum_height > p_ph)) {
      pum_height = p_ph;
    }
  }

  // don't display when we only have room for one line
  if ((pum_height < 1) || ((pum_height == 1) && (size > 1))) {
    return;
  }

  // If there is a preview window at the top avoid drawing over it.
  if (firstwin->w_p_pvw
      && (pum_row < firstwin->w_height)
      && (pum_height > firstwin->w_height + 4)) {
    pum_row += firstwin->w_height;
    pum_height -= firstwin->w_height;
  }

  // Compute the width of the widest match and the widest extra.
  for (i = 0; i < size; ++i) {
    w = vim_strsize(array[i].pum_text);

    if (max_width < w) {
      max_width = w;
    }

    if (array[i].pum_kind != NULL) {
      w = vim_strsize(array[i].pum_kind) + 1;

      if (kind_width < w) {
        kind_width = w;
      }
    }

    if (array[i].pum_extra != NULL) {
      w = vim_strsize(array[i].pum_extra) + 1;

      if (extra_width < w) {
        extra_width = w;
      }
    }
  }
  pum_base_width = max_width;
  pum_kind_width = kind_width;

  // Calculate column
  if (curwin->w_p_rl) {
    col = curwin->w_wincol + curwin->w_width - curwin->w_wcol - 1;
  } else {
    col = curwin->w_wincol + curwin->w_wcol;
  }

  // if there are more items than room we need a scrollbar
  if (pum_height < size) {
    pum_scrollbar = 1;
    max_width++;
  } else {
    pum_scrollbar = 0;
  }

  if (def_width < max_width) {
    def_width = max_width;
  }

  if ((((col < Columns - PUM_DEF_WIDTH) || (col < Columns - max_width))
       && !curwin->w_p_rl)
      || (curwin->w_p_rl && ((col > PUM_DEF_WIDTH) || (col > max_width)))) {
    // align pum column with "col"
    pum_col = col;
    if (curwin->w_p_rl) {
      pum_width = pum_col - pum_scrollbar + 1;
    } else {
      pum_width = Columns - pum_col - pum_scrollbar;
    }

    if ((pum_width > max_width + kind_width + extra_width + 1)
        && (pum_width > PUM_DEF_WIDTH)) {
      pum_width = max_width + kind_width + extra_width + 1;

      if (pum_width < PUM_DEF_WIDTH) {
        pum_width = PUM_DEF_WIDTH;
      }
    }
  } else if (Columns < def_width) {
    // not enough room, will use what we have
    if (curwin->w_p_rl) {
      pum_col = Columns - 1;
    } else {
      pum_col = 0;
    }
    pum_width = Columns - 1;
  } else {
    if (max_width > PUM_DEF_WIDTH) {
      // truncate
      max_width = PUM_DEF_WIDTH;
    }

    if (curwin->w_p_rl) {
      pum_col = max_width - 1;
    } else {
      pum_col = Columns - max_width;
    }
    pum_width = max_width - pum_scrollbar;
  }

  pum_array = array;
  pum_size = size;

  // Set selected item and redraw.  If the window size changed need to redo
  // the positioning.  Limit this to two times, when there is not much
  // room the window size will keep changing.
  if (pum_set_selected(selected, redo_count) && (++redo_count <= 2)) {
    goto redo;
  }
}
Пример #4
0
/*
 * Show the popup menu with items "array[size]".
 * "array" must remain valid until pum_undisplay() is called!
 * When possible the leftmost character is aligned with screen column "col".
 * The menu appears above the screen line "row" or at "row" + "height" - 1.
 */
void
pum_display (
    pumitem_T *array,
    int size,
    int selected                   /* index of initially selected item, none if
                                   out of range */
)
{
    int w;
    int def_width;
    int max_width;
    int kind_width;
    int extra_width;
    int i;
    int top_clear;
    int row;
    int context_lines;
    int col;
    int above_row = cmdline_row;
    int redo_count = 0;

redo:
    def_width = PUM_DEF_WIDTH;
    max_width = 0;
    kind_width = 0;
    extra_width = 0;

    /* Pretend the pum is already there to avoid that must_redraw is set when
     * 'cuc' is on. */
    pum_array = (pumitem_T *)1;
    validate_cursor_col();
    pum_array = NULL;

    row = curwin->w_wrow + W_WINROW(curwin);

    if (firstwin->w_p_pvw)
        top_clear = firstwin->w_height;
    else
        top_clear = 0;

    /* When the preview window is at the bottom stop just above it.  Also
     * avoid drawing over the status line so that it's clear there is a window
     * boundary. */
    if (lastwin->w_p_pvw)
        above_row -= lastwin->w_height + lastwin->w_status_height + 1;

    /*
     * Figure out the size and position of the pum.
     */
    if (size < PUM_DEF_HEIGHT)
        pum_height = size;
    else
        pum_height = PUM_DEF_HEIGHT;
    if (p_ph > 0 && pum_height > p_ph)
        pum_height = p_ph;

    /* Put the pum below "row" if possible.  If there are few lines decide on
     * where there is more room. */
    if (row  + 2 >= above_row - pum_height
            && row > (above_row - top_clear) / 2) {
        /* pum above "row" */

        /* Leave two lines of context if possible */
        if (curwin->w_wrow - curwin->w_cline_row >= 2)
            context_lines = 2;
        else
            context_lines = curwin->w_wrow - curwin->w_cline_row;

        if (row >= size + context_lines) {
            pum_row = row - size - context_lines;
            pum_height = size;
        } else   {
            pum_row = 0;
            pum_height = row - context_lines;
        }
        if (p_ph > 0 && pum_height > p_ph) {
            pum_row += pum_height - p_ph;
            pum_height = p_ph;
        }
    } else   {
        /* pum below "row" */

        /* Leave two lines of context if possible */
        if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3)
            context_lines = 3;
        else
            context_lines = curwin->w_cline_row
                            + curwin->w_cline_height - curwin->w_wrow;

        pum_row = row + context_lines;
        if (size > above_row - pum_row)
            pum_height = above_row - pum_row;
        else
            pum_height = size;
        if (p_ph > 0 && pum_height > p_ph)
            pum_height = p_ph;
    }

    /* don't display when we only have room for one line */
    if (pum_height < 1 || (pum_height == 1 && size > 1))
        return;

    /* If there is a preview window at the top avoid drawing over it. */
    if (firstwin->w_p_pvw
            && pum_row < firstwin->w_height
            && pum_height > firstwin->w_height + 4) {
        pum_row += firstwin->w_height;
        pum_height -= firstwin->w_height;
    }

    /* Compute the width of the widest match and the widest extra. */
    for (i = 0; i < size; ++i) {
        w = vim_strsize(array[i].pum_text);
        if (max_width < w)
            max_width = w;
        if (array[i].pum_kind != NULL) {
            w = vim_strsize(array[i].pum_kind) + 1;
            if (kind_width < w)
                kind_width = w;
        }
        if (array[i].pum_extra != NULL) {
            w = vim_strsize(array[i].pum_extra) + 1;
            if (extra_width < w)
                extra_width = w;
        }
    }
    pum_base_width = max_width;
    pum_kind_width = kind_width;

    /* Calculate column */
    if (curwin->w_p_rl)
        col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol - 1;
    else
        col = W_WINCOL(curwin) + curwin->w_wcol;

    /* if there are more items than room we need a scrollbar */
    if (pum_height < size) {
        pum_scrollbar = 1;
        ++max_width;
    } else
        pum_scrollbar = 0;

    if (def_width < max_width)
        def_width = max_width;

    if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
            && !curwin->w_p_rl)
            || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
               )) {
        /* align pum column with "col" */
        pum_col = col;

        if (curwin->w_p_rl)
            pum_width = pum_col - pum_scrollbar + 1;
        else
            pum_width = Columns - pum_col - pum_scrollbar;

        if (pum_width > max_width + kind_width + extra_width + 1
                && pum_width > PUM_DEF_WIDTH) {
            pum_width = max_width + kind_width + extra_width + 1;
            if (pum_width < PUM_DEF_WIDTH)
                pum_width = PUM_DEF_WIDTH;
        }
    } else if (Columns < def_width)   {
        /* not enough room, will use what we have */
        if (curwin->w_p_rl)
            pum_col = Columns - 1;
        else
            pum_col = 0;
        pum_width = Columns - 1;
    } else   {
        if (max_width > PUM_DEF_WIDTH)
            max_width = PUM_DEF_WIDTH;        /* truncate */
        if (curwin->w_p_rl)
            pum_col = max_width - 1;
        else
            pum_col = Columns - max_width;
        pum_width = max_width - pum_scrollbar;
    }

    pum_array = array;
    pum_size = size;

    /* Set selected item and redraw.  If the window size changed need to redo
     * the positioning.  Limit this to two times, when there is not much
     * room the window size will keep changing. */
    if (pum_set_selected(selected, redo_count) && ++redo_count <= 2)
        goto redo;
}