If BYTEPOS is out of range, the value is nil. */)
(Lisp_Object bytepos)
{
+ ptrdiff_t pos_byte;
+
CHECK_NUMBER (bytepos);
- if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
+ pos_byte = XINT (bytepos);
+ if (pos_byte < BEG_BYTE || pos_byte > Z_BYTE)
return Qnil;
- return make_number (BYTE_TO_CHAR (XINT (bytepos)));
+ 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_number (BYTE_TO_CHAR (pos_byte));
}
\f
DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,