/// Get the line number relative to the current cursor position, i.e. the /// difference between line number and cursor position. Only look for lines that /// can be visible, folded lines don't count. /// /// @param lnum line number to get the result for linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum) { linenr_T cursor = wp->w_cursor.lnum; if (lnum == cursor || !hasAnyFolding(wp)) { return lnum - cursor; } linenr_T from_line = lnum < cursor ? lnum : cursor; linenr_T to_line = lnum > cursor ? lnum : cursor; linenr_T retval = 0; // Loop until we reach to_line, skipping folds. for (; from_line < to_line; from_line++, retval++) { // If from_line is in a fold, set it to the last line of that fold. (void)hasFoldingWin(wp, from_line, NULL, &from_line, true, NULL); } // If to_line is in a closed fold, the line count is off by +1. Correct it. if (from_line > to_line) { retval--; } return (lnum < cursor) ? -retval : retval; }
// Compute the position in the buffer line from the posn on the screen in // window "win". // Returns true if the position is below the last line. bool mouse_comp_pos(win_T *win, int *rowp, int *colp, linenr_T *lnump) { int col = *colp; int row = *rowp; linenr_T lnum; bool retval = false; int off; int count; if (win->w_p_rl) col = win->w_width - 1 - col; lnum = win->w_topline; while (row > 0) { // Don't include filler lines in "count" if (win->w_p_diff && !hasFoldingWin(win, lnum, NULL, NULL, true, NULL)) { if (lnum == win->w_topline) { row -= win->w_topfill; } else { row -= diff_check_fill(win, lnum); } count = plines_win_nofill(win, lnum, true); } else { count = plines_win(win, lnum, true); } if (count > row) { break; // Position is in this buffer line. } (void)hasFoldingWin(win, lnum, NULL, &lnum, true, NULL); if (lnum == win->w_buffer->b_ml.ml_line_count) { retval = true; break; // past end of file } row -= count; ++lnum; } if (!retval) { // Compute the column without wrapping. off = win_col_off(win) - win_col_off2(win); if (col < off) col = off; col += row * (win->w_width - off); // add skip column (for long wrapping line) col += win->w_skipcol; } if (!win->w_p_wrap) { col += win->w_leftcol; } // skip line number and fold column in front of the line col -= win_col_off(win); if (col < 0) { col = 0; } *colp = col; *rowp = row; *lnump = lnum; return retval; }