From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Damien Cassou Newsgroups: gmane.emacs.devel Subject: Re: Emacs master commit: "Add current-line in simple.el" - Wrong commit log? Date: Mon, 03 Jul 2017 09:03:12 +0200 Message-ID: <871spyhuj3.fsf@cassou.me> References: <87wp88w3lo.fsf@petton.fr> <87d19tkfdh.fsf@cassou.me> <87h8yzt26o.fsf@petton.fr> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1499065429 26573 195.159.176.226 (3 Jul 2017 07:03:49 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 3 Jul 2017 07:03:49 +0000 (UTC) To: Nicolas Petton , Emacs developers Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jul 03 09:03:39 2017 Return-path: Envelope-to: ged-emacs-devel@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 1dRvON-00062N-Gb for ged-emacs-devel@m.gmane.org; Mon, 03 Jul 2017 09:03:32 +0200 Original-Received: from localhost ([::1]:60691 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRvOS-00041i-VX for ged-emacs-devel@m.gmane.org; Mon, 03 Jul 2017 03:03:36 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40308) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRvOL-0003tp-Mv for emacs-devel@gnu.org; Mon, 03 Jul 2017 03:03:31 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dRvOI-0002si-Ez for emacs-devel@gnu.org; Mon, 03 Jul 2017 03:03:29 -0400 Original-Received: from mail.choca.pics ([62.210.108.126]:47536) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dRvOI-0002lj-0t for emacs-devel@gnu.org; Mon, 03 Jul 2017 03:03:26 -0400 Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id A81E92328C13; Mon, 3 Jul 2017 09:03:15 +0200 (CEST) Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id k6cSdKw4ixHp; Mon, 3 Jul 2017 09:03:14 +0200 (CEST) Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 3A283232906A; Mon, 3 Jul 2017 09:03:14 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.choca.pics 3A283232906A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cassou.me; s=9EA44F5C-DEF4-11E6-A83F-A08DF322D8FB; t=1499065394; bh=SvjuUmKkO3VxtpzZHxoRY0rKYluPGoo8OUfHvpPIaZE=; h=From:To:Date:Message-ID:MIME-Version; b=JVGiJcINU7gob1r12aci9NPQGuQvSM6O8SCVAHYKNOBr+sHTdLzxXrsZu9eItWBZO kS8CwnClWApAUvtZqZVcsBNrNQ83rG73iC3/+2Wb+ZLb/2tGpVch3HOPSo83Ewk/EC vNUHSHK6G3EczpM/mVLCsXsPnKIhMNfzdDr5UlECWmboPcfvpP3VXxwJ2w0N9BwGay ldZvJHp9aMHHt5aWZvTAE6m6kfu/S5qxr1z8TSKHevSGg7vDK+8YiK8X4sJnAJZ9HP Ca9kkvu5thn9Nr+uUXsDq4jk619ts8AFyYw2GUb5+CT1j8uHTixRzNzn+uuEm8raDU t+qbLAQ+pmIJQ== 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 f5Tm3ThGpwss; Mon, 3 Jul 2017 09:03:14 +0200 (CEST) Original-Received: from luz4 (192.66.89.92.rev.sfr.net [92.89.66.192]) by mail.choca.pics (Postfix) with ESMTPSA id EB8AB2328C13; Mon, 3 Jul 2017 09:03:13 +0200 (CEST) In-Reply-To: <87h8yzt26o.fsf@petton.fr> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 62.210.108.126 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:216129 Archived-At: --=-=-= Content-Type: text/plain; format=flowed Nicolas Petton writes: > Why not simply `absolute' instead of `absolute-p'. It's not a > predicate, is it? fixed. -- 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-absolute-optional-parameter-to-line-number-at-.patch >From c9c50d91f19de77577c95e610420ee8e9622efeb Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Sun, 9 Apr 2017 12:46:57 +0200 Subject: [PATCH] Add absolute optional parameter to line-number-at-pos (Bug#26417) * lisp/simple.el (line-number-at-pos): Add a second optional argument 'absolute'. * test/list/simple-tests.el: Add tests for 'line-number-at-pos'. --- doc/lispref/positions.texi | 11 ++++++++--- etc/NEWS | 7 +++++++ lisp/simple.el | 29 ++++++++++++++++----------- test/lisp/simple-tests.el | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 14 deletions(-) diff --git a/doc/lispref/positions.texi b/doc/lispref/positions.texi index 7c30fe9..5e1e92f 100644 --- a/doc/lispref/positions.texi +++ b/doc/lispref/positions.texi @@ -432,11 +432,16 @@ Text Lines in the buffer, or in the region if the region is active. @end deffn -@defun line-number-at-pos &optional pos +@defun line-number-at-pos &optional pos absolute @cindex line number This function returns the line number in the current buffer -corresponding to the buffer position @var{pos}. If @var{pos} is @code{nil} -or omitted, the current buffer position is used. +corresponding to the buffer position @var{pos}. If @var{pos} is +@code{nil} or omitted, the current buffer position is used. If +@var{absolute} is @code{nil}, the default, counting starts at +@code{(point-min)}, so the value refers to the contents of the +accessible portion of the (potentially narrowed) buffer. If +@var{absolute} is non-@code{nil}, ignore any narrowing and return +the absolute line number. @end defun @ignore diff --git a/etc/NEWS b/etc/NEWS index 78d37484..68cc686 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1187,6 +1187,13 @@ or its files before 'delete-directory' gets to them. *** New error type 'user-search-failed' like 'search-failed' but avoids debugger like 'user-error'. ++++ +** The function 'line-number-at-pos' now takes a second optional +argument 'absolute'. If this parameter is nil, the default, this +function keeps on returning the line number taking potential narrowing +into account. If this parameter is non-nil, the function ignores +narrowing and returns the absolute line number. + ** Changes in Frame- and Window- Handling +++ diff --git a/lisp/simple.el b/lisp/simple.el index a5565ab..e3b7665 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) + "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 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 is non-nil, ignore any narrowing and return the +absolute line number." + (save-restriction + (when absolute + (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 --=-=-=--