From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Damien Cassou Newsgroups: gmane.emacs.bugs Subject: bug#26417: 25.2; Add current-line in simple.el Date: Wed, 03 May 2017 13:11:17 +0200 Message-ID: <87o9vatctm.fsf@cassou.me> References: <87fuhhzwbe.fsf@cassou.me> <87a87m2fk2.fsf@petton.fr> <87wpaq57og.fsf@cassou.me> <87fuh3e9hf.fsf@petton.fr> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1493809933 18455 195.159.176.226 (3 May 2017 11:12:13 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 3 May 2017 11:12:13 +0000 (UTC) To: Nicolas Petton , 26417@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed May 03 13:12:07 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5sCU-0004f3-KJ for geb-bug-gnu-emacs@m.gmane.org; Wed, 03 May 2017 13:12:06 +0200 Original-Received: from localhost ([::1]:35829 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5sCa-000409-9T for geb-bug-gnu-emacs@m.gmane.org; Wed, 03 May 2017 07:12:12 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37466) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5sCU-000401-9m for bug-gnu-emacs@gnu.org; Wed, 03 May 2017 07:12:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d5sCR-0007u4-1t for bug-gnu-emacs@gnu.org; Wed, 03 May 2017 07:12:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:53747) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d5sCQ-0007tP-Mu for bug-gnu-emacs@gnu.org; Wed, 03 May 2017 07:12:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1d5sCQ-0000T2-AV for bug-gnu-emacs@gnu.org; Wed, 03 May 2017 07:12:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Damien Cassou Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 03 May 2017 11:12:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 26417 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 26417-submit@debbugs.gnu.org id=B26417.14938098861750 (code B ref 26417); Wed, 03 May 2017 11:12:02 +0000 Original-Received: (at 26417) by debbugs.gnu.org; 3 May 2017 11:11:26 +0000 Original-Received: from localhost ([127.0.0.1]:51946 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5sBq-0000SA-6R for submit@debbugs.gnu.org; Wed, 03 May 2017 07:11:26 -0400 Original-Received: from mail.choca.pics ([62.210.108.126]:51578) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5sBo-0000S1-39 for 26417@debbugs.gnu.org; Wed, 03 May 2017 07:11:25 -0400 Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 17AE9232904F; Wed, 3 May 2017 13:11:23 +0200 (CEST) Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id oSndCxX8uR_H; Wed, 3 May 2017 13:11:21 +0200 (CEST) Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id A600F2329086; Wed, 3 May 2017 13:11:21 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.choca.pics A600F2329086 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cassou.me; s=9EA44F5C-DEF4-11E6-A83F-A08DF322D8FB; t=1493809881; bh=YSD55OS4UtkgqQFYAQi8Ws/ts8hlqAEpqe/X/Q452oc=; h=From:To:Date:Message-ID:MIME-Version; b=CNUp2eOl/cENGFi9NVCmkRrmVEHsNZ/rqArO9MbJQgAEn9dLiVG7LcK9WJSGImlyz 2tvF977LiTLMvvlDotBa4/JMNPrwrlYsG9ORBVIFtH1ZGKNWQo4k6iRcYgqlzukBt/ HHmU4vnVpx//FZ01FCosT8tfVaY0kLxfamVKOQt3LqS9AqoqgAyWaAdCZRkjEGWoiG TS2YlST8wf3apAsbG9lbpy1OFddDfSjE0VTk2EbbYKpq87Okdvp9N9hmhUPxLau8WZ uYxBftCS1Ami/a/NDbGG4VSECTYcuw2EPgt+uMoOU6PRN4kgH6wNDTffMb6mxDwlqA n7Iz3MK63cR3A== X-Virus-Scanned: amavisd-new at choca.pics Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id aQ923-y5cxQB; Wed, 3 May 2017 13:11:21 +0200 (CEST) Original-Received: from luz4 (31-211-216-84.customers.ownit.se [31.211.216.84]) by mail.choca.pics (Postfix) with ESMTPSA id 55AD5232904F; Wed, 3 May 2017 13:11:21 +0200 (CEST) In-Reply-To: <87fuh3e9hf.fsf@petton.fr> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:132215 Archived-At: --=-=-= Content-Type: text/plain; format=flowed Nicolas Petton 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-current-line-in-simple.el.patch >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 --=-=-=--