From: Lars Ingebrigtsen Date: Sun, 7 Feb 2021 15:28:30 +0000 (+0100) Subject: Move line-number-at-pos to C X-Git-Tag: emacs-28.0.90~3886 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=56e76f0eb00d92b49ddd5757d0a68d09dc522d39;p=emacs.git 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. --- 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 in the buffer, or in the region if the region is active. @end deffn -@defun line-number-at-position pos -This function returns the line number in the current buffer -corresponding to the buffer position @var{pos}. If narrowing is in -effect, this is the line number in the visible part of the buffer. -@end defun - @defun line-number-at-pos &optional pos absolute @cindex line number -This function is like @code{line-number-at-position}, but if @var{pos} -is @code{nil} or omitted, the current buffer position is used. In -addition, if @var{absolute} is non-@code{nil}, ignore any narrowing -and return the absolute line number. +This function returns the line number in the current buffer +corresponding to the buffer position @var{pos}. If @var{pos} is +@code{nil} or omitted, the current buffer position is used. If +@var{absolute} is @code{nil}, the default, counting starts at +@code{(point-min)}, so the value refers to the contents of the +accessible portion of the (potentially narrowed) buffer. If +@var{absolute} is non-@code{nil}, ignore any narrowing and return +the absolute line number. @end defun @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', * Lisp Changes in Emacs 28.1 -+++ -** New function 'line-number-at-position'. -This returns the line number in the visible portion of the buffer. - --- ** New variable 'indent-line-ignored-functions'. This 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." (assq prop buffer-invisibility-spec))) (setq invisible-count (1+ invisible-count)))) invisible-count)))) - (t (1- (line-number-at-position (point-max)))))))) - -(defun line-number-at-pos (&optional pos absolute) - "Return buffer line number at position POS. -If POS is nil, use current buffer location. - -If ABSOLUTE is nil, the default, counting starts -at (point-min), so the value refers to the contents of the -accessible portion of the (potentially narrowed) buffer. If -ABSOLUTE is non-nil, ignore any narrowing and return the -absolute line number." - (if absolute - (save-restriction - (widen) - (line-number-at-position (or pos (point)))) - (line-number-at-position (or pos (point))))) + (t (- (buffer-size) (forward-line (buffer-size)))))))) (defcustom what-cursor-show-names nil "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. */) return CDR (collector); } -DEFUN ("line-number-at-position", Fline_number_at_position, - Sline_number_at_position, 1, 1, 0, +DEFUN ("line-number-at-pos", Fline_number_at_pos, + Sline_number_at_pos, 0, 2, 0, doc: /* Return the line number at POSITION. +If POSITION is nil, use the current buffer location. + If the buffer is narrowed, the position returned is the position in the -visible part of the buffer. */) - (register Lisp_Object position) +visible part of the buffer. If ABSOLUTE is non-nil, count the lines +from the absolute start of the buffer. */) + (register Lisp_Object position, Lisp_Object absolute) { - CHECK_FIXNUM (position); - ptrdiff_t pos = XFIXNUM (position); + ptrdiff_t pos, start = BEGV; + + if (NILP (position)) + pos = PT; + else + { + CHECK_FIXNUM (position); + pos = XFIXNUM (position); + } + + if (!NILP (absolute)) + start = BEG_BYTE; /* Check that POSITION is n the visible range of the buffer. */ if (pos < BEGV || pos > ZV) - args_out_of_range (make_int (BEGV), make_int (ZV)); + args_out_of_range (make_int (start), make_int (ZV)); - return make_int (count_lines (BEGV_BYTE, CHAR_TO_BYTE (pos)) + 1); + return make_int (count_lines (start, CHAR_TO_BYTE (pos)) + 1); } @@ -5817,7 +5830,7 @@ syms_of_fns (void) defsubr (&Sdefine_hash_table_test); defsubr (&Sstring_search); defsubr (&Sobject_intervals); - defsubr (&Sline_number_at_position); + defsubr (&Sline_number_at_pos); /* Crypto and hashing stuff. */ 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 @@ (ert-deftest test-line-number-at-position () (with-temp-buffer (insert (make-string 10 ?\n)) - (should (= (line-number-at-position (point)) 11)) - (should-error (line-number-at-position nil)) - (should-error (line-number-at-position -1)) - (should-error (line-number-at-position 100)))) + (should (= (line-number-at-pos (point)) 11)) + (should (= (line-number-at-pos nil) 11)) + (should-error (line-number-at-pos -1)) + (should-error (line-number-at-pos 100))))