changeset 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 1b2fdcc3cc5c
children 1af53d35dd53
files lisp/ChangeLog lisp/simple.el src/ChangeLog src/buffer.c src/line-number.c src/line-number.h src/redisplay.c
diffstat 7 files changed, 57 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/ChangeLog	Mon May 11 18:04:58 2015 +0100
+++ b/lisp/ChangeLog	Fri May 15 18:11:47 2015 +0100
@@ -1,3 +1,9 @@
+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.
+
 2015-04-11  Aidan Kehoe  <kehoea@parhasard.net>
 
 	* mule/mule-cmds.el (set-locale-for-language-environment):
--- a/lisp/simple.el	Mon May 11 18:04:58 2015 +0100
+++ b/lisp/simple.el	Fri May 15 18:11:47 2015 +0100
@@ -809,19 +809,7 @@
 		 (message "Line %d" buffer-line)))))))
   (setq zmacs-region-stays t))
 
-;; new in XEmacs 21.2 (not in FSF).
-(defun line-number (&optional pos respect-narrowing)
-  "Return the line number of POS (defaults to point).
-If RESPECT-NARROWING is non-nil, then the narrowed line number is returned;
-otherwise, the absolute line number is returned.  The returned line can always
-be given to `goto-line' to get back to the current line."
-  (if (and pos (/= pos (point)))
-      (save-excursion
-	(goto-char pos)
-	(line-number nil respect-narrowing))
-    (1+ (count-lines (if respect-narrowing (point-min) 1) (point-at-bol)))))
-
-;; FSF 22.0.50.1 (CVS) version of above.
+;; FSF 22.0.50.1 (CVS) version of #'line-number.
 (defun line-number-at-pos (&optional pos)
   (line-number pos t))
 
--- a/src/ChangeLog	Mon May 11 18:04:58 2015 +0100
+++ b/src/ChangeLog	Fri May 15 18:11:47 2015 +0100
@@ -1,3 +1,19 @@
+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.
+
 2015-05-11  Aidan Kehoe  <kehoea@parhasard.net>
 
 	* unicode.c (encode_unicode_char):
--- a/src/buffer.c	Mon May 11 18:04:58 2015 +0100
+++ b/src/buffer.c	Fri May 15 18:11:47 2015 +0100
@@ -85,6 +85,7 @@
 #include "file-coding.h"
 #include "frame-impl.h"
 #include "insdel.h"
+#include "line-number.h"
 #include "lstream.h"
 #include "process.h"            /* for kill_buffer_processes */
 #ifdef REGION_CACHE_NEEDS_WORK
@@ -1746,7 +1747,28 @@
 
   return Qnil;
 }
-
+
+/* It was a shame to have the line number cache around and not used from
+   Lisp, so move this here from simple.el. */
+
+DEFUN ("line-number", Fline_number, 0, 3, 0, /*
+Return the line number of POSITION within BUFFER.
+
+POSITION defaults to point. If RESPECT-NARROWING is non-nil, then the narrowed
+line number is returned; otherwise, the absolute line number is returned.  The
+returned line can always be given to `goto-line' to get back to the current
+line.
+*/
+       (position, respect_narrowing, buffer_))
+{
+  struct buffer *buf = decode_buffer (buffer_, 0);
+  Charbpos pos = (NILP (position) ? BUF_PT (buf) :
+		  get_buffer_pos_char (buf, position, GB_COERCE_RANGE));
+
+  return make_fixnum (buffer_line_number (buf, pos, 1,
+                                          !NILP (respect_narrowing)) + 1);
+}
+
 #ifdef MEMORY_USAGE_STATS
 
 struct buffer_stats
@@ -1952,6 +1974,7 @@
   DEFSUBR (Fbarf_if_buffer_read_only);
   DEFSUBR (Fbury_buffer);
   DEFSUBR (Fkill_all_local_variables);
+  DEFSUBR (Fline_number);
 #if defined (DEBUG_XEMACS) && defined (MULE)
   DEFSUBR (Fbuffer_char_byte_converion_info);
   DEFSUBR (Fstring_char_byte_converion_info);
--- 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)
--- a/src/line-number.h	Mon May 11 18:04:58 2015 +0100
+++ b/src/line-number.h	Fri May 15 18:11:47 2015 +0100
@@ -25,6 +25,6 @@
 					  const Ibyte *, Bytecount);
 void delete_invalidate_line_number_cache (struct buffer *, Charbpos, Charbpos);
 
-EMACS_INT buffer_line_number (struct buffer *, Charbpos, int);
+EMACS_INT buffer_line_number (struct buffer *, Charbpos, int, Boolint);
 
 #endif /* INCLUDED_line_number_h_ */
--- a/src/redisplay.c	Mon May 11 18:04:58 2015 +0100
+++ b/src/redisplay.c	Fri May 15 18:11:47 2015 +0100
@@ -7320,7 +7320,7 @@
      : marker_position (w->pointm[type]));
   EMACS_INT line;
 
-  line = buffer_line_number (b, pos, 1);
+  line = buffer_line_number (b, pos, 1, 1);
 
   {
     static Ascbyte window_line_number_buf[DECIMAL_PRINT_SIZE (long)];