Function: byte-to-position

byte-to-position is a function defined in editfns.c.

Signature

(byte-to-position BYTEPOS)

Documentation

Return the character position for byte position BYTEPOS.

If BYTEPOS is out of range, the value is nil.

View in manual

Probably introduced at or before Emacs version 20.4.

Source Code

// Defined in /usr/src/emacs/src/editfns.c
{
  ptrdiff_t pos_byte;

  CHECK_FIXNUM (bytepos);
  pos_byte = XFIXNUM (bytepos);
  if (pos_byte < BEG_BYTE || pos_byte > Z_BYTE)
    return Qnil;
  if (Z != Z_BYTE)
    /* There are multibyte characters in the buffer.
       The argument of BYTE_TO_CHAR must be a byte position at
       a character boundary, so search for the start of the current
       character.  */
    while (!CHAR_HEAD_P (FETCH_BYTE (pos_byte)))
      pos_byte--;
  return make_fixnum (BYTE_TO_CHAR (pos_byte));
}