/// /// Return length of line "lnum" for horizontal scrolling. /// static colnr_T scroll_line_len(linenr_T lnum) { colnr_T col = 0; char_u *line = ml_get(lnum); if (*line != NUL) { for (;;) { int numchar = chartabsize(line, col); MB_PTR_ADV(line); if (*line == NUL) { // don't count the last character break; } col += numchar; } } return col; }
/* * Return the line at mark "mp". Truncate to fit in window. * The returned string has been allocated. */ static char_u *mark_line(pos_T *mp, int lead_len) { char_u *s, *p; int len; if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count) return vim_strsave((char_u *)"-invalid-"); assert(Columns >= 0 && (size_t)Columns <= SIZE_MAX); s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (size_t)Columns); /* Truncate the line to fit it in the window */ len = 0; for (p = s; *p != NUL; MB_PTR_ADV(p)) { len += ptr2cells(p); if (len >= Columns - lead_len) break; } *p = NUL; return s; }
/* * Redraw the popup menu, using "pum_first" and "pum_selected". */ void pum_redraw(void) { int row = pum_row; int col; int attr_norm = highlight_attr[HLF_PNI]; int attr_select = highlight_attr[HLF_PSI]; int attr_scroll = highlight_attr[HLF_PSB]; int attr_thumb = highlight_attr[HLF_PST]; int attr; int i; int idx; char_u *s; char_u *p = NULL; int totwidth, width, w; int thumb_pos = 0; int thumb_heigth = 1; int round; int n; /* Never display more than we have */ if (pum_first > pum_size - pum_height) pum_first = pum_size - pum_height; if (pum_scrollbar) { thumb_heigth = pum_height * pum_height / pum_size; if (thumb_heigth == 0) thumb_heigth = 1; thumb_pos = (pum_first * (pum_height - thumb_heigth) + (pum_size - pum_height) / 2) / (pum_size - pum_height); } for (i = 0; i < pum_height; ++i) { idx = i + pum_first; attr = (idx == pum_selected) ? attr_select : attr_norm; /* prepend a space if there is room */ #ifdef FEAT_RIGHTLEFT if (curwin->w_p_rl) { if (pum_col < curwin->w_wincol + curwin->w_width - 1) screen_putchar(' ', row, pum_col + 1, attr); } else #endif if (pum_col > 0) screen_putchar(' ', row, pum_col - 1, attr); /* Display each entry, use two spaces for a Tab. * Do this 3 times: For the main text, kind and extra info */ col = pum_col; totwidth = 0; for (round = 1; round <= 3; ++round) { width = 0; s = NULL; switch (round) { case 1: p = pum_array[idx].pum_text; break; case 2: p = pum_array[idx].pum_kind; break; case 3: p = pum_array[idx].pum_extra; break; } if (p != NULL) for ( ; ; MB_PTR_ADV(p)) { if (s == NULL) s = p; w = ptr2cells(p); if (*p == NUL || *p == TAB || totwidth + w > pum_width) { /* Display the text that fits or comes before a Tab. * First convert it to printable characters. */ char_u *st; int saved = *p; if (saved != NUL) *p = NUL; st = transstr(s); if (saved != NUL) *p = saved; #ifdef FEAT_RIGHTLEFT if (curwin->w_p_rl) { if (st != NULL) { char_u *rt = reverse_text(st); if (rt != NULL) { char_u *rt_start = rt; int size; size = vim_strsize(rt); if (size > pum_width) { do { size -= has_mbyte ? (*mb_ptr2cells)(rt) : 1; MB_PTR_ADV(rt); } while (size > pum_width); if (size < pum_width) { /* Most left character requires * 2-cells but only 1 cell is * available on screen. Put a * '<' on the left of the pum * item */ *(--rt) = '<'; size++; } } screen_puts_len(rt, (int)STRLEN(rt), row, col - size + 1, attr); vim_free(rt_start); } vim_free(st); } col -= width; } else #endif { if (st != NULL) { screen_puts_len(st, (int)STRLEN(st), row, col, attr); vim_free(st); } col += width; } if (*p != TAB) break; /* Display two spaces for a Tab. */ #ifdef FEAT_RIGHTLEFT if (curwin->w_p_rl) { screen_puts_len((char_u *)" ", 2, row, col - 1, attr); col -= 2; } else #endif { screen_puts_len((char_u *)" ", 2, row, col, attr); col += 2; } totwidth += 2; s = NULL; /* start text at next char */ width = 0; } else width += w; } if (round > 1) n = pum_kind_width + 1; else n = 1; /* Stop when there is nothing more to display. */ if (round == 3 || (round == 2 && pum_array[idx].pum_extra == NULL) || (round == 1 && pum_array[idx].pum_kind == NULL && pum_array[idx].pum_extra == NULL) || pum_base_width + n >= pum_width) break; #ifdef FEAT_RIGHTLEFT if (curwin->w_p_rl) { screen_fill(row, row + 1, pum_col - pum_base_width - n + 1, col + 1, ' ', ' ', attr); col = pum_col - pum_base_width - n + 1; } else #endif { screen_fill(row, row + 1, col, pum_col + pum_base_width + n, ' ', ' ', attr); col = pum_col + pum_base_width + n; } totwidth = pum_base_width + n; } #ifdef FEAT_RIGHTLEFT if (curwin->w_p_rl) screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ', ' ', attr); else #endif screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', attr); if (pum_scrollbar > 0) { #ifdef FEAT_RIGHTLEFT if (curwin->w_p_rl) screen_putchar(' ', row, pum_col - pum_width, i >= thumb_pos && i < thumb_pos + thumb_heigth ? attr_thumb : attr_scroll); else #endif screen_putchar(' ', row, pum_col + pum_width, i >= thumb_pos && i < thumb_pos + thumb_heigth ? attr_thumb : attr_scroll); } ++row; } }
static int coladvance2( pos_T *pos, bool addspaces, /* change the text to achieve our goal? */ bool finetune, /* change char offset for the exact column */ colnr_T wcol /* column to move to */ ) { int idx; char_u *ptr; char_u *line; colnr_T col = 0; int csize = 0; int one_more; int head = 0; one_more = (State & INSERT) || restart_edit != NUL || (VIsual_active && *p_sel != 'o') || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL); line = ml_get_buf(curbuf, pos->lnum, false); if (wcol >= MAXCOL) { idx = (int)STRLEN(line) - 1 + one_more; col = wcol; if ((addspaces || finetune) && !VIsual_active) { curwin->w_curswant = linetabsize(line) + one_more; if (curwin->w_curswant > 0) --curwin->w_curswant; } } else { int width = curwin->w_width - win_col_off(curwin); if (finetune && curwin->w_p_wrap && curwin->w_width != 0 && wcol >= (colnr_T)width) { csize = linetabsize(line); if (csize > 0) csize--; if (wcol / width > (colnr_T)csize / width && ((State & INSERT) == 0 || (int)wcol > csize + 1)) { /* In case of line wrapping don't move the cursor beyond the * right screen edge. In Insert mode allow going just beyond * the last character (like what happens when typing and * reaching the right window edge). */ wcol = (csize / width + 1) * width - 1; } } ptr = line; while (col <= wcol && *ptr != NUL) { /* Count a tab for what it's worth (if list mode not on) */ csize = win_lbr_chartabsize(curwin, line, ptr, col, &head); MB_PTR_ADV(ptr); col += csize; } idx = (int)(ptr - line); /* * Handle all the special cases. The virtual_active() check * is needed to ensure that a virtual position off the end of * a line has the correct indexing. The one_more comparison * replaces an explicit add of one_more later on. */ if (col > wcol || (!virtual_active() && one_more == 0)) { idx -= 1; /* Don't count the chars from 'showbreak'. */ csize -= head; col -= csize; } if (virtual_active() && addspaces && ((col != wcol && col != wcol + 1) || csize > 1)) { /* 'virtualedit' is set: The difference between wcol and col is * filled with spaces. */ if (line[idx] == NUL) { /* Append spaces */ int correct = wcol - col; char_u *newline = xmallocz((size_t)(idx + correct)); memcpy(newline, line, (size_t)idx); memset(newline + idx, ' ', (size_t)correct); ml_replace(pos->lnum, newline, false); changed_bytes(pos->lnum, (colnr_T)idx); idx += correct; col = wcol; } else { /* Break a tab */ int linelen = (int)STRLEN(line); int correct = wcol - col - csize + 1; /* negative!! */ char_u *newline; if (-correct > csize) return FAIL; newline = xmallocz((size_t)(linelen - 1 + csize)); // Copy first idx chars memcpy(newline, line, (size_t)idx); // Replace idx'th char with csize spaces memset(newline + idx, ' ', (size_t)csize); // Copy the rest of the line memcpy(newline + idx + csize, line + idx + 1, (size_t)(linelen - idx - 1)); ml_replace(pos->lnum, newline, false); changed_bytes(pos->lnum, idx); idx += (csize - 1 + correct); col += correct; } } } if (idx < 0) pos->col = 0; else pos->col = idx; pos->coladd = 0; if (finetune) { if (wcol == MAXCOL) { /* The width of the last character is used to set coladd. */ if (!one_more) { colnr_T scol, ecol; getvcol(curwin, pos, &scol, NULL, &ecol); pos->coladd = ecol - scol; } } else { int b = (int)wcol - (int)col; /* The difference between wcol and col is used to set coladd. */ if (b > 0 && b < (MAXCOL - 2 * curwin->w_width)) pos->coladd = b; col += b; } } // Prevent from moving onto a trail byte. if (has_mbyte) { mark_mb_adjustpos(curbuf, pos); } if (col < wcol) return FAIL; return OK; }