- 论坛徽章:
- 0
|
我写的,计算 NCSL 的 elisp 脚本 
http://blog.chinaunix.net/u/23408/showart.php?id=480258
- #!/home/lungangfang/local/bin/emacs --script
- ;;; ncsl.el --- counting NCSL
- ;; Created: Fang lungang 02/27/2008
- ;; Modified: Fang lungang 02/28/2008 19:25>
- ;; Copyright (C) 2008 Fang lungang
- ;; Author: Fang lungang <lgfang at users.sourceforge.net>
- ;; Keywords: files, tools
- ;; This file is NOT part of GNU Emacs.
- ;; This file is free software; you can redistribute it and/or modify
- ;; it under the terms of the GNU General Public License as published by
- ;; the Free Software Foundation; either version 2, or (at your option)
- ;; any later version.
- ;; This file is distributed in the hope that it will be useful,
- ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
- ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ;; GNU General Public License for more details.
- ;; You should have received a copy of the GNU General Public License
- ;; along with GNU Emacs; see the file COPYING. If not, write to
- ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- ;; Boston, MA 02110-1301, USA.
- ;;; Commentary:
- ;; This script is to count (and sum up) number of NCSLs (Non-Comment
- ;; Source Line). Comments and blank lines are ignored. For C/C++
- ;; code, lines between '#if 0' and '#endif' are ignored as well.
- ;; Examples:
- ;; find . -type f -name '*.[hc]' | ncsl.el
- ;; find . -type f -name '*.[hc]' | emacs --script /path/to/ncsl.el
- ;;; Code:
- (require 'hideif)
- (require 'newcomment)
- (let ((file nil) (sum 0) (ncsl 0))
- (condition-case nil
- (while (setq file (read-string "")) ; for each file
- (find-file file)
- (setq ncsl 0)
- ;; for C/c++ files, hide if 0
- (when (or (equal 'c-mode major-mode) (equal 'c++-mode major-mode))
- (goto-char (point-min))
- (while (re-search-forward "^[ \t]*#if[ \t]*0" nil t)
- (hide-ifdef-block)))
- (goto-char (point-min))
- (while (not (eobp))
- ;; suppose there won't be 10000 consecutive comments
- (forward-comment 10000)
- (setq ncsl (+ 1 ncsl))
- ;; In case a comment starts at current line and spans several
- ;; lines.
- (let* ((eol (line-end-position)) (cs (comment-search-forward eol t)))
- (while cs
- (goto-char cs)
- (forward-comment 10000)
- (setq cs (comment-search-forward eol t)))
- (unless (> (point) eol) ; unless already a new line
- (forward-visible-line 1))))
- (setq sum (+ sum ncsl))
- (kill-buffer nil)
- (princ (format "%d %s\n" ncsl file)))
- (error (princ (format "%s total\n" sum)))))
- ;;; ncsl.el ends here
复制代码
[ 本帖最后由 lgfang 于 2008-2-28 19:25 编辑 ] |
|