unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: Feature request: Emacs 25.0.94: count-lines should offer a way to ignore invisible lines, e.g. outline mode
       [not found] <CA+OMD9g0-oCrr1XXj=mH1Ma7=C8yGR8DKLdOYgtB7kKa+2aqmw@mail.gmail.com>
@ 2016-06-06 23:35 ` Robert Weiner
  2019-06-25 13:30   ` bug#23675: " Lars Ingebrigtsen
  2020-08-11 14:53   ` Lars Ingebrigtsen
  0 siblings, 2 replies; 3+ messages in thread
From: Robert Weiner @ 2016-06-06 23:35 UTC (permalink / raw)
  To: 23675; +Cc: emacs-devel


[-- 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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: bug#23675: Feature request: Emacs 25.0.94: count-lines should offer a way to ignore invisible lines, e.g. outline mode
  2016-06-06 23:35 ` Feature request: Emacs 25.0.94: count-lines should offer a way to ignore invisible lines, e.g. outline mode Robert Weiner
@ 2019-06-25 13:30   ` Lars Ingebrigtsen
  2020-08-11 14:53   ` Lars Ingebrigtsen
  1 sibling, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2019-06-25 13:30 UTC (permalink / raw)
  To: Robert Weiner; +Cc: rswgnu, 23675, emacs-devel

Some comments on the patch:

Robert Weiner <rsw@gnu.org> writes:

> ! (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

Hm...  the current version of the function doesn't mention
selective-display at all, which it probably should, anyway...

> ! (defun count-lines (start end &optional ignore-invisible-lines-flag)

I think we tend to avoid -flag in arguments these days?

> ! With optional IGNORE-INVISIBLE-LINES-FLAG non-nil,
> ! lines collapsed with selective-display are excluded
> ! from the line count."

This is very confusing, because it only mentions selective-display when
this flag is non-nil, but not what the flag does otherwise.

> !       (cond ((and (not ignore-invisible-lines-flag) (eq selective-display t))
> ! 	     (save-match-data

[...]

> ! 		  (while (re-search-forward "\n\\|\r[^\n]" nil t)

And the selective-display bit saves match data and the
non-selective-display one doesn't.

But other than that, I think this sounds like a useful addition.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#23675: Feature request: Emacs 25.0.94: count-lines should offer a way to ignore invisible lines, e.g. outline mode
  2016-06-06 23:35 ` Feature request: Emacs 25.0.94: count-lines should offer a way to ignore invisible lines, e.g. outline mode Robert Weiner
  2019-06-25 13:30   ` bug#23675: " Lars Ingebrigtsen
@ 2020-08-11 14:53   ` Lars Ingebrigtsen
  1 sibling, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2020-08-11 14:53 UTC (permalink / raw)
  To: Robert Weiner; +Cc: rswgnu, 23675, emacs-devel

Robert Weiner <rsw@gnu.org> writes:

> 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.

There were some comments about implementing this based on other
functions, but that was never followed up, so I went ahead and applied
the patch to Emacs 28.  Further improvements are left as an exercise for
the reader.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-08-11 14:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CA+OMD9g0-oCrr1XXj=mH1Ma7=C8yGR8DKLdOYgtB7kKa+2aqmw@mail.gmail.com>
2016-06-06 23:35 ` Feature request: Emacs 25.0.94: count-lines should offer a way to ignore invisible lines, e.g. outline mode Robert Weiner
2019-06-25 13:30   ` bug#23675: " Lars Ingebrigtsen
2020-08-11 14:53   ` Lars Ingebrigtsen

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).