From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: Counting SLOC in Emacs Date: Fri, 28 Nov 2014 14:41:30 +0100 Message-ID: <8761dzjvcl.fsf@wmi.amu.edu.pl> References: <877fyfk1qn.fsf@wmi.amu.edu.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1417182165 17372 80.91.229.3 (28 Nov 2014 13:42:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 28 Nov 2014 13:42:45 +0000 (UTC) To: Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Nov 28 14:42:37 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XuLoe-00080a-RK for geh-help-gnu-emacs@m.gmane.org; Fri, 28 Nov 2014 14:42:32 +0100 Original-Received: from localhost ([::1]:44357 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XuLoe-0002xB-DP for geh-help-gnu-emacs@m.gmane.org; Fri, 28 Nov 2014 08:42:32 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:53930) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XuLnl-0002IL-00 for help-gnu-emacs@gnu.org; Fri, 28 Nov 2014 08:41:37 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XuLnj-0001M3-Te for help-gnu-emacs@gnu.org; Fri, 28 Nov 2014 08:41:36 -0500 Original-Received: from msg.wmi.amu.edu.pl ([2001:808:114:2::50]:60964) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XuLnj-0001Ls-Mz for help-gnu-emacs@gnu.org; Fri, 28 Nov 2014 08:41:35 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by msg.wmi.amu.edu.pl (Postfix) with ESMTP id C9F6A42068 for ; Fri, 28 Nov 2014 14:41:33 +0100 (CET) Original-Received: from msg.wmi.amu.edu.pl ([127.0.0.1]) by localhost (msg.wmi.amu.edu.pl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id c-Jcw4EjK8YX for ; Fri, 28 Nov 2014 14:41:33 +0100 (CET) Original-Received: from localhost (unknown [IPv6:2001:808:114:6:9d5b:51e1:6d47:a8a9]) by msg.wmi.amu.edu.pl (Postfix) with ESMTPSA id 9C82942062 for ; Fri, 28 Nov 2014 14:41:33 +0100 (CET) In-reply-to: <877fyfk1qn.fsf@wmi.amu.edu.pl> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:808:114:2::50 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:101265 Archived-At: On 2014-11-28, at 12:23, Marcin Borkowski wrote: > Hello, > > there is count-lines-region, but I'd like to exclude empty lines and > lines with only comments. Is there anything like that? (If not, I'll > try to hack it myself and share.) OK, so replyaing to myself: what do you think of this? (defun count-sloc-region (beg end) "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)) (list (point-min) (point-max)))) (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (let ((count 0)) (while (not (eobp)) (if (not (comment-only-p (line-beginning-position) (line-end-position))) (setq count (1+ count))) (forward-line)) (message "SLOC in %s: %s." (if (use-region-p) "region" "buffer") count))))) Regards, -- Marcin Borkowski http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski Faculty of Mathematics and Computer Science Adam Mickiewicz University