* bug#26417: 25.2; Add current-line in simple.el
@ 2017-04-09 10:53 Damien Cassou
2017-04-12 8:30 ` Nicolas Petton
0 siblings, 1 reply; 12+ messages in thread
From: Damien Cassou @ 2017-04-09 10:53 UTC (permalink / raw)
To: 26417
[-- 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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* bug#26417: 25.2; Add current-line in simple.el
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
0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Petton @ 2017-04-12 8:30 UTC (permalink / raw)
To: Damien Cassou, 26417
[-- Attachment #1: Type: text/plain, Size: 616 bytes --]
Damien Cassou <damien@cassou.me> writes:
> Hi,
Hi Damien,
> attached patch adds `current-line' and its tests. This new
> function returns the line number at given position ignoring
> narrowing.
I know narrowing have no effect on `current-column', but does it make
sense to widen the buffer for `current-line'? (I don't have a strong
opinion, but I think it's worth asking the question).
Other than that, I think this is useful, thanks for the patch.
> I guess that `what-line' could be refactored using
> `current-line'. I can do that if you want.
Yes, that'd be good.
Cheers,
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* bug#26417: 25.2; Add current-line in simple.el
2017-04-12 8:30 ` Nicolas Petton
@ 2017-04-12 8:52 ` Damien Cassou
2017-04-20 9:03 ` Nicolas Petton
0 siblings, 1 reply; 12+ messages in thread
From: Damien Cassou @ 2017-04-12 8:52 UTC (permalink / raw)
To: Nicolas Petton, 26417
Nicolas Petton <nicolas@petton.fr> writes:
> I know narrowing have no effect on `current-column', but does it
> make sense to widen the buffer for `current-line'? (I don't have
> a strong opinion, but I think it's worth asking the question).
The existing function `line-number-at-pos` returns the line number
relative to current narrowing. I need a function that returns the
absolute line number (as I need to save the line number in a
separate file so that I can later open at the appropriate
line). Do you think the function name is misleading? What about
one of these?
- line-number-at-pos-absolute
- current-absolute-line
--
Damien Cassou
http://damiencassou.seasidehosting.st
"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill
^ permalink raw reply [flat|nested] 12+ messages in thread
* bug#26417: 25.2; Add current-line in simple.el
2017-04-12 8:52 ` Damien Cassou
@ 2017-04-20 9:03 ` Nicolas Petton
2017-05-03 11:11 ` Damien Cassou
0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Petton @ 2017-04-20 9:03 UTC (permalink / raw)
To: Damien Cassou, 26417
[-- Attachment #1: Type: text/plain, Size: 774 bytes --]
Damien Cassou <damien@cassou.me> writes:
> Nicolas Petton <nicolas@petton.fr> writes:
>> I know narrowing have no effect on `current-column', but does it
>> make sense to widen the buffer for `current-line'? (I don't have
>> a strong opinion, but I think it's worth asking the question).
>
> The existing function `line-number-at-pos` returns the line number
> relative to current narrowing. I need a function that returns the
> absolute line number (as I need to save the line number in a
> separate file so that I can later open at the appropriate
> line). Do you think the function name is misleading? What about
> one of these?
What about something like this:
(defun current-line (&optional ignore-narrowing)
...)
Cheers,
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* bug#26417: 25.2; Add current-line in simple.el
2017-04-20 9:03 ` Nicolas Petton
@ 2017-05-03 11:11 ` Damien Cassou
2017-05-03 14:37 ` Nicolas Petton
0 siblings, 1 reply; 12+ messages in thread
From: Damien Cassou @ 2017-05-03 11:11 UTC (permalink / raw)
To: Nicolas Petton, 26417
[-- 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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* bug#26417: 25.2; Add current-line in simple.el
2017-05-03 11:11 ` Damien Cassou
@ 2017-05-03 14:37 ` Nicolas Petton
2017-05-03 15:38 ` Damien Cassou
0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Petton @ 2017-05-03 14:37 UTC (permalink / raw)
To: Damien Cassou, 26417
[-- Attachment #1: Type: text/plain, Size: 477 bytes --]
Damien Cassou <damien@cassou.me> writes:
> 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:
Well, it would be the same as (line-number-at-pos (point)), yes, but I
think it's the most consistent implementation WRT `current-column'.
Cheers,
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* bug#26417: 25.2; Add current-line in simple.el
2017-05-03 14:37 ` Nicolas Petton
@ 2017-05-03 15:38 ` Damien Cassou
2017-05-03 16:07 ` Nicolas Petton
0 siblings, 1 reply; 12+ messages in thread
From: Damien Cassou @ 2017-05-03 15:38 UTC (permalink / raw)
To: Nicolas Petton, 26417
Nicolas Petton <nicolas@petton.fr> writes:
> Well, it would be the same as (line-number-at-pos (point))
which is the same as (line-number-at-pos) according to
documentation.
>, yes, but I think it's the most consistent implementation WRT
>`current-column'.
then merge it :-).
--
Damien Cassou
http://damiencassou.seasidehosting.st
"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill
^ permalink raw reply [flat|nested] 12+ messages in thread
* bug#26417: 25.2; Add current-line in simple.el
2017-05-03 15:38 ` Damien Cassou
@ 2017-05-03 16:07 ` Nicolas Petton
2017-05-23 10:30 ` Damien Cassou
0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Petton @ 2017-05-03 16:07 UTC (permalink / raw)
To: Damien Cassou, 26417
[-- Attachment #1: Type: text/plain, Size: 212 bytes --]
Damien Cassou <damien@cassou.me> writes:
> which is the same as (line-number-at-pos) according to
> documentation.
Indeed. What about adding the optional argument to `line-number-at-pos'
then?
Nico
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* bug#26417: 25.2; Add current-line in simple.el
2017-05-03 16:07 ` Nicolas Petton
@ 2017-05-23 10:30 ` Damien Cassou
2017-06-16 13:46 ` Damien Cassou
2017-06-19 9:19 ` Nicolas Petton
0 siblings, 2 replies; 12+ messages in thread
From: Damien Cassou @ 2017-05-23 10:30 UTC (permalink / raw)
To: Nicolas Petton, 26417
[-- Attachment #1: Type: text/plain, Size: 354 bytes --]
Nicolas Petton <nicolas@petton.fr> writes:
> Indeed. What about adding the optional argument to
> `line-number-at-pos' then?
ok. Here is another version following your piece of advice.
--
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: 4093 bytes --]
From f613a3c9c4219220e164b7c326a8064f1ec25656 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 | 29 +++++++++++++++++-----------
test/lisp/simple-tests.el | 49 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 67 insertions(+), 11 deletions(-)
diff --git a/lisp/simple.el b/lisp/simple.el
index ea3a495..e836018 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1270,18 +1270,25 @@ (defun count-lines (start end)
done)))
(- (buffer-size) (forward-line (buffer-size)))))))
-(defun line-number-at-pos (&optional pos)
- "Return (narrowed) buffer line number at position POS.
+(defun line-number-at-pos (&optional pos absolute-p)
+ "Return buffer line number at position POS.
If POS is nil, use current buffer location.
-Counting starts at (point-min), so the value refers
-to the contents of the accessible portion of the buffer."
- (let ((opoint (or pos (point))) start)
- (save-excursion
- (goto-char (point-min))
- (setq start (point))
- (goto-char opoint)
- (forward-line 0)
- (1+ (count-lines start (point))))))
+
+If ABSOLUTE-P is nil, the default, counting starts
+at (point-min), so the value refers to the contents of the
+accessible portion of the (potentially narrowed) buffer. If
+ABSOLUTE-P is non-nil, ignore any narrowing and return the
+absolute line number."
+ (save-restriction
+ (when absolute-p
+ (widen))
+ (let ((opoint (or pos (point))) start)
+ (save-excursion
+ (goto-char (point-min))
+ (setq start (point))
+ (goto-char opoint)
+ (forward-line 0)
+ (1+ (count-lines start (point)))))))
(defun what-cursor-position (&optional detail)
"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 180dcc0..ad7aee1 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -448,5 +448,54 @@ (ert-deftest eval-expression-print-format-large-int-echo ()
(call-interactively #'eval-expression)
(should (equal (current-message) "66 (#o102, #x42, ?B)"))))))
+(ert-deftest line-number-at-pos-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 (line-number-at-pos) target-line))
+ (should (equal (line-number-at-pos nil t) target-line)))))
+
+(ert-deftest line-number-at-pos-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 (line-number-at-pos) 1))
+ (should (equal (line-number-at-pos nil t) target-line)))))
+
+(ert-deftest line-number-at-pos-keeps-restriction ()
+ (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))
+ (line-number-at-pos nil t)
+ (should (equal (line-number-at-pos) 1))))
+
+(ert-deftest line-number-at-pos-keeps-point ()
+ (let (pos)
+ (with-temp-buffer
+ (insert "a\nb\nc\nd\n")
+ (goto-char (point-min))
+ (forward-line 2)
+ (setq pos (point))
+ (line-number-at-pos)
+ (line-number-at-pos nil t)
+ (should (equal pos (point))))))
+
+(ert-deftest line-number-at-pos-when-passing-point ()
+ (let (pos)
+ (with-temp-buffer
+ (insert "a\nb\nc\nd\n")
+ (should (equal (line-number-at-pos 1) 1))
+ (should (equal (line-number-at-pos 3) 2))
+ (should (equal (line-number-at-pos 5) 3))
+ (should (equal (line-number-at-pos 7) 4)))))
+
(provide 'simple-test)
;;; simple-test.el ends here
--
2.9.4
^ permalink raw reply related [flat|nested] 12+ messages in thread
end of thread, other threads:[~2017-06-19 9:19 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).