all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Damien Cassou <damien@cassou.me>
To: Nicolas Petton <nicolas@petton.fr>, 26417@debbugs.gnu.org
Subject: bug#26417: 25.2; Add current-line in simple.el
Date: Wed, 03 May 2017 13:11:17 +0200	[thread overview]
Message-ID: <87o9vatctm.fsf@cassou.me> (raw)
In-Reply-To: <87fuh3e9hf.fsf@petton.fr>

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

Nicolas Petton <nicolas@petton.fr> writes:
> What about something like this: 
> 
>     (defun current-line (&optional ignore-narrowing) 
>       ...) 

thanks for your suggestion. I applied it in attached patch. I'm 
not sure I like it though because the simplest call:

    (current-line)

is now exactly the same as (line-number-at-pos). To get a 
different behavior, non-nil must be passed as argument:

    (current-line t)

Nevertheless, I understand this implementation is probably the one 
closest to existing API and usages.

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: 2830 bytes --]

From 727955fb2a063117b464309b6303cb270d91b71b 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            |  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


  reply	other threads:[~2017-05-03 11:11 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-09 10:53 bug#26417: 25.2; Add current-line in simple.el Damien Cassou
2017-04-12  8:30 ` Nicolas Petton
2017-04-12  8:52   ` Damien Cassou
2017-04-20  9:03     ` Nicolas Petton
2017-05-03 11:11       ` Damien Cassou [this message]
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=87o9vatctm.fsf@cassou.me \
    --to=damien@cassou.me \
    --cc=26417@debbugs.gnu.org \
    --cc=nicolas@petton.fr \
    /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.