Function: newline-cache-check

newline-cache-check is a function defined in search.c.

Signature

(newline-cache-check &optional BUFFER)

Documentation

Check the newline cache of BUFFER against buffer contents.

BUFFER defaults to the current buffer.

Value is an array of 2 sub-arrays of buffer positions for newlines, the first based on the cache, the second based on actually scanning the buffer. If the buffer doesn't have a cache, the value is nil.

Source Code

// Defined in /usr/src/emacs/src/search.c
{
  struct buffer *buf, *old = NULL;
  ptrdiff_t nl_count_cache, nl_count_buf;
  Lisp_Object cache_newlines, buf_newlines, val;
  ptrdiff_t from, found, i;

  if (NILP (buffer))
    buf = current_buffer;
  else
    {
      CHECK_BUFFER (buffer);
      buf = XBUFFER (buffer);
      old = current_buffer;
    }
  if (buf->base_buffer)
    buf = buf->base_buffer;

  /* If the buffer doesn't have a newline cache, return nil.  */
  if (NILP (BVAR (buf, cache_long_scans))
      || buf->newline_cache == NULL)
    return Qnil;

  /* find_newline can only work on the current buffer.  */
  if (old != NULL)
    set_buffer_internal_1 (buf);

  /* How many newlines are there according to the cache?  */
  find_newline (BEGV, BEGV_BYTE, ZV, ZV_BYTE,
		TYPE_MAXIMUM (ptrdiff_t), &nl_count_cache, NULL, true);

  /* Create vector and populate it.  */
  cache_newlines = make_vector (nl_count_cache, make_fixnum (-1));

  if (nl_count_cache)
    {
      for (from = BEGV, found = from, i = 0; from < ZV; from = found, i++)
	{
	  ptrdiff_t from_byte = CHAR_TO_BYTE (from), counted;

	  found = find_newline (from, from_byte, 0, -1, 1, &counted,
				NULL, true);
	  if (counted == 0 || i >= nl_count_cache)
	    break;
	  ASET (cache_newlines, i, make_fixnum (found - 1));
	}
    }

  /* Now do the same, but without using the cache.  */
  find_newline1 (BEGV, BEGV_BYTE, ZV, ZV_BYTE,
		 TYPE_MAXIMUM (ptrdiff_t), &nl_count_buf, NULL, true);
  buf_newlines = make_vector (nl_count_buf, make_fixnum (-1));
  if (nl_count_buf)
    {
      for (from = BEGV, found = from, i = 0; from < ZV; from = found, i++)
	{
	  ptrdiff_t from_byte = CHAR_TO_BYTE (from), counted;

	  found = find_newline1 (from, from_byte, 0, -1, 1, &counted,
				 NULL, true);
	  if (counted == 0 || i >= nl_count_buf)
	    break;
	  ASET (buf_newlines, i, make_fixnum (found - 1));
	}
    }

  /* Construct the value and return it.  */
  val = CALLN (Fvector, cache_newlines, buf_newlines);

  if (old != NULL)
    set_buffer_internal_1 (old);
  return val;
}