aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/simple.el29
-rw-r--r--test/lisp/simple-tests.el49
2 files changed, 11 insertions, 67 deletions
diff --git a/lisp/simple.el b/lisp/simple.el
index e3b7665bf5a..a5565ab6e73 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1270,25 +1270,18 @@ 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 absolute-p) 1273(defun line-number-at-pos (&optional pos)
1274 "Return buffer line number at position POS. 1274 "Return (narrowed) buffer line number at position POS.
1275If POS is nil, use current buffer location. 1275If POS is nil, use current buffer location.
1276 1276Counting starts at (point-min), so the value refers
1277If ABSOLUTE-P is nil, the default, counting starts 1277to the contents of the accessible portion of the buffer."
1278at (point-min), so the value refers to the contents of the 1278 (let ((opoint (or pos (point))) start)
1279accessible portion of the (potentially narrowed) buffer. If 1279 (save-excursion
1280ABSOLUTE-P is non-nil, ignore any narrowing and return the 1280 (goto-char (point-min))
1281absolute line number." 1281 (setq start (point))
1282 (save-restriction 1282 (goto-char opoint)
1283 (when absolute-p 1283 (forward-line 0)
1284 (widen)) 1284 (1+ (count-lines start (point))))))
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)))))))
1292 1285
1293(defun what-cursor-position (&optional detail) 1286(defun what-cursor-position (&optional detail)
1294 "Print info on cursor position (on screen and within buffer). 1287 "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 ad7aee1db17..180dcc0a209 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -448,54 +448,5 @@ 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
500(provide 'simple-test) 451(provide 'simple-test)
501;;; simple-test.el ends here 452;;; simple-test.el ends here