>From 727955fb2a063117b464309b6303cb270d91b71b Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Sun, 9 Apr 2017 12:46:57 +0200 Subject: [PATCH] Add current-line in simple.el * lisp/simple.el (current-line): New function. * test/list/simple-tests.el: Add tests for current-line. --- lisp/simple.el | 9 +++++++++ test/lisp/simple-tests.el | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lisp/simple.el b/lisp/simple.el index edc822e..becbb6c 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -1283,6 +1283,15 @@ (defun line-number-at-pos (&optional pos) (forward-line 0) (1+ (count-lines start (point)))))) +(defun current-line (&optional ignore-narrowing) + "Return line number at point. +If IGNORE-NARROWING is not-nil, return the absolute line number." + (if (not ignore-narrowing) + (line-number-at-pos (point)) + (save-restriction + (widen) + (line-number-at-pos (point))))) + (defun what-cursor-position (&optional detail) "Print info on cursor position (on screen and within buffer). Also describe the character after point, and give its character code diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el index f4849c4..5b00d0c 100644 --- a/test/lisp/simple-tests.el +++ b/test/lisp/simple-tests.el @@ -374,5 +374,46 @@ (ert-deftest missing-record-point-in-undo () (undo) (point))))) +(ert-deftest current-line-in-widen-buffer () + (let ((target-line 3)) + (with-temp-buffer + (insert "a\nb\nc\nd\n") + (goto-char (point-min)) + (forward-line (1- target-line)) + (should (equal (current-line) target-line)) + (should (equal (current-line t) target-line))))) + +(ert-deftest current-line-in-narrow-buffer () + (let ((target-line 3)) + (with-temp-buffer + (insert "a\nb\nc\nd\n") + (goto-char (point-min)) + (forward-line (1- target-line)) + (narrow-to-region (line-beginning-position) (line-end-position)) + (should (equal (current-line) 1)) + (should (equal (current-line t) target-line))))) + +(ert-deftest current-line-keeps-restriction () + (let (pos) + (with-temp-buffer + (insert "a\nb\nc\nd\n") + (goto-char (point-min)) + (forward-line 2) + (narrow-to-region (line-beginning-position) (line-end-position)) + (should (equal (line-number-at-pos) 1)) + (current-line t) + (should (equal (line-number-at-pos) 1))))) + +(ert-deftest current-line-keeps-point () + (let (pos) + (with-temp-buffer + (insert "a\nb\nc\nd\n") + (goto-char (point-min)) + (forward-line 2) + (setq pos (point)) + (current-line) + (current-line t) + (should (equal pos (point)))))) + (provide 'simple-test) ;;; simple-test.el ends here -- 2.9.3