all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Damien Cassou <damien@cassou.me>
To: 26417@debbugs.gnu.org
Subject: bug#26417: 25.2; Add current-line in simple.el
Date: Sun, 09 Apr 2017 12:53:25 +0200	[thread overview]
Message-ID: <87fuhhzwbe.fsf@cassou.me> (raw)

[-- Attachment #1: Type: text/plain, Size: 405 bytes --]

Hi,

attached patch adds `current-line' and its tests. This new 
function returns the line number at given position ignoring 
narrowing.

I guess that `what-line' could be refactored using 
`current-line'. I can do that if you want.

Best, 

-- 
Damien Cassou
http://damiencassou.seasidehosting.st

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-current-line-in-simple.el.patch --]
[-- Type: text/x-patch, Size: 2961 bytes --]

From 73e6238bad1e06b8c7ff14c1e4bf934f6de8335a Mon Sep 17 00:00:00 2001
From: Damien Cassou <damien@cassou.me>
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            |  8 ++++++++
 test/lisp/simple-tests.el | 49 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/lisp/simple.el b/lisp/simple.el
index 48c1a9b..c7e02a6 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1283,6 +1283,14 @@ line-number-at-pos
       (forward-line 0)
       (1+ (count-lines start (point))))))
 
+(defun current-line (&optional pos)
+  "Return (widen) line number at position POS.
+If POS is nil, use current buffer location."
+  (save-excursion
+    (save-restriction
+      (widen)
+      (line-number-at-pos pos))))
+
 (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..9dc82a7 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -374,5 +374,54 @@ simple-test-undo-with-switched-buffer
        (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)))))
+
+(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) target-line)))))
+
+(ert-deftest current-line-given-pos ()
+  (let ((target-line 3)
+        pos)
+    (with-temp-buffer
+      (insert "a\nb\nc\nd\n")
+      (goto-char (point-min))
+      (forward-line (1- target-line))
+      (setq pos (point))
+      (goto-char (point-min))
+      (should (equal (current-line pos) target-line)))))
+
+(ert-deftest current-line-saves-excursion ()
+  (let (pos)
+    (with-temp-buffer
+      (insert "a\nb\nc\nd\n")
+      (goto-char (point-min))
+      (forward-line 2)
+      (setq pos (point))
+      (current-line (point-max))
+      (should (equal pos (point))))))
+
+(ert-deftest current-line-saves-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 (point-max))
+      (should (equal (line-number-at-pos) 1)))))
+
 (provide 'simple-test)
 ;;; simple-test.el ends here
-- 
2.9.3


             reply	other threads:[~2017-04-09 10:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-09 10:53 Damien Cassou [this message]
2017-04-12  8:30 ` bug#26417: 25.2; Add current-line in simple.el Nicolas Petton
2017-04-12  8:52   ` Damien Cassou
2017-04-20  9:03     ` Nicolas Petton
2017-05-03 11:11       ` Damien Cassou
2017-05-03 14:37         ` Nicolas Petton
2017-05-03 15:38           ` Damien Cassou
2017-05-03 16:07             ` Nicolas Petton
2017-05-23 10:30               ` Damien Cassou
2017-06-16 13:46                 ` Damien Cassou
2017-06-16 14:30                   ` Nicolas Petton
2017-06-19  9:19                 ` Nicolas Petton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fuhhzwbe.fsf@cassou.me \
    --to=damien@cassou.me \
    --cc=26417@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.