Function: count-lines

count-lines is a byte-compiled function defined in simple.el.gz.

Signature

(count-lines START END &optional IGNORE-INVISIBLE-LINES)

Documentation

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.

When IGNORE-INVISIBLE-LINES is non-nil, invisible lines are not included in the count.

Probably introduced at or before Emacs version 28.1.

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun count-lines (start end &optional ignore-invisible-lines)
  "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.

When IGNORE-INVISIBLE-LINES is non-nil, invisible lines are not
included in the count."
  (save-excursion
    (save-restriction
      (narrow-to-region start end)
      (cond ((and (not ignore-invisible-lines)
                  (eq selective-display t))
             (goto-char (point-min))
	     (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
             (goto-char (point-min))
	     (save-match-data
	       (- (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
             (goto-char (point-max))
             (if (bolp)
                 (1- (line-number-at-pos))
               (line-number-at-pos)))))))