diff src/line-number.c @ 5914:bd4d2c8ef9cc

Use the existing C-level line number cache within #'line-number. src/ChangeLog addition: 2015-05-15 Aidan Kehoe <kehoea@parhasard.net> * buffer.c: * buffer.c (Fline_number): New C implementation, using the line number cache of line-number.c, with a new optional BUFFER argument. * buffer.c (syms_of_buffer): Make it available to Lisp. * line-number.c (buffer_line_number): New argument, RESPECT-NARROWING, describing whether to count from the beginning of the visible region or from the beginning of the buffer. * line-number.h: * line-number.h (buffer_line_number): Update its declaration. * redisplay.c (window_line_number): Call it with the new argument. lisp/ChangeLog addition: 2015-05-15 Aidan Kehoe <kehoea@parhasard.net> * simple.el: * simple.el (line-number): Moved to buffer.c; we have an existing line number cache in C, it's a shame not to have it available.
author Aidan Kehoe <kehoea@parhasard.net>
date Fri, 15 May 2015 18:11:47 +0100
parents 56144c8593a8
children
line wrap: on
line diff
--- a/src/line-number.c	Mon May 11 18:04:58 2015 +0100
+++ b/src/line-number.c	Fri May 15 18:11:47 2015 +0100
@@ -266,16 +266,17 @@
    If the calculation (with or without the cache lookup) required more
    than LINE_NUMBER_FAR characters of traversal, update the cache.  */
 EMACS_INT
-buffer_line_number (struct buffer *b, Charbpos pos, int cachep)
+buffer_line_number (struct buffer *b, Charbpos pos, Boolint cachep,
+                    Boolint respect_narrowing)
 {
-  Charbpos beg = BUF_BEGV (b);
+  Charbpos beg = respect_narrowing ? BUF_BEGV (b) : BUF_BEG (b);
   EMACS_INT cached_lines = 0;
   EMACS_INT shortage, line;
 
   if ((pos > beg ? pos - beg : beg - pos) <= LINE_NUMBER_FAR)
     cachep = 0;
 
-  if (cachep)
+  if (cachep && (respect_narrowing || BUF_BEG (b) == BUF_BEGV (b)))
     {
       if (NILP (b->text->line_number_cache))
 	allocate_line_number_cache (b);
@@ -285,13 +286,14 @@
 	  LINE_NUMBER_BEGV (b) = Qzero;
 	  /* #### This has a side-effect of changing the cache.  */
 	  LINE_NUMBER_BEGV (b) =
-	    make_fixnum (buffer_line_number (b, BUF_BEGV (b), 1));
+	    make_fixnum (buffer_line_number (b, BUF_BEGV (b), 1, 0));
 	}
       cached_lines = XFIXNUM (LINE_NUMBER_BEGV (b));
       get_nearest_line_number (b, &beg, pos, &cached_lines);
     }
 
-  scan_buffer (b, '\n', beg, pos, pos > beg ? MOST_POSITIVE_FIXNUM : -MOST_POSITIVE_FIXNUM,
+  scan_buffer (b, '\n', beg, pos,
+               pos > beg ? MOST_POSITIVE_FIXNUM : -MOST_POSITIVE_FIXNUM,
 	       &shortage, 0);
 
   line = MOST_POSITIVE_FIXNUM - shortage;
@@ -299,7 +301,7 @@
     line = -line;
   line += cached_lines;
 
-  if (cachep)
+  if (cachep && (respect_narrowing || BUF_BEG (b) == BUF_BEGV (b)))
     {
       /* If too far, update the cache. */
       if ((pos > beg ? pos - beg : beg - pos) > LINE_NUMBER_FAR)