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: Sun, 09 Apr 2017 12:53:25 +0200 Message-ID: <87fuhhzwbe.fsf@cassou.me> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1491735260 12014 195.159.176.226 (9 Apr 2017 10:54:20 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 9 Apr 2017 10:54:20 +0000 (UTC) To: 26417@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Apr 09 12:54:11 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 1cxATy-0002BU-DB for geb-bug-gnu-emacs@m.gmane.org; Sun, 09 Apr 2017 12:54:10 +0200 Original-Received: from localhost ([::1]:58028 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxAU4-0001yI-9e for geb-bug-gnu-emacs@m.gmane.org; Sun, 09 Apr 2017 06:54:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:36653) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxATv-0001yB-1k for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2017 06:54:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cxATq-0001pL-6o for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2017 06:54:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:40771) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cxATq-0001pH-3Z for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2017 06:54:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cxATp-0007gU-QV for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2017 06:54:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Damien Cassou Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 09 Apr 2017 10:54:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 26417 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.149173523529524 (code B ref -1); Sun, 09 Apr 2017 10:54:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 9 Apr 2017 10:53:55 +0000 Original-Received: from localhost ([127.0.0.1]:38970 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cxATj-0007g8-Az for submit@debbugs.gnu.org; Sun, 09 Apr 2017 06:53:55 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:45643) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cxATg-0007fv-Mt for submit@debbugs.gnu.org; Sun, 09 Apr 2017 06:53:53 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cxATa-0001o0-EP for submit@debbugs.gnu.org; Sun, 09 Apr 2017 06:53:47 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:46440) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cxATa-0001nw-Bc for submit@debbugs.gnu.org; Sun, 09 Apr 2017 06:53:46 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:36602) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxATY-0001xi-Nq for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2017 06:53:46 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cxATT-0001mv-Oi for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2017 06:53:44 -0400 Original-Received: from mail.choca.pics ([62.210.108.126]:38700) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cxATT-0001l6-Bd for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2017 06:53:39 -0400 Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 185A62329FDE for ; Sun, 9 Apr 2017 12:53:30 +0200 (CEST) Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id 0IeFiQChpGwb for ; Sun, 9 Apr 2017 12:53:29 +0200 (CEST) Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id F1186232A304 for ; Sun, 9 Apr 2017 12:53:28 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.choca.pics F1186232A304 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cassou.me; s=9EA44F5C-DEF4-11E6-A83F-A08DF322D8FB; t=1491735209; bh=4i7F85urcGAAn6GG/kZufwU0E5WG8M2rCvHGf78LU54=; h=From:To:Date:Message-ID:MIME-Version; b=QMgkViTTP3ySL9fVp4KMBUwP+S+WDH6NYX1cwbowbXek5hPvNmxhp5anbVMkx1pW/ bZNZ8mb/dc7f4BmSZdm4kWLbniYf2SO7Kx6fTFbw6mP3sdzuCekVgcwPUvNJ3WhHEL oaG3Krbb6AealBwqSGHAH/IcuGy9Mts04by4wniFJfNAxwLNrv3WxnG29R1TuV2nmN w6yZVRoAm4wIiDeIYTOMvK+wb0gDvnFT/qZop5JLoJkWw6sBO0CmufOPuqiMw6WRt2 ymVLlPZSW3slOfF5ThIs93kEH9kSnRe/C8as4N3deV3czfUM9t3sCbLQK17R9wdzXp duujkHHme1T4g== 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 RBnp3lM0Fcsk for ; Sun, 9 Apr 2017 12:53:28 +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 917852329FDE for ; Sun, 9 Apr 2017 12:53:28 +0200 (CEST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x 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:131403 Archived-At: --=-=-= Content-Type: text/plain; format=flowed 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-current-line-in-simple.el.patch >From 73e6238bad1e06b8c7ff14c1e4bf934f6de8335a 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 | 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 --=-=-=--