Function: line-number-at-pos

line-number-at-pos is a function defined in fns.c.

Signature

(line-number-at-pos &optional POSITION ABSOLUTE)

Documentation

Return the line number at POSITION in the current buffer.

If POSITION is nil or omitted, it defaults to point's position in the current buffer.

If the buffer is narrowed, the return value by default counts the lines from the beginning of the accessible portion of the buffer. But if the second optional argument ABSOLUTE is non-nil, the value counts the lines from the absolute start of the buffer, disregarding the narrowing.

Probably introduced at or before Emacs version 22.1.

Source Code

// Defined in /usr/src/emacs/src/fns.c
{
  ptrdiff_t pos, start = BEGV_BYTE;

  if (MARKERP (position))
    pos = marker_position (position);
  else if (NILP (position))
    pos = PT;
  else
    {
      CHECK_FIXNUM (position);
      pos = XFIXNUM (position);
    }

  if (!NILP (absolute))
    start = BEG_BYTE;

  /* Check that POSITION is in the accessible range of the buffer. */
  if (pos < BEGV || pos > ZV)
    args_out_of_range_3 (make_int (pos), make_int (BEGV), make_int (ZV));

  return make_int (count_lines (start, CHAR_TO_BYTE (pos)) + 1);
}