aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2021-02-07 16:28:30 +0100
committerLars Ingebrigtsen2021-02-07 16:28:34 +0100
commit56e76f0eb00d92b49ddd5757d0a68d09dc522d39 (patch)
tree99a987cbdce101f56f7c82722079d08fb86a2b6b
parent5a4d50dfb136080fa2353461ee888d552da44a29 (diff)
downloademacs-56e76f0eb00d92b49ddd5757d0a68d09dc522d39.tar.gz
emacs-56e76f0eb00d92b49ddd5757d0a68d09dc522d39.zip
Move line-number-at-pos to C
* doc/lispref/positions.texi (Text Lines): Revert previous change. * lisp/simple.el (line-number-at-pos): Remove definition. * lisp/simple.el (count-lines): Revert back to using `forward-line', because there seems to be a disagreement on how lines should be counted in a region... * src/fns.c (Fline_number_at_pos): Rename from Fline_number_at_position and adjust parameter list.
-rw-r--r--doc/lispref/positions.texi18
-rw-r--r--etc/NEWS4
-rw-r--r--lisp/simple.el17
-rw-r--r--src/fns.c31
-rw-r--r--test/src/fns-tests.el8
5 files changed, 35 insertions, 43 deletions
diff --git a/doc/lispref/positions.texi b/doc/lispref/positions.texi
index 9adce21baec..dc0c7442d8d 100644
--- a/doc/lispref/positions.texi
+++ b/doc/lispref/positions.texi
@@ -437,18 +437,16 @@ prints a message reporting the number of lines, words, and characters
437in the buffer, or in the region if the region is active. 437in the buffer, or in the region if the region is active.
438@end deffn 438@end deffn
439 439
440@defun line-number-at-position pos
441This function returns the line number in the current buffer
442corresponding to the buffer position @var{pos}. If narrowing is in
443effect, this is the line number in the visible part of the buffer.
444@end defun
445
446@defun line-number-at-pos &optional pos absolute 440@defun line-number-at-pos &optional pos absolute
447@cindex line number 441@cindex line number
448This function is like @code{line-number-at-position}, but if @var{pos} 442This function returns the line number in the current buffer
449is @code{nil} or omitted, the current buffer position is used. In 443corresponding to the buffer position @var{pos}. If @var{pos} is
450addition, if @var{absolute} is non-@code{nil}, ignore any narrowing 444@code{nil} or omitted, the current buffer position is used. If
451and return the absolute line number. 445@var{absolute} is @code{nil}, the default, counting starts at
446@code{(point-min)}, so the value refers to the contents of the
447accessible portion of the (potentially narrowed) buffer. If
448@var{absolute} is non-@code{nil}, ignore any narrowing and return
449the absolute line number.
452@end defun 450@end defun
453 451
454@ignore 452@ignore
diff --git a/etc/NEWS b/etc/NEWS
index 93a60bf14cf..0faed3e5aa2 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2192,10 +2192,6 @@ back in Emacs 23.1. The affected functions are: 'make-obsolete',
2192 2192
2193* Lisp Changes in Emacs 28.1 2193* Lisp Changes in Emacs 28.1
2194 2194
2195+++
2196** New function 'line-number-at-position'.
2197This returns the line number in the visible portion of the buffer.
2198
2199--- 2195---
2200** New variable 'indent-line-ignored-functions'. 2196** New variable 'indent-line-ignored-functions'.
2201This allows modes to cycle through a set of indentation functions 2197This allows modes to cycle through a set of indentation functions
diff --git a/lisp/simple.el b/lisp/simple.el
index eab2ac25691..73e3fb9f847 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1472,22 +1472,7 @@ included in the count."
1472 (assq prop buffer-invisibility-spec))) 1472 (assq prop buffer-invisibility-spec)))
1473 (setq invisible-count (1+ invisible-count)))) 1473 (setq invisible-count (1+ invisible-count))))
1474 invisible-count)))) 1474 invisible-count))))
1475 (t (1- (line-number-at-position (point-max)))))))) 1475 (t (- (buffer-size) (forward-line (buffer-size))))))))
1476
1477(defun line-number-at-pos (&optional pos absolute)
1478 "Return buffer line number at position POS.
1479If POS is nil, use current buffer location.
1480
1481If ABSOLUTE is nil, the default, counting starts
1482at (point-min), so the value refers to the contents of the
1483accessible portion of the (potentially narrowed) buffer. If
1484ABSOLUTE is non-nil, ignore any narrowing and return the
1485absolute line number."
1486 (if absolute
1487 (save-restriction
1488 (widen)
1489 (line-number-at-position (or pos (point))))
1490 (line-number-at-position (or pos (point)))))
1491 1476
1492(defcustom what-cursor-show-names nil 1477(defcustom what-cursor-show-names nil
1493 "Whether to show character names in `what-cursor-position'." 1478 "Whether to show character names in `what-cursor-position'."
diff --git a/src/fns.c b/src/fns.c
index 479a5975ce7..d27f63222c4 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5759,21 +5759,34 @@ in OBJECT. */)
5759 return CDR (collector); 5759 return CDR (collector);
5760} 5760}
5761 5761
5762DEFUN ("line-number-at-position", Fline_number_at_position, 5762DEFUN ("line-number-at-pos", Fline_number_at_pos,
5763 Sline_number_at_position, 1, 1, 0, 5763 Sline_number_at_pos, 0, 2, 0,
5764 doc: /* Return the line number at POSITION. 5764 doc: /* Return the line number at POSITION.
5765If POSITION is nil, use the current buffer location.
5766
5765If the buffer is narrowed, the position returned is the position in the 5767If the buffer is narrowed, the position returned is the position in the
5766visible part of the buffer. */) 5768visible part of the buffer. If ABSOLUTE is non-nil, count the lines
5767 (register Lisp_Object position) 5769from the absolute start of the buffer. */)
5770 (register Lisp_Object position, Lisp_Object absolute)
5768{ 5771{
5769 CHECK_FIXNUM (position); 5772 ptrdiff_t pos, start = BEGV;
5770 ptrdiff_t pos = XFIXNUM (position); 5773
5774 if (NILP (position))
5775 pos = PT;
5776 else
5777 {
5778 CHECK_FIXNUM (position);
5779 pos = XFIXNUM (position);
5780 }
5781
5782 if (!NILP (absolute))
5783 start = BEG_BYTE;
5771 5784
5772 /* Check that POSITION is n the visible range of the buffer. */ 5785 /* Check that POSITION is n the visible range of the buffer. */
5773 if (pos < BEGV || pos > ZV) 5786 if (pos < BEGV || pos > ZV)
5774 args_out_of_range (make_int (BEGV), make_int (ZV)); 5787 args_out_of_range (make_int (start), make_int (ZV));
5775 5788
5776 return make_int (count_lines (BEGV_BYTE, CHAR_TO_BYTE (pos)) + 1); 5789 return make_int (count_lines (start, CHAR_TO_BYTE (pos)) + 1);
5777} 5790}
5778 5791
5779 5792
@@ -5817,7 +5830,7 @@ syms_of_fns (void)
5817 defsubr (&Sdefine_hash_table_test); 5830 defsubr (&Sdefine_hash_table_test);
5818 defsubr (&Sstring_search); 5831 defsubr (&Sstring_search);
5819 defsubr (&Sobject_intervals); 5832 defsubr (&Sobject_intervals);
5820 defsubr (&Sline_number_at_position); 5833 defsubr (&Sline_number_at_pos);
5821 5834
5822 /* Crypto and hashing stuff. */ 5835 /* Crypto and hashing stuff. */
5823 DEFSYM (Qiv_auto, "iv-auto"); 5836 DEFSYM (Qiv_auto, "iv-auto");
diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el
index 3a43142106b..928fb15f109 100644
--- a/test/src/fns-tests.el
+++ b/test/src/fns-tests.el
@@ -1102,7 +1102,7 @@
1102(ert-deftest test-line-number-at-position () 1102(ert-deftest test-line-number-at-position ()
1103 (with-temp-buffer 1103 (with-temp-buffer
1104 (insert (make-string 10 ?\n)) 1104 (insert (make-string 10 ?\n))
1105 (should (= (line-number-at-position (point)) 11)) 1105 (should (= (line-number-at-pos (point)) 11))
1106 (should-error (line-number-at-position nil)) 1106 (should (= (line-number-at-pos nil) 11))
1107 (should-error (line-number-at-position -1)) 1107 (should-error (line-number-at-pos -1))
1108 (should-error (line-number-at-position 100)))) 1108 (should-error (line-number-at-pos 100))))