unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Robert Weiner <rsw@gnu.org>
To: 23675@debbugs.gnu.org
Cc: emacs-devel <emacs-devel@gnu.org>
Subject: bug#23675: Feature request: Emacs 25.0.94: count-lines should offer a way to ignore invisible lines, e.g. outline mode
Date: Mon, 6 Jun 2016 19:35:47 -0400	[thread overview]
Message-ID: <CA+OMD9houRLimgDdDkC0TEg7Rk_c9qmQ9+7ebURoxy6c=vZvVw__20813.892017077$1465256272$gmane$org@mail.gmail.com> (raw)
In-Reply-To: <CA+OMD9g0-oCrr1XXj=mH1Ma7=C8yGR8DKLdOYgtB7kKa+2aqmw@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 1993 bytes --]

Attached is a patch to add this feature based on 25.0.94.  Please consider
applying it to Emacs.  I have also included the full new version of the
function below.

---------------------
(defun count-lines (start end &optional ignore-invisible-lines-flag)
  "Return number of lines between START and END.
This is usually the number of newlines between them,
but can be one more if START is not equal to END
and the greater of them is not at the start of a line.

With optional IGNORE-INVISIBLE-LINES-FLAG non-nil,
lines collapsed with selective-display are excluded
from the line count."
  (save-excursion
    (save-restriction
      (narrow-to-region start end)
      (goto-char (point-min))
      (cond ((and (not ignore-invisible-lines-flag) (eq selective-display
t))
    (save-match-data
      (let ((done 0))
(while (re-search-forward "\n\\|\r[^\n]" nil t 40)
  (setq done (+ 40 done)))
(while (re-search-forward "\n\\|\r[^\n]" nil t 1)
  (setq done (+ 1 done)))
(goto-char (point-max))
(if (and (/= start end)
 (not (bolp)))
    (1+ done)
  done))))
   (ignore-invisible-lines-flag
    (- (buffer-size) (forward-line (buffer-size))
(let ((invisible-count 0)
     prop)
 (goto-char (point-min))
 (while (re-search-forward "\n\\|\r[^\n]" nil t)
   (setq prop (get-char-property (1- (point)) 'invisible))
   (if (if (eq buffer-invisibility-spec t)
   prop
 (or (memq prop buffer-invisibility-spec)
     (assq prop buffer-invisibility-spec)))
(setq invisible-count (1+ invisible-count))))
 invisible-count)))
   (t (- (buffer-size) (forward-line (buffer-size))))))))

---------------------


On Wed, Jun 1, 2016 at 10:23 AM, Robert Weiner <rsw@gnu.org> wrote:
>
> Often when particular lines are hidden, one  wants to count only visible
lines.  count-lines presently cannot do this.  XEmacs has for a long-time
had count-lines take an optional 3rd parameter flag which if non-nil,
ignores invisible lines in the line count.  This would be very helpful
whenever selective-display is in use.
>

[-- Attachment #1.2: Type: text/html, Size: 3843 bytes --]

[-- Attachment #2: count-lines-diff.txt --]
[-- Type: text/plain, Size: 2980 bytes --]

*** simple-orig.el	2016-06-06 19:24:50.000000000 -0400
--- simple.el	2016-06-06 19:24:50.000000000 -0400
***************
*** 1214,1241 ****
  	  (message "line %d (narrowed line %d)"
  		   (+ n (line-number-at-pos start) -1) n))))))
  
! (defun count-lines (start end)
    "Return number of lines between START and END.
  This is usually the number of newlines between them,
  but can be one more if START is not equal to END
! and the greater of them is not at the start of a line."
    (save-excursion
      (save-restriction
        (narrow-to-region start end)
        (goto-char (point-min))
!       (if (eq selective-display t)
! 	  (save-match-data
! 	    (let ((done 0))
!                      (while (re-search-forward "[\n\C-m]" nil t 40)
!                        (setq done (+ 40 done)))
!                      (while (re-search-forward "[\n\C-m]" nil t 1)
!                        (setq done (+ 1 done)))
!                      (goto-char (point-max))
!                      (if (and (/= start end)
! 		       (not (bolp)))
! 		  (1+ done)
! 		done)))
! 	(- (buffer-size) (forward-line (buffer-size)))))))
  
  (defun line-number-at-pos (&optional pos)
    "Return (narrowed) buffer line number at position POS.
--- 1214,1258 ----
  	  (message "line %d (narrowed line %d)"
  		   (+ n (line-number-at-pos start) -1) n))))))
  
