From: Stefan Monnier <monnier@iro.umontreal.ca>
To: help-gnu-emacs@gnu.org
Subject: Re: Counting SLOC in Emacs
Date: Fri, 28 Nov 2014 09:49:27 -0500 [thread overview]
Message-ID: <jwvzjbbcrps.fsf-monnier+gmane.emacs.help@gnu.org> (raw)
In-Reply-To: 874mtjjt31.fsf@wmi.amu.edu.pl
>> And don't re-call use-region-p here, in the off-chance that it returns
>> something else than in the first call. E.g. you can use (if (and (=
>> beg (point-min)) (= end (point-max))) "buffer" "region") instead.
> I don't think it's probable (or even possible), but definitely my
> solution was not very elegant. I wonder whether (let)ting
> (use-region-p) to a temporary variable wouldn't be better.
Yes, it should be passed as an additional argument (you could pass
`region' or `buffer' as argument, with nil meaning "not interactive,
just return the count without displaying a message").
> Anyway, thanks for your review! I will write a blog post about this
> function (googling for "emacs count sloc" doesn't yield anything
> useful, let's change it! ;-) ).
Of course, you'll bump into further problems:
(* foo bar
* baz *)
will count as 1 lines, IIUC. And similarly
printf ("toto\n"); (* foo bar
* baz *)
will count as 2 lines. You might want to try something like:
(defun count-sloc-region (beg end kind)
"Count source lines of code in region (or (narrowed part of)
the buffer when no region is active). SLOC means that empty
lines and comment-only lines are not taken into consideration."
(interactive
(if (use-region-p)
(list (region-beginning) (region-end) 'region)
(list (point-min) (point-max) 'buffer)))
(save-excursion
(goto-char beg
(let ((count 0))
(while (< (point) end)
(cond
((nth 4 (syntax-ppss)) ;; BOL is already inside a comment.
(let ((pos (point)))
(goto-char (nth 8 (syntax-ppss)))
(forward-comment (point-max))
(if (< (point) pos) (goto-char pos)))) ;; Just paranoia.
(t (forward-comment (point-max))))
(setq count (1+ count))
(forward-line))
(when kind
(message "SLOC in %s: %s." kind count))))))
-- Stefan
next prev parent reply other threads:[~2014-11-28 14:49 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-28 11:23 Counting SLOC in Emacs Marcin Borkowski
2014-11-28 13:41 ` Marcin Borkowski
2014-11-28 14:04 ` Stefan Monnier
2014-11-28 14:31 ` Marcin Borkowski
2014-11-28 14:49 ` Stefan Monnier [this message]
2014-11-28 16:00 ` Marcin Borkowski
2014-11-29 9:46 ` Thien-Thi Nguyen
2014-11-29 11:49 ` Marcin Borkowski
2014-11-29 14:36 ` Grant Rettke
2014-12-02 20:43 ` Robert Thorpe
2014-12-02 22:10 ` Marcin Borkowski
[not found] ` <mailman.14877.1417186246.1147.help-gnu-emacs@gnu.org>
2014-12-02 14:35 ` Ted Zlatanov
2014-12-03 1:14 ` Leo Liu
2014-12-30 14:05 ` Marcin Borkowski
2014-12-31 11:50 ` Leo Liu
2014-12-31 13:25 ` Marcin Borkowski
2014-11-28 16:46 ` Phillip Lord
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwvzjbbcrps.fsf-monnier+gmane.emacs.help@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=help-gnu-emacs@gnu.org \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.