help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Counting SLOC in Emacs


From: Ted Zlatanov
Subject: Re: Counting SLOC in Emacs
Date: Tue, 02 Dec 2014 09:35:59 -0500
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux)

On Fri, 28 Nov 2014 09:49:27 -0500 Stefan Monnier <monnier@iro.umontreal.ca> 
wrote: 

SM>    (defun count-sloc-region (beg end kind)
SM>      "Count source lines of code in region (or (narrowed part of)
SM>    the buffer when no region is active).  SLOC means that empty
SM>    lines and comment-only lines are not taken into consideration."
SM>      (interactive
SM>       (if (use-region-p)
SM>           (list (region-beginning) (region-end) 'region)
SM>         (list (point-min) (point-max) 'buffer)))
SM>      (save-excursion
SM>        (goto-char beg
SM>        (let ((count 0))
SM>          (while (< (point) end)
SM>       (cond
SM>            ((nth 4 (syntax-ppss)) ;; BOL is already inside a comment.
SM>             (let ((pos (point)))
SM>               (goto-char (nth 8 (syntax-ppss)))
SM>               (forward-comment (point-max))
SM>               (if (< (point) pos) (goto-char pos)))) ;; Just paranoia.
SM>            (t (forward-comment (point-max))))
SM>       (setq count (1+ count))
SM>       (forward-line))
SM>         (when kind
SM>       (message "SLOC in %s: %s." kind count))))))

This is useful.

Could we have this added to `count-lines-region' with an optional
parameter, or provided by `prog-mode' specifically? It even merits a
modeline indicator that users can enable, I think.

Ted


reply via email to

[Prev in Thread] Current Thread [Next in Thread]