! (defun count-lines (start end &optional ignore-invisible-lines-flag)
    "Return number of lines between START and END.
  This is usually the number of newlines between them,
  but can be one more if START is not equal to END
! and the greater of them is not at the start of a line.
! 
! With optional IGNORE-INVISIBLE-LINES-FLAG non-nil,
! lines collapsed with selective-display are excluded
! from the line count."
    (save-excursion
      (save-restriction
        (narrow-to-region start end)
        (goto-char (point-min))
!       (cond ((and (not ignore-invisible-lines-flag) (eq selective-display t))
! 	     (save-match-data
! 	       (let ((done 0))
! 		 (while (re-search-forward "\n\\|\r[^\n]" nil t 40)
! 		   (setq done (+ 40 done)))
! 		 (while (re-search-forward "\n\\|\r[^\n]" nil t 1)
! 		   (setq done (+ 1 done)))
! 		 (goto-char (point-max))
! 		 (if (and (/= start end)
! 			  (not (bolp)))
! 		     (1+ done)
! 		   done))))
! 	    (ignore-invisible-lines-flag
! 	     (- (buffer-size) (forward-line (buffer-size))
! 		(let ((invisible-count 0)
! 		      prop)
! 		  (goto-char (point-min))
! 		  (while (re-search-forward "\n\\|\r[^\n]" nil t)
! 		    (setq prop (get-char-property (1- (point)) 'invisible))
! 		    (if (if (eq buffer-invisibility-spec t)
! 			    prop
! 			  (or (memq prop buffer-invisibility-spec)
! 			      (assq prop buffer-invisibility-spec)))
! 			(setq invisible-count (1+ invisible-count))))
! 		  invisible-count)))
! 	    (t (- (buffer-size) (forward-line (buffer-size))))))))
  
  (defun line-number-at-pos (&optional pos)
    "Return (narrowed) buffer line number at position POS.

  parent reply	other threads:[~2016-06-06 23:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-01 14:23 bug#23675: Feature request: Emacs 25.0.94: count-lines should offer a way to ignore invisible lines, e.g. outline mode Robert Weiner
2016-06-04  8:58 ` Eli Zaretskii
2016-06-06 23:35 ` Robert Weiner [this message]
     [not found] ` <CA+OMD9houRLimgDdDkC0TEg7Rk_c9qmQ9+7ebURoxy6c=vZvVw@mail.gmail.com>
2016-06-07 15:58   ` Eli Zaretskii
2016-06-07 22:17     ` Robert Weiner
2019-06-25 15:33       ` Stefan Monnier
2019-06-25 13:30   ` Lars Ingebrigtsen
     [not found]   ` <m35zotaj23.fsf@gnus.org>
2019-06-25 15:26     ` Stefan Monnier
2019-06-25 15:29       ` Lars Ingebrigtsen
2020-08-11 14:53   ` Lars Ingebrigtsen
2023-08-05  0:35 ` bug#23675: 30.0.50: make count-lines optionally ignore invisible lines J.P.
     [not found] ` <87ttteuy0g.fsf@neverwas.me>
2023-08-05  6:32   ` Eli Zaretskii
     [not found]   ` <83v8dut2x1.fsf@gnu.org>
2023-08-06 13:03     ` J.P.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+OMD9houRLimgDdDkC0TEg7Rk_c9qmQ9+7ebURoxy6c=vZvVw__20813.892017077$1465256272$gmane$org@mail.gmail.com' \
    --to=rsw@gnu.org \
    --cc=23675@debbugs.gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=rswgnu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).