aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2018-11-01 19:15:43 +0100
committerBjörn Linse <bjorn.linse@gmail.com>2018-11-01 22:00:40 +0100
commitc40f992e10355edd586d5cfbca246b9659554438 (patch)
tree7411e57a18710d161c5d48aee37eaa59fbd8e960 /src
parent88f77c28e52a17e76f226beded0c66a018f0540d (diff)
downloadrneovim-c40f992e10355edd586d5cfbca246b9659554438.tar.gz
rneovim-c40f992e10355edd586d5cfbca246b9659554438.tar.bz2
rneovim-c40f992e10355edd586d5cfbca246b9659554438.zip
api: simplify nvim_buf_get_offset function
Diffstat (limited to 'src')
-rw-r--r--src/nvim/api/buffer.c13
-rw-r--r--src/nvim/buffer.c3
-rw-r--r--src/nvim/eval.c4
-rw-r--r--src/nvim/memline.c22
4 files changed, 23 insertions, 19 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index 9866f1140e..39330690e8 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -493,11 +493,10 @@ end:
/// Return the byte offset for a line.
//
-/// This includes the end-of-line character, depending on the 'fileformat'
-/// option for the current buffer. The first line returns 0. UTF-8 encoding is
-/// used, 'fileencoding' is ignored. Sending in the index just below the last
-/// line gives the total byte count for the entire file. A final newline is
-/// included if it would be written, see 'eol'.
+/// The first line returns 0. UTF-8 bytes are counted, and EOL counts as one
+/// byte. 'fileformat' and 'fileencoding' are ignored. Sending in the index
+/// just below the last line gives the total byte count for the entire buffer.
+/// A final EOL is included if it would be written, see 'eol'.
///
/// Unlike |line2byte()|, throws error for out-of-bounds indexing.
/// Returns -1 for unloaded buffer.
@@ -506,7 +505,7 @@ end:
/// @param index Line index
/// @param[out] err Error details, if any
/// @return Integer Byte offset
-Integer nvim_buf_get_offset_for_line(Buffer buffer, Integer index, Error *err)
+Integer nvim_buf_get_offset(Buffer buffer, Integer index, Error *err)
FUNC_API_SINCE(5)
{
buf_T *buf = find_buffer_by_handle(buffer, err);
@@ -524,7 +523,7 @@ Integer nvim_buf_get_offset_for_line(Buffer buffer, Integer index, Error *err)
return 0;
}
- return ml_find_line_or_offset(buf, (int)index+1, NULL);
+ return ml_find_line_or_offset(buf, (int)index+1, NULL, true);
}
/// Gets a buffer-scoped (b:) variable.
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c
index ce6aa69239..cd77f48320 100644
--- a/src/nvim/buffer.c
+++ b/src/nvim/buffer.c
@@ -3734,7 +3734,8 @@ int build_stl_str_hl(
FALLTHROUGH;
case STL_OFFSET:
{
- long l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
+ long l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL,
+ false);
num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
0L : l + 1 + (!(State & INSERT) && empty_line ?
0 : (int)wp->w_cursor.col);
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 052e0e3f35..4e0e3f6f1f 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -7232,7 +7232,7 @@ static void f_byte2line(typval_T *argvars, typval_T *rettv, FunPtr fptr)
rettv->vval.v_number = -1;
} else {
rettv->vval.v_number = (varnumber_T)ml_find_line_or_offset(curbuf, 0,
- &boff);
+ &boff, false);
}
}
@@ -12084,7 +12084,7 @@ static void f_line2byte(typval_T *argvars, typval_T *rettv, FunPtr fptr)
if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) {
rettv->vval.v_number = -1;
} else {
- rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
+ rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL, false);
}
if (rettv->vval.v_number >= 0) {
rettv->vval.v_number++;
diff --git a/src/nvim/memline.c b/src/nvim/memline.c
index aad1750c85..95f3b0c623 100644
--- a/src/nvim/memline.c
+++ b/src/nvim/memline.c
@@ -3850,13 +3850,17 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
ml_upd_lastcurix = curix;
}
-/*
- * Find offset for line or line with offset.
- * Find line with offset if "lnum" is 0; return remaining offset in offp
- * Find offset of line if "lnum" > 0
- * return -1 if information is not available
- */
-long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
+/// Find offset for line or line with offset.
+///
+/// @param buf buffer to use
+/// @param lnum if > 0, find offset of lnum, store offset in offp
+/// if == 0, return line with offset *offp
+/// @param offp Location where offset of line is stored, or to read offset to
+/// use to find line. In the later case, store remaining offset.
+/// @param no_ff ignore 'fileformat' option, always use one byte for NL.
+///
+/// @return -1 if information is not available
+long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
{
linenr_T curline;
int curix;
@@ -3869,7 +3873,7 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
int text_end;
long offset;
int len;
- int ffdos = (get_fileformat(buf) == EOL_DOS);
+ int ffdos = !no_ff && (get_fileformat(buf) == EOL_DOS);
int extra = 0;
/* take care of cached line first */
@@ -3983,7 +3987,7 @@ void goto_byte(long cnt)
if (boff) {
boff--;
}
- lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff);
+ lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff, false);
if (lnum < 1) { // past the end
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
curwin->w_curswant = MAXCOL;