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: Tue, 23 May 2017 12:30:24 +0200 Message-ID: <87r2zfrhkv.fsf@cassou.me> References: <87fuhhzwbe.fsf@cassou.me> <87a87m2fk2.fsf@petton.fr> <87wpaq57og.fsf@cassou.me> <87fuh3e9hf.fsf@petton.fr> <87o9vatctm.fsf@cassou.me> <87h912atvo.fsf@petton.fr> <877f1yt0gf.fsf@cassou.me> <87efw5c4a6.fsf@petton.fr> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1495535486 15348 195.159.176.226 (23 May 2017 10:31:26 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 23 May 2017 10:31:26 +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 Tue May 23 12:31:18 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 1dD75v-0003fS-Hk for geb-bug-gnu-emacs@m.gmane.org; Tue, 23 May 2017 12:31:16 +0200 Original-Received: from localhost ([::1]:47458 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD75x-0005j7-RN for geb-bug-gnu-emacs@m.gmane.org; Tue, 23 May 2017 06:31:17 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60579) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD75m-0005j0-8P for bug-gnu-emacs@gnu.org; Tue, 23 May 2017 06:31:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dD75j-0007qX-3Y for bug-gnu-emacs@gnu.org; Tue, 23 May 2017 06:31:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:59588) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dD75i-0007qK-It for bug-gnu-emacs@gnu.org; Tue, 23 May 2017 06:31:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1dD75i-0004I2-DI for bug-gnu-emacs@gnu.org; Tue, 23 May 2017 06:31: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: Tue, 23 May 2017 10:31: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.149553543416452 (code B ref 26417); Tue, 23 May 2017 10:31:02 +0000 Original-Received: (at 26417) by debbugs.gnu.org; 23 May 2017 10:30:34 +0000 Original-Received: from localhost ([127.0.0.1]:34032 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dD75F-0004HI-LH for submit@debbugs.gnu.org; Tue, 23 May 2017 06:30:33 -0400 Original-Received: from mail.choca.pics ([62.210.108.126]:35382) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dD75C-0004H8-N4 for 26417@debbugs.gnu.org; Tue, 23 May 2017 06:30:31 -0400 Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 3CCE1232B33A; Tue, 23 May 2017 12:30:29 +0200 (CEST) Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id lYgx8qSPA2Cz; Tue, 23 May 2017 12:30:28 +0200 (CEST) Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id F3958232B33B; Tue, 23 May 2017 12:30:27 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.choca.pics F3958232B33B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cassou.me; s=9EA44F5C-DEF4-11E6-A83F-A08DF322D8FB; t=1495535428; bh=PQ66kODTQWW5FPy1QAV6moMIXFPkI2ppz4Ipxe76NKY=; h=From:To:Date:Message-ID:MIME-Version; b=WjZAMXAcemFyXeGtX/u9tfZkO7cjlS9sAkJSrzBZ74qPtcycfQ7sZIaSmLfZezcjC FMKY36TqSR60ImVu6E7J2oP1zpFUycxpZ38czEkguJ/KbuY21hLhrJZeA4zIf0mkzc i2FBkwzWZJ4hocgCMSfsCi9xDmVlpQml2YrGL6VV7dG4RltcxRDpp/BT51OytF6cMR I2Bcc0gLQNKCDurm40WLefsQbqshfzaGzBfjx/VnNFu8MaEik6wfhh32Jh5E2VJ7xR o/maUThGR+B8ilNH5yLpFQCz3qrzxExcFvCjSi9Z9OvgJQGhD9MWPjAXy9bBm5WwQg pFDxIuEb4BUEQ== 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 1uURd90hf7-z; Tue, 23 May 2017 12:30:27 +0200 (CEST) Original-Received: from luz4 (c80-216-198-172.bredband.comhem.se [80.216.198.172]) by mail.choca.pics (Postfix) with ESMTPSA id 75925232B33A; Tue, 23 May 2017 12:30:27 +0200 (CEST) In-Reply-To: <87efw5c4a6.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:132761 Archived-At: --=-=-= Content-Type: text/plain; format=flowed Nicolas Petton 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-current-line-in-simple.el.patch >From f613a3c9c4219220e164b7c326a8064f1ec25656 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 | 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 --=-=-=--