aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2021-02-07 16:02:56 +0100
committerLars Ingebrigtsen2021-02-07 16:03:02 +0100
commit094a109b8eefbabbc99dba925ebec9887c101a91 (patch)
treede1959ee43a6a1b8bda4de6b4ec781a8eccdd0a3
parent4e8d36fdaadade020f0bcadc70d617d8b07b739c (diff)
downloademacs-094a109b8eefbabbc99dba925ebec9887c101a91.tar.gz
emacs-094a109b8eefbabbc99dba925ebec9887c101a91.zip
Add a new function 'line-number-at-position'
* doc/lispref/positions.texi (Text Lines): Document it. * lisp/simple.el (count-lines): Use it. (line-number-at-pos): Ditto. * src/fns.c (Fline_number_at_position): New function (bug#22763).
-rw-r--r--doc/lispref/positions.texi18
-rw-r--r--etc/NEWS4
-rw-r--r--lisp/simple.el17
-rw-r--r--src/fns.c18
-rw-r--r--test/src/fns-tests.el8
5 files changed, 46 insertions, 19 deletions
diff --git a/doc/lispref/positions.texi b/doc/lispref/positions.texi
index dc0c7442d8d..9adce21baec 100644
--- a/doc/lispref/positions.texi
+++ b/doc/lispref/positions.texi
@@ -437,16 +437,18 @@ 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
440@defun line-number-at-pos &optional pos absolute 446@defun line-number-at-pos &optional pos absolute
441@cindex line number 447@cindex line number
442This function returns the line number in the current buffer 448This function is like @code{line-number-at-position}, but if @var{pos}
443corresponding to the buffer position @var{pos}. If @var{pos} is 449is @code{nil} or omitted, the current buffer position is used. In
444@code{nil} or omitted, the current buffer position is used. If 450addition, if @var{absolute} is non-@code{nil}, ignore any narrowing
445@var{absolute} is @code{nil}, the default, counting starts at 451and return the absolute line number.
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.
450@end defun 452@end defun
451 453
452@ignore 454@ignore
diff --git a/etc/NEWS b/etc/NEWS
index 0faed3e5aa2..93a60bf14cf 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2192,6 +2192,10 @@ 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
2195--- 2199---
2196** New variable 'indent-line-ignored-functions'. 2200** New variable 'indent-line-ignored-functions'.
2197This allows modes to cycle through a set of indentation functions 2201This allows modes to cycle through a set of indentation functions
diff --git a/lisp/simple.el b/lisp/simple.el
index e4a363a9a59..eab2ac25691 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1472,7 +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 (- (buffer-size) (forward-line (buffer-size)))))))) 1475 (t (1- (line-number-at-position (point-max))))))))
1476 1476
1477(defun line-number-at-pos (&optional pos absolute) 1477(defun line-number-at-pos (&optional pos absolute)
1478 "Return buffer line number at position POS. 1478 "Return buffer line number at position POS.
@@ -1483,16 +1483,11 @@ at (point-min), so the value refers to the contents of the
1483accessible portion of the (potentially narrowed) buffer. If 1483accessible portion of the (potentially narrowed) buffer. If
1484ABSOLUTE is non-nil, ignore any narrowing and return the 1484ABSOLUTE is non-nil, ignore any narrowing and return the
1485absolute line number." 1485absolute line number."
1486 (save-restriction 1486 (if absolute
1487 (when absolute 1487 (save-restriction
1488 (widen)) 1488 (widen)
1489 (let ((opoint (or pos (point))) start) 1489 (line-number-at-position (or pos (point))))
1490 (save-excursion 1490 (line-number-at-position (or pos (point)))))
1491 (goto-char (point-min))
1492 (setq start (point))
1493 (goto-char opoint)
1494 (forward-line 0)
1495 (1+ (count-lines start (point)))))))
1496 1491
1497(defcustom what-cursor-show-names nil 1492(defcustom what-cursor-show-names nil
1498 "Whether to show character names in `what-cursor-position'." 1493 "Whether to show character names in `what-cursor-position'."
diff --git a/src/fns.c b/src/fns.c
index bd4afa0c4e9..479a5975ce7 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5758,6 +5758,23 @@ in OBJECT. */)
5758 traverse_intervals (intervals, 0, collect_interval, collector); 5758 traverse_intervals (intervals, 0, collect_interval, collector);
5759 return CDR (collector); 5759 return CDR (collector);
5760} 5760}
5761
5762DEFUN ("line-number-at-position", Fline_number_at_position,
5763 Sline_number_at_position, 1, 1, 0,
5764 doc: /* Return the line number at POSITION.
5765If the buffer is narrowed, the position returned is the position in the
5766visible part of the buffer. */)
5767 (register Lisp_Object position)
5768{
5769 CHECK_FIXNUM (position);
5770 ptrdiff_t pos = XFIXNUM (position);
5771
5772 /* Check that POSITION is n the visible range of the buffer. */
5773 if (pos < BEGV || pos > ZV)
5774 args_out_of_range (make_int (BEGV), make_int (ZV));
5775
5776 return make_int (count_lines (BEGV_BYTE, CHAR_TO_BYTE (pos)) + 1);
5777}
5761 5778
5762 5779
5763void 5780void
@@ -5800,6 +5817,7 @@ syms_of_fns (void)
5800 defsubr (&Sdefine_hash_table_test); 5817 defsubr (&Sdefine_hash_table_test);
5801 defsubr (&Sstring_search); 5818 defsubr (&Sstring_search);
5802 defsubr (&Sobject_intervals); 5819 defsubr (&Sobject_intervals);
5820 defsubr (&Sline_number_at_position);
5803 5821
5804 /* Crypto and hashing stuff. */ 5822 /* Crypto and hashing stuff. */
5805 DEFSYM (Qiv_auto, "iv-auto"); 5823 DEFSYM (Qiv_auto, "iv-auto");
diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el
index e0aed2a71b6..3a43142106b 100644
--- a/test/src/fns-tests.el
+++ b/test/src/fns-tests.el
@@ -1098,3 +1098,11 @@
1098 (goto-char (point-max)) 1098 (goto-char (point-max))
1099 (insert "fóo") 1099 (insert "fóo")
1100 (should (approx-equal (buffer-line-statistics) '(1002 50 49.9))))) 1100 (should (approx-equal (buffer-line-statistics) '(1002 50 49.9)))))
1101
1102(ert-deftest test-line-number-at-position ()
1103 (with-temp-buffer
1104 (insert (make-string 10 ?\n))
1105 (should (= (line-number-at-position (point)) 11))
1106 (should-error (line-number-at-position nil))
1107 (should-error (line-number-at-position -1))
1108 (should-error (line-number-at-position 100))))