aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamien Cassou2017-04-09 12:46:57 +0200
committerNicolas Petton2017-07-03 14:43:02 +0200
commita7754a250b74c17e1f63194e601f20fdb911dd7c (patch)
tree965a6cdeff62984195ff7f41fe55d749e200ba3d
parent2d846eece7641cbf4ca840f26a835af266d6887e (diff)
downloademacs-a7754a250b74c17e1f63194e601f20fdb911dd7c.tar.gz
emacs-a7754a250b74c17e1f63194e601f20fdb911dd7c.zip
Add absolute optional parameter to line-number-at-pos (Bug#26417)
* lisp/simple.el (line-number-at-pos): Add a second optional argument 'absolute'. * test/list/simple-tests.el: Add tests for 'line-number-at-pos'.
-rw-r--r--doc/lispref/positions.texi11
-rw-r--r--etc/NEWS7
-rw-r--r--lisp/simple.el29
-rw-r--r--test/lisp/simple-tests.el49
4 files changed, 82 insertions, 14 deletions
diff --git a/doc/lispref/positions.texi b/doc/lispref/positions.texi
index 7c30fe977ca..9fd4bd8fe8e 100644
--- a/doc/lispref/positions.texi
+++ b/doc/lispref/positions.texi
@@ -432,11 +432,16 @@ prints a message reporting the number of lines, words, and characters
432in the buffer, or in the region if the region is active. 432in the buffer, or in the region if the region is active.
433@end deffn 433@end deffn
434 434
435@defun line-number-at-pos &optional pos 435@defun line-number-at-pos &optional pos absolute
436@cindex line number 436@cindex line number
437This function returns the line number in the current buffer 437This function returns the line number in the current buffer
438corresponding to the buffer position @var{pos}. If @var{pos} is @code{nil} 438corresponding to the buffer position @var{pos}. If @var{pos} is
439or omitted, the current buffer position is used. 439@code{nil} or omitted, the current buffer position is used. If
440@var{absolute} is @code{nil}, the default, counting starts at
441@code{(point-min)}, so the value refers to the contents of the
442accessible portion of the (potentially narrowed) buffer. If
443@var{absolute} is non-@code{nil}, ignore any narrowing and return
444the absolute line number.
440@end defun 445@end defun
441 446
442@ignore 447@ignore
diff --git a/etc/NEWS b/etc/NEWS
index 39c88c60e77..2afed2d2534 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1228,6 +1228,13 @@ or its files before 'delete-directory' gets to them.
1228*** New error type 'user-search-failed' like 'search-failed' but 1228*** New error type 'user-search-failed' like 'search-failed' but
1229avoids debugger like 'user-error'. 1229avoids debugger like 'user-error'.
1230 1230
1231+++
1232** The function 'line-number-at-pos' now takes a second optional
1233argument 'absolute'. If this parameter is nil, the default, this
1234function keeps on returning the line number taking potential narrowing
1235into account. If this parameter is non-nil, the function ignores
1236narrowing and returns the absolute line number.
1237
1231** Changes in Frame- and Window- Handling 1238** Changes in Frame- and Window- Handling
1232 1239
1233+++ 1240+++
diff --git a/lisp/simple.el b/lisp/simple.el
index a5565ab6e73..1db14a859d6 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1270,18 +1270,25 @@ and the greater of them is not at the start of a line."
1270 done))) 1270 done)))
1271 (- (buffer-size) (forward-line (buffer-size))))))) 1271 (- (buffer-size) (forward-line (buffer-size)))))))
1272 1272
1273(defun line-number-at-pos (&optional pos) 1273(defun line-number-at-pos (&optional pos absolute)
1274 "Return (narrowed) buffer line number at position POS. 1274 "Return buffer line number at position POS.
1275If POS is nil, use current buffer location. 1275If POS is nil, use current buffer location.
1276Counting starts at (point-min), so the value refers 1276
1277to the contents of the accessible portion of the buffer." 1277If ABSOLUTE is nil, the default, counting starts
1278 (let ((opoint (or pos (point))) start) 1278at (point-min), so the value refers to the contents of the
1279 (save-excursion 1279accessible portion of the (potentially narrowed) buffer. If
1280 (goto-char (point-min)) 1280ABSOLUTE is non-nil, ignore any narrowing and return the
1281 (setq start (point)) 1281absolute line number."
1282 (goto-char opoint) 1282 (save-restriction
1283 (forward-line 0) 1283 (when absolute
1284 (1+ (count-lines start (point)))))) 1284 (widen))
1285 (let ((opoint (or pos (point))) start)
1286 (save-excursion
1287 (goto-char (point-min))
1288 (setq start (point))
1289 (goto-char opoint)
1290 (forward-line 0)
1291 (1+ (count-lines start (point)))))))
1285 1292
1286(defun what-cursor-position (&optional detail) 1293(defun what-cursor-position (&optional detail)
1287 "Print info on cursor position (on screen and within buffer). 1294 "Print info on cursor position (on screen and within buffer).
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el
index 180dcc0a209..ad7aee1db17 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -448,5 +448,54 @@ See Bug#21722."
448 (call-interactively #'eval-expression) 448 (call-interactively #'eval-expression)
449 (should (equal (current-message) "66 (#o102, #x42, ?B)")))))) 449 (should (equal (current-message) "66 (#o102, #x42, ?B)"))))))
450 450
451(ert-deftest line-number-at-pos-in-widen-buffer ()
452 (let ((target-line 3))
453 (with-temp-buffer
454 (insert "a\nb\nc\nd\n")
455 (goto-char (point-min))
456 (forward-line (1- target-line))
457 (should (equal (line-number-at-pos) target-line))
458 (should (equal (line-number-at-pos nil t) target-line)))))
459
460(ert-deftest line-number-at-pos-in-narrow-buffer ()
461 (let ((target-line 3))
462 (with-temp-buffer
463 (insert "a\nb\nc\nd\n")
464 (goto-char (point-min))
465 (forward-line (1- target-line))
466 (narrow-to-region (line-beginning-position) (line-end-position))
467 (should (equal (line-number-at-pos) 1))
468 (should (equal (line-number-at-pos nil t) target-line)))))
469
470(ert-deftest line-number-at-pos-keeps-restriction ()
471 (with-temp-buffer
472 (insert "a\nb\nc\nd\n")
473 (goto-char (point-min))
474 (forward-line 2)
475 (narrow-to-region (line-beginning-position) (line-end-position))
476 (should (equal (line-number-at-pos) 1))
477 (line-number-at-pos nil t)
478 (should (equal (line-number-at-pos) 1))))
479
480(ert-deftest line-number-at-pos-keeps-point ()
481 (let (pos)
482 (with-temp-buffer
483 (insert "a\nb\nc\nd\n")
484 (goto-char (point-min))
485 (forward-line 2)
486 (setq pos (point))
487 (line-number-at-pos)
488 (line-number-at-pos nil t)
489 (should (equal pos (point))))))
490
491(ert-deftest line-number-at-pos-when-passing-point ()
492 (let (pos)
493 (with-temp-buffer
494 (insert "a\nb\nc\nd\n")
495 (should (equal (line-number-at-pos 1) 1))
496 (should (equal (line-number-at-pos 3) 2))
497 (should (equal (line-number-at-pos 5) 3))
498 (should (equal (line-number-at-pos 7) 4)))))
499
451(provide 'simple-test) 500(provide 'simple-test)
452;;; simple-test.el ends here 501;;; simple-test.el ends